// tools/steering-handler.ts — SPEC-434: Steering file tool handlers
import { generateSteeringFiles, writeSteeringFiles, formatGenerationReport, } from '../engine/steering/steering-generator.js';
import { CORE_AGENTS_MD_TOOLS, loadHostRegistry, filterToolsByHost, formatToolsForAgentsMd, } from '../engine/host-tool-filter.js';
import { getSteeringResult, saveSteeringResult } from '../storage/steering-store.js';
/** Build the tools markdown section for host-aware AGENTS.md generation. */
async function buildToolsMarkdown(hostId) {
    const registry = await loadHostRegistry(hostId);
    const result = filterToolsByHost(CORE_AGENTS_MD_TOOLS, hostId, registry);
    return formatToolsForAgentsMd(result, { prefix: 'mcp__planu__', tableFormat: true });
}
/** Handle generate_steering_file tool call. */
export async function handleGenerateSteeringFile(input) {
    const { projectPath, target, stack, specCount, conventions } = input;
    const context = {
        stack,
        specCount,
        conventions,
    };
    // SPEC-972: When generating for Codex, filter tools by host capability
    if (target === 'codex' || target === 'all') {
        context.host = 'codex';
        context.toolsMarkdown = await buildToolsMarkdown('codex');
    }
    const files = generateSteeringFiles(context, [target]);
    await writeSteeringFiles(files, projectPath);
    const result = {
        target,
        filesGenerated: files.length,
        files,
        generatedAt: new Date().toISOString(),
    };
    await saveSteeringResult(projectPath, result);
    return {
        content: [{ type: 'text', text: formatGenerationReport(result) }],
    };
}
/** Handle list_steering_files tool call. */
export async function handleListSteeringFiles(input) {
    const { projectPath } = input;
    const result = await getSteeringResult(projectPath);
    if (result === null) {
        return {
            content: [
                {
                    type: 'text',
                    text: 'No steering files generated yet. Run generate_steering_file.',
                },
            ],
        };
    }
    const fileList = result.files.map((f) => `- ${f.outputPath}`).join('\n');
    return {
        content: [
            {
                type: 'text',
                text: `## Previously Generated Steering Files\n\n**Target**: ${result.target}\n**Generated**: ${result.generatedAt}\n\n${fileList}`,
            },
        ],
    };
}
//# sourceMappingURL=steering-handler.js.map