Skip to content

Commit 1ec27e3

Browse files
committed
better implementation
1 parent 61811b8 commit 1ec27e3

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

lua/codecompanion/adapters/http/mistral.lua

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)