import fs from "node:fs";
import path from "node:path";
import { discoverAgents } from "../../agents/discovery.js";
import { loadAgent } from "../../agents/loader.js";
import { normalizeAgentName, renderAgentDocument, validateAgentDocument, } from "../../agents/manifest.js";
import { baseTools } from "../../tools/registry.js";
export class AgentService {
    projectPath;
    dataDir;
    constructor(projectPath, dataDir) {
        this.projectPath = projectPath;
        this.dataDir = dataDir;
    }
    list() {
        return discoverAgents(this.projectPath, this.dataDir);
    }
    load(name) {
        return loadAgent(this.projectPath, this.dataDir, name);
    }
    createProjectAgent(input) {
        const name = normalizeAgentName(input.name);
        if (!name)
            throw new Error("agent name is empty");
        const description = input.description.trim();
        if (!description)
            throw new Error("agent description is empty");
        const agentPath = path.join(this.projectPath, ".deepseekcode", "agents", `${name}.md`);
        if (fs.existsSync(agentPath) && !input.overwrite) {
            throw new Error(`agent already exists: ${name}`);
        }
        fs.mkdirSync(path.dirname(agentPath), { recursive: true });
        fs.writeFileSync(agentPath, renderAgentDocument({
            name,
            description,
            body: input.body,
            model: input.model,
            tools: input.tools,
            disallowedTools: input.disallowedTools,
            color: input.color,
            maxTurns: input.maxTurns,
        }), "utf8");
        const agent = this.load(name);
        if (!agent)
            throw new Error(`failed to load created agent: ${name}`);
        return agent;
    }
    validate(name) {
        const agents = name
            ? this.list().filter((candidate) => candidate.name === name)
            : this.list();
        if (name && agents.length === 0) {
            return [{
                    name,
                    path: "",
                    ok: false,
                    errors: [`agent not found: ${name}`],
                    warnings: [],
                }];
        }
        const knownTools = baseTools.map((tool) => tool.name);
        return agents.map((agent) => validateAgentDocument(agent.name, agent.path, fs.existsSync(agent.path) ? fs.readFileSync(agent.path, "utf8") : null, knownTools));
    }
}
