refactor: rename page-agent to page-agent-core

This commit is contained in:
Simon
2026-01-19 16:06:07 +08:00
parent 09c3084629
commit c9f049a733
21 changed files with 8433 additions and 8543 deletions

17
package-lock.json generated
View File

@@ -12,6 +12,7 @@
"packages/page-controller",
"packages/ui",
"packages/llms",
"packages/core",
"packages/page-agent",
"packages/cdn",
"packages/website"
@@ -1588,6 +1589,10 @@
"resolved": "packages/cdn",
"link": true
},
"node_modules/@page-agent/core": {
"resolved": "packages/core",
"link": true
},
"node_modules/@page-agent/llms": {
"resolved": "packages/llms",
"link": true
@@ -8139,6 +8144,17 @@
"page-agent": "0.2.5"
}
},
"packages/core": {
"name": "@page-agent/core",
"version": "0.2.5",
"license": "MIT",
"dependencies": {
"@page-agent/llms": "0.2.5",
"@page-agent/page-controller": "0.2.5",
"chalk": "^5.6.2",
"zod": "^4.3.5"
}
},
"packages/llms": {
"name": "@page-agent/llms",
"version": "0.2.5",
@@ -8152,6 +8168,7 @@
"version": "0.2.5",
"license": "MIT",
"dependencies": {
"@page-agent/core": "0.2.5",
"@page-agent/llms": "0.2.5",
"@page-agent/page-controller": "0.2.5",
"@page-agent/ui": "0.2.5",

View File

@@ -7,6 +7,7 @@
"packages/page-controller",
"packages/ui",
"packages/llms",
"packages/core",
"packages/page-agent",
"packages/cdn",
"packages/website"

View File

@@ -0,0 +1,51 @@
{
"name": "@page-agent/core",
"private": false,
"version": "0.2.5",
"type": "module",
"main": "./dist/esm/page-agent-core.js",
"module": "./dist/esm/page-agent-core.js",
"types": "./dist/esm/PageAgentCore.d.ts",
"exports": {
".": {
"types": "./dist/esm/PageAgentCore.d.ts",
"import": "./dist/esm/page-agent-core.js",
"default": "./dist/esm/page-agent-core.js"
}
},
"files": [
"dist/"
],
"description": "GUI agent for web applications - add intelligent automation to any webpage with a single script",
"keywords": [
"ai",
"automation",
"ui-agent",
"GUI-agent",
"browser-automation",
"web-agent",
"llm",
"dom-interaction",
"web-automation",
"GUI-simulation"
],
"author": "Simon<gaomeng1900>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/alibaba/page-agent.git"
},
"homepage": "https://alibaba.github.io/page-agent/",
"scripts": {
"build": "vite build",
"dev:iife": "concurrently \"vite build --config vite.iife.config.js --watch\" \"npx serve dist/iife -p 5174\"",
"prepublishOnly": "node -e \"const fs=require('fs');['README.md','LICENSE'].forEach(f=>fs.copyFileSync('../../'+f,f))\"",
"postpublish": "node -e \"['README.md','LICENSE'].forEach(f=>{try{require('fs').unlinkSync(f)}catch{}})\""
},
"dependencies": {
"chalk": "^5.6.2",
"zod": "^4.3.5",
"@page-agent/llms": "0.2.5",
"@page-agent/page-controller": "0.2.5"
}
}

View File

@@ -7,7 +7,7 @@ import { PageController } from '@page-agent/page-controller'
import chalk from 'chalk'
import zod from 'zod'
import type { PageAgentConfig } from './config'
import { type PageAgentConfig } from './config'
import { MAX_STEPS } from './config/constants'
import SYSTEM_PROMPT from './prompts/system_prompt.md?raw'
import { tools } from './tools'
@@ -24,7 +24,7 @@ import {
import { normalizeResponse, trimLines, uid } from './utils'
import { assert } from './utils/assert'
export type { PageAgentConfig }
export { type PageAgentConfig }
export { tool, type PageAgentTool } from './tools'
/**
@@ -48,7 +48,7 @@ export { tool, type PageAgentTool } from './tools'
* - NOT included in LLM context
* - Types: thinking, executing, executed, retrying, error
*/
export class PageAgent extends EventTarget {
export class PageAgentCore extends EventTarget {
config: PageAgentConfig
id = uid()
tools: typeof tools

View File

@@ -1,7 +1,7 @@
import type { LLMConfig } from '@page-agent/llms'
import type { PageController, PageControllerConfig } from '@page-agent/page-controller'
import type { PageAgent } from '../PageAgent'
import type { PageAgentCore } from '../PageAgentCore'
import type { PageAgentTool } from '../tools'
import type { ExecutionResult, HistoricalEvent } from '../types'
@@ -67,16 +67,16 @@ export interface AgentConfig {
// @todo: use event instead of hooks
// @todo: remove `this` binding, pass agent as explicit parameter instead
onBeforeStep?: (this: PageAgent, stepCnt: number) => Promise<void> | void
onAfterStep?: (this: PageAgent, history: HistoricalEvent[]) => Promise<void> | void
onBeforeTask?: (this: PageAgent) => Promise<void> | void
onAfterTask?: (this: PageAgent, result: ExecutionResult) => Promise<void> | void
onBeforeStep?: (this: PageAgentCore, stepCnt: number) => Promise<void> | void
onAfterStep?: (this: PageAgentCore, history: HistoricalEvent[]) => Promise<void> | void
onBeforeTask?: (this: PageAgentCore) => Promise<void> | void
onAfterTask?: (this: PageAgentCore, result: ExecutionResult) => Promise<void> | void
/**
* @note this hook can block the disposal process
* @todo remove `this` binding, pass agent as explicit parameter instead
*/
onDispose?: (this: PageAgent, reason?: string) => void
onDispose?: (this: PageAgentCore, reason?: string) => void
// page behavior hooks

6
packages/core/src/env.d.ts vendored Normal file
View File

@@ -0,0 +1,6 @@
/// <reference types="vite/client" />
declare module '*.md?raw' {
const content: string
export default content
}

View File

@@ -4,7 +4,7 @@
*/
import zod, { type z } from 'zod'
import type { PageAgent } from '../PageAgent'
import type { PageAgentCore } from '../PageAgentCore'
import { waitFor } from '../utils'
/**
@@ -14,7 +14,7 @@ export interface PageAgentTool<TParams = any> {
// name: string
description: string
inputSchema: z.ZodType<TParams>
execute: (this: PageAgent, args: TParams) => Promise<string>
execute: (this: PageAgentCore, args: TParams) => Promise<string>
}
export function tool<TParams>(options: PageAgentTool<TParams>): PageAgentTool<TParams> {
@@ -36,7 +36,7 @@ tools.set(
text: zod.string(),
success: zod.boolean().default(true),
}),
execute: async function (this: PageAgent, input) {
execute: async function (this: PageAgentCore, input) {
// @note main loop will handle this one
// this.onDone(input.text, input.success)
return Promise.resolve('Task completed')
@@ -52,7 +52,7 @@ tools.set(
inputSchema: zod.object({
seconds: zod.number().min(1).max(10).default(1),
}),
execute: async function (this: PageAgent, input) {
execute: async function (this: PageAgentCore, input) {
const lastTimeUpdate = await this.pageController.getLastUpdateTime()
const actualWaitTime = Math.max(0, input.seconds - (Date.now() - lastTimeUpdate) / 1000)
console.log(`actualWaitTime: ${actualWaitTime} seconds`)
@@ -79,7 +79,7 @@ tools.set(
inputSchema: zod.object({
question: zod.string(),
}),
execute: async function (this: PageAgent, input) {
execute: async function (this: PageAgentCore, input) {
if (!this.onAskUser) {
throw new Error('ask_user tool requires onAskUser callback to be set')
}
@@ -96,7 +96,7 @@ tools.set(
inputSchema: zod.object({
index: zod.int().min(0),
}),
execute: async function (this: PageAgent, input) {
execute: async function (this: PageAgentCore, input) {
const result = await this.pageController.clickElement(input.index)
return result.message
},
@@ -111,7 +111,7 @@ tools.set(
index: zod.int().min(0),
text: zod.string(),
}),
execute: async function (this: PageAgent, input) {
execute: async function (this: PageAgentCore, input) {
const result = await this.pageController.inputText(input.index, input.text)
return result.message
},
@@ -127,7 +127,7 @@ tools.set(
index: zod.int().min(0),
text: zod.string(),
}),
execute: async function (this: PageAgent, input) {
execute: async function (this: PageAgentCore, input) {
const result = await this.pageController.selectOption(input.index, input.text)
return result.message
},
@@ -148,7 +148,7 @@ tools.set(
pixels: zod.number().int().min(0).optional(),
index: zod.number().int().min(0).optional(),
}),
execute: async function (this: PageAgent, input) {
execute: async function (this: PageAgentCore, input) {
const result = await this.pageController.scroll({
...input,
numPages: input.num_pages,
@@ -168,7 +168,7 @@ tools.set(
pixels: zod.number().int().min(0),
index: zod.number().int().min(0).optional(),
}),
execute: async function (this: PageAgent, input) {
execute: async function (this: PageAgentCore, input) {
const result = await this.pageController.scrollHorizontally(input)
return result.message
},
@@ -183,7 +183,7 @@ tools.set(
inputSchema: zod.object({
script: zod.string(),
}),
execute: async function (this: PageAgent, input) {
execute: async function (this: PageAgentCore, input) {
const result = await this.pageController.executeJavascript(input.script)
return result.message
},

View File

@@ -68,12 +68,13 @@ export function randomID(existingIDs?: string[]): string {
}
//
const _global = globalThis as any
if (!window.__PAGE_AGENT_IDS__) {
window.__PAGE_AGENT_IDS__ = []
if (!_global.__PAGE_AGENT_IDS__) {
_global.__PAGE_AGENT_IDS__ = []
}
const ids = window.__PAGE_AGENT_IDS__
const ids = _global.__PAGE_AGENT_IDS__
/**
* Generate a random ID.

View File

@@ -9,8 +9,7 @@
"paths": {
//
"@page-agent/llms": ["../llms/src/index.ts"],
"@page-agent/page-controller": ["../page-controller/src/PageController.ts"],
"@page-agent/ui": ["../ui/src/index.ts"]
"@page-agent/page-controller": ["../page-controller/src/PageController.ts"]
}
},
"include": ["**/*.ts"],
@@ -18,7 +17,6 @@
"references": [
//
{ "path": "../llms" },
{ "path": "../page-controller" },
{ "path": "../ui" }
{ "path": "../page-controller" }
]
}

View File

@@ -20,9 +20,9 @@ export default defineConfig({
},
build: {
lib: {
entry: resolve(__dirname, 'src/PageAgent.ts'),
name: 'PageAgent',
fileName: 'page-agent',
entry: resolve(__dirname, 'src/PageAgentCore.ts'),
name: 'PageAgentCore',
fileName: 'page-agent-core',
formats: ['es'],
},
outDir: resolve(__dirname, 'dist', 'esm'),

View File

@@ -1,52 +0,0 @@
{
"name": "page-agent",
"private": false,
"version": "0.2.5",
"type": "module",
"main": "./dist/esm/page-agent.js",
"module": "./dist/esm/page-agent.js",
"types": "./dist/esm/PageAgent.d.ts",
"exports": {
".": {
"types": "./dist/esm/PageAgent.d.ts",
"import": "./dist/esm/page-agent.js",
"default": "./dist/esm/page-agent.js"
}
},
"files": [
"dist/"
],
"description": "GUI agent for web applications - add intelligent automation to any webpage with a single script",
"keywords": [
"ai",
"automation",
"ui-agent",
"GUI-agent",
"browser-automation",
"web-agent",
"llm",
"dom-interaction",
"web-automation",
"GUI-simulation"
],
"author": "Simon<gaomeng1900>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/alibaba/page-agent.git"
},
"homepage": "https://alibaba.github.io/page-agent/",
"scripts": {
"build": "vite build",
"dev:iife": "concurrently \"vite build --config vite.iife.config.js --watch\" \"npx serve dist/iife -p 5174\"",
"prepublishOnly": "node -e \"const fs=require('fs');['README.md','LICENSE'].forEach(f=>fs.copyFileSync('../../'+f,f))\"",
"postpublish": "node -e \"['README.md','LICENSE'].forEach(f=>{try{require('fs').unlinkSync(f)}catch{}})\""
},
"dependencies": {
"chalk": "^5.6.2",
"zod": "^4.3.5",
"@page-agent/llms": "0.2.5",
"@page-agent/page-controller": "0.2.5",
"@page-agent/ui": "0.2.5"
}
}

View File

@@ -1,20 +0,0 @@
/// <reference types="vite/client" />
import type { PageAgent } from './PageAgent'
declare module '*.module.css' {
const classes: Record<string, string>
export default classes
}
declare module '*.md?raw' {
const content: string
export default content
}
declare global {
interface Window {
pageAgent?: PageAgent
PageAgent: typeof PageAgent
__PAGE_AGENT_IDS__: string[]
}
}

View File

@@ -1,56 +0,0 @@
/**
* Auto-run entry for page-agent.js. Insert this script into your page to get page-agent functionality.
*/
import { Panel } from '@page-agent/ui'
import { PageAgent, type PageAgentConfig } from './PageAgent'
// Clean up existing instances to prevent multiple injections from bookmarklet
if (window.pageAgent) {
window.pageAgent.dispose()
}
// Mount to global window object
window.PageAgent = PageAgent
// Export for ES module usage
// export { PageAgent }
console.log('🚀 page-agent.js loaded!')
const DEMO_MODEL = 'PAGE-AGENT-FREE-TESTING-RANDOM'
const DEMO_BASE_URL = 'https://hwcxiuzfylggtcktqgij.supabase.co/functions/v1/llm-testing-proxy'
const DEMO_API_KEY = 'PAGE-AGENT-FREE-TESTING-RANDOM'
// in case document.x is not ready yet
// @todo give a switch to disable auto-init
setTimeout(() => {
const currentScript = document.currentScript as HTMLScriptElement | null
let config: PageAgentConfig
if (currentScript) {
console.log('🚀 page-agent.js detected current script:', currentScript.src)
const url = new URL(currentScript.src)
const model = url.searchParams.get('model') || DEMO_MODEL
const baseURL = url.searchParams.get('baseURL') || DEMO_BASE_URL
const apiKey = url.searchParams.get('apiKey') || DEMO_API_KEY
const language = (url.searchParams.get('lang') as 'zh-CN' | 'en-US') || 'zh-CN'
config = { model, baseURL, apiKey, language }
} else {
console.log('🚀 page-agent.js no current script detected, using default demo config')
config = {
model: import.meta.env.LLM_MODEL_NAME ? import.meta.env.LLM_MODEL_NAME : DEMO_MODEL,
baseURL: import.meta.env.LLM_BASE_URL ? import.meta.env.LLM_BASE_URL : DEMO_BASE_URL,
apiKey: import.meta.env.LLM_API_KEY ? import.meta.env.LLM_API_KEY : DEMO_API_KEY,
}
}
// Create agent
window.pageAgent = new PageAgent(config)
// Create and bind Panel
const panel = new Panel(window.pageAgent, { language: config.language })
panel.show()
console.log('🚀 page-agent.js initialized with config:', window.pageAgent.config)
})

View File

@@ -1,56 +0,0 @@
// @ts-check
import { config as dotenvConfig } from 'dotenv'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import { defineConfig } from 'vite'
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
const __dirname = dirname(fileURLToPath(import.meta.url))
// Load .env from repo root
dotenvConfig({ path: resolve(__dirname, '../../.env') })
// UMD Bundle for CDN
// - alias all local packages so that they can be build in
// - no external
// - no d.ts. dts does not work with monorepo aliasing
export default defineConfig(({ mode }) => ({
plugins: [cssInjectedByJsPlugin({ relativeCSSInjection: true })],
publicDir: false,
esbuild: {
keepNames: true,
},
resolve: {
alias: {
'@page-agent/page-controller': resolve(__dirname, '../page-controller/src/PageController.ts'),
'@page-agent/llms': resolve(__dirname, '../llms/src/index.ts'),
'@page-agent/ui': resolve(__dirname, '../ui/src/index.ts'),
},
},
build: {
lib: {
entry: resolve(__dirname, 'src/iife.ts'),
name: 'PageAgent',
fileName: 'page-agent',
formats: ['iife'],
},
outDir: resolve(__dirname, 'dist', 'iife'),
cssCodeSplit: true,
minify: false,
rollupOptions: {
output: {
// force use .js as extension
entryFileNames: 'page-agent.js',
},
onwarn: function (message, handler) {
if (message.code === 'EVAL') return
handler(message)
},
},
},
define: {
'import.meta.env.LLM_MODEL_NAME': JSON.stringify(process.env.LLM_MODEL_NAME),
'import.meta.env.LLM_API_KEY': JSON.stringify(process.env.LLM_API_KEY),
'import.meta.env.LLM_BASE_URL': JSON.stringify(process.env.LLM_BASE_URL),
},
}))