@@ -49,4 +49,52 @@ describe("convertMessages assistant text replay", () => {
4949 const replayed = converted . find ( ( message ) => message . role === "assistant" ) ;
5050 expect ( replayed ?. content ) . toBe ( "Let me check the file.\nThe file contains X." ) ;
5151 } ) ;
52+
53+ it ( "keeps paired OpenAI tool call ids UTF-16 safe when truncating" , ( ) => {
54+ const prefix = "a" . repeat ( 39 ) ;
55+ const oversizedId = `${ prefix } 🐱` ;
56+ const targetModel : Model < "openai-completions" > = {
57+ ...model ,
58+ id : "target-model" ,
59+ provider : "openai" ,
60+ } ;
61+ const assistant : AssistantMessage = {
62+ role : "assistant" ,
63+ api : targetModel . api ,
64+ provider : targetModel . provider ,
65+ model : "source-model" ,
66+ content : [ { type : "toolCall" , id : oversizedId , name : "lookup" , arguments : { } } ] ,
67+ usage : emptyUsage ,
68+ stopReason : "toolUse" ,
69+ timestamp : 1 ,
70+ } ;
71+ const context : Context = {
72+ messages : [
73+ assistant ,
74+ {
75+ role : "toolResult" ,
76+ toolCallId : oversizedId ,
77+ toolName : "lookup" ,
78+ content : [ { type : "text" , text : "ok" } ] ,
79+ isError : false ,
80+ timestamp : 2 ,
81+ } ,
82+ ] ,
83+ } ;
84+
85+ const converted = convertMessages (
86+ targetModel ,
87+ context ,
88+ resolveOpenAICompletionsCompat ( targetModel ) ,
89+ ) ;
90+ const assistantParam = converted . find ( ( message ) => message . role === "assistant" ) ;
91+ const toolParam = converted . find ( ( message ) => message . role === "tool" ) ;
92+ const normalizedAssistantId =
93+ assistantParam ?. role === "assistant" ? assistantParam . tool_calls ?. [ 0 ] ?. id : undefined ;
94+ const normalizedToolResultId = toolParam ?. role === "tool" ? toolParam . tool_call_id : undefined ;
95+
96+ expect ( oversizedId . slice ( 0 , 40 ) . charCodeAt ( 39 ) ) . toBe ( 0xd83d ) ;
97+ expect ( normalizedAssistantId ) . toBe ( prefix ) ;
98+ expect ( normalizedToolResultId ) . toBe ( prefix ) ;
99+ } ) ;
52100} ) ;
0 commit comments