feat: change observation timing
This commit is contained in:
@@ -67,6 +67,7 @@ export class PageAgentCore extends EventTarget {
|
|||||||
|
|
||||||
#llm: LLM
|
#llm: LLM
|
||||||
#abortController = new AbortController()
|
#abortController = new AbortController()
|
||||||
|
#observations: string[] = []
|
||||||
|
|
||||||
/** PageController for DOM operations */
|
/** PageController for DOM operations */
|
||||||
pageController: PageController
|
pageController: PageController
|
||||||
@@ -165,12 +166,13 @@ export class PageAgentCore extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Push a persistent observation to the history event stream.
|
* Push a observation message to the history event stream.
|
||||||
* This will be visible in <agent_history> and remain in memory across steps.
|
* 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 {
|
pushObservation(content: string): void {
|
||||||
this.history.push({ type: 'observation', content })
|
this.#observations.push(content)
|
||||||
this.#emitHistoryChange()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async execute(task: string): Promise<ExecutionResult> {
|
async execute(task: string): Promise<ExecutionResult> {
|
||||||
@@ -214,7 +216,15 @@ export class PageAgentCore extends EventTarget {
|
|||||||
try {
|
try {
|
||||||
console.group(`step: ${step}`)
|
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)
|
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
|
* - URL change detection
|
||||||
* - Too many steps warning
|
* - Too many steps warning
|
||||||
* @todo loop detection
|
* @todo loop detection
|
||||||
* @todo console error
|
* @todo console error
|
||||||
*/
|
*/
|
||||||
async #generateObservations(stepCount: number): Promise<void> {
|
async #systemObservations(stepCount: number): Promise<void> {
|
||||||
// Detect URL change
|
// Detect URL change
|
||||||
const currentURL = await this.pageController.getCurrentUrl()
|
const currentURL = await this.pageController.getCurrentUrl()
|
||||||
if (currentURL !== this.states.lastURL) {
|
if (currentURL !== this.states.lastURL) {
|
||||||
|
|||||||
@@ -376,13 +376,6 @@ const result = await agent.execute('Fill in the form with test data')`}
|
|||||||
? '执行任务并返回结果。包含 success、data 和 history 字段。'
|
? '执行任务并返回结果。包含 success、data 和 history 字段。'
|
||||||
: 'Execute a task and return result. Contains success, data, and history fields.',
|
: '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)',
|
name: 'dispose(reason?: string)',
|
||||||
type: 'void',
|
type: 'void',
|
||||||
|
|||||||
Reference in New Issue
Block a user