refactor: unify zod imports
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
import { InvokeError, LLM, type Tool } from '@page-agent/llms'
|
import { InvokeError, LLM, type Tool } from '@page-agent/llms'
|
||||||
import type { BrowserState, PageController } from '@page-agent/page-controller'
|
import type { BrowserState, PageController } from '@page-agent/page-controller'
|
||||||
import chalk from 'chalk'
|
import chalk from 'chalk'
|
||||||
import * as zod from 'zod'
|
import * as z from 'zod'
|
||||||
|
|
||||||
import { type PageAgentConfig, type SupportedLanguage } from './config'
|
import { type PageAgentConfig, type SupportedLanguage } from './config'
|
||||||
import { DEFAULT_MAX_STEPS } from './config/constants'
|
import { DEFAULT_MAX_STEPS } from './config/constants'
|
||||||
@@ -358,24 +358,22 @@ export class PageAgentCore extends EventTarget {
|
|||||||
const tools = this.tools
|
const tools = this.tools
|
||||||
|
|
||||||
const actionSchemas = Array.from(tools.entries()).map(([toolName, tool]) => {
|
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(
|
const actionSchema = z.union(actionSchemas as unknown as [z.ZodType, z.ZodType, ...z.ZodType[]])
|
||||||
actionSchemas as unknown as [zod.ZodType, zod.ZodType, ...zod.ZodType[]]
|
|
||||||
)
|
|
||||||
|
|
||||||
const macroToolSchema = zod.object({
|
const macroToolSchema = z.object({
|
||||||
// thinking: zod.string().optional(),
|
// thinking: z.string().optional(),
|
||||||
evaluation_previous_goal: zod.string().optional(),
|
evaluation_previous_goal: z.string().optional(),
|
||||||
memory: zod.string().optional(),
|
memory: z.string().optional(),
|
||||||
next_goal: zod.string().optional(),
|
next_goal: z.string().optional(),
|
||||||
action: actionSchema,
|
action: actionSchema,
|
||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
description: 'You MUST call this tool every step!',
|
description: 'You MUST call this tool every step!',
|
||||||
inputSchema: macroToolSchema as zod.ZodType<MacroToolInput>,
|
inputSchema: macroToolSchema as z.ZodType<MacroToolInput>,
|
||||||
execute: async (input: MacroToolInput): Promise<MacroToolResult> => {
|
execute: async (input: MacroToolInput): Promise<MacroToolResult> => {
|
||||||
// abort
|
// abort
|
||||||
if (this.#abortController.signal.aborted) throw new Error('AbortError')
|
if (this.#abortController.signal.aborted) throw new Error('AbortError')
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
* Internal tools for PageAgent.
|
* Internal tools for PageAgent.
|
||||||
* @note Adapted from browser-use
|
* @note Adapted from browser-use
|
||||||
*/
|
*/
|
||||||
import * as zod from 'zod'
|
import * as z from 'zod'
|
||||||
|
|
||||||
import type { PageAgentCore } from '../PageAgentCore'
|
import type { PageAgentCore } from '../PageAgentCore'
|
||||||
import { waitFor } from '../utils'
|
import { waitFor } from '../utils'
|
||||||
@@ -13,7 +13,7 @@ import { waitFor } from '../utils'
|
|||||||
export interface PageAgentTool<TParams = any> {
|
export interface PageAgentTool<TParams = any> {
|
||||||
// name: string
|
// name: string
|
||||||
description: string
|
description: string
|
||||||
inputSchema: zod.ZodType<TParams>
|
inputSchema: z.ZodType<TParams>
|
||||||
execute: (this: PageAgentCore, args: TParams) => Promise<string>
|
execute: (this: PageAgentCore, args: TParams) => Promise<string>
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,9 +32,9 @@ tools.set(
|
|||||||
tool({
|
tool({
|
||||||
description:
|
description:
|
||||||
'Complete task. Text is your final response to the user — keep it concise unless the user explicitly asks for detail.',
|
'Complete task. Text is your final response to the user — keep it concise unless the user explicitly asks for detail.',
|
||||||
inputSchema: zod.object({
|
inputSchema: z.object({
|
||||||
text: zod.string(),
|
text: z.string(),
|
||||||
success: zod.boolean().default(true),
|
success: z.boolean().default(true),
|
||||||
}),
|
}),
|
||||||
execute: async function (this: PageAgentCore, input) {
|
execute: async function (this: PageAgentCore, input) {
|
||||||
// @note main loop will handle this one
|
// @note main loop will handle this one
|
||||||
@@ -47,8 +47,8 @@ tools.set(
|
|||||||
'wait',
|
'wait',
|
||||||
tool({
|
tool({
|
||||||
description: 'Wait for x seconds. Can be used to wait until the page or data is fully loaded.',
|
description: 'Wait for x seconds. Can be used to wait until the page or data is fully loaded.',
|
||||||
inputSchema: zod.object({
|
inputSchema: z.object({
|
||||||
seconds: zod.number().min(1).max(10).default(1),
|
seconds: z.number().min(1).max(10).default(1),
|
||||||
}),
|
}),
|
||||||
execute: async function (this: PageAgentCore, input) {
|
execute: async function (this: PageAgentCore, input) {
|
||||||
// try to subtract LLM calling time from the actual wait time
|
// try to subtract LLM calling time from the actual wait time
|
||||||
@@ -67,8 +67,8 @@ tools.set(
|
|||||||
tool({
|
tool({
|
||||||
description:
|
description:
|
||||||
'Ask the user a question and wait for their answer. Use this if you need more information or clarification.',
|
'Ask the user a question and wait for their answer. Use this if you need more information or clarification.',
|
||||||
inputSchema: zod.object({
|
inputSchema: z.object({
|
||||||
question: zod.string(),
|
question: z.string(),
|
||||||
}),
|
}),
|
||||||
execute: async function (this: PageAgentCore, input) {
|
execute: async function (this: PageAgentCore, input) {
|
||||||
if (!this.onAskUser) {
|
if (!this.onAskUser) {
|
||||||
@@ -84,8 +84,8 @@ tools.set(
|
|||||||
'click_element_by_index',
|
'click_element_by_index',
|
||||||
tool({
|
tool({
|
||||||
description: 'Click element by index',
|
description: 'Click element by index',
|
||||||
inputSchema: zod.object({
|
inputSchema: z.object({
|
||||||
index: zod.int().min(0),
|
index: z.int().min(0),
|
||||||
}),
|
}),
|
||||||
execute: async function (this: PageAgentCore, input) {
|
execute: async function (this: PageAgentCore, input) {
|
||||||
const result = await this.pageController.clickElement(input.index)
|
const result = await this.pageController.clickElement(input.index)
|
||||||
@@ -98,9 +98,9 @@ tools.set(
|
|||||||
'input_text',
|
'input_text',
|
||||||
tool({
|
tool({
|
||||||
description: 'Click and type text into an interactive input element',
|
description: 'Click and type text into an interactive input element',
|
||||||
inputSchema: zod.object({
|
inputSchema: z.object({
|
||||||
index: zod.int().min(0),
|
index: z.int().min(0),
|
||||||
text: zod.string(),
|
text: z.string(),
|
||||||
}),
|
}),
|
||||||
execute: async function (this: PageAgentCore, input) {
|
execute: async function (this: PageAgentCore, input) {
|
||||||
const result = await this.pageController.inputText(input.index, input.text)
|
const result = await this.pageController.inputText(input.index, input.text)
|
||||||
@@ -114,9 +114,9 @@ tools.set(
|
|||||||
tool({
|
tool({
|
||||||
description:
|
description:
|
||||||
'Select dropdown option for interactive element index by the text of the option you want to select',
|
'Select dropdown option for interactive element index by the text of the option you want to select',
|
||||||
inputSchema: zod.object({
|
inputSchema: z.object({
|
||||||
index: zod.int().min(0),
|
index: z.int().min(0),
|
||||||
text: zod.string(),
|
text: z.string(),
|
||||||
}),
|
}),
|
||||||
execute: async function (this: PageAgentCore, input) {
|
execute: async function (this: PageAgentCore, input) {
|
||||||
const result = await this.pageController.selectOption(input.index, input.text)
|
const result = await this.pageController.selectOption(input.index, input.text)
|
||||||
@@ -132,11 +132,11 @@ tools.set(
|
|||||||
'scroll',
|
'scroll',
|
||||||
tool({
|
tool({
|
||||||
description: 'Scroll the page vertically. Use index for scroll elements (dropdowns/custom UI).',
|
description: 'Scroll the page vertically. Use index for scroll elements (dropdowns/custom UI).',
|
||||||
inputSchema: zod.object({
|
inputSchema: z.object({
|
||||||
down: zod.boolean().default(true),
|
down: z.boolean().default(true),
|
||||||
num_pages: zod.number().min(0).max(10).optional().default(0.1),
|
num_pages: z.number().min(0).max(10).optional().default(0.1),
|
||||||
pixels: zod.number().int().min(0).optional(),
|
pixels: z.number().int().min(0).optional(),
|
||||||
index: zod.number().int().min(0).optional(),
|
index: z.number().int().min(0).optional(),
|
||||||
}),
|
}),
|
||||||
execute: async function (this: PageAgentCore, input) {
|
execute: async function (this: PageAgentCore, input) {
|
||||||
const result = await this.pageController.scroll({
|
const result = await this.pageController.scroll({
|
||||||
@@ -156,10 +156,10 @@ tools.set(
|
|||||||
tool({
|
tool({
|
||||||
description:
|
description:
|
||||||
'Scroll the page horizontally, or within a specific element by index. Useful for wide tables.',
|
'Scroll the page horizontally, or within a specific element by index. Useful for wide tables.',
|
||||||
inputSchema: zod.object({
|
inputSchema: z.object({
|
||||||
right: zod.boolean().default(true),
|
right: z.boolean().default(true),
|
||||||
pixels: zod.number().int().min(0),
|
pixels: z.number().int().min(0),
|
||||||
index: zod.number().int().min(0).optional(),
|
index: z.number().int().min(0).optional(),
|
||||||
}),
|
}),
|
||||||
execute: async function (this: PageAgentCore, input) {
|
execute: async function (this: PageAgentCore, input) {
|
||||||
const result = await this.pageController.scrollHorizontally(input)
|
const result = await this.pageController.scrollHorizontally(input)
|
||||||
@@ -173,8 +173,8 @@ tools.set(
|
|||||||
tool({
|
tool({
|
||||||
description:
|
description:
|
||||||
'Execute JavaScript code on the current page. Supports async/await syntax. Use with caution!',
|
'Execute JavaScript code on the current page. Supports async/await syntax. Use with caution!',
|
||||||
inputSchema: zod.object({
|
inputSchema: z.object({
|
||||||
script: zod.string(),
|
script: z.string(),
|
||||||
}),
|
}),
|
||||||
execute: async function (this: PageAgentCore, input) {
|
execute: async function (this: PageAgentCore, input) {
|
||||||
const result = await this.pageController.executeJavascript(input.script)
|
const result = await this.pageController.executeJavascript(input.script)
|
||||||
|
|||||||
@@ -6,14 +6,14 @@
|
|||||||
* - switch_to_tab: Switch to an existing tab
|
* - switch_to_tab: Switch to an existing tab
|
||||||
* - close_tab: Close a tab (optionally switch to another)
|
* - close_tab: Close a tab (optionally switch to another)
|
||||||
*/
|
*/
|
||||||
import * as zod from 'zod'
|
import * as z from 'zod'
|
||||||
|
|
||||||
import type { TabsController } from './TabsController'
|
import type { TabsController } from './TabsController'
|
||||||
|
|
||||||
/** Tool definition compatible with PageAgentCore customTools */
|
/** Tool definition compatible with PageAgentCore customTools */
|
||||||
interface TabTool {
|
interface TabTool {
|
||||||
description: string
|
description: string
|
||||||
inputSchema: zod.ZodType
|
inputSchema: z.ZodType
|
||||||
execute: (input: unknown) => Promise<string>
|
execute: (input: unknown) => Promise<string>
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,8 +26,8 @@ export function createTabTools(tabsController: TabsController): Record<string, T
|
|||||||
open_new_tab: {
|
open_new_tab: {
|
||||||
description:
|
description:
|
||||||
'Open a new browser tab with the specified URL. The new tab becomes the current tab for all subsequent page operations.',
|
'Open a new browser tab with the specified URL. The new tab becomes the current tab for all subsequent page operations.',
|
||||||
inputSchema: zod.object({
|
inputSchema: z.object({
|
||||||
url: zod.string().describe('The URL to open in the new tab'),
|
url: z.string().describe('The URL to open in the new tab'),
|
||||||
}),
|
}),
|
||||||
execute: async (input: unknown) => {
|
execute: async (input: unknown) => {
|
||||||
const { url } = input as { url: string }
|
const { url } = input as { url: string }
|
||||||
@@ -42,8 +42,8 @@ export function createTabTools(tabsController: TabsController): Record<string, T
|
|||||||
switch_to_tab: {
|
switch_to_tab: {
|
||||||
description:
|
description:
|
||||||
'Switch to an existing tab by its ID. After switching, all page operations will target the new current tab. You can only switch to tabs in the tab list shown in browser state.',
|
'Switch to an existing tab by its ID. After switching, all page operations will target the new current tab. You can only switch to tabs in the tab list shown in browser state.',
|
||||||
inputSchema: zod.object({
|
inputSchema: z.object({
|
||||||
tab_id: zod.number().int().describe('The tab ID to switch to'),
|
tab_id: z.number().int().describe('The tab ID to switch to'),
|
||||||
}),
|
}),
|
||||||
execute: async (input: unknown) => {
|
execute: async (input: unknown) => {
|
||||||
const { tab_id } = input as { tab_id: number }
|
const { tab_id } = input as { tab_id: number }
|
||||||
@@ -58,8 +58,8 @@ export function createTabTools(tabsController: TabsController): Record<string, T
|
|||||||
close_tab: {
|
close_tab: {
|
||||||
description:
|
description:
|
||||||
'Close a tab by its ID. Cannot close the initial tab. Optionally specify which tab to switch to after closing.',
|
'Close a tab by its ID. Cannot close the initial tab. Optionally specify which tab to switch to after closing.',
|
||||||
inputSchema: zod.object({
|
inputSchema: z.object({
|
||||||
tab_id: zod.number().int().describe('The tab ID to close'),
|
tab_id: z.number().int().describe('The tab ID to close'),
|
||||||
}),
|
}),
|
||||||
execute: async (input: unknown) => {
|
execute: async (input: unknown) => {
|
||||||
const { tab_id } = input as { tab_id: number }
|
const { tab_id } = input as { tab_id: number }
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* Core types for LLM integration
|
* Core types for LLM integration
|
||||||
*/
|
*/
|
||||||
import type { z } from 'zod'
|
import type * as z from 'zod'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Message format - OpenAI standard (industry standard)
|
* Message format - OpenAI standard (industry standard)
|
||||||
|
|||||||
Reference in New Issue
Block a user