कस्टम टूल्स
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।
आर्ग्युमेंट्स
आप 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) यूटिलिटी का उपयोग कर रहे हैं।