mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-24 16:12:23 +03:00
fix(api): save call logs and add endpoint fallback for local rerank providers (#11081)
Validated on a combined board over tip aa128736: focused tests local-rerank-logging + call-logs-row-filter green (7/7 across runs), typecheck:core clean. One pre-merge fix: widened waitForCallLogSaves 5s→15s — the call-log artifact writer's fsync latency exceeds 5s under a loaded host (pre-existing call-log-save-drain flakes identically on the pure tip), so the budget was load-flaky, not the code. Local rerank now logs call entries on success and error paths like embeddings/cloud rerank, captures full request/response payloads, attaches x-omniroute-* headers, and falls back /v1/rerank→/rerank on 404. Thank you @AndrianBalanescu!
This commit is contained in:
@@ -10,11 +10,15 @@ import { HTTP_STATUS } from "@omniroute/open-sse/config/constants.ts";
|
||||
import { enforceApiKeyPolicy } from "@/shared/utils/apiKeyPolicy";
|
||||
import { v1RerankSchema } from "@/shared/validation/schemas";
|
||||
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
|
||||
import { getCachedProviderNodes } from "@/lib/localDb";
|
||||
import { getCachedProviderNodes } from "@/lib/db/readCache";
|
||||
import {
|
||||
isAllRateLimitedCredentials,
|
||||
rateLimitedProviderResponse,
|
||||
} from "@/app/api/v1/_shared/rateLimit";
|
||||
import { saveCallLog } from "@/lib/usageDb";
|
||||
import { attachOmniRouteMetaHeaders } from "@/domain/omnirouteResponseMeta";
|
||||
import { generateRequestId } from "@/shared/utils/requestId";
|
||||
import { CORS_HEADERS } from "@omniroute/open-sse/utils/cors.ts";
|
||||
|
||||
/**
|
||||
* Handle CORS preflight
|
||||
@@ -121,6 +125,8 @@ async function postHandler(request, context) {
|
||||
return_documents: body.return_documents,
|
||||
credentials,
|
||||
connectionId: (credentials as { connectionId?: string } | null)?.connectionId || null,
|
||||
apiKeyId: policy.apiKeyInfo?.id || null,
|
||||
apiKeyName: policy.apiKeyInfo?.name || null,
|
||||
});
|
||||
if (response?.ok) {
|
||||
await clearRecoveredProviderState(credentials);
|
||||
@@ -148,8 +154,9 @@ async function postHandler(request, context) {
|
||||
}
|
||||
|
||||
const token = credentials?.apiKey || credentials?.accessToken;
|
||||
const startTime = Date.now();
|
||||
try {
|
||||
const res = await fetch(localProvider.baseUrl, {
|
||||
let res = await fetch(localProvider.baseUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
@@ -164,19 +171,110 @@ async function postHandler(request, context) {
|
||||
}),
|
||||
});
|
||||
|
||||
// Some local providers (e.g. Infinity, TEI) mount at /rerank rather than /v1/rerank
|
||||
if (res.status === 404 && localProvider.baseUrl.endsWith("/v1/rerank")) {
|
||||
const fallbackUrl = localProvider.baseUrl.replace(/\/v1\/rerank$/, "/rerank");
|
||||
try {
|
||||
const fallbackRes = await fetch(fallbackUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: localModel,
|
||||
query: body.query,
|
||||
documents: body.documents,
|
||||
top_n: body.top_n || body.documents.length,
|
||||
return_documents: body.return_documents !== false,
|
||||
}),
|
||||
});
|
||||
if (fallbackRes.ok || fallbackRes.status !== 404) {
|
||||
res = fallbackRes;
|
||||
}
|
||||
} catch {
|
||||
// retain original 404 response if fallback fetch fails
|
||||
}
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const errData = await res.json().catch(() => ({}));
|
||||
return errorResponse(
|
||||
res.status,
|
||||
errData.message || errData.detail || `Provider returned HTTP ${res.status}`
|
||||
);
|
||||
const errorMessage =
|
||||
errData.message || errData.detail || `Provider returned HTTP ${res.status}`;
|
||||
saveCallLog({
|
||||
method: "POST",
|
||||
path: "/v1/rerank",
|
||||
status: res.status,
|
||||
model: body.model,
|
||||
provider: prefix,
|
||||
connectionId:
|
||||
(credentials as { connectionId?: string } | null)?.connectionId || undefined,
|
||||
duration: Date.now() - startTime,
|
||||
requestBody: {
|
||||
model: body.model,
|
||||
query: body.query,
|
||||
documents: body.documents,
|
||||
top_n: body.top_n,
|
||||
return_documents: body.return_documents,
|
||||
},
|
||||
responseBody: errData,
|
||||
error: errorMessage,
|
||||
apiKeyId: policy.apiKeyInfo?.id || undefined,
|
||||
apiKeyName: policy.apiKeyInfo?.name || undefined,
|
||||
}).catch(() => {});
|
||||
return errorResponse(res.status, errorMessage);
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
return Response.json(data, {
|
||||
headers: {},
|
||||
const latencyMs = Date.now() - startTime;
|
||||
saveCallLog({
|
||||
method: "POST",
|
||||
path: "/v1/rerank",
|
||||
status: 200,
|
||||
model: body.model,
|
||||
provider: prefix,
|
||||
connectionId:
|
||||
(credentials as { connectionId?: string } | null)?.connectionId || undefined,
|
||||
duration: latencyMs,
|
||||
tokens: { prompt_tokens: 0, completion_tokens: 0 },
|
||||
requestBody: {
|
||||
model: body.model,
|
||||
query: body.query,
|
||||
documents: body.documents,
|
||||
top_n: body.top_n,
|
||||
return_documents: body.return_documents,
|
||||
},
|
||||
responseBody: data,
|
||||
apiKeyId: policy.apiKeyInfo?.id || undefined,
|
||||
apiKeyName: policy.apiKeyInfo?.name || undefined,
|
||||
}).catch(() => {});
|
||||
|
||||
const headers = new Headers({ ...CORS_HEADERS, "Content-Type": "application/json" });
|
||||
attachOmniRouteMetaHeaders(headers, {
|
||||
provider: prefix,
|
||||
model: localModel,
|
||||
costUsd: 0,
|
||||
latencyMs,
|
||||
requestId: generateRequestId(),
|
||||
});
|
||||
return new Response(JSON.stringify(data), {
|
||||
status: 200,
|
||||
headers,
|
||||
});
|
||||
} catch (err: any) {
|
||||
saveCallLog({
|
||||
method: "POST",
|
||||
path: "/v1/rerank",
|
||||
status: 500,
|
||||
model: body.model,
|
||||
provider: prefix,
|
||||
connectionId:
|
||||
(credentials as { connectionId?: string } | null)?.connectionId || undefined,
|
||||
duration: Date.now() - startTime,
|
||||
error: err.message,
|
||||
apiKeyId: policy.apiKeyInfo?.id || undefined,
|
||||
apiKeyName: policy.apiKeyInfo?.name || undefined,
|
||||
}).catch(() => {});
|
||||
return errorResponse(500, `Rerank request failed: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user