// engine/ai-integration/mastra/agent-scaffold.ts — Mastra agent scaffolding (SPEC-274)
import { writeFileSync, mkdirSync, existsSync } from 'node:fs';
import { join } from 'node:path';
/**
 * Returns a TypeScript template for a Mastra spec-implementer agent.
 */
export function generateMastraSpecAgent() {
    return [
        `// .mastra/spec-implementer.ts — Generated by Planu (SPEC-274)`,
        `// Spec-implementer agent: reads approved specs and implements them.`,
        ``,
        `import { Agent } from '@mastra/core/agent';`,
        `import { MCPClient } from '@mastra/mcp';`,
        ``,
        `const planuMcp = new MCPClient({`,
        `  servers: {`,
        `    planu: { command: 'npx @planu/cli start' },`,
        `  },`,
        `});`,
        ``,
        `export const specImplementer = new Agent({`,
        `  name: 'spec-implementer',`,
        `  instructions: [`,
        `    'You implement software specs following the Spec Driven Development workflow.',`,
        `    'Step 1: Call mcp__planu__list_specs to find specs with status=approved.',`,
        `    'Step 2: Call mcp__planu__summarize_spec to understand requirements.',`,
        `    'Step 3: Implement the code to satisfy all acceptance criteria.',`,
        `    'Step 4: Call mcp__planu__validate to verify compliance.',`,
        `    'Step 5: Call mcp__planu__update_status with status=done when validated.',`,
        `    'Never skip validation — it is the gate between implementation and done.',`,
        `  ].join(' '),`,
        `  tools: await planuMcp.getTools(),`,
        `  model: {`,
        `    provider: 'ANTHROPIC',`,
        `    name: 'claude-sonnet-4-5',`,
        `  },`,
        `});`,
    ].join('\n');
}
/**
 * Returns a TypeScript template for a Mastra SDD workflow.
 * The workflow covers: spec-approved → implement → validate → done.
 */
export function generateMastraWorkflow() {
    return [
        `// .mastra/planu-workflow.ts — Generated by Planu (SPEC-274)`,
        `// SDD workflow: spec-approved → implement → validate → done`,
        ``,
        `import { createWorkflow, createStep } from '@mastra/core/workflow';`,
        `import { specImplementer } from './spec-implementer.js';`,
        ``,
        `const readSpecStep = createStep({`,
        `  id: 'read-spec',`,
        `  description: 'Reads the approved spec and extracts acceptance criteria.',`,
        `  execute: async ({ context }) => {`,
        `    const specId = context.triggerData?.specId as string;`,
        `    return { specId, status: 'read' };`,
        `  },`,
        `});`,
        ``,
        `const implementStep = createStep({`,
        `  id: 'implement',`,
        `  description: 'Implements the spec using the planu-implementer agent.',`,
        `  execute: async ({ context }) => {`,
        `    const { specId } = context.getStepResult('read-spec') as { specId: string };`,
        `    await specImplementer.generate(\`Implement spec \${specId}\`);`,
        `    return { specId, status: 'implemented' };`,
        `  },`,
        `});`,
        ``,
        `const validateStep = createStep({`,
        `  id: 'validate',`,
        `  description: 'Validates implementation against spec criteria.',`,
        `  execute: async ({ context }) => {`,
        `    const { specId } = context.getStepResult('implement') as { specId: string };`,
        `    await specImplementer.generate(\`Validate spec \${specId} with mcp__planu__validate\`);`,
        `    return { specId, status: 'validated' };`,
        `  },`,
        `});`,
        ``,
        `export const planuWorkflow = createWorkflow({`,
        `  name: 'planu-sdd-workflow',`,
        `  triggerSchema: { specId: 'string' },`,
        `})`,
        `  .then(readSpecStep)`,
        `  .then(implementStep)`,
        `  .then(validateStep)`,
        `  .commit();`,
    ].join('\n');
}
/**
 * Creates `.mastra/` directory in the project and writes scaffold files.
 * Returns list of created file paths and the config path.
 */
export function writeMastraScaffold(projectPath) {
    const mastraDir = join(projectPath, '.mastra');
    /* v8 ignore start */
    if (!existsSync(mastraDir)) {
        mkdirSync(mastraDir, { recursive: true });
    }
    /* v8 ignore stop */
    const agentPath = join(mastraDir, 'spec-implementer.ts');
    const workflowPath = join(mastraDir, 'planu-workflow.ts');
    writeFileSync(agentPath, generateMastraSpecAgent(), 'utf8');
    writeFileSync(workflowPath, generateMastraWorkflow(), 'utf8');
    return {
        filesCreated: [agentPath, workflowPath],
        configPath: mastraDir,
    };
}
//# sourceMappingURL=agent-scaffold.js.map