@@ -98,6 +98,52 @@ async function writeSessionWithToolResultContent(
9898 await fs . writeFile ( sessionFile , `${ entries . map ( ( entry ) => JSON . stringify ( entry ) ) . join ( "\n" ) } \n` ) ;
9999}
100100
101+ async function writeSessionWithAssistantContent (
102+ sessionFile : string ,
103+ content : unknown ,
104+ ) : Promise < void > {
105+ const entries = [
106+ {
107+ type : "session" ,
108+ version : 3 ,
109+ id : "string-assistant-session" ,
110+ timestamp : "2026-07-01T00:00:00.000Z" ,
111+ cwd : "/tmp/tool-result-replay" ,
112+ } ,
113+ {
114+ type : "message" ,
115+ id : "user-1" ,
116+ parentId : null ,
117+ timestamp : "2026-07-01T00:00:01.000Z" ,
118+ message : { role : "user" , content : "say hello" , timestamp : 1 } ,
119+ } ,
120+ {
121+ type : "message" ,
122+ id : "assistant-1" ,
123+ parentId : "user-1" ,
124+ timestamp : "2026-07-01T00:00:02.000Z" ,
125+ message : {
126+ role : "assistant" ,
127+ provider : "anthropic" ,
128+ api : "anthropic-messages" ,
129+ model : "claude-sonnet-4-6" ,
130+ stopReason : "stop" ,
131+ timestamp : 2 ,
132+ usage : {
133+ input : 0 ,
134+ output : 0 ,
135+ cacheRead : 0 ,
136+ cacheWrite : 0 ,
137+ totalTokens : 0 ,
138+ cost : { input : 0 , output : 0 , cacheRead : 0 , cacheWrite : 0 , total : 0 } ,
139+ } ,
140+ content,
141+ } ,
142+ } ,
143+ ] ;
144+ await fs . writeFile ( sessionFile , `${ entries . map ( ( entry ) => JSON . stringify ( entry ) ) . join ( "\n" ) } \n` ) ;
145+ }
146+
101147describe ( "SessionManager tool-result replay" , ( ) => {
102148 afterEach ( async ( ) => {
103149 await Promise . all (
@@ -120,6 +166,43 @@ describe("SessionManager tool-result replay", () => {
120166 expect ( toolResult . content ) . toEqual ( [ { type : "text" , text : "lookup result text" } ] ) ;
121167 } ) ;
122168
169+ it ( "replays string assistant JSONL content as Anthropic assistant text" , async ( ) => {
170+ const dir = await makeTempDir ( ) ;
171+ const sessionFile = path . join ( dir , "session.jsonl" ) ;
172+ await writeSessionWithAssistantContent ( sessionFile , "assistant replay text" ) ;
173+ const context = SessionManager . open (
174+ sessionFile ,
175+ dir ,
176+ "/tmp/tool-result-replay" ,
177+ ) . buildSessionContext ( ) ;
178+ const assistant = context . messages . find ( ( message ) => message . role === "assistant" ) ;
179+ if ( ! assistant || assistant . role !== "assistant" ) {
180+ throw new Error ( "assistant message missing" ) ;
181+ }
182+ expect ( assistant . content ) . toEqual ( [ { type : "text" , text : "assistant replay text" } ] ) ;
183+
184+ let capturedPayload : unknown ;
185+ const stream = streamAnthropic ( makeAnthropicModel ( ) , toLlmContext ( context ) , {
186+ apiKey : "sk-ant-provider" ,
187+ onPayload : ( payload ) => {
188+ capturedPayload = payload ;
189+ throw new Error ( "stop before network" ) ;
190+ } ,
191+ } ) ;
192+
193+ await stream . result ( ) ;
194+
195+ const payload = capturedPayload as {
196+ messages : Array < {
197+ role : string ;
198+ content : string | Array < { type ?: unknown ; text ?: unknown } > ;
199+ } > ;
200+ } ;
201+ const assistantPayload = payload . messages . find ( ( message ) => message . role === "assistant" ) ;
202+
203+ expect ( assistantPayload ?. content ) . toEqual ( [ { type : "text" , text : "assistant replay text" } ] ) ;
204+ } ) ;
205+
123206 it ( "replays string tool-result JSONL content as Anthropic tool text" , async ( ) => {
124207 const dir = await makeTempDir ( ) ;
125208 const sessionFile = path . join ( dir , "session.jsonl" ) ;
0 commit comments