Files
page-agent/packages/website/src/pages/docs/features/custom-tools/page.tsx
T

113 lines
3.7 KiB
TypeScript
Raw Normal View History

2025-12-01 20:11:12 +08:00
import CodeEditor from '@/components/CodeEditor'
2026-02-27 19:46:44 +08:00
import { Heading } from '@/components/Heading'
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
2026-03-04 21:17:32 +08:00
? '通过注册自定义工具,扩展 AI Agent 的能力边界。使用 Zod 定义输入接口,让 AI 安全调用你的业务逻辑。'
: 'Extend AI Agent capabilities by registering custom tools. Define input schemas with Zod for safe business logic invocation.'}
2025-09-29 16:33:15 +08:00
</p>
<div className="space-y-8">
2026-03-05 19:15:03 +08:00
<section>
<Heading id="zod-version" className="text-2xl font-bold mb-4">
{isZh ? 'Zod 版本' : 'Zod Version'}
</Heading>
<p className="text-gray-600 dark:text-gray-300 mb-4">
{isZh
? 'Page Agent 使用 Zod 定义工具的输入 schema。支持 Zod 3 (>=3.25.0) 和 Zod 4,请从 zod/v4 子路径导入。不支持 Zod Mini。'
: 'Page Agent uses Zod for tool input schemas. Both Zod 3 (>=3.25.0) and Zod 4 are supported. Always import from the zod/v4 subpath. Zod Mini is not supported.'}
</p>
<CodeEditor
code={`// Zod 3 (>=3.25.0) or Zod 4
import { z } from 'zod/v4'`}
language="javascript"
/>
</section>
2025-09-29 16:33:15 +08:00
<section>
2026-03-04 21:17:32 +08:00
<Heading id="define-tools" className="text-2xl font-bold mb-4">
{isZh ? '定义工具' : 'Define Tools'}
2026-02-27 19:46:44 +08:00
</Heading>
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
2026-03-04 21:17:32 +08:00
? '使用 tool() 辅助函数定义自定义工具,每个工具包含 description、inputSchema 和 execute 三个属性。'
: 'Use the tool() helper to define custom tools with description, inputSchema, and execute.'}
2025-09-29 16:33:15 +08:00
</p>
<CodeEditor
2026-03-05 19:15:03 +08:00
code={`import { z } from 'zod/v4'
2025-10-21 21:53:24 +08:00
import { PageAgent, tool } from 'page-agent'
2025-09-29 16:33:15 +08:00
2026-03-04 21:17:32 +08:00
const pageAgent = new PageAgent({
customTools: {
//
add_to_cart: tool({
description: 'Add a product to the shopping cart by its product ID.',
inputSchema: z.object({
productId: z.string(),
quantity: z.number().min(1).default(1),
}),
execute: async function (input) {
await fetch('/api/cart', {
method: 'POST',
body: JSON.stringify(input),
})
return \`Added \${input.quantity}x \${input.productId} to cart.\`
},
2026-01-30 16:34:27 +08:00
}),
2025-09-29 16:33:15 +08:00
2026-03-04 21:17:32 +08:00
//
search_knowledge_base: tool({
description: 'Search the internal knowledge base and return relevant articles.',
inputSchema: z.object({
query: z.string(),
limit: z.number().max(10).default(3),
}),
execute: async function (input) {
const res = await fetch(
\`/api/kb?q=\${encodeURIComponent(input.query)}&limit=\${input.limit}\`
)
const articles = await res.json()
return JSON.stringify(articles)
},
}),
},
})`}
2025-09-29 16:33:15 +08:00
language="javascript"
/>
</section>
<section>
2026-03-04 21:17:32 +08:00
<Heading id="override-remove" className="text-2xl font-bold mb-4">
{isZh ? '覆盖与移除内置工具' : 'Override & Remove Built-in Tools'}
2026-02-27 19:46:44 +08:00
</Heading>
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
2026-03-04 21:17:32 +08:00
? '使用相同的名称可以覆盖内置工具的行为,设置为 null 则完全移除该工具。'
: 'Use the same name to override a built-in tool, or set it to null to remove it entirely.'}
2025-09-29 16:33:15 +08:00
</p>
<CodeEditor
2026-03-04 21:17:32 +08:00
code={`const pageAgent = new PageAgent({
customTools: {
scroll: null, // remove scroll tool
execute_javascript: null, // remove script execution
2025-09-29 16:33:15 +08:00
},
})`}
language="javascript"
/>
</section>
</div>
</div>
)
}