// ── 工具组: sf_knowledge_audit, sf_knowledge_add, sf_knowledge_update ──
// 从 tools.ts 第 4476-4565 行提取。
import { z } from "zod";
import { createToolRegistrar } from "../middleware.js";
import { lazyKnowledge } from "../lazy_loaders.js";
export async function registerKnowledgeTools(ctx) {
    const { registerSafeTool } = createToolRegistrar(ctx);
    const { config, knowledgeIndex } = ctx;
    // ── sf_knowledge_audit: 知识库健康审计 ──
    registerSafeTool("sf_knowledge_audit", "审计知识库：识别过时条目、重复触发词、格式缺失、覆盖缺口。定期执行或手动触发", {}, async () => {
        const result = await (await lazyKnowledge()).auditKnowledge(knowledgeIndex, config);
        return {
            result: result,
        };
    });
    // ── sf_knowledge_add: 新增知识条目（默认保存为草稿） ──
    const KnowledgeAddSchema = {
        title: z.string().describe("知识条目标题（英文 kebab-case，如 payment-rules）"),
        type: z.enum(["pattern", "procedure", "pipeline_procedure", "domain", "acceptance_template", "review_rule"]).describe("知识类型"),
        scope: z.array(z.string()).describe("适用范围（如 [backend]、[frontend]、[backend, frontend]）"),
        when_triggers: z.string().describe("触发关键词（逗号分隔）"),
        decision_rules: z.string().optional().describe("决策规则（每行一条）"),
        save_to_drafts: z.boolean().optional().describe("是否保存为草稿（默认 true，需人工 review 后移入正式目录）"),
        auto_enrich: z.boolean().optional().describe("是否返回行业最佳实践探索指引（默认 true，宿主 AI 将自动执行探索并填充内容）"),
        confirm: z.boolean().optional().describe("显式确认写入知识库，必须为 true"),
        authorized: z.boolean().optional().describe("confirm 的兼容别名"),
    };
    registerSafeTool("sf_knowledge_add", "新增知识条目。默认保存为草稿到 .soloforge/knowledge/drafts/，人工确认后移入正式目录", KnowledgeAddSchema, async (args) => {
        const result = await (await lazyKnowledge()).addKnowledge({
            title: args.title,
            type: args.type,
            scope: args.scope,
            when_triggers: args.when_triggers,
            decision_rules: args.decision_rules,
            save_to_drafts: args.save_to_drafts,
            auto_enrich: args.auto_enrich,
        }, config);
        return {
            result: result,
        };
    });
    // ── sf_knowledge_update: 更新已有知识条目（自动创建备份） ──
    const KnowledgeUpdateSchema = {
        entry_name: z.string().describe("要更新的知识条目名称（如 payment-rules）"),
        when_triggers: z.string().optional().describe("新的触发关键词（替换现有）"),
        add_decision_rules: z.array(z.string()).optional().describe("追加的决策规则"),
        add_acceptance_criteria: z.array(z.string()).optional().describe("追加的验收项"),
        status: z.enum(["active", "deprecated"]).optional().describe("更新条目状态"),
        confirm: z.boolean().optional().describe("显式确认写入知识库，必须为 true"),
        authorized: z.boolean().optional().describe("confirm 的兼容别名"),
    };
    registerSafeTool("sf_knowledge_update", "更新已有知识条目：追加规则、更新触发词、标记废弃。自动创建备份", KnowledgeUpdateSchema, async (args) => {
        const result = await (await lazyKnowledge()).updateKnowledge({
            entry_name: args.entry_name,
            updates: {
                when_triggers: args.when_triggers,
                add_decision_rules: args.add_decision_rules,
                add_acceptance_criteria: args.add_acceptance_criteria,
                status: args.status,
            },
        }, knowledgeIndex, config);
        // 重载知识索引
        if (result.success) {
            await knowledgeIndex.reload();
        }
        return {
            result: result,
        };
    });
}
//# sourceMappingURL=knowledge.js.map