fix: abort may affect onAfterTask; clean up while loop

This commit is contained in:
Simon
2026-06-08 19:46:19 +08:00
parent 6530f0ef40
commit 4445bec08a

View File

@@ -225,6 +225,8 @@ export class PageAgentCore extends EventTarget {
this.#states = { totalWaitTime: 0, lastURL: '', browserState: null } this.#states = { totalWaitTime: 0, lastURL: '', browserState: null }
let step = 0 let step = 0
let taskSuccess: boolean
let taskResult: string
while (true) { while (true) {
try { try {
@@ -286,64 +288,49 @@ export class PageAgentCore extends EventTarget {
} as AgentStepEvent) } as AgentStepEvent)
this.#emitHistoryChange() this.#emitHistoryChange()
//
await onAfterStep?.(this, this.history) await onAfterStep?.(this, this.history)
console.groupEnd() console.groupEnd()
// finish task if done
if (actionName === 'done') { if (actionName === 'done') {
const success = action.input?.success ?? false taskSuccess = action.input?.success ?? false
const text = action.input?.text || 'no text provided' taskResult = action.input?.text || 'no text provided'
console.log(chalk.green.bold('Task completed'), success, text) console.log(chalk.green.bold('Task completed'), taskSuccess, taskResult)
this.#onDone(success) break
const result: ExecutionResult = {
success,
data: text,
history: this.history,
}
await onAfterTask?.(this, result)
return result
} }
} catch (error: unknown) { } catch (error: unknown) {
console.groupEnd() // to prevent nested groups console.groupEnd()
// Canonical abort check, independent of how the error was wrapped. const isAbortError = (error as any)?.name === 'AbortError'
const isAbortError = this.#abortController.signal.aborted
if (!isAbortError) console.error('Task failed', error) if (!isAbortError) console.error('Task failed', error)
const errorMessage = isAbortError ? 'Task stopped' : String(error) taskResult = isAbortError ? 'Task aborted' : String(error)
this.#emitActivity({ type: 'error', message: errorMessage }) taskSuccess = false
this.history.push({ type: 'error', message: errorMessage, rawResponse: error }) this.#emitActivity({ type: 'error', message: taskResult })
this.history.push({ type: 'error', message: taskResult, rawResponse: error })
this.#emitHistoryChange() this.#emitHistoryChange()
this.#onDone(false) break
const result: ExecutionResult = {
success: false,
data: errorMessage,
history: this.history,
}
await onAfterTask?.(this, result)
return result
} }
step++ step++
if (step > this.config.maxSteps) { if (step > this.config.maxSteps) {
const errorMessage = 'Step count exceeded maximum limit' taskResult = 'Step count exceeded maximum limit'
this.history.push({ type: 'error', message: errorMessage }) taskSuccess = false
this.#emitActivity({ type: 'error', message: taskResult })
this.history.push({ type: 'error', message: taskResult })
this.#emitHistoryChange() this.#emitHistoryChange()
this.#onDone(false) break
const result: ExecutionResult = {
success: false,
data: errorMessage,
history: this.history,
}
await onAfterTask?.(this, result)
return result
} }
await waitFor(this.config.stepDelay ?? 0.4) await waitFor(this.config.stepDelay ?? 0.4)
} }
this.#onDone(taskSuccess)
const result: ExecutionResult = {
success: taskSuccess,
data: taskResult,
history: this.history,
}
await onAfterTask?.(this, result)
return result
} }
/** /**