( options: SubAgentExecutionOptions, )
| 56 | */ |
| 57 | // eslint-disable-next-line complexity |
| 58 | export async function executeSubAgent( |
| 59 | options: SubAgentExecutionOptions, |
| 60 | ): Promise<SubAgentResult> { |
| 61 | const { agent: subAgent, prompt, abortController, onOutputUpdate } = options; |
| 62 | |
| 63 | const mainAgentPermissionsState = |
| 64 | await serviceContainer.get<ToolPermissionServiceState>( |
| 65 | SERVICE_NAMES.TOOL_PERMISSIONS, |
| 66 | ); |
| 67 | |
| 68 | try { |
| 69 | logger.debug("Starting subagent execution", { |
| 70 | agent: subAgent.model?.name, |
| 71 | }); |
| 72 | |
| 73 | const { model, llmApi } = subAgent; |
| 74 | if (!model || !llmApi) { |
| 75 | throw new Error("Model or LLM API not available"); |
| 76 | } |
| 77 | |
| 78 | // allow all tools for now |
| 79 | // todo: eventually we want to show the same prompt in a dialog whether asking whether that tool call is allowed or not |
| 80 | |
| 81 | serviceContainer.set<ToolPermissionServiceState>( |
| 82 | SERVICE_NAMES.TOOL_PERMISSIONS, |
| 83 | { |
| 84 | ...mainAgentPermissionsState, |
| 85 | permissions: { |
| 86 | policies: [{ tool: "*", permission: "allow" }], |
| 87 | }, |
| 88 | }, |
| 89 | ); |
| 90 | |
| 91 | // Build agent system message |
| 92 | const systemMessage = await buildAgentSystemMessage(subAgent, services); |
| 93 | |
| 94 | // Store original system message function |
| 95 | const originalGetSystemMessage = services.systemMessage?.getSystemMessage; |
| 96 | |
| 97 | // Store original ChatHistoryService ready state |
| 98 | const chatHistorySvc = services.chatHistory; |
| 99 | const originalIsReady = |
| 100 | chatHistorySvc && typeof chatHistorySvc.isReady === "function" |
| 101 | ? chatHistorySvc.isReady |
| 102 | : undefined; |
| 103 | |
| 104 | // Override system message for this execution |
| 105 | if (services.systemMessage) { |
| 106 | services.systemMessage.getSystemMessage = async () => systemMessage; |
| 107 | } |
| 108 | |
| 109 | // Temporarily disable ChatHistoryService to prevent it from interfering with child session |
| 110 | if (chatHistorySvc && originalIsReady) { |
| 111 | chatHistorySvc.isReady = () => false; |
| 112 | } |
| 113 | |
| 114 | const chatHistory = [ |
| 115 | { |
no test coverage detected