fix: mke sure taskResult and onAfterStep wont get lost
This commit is contained in:
@@ -309,23 +309,31 @@ export class PageAgentCore extends EventTarget {
|
|||||||
const data = action.input?.text || 'no text provided'
|
const data = action.input?.text || 'no text provided'
|
||||||
console.log(chalk.green.bold('Task completed'), success, data)
|
console.log(chalk.green.bold('Task completed'), success, data)
|
||||||
taskResult = { success, data, history: this.history }
|
taskResult = { success, data, history: this.history }
|
||||||
|
this.#lastResult = taskResult
|
||||||
this.#setStatus('completed')
|
this.#setStatus('completed')
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
|
// catch block must not throw error. otherwise the error may be overridden if finally block also throws error.
|
||||||
|
|
||||||
const isAbortError = (error as any)?.name === 'AbortError'
|
const isAbortError = (error as any)?.name === 'AbortError'
|
||||||
if (!isAbortError) console.error('Task failed', error)
|
if (!isAbortError) console.error('Task failed', error)
|
||||||
const message = isAbortError ? 'Task aborted' : String(error)
|
const message = isAbortError ? 'Task aborted' : String(error)
|
||||||
this.#emitActivity({ type: 'error', message: message })
|
this.#emitActivity({ type: 'error', message: message })
|
||||||
this.#emitHistoryChange({ type: 'error', message: message, rawResponse: error })
|
this.#emitHistoryChange({ type: 'error', message: message, rawResponse: error })
|
||||||
taskResult = { success: false, data: message, history: this.history }
|
taskResult = { success: false, data: message, history: this.history }
|
||||||
|
this.#lastResult = taskResult
|
||||||
this.#setStatus(isAbortError ? 'stopped' : 'error')
|
this.#setStatus(isAbortError ? 'stopped' : 'error')
|
||||||
break
|
break
|
||||||
} finally {
|
} finally {
|
||||||
console.groupEnd()
|
// finally block runs before the break above.
|
||||||
}
|
|
||||||
|
|
||||||
|
console.groupEnd()
|
||||||
|
// @note hook may throw error.
|
||||||
|
// which will override the `break` above and be handled as an external error.
|
||||||
|
// as expected.
|
||||||
await onAfterStep?.(this, this.history)
|
await onAfterStep?.(this, this.history)
|
||||||
|
}
|
||||||
|
|
||||||
step++
|
step++
|
||||||
if (step > this.config.maxSteps) {
|
if (step > this.config.maxSteps) {
|
||||||
@@ -334,6 +342,7 @@ export class PageAgentCore extends EventTarget {
|
|||||||
this.#emitActivity({ type: 'error', message: message })
|
this.#emitActivity({ type: 'error', message: message })
|
||||||
this.#emitHistoryChange({ type: 'error', message: message })
|
this.#emitHistoryChange({ type: 'error', message: message })
|
||||||
taskResult = { success: false, data: message, history: this.history }
|
taskResult = { success: false, data: message, history: this.history }
|
||||||
|
this.#lastResult = taskResult
|
||||||
this.#setStatus('error')
|
this.#setStatus('error')
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -343,7 +352,6 @@ export class PageAgentCore extends EventTarget {
|
|||||||
|
|
||||||
await onAfterTask?.(this, taskResult)
|
await onAfterTask?.(this, taskResult)
|
||||||
|
|
||||||
this.#lastResult = taskResult
|
|
||||||
return taskResult
|
return taskResult
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.#emitActivity({ type: 'error', message: String(error) })
|
this.#emitActivity({ type: 'error', message: String(error) })
|
||||||
|
|||||||
Reference in New Issue
Block a user