From 6f0ff1fd33f1f30d27a400fac53c1c09dc21cf7f Mon Sep 17 00:00:00 2001
From: Simon <10131203+gaomeng1900@users.noreply.github.com>
Date: Mon, 9 Feb 2026 17:49:10 +0800
Subject: [PATCH] refactor: zod tree-shaking; better error handling in agent
steps
---
package-lock.json | 11 +++
package.json | 3 +-
packages/core/src/PageAgentCore.ts | 69 ++++++++++---------
packages/core/src/tools/index.ts | 4 +-
packages/core/src/utils/assert.ts | 17 -----
packages/core/src/utils/index.ts | 18 +++++
packages/extension/src/agent/tabTools.ts | 2 +-
packages/llms/src/utils.ts | 2 +-
packages/page-agent/package.json | 8 +--
packages/page-agent/vite.iife.config.js | 8 ++-
.../pages/docs/features/custom-tools/page.tsx | 2 +-
.../introduction/troubleshooting/page.tsx | 5 --
12 files changed, 82 insertions(+), 67 deletions(-)
delete mode 100644 packages/core/src/utils/assert.ts
diff --git a/package-lock.json b/package-lock.json
index e0cc8d8..be34bc2 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -41,6 +41,7 @@
"typescript-eslint": "^8.54.0",
"unplugin-dts": "^1.0.0-beta.6",
"vite": "^7.3.1",
+ "vite-bundle-analyzer": "^1.3.6",
"vite-plugin-css-injected-by-js": "^3.5.2"
},
"engines": {
@@ -10411,6 +10412,16 @@
}
}
},
+ "node_modules/vite-bundle-analyzer": {
+ "version": "1.3.6",
+ "resolved": "https://registry.npmjs.org/vite-bundle-analyzer/-/vite-bundle-analyzer-1.3.6.tgz",
+ "integrity": "sha512-elFXkF9oGy4CJEeOdP1DSEHRDKWfmaEDfT9/GuF4jmyfmUF6nC//iN+ythqMEVvrtchOEHGKLumZwnu1NjoI0w==",
+ "dev": true,
+ "license": "MIT",
+ "bin": {
+ "analyze": "dist/bin.js"
+ }
+ },
"node_modules/vite-node": {
"version": "5.3.0",
"resolved": "https://registry.npmjs.org/vite-node/-/vite-node-5.3.0.tgz",
diff --git a/package.json b/package.json
index 3ee9c0a..c21a6df 100644
--- a/package.json
+++ b/package.json
@@ -59,7 +59,8 @@
"typescript-eslint": "^8.54.0",
"unplugin-dts": "^1.0.0-beta.6",
"vite": "^7.3.1",
- "vite-plugin-css-injected-by-js": "^3.5.2"
+ "vite-plugin-css-injected-by-js": "^3.5.2",
+ "vite-bundle-analyzer": "^1.3.6"
},
"overrides": {
"typescript": "^5.9.3"
diff --git a/packages/core/src/PageAgentCore.ts b/packages/core/src/PageAgentCore.ts
index ee38e47..c069e97 100644
--- a/packages/core/src/PageAgentCore.ts
+++ b/packages/core/src/PageAgentCore.ts
@@ -5,13 +5,13 @@
import { InvokeError, LLM, type Tool } from '@page-agent/llms'
import type { PageController } from '@page-agent/page-controller'
import chalk from 'chalk'
-import zod from 'zod'
+import * as zod from 'zod'
import { type PageAgentConfig } from './config'
import { DEFAULT_MAX_STEPS } from './config/constants'
import SYSTEM_PROMPT from './prompts/system_prompt.md?raw'
import { tools } from './tools'
-import {
+import type {
AgentActivity,
AgentReflection,
AgentStatus,
@@ -21,8 +21,7 @@ import {
MacroToolInput,
MacroToolResult,
} from './types'
-import { normalizeResponse, trimLines, uid, waitFor } from './utils'
-import { assert } from './utils/assert'
+import { assert, normalizeResponse, trimLines, uid, waitFor } from './utils'
export { type PageAgentConfig }
export { tool, type PageAgentTool } from './tools'
@@ -209,20 +208,20 @@ export class PageAgentCore extends EventTarget {
lastURL: '',
}
- try {
- let step = 0
+ let step = 0
+
+ while (true) {
+ try {
+ console.group(`step: ${step}`)
- while (true) {
await this.#generateObservations(step)
await onBeforeStep?.(this, step)
- console.group(`step: ${step}`)
-
// abort
if (this.#abortController.signal.aborted) throw new Error('AbortError')
- // Thinking
+ // status
console.log(chalk.blue('Thinking...'))
this.#emitActivity({ type: 'thinking' })
@@ -270,22 +269,13 @@ export class PageAgentCore extends EventTarget {
//
+ await onAfterStep?.(this, this.history)
+
console.log(chalk.green('Step finished:'), actionName)
console.groupEnd()
- await onAfterStep?.(this, this.history)
+ // finish task if done
- step++
- if (step > this.config.maxSteps) {
- this.#onDone(false)
- const result: ExecutionResult = {
- success: false,
- data: 'Step count exceeded maximum limit',
- history: this.history,
- }
- await onAfterTask?.(this, result)
- return result
- }
if (actionName === 'done') {
const success = action.input?.success ?? false
const text = action.input?.text || 'no text provided'
@@ -299,19 +289,32 @@ export class PageAgentCore extends EventTarget {
await onAfterTask?.(this, result)
return result
}
+ } catch (error: unknown) {
+ console.groupEnd() // to prevent nested groups
+ console.error('Task failed', error)
+ const errorMessage = String(error)
+ this.#emitActivity({ type: 'error', message: errorMessage })
+ this.#onDone(false)
+ const result: ExecutionResult = {
+ success: false,
+ data: errorMessage,
+ history: this.history,
+ }
+ await onAfterTask?.(this, result)
+ return result
}
- } catch (error: unknown) {
- console.error('Task failed', error)
- const errorMessage = String(error)
- this.#emitActivity({ type: 'error', message: errorMessage })
- this.#onDone(false)
- const result: ExecutionResult = {
- success: false,
- data: errorMessage,
- history: this.history,
+
+ step++
+ if (step > this.config.maxSteps) {
+ this.#onDone(false)
+ const result: ExecutionResult = {
+ success: false,
+ data: 'Step count exceeded maximum limit',
+ history: this.history,
+ }
+ await onAfterTask?.(this, result)
+ return result
}
- await onAfterTask?.(this, result)
- return result
}
}
diff --git a/packages/core/src/tools/index.ts b/packages/core/src/tools/index.ts
index 3358cd0..458f9fd 100644
--- a/packages/core/src/tools/index.ts
+++ b/packages/core/src/tools/index.ts
@@ -2,7 +2,7 @@
* Internal tools for PageAgent.
* @note Adapted from browser-use
*/
-import zod, { type z } from 'zod'
+import * as zod from 'zod'
import type { PageAgentCore } from '../PageAgentCore'
import { waitFor } from '../utils'
@@ -13,7 +13,7 @@ import { waitFor } from '../utils'
export interface PageAgentTool
- {isZh - ? '遇到问题了?按症状查找解决方案。' - : 'Running into issues? Find solutions by symptom.'} -
*/} {/* Two-column: content + TOC */}