@@ -20,7 +20,15 @@ import type {
2020 MacroToolInput ,
2121 MacroToolResult ,
2222} from './types'
23- import { assert , fetchLlmsTxt , normalizeResponse , uid , waitFor } from './utils'
23+ import {
24+ assert ,
25+ fetchLlmsTxt ,
26+ getEventDetail ,
27+ isAbortError ,
28+ normalizeResponse ,
29+ uid ,
30+ waitFor ,
31+ } from './utils'
2432
2533export { tool , type PageAgentTool } from './tools'
2634export type * from './types'
@@ -104,27 +112,35 @@ export class PageAgentCore extends EventTarget {
104112
105113 // Listen to LLM retry events
106114 this . #llm. addEventListener ( 'retry' , ( e ) => {
107- const { attempt, maxAttempts } = ( e as CustomEvent ) . detail
108- this . #emitActivity( { type : 'retrying' , attempt, maxAttempts } )
115+ const detail = getEventDetail < { attempt : number ; maxAttempts : number } > ( e )
116+ if ( ! detail ) return
117+ this . #emitActivity( {
118+ type : 'retrying' ,
119+ attempt : detail . attempt ,
120+ maxAttempts : detail . maxAttempts ,
121+ } )
109122 // Also push to history for panel rendering
110123 this . history . push ( {
111124 type : 'retry' ,
112- message : `LLM retry attempt ${ attempt } of ${ maxAttempts } ` ,
113- attempt,
114- maxAttempts,
125+ message : `LLM retry attempt ${ detail . attempt } of ${ detail . maxAttempts } ` ,
126+ attempt : detail . attempt ,
127+ maxAttempts : detail . maxAttempts ,
115128 } )
116129 this . #emitHistoryChange( )
117130 } )
118131 this . #llm. addEventListener ( 'error' , ( e ) => {
119- const error = ( e as CustomEvent ) . detail . error as Error | InvokeError
120- if ( ( error as any ) ?. rawError ?. name === 'AbortError' ) return
121- const message = String ( error )
132+ const detail = getEventDetail < { error : unknown } > ( e )
133+ if ( ! detail ) return
134+ const error = detail . error
135+ if ( isAbortError ( error ) ) return
136+ const message = error instanceof Error ? error . message : String ( error )
122137 this . #emitActivity( { type : 'error' , message } )
123138 // Also push to history for panel rendering
124139 this . history . push ( {
125140 type : 'error' ,
126141 message,
127- rawResponse : ( error as InvokeError ) . rawResponse ,
142+ rawResponse :
143+ error instanceof Error ? ( error as InvokeError ) . rawResponse : undefined ,
128144 } )
129145 this . #emitHistoryChange( )
130146 } )
@@ -311,10 +327,10 @@ export class PageAgentCore extends EventTarget {
311327 }
312328 } catch ( error : unknown ) {
313329 console . groupEnd ( ) // to prevent nested groups
314- const isAbortError = ( error as any ) ?. rawError ?. name === 'AbortError'
330+ const isAborted = isAbortError ( error )
315331
316332 console . error ( 'Task failed' , error )
317- const errorMessage = isAbortError ? 'Task stopped' : String ( error )
333+ const errorMessage = isAborted ? 'Task stopped' : String ( error )
318334 this . #emitActivity( { type : 'error' , message : errorMessage } )
319335 this . history . push ( { type : 'error' , message : errorMessage , rawResponse : error } )
320336 this . #emitHistoryChange( )
@@ -511,7 +527,8 @@ export class PageAgentCore extends EventTarget {
511527 // Accumulated wait time warning
512528 if ( this . #states. totalWaitTime >= 3 ) {
513529 this . pushObservation (
514- `You have waited ${ this . #states. totalWaitTime } seconds accumulatively. DO NOT wait any longer unless you have a good reason.`
530+ `You have waited ${ this . #states. totalWaitTime } seconds accumulatively. ` +
531+ `DO NOT wait any longer unless you have a good reason.`
515532 )
516533 }
517534
@@ -527,11 +544,13 @@ export class PageAgentCore extends EventTarget {
527544 const remaining = this . config . maxSteps - step
528545 if ( remaining === 5 ) {
529546 this . pushObservation (
530- `⚠️ Only ${ remaining } steps remaining. Consider wrapping up or calling done with partial results.`
547+ `⚠️ Only ${ remaining } steps remaining. ` +
548+ `Consider wrapping up or calling done with partial results.`
531549 )
532550 } else if ( remaining === 2 ) {
533551 this . pushObservation (
534- `⚠️ Critical: Only ${ remaining } steps left! You must finish the task or call done immediately.`
552+ `⚠️ Critical: Only ${ remaining } steps left! ` +
553+ `You must finish the task or call done immediately.`
535554 )
536555 }
537556
0 commit comments