import { analyzeUserIntentV2 } from "../../goal/intent-analyzer.js";
import { buildIntentFrame } from "../../goal/intent-frame.js";
import { createDag } from "../../orchestration/dag.js";
import { buildDynamicNodes } from "../parallel/orchestrator.js";
export async function buildChatTurnDag(input) {
    const prompt = input.prompt.trim();
    if (!prompt) {
        throw new TypeError("chat turn prompt must not be empty");
    }
    const intent = input.intent ?? await analyzeUserIntentV2({
        rawPrompt: prompt,
        allowRuntimeRefine: input.allowRuntimeRefine ?? true,
    });
    const intentFrame = input.intentFrame ?? buildIntentFrame(prompt);
    if (isSingleNodeChatTurn(intent)) {
        return createDag({
            nodes: [buildSingleChatTurnNode({
                    prompt,
                    providerPolicy: input.providerPolicy,
                    providerModel: input.providerModel,
                    mcpAllowlist: input.mcpAllowlist,
                    skillNames: input.skillNames,
                    hookNames: input.hookNames,
                    toolNames: input.toolNames,
                })],
        });
    }
    return createDag({
        nodes: buildDynamicNodes({
            flow: "chat-turn",
            goal: prompt,
            intentFrame,
            startedAt: (input.now ?? (() => new Date()))().toISOString(),
            workerCount: Math.max(1, input.workerCount ?? intent.estimatedWorkers ?? 1),
            intent,
            providerPolicy: input.providerPolicy,
            executionStrategy: selectChatExecutionStrategy(input.executionStrategy, intent),
        }),
    });
}
function isSingleNodeChatTurn(intent) {
    return intent.complexity === "simple" && intent.isReadOnly && intent.parallelizable === false;
}
function selectChatExecutionStrategy(requested, intent) {
    return requested
        ?? intent.routingHints.preferredExecutionStrategy
        ?? (intent.parallelizable ? "parallel" : "sequential");
}
function buildSingleChatTurnNode(input) {
    const routing = {
        provider: input.providerPolicy,
        providerModel: input.providerModel,
        assignedProviderCapabilities: ["read", "review", "advisory"],
        contextBudget: "normal",
        readOnly: true,
        evidenceRequired: false,
        mcpServers: [...(input.mcpAllowlist ?? [])],
        skills: [...(input.skillNames ?? [])],
        hooks: [...(input.hookNames ?? [])],
        tools: [...(input.toolNames ?? [])],
        rationale: "simple read-only chat turn routed through the shared DAG harness",
    };
    return {
        id: "chat-turn",
        name: input.prompt,
        role: "coordinator",
        dependsOn: [],
        maxRetries: 1,
        outputs: [{ name: "chat response", gate: "none" }],
        routing,
    };
}
