diff --git a/extensions/cli/src/subagent/index.ts b/extensions/cli/src/subagent/index.ts new file mode 100644 index 000000000..29f89fadc --- /dev/null +++ b/extensions/cli/src/subagent/index.ts @@ -0,0 +1,28 @@ +import { Tool } from "../tools/types.js"; + +export const SUBAGENT_TOOL_META: Tool = { + name: "Subagent", + displayName: "Subagent", + description: "Use a subagent to handle a specialized task.", + readonly: false, + isBuiltIn: true, + parameters: { + type: "object", + required: ["description", "prompt", "subagent_name"], + properties: { + description: { + type: "string", + description: "A short description of the task", + }, + prompt: { + type: "string", + description: "The task for the agent to perform", + }, + subagent_name: { + type: "string", + description: "The type of specialized agent to use for this task.", + }, + }, + }, + run: async () => "", +}; diff --git a/extensions/cli/src/tools/allBuiltIns.ts b/extensions/cli/src/tools/allBuiltIns.ts index 0d46de4b9..4e2f4a6d9 100644 --- a/extensions/cli/src/tools/allBuiltIns.ts +++ b/extensions/cli/src/tools/allBuiltIns.ts @@ -1,3 +1,5 @@ +import { SUBAGENT_TOOL_META } from "../subagent/index.js"; + import { editTool } from "./edit.js"; import { exitTool } from "./exit.js"; import { fetchTool } from "./fetch.js"; @@ -7,7 +9,6 @@ import { readFileTool } from "./readFile.js"; import { reportFailureTool } from "./reportFailure.js"; import { runTerminalCommandTool } from "./runTerminalCommand.js"; import { searchCodeTool } from "./searchCode.js"; -import { SUBAGENT_TOOL_META } from "./subagent.js"; import type { Tool } from "./types.js"; import { uploadArtifactTool } from "./uploadArtifact.js"; import { writeChecklistTool } from "./writeChecklist.js"; diff --git a/extensions/cli/src/tools/index.tsx b/extensions/cli/src/tools/index.tsx index f95cf02ef..8ec0aca35 100644 --- a/extensions/cli/src/tools/index.tsx +++ b/extensions/cli/src/tools/index.tsx @@ -124,11 +124,7 @@ export async function getAllAvailableTools( } if (isBetaSubagentToolEnabled()) { - tools.push( - subagentTool({ - modelServiceState: modelState, - }), - ); + tools.push(await subagentTool()); } const mcpState = await serviceContainer.get( diff --git a/extensions/cli/src/tools/subagent.ts b/extensions/cli/src/tools/subagent.ts index aad198b8b..c457440c3 100644 --- a/extensions/cli/src/tools/subagent.ts +++ b/extensions/cli/src/tools/subagent.ts @@ -1,133 +1,117 @@ +import { serviceContainer } from "../services/ServiceContainer.js"; +import { ModelServiceState, SERVICE_NAMES } from "../services/types.js"; import { generateSubagentToolDescription, getSubagent, getAgentNames as getSubagentNames, } from "../subagent/get-agents.js"; +import { SUBAGENT_TOOL_META } from "../subagent/index.js"; import { logger } from "../util/logger.js"; -import { GetTool, Tool } from "./types.js"; +import { Tool } from "./types.js"; -export const SUBAGENT_TOOL_META: Tool = { - name: "Subagent", - displayName: "Subagent", - description: "Use a subagent to handle a specialized task.", - readonly: false, - isBuiltIn: true, - parameters: { - type: "object", - required: ["description", "prompt", "subagent_name"], - properties: { - description: { - type: "string", - description: "A short description of the task", - }, - prompt: { - type: "string", - description: "The task for the agent to perform", - }, - subagent_name: { - type: "string", - description: "The type of specialized agent to use for this task.", - }, - }, - }, - run: async () => "", -}; +export const subagentTool = async (): Promise => { + const modelServiceState = await serviceContainer.get( + SERVICE_NAMES.MODEL, + ); -export const subagentTool: GetTool = (params) => ({ - ...SUBAGENT_TOOL_META, + return { + ...SUBAGENT_TOOL_META, - description: generateSubagentToolDescription(params.modelServiceState), + description: generateSubagentToolDescription(modelServiceState), - parameters: { - ...SUBAGENT_TOOL_META.parameters, - properties: { - ...SUBAGENT_TOOL_META.parameters.properties, - subagent_name: { - type: "string", - description: `The type of specialized agent to use for this task. Available agents: ${ - params ? getSubagentNames(params.modelServiceState).join(", ") : "" - }`, - }, - }, - }, - - preprocess: async (args: any) => { - const { description, subagent_name } = args; - - const agent = getSubagent(params.modelServiceState, subagent_name); - if (!agent) { - throw new Error( - `Unknown agent type: ${subagent_name}. Available agents: ${getSubagentNames( - params.modelServiceState, - ).join(", ")}`, - ); - } - - return { - args, - preview: [ - { - type: "text", - content: `Spawning ${agent.model.name} to: ${description}`, + parameters: { + ...SUBAGENT_TOOL_META.parameters, + properties: { + ...SUBAGENT_TOOL_META.parameters.properties, + subagent_name: { + type: "string", + description: `The type of specialized agent to use for this task. Available agents: ${ + modelServiceState + ? getSubagentNames(modelServiceState).join(", ") + : "" + }`, }, - ], - }; - }, + }, + }, - run: async (args: any, context?: { toolCallId: string }) => { - const { prompt, subagent_name } = args; + preprocess: async (args: any) => { + const { description, subagent_name } = args; - logger.debug("subagent args", { args, context }); + const agent = getSubagent(modelServiceState, subagent_name); + if (!agent) { + throw new Error( + `Unknown agent type: ${subagent_name}. Available agents: ${getSubagentNames( + modelServiceState, + ).join(", ")}`, + ); + } - // get agent configuration - const agent = getSubagent(params.modelServiceState, subagent_name); - if (!agent) { - throw new Error(`Unknown agent type: ${subagent_name}`); - } + return { + args, + preview: [ + { + type: "text", + content: `Spawning ${agent.model.name} to: ${description}`, + }, + ], + }; + }, - // Dynamically import to avoid circular dependency and passing repeated params - const { executeSubAgent } = await import("../subagent/executor.js"); - const { services } = await import("../services/index.js"); + run: async (args: any, context?: { toolCallId: string }) => { + const { prompt, subagent_name } = args; - const chatHistoryService = services.chatHistory; - const parentSessionId = chatHistoryService.getSessionId(); - if (!parentSessionId) { - throw new Error("No active session found"); - } + logger.debug("subagent args", { args, context }); - // Execute subagent with output streaming - const result = await executeSubAgent({ - agent, - prompt, - parentSessionId, - abortController: new AbortController(), - onOutputUpdate: context?.toolCallId - ? (output: string) => { - try { - chatHistoryService.addToolResult( - context.toolCallId, - output, - "calling", - ); - } catch { - // Ignore errors during streaming updates + // get agent configuration + const agent = getSubagent(modelServiceState, subagent_name); + if (!agent) { + throw new Error(`Unknown agent type: ${subagent_name}`); + } + + // Dynamically import to avoid circular dependency and passing repeated params + const { executeSubAgent } = await import("../subagent/executor.js"); + const { services } = await import("../services/index.js"); + + const chatHistoryService = services.chatHistory; + const parentSessionId = chatHistoryService.getSessionId(); + if (!parentSessionId) { + throw new Error("No active session found"); + } + + // Execute subagent with output streaming + const result = await executeSubAgent({ + agent, + prompt, + parentSessionId, + abortController: new AbortController(), + onOutputUpdate: context?.toolCallId + ? (output: string) => { + try { + chatHistoryService.addToolResult( + context.toolCallId, + output, + "calling", + ); + } catch { + // Ignore errors during streaming updates + } } - } - : undefined, - }); + : undefined, + }); - logger.debug("subagent result", { result }); + logger.debug("subagent result", { result }); - const output = [ - result.response, - "", - `status: ${result.success ? "completed" : "failed"}`, - "", - ] - .filter(Boolean) - .join("\n"); + const output = [ + result.response, + "", + `status: ${result.success ? "completed" : "failed"}`, + "", + ] + .filter(Boolean) + .join("\n"); - return output; - }, -}); + return output; + }, + }; +}; diff --git a/extensions/cli/src/tools/types.ts b/extensions/cli/src/tools/types.ts index faf68cd4e..544c6d970 100644 --- a/extensions/cli/src/tools/types.ts +++ b/extensions/cli/src/tools/types.ts @@ -1,7 +1,5 @@ import type { ToolPolicy } from "@continuedev/terminal-security"; -import type { ModelServiceState } from "src/services/types.js"; - // JSON Schema compatible parameter definition export interface ParameterSchema { type: string; @@ -49,10 +47,6 @@ export interface Tool { ) => ToolPolicy; } -export type GetTool = (params: { - modelServiceState: ModelServiceState; -}) => Tool; - export interface ToolCall { id: string; name: string;