* Creates and validates a new SubAgentScope instance. * This factory method ensures that all tools provided in the prompt configuration * are valid for non-interactive use before creating the subagent instance. * @param {string} name - The name of the subagent. * @param {Config} runtimeC
(
name: string,
runtimeContext: Config,
promptConfig: PromptConfig,
modelConfig: ModelConfig,
runConfig: RunConfig,
toolConfig?: ToolConfig,
outputConfig?: OutputConfig,
)
| 276 | * @throws {Error} If any tool requires user confirmation. |
| 277 | */ |
| 278 | static async create( |
| 279 | name: string, |
| 280 | runtimeContext: Config, |
| 281 | promptConfig: PromptConfig, |
| 282 | modelConfig: ModelConfig, |
| 283 | runConfig: RunConfig, |
| 284 | toolConfig?: ToolConfig, |
| 285 | outputConfig?: OutputConfig, |
| 286 | ): Promise<SubAgentScope> { |
| 287 | if (toolConfig) { |
| 288 | const toolRegistry: ToolRegistry = await runtimeContext.getToolRegistry(); |
| 289 | const toolsToLoad: string[] = []; |
| 290 | for (const tool of toolConfig.tools) { |
| 291 | if (typeof tool === 'string') { |
| 292 | toolsToLoad.push(tool); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | for (const toolName of toolsToLoad) { |
| 297 | const tool = toolRegistry.getTool(toolName); |
| 298 | if (tool) { |
| 299 | const requiredParams = tool.schema.parameters?.required ?? []; |
| 300 | if (requiredParams.length > 0) { |
| 301 | // This check is imperfect. A tool might require parameters but still |
| 302 | // be interactive (e.g., `delete_file(path)`). However, we cannot |
| 303 | // build a generic invocation without knowing what dummy parameters |
| 304 | // to provide. Crashing here because `build({})` fails is worse |
| 305 | // than allowing a potential hang later if an interactive tool is |
| 306 | // used. This is a best-effort check. |
| 307 | console.warn( |
| 308 | `Cannot check tool "${toolName}" for interactivity because it requires parameters. Assuming it is safe for non-interactive use.`, |
| 309 | ); |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | const invocation = tool.build({}); |
| 314 | const confirmationDetails = await invocation.shouldConfirmExecute( |
| 315 | new AbortController().signal, |
| 316 | ); |
| 317 | if (confirmationDetails) { |
| 318 | throw new Error( |
| 319 | `Tool "${toolName}" requires user confirmation and cannot be used in a non-interactive subagent.`, |
| 320 | ); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return new SubAgentScope( |
| 327 | name, |
| 328 | runtimeContext, |
| 329 | promptConfig, |
| 330 | modelConfig, |
| 331 | runConfig, |
| 332 | toolConfig, |
| 333 | outputConfig, |
| 334 | ); |
| 335 | } |
no test coverage detected