feat(agent)!: rename brain to reflection; fix undefined lines in panel

This commit is contained in:
Simon
2026-01-15 20:45:40 +08:00
parent ee4719cdcf
commit 8f5f98f889

View File

@@ -16,7 +16,7 @@ import { normalizeResponse, trimLines, uid, waitUntil } from './utils'
import { assert } from './utils/assert' import { assert } from './utils/assert'
/** /**
* Agent brain state - the reflection-before-action model * Agent reflection state - the reflection-before-action model
* *
* Every tool call must first reflect on: * Every tool call must first reflect on:
* - evaluation_previous_goal: How well did the previous action achieve its goal? * - evaluation_previous_goal: How well did the previous action achieve its goal?
@@ -55,7 +55,7 @@ export { tool, type PageAgentTool } from './tools'
*/ */
export interface AgentStep { export interface AgentStep {
type: 'step' type: 'step'
brain: Partial<AgentReflection> reflection: Partial<AgentReflection>
action: { action: {
name: string name: string
input: any input: any
@@ -266,13 +266,13 @@ export class PageAgent extends EventTarget {
const macroResult = result.toolResult as MacroToolResult const macroResult = result.toolResult as MacroToolResult
const input = macroResult.input const input = macroResult.input
const output = macroResult.output const output = macroResult.output
const brain = { const reflection: Partial<AgentReflection> = {
evaluation_previous_goal: input.evaluation_previous_goal || '', evaluation_previous_goal: input.evaluation_previous_goal,
memory: input.memory || '', memory: input.memory,
next_goal: input.next_goal || '', next_goal: input.next_goal,
} }
const actionName = Object.keys(input.action)[0] const actionName = Object.keys(input.action)[0]
const action = { const action: AgentStep['action'] = {
name: actionName, name: actionName,
input: input.action[actionName], input: input.action[actionName],
output: output, output: output,
@@ -280,10 +280,10 @@ export class PageAgent extends EventTarget {
this.history.push({ this.history.push({
type: 'step', type: 'step',
brain, reflection,
action, action,
usage: result.usage, usage: result.usage,
}) } as AgentStep)
console.log(chalk.green('Step finished:'), actionName) console.log(chalk.green('Step finished:'), actionName)
console.groupEnd() console.groupEnd()
@@ -370,13 +370,20 @@ export class PageAgent extends EventTarget {
const toolName = Object.keys(action)[0] const toolName = Object.keys(action)[0]
const toolInput = action[toolName] const toolInput = action[toolName]
const brain = trimLines(`✅: ${input.evaluation_previous_goal}
💾: ${input.memory}
🎯: ${input.next_goal}
`)
console.log(brain) // Build reflection text, only include non-empty fields
this.panel.update({ type: 'thinking', text: brain }) const reflectionLines: string[] = []
if (input.evaluation_previous_goal)
reflectionLines.push(`✅: ${input.evaluation_previous_goal}`)
if (input.memory) reflectionLines.push(`💾: ${input.memory}`)
if (input.next_goal) reflectionLines.push(`🎯: ${input.next_goal}`)
const reflectionText = reflectionLines.length > 0 ? reflectionLines.join('\n') : ''
if (reflectionText) {
console.log(reflectionText)
this.panel.update({ type: 'thinking', text: reflectionText })
}
// Find the corresponding tool // Find the corresponding tool
const tool = tools.get(toolName) const tool = tools.get(toolName)
@@ -482,10 +489,10 @@ export class PageAgent extends EventTarget {
async #generateObservations(stepCount: number): Promise<void> { async #generateObservations(stepCount: number): Promise<void> {
// Detect URL change // Detect URL change
const currentURL = await this.pageController.getCurrentUrl() const currentURL = await this.pageController.getCurrentUrl()
if (this.states.lastURL && currentURL !== this.states.lastURL) { if (currentURL !== this.states.lastURL) {
this.pushObservation(`Page navigated to → ${currentURL}`) this.pushObservation(`Page navigated to → ${currentURL}`)
this.states.lastURL = currentURL
} }
this.states.lastURL = currentURL
// Warn about remaining steps // Warn about remaining steps
const remaining = MAX_STEPS - stepCount const remaining = MAX_STEPS - stepCount
@@ -535,9 +542,9 @@ export class PageAgent extends EventTarget {
if (event.type === 'step') { if (event.type === 'step') {
stepIndex++ stepIndex++
prompt += `<step_${stepIndex}> prompt += `<step_${stepIndex}>
Evaluation of Previous Step: ${event.brain.evaluation_previous_goal} Evaluation of Previous Step: ${event.reflection.evaluation_previous_goal}
Memory: ${event.brain.memory} Memory: ${event.reflection.memory}
Next Goal: ${event.brain.next_goal} Next Goal: ${event.reflection.next_goal}
Action Results: ${event.action.output} Action Results: ${event.action.output}
</step_${stepIndex}> </step_${stepIndex}>
` `