mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-26 09:52:11 +03:00
OmniRoute is an intelligent API gateway that unifies 20+ AI providers behind a single OpenAI-compatible endpoint. Features include intelligent routing with 6 strategies, multi-format translation (OpenAI/Claude/Gemini/Responses API), circuit breakers, semantic caching, combo fallback chains, real-time health monitoring, and a full dashboard with provider management, analytics, and CLI tool integration. Key highlights: - 20+ providers (Claude Code, Codex, Gemini CLI, GitHub Copilot, iFlow, Qwen, Kiro, etc.) - 6 routing strategies (Fill First, Round Robin, P2C, Random, Least Used, Cost Optimized) - Export/Import database backup with full archive support - Translator Playground with 4 modes (Playground, Chat Tester, Test Bench, Live Monitor) - 100% TypeScript across src/ and open-sse/ - Docker support with multi-stage builds - Comprehensive documentation and 9 dashboard screenshots
70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
/**
|
|
* Moderation Handler
|
|
*
|
|
* Handles POST /v1/moderations (OpenAI Moderations API format).
|
|
*/
|
|
|
|
import { getModerationProvider, parseModerationModel } from "../config/moderationRegistry.ts";
|
|
import { errorResponse } from "../utils/error.ts";
|
|
|
|
/**
|
|
* Handle moderation request
|
|
*
|
|
* @param {Object} options
|
|
* @param {Object} options.body - JSON body { model, input }
|
|
* @param {Object} options.credentials - Provider credentials { apiKey }
|
|
* @returns {Response}
|
|
*/
|
|
/** @returns {Promise<any>} */
|
|
export async function handleModeration({ body, credentials }) {
|
|
if (!body.input) {
|
|
return errorResponse(400, "input is required");
|
|
}
|
|
|
|
// Default to latest moderation model
|
|
const model = body.model || "omni-moderation-latest";
|
|
const { provider: providerId, model: modelId } = parseModerationModel(model);
|
|
const providerConfig = providerId ? getModerationProvider(providerId) : null;
|
|
|
|
if (!providerConfig) {
|
|
return errorResponse(
|
|
400,
|
|
`No moderation provider found for model "${model}". Available: openai`
|
|
);
|
|
}
|
|
|
|
const token = credentials?.apiKey || credentials?.accessToken;
|
|
if (!token) {
|
|
return errorResponse(401, `No credentials for moderation provider: ${providerId}`);
|
|
}
|
|
|
|
try {
|
|
const res = await fetch(providerConfig.baseUrl, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
body: JSON.stringify({
|
|
model: modelId,
|
|
input: body.input,
|
|
}),
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const errText = await res.text();
|
|
return new Response(errText, {
|
|
status: res.status,
|
|
headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" },
|
|
});
|
|
}
|
|
|
|
const data = await res.json();
|
|
return Response.json(data, {
|
|
headers: { "Access-Control-Allow-Origin": "*" },
|
|
});
|
|
} catch (err) {
|
|
return errorResponse(500, `Moderation request failed: ${err.message}`);
|
|
}
|
|
}
|