GeezerVison is showing github.com — wip: plugins · ferrao/opencode@ca03127
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bun.lock +13Lines changed: 13 additions & 0 deletions Collapse file Copy file name to clipboardExpand all lines: opencode.json +1-11Lines changed: 1 addition & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | { | 2 | 2 | "$schema": "https://opencode.ai/config.json", | 3 | | - "provider": { | 4 | | - "cerebras": { | 5 | | - "options": { | 6 | | - "apiKey": "csk-m33xrvt43whkypn8r4ph9xc8fenhx2f68c3pj22ext45v5k9" | 7 | | - }, | 8 | | - "npm": "@ai-sdk/cerebras", | 9 | | - "models": { | 10 | | - "qwen-3-coder-480b": {} | 11 | | - } | 12 | | - } | 13 | | - }, | | 3 | + "plugin": ["./packages/plugin/src/example.ts"], | 14 | 4 | "mcp": { | 15 | 5 | "context7": { | 16 | 6 | "type": "remote", | |
Collapse file Copy file name to clipboardExpand all lines: packages/function/package.json +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | { | 2 | 2 | "name": "@opencode/function", | 3 | | - "version": "0.0.1", | | 3 | + "version": "0.0.0-202508022246", | 4 | 4 | "$schema": "https://json.schemastore.org/package.json", | 5 | 5 | "private": true, | 6 | 6 | "type": "module", | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/package.json +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | { | 2 | 2 | "$schema": "https://json.schemastore.org/package.json", | 3 | | - "version": "0.0.0", | | 3 | + "version": "0.0.0-202508022246", | 4 | 4 | "name": "opencode", | 5 | 5 | "type": "module", | 6 | 6 | "private": true, | | 36 | 36 | "@octokit/graphql": "9.0.1", | 37 | 37 | "@octokit/rest": "22.0.0", | 38 | 38 | "@openauthjs/openauth": "0.4.3", | | 39 | + "@opencode-ai/plugin": "workspace:*", | 39 | 40 | "@standard-schema/spec": "1.0.0", | 40 | 41 | "@zip.js/zip.js": "2.7.62", | 41 | 42 | "ai": "catalog:", | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/bootstrap.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,13 +2,15 @@ import { App } from "../app/app" | 2 | 2 | import { ConfigHooks } from "../config/hooks" | 3 | 3 | import { Format } from "../format" | 4 | 4 | import { LSP } from "../lsp" | | 5 | +import { Plugin } from "../plugin" | 5 | 6 | import { Share } from "../share/share" | 6 | 7 | import { Snapshot } from "../snapshot" | 7 | 8 |
| 8 | 9 | export async function bootstrap<T>(input: App.Input, cb: (app: App.Info) => Promise<T>) { | 9 | 10 | return App.provide(input, async (app) => { | 10 | 11 | Share.init() | 11 | 12 | Format.init() | | 13 | + Plugin.init() | 12 | 14 | ConfigHooks.init() | 13 | 15 | LSP.init() | 14 | 16 | Snapshot.init() | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/config/config.ts +28-16Lines changed: 28 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,21 +23,21 @@ export namespace Config { | 23 | 23 | for (const file of ["opencode.jsonc", "opencode.json"]) { | 24 | 24 | const found = await Filesystem.findUp(file, app.path.cwd, app.path.root) | 25 | 25 | for (const resolved of found.toReversed()) { | 26 | | - result = mergeDeep(result, await load(resolved)) | | 26 | + result = mergeDeep(result, await loadFile(resolved)) | 27 | 27 | } | 28 | 28 | } | 29 | 29 |
| 30 | 30 | // Override with custom config if provided | 31 | 31 | if (Flag.OPENCODE_CONFIG) { | 32 | | - result = mergeDeep(result, await load(Flag.OPENCODE_CONFIG)) | | 32 | + result = mergeDeep(result, await loadFile(Flag.OPENCODE_CONFIG)) | 33 | 33 | log.debug("loaded custom config", { path: Flag.OPENCODE_CONFIG }) | 34 | 34 | } | 35 | 35 |
| 36 | 36 | for (const [key, value] of Object.entries(auth)) { | 37 | 37 | if (value.type === "wellknown") { | 38 | 38 | process.env[value.key] = value.token | 39 | 39 | const wellknown = await fetch(`${key}/.well-known/opencode`).then((x) => x.json()) | 40 | | - result = mergeDeep(result, await loadRaw(JSON.stringify(wellknown.config ?? {}), process.cwd())) | | 40 | + result = mergeDeep(result, await load(JSON.stringify(wellknown.config ?? {}), process.cwd())) | 41 | 41 | } | 42 | 42 | } | 43 | 43 |
| @@ -223,6 +223,7 @@ export namespace Config { | 223 | 223 | $schema: z.string().optional().describe("JSON schema reference for configuration validation"), | 224 | 224 | theme: z.string().optional().describe("Theme name to use for the interface"), | 225 | 225 | keybinds: Keybinds.optional().describe("Custom keybind configurations"), | | 226 | + plugin: z.string().array().optional(), | 226 | 227 | share: z | 227 | 228 | .enum(["manual", "auto", "disabled"]) | 228 | 229 | .optional() | @@ -352,9 +353,9 @@ export namespace Config { | 352 | 353 | export const global = lazy(async () => { | 353 | 354 | let result: Info = pipe( | 354 | 355 | {}, | 355 | | - mergeDeep(await load(path.join(Global.Path.config, "config.json"))), | 356 | | - mergeDeep(await load(path.join(Global.Path.config, "opencode.json"))), | 357 | | - mergeDeep(await load(path.join(Global.Path.config, "opencode.jsonc"))), | | 356 | + mergeDeep(await loadFile(path.join(Global.Path.config, "config.json"))), | | 357 | + mergeDeep(await loadFile(path.join(Global.Path.config, "opencode.json"))), | | 358 | + mergeDeep(await loadFile(path.join(Global.Path.config, "opencode.jsonc"))), | 358 | 359 | ) | 359 | 360 |
| 360 | 361 | await import(path.join(Global.Path.config, "config"), { | @@ -375,25 +376,26 @@ export namespace Config { | 375 | 376 | return result | 376 | 377 | }) | 377 | 378 |
| 378 | | - async function load(configPath: string): Promise<Info> { | 379 | | - let text = await Bun.file(configPath) | | 379 | + async function loadFile(filepath: string): Promise<Info> { | | 380 | + log.info("loading", { path: filepath }) | | 381 | + let text = await Bun.file(filepath) | 380 | 382 | .text() | 381 | 383 | .catch((err) => { | 382 | 384 | if (err.code === "ENOENT") return | 383 | | - throw new JsonError({ path: configPath }, { cause: err }) | | 385 | + throw new JsonError({ path: filepath }, { cause: err }) | 384 | 386 | }) | 385 | 387 | if (!text) return {} | 386 | | - return loadRaw(text, configPath) | | 388 | + return load(text, filepath) | 387 | 389 | } | 388 | 390 |
| 389 | | - async function loadRaw(text: string, configPath: string) { | | 391 | + async function load(text: string, filepath: string) { | 390 | 392 | text = text.replace(/\{env:([^}]+)\}/g, (_, varName) => { | 391 | 393 | return process.env[varName] || "" | 392 | 394 | }) | 393 | 395 |
| 394 | 396 | const fileMatches = text.match(/\{file:[^}]+\}/g) | 395 | 397 | if (fileMatches) { | 396 | | - const configDir = path.dirname(configPath) | | 398 | + const configDir = path.dirname(filepath) | 397 | 399 | const lines = text.split("\n") | 398 | 400 |
| 399 | 401 | for (const match of fileMatches) { | @@ -428,7 +430,7 @@ export namespace Config { | 428 | 430 | .join("\n") | 429 | 431 |
| 430 | 432 | throw new JsonError({ | 431 | | - path: configPath, | | 433 | + path: filepath, | 432 | 434 | message: `\n--- JSONC Input ---\n${text}\n--- Errors ---\n${errorDetails}\n--- End ---`, | 433 | 435 | }) | 434 | 436 | } | @@ -437,11 +439,21 @@ export namespace Config { | 437 | 439 | if (parsed.success) { | 438 | 440 | if (!parsed.data.$schema) { | 439 | 441 | parsed.data.$schema = "https://opencode.ai/config.json" | 440 | | - await Bun.write(configPath, JSON.stringify(parsed.data, null, 2)) | | 442 | + await Bun.write(filepath, JSON.stringify(parsed.data, null, 2)) | 441 | 443 | } | 442 | | - return parsed.data | | 444 | + const data = parsed.data | | 445 | + if (data.plugin) { | | 446 | + for (let i = 0; i < data.plugin?.length; i++) { | | 447 | + const plugin = data.plugin[i] | | 448 | + if (typeof plugin === "string") { | | 449 | + data.plugin[i] = path.resolve(path.dirname(filepath), plugin) | | 450 | + } | | 451 | + } | | 452 | + } | | 453 | + return data | 443 | 454 | } | 444 | | - throw new InvalidError({ path: configPath, issues: parsed.error.issues }) | | 455 | + | | 456 | + throw new InvalidError({ path: filepath, issues: parsed.error.issues }) | 445 | 457 | } | 446 | 458 | export const JsonError = NamedError.create( | 447 | 459 | "ConfigJsonError", | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/permission/index.ts +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ import { z } from "zod" | 3 | 3 | import { Bus } from "../bus" | 4 | 4 | import { Log } from "../util/log" | 5 | 5 | import { Identifier } from "../id/id" | | 6 | +import { Plugin } from "../plugin" | 6 | 7 |
| 7 | 8 | export namespace Permission { | 8 | 9 | const log = Log.create({ service: "permission" }) | @@ -67,7 +68,7 @@ export namespace Permission { | 67 | 68 | }, | 68 | 69 | ) | 69 | 70 |
| 70 | | - export function ask(input: { | | 71 | + export async function ask(input: { | 71 | 72 | type: Info["type"] | 72 | 73 | title: Info["title"] | 73 | 74 | pattern?: Info["pattern"] | @@ -95,6 +96,18 @@ export namespace Permission { | 95 | 96 | created: Date.now(), | 96 | 97 | }, | 97 | 98 | } | | 99 | + | | 100 | + switch ( | | 101 | + await Plugin.trigger("permission.ask", info, { | | 102 | + status: "ask", | | 103 | + }).then((x) => x.status) | | 104 | + ) { | | 105 | + case "deny": | | 106 | + throw new RejectedError(info.sessionID, info.id, info.callID) | | 107 | + case "allow": | | 108 | + return | | 109 | + } | | 110 | + | 98 | 111 | pending[input.sessionID] = pending[input.sessionID] || {} | 99 | 112 | return new Promise<void>((resolve, reject) => { | 100 | 113 | pending[input.sessionID][info.id] = { | |
Collapse file Copy file name to clipboard +85Lines changed: 85 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import type { Hooks, Plugin as PluginInstance } from "@opencode-ai/plugin" | | 2 | +import { App } from "../app/app" | | 3 | +import { Config } from "../config/config" | | 4 | +import { Bus } from "../bus" | | 5 | +import { Log } from "../util/log" | | 6 | +import { createOpencodeClient } from "@opencode-ai/sdk" | | 7 | +import { Server } from "../server/server" | | 8 | +import { pathOr } from "remeda" | | 9 | + | | 10 | +export namespace Plugin { | | 11 | + const log = Log.create({ service: "plugin" }) | | 12 | + | | 13 | + const state = App.state("plugin", async (app) => { | | 14 | + const client = createOpencodeClient({ | | 15 | + baseUrl: "http://localhost:4096", | | 16 | + fetch: async (...args) => Server.app().fetch(...args), | | 17 | + }) | | 18 | + const config = await Config.get() | | 19 | + const hooks = [] | | 20 | + for (const plugin of config.plugin ?? []) { | | 21 | + log.info("loading plugin", { path: plugin }) | | 22 | + const mod = await import(plugin) | | 23 | + for (const [_name, fn] of Object.entries<PluginInstance>(mod)) { | | 24 | + const init = await fn({ | | 25 | + client, | | 26 | + app, | | 27 | + $: Bun.$, | | 28 | + }) | | 29 | + hooks.push(init) | | 30 | + } | | 31 | + } | | 32 | + | | 33 | + return { | | 34 | + hooks, | | 35 | + } | | 36 | + }) | | 37 | + | | 38 | + type Path<T, Prefix extends string = ""> = T extends object | | 39 | + ? { | | 40 | + [K in keyof T]: K extends string | | 41 | + ? T[K] extends Function | undefined | | 42 | + ? `${Prefix}${K}` | | 43 | + : Path<T[K], `${Prefix}${K}.`> | | 44 | + : never | | 45 | + }[keyof T] | | 46 | + : never | | 47 | + | | 48 | + export type FunctionFromKey<T, P extends Path<T>> = P extends `${infer K}.${infer R}` | | 49 | + ? K extends keyof T | | 50 | + ? R extends Path<T[K]> | | 51 | + ? FunctionFromKey<T[K], R> | | 52 | + : never | | 53 | + : never | | 54 | + : P extends keyof T | | 55 | + ? T[P] | | 56 | + : never | | 57 | + | | 58 | + export async function trigger< | | 59 | + Name extends Path<Required<Hooks>>, | | 60 | + Input = Parameters<FunctionFromKey<Required<Hooks>, Name>>[0], | | 61 | + Output = Parameters<FunctionFromKey<Required<Hooks>, Name>>[1], | | 62 | + >(fn: Name, input: Input, output: Output): Promise<Output> { | | 63 | + if (!fn) return output | | 64 | + const path = fn.split(".") | | 65 | + for (const hook of await state().then((x) => x.hooks)) { | | 66 | + // @ts-expect-error | | 67 | + const fn = pathOr(hook, path, undefined) | | 68 | + if (!fn) continue | | 69 | + // @ts-expect-error | | 70 | + await fn(input, output) | | 71 | + } | | 72 | + return output | | 73 | + } | | 74 | + | | 75 | + export function init() { | | 76 | + Bus.subscribeAll(async (input) => { | | 77 | + const hooks = await state().then((x) => x.hooks) | | 78 | + for (const hook of hooks) { | | 79 | + hook["event"]?.({ | | 80 | + event: input, | | 81 | + }) | | 82 | + } | | 83 | + }) | | 84 | + } | | 85 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/provider/provider.ts +18-14Lines changed: 18 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -97,7 +97,7 @@ export namespace Provider { | 97 | 97 | Array.isArray(msg.content) && msg.content.some((part: any) => part.type === "image_url"), | 98 | 98 | ) | 99 | 99 | } | 100 | | - } catch { } | | 100 | + } catch {} | 101 | 101 | const headers: Record<string, string> = { | 102 | 102 | ...init.headers, | 103 | 103 | ...copilot.HEADERS, | @@ -283,26 +283,26 @@ export namespace Provider { | 283 | 283 | cost: | 284 | 284 | !model.cost && !existing?.cost | 285 | 285 | ? { | 286 | | - input: 0, | 287 | | - output: 0, | 288 | | - cache_read: 0, | 289 | | - cache_write: 0, | 290 | | - } | | 286 | + input: 0, | | 287 | + output: 0, | | 288 | + cache_read: 0, | | 289 | + cache_write: 0, | | 290 | + } | 291 | 291 | : { | 292 | | - cache_read: 0, | 293 | | - cache_write: 0, | 294 | | - ...existing?.cost, | 295 | | - ...model.cost, | 296 | | - }, | | 292 | + cache_read: 0, | | 293 | + cache_write: 0, | | 294 | + ...existing?.cost, | | 295 | + ...model.cost, | | 296 | + }, | 297 | 297 | options: { | 298 | 298 | ...existing?.options, | 299 | 299 | ...model.options, | 300 | 300 | }, | 301 | 301 | limit: model.limit ?? | 302 | 302 | existing?.limit ?? { | 303 | | - context: 0, | 304 | | - output: 0, | 305 | | - }, | | 303 | + context: 0, | | 304 | + output: 0, | | 305 | + }, | 306 | 306 | } | 307 | 307 | parsed.models[modelID] = parsedModel | 308 | 308 | } | @@ -386,6 +386,10 @@ export namespace Provider { | 386 | 386 | }) | 387 | 387 | } | 388 | 388 |
| | 389 | + export async function getProvider(providerID: string) { | | 390 | + return state().then((s) => s.providers[providerID]) | | 391 | + } | | 392 | + | 389 | 393 | export async function getModel(providerID: string, modelID: string) { | 390 | 394 | const key = `${providerID}/${modelID}` | 391 | 395 | const s = await state() | |
|