chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* Request Logger Plugin — logs API requests and responses with timing.
|
||||
*
|
||||
* @module request-logger
|
||||
*/
|
||||
|
||||
const LEVELS = { debug: 0, info: 1, warn: 2, error: 3 };
|
||||
|
||||
function shouldLog(configLevel, messageLevel) {
|
||||
return (LEVELS[messageLevel] ?? 1) >= (LEVELS[configLevel] ?? 1);
|
||||
}
|
||||
|
||||
function truncate(str, max) {
|
||||
if (typeof str !== "string") return str;
|
||||
return str.length > max ? str.slice(0, max) + "..." : str;
|
||||
}
|
||||
|
||||
function formatBody(body, maxLen) {
|
||||
if (body === undefined || body === null) return undefined;
|
||||
try {
|
||||
const json = JSON.stringify(body);
|
||||
return truncate(json, maxLen);
|
||||
} catch {
|
||||
return truncate(String(body), maxLen);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* onRequest hook — records request start time and logs request details.
|
||||
*/
|
||||
export function onRequest(ctx) {
|
||||
const config = ctx?.config || {};
|
||||
const level = config.logLevel || "info";
|
||||
|
||||
if (ctx?.metadata) {
|
||||
ctx.metadata.__requestStart = Date.now();
|
||||
}
|
||||
|
||||
if (shouldLog(level, "info")) {
|
||||
const logEntry = {
|
||||
timestamp: new Date().toISOString(),
|
||||
requestId: ctx.requestId,
|
||||
model: ctx.model,
|
||||
provider: ctx.provider,
|
||||
method: ctx.method || "POST",
|
||||
};
|
||||
|
||||
if (config.includeBody) {
|
||||
logEntry.body = formatBody(ctx.body, config.maxBodyLength || 500);
|
||||
}
|
||||
|
||||
console.log("[request-logger] REQUEST:", JSON.stringify(logEntry));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* onResponse hook — logs response with timing.
|
||||
*/
|
||||
export function onResponse(ctx, response) {
|
||||
const config = ctx?.config || {};
|
||||
const level = config.logLevel || "info";
|
||||
const startTime = ctx?.metadata?.__requestStart || Date.now();
|
||||
const duration = Date.now() - startTime;
|
||||
|
||||
if (shouldLog(level, "info")) {
|
||||
const logEntry = {
|
||||
timestamp: new Date().toISOString(),
|
||||
requestId: ctx.requestId,
|
||||
model: ctx.model,
|
||||
durationMs: duration,
|
||||
status: response?.status || 200,
|
||||
};
|
||||
|
||||
if (config.includeBody && response?.data) {
|
||||
logEntry.response = formatBody(response.data, config.maxBodyLength || 500);
|
||||
}
|
||||
|
||||
console.log("[request-logger] RESPONSE:", JSON.stringify(logEntry));
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* onError hook — logs errors with context.
|
||||
*/
|
||||
export function onError(ctx, error) {
|
||||
const config = ctx?.config || {};
|
||||
const level = config.logLevel || "info";
|
||||
const startTime = ctx?.metadata?.__requestStart || Date.now();
|
||||
const duration = Date.now() - startTime;
|
||||
|
||||
if (shouldLog(level, "error")) {
|
||||
console.error("[request-logger] ERROR:", JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
requestId: ctx.requestId,
|
||||
model: ctx.model,
|
||||
durationMs: duration,
|
||||
error: error?.message || String(error),
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"name": "request-logger",
|
||||
"version": "1.0.0",
|
||||
"description": "Logs all API requests and responses with timing information",
|
||||
"author": "OmniRoute",
|
||||
"license": "MIT",
|
||||
"main": "index.mjs",
|
||||
"source": "local",
|
||||
"tags": ["logging", "debug", "monitoring"],
|
||||
"hooks": {
|
||||
"onRequest": true,
|
||||
"onResponse": true,
|
||||
"onError": true
|
||||
},
|
||||
"requires": {
|
||||
"permissions": []
|
||||
},
|
||||
"enabledByDefault": false,
|
||||
"configSchema": {
|
||||
"logLevel": {
|
||||
"type": "select",
|
||||
"default": "info",
|
||||
"enum": ["debug", "info", "warn", "error"],
|
||||
"description": "Minimum log level to output"
|
||||
},
|
||||
"includeBody": {
|
||||
"type": "boolean",
|
||||
"default": false,
|
||||
"description": "Include request/response body in logs"
|
||||
},
|
||||
"maxBodyLength": {
|
||||
"type": "number",
|
||||
"default": 500,
|
||||
"min": 100,
|
||||
"max": 10000,
|
||||
"description": "Maximum body length to log"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* Theme Manager Plugin — injects CSS custom properties into HTML responses.
|
||||
*
|
||||
* Reads the X-User-Theme header from requests, merges with plugin config,
|
||||
* and injects CSS variables into HTML responses via a <style> tag.
|
||||
*
|
||||
* @module theme-manager
|
||||
*/
|
||||
|
||||
const DEFAULT_THEMES = {
|
||||
light: {
|
||||
"--bg-primary": "#ffffff",
|
||||
"--bg-secondary": "#f5f5f5",
|
||||
"--text-primary": "#1a1a1a",
|
||||
"--text-secondary": "#666666",
|
||||
"--border-color": "#e0e0e0",
|
||||
"--shadow": "0 1px 3px rgba(0,0,0,0.1)",
|
||||
},
|
||||
dark: {
|
||||
"--bg-primary": "#1a1a1a",
|
||||
"--bg-secondary": "#2d2d2d",
|
||||
"--text-primary": "#f5f5f5",
|
||||
"--text-secondary": "#a0a0a0",
|
||||
"--border-color": "#404040",
|
||||
"--shadow": "0 1px 3px rgba(0,0,0,0.4)",
|
||||
},
|
||||
};
|
||||
|
||||
function resolveThemeMode(darkMode, header) {
|
||||
if (header === "light" || header === "dark") return header;
|
||||
if (darkMode === "light" || darkMode === "dark") return darkMode;
|
||||
return "light";
|
||||
}
|
||||
|
||||
function buildCssVariables(mode, config) {
|
||||
const base = DEFAULT_THEMES[mode] || DEFAULT_THEMES.light;
|
||||
const vars = { ...base };
|
||||
if (config.primaryColor) vars["--color-primary"] = config.primaryColor;
|
||||
if (config.borderRadius) vars["--border-radius"] = config.borderRadius;
|
||||
if (config.fontFamily) vars["--font-family"] = config.fontFamily;
|
||||
return vars;
|
||||
}
|
||||
|
||||
function varsToCss(vars) {
|
||||
return Object.entries(vars)
|
||||
.map(([k, v]) => ` ${k}: ${v};`)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* onRequest hook — reads X-User-Theme header and stores resolved mode in metadata.
|
||||
*/
|
||||
export function onRequest(ctx) {
|
||||
const config = ctx?.config || {};
|
||||
const header = ctx?.headers?.["x-user-theme"] || ctx?.headers?.["X-User-Theme"];
|
||||
const mode = resolveThemeMode(config.darkMode, header);
|
||||
if (ctx?.metadata) {
|
||||
ctx.metadata.__themeMode = mode;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* onResponse hook — injects <style> tag with CSS variables into HTML responses.
|
||||
*/
|
||||
export function onResponse(ctx, response) {
|
||||
const config = ctx?.config || {};
|
||||
const mode = ctx?.metadata?.__themeMode || "light";
|
||||
|
||||
const isHtml =
|
||||
response?.headers?.["content-type"]?.includes("text/html") ||
|
||||
typeof response?.body === "string" &&
|
||||
(response.body.includes("<!DOCTYPE html>") || response.body.includes("<html"));
|
||||
|
||||
if (!isHtml || typeof response?.body !== "string") return response;
|
||||
|
||||
const vars = buildCssVariables(mode, config);
|
||||
const styleTag = `<style id="omniroute-theme">\n:root {\n${varsToCss(vars)}\n}\n</style>`;
|
||||
|
||||
const body = response.body.replace(/<head([^>]*)>/i, `<head$1>${styleTag}`);
|
||||
|
||||
return { ...response, body };
|
||||
}
|
||||
|
||||
/**
|
||||
* onInstall lifecycle hook — log installation.
|
||||
*/
|
||||
export function onInstall(ctx) {
|
||||
console.log(`[theme-manager] Installed v${ctx?.version || "unknown"}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* onActivate lifecycle hook — log activation.
|
||||
*/
|
||||
export function onActivate(ctx) {
|
||||
console.log(`[theme-manager] Activated with config:`, ctx?.config || {});
|
||||
}
|
||||
|
||||
/**
|
||||
* onDeactivate lifecycle hook — log deactivation.
|
||||
*/
|
||||
export function onDeactivate(ctx) {
|
||||
console.log(`[theme-manager] Deactivated`);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "theme-manager",
|
||||
"version": "1.0.0",
|
||||
"description": "Dynamic UI theme management via CSS variable injection",
|
||||
"author": "OmniRoute",
|
||||
"license": "MIT",
|
||||
"main": "index.mjs",
|
||||
"source": "local",
|
||||
"tags": ["theme", "ui", "css", "customization"],
|
||||
"hooks": {
|
||||
"onRequest": true,
|
||||
"onResponse": true,
|
||||
"onError": false,
|
||||
"onInstall": true,
|
||||
"onActivate": true,
|
||||
"onDeactivate": true,
|
||||
"onUninstall": false
|
||||
},
|
||||
"requires": {
|
||||
"permissions": []
|
||||
},
|
||||
"enabledByDefault": true,
|
||||
"configSchema": {
|
||||
"primaryColor": {
|
||||
"type": "string",
|
||||
"default": "#6C5CE7",
|
||||
"description": "Primary accent color (CSS hex value)"
|
||||
},
|
||||
"darkMode": {
|
||||
"type": "select",
|
||||
"default": "auto",
|
||||
"enum": ["light", "dark", "auto"],
|
||||
"description": "Theme mode preference"
|
||||
},
|
||||
"borderRadius": {
|
||||
"type": "string",
|
||||
"default": "8px",
|
||||
"description": "Default border radius for UI elements"
|
||||
},
|
||||
"fontFamily": {
|
||||
"type": "string",
|
||||
"default": "Inter, system-ui, sans-serif",
|
||||
"description": "Default font family"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Welcome Banner Plugin — PoC demonstrating the OmniRoute plugin system.
|
||||
*
|
||||
* Adds a banner message to request metadata on every request.
|
||||
* Logs a delivery confirmation on every response.
|
||||
*
|
||||
* @module welcome-banner
|
||||
*/
|
||||
|
||||
/**
|
||||
* onRequest hook — injects banner text into request metadata.
|
||||
*
|
||||
* @param {object} ctx - Plugin context
|
||||
* @param {object} [ctx.config] - Plugin configuration
|
||||
* @param {object} [ctx.metadata] - Request metadata (mutable)
|
||||
*/
|
||||
export function onRequest(ctx) {
|
||||
const config = ctx?.config || {};
|
||||
const enabled = config.enabled !== false; // default true
|
||||
if (!enabled) return;
|
||||
|
||||
const bannerText = config.bannerText || "Welcome to OmniRoute!";
|
||||
if (ctx.metadata) {
|
||||
ctx.metadata.banner = bannerText;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* onResponse hook — fire-and-forget banner delivery log.
|
||||
*
|
||||
* @param {object} ctx - Plugin context
|
||||
* @param {object} response - Upstream response
|
||||
*/
|
||||
export function onResponse() {
|
||||
// No-op — banner is request-side only
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "welcome-banner",
|
||||
"version": "1.0.0",
|
||||
"description": "Adds a welcome banner to API responses",
|
||||
"author": "OmniRoute",
|
||||
"license": "MIT",
|
||||
"main": "index.mjs",
|
||||
"source": "local",
|
||||
"tags": ["demo", "banner"],
|
||||
"hooks": {
|
||||
"onRequest": true,
|
||||
"onResponse": true,
|
||||
"onError": false
|
||||
},
|
||||
"permissions": [],
|
||||
"enabledByDefault": true,
|
||||
"configSchema": {
|
||||
"bannerText": {
|
||||
"type": "string",
|
||||
"default": "Welcome to OmniRoute!",
|
||||
"description": "Banner message to display"
|
||||
},
|
||||
"enabled": {
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"description": "Enable or disable the banner"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user