// Planu — Agent Generator Engine
// Generates sub-agent definitions for various multi-agent frameworks.
// Orchestrator: delegates to agent-generator/ sub-modules.
import { readFile } from 'node:fs/promises';
import { buildSystemPrompt, buildCoordinatorPrompt, inferModel, inferTools, inferMaxTurns, inferHandoffConditions, } from './agent-generator/builders.js';
import { generateFrameworkFiles, buildInstallInstructions, } from './agent-generator/framework-files.js';
// Re-export sub-module functions so existing callers via this module still work.
export { buildSystemPrompt, buildCoordinatorPrompt, inferModel, inferTools, inferMaxTurns, inferHandoffConditions, generateFrameworkFiles, buildInstallInstructions, };
// === Framework Config ===
let frameworksCache = null;
async function loadFrameworks() {
    if (frameworksCache) {
        return frameworksCache;
    }
    try {
        const configPath = new URL('../config/agent-platforms.json', import.meta.url);
        const content = await readFile(configPath, 'utf-8');
        frameworksCache = JSON.parse(content);
        return frameworksCache;
    }
    catch {
        /* v8 ignore next -- defensive catch for missing config file */
        return { subAgentFrameworks: {} };
    }
}
// === Public API ===
/**
 * Generate a sub-agent definition for a given multi-agent framework.
 */
export async function generateSubAgent(framework, agentName, role, capabilities, knowledge, tools = [], systemPrompt) {
    const config = await getFrameworkConfig(framework);
    const resolvedPrompt = systemPrompt ?? buildSystemPrompt(agentName, role, capabilities, knowledge);
    const resolvedTools = tools.length > 0 ? tools : inferTools(role, capabilities, knowledge);
    const agentDefinition = {
        name: agentName,
        role,
        model: inferModel(role, capabilities),
        systemPrompt: resolvedPrompt,
        tools: resolvedTools,
        maxTurns: inferMaxTurns(role),
        // SPEC-022: Only generate handoff conditions for frameworks that support them natively
        handoffConditions: config.supportsHandoffs ? inferHandoffConditions(role) : undefined,
    };
    const files = generateFrameworkFiles(framework, agentDefinition, knowledge, config);
    return {
        framework,
        files,
        agentDefinition,
        installInstructions: buildInstallInstructions(framework, config, files),
    };
}
/**
 * Generate a complete multi-agent system definition for a project.
 * Creates a coordinator agent plus specialized sub-agents.
 */
export async function generateAgentSystem(framework, knowledge) {
    const agents = [];
    // Coordinator agent
    agents.push(await generateSubAgent(framework, 'coordinator', 'orchestrator', ['task-routing', 'spec-management', 'workflow-control'], knowledge, ['sdd_init_project', 'sdd_create_spec', 'sdd_update_status', 'sdd_orchestrate'], buildCoordinatorPrompt(knowledge)));
    // Analyst agent
    agents.push(await generateSubAgent(framework, 'analyst', 'code-analyst', ['code-analysis', 'architecture-review', 'dependency-scanning'], knowledge, ['sdd_audit_code', 'sdd_detect_drift', 'sdd_reverse_engineer']));
    // Implementor agent
    agents.push(await generateSubAgent(framework, 'implementor', 'code-implementor', ['code-generation', 'file-editing', 'refactoring'], knowledge, ['sdd_validate_spec', 'sdd_check_versions']));
    // Reviewer agent
    agents.push(await generateSubAgent(framework, 'reviewer', 'code-reviewer', ['code-review', 'quality-check', 'security-audit'], knowledge, ['sdd_audit_code', 'sdd_generate_checklist', 'sdd_detect_drift']));
    return agents;
}
/**
 * Get framework configuration details.
 */
export async function getFrameworkConfig(framework) {
    const platforms = await loadFrameworks();
    return (platforms.subAgentFrameworks[framework] ?? {
        name: framework,
        languages: ['typescript'],
        docsUrl: '',
        supportsHandoffs: false,
        supportsMcp: false,
    });
}
//# sourceMappingURL=agent-generator.js.map