mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
refactor(gateway): dedupe session usage file resolution
This commit is contained in:
@@ -10,7 +10,7 @@ import type {
|
||||
SessionModelUsage,
|
||||
SessionToolUsage,
|
||||
} 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 {
|
||||
resolveSessionFilePath,
|
||||
@@ -50,6 +50,40 @@ type 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.
|
||||
* Returns undefined if invalid.
|
||||
@@ -752,26 +786,11 @@ export const usageHandlers: GatewayRequestHandlers = {
|
||||
return;
|
||||
}
|
||||
|
||||
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}`),
|
||||
);
|
||||
const resolved = resolveSessionUsageFileOrRespond(key, respond);
|
||||
if (!resolved) {
|
||||
return;
|
||||
}
|
||||
const { config, entry, agentId, sessionId, sessionFile } = resolved;
|
||||
|
||||
const timeseries = await loadSessionUsageTimeSeries({
|
||||
sessionId,
|
||||
@@ -805,26 +824,11 @@ export const usageHandlers: GatewayRequestHandlers = {
|
||||
? Math.min(params.limit, 1000)
|
||||
: 200;
|
||||
|
||||
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}`),
|
||||
);
|
||||
const resolved = resolveSessionUsageFileOrRespond(key, respond);
|
||||
if (!resolved) {
|
||||
return;
|
||||
}
|
||||
const { config, entry, agentId, sessionId, sessionFile } = resolved;
|
||||
|
||||
const { loadSessionLogs } = await import("../../infra/session-cost-usage.js");
|
||||
const logs = await loadSessionLogs({
|
||||
|
||||
Reference in New Issue
Block a user