feat(daemon): include client ID in usage tracking entries

- Add getClientIdFromRequest helper to extract API key from Authorization header
- Look up clientId from store's clientIds array
- Include client field in both streaming and non-streaming usage entries
- Enables per-client usage tracking for integrations
This commit is contained in:
redshift
2026-03-21 16:42:59 +00:00
parent 5aadad1d34
commit 0cb95cdae2
+29
View File
@@ -12,6 +12,33 @@ import {
import type { UsageData } from "../types";
import { logger } from "../../utils/logger";
/**
* Extracts the client ID from an incoming request by looking up the API key
* in the store's clientIds list.
*/
function getClientIdFromRequest(
req: IncomingMessage,
store: { getState(): any },
): string | undefined {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith("Bearer ")) {
return undefined;
}
const apiKey = authHeader.slice(7); // Remove "Bearer " prefix
if (!apiKey.startsWith("sk-")) {
return undefined;
}
const state = store.getState();
const clientIds = state.clientIds || [];
const matchingClient = (clientIds as { clientId: string; apiKey: string }[]).find(
(c) => c.apiKey === apiKey,
);
return matchingClient?.clientId;
}
async function readBody(req: IncomingMessage): Promise<string> {
return new Promise((resolve, reject) => {
let data = "";
@@ -419,6 +446,7 @@ export function createDaemonRequestHandler(deps: {
modelId,
baseUrl: usageBaseUrl,
requestId: usageRequestId,
client: getClientIdFromRequest(req, deps.store),
...capturedUsage,
});
logger.log(
@@ -447,6 +475,7 @@ export function createDaemonRequestHandler(deps: {
modelId,
baseUrl: usageBaseUrl,
requestId: responseRequestId,
client: getClientIdFromRequest(req, deps.store),
...nonStreamUsage,
});
}