Add Feishu online document conversion
This commit is contained in:
@@ -17,11 +17,17 @@ export async function GET(req: Request) {
|
||||
const url = new URL(req.url);
|
||||
const requestedPath = url.searchParams.get("path");
|
||||
const sessionId = normalizeSessionId(url.searchParams.get("session_id"));
|
||||
const onlineDocs = await readOnlineDocMap(account.id);
|
||||
if (!requestedPath && sessionId) {
|
||||
return Response.json({
|
||||
session_id: sessionId,
|
||||
input: await listSessionFiles(account.id, sessionId, "input"),
|
||||
output: await listSessionFiles(account.id, sessionId, "output"),
|
||||
input: await listSessionFiles(account.id, sessionId, "input", onlineDocs),
|
||||
output: await listSessionFiles(
|
||||
account.id,
|
||||
sessionId,
|
||||
"output",
|
||||
onlineDocs,
|
||||
),
|
||||
});
|
||||
}
|
||||
if (!requestedPath) {
|
||||
@@ -31,8 +37,18 @@ export async function GET(req: Request) {
|
||||
}
|
||||
return Response.json({
|
||||
session_id: latestSessionId,
|
||||
input: await listSessionFiles(account.id, latestSessionId, "input"),
|
||||
output: await listSessionFiles(account.id, latestSessionId, "output"),
|
||||
input: await listSessionFiles(
|
||||
account.id,
|
||||
latestSessionId,
|
||||
"input",
|
||||
onlineDocs,
|
||||
),
|
||||
output: await listSessionFiles(
|
||||
account.id,
|
||||
latestSessionId,
|
||||
"output",
|
||||
onlineDocs,
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -63,6 +79,7 @@ async function listSessionFiles(
|
||||
accountId: string,
|
||||
sessionId: string,
|
||||
kind: "input" | "output",
|
||||
onlineDocs: Record<string, OnlineDocRecord>,
|
||||
) {
|
||||
const accountRoot = path.resolve(accountBaseRoot(accountId));
|
||||
const dir =
|
||||
@@ -80,6 +97,7 @@ async function listSessionFiles(
|
||||
.map(async (entry) => {
|
||||
const filePath = path.join(resolvedDir, entry.name);
|
||||
const fileStat = await stat(filePath);
|
||||
const onlineDoc = onlineDocs[path.resolve(filePath)];
|
||||
return {
|
||||
name: entry.name,
|
||||
path: filePath,
|
||||
@@ -87,6 +105,9 @@ async function listSessionFiles(
|
||||
size: fileStat.size,
|
||||
modified_at: fileStat.mtime.toISOString(),
|
||||
download_url: `/api/claw/files?path=${encodeURIComponent(filePath)}`,
|
||||
online_doc_url: cleanOnlineDocUrl(onlineDoc?.url) ?? null,
|
||||
online_doc_title: onlineDoc?.title ?? null,
|
||||
online_doc_updated_at: onlineDoc?.updated_at ?? null,
|
||||
};
|
||||
}),
|
||||
);
|
||||
@@ -96,6 +117,34 @@ async function listSessionFiles(
|
||||
}
|
||||
}
|
||||
|
||||
function cleanOnlineDocUrl(value: unknown) {
|
||||
if (typeof value !== "string") return null;
|
||||
return value.trim().replace(/[.,;,。;、"'“”‘’]+$/u, "") || null;
|
||||
}
|
||||
|
||||
type OnlineDocRecord = {
|
||||
url?: string;
|
||||
title?: string;
|
||||
updated_at?: number;
|
||||
};
|
||||
|
||||
async function readOnlineDocMap(accountId: string) {
|
||||
const mapPath = path.join(
|
||||
accountBaseRoot(accountId),
|
||||
"integrations",
|
||||
"feishu",
|
||||
"online-docs.json",
|
||||
);
|
||||
try {
|
||||
const payload = JSON.parse(await readFile(mapPath, "utf8")) as {
|
||||
files?: Record<string, OnlineDocRecord>;
|
||||
};
|
||||
return payload.files ?? {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeSessionId(value: string | null) {
|
||||
const trimmed = value?.trim();
|
||||
return trimmed || null;
|
||||
|
||||
Reference in New Issue
Block a user