@@ -19,6 +19,7 @@ export interface Turn {
1919 attachments ?: string [ ] ;
2020 encrypted ?: boolean ;
2121 hiddenType ?: string ;
22+ raw ?: unknown ;
2223}
2324
2425export function extractRequestTurns ( rawJson : string ) : Turn [ ] | null {
@@ -33,11 +34,11 @@ export function extractRequestTurns(rawJson: string): Turn[] | null {
3334
3435 const turns : Turn [ ] = [ ] ;
3536
36- appendInstructionTurn ( turns , "system" , o . system ) ;
37- appendInstructionTurn ( turns , "system" , o . instructions ) ;
38- appendInstructionTurn ( turns , "system" , o . systemInstruction ) ;
39- appendInstructionTurn ( turns , "system" , o . system_instruction ) ;
40- appendInstructionTurn ( turns , "developer" , o . developer ) ;
37+ appendInstructionTurn ( turns , "system" , "system" , o . system ) ;
38+ appendInstructionTurn ( turns , "system" , "instructions" , o . instructions ) ;
39+ appendInstructionTurn ( turns , "system" , "systemInstruction" , o . systemInstruction ) ;
40+ appendInstructionTurn ( turns , "system" , "system_instruction" , o . system_instruction ) ;
41+ appendInstructionTurn ( turns , "developer" , "developer" , o . developer ) ;
4142
4243 if ( Array . isArray ( o . messages ) ) {
4344 for ( const m of o . messages ) turns . push ( messageToTurn ( m ) ) ;
@@ -68,9 +69,9 @@ export function extractRequestToolDeclarations(rawJson: string): Turn | null {
6869 return toolDeclarationTurn ( o . tools ) ;
6970}
7071
71- function appendInstructionTurn ( turns : Turn [ ] , role : string , raw : unknown ) {
72+ function appendInstructionTurn ( turns : Turn [ ] , role : string , key : string , raw : unknown ) {
7273 const text = instructionToText ( raw ) ;
73- if ( text . trim ( ) ) turns . push ( { role, text } ) ;
74+ if ( text . trim ( ) ) turns . push ( { role, text, raw : { [ key ] : raw } } ) ;
7475}
7576
7677function instructionToText ( raw : unknown ) : string {
@@ -150,9 +151,9 @@ function messageToTurn(raw: unknown): Turn {
150151 }
151152 if ( ! text && attachments . length === 0 ) {
152153 const fallback = messageFallbackMarkdown ( m , type || "message" ) ;
153- if ( fallback ) return { role, text : fallback } ;
154+ if ( fallback ) return { role, text : fallback , raw } ;
154155 }
155- return { role, text, attachments : attachments . length ? attachments : undefined } ;
156+ return { role, text, attachments : attachments . length ? attachments : undefined , raw } ;
156157}
157158
158159function isImagePartType ( type : string ) : boolean {
@@ -203,6 +204,7 @@ function toolDeclarationTurn(raw: unknown): Turn | null {
203204 return {
204205 role : "tool" ,
205206 text : toolDeclarationsMarkdown ( raw ) ,
207+ raw,
206208 } ;
207209}
208210
@@ -404,6 +406,7 @@ function responsesInputToTurn(raw: unknown): Turn {
404406 return {
405407 role : "assistant" ,
406408 text : toolUseMarkdown ( name , m . call_id , json , "json" ) ,
409+ raw : m ,
407410 } ;
408411 }
409412 if ( type === "custom_tool_call" ) {
@@ -412,13 +415,15 @@ function responsesInputToTurn(raw: unknown): Turn {
412415 return {
413416 role : "assistant" ,
414417 text : toolUseMarkdown ( name , m . call_id , input , customToolLanguage ( name , input ) ) ,
418+ raw : m ,
415419 } ;
416420 }
417421 if ( type === "function_call_output" || type === "custom_tool_call_output" ) {
418422 const out = typeof m . output === "string" ? m . output : JSON . stringify ( m . output ?? "" , null , 2 ) ;
419423 return {
420424 role : "tool" ,
421425 text : toolResultMarkdown ( m . call_id , out ) ,
426+ raw : m ,
422427 } ;
423428 }
424429 if ( type === "reasoning" ) {
@@ -430,17 +435,18 @@ function responsesInputToTurn(raw: unknown): Turn {
430435 : "" ;
431436 if ( summary ) {
432437 const quoted = summary . split ( "\n" ) . map ( ( l ) => "> " + l ) . join ( "\n" ) ;
433- return { role : "assistant" , text : "_thinking_\n" + quoted } ;
438+ return { role : "assistant" , text : "_thinking_\n" + quoted , raw : m } ;
434439 }
435440 if ( hasEncryptedReasoning ( m ) ) {
436- return { role : "assistant" , text : hiddenTypeText ( type ) , encrypted : true , hiddenType : type } ;
441+ return { role : "assistant" , text : hiddenTypeText ( type ) , encrypted : true , hiddenType : type , raw : m } ;
437442 }
438- return { role : "assistant" , text : "" , attachments : [ "reasoning" ] } ;
443+ return { role : "assistant" , text : "" , attachments : [ "reasoning" ] , raw : m } ;
439444 }
440445 if ( type === "web_search_call" ) {
441446 return {
442447 role : "assistant" ,
443448 text : webSearchCallMarkdown ( m ) ,
449+ raw : m ,
444450 } ;
445451 }
446452 return typedItemToTurn ( m , type ) ;
@@ -450,6 +456,7 @@ function typedItemToTurn(item: Record<string, unknown>, type: string): Turn {
450456 return {
451457 role : type ,
452458 text : typedItemMarkdown ( item , type ) ,
459+ raw : item ,
453460 } ;
454461}
455462
@@ -570,7 +577,7 @@ function geminiContentToTurn(raw: unknown): Turn {
570577 else if ( o . functionResponse ) attachments . push ( "function_response" ) ;
571578 else attachments . push ( Object . keys ( o ) . join ( "," ) || "part" ) ;
572579 }
573- return { role, text, attachments : attachments . length ? attachments : undefined } ;
580+ return { role, text, attachments : attachments . length ? attachments : undefined , raw } ;
574581}
575582
576583
0 commit comments