File size: 2,372 Bytes
6111b2b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | /**
* Plugin SDK β typed API for plugin developers.
*
* Provides `definePlugin()` factory and re-exports all types needed
* to build OmniRoute plugins.
*
* @module plugins/sdk
*/
import type {
Plugin,
PluginContext,
PluginResult,
BlockingHookResult,
} from "./hooks.ts";
export type { Plugin, PluginContext, PluginResult, BlockingHookResult };
// ββ Plugin Definition Helper ββ
export interface PluginDefinition {
/** Plugin name (kebab-case) */
name: string;
/** Priority (lower = runs first, default 100) */
priority?: number;
/** Start enabled? (default true) */
enabled?: boolean;
/** Hook: runs before chat handler. Can block or modify request. */
onRequest?: (ctx: PluginContext) => Promise<PluginResult | void> | PluginResult | void;
/** Hook: runs after chat handler. Can modify response. */
onResponse?: (ctx: PluginContext, response: unknown) => Promise<unknown | void> | unknown | void;
/** Hook: runs on handler error. Can recover or re-throw. */
onError?: (ctx: PluginContext, error: Error) => Promise<unknown | void> | unknown | void;
}
/**
* Define an OmniRoute plugin with type safety.
*
* @example
* ```ts
* import { definePlugin } from "omniroute/plugins/sdk";
*
* export default definePlugin({
* name: "my-plugin",
* priority: 50,
* onRequest: async (ctx) => {
* console.log(`Request ${ctx.requestId} for ${ctx.model}`);
* },
* onResponse: async (ctx, response) => {
* console.log(`Response for ${ctx.requestId}`);
* return response;
* },
* });
* ```
*/
export function definePlugin(def: PluginDefinition): Plugin {
return {
name: def.name,
priority: def.priority ?? 100,
enabled: def.enabled ?? true,
onRequest: def.onRequest,
onResponse: def.onResponse,
onError: def.onError,
};
}
// ββ Utility Helpers ββ
/**
* Block a request with a 403 response.
*/
export function blockRequest(response?: unknown): PluginResult {
return { blocked: true, response };
}
/**
* Modify the request body.
*/
export function modifyBody(body: unknown): PluginResult {
return { body };
}
/**
* Add metadata to the request context.
*/
export function addMetadata(metadata: Record<string, unknown>): PluginResult {
return { metadata };
}
|