Skip to content

Commit 5239872

Browse files
committed
Refactor
1 parent d7835c8 commit 5239872

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

packages/agents-core/src/runImplementation.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -302,19 +302,18 @@ export async function executeInterruptedToolsAndSideEffects<TContext>(
302302
runner: Runner,
303303
state: RunState<TContext, Agent<TContext, any>>,
304304
): Promise<SingleStepResult> {
305-
const preStepItems = originalPreStepItems.filter((item) => {
306-
return !(item instanceof RunToolApprovalItem);
307-
});
308-
const approvalRequestCallIds = originalPreStepItems
309-
.filter((item) => {
310-
return item instanceof RunToolApprovalItem && 'callId' in item.rawItem;
311-
})
312-
.map((item) => {
313-
return 'callId' in item.rawItem && item.rawItem.callId!;
314-
});
305+
// call_ids for function tools
306+
const functionCallIds = originalPreStepItems
307+
.filter(
308+
(item) =>
309+
item instanceof RunToolApprovalItem &&
310+
'callId' in item.rawItem &&
311+
item.rawItem.type === 'function_call',
312+
)
313+
.map((item) => (item.rawItem as protocol.FunctionCallItem).callId);
315314
// Run function tools that require approval after they get their approval results
316315
const functionToolRuns = processedResponse.functions.filter((run) => {
317-
return approvalRequestCallIds.includes(run.toolCall.callId);
316+
return functionCallIds.includes(run.toolCall.callId);
318317
});
319318

320319
const functionResults = await executeFunctionToolCalls(
@@ -324,7 +323,7 @@ export async function executeInterruptedToolsAndSideEffects<TContext>(
324323
state,
325324
);
326325

327-
// The output items
326+
// Create the initial set of the output items
328327
const newItems: RunItem[] = functionResults.map((r) => r.runItem);
329328

330329
// Run MCP tools that require approval after they get their approval results
@@ -338,17 +337,20 @@ export async function executeInterruptedToolsAndSideEffects<TContext>(
338337
},
339338
);
340339
for (const run of mcpApprovalRuns) {
341-
const callId = run.requestItem.rawItem.id!;
340+
// the approval_request_id "mcpr_123..."
341+
const approvalRequestId = run.requestItem.rawItem.id!;
342342
const approved = state._context.isToolApproved({
343+
// Since this item name must be the same with the one sent from Responses API server
343344
toolName: run.requestItem.rawItem.name,
344-
callId,
345+
callId: approvalRequestId,
345346
});
346347
if (typeof approved !== 'undefined') {
347348
const providerData: ProviderData.HostedMCPApprovalResponse = {
348349
approve: approved,
349-
approvalRequestId: callId,
350+
approvalRequestId,
350351
reason: undefined,
351352
};
353+
// Tell Responses API server the approval result in the next turn
352354
newItems.push(
353355
new RunToolCallItem(
354356
{
@@ -368,6 +370,12 @@ export async function executeInterruptedToolsAndSideEffects<TContext>(
368370
state,
369371
);
370372

373+
// Exclude the tool approval items, which should not be sent to Responses API,
374+
// from the SingleStepResult's preStepItems
375+
const preStepItems = originalPreStepItems.filter((item) => {
376+
return !(item instanceof RunToolApprovalItem);
377+
});
378+
371379
if (checkToolOutput.isFinalOutput) {
372380
runner.emit(
373381
'agent_end',

0 commit comments

Comments
 (0)