Roo-Code/src/shared/subagent.ts
Ruslan Andreev 2d8842b728 feat: enhance subagent functionality for parallel execution
- Introduced support for running multiple subagents in parallel by managing child tasks with unique toolCallIds.
- Updated the Task class to hold active subagent children in a Map for better tracking and management.
- Enhanced the reportSubagentProgress method to update specific subagent messages based on runId, allowing for real-time progress updates.
- Modified the ClineProvider to handle cancellation of multiple running subagents.
- Improved error handling and messaging for subagent execution.

These changes significantly improve the subagent's capabilities, enabling more efficient task management and user feedback during concurrent operations.
2026-03-02 12:14:15 +03:00

78 lines
3 KiB
TypeScript

/**
* Shared constants and types for the subagent feature.
* Used by SubagentTool, Task, build-tools, and UI to stay in sync with tool message shapes.
*/
/** Tool names used in say("tool", ...) payloads for subagent progress and completion. */
export const SUBAGENT_TOOL_NAMES = {
running: "subagentRunning",
completed: "subagentCompleted",
} as const
/** i18n keys sent as currentTask; webview uses t(key) to show locale. */
export const SUBAGENT_STATUS_STARTING = "chat:subagents.starting"
export const SUBAGENT_STATUS_THINKING = "chat:subagents.thinking"
/** Structured result codes for subagent completion; webview uses messageKey with t() for display. */
export const SUBAGENT_RESULT_CODE_CANCELLED = "CANCELLED" as const
export const SUBAGENT_CANCELLED_MESSAGE_KEY = "chat:subagents.cancelledByUser" as const
/** Sent when the user cancels the subagent. Extension passes this to backgroundCompletionResolve. */
export const SUBAGENT_CANCELLED_STRUCTURED_RESULT = {
code: SUBAGENT_RESULT_CODE_CANCELLED,
messageKey: SUBAGENT_CANCELLED_MESSAGE_KEY,
} as const
/** English message shown to the model when subagent is cancelled (tool result). */
export const SUBAGENT_CANCELLED_MODEL_MESSAGE = "Subagent was cancelled by the user."
/** Generic message shown to the model/UI when subagent fails (avoids leaking internal error details). */
export const SUBAGENT_FAILED_MODEL_MESSAGE = "The subagent failed."
export type SubagentStructuredResult = typeof SUBAGENT_CANCELLED_STRUCTURED_RESULT
/** Payload for the "subagentRunning" tool message (progress updates). */
export interface SubagentRunningPayload {
tool: typeof SUBAGENT_TOOL_NAMES.running
description?: string
currentTask?: string
/** Identifies this run for progress updates when multiple subagents run in parallel (e.g. block.id). */
runId?: string
}
/** Payload for the "subagentCompleted" tool message (result or error). */
export interface SubagentCompletedPayload {
tool: typeof SUBAGENT_TOOL_NAMES.completed
description?: string
result?: string
error?: string
/** When set, webview shows t(messageKey) instead of result/error. */
resultCode?: string
messageKey?: string
}
export type SubagentType = "general" | "explore"
/** Parameters for running a subagent in the background. */
export interface RunSubagentInBackgroundParams {
parentTaskId: string
prompt: string
subagentType: SubagentType
onProgress?: (currentTask: string) => void
/** Required: keys the child in activeSubagentChildren and ties completion to the correct tool_use_id. */
toolCallId: string
}
/** Provider interface for running a subagent. Allows SubagentTool to call without type assertion. */
export interface SubagentRunner {
runSubagentInBackground(params: RunSubagentInBackgroundParams): Promise<string | SubagentStructuredResult>
}
export function isSubagentRunner(provider: unknown): provider is SubagentRunner {
return (
typeof provider === "object" &&
provider !== null &&
"runSubagentInBackground" in provider &&
typeof (provider as SubagentRunner).runSubagentInBackground === "function"
)
}