mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 06:47:26 +00:00
refactor(gateway): dedupe transcript tail preview
This commit is contained in:
@@ -306,32 +306,7 @@ export function readSessionTitleFieldsFromTranscript(
|
|||||||
// Tail (last message preview)
|
// Tail (last message preview)
|
||||||
let lastMessagePreview: string | null = null;
|
let lastMessagePreview: string | null = null;
|
||||||
try {
|
try {
|
||||||
const readStart = Math.max(0, size - LAST_MSG_MAX_BYTES);
|
lastMessagePreview = readLastMessagePreviewFromOpenTranscript({ fd, size });
|
||||||
const readLen = Math.min(size, LAST_MSG_MAX_BYTES);
|
|
||||||
const buf = Buffer.alloc(readLen);
|
|
||||||
fs.readSync(fd, buf, 0, readLen, readStart);
|
|
||||||
|
|
||||||
const chunk = buf.toString("utf-8");
|
|
||||||
const lines = chunk.split(/\r?\n/).filter((l) => l.trim());
|
|
||||||
const tailLines = lines.slice(-LAST_MSG_MAX_LINES);
|
|
||||||
|
|
||||||
for (let i = tailLines.length - 1; i >= 0; i--) {
|
|
||||||
const line = tailLines[i];
|
|
||||||
try {
|
|
||||||
const parsed = JSON.parse(line);
|
|
||||||
const msg = parsed?.message as TranscriptMessage | undefined;
|
|
||||||
if (msg?.role !== "user" && msg?.role !== "assistant") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const text = extractTextFromContent(msg.content);
|
|
||||||
if (text) {
|
|
||||||
lastMessagePreview = text;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// skip malformed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
// ignore tail read errors
|
// ignore tail read errors
|
||||||
}
|
}
|
||||||
@@ -430,6 +405,38 @@ export function readFirstUserMessageFromTranscript(
|
|||||||
const LAST_MSG_MAX_BYTES = 16384;
|
const LAST_MSG_MAX_BYTES = 16384;
|
||||||
const LAST_MSG_MAX_LINES = 20;
|
const LAST_MSG_MAX_LINES = 20;
|
||||||
|
|
||||||
|
function readLastMessagePreviewFromOpenTranscript(params: {
|
||||||
|
fd: number;
|
||||||
|
size: number;
|
||||||
|
}): string | null {
|
||||||
|
const readStart = Math.max(0, params.size - LAST_MSG_MAX_BYTES);
|
||||||
|
const readLen = Math.min(params.size, LAST_MSG_MAX_BYTES);
|
||||||
|
const buf = Buffer.alloc(readLen);
|
||||||
|
fs.readSync(params.fd, buf, 0, readLen, readStart);
|
||||||
|
|
||||||
|
const chunk = buf.toString("utf-8");
|
||||||
|
const lines = chunk.split(/\r?\n/).filter((l) => l.trim());
|
||||||
|
const tailLines = lines.slice(-LAST_MSG_MAX_LINES);
|
||||||
|
|
||||||
|
for (let i = tailLines.length - 1; i >= 0; i--) {
|
||||||
|
const line = tailLines[i];
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(line);
|
||||||
|
const msg = parsed?.message as TranscriptMessage | undefined;
|
||||||
|
if (msg?.role !== "user" && msg?.role !== "assistant") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const text = extractTextFromContent(msg.content);
|
||||||
|
if (text) {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// skip malformed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
export function readLastMessagePreviewFromTranscript(
|
export function readLastMessagePreviewFromTranscript(
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
storePath: string | undefined,
|
storePath: string | undefined,
|
||||||
@@ -450,31 +457,7 @@ export function readLastMessagePreviewFromTranscript(
|
|||||||
if (size === 0) {
|
if (size === 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
return readLastMessagePreviewFromOpenTranscript({ fd, size });
|
||||||
const readStart = Math.max(0, size - LAST_MSG_MAX_BYTES);
|
|
||||||
const readLen = Math.min(size, LAST_MSG_MAX_BYTES);
|
|
||||||
const buf = Buffer.alloc(readLen);
|
|
||||||
fs.readSync(fd, buf, 0, readLen, readStart);
|
|
||||||
|
|
||||||
const chunk = buf.toString("utf-8");
|
|
||||||
const lines = chunk.split(/\r?\n/).filter((l) => l.trim());
|
|
||||||
const tailLines = lines.slice(-LAST_MSG_MAX_LINES);
|
|
||||||
|
|
||||||
for (let i = tailLines.length - 1; i >= 0; i--) {
|
|
||||||
const line = tailLines[i];
|
|
||||||
try {
|
|
||||||
const parsed = JSON.parse(line);
|
|
||||||
const msg = parsed?.message as TranscriptMessage | undefined;
|
|
||||||
if (msg?.role === "user" || msg?.role === "assistant") {
|
|
||||||
const text = extractTextFromContent(msg.content);
|
|
||||||
if (text) {
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// skip malformed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
// file error
|
// file error
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user