"use strict";
// create_custom_agent — let the running agent define new specialised
// sub-agents from observed patterns. Stored as `.harness/agents/<id>.md`.
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateCustomAgentTool = void 0;
const agentLoader_1 = require("../agents/agentLoader");
function agentsProjectDir() {
    return process.env.HARNESS_PROJECT_DIR && process.env.HARNESS_PROJECT_DIR.trim()
        ? process.env.HARNESS_PROJECT_DIR
        : process.cwd();
}
function asString(value) {
    return typeof value === 'string' && value.trim() ? value : undefined;
}
function asStringArray(value) {
    if (!Array.isArray(value))
        return undefined;
    return value.filter((item) => typeof item === 'string');
}
exports.CreateCustomAgentTool = {
    name: 'create_custom_agent',
    description: 'Define a new sub-agent and persist it under .harness/agents/. Future calls can route to this agent by id.',
    parameters: {
        type: 'object',
        properties: {
            id: { type: 'string', description: 'Stable id (alphanumeric, dashes, underscores)' },
            name: { type: 'string', description: 'Display name' },
            description: { type: 'string', description: 'One-line summary of what this agent does' },
            role: { type: 'string', description: 'Optional role label (e.g. researcher, developer, qa)' },
            preset: { type: 'string', description: 'Optional helper preset: explore, plan, search, general, verify, summarize' },
            model: { type: 'string', description: 'Optional preferred model id' },
            personality: { type: 'string' },
            goal: { type: 'string' },
            system_prompt: { type: 'string', description: 'The system prompt that defines this agent' },
            allowed_tools: { type: 'array', items: { type: 'string' }, description: 'Optional tool name allowlist' },
        },
        required: ['id', 'name', 'description', 'system_prompt'],
    },
    isReadOnly: false,
    async execute(input) {
        const id = asString(input.id);
        const name = asString(input.name);
        const description = asString(input.description);
        const systemPrompt = asString(input.system_prompt);
        if (!id || !name || !description || !systemPrompt) {
            const missing = ['id', 'name', 'description', 'system_prompt'].filter((k) => !asString(input[k]));
            const message = `Missing required fields: ${missing.join(', ')}`;
            return { success: false, output: message, error: message };
        }
        try {
            const filePath = await (0, agentLoader_1.writeCustomAgent)(agentsProjectDir(), {
                id,
                name,
                description,
                role: asString(input.role),
                model: asString(input.model),
                preset: asString(input.preset),
                personality: asString(input.personality),
                goal: asString(input.goal),
                systemPrompt,
                allowedTools: asStringArray(input.allowed_tools),
            });
            return { success: true, output: `Saved agent "${id}" to ${filePath}` };
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            return { success: false, output: message, error: message };
        }
    },
};
//# sourceMappingURL=agentTools.js.map