自訂工具

為 OpenCode 大型語言模型建立自訂工具,支援 TypeScript 與 JavaScript 定義、Zod schema 驗證,以及單一檔案匯出多個工具。

本頁內文若連結到尚未提供繁體版本的頁面(例如 /ecosystem/plugins/、/news/),會開啟英文原頁;導覽與頁尾標有 EN 的項目即為這類頁面。

自訂工具是你建立的一些函式,LLM 可以在工作階段期間呼叫它們。它們與 OpenCode 的內建工具(如 read、write 和 bash)搭配使用。


建立一個工具

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


位置

可以透過以下方式定義工具:

  • 在專案的 .opencode/tools/ 目錄中本機定義。
  • 或者全域定義在 ~/.config/opencode/tools/ 中。

結構

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

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>:

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).toString()
  },
})

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).toString()
  },
})

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


與內建工具的名稱衝突

自訂工具是以工具名稱為鍵。若某個自訂工具與內建工具同名,自訂工具會優先採用。

例如,下方這個檔案會取代內建的 bash 工具:

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)來定義參數型別。

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

你也可以直接匯入 Zod 並回傳一個一般物件:

import { z } from "zod"

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

上下文

工具可以接收關於目前工作階段的上下文:

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 實作兩數相加的範例。

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

import sys

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

接著建立呼叫它的工具定義:

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 指令碼。