हिन्दी
दस्तावेज़
प्लगइन्स

प्लगइन्स

OpenCode को एक्सटेंड करने के लिए अपने खुद के प्लगइन्स लिखें।

प्लगइन्स आपको विभिन्न इवेंट्स में हुक करके और व्यवहार को कस्टमाइज़ करके OpenCode को एक्सटेंड करने की अनुमति देते हैं। आप नई सुविधाएं जोड़ने, बाहरी सेवाओं के साथ इंटीग्रेट करने, या OpenCode के डिफ़ॉल्ट व्यवहार को संशोधित करने के लिए प्लगइन्स बना सकते हैं।

कम्युनिटी द्वारा बनाए गए प्लगइन्स के उदाहरण देखें।


प्लगइन का उपयोग करें

प्लगइन्स लोड करने के दो तरीके हैं।


लोकल फाइल्स से

JavaScript या TypeScript फाइल्स को प्लगइन डायरेक्टरी में रखें।

  • .opencode/plugins/ - प्रोजेक्ट-लेवल प्लगइन्स
  • ~/.config/opencode/plugins/ - ग्लोबल प्लगइन्स

इन डायरेक्टरीज़ में फाइल्स स्टार्टअप पर ऑटोमैटिकली लोड होती हैं।


npm से

अपनी कॉन्फ़िग फाइल में npm पैकेजेस स्पेसिफाई करें।

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "plugin": ["opencode-helicone-session", "opencode-wakatime", "@my-org/custom-plugin"]
}

रेगुलर और स्कोप्ड दोनों npm पैकेजेस सपोर्टेड हैं।

इकोसिस्टम में उपलब्ध प्लगइन्स ब्राउज़ करें।


प्लगइन्स कैसे इंस्टॉल होते हैं

npm प्लगइन्स स्टार्टअप पर Bun का उपयोग करके ऑटोमैटिकली इंस्टॉल होते हैं। पैकेजेस और उनकी डिपेंडेंसीज़ ~/.cache/opencode/node_modules/ में कैश होती हैं।

लोकल प्लगइन्स सीधे प्लगइन डायरेक्टरी से लोड होते हैं। एक्सटर्नल पैकेजेस का उपयोग करने के लिए, आपको अपनी कॉन्फ़िग डायरेक्टरी में package.json बनाना होगा (डिपेंडेंसीज़ देखें), या प्लगइन को npm पर पब्लिश करें और इसे अपनी कॉन्फ़िग में जोड़ें


लोड ऑर्डर

प्लगइन्स सभी सोर्सेस से लोड होते हैं और सभी हुक्स क्रम में चलते हैं। लोड ऑर्डर है:

  1. ग्लोबल कॉन्फ़िग (~/.config/opencode/opencode.json)
  2. प्रोजेक्ट कॉन्फ़िग (opencode.json)
  3. ग्लोबल प्लगइन डायरेक्टरी (~/.config/opencode/plugins/)
  4. प्रोजेक्ट प्लगइन डायरेक्टरी (.opencode/plugins/)

समान नाम और वर्जन वाले डुप्लिकेट npm पैकेजेस एक बार लोड होते हैं। हालांकि, समान नामों वाले लोकल प्लगइन और npm प्लगइन अलग-अलग लोड होते हैं।


प्लगइन बनाएं

एक प्लगइन एक JavaScript/TypeScript मॉड्यूल है जो एक या अधिक प्लगइन फंक्शन्स एक्सपोर्ट करता है। प्रत्येक फंक्शन एक कॉन्टेक्स्ट ऑब्जेक्ट प्राप्त करता है और एक हुक्स ऑब्जेक्ट रिटर्न करता है।


डिपेंडेंसीज़

लोकल प्लगइन्स और कस्टम टूल्स एक्सटर्नल npm पैकेजेस का उपयोग कर सकते हैं। अपनी कॉन्फ़िग डायरेक्टरी में आवश्यक डिपेंडेंसीज़ के साथ package.json जोड़ें।

.opencode/package.json
{
  "dependencies": {
    "shescape": "^2.1.0"
  }
}

OpenCode इन्हें इंस्टॉल करने के लिए स्टार्टअप पर bun install चलाता है। फिर आपके प्लगइन्स और टूल्स इन्हें इम्पोर्ट कर सकते हैं।

.opencode/plugins/my-plugin.ts
import { escape } from "shescape"
 
export const MyPlugin = async (ctx) => {
  return {
    "tool.execute.before": async (input, output) => {
      if (input.tool === "bash") {
        output.args.command = escape(output.args.command)
      }
    },
  }
}

बेसिक स्ट्रक्चर

.opencode/plugins/example.js
export const MyPlugin = async ({ project, client, $, directory, worktree }) => {
  console.log("Plugin initialized!")
 
  return {
    // हुक इम्प्लीमेंटेशन यहां जाते हैं
  }
}

प्लगइन फंक्शन प्राप्त करता है:

  • project: वर्तमान प्रोजेक्ट जानकारी।
  • directory: वर्तमान वर्किंग डायरेक्टरी।
  • worktree: git worktree पाथ।
  • client: AI के साथ इंटरैक्ट करने के लिए opencode SDK क्लाइंट।
  • $: कमांड्स एक्जीक्यूट करने के लिए Bun की shell API (opens in a new tab)

TypeScript सपोर्ट

TypeScript प्लगइन्स के लिए, आप प्लगइन पैकेज से टाइप्स इम्पोर्ट कर सकते हैं:

my-plugin.ts
import type { Plugin } from "@opencode-ai/plugin"
 
export const MyPlugin: Plugin = async ({ project, client, $, directory, worktree }) => {
  return {
    // टाइप-सेफ हुक इम्प्लीमेंटेशन
  }
}

इवेंट्स

प्लगइन्स नीचे Examples सेक्शन में दिखाए अनुसार इवेंट्स को सब्सक्राइब कर सकते हैं। यहां उपलब्ध विभिन्न इवेंट्स की सूची है।

कमांड इवेंट्स

  • command.executed

फाइल इवेंट्स

  • file.edited
  • file.watcher.updated

इंस्टॉलेशन इवेंट्स

  • installation.updated

LSP इवेंट्स

  • lsp.client.diagnostics
  • lsp.updated

मैसेज इवेंट्स

  • message.part.removed
  • message.part.updated
  • message.removed
  • message.updated

परमिशन इवेंट्स

  • permission.asked
  • permission.replied

सर्वर इवेंट्स

  • server.connected

सेशन इवेंट्स

  • session.created
  • session.compacted
  • session.deleted
  • session.diff
  • session.error
  • session.idle
  • session.status
  • session.updated

Todo इवेंट्स

  • todo.updated

Shell इवेंट्स

  • shell.env

टूल इवेंट्स

  • tool.execute.after
  • tool.execute.before

TUI इवेंट्स

  • tui.prompt.append
  • tui.command.execute
  • tui.toast.show

उदाहरण

यहां कुछ प्लगइन्स के उदाहरण हैं जिनका उपयोग आप opencode को एक्सटेंड करने के लिए कर सकते हैं।


नोटिफिकेशन भेजें

कुछ इवेंट्स होने पर नोटिफिकेशन भेजें:

.opencode/plugins/notification.js
export const NotificationPlugin = async ({ project, client, $, directory, worktree }) => {
  return {
    event: async ({ event }) => {
      // सेशन पूरा होने पर नोटिफिकेशन भेजें
      if (event.type === "session.idle") {
        await $`osascript -e 'display notification "Session completed!" with title "opencode"'`
      }
    },
  }
}

हम macOS पर AppleScript चलाने के लिए osascript का उपयोग कर रहे हैं। यहां हम इसका उपयोग नोटिफिकेशन भेजने के लिए कर रहे हैं।

नोट: यदि आप OpenCode डेस्कटॉप ऐप का उपयोग कर रहे हैं, तो यह रिस्पॉन्स तैयार होने या सेशन में एरर होने पर ऑटोमैटिकली सिस्टम नोटिफिकेशन भेज सकता है।


.env प्रोटेक्शन

opencode को .env फाइल्स पढ़ने से रोकें:

.opencode/plugins/env-protection.js
export const EnvProtection = async ({ project, client, $, directory, worktree }) => {
  return {
    "tool.execute.before": async (input, output) => {
      if (input.tool === "read" && output.args.filePath.includes(".env")) {
        throw new Error("Do not read .env files")
      }
    },
  }
}

एनवायरनमेंट वेरिएबल्स इंजेक्ट करें

सभी shell एक्जीक्यूशन (AI टूल्स और यूजर टर्मिनल्स) में एनवायरनमेंट वेरिएबल्स इंजेक्ट करें:

.opencode/plugins/inject-env.js
export const InjectEnvPlugin = async () => {
  return {
    "shell.env": async (input, output) => {
      output.env.MY_API_KEY = "secret"
      output.env.PROJECT_ROOT = input.cwd
    },
  }
}

कस्टम टूल्स

प्लगइन्स opencode में कस्टम टूल्स भी जोड़ सकते हैं:

.opencode/plugins/custom-tools.ts
import { type Plugin, tool } from "@opencode-ai/plugin"
 
export const CustomToolsPlugin: Plugin = async (ctx) => {
  return {
    tool: {
      mytool: tool({
        description: "This is a custom tool",
        args: {
          foo: tool.schema.string(),
        },
        async execute(args, context) {
          const { directory, worktree } = context
          return `Hello ${args.foo} from ${directory} (worktree: ${worktree})`
        },
      }),
    },
  }
}

tool हेल्पर एक कस्टम टूल बनाता है जिसे opencode कॉल कर सकता है। यह एक Zod स्कीमा फंक्शन लेता है और एक टूल डेफिनिशन रिटर्न करता है जिसमें:

  • description: टूल क्या करता है
  • args: टूल के आर्ग्युमेंट्स के लिए Zod स्कीमा
  • execute: फंक्शन जो टूल कॉल होने पर चलता है

आपके कस्टम टूल्स बिल्ट-इन टूल्स के साथ opencode में उपलब्ध होंगे।


लॉगिंग

स्ट्रक्चर्ड लॉगिंग के लिए console.log के बजाय client.app.log() का उपयोग करें:

.opencode/plugins/my-plugin.ts
export const MyPlugin = async ({ client }) => {
  await client.app.log({
    body: {
      service: "my-plugin",
      level: "info",
      message: "Plugin initialized",
      extra: { foo: "bar" },
    },
  })
}

लेवल्स: debug, info, warn, error। विवरण के लिए SDK डॉक्यूमेंटेशन (opens in a new tab) देखें।


कॉम्पैक्शन हुक्स

सेशन कॉम्पैक्ट होने पर शामिल कॉन्टेक्स्ट को कस्टमाइज़ करें:

.opencode/plugins/compaction.ts
import type { Plugin } from "@opencode-ai/plugin"
 
export const CompactionPlugin: Plugin = async (ctx) => {
  return {
    "experimental.session.compacting": async (input, output) => {
      // कॉम्पैक्शन प्रॉम्प्ट में अतिरिक्त कॉन्टेक्स्ट इंजेक्ट करें
      output.context.push(`
## Custom Context
 
Include any state that should persist across compaction:
- Current task status
- Important decisions made
- Files being actively worked on
`)
    },
  }
}

experimental.session.compacting हुक LLM द्वारा कंटिन्यूएशन समरी जेनरेट करने से पहले फायर होता है। इसका उपयोग डोमेन-स्पेसिफिक कॉन्टेक्स्ट इंजेक्ट करने के लिए करें जो डिफ़ॉल्ट कॉम्पैक्शन प्रॉम्प्ट मिस कर सकता है।

आप output.prompt सेट करके कॉम्पैक्शन प्रॉम्प्ट को पूरी तरह से रिप्लेस भी कर सकते हैं:

.opencode/plugins/custom-compaction.ts
import type { Plugin } from "@opencode-ai/plugin"
 
export const CustomCompactionPlugin: Plugin = async (ctx) => {
  return {
    "experimental.session.compacting": async (input, output) => {
      // पूरे कॉम्पैक्शन प्रॉम्प्ट को रिप्लेस करें
      output.prompt = `
You are generating a continuation prompt for a multi-agent swarm session.
 
Summarize:
1. The current task and its status
2. Which files are being modified and by whom
3. Any blockers or dependencies between agents
4. The next steps to complete the work
 
Format as a structured prompt that a new agent can use to resume work.
`
    },
  }
}

जब output.prompt सेट होता है, तो यह डिफ़ॉल्ट कॉम्पैक्शन प्रॉम्प्ट को पूरी तरह से रिप्लेस कर देता है। इस केस में output.context एरे को इग्नोर किया जाता है।