@@ -6,8 +6,6 @@ export default function CustomTools() {
< div >
< h1 className = "text-4xl font-bold mb-6" > 自 定 义 工 具 < / h1 >
< BetaNotice / >
< p className = "text-xl text-foreground/80 mb-8 leading-relaxed" >
通 过 注 册 自 定 义 工 具 , 扩 展 AI Agent 的 能 力 边 界 。 使 用 Zod 定 义 严 格 的 输 入 接 口 , 让 AI
安 全 调 用 你 的 业 务 逻 辑 。
@@ -21,92 +19,40 @@ export default function CustomTools() {
< / p >
< CodeEditor
code = { ` import { z } from 'zod'
import { p ageAgent } from 'page-agent'
code = { ` import zod from 'zod'
import { P ageAgent, tool } from 'page-agent'
// 定义输入 schema
const CreateUserSchema = z.object( {
name: z.string().min(1, '姓名不能为空'),
email: z.string().email('邮箱格式不正确'),
role: z.enum(['admin', 'user', 'guest']).default('user')
})
// override internal tool
const customTools = {
ask_user: tool({
description:
'Ask the user or parent model a question and wait for their answer. Use this if you need more information or clarification.',
inputSchema: zod.object({
question: zod.string(),
}),
execute: async function (this: PageAgent, input) {
const answer = await do_some_thing(input.question)
return "✅ Received user answer: " + answer
},
})
}
// 注册工具
pageAgent.registerTool( {
name: 'createUser',
description: '创建新用户账户',
input: CreateUserSchema,
execute: async (params) => {
// 执行业务逻辑
const response = await fetch('/api/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(params)
})
return await response.json()
}
}) ` }
// remove internal tool
const customTools = {
ask_user: null // never ask user questions
}
const pageAgent = new PageAgent({customTools})
` }
language = "javascript"
/ >
< / section >
< section >
< h2 className = "text-2xl font-bold mb-4" > 属 性 详 解 < / h2 >
< div className = "space-y-4" >
< div className = "p-4 bg-blue-50 dark:bg-blue-900/20 rounded-lg" >
< h3 className = "text-lg font-semibold text-blue-900 dark:text-blue-300 mb-2" >
📝 name ( 必 需 )
< / h3 >
< p className = "text-foreground/80 mb-2" > 工 具 的 唯 一 标 识 符 , AI 通 过 此 名 称 调 用 工 具 。 < / p >
< div className = "bg-white dark:bg-gray-800 rounded p-3 text-sm" >
< code > name : 'searchProducts' // 驼峰命名,语义清晰</code>
< / div >
< / div >
< div className = "p-4 bg-green-50 dark:bg-green-900/20 rounded-lg" >
< h3 className = "text-lg font-semibold text-green-900 dark:text-green-300 mb-2" >
💬 description ( 必 需 )
< / h3 >
< p className = "text-foreground/80 mb-2" > 详 细 描 述 工 具 功 能 , 帮 助 AI 理 解 使 用 场 景 。 < / p >
< div className = "bg-white dark:bg-gray-800 rounded p-3 text-sm" >
< code > description : '根据关键词搜索商品,支持价格区间和分类筛选' < / code >
< / div >
< / div >
< div className = "p-4 bg-purple-50 dark:bg-purple-900/20 rounded-lg" >
< h3 className = "text-lg font-semibold text-purple-900 dark:text-purple-300 mb-2" >
🔧 input ( 必 需 )
< / h3 >
< p className = "text-foreground/80 mb-2" > Zod schema 定 义 输 入 参 数 的 类 型 和 验 证 规 则 。 < / p >
< div className = "bg-white dark:bg-gray-800 rounded p-3 text-sm" >
< code > { ` input: z.object({
keyword: z.string().min(1),
priceRange: z.object({
min: z.number().optional(),
max: z.number().optional()
}).optional()
}) ` } < / code >
< / div >
< / div >
< div className = "p-4 bg-orange-50 dark:bg-orange-900/20 rounded-lg" >
< h3 className = "text-lg font-semibold text-orange-900 dark:text-orange-300 mb-2" >
⚡ execute ( 必 需 )
< / h3 >
< p className = "text-foreground/80 mb-2" > 异 步 函 数 , 接 收 验 证 后 的 参 数 并 执 行 具 体 逻 辑 。 < / p >
< div className = "bg-white dark:bg-gray-800 rounded p-3 text-sm" >
< code > { ` execute: async (params) => {
// params 已通过 Zod 验证
const result = await businessLogic(params)
return result // 返回结果给 AI
} ` } < / code >
< / div >
< / div >
< / div >
< / section >
< section >
< h2 className = "text-2xl font-bold mb-4" > 页 面 过 滤 器 < / h2 >
< BetaNotice / >
< p className = "text-foreground/80 mb-4" >
通 过 < code className = "bg-gray-200 dark:bg-gray-700 px-2 py-1 rounded" > pageFilter < / code > { ' ' }
属 性 控 制 工 具 在 哪 些 页 面 可 见 , 提 升 安 全 性 和 用 户 体 验 。