diff --git a/packages/page-agent/src/PageAgent.ts b/packages/page-agent/src/PageAgent.ts index c1402ec..cdf6efe 100644 --- a/packages/page-agent/src/PageAgent.ts +++ b/packages/page-agent/src/PageAgent.ts @@ -118,8 +118,13 @@ export class PageAgent extends EventTarget { /** PageController for DOM operations */ pageController: PageController - /** Accumulated wait time in seconds, used by wait tool to track total waiting */ - totalWaitTime = 0 + /** Runtime states for tracking across steps */ + states = { + /** Accumulated wait time in seconds, used by wait tool */ + totalWaitTime: 0, + /** Last known URL for detecting navigation */ + lastURL: '', + } /** History event stream */ history: HistoryEvent[] = [] @@ -214,10 +219,18 @@ export class PageAgent extends EventTarget { this.history = [] + // Reset states + this.states = { + totalWaitTime: 0, + lastURL: '', + } + try { let step = 0 while (true) { + await this.#generateObservations(step) + await onBeforeStep.call(this, step) console.group(`step: ${step}`) @@ -382,7 +395,7 @@ export class PageAgent extends EventTarget { // Reset wait time for non-wait tools if (toolName !== 'wait') { - this.totalWaitTime = 0 + this.states.totalWaitTime = 0 } // Briefly display execution result @@ -459,6 +472,34 @@ export class PageAgent extends EventTarget { return result } + /** + * Generate observations before each step + * - URL change detection + * - Too many steps warning + * @todo loop detection + * @todo console error + */ + async #generateObservations(stepCount: number): Promise { + // Detect URL change + const currentURL = await this.pageController.getCurrentUrl() + if (this.states.lastURL && currentURL !== this.states.lastURL) { + this.pushObservation(`Page navigated to → ${currentURL}`) + } + this.states.lastURL = currentURL + + // Warn about remaining steps + const remaining = MAX_STEPS - stepCount + if (remaining === 5) { + this.pushObservation( + `⚠️ Only ${remaining} steps remaining. Consider wrapping up or calling done with partial results.` + ) + } else if (remaining === 2) { + this.pushObservation( + `⚠️ Critical: Only ${remaining} steps left! You must finish the task or call done immediately.` + ) + } + } + async #assembleUserPrompt(): Promise { let prompt = '' @@ -544,7 +585,7 @@ export class PageAgent extends EventTarget { return trimLines(` Current Page: [${state.title}](${state.url}) - + ${state.header} ${content} ${state.footer} diff --git a/packages/page-agent/src/tools/index.ts b/packages/page-agent/src/tools/index.ts index 4f18afc..7415009 100644 --- a/packages/page-agent/src/tools/index.ts +++ b/packages/page-agent/src/tools/index.ts @@ -58,11 +58,11 @@ tools.set( console.log(`actualWaitTime: ${actualWaitTime} seconds`) await waitFor(actualWaitTime) - this.totalWaitTime += input.seconds + this.states.totalWaitTime += input.seconds - if (this.totalWaitTime >= 3) { + if (this.states.totalWaitTime >= 3) { this.pushObservation( - `You have waited ${this.totalWaitTime} seconds accumulatively. Do NOT wait any longer unless you have a good reason.` + `You have waited ${this.states.totalWaitTime} seconds accumulatively. Do NOT wait any longer unless you have a good reason.` ) }