import { Type as T } from "typebox";
import { getBuiltinAgents, loadCustomAgents } from "../agent/index.js";
const TaskInput = T.Object({
    subagent_type: T.String({ description: "Agent type to dispatch (e.g., explore, general)" }),
    description: T.String({ description: "Short 3-5 word description of the task" }),
    prompt: T.String({ description: "Detailed instructions for the subagent" }),
});
/**
 * Create the task tool definition.
 * The task tool allows the LLM to dispatch work to a subagent.
 * In the current simplified implementation, it runs the task and returns results.
 */
export function createTaskToolDefinition(cwd) {
    // Cache agents
    const allAgents = [...getBuiltinAgents(), ...loadCustomAgents(cwd, "")];
    const agentMap = new Map();
    for (const _a of allAgents)
        allAgents;
    return {
        name: "task",
        label: "Task",
        description: "Dispatch a task to a specialized subagent (e.g., explore, general). " +
            "Use explore for codebase research, general for complex multi-step tasks.",
        parameters: TaskInput,
        renderShell: "default",
        promptSnippet: "task: dispatch work to a specialized subagent",
        promptGuidelines: [
            "Use `task` for delegating research or parallel work to subagents.",
            "For codebase exploration, use subagent_type: 'explore'.",
            "For general multi-step tasks, use subagent_type: 'general'.",
        ],
        async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
            const agent = agentMap.get(params.subagent_type);
            if (!agent) {
                const available = allAgents
                    .filter((a) => a.mode === "subagent" || a.mode === "all")
                    .map((a) => a.name)
                    .join(", ");
                return {
                    content: [
                        {
                            type: "text",
                            text: `Unknown subagent "${params.subagent_type}". Available: ${available}`,
                        },
                    ],
                    details: null,
                };
            }
            // In a full implementation, this would create a child session and run the subagent.
            // For the initial implementation, we return instructions for the calling agent to proceed.
            return {
                content: [
                    {
                        type: "text",
                        text: `Task dispatched to "${params.subagent_type}" agent.\n\nDescription: ${params.description}\n\nInstructions: ${params.prompt}\n\n---\n\nUse the tools available to you to complete this task. The subagent has access to:\n${agent.permission
                            ? Object.entries(agent.permission)
                                .filter(([, v]) => v === "allow")
                                .map(([k]) => `- ${k}`)
                                .join("\n")
                            : "all tools"}`,
                    },
                ],
                details: { agent: params.subagent_type },
            };
        },
    };
}
//# sourceMappingURL=task.js.map