mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 22:48:27 +00:00
refactor(channels): dedupe transport and gateway test scaffolds
This commit is contained in:
@@ -271,33 +271,9 @@ export function readSessionTitleFieldsFromTranscript(
|
||||
// Head (first user message)
|
||||
let firstUserMessage: string | null = null;
|
||||
try {
|
||||
const buf = Buffer.alloc(8192);
|
||||
const bytesRead = fs.readSync(fd, buf, 0, buf.length, 0);
|
||||
if (bytesRead > 0) {
|
||||
const chunk = buf.toString("utf-8", 0, bytesRead);
|
||||
const lines = chunk.split(/\r?\n/).slice(0, MAX_LINES_TO_SCAN);
|
||||
for (const line of lines) {
|
||||
if (!line.trim()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(line);
|
||||
const msg = parsed?.message as TranscriptMessage | undefined;
|
||||
if (msg?.role !== "user") {
|
||||
continue;
|
||||
}
|
||||
if (opts?.includeInterSession !== true && hasInterSessionUserProvenance(msg)) {
|
||||
continue;
|
||||
}
|
||||
const text = extractTextFromContent(msg.content);
|
||||
if (text) {
|
||||
firstUserMessage = text;
|
||||
break;
|
||||
}
|
||||
} catch {
|
||||
// skip malformed lines
|
||||
}
|
||||
}
|
||||
const chunk = readTranscriptHeadChunk(fd);
|
||||
if (chunk) {
|
||||
firstUserMessage = extractFirstUserMessageFromTranscriptChunk(chunk, opts);
|
||||
}
|
||||
} catch {
|
||||
// ignore head read errors
|
||||
@@ -348,6 +324,44 @@ function extractTextFromContent(content: TranscriptMessage["content"]): string |
|
||||
return null;
|
||||
}
|
||||
|
||||
function readTranscriptHeadChunk(fd: number, maxBytes = 8192): string | null {
|
||||
const buf = Buffer.alloc(maxBytes);
|
||||
const bytesRead = fs.readSync(fd, buf, 0, buf.length, 0);
|
||||
if (bytesRead <= 0) {
|
||||
return null;
|
||||
}
|
||||
return buf.toString("utf-8", 0, bytesRead);
|
||||
}
|
||||
|
||||
function extractFirstUserMessageFromTranscriptChunk(
|
||||
chunk: string,
|
||||
opts?: { includeInterSession?: boolean },
|
||||
): string | null {
|
||||
const lines = chunk.split(/\r?\n/).slice(0, MAX_LINES_TO_SCAN);
|
||||
for (const line of lines) {
|
||||
if (!line.trim()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(line);
|
||||
const msg = parsed?.message as TranscriptMessage | undefined;
|
||||
if (msg?.role !== "user") {
|
||||
continue;
|
||||
}
|
||||
if (opts?.includeInterSession !== true && hasInterSessionUserProvenance(msg)) {
|
||||
continue;
|
||||
}
|
||||
const text = extractTextFromContent(msg.content);
|
||||
if (text) {
|
||||
return text;
|
||||
}
|
||||
} catch {
|
||||
// skip malformed lines
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function readFirstUserMessageFromTranscript(
|
||||
sessionId: string,
|
||||
storePath: string | undefined,
|
||||
@@ -364,34 +378,11 @@ export function readFirstUserMessageFromTranscript(
|
||||
let fd: number | null = null;
|
||||
try {
|
||||
fd = fs.openSync(filePath, "r");
|
||||
const buf = Buffer.alloc(8192);
|
||||
const bytesRead = fs.readSync(fd, buf, 0, buf.length, 0);
|
||||
if (bytesRead === 0) {
|
||||
const chunk = readTranscriptHeadChunk(fd);
|
||||
if (!chunk) {
|
||||
return null;
|
||||
}
|
||||
const chunk = buf.toString("utf-8", 0, bytesRead);
|
||||
const lines = chunk.split(/\r?\n/).slice(0, MAX_LINES_TO_SCAN);
|
||||
|
||||
for (const line of lines) {
|
||||
if (!line.trim()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(line);
|
||||
const msg = parsed?.message as TranscriptMessage | undefined;
|
||||
if (msg?.role === "user") {
|
||||
if (opts?.includeInterSession !== true && hasInterSessionUserProvenance(msg)) {
|
||||
continue;
|
||||
}
|
||||
const text = extractTextFromContent(msg.content);
|
||||
if (text) {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// skip malformed lines
|
||||
}
|
||||
}
|
||||
return extractFirstUserMessageFromTranscriptChunk(chunk, opts);
|
||||
} catch {
|
||||
// file read error
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user