mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-12 16:36:05 +00:00
refactor(gateway): share transcript path comparison
This commit is contained in:
@@ -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>;
|
||||
|
||||
16
src/gateway/session-transcript-path.ts
Normal file
16
src/gateway/session-transcript-path.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user