Files
page-agent/pages/docs/features/custom-tools/page.tsx
2025-09-29 16:33:15 +08:00

157 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import BetaNotice from '@pages/components/BetaNotice'
import CodeEditor from '@pages/components/CodeEditor'
export default function CustomTools() {
return (
<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
</p>
<div className="space-y-8">
<section>
<h2 className="text-2xl font-bold mb-4"></h2>
<p className="text-foreground/80 mb-4">
namedescriptioninput schema execute
</p>
<CodeEditor
code={`import { z } from 'zod'
import { pageAgent } 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')
})
// 注册工具
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()
}
})`}
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>
<p className="text-foreground/80 mb-4">
<code className="bg-gray-200 dark:bg-gray-700 px-2 py-1 rounded">pageFilter</code>{' '}
</p>
<CodeEditor
code={`pageAgent.registerTool({
name: 'approveOrder',
description: '审批订单',
input: z.object({
orderId: z.string(),
approved: z.boolean()
}),
execute: async (params) => {
// 审批逻辑
},
// 可选:页面过滤器
pageFilter: {
// 只在订单管理页面显示
include: ['/admin/orders', '/admin/orders/*'],
// 排除特定页面
exclude: ['/admin/orders/archived']
}
})`}
language="javascript"
/>
</section>
<section>
<h2 className="text-2xl font-bold mb-4"></h2>
<div className="space-y-4">
<div className="p-4 bg-yellow-50 dark:bg-yellow-900/20 rounded-lg">
<h3 className="text-lg font-semibold text-yellow-900 dark:text-yellow-300 mb-2">
</h3>
<ul className="text-foreground/80 space-y-1 text-sm">
<li> 使 pageFilter </li>
<li> execute </li>
<li> </li>
</ul>
</div>
</div>
</section>
</div>
</div>
)
}