// ── sf_classify 工具处理器 ──
// 从 classify_expand.ts 第 49-113 行提取。
import { createToolRegistrar } from "../middleware.js";
import { ClassifySchema } from "../schemas.js";
import { lazyClassifier, lazyIntentRouter, lazyNavigation, } from "../lazy_loaders.js";
export async function registerClassifyTool(ctx) {
    const { registerSafeTool } = createToolRegistrar(ctx);
    const { taskContext, config, projectPath } = ctx;
    registerSafeTool("sf_classify", "分析开发者意图，返回任务类型、风险、复杂度和执行策略", ClassifySchema, async (args) => {
        const asksForNavigation = /(?:下一步|继续(?:做|执行|开发)?|现在(?:可以|能)(?:开始)?(?:写代码|编码|开发|测试|交付)|接下来(?:做什么|该做什么)|该(?:做什么|怎么继续))/i.test(args.intent);
        if (asksForNavigation) {
            const navigation = await (await lazyNavigation()).planNextAction(projectPath);
            return {
                result: {
                    status: "navigation_required",
                    reason_zh: "检测到阶段推进意图，必须先依据 SoloForge 状态规划下一步",
                    navigation,
                },
            };
        }
        // 检查是否存在进行中的任务
        const existing = await taskContext.getCurrentTask();
        if (existing && !["done", "failed"].includes(existing.status)) {
            const navigation = await (await lazyNavigation()).planNextAction(projectPath);
            if (navigation.stale_task?.is_stale) {
                return {
                    result: {
                        status: "stale_task_requires_resolution",
                        error: "存在陈旧任务指针，必须先选择恢复、归档或开始新任务",
                        existing_task_id: existing.task_id,
                        existing_status: existing.status,
                        navigation,
                        hint: "先调用 sf_navigation 查看可执行处理步骤，不得绕过 SoloForge 状态直接分析或编码",
                    },
                };
            }
            return {
                result: {
                    error: "存在未完成的任务",
                    warning: "存在未完成的任务",
                    existing_task_id: existing.task_id,
                    existing_status: existing.status,
                    existing_intent: existing.intent,
                    hint: "调用 sf_status action=cancel 取消旧任务，或继续完成它",
                },
            };
        }
        // 创建任务上下文
        const taskCtx = await taskContext.create(args.intent, config.product_profile);
        const input = { intent: args.intent, project_path: projectPath, task_id: taskCtx.task_id };
        const classification = (await lazyClassifier()).classify(input);
        // 存储分类结果
        await taskContext.setClassification(taskCtx.task_id, classification);
        if (!(await lazyIntentRouter()).shouldEnterSoloForge(classification?.route_decision)) {
            await taskContext.updateStatus(taskCtx.task_id, "done");
        }
        return {
            result: {
                task_id: taskCtx.task_id,
                ...classification,
            },
        };
    });
}
//# sourceMappingURL=classify_handler.js.map