// tools/create-spec/agent-spec-adapter-b.ts — Agent Contract, Memory, Inter-Agent, and HU section
// SPEC-016a: Criteria 16, 17, 18, and HU section builder.
import { generateAgentSpec, generateAgentConstitution, generateBeginnerAgentGuide, generateExpertAgentSpec, buildDefaultAgentTools, } from './agent-spec-adapter.js';
// === Agent Contract (criterion 16) ===
export function generateAgentContract(title, description) {
    const tools = buildDefaultAgentTools(description);
    const signatures = tools.map((t) => ({
        name: t.name,
        description: t.description,
        params: Object.fromEntries(Object.entries(t.inputSchema).map(([k, v]) => [k, v.type ?? 'string'])),
        output: Object.keys(t.outputSchema).join(', '),
    }));
    return {
        naturalLanguageObjective: `${title}: ${description}`,
        toolSignatures: signatures,
        terminationCriterion: 'Task completed successfully OR escalation triggered OR max_iterations reached',
        maxIterations: 10,
        toolFailureBehavior: 'Retry once with exponential backoff; if still failing, return error response',
    };
}
// === Memory Contract (criterion 17) ===
export function generateMemoryContract(description) {
    const lower = description.toLowerCase();
    const needsVector = lower.includes('document') || lower.includes('knowledge') || lower.includes('rag');
    return {
        memoryType: needsVector ? 'vector-store' : 'in-context',
        persistedFields: ['user-preferences', 'session-history', 'learned-context'],
        ephemeralFields: ['current-tool-output', 'intermediate-reasoning', 'scratch-pad'],
        retrievalCriteria: 'Semantic similarity threshold > 0.8 for vector-store; last 20 turns for in-context',
        corruptionBehavior: 'On corrupt memory: clear ephemeral fields, reload from persistent store, notify user of context reset',
    };
}
// === Inter-agent Communication Contract (criterion 18) ===
export function generateInterAgentContract() {
    return {
        handoffMessageSchema: {
            from: 'string',
            to: 'string',
            task: 'string',
            context: 'object',
            priority: "'low' | 'medium' | 'high'",
            timeout_ms: 'number',
        },
        callerPermissions: {
            coordinator: ['all-agents'],
            analyst: ['implementor', 'reviewer'],
            implementor: [],
            reviewer: ['coordinator'],
        },
        conflictResolution: 'Coordinator agent has final authority. On conflict, pause execution and request human input.',
        criticalSubagentFailureBehavior: 'Mark task as failed, rollback any partial state, escalate to coordinator with failure report.',
    };
}
// === HU.md agent section builder ===
export function buildAgentHuSection(title, description, knowledge, experienceLevel) {
    const spec = generateAgentSpec(title, description);
    const constitution = generateAgentConstitution();
    const contract = generateAgentContract(title, description);
    const memory = generateMemoryContract(description);
    const lines = ['', '## AI Agent Specification', ''];
    if (experienceLevel === 'beginner') {
        lines.push(generateBeginnerAgentGuide(title, description));
        lines.push('');
    }
    else {
        lines.push(generateExpertAgentSpec(title, description, knowledge));
        lines.push('');
    }
    lines.push('### Agent Goal');
    lines.push(spec.goal);
    lines.push('');
    lines.push('### Triggers');
    for (const trigger of spec.triggers) {
        lines.push(`- ${trigger}`);
    }
    lines.push('');
    lines.push('### Tools');
    for (const tool of spec.tools) {
        lines.push(`- **${tool.name}**: ${tool.description}`);
        if (tool.sideEffects.length > 0) {
            lines.push(`  - Side effects: ${tool.sideEffects.join(', ')}`);
        }
    }
    lines.push('');
    lines.push('### Agent Contract');
    lines.push(`- **Objective**: ${contract.naturalLanguageObjective}`);
    lines.push(`- **Max iterations**: ${contract.maxIterations}`);
    lines.push(`- **Termination**: ${contract.terminationCriterion}`);
    lines.push(`- **Tool failure**: ${contract.toolFailureBehavior}`);
    lines.push('');
    lines.push('### Memory Contract');
    lines.push(`- **Memory type**: ${memory.memoryType}`);
    lines.push(`- **Persisted fields**: ${memory.persistedFields.join(', ')}`);
    lines.push(`- **Retrieval**: ${memory.retrievalCriteria}`);
    lines.push(`- **Corruption behavior**: ${memory.corruptionBehavior}`);
    lines.push('');
    lines.push('### Agent Constitution');
    lines.push('**Accuracy:**');
    for (const a of constitution.accuracy) {
        lines.push(`- ${a}`);
    }
    lines.push('**Safety:**');
    for (const s of constitution.safety) {
        lines.push(`- ${s}`);
    }
    lines.push('**Privacy:**');
    for (const p of constitution.privacy) {
        lines.push(`- ${p}`);
    }
    lines.push('**Escalation:**');
    for (const e of constitution.escalation) {
        lines.push(`- ${e}`);
    }
    lines.push('');
    lines.push('### Error Handling');
    lines.push(spec.errorHandling);
    return lines.join('\n');
}
//# sourceMappingURL=agent-spec-adapter-b.js.map