Skip to content

Commit fb6444b

Browse files
authored
fix(chat): prevent duplicate references
1 parent ad49fcf commit fb6444b

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

lua/codecompanion/adapters/ollama.lua

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,7 @@ return {
123123
local ok, json = pcall(vim.json.decode, data, { luanil = { object = true } })
124124

125125
if not ok then
126-
return {
127-
status = "error",
128-
output = string.format("Error malformed json: %s", json),
129-
}
126+
return { status = "error" }
130127
end
131128

132129
local message = json.message
@@ -136,8 +133,6 @@ return {
136133
output.role = message.role or nil
137134
end
138135

139-
-- log:trace("----- For Adapter test creation -----\nOutput: %s\n ---------- // END ----------", output)
140-
141136
return {
142137
status = "success",
143138
output = output,

lua/codecompanion/strategies/chat/init.lua

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -699,23 +699,22 @@ function Chat:submit(opts)
699699
end
700700

701701
local result = self.adapter.handlers.chat_output(self.adapter, data)
702-
if result and result.status == CONSTANTS.STATUS_SUCCESS then
703-
if result.output.role then
704-
result.output.role = config.constants.LLM_ROLE
702+
if result and result.status then
703+
self.status = result.status
704+
if self.status == CONSTANTS.STATUS_SUCCESS then
705+
if result.output.role then
706+
result.output.role = config.constants.LLM_ROLE
707+
end
708+
table.insert(output, result.output.content)
709+
self:add_buf_message(result.output)
705710
end
706-
self.status = CONSTANTS.STATUS_SUCCESS
707-
table.insert(output, result.output.content)
708-
self:add_buf_message(result.output)
709711
end
710712
end
711713
end,
712714
done = function()
713715
self:done(output)
714716
end,
715-
}, {
716-
bufnr = bufnr,
717-
strategy = "chat",
718-
})
717+
}, { bufnr = bufnr, strategy = "chat" })
719718
end
720719

721720
---Increment the cycle count in the chat buffer
@@ -738,6 +737,11 @@ end
738737
function Chat:done(output)
739738
self.current_request = nil
740739

740+
-- Commonly, a status may not be set if the message exceeds a token limit
741+
if not self.status or self.status == "" then
742+
return self:reset()
743+
end
744+
741745
if output and not vim.tbl_isempty(output) then
742746
self:add_message({
743747
role = config.constants.LLM_ROLE,
@@ -750,6 +754,7 @@ function Chat:done(output)
750754

751755
local assistant_range = self.header_line
752756
self:set_range(-2)
757+
753758
self.ui:display_tokens(self.parser, self.header_line)
754759
self.references:render()
755760

tests/strategies/chat/test_references.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,11 @@ T["References"]["Can be pinned"] = function()
150150
h.eq(#chat.messages, 5, "There are four messages")
151151
h.eq(chat.messages[#chat.messages].content, "Basic Slash Command")
152152

153-
chat:done({})
153+
chat.status = "success"
154+
chat:done({ content = "Some data" })
154155

155156
local buffer = h.get_buf_lines(chat.bufnr)
157+
156158
h.eq("> Sharing:", buffer[3])
157159
h.eq(string.format("> - %s<buf>pinned example</buf>", icon), buffer[8])
158160
h.eq("> - <buf>unpinned example</buf>", buffer[9])

0 commit comments

Comments
 (0)