@@ -267,22 +267,57 @@ async function readRecentTranscriptTailLinesAsync(
267267}
268268
269269const MAX_TRANSCRIPT_PARSE_LINE_BYTES = 256 * 1024 ;
270+ const OVERSIZED_TRANSCRIPT_METADATA_PREFIX_CHARS = 64 * 1024 ;
270271const TRANSCRIPT_OVERSIZED_MESSAGE_PLACEHOLDER = "[chat.history omitted: message too large]" ;
271272
272273function isOversizedTranscriptLine ( line : string ) : boolean {
273274 return Buffer . byteLength ( line , "utf8" ) > MAX_TRANSCRIPT_PARSE_LINE_BYTES ;
274275}
275276
276- function buildOversizedTranscriptRecord ( ) : TailTranscriptRecord {
277- return {
278- record : {
279- message : {
280- role : "assistant" ,
281- content : [ { type : "text" , text : TRANSCRIPT_OVERSIZED_MESSAGE_PLACEHOLDER } ] ,
282- __openclaw : { truncated : true , reason : "oversized" } ,
283- } ,
277+ function extractJsonStringFieldPrefix ( prefix : string , field : string ) : string | undefined {
278+ const match = new RegExp ( `"${ field } "\\s*:\\s*"((?:\\\\.|[^"\\\\])*)"` ) . exec ( prefix ) ;
279+ if ( ! match ) {
280+ return undefined ;
281+ }
282+ try {
283+ const decoded = JSON . parse ( `"${ match [ 1 ] } "` ) as unknown ;
284+ return normalizeTailEntryString ( decoded ) ;
285+ } catch {
286+ return undefined ;
287+ }
288+ }
289+
290+ function extractJsonNullableStringFieldPrefix (
291+ prefix : string ,
292+ field : string ,
293+ ) : string | null | undefined {
294+ if ( new RegExp ( `"${ field } "\\s*:\\s*null` ) . test ( prefix ) ) {
295+ return null ;
296+ }
297+ return extractJsonStringFieldPrefix ( prefix , field ) ;
298+ }
299+
300+ function buildOversizedTranscriptRecord ( line : string ) : TailTranscriptRecord {
301+ const prefix = line . slice ( 0 , OVERSIZED_TRANSCRIPT_METADATA_PREFIX_CHARS ) ;
302+ const id = extractJsonStringFieldPrefix ( prefix , "id" ) ;
303+ const parentId = extractJsonNullableStringFieldPrefix ( prefix , "parentId" ) ;
304+ const type = extractJsonStringFieldPrefix ( prefix , "type" ) ;
305+ const role = extractJsonStringFieldPrefix ( prefix , "role" ) ?? "assistant" ;
306+ const record : Record < string , unknown > = {
307+ ...( type ? { type } : { } ) ,
308+ ...( id ? { id } : { } ) ,
309+ ...( parentId !== undefined ? { parentId } : { } ) ,
310+ message : {
311+ role,
312+ content : [ { type : "text" , text : TRANSCRIPT_OVERSIZED_MESSAGE_PLACEHOLDER } ] ,
313+ __openclaw : { truncated : true , reason : "oversized" } ,
284314 } ,
285315 } ;
316+ return {
317+ ...( id ? { id } : { } ) ,
318+ ...( parentId !== undefined ? { parentId } : { } ) ,
319+ record,
320+ } ;
286321}
287322
288323function normalizeTailEntryString ( value : unknown ) : string | undefined {
@@ -291,7 +326,7 @@ function normalizeTailEntryString(value: unknown): string | undefined {
291326
292327function parseTailTranscriptRecord ( line : string ) : TailTranscriptRecord | null {
293328 if ( isOversizedTranscriptLine ( line ) ) {
294- return buildOversizedTranscriptRecord ( ) ;
329+ return buildOversizedTranscriptRecord ( line ) ;
295330 }
296331 try {
297332 const parsed = JSON . parse ( line ) as unknown ;
0 commit comments