chore(ext): refine MultiPageAgent api and clean up

This commit is contained in:
Simon
2026-01-28 13:36:46 +08:00
parent 55ffbb8f08
commit dd593f77e9
5 changed files with 15 additions and 26 deletions

View File

@@ -8,8 +8,7 @@ import { createTabTools } from './tabTools'
export class MultiPageAgent extends PageAgentCore { export class MultiPageAgent extends PageAgentCore {
constructor(config: Omit<PageAgentConfig, 'pageController'>) { constructor(config: Omit<PageAgentConfig, 'pageController'>) {
const tabsController = new TabsController() const tabsController = new TabsController()
const pageController = new RemotePageController() const pageController = new RemotePageController(tabsController)
pageController.tabsController = tabsController
const customTools = createTabTools(tabsController) const customTools = createTabTools(tabsController)
super({ super({

View File

@@ -3,11 +3,6 @@
* - redirect messages from RemotePageController(Agent, extension pages) to ContentScript * - redirect messages from RemotePageController(Agent, extension pages) to ContentScript
*/ */
// chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// if (message.type !== 'PAGE_CONTROL') {
// return
// }
export function handlePageControlMessage( export function handlePageControlMessage(
message: { type: 'PAGE_CONTROL'; action: string; payload: any; targetTabId: number }, message: { type: 'PAGE_CONTROL'; action: string; payload: any; targetTabId: number },
sender: chrome.runtime.MessageSender, sender: chrome.runtime.MessageSender,

View File

@@ -7,9 +7,12 @@ import type { TabsController } from './TabsController'
* - live in the agent env (extension page or content script) * - live in the agent env (extension page or content script)
* - communicates with remote PageController via sw * - communicates with remote PageController via sw
*/ */
export class RemotePageController { export class RemotePageController {
tabsController!: TabsController private tabsController: TabsController
constructor(tabsController: TabsController) {
this.tabsController = tabsController
}
get currentTabId(): number | null { get currentTabId(): number | null {
return this.tabsController.currentTabId return this.tabsController.currentTabId
@@ -21,10 +24,6 @@ export class RemotePageController {
return url || '' return url || ''
} }
get currentTabUrl(): Promise<string> {
return this.getCurrentUrl()
}
async getCurrentTitle(): Promise<string> { async getCurrentTitle(): Promise<string> {
if (!this.currentTabId) return '' if (!this.currentTabId) return ''
const { title } = await this.tabsController.getTabInfo(this.currentTabId) const { title } = await this.tabsController.getTabInfo(this.currentTabId)
@@ -127,12 +126,11 @@ export class RemotePageController {
return this.remoteCallDomAction('execute_javascript', args) return this.remoteCallDomAction('execute_javascript', args)
} }
/** @note Mask visibility is managed by content script via storage polling. */ /** @note Managed by content script via storage polling. */
async showMask(): Promise<void> {} async showMask(): Promise<void> {}
/** @note Mask visibility is managed by content script via storage polling. */ /** @note Managed by content script via storage polling. */
async hideMask(): Promise<void> {} async hideMask(): Promise<void> {}
/** @note Managed by content script via storage polling. */
// dispose
dispose(): void {} dispose(): void {}
private async preCheck() { private async preCheck() {
@@ -160,6 +158,10 @@ export class RemotePageController {
payload, payload,
}) })
} }
private get currentTabUrl(): Promise<string> {
return this.getCurrentUrl()
}
} }
interface DomActionReturn { interface DomActionReturn {
@@ -170,7 +172,7 @@ interface DomActionReturn {
/** /**
* Check if a URL can run content scripts. * Check if a URL can run content scripts.
*/ */
export function isContentScriptAllowed(url: string | undefined): boolean { function isContentScriptAllowed(url: string | undefined): boolean {
if (!url) return false if (!url) return false
const restrictedPatterns = [ const restrictedPatterns = [

View File

@@ -1,9 +1,4 @@
// Demo build (auto-init with demo LLM, for quick testing) // Demo LLM for testing
export const CDN_DEMO_URL = 'https://cdn.jsdelivr.net/npm/page-agent/dist/iife/page-agent.demo.js'
export const CDN_DEMO_CN_URL =
'https://registry.npmmirror.com/page-agent/latest/files/dist/iife/page-agent.demo.js'
// Demo LLM for website testing
export const DEMO_MODEL = 'PAGE-AGENT-FREE-TESTING-RANDOM' export const DEMO_MODEL = 'PAGE-AGENT-FREE-TESTING-RANDOM'
export const DEMO_BASE_URL = export const DEMO_BASE_URL =
'https://hwcxiuzfylggtcktqgij.supabase.co/functions/v1/llm-testing-proxy' 'https://hwcxiuzfylggtcktqgij.supabase.co/functions/v1/llm-testing-proxy'

View File

@@ -9,8 +9,6 @@ import { DEMO_API_KEY, DEMO_BASE_URL, DEMO_MODEL } from '@/agent/constants'
import { MultiPageAgent } from './MultiPageAgent' import { MultiPageAgent } from './MultiPageAgent'
// import { type AgentController, type LLMConfig, getAgentController } from './old/AgentController'
export interface UseAgentResult { export interface UseAgentResult {
status: AgentStatus status: AgentStatus
history: HistoricalEvent[] history: HistoricalEvent[]