@@ -87,14 +87,17 @@ async function readError(response: Response): Promise<string> {
8787
8888async function pollForCompletion (
8989 runId : string ,
90- apiKey : string
90+ apiKey : string ,
91+ signal ?: AbortSignal
9192) : Promise < { summary ?: V4RunSummary ; error ?: string } > {
9293 const deadline = Date . now ( ) + MAX_POLL_TIME_MS
9394
9495 for ( ; ; ) {
96+ signal ?. throwIfAborted ( )
9597 const statusResponse = await fetch ( `${ API_BASE } /runs/${ runId } /status` , {
9698 method : 'GET' ,
9799 headers : { 'X-Browser-Use-API-Key' : apiKey } ,
100+ signal,
98101 } )
99102 if ( ! statusResponse . ok ) {
100103 return { error : `Failed to read run status: ${ await readError ( statusResponse ) } ` }
@@ -105,6 +108,7 @@ async function pollForCompletion(
105108 const runResponse = await fetch ( `${ API_BASE } /runs/${ runId } ` , {
106109 method : 'GET' ,
107110 headers : { 'X-Browser-Use-API-Key' : apiKey } ,
111+ signal,
108112 } )
109113 if ( ! runResponse . ok ) {
110114 return { error : `Failed to read completed run: ${ await readError ( runResponse ) } ` }
@@ -115,6 +119,7 @@ async function pollForCompletion(
115119 if ( Date . now ( ) >= deadline ) {
116120 return { error : `Run did not complete within ${ MAX_POLL_TIME_MS / 1000 } s` }
117121 }
122+ signal ?. throwIfAborted ( )
118123 await sleep ( POLL_INTERVAL_MS )
119124 }
120125}
@@ -201,7 +206,10 @@ export const runV4Tool: ToolConfig<BrowserUseRunV4Params, BrowserUseRunV4Respons
201206 } ,
202207 } ,
203208
204- directExecution : async ( params : BrowserUseRunV4Params ) : Promise < ToolResponse > => {
209+ directExecution : async (
210+ params : BrowserUseRunV4Params ,
211+ signal ?: AbortSignal
212+ ) : Promise < ToolResponse > => {
205213 const body : Record < string , unknown > = { task : params . task }
206214 if ( params . model ) body . model = params . model
207215 if ( params . sessionId ) body . sessionId = params . sessionId
@@ -226,6 +234,7 @@ export const runV4Tool: ToolConfig<BrowserUseRunV4Params, BrowserUseRunV4Respons
226234 'X-Browser-Use-API-Key' : params . apiKey ,
227235 } ,
228236 body : JSON . stringify ( body ) ,
237+ signal,
229238 } )
230239 if ( ! response . ok ) {
231240 return {
@@ -237,7 +246,7 @@ export const runV4Tool: ToolConfig<BrowserUseRunV4Params, BrowserUseRunV4Respons
237246
238247 const created = ( await response . json ( ) ) as V4RunCreateResponse
239248 logger . info ( `Created Browser Use V4 run ${ created . id } ` )
240- const completed = await pollForCompletion ( created . id , params . apiKey )
249+ const completed = await pollForCompletion ( created . id , params . apiKey , signal )
241250 if ( ! completed . summary ) {
242251 return { success : false , output : emptyOutput ( created ) , error : completed . error }
243252 }
0 commit comments