diff --git a/extensions/cli/src/services/index.ts b/extensions/cli/src/services/index.ts index 1ddc73da7..56a5d4e93 100644 --- a/extensions/cli/src/services/index.ts +++ b/extensions/cli/src/services/index.ts @@ -389,6 +389,8 @@ export const services = { gitAiIntegration: gitAiIntegrationService, } as const; +export type ServicesType = typeof services; + // Export the service container for advanced usage export { serviceContainer }; diff --git a/extensions/cli/src/subagent/executor.ts b/extensions/cli/src/subagent/executor.ts index b42b86baf..405e70705 100644 --- a/extensions/cli/src/subagent/executor.ts +++ b/extensions/cli/src/subagent/executor.ts @@ -1,12 +1,9 @@ import type { ChatHistoryItem } from "core"; -import { - ModelServiceState, - SERVICE_NAMES, - serviceContainer, - services, -} from "../services/index.js"; -import { ToolPermissionServiceState } from "../services/ToolPermissionService.js"; +import type { ServicesType } from "../services/index.js"; +import type { ServiceContainer } from "../services/ServiceContainer.js"; +import type { ToolPermissionServiceState } from "../services/ToolPermissionService.js"; +import { ModelServiceState, SERVICE_NAMES } from "../services/types.js"; import { streamChatResponse } from "../stream/streamChatResponse.js"; import { escapeEvents } from "../util/cli.js"; import { logger } from "../util/logger.js"; @@ -20,6 +17,8 @@ export interface SubAgentExecutionOptions { parentSessionId: string; abortController: AbortController; onOutputUpdate?: (output: string) => void; + services: ServicesType; + serviceContainer: ServiceContainer; } /** @@ -61,13 +60,20 @@ async function buildAgentSystemMessage( export async function executeSubAgent( options: SubAgentExecutionOptions, ): Promise { + const { + agent: subAgent, + prompt, + abortController, + onOutputUpdate, + services, + serviceContainer, + } = options; + const mainAgentPermissionsState = await serviceContainer.get( SERVICE_NAMES.TOOL_PERMISSIONS, ); - const { agent: subAgent, prompt, abortController, onOutputUpdate } = options; - try { logger.debug("Starting subagent execution", { agent: subAgent.model?.name, diff --git a/extensions/cli/src/subagent/get-agents.ts b/extensions/cli/src/subagent/get-agents.ts index 3b5282e4a..9abdc2d3a 100644 --- a/extensions/cli/src/subagent/get-agents.ts +++ b/extensions/cli/src/subagent/get-agents.ts @@ -1,5 +1,5 @@ -import { ModelServiceState } from "../services/index.js"; import { ModelService } from "../services/ModelService.js"; +import type { ModelServiceState } from "../services/types.js"; /** * Get an agent by name diff --git a/extensions/cli/src/tools/allBuiltIns.ts b/extensions/cli/src/tools/allBuiltIns.ts index be5e68b36..0d46de4b9 100644 --- a/extensions/cli/src/tools/allBuiltIns.ts +++ b/extensions/cli/src/tools/allBuiltIns.ts @@ -7,13 +7,14 @@ import { readFileTool } from "./readFile.js"; import { reportFailureTool } from "./reportFailure.js"; import { runTerminalCommandTool } from "./runTerminalCommand.js"; import { searchCodeTool } from "./searchCode.js"; -import { subagentTool } from "./subagent.js"; +import { SUBAGENT_TOOL_META } from "./subagent.js"; +import type { Tool } from "./types.js"; import { uploadArtifactTool } from "./uploadArtifact.js"; import { writeChecklistTool } from "./writeChecklist.js"; import { writeFileTool } from "./writeFile.js"; // putting in here for circular import issue -export const ALL_BUILT_IN_TOOLS = [ +export const ALL_BUILT_IN_TOOLS: Tool[] = [ readFileTool, editTool, multiEditTool, @@ -23,7 +24,7 @@ export const ALL_BUILT_IN_TOOLS = [ runTerminalCommandTool, fetchTool, writeChecklistTool, - subagentTool(), + SUBAGENT_TOOL_META, exitTool, reportFailureTool, uploadArtifactTool, diff --git a/extensions/cli/src/tools/index.tsx b/extensions/cli/src/tools/index.tsx index f95cf02ef..67c4f4482 100644 --- a/extensions/cli/src/tools/index.tsx +++ b/extensions/cli/src/tools/index.tsx @@ -127,6 +127,8 @@ export async function getAllAvailableTools( tools.push( subagentTool({ modelServiceState: modelState, + services, + serviceContainer, }), ); } diff --git a/extensions/cli/src/tools/subagent.ts b/extensions/cli/src/tools/subagent.ts index a41cc86c7..5e38e3719 100644 --- a/extensions/cli/src/tools/subagent.ts +++ b/extensions/cli/src/tools/subagent.ts @@ -1,3 +1,4 @@ +import { executeSubAgent } from "../subagent/executor.js"; import { generateSubagentToolDescription, getSubagent, @@ -5,17 +6,14 @@ import { } from "../subagent/get-agents.js"; import { logger } from "../util/logger.js"; -import { GetTool } from "./types.js"; +import { GetTool, Tool } from "./types.js"; -export const subagentTool: GetTool = (params) => ({ +export const SUBAGENT_TOOL_META: Tool = { name: "Subagent", displayName: "Subagent", - description: params - ? generateSubagentToolDescription(params.modelServiceState) - : "Use a subagent to handle a specialized task.", + description: "Use a subagent to handle a specialized task.", readonly: false, isBuiltIn: true, - parameters: { type: "object", required: ["description", "prompt", "subagent_name"], @@ -28,6 +26,24 @@ export const subagentTool: GetTool = (params) => ({ 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: GetTool = (params) => ({ + ...SUBAGENT_TOOL_META, + + description: generateSubagentToolDescription(params.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: ${ @@ -38,18 +54,6 @@ export const subagentTool: GetTool = (params) => ({ }, preprocess: async (args: any) => { - if (!params) { - return { - args, - preview: [ - { - type: "text", - content: "Subagent not found", - }, - ], - }; - } - const { description, subagent_name } = args; const agent = getSubagent(params.modelServiceState, subagent_name); @@ -73,8 +77,6 @@ export const subagentTool: GetTool = (params) => ({ }, run: async (args: any, context?: { toolCallId: string }) => { - if (!params) return ""; - const { prompt, subagent_name } = args; logger.debug("subagent args", { args, context }); @@ -85,10 +87,8 @@ export const subagentTool: GetTool = (params) => ({ throw new Error(`Unknown agent type: ${subagent_name}`); } - const { executeSubAgent } = await import("../subagent/executor.js"); - const { services } = await import("../services/index.js"); - - const parentSessionId = services.chatHistory.getSessionId(); + const chatHistoryService = params.services.chatHistory; + const parentSessionId = chatHistoryService.getSessionId(); if (!parentSessionId) { throw new Error("No active session found"); } @@ -99,10 +99,12 @@ export const subagentTool: GetTool = (params) => ({ prompt, parentSessionId, abortController: new AbortController(), + services: params.services, + serviceContainer: params.serviceContainer, onOutputUpdate: context?.toolCallId ? (output: string) => { try { - services.chatHistory.addToolResult( + chatHistoryService.addToolResult( context.toolCallId, output, "calling", diff --git a/extensions/cli/src/tools/types.ts b/extensions/cli/src/tools/types.ts index a7d8bcde3..aea513fcf 100644 --- a/extensions/cli/src/tools/types.ts +++ b/extensions/cli/src/tools/types.ts @@ -1,6 +1,8 @@ import type { ToolPolicy } from "@continuedev/terminal-security"; -import { ModelServiceState } from "src/services/types.js"; +import type { ServicesType } from "src/services/index.js"; +import type { ServiceContainer } from "src/services/ServiceContainer.js"; +import type { ModelServiceState } from "src/services/types.js"; // JSON Schema compatible parameter definition export interface ParameterSchema { @@ -49,8 +51,10 @@ export interface Tool { ) => ToolPolicy; } -export type GetTool = (params?: { +export type GetTool = (params: { modelServiceState: ModelServiceState; + services: ServicesType; + serviceContainer: ServiceContainer; }) => Tool; export interface ToolCall {