कस्टम टूल
ऐसे टूल बनाएँ जिन्हें LLM opencode में कॉल कर सके।
कस्टम टूल वे फ़ंक्शन हैं जो आप बनाते हैं और जिन्हें 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
},
})
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।
बिल्ट-इन टूल के साथ नाम टकराव
कस्टम टूल टूल नाम द्वारा keyed होते हैं। यदि कोई कस्टम टूल किसी बिल्ट-इन टूल के समान नाम का उपयोग करता है, तो कस्टम टूल प्राथमिकता लेता है।
उदाहरण के लिए, यह फ़ाइल बिल्ट-इन 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}`
},
})Note: अद्वितीय नामों को प्राथमिकता दें जब तक कि आप जानबूझकर किसी बिल्ट-इन टूल को प्रतिस्थापित न करना चाहें। यदि आप किसी बिल्ट-इन टूल को अक्षम करना चाहते हैं लेकिन उसे ओवरराइड नहीं करना चाहते, तो permissions का उपयोग करें।
आर्ग्यूमेंट
आप argument प्रकार परिभाषित करने के लिए tool.schema का उपयोग कर सकते हैं, जो कि बस Zod (opens in a new tab) है।
args: {
query: tool.schema.string().describe("SQL query to execute")
}आप Zod (opens in a new tab) को सीधे इम्पोर्ट करके एक plain object भी रिटर्न कर सकते हैं:
import { z } from "zod"
export default {
description: "Tool description",
args: {
param: z.string().describe("Parameter description"),
},
async execute(args, context) {
// Tool implementation
return "result"
},
}Context
टूल वर्तमान सत्र के बारे में संदर्भ प्राप्त करते हैं:
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 का उपयोग करें।
git worktree रूट के लिए context.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()
},
})यहाँ हम Python स्क्रिप्ट चलाने के लिए Bun.$ (opens in a new tab) यूटिलिटी का उपयोग कर रहे हैं।