カスタムツール
カスタムツールは、会話中に LLM が呼び出せる関数です。read、write、bash などの opencode の組み込みツール と並行して動作します。
ツールの作成
ツールは TypeScript または JavaScript ファイルとして定義されます。ただし、ツール定義は 任意の言語 で記述されたスクリプトを呼び出すことができます。TypeScript または JavaScript はツール定義自体にのみ使用されます。
配置場所
ツールは次の場所に定義できます。
- プロジェクトの
.opencode/tools/ディレクトリに配置してローカルに定義 - または
~/.config/opencode/tools/に配置してグローバルに定義
構造
ツールを作成する最も簡単な方法は、型安全性と検証を提供する tool() ヘルパーを使用することです。
import { tool } from "@opencode-ai/plugin"
export default tool({
description: "プロジェクトデータベースにクエリを実行します",
args: {
query: tool.schema.string().describe("実行する SQL クエリ"),
},
async execute(args) {
// ここにデータベースのロジックを記述します
return `Executed query: ${args.query}`
},
})
ファイル名 が ツール名 になります。上記は database ツールを作成します。
1 ファイルに複数のツールを定義する
1 つのファイルから複数のツールをエクスポートすることもできます。各エクスポートは <filename>_<exportname> という名前の 個別のツール になります。
import { tool } from "@opencode-ai/plugin"
export const add = tool({
description: "2 つの数値を加算します",
args: {
a: tool.schema.number().describe("1 番目の数値"),
b: tool.schema.number().describe("2 番目の数値"),
},
async execute(args) {
return (args.a + args.b).toString()
},
})
export const multiply = tool({
description: "2 つの数値を乗算します",
args: {
a: tool.schema.number().describe("1 番目の数値"),
b: tool.schema.number().describe("2 番目の数値"),
},
async execute(args) {
return (args.a * args.b).toString()
},
})
これにより math_add と math_multiply の 2 つのツールが作成されます。
組み込みツールとの名前衝突
カスタムツールはツール名でキー付けされます。カスタムツールが組み込みツールと同じ名前を使用する場合、カスタムツールが優先されます。
たとえば、次のファイルは組み込みの bash ツールを置き換えます。
import { tool } from "@opencode-ai/plugin"
export default tool({
description: "制限付きの bash ラッパー",
args: {
command: tool.schema.string(),
},
async execute(args) {
return `blocked: ${args.command}`
},
})
引数
tool.schema を使用できます。これは引数型を定義するための Zod です。
args: {
query: tool.schema.string().describe("実行する SQL クエリ")
}
Zod を直接インポートして、プレーンオブジェクトを返すこともできます。
import { z } from "zod"
export default {
description: "ツールの説明",
args: {
param: z.string().describe("パラメータの説明"),
},
async execute(args, context) {
// ツールの実装
return "result"
},
}
コンテキスト
ツールは現在のセッションに関するコンテキストを受け取ります。
import { tool } from "@opencode-ai/plugin"
export default tool({
description: "プロジェクト情報を取得します",
args: {},
async execute(args, context) {
// コンテキスト情報にアクセス
const { agent, sessionID, messageID, directory, worktree } = context
return `Agent: ${agent}, Session: ${sessionID}, Message: ${messageID}, Directory: ${directory}, Worktree: ${worktree}`
},
})
セッションの作業ディレクトリには context.directory を使用します。
git worktree のルートには context.worktree を使用します。
例
Python でツールを作成する
任意の言語でツールを作成できます。Python を使用して 2 つの数値を加算する例を次に示します。
まず、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: "Python を使用して 2 つの数値を加算します",
args: {
a: tool.schema.number().describe("1 番目の数値"),
b: tool.schema.number().describe("2 番目の数値"),
},
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 スクリプトを実行しています。