Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve stream parsing and error handling #671

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading