0.1.13 AI功能大改
This commit is contained in:
@@ -0,0 +1,258 @@
|
||||
import type { Json } from "@/types/supabase";
|
||||
|
||||
export type DocToolContext = {
|
||||
documentId: string;
|
||||
userId: string;
|
||||
/**
|
||||
* 来自前端的“最新文档快照”(优先使用,避免覆盖用户尚未落盘的编辑)。
|
||||
* 允许是 blocks 数组,或 { blocks } 结构。
|
||||
*/
|
||||
baseBlocks?: unknown;
|
||||
};
|
||||
|
||||
type SupabaseRouteClient = {
|
||||
from: (table: string) => {
|
||||
select: (columns: string) => SupabaseQuery;
|
||||
update: (values: Record<string, unknown>) => SupabaseUpdateQuery;
|
||||
};
|
||||
};
|
||||
|
||||
type SupabaseQuery = {
|
||||
eq: (column: string, value: unknown) => SupabaseQuery;
|
||||
single: () => Promise<{ data: unknown; error: unknown }>;
|
||||
};
|
||||
|
||||
type SupabaseUpdateQuery = {
|
||||
eq: (column: string, value: unknown) => SupabaseUpdateQuery;
|
||||
};
|
||||
|
||||
export type DocSupabaseClient = SupabaseRouteClient;
|
||||
|
||||
type DocBlockSummary = {
|
||||
id: string;
|
||||
type: string;
|
||||
text: string;
|
||||
depth: number;
|
||||
childCount: number;
|
||||
};
|
||||
|
||||
type DocBlockSpec = {
|
||||
type: "paragraph" | "heading";
|
||||
text: string;
|
||||
level?: number;
|
||||
};
|
||||
|
||||
const isRecord = (v: unknown): v is Record<string, unknown> =>
|
||||
typeof v === "object" && v !== null && !Array.isArray(v);
|
||||
|
||||
const getValue = (obj: unknown, key: string): unknown => (isRecord(obj) ? obj[key] : undefined);
|
||||
|
||||
const normalizeBlocks = (content: unknown): unknown[] => {
|
||||
if (Array.isArray(content)) return content;
|
||||
const blocks = getValue(content, "blocks");
|
||||
if (Array.isArray(blocks)) return blocks;
|
||||
return [];
|
||||
};
|
||||
|
||||
const extractInlineText = (block: unknown): string => {
|
||||
const content = getValue(block, "content");
|
||||
const nodes = Array.isArray(content) ? content : [];
|
||||
const pieces: string[] = [];
|
||||
for (const n of nodes) {
|
||||
const t = getValue(n, "text");
|
||||
if (typeof t === "string") pieces.push(t);
|
||||
}
|
||||
return pieces.join("").trim();
|
||||
};
|
||||
|
||||
const walkSummaries = (rootBlocks: unknown[], maxNodes: number): DocBlockSummary[] => {
|
||||
const list: DocBlockSummary[] = [];
|
||||
const queue: Array<{ block: unknown; depth: number }> = rootBlocks.map((b) => ({ block: b, depth: 0 }));
|
||||
while (queue.length > 0 && list.length < maxNodes) {
|
||||
const item = queue.shift();
|
||||
if (!item) break;
|
||||
const { block, depth } = item;
|
||||
const id = String(getValue(block, "id") ?? "").trim();
|
||||
const type = String(getValue(block, "type") ?? "").trim();
|
||||
const childrenRaw = getValue(block, "children");
|
||||
const children = Array.isArray(childrenRaw) ? childrenRaw : [];
|
||||
const text = extractInlineText(block);
|
||||
if (id) {
|
||||
list.push({ id, type: type || "unknown", text, depth, childCount: children.length });
|
||||
}
|
||||
children.forEach((c) => queue.push({ block: c, depth: depth + 1 }));
|
||||
}
|
||||
return list;
|
||||
};
|
||||
|
||||
const findContainerById = (
|
||||
blocks: unknown[],
|
||||
targetId: string,
|
||||
): { container: unknown[]; index: number } | null => {
|
||||
const id = String(targetId || "").trim();
|
||||
if (!id) return null;
|
||||
for (let i = 0; i < blocks.length; i += 1) {
|
||||
const b = blocks[i];
|
||||
const bid = String(getValue(b, "id") ?? "").trim();
|
||||
if (bid === id) return { container: blocks, index: i };
|
||||
const childrenRaw = getValue(b, "children");
|
||||
const children = Array.isArray(childrenRaw) ? childrenRaw : [];
|
||||
const found = findContainerById(children, id);
|
||||
if (found) return found;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const createTextContent = (text: string): unknown[] => [{ type: "text", text }];
|
||||
|
||||
const generateId = () => {
|
||||
if (typeof crypto !== "undefined" && "randomUUID" in crypto) {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
return `bn_${Math.random().toString(36).slice(2, 10)}`;
|
||||
};
|
||||
|
||||
const buildBlockFromSpec = (spec: DocBlockSpec): Record<string, unknown> => {
|
||||
const type = spec.type;
|
||||
const text = String(spec.text ?? "").trim();
|
||||
const base: Record<string, unknown> = {
|
||||
id: generateId(),
|
||||
type,
|
||||
props: {},
|
||||
content: createTextContent(text),
|
||||
children: [],
|
||||
};
|
||||
if (type === "heading") {
|
||||
const level = Number(spec.level ?? 2);
|
||||
base.props = { level: Math.max(1, Math.min(5, Number.isFinite(level) ? Math.floor(level) : 2)) };
|
||||
}
|
||||
return base;
|
||||
};
|
||||
|
||||
const loadDocumentBlocks = async (supabase: DocSupabaseClient, ctx: DocToolContext) => {
|
||||
const base = normalizeBlocks(ctx.baseBlocks);
|
||||
if (base.length > 0) {
|
||||
return { blocks: base, source: "client" as const };
|
||||
}
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from("documents")
|
||||
.select("id,content")
|
||||
.eq("id", ctx.documentId)
|
||||
.eq("user_id", ctx.userId)
|
||||
.single();
|
||||
if (error) {
|
||||
throw new Error(isRecord(error) && typeof error.message === "string" ? error.message : "读取文档失败");
|
||||
}
|
||||
const content = isRecord(data) ? data.content : null;
|
||||
const blocks = normalizeBlocks(content);
|
||||
return { blocks, source: "db" as const };
|
||||
};
|
||||
|
||||
const saveDocumentBlocks = async (supabase: DocSupabaseClient, ctx: DocToolContext, blocks: unknown[]) => {
|
||||
const resp = (await (supabase
|
||||
.from("documents")
|
||||
.update({ content: blocks as unknown as Json })
|
||||
.eq("id", ctx.documentId)
|
||||
.eq("user_id", ctx.userId) as unknown as Promise<{ error: unknown }>)) ?? { error: null };
|
||||
const error = resp.error;
|
||||
if (error) {
|
||||
throw new Error(isRecord(error) && typeof error.message === "string" ? error.message : "保存文档失败");
|
||||
}
|
||||
};
|
||||
|
||||
export const createDocServerTools = (args: {
|
||||
supabase: DocSupabaseClient;
|
||||
ctx: DocToolContext;
|
||||
allowedToolIds: Set<string>;
|
||||
}) => {
|
||||
const run = async (toolId: string, toolArgs: Record<string, unknown>) => {
|
||||
if (!args.allowedToolIds.has(toolId)) {
|
||||
throw new Error(`工具未被允许:${toolId}`);
|
||||
}
|
||||
|
||||
if (toolId === "doc_get") {
|
||||
const maxNodesRaw = Number(toolArgs.maxBlocks ?? 80);
|
||||
const maxBlocks = Math.max(10, Math.min(240, Number.isFinite(maxNodesRaw) ? Math.floor(maxNodesRaw) : 80));
|
||||
const { blocks, source } = await loadDocumentBlocks(args.supabase, args.ctx);
|
||||
const summary = walkSummaries(blocks, maxBlocks);
|
||||
return { ok: true, source, totalTopLevelBlocks: blocks.length, blocks: summary };
|
||||
}
|
||||
|
||||
if (toolId === "doc_find") {
|
||||
const query = String(toolArgs.query ?? "").trim();
|
||||
if (!query) throw new Error("缺少 query");
|
||||
const maxRaw = Number(toolArgs.maxResults ?? 8);
|
||||
const maxResults = Math.max(1, Math.min(30, Number.isFinite(maxRaw) ? Math.floor(maxRaw) : 8));
|
||||
const { blocks, source } = await loadDocumentBlocks(args.supabase, args.ctx);
|
||||
const summary = walkSummaries(blocks, 400);
|
||||
const q = query.toLowerCase();
|
||||
const hits = summary.filter((x) => x.text.toLowerCase().includes(q)).slice(0, maxResults);
|
||||
return { ok: true, source, query, results: hits };
|
||||
}
|
||||
|
||||
if (toolId === "doc_insert_blocks") {
|
||||
const afterBlockId = String(toolArgs.afterBlockId ?? "").trim();
|
||||
const beforeBlockId = String(toolArgs.beforeBlockId ?? "").trim();
|
||||
const specsRaw = toolArgs.blocks;
|
||||
if (!Array.isArray(specsRaw) || specsRaw.length === 0) throw new Error("缺少 blocks");
|
||||
if (specsRaw.length > 20) throw new Error("blocks 过多(最多 20)");
|
||||
|
||||
const specs: DocBlockSpec[] = specsRaw.map((x) => {
|
||||
const t = isRecord(x) ? String(x.type ?? "paragraph") : "paragraph";
|
||||
const text = isRecord(x) ? String(x.text ?? "") : "";
|
||||
const level = isRecord(x) ? Number(x.level ?? 2) : 2;
|
||||
return { type: t === "heading" ? "heading" : "paragraph", text, level };
|
||||
});
|
||||
|
||||
const created = specs.map(buildBlockFromSpec);
|
||||
|
||||
const { blocks, source } = await loadDocumentBlocks(args.supabase, args.ctx);
|
||||
const targetId = beforeBlockId || afterBlockId;
|
||||
const found = targetId ? findContainerById(blocks, targetId) : null;
|
||||
if (targetId && !found) {
|
||||
throw new Error(`未找到 blockId:${targetId}`);
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
blocks.push(...created);
|
||||
} else {
|
||||
const insertAt = beforeBlockId ? found.index : found.index + 1;
|
||||
found.container.splice(insertAt, 0, ...created);
|
||||
}
|
||||
|
||||
await saveDocumentBlocks(args.supabase, args.ctx, blocks);
|
||||
return {
|
||||
ok: true,
|
||||
source,
|
||||
inserted: created.map((b) => String(b.id ?? "")),
|
||||
data: blocks,
|
||||
};
|
||||
}
|
||||
|
||||
if (toolId === "doc_replace_range") {
|
||||
const blockId = String(toolArgs.blockId ?? "").trim();
|
||||
const text = String(toolArgs.text ?? "").trim();
|
||||
if (!blockId) throw new Error("缺少 blockId");
|
||||
if (!text) throw new Error("缺少 text");
|
||||
const modeRaw = String(toolArgs.mode ?? "replace").trim();
|
||||
const mode = modeRaw === "append" || modeRaw === "prepend" ? modeRaw : "replace";
|
||||
|
||||
const { blocks, source } = await loadDocumentBlocks(args.supabase, args.ctx);
|
||||
const found = findContainerById(blocks, blockId);
|
||||
if (!found) throw new Error(`未找到 blockId:${blockId}`);
|
||||
const block = found.container[found.index];
|
||||
if (!isRecord(block)) throw new Error(`block 数据异常:${blockId}`);
|
||||
const prevText = extractInlineText(block);
|
||||
const nextText = mode === "append" ? `${prevText}${text}` : mode === "prepend" ? `${text}${prevText}` : text;
|
||||
found.container[found.index] = { ...block, content: createTextContent(nextText) };
|
||||
|
||||
await saveDocumentBlocks(args.supabase, args.ctx, blocks);
|
||||
return { ok: true, source, blockId, mode, data: blocks };
|
||||
}
|
||||
|
||||
throw new Error(`未知工具:${toolId}`);
|
||||
};
|
||||
|
||||
return { run };
|
||||
};
|
||||
@@ -0,0 +1,144 @@
|
||||
type SupabaseRouteClient = {
|
||||
from: (table: string) => any;
|
||||
};
|
||||
|
||||
export type DocsSupabaseClient = SupabaseRouteClient;
|
||||
|
||||
export type DocsToolContext = {
|
||||
userId: string;
|
||||
};
|
||||
|
||||
const isRecord = (v: unknown): v is Record<string, unknown> =>
|
||||
typeof v === "object" && v !== null && !Array.isArray(v);
|
||||
|
||||
const escapeIlike = (value: string) =>
|
||||
// 注意:PostgREST 的 or(...) 语法里逗号/括号有特殊含义,这里一并转义,避免解析失败
|
||||
value.replace(/[%_,()]/g, (m) => `\\${m}`);
|
||||
|
||||
const snippetAround = (text: string, q: string, maxLen: number) => {
|
||||
const s = String(text || "");
|
||||
const query = String(q || "").trim();
|
||||
if (!s) return "";
|
||||
const limit = Math.max(80, Math.min(2000, Math.floor(maxLen || 0) || 320));
|
||||
if (!query) return s.slice(0, limit);
|
||||
const lower = s.toLowerCase();
|
||||
const ql = query.toLowerCase();
|
||||
const idx = lower.indexOf(ql);
|
||||
if (idx === -1) return s.slice(0, limit);
|
||||
const start = Math.max(0, idx - Math.floor(limit / 3));
|
||||
const end = Math.min(s.length, start + limit);
|
||||
const head = start > 0 ? "…" : "";
|
||||
const tail = end < s.length ? "…" : "";
|
||||
return `${head}${s.slice(start, end)}${tail}`.trim();
|
||||
};
|
||||
|
||||
const loadWorkspaceIds = async (supabase: DocsSupabaseClient, userId: string) => {
|
||||
const { data, error } = await supabase
|
||||
.from("workspace_members")
|
||||
.select("workspace_id")
|
||||
.eq("user_id", userId);
|
||||
if (error) {
|
||||
throw new Error(isRecord(error) && typeof error.message === "string" ? error.message : "读取工作区失败");
|
||||
}
|
||||
const rows = Array.isArray(data) ? data : [];
|
||||
return rows.map((r) => String((r as any)?.workspace_id ?? "")).filter(Boolean);
|
||||
};
|
||||
|
||||
export const createDocsServerTools = (args: {
|
||||
supabase: DocsSupabaseClient;
|
||||
ctx: DocsToolContext;
|
||||
allowedToolIds: Set<string>;
|
||||
}) => {
|
||||
const run = async (toolId: string, toolArgs: Record<string, unknown>) => {
|
||||
if (!args.allowedToolIds.has(toolId)) {
|
||||
throw new Error(`工具未被允许:${toolId}`);
|
||||
}
|
||||
|
||||
if (toolId === "docs_search") {
|
||||
const query = String(toolArgs.query ?? "").trim();
|
||||
if (!query) throw new Error("缺少 query");
|
||||
const limitRaw = Number(toolArgs.limit ?? 12);
|
||||
const limit = Math.max(1, Math.min(30, Number.isFinite(limitRaw) ? Math.floor(limitRaw) : 12));
|
||||
const workspaceId = String(toolArgs.workspaceId ?? "").trim() || null;
|
||||
const includeDeleted = Boolean(toolArgs.includeDeleted ?? false);
|
||||
|
||||
const wsIds = await loadWorkspaceIds(args.supabase, args.ctx.userId);
|
||||
const wsFilter = workspaceId ? [workspaceId] : wsIds;
|
||||
if (wsFilter.length === 0) return { ok: true, query, results: [] };
|
||||
|
||||
const pattern = `%${escapeIlike(query)}%`;
|
||||
const q = args.supabase
|
||||
.from("documents")
|
||||
.select("id,title,workspace_id,parent_id,updated_at,raw_text,deleted_at")
|
||||
.in("workspace_id", wsFilter)
|
||||
.or(`title.ilike.${pattern},raw_text.ilike.${pattern}`)
|
||||
.order("updated_at", { ascending: false })
|
||||
.limit(limit);
|
||||
if (!includeDeleted) q.is("deleted_at", null);
|
||||
|
||||
const { data, error } = await q;
|
||||
if (error) {
|
||||
throw new Error(isRecord(error) && typeof error.message === "string" ? error.message : "搜索文档失败");
|
||||
}
|
||||
const rows = Array.isArray(data) ? data : [];
|
||||
const results = rows.map((r) => {
|
||||
const id = String((r as any)?.id ?? "");
|
||||
const title = String((r as any)?.title ?? "");
|
||||
const raw = String((r as any)?.raw_text ?? "");
|
||||
const updatedAt = (r as any)?.updated_at ?? null;
|
||||
const wid = String((r as any)?.workspace_id ?? "");
|
||||
const pid = (r as any)?.parent_id ? String((r as any)?.parent_id) : null;
|
||||
return {
|
||||
id,
|
||||
title,
|
||||
workspaceId: wid,
|
||||
parentId: pid,
|
||||
updatedAt,
|
||||
snippet: snippetAround(raw, query, 360),
|
||||
};
|
||||
});
|
||||
|
||||
return { ok: true, query, results };
|
||||
}
|
||||
|
||||
if (toolId === "docs_read") {
|
||||
const documentId = String(toolArgs.documentId ?? "").trim();
|
||||
if (!documentId) throw new Error("缺少 documentId");
|
||||
const maxCharsRaw = Number(toolArgs.maxChars ?? 2500);
|
||||
const maxChars = Math.max(200, Math.min(20_000, Number.isFinite(maxCharsRaw) ? Math.floor(maxCharsRaw) : 2500));
|
||||
const includeContent = Boolean(toolArgs.includeContent ?? false);
|
||||
|
||||
const { data, error } = await args.supabase
|
||||
.from("documents")
|
||||
.select(includeContent ? "id,title,raw_text,content,workspace_id,parent_id,updated_at" : "id,title,raw_text,workspace_id,parent_id,updated_at")
|
||||
.eq("id", documentId)
|
||||
.single();
|
||||
if (error) {
|
||||
throw new Error(isRecord(error) && typeof error.message === "string" ? error.message : "读取文档失败");
|
||||
}
|
||||
const title = isRecord(data) ? String(data.title ?? "") : "";
|
||||
const rawText = isRecord(data) ? String(data.raw_text ?? "") : "";
|
||||
const trimmed = rawText.length > maxChars ? `${rawText.slice(0, maxChars)}…` : rawText;
|
||||
const workspaceId = isRecord(data) ? String(data.workspace_id ?? "") : "";
|
||||
const parentId = isRecord(data) && data.parent_id ? String(data.parent_id) : null;
|
||||
const updatedAt = isRecord(data) ? (data as any).updated_at ?? null : null;
|
||||
const content = includeContent && isRecord(data) ? (data as any).content ?? null : null;
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
documentId,
|
||||
title,
|
||||
workspaceId,
|
||||
parentId,
|
||||
updatedAt,
|
||||
rawTextLength: rawText.length,
|
||||
rawText: trimmed,
|
||||
...(includeContent ? { content } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(`未知工具:${toolId}`);
|
||||
};
|
||||
|
||||
return { run };
|
||||
};
|
||||
@@ -0,0 +1,112 @@
|
||||
type SupabaseRouteClient = {
|
||||
from: (table: string) => any;
|
||||
};
|
||||
|
||||
export type MediaSupabaseClient = SupabaseRouteClient;
|
||||
|
||||
export type MediaToolContext = {
|
||||
userId: string;
|
||||
attachments?: Array<{ id: string; title: string; fileUrl: string; mimeType?: string | null }>;
|
||||
};
|
||||
|
||||
const isRecord = (v: unknown): v is Record<string, unknown> =>
|
||||
typeof v === "object" && v !== null && !Array.isArray(v);
|
||||
|
||||
type ResolvedAttachment = { id: string; title: string; fileUrl: string; mimeType?: string | null };
|
||||
|
||||
const resolveAttachment = (ctx: MediaToolContext, ref: string): ResolvedAttachment | null => {
|
||||
const s = String(ref || "").trim();
|
||||
if (!s) return null;
|
||||
const list = Array.isArray(ctx.attachments) ? ctx.attachments : [];
|
||||
const byId = list.find((a) => String(a.id) === s);
|
||||
if (byId) return { id: String(byId.id), title: String(byId.title || byId.id), fileUrl: String(byId.fileUrl || ""), mimeType: byId.mimeType ?? null };
|
||||
const exactTitle = list.find((a) => String(a.title) === s);
|
||||
if (exactTitle) return { id: String(exactTitle.id), title: String(exactTitle.title || exactTitle.id), fileUrl: String(exactTitle.fileUrl || ""), mimeType: exactTitle.mimeType ?? null };
|
||||
const fuzzy = list.find((a) => String(a.title || "").includes(s) || String(a.fileUrl || "").includes(s));
|
||||
if (fuzzy) return { id: String(fuzzy.id), title: String(fuzzy.title || fuzzy.id), fileUrl: String(fuzzy.fileUrl || ""), mimeType: fuzzy.mimeType ?? null };
|
||||
return null;
|
||||
};
|
||||
|
||||
const pick = (obj: unknown, key: string) => (isRecord(obj) ? obj[key] : undefined);
|
||||
|
||||
export const createMediaServerTools = (args: {
|
||||
supabase: MediaSupabaseClient;
|
||||
ctx: MediaToolContext;
|
||||
allowedToolIds: Set<string>;
|
||||
}) => {
|
||||
const run = async (toolId: string, toolArgs: Record<string, unknown>) => {
|
||||
if (!args.allowedToolIds.has(toolId)) throw new Error(`工具未被允许:${toolId}`);
|
||||
|
||||
if (toolId === "image_read") {
|
||||
const assetId = String(toolArgs.assetId ?? "").trim();
|
||||
const fileUrl = String(toolArgs.fileUrl ?? "").trim();
|
||||
const attachmentRef = String(toolArgs.attachmentRef ?? "").trim();
|
||||
|
||||
const resolved = attachmentRef ? resolveAttachment(args.ctx, attachmentRef) : null;
|
||||
const targetAssetId = assetId || resolved?.id || "";
|
||||
const targetUrl = fileUrl || resolved?.fileUrl || "";
|
||||
|
||||
let row: unknown = null;
|
||||
if (targetAssetId) {
|
||||
const { data, error } = await args.supabase
|
||||
.from("media_assets")
|
||||
.select("id,file_name,file_url,mime_type,ocr_text,ocr_status,ocr_payload,storage_path,bucket,document_id,workspace_id,deleted_at,purged_at,updated_at")
|
||||
.eq("id", targetAssetId)
|
||||
.single();
|
||||
if (error) {
|
||||
throw new Error(isRecord(error) && typeof error.message === "string" ? error.message : "读取图片失败");
|
||||
}
|
||||
row = data;
|
||||
} else if (targetUrl) {
|
||||
const { data, error } = await args.supabase
|
||||
.from("media_assets")
|
||||
.select("id,file_name,file_url,mime_type,ocr_text,ocr_status,ocr_payload,storage_path,bucket,document_id,workspace_id,deleted_at,purged_at,updated_at")
|
||||
.eq("file_url", targetUrl)
|
||||
.order("updated_at", { ascending: false })
|
||||
.limit(1)
|
||||
.maybeSingle();
|
||||
if (error) {
|
||||
throw new Error(isRecord(error) && typeof error.message === "string" ? error.message : "读取图片失败");
|
||||
}
|
||||
row = data;
|
||||
} else {
|
||||
throw new Error("缺少 assetId / fileUrl / attachmentRef");
|
||||
}
|
||||
|
||||
if (!row) return { ok: true, found: false };
|
||||
|
||||
const ocrText = String(pick(row, "ocr_text") ?? "");
|
||||
const ocrStatus = String(pick(row, "ocr_status") ?? "");
|
||||
const mimeType = String(pick(row, "mime_type") ?? "");
|
||||
const result = {
|
||||
ok: true,
|
||||
found: true,
|
||||
asset: {
|
||||
id: String(pick(row, "id") ?? ""),
|
||||
fileName: String(pick(row, "file_name") ?? ""),
|
||||
fileUrl: String(pick(row, "file_url") ?? ""),
|
||||
mimeType,
|
||||
storagePath: pick(row, "storage_path") ? String(pick(row, "storage_path")) : null,
|
||||
bucket: pick(row, "bucket") ? String(pick(row, "bucket")) : null,
|
||||
documentId: pick(row, "document_id") ? String(pick(row, "document_id")) : null,
|
||||
workspaceId: pick(row, "workspace_id") ? String(pick(row, "workspace_id")) : null,
|
||||
deletedAt: pick(row, "deleted_at") ?? null,
|
||||
purgedAt: pick(row, "purged_at") ?? null,
|
||||
updatedAt: pick(row, "updated_at") ?? null,
|
||||
},
|
||||
ocrStatus,
|
||||
ocrText,
|
||||
hasOcrText: Boolean(ocrText.trim()),
|
||||
note: ocrText.trim()
|
||||
? "已返回 ocr_text。"
|
||||
: "该图片暂未生成 ocr_text(可等待后台 OCR,或后续再补一个 image_ocr 写工具)。",
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
throw new Error(`未知工具:${toolId}`);
|
||||
};
|
||||
|
||||
return { run };
|
||||
};
|
||||
|
||||
@@ -0,0 +1,736 @@
|
||||
import type { OpenAiCompatibleChatOptions } from "@/lib/ai/openaiCompatibleChat";
|
||||
import { openAiCompatibleChat, tryExtractJsonObject } from "@/lib/ai/openaiCompatibleChat";
|
||||
import { applyMindmapOps, ensureMindmapUids, type MindmapOp, type MindmapTreeNode, type NodeRef } from "@/lib/mindmap/mindmapOps";
|
||||
import { readMindmapLocal, writeMindmapLocal } from "@/lib/mindmap/mindmapLocalStore";
|
||||
import { searchSearxng, type SearxResult } from "../searchWeb";
|
||||
|
||||
export type MindmapToolContext = {
|
||||
documentId: string;
|
||||
mindmapId: string;
|
||||
userId: string;
|
||||
// 仅用于生成更贴近当前选中节点的行为(可选)
|
||||
selectedUids?: string[];
|
||||
// 来自前端(@ 选择/上传)的附件列表,优先使用(避免额外查询)
|
||||
attachments?: Array<{ id: string; title: string; fileUrl: string; mimeType?: string | null }>;
|
||||
};
|
||||
|
||||
type SupabaseRouteClient = {
|
||||
from: (table: string) => {
|
||||
select: (columns: string) => SupabaseQuery;
|
||||
};
|
||||
};
|
||||
|
||||
type SupabaseQuery = {
|
||||
eq: (column: string, value: unknown) => SupabaseQuery;
|
||||
is: (column: string, value: unknown) => SupabaseQuery;
|
||||
maybeSingle: () => Promise<{ data: unknown; error: unknown }>;
|
||||
};
|
||||
|
||||
export type MindmapSupabaseClient = SupabaseRouteClient;
|
||||
|
||||
const defaultMindmapData: MindmapTreeNode = { data: { text: "中心主题" }, children: [] };
|
||||
|
||||
const safeUrlOrNull = (value: unknown) => {
|
||||
const s = typeof value === "string" ? value.trim() : "";
|
||||
if (!s) return null;
|
||||
try {
|
||||
const u = new URL(s);
|
||||
if (u.protocol !== "http:" && u.protocol !== "https:") return null;
|
||||
return u.toString();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const findNodeByUid = (root: MindmapTreeNode, uid: string): MindmapTreeNode | null => {
|
||||
const target = String(uid || "");
|
||||
if (!target) return null;
|
||||
const walk = (n: MindmapTreeNode): MindmapTreeNode | null => {
|
||||
if (String(n?.data?.uid || "") === target) return n;
|
||||
const children = Array.isArray(n.children) ? n.children : [];
|
||||
for (const c of children) {
|
||||
const hit = walk(c);
|
||||
if (hit) return hit;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
return walk(root);
|
||||
};
|
||||
|
||||
const walkSummaries = (root: MindmapTreeNode, maxNodes = 120) => {
|
||||
const list: Array<{ uid: string; text: string; parentUid: string | null; depth: number; childCount: number }> = [];
|
||||
const queue: Array<{ node: MindmapTreeNode; parentUid: string | null; depth: number }> = [{ node: root, parentUid: null, depth: 0 }];
|
||||
while (queue.length && list.length < maxNodes) {
|
||||
const { node, parentUid, depth } = queue.shift()!;
|
||||
const uid = String(node?.data?.uid || "");
|
||||
const text = String(node?.data?.text ?? "").replace(/<[^>]+>/g, "").trim();
|
||||
const children = Array.isArray(node?.children) ? node.children : [];
|
||||
if (uid) list.push({ uid, text, parentUid, depth, childCount: children.length });
|
||||
children.forEach((c) => queue.push({ node: c, parentUid: uid || parentUid, depth: depth + 1 }));
|
||||
}
|
||||
return list;
|
||||
};
|
||||
|
||||
const summarizeSubtree = (node: MindmapTreeNode, depthLimit = 2, maxNodes = 60) => {
|
||||
const list: Array<{ uid: string; text: string; depth: number; childCount: number }> = [];
|
||||
const queue: Array<{ node: MindmapTreeNode; depth: number }> = [{ node, depth: 0 }];
|
||||
while (queue.length && list.length < maxNodes) {
|
||||
const { node: n, depth } = queue.shift()!;
|
||||
const uid = String(n?.data?.uid || "");
|
||||
const text = String(n?.data?.text ?? "").replace(/<[^>]+>/g, "").trim();
|
||||
const children = Array.isArray(n.children) ? n.children : [];
|
||||
if (uid) list.push({ uid, text, depth, childCount: children.length });
|
||||
if (depth < depthLimit) children.forEach((c) => queue.push({ node: c, depth: depth + 1 }));
|
||||
}
|
||||
return list;
|
||||
};
|
||||
|
||||
const refsFromSearx = (r: SearxResult): NodeRef[] => [
|
||||
{ kind: "url", fileUrl: r.url, title: r.title, snippet: r.snippet ? r.snippet.slice(0, 300) : undefined },
|
||||
];
|
||||
|
||||
const coerceMimeKind = (mimeType: string) => {
|
||||
const m = String(mimeType || "").toLowerCase();
|
||||
if (m.includes("pdf")) return "pdf" as const;
|
||||
if (m.includes("word") || m.includes("docx")) return "docx" as const;
|
||||
if (m.includes("presentation") || m.includes("ppt")) return "pptx" as const;
|
||||
return "url" as const;
|
||||
};
|
||||
|
||||
type ResolvedAttachment = { id: string; title: string; fileUrl: string; mimeType?: string | null };
|
||||
|
||||
const resolveAttachmentFromContext = (ctx: MindmapToolContext, ref: string): ResolvedAttachment | null => {
|
||||
const s = String(ref || "").trim();
|
||||
if (!s) return null;
|
||||
const list = Array.isArray(ctx.attachments) ? ctx.attachments : [];
|
||||
const byId = list.find((a) => String(a.id) === s);
|
||||
if (byId) return { id: String(byId.id), title: String(byId.title || byId.id), fileUrl: String(byId.fileUrl || ""), mimeType: byId.mimeType ?? null };
|
||||
const exactTitle = list.find((a) => String(a.title) === s);
|
||||
if (exactTitle)
|
||||
return { id: String(exactTitle.id), title: String(exactTitle.title || exactTitle.id), fileUrl: String(exactTitle.fileUrl || ""), mimeType: exactTitle.mimeType ?? null };
|
||||
const fuzzy = list.find((a) => String(a.title || "").includes(s) || String(a.fileUrl || "").includes(s));
|
||||
if (fuzzy) return { id: String(fuzzy.id), title: String(fuzzy.title || fuzzy.id), fileUrl: String(fuzzy.fileUrl || ""), mimeType: fuzzy.mimeType ?? null };
|
||||
return null;
|
||||
};
|
||||
|
||||
const mergeRefsUnique = (a: NodeRef[], b: NodeRef[]) => {
|
||||
const keyOf = (r: NodeRef) => `${r.kind}|${r.assetId ?? ""}|${r.fileUrl ?? ""}|${r.page ?? ""}|${r.slide ?? ""}`;
|
||||
const map = new Map<string, NodeRef>();
|
||||
for (const r of [...a, ...b]) {
|
||||
if (!r || typeof r !== "object") continue;
|
||||
map.set(keyOf(r), r);
|
||||
}
|
||||
return Array.from(map.values());
|
||||
};
|
||||
|
||||
const sanitizeAddChildOps = (args: {
|
||||
targetUid: string;
|
||||
currentChildren: string[];
|
||||
ops: MindmapOp[];
|
||||
searxResults: SearxResult[];
|
||||
}) => {
|
||||
const existed = new Set(args.currentChildren);
|
||||
const fixed: MindmapOp[] = [];
|
||||
for (const raw of args.ops) {
|
||||
if (!raw || typeof raw !== "object" || (raw as { op?: string }).op !== "addChild") continue;
|
||||
const parentUid = String((raw as { parentUid?: string }).parentUid ?? "");
|
||||
if (parentUid !== args.targetUid) continue;
|
||||
const node = (raw as { node?: Record<string, unknown> }).node ?? {};
|
||||
const textVal = String(node.text ?? "").trim();
|
||||
if (!textVal) continue;
|
||||
if (existed.has(textVal)) continue;
|
||||
existed.add(textVal);
|
||||
|
||||
const href = safeUrlOrNull(node.hyperlink) ?? safeUrlOrNull(node.url) ?? null;
|
||||
const refs = Array.isArray(node.refs) ? (node.refs as NodeRef[]) : [];
|
||||
const hasRef = refs.some((x) => {
|
||||
if (!x || x.kind !== "url") return false;
|
||||
const fileUrl = typeof x.fileUrl === "string" ? x.fileUrl : "";
|
||||
return Boolean(safeUrlOrNull(fileUrl));
|
||||
});
|
||||
const finalRefs = hasRef ? refs : args.searxResults[0] ? refsFromSearx(args.searxResults[0]) : [];
|
||||
|
||||
let finalText = textVal;
|
||||
if (!finalRefs.length && !/待核验/.test(finalText)) finalText = `${finalText}(待核验)`;
|
||||
|
||||
fixed.push({
|
||||
op: "addChild",
|
||||
parentUid: args.targetUid,
|
||||
node: {
|
||||
text: finalText,
|
||||
...(href ? { hyperlink: href } : {}),
|
||||
...(finalRefs.length ? { refs: finalRefs } : {}),
|
||||
},
|
||||
});
|
||||
if (fixed.length >= 6) break;
|
||||
}
|
||||
|
||||
if (!fixed.length && args.searxResults.length) {
|
||||
for (const r of args.searxResults.slice(0, 6)) {
|
||||
const title = String(r.title || "").trim();
|
||||
const url = safeUrlOrNull(r.url);
|
||||
if (!title || !url) continue;
|
||||
if (existed.has(title)) continue;
|
||||
existed.add(title);
|
||||
fixed.push({ op: "addChild", parentUid: args.targetUid, node: { text: title, hyperlink: url, refs: refsFromSearx(r) } });
|
||||
if (fixed.length >= 6) break;
|
||||
}
|
||||
}
|
||||
|
||||
if (fixed.length < 3) {
|
||||
const base = args.currentChildren[0] ? "补完" : "补完";
|
||||
for (let i = fixed.length + 1; i <= 3; i += 1) {
|
||||
fixed.push({ op: "addChild", parentUid: args.targetUid, node: { text: `${base}(待核验)${i}` } });
|
||||
}
|
||||
}
|
||||
|
||||
return fixed.slice(0, 6);
|
||||
};
|
||||
|
||||
export const createMindmapServerTools = (args: {
|
||||
supabase: SupabaseRouteClient;
|
||||
ctx: MindmapToolContext;
|
||||
cfg: OpenAiCompatibleChatOptions;
|
||||
allowedToolIds: Set<string>;
|
||||
}) => {
|
||||
const loadDoc = async () => {
|
||||
const { documentId, userId } = args.ctx;
|
||||
const query = args.supabase
|
||||
.from("documents")
|
||||
.select("id,title,workspace_id,mindmap_data")
|
||||
.eq("id", documentId)
|
||||
.eq("user_id", userId);
|
||||
const { data, error } = await query.maybeSingle();
|
||||
if (error && typeof error === "object" && "message" in (error as Record<string, unknown>)) {
|
||||
throw new Error(String((error as Record<string, unknown>).message ?? "读取页面失败"));
|
||||
}
|
||||
if (error) throw new Error("读取页面失败");
|
||||
if (!data) throw new Error("页面不存在");
|
||||
return data as { id: string; title: string | null; workspace_id: string | null; mindmap_data: unknown };
|
||||
};
|
||||
|
||||
const loadMindmap = async () => {
|
||||
const doc = await loadDoc();
|
||||
const local = await readMindmapLocal(args.ctx.documentId, args.ctx.mindmapId);
|
||||
const base = (local.ok ? local.data : (doc.mindmap_data ?? defaultMindmapData)) as MindmapTreeNode;
|
||||
ensureMindmapUids(base);
|
||||
return { doc, base };
|
||||
};
|
||||
|
||||
const mindmap_get = async (toolArgs: Record<string, unknown>) => {
|
||||
const maxNodes = Number(toolArgs.maxNodes ?? 120);
|
||||
const { base } = await loadMindmap();
|
||||
const list = walkSummaries(base, Number.isFinite(maxNodes) ? Math.max(10, Math.min(300, Math.floor(maxNodes))) : 120);
|
||||
return { ok: true, documentId: args.ctx.documentId, mindmapId: args.ctx.mindmapId, nodes: list };
|
||||
};
|
||||
|
||||
const mindmap_get_subtree = async (toolArgs: Record<string, unknown>) => {
|
||||
const uid = String(toolArgs.uid ?? "").trim();
|
||||
if (!uid) throw new Error("缺少 uid");
|
||||
const depth = Number(toolArgs.depth ?? 2);
|
||||
const maxNodes = Number(toolArgs.maxNodes ?? 60);
|
||||
const { base } = await loadMindmap();
|
||||
const hit = findNodeByUid(base, uid);
|
||||
if (!hit) throw new Error(`未找到 uid=${uid}`);
|
||||
const list = summarizeSubtree(
|
||||
hit,
|
||||
Number.isFinite(depth) ? Math.max(0, Math.min(6, Math.floor(depth))) : 2,
|
||||
Number.isFinite(maxNodes) ? Math.max(5, Math.min(200, Math.floor(maxNodes))) : 60,
|
||||
);
|
||||
return { ok: true, uid, nodes: list };
|
||||
};
|
||||
|
||||
const mindmap_apply_ops = async (toolArgs: Record<string, unknown>) => {
|
||||
const ops = (toolArgs.ops ?? []) as unknown;
|
||||
const reason = String(toolArgs.reason ?? "").trim() || null;
|
||||
if (!Array.isArray(ops) || ops.length === 0) throw new Error("缺少 ops");
|
||||
if (ops.length > 80) throw new Error("ops 过多(最多 80)");
|
||||
|
||||
const normalizeOp = (raw: unknown): MindmapOp | null => {
|
||||
if (!raw || typeof raw !== "object") return null;
|
||||
const o = raw as Record<string, unknown>;
|
||||
const op = String(o.op ?? "").trim();
|
||||
|
||||
// 兼容历史/模型常见写法:update_node/add_child/set_link/append_note/delete_node
|
||||
if (op === "update_node" || op === "updateNode") {
|
||||
const uid = String(o.uid ?? o.id ?? "").trim();
|
||||
const text = String(o.text ?? o.value ?? "").trim();
|
||||
if (!uid || !text) return null;
|
||||
return { op: "updateText", uid, text };
|
||||
}
|
||||
if (op === "add_child" || op === "addChild") {
|
||||
const parentUid = String(o.parentUid ?? o.parent_uid ?? "").trim();
|
||||
const node = (o.node && typeof o.node === "object" ? (o.node as Record<string, unknown>) : {}) as Record<string, unknown>;
|
||||
const text = String(node.text ?? "").trim();
|
||||
if (!parentUid || !text) return null;
|
||||
const hyperlink = safeUrlOrNull(node.hyperlink) ?? null;
|
||||
const note = String(node.note ?? "").trim() || null;
|
||||
const refs = Array.isArray(node.refs) ? (node.refs as NodeRef[]) : [];
|
||||
return { op: "addChild", parentUid, node: { text, ...(hyperlink ? { hyperlink } : {}), ...(note ? { note } : {}), ...(refs.length ? { refs } : {}) } };
|
||||
}
|
||||
if (op === "set_link" || op === "set_hyperlink" || op === "setHyperlink") {
|
||||
const uid = String(o.uid ?? o.id ?? "").trim();
|
||||
const hyperlinkRaw = o.hyperlink ?? o.url ?? null;
|
||||
const hyperlink = hyperlinkRaw === null ? null : safeUrlOrNull(hyperlinkRaw);
|
||||
if (!uid) return null;
|
||||
return { op: "setHyperlink", uid, hyperlink: hyperlinkRaw === null ? null : hyperlink };
|
||||
}
|
||||
if (op === "append_note" || op === "appendNote") {
|
||||
const uid = String(o.uid ?? o.id ?? "").trim();
|
||||
const markdown = String(o.markdown ?? o.note ?? "").trim();
|
||||
if (!uid || !markdown) return null;
|
||||
return { op: "appendNote", uid, markdown };
|
||||
}
|
||||
if (op === "set_refs" || op === "setRefs") {
|
||||
const uid = String(o.uid ?? o.id ?? "").trim();
|
||||
const refs = Array.isArray(o.refs) ? (o.refs as NodeRef[]) : [];
|
||||
if (!uid || refs.length === 0) return null;
|
||||
return { op: "setRefs", uid, refs };
|
||||
}
|
||||
if (op === "delete_node" || op === "deleteNode") {
|
||||
const uid = String(o.uid ?? o.id ?? "").trim();
|
||||
if (!uid) return null;
|
||||
return { op: "deleteNode", uid };
|
||||
}
|
||||
|
||||
// 原生协议(MindmapOp)
|
||||
if (
|
||||
op === "addChild" ||
|
||||
op === "addSiblingAfter" ||
|
||||
op === "updateText" ||
|
||||
op === "setHyperlink" ||
|
||||
op === "setRefs" ||
|
||||
op === "appendNote" ||
|
||||
op === "deleteNode"
|
||||
) {
|
||||
return o as unknown as MindmapOp;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const normalized = ops.map((x) => normalizeOp(x)).filter(Boolean) as MindmapOp[];
|
||||
if (normalized.length === 0) throw new Error("ops 无有效操作(请使用 MindmapOp 协议或已支持的别名)");
|
||||
|
||||
const { doc, base } = await loadMindmap();
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, normalized);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
return { ok: true, applied, errors, data: nextData, meta: { reason } };
|
||||
};
|
||||
|
||||
const mindmap_add_child = async (toolArgs: Record<string, unknown>) => {
|
||||
const parentUid = String(toolArgs.parentUid ?? "").trim();
|
||||
const text = String(toolArgs.text ?? "").trim();
|
||||
const hyperlink = safeUrlOrNull(toolArgs.hyperlink) ?? null;
|
||||
const note = String(toolArgs.note ?? "").trim() || null;
|
||||
const reason = String(toolArgs.reason ?? "").trim() || null;
|
||||
const refs = Array.isArray(toolArgs.refs) ? (toolArgs.refs as NodeRef[]) : [];
|
||||
if (!parentUid) throw new Error("缺少 parentUid");
|
||||
if (!text) throw new Error("缺少 text");
|
||||
|
||||
const { doc, base } = await loadMindmap();
|
||||
const op: MindmapOp = {
|
||||
op: "addChild",
|
||||
parentUid,
|
||||
node: { text, ...(hyperlink ? { hyperlink } : {}), ...(note ? { note } : {}), ...(refs.length ? { refs } : {}) },
|
||||
};
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, [op]);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
return { ok: true, applied, errors, data: nextData, meta: { reason } };
|
||||
};
|
||||
|
||||
const mindmap_add_sibling_after = async (toolArgs: Record<string, unknown>) => {
|
||||
const targetUid = String(toolArgs.targetUid ?? "").trim();
|
||||
const text = String(toolArgs.text ?? "").trim();
|
||||
const hyperlink = safeUrlOrNull(toolArgs.hyperlink) ?? null;
|
||||
const note = String(toolArgs.note ?? "").trim() || null;
|
||||
const reason = String(toolArgs.reason ?? "").trim() || null;
|
||||
const refs = Array.isArray(toolArgs.refs) ? (toolArgs.refs as NodeRef[]) : [];
|
||||
if (!targetUid) throw new Error("缺少 targetUid");
|
||||
if (!text) throw new Error("缺少 text");
|
||||
|
||||
const { doc, base } = await loadMindmap();
|
||||
const op: MindmapOp = {
|
||||
op: "addSiblingAfter",
|
||||
targetUid,
|
||||
node: { text, ...(hyperlink ? { hyperlink } : {}), ...(note ? { note } : {}), ...(refs.length ? { refs } : {}) },
|
||||
};
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, [op]);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
return { ok: true, applied, errors, data: nextData, meta: { reason } };
|
||||
};
|
||||
|
||||
const mindmap_update_node_text = async (toolArgs: Record<string, unknown>) => {
|
||||
const uid = String(toolArgs.uid ?? "").trim();
|
||||
const text = String(toolArgs.text ?? "").trim();
|
||||
const reason = String(toolArgs.reason ?? "").trim() || null;
|
||||
if (!uid) throw new Error("缺少 uid");
|
||||
if (!text) throw new Error("缺少 text");
|
||||
const { doc, base } = await loadMindmap();
|
||||
const op: MindmapOp = { op: "updateText", uid, text };
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, [op]);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
return { ok: true, applied, errors, data: nextData, meta: { reason } };
|
||||
};
|
||||
|
||||
const mindmap_set_hyperlink = async (toolArgs: Record<string, unknown>) => {
|
||||
const uid = String(toolArgs.uid ?? "").trim();
|
||||
const hyperlinkRaw = toolArgs.hyperlink;
|
||||
const hyperlink = hyperlinkRaw === null ? null : safeUrlOrNull(hyperlinkRaw);
|
||||
const reason = String(toolArgs.reason ?? "").trim() || null;
|
||||
if (!uid) throw new Error("缺少 uid");
|
||||
if (hyperlinkRaw !== null && hyperlinkRaw !== undefined && !hyperlink) {
|
||||
throw new Error("hyperlink 必须是 http(s) URL 或 null");
|
||||
}
|
||||
const { doc, base } = await loadMindmap();
|
||||
const op: MindmapOp = { op: "setHyperlink", uid, hyperlink: hyperlinkRaw === null ? null : hyperlink };
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, [op]);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
return { ok: true, applied, errors, data: nextData, meta: { reason } };
|
||||
};
|
||||
|
||||
const mindmap_append_note = async (toolArgs: Record<string, unknown>) => {
|
||||
const uid = String(toolArgs.uid ?? "").trim();
|
||||
const markdown = String(toolArgs.markdown ?? "").trim();
|
||||
const reason = String(toolArgs.reason ?? "").trim() || null;
|
||||
if (!uid) throw new Error("缺少 uid");
|
||||
if (!markdown) throw new Error("缺少 markdown");
|
||||
const { doc, base } = await loadMindmap();
|
||||
const op: MindmapOp = { op: "appendNote", uid, markdown };
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, [op]);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
return { ok: true, applied, errors, data: nextData, meta: { reason } };
|
||||
};
|
||||
|
||||
const mindmap_set_refs = async (toolArgs: Record<string, unknown>) => {
|
||||
const uid = String(toolArgs.uid ?? "").trim();
|
||||
const reason = String(toolArgs.reason ?? "").trim() || null;
|
||||
const refs = Array.isArray(toolArgs.refs) ? (toolArgs.refs as NodeRef[]) : [];
|
||||
if (!uid) throw new Error("缺少 uid");
|
||||
if (!refs.length) throw new Error("缺少 refs");
|
||||
const { doc, base } = await loadMindmap();
|
||||
const op: MindmapOp = { op: "setRefs", uid, refs };
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, [op]);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
return { ok: true, applied, errors, data: nextData, meta: { reason } };
|
||||
};
|
||||
|
||||
const mindmap_delete_node = async (toolArgs: Record<string, unknown>) => {
|
||||
const uid = String(toolArgs.uid ?? "").trim();
|
||||
const reason = String(toolArgs.reason ?? "").trim() || null;
|
||||
if (!uid) throw new Error("缺少 uid");
|
||||
const { doc, base } = await loadMindmap();
|
||||
const op: MindmapOp = { op: "deleteNode", uid };
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, [op]);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
return { ok: true, applied, errors, data: nextData, meta: { reason } };
|
||||
};
|
||||
|
||||
const mindmap_add_attachment_ref = async (toolArgs: Record<string, unknown>) => {
|
||||
const uid = String(toolArgs.uid ?? "").trim();
|
||||
const attachmentId = String(toolArgs.attachmentId ?? toolArgs.attachmentRef ?? "").trim();
|
||||
const fileUrlDirect = String(toolArgs.fileUrl ?? "").trim();
|
||||
const mode = String(toolArgs.mode ?? "append").trim() as "append" | "replace";
|
||||
const page = Number(toolArgs.page ?? 0);
|
||||
const slide = Number(toolArgs.slide ?? 0);
|
||||
const reason = String(toolArgs.reason ?? "").trim() || null;
|
||||
const snippet = String(toolArgs.snippet ?? "").trim() || null;
|
||||
if (!uid) throw new Error("缺少 uid");
|
||||
if (!attachmentId && !fileUrlDirect) throw new Error("缺少 attachmentId 或 fileUrl");
|
||||
|
||||
const { doc, base } = await loadMindmap();
|
||||
const node = findNodeByUid(base, uid);
|
||||
if (!node) throw new Error(`未找到 uid=${uid}`);
|
||||
|
||||
let resolved: ResolvedAttachment | null = null;
|
||||
if (attachmentId) resolved = resolveAttachmentFromContext(args.ctx, attachmentId);
|
||||
|
||||
if (!resolved && attachmentId) {
|
||||
const workspaceId = String((doc as any).workspace_id ?? "").trim();
|
||||
if (!workspaceId) throw new Error("缺少 workspace_id,无法解析附件");
|
||||
const query = args.supabase
|
||||
.from("media_assets")
|
||||
.select("id,file_url,file_name,mime_type,document_id,workspace_id,deleted_at")
|
||||
.eq("id", attachmentId)
|
||||
.eq("document_id", args.ctx.documentId)
|
||||
.eq("workspace_id", workspaceId)
|
||||
.is("deleted_at", null);
|
||||
const { data, error } = await query.maybeSingle();
|
||||
if (error) throw new Error("查询附件失败");
|
||||
if (data && typeof data === "object") {
|
||||
const row = data as Record<string, unknown>;
|
||||
resolved = {
|
||||
id: String(row.id ?? attachmentId),
|
||||
title: String(row.file_name ?? row.id ?? attachmentId),
|
||||
fileUrl: String(row.file_url ?? ""),
|
||||
mimeType: (row.mime_type as string | null | undefined) ?? null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const finalUrl = (resolved?.fileUrl || fileUrlDirect || "").trim();
|
||||
const finalTitle = String(resolved?.title || toolArgs.title || attachmentId || "附件").trim();
|
||||
if (!finalUrl) throw new Error("附件缺少 fileUrl");
|
||||
|
||||
const kindOverride = String(toolArgs.kind ?? "").trim();
|
||||
const kind =
|
||||
kindOverride === "pdf" || kindOverride === "docx" || kindOverride === "pptx" || kindOverride === "url"
|
||||
? (kindOverride as NodeRef["kind"])
|
||||
: resolved?.mimeType
|
||||
? coerceMimeKind(resolved.mimeType)
|
||||
: ("url" as const);
|
||||
|
||||
const ref: NodeRef = {
|
||||
kind,
|
||||
...(attachmentId ? { assetId: attachmentId } : {}),
|
||||
fileUrl: finalUrl,
|
||||
...(Number.isFinite(page) && page > 0 ? { page: Math.floor(page) } : {}),
|
||||
...(Number.isFinite(slide) && slide > 0 ? { slide: Math.floor(slide) } : {}),
|
||||
...(finalTitle ? { title: finalTitle } : {}),
|
||||
...(snippet ? { snippet } : {}),
|
||||
};
|
||||
|
||||
const prevRefs = Array.isArray(node.data?.refs) ? (node.data.refs as NodeRef[]) : [];
|
||||
const nextRefs = mode === "replace" ? [ref] : mergeRefsUnique(prevRefs, [ref]);
|
||||
const op: MindmapOp = { op: "setRefs", uid, refs: nextRefs };
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, [op]);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
return { ok: true, applied, errors, data: nextData, meta: { reason } };
|
||||
};
|
||||
|
||||
const mindmap_add_attachment_child = async (toolArgs: Record<string, unknown>) => {
|
||||
const parentUid = String(toolArgs.parentUid ?? "").trim();
|
||||
const attachmentId = String(toolArgs.attachmentId ?? toolArgs.attachmentRef ?? "").trim();
|
||||
const fileUrlDirect = String(toolArgs.fileUrl ?? "").trim();
|
||||
const titleOverride = String(toolArgs.title ?? "").trim();
|
||||
const textOverride = String(toolArgs.text ?? "").trim();
|
||||
const note = String(toolArgs.note ?? "").trim() || null;
|
||||
const page = Number(toolArgs.page ?? 0);
|
||||
const slide = Number(toolArgs.slide ?? 0);
|
||||
const reason = String(toolArgs.reason ?? "").trim() || null;
|
||||
if (!parentUid) throw new Error("缺少 parentUid");
|
||||
if (!attachmentId && !fileUrlDirect) throw new Error("缺少 attachmentId 或 fileUrl");
|
||||
|
||||
const { doc, base } = await loadMindmap();
|
||||
const resolved = attachmentId ? resolveAttachmentFromContext(args.ctx, attachmentId) : null;
|
||||
const finalUrl = String(resolved?.fileUrl || fileUrlDirect || "").trim();
|
||||
const finalTitle = String(titleOverride || resolved?.title || attachmentId || "附件").trim();
|
||||
if (!finalUrl) throw new Error("附件缺少 fileUrl");
|
||||
|
||||
const kindOverride = String(toolArgs.kind ?? "").trim();
|
||||
const kind =
|
||||
kindOverride === "pdf" || kindOverride === "docx" || kindOverride === "pptx" || kindOverride === "url"
|
||||
? (kindOverride as NodeRef["kind"])
|
||||
: resolved?.mimeType
|
||||
? coerceMimeKind(resolved.mimeType)
|
||||
: ("url" as const);
|
||||
|
||||
const parts: string[] = [];
|
||||
parts.push(textOverride || `附件:${finalTitle}`);
|
||||
if (Number.isFinite(page) && page > 0) parts.push(`p.${Math.floor(page)}`);
|
||||
if (Number.isFinite(slide) && slide > 0) parts.push(`slide ${Math.floor(slide)}`);
|
||||
const text = parts.join(" ");
|
||||
|
||||
const ref: NodeRef = {
|
||||
kind,
|
||||
...(attachmentId ? { assetId: attachmentId } : {}),
|
||||
fileUrl: finalUrl,
|
||||
...(Number.isFinite(page) && page > 0 ? { page: Math.floor(page) } : {}),
|
||||
...(Number.isFinite(slide) && slide > 0 ? { slide: Math.floor(slide) } : {}),
|
||||
...(finalTitle ? { title: finalTitle } : {}),
|
||||
};
|
||||
|
||||
const hyperlink = safeUrlOrNull(finalUrl) ?? null;
|
||||
const op: MindmapOp = {
|
||||
op: "addChild",
|
||||
parentUid,
|
||||
node: { text, ...(hyperlink ? { hyperlink } : {}), refs: [ref], ...(note ? { note } : {}) },
|
||||
};
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, [op]);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
return { ok: true, applied, errors, data: nextData, meta: { reason } };
|
||||
};
|
||||
|
||||
const mindmap_add_image_child = async (toolArgs: Record<string, unknown>) => {
|
||||
const parentUid = String(toolArgs.parentUid ?? "").trim();
|
||||
const imageRef = String(toolArgs.imageRef ?? toolArgs.imageId ?? toolArgs.imageUrl ?? "").trim();
|
||||
const alt = String(toolArgs.alt ?? "").trim() || "image";
|
||||
const caption = String(toolArgs.caption ?? "").trim() || "";
|
||||
const reason = String(toolArgs.reason ?? "").trim() || null;
|
||||
if (!parentUid) throw new Error("缺少 parentUid");
|
||||
if (!imageRef) throw new Error("缺少 imageRef(可用 attachmentId 或图片 URL)");
|
||||
|
||||
const resolved = resolveAttachmentFromContext(args.ctx, imageRef);
|
||||
const url = String(resolved?.fileUrl || imageRef).trim();
|
||||
const title = String(resolved?.title || caption || "图片").trim();
|
||||
if (!url) throw new Error("图片缺少 URL");
|
||||
|
||||
const markdown = ``;
|
||||
const hyperlink = safeUrlOrNull(url) ?? null;
|
||||
const op: MindmapOp = {
|
||||
op: "addChild",
|
||||
parentUid,
|
||||
node: {
|
||||
text: caption ? caption : `图片:${title}`,
|
||||
...(hyperlink ? { hyperlink } : {}),
|
||||
note: markdown,
|
||||
refs: [{ kind: "url", ...(resolved?.id ? { assetId: resolved.id } : {}), fileUrl: url, title }],
|
||||
},
|
||||
};
|
||||
|
||||
const { doc, base } = await loadMindmap();
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, [op]);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
return { ok: true, applied, errors, data: nextData, meta: { reason } };
|
||||
};
|
||||
|
||||
const mindmap_append_image_note = async (toolArgs: Record<string, unknown>) => {
|
||||
const uid = String(toolArgs.uid ?? "").trim();
|
||||
const imageRef = String(toolArgs.imageRef ?? toolArgs.imageId ?? toolArgs.imageUrl ?? "").trim();
|
||||
const alt = String(toolArgs.alt ?? "").trim() || "image";
|
||||
const reason = String(toolArgs.reason ?? "").trim() || null;
|
||||
if (!uid) throw new Error("缺少 uid");
|
||||
if (!imageRef) throw new Error("缺少 imageRef(可用 attachmentId 或图片 URL)");
|
||||
|
||||
const resolved = resolveAttachmentFromContext(args.ctx, imageRef);
|
||||
const url = String(resolved?.fileUrl || imageRef).trim();
|
||||
const title = String(resolved?.title || "图片").trim();
|
||||
if (!url) throw new Error("图片缺少 URL");
|
||||
|
||||
const markdown = ``;
|
||||
const { doc, base } = await loadMindmap();
|
||||
const op: MindmapOp = { op: "appendNote", uid, markdown };
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, [op]);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
return {
|
||||
ok: true,
|
||||
applied,
|
||||
errors,
|
||||
data: nextData,
|
||||
meta: { reason, ref: { kind: "url", ...(resolved?.id ? { assetId: resolved.id } : {}), fileUrl: url, title } },
|
||||
};
|
||||
};
|
||||
|
||||
const mindmap_expand_node = async (toolArgs: Record<string, unknown>) => {
|
||||
const targetUid = String(toolArgs.targetUid ?? "").trim();
|
||||
if (!targetUid) throw new Error("缺少 targetUid");
|
||||
const instruction = String(toolArgs.instruction ?? "").trim();
|
||||
const useSearx = args.allowedToolIds.has("search_web");
|
||||
|
||||
const { doc, base } = await loadMindmap();
|
||||
const target = findNodeByUid(base, targetUid);
|
||||
if (!target) throw new Error("未找到目标节点(uid 不存在)");
|
||||
|
||||
const targetText = String(target?.data?.text ?? "").trim();
|
||||
const currentChildren = Array.isArray(target?.children)
|
||||
? target.children
|
||||
.map((c) => String(c?.data?.text ?? "").trim())
|
||||
.filter(Boolean)
|
||||
.slice(0, 20)
|
||||
: [];
|
||||
|
||||
const query = [targetText, instruction].filter(Boolean).join(" ").trim();
|
||||
const searxResults = useSearx && query ? await searchSearxng(query, 6).catch(() => []) : [];
|
||||
|
||||
const system = [
|
||||
"你是一个“思维导图补完器”。",
|
||||
"你必须输出严格 JSON(不要 Markdown/不要代码块/不要解释)。",
|
||||
"你只能输出 {\"ops\": MindmapOp[]} 这一个对象。",
|
||||
"默认策略:为 targetUid 新增 3~6 个子节点(addChild)。",
|
||||
"每个新增节点必须提供 text,并尽量提供 hyperlink 与 refs(引用来自搜索结果 URL)。",
|
||||
"不要删除/改名已有节点;不要输出 addSiblingAfter/updateText/deleteNode。",
|
||||
].join("\n");
|
||||
|
||||
const user = [
|
||||
`documentId=${args.ctx.documentId}`,
|
||||
`mindmapId=${args.ctx.mindmapId}`,
|
||||
`targetUid=${targetUid}`,
|
||||
"",
|
||||
`目标节点:${targetText || "(empty)"}`,
|
||||
currentChildren.length ? `当前子节点(供去重):${currentChildren.join(";")}` : "",
|
||||
instruction ? `用户要求:${instruction}` : "",
|
||||
"",
|
||||
"可用证据(搜索结果):",
|
||||
...(searxResults.length
|
||||
? searxResults.map((r, idx) => {
|
||||
const snip = (r.snippet || "").replace(/\s+/g, " ").slice(0, 180);
|
||||
return `${idx + 1}. ${r.title}\n url: ${r.url}\n snippet: ${snip}`;
|
||||
})
|
||||
: ["(无)"]),
|
||||
"",
|
||||
"MindmapOp JSON Schema(仅供理解):",
|
||||
'{ "ops": [ { "op": "addChild", "parentUid": string, "node": { "text": string, "hyperlink"?: string, "refs"?: NodeRef[] } } ] }',
|
||||
'NodeRef 示例:{ "kind": "url", "fileUrl": "https://example.com", "title": "来源标题", "snippet": "..." }',
|
||||
"",
|
||||
"硬性约束:",
|
||||
"- 仅输出 addChild;parentUid 必须等于 targetUid。",
|
||||
"- 新增节点 text 不要与当前子节点重复。",
|
||||
"- hyperlink 必须是 http(s) URL。",
|
||||
"- 每个新增节点 refs 至少 1 条(kind=url, fileUrl=来源url);若没有来源,则在 text 末尾追加“(待核验)”。",
|
||||
"- 输出规模控制:最多 6 个节点。",
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join("\n");
|
||||
|
||||
let ops: MindmapOp[] = [];
|
||||
let finishReason = "";
|
||||
try {
|
||||
const { text, raw } = await openAiCompatibleChat(
|
||||
[
|
||||
{ role: "system", content: system },
|
||||
{ role: "user", content: user },
|
||||
],
|
||||
{
|
||||
...args.cfg,
|
||||
timeoutMs: 40_000,
|
||||
maxTokens: 1800,
|
||||
maxCompletionTokens: 1800,
|
||||
responseFormat: "json_object",
|
||||
},
|
||||
);
|
||||
if (typeof raw === "object" && raw) {
|
||||
const r = raw as Record<string, unknown>;
|
||||
const choices = Array.isArray(r.choices) ? (r.choices as unknown[]) : [];
|
||||
const first = (choices[0] && typeof choices[0] === "object" ? (choices[0] as Record<string, unknown>) : null) ?? null;
|
||||
finishReason = first ? String(first.finish_reason ?? "") : "";
|
||||
}
|
||||
const json = tryExtractJsonObject(text);
|
||||
if (json && Array.isArray((json as Record<string, unknown>).ops)) ops = (json as Record<string, unknown>).ops as MindmapOp[];
|
||||
} catch {
|
||||
ops = [];
|
||||
}
|
||||
|
||||
const fixed = sanitizeAddChildOps({ targetUid, currentChildren, ops, searxResults });
|
||||
const { data: nextData, applied, errors } = applyMindmapOps(base, fixed);
|
||||
await writeMindmapLocal(args.ctx.documentId, args.ctx.mindmapId, nextData, doc.title ?? "无标题");
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
applied,
|
||||
errors,
|
||||
ops: fixed,
|
||||
data: nextData,
|
||||
meta: { finishReason, searched: useSearx, searxCount: searxResults.length },
|
||||
};
|
||||
};
|
||||
|
||||
const run = async (toolId: string, toolArgs: Record<string, unknown>) => {
|
||||
if (toolId === "mindmap_get") return await mindmap_get(toolArgs);
|
||||
if (toolId === "mindmap_get_subtree") return await mindmap_get_subtree(toolArgs);
|
||||
if (toolId === "mindmap_apply_ops") return await mindmap_apply_ops(toolArgs);
|
||||
if (toolId === "mindmap_add_child") return await mindmap_add_child(toolArgs);
|
||||
if (toolId === "mindmap_add_sibling_after") return await mindmap_add_sibling_after(toolArgs);
|
||||
if (toolId === "mindmap_update_node_text") return await mindmap_update_node_text(toolArgs);
|
||||
if (toolId === "mindmap_set_hyperlink") return await mindmap_set_hyperlink(toolArgs);
|
||||
if (toolId === "mindmap_append_note") return await mindmap_append_note(toolArgs);
|
||||
if (toolId === "mindmap_set_refs") return await mindmap_set_refs(toolArgs);
|
||||
if (toolId === "mindmap_delete_node") return await mindmap_delete_node(toolArgs);
|
||||
if (toolId === "mindmap_add_attachment_ref") return await mindmap_add_attachment_ref(toolArgs);
|
||||
if (toolId === "mindmap_add_attachment_child") return await mindmap_add_attachment_child(toolArgs);
|
||||
if (toolId === "mindmap_add_image_child") return await mindmap_add_image_child(toolArgs);
|
||||
if (toolId === "mindmap_append_image_note") return await mindmap_append_image_note(toolArgs);
|
||||
if (toolId === "mindmap_expand_node") return await mindmap_expand_node(toolArgs);
|
||||
throw new Error(`不支持的工具:${toolId}`);
|
||||
};
|
||||
|
||||
return { run };
|
||||
};
|
||||
@@ -0,0 +1,123 @@
|
||||
export type RagToolContext = {
|
||||
userId: string;
|
||||
};
|
||||
|
||||
type RagFetchResult =
|
||||
| { ok: true; status: number; data: unknown }
|
||||
| { ok: false; status: number; error: string; data?: unknown };
|
||||
|
||||
const readEnv = (key: string) => {
|
||||
try {
|
||||
const v = process.env[key];
|
||||
return typeof v === "string" ? v.trim() : "";
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
};
|
||||
|
||||
const safeJoinUrl = (base: string, path: string) => {
|
||||
const b = String(base || "").trim().replace(/\/+$/, "");
|
||||
const p = String(path || "").trim().replace(/^\/+/, "");
|
||||
if (!b) return `/${p}`;
|
||||
return `${b}/${p}`;
|
||||
};
|
||||
|
||||
const fetchJson = async (url: string, init: RequestInit, timeoutMs: number): Promise<RagFetchResult> => {
|
||||
const ms = Math.max(500, Math.min(60_000, Math.floor(timeoutMs || 0) || 20_000));
|
||||
const controller = new AbortController();
|
||||
const t = setTimeout(() => controller.abort(), ms);
|
||||
try {
|
||||
const res = await fetch(url, { ...init, signal: controller.signal });
|
||||
const data = (await res.json().catch(() => null)) as unknown;
|
||||
if (!res.ok) {
|
||||
const detail =
|
||||
typeof data === "object" && data && "detail" in (data as any) ? String((data as any).detail ?? "") : "";
|
||||
const msg = detail || `HTTP ${res.status}`;
|
||||
return { ok: false, status: res.status, error: msg, data };
|
||||
}
|
||||
return { ok: true, status: res.status, data };
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
return { ok: false, status: 0, error: msg };
|
||||
} finally {
|
||||
clearTimeout(t);
|
||||
}
|
||||
};
|
||||
|
||||
export const createRagServerTools = (args: {
|
||||
ctx: RagToolContext;
|
||||
allowedToolIds: Set<string>;
|
||||
}) => {
|
||||
const run = async (toolId: string, toolArgs: Record<string, unknown>) => {
|
||||
if (!args.allowedToolIds.has(toolId)) {
|
||||
throw new Error(`工具未被允许:${toolId}`);
|
||||
}
|
||||
|
||||
if (toolId === "rag_lightrag_query") {
|
||||
const query = String(toolArgs.query ?? "").trim();
|
||||
if (!query) throw new Error("缺少 query");
|
||||
if (query.length < 2) throw new Error("query 太短(至少 2 个字符)");
|
||||
|
||||
const modeRaw = String(toolArgs.mode ?? "mix").trim();
|
||||
const mode =
|
||||
modeRaw === "local" ||
|
||||
modeRaw === "global" ||
|
||||
modeRaw === "hybrid" ||
|
||||
modeRaw === "naive" ||
|
||||
modeRaw === "mix" ||
|
||||
modeRaw === "bypass"
|
||||
? modeRaw
|
||||
: "mix";
|
||||
|
||||
const topKRaw = Number(toolArgs.topK ?? toolArgs.top_k ?? 12);
|
||||
const topK = Math.max(1, Math.min(60, Number.isFinite(topKRaw) ? Math.floor(topKRaw) : 12));
|
||||
|
||||
const chunkTopKRaw = Number(toolArgs.chunkTopK ?? toolArgs.chunk_top_k ?? topK);
|
||||
const chunkTopK = Math.max(1, Math.min(120, Number.isFinite(chunkTopKRaw) ? Math.floor(chunkTopKRaw) : topK));
|
||||
|
||||
const includeReferences = toolArgs.includeReferences !== false;
|
||||
const includeChunkContent = Boolean(toolArgs.includeChunkContent ?? true);
|
||||
const onlyNeedContext = Boolean(toolArgs.onlyNeedContext ?? false);
|
||||
const responseType = String(toolArgs.responseType ?? "").trim() || undefined;
|
||||
|
||||
const baseUrl = readEnv("LIGHTRAG_URL");
|
||||
if (!baseUrl) throw new Error("缺少环境变量:LIGHTRAG_URL");
|
||||
const apiKey = readEnv("LIGHTRAG_API_KEY");
|
||||
|
||||
const url = safeJoinUrl(baseUrl, "/query");
|
||||
const payload = {
|
||||
query,
|
||||
mode,
|
||||
top_k: topK,
|
||||
chunk_top_k: chunkTopK,
|
||||
include_references: includeReferences,
|
||||
include_chunk_content: includeChunkContent,
|
||||
only_need_context: onlyNeedContext,
|
||||
...(responseType ? { response_type: responseType } : {}),
|
||||
};
|
||||
|
||||
const r = await fetchJson(
|
||||
url,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
...(apiKey ? { "X-API-Key": apiKey } : {}),
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
},
|
||||
35_000,
|
||||
);
|
||||
|
||||
if (!r.ok) {
|
||||
throw new Error(`LightRAG 查询失败:${r.error}`);
|
||||
}
|
||||
return { ok: true, provider: "lightrag", query, mode, result: r.data };
|
||||
}
|
||||
|
||||
throw new Error(`未知工具:${toolId}`);
|
||||
};
|
||||
|
||||
return { run };
|
||||
};
|
||||
|
||||
@@ -0,0 +1,304 @@
|
||||
import type { AiAgentTool, AiAgentToolSet } from "../types";
|
||||
|
||||
export const builtinTools: AiAgentTool[] = [
|
||||
{
|
||||
id: "search_web",
|
||||
displayName: "联网检索(SearxNG)",
|
||||
modelDescription: "使用 SearxNG 搜索,返回标题/URL/摘要;用于提供可追溯来源。",
|
||||
inputSchemaText: `{ "query": "string", "count?": "number (1~10, 默认6)" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: false,
|
||||
isWriteTool: false,
|
||||
},
|
||||
{
|
||||
id: "rag_lightrag_query",
|
||||
displayName: "LightRAG 检索(RAG)",
|
||||
modelDescription:
|
||||
"调用本地/远程 LightRAG 服务进行检索与生成,返回 response + references(不写入)。",
|
||||
inputSchemaText:
|
||||
`{ "query": "string", "mode?": "\"mix\"|\"naive\"|\"local\"|\"global\"|\"hybrid\"|\"bypass\"", "topK?": "number (1~60, 默认12)", "chunkTopK?": "number (1~120, 默认与topK一致)", "includeReferences?": "boolean (默认 true)", "includeChunkContent?": "boolean (默认 true)", "onlyNeedContext?": "boolean (默认 false)", "responseType?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: false,
|
||||
isWriteTool: false,
|
||||
},
|
||||
{
|
||||
id: "docs_search",
|
||||
displayName: "搜索文档(跨页面)",
|
||||
modelDescription: "在工作区内按 title/raw_text 搜索文档,返回匹配列表与摘要片段(不写入)。",
|
||||
inputSchemaText:
|
||||
`{ "query": "string", "limit?": "number (1~30, 默认12)", "workspaceId?": "string", "includeDeleted?": "boolean (默认 false)" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: false,
|
||||
isWriteTool: false,
|
||||
},
|
||||
{
|
||||
id: "docs_read",
|
||||
displayName: "读取文档(跨页面)",
|
||||
modelDescription: "读取指定 documentId 的 title/raw_text(可选含 content),用于引用与对比(不写入)。",
|
||||
inputSchemaText:
|
||||
`{ "documentId": "string", "maxChars?": "number (200~20000, 默认2500)", "includeContent?": "boolean (默认 false)" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: false,
|
||||
isWriteTool: false,
|
||||
},
|
||||
{
|
||||
id: "image_read",
|
||||
displayName: "读取图片(OCR)",
|
||||
modelDescription: "读取图片/附件的 OCR 文本(优先从 media_assets.ocr_text 获取,不写入)。",
|
||||
inputSchemaText:
|
||||
`{ "assetId?": "string", "fileUrl?": "string", "attachmentRef?": "string (可用附件 id/title/url 片段)" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: false,
|
||||
isWriteTool: false,
|
||||
},
|
||||
{
|
||||
id: "slash_run",
|
||||
displayName: "斜杠命令(创建/改名)",
|
||||
modelDescription:
|
||||
"执行预置斜杠命令(例如 /new 创建文档、/rename 重命名)。这是写工具,执行前必须确认。",
|
||||
inputSchemaText:
|
||||
`{ "text?": "string (以 / 开头,例如 /new 标题)", "command?": "\"new_doc\"|\"rename_doc\"", "params?": "object (见命令说明)", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "doc_get",
|
||||
displayName: "读取页面内容(摘要)",
|
||||
modelDescription: "读取当前文档的块摘要(blockId/type/text/depth),用于定位与规划(不写入)。",
|
||||
inputSchemaText: `{ "maxBlocks?": "number (10~240, 默认80)" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: false,
|
||||
isWriteTool: false,
|
||||
},
|
||||
{
|
||||
id: "doc_find",
|
||||
displayName: "在页面中查找(按块)",
|
||||
modelDescription: "按块内容查找匹配的段落/标题,返回 blockId 列表(不写入)。",
|
||||
inputSchemaText: `{ "query": "string", "maxResults?": "number (1~30, 默认8)" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: false,
|
||||
isWriteTool: false,
|
||||
},
|
||||
{
|
||||
id: "doc_insert_blocks",
|
||||
displayName: "插入块(写入页面)",
|
||||
modelDescription:
|
||||
"在指定 block 前/后插入新块(目前支持 paragraph/heading)。写入后返回最新 blocks 快照(data)。",
|
||||
inputSchemaText:
|
||||
`{ "afterBlockId?": "string", "beforeBlockId?": "string", "blocks": "Array<{type:'paragraph'|'heading', text:string, level?:1..5}>", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "doc_replace_range",
|
||||
displayName: "替换块文本(写入页面)",
|
||||
modelDescription:
|
||||
"替换指定 block 的纯文本内容(不改 block 类型/props)。写入后返回最新 blocks 快照(data)。",
|
||||
inputSchemaText: `{ "blockId": "string", "text": "string", "mode?": "'replace'|'append'|'prepend'", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_get",
|
||||
displayName: "读取思维导图(摘要)",
|
||||
modelDescription: "读取当前 mindmap 的精简结构(uid/text/父子关系/子数量),用于定位与规划。",
|
||||
inputSchemaText: `{ "maxNodes?": "number (10~300, 默认120)" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: false,
|
||||
isWriteTool: false,
|
||||
},
|
||||
{
|
||||
id: "mindmap_get_subtree",
|
||||
displayName: "读取思维导图子树(摘要)",
|
||||
modelDescription: "读取指定 uid 的子树摘要(用于精确改写/补完)。",
|
||||
inputSchemaText: `{ "uid": "string", "depth?": "number (0~6, 默认2)", "maxNodes?": "number (5~200, 默认60)" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: false,
|
||||
isWriteTool: false,
|
||||
},
|
||||
{
|
||||
id: "mindmap_apply_ops",
|
||||
displayName: "应用思维导图增量操作(写入)",
|
||||
modelDescription: "对 mindmap 应用增量 ops(新增/改名/引用/备注/删除等),并落盘保存。",
|
||||
inputSchemaText: `{ "ops": "MindmapOp[]", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_add_child",
|
||||
displayName: "新增子节点",
|
||||
modelDescription: "在 parentUid 下新增一个子节点(可带 hyperlink/refs/note)。",
|
||||
inputSchemaText: `{ "parentUid": "string", "text": "string", "hyperlink?": "string (http/https)", "note?": "string", "refs?": "NodeRef[]", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_add_sibling_after",
|
||||
displayName: "在后面新增同级节点",
|
||||
modelDescription: "在 targetUid 后插入一个同级节点(不能用于根节点)。",
|
||||
inputSchemaText: `{ "targetUid": "string", "text": "string", "hyperlink?": "string (http/https)", "note?": "string", "refs?": "NodeRef[]", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_update_node_text",
|
||||
displayName: "更新节点文本",
|
||||
modelDescription: "更新指定 uid 的节点 text。",
|
||||
inputSchemaText: `{ "uid": "string", "text": "string", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_set_hyperlink",
|
||||
displayName: "设置/清除节点超链接",
|
||||
modelDescription: "为节点设置 hyperlink(http/https),或传 null 清除。",
|
||||
inputSchemaText: `{ "uid": "string", "hyperlink": "string|null", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_append_note",
|
||||
displayName: "追加节点备注",
|
||||
modelDescription: "向节点 note 追加一段 markdown 备注(不会覆盖原备注)。",
|
||||
inputSchemaText: `{ "uid": "string", "markdown": "string", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_set_refs",
|
||||
displayName: "设置节点引用(refs)",
|
||||
modelDescription: "设置指定 uid 的 refs(覆盖写入)。",
|
||||
inputSchemaText: `{ "uid": "string", "refs": "NodeRef[]", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_delete_node",
|
||||
displayName: "删除节点",
|
||||
modelDescription: "删除指定 uid 的节点(不能删除根节点)。",
|
||||
inputSchemaText: `{ "uid": "string", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_add_attachment_ref",
|
||||
displayName: "给节点增加附件引用(refs)",
|
||||
modelDescription: "把附件作为 refs 追加/替换到指定节点(支持 pdf/docx/pptx/url + page/slide)。",
|
||||
inputSchemaText:
|
||||
`{ "uid": "string", "attachmentId?": "string", "fileUrl?": "string", "title?": "string", "kind?": "\"pdf\"|\"docx\"|\"pptx\"|\"url\"", "page?": "number", "slide?": "number", "snippet?": "string", "mode?": "\"append\"|\"replace\"", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_add_attachment_child",
|
||||
displayName: "把附件插入为子节点",
|
||||
modelDescription: "在 parentUid 下新增一个“附件节点”,并自动写 refs +(可选)hyperlink。",
|
||||
inputSchemaText:
|
||||
`{ "parentUid": "string", "attachmentId?": "string", "fileUrl?": "string", "title?": "string", "text?": "string", "note?": "string", "kind?": "\"pdf\"|\"docx\"|\"pptx\"|\"url\"", "page?": "number", "slide?": "number", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_add_image_child",
|
||||
displayName: "插入图片为子节点",
|
||||
modelDescription: "在 parentUid 下新增一个图片子节点(note 中包含 markdown 图片)。",
|
||||
inputSchemaText:
|
||||
`{ "parentUid": "string", "imageRef": "string (attachmentId 或 图片URL)", "alt?": "string", "caption?": "string", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_append_image_note",
|
||||
displayName: "在节点备注中插入图片",
|
||||
modelDescription: "向指定节点的 note 追加一张图片(markdown 格式)。",
|
||||
inputSchemaText: `{ "uid": "string", "imageRef": "string (attachmentId 或 图片URL)", "alt?": "string", "reason?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
{
|
||||
id: "mindmap_expand_node",
|
||||
displayName: "补完思维导图节点(检索→生成→落盘)",
|
||||
modelDescription: "补完指定节点:可选联网检索作为证据,生成 3~6 个子节点并强制 refs,不足则标记“待核验”。",
|
||||
inputSchemaText: `{ "targetUid": "string", "instruction?": "string" }`,
|
||||
source: "builtin",
|
||||
requiresConfirmation: true,
|
||||
isWriteTool: true,
|
||||
},
|
||||
];
|
||||
|
||||
export const builtinToolSets: AiAgentToolSet[] = [
|
||||
{
|
||||
id: "toolset.readonly",
|
||||
displayName: "只读工具(全局)",
|
||||
toolIds: ["search_web"],
|
||||
},
|
||||
{
|
||||
id: "toolset.rag_read",
|
||||
displayName: "RAG 检索(LightRAG)",
|
||||
toolIds: ["rag_lightrag_query"],
|
||||
},
|
||||
{
|
||||
id: "toolset.docs_read",
|
||||
displayName: "文档检索/读取(跨页面)",
|
||||
toolIds: ["docs_search", "docs_read"],
|
||||
},
|
||||
{
|
||||
id: "toolset.media_read",
|
||||
displayName: "媒体读取(OCR/元信息)",
|
||||
toolIds: ["image_read"],
|
||||
},
|
||||
{
|
||||
id: "toolset.slash_write",
|
||||
displayName: "斜杠命令(写入)",
|
||||
toolIds: ["slash_run"],
|
||||
},
|
||||
{
|
||||
id: "toolset.doc_read",
|
||||
displayName: "页面读取(BlockNote)",
|
||||
toolIds: ["doc_get", "doc_find"],
|
||||
},
|
||||
{
|
||||
id: "toolset.doc_write",
|
||||
displayName: "页面写入(BlockNote)",
|
||||
toolIds: ["doc_insert_blocks", "doc_replace_range"],
|
||||
},
|
||||
{
|
||||
id: "toolset.mindmap_read",
|
||||
displayName: "思维导图读取",
|
||||
toolIds: ["mindmap_get", "mindmap_get_subtree"],
|
||||
},
|
||||
{
|
||||
id: "toolset.mindmap_write",
|
||||
displayName: "思维导图写入",
|
||||
toolIds: [
|
||||
"mindmap_apply_ops",
|
||||
"mindmap_add_child",
|
||||
"mindmap_add_sibling_after",
|
||||
"mindmap_update_node_text",
|
||||
"mindmap_set_hyperlink",
|
||||
"mindmap_append_note",
|
||||
"mindmap_set_refs",
|
||||
"mindmap_delete_node",
|
||||
"mindmap_add_attachment_ref",
|
||||
"mindmap_add_attachment_child",
|
||||
"mindmap_add_image_child",
|
||||
"mindmap_append_image_note",
|
||||
"mindmap_expand_node",
|
||||
],
|
||||
},
|
||||
];
|
||||
@@ -0,0 +1,53 @@
|
||||
export type SearxResult = { title: string; url: string; snippet?: string; engine?: string };
|
||||
|
||||
const safeUrlOrNull = (value: unknown) => {
|
||||
const s = typeof value === "string" ? value.trim() : "";
|
||||
if (!s) return null;
|
||||
try {
|
||||
const u = new URL(s);
|
||||
if (u.protocol !== "http:" && u.protocol !== "https:") return null;
|
||||
return u.toString();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export const searchSearxng = async (q: string, count = 6): Promise<SearxResult[]> => {
|
||||
const query = String(q || "").trim();
|
||||
if (!query) return [];
|
||||
|
||||
const base = (process.env.SEARXNG_BASE_URL ?? "http://127.0.0.1:8889").replace(/\/+$/, "");
|
||||
const token = (process.env.SEARXNG_API_TOKEN ?? "").trim();
|
||||
const url = `${base}/search?q=${encodeURIComponent(query)}&format=json&language=zhh-CN&categories=general&safesearch=1`;
|
||||
|
||||
const tryFetch = async (headers: Record<string, string>) => {
|
||||
const res = await fetch(url, { headers, method: "GET" });
|
||||
if (!res.ok) return null;
|
||||
return (await res.json().catch(() => null)) as unknown;
|
||||
};
|
||||
|
||||
let json: unknown = null;
|
||||
if (token) {
|
||||
json = (await tryFetch({ Authorization: `Bearer ${token}` })) ?? (await tryFetch({ "X-API-Key": token })) ?? null;
|
||||
}
|
||||
if (!json) json = await tryFetch({});
|
||||
|
||||
const results = (() => {
|
||||
if (typeof json !== "object" || !json) return [];
|
||||
const obj = json as Record<string, unknown>;
|
||||
const value = obj.results;
|
||||
return Array.isArray(value) ? (value as unknown[]) : [];
|
||||
})();
|
||||
return results
|
||||
.map((r: unknown) => {
|
||||
const obj = (typeof r === "object" && r ? (r as Record<string, unknown>) : {}) as Record<string, unknown>;
|
||||
return {
|
||||
title: String(obj.title ?? "").trim(),
|
||||
url: String(obj.url ?? "").trim(),
|
||||
snippet: String(obj.content ?? obj.snippet ?? "").trim(),
|
||||
engine: String(obj.engine ?? "").trim(),
|
||||
};
|
||||
})
|
||||
.filter((r: SearxResult) => r.title && safeUrlOrNull(r.url))
|
||||
.slice(0, Math.max(1, Math.min(10, count)));
|
||||
};
|
||||
@@ -0,0 +1,175 @@
|
||||
type SupabaseRouteClient = {
|
||||
from: (table: string) => any;
|
||||
};
|
||||
|
||||
export type SlashSupabaseClient = SupabaseRouteClient;
|
||||
|
||||
export type SlashToolContext = {
|
||||
userId: string;
|
||||
// 可选:在 document scope 下提供,便于默认继承 workspace_id / parent_id
|
||||
currentDocumentId?: string;
|
||||
};
|
||||
|
||||
const isRecord = (v: unknown): v is Record<string, unknown> =>
|
||||
typeof v === "object" && v !== null && !Array.isArray(v);
|
||||
|
||||
const pick = (obj: unknown, key: string) => (isRecord(obj) ? obj[key] : undefined);
|
||||
|
||||
const loadWorkspaceIds = async (supabase: SlashSupabaseClient, userId: string) => {
|
||||
const { data, error } = await supabase
|
||||
.from("workspace_members")
|
||||
.select("workspace_id")
|
||||
.eq("user_id", userId);
|
||||
if (error) {
|
||||
throw new Error(isRecord(error) && typeof error.message === "string" ? error.message : "读取工作区失败");
|
||||
}
|
||||
const rows = Array.isArray(data) ? data : [];
|
||||
return rows.map((r) => String((r as any)?.workspace_id ?? "")).filter(Boolean);
|
||||
};
|
||||
|
||||
const inferWorkspaceIdFromDoc = async (supabase: SlashSupabaseClient, documentId: string) => {
|
||||
const { data, error } = await supabase.from("documents").select("workspace_id").eq("id", documentId).single();
|
||||
if (error) return null;
|
||||
const wid = String(pick(data, "workspace_id") ?? "").trim();
|
||||
return wid || null;
|
||||
};
|
||||
|
||||
type ParsedSlash =
|
||||
| { ok: true; command: "new_doc"; params: { title: string; parentId?: string | null; workspaceId?: string | null } }
|
||||
| { ok: true; command: "rename_doc"; params: { documentId: string; title: string } }
|
||||
| { ok: false; error: string };
|
||||
|
||||
const parseSlash = (text: string): ParsedSlash => {
|
||||
const raw = String(text || "").trim();
|
||||
if (!raw.startsWith("/")) return { ok: false, error: "不是斜杠命令(必须以 / 开头)" };
|
||||
const parts = raw.split(/\s+/).filter(Boolean);
|
||||
const cmd = parts[0] ?? "";
|
||||
const rest = raw.slice(cmd.length).trim();
|
||||
|
||||
if (cmd === "/new" || cmd === "/new-doc" || cmd === "/newdoc") {
|
||||
const title = rest.trim();
|
||||
if (!title) return { ok: false, error: "用法:/new <标题>" };
|
||||
return { ok: true, command: "new_doc", params: { title } };
|
||||
}
|
||||
|
||||
if (cmd === "/rename" || cmd === "/rename-doc" || cmd === "/renamedoc") {
|
||||
const documentId = String(parts[1] ?? "").trim();
|
||||
const title = raw.split(/\s+/).slice(2).join(" ").trim();
|
||||
if (!documentId || !title) return { ok: false, error: "用法:/rename <documentId> <新标题>" };
|
||||
return { ok: true, command: "rename_doc", params: { documentId, title } };
|
||||
}
|
||||
|
||||
return { ok: false, error: `未知命令:${cmd}` };
|
||||
};
|
||||
|
||||
export const createSlashServerTools = (args: {
|
||||
supabase: SlashSupabaseClient;
|
||||
ctx: SlashToolContext;
|
||||
allowedToolIds: Set<string>;
|
||||
}) => {
|
||||
const run = async (toolId: string, toolArgs: Record<string, unknown>) => {
|
||||
if (!args.allowedToolIds.has(toolId)) throw new Error(`工具未被允许:${toolId}`);
|
||||
|
||||
if (toolId === "slash_run") {
|
||||
const text = String(toolArgs.text ?? "").trim();
|
||||
const command = String(toolArgs.command ?? "").trim();
|
||||
const params = isRecord(toolArgs.params) ? toolArgs.params : null;
|
||||
|
||||
const parsed: ParsedSlash =
|
||||
text && text.startsWith("/") ? parseSlash(text) : command === "new_doc"
|
||||
? {
|
||||
ok: true,
|
||||
command: "new_doc",
|
||||
params: {
|
||||
title: String(pick(params, "title") ?? "").trim(),
|
||||
parentId: pick(params, "parentId") ? String(pick(params, "parentId")) : null,
|
||||
workspaceId: pick(params, "workspaceId") ? String(pick(params, "workspaceId")) : null,
|
||||
},
|
||||
}
|
||||
: command === "rename_doc"
|
||||
? {
|
||||
ok: true,
|
||||
command: "rename_doc",
|
||||
params: {
|
||||
documentId: String(pick(params, "documentId") ?? "").trim(),
|
||||
title: String(pick(params, "title") ?? "").trim(),
|
||||
},
|
||||
}
|
||||
: { ok: false, error: "缺少 text(以 / 开头)或 command" };
|
||||
|
||||
if (!parsed.ok) throw new Error(parsed.error);
|
||||
|
||||
if (parsed.command === "new_doc") {
|
||||
const title = String(parsed.params.title ?? "").trim();
|
||||
if (!title) throw new Error("缺少标题");
|
||||
const parentId = parsed.params.parentId ? String(parsed.params.parentId) : null;
|
||||
const workspaceIdFromParams = parsed.params.workspaceId ? String(parsed.params.workspaceId) : null;
|
||||
const workspaceId =
|
||||
workspaceIdFromParams ||
|
||||
(args.ctx.currentDocumentId ? await inferWorkspaceIdFromDoc(args.supabase, args.ctx.currentDocumentId) : null) ||
|
||||
(await loadWorkspaceIds(args.supabase, args.ctx.userId))[0] ||
|
||||
null;
|
||||
if (!workspaceId) throw new Error("无法推断 workspaceId(请在 params.workspaceId 指定)");
|
||||
|
||||
const payload = {
|
||||
workspace_id: workspaceId,
|
||||
user_id: args.ctx.userId,
|
||||
parent_id: parentId,
|
||||
title,
|
||||
content: [] as unknown[],
|
||||
raw_text: "",
|
||||
};
|
||||
const { data, error } = await args.supabase.from("documents").insert(payload).select("id,workspace_id,parent_id,title,created_at,updated_at").single();
|
||||
if (error) {
|
||||
throw new Error(isRecord(error) && typeof error.message === "string" ? error.message : "创建文档失败");
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
command: "new_doc",
|
||||
document: {
|
||||
id: String(pick(data, "id") ?? ""),
|
||||
title: String(pick(data, "title") ?? ""),
|
||||
workspaceId: String(pick(data, "workspace_id") ?? ""),
|
||||
parentId: pick(data, "parent_id") ? String(pick(data, "parent_id")) : null,
|
||||
createdAt: pick(data, "created_at") ?? null,
|
||||
updatedAt: pick(data, "updated_at") ?? null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (parsed.command === "rename_doc") {
|
||||
const documentId = String(parsed.params.documentId ?? "").trim();
|
||||
const title = String(parsed.params.title ?? "").trim();
|
||||
if (!documentId || !title) throw new Error("缺少 documentId 或 title");
|
||||
|
||||
const { data, error } = await args.supabase
|
||||
.from("documents")
|
||||
.update({ title })
|
||||
.eq("id", documentId)
|
||||
.select("id,workspace_id,parent_id,title,updated_at")
|
||||
.single();
|
||||
if (error) {
|
||||
throw new Error(isRecord(error) && typeof error.message === "string" ? error.message : "重命名失败");
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
command: "rename_doc",
|
||||
document: {
|
||||
id: String(pick(data, "id") ?? ""),
|
||||
title: String(pick(data, "title") ?? ""),
|
||||
workspaceId: String(pick(data, "workspace_id") ?? ""),
|
||||
parentId: pick(data, "parent_id") ? String(pick(data, "parent_id")) : null,
|
||||
updatedAt: pick(data, "updated_at") ?? null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(`未实现命令:${(parsed as any).command}`);
|
||||
}
|
||||
|
||||
throw new Error(`未知工具:${toolId}`);
|
||||
};
|
||||
|
||||
return { run };
|
||||
};
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import type { AiAgentTool, AiAgentToolSet, ToolPermissions } from "./types";
|
||||
|
||||
export type ToolRegistry = {
|
||||
toolsById: Map<string, AiAgentTool>;
|
||||
toolSetsById: Map<string, AiAgentToolSet>;
|
||||
permissions: ToolPermissions;
|
||||
};
|
||||
|
||||
export const createToolRegistry = (args: {
|
||||
tools: AiAgentTool[];
|
||||
toolSets: AiAgentToolSet[];
|
||||
permissions?: Partial<ToolPermissions>;
|
||||
}): ToolRegistry => {
|
||||
const toolsById = new Map<string, AiAgentTool>();
|
||||
for (const t of args.tools) toolsById.set(t.id, t);
|
||||
|
||||
const toolSetsById = new Map<string, AiAgentToolSet>();
|
||||
for (const s of args.toolSets) toolSetsById.set(s.id, s);
|
||||
|
||||
const permissions: ToolPermissions = {
|
||||
read: args.permissions?.read ?? "allow",
|
||||
write: args.permissions?.write ?? "confirm",
|
||||
};
|
||||
|
||||
return { toolsById, toolSetsById, permissions };
|
||||
};
|
||||
|
||||
export const resolveAllowedToolIds = (args: {
|
||||
registry: ToolRegistry;
|
||||
mode: "auto" | "manual";
|
||||
toolSetIds?: string[];
|
||||
toolIds?: string[];
|
||||
}) => {
|
||||
const allowed = new Set<string>();
|
||||
|
||||
if (args.mode === "auto") {
|
||||
// v1:auto 默认启用 readonly;若显式传入 toolSets,则在这些集合内自动选择
|
||||
const toolSetIds = Array.isArray(args.toolSetIds) ? args.toolSetIds : [];
|
||||
if (toolSetIds.length) {
|
||||
for (const sid of toolSetIds) {
|
||||
const s = args.registry.toolSetsById.get(String(sid));
|
||||
s?.toolIds.forEach((id) => allowed.add(id));
|
||||
}
|
||||
if (allowed.size > 0) return allowed;
|
||||
}
|
||||
const readonly = args.registry.toolSetsById.get("toolset.readonly");
|
||||
readonly?.toolIds.forEach((id) => allowed.add(id));
|
||||
return allowed;
|
||||
}
|
||||
|
||||
const toolSetIds = Array.isArray(args.toolSetIds) ? args.toolSetIds : [];
|
||||
for (const sid of toolSetIds) {
|
||||
const s = args.registry.toolSetsById.get(String(sid));
|
||||
s?.toolIds.forEach((id) => allowed.add(id));
|
||||
}
|
||||
|
||||
const toolIds = Array.isArray(args.toolIds) ? args.toolIds : [];
|
||||
for (const tid of toolIds) {
|
||||
if (args.registry.toolsById.has(String(tid))) allowed.add(String(tid));
|
||||
}
|
||||
|
||||
// 手动模式但用户未选:至少保留 readonly,避免完全不可用
|
||||
if (allowed.size === 0) {
|
||||
const readonly = args.registry.toolSetsById.get("toolset.readonly");
|
||||
readonly?.toolIds.forEach((id) => allowed.add(id));
|
||||
}
|
||||
|
||||
return allowed;
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
export type AiAgentToolSource = "builtin" | "mcp" | "custom";
|
||||
|
||||
export type AiAgentTool = {
|
||||
id: string;
|
||||
displayName: string;
|
||||
modelDescription: string;
|
||||
// v1 先用“文字 schema”,后续再引入 zod/jsonschema
|
||||
inputSchemaText: string;
|
||||
source: AiAgentToolSource;
|
||||
requiresConfirmation: boolean;
|
||||
isWriteTool: boolean;
|
||||
};
|
||||
|
||||
export type AiAgentToolSet = {
|
||||
id: string;
|
||||
displayName: string;
|
||||
toolIds: string[];
|
||||
};
|
||||
|
||||
export type ToolPermissionMode = "allow" | "confirm" | "deny";
|
||||
|
||||
export type ToolPermissions = {
|
||||
// 默认:读允许、写确认(和 design 文档一致)
|
||||
read: ToolPermissionMode;
|
||||
write: ToolPermissionMode;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user