Add Feishu online document conversion

This commit is contained in:
wuyang6
2026-05-11 17:18:40 +08:00
parent 64ad7aa97a
commit 2984cc44e8
7 changed files with 1045 additions and 8 deletions
@@ -0,0 +1,43 @@
import { getCurrentAccount } from "@/lib/claw-auth";
const CLAW_API_URL = process.env.CLAW_API_URL ?? "http://127.0.0.1:8765";
export async function POST(request: Request) {
const account = await getCurrentAccount();
if (!account)
return Response.json({ error: "请先登录账号" }, { status: 401 });
const body = (await request.json().catch(() => ({}))) as {
path?: unknown;
title?: unknown;
folder_token?: unknown;
};
const filePath = typeof body.path === "string" ? body.path.trim() : "";
if (!filePath) {
return Response.json({ error: "Missing file path" }, { status: 400 });
}
const response = await fetch(`${CLAW_API_URL}/api/files/online-doc`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
account_id: account.id,
path: filePath,
...(typeof body.title === "string" && body.title.trim()
? { title: body.title.trim() }
: {}),
...(typeof body.folder_token === "string" && body.folder_token.trim()
? { folder_token: body.folder_token.trim() }
: {}),
}),
cache: "no-store",
});
const payload = await response.text();
return new Response(payload, {
status: response.status,
headers: {
"content-type":
response.headers.get("content-type") ?? "application/json",
},
});
}
+53 -4
View File
@@ -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;
@@ -0,0 +1,48 @@
import { getCurrentAccount } from "@/lib/claw-auth";
const CLAW_API_URL = process.env.CLAW_API_URL ?? "http://127.0.0.1:8765";
export async function GET() {
const account = await getCurrentAccount();
if (!account)
return Response.json({ error: "请先登录账号" }, { status: 401 });
const url = new URL(`${CLAW_API_URL}/api/integrations/feishu/status`);
url.searchParams.set("account_id", account.id);
const response = await fetch(url, { cache: "no-store" });
return proxyResponse(response);
}
export async function POST(request: Request) {
const account = await getCurrentAccount();
if (!account)
return Response.json({ error: "请先登录账号" }, { status: 401 });
const body = (await request.json().catch(() => ({}))) as {
action?: unknown;
};
const action = typeof body.action === "string" ? body.action : "login";
const endpoint =
action === "logout"
? "/api/integrations/feishu/logout"
: "/api/integrations/feishu/login";
const response = await fetch(`${CLAW_API_URL}${endpoint}`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ account_id: account.id }),
cache: "no-store",
});
return proxyResponse(response);
}
async function proxyResponse(response: Response) {
const payload = await response.text();
return new Response(payload, {
status: response.status,
headers: {
"content-type":
response.headers.get("content-type") ?? "application/json",
},
});
}