feat: change observation timing

This commit is contained in:
Simon
2026-02-09 21:05:29 +08:00
parent 1517bc4868
commit 840162df8c
2 changed files with 17 additions and 14 deletions

View File

@@ -67,6 +67,7 @@ export class PageAgentCore extends EventTarget {
#llm: LLM
#abortController = new AbortController()
#observations: string[] = []
/** PageController for DOM operations */
pageController: PageController
@@ -165,12 +166,13 @@ export class PageAgentCore extends EventTarget {
}
/**
* Push a persistent observation to the history event stream.
* This will be visible in <agent_history> and remain in memory across steps.
* Push a observation message to the history event stream.
* This will be visible in <agent_history> and remain persistent in memory across steps.
* @experimental @internal
* @note history change will be emitted before next step starts
*/
pushObservation(content: string): void {
this.history.push({ type: 'observation', content })
this.#emitHistoryChange()
this.#observations.push(content)
}
async execute(task: string): Promise<ExecutionResult> {
@@ -214,7 +216,15 @@ export class PageAgentCore extends EventTarget {
try {
console.group(`step: ${step}`)
await this.#generateObservations(step)
await this.#systemObservations(step)
if (this.#observations.length > 0) {
for (const content of this.#observations) {
this.history.push({ type: 'observation', content })
}
this.#observations = []
this.#emitHistoryChange()
}
await onBeforeStep?.(this, step)
@@ -468,13 +478,13 @@ export class PageAgentCore extends EventTarget {
}
/**
* Generate observations before each step
* Generate system observations before each step
* - URL change detection
* - Too many steps warning
* @todo loop detection
* @todo console error
*/
async #generateObservations(stepCount: number): Promise<void> {
async #systemObservations(stepCount: number): Promise<void> {
// Detect URL change
const currentURL = await this.pageController.getCurrentUrl()
if (currentURL !== this.states.lastURL) {

View File

@@ -376,13 +376,6 @@ const result = await agent.execute('Fill in the form with test data')`}
? '执行任务并返回结果。包含 success、data 和 history 字段。'
: 'Execute a task and return result. Contains success, data, and history fields.',
},
{
name: 'pushObservation(content: string)',
type: 'void',
description: isZh
? '向历史流推送一个观察事件,会在下一步时被 LLM 看到'
: 'Push an observation to history stream, will be seen by LLM in next step',
},
{
name: 'dispose(reason?: string)',
type: 'void',