跳到主要內容
建立工具

自訂工具

自訂工具是您建立的函式,LLM 可以在對話過程中呼叫它們。它們與 OpenCode 的內建工具(如 read、write 和 bash)協同工作。


工具以 TypeScript 或 JavaScript 檔案的形式定義。不過,工具定義可以呼叫任何語言編寫的指令碼——TypeScript 或 JavaScript 僅用於工具定義本身。


工具可以在以下位置定義:

  • 本地定義:將工具檔案放在專案的 .opencode/tools/ 目錄中。
  • 全域定義:將工具檔案放在 ~/.config/opencode/tools/ 中。

建立工具最簡單的方式是使用 tool() 輔助函式,它提供型別安全和參數校驗。

.opencode/tools/database.ts
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) {
    // Your database logic here
    return `Executed query: ${args.query}`
  },
})

檔案名稱即為工具名稱。上面的範例建立了一個名為 database 的工具。


單檔案多工具

您也可以從單個檔案中匯出多個工具。每個匯出都會成為一個獨立的工具,命名格式為 <filename>_<exportname>:

.opencode/tools/math.ts
import { tool } from "@opencode-ai/plugin"

export const add = tool({
  description: "Add two numbers",
  args: {
    a: tool.schema.number().describe("First number"),
    b: tool.schema.number().describe("Second number"),
  },
  async execute(args) {
    return args.a + args.b
  },
})

export const multiply = tool({
  description: "Multiply two numbers",
  args: {
    a: tool.schema.number().describe("First number"),
    b: tool.schema.number().describe("Second number"),
  },
  async execute(args) {
    return args.a * args.b
  },
})

這會建立兩個工具:math_add 和 math_multiply。


與內建工具名稱衝突

自訂工具以工具名稱作為鍵值。如果自訂工具使用與內建工具相同的名稱,則自訂工具具有優先權。

例如,此檔案將替換內建的 bash 工具:

.opencode/tools/bash.ts
import { tool } from "@opencode-ai/plugin"

export default tool({
  description: "Restricted bash wrapper",
  args: {
    command: tool.schema.string(),
  },
  async execute(args) {
    return `blocked: ${args.command}`
  },
})

您可以使用 tool.schema(即 Zod)來定義參數型別。

TypeScript
args: {
  query: tool.schema.string().describe("SQL query to execute")
}

您也可以直接匯入 Zod 並回傳一個普通物件:

TypeScript
import { z } from "zod"

export default {
  description: "Tool description",
  args: {
    param: z.string().describe("Parameter description"),
  },
  async execute(args, context) {
    // Tool implementation
    return "result"
  },
}

工具會接收當前工作階段的上下文資訊:

.opencode/tools/project.ts
import { tool } from "@opencode-ai/plugin"

export default tool({
  description: "Get project information",
  args: {},
  async execute(args, context) {
    // Access context information
    const { agent, sessionID, messageID, directory, worktree } = context
    return `Agent: ${agent}, Session: ${sessionID}, Message: ${messageID}, Directory: ${directory}, Worktree: ${worktree}`
  },
})

使用 context.directory 取得工作階段的工作目錄。 使用 context.worktree 取得 git worktree 根目錄。


您可以使用任何語言編寫工具。以下範例展示了如何用 Python 實作兩數相加。

首先,建立一個 Python 指令碼作為工具:

.opencode/tools/add.py
import sys

a = int(sys.argv[1])
b = int(sys.argv[2])
print(a + b)

然後建立呼叫該指令碼的工具定義:

.opencode/tools/python-add.ts
import { tool } from "@opencode-ai/plugin"
import path from "path"

export default tool({
  description: "Add two numbers using Python",
  args: {
    a: tool.schema.number().describe("First number"),
    b: tool.schema.number().describe("Second 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()
  },
})

這裡我們使用 Bun.$ 工具函式來執行 Python 指令碼。