From 22fe448d95e009fca4b5f0b9d9f772d833b63752 Mon Sep 17 00:00:00 2001 From: Simon <10131203+gaomeng1900@users.noreply.github.com> Date: Wed, 25 Feb 2026 16:18:56 +0800 Subject: [PATCH] refactor: unify zod imports --- packages/core/src/PageAgentCore.ts | 20 ++++----- packages/core/src/tools/index.ts | 56 ++++++++++++------------ packages/extension/src/agent/tabTools.ts | 16 +++---- packages/llms/src/types.ts | 2 +- 4 files changed, 46 insertions(+), 48 deletions(-) diff --git a/packages/core/src/PageAgentCore.ts b/packages/core/src/PageAgentCore.ts index 0a9c63c..d1a4ac6 100644 --- a/packages/core/src/PageAgentCore.ts +++ b/packages/core/src/PageAgentCore.ts @@ -5,7 +5,7 @@ import { InvokeError, LLM, type Tool } from '@page-agent/llms' import type { BrowserState, PageController } from '@page-agent/page-controller' import chalk from 'chalk' -import * as zod from 'zod' +import * as z from 'zod' import { type PageAgentConfig, type SupportedLanguage } from './config' import { DEFAULT_MAX_STEPS } from './config/constants' @@ -358,24 +358,22 @@ export class PageAgentCore extends EventTarget { const tools = this.tools const actionSchemas = Array.from(tools.entries()).map(([toolName, tool]) => { - return zod.object({ [toolName]: tool.inputSchema }).describe(tool.description) + return z.object({ [toolName]: tool.inputSchema }).describe(tool.description) }) - const actionSchema = zod.union( - actionSchemas as unknown as [zod.ZodType, zod.ZodType, ...zod.ZodType[]] - ) + const actionSchema = z.union(actionSchemas as unknown as [z.ZodType, z.ZodType, ...z.ZodType[]]) - const macroToolSchema = zod.object({ - // thinking: zod.string().optional(), - evaluation_previous_goal: zod.string().optional(), - memory: zod.string().optional(), - next_goal: zod.string().optional(), + const macroToolSchema = z.object({ + // thinking: z.string().optional(), + evaluation_previous_goal: z.string().optional(), + memory: z.string().optional(), + next_goal: z.string().optional(), action: actionSchema, }) return { description: 'You MUST call this tool every step!', - inputSchema: macroToolSchema as zod.ZodType, + inputSchema: macroToolSchema as z.ZodType, execute: async (input: MacroToolInput): Promise => { // abort if (this.#abortController.signal.aborted) throw new Error('AbortError') diff --git a/packages/core/src/tools/index.ts b/packages/core/src/tools/index.ts index a39c56d..69bfbfb 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 * as zod from 'zod' +import * as z from 'zod' import type { PageAgentCore } from '../PageAgentCore' import { waitFor } from '../utils' @@ -13,7 +13,7 @@ import { waitFor } from '../utils' export interface PageAgentTool { // name: string description: string - inputSchema: zod.ZodType + inputSchema: z.ZodType execute: (this: PageAgentCore, args: TParams) => Promise } @@ -32,9 +32,9 @@ tools.set( tool({ description: 'Complete task. Text is your final response to the user — keep it concise unless the user explicitly asks for detail.', - inputSchema: zod.object({ - text: zod.string(), - success: zod.boolean().default(true), + inputSchema: z.object({ + text: z.string(), + success: z.boolean().default(true), }), execute: async function (this: PageAgentCore, input) { // @note main loop will handle this one @@ -47,8 +47,8 @@ tools.set( 'wait', tool({ description: 'Wait for x seconds. Can be used to wait until the page or data is fully loaded.', - inputSchema: zod.object({ - seconds: zod.number().min(1).max(10).default(1), + inputSchema: z.object({ + seconds: z.number().min(1).max(10).default(1), }), execute: async function (this: PageAgentCore, input) { // try to subtract LLM calling time from the actual wait time @@ -67,8 +67,8 @@ tools.set( tool({ description: 'Ask the user a question and wait for their answer. Use this if you need more information or clarification.', - inputSchema: zod.object({ - question: zod.string(), + inputSchema: z.object({ + question: z.string(), }), execute: async function (this: PageAgentCore, input) { if (!this.onAskUser) { @@ -84,8 +84,8 @@ tools.set( 'click_element_by_index', tool({ description: 'Click element by index', - inputSchema: zod.object({ - index: zod.int().min(0), + inputSchema: z.object({ + index: z.int().min(0), }), execute: async function (this: PageAgentCore, input) { const result = await this.pageController.clickElement(input.index) @@ -98,9 +98,9 @@ tools.set( 'input_text', tool({ description: 'Click and type text into an interactive input element', - inputSchema: zod.object({ - index: zod.int().min(0), - text: zod.string(), + inputSchema: z.object({ + index: z.int().min(0), + text: z.string(), }), execute: async function (this: PageAgentCore, input) { const result = await this.pageController.inputText(input.index, input.text) @@ -114,9 +114,9 @@ tools.set( tool({ description: 'Select dropdown option for interactive element index by the text of the option you want to select', - inputSchema: zod.object({ - index: zod.int().min(0), - text: zod.string(), + inputSchema: z.object({ + index: z.int().min(0), + text: z.string(), }), execute: async function (this: PageAgentCore, input) { const result = await this.pageController.selectOption(input.index, input.text) @@ -132,11 +132,11 @@ tools.set( 'scroll', tool({ description: 'Scroll the page vertically. Use index for scroll elements (dropdowns/custom UI).', - inputSchema: zod.object({ - down: zod.boolean().default(true), - num_pages: zod.number().min(0).max(10).optional().default(0.1), - pixels: zod.number().int().min(0).optional(), - index: zod.number().int().min(0).optional(), + inputSchema: z.object({ + down: z.boolean().default(true), + num_pages: z.number().min(0).max(10).optional().default(0.1), + pixels: z.number().int().min(0).optional(), + index: z.number().int().min(0).optional(), }), execute: async function (this: PageAgentCore, input) { const result = await this.pageController.scroll({ @@ -156,10 +156,10 @@ tools.set( tool({ description: 'Scroll the page horizontally, or within a specific element by index. Useful for wide tables.', - inputSchema: zod.object({ - right: zod.boolean().default(true), - pixels: zod.number().int().min(0), - index: zod.number().int().min(0).optional(), + inputSchema: z.object({ + right: z.boolean().default(true), + pixels: z.number().int().min(0), + index: z.number().int().min(0).optional(), }), execute: async function (this: PageAgentCore, input) { const result = await this.pageController.scrollHorizontally(input) @@ -173,8 +173,8 @@ tools.set( tool({ description: 'Execute JavaScript code on the current page. Supports async/await syntax. Use with caution!', - inputSchema: zod.object({ - script: zod.string(), + inputSchema: z.object({ + script: z.string(), }), execute: async function (this: PageAgentCore, input) { const result = await this.pageController.executeJavascript(input.script) diff --git a/packages/extension/src/agent/tabTools.ts b/packages/extension/src/agent/tabTools.ts index fda681c..371b14f 100644 --- a/packages/extension/src/agent/tabTools.ts +++ b/packages/extension/src/agent/tabTools.ts @@ -6,14 +6,14 @@ * - switch_to_tab: Switch to an existing tab * - close_tab: Close a tab (optionally switch to another) */ -import * as zod from 'zod' +import * as z from 'zod' import type { TabsController } from './TabsController' /** Tool definition compatible with PageAgentCore customTools */ interface TabTool { description: string - inputSchema: zod.ZodType + inputSchema: z.ZodType execute: (input: unknown) => Promise } @@ -26,8 +26,8 @@ export function createTabTools(tabsController: TabsController): Record { const { url } = input as { url: string } @@ -42,8 +42,8 @@ export function createTabTools(tabsController: TabsController): Record { const { tab_id } = input as { tab_id: number } @@ -58,8 +58,8 @@ export function createTabTools(tabsController: TabsController): Record { const { tab_id } = input as { tab_id: number } diff --git a/packages/llms/src/types.ts b/packages/llms/src/types.ts index cd90387..fff25e0 100644 --- a/packages/llms/src/types.ts +++ b/packages/llms/src/types.ts @@ -1,7 +1,7 @@ /** * Core types for LLM integration */ -import type { z } from 'zod' +import type * as z from 'zod' /** * Message format - OpenAI standard (industry standard)