From ca031278ca9ca30277620e344f7a95c597a8a0de Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Sat, 2 Aug 2025 18:50:19 -0400 Subject: wip: plugins --- packages/plugin/.gitignore | 1 + packages/plugin/package.json | 21 +++++++++++++++ packages/plugin/script/publish.ts | 18 +++++++++++++ packages/plugin/src/example.ts | 7 +++++ packages/plugin/src/index.ts | 56 +++++++++++++++++++++++++++++++++++++++ packages/plugin/tsconfig.json | 16 +++++++++++ 6 files changed, 119 insertions(+) create mode 100644 packages/plugin/.gitignore create mode 100644 packages/plugin/package.json create mode 100644 packages/plugin/script/publish.ts create mode 100644 packages/plugin/src/example.ts create mode 100644 packages/plugin/src/index.ts create mode 100644 packages/plugin/tsconfig.json (limited to 'packages/plugin') diff --git a/packages/plugin/.gitignore b/packages/plugin/.gitignore new file mode 100644 index 000000000..1521c8b76 --- /dev/null +++ b/packages/plugin/.gitignore @@ -0,0 +1 @@ +dist diff --git a/packages/plugin/package.json b/packages/plugin/package.json new file mode 100644 index 000000000..b7e75af1c --- /dev/null +++ b/packages/plugin/package.json @@ -0,0 +1,21 @@ +{ + "$schema": "https://json.schemastore.org/package.json", + "name": "@opencode-ai/plugin", + "version": "0.0.0", + "type": "module", + "scripts": { + "typecheck": "tsc --noEmit" + }, + "exports": { + ".": "./src/index.ts" + }, + "files": [ + "dist" + ], + "devDependencies": { + "typescript": "catalog:", + "@hey-api/openapi-ts": "0.80.1", + "@tsconfig/node22": "catalog:", + "@opencode-ai/sdk": "workspace:*" + } +} diff --git a/packages/plugin/script/publish.ts b/packages/plugin/script/publish.ts new file mode 100644 index 000000000..b984fd4f4 --- /dev/null +++ b/packages/plugin/script/publish.ts @@ -0,0 +1,18 @@ +#!/usr/bin/env bun + +const dir = new URL("..", import.meta.url).pathname +process.chdir(dir) + +import { $ } from "bun" + +const snapshot = process.env["OPENCODE_SNAPSHOT"] === "true" + +await $`bun tsc` + +if (snapshot) { + await $`bun publish --tag snapshot --access public` + await $`git checkout package.json` +} +if (!snapshot) { + await $`bun publish --access public` +} diff --git a/packages/plugin/src/example.ts b/packages/plugin/src/example.ts new file mode 100644 index 000000000..ac8c0e344 --- /dev/null +++ b/packages/plugin/src/example.ts @@ -0,0 +1,7 @@ +import { Plugin } from "./index" + +export const ExamplePlugin: Plugin = async ({ app, client }) => { + return { + permission: {}, + } +} diff --git a/packages/plugin/src/index.ts b/packages/plugin/src/index.ts new file mode 100644 index 000000000..9040fb517 --- /dev/null +++ b/packages/plugin/src/index.ts @@ -0,0 +1,56 @@ +import type { Event, createOpencodeClient, App, Model, Provider, Permission, UserMessage, Part } from "@opencode-ai/sdk" +import { $ } from "bun" + +export type PluginInput = { + client: ReturnType + app: App + $: $ +} +export type Plugin = (input: PluginInput) => Promise + +export interface Hooks { + event?: (input: { event: Event }) => Promise + chat?: { + /** + * Called when a new message is received + */ + message?: (input: {}, output: { message: UserMessage; parts: Part[] }) => Promise + /** + * Modify parameters sent to LLM + */ + params?: ( + input: { model: Model; provider: Provider; message: UserMessage }, + output: { temperature: number; topP: number }, + ) => Promise + } + permission?: { + /** + * Called when a permission is asked + */ + ask?: (input: Permission, output: { status: "ask" | "deny" | "allow" }) => Promise + } + tool?: { + execute?: { + /** + * Called before a tool is executed + */ + before?: ( + input: { tool: string; sessionID: string; callID: string }, + output: { + args: any + }, + ) => Promise + /** + * Called after a tool is executed + */ + after?: ( + input: { tool: string; sessionID: string; callID: string }, + output: { + title: string + output: string + metadata: any + }, + ) => Promise + } + } +} diff --git a/packages/plugin/tsconfig.json b/packages/plugin/tsconfig.json new file mode 100644 index 000000000..34d4a8718 --- /dev/null +++ b/packages/plugin/tsconfig.json @@ -0,0 +1,16 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig.json", + "extends": "@tsconfig/node22/tsconfig.json", + "compilerOptions": { + "outDir": "dist", + "module": "preserve", + "declaration": true, + "moduleResolution": "bundler", + "customConditions": [ + "development" + ] + }, + "include": [ + "src" + ] +} -- cgit v1.2.3