feat: decouple PageController from PageAgent

This commit is contained in:
Simon
2026-01-19 17:14:09 +08:00
parent 3ce4d8e3fe
commit 4c5dd23fcd
3 changed files with 11 additions and 18 deletions

View File

@@ -3,7 +3,7 @@
* All rights reserved. * All rights reserved.
*/ */
import { LLM, type Tool } from '@page-agent/llms' import { LLM, type Tool } from '@page-agent/llms'
import { PageController } from '@page-agent/page-controller' import type { PageController } from '@page-agent/page-controller'
import chalk from 'chalk' import chalk from 'chalk'
import zod from 'zod' import zod from 'zod'
@@ -83,20 +83,13 @@ export class PageAgentCore extends EventTarget {
/** History events */ /** History events */
history: HistoricalEvent[] = [] history: HistoricalEvent[] = []
constructor(config: PageAgentConfig) { constructor(config: PageAgentConfig & { pageController: PageController }) {
super() super()
this.config = config this.config = config
this.#llm = new LLM(this.config) this.#llm = new LLM(this.config)
this.tools = new Map(tools) this.tools = new Map(tools)
this.pageController = config.pageController
// Initialize PageController with config (mask enabled by default)
this.pageController =
this.config.pageController ??
new PageController({
...this.config,
enableMask: this.config.enableMask ?? true,
})
// Listen to LLM retry events // Listen to LLM retry events
this.#llm.addEventListener('retry', (e) => { this.#llm.addEventListener('retry', (e) => {

View File

@@ -104,13 +104,6 @@ export interface AgentConfig {
*/ */
transformPageContent?: (content: string) => Promise<string> | string transformPageContent?: (content: string) => Promise<string> | string
/**
* @experimental
* Custom PageController instance to control page navigation and actions
* @note If not provided, a default PageController will be created
*/
pageController?: PageController
/** /**
* TODO: @unimplemented * TODO: @unimplemented
* hook when action causes a new page to be opened * hook when action causes a new page to be opened

View File

@@ -3,6 +3,7 @@
* All rights reserved. * All rights reserved.
*/ */
import { type PageAgentConfig, PageAgentCore } from '@page-agent/core' import { type PageAgentConfig, PageAgentCore } from '@page-agent/core'
import { PageController } from '@page-agent/page-controller'
import { Panel } from '@page-agent/ui' import { Panel } from '@page-agent/ui'
export type { PageAgentConfig } export type { PageAgentConfig }
@@ -11,7 +12,13 @@ export class PageAgent extends PageAgentCore {
panel: Panel panel: Panel
constructor(config: PageAgentConfig) { constructor(config: PageAgentConfig) {
super(config) const pageController = new PageController({
...config,
enableMask: config.enableMask ?? true,
})
super({ ...config, pageController })
this.panel = new Panel(this, { this.panel = new Panel(this, {
language: config.language, language: config.language,
}) })