@@ -3,6 +3,7 @@ import { Anthropic } from "@anthropic-ai/sdk"
33
44import { LiteLLMHandler } from "../lite-llm"
55import { getModels } from "../fetchers/modelCache"
6+ import { sanitizeToolUseId } from "../../../utils/tool-id"
67import type { ApiHandlerCreateMessageMetadata } from "../../index"
78import { ApiHandlerOptions } from "../../../shared/api"
89import { type ModelRecord , litellmDefaultModelId , litellmDefaultModelInfo } from "@roo-code/types"
@@ -760,46 +761,60 @@ describe("LiteLLMHandler", () => {
760761 expect ( assistantMessage . tool_calls [ 0 ] . provider_specific_fields . thought_signature ) . toBe ( dummySignature )
761762 } )
762763
763- it ( "should send tool call ids that carry a thought signature unchanged" , async ( ) => {
764- // LiteLLM encodes the Gemini signature into the id; truncating it breaks the signature.
765- const signature = "AY89a18ErUKBAfkTYnDbzq6mGnRG5QS8VFogncpDzYVXzC0i+aDwY5o/0123456789abcdef=="
766- const toolCallId = `call_864944__thought__${ signature } `
767- expect ( toolCallId . length ) . toBeGreaterThan ( 64 )
768-
769- const messages : Anthropic . Messages . MessageParam [ ] = [
770- { role : "user" , content : "Hello" } ,
771- {
772- role : "assistant" ,
773- content : [ { type : "tool_use" , id : toolCallId , name : "read_file" , input : { path : "test.txt" } } ] ,
774- } ,
775- {
776- role : "user" ,
777- content : [ { type : "tool_result" , tool_use_id : toolCallId , content : "file contents" } ] ,
778- } ,
779- ]
780- mockCreate . mockReturnValue ( {
781- withResponse : vi . fn ( ) . mockResolvedValue ( {
782- data : asyncStreamFrom ( [ { choices : [ { delta : { content : "ok" } } ] } ] ) ,
783- } ) ,
784- } )
785- const metadata : ApiHandlerCreateMessageMetadata = {
786- taskId : "task-1" ,
787- tools : [
764+ it . each ( [
765+ { path : "convertToOpenAiMessages" , preserveReasoning : undefined } ,
766+ { path : "convertToR1Format" , preserveReasoning : true } ,
767+ ] ) (
768+ "should send tool call ids that carry a thought signature unchanged ($path)" ,
769+ async ( { preserveReasoning } ) => {
770+ const signature = "AY89a18ErUKBAfkTYnDbzq6mGnRG5QS8VFogncpDzYVXzC0i+aDwY5o/0123456789abcdef=="
771+ const toolCallId = `call_864944__thought__${ signature } `
772+ const historyId = sanitizeToolUseId ( toolCallId )
773+ expect ( toolCallId . length ) . toBeGreaterThan ( 64 )
774+
775+ vi . spyOn ( handler , "fetchModel" ) . mockResolvedValue ( {
776+ id : "gemini-3-pro" ,
777+ info : { ...litellmDefaultModelInfo , preserveReasoning } ,
778+ } )
779+ const messages : Anthropic . Messages . MessageParam [ ] = [
780+ { role : "user" , content : "Hello" } ,
788781 {
789- type : "function" ,
790- function : { name : "read_file" , description : "Read a file" , parameters : { } } ,
782+ role : "assistant" ,
783+ content : [
784+ { type : "tool_use" , id : historyId , name : "read_file" , input : { path : "test.txt" } } ,
785+ ] ,
791786 } ,
792- ] ,
793- }
787+ {
788+ role : "user" ,
789+ content : [ { type : "tool_result" , tool_use_id : historyId , content : "file contents" } ] ,
790+ } ,
791+ ]
792+ mockCreate . mockReturnValue ( {
793+ withResponse : vi . fn ( ) . mockResolvedValue ( {
794+ data : asyncStreamFrom ( [ { choices : [ { delta : { content : "ok" } } ] } ] ) ,
795+ } ) ,
796+ } )
797+ const metadata : ApiHandlerCreateMessageMetadata = {
798+ taskId : "task-1" ,
799+ tools : [
800+ {
801+ type : "function" ,
802+ function : { name : "read_file" , description : "Read a file" , parameters : { } } ,
803+ } ,
804+ ] ,
805+ }
794806
795- await collectStream ( handler . createMessage ( "You are a helpful assistant" , messages , metadata ) )
807+ await collectStream ( handler . createMessage ( "You are a helpful assistant" , messages , metadata ) )
796808
797- const sent : OpenAI . Chat . ChatCompletionMessageParam [ ] = mockCreate . mock . calls [ 0 ] [ 0 ] . messages
798- const assistantMessage = sent . find ( ( msg ) => msg . role === "assistant" && msg . tool_calls ?. length )
799- const toolMessage = sent . find ( ( msg ) => msg . role === "tool" )
800- expect ( assistantMessage ?. role === "assistant" && assistantMessage . tool_calls ?. [ 0 ] . id ) . toBe ( toolCallId )
801- expect ( toolMessage ?. role === "tool" && toolMessage . tool_call_id ) . toBe ( toolCallId )
802- } )
809+ const sent : OpenAI . Chat . ChatCompletionMessageParam [ ] = mockCreate . mock . calls [ 0 ] [ 0 ] . messages
810+ const assistantMessage = sent . find ( ( msg ) => msg . role === "assistant" && msg . tool_calls ?. length )
811+ const toolMessage = sent . find ( ( msg ) => msg . role === "tool" )
812+ expect ( assistantMessage ?. role === "assistant" && assistantMessage . tool_calls ?. [ 0 ] . id ) . toBe (
813+ toolCallId ,
814+ )
815+ expect ( toolMessage ?. role === "tool" && toolMessage . tool_call_id ) . toBe ( toolCallId )
816+ } ,
817+ )
803818
804819 it ( "should not inject thought signatures for non-Gemini models" , async ( ) => {
805820 const optionsWithGPT4 : ApiHandlerOptions = {
0 commit comments