import { existsSync, readdirSync, readFileSync, statSync } from "fs";
import { basename, join } from "path";
import { CONFIG_DIR_NAME } from "../../config.js";
import { parseFrontmatter } from "../../utils/frontmatter.js";
import { resolvePath } from "../../utils/paths.js";
import { createSyntheticSourceInfo } from "../source-info.js";
function builtinAgent(name, partial) {
    return {
        ...partial,
        name,
        sourceInfo: createSyntheticSourceInfo(`builtin:agent/${name}`, { source: "builtin" }),
    };
}
const BUILTIN_AGENTS = [
    builtinAgent("build", {
        description: "Default working agent with full tool access",
        mode: "primary",
        hidden: false,
    }),
    builtinAgent("explore", {
        description: "Fast agent specialized for exploring codebases (read-only)",
        mode: "subagent",
        hidden: false,
        prompt: "You are a codebase exploration specialist. Your job is to quickly find relevant files, codes patterns, and understand project structure. Use grep, glob, read, and ls tools. Be thorough in your searches. Return findings with file paths and line numbers.",
        permission: {
            read: "allow",
            glob: "allow",
            grep: "allow",
            list: "allow",
            bash: "allow",
            edit: "deny",
            write: "deny",
            webfetch: "allow",
            websearch: "allow",
        },
        steps: 10,
    }),
    builtinAgent("general", {
        description: "General-purpose agent for researching complex questions and executing multi-step tasks",
        mode: "subagent",
        hidden: false,
        prompt: "You are a general-purpose agent. Complete the assigned task using the tools available to you.",
        steps: 25,
    }),
];
export function getBuiltinAgents() {
    return [...BUILTIN_AGENTS];
}
export function loadCustomAgents(cwd, agentDir) {
    const resolvedCwd = resolvePath(cwd);
    const resolvedAgentDir = resolvePath(agentDir);
    const agents = [];
    const dirs = [
        { path: join(resolvedAgentDir, "agents"), scope: "user" },
        { path: join(resolvedAgentDir, "agent"), scope: "user" },
        { path: join(resolvedCwd, CONFIG_DIR_NAME, "agents"), scope: "project" },
        { path: join(resolvedCwd, CONFIG_DIR_NAME, "agent"), scope: "project" },
    ];
    for (const { path: dirPath, scope } of dirs) {
        if (!existsSync(dirPath))
            continue;
        try {
            const entries = readdirSync(dirPath, { withFileTypes: true });
            for (const entry of entries) {
                const fullPath = join(dirPath, entry.name);
                let isFile = entry.isFile();
                if (entry.isSymbolicLink()) {
                    try {
                        const stats = statSync(fullPath);
                        isFile = stats.isFile();
                    }
                    catch {
                        continue;
                    }
                }
                if (!isFile || !entry.name.endsWith(".md"))
                    continue;
                try {
                    const content = readFileSync(fullPath, "utf-8");
                    const { frontmatter, body } = parseFrontmatter(content);
                    const name = basename(fullPath).replace(/\.md$/, "");
                    const modeStr = frontmatter.mode ?? "all";
                    const mode = modeStr === "primary" || modeStr === "subagent" || modeStr === "all" ? modeStr : "all";
                    // Build permission from frontmatter
                    const permission = frontmatter.permission;
                    agents.push({
                        name,
                        description: frontmatter.description,
                        mode,
                        hidden: frontmatter.hidden,
                        model: frontmatter.model,
                        permission,
                        prompt: body || undefined,
                        steps: frontmatter.steps,
                        sourceInfo: createSyntheticSourceInfo(fullPath, {
                            source: "local",
                            scope,
                            baseDir: dirPath,
                        }),
                    });
                }
                catch {
                    // Skip invalid agent files
                }
            }
        }
        catch {
            // Skip unreadable directories
        }
    }
    return agents;
}
//# sourceMappingURL=index.js.map