fix(core): remove trimLines to fix indentation lost
This commit is contained in:
@@ -21,7 +21,7 @@ import type {
|
|||||||
MacroToolInput,
|
MacroToolInput,
|
||||||
MacroToolResult,
|
MacroToolResult,
|
||||||
} from './types'
|
} from './types'
|
||||||
import { assert, normalizeResponse, trimLines, uid, waitFor } from './utils'
|
import { assert, normalizeResponse, uid, waitFor } from './utils'
|
||||||
|
|
||||||
export { type PageAgentConfig }
|
export { type PageAgentConfig }
|
||||||
export { tool, type PageAgentTool } from './tools'
|
export { tool, type PageAgentTool } from './tools'
|
||||||
@@ -428,7 +428,7 @@ export class PageAgentCore extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get instructions from config and format as XML block
|
* Get instructions from config
|
||||||
*/
|
*/
|
||||||
async #getInstructions(): Promise<string> {
|
async #getInstructions(): Promise<string> {
|
||||||
const { instructions } = this.config
|
const { instructions } = this.config
|
||||||
@@ -512,6 +512,7 @@ export class PageAgentCore extends EventTarget {
|
|||||||
let prompt = ''
|
let prompt = ''
|
||||||
|
|
||||||
// <instructions> (optional)
|
// <instructions> (optional)
|
||||||
|
|
||||||
prompt += await this.#getInstructions()
|
prompt += await this.#getInstructions()
|
||||||
|
|
||||||
// <agent_state>
|
// <agent_state>
|
||||||
@@ -521,38 +522,36 @@ export class PageAgentCore extends EventTarget {
|
|||||||
|
|
||||||
const stepCount = this.history.filter((e) => e.type === 'step').length
|
const stepCount = this.history.filter((e) => e.type === 'step').length
|
||||||
|
|
||||||
prompt += `<agent_state>
|
prompt += '<agent_state>\n'
|
||||||
<user_request>
|
prompt += '<user_request>\n'
|
||||||
${this.task}
|
prompt += `${this.task}\n`
|
||||||
</user_request>
|
prompt += '</user_request>\n'
|
||||||
<step_info>
|
prompt += '<step_info>\n'
|
||||||
Step ${stepCount + 1} of ${this.config.maxSteps} max possible steps
|
prompt += `Step ${stepCount + 1} of ${this.config.maxSteps} max possible steps\n`
|
||||||
Current date and time: ${new Date().toLocaleString()}
|
prompt += `Current time: ${new Date().toLocaleString()}\n`
|
||||||
</step_info>
|
prompt += '</step_info>\n'
|
||||||
</agent_state>
|
prompt += '</agent_state>\n\n'
|
||||||
`
|
|
||||||
|
|
||||||
// <agent_history>
|
// <agent_history>
|
||||||
// - <step_N> for steps
|
// - <step_N> for steps
|
||||||
// - <sys> for observations and system messages
|
// - <sys> for observations and system messages
|
||||||
|
|
||||||
prompt += '\n<agent_history>\n'
|
prompt += '<agent_history>\n'
|
||||||
|
|
||||||
let stepIndex = 0
|
let stepIndex = 0
|
||||||
for (const event of this.history) {
|
for (const event of this.history) {
|
||||||
if (event.type === 'step') {
|
if (event.type === 'step') {
|
||||||
stepIndex++
|
stepIndex++
|
||||||
prompt += `<step_${stepIndex}>
|
prompt += `<step_${stepIndex}>\n`
|
||||||
Evaluation of Previous Step: ${event.reflection.evaluation_previous_goal}
|
prompt += `Evaluation of Previous Step: ${event.reflection.evaluation_previous_goal}\n`
|
||||||
Memory: ${event.reflection.memory}
|
prompt += `Memory: ${event.reflection.memory}\n`
|
||||||
Next Goal: ${event.reflection.next_goal}
|
prompt += `Next Goal: ${event.reflection.next_goal}\n`
|
||||||
Action Results: ${event.action.output}
|
prompt += `Action Results: ${event.action.output}\n`
|
||||||
</step_${stepIndex}>
|
prompt += `</step_${stepIndex}>\n`
|
||||||
`
|
|
||||||
} else if (event.type === 'observation') {
|
} else if (event.type === 'observation') {
|
||||||
prompt += `<sys>${event.content}</sys>\n`
|
prompt += `<sys>${event.content}</sys>\n`
|
||||||
} else if (event.type === 'user_takeover') {
|
} else if (event.type === 'user_takeover') {
|
||||||
prompt += `<sys>User took over control and made changes to the page.</sys>\n`
|
prompt += `<sys>User took over control and made changes to the page</sys>\n`
|
||||||
} else if (event.type === 'error') {
|
} else if (event.type === 'error') {
|
||||||
// Error events are mainly for panel rendering, not included in LLM context
|
// Error events are mainly for panel rendering, not included in LLM context
|
||||||
// to avoid polluting the agent's reasoning with transient errors
|
// to avoid polluting the agent's reasoning with transient errors
|
||||||
@@ -563,9 +562,20 @@ export class PageAgentCore extends EventTarget {
|
|||||||
|
|
||||||
// <browser_state>
|
// <browser_state>
|
||||||
|
|
||||||
prompt += await this.#getBrowserState()
|
const browserState = await this.pageController.getBrowserState()
|
||||||
|
|
||||||
return trimLines(prompt)
|
let pageContent = browserState.content
|
||||||
|
if (this.config.transformPageContent) {
|
||||||
|
pageContent = await this.config.transformPageContent(pageContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
prompt += '<browser_state>\n'
|
||||||
|
prompt += browserState.header + '\n'
|
||||||
|
prompt += pageContent + '\n'
|
||||||
|
prompt += browserState.footer + '\n\n'
|
||||||
|
prompt += '</browser_state>\n\n'
|
||||||
|
|
||||||
|
return prompt
|
||||||
}
|
}
|
||||||
|
|
||||||
#onDone(success = true) {
|
#onDone(success = true) {
|
||||||
@@ -575,23 +585,6 @@ export class PageAgentCore extends EventTarget {
|
|||||||
this.#abortController.abort()
|
this.#abortController.abort()
|
||||||
}
|
}
|
||||||
|
|
||||||
async #getBrowserState(): Promise<string> {
|
|
||||||
const state = await this.pageController.getBrowserState()
|
|
||||||
|
|
||||||
let content = state.content
|
|
||||||
if (this.config.transformPageContent) {
|
|
||||||
content = await this.config.transformPageContent(content)
|
|
||||||
}
|
|
||||||
|
|
||||||
return trimLines(`<browser_state>
|
|
||||||
${state.header}
|
|
||||||
${content}
|
|
||||||
${state.footer}
|
|
||||||
|
|
||||||
</browser_state>
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
dispose() {
|
dispose() {
|
||||||
console.log('Disposing PageAgent...')
|
console.log('Disposing PageAgent...')
|
||||||
this.pageController.dispose()
|
this.pageController.dispose()
|
||||||
|
|||||||
@@ -39,15 +39,6 @@ export function truncate(text: string, maxLength: number): string {
|
|||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
export function trimLines(text: string): string {
|
|
||||||
return text
|
|
||||||
.split('\n')
|
|
||||||
.map((line) => line.trim())
|
|
||||||
.join('\n')
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
export function randomID(existingIDs?: string[]): string {
|
export function randomID(existingIDs?: string[]): string {
|
||||||
let id = Math.random().toString(36).substring(2, 11)
|
let id = Math.random().toString(36).substring(2, 11)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user