feat: observation
This commit is contained in:
@@ -118,8 +118,13 @@ export class PageAgent extends EventTarget {
|
|||||||
/** PageController for DOM operations */
|
/** PageController for DOM operations */
|
||||||
pageController: PageController
|
pageController: PageController
|
||||||
|
|
||||||
/** Accumulated wait time in seconds, used by wait tool to track total waiting */
|
/** Runtime states for tracking across steps */
|
||||||
totalWaitTime = 0
|
states = {
|
||||||
|
/** Accumulated wait time in seconds, used by wait tool */
|
||||||
|
totalWaitTime: 0,
|
||||||
|
/** Last known URL for detecting navigation */
|
||||||
|
lastURL: '',
|
||||||
|
}
|
||||||
|
|
||||||
/** History event stream */
|
/** History event stream */
|
||||||
history: HistoryEvent[] = []
|
history: HistoryEvent[] = []
|
||||||
@@ -214,10 +219,18 @@ export class PageAgent extends EventTarget {
|
|||||||
|
|
||||||
this.history = []
|
this.history = []
|
||||||
|
|
||||||
|
// Reset states
|
||||||
|
this.states = {
|
||||||
|
totalWaitTime: 0,
|
||||||
|
lastURL: '',
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let step = 0
|
let step = 0
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
await this.#generateObservations(step)
|
||||||
|
|
||||||
await onBeforeStep.call(this, step)
|
await onBeforeStep.call(this, step)
|
||||||
|
|
||||||
console.group(`step: ${step}`)
|
console.group(`step: ${step}`)
|
||||||
@@ -382,7 +395,7 @@ export class PageAgent extends EventTarget {
|
|||||||
|
|
||||||
// Reset wait time for non-wait tools
|
// Reset wait time for non-wait tools
|
||||||
if (toolName !== 'wait') {
|
if (toolName !== 'wait') {
|
||||||
this.totalWaitTime = 0
|
this.states.totalWaitTime = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Briefly display execution result
|
// Briefly display execution result
|
||||||
@@ -459,6 +472,34 @@ export class PageAgent extends EventTarget {
|
|||||||
return result
|
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<void> {
|
||||||
|
// 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<string> {
|
async #assembleUserPrompt(): Promise<string> {
|
||||||
let prompt = ''
|
let prompt = ''
|
||||||
|
|
||||||
|
|||||||
@@ -58,11 +58,11 @@ tools.set(
|
|||||||
console.log(`actualWaitTime: ${actualWaitTime} seconds`)
|
console.log(`actualWaitTime: ${actualWaitTime} seconds`)
|
||||||
await waitFor(actualWaitTime)
|
await waitFor(actualWaitTime)
|
||||||
|
|
||||||
this.totalWaitTime += input.seconds
|
this.states.totalWaitTime += input.seconds
|
||||||
|
|
||||||
if (this.totalWaitTime >= 3) {
|
if (this.states.totalWaitTime >= 3) {
|
||||||
this.pushObservation(
|
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.`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user