@@ -61,20 +61,33 @@ return {
6161 return openai .handlers .form_parameters (self , params , messages )
6262 end ,
6363 form_messages = function (self , messages )
64+ local pending_messages = self .pending_messages or {}
6465 local is_previous_tool = false
65- local messages = vim . iter (messages ): filter ( function ( msg )
66+ for k , msg in ipairs (messages ) do
6667 local is_tool = msg .role == " tool"
6768 local is_user = msg .role == " user"
68- local keep = true
6969 -- Mistral does not like user after tool messages, those should always be assistant
70- -- Worst case we drop 1 user message, but the assistant will respond to the tool
7170 if is_previous_tool and is_user then
72- log :debug (" Dropping user after tool message: %s" , msg .content )
73- keep = false
71+ table.insert (pending_messages , msg )
72+ messages [k ] = nil
73+ -- message was dropped, so for the next message, the previous one
74+ -- is still a tool
75+ is_tool = true
76+ else
77+ if not is_previous_tool then
78+ -- Flush pending messages whenever we can
79+ for i , m in ipairs (pending_messages ) do
80+ table.insert (messages , m )
81+ end
82+ pending_messages = {}
83+ end
7484 end
7585 is_previous_tool = is_tool
76- return keep
77- end )
86+ end
87+
88+ -- Keep the pending messages for next round
89+ self .pending_messages = pending_messages
90+
7891 return openai .handlers .form_messages (self , messages )
7992 end ,
8093 chat_output = function (self , data , tools )
0 commit comments