| --- |
| title: Programtillegg |
| description: Skriv dine egne programtillegg for å utvide OpenCode. |
| --- |
| |
| Plugins lar deg utvide OpenCode ved å koble til ulike hendelser og tilpasse atferd. Du kan lage plugins for å legge til nye funksjoner, integrere med eksterne tjenester eller endre standardoppførselen til OpenCode. |
|
|
| For eksempler, sjekk ut [plugins](/docs/ecosystem#plugins) opprettet av fellesskapet. |
|
|
| --- |
| |
| |
|
|
| Det er to måter å laste inn plugins. |
|
|
| --- |
| |
| |
|
|
| Plasser JavaScript- eller TypeScript-filer i plugin-katalogen. |
|
|
| - `.opencode/plugins/` - Programtillegg på prosjektnivå |
| - `~/.config/opencode/plugins/` - Globale plugins |
|
|
| Filer i disse katalogene lastes automatisk ved oppstart. |
|
|
| --- |
| |
| |
|
|
| Spesifiser npm-pakker i konfigurasjonsfilen. |
|
|
| ```json title="opencode.json" |
| { |
| "$schema": "https://opencode.ai/config.json", |
| "plugin": ["opencode-helicone-session", "opencode-wakatime", "@my-org/custom-plugin"] |
| } |
| ``` |
|
|
| Både vanlige og scoped npm-pakker støttes. |
|
|
| Bla gjennom tilgjengelige plugins i [økosystemet](/docs/ecosystem#plugins). |
|
|
| --- |
| |
| |
|
|
| **npm-plugins** installeres automatisk ved hjelp av Bun ved oppstart. Pakker og deres avhengigheter er bufret i `~/.cache/opencode/node_modules/`. |
|
|
| **Lokale plugins** lastes direkte fra plugin-katalogen. For å bruke eksterne pakker, må du opprette en `package.json` i konfigurasjonskatalogen din (se [Dependencies](#dependencies)), eller publisere plugin-en til npm og [legg den til i konfigurasjonen din](/docs/config#plugins). |
|
|
| --- |
| |
| |
|
|
| Plugins lastes inn fra alle kilder og alle kroker kjøres i rekkefølge. Lastrekkefølgen er: |
|
|
| 1. Global konfigurasjon (`~/.config/opencode/opencode.json`) |
| 2. Prosjektkonfigurasjon (`opencode.json`) |
| 3. Global plugin-katalog (`~/.config/opencode/plugins/`) |
| 4. Prosjektpluginkatalog (`.opencode/plugins/`) |
|
|
| Dupliserte npm-pakker med samme navn og versjon lastes inn én gang. Imidlertid lastes en lokal plugin og en npm plugin med lignende navn begge separat. |
|
|
| --- |
| |
| |
|
|
| En plugin er en **JavaScript/TypeScript-modul** som eksporterer en eller flere plugin-moduler |
| funksjoner. Hver funksjon mottar et kontekstobjekt og returnerer et krokobjekt. |
|
|
| --- |
| |
| |
|
|
| Lokale plugins og tilpassede verktøy kan bruke eksterne npm-pakker. Legg til en `package.json` til konfigurasjonskatalogen med avhengighetene du trenger. |
|
|
| ```json title=".opencode/package.json" |
| { |
| "dependencies": { |
| "shescape": "^2.1.0" |
| } |
| } |
| ``` |
|
|
| OpenCode kjører `bun install` ved oppstart for å installere disse. Programtilleggene og verktøyene dine kan deretter importere dem. |
|
|
| ```ts title=".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) |
| } |
| }, |
| } |
| } |
| ``` |
|
|
| --- |
| |
| |
|
|
| ```js title=".opencode/plugins/example.js" |
| export const MyPlugin = async ({ project, client, $, directory, worktree }) => { |
| console.log("Plugin initialized!") |
|
|
| return { |
| // Hook implementations go here |
| } |
| } |
| ``` |
|
|
| Plugin-funksjonen mottar: |
|
|
| - `project`: Gjeldende prosjektinformasjon. |
| - `directory`: Gjeldende arbeidskatalog. |
| - `worktree`: Git-arbeidstrebanen. |
| - `client`: En OpenCode SDK klient for samhandling med AI. |
| - `$`: Buns [shell API](https://bun.com/docs/runtime/shell) for å utføre kommandoer. |
|
|
| --- |
| |
| |
|
|
| For TypeScript-plugins kan du importere typer fra plugin-pakken: |
|
|
| ```ts title="my-plugin.ts" {1} |
| import type { Plugin } from "@opencode-ai/plugin" |
|
|
| export const MyPlugin: Plugin = async ({ project, client, $, directory, worktree }) => { |
| return { |
| // Type-safe hook implementations |
| } |
| } |
| ``` |
|
|
| --- |
| |
| |
|
|
| Plugins kan abonnere på hendelser som vist nedenfor i Eksempler-delen. Her er en liste over de forskjellige hendelsene som er tilgjengelige. |
|
|
| |
|
|
| - `command.executed` |
|
|
| |
|
|
| - `file.edited` |
| - `file.watcher.updated` |
|
|
| |
|
|
| - `installation.updated` |
|
|
| |
|
|
| - `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.updated` |
|
|
| |
|
|
| - `shell.env` |
|
|
| |
|
|
| - `tool.execute.after` |
| - `tool.execute.before` |
|
|
| |
|
|
| - `tui.prompt.append` |
| - `tui.command.execute` |
| - `tui.toast.show` |
|
|
| --- |
| |
| |
|
|
| Her er noen eksempler på plugins du kan bruke for å utvide OpenCode. |
|
|
| --- |
| |
| |
|
|
| Send varsler når visse hendelser inntreffer: |
|
|
| ```js title=".opencode/plugins/notification.js" |
| export const NotificationPlugin = async ({ project, client, $, directory, worktree }) => { |
| return { |
| event: async ({ event }) => { |
| // Send notification on session completion |
| if (event.type === "session.idle") { |
| await $`osascript -e 'display notification "Session completed!" with title "opencode"'` |
| } |
| }, |
| } |
| } |
| ``` |
|
|
| Vi bruker `osascript` for å kjøre AppleScript på macOS. Her bruker vi den til å sende varsler. |
|
|
| :::note |
| Hvis du bruker OpenCode-skrivebordsappen, kan den sende systemvarsler automatisk når et svar er klart eller når en økt feiler. |
| ::: |
|
|
| --- |
| |
| |
|
|
| Hindre OpenCode fra å lese `.env` filer: |
|
|
| ```javascript title=".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") |
| } |
| }, |
| } |
| } |
| ``` |
|
|
| --- |
| |
| |
|
|
| Injiser miljøvariabler i all shell-utførelse (AI-verktøy og brukerterminaler): |
|
|
| ```javascript title=".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 |
| }, |
| } |
| } |
| ``` |
|
|
| --- |
| |
| |
|
|
| Plugins kan også legge til egendefinerte verktøy til OpenCode: |
|
|
| ```ts title=".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`-hjelperen lager et tilpasset verktøy som OpenCode kan kalle. Den tar en Zod-skjemafunksjon og returnerer en verktøydefinisjon med: |
|
|
| - `description`: Hva verktøyet gjør |
| - `args`: Zod-skjema for verktøyets argumenter |
| - `execute`: Funksjon som kjører når verktøyet kalles |
|
|
| Dine egendefinerte verktøy vil være tilgjengelige for OpenCode sammen med innebygde verktøy. |
|
|
| :::note |
| Hvis et plugin-verktøy bruker samme navn som et innebygd verktøy, vil plugin-verktøyet ha forrang. |
| ::: |
|
|
| --- |
| |
| |
|
|
| Bruk `client.app.log()` i stedet for `console.log` for strukturert logging: |
|
|
| ```ts title=".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" }, |
| }, |
| }) |
| } |
| ``` |
|
|
| Nivåer: `debug`, `info`, `warn`, `error`. Se [SDK dokumentasjon](https://opencode.ai/docs/sdk) for detaljer. |
|
|
| --- |
| |
| |
|
|
| Tilpass konteksten inkludert når en økt komprimeres: |
|
|
| ```ts title=".opencode/plugins/compaction.ts" |
| import type { Plugin } from "@opencode-ai/plugin" |
|
|
| export const CompactionPlugin: Plugin = async (ctx) => { |
| return { |
| "experimental.session.compacting": async (input, output) => { |
| // Inject additional context into the compaction prompt |
| output.context.push(` |
| |
|
|
| Include any state that should persist across compaction: |
| - Current task status |
| - Important decisions made |
| - Files being actively worked on |
| `) |
| }, |
| } |
| } |
| ``` |
|
|
| `experimental.session.compacting`-kroken trigges før LLM genererer et fortsettelsessammendrag. Bruk den til å injisere domenespesifikk kontekst som standard komprimeringsprompt ville gå glipp av. |
|
|
| Du kan også erstatte komprimeringsprompten helt ved å stille inn `output.prompt`: |
|
|
| ```ts title=".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) => { |
| // Replace the entire compaction prompt |
| 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. |
| ` |
| }, |
| } |
| } |
| ``` |
|
|
| Når `output.prompt` er angitt, erstatter den standard komprimeringsprompt fullstendig. `output.context`-matrisen ignoreres i dette tilfellet. |
|
|