@@ -114,9 +114,18 @@ export function convertToVsCodeLmMessages(
114114 { nonToolMessages : [ ] , toolMessages : [ ] } ,
115115 )
116116
117- // Process tool messages first then non-tool messages
117+ // Process non-tool messages first, then tool messages
118+ // Tool calls must come at the end so they are properly followed by user message with tool results
118119 const contentParts = [
119- // Convert tool messages to ToolCallParts first
120+ // Convert non-tool messages to TextParts first
121+ ...nonToolMessages . map ( ( part ) => {
122+ if ( part . type === "image" ) {
123+ return new vscode . LanguageModelTextPart ( "[Image generation not supported by VSCode LM API]" )
124+ }
125+ return new vscode . LanguageModelTextPart ( part . text )
126+ } ) ,
127+
128+ // Convert tool messages to ToolCallParts after text
120129 ...toolMessages . map (
121130 ( toolMessage ) =>
122131 new vscode . LanguageModelToolCallPart (
@@ -125,14 +134,6 @@ export function convertToVsCodeLmMessages(
125134 asObjectSafe ( toolMessage . input ) ,
126135 ) ,
127136 ) ,
128-
129- // Convert non-tool messages to TextParts after tool messages
130- ...nonToolMessages . map ( ( part ) => {
131- if ( part . type === "image" ) {
132- return new vscode . LanguageModelTextPart ( "[Image generation not supported by VSCode LM API]" )
133- }
134- return new vscode . LanguageModelTextPart ( part . text )
135- } ) ,
136137 ]
137138
138139 // Add the assistant message to the list of messages
0 commit comments