カスタムツール
Dropstone内でLLMが呼び出せるツールを作成します。
カスタムツールは、会話中にLLMが呼び出せる関数です。Dropstoneの組み込みツール(read、write、bashなど)と一緒に動作します。
ツールの作成
ツールはTypeScriptまたはJavaScriptファイルとして定義されます。ツール定義自体はTS/JSですが、実装は任意の言語で行えます:シェルスクリプト、Python、Go、Bunのシェルヘルパーから実行できるものなら何でも構いません。
場所
ツールは以下の場所に定義できます:
- ローカルで、プロジェクトの
.dropstone/tools/ディレクトリに配置する。 - またはグローバルで、
~/.config/dropstone/tools/に配置する。
構造
ツールを作成する最も簡単な方法は、tool() ヘルパーを使用することです。これは型安全性と検証を提供します。
import { tool } from "@blankline/dropstone-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 ツールを作成します。
ファイルごとに複数のツール
1つのファイルから複数のツールをエクスポートすることもできます。各エクスポートは個別のツールになり、名前は**<filename>_<exportname>**になります:
import { tool } from "@blankline/dropstone-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
},
})
これにより2つのツール math_add と math_multiply が作成されます。
組み込みツールとの名前の衝突
カスタムツールはツール名でキーイングされます。カスタムツールが組み込みツールと同じ名前を使用する場合、カスタムツールが優先されます。
例えば、このファイルは組み込みの bash ツールを置き換えます:
import { tool } from "@blankline/dropstone-plugin"
export default tool({
description: "Restricted bash wrapper",
args: {
command: tool.schema.string(),
},
async execute(args) {
return `blocked: ${args.command}`
},
})
Note:
意図的に組み込みツールを置き換えたい場合を除き、一意の名前を使用してください。組み込みツールを無効にしたいが上書きしたくない場合は、permissionsを使用してください。
引数
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 "@blankline/dropstone-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 を使用します。
Gitワークツリーのルートには context.worktree を使用します。
例
Pythonでツールを書く
任意の言語でツールを書くことができます。以下はPythonを使用して2つの数値を加算する例です。
まず、Pythonスクリプトとしてツールを作成します:
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
print(a + b)
次に、それを呼び出すツール定義を作成します:
import { tool } from "@blankline/dropstone-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, ".dropstone/tools/add.py")
const result = await Bun.$`python3 ${script} ${args.a} ${args.b}`.text()
return result.trim()
},
})
ここではBun.$ユーティリティを使用してPythonスクリプトを実行しています。