Skip to content

Commit 61811b8

Browse files
committed
fix: Ensure we drop user after tool messages
1 parent d156050 commit 61811b8

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

lua/codecompanion/adapters/http/mistral.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ return {
6161
return openai.handlers.form_parameters(self, params, messages)
6262
end,
6363
form_messages = function(self, messages)
64+
local is_previous_tool = false
65+
local messages = vim.iter(messages):filter(function(msg)
66+
local is_tool = msg.role == "tool"
67+
local is_user = msg.role == "user"
68+
local keep = true
69+
-- 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
71+
if is_previous_tool and is_user then
72+
log:debug("Dropping user after tool message: %s", msg.content)
73+
keep = false
74+
end
75+
is_previous_tool = is_tool
76+
return keep
77+
end)
6478
return openai.handlers.form_messages(self, messages)
6579
end,
6680
chat_output = function(self, data, tools)

tests/adapters/http/test_mistral.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ T["Mistral adapter"]["form_messages"]["it can form messages with tools"] = funct
5050
},
5151
},
5252
},
53+
{ role = "tool", content = "ok" },
54+
{ role = "assistant", content = "that works" },
5355
}
5456

5557
local expected = {
@@ -83,6 +85,56 @@ T["Mistral adapter"]["form_messages"]["it can form messages with tools"] = funct
8385
},
8486
},
8587
},
88+
{ role = "tool", content = "ok" },
89+
{ role = "assistant", content = "that works" },
90+
},
91+
}
92+
93+
h.eq(expected, adapter.handlers.form_messages(adapter, input))
94+
end
95+
96+
T["Mistral adapter"]["form_messages"]["it skips messages when user follows tools"] = function()
97+
local input = {
98+
{ role = "user", content = "User1" },
99+
{
100+
role = "llm",
101+
tools = {
102+
calls = {
103+
{
104+
["function"] = {
105+
arguments = '{"location":"London, UK","units":"fahrenheit"}',
106+
name = "weather",
107+
},
108+
id = "call_1_a460d461-60a7-468c-a699-ef9e2dced125",
109+
type = "function",
110+
},
111+
},
112+
},
113+
},
114+
{ role = "tool", content = "ok" },
115+
{ role = "user", content = "my message" },
116+
}
117+
118+
local expected = {
119+
messages = {
120+
{
121+
content = "User1",
122+
role = "user",
123+
},
124+
{
125+
role = "llm",
126+
tool_calls = {
127+
{
128+
["function"] = {
129+
arguments = '{"location":"London, UK","units":"fahrenheit"}',
130+
name = "weather",
131+
},
132+
id = "call_1_a460d461-60a7-468c-a699-ef9e2dced125",
133+
type = "function",
134+
},
135+
},
136+
},
137+
{ role = "tool", content = "ok" },
86138
},
87139
}
88140

0 commit comments

Comments
 (0)