Merge pull request #536 from alibaba/fix/prevent-concurrent-execute
fix(core): prevent concurrent execute() calls
This commit is contained in:
@@ -192,10 +192,20 @@ export class PageAgentCore extends EventTarget {
|
|||||||
|
|
||||||
async execute(task: string): Promise<ExecutionResult> {
|
async execute(task: string): Promise<ExecutionResult> {
|
||||||
if (this.disposed) throw new Error('PageAgent has been disposed. Create a new instance.')
|
if (this.disposed) throw new Error('PageAgent has been disposed. Create a new instance.')
|
||||||
|
if (this.#status === 'running') throw new Error('A task is already running.')
|
||||||
if (!task) throw new Error('Task is required')
|
if (!task) throw new Error('Task is required')
|
||||||
|
|
||||||
this.task = task
|
this.task = task
|
||||||
this.taskId = uid()
|
this.taskId = uid()
|
||||||
|
|
||||||
|
this.history = []
|
||||||
|
this.#observations = []
|
||||||
|
this.#states = { totalWaitTime: 0, lastURL: '', browserState: null }
|
||||||
|
this.#abortController = new AbortController()
|
||||||
|
|
||||||
|
this.#setStatus('running')
|
||||||
|
this.#emitHistoryChange()
|
||||||
|
|
||||||
// Disable ask_user tool if onAskUser is not set
|
// Disable ask_user tool if onAskUser is not set
|
||||||
if (!this.onAskUser) {
|
if (!this.onAskUser) {
|
||||||
this.tools.delete('ask_user')
|
this.tools.delete('ask_user')
|
||||||
@@ -206,24 +216,14 @@ export class PageAgentCore extends EventTarget {
|
|||||||
const onBeforeTask = this.config.onBeforeTask
|
const onBeforeTask = this.config.onBeforeTask
|
||||||
const onAfterTask = this.config.onAfterTask
|
const onAfterTask = this.config.onAfterTask
|
||||||
|
|
||||||
await onBeforeTask?.(this)
|
try {
|
||||||
|
await onBeforeTask?.(this)
|
||||||
// Show mask
|
await this.pageController.showMask()
|
||||||
await this.pageController.showMask()
|
} catch (error) {
|
||||||
|
this.#setStatus('error')
|
||||||
if (this.#abortController) {
|
throw error
|
||||||
this.#abortController.abort()
|
|
||||||
this.#abortController = new AbortController()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.history = []
|
|
||||||
this.#setStatus('running')
|
|
||||||
this.#emitHistoryChange()
|
|
||||||
this.#observations = []
|
|
||||||
|
|
||||||
// Reset internal states
|
|
||||||
this.#states = { totalWaitTime: 0, lastURL: '', browserState: null }
|
|
||||||
|
|
||||||
let step = 0
|
let step = 0
|
||||||
let taskSuccess: boolean
|
let taskSuccess: boolean
|
||||||
let taskResult: string
|
let taskResult: string
|
||||||
|
|||||||
Reference in New Issue
Block a user