// 遗留兼容：builtin 声明仍保留给历史兼容与文档对照，前端主链不再从这里继续扩展 Hermes 前置编排。
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,
  },
  {
    id: "asset_extract_outline",
    displayName: "提取附件大纲（PDF 优先）",
    modelDescription:
      "从附件（当前优先 PDF）提取结构化大纲（含层级与页码）。用于“文档驱动导图/可跳转引用”（不写入）。",
    inputSchemaText: `{ "assetId?": "string", "attachmentRef?": "string (可用附件 id/title/url 片段)" }`,
    source: "builtin",
    requiresConfirmation: false,
    isWriteTool: false,
  },
  {
    id: "asset_to_mindmap",
    displayName: "附件生成思维导图（写入）",
    modelDescription:
      "把附件大纲转为思维导图节点并落盘（PDF 页码会写入 refs，并尽量生成可跳转 hyperlink）。这是写工具，执行前必须确认。",
    inputSchemaText:
      `{ "mindmapId": "string", "parentUid?": "string (默认根节点)", "assetId?": "string", "attachmentRef?": "string", "maxItems?": "number (10~600, 默认120)", "reason?": "string" }`,
    source: "builtin",
    requiresConfirmation: true,
    isWriteTool: true,
  },
  {
    id: "oo_get_selection",
    displayName: "读取选区（OnlyOffice）",
    modelDescription:
      "读取 OnlyOffice 当前选区（用于增/删/改/查的基础能力：查/改前先读）。注意：该工具在客户端插件内执行。",
    inputSchemaText: `{ "format?": "\"text\"|\"html\" (默认 text)" }`,
    source: "builtin",
    requiresConfirmation: false,
    isWriteTool: false,
  },
  {
    id: "oo_replace_selection",
    displayName: "替换选区（OnlyOffice）",
    modelDescription:
      "用文本/HTML 替换 OnlyOffice 当前选区（基础改/删：删=传空字符串）。注意：该工具在客户端插件内执行。",
    inputSchemaText: `{ "text": "string", "format?": "\"text\"|\"html\" (默认 text)", "reason?": "string" }`,
    source: "builtin",
    requiresConfirmation: true,
    isWriteTool: true,
  },
  {
    id: "oo_insert_text",
    displayName: "插入文本（OnlyOffice）",
    modelDescription:
      "在 OnlyOffice 光标/选区位置插入文本（若存在选区通常会覆盖选区）。注意：该工具在客户端插件内执行。",
    inputSchemaText: `{ "text": "string", "reason?": "string" }`,
    source: "builtin",
    requiresConfirmation: true,
    isWriteTool: true,
  },
  {
    id: "oo_insert_html",
    displayName: "插入 HTML（OnlyOffice）",
    modelDescription:
      "在 OnlyOffice 光标/选区位置插入 HTML（用于保留简单格式）。注意：该工具在客户端插件内执行。",
    inputSchemaText: `{ "html": "string", "reason?": "string" }`,
    source: "builtin",
    requiresConfirmation: true,
    isWriteTool: true,
  },
  {
    id: "oo_insert_image",
    displayName: "插入图片（OnlyOffice）",
    modelDescription:
      "在 OnlyOffice 光标/选区位置插入图片（imageRef 可为附件 id 或 URL）。注意：该工具在客户端插件内执行。",
    inputSchemaText: `{ "imageRef": "string (attachmentId 或 URL)", "width?": "number", "height?": "number", "reason?": "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.onlyoffice_read",
    displayName: "OnlyOffice/附件结构化读取（M3）",
    toolIds: ["asset_extract_outline"],
  },
  {
    id: "toolset.onlyoffice_write",
    displayName: "OnlyOffice→思维导图（写入，M3）",
    toolIds: ["asset_to_mindmap"],
  },
  {
    id: "toolset.onlyoffice_editor",
    displayName: "OnlyOffice 编辑器（选区读写）",
    toolIds: [
      "oo_get_selection",
      "oo_replace_selection",
      "oo_insert_text",
      "oo_insert_html",
      "oo_insert_image",
    ],
  },
  {
    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",
    ],
  },
];

export type BuiltinRustCutoverStatus = "rust" | "mixed" | "ts" | "transport";

export type BuiltinRustCutoverBinding = {
  rustToolsetId: string;
  rustToolName: string | null;
  status: BuiltinRustCutoverStatus;
  note: string;
};

/**
 * 当前只把已经进入 Rust runtime 的 builtin tool 做成代码侧绑定。
 * 完整的一一对应矩阵见 `design/ai-tool-cutover-matrix.md`。
 */
export const builtinRustCutoverBindings: Record<string, BuiltinRustCutoverBinding> = {
  docs_search: {
    rustToolsetId: "toolset.docs_read",
    rustToolName: "docs_search",
    status: "rust",
    note: "跨页文档搜索已切到 Rust runtime，TS 仅负责拉取搜索数据集 transport。",
  },
  docs_read: {
    rustToolsetId: "toolset.docs_read",
    rustToolName: "docs_read",
    status: "rust",
    note: "跨页文档读取结果已由 Rust runtime 统一裁剪与归一化，TS 仅负责读取目标文档 transport。",
  },
  search_web: {
    rustToolsetId: "toolset.readonly",
    rustToolName: "search_web",
    status: "rust",
    note: "联网检索已切到 Rust runtime，TS 仅保留 route transport 壳。",
  },
  image_read: {
    rustToolsetId: "toolset.media_read",
    rustToolName: "image_read",
    status: "rust",
    note: "图片/附件 OCR 结果归一化已切到 Rust runtime，TS 只负责最小 transport 取数。",
  },
  slash_run: {
    rustToolsetId: "toolset.slash_write",
    rustToolName: "slash_run",
    status: "rust",
    note: "斜杠命令解析已切到 Rust runtime，TS 仅保留创建/重命名 transport 写壳。",
  },
} as const;

export const builtinRustRuntimeToolIds = new Set<string>(
  Object.entries(builtinRustCutoverBindings)
    .filter(([, binding]) => binding.status === "rust" || binding.status === "mixed")
    .map(([toolId]) => toolId),
);
