refactor(gateway): dedupe session usage file resolution

This commit is contained in:
Peter Steinberger
2026-02-15 06:11:53 +00:00
parent 7793f2efd5
commit 2c5e24cbb5

View File

@@ -10,7 +10,7 @@ import type {
SessionModelUsage, SessionModelUsage,
SessionToolUsage, SessionToolUsage,
} from "../../infra/session-cost-usage.js"; } from "../../infra/session-cost-usage.js";
import type { GatewayRequestHandlers } from "./types.js"; import type { GatewayRequestHandlers, RespondFn } from "./types.js";
import { loadConfig } from "../../config/config.js"; import { loadConfig } from "../../config/config.js";
import { import {
resolveSessionFilePath, resolveSessionFilePath,
@@ -50,6 +50,40 @@ type CostUsageCacheEntry = {
const costUsageCache = new Map<string, CostUsageCacheEntry>(); const costUsageCache = new Map<string, CostUsageCacheEntry>();
function resolveSessionUsageFileOrRespond(
key: string,
respond: RespondFn,
): {
config: ReturnType<typeof loadConfig>;
entry: SessionEntry | undefined;
agentId: string | undefined;
sessionId: string;
sessionFile: string;
} | null {
const config = loadConfig();
const { entry, storePath } = loadSessionEntry(key);
// For discovered sessions (not in store), try using key as sessionId directly
const parsed = parseAgentSessionKey(key);
const agentId = parsed?.agentId;
const rawSessionId = parsed?.rest ?? key;
const sessionId = entry?.sessionId ?? rawSessionId;
let sessionFile: string;
try {
const pathOpts = resolveSessionFilePathOptions({ storePath, agentId });
sessionFile = resolveSessionFilePath(sessionId, entry, pathOpts);
} catch {
respond(
false,
undefined,
errorShape(ErrorCodes.INVALID_REQUEST, `Invalid session key: ${key}`),
);
return null;
}
return { config, entry, agentId, sessionId, sessionFile };
}
/** /**
* Parse a date string (YYYY-MM-DD) to start of day timestamp in UTC. * Parse a date string (YYYY-MM-DD) to start of day timestamp in UTC.
* Returns undefined if invalid. * Returns undefined if invalid.
@@ -752,26 +786,11 @@ export const usageHandlers: GatewayRequestHandlers = {
return; return;
} }
const config = loadConfig(); const resolved = resolveSessionUsageFileOrRespond(key, respond);
const { entry, storePath } = loadSessionEntry(key); if (!resolved) {
// For discovered sessions (not in store), try using key as sessionId directly
const parsed = parseAgentSessionKey(key);
const agentId = parsed?.agentId;
const rawSessionId = parsed?.rest ?? key;
const sessionId = entry?.sessionId ?? rawSessionId;
let sessionFile: string;
try {
const pathOpts = resolveSessionFilePathOptions({ storePath, agentId });
sessionFile = resolveSessionFilePath(sessionId, entry, pathOpts);
} catch {
respond(
false,
undefined,
errorShape(ErrorCodes.INVALID_REQUEST, `Invalid session key: ${key}`),
);
return; return;
} }
const { config, entry, agentId, sessionId, sessionFile } = resolved;
const timeseries = await loadSessionUsageTimeSeries({ const timeseries = await loadSessionUsageTimeSeries({
sessionId, sessionId,
@@ -805,26 +824,11 @@ export const usageHandlers: GatewayRequestHandlers = {
? Math.min(params.limit, 1000) ? Math.min(params.limit, 1000)
: 200; : 200;
const config = loadConfig(); const resolved = resolveSessionUsageFileOrRespond(key, respond);
const { entry, storePath } = loadSessionEntry(key); if (!resolved) {
// For discovered sessions (not in store), try using key as sessionId directly
const parsed = parseAgentSessionKey(key);
const agentId = parsed?.agentId;
const rawSessionId = parsed?.rest ?? key;
const sessionId = entry?.sessionId ?? rawSessionId;
let sessionFile: string;
try {
const pathOpts = resolveSessionFilePathOptions({ storePath, agentId });
sessionFile = resolveSessionFilePath(sessionId, entry, pathOpts);
} catch {
respond(
false,
undefined,
errorShape(ErrorCodes.INVALID_REQUEST, `Invalid session key: ${key}`),
);
return; return;
} }
const { config, entry, agentId, sessionId, sessionFile } = resolved;
const { loadSessionLogs } = await import("../../infra/session-cost-usage.js"); const { loadSessionLogs } = await import("../../infra/session-cost-usage.js");
const logs = await loadSessionLogs({ const logs = await loadSessionLogs({