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> {
|
||||
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')
|
||||
|
||||
this.task = task
|
||||
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
|
||||
if (!this.onAskUser) {
|
||||
this.tools.delete('ask_user')
|
||||
@@ -206,24 +216,14 @@ export class PageAgentCore extends EventTarget {
|
||||
const onBeforeTask = this.config.onBeforeTask
|
||||
const onAfterTask = this.config.onAfterTask
|
||||
|
||||
try {
|
||||
await onBeforeTask?.(this)
|
||||
|
||||
// Show mask
|
||||
await this.pageController.showMask()
|
||||
|
||||
if (this.#abortController) {
|
||||
this.#abortController.abort()
|
||||
this.#abortController = new AbortController()
|
||||
} catch (error) {
|
||||
this.#setStatus('error')
|
||||
throw error
|
||||
}
|
||||
|
||||
this.history = []
|
||||
this.#setStatus('running')
|
||||
this.#emitHistoryChange()
|
||||
this.#observations = []
|
||||
|
||||
// Reset internal states
|
||||
this.#states = { totalWaitTime: 0, lastURL: '', browserState: null }
|
||||
|
||||
let step = 0
|
||||
let taskSuccess: boolean
|
||||
let taskResult: string
|
||||
|
||||
Reference in New Issue
Block a user