// engine/ai-integration/agent-frameworks/crewai-exporter.ts — CrewAI config exporter (SPEC-274)
import { writeFileSync, mkdirSync, existsSync } from 'node:fs';
import { join } from 'node:path';
/**
 * Converts a Planu spec into a CrewAI YAML configuration object.
 * Produces 3 agents (spec-writer, implementer, reviewer) and 3 tasks.
 */
export function exportSpecToCrewai(spec) {
    const stackNote = spec.stack.length > 0 ? ` Stack: ${spec.stack.join(', ')}.` : '';
    return {
        agents: [
            {
                name: 'spec-writer',
                role: 'Spec Writer',
                goal: `Create detailed Planu specs from user requirements for: ${spec.title}`,
                backstory: 'You are an expert at translating business requirements into precise, testable acceptance criteria using the Spec Driven Development methodology.',
            },
            {
                name: 'implementer',
                role: 'Software Implementer',
                goal: `Implement the approved spec: ${spec.id} — ${spec.title}.${stackNote}`,
                backstory: 'You implement software according to approved specs. You never write code without a spec, and you always validate after implementation.',
            },
            {
                name: 'reviewer',
                role: 'Code Reviewer',
                goal: `Review the implementation of spec ${spec.id} against its acceptance criteria`,
                backstory: 'You perform thorough code reviews against spec criteria. You produce structured review reports and never modify code directly.',
            },
        ],
        tasks: [
            {
                name: 'create-spec',
                description: `Create a Planu spec for: ${spec.description}. Acceptance criteria: ${spec.acceptanceCriteria.join('; ')}.`,
                agent: 'spec-writer',
                expectedOutput: 'A complete Planu spec with id, title, description, and acceptance criteria.',
            },
            {
                name: 'implement',
                description: `Implement all acceptance criteria for spec ${spec.id}: ${spec.acceptanceCriteria.join('; ')}.`,
                agent: 'implementer',
                expectedOutput: 'Working code that satisfies all acceptance criteria, with passing tests.',
            },
            {
                name: 'validate',
                description: `Review implementation of spec ${spec.id} and confirm all criteria are met.`,
                agent: 'reviewer',
                expectedOutput: 'A review report listing covered and uncovered criteria with pass/fail status.',
            },
        ],
    };
}
/**
 * Serializes the CrewAI config to YAML and writes `planu-crew.yaml` to the project path.
 */
export function writeCrewaiConfig(projectPath, spec) {
    const config = exportSpecToCrewai(spec);
    /* v8 ignore start */
    if (!existsSync(projectPath)) {
        mkdirSync(projectPath, { recursive: true });
    }
    /* v8 ignore stop */
    const yaml = buildCrewaiYaml(config);
    writeFileSync(join(projectPath, 'planu-crew.yaml'), yaml, 'utf8');
}
function buildCrewaiYaml(config) {
    const lines = ['# planu-crew.yaml — Generated by Planu (SPEC-274)', ''];
    lines.push('agents:');
    for (const agent of config.agents) {
        lines.push(`  - name: ${agent.name}`);
        lines.push(`    role: ${agent.role}`);
        lines.push(`    goal: >-`);
        lines.push(`      ${agent.goal}`);
        lines.push(`    backstory: >-`);
        lines.push(`      ${agent.backstory}`);
    }
    lines.push('');
    lines.push('tasks:');
    for (const task of config.tasks) {
        lines.push(`  - name: ${task.name}`);
        lines.push(`    description: >-`);
        lines.push(`      ${task.description}`);
        lines.push(`    agent: ${task.agent}`);
        lines.push(`    expected_output: >-`);
        lines.push(`      ${task.expectedOutput}`);
    }
    return lines.join('\n');
}
//# sourceMappingURL=crewai-exporter.js.map