mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-04 22:32:12 +03:00
Integrates two community contributions into release/v3.8.8 with security hardening and conflict resolution. - **Plugins framework** (#2913 — thanks @oyi77): hooks + registry unification, plugin SDK (`definePlugin`), worker-thread sandbox, per-plugin hook rate limiting, SHA-256 integrity verification, semver-gated upgrade, and execution analytics. Plugin routes are loopback-only (`isLocalOnlyPath`); `child_process` exec is opt-in via `OMNIROUTE_PLUGINS_ALLOW_EXEC` (default off). - **API key option: disable non-published models** (#3017 — thanks @androw): a per-key flag restricting the key to discovered public models (combos / `auto/*` / `qtSd/*` routing still allowed). Hardening applied during integration: migration renumber (089/090/091), `/api/plugins` LOCAL_ONLY route-guard classification (closes the plugin-RCE vector), atomic install/upgrade with path containment, `O_EXCL` tmp-file creation (TOCTOU), rate-limit-map eviction, `validatePluginConfig` on configure, `buildErrorBody` on all plugin error paths. 246/246 tests; typecheck / cycles / docs-sync clean. Co-authored-by: oyi77 <14921983+oyi77@users.noreply.github.com> Co-authored-by: Nicolas Lorin <androw95220@gmail.com>
89 lines
2.2 KiB
TypeScript
89 lines
2.2 KiB
TypeScript
/**
|
|
* 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 };
|
|
}
|