diff --git a/extensions/cli/src/index.ts b/extensions/cli/src/index.ts index a3a718e3ef..f251a427ee 100644 --- a/extensions/cli/src/index.ts +++ b/extensions/cli/src/index.ts @@ -200,10 +200,6 @@ addCommonOptions(program) ) .option("--resume", "Resume from last session") .option("--fork ", "Fork from an existing session ID") - .option( - "--beta-subagent-tool", - "Enable beta Subagent tool for invoking subagents", - ) .action(async (prompt, options) => { // Telemetry: record command invocation await posthogService.capture("cliCommand", { command: "cn" }); diff --git a/extensions/cli/src/services/index.ts b/extensions/cli/src/services/index.ts index ffb7cba2ca..dafc7075bd 100644 --- a/extensions/cli/src/services/index.ts +++ b/extensions/cli/src/services/index.ts @@ -1,9 +1,6 @@ import { loadAuthConfig } from "../auth/workos.js"; import { initializeWithOnboarding } from "../onboarding.js"; -import { - setBetaSubagentToolEnabled, - setBetaUploadArtifactToolEnabled, -} from "../tools/toolsConfig.js"; +import { setBetaUploadArtifactToolEnabled } from "../tools/toolsConfig.js"; import { logger } from "../util/logger.js"; import { AgentFileService } from "./AgentFileService.js"; @@ -67,9 +64,6 @@ export async function initializeServices(initOptions: ServiceInitOptions = {}) { if (commandOptions.betaUploadArtifactTool) { setBetaUploadArtifactToolEnabled(true); } - if (commandOptions.betaSubagentTool) { - setBetaSubagentToolEnabled(true); - } // Handle onboarding for TUI mode (headless: false) unless explicitly skipped if (!initOptions.headless && !initOptions.skipOnboarding) { const authConfig = loadAuthConfig(); diff --git a/extensions/cli/src/stream/handleToolCalls.ts b/extensions/cli/src/stream/handleToolCalls.ts index bc5d99176e..f4f477da8b 100644 --- a/extensions/cli/src/stream/handleToolCalls.ts +++ b/extensions/cli/src/stream/handleToolCalls.ts @@ -147,13 +147,41 @@ export async function handleToolCalls( // Execute the valid preprocessed tool calls // Note: executeStreamedToolCalls adds tool results to toolCallStates via - // services.chatHistory.addToolResult() internally - const { hasRejection } = await executeStreamedToolCalls( + // services.chatHistory.addToolResult() internally when service is available + const { hasRejection, chatHistoryEntries } = await executeStreamedToolCalls( preprocessedCalls, callbacks, isHeadless, ); + // When ChatHistoryService is disabled in subagent execution, + // manually update the local chatHistory array with tool results + if (!useService && chatHistoryEntries.length > 0) { + const lastAssistantIndex = chatHistory.findLastIndex( + (item) => item.message.role === "assistant" && item.toolCallStates, + ); + if ( + lastAssistantIndex >= 0 && + chatHistory[lastAssistantIndex].toolCallStates + ) { + for (const entry of chatHistoryEntries) { + const toolState = chatHistory[lastAssistantIndex].toolCallStates!.find( + (ts) => ts.toolCallId === entry.tool_call_id, + ); + if (toolState) { + toolState.status = entry.status; + toolState.output = [ + { + content: String(entry.content) || "", + name: "Tool Result", + description: "Tool execution result", + }, + ]; + } + } + } + } + if (isHeadless && hasRejection) { logger.debug( "Tool call rejected in headless mode - returning current content", diff --git a/extensions/cli/src/subagent/builtInSubagents.ts b/extensions/cli/src/subagent/builtInSubagents.ts new file mode 100644 index 0000000000..2880c7e422 --- /dev/null +++ b/extensions/cli/src/subagent/builtInSubagents.ts @@ -0,0 +1,86 @@ +import type { ModelConfig } from "@continuedev/config-yaml"; + +import { logger } from "src/util/logger.js"; + +export interface BuiltInSubagent { + name: string; + systemPrompt: string; + model: string; +} + +export const NAVIGATOR_SUBAGENT: BuiltInSubagent = { + name: "navigator", + model: "claude-haiku-4-5", + systemPrompt: `You are a Codebase Navigator subagent specialized in exploring, searching, and mapping large codebases. + +When to use: + + Use this subagent whenever you need to explore or find or understand a codebase or a folder. + +When navigating a codebase, you will: + +1. **Locate Relevant Code**: Use file and code search tools to find the most relevant files, modules, functions, and types. Prefer a small, high-signal set of locations over exhaustive listings. + +2. **Trace Behavior and Dependencies**: Follow call chains, imports, and data flow to understand how the relevant pieces interact, including upstream/downstream dependencies and important side effects. + +3. **Map the Codebase for Others**: Build a concise mental map: which components are core, which are helpers, where entry points live, and how configuration or environment affects behavior. + +Your output should be concise and actionable, starting with a brief summary of what you found and listing the key files/paths, functions, symbols, and important relationships or flows between them in plain language. If you cannot find something, describe what you searched for, where you looked, and suggest next places or strategies to investigate.`, +}; + +export const GENERALIST_SUBAGENT: BuiltInSubagent = { + name: "general-tasker", + model: "claude-sonnet-4-6", + systemPrompt: `You are a Generalist subagent capable of handling any development task delegated to you. + +When to use: + + Use this subagent for any task that doesn't require a specialized subagent, including but not limited to: implementing features, fixing bugs, refactoring, code review, documentation, research, debugging, and analysis. + +When handling a task, you will: + +1. **Interpret the Request**: Understand what is being asked, whether it's exploration, implementation, review, analysis, or something else entirely. Adapt your approach based on the nature of the task. + +2. **Gather Context**: Use available tools to explore the codebase, read relevant files, and understand the surrounding architecture before taking action or forming conclusions. + +3. **Communicate Results**: Provide clear, actionable output tailored to the task. Summarize what you did or discovered, highlight key insights or changes, and note any open questions or recommended next steps. + +You are flexible and resourceful. If a task is ambiguous, make reasonable assumptions and state them. If you encounter blockers, describe what you attempted and suggest alternatives.`, +}; + +export const BUILT_IN_SUBAGENTS: BuiltInSubagent[] = [ + NAVIGATOR_SUBAGENT, + GENERALIST_SUBAGENT, +]; + +export function createBuiltInSubagentModel( + subagent: BuiltInSubagent, + baseModel: ModelConfig, +): ModelConfig { + return { + ...baseModel, + name: subagent.name, + model: subagent.model, + roles: ["subagent"], + chatOptions: { + ...baseModel.chatOptions, + baseSystemMessage: subagent.systemPrompt, + }, + }; +} + +export function isLocalAnthropicModel(model: ModelConfig | null): boolean { + if (!model) { + return false; + } + + const isAnthropic = model.provider === "anthropic"; + const hasDirectApiKey = + typeof model.apiKey === "string" && model.apiKey.length > 0; + + logger.debug("subagent_enabled_for_anthropic", { + enabled: isAnthropic && hasDirectApiKey, + }); + + return isAnthropic && hasDirectApiKey; +} diff --git a/extensions/cli/src/subagent/executor.ts b/extensions/cli/src/subagent/executor.ts index 1580e7f7f9..3790250bb7 100644 --- a/extensions/cli/src/subagent/executor.ts +++ b/extensions/cli/src/subagent/executor.ts @@ -8,6 +8,8 @@ import { streamChatResponse } from "../stream/streamChatResponse.js"; import { escapeEvents } from "../util/cli.js"; import { logger } from "../util/logger.js"; +let isInsideSubagent = false; + /** * Options for executing a subagent */ @@ -54,10 +56,17 @@ async function buildAgentSystemMessage( /** * Execute a subagent in a child session */ -// eslint-disable-next-line complexity export async function executeSubAgent( options: SubAgentExecutionOptions, ): Promise { + if (isInsideSubagent) { + return { + success: false, + response: "", + error: "Nested subagent invocation is not allowed", + }; + } + const { agent: subAgent, prompt, abortController, onOutputUpdate } = options; const mainAgentPermissionsState = @@ -65,6 +74,8 @@ export async function executeSubAgent( SERVICE_NAMES.TOOL_PERMISSIONS, ); + isInsideSubagent = true; + try { logger.debug("Starting subagent execution", { agent: subAgent.model?.name, @@ -209,5 +220,7 @@ export async function executeSubAgent( response: "", error: error.message, }; + } finally { + isInsideSubagent = false; } } diff --git a/extensions/cli/src/subagent/get-agents.ts b/extensions/cli/src/subagent/get-agents.ts deleted file mode 100644 index 9abdc2d3ad..0000000000 --- a/extensions/cli/src/subagent/get-agents.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { ModelService } from "../services/ModelService.js"; -import type { ModelServiceState } from "../services/types.js"; - -/** - * Get an agent by name - */ -export function getSubagent(modelState: ModelServiceState, name: string) { - return ( - ModelService.getSubagentModels(modelState).find( - (model) => model.model.name === name, - ) ?? null - ); -} - -/** - * Generate dynamic tool description listing available agents - */ -export function generateSubagentToolDescription( - modelState: ModelServiceState, -): string { - const agentList = ModelService.getSubagentModels(modelState) - .map( - (subagentModel) => - ` - ${subagentModel.model.name}: ${subagentModel.model.chatOptions?.baseSystemMessage}`, - ) - .join("\n"); - - // todo: refine this prompt later - return `Launch a specialized subagent to handle a specific task. - -Here are the available subagents: -${agentList} -`; -} - -export function getAgentNames(modelState: ModelServiceState): string[] { - return ModelService.getSubagentModels(modelState).map( - (model) => model.model.name, - ); -} diff --git a/extensions/cli/src/subagent/getAgents.ts b/extensions/cli/src/subagent/getAgents.ts new file mode 100644 index 0000000000..563ac9820e --- /dev/null +++ b/extensions/cli/src/subagent/getAgents.ts @@ -0,0 +1,65 @@ +import { createLlmApi } from "../config.js"; +import { ModelService } from "../services/ModelService.js"; +import type { ModelServiceState } from "../services/types.js"; + +import { + BUILT_IN_SUBAGENTS, + createBuiltInSubagentModel, + isLocalAnthropicModel, +} from "./builtInSubagents.js"; + +function getAllSubagentModels(modelState: ModelServiceState) { + const configSubagents = ModelService.getSubagentModels(modelState); + + if (!isLocalAnthropicModel(modelState.model)) { + return configSubagents; + } + + const builtInSubagents = BUILT_IN_SUBAGENTS.map((subagent) => { + const subagentModel = createBuiltInSubagentModel( + subagent, + modelState.model!, + ); + return { + llmApi: createLlmApi(subagentModel, modelState.authConfig), + model: subagentModel, + assistant: modelState.assistant, + authConfig: modelState.authConfig, + }; + }); + + return [...configSubagents, ...builtInSubagents]; +} + +export function getSubagent(modelState: ModelServiceState, name: string) { + return ( + getAllSubagentModels(modelState).find( + (model) => model.model.name === name, + ) ?? null + ); +} + +export function generateSubagentToolDescription( + modelState: ModelServiceState, +): string { + const agentList = getAllSubagentModels(modelState) + .map( + (subagentModel) => + ` - ${subagentModel.model.name}: ${subagentModel.model.chatOptions?.baseSystemMessage}`, + ) + .join("\n"); + + return `Launch an autonomous specialized subagent to handle a specific task. + +You have the independence to make decisions within you scope. You should focus on the specific task given by the main agent. + +Remember: You are part of a larger system. Your specialized focus helps the main agent handle multiple concerns efficiently. + +Here are the available subagents: +${agentList} +`; +} + +export function getAgentNames(modelState: ModelServiceState): string[] { + return getAllSubagentModels(modelState).map((model) => model.model.name); +} diff --git a/extensions/cli/src/tools/index.tsx b/extensions/cli/src/tools/index.tsx index fb43739f8b..0f22239627 100644 --- a/extensions/cli/src/tools/index.tsx +++ b/extensions/cli/src/tools/index.tsx @@ -32,10 +32,7 @@ import { runTerminalCommandTool } from "./runTerminalCommand.js"; import { checkIfRipgrepIsInstalled, searchCodeTool } from "./searchCode.js"; import { skillsTool } from "./skills.js"; import { subagentTool } from "./subagent.js"; -import { - isBetaSubagentToolEnabled, - isBetaUploadArtifactToolEnabled, -} from "./toolsConfig.js"; +import { isBetaUploadArtifactToolEnabled } from "./toolsConfig.js"; import { type Tool, type ToolCall, @@ -129,9 +126,7 @@ export async function getAllAvailableTools( tools.push(exitTool); } - if (isBetaSubagentToolEnabled()) { - tools.push(await subagentTool()); - } + tools.push(await subagentTool()); tools.push(await skillsTool()); diff --git a/extensions/cli/src/tools/subagent.test.ts b/extensions/cli/src/tools/subagent.test.ts index 3af2ff4bfe..9aae206637 100644 --- a/extensions/cli/src/tools/subagent.test.ts +++ b/extensions/cli/src/tools/subagent.test.ts @@ -3,7 +3,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import { services } from "../services/index.js"; import { serviceContainer } from "../services/ServiceContainer.js"; import { executeSubAgent } from "../subagent/executor.js"; -import { getAgentNames, getSubagent } from "../subagent/get-agents.js"; +import { getAgentNames, getSubagent } from "../subagent/getAgents.js"; import { subagentTool } from "./subagent.js"; diff --git a/extensions/cli/src/tools/subagent.ts b/extensions/cli/src/tools/subagent.ts index 43d2d29eb4..86645b5062 100644 --- a/extensions/cli/src/tools/subagent.ts +++ b/extensions/cli/src/tools/subagent.ts @@ -6,7 +6,7 @@ import { generateSubagentToolDescription, getSubagent, getAgentNames as getSubagentNames, -} from "../subagent/get-agents.js"; +} from "../subagent/getAgents.js"; import { SUBAGENT_TOOL_META } from "../subagent/index.js"; import { logger } from "../util/logger.js"; diff --git a/extensions/cli/src/tools/toolsConfig.ts b/extensions/cli/src/tools/toolsConfig.ts index e9a2e02330..b1703a545b 100644 --- a/extensions/cli/src/tools/toolsConfig.ts +++ b/extensions/cli/src/tools/toolsConfig.ts @@ -4,7 +4,6 @@ */ let betaUploadArtifactToolEnabled = false; -let betaSubagentToolEnabled = false; export function setBetaUploadArtifactToolEnabled(enabled: boolean): void { betaUploadArtifactToolEnabled = enabled; @@ -13,11 +12,3 @@ export function setBetaUploadArtifactToolEnabled(enabled: boolean): void { export function isBetaUploadArtifactToolEnabled(): boolean { return betaUploadArtifactToolEnabled; } - -export function setBetaSubagentToolEnabled(enabled: boolean): void { - betaSubagentToolEnabled = enabled; -} - -export function isBetaSubagentToolEnabled(): boolean { - return betaSubagentToolEnabled; -}