import { TOOL_NAME_BASH, TOOL_NAME_EDIT, TOOL_NAME_VIEW_IMAGE, TOOL_NAME_WEB_FETCH, TOOL_NAME_WEB_SEARCH, TOOL_NAME_WRITE, } from "../tools/tool_names.js";
import { buildDefaultSubagentSystemPrompt, DEFAULT_SUBAGENT_DESCRIPTION } from "./default.js";
import { DEFAULT_SUBAGENT_NAME, } from "./types.js";
const INHERITABLE_TOOL_NAMES = new Set([
    TOOL_NAME_BASH,
    TOOL_NAME_WRITE,
    TOOL_NAME_EDIT,
    TOOL_NAME_VIEW_IMAGE,
    TOOL_NAME_WEB_SEARCH,
    TOOL_NAME_WEB_FETCH,
]);
function normalizeTools(tools) {
    const seen = new Set();
    const normalized = [];
    for (const tool of tools) {
        if (seen.has(tool))
            continue;
        seen.add(tool);
        normalized.push(tool);
    }
    return normalized;
}
function getInheritedSubagentTools(persona) {
    const toolNames = persona.tools ?? [TOOL_NAME_BASH, TOOL_NAME_WRITE, TOOL_NAME_EDIT];
    const selected = [];
    for (const name of toolNames) {
        if (INHERITABLE_TOOL_NAMES.has(name)) {
            selected.push(name);
        }
    }
    return normalizeTools(selected);
}
export function resolveSubagentEffectiveSettings(args) {
    const model = args.launchModel?.model ?? args.config.model ?? args.persona.model;
    const baseSettings = args.persona.settings ?? {};
    const mergedSettings = args.config.settings
        ? { ...baseSettings, ...args.config.settings }
        : { ...baseSettings };
    if (args.launchModel) {
        mergedSettings.reasoning = args.launchModel.reasoning;
    }
    const tools = args.config.tools
        ? normalizeTools(args.config.tools)
        : getInheritedSubagentTools(args.persona);
    const riskLevel = args.config.riskLevel ?? args.riskLevel;
    return {
        model,
        settings: Object.keys(mergedSettings).length > 0 ? mergedSettings : undefined,
        tools,
        riskLevel,
    };
}
export function getSubagentBasePrompt(args) {
    if (args.name === DEFAULT_SUBAGENT_NAME) {
        return buildDefaultSubagentSystemPrompt(args.mainPersonaSystemPrompt);
    }
    if (!args.config.systemPrompt) {
        throw new Error(`Subagent '${args.name}' is missing a system prompt.`);
    }
    return args.config.systemPrompt;
}
export function getSubagentDescription(name, config) {
    if (config.description)
        return config.description;
    if (name === DEFAULT_SUBAGENT_NAME) {
        return DEFAULT_SUBAGENT_DESCRIPTION;
    }
    return undefined;
}
export function formatSubagentsForPrompt(persona) {
    if (!persona.subagents) {
        return undefined;
    }
    const subagentLines = Object.entries(persona.subagents).map(([name, config]) => {
        const description = getSubagentDescription(name, config) ?? "(No description provided.)";
        const launchModels = config.launchModels ?? [];
        const launchModelsText = launchModels.length > 0
            ? `\n  - Launch model overrides: ${launchModels.map((entry) => `\`${entry}\``).join(", ")}\n    By default, launch the subagent without a model override unless the user explicitly asks to use a specific model.`
            : "";
        return `- \`${name}\`: ${description}${launchModelsText}`;
    });
    if (subagentLines.length === 0) {
        return undefined;
    }
    return `\n\n### Available sub-agents\n\nYou have access to the following sub-agents:\n\n${subagentLines.join("\n")}`;
}
//# sourceMappingURL=registry.js.map