@@ -45,6 +45,7 @@ const taskStatusResponseSchema = z.object({
4545 output : z . unknown ( ) . optional ( ) ,
4646 steps : z . array ( taskStepSchema ) . optional ( ) ,
4747} )
48+ const SESSION_CLEANUP_TIMEOUT_MS = 10_000
4849
4950const createTaskResponseSchema = z . object ( {
5051 id : z . string ( ) . min ( 1 ) ,
@@ -159,6 +160,7 @@ async function stopSession(sessionId: string, apiKey: string): Promise<void> {
159160 const response = await fetchBrowserUse ( `/sessions/${ encodeURIComponent ( sessionId ) } ` , apiKey , {
160161 method : 'PATCH' ,
161162 body : { action : 'stop' } ,
163+ signal : AbortSignal . timeout ( SESSION_CLEANUP_TIMEOUT_MS ) ,
162164 } )
163165
164166 if ( response . ok ) {
@@ -313,10 +315,11 @@ interface PollResult {
313315async function pollForCompletion (
314316 taskId : string ,
315317 apiKey : string ,
318+ initialSessionId : string | null ,
316319 signal ?: AbortSignal
317320) : Promise < PollResult > {
318321 let consecutiveErrors = 0
319- let sessionId : string | null = null
322+ let sessionId = initialSessionId
320323 let liveUrl : string | null = null
321324 let publicShareUrl : string | null = null
322325 const startTime = Date . now ( )
@@ -364,13 +367,20 @@ async function pollForCompletion(
364367 }
365368
366369 if ( [ 'finished' , 'failed' , 'stopped' ] . includes ( status ) ) {
370+ const output = taskData . output ?? null
367371 return {
368372 success : status === 'finished' ,
369- output : taskData . output ?? null ,
373+ output,
370374 steps : taskData . steps ?? [ ] ,
371375 sessionId,
372376 liveUrl,
373377 publicShareUrl,
378+ error :
379+ status === 'finished'
380+ ? undefined
381+ : typeof output === 'string' && output . trim ( )
382+ ? `BrowserUse task ${ status } : ${ output . trim ( ) } `
383+ : `BrowserUse task ${ status } ` ,
374384 }
375385 }
376386
@@ -379,13 +389,21 @@ async function pollForCompletion(
379389
380390 const finalResult = await fetchTaskStatus ( taskId , apiKey , signal )
381391 if ( finalResult . ok && [ 'finished' , 'failed' , 'stopped' ] . includes ( finalResult . data . status ) ) {
392+ const status = finalResult . data . status
393+ const output = finalResult . data . output ?? null
382394 return {
383- success : finalResult . data . status === 'finished' ,
384- output : finalResult . data . output ?? null ,
395+ success : status === 'finished' ,
396+ output,
385397 steps : finalResult . data . steps ?? [ ] ,
386398 sessionId : finalResult . data . sessionId ?? sessionId ,
387399 liveUrl,
388400 publicShareUrl,
401+ error :
402+ status === 'finished'
403+ ? undefined
404+ : typeof output === 'string' && output . trim ( )
405+ ? `BrowserUse task ${ status } : ${ output . trim ( ) } `
406+ : `BrowserUse task ${ status } ` ,
389407 }
390408 }
391409
@@ -497,7 +515,7 @@ export const executeRunTaskOperation: InternalToolOperationImplementation<
497515 const initialSessionId = sessionId ?? data . sessionId ?? null
498516 logger . info ( `Created BrowserUse task ${ taskId } ` , { sessionId : initialSessionId } )
499517
500- const result = await pollForCompletion ( taskId , params . apiKey , signal )
518+ const result = await pollForCompletion ( taskId , params . apiKey , initialSessionId , signal )
501519
502520 const finalSessionId = result . sessionId ?? initialSessionId
503521 const shareUrl =
0 commit comments