Skip to content

Commit d23a3ca

Browse files
committed
fix: update token handling and message ordering
Move last_message assignment before references check to ensure proper state tracking. Improve token usage and finish reason handling by adding fallback paths for different response formats from the Copilot API.
1 parent b87b054 commit d23a3ca

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

lua/CopilotChat/client.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,14 +535,14 @@ function Client:ask(prompt, opts)
535535
end
536536

537537
local out = provider.prepare_output(content, options)
538+
last_message = out
539+
538540
if out.references then
539541
for _, reference in ipairs(out.references) do
540542
table.insert(references, reference)
541543
end
542544
end
543545

544-
last_message = out
545-
546546
if out.content then
547547
full_response = full_response .. out.content
548548
on_progress(out.content)

lua/CopilotChat/config/providers.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,18 @@ M.copilot = {
244244
local content = message.message and message.message.content
245245
or message.delta and message.delta.content
246246

247+
local usage = message.usage and message.usage.total_tokens
248+
or output.usage and output.usage.total_tokens
249+
250+
local finish_reason = message.finish_reason
251+
or message.done_reason
252+
or output.finish_reason
253+
or output.done_reason
254+
247255
return {
248256
content = content,
249-
finish_reason = message.finish_reason or message.done_reason,
250-
total_tokens = message.usage and message.usage.total_tokens,
257+
finish_reason = finish_reason,
258+
total_tokens = usage,
251259
references = references,
252260
}
253261
end,

0 commit comments

Comments
 (0)