1- import * as readline from 'readline/promises' ;
2- import { stdin , stdout } from 'node:process' ;
31import { Agent , run , hostedMcpTool , RunToolApprovalItem } from '@openai/agents' ;
2+ import * as readline from 'node:readline/promises' ;
3+ import { stdin , stdout } from 'node:process' ;
44
5- async function promptApproval ( item : RunToolApprovalItem ) : Promise < boolean > {
5+ async function confirm ( item : RunToolApprovalItem ) : Promise < boolean > {
66 const rl = readline . createInterface ( { input : stdin , output : stdout } ) ;
77 const name = item . rawItem . name ;
88 const params = JSON . parse ( item . rawItem . providerData ?. arguments || '{}' ) ;
@@ -14,9 +14,7 @@ async function promptApproval(item: RunToolApprovalItem): Promise<boolean> {
1414}
1515
1616async function main ( verbose : boolean , stream : boolean ) : Promise < void > {
17- // 'always' |
18- // 'never' |
19- // { never?: { toolNames: string[] }; always?: { toolNames: string[] } }
17+ // 'always' | 'never' | { never, always }
2018 const requireApproval = {
2119 never : { toolNames : [ 'search_codex_code' , 'fetch_codex_documentation' ] } ,
2220 always : { toolNames : [ 'fetch_generic_url_content' ] } ,
@@ -54,17 +52,18 @@ async function main(verbose: boolean, stream: boolean): Promise<void> {
5452 console . log ( `Done streaming; final result: ${ result . finalOutput } ` ) ;
5553 } else {
5654 // Non-streaming
57- let result = await run ( agent , input , { maxTurns : 100 } ) ;
55+ let result = await run ( agent , input ) ;
5856 while ( result . interruptions && result . interruptions . length ) {
5957 for ( const interruption of result . interruptions ) {
60- const approval = await promptApproval ( interruption ) ;
58+ // Human in the loop here
59+ const approval = await confirm ( interruption ) ;
6160 if ( approval ) {
6261 result . state . approve ( interruption ) ;
6362 } else {
6463 result . state . reject ( interruption ) ;
6564 }
6665 }
67- result = await run ( agent , result . state , { maxTurns : 100 } ) ;
66+ result = await run ( agent , result . state ) ;
6867 }
6968 console . log ( result . finalOutput ) ;
7069
0 commit comments