mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-20 06:02:14 +03:00
Extract 3 high-value CPU/RAM optimizations from perf branch: 1. estimateSizeFast() — fast object-tree size estimator replacing JSON.stringify().length in isSmallEnoughForSemanticCache(). Walks object tree with a stack, zero string allocation, early exit at 256KB. 2. Consolidate settings reads — move getCachedSettings() to a single early read in handleChatCore(), eliminating a redundant second read 200 lines later. Also removes the isDetailedLoggingEnabled() wrapper call (reads settings internally) in favor of direct field check. 3. Registry Proxy→direct export — convert 8 registries from lazy Proxy+getOrCreate pattern to simple exported const objects. Eliminates Proxy trap overhead on every provider property access during routing. Affected: audio, embedding, image, moderation, music, rerank, search, video registries (-451 lines of Proxy boilerplate). These changes are independent of the CPU leak fix (limiter eviction) and complement it by reducing per-request CPU overhead.
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
/**
|
|
* Moderation Provider Registry
|
|
*
|
|
* Defines providers that support the /v1/moderations endpoint.
|
|
* Follows OpenAI's moderation API format.
|
|
*/
|
|
|
|
export const MODERATION_PROVIDERS = {
|
|
openai: {
|
|
id: "openai",
|
|
baseUrl: "https://api.openai.com/v1/moderations",
|
|
authType: "apikey",
|
|
authHeader: "bearer",
|
|
models: [
|
|
{ id: "omni-moderation-latest", name: "Omni Moderation Latest" },
|
|
{ id: "text-moderation-latest", name: "Text Moderation Latest" },
|
|
],
|
|
},
|
|
};
|
|
|
|
/**
|
|
* Get moderation provider config by ID
|
|
*/
|
|
export function getModerationProvider(providerId) {
|
|
return MODERATION_PROVIDERS[providerId] || null;
|
|
}
|
|
|
|
/**
|
|
* Parse moderation model string
|
|
*/
|
|
export function parseModerationModel(modelStr) {
|
|
if (!modelStr) return { provider: null, model: null };
|
|
|
|
for (const [providerId, config] of Object.entries(MODERATION_PROVIDERS)) {
|
|
if (modelStr.startsWith(providerId + "/")) {
|
|
return { provider: providerId, model: modelStr.slice(providerId.length + 1) };
|
|
}
|
|
}
|
|
|
|
for (const [providerId, config] of Object.entries(MODERATION_PROVIDERS)) {
|
|
if (config.models.some((m) => m.id === modelStr)) {
|
|
return { provider: providerId, model: modelStr };
|
|
}
|
|
}
|
|
|
|
return { provider: null, model: modelStr };
|
|
}
|
|
|
|
/**
|
|
* Get all moderation models as a flat list
|
|
*/
|
|
export function getAllModerationModels() {
|
|
const models = [];
|
|
for (const [providerId, config] of Object.entries(MODERATION_PROVIDERS)) {
|
|
for (const model of config.models) {
|
|
models.push({
|
|
id: `${providerId}/${model.id}`,
|
|
name: model.name,
|
|
provider: providerId,
|
|
});
|
|
}
|
|
}
|
|
return models;
|
|
}
|