@@ -12,7 +12,6 @@ export type MistralMessage =
1212
1313export function convertToMistralMessages ( anthropicMessages : Anthropic . Messages . MessageParam [ ] ) : MistralMessage [ ] {
1414 const mistralMessages : MistralMessage [ ] = [ ]
15-
1615 for ( const anthropicMessage of anthropicMessages ) {
1716 if ( typeof anthropicMessage . content === "string" ) {
1817 mistralMessages . push ( {
@@ -21,45 +20,15 @@ export function convertToMistralMessages(anthropicMessages: Anthropic.Messages.M
2120 } )
2221 } else {
2322 if ( anthropicMessage . role === "user" ) {
24- // Handle user messages with potential tool results
25- const { nonToolMessages, toolMessages } = anthropicMessage . content . reduce < {
26- nonToolMessages : ( Anthropic . TextBlockParam | Anthropic . ImageBlockParam ) [ ]
27- toolMessages : Anthropic . ToolResultBlockParam [ ]
28- } > (
29- ( acc , part ) => {
30- if ( part . type === "tool_result" ) {
31- acc . toolMessages . push ( part )
32- } else if ( part . type === "text" || part . type === "image" ) {
33- acc . nonToolMessages . push ( part )
34- }
35- return acc
36- } ,
37- { nonToolMessages : [ ] , toolMessages : [ ] } ,
23+ // Filter to only include text and image blocks
24+ const textAndImageBlocks = anthropicMessage . content . filter (
25+ ( part ) => part . type === "text" || part . type === "image" ,
3826 )
3927
40- // First add any tool results
41- for ( const toolMsg of toolMessages ) {
42- const content =
43- typeof toolMsg . content === "string"
44- ? toolMsg . content
45- : toolMsg . content ?. map ( ( c ) => ( c . type === "text" ? c . text : "" ) ) . join ( "\n" )
46-
47- if ( content ) {
48- mistralMessages . push ( {
49- role : "tool" ,
50- content : JSON . stringify ( {
51- tool_use_id : toolMsg . tool_use_id ,
52- content,
53- } ) ,
54- } )
55- }
56- }
57-
58- // Then add the user message if there are non-tool messages
59- if ( nonToolMessages . length > 0 ) {
28+ if ( textAndImageBlocks . length > 0 ) {
6029 mistralMessages . push ( {
6130 role : "user" ,
62- content : nonToolMessages . map ( ( part ) => {
31+ content : textAndImageBlocks . map ( ( part ) => {
6332 if ( part . type === "image" ) {
6433 return {
6534 type : "image_url" ,
@@ -73,58 +42,15 @@ export function convertToMistralMessages(anthropicMessages: Anthropic.Messages.M
7342 } )
7443 }
7544 } else if ( anthropicMessage . role === "assistant" ) {
76- // Handle assistant messages with potential tool uses
77- const { nonToolMessages, toolMessages } = anthropicMessage . content . reduce < {
78- nonToolMessages : ( Anthropic . TextBlockParam | Anthropic . ImageBlockParam ) [ ]
79- toolMessages : Anthropic . ToolUseBlockParam [ ]
80- } > (
81- ( acc , part ) => {
82- if ( part . type === "tool_use" ) {
83- acc . toolMessages . push ( part )
84- } else if ( part . type === "text" || part . type === "image" ) {
85- acc . nonToolMessages . push ( part )
86- }
87- return acc
88- } ,
89- { nonToolMessages : [ ] , toolMessages : [ ] } ,
90- )
91-
92- // Convert text content
93- let textContent = nonToolMessages
94- . map ( ( part ) => {
95- if ( part . type === "image" ) return ""
96- return part . text
97- } )
98- . filter ( Boolean )
99- . join ( "\n" )
45+ // Only process text blocks - assistant cannot send images or other content types in Mistral's API format
46+ const textBlocks = anthropicMessage . content . filter ( ( part ) => part . type === "text" )
10047
101- // Add tool uses as structured content
102- if ( toolMessages . length > 0 ) {
103- // If there's text content, add it first
104- if ( textContent ) {
105- mistralMessages . push ( {
106- role : "assistant" ,
107- content : textContent ,
108- } )
109- }
48+ if ( textBlocks . length > 0 ) {
49+ const content = textBlocks . map ( ( part ) => part . text ) . join ( "\n" )
11050
111- // Add each tool use as a separate message
112- for ( const toolMsg of toolMessages ) {
113- mistralMessages . push ( {
114- role : "assistant" ,
115- content : JSON . stringify ( {
116- type : "function" ,
117- name : toolMsg . name ,
118- id : toolMsg . id ,
119- input : toolMsg . input ,
120- } ) ,
121- } )
122- }
123- } else if ( textContent ) {
124- // If no tools but we have text, add it
12551 mistralMessages . push ( {
12652 role : "assistant" ,
127- content : textContent ,
53+ content,
12854 } )
12955 }
13056 }
0 commit comments