कस्टम टूल्स
opencode में LLM द्वारा कॉल किए जा सकने वाले टूल्स बनाएँ।
कस्टम टूल्स ऐसे फ़ंक्शन हैं जो आप बनाते हैं और जिन्हें 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।
बिल्ट-इन टूल्स के साथ नाम टकराव
कस्टम टूल्स टूल नाम द्वारा कुंजीबद्ध होते हैं। यदि कोई कस्टम टूल बिल्ट-इन टूल के समान नाम का उपयोग करता है, तो कस्टम टूल को प्राथमिकता मिलती है।
उदाहरण के लिए, यह फ़ाइल बिल्ट-इन 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 (opens in a new tab) है।
args: {
query: tool.schema.string().describe("SQL query to execute")
}आप Zod (opens in a new tab) को सीधे इम्पोर्ट भी कर सकते हैं और एक सादा ऑब्जेक्ट रिटर्न कर सकते हैं:
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 का उपयोग करें। 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) यूटिलिटी का उपयोग कर रहे हैं।