GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — OpenCode 自定义工具 | 菜鸟教程
OpenCode 教程
OpenCode 教程
OpenCode 简介
OpenCode 安装
OpenCode API 设置
OpenCode DeepSeek
OpenCode 配置文件
OpenCode 第一次使用
OpenCode VSCode
OpenCode TUI
OpenCode CLI 使用
OpenCode Web 使用
OpenCode GitHub
OpenCode GitLab
OpenCode 工具
OpenCode 规则
OpenCode 代理
OpenCode 主题
OpenCode 快捷键
OpenCode 自定义命令
OpenCode 格式化工具
OpenCode 权限配置
OpenCode LSP 服务器配置
OpenCode MCP 服务器配置
OpenCode Skills
OpenCode skills 使用
OpenCode 自定义工具
OpenCode oh-my-openagent
OpenCode 生态系统
OpenCode 配置文件
OpenCode 自定义工具自定义工具(Custom Tools)允许你为 OpenCode 扩展能力,让 LLM 在对话过程中调用你定义的函数,从而实现数据库查询、脚本执行、API 调用等复杂操作。
这些工具会与内置工具(如 自定义工具是 OpenCode 的核心扩展能力,它让 LLM 从只会生成代码,升级为可以执行任务。 通过合理设计工具,你可以构建:
一、什么是自定义工具自定义工具本质上是一个函数接口,LLM 可以在运行过程中主动调用它,并获取执行结果。 典型应用场景:
你可以理解为:让 AI 拥有执行能力的插件机制。 二、工具存放位置OpenCode 会自动加载以下目录中的工具:
文件名即工具名称(非常关键)。 三、基础工具定义(推荐方式)
使用 import { tool } from "@opencode-ai/plugin"
export default tool({
description: "Query the project database",
args: {
query: tool.schema.string().describe("SQL query to execute"),
},
async execute(args) {
return `Executed query: ${args.query}`
},
})
说明:
四、单文件多工具一个文件可以导出多个工具,每个工具会自动命名为:
import { tool } from "@opencode-ai/plugin"
export const add = tool({
description: "Add two numbers",
args: {
a: tool.schema.number(),
b: tool.schema.number(),
},
async execute(args) {
return args.a + args.b
},
})
export const multiply = tool({
description: "Multiply two numbers",
args: {
a: tool.schema.number(),
b: tool.schema.number(),
},
async execute(args) {
return args.a * args.b
},
})
最终生成工具:
五、与内置工具冲突如果你的工具名称与内置工具相同,会覆盖内置工具。 // 覆盖内置 bash 工具
export default tool({
description: "Restricted bash wrapper",
args: {
command: tool.schema.string(),
},
async execute(args) {
return `blocked: ${args.command}`
},
})
建议:
六、参数定义(Zod)参数使用 Zod Schema 定义,支持类型校验与描述: args: {
query: tool.schema.string().describe("SQL query"),
limit: tool.schema.number().optional(),
}
也可以直接使用 Zod: import { z } from "zod"
export default {
description: "Example tool",
args: {
name: z.string(),
},
async execute(args) {
return `Hello ${args.name}`
},
}
七、上下文(Context)工具执行时会自动注入上下文信息: export default tool({
description: "Get project info",
args: {},
async execute(args, context) {
const { agent, sessionID, directory, worktree } = context
return `Agent: ${agent}, Dir: ${directory}`
},
})<
可用字段:
八、调用外部语言(Python 示例)你可以用任何语言实现工具逻辑,例如 Python。 1、Python 脚本# .opencode/tools/add.py import sys a = int(sys.argv[1]) b = int(sys.argv[2]) print(a + b) 2、工具封装import { tool } from "@opencode-ai/plugin"
import path from "path"
export default tool({
description: "Add two numbers using Python",
args: {
a: tool.schema.number(),
b: tool.schema.number(),
},
async execute(args, context) {
const script = path.join(context.worktree, ".opencode/tools/add.py")
const result = await Bun.$`python3 ${script} ${args.a} ${args.b}`.text()
return result.trim()
},
})
这种方式适合:
九、最佳实践
AI 思考中...
点我分享笔记取消
分类导航
Advertisement
|
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |