"use strict";
/**
 * Generate a decomposition plan from a specification using an LLM.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePlan = generatePlan;
const llm_1 = require("../../../../services/llm");
const defaults_1 = require("../../../../modules/defaults");
const prompts_1 = require("../prompts");
async function generatePlan(specification) {
    const response = await (0, llm_1.callLLM)({
        model: defaults_1.LLM_MODEL_PRIMARY,
        max_tokens: 4096,
        temperature: 0,
        messages: [
            { role: 'system', content: prompts_1.PLANNER_SYSTEM_PROMPT },
            { role: 'user', content: specification },
        ],
    });
    const raw = response.content || '{}';
    const cleaned = raw.replace(/^```(?:json)?\s*/m, '').replace(/\s*```$/m, '').trim();
    const parsed = JSON.parse(cleaned);
    if (!Array.isArray(parsed.workflows) || parsed.workflows.length === 0) {
        throw new Error('Planner produced empty workflow list');
    }
    // Sort by build_order (leaf-first)
    parsed.workflows.sort((a, b) => a.build_order - b.build_order);
    // Extract unique namespaces
    const namespaces = [...new Set(parsed.workflows.map(w => w.namespace))];
    return {
        plan_name: parsed.plan_name || 'unnamed-plan',
        plan_description: parsed.plan_description || '',
        workflows: parsed.workflows.map(w => ({
            name: w.name,
            description: w.description,
            namespace: w.namespace,
            role: w.role || 'leaf',
            dependencies: w.dependencies || [],
            build_order: w.build_order ?? 0,
            io_contract: w.io_contract || { input_schema: {}, output_schema: {} },
        })),
    };
}
