mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-02 21:32:10 +03:00
* chore(release): open v3.8.18 development cycle * fix(catalog): stop Codex CLI model-catalog refresh from erroring (#3481) Codex's model-catalog refresh (codex_models_manager) does GET /v1/models?client_version=<v> and decodes a JSON object with a TOP-LEVEL `models` array. OmniRoute answers in the OpenAI-standard `{object,data}` shape, so codex fails with "missing field `models`" and logs "failed to refresh available models" on every startup. Detect codex clients via the `originator` / `user-agent` = `codex_*` headers they send and add an EMPTY top-level `models: []` so the decode succeeds. Non-codex OpenAI clients keep the byte-identical `{object,data}` response. The array is intentionally empty: codex replaces its built-in per-model agent prompt (`base_instructions`, ~21k chars) with whatever a populated entry carries for the selected model, so emitting our catalog would drop the agent prompt to nothing and break codex's agent behaviour (verified empirically against codex 0.137). An empty list keeps codex on its built-in model info — same inference as before, minus the error. Validated end-to-end with the real handler against codex 0.137: "failed to refresh available models" → 0 occurrences, instructions preserved (built-in Codex agent prompt, not empty). Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * chore: ignore quality reports and local prompt artifacts Add generated quality gate reports, metrics files, and local setup prompt artifacts to .gitignore to prevent committing environment-specific or temporary files. * fix(provider): detect Responses API format when body has `input` but … (#3490) Integrated into release/v3.8.18 * fix(sse): normalize numeric provider ids to strings (#3451) Integrated into release/v3.8.18 * feat(browserPool): resolve Playwright proxy from proxy_registry DB (#3492) Integrated into release/v3.8.18 * fix(theoldllm): generate X-Request-Token server-side, drop Playwright (#3491) Integrated into release/v3.8.18 * feat(plugins): add lifecycle hooks and theme-manager plugin (#3473) Integrated into release/v3.8.18 * fix(combo): parallel pre-screen + circuit-breaker fast-exit for priority combos (#3169) Integrated into release/v3.8.18 * feat(ui): unifi active and finished requests into single view #1422 (#3401) Integrated into release/v3.8.18 * docs(changelog): record #3401, #3473, #3492, #3490, #3451, #3491, #3169 under v3.8.18 * feat(docs): add doc accuracy gate + refresh AGENTS.md counts (#3510) Integrated into release/v3.8.18 * fix(sse): drop empty-choices chunks without usage instead of injecting retry text (#3513) PR #3422 ('allow OpenAI usage-only empty choices chunks') reintroduced the assistant-content injection '[OmniRoute] Upstream returned an empty response. Please retry.' for empty `choices: []` chunks that carry no valid usage. Clients (Goose/opencode) feed that text back as a turn and spin in a retry loop -- the exact regression #3400 had fixed by dropping the chunk. Restore the drop behavior for the no-usage case while preserving #3422's standards-compliant forwarding of usage-only `include_usage` final chunks. Realign the mislabeled stream-utils test (it asserted the injection) and add a dedicated regression guard. Reported-by: @mochizzan Refs: #3502, #3388, #3400, #3422 * fix(authz): fall back to URL token when Authorization isn't a usable Bearer (#3504) Integrated into release/v3.8.18 * fix(playground): authenticate via session, test key policy by id (#3503) Integrated into release/v3.8.18 * docs(changelog): record #3510, #3504, #3503 under v3.8.18 * fix: llama base url normalization (#3519) * docs(changelog): reconcile v3.8.18 — add #3519, #3513, #3435-repair, gitignore chore (full commit↔changelog coverage) * fix(opencode-plugin): bound regex quantifiers in normaliseFreeLabel (polynomial-ReDoS) CodeQL js/polynomial-redos: unbounded \s* before an anchored \s*$ allowed O(n²) backtracking on attacker-influenced display names. Bounded to {0,8}/{1,8} (ample for any real label spacing). Plugin builds + 254 tests green. * fix(types): restore clean typecheck:core for v3.8.18 release gate - getPendingRequests() typed to real shape (was widened to object) → fixes unknown 'count' in the unified-requests view (#3401) - streamChunks log payload cast to its declared type (callLogs.ts) - preScreenTargets aligned to canonical IsModelAvailable signature (#3169), Promise.resolve-normalized so .catch never hits a bare boolean All 5 gates green: lint(0 err) + typecheck:core + cycles + docs-all + unit + vitest(146). --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Andrey Borodulin <borodulin@gmail.com> Co-authored-by: Dmitrii Safronov <zimniy@cyberbrain.cc> Co-authored-by: Paijo <14921983+oyi77@users.noreply.github.com> Co-authored-by: PizzaV <103120356+pizzav-xyz@users.noreply.github.com> Co-authored-by: Markus Hartung <mail@hartmark.se> Co-authored-by: Felipe Almeman <4226997+zhiru@users.noreply.github.com>
202 lines
6.3 KiB
TypeScript
202 lines
6.3 KiB
TypeScript
export type ErrorInfo = {
|
|
type: string;
|
|
code: string;
|
|
};
|
|
|
|
export type ConfiguredErrorReason =
|
|
| "auth_error"
|
|
| "quota_exhausted"
|
|
| "rate_limit_exceeded"
|
|
| "model_capacity"
|
|
| "server_error"
|
|
| "unknown";
|
|
|
|
export type ErrorRule = {
|
|
id: string;
|
|
text?: string;
|
|
status?: number;
|
|
reason?: ConfiguredErrorReason;
|
|
cooldownMs?: number;
|
|
backoff?: boolean;
|
|
};
|
|
|
|
// OpenAI-compatible error types mapping (client-facing)
|
|
export const ERROR_TYPES: Record<number, ErrorInfo> = {
|
|
400: { type: "invalid_request_error", code: "bad_request" },
|
|
401: { type: "authentication_error", code: "invalid_api_key" },
|
|
402: { type: "billing_error", code: "payment_required" },
|
|
403: { type: "permission_error", code: "insufficient_quota" },
|
|
404: { type: "invalid_request_error", code: "model_not_found" },
|
|
406: { type: "invalid_request_error", code: "model_not_supported" },
|
|
429: { type: "rate_limit_error", code: "rate_limit_exceeded" },
|
|
500: { type: "server_error", code: "internal_server_error" },
|
|
502: { type: "server_error", code: "bad_gateway" },
|
|
503: { type: "server_error", code: "service_unavailable" },
|
|
504: { type: "server_error", code: "gateway_timeout" },
|
|
};
|
|
|
|
// Default error messages per status code (client-facing)
|
|
export const DEFAULT_ERROR_MESSAGES: Record<number, string> = {
|
|
400: "Bad request",
|
|
401: "Invalid API key provided",
|
|
402: "Payment required",
|
|
403: "You exceeded your current quota",
|
|
404: "Model not found",
|
|
406: "Model not supported",
|
|
429: "Rate limit exceeded",
|
|
500: "Internal server error",
|
|
502: "Bad gateway - upstream provider error",
|
|
503: "Service temporarily unavailable",
|
|
504: "Gateway timeout",
|
|
};
|
|
|
|
// Exponential backoff config for rate limits.
|
|
// Preserve OmniRoute's existing 2-minute cap to avoid changing runtime behavior.
|
|
export const BACKOFF_CONFIG = {
|
|
base: 1000,
|
|
max: 2 * 60 * 1000,
|
|
maxLevel: 15,
|
|
};
|
|
|
|
export const TRANSIENT_COOLDOWN_MS = 5 * 1000;
|
|
|
|
// Cooldown durations (ms)
|
|
export const COOLDOWN_MS = {
|
|
unauthorized: 2 * 60 * 1000,
|
|
paymentRequired: 2 * 60 * 1000,
|
|
notFound: 2 * 60 * 1000,
|
|
notFoundLocal: 5 * 1000,
|
|
transientInitial: TRANSIENT_COOLDOWN_MS,
|
|
transientMax: 60 * 1000,
|
|
transient: TRANSIENT_COOLDOWN_MS,
|
|
requestNotAllowed: 5 * 1000,
|
|
rateLimit: 2 * 60 * 1000,
|
|
serviceUnavailable: 2 * 1000,
|
|
authExpired: 2 * 60 * 1000,
|
|
};
|
|
|
|
/**
|
|
* Shared rules for account fallback classification.
|
|
* Checked top-to-bottom: text rules first, then status rules.
|
|
*/
|
|
export const ERROR_RULES: ErrorRule[] = [
|
|
{
|
|
id: "no_credentials",
|
|
text: "no credentials",
|
|
cooldownMs: COOLDOWN_MS.notFound,
|
|
reason: "auth_error",
|
|
},
|
|
{
|
|
id: "request_not_allowed",
|
|
text: "request not allowed",
|
|
cooldownMs: COOLDOWN_MS.requestNotAllowed,
|
|
reason: "rate_limit_exceeded",
|
|
},
|
|
{
|
|
id: "improperly_formed_request",
|
|
text: "improperly formed request",
|
|
cooldownMs: 0,
|
|
reason: "model_capacity",
|
|
},
|
|
{ id: "rate_limit", text: "rate limit", backoff: true, reason: "rate_limit_exceeded" },
|
|
{
|
|
id: "too_many_requests",
|
|
text: "too many requests",
|
|
backoff: true,
|
|
reason: "rate_limit_exceeded",
|
|
},
|
|
{
|
|
id: "hour_quota_exceeded",
|
|
text: "hour quota",
|
|
backoff: true,
|
|
reason: "quota_exhausted",
|
|
},
|
|
{
|
|
id: "quota_has_been_exceeded",
|
|
text: "quota has been exceeded",
|
|
backoff: true,
|
|
reason: "quota_exhausted",
|
|
},
|
|
{
|
|
id: "quota_exceeded",
|
|
text: "quota exceeded",
|
|
backoff: true,
|
|
reason: "quota_exhausted",
|
|
},
|
|
{
|
|
id: "quota_will_reset",
|
|
text: "quota will reset",
|
|
backoff: true,
|
|
reason: "quota_exhausted",
|
|
},
|
|
{
|
|
id: "capacity_exhausted",
|
|
text: "exhausted your capacity",
|
|
backoff: true,
|
|
reason: "quota_exhausted",
|
|
},
|
|
{
|
|
id: "quota_exhausted",
|
|
text: "quota exhausted",
|
|
backoff: true,
|
|
reason: "quota_exhausted",
|
|
},
|
|
{
|
|
id: "free_tier_exhausted",
|
|
text: "free tier of the model has been exhausted",
|
|
backoff: true,
|
|
reason: "quota_exhausted",
|
|
},
|
|
{ id: "capacity", text: "capacity", backoff: true, reason: "model_capacity" },
|
|
{ id: "overloaded", text: "overloaded", backoff: true, reason: "model_capacity" },
|
|
{ id: "high_demand", text: "high demand", backoff: true, reason: "model_capacity" },
|
|
{ id: "status_401", status: 401, cooldownMs: 0, reason: "auth_error" },
|
|
{ id: "status_402", status: 402, cooldownMs: 0, reason: "quota_exhausted" },
|
|
{ id: "status_403", status: 403, cooldownMs: 0, reason: "quota_exhausted" },
|
|
{ id: "status_404", status: 404, cooldownMs: COOLDOWN_MS.notFound, reason: "unknown" },
|
|
{ id: "status_406", status: 406, backoff: true, reason: "server_error" },
|
|
{ id: "status_408", status: 408, backoff: true, reason: "server_error" },
|
|
{ id: "status_429", status: 429, backoff: true, reason: "rate_limit_exceeded" },
|
|
{ id: "status_500", status: 500, backoff: true, reason: "server_error" },
|
|
{ id: "status_502", status: 502, backoff: true, reason: "server_error" },
|
|
{ id: "status_503", status: 503, backoff: true, reason: "server_error" },
|
|
{ id: "status_504", status: 504, backoff: true, reason: "server_error" },
|
|
];
|
|
|
|
function normalizeErrorMessage(message: unknown): string {
|
|
return String(message || "").toLowerCase();
|
|
}
|
|
|
|
export function getErrorInfo(statusCode: number): ErrorInfo {
|
|
return (
|
|
ERROR_TYPES[statusCode] ||
|
|
(statusCode >= 500
|
|
? { type: "server_error", code: "internal_server_error" }
|
|
: { type: "invalid_request_error", code: "" })
|
|
);
|
|
}
|
|
|
|
export function getDefaultErrorMessage(statusCode: number): string {
|
|
return DEFAULT_ERROR_MESSAGES[statusCode] || "An error occurred";
|
|
}
|
|
|
|
export function calculateBackoffCooldown(level = 0): number {
|
|
const safeLevel = Math.max(0, Math.floor(level));
|
|
const cooldown = BACKOFF_CONFIG.base * Math.pow(2, safeLevel);
|
|
return Math.min(cooldown, BACKOFF_CONFIG.max);
|
|
}
|
|
|
|
export function matchErrorRuleByText(message: unknown): ErrorRule | null {
|
|
const lower = normalizeErrorMessage(message);
|
|
if (!lower) return null;
|
|
return ERROR_RULES.find((rule) => rule.text && lower.includes(rule.text)) || null;
|
|
}
|
|
|
|
export function matchErrorRuleByStatus(statusCode: number): ErrorRule | null {
|
|
return ERROR_RULES.find((rule) => rule.status === statusCode) || null;
|
|
}
|
|
|
|
export function findMatchingErrorRule(statusCode: number, message: unknown): ErrorRule | null {
|
|
return matchErrorRuleByText(message) || matchErrorRuleByStatus(statusCode);
|
|
}
|