mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 23:58:25 +00:00
refactor(gateway): share transcript path/fd helpers
This commit is contained in:
@@ -423,27 +423,21 @@ function extractFirstUserMessageFromTranscriptChunk(
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function readFirstUserMessageFromTranscript(
|
function findExistingTranscriptPath(
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
storePath: string | undefined,
|
storePath: string | undefined,
|
||||||
sessionFile?: string,
|
sessionFile?: string,
|
||||||
agentId?: string,
|
agentId?: string,
|
||||||
opts?: { includeInterSession?: boolean },
|
|
||||||
): string | null {
|
): string | null {
|
||||||
const candidates = resolveSessionTranscriptCandidates(sessionId, storePath, sessionFile, agentId);
|
const candidates = resolveSessionTranscriptCandidates(sessionId, storePath, sessionFile, agentId);
|
||||||
const filePath = candidates.find((p) => fs.existsSync(p));
|
return candidates.find((p) => fs.existsSync(p)) ?? null;
|
||||||
if (!filePath) {
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
function withOpenTranscriptFd<T>(filePath: string, read: (fd: number) => T | null): T | null {
|
||||||
let fd: number | null = null;
|
let fd: number | null = null;
|
||||||
try {
|
try {
|
||||||
fd = fs.openSync(filePath, "r");
|
fd = fs.openSync(filePath, "r");
|
||||||
const chunk = readTranscriptHeadChunk(fd);
|
return read(fd);
|
||||||
if (!chunk) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return extractFirstUserMessageFromTranscriptChunk(chunk, opts);
|
|
||||||
} catch {
|
} catch {
|
||||||
// file read error
|
// file read error
|
||||||
} finally {
|
} finally {
|
||||||
@@ -454,6 +448,27 @@ export function readFirstUserMessageFromTranscript(
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function readFirstUserMessageFromTranscript(
|
||||||
|
sessionId: string,
|
||||||
|
storePath: string | undefined,
|
||||||
|
sessionFile?: string,
|
||||||
|
agentId?: string,
|
||||||
|
opts?: { includeInterSession?: boolean },
|
||||||
|
): string | null {
|
||||||
|
const filePath = findExistingTranscriptPath(sessionId, storePath, sessionFile, agentId);
|
||||||
|
if (!filePath) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return withOpenTranscriptFd(filePath, (fd) => {
|
||||||
|
const chunk = readTranscriptHeadChunk(fd);
|
||||||
|
if (!chunk) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return extractFirstUserMessageFromTranscriptChunk(chunk, opts);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const LAST_MSG_MAX_BYTES = 16384;
|
const LAST_MSG_MAX_BYTES = 16384;
|
||||||
const LAST_MSG_MAX_LINES = 20;
|
const LAST_MSG_MAX_LINES = 20;
|
||||||
|
|
||||||
@@ -495,29 +510,19 @@ export function readLastMessagePreviewFromTranscript(
|
|||||||
sessionFile?: string,
|
sessionFile?: string,
|
||||||
agentId?: string,
|
agentId?: string,
|
||||||
): string | null {
|
): string | null {
|
||||||
const candidates = resolveSessionTranscriptCandidates(sessionId, storePath, sessionFile, agentId);
|
const filePath = findExistingTranscriptPath(sessionId, storePath, sessionFile, agentId);
|
||||||
const filePath = candidates.find((p) => fs.existsSync(p));
|
|
||||||
if (!filePath) {
|
if (!filePath) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
let fd: number | null = null;
|
return withOpenTranscriptFd(filePath, (fd) => {
|
||||||
try {
|
|
||||||
fd = fs.openSync(filePath, "r");
|
|
||||||
const stat = fs.fstatSync(fd);
|
const stat = fs.fstatSync(fd);
|
||||||
const size = stat.size;
|
const size = stat.size;
|
||||||
if (size === 0) {
|
if (size === 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return readLastMessagePreviewFromOpenTranscript({ fd, size });
|
return readLastMessagePreviewFromOpenTranscript({ fd, size });
|
||||||
} catch {
|
});
|
||||||
// file error
|
|
||||||
} finally {
|
|
||||||
if (fd !== null) {
|
|
||||||
fs.closeSync(fd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const PREVIEW_READ_SIZES = [64 * 1024, 256 * 1024, 1024 * 1024];
|
const PREVIEW_READ_SIZES = [64 * 1024, 256 * 1024, 1024 * 1024];
|
||||||
|
|||||||
Reference in New Issue
Block a user