fix: handle bedrock exceptions (#1483)

This commit is contained in:
brook hong
2025-03-04 23:47:28 +08:00
committed by GitHub
parent ab63b52ffb
commit e1d2d825e2
2 changed files with 11 additions and 1 deletions

View File

@@ -357,7 +357,7 @@ function M._stream(opts)
end
-- If stream is not enabled, then handle the response here
if spec.body.stream == false and result.status == 200 then
if (spec.body.stream == nil or spec.body.stream == false) and result.status == 200 then
vim.schedule(function()
completed = true
parse_response_without_stream(result.body)

View File

@@ -41,6 +41,16 @@ function M.parse_stream_data(ctx, data, opts)
end
end
function M.parse_response_without_stream(data, event_state, opts)
local bedrock_match = data:gmatch("exception(%b{})")
opts.on_chunk("\n**Exception caught**\n\n")
for bedrock_data_match in bedrock_match do
local jsn = vim.json.decode(bedrock_data_match)
opts.on_chunk("- " .. jsn.message .. "\n")
end
vim.schedule(function() opts.on_stop({ reason = "complete" }) end)
end
---@param provider AvanteBedrockProviderFunctor
---@param prompt_opts AvantePromptOptions
---@return table