@@ -33,71 +33,6 @@ function extractMessageContent(parsed: any): string {
3333 }
3434}
3535
36- /**
37- * Extract last message of specified role from transcript JSONL file
38- * @param transcriptPath Path to transcript file
39- * @param role 'user' or 'assistant'
40- * @param stripSystemReminders Whether to remove <system-reminder> tags (for assistant)
41- */
42- export function extractLastMessage (
43- transcriptPath : string ,
44- role : "user" | "assistant" ,
45- stripSystemReminders : boolean = false
46- ) : string {
47- if ( ! transcriptPath || ! existsSync ( transcriptPath ) ) {
48- throw new Error ( `Transcript path missing or file does not exist: ${ transcriptPath } ` ) ;
49- }
50-
51- const content = readFileSync ( transcriptPath , "utf-8" ) . trim ( ) ;
52- if ( ! content ) {
53- throw new Error ( `Transcript file exists but is empty: ${ transcriptPath } ` ) ;
54- }
55-
56- const lines = content . split ( "\n" ) ;
57- let foundMatchingRole = false ;
58-
59- for ( let i = lines . length - 1 ; i >= 0 ; i -- ) {
60- const line = JSON . parse ( lines [ i ] ) ;
61- if ( line . type === role ) {
62- foundMatchingRole = true ;
63-
64- if ( line . message ?. content ) {
65- let text = "" ;
66- const msgContent = line . message . content ;
67-
68- if ( typeof msgContent === "string" ) {
69- text = msgContent ;
70- } else if ( Array . isArray ( msgContent ) ) {
71- text = msgContent
72- . filter ( ( c : any ) => c . type === "text" )
73- . map ( ( c : any ) => c . text )
74- . join ( "\n" ) ;
75- } else {
76- // Unknown content format - throw error
77- throw new Error (
78- `Unknown message content format in transcript. Type: ${ typeof msgContent } `
79- ) ;
80- }
81-
82- if ( stripSystemReminders ) {
83- text = text . replace ( / < s y s t e m - r e m i n d e r > [ \s \S ] * ?< \/ s y s t e m - r e m i n d e r > / g, "" ) ;
84- text = text . replace ( / \n { 3 , } / g, "\n\n" ) . trim ( ) ;
85- }
86-
87- // Return text even if empty - caller decides if that's an error
88- return text ;
89- }
90- }
91- }
92-
93- // If we searched the whole transcript and didn't find any message of this role
94- if ( ! foundMatchingRole ) {
95- throw new Error ( `No message found for role '${ role } ' in transcript: ${ transcriptPath } ` ) ;
96- }
97-
98- return "" ;
99- }
100-
10136const DEFAULTS = [ "[Request interrupted by user for tool use]" ] ;
10237
10338/**
@@ -126,7 +61,7 @@ export function extractLastAssistantWithPrecedingUsers(
12661 // Find the last assistant message
12762 let lastAssistantIndex = - 1 ;
12863 for ( let i = parsedLines . length - 1 ; i >= 0 ; i -- ) {
129- let assistantMessage = extractMessageContent ( parsedLines [ lastAssistantIndex ] ) ;
64+ let assistantMessage = extractMessageContent ( parsedLines [ i ] ) ;
13065
13166 if ( parsedLines [ i ] . type === "assistant" && assistantMessage ) {
13267 lastAssistantIndex = i ;
@@ -158,9 +93,19 @@ export function extractLastAssistantWithPrecedingUsers(
15893 if ( parsed . type === "assistant" && parsed . message && gotAtleastOne && content ) {
15994 // Stop when we hit another assistant message
16095 break ;
161- } else if ( parsed . type === "user" && ! DEFAULTS . includes ( content ) && content ) {
96+ } else if ( parsed . type === "user" && parsed . message && ! DEFAULTS . includes ( content ) && content ) {
16297 // Collect user message (we'll reverse later to maintain chronological order)
163- userMessages . unshift ( content ) ;
98+
99+ let strippedcontent = content . replace (
100+ / < l o c a l - c o m m a n d - c a v e a t > [ \s \S ] * ?< \/ l o c a l - c o m m a n d - c a v e a t > / g,
101+ ""
102+ ) ;
103+ strippedcontent = content . replace (
104+ / < l o c a l - c o m m a n d - s t d o u t > [ \s \S ] * ?< \/ l o c a l - c o m m a n d - s t d o u t > / g,
105+ ""
106+ ) ;
107+
108+ userMessages . unshift ( strippedcontent ) ;
164109 gotAtleastOne = true ;
165110 }
166111 }
0 commit comments