329
collectedHooks = mergeHooks(collectedHooks, remapped);
330
}
332
>
// Build the agent request
333
>
const agentRequest: IChatAgentRequest = {
334
>
sessionResource: invocation.context.sessionResource,
335
>
requestId: invocation.callId ?? `subagent-${Date.now()}`,
336
>
agentId: defaultAgent.id,
337
>
message: args.prompt,
338
>
variables: { variables: variableSet.asArray() },
339
>
location: ChatAgentLocation.Chat,
340
>
subAgentInvocationId: subAgentInvocationId,
341
>
subAgentName: effectiveSubAgentName,
342
>
userSelectedModelId: modeModelId,
343
>
modelConfiguration: modeModelId ? this.languageModelsService.getModelConfiguration(modeModelId) : undefined,
344
>
userSelectedTools: modeTools,
345
>
modeInstructions,
346
>
parentRequestId: invocation.chatRequestId,
347
>
hooks: collectedHooks,
348
>
hasHooksEnabled: !!collectedHooks && Object.values(collectedHooks).some(arr => arr && arr.length > 0),
349
>
};
350
>
351
>
// Subscribe to tool invocations to clear markdown parts when a tool is invoked
352
>
store.add(this.languageModelToolsService.onDidInvokeTool(e => {
353
if (e.subagentInvocationId === subAgentInvocationId) {
354
markdownParts.length = 0;
355
}
357
>
358
>
// Invoke the agent, tracking nesting depth for recursion detection
359
>
this._sessionDepth.set(sessionKey, currentDepth + 1);
360
>
let result: IChatAgentResult | undefined;
361
>
try {
362
>
result = await this.chatAgentService.invokeAgent(
363
>
defaultAgent.id,
364
>
agentRequest,
365
>
progressCallback,
366
>
[],
367
>
token
368
>
);
369
>
} finally {
370
>
const newDepth = (this._sessionDepth.get(sessionKey) ?? 1) - 1;
371
>
if (newDepth <= 0) {
372
>
this._sessionDepth.delete(sessionKey);
373
>
} else {
374
this._sessionDepth.set(sessionKey, newDepth);
375
}
377
>
378
>
// Check for errors
379
>
if (result?.errorDetails) {
380
return createToolSimpleTextResult(`Agent error: ${result.errorDetails.message}`);
381
}