/**
 * Save a running worker (or define a new one) as a persistent specialist.
 *
 * Specialists survive cleanup and are auto-spawned on agent_serve.
 */
import { AgentRegistryManager } from '../core/agent-registry.js';
import { saveSpecialist } from '../utils/specialist-utils.js';
import { loadPersona } from '../utils/persona-utils.js';
export const agentSpecialistSaveTool = {
    name: 'agent_specialist_save',
    label: 'Save Specialist',
    description: 'Save a running worker as a persistent specialist, or define a new specialist. Specialists survive cleanup and auto-spawn on agent_serve.',
    parameters: {
        type: 'object',
        properties: {
            agent_name: {
                type: 'string',
                description: 'Name of existing worker to save as specialist',
            },
            name: {
                type: 'string',
                description: 'Specialist name (if creating new without existing worker)',
            },
            role: {
                type: 'string',
                description: 'Specialist role description',
            },
            tone: {
                type: 'string',
                description: 'Communication tone',
            },
            strengths: {
                type: 'array',
                items: { type: 'string' },
                description: 'Key strengths and capabilities',
            },
            goals: {
                type: 'array',
                items: { type: 'string' },
                description: 'Goals or objectives',
            },
            allowed_tools: {
                type: 'array',
                items: { type: 'string' },
                description: 'Allowed tools (empty = all)',
            },
            setup: {
                type: 'string',
                description: 'Setup command to run after spawn (e.g. "pip install nltk spacy")',
            },
        },
        required: [],
    },
    async execute(_toolCallId, params) {
        const agentName = params.agent_name || params.agentName;
        const newName = params.name;
        if (!agentName && !newName) {
            return {
                content: [{ type: 'text', text: '❌ Provide agent_name (existing worker) or name (new specialist)' }],
                isError: true,
            };
        }
        let def;
        if (agentName) {
            // Save from existing worker
            const registry = new AgentRegistryManager();
            const agent = registry.getAgentByName(agentName);
            if (!agent) {
                return {
                    content: [{ type: 'text', text: `❌ Worker "${agentName}" not found in registry` }],
                    isError: true,
                };
            }
            // Load persona if available
            const persona = agent.workspace ? loadPersona(agent.workspace) : null;
            const now = Date.now();
            def = {
                name: agentName,
                persona: {
                    name: persona?.name || agentName,
                    role: params.role || persona?.role || 'Specialist worker',
                    tone: params.tone || persona?.tone,
                    strengths: params.strengths || persona?.strengths || [],
                    goals: params.goals || persona?.goals || [],
                },
                allowedTools: params.allowed_tools || agent.capabilities || [],
                setup: params.setup,
                created: now,
                updated: now,
            };
            // Mark agent as persistent in registry
            agent.persistent = true;
            registry.upsertAgent(agent);
            registry.save();
        }
        else {
            // Create new specialist definition
            const now = Date.now();
            def = {
                name: newName,
                persona: {
                    name: newName,
                    role: params.role || 'Specialist worker',
                    tone: params.tone,
                    strengths: params.strengths || [],
                    goals: params.goals || [],
                },
                allowedTools: params.allowed_tools || [],
                setup: params.setup,
                created: now,
                updated: now,
            };
        }
        saveSpecialist(def);
        const lines = [
            `✅ Specialist "${def.name}" saved`,
            '',
            `**Role**: ${def.persona.role}`,
        ];
        if (def.persona.strengths && def.persona.strengths.length > 0) {
            lines.push(`**Strengths**: ${def.persona.strengths.join(', ')}`);
        }
        if (def.allowedTools.length > 0) {
            lines.push(`**Tools**: ${def.allowedTools.join(', ')}`);
        }
        else {
            lines.push(`**Tools**: all`);
        }
        if (def.setup) {
            lines.push(`**Setup**: ${def.setup}`);
        }
        lines.push('', 'This specialist will auto-spawn on agent_serve and survive cleanup.');
        return {
            content: [{ type: 'text', text: lines.join('\n') }],
            details: { specialist: def },
        };
    },
};
//# sourceMappingURL=agentSpecialistSave.js.map