/** * kingdee-tools.ts — 金蝶真领域工具注册 * * 只保留 KCode 没有原生替代的金蝶真领域工具。 * 已转 KCode 原生 + 引导的(kd_search→KCode search/read、kd_table→KCode read tables.json、 * kd_cosmic_config/kd_cosmic_metadata→KCode bash mvn、kd_check/kd_debug/ * kd_ksql_lint/kd_sdk_signature/kd_doc_read/kd_find_file/kd_list_dir/kd_anchor_read * →规则 .md 或 KCode 原生工具)不再在此注册;引导见 kd-delivery-rules.md 与 ok-cosmic SKILL.md。 */ import { z } from "zod/v4"; import type { ExtensionAPI, ToolDefinition } from "@kingdee-kcode/coding-agent"; import { saveToken, loadToken, queryCosmicQa, formatCosmicQaResult, TOKEN_NOT_FOUND_MSG, } from "../src/cosmic-qa/api"; import { COMMUNITY_PRODUCT_KEYS, resolveCommunityProduct, saveCommunityProductConfig, type CommunityProductKey, } from "../src/cosmic-qa/community-product"; /** * defineTool:保留 ToolDefinition 上下文类型,使 registerTool 的 TParams/TDetails 正确推导。 * KCode 不再从主入口导出 defineTool,这里按 ToolDefinition 约束透传。 */ function defineTool( def: ToolDefinition, ): ToolDefinition { return def; } // ── kd_cosmic_qa:金蝶云社区问答 ────────────────────────────────────────── const kdCosmicQaTool = defineTool({ name: "kd_cosmic_qa", label: "KD Cosmic 问答", description: [ "查询金蝶云社区/API 知识库,获取官方解答。", "", "产品目录选择规则:", "- 企业版项目:对应金蝶AI星空企业版/标准版,不要询问用户,直接使用 communityProduct: \"enterprise\"。", "- Cosmic Java 项目:调用前若未配置,必须用 KCode question/选择交互询问用户,选项只给中文产品名:金蝶AI苍穹、金蝶AI星瀚、金蝶AI套件;不要询问 productId。", "- 用户选择后传入对应 communityProduct:金蝶AI苍穹=cosmic,金蝶AI星瀚=xinghan,金蝶AI套件=flagship。工具会保存选择,下次可省略。", "", "调用示例:", ' kd_cosmic_qa({ query: "操作服务插件 BeforeExecuteOperation 怎么写", communityProduct: "cosmic" })', ' kd_cosmic_qa({ query: "KSQL 关联子查询", saveToken: "kdt_xxx", communityProduct: "xinghan" })', ].join("\n"), parameters: z.object({ query: z.string().describe("搜索关键词。"), communityProduct: z.enum(COMMUNITY_PRODUCT_KEYS).optional().describe("金蝶云社区产品目录。企业版用 enterprise;Cosmic 项目只允许在询问用户后选择 cosmic(金蝶AI苍穹)、xinghan(金蝶AI星瀚)、flagship(金蝶AI套件)。不要向用户询问 productId。"), /** @deprecated 兼容旧调用;新代码一律用 communityProduct。 */ productId: z.number().int().positive().optional().describe("兼容旧调用的社区 productId。优先使用 communityProduct;除非用户显式给出数字 ID,否则不要使用。"), /** @deprecated 兼容旧调用;新代码让 SDK 自动从 communityProduct 推导。 */ productLineId: z.string().optional().describe("兼容旧调用的社区 productLineId;使用 communityProduct 时自动设置。"), saveToken: z.string().optional().describe("首次使用时传入 PAT Token(kdt_xxx)保存;后续会话无需重复提供。"), useDeepThink: z.boolean().optional().describe("启用深度思考模式。"), }), async execute(_toolCallId, params, _signal, _onUpdate, ctx) { // 优先保存传入的 token if (params.saveToken) { saveToken(params.saveToken); } const productSelection = params.productId ? { ok: true as const, config: { key: "enterprise" as CommunityProductKey, label: "自定义社区产品", productId: params.productId, productLineId: params.productLineId ?? "35", source: "user-selection" as const, }, shouldPersist: false, } : resolveCommunityProduct(ctx.cwd, params.communityProduct); if (!productSelection.ok) { return { content: [{ type: "text", text: productSelection.message, }], isError: true, }; } if (productSelection.shouldPersist) { saveCommunityProductConfig(ctx.cwd, productSelection.config.key); } const tokenResult = loadToken(); if ("error" in tokenResult) { return { content: [{ type: "text", text: TOKEN_NOT_FOUND_MSG }], }; } try { const result = await queryCosmicQa(params.query, productSelection.config.productId, { useDeepThink: params.useDeepThink, productLineId: productSelection.config.productLineId, }); return { content: [{ type: "text", text: [ `社区产品目录:${productSelection.config.label}(productId=${productSelection.config.productId}, productLineId=${productSelection.config.productLineId})`, "", formatCosmicQaResult(result, params.query), ].join("\n"), }], }; } catch (e) { return { content: [{ type: "text", text: `查询失败:${e}` }], isError: true, }; } }, }); // ── 注册 ────────────────────────────────────────────────────────────────── export default function (pi: ExtensionAPI) { pi.registerTool(kdCosmicQaTool); }