2025-12-01 20:11:12 +08:00
|
|
|
import BetaNotice from '@/components/BetaNotice'
|
|
|
|
|
import CodeEditor from '@/components/CodeEditor'
|
2026-01-30 16:47:23 +08:00
|
|
|
import { useLanguage } from '@/i18n/context'
|
2025-12-01 20:11:12 +08:00
|
|
|
|
2025-09-29 16:33:15 +08:00
|
|
|
export default function CustomTools() {
|
2026-01-30 16:47:23 +08:00
|
|
|
const { isZh } = useLanguage()
|
2025-10-22 22:35:25 +08:00
|
|
|
|
2025-09-29 16:33:15 +08:00
|
|
|
return (
|
|
|
|
|
<div>
|
2026-01-30 15:13:31 +08:00
|
|
|
<h1 className="text-4xl font-bold mb-6">{isZh ? '自定义工具' : 'Custom Tools'}</h1>
|
2025-09-29 16:33:15 +08:00
|
|
|
|
2025-10-22 22:35:25 +08:00
|
|
|
<p className="text-xl text-gray-600 dark:text-gray-300 mb-8 leading-relaxed">
|
2026-01-30 15:13:31 +08:00
|
|
|
{isZh
|
|
|
|
|
? '通过注册自定义工具,扩展 AI Agent 的能力边界。使用 Zod 定义严格的输入接口,让 AI 安全调用你的业务逻辑。'
|
|
|
|
|
: 'Extend AI Agent capabilities by registering custom tools. Use Zod to define strict input schemas for safe business logic calls.'}
|
2025-09-29 16:33:15 +08:00
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<div className="space-y-8">
|
|
|
|
|
<section>
|
2026-01-30 15:13:31 +08:00
|
|
|
<h2 className="text-2xl font-bold mb-4">{isZh ? '工具注册' : 'Tool Registration'}</h2>
|
2025-10-22 22:35:25 +08:00
|
|
|
<p className="text-gray-600 dark:text-gray-300 mb-4">
|
2026-01-30 15:13:31 +08:00
|
|
|
{isZh
|
|
|
|
|
? '每个自定义工具需要定义四个核心属性:name、description、input schema 和 execute 函数。'
|
|
|
|
|
: 'Each custom tool requires four core properties: name, description, input schema, and execute function.'}
|
2025-09-29 16:33:15 +08:00
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<CodeEditor
|
2026-02-09 17:49:10 +08:00
|
|
|
code={`import * as zod from 'zod'
|
2025-10-21 21:53:24 +08:00
|
|
|
import { PageAgent, tool } from 'page-agent'
|
2025-09-29 16:33:15 +08:00
|
|
|
|
2025-10-21 21:53:24 +08:00
|
|
|
// override internal tool
|
|
|
|
|
const customTools = {
|
2026-01-30 16:34:27 +08:00
|
|
|
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
|
|
|
|
|
},
|
|
|
|
|
})
|
2025-10-21 21:53:24 +08:00
|
|
|
}
|
2025-09-29 16:33:15 +08:00
|
|
|
|
2025-10-21 21:53:24 +08:00
|
|
|
// remove internal tool
|
|
|
|
|
const customTools = {
|
2026-01-30 16:34:27 +08:00
|
|
|
scroll: null, // never scroll
|
2025-10-21 21:53:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pageAgent = new PageAgent({customTools})
|
|
|
|
|
`}
|
2025-09-29 16:33:15 +08:00
|
|
|
language="javascript"
|
|
|
|
|
/>
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
<section>
|
2026-01-30 15:13:31 +08:00
|
|
|
<h2 className="text-2xl font-bold mb-4">{isZh ? '页面过滤器' : 'Page Filter'}</h2>
|
2025-09-29 16:33:15 +08:00
|
|
|
|
2025-10-21 21:53:24 +08:00
|
|
|
<BetaNotice />
|
2025-09-29 16:33:15 +08:00
|
|
|
|
2025-10-22 22:35:25 +08:00
|
|
|
<p className="text-gray-600 dark:text-gray-300 mb-4">
|
2026-01-30 15:13:31 +08:00
|
|
|
{isZh
|
|
|
|
|
? '通过 pageFilter 属性控制工具在哪些页面可见,提升安全性和用户体验。'
|
|
|
|
|
: 'Control tool visibility on specific pages via the pageFilter property to enhance security and UX.'}
|
2025-09-29 16:33:15 +08:00
|
|
|
</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>
|
2026-01-30 15:13:31 +08:00
|
|
|
<h2 className="text-2xl font-bold mb-4">{isZh ? '最佳实践' : 'Best Practices'}</h2>
|
2025-09-29 16:33:15 +08:00
|
|
|
<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">
|
2026-01-30 15:13:31 +08:00
|
|
|
{isZh ? '⚡ 性能优化' : '⚡ Performance Optimization'}
|
2025-09-29 16:33:15 +08:00
|
|
|
</h3>
|
2025-10-22 22:35:25 +08:00
|
|
|
<ul className="text-gray-600 dark:text-gray-300 space-y-1 text-sm">
|
2026-01-30 15:13:31 +08:00
|
|
|
<li>
|
|
|
|
|
{isZh
|
|
|
|
|
? '• 使用 pageFilter 减少不必要的工具加载'
|
|
|
|
|
: '• Use pageFilter to reduce unnecessary tool loading'}
|
|
|
|
|
</li>
|
|
|
|
|
<li>
|
|
|
|
|
{isZh
|
|
|
|
|
? '• 在 execute 函数中实现适当的缓存机制'
|
|
|
|
|
: '• Implement appropriate caching in execute functions'}
|
|
|
|
|
</li>
|
|
|
|
|
<li>
|
|
|
|
|
{isZh
|
|
|
|
|
? '• 避免在工具中执行耗时的同步操作'
|
|
|
|
|
: '• Avoid long-running sync operations in tools'}
|
|
|
|
|
</li>
|
2025-09-29 16:33:15 +08:00
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|