mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-04 22:32:12 +03:00
Capture actual upstream provider requests (#3941)
Integrated into release/v3.8.27
This commit is contained in:
26
src/lib/usage/pendingRequestScope.ts
Normal file
26
src/lib/usage/pendingRequestScope.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import {
|
||||
finalizePendingRequest,
|
||||
finalizePendingRequestById,
|
||||
updatePendingRequest,
|
||||
updatePendingRequestById,
|
||||
type PendingRequestMetadata,
|
||||
} from "./usageHistory";
|
||||
|
||||
export type PendingRequestScope = {
|
||||
id: string | null | undefined;
|
||||
model: string;
|
||||
provider: string;
|
||||
connectionId: string | null;
|
||||
};
|
||||
|
||||
export function updatePendingScope(scope: PendingRequestScope, metadata: PendingRequestMetadata) {
|
||||
if (!updatePendingRequestById(scope.id || null, metadata)) {
|
||||
updatePendingRequest(scope.model, scope.provider, scope.connectionId, metadata);
|
||||
}
|
||||
}
|
||||
|
||||
export function finalizePendingScope(scope: PendingRequestScope, metadata: PendingRequestMetadata) {
|
||||
if (!finalizePendingRequestById(scope.id, metadata)) {
|
||||
finalizePendingRequest(scope.model, scope.provider, scope.connectionId, metadata);
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ import {
|
||||
} from "./tokenAccounting";
|
||||
|
||||
type JsonRecord = Record<string, unknown>;
|
||||
type PendingRequestMetadata = {
|
||||
export type PendingRequestMetadata = {
|
||||
clientEndpoint?: string | null;
|
||||
clientRequest?: unknown;
|
||||
providerRequest?: unknown;
|
||||
@@ -308,6 +308,13 @@ export function updatePendingRequest(
|
||||
Object.assign(details[lastIdx], normalizePendingMetadata(metadata));
|
||||
}
|
||||
|
||||
export function updatePendingRequestById(id: string | null, metadata: PendingRequestMetadata) {
|
||||
const detail = id ? pendingById.get(id) : null;
|
||||
if (!detail) return false;
|
||||
Object.assign(detail, normalizePendingMetadata(metadata));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the first (oldest) pending request detail and then remove it.
|
||||
* Unlike updatePendingRequest which targets the last entry, this is designed
|
||||
|
||||
Reference in New Issue
Block a user