refactor(gateway): share transcript path comparison

This commit is contained in:
Vincent Koc
2026-06-02 11:35:14 +02:00
parent 286c8e3632
commit 45f7aec156
3 changed files with 20 additions and 33 deletions

View File

@@ -1,11 +1,9 @@
import fs from "node:fs";
import path from "node:path";
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import { getRuntimeConfig } from "../config/io.js";
import type { SessionEntry } from "../config/sessions/types.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { normalizeAgentId } from "../routing/session-key.js";
import { resolvePreferredSessionKeyForSessionIdMatches } from "../sessions/session-id-resolution.js";
import { resolveTranscriptPathForComparison } from "./session-transcript-path.js";
import {
loadCombinedSessionStoreForGateway,
resolveGatewaySessionStoreTarget,
@@ -15,19 +13,6 @@ import {
const TRANSCRIPT_SESSION_KEY_CACHE = new Map<string, string>();
const TRANSCRIPT_SESSION_KEY_CACHE_MAX = 256;
function resolveTranscriptPathForComparison(value: string | undefined): string | undefined {
const trimmed = normalizeOptionalString(value);
if (!trimmed) {
return undefined;
}
const resolved = path.resolve(trimmed);
try {
return fs.realpathSync(resolved);
} catch {
return resolved;
}
}
function sessionKeyMatchesTranscriptPath(params: {
cfg: OpenClawConfig;
store: Record<string, SessionEntry>;

View File

@@ -0,0 +1,16 @@
import fs from "node:fs";
import path from "node:path";
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
export function resolveTranscriptPathForComparison(value: string | undefined): string | undefined {
const trimmed = normalizeOptionalString(value);
if (!trimmed) {
return undefined;
}
const resolved = path.resolve(trimmed);
try {
return fs.realpathSync(resolved);
} catch {
return resolved;
}
}

View File

@@ -1,6 +1,4 @@
import fs from "node:fs";
import type { IncomingMessage, ServerResponse } from "node:http";
import path from "node:path";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalString,
@@ -30,6 +28,7 @@ import {
resolveSessionHistoryTailReadOptions,
SessionHistorySseState,
} from "./session-history-state.js";
import { resolveTranscriptPathForComparison } from "./session-transcript-path.js";
import {
readRecentSessionMessagesWithStatsAsync,
readSessionMessagesAsync,
@@ -77,19 +76,6 @@ function resolveLimit(req: IncomingMessage): number | undefined {
return Math.min(MAX_SESSION_HISTORY_LIMIT, Math.max(1, value));
}
function canonicalizePath(value: string | undefined): string | undefined {
const trimmed = normalizeOptionalString(value);
if (!trimmed) {
return undefined;
}
const resolved = path.resolve(trimmed);
try {
return fs.realpathSync(resolved);
} catch {
return resolved;
}
}
function sseWrite(res: ServerResponse, event: string, payload: unknown): void {
res.write(`event: ${event}\n`);
res.write(`data: ${JSON.stringify(payload)}\n\n`);
@@ -198,7 +184,7 @@ export async function handleSessionHistoryHttpRequest(
entry.sessionFile,
target.agentId,
)
.map((candidate) => canonicalizePath(candidate))
.map((candidate) => resolveTranscriptPathForComparison(candidate))
.filter((candidate): candidate is string => typeof candidate === "string"),
)
: new Set<string>();
@@ -306,7 +292,7 @@ export async function handleSessionHistoryHttpRequest(
if (!entry?.sessionId) {
return;
}
const updatePath = canonicalizePath(update.sessionFile);
const updatePath = resolveTranscriptPathForComparison(update.sessionFile);
if (!updatePath || !transcriptCandidates.has(updatePath)) {
return;
}