@@ -302,19 +302,18 @@ export async function executeInterruptedToolsAndSideEffects<TContext>(
302
302
runner : Runner ,
303
303
state : RunState < TContext , Agent < TContext , any > > ,
304
304
) : 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 ) ;
315
314
// Run function tools that require approval after they get their approval results
316
315
const functionToolRuns = processedResponse . functions . filter ( ( run ) => {
317
- return approvalRequestCallIds . includes ( run . toolCall . callId ) ;
316
+ return functionCallIds . includes ( run . toolCall . callId ) ;
318
317
} ) ;
319
318
320
319
const functionResults = await executeFunctionToolCalls (
@@ -324,7 +323,7 @@ export async function executeInterruptedToolsAndSideEffects<TContext>(
324
323
state ,
325
324
) ;
326
325
327
- // The output items
326
+ // Create the initial set of the output items
328
327
const newItems : RunItem [ ] = functionResults . map ( ( r ) => r . runItem ) ;
329
328
330
329
// Run MCP tools that require approval after they get their approval results
@@ -338,17 +337,20 @@ export async function executeInterruptedToolsAndSideEffects<TContext>(
338
337
} ,
339
338
) ;
340
339
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 ! ;
342
342
const approved = state . _context . isToolApproved ( {
343
+ // Since this item name must be the same with the one sent from Responses API server
343
344
toolName : run . requestItem . rawItem . name ,
344
- callId,
345
+ callId : approvalRequestId ,
345
346
} ) ;
346
347
if ( typeof approved !== 'undefined' ) {
347
348
const providerData : ProviderData . HostedMCPApprovalResponse = {
348
349
approve : approved ,
349
- approvalRequestId : callId ,
350
+ approvalRequestId,
350
351
reason : undefined ,
351
352
} ;
353
+ // Tell Responses API server the approval result in the next turn
352
354
newItems . push (
353
355
new RunToolCallItem (
354
356
{
@@ -368,6 +370,12 @@ export async function executeInterruptedToolsAndSideEffects<TContext>(
368
370
state ,
369
371
) ;
370
372
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
+
371
379
if ( checkToolOutput . isFinalOutput ) {
372
380
runner . emit (
373
381
'agent_end' ,
0 commit comments