Skip to content

Commit

Permalink
fix: improve stream parsing and error handling
Browse files Browse the repository at this point in the history
Enhance the stream parsing logic by better handling finish_reason and error
conditions. Changes include:
- Add finish_reason check to properly close streams
- Improve data prefix trimming in stream messages
- Pass job context to parse_line for better error handling
- Add debug logging for stream completion
  • Loading branch information
deathbeam committed Dec 4, 2024
1 parent b82e90b commit 51dd370
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions lua/CopilotChat/copilot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,12 @@ function Copilot:ask(prompt, opts)
full_response = err
end

log.debug('Finishing stream', err)
finished = true
job:shutdown(0)
end

local function parse_line(line)
local function parse_line(line, job)
if not line then
return
end
Expand All @@ -624,7 +625,13 @@ function Copilot:ask(prompt, opts)
})

if not ok then
return content
if job then
finish_stream(
'Failed to parse response: ' .. utils.make_string(content) .. '\n' .. line,
job
)
end
return
end

if content.copilot_references then
Expand All @@ -649,6 +656,14 @@ function Copilot:ask(prompt, opts)

last_message = content
local choice = content.choices[1]

if choice.finish_reason then
if job then
finish_stream(nil, job)
end
return
end

content = choice.message and choice.message.content or choice.delta and choice.delta.content

if not content then
Expand All @@ -664,10 +679,11 @@ function Copilot:ask(prompt, opts)

local function parse_stream_line(line, job)
line = vim.trim(line)
if not vim.startswith(line, 'data: ') then
if not vim.startswith(line, 'data:') then
return
end
line = line:gsub('^data:%s*', '')
line = line:gsub('^data:', '')
line = vim.trim(line)

if line == '[DONE]' then
if job then
Expand All @@ -676,10 +692,7 @@ function Copilot:ask(prompt, opts)
return
end

local err = parse_line(line)
if err and job then
finish_stream('Failed to parse response: ' .. utils.make_string(err) .. '\n' .. line, job)
end
parse_line(line, job)
end

local function stream_func(err, line, job)
Expand Down

0 comments on commit 51dd370

Please sign in to comment.