mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-04 00:03:59 +00:00
fix(openai adapter): handle null response body and use type-safe field access
- Fix transform_response crash when json.decode returns nil (null body) - Replace truthy checks with type() == "table" for safer field access - Prevents 'attempt to index a non-table object(nil)' errors
This commit is contained in:
@ -21,7 +21,7 @@ end
|
|||||||
|
|
||||||
function adapter.transform_response(raw_body)
|
function adapter.transform_response(raw_body)
|
||||||
local ok, resp = pcall(json.decode, raw_body)
|
local ok, resp = pcall(json.decode, raw_body)
|
||||||
if not ok then return raw_body end
|
if not ok or resp == nil then return raw_body end
|
||||||
|
|
||||||
local unified = {
|
local unified = {
|
||||||
content = "",
|
content = "",
|
||||||
@ -29,20 +29,20 @@ function adapter.transform_response(raw_body)
|
|||||||
token_usage = { prompt = 0, completion = 0, total = 0 }
|
token_usage = { prompt = 0, completion = 0, total = 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.usage then
|
if type(resp.usage) == "table" then
|
||||||
unified.token_usage.prompt = resp.usage.prompt_tokens or 0
|
unified.token_usage.prompt = resp.usage.prompt_tokens or 0
|
||||||
unified.token_usage.completion = resp.usage.completion_tokens or 0
|
unified.token_usage.completion = resp.usage.completion_tokens or 0
|
||||||
unified.token_usage.total = resp.usage.total_tokens or 0
|
unified.token_usage.total = resp.usage.total_tokens or 0
|
||||||
end
|
end
|
||||||
|
|
||||||
if resp.choices and #resp.choices > 0 then
|
if type(resp.choices) == "table" and #resp.choices > 0 then
|
||||||
local ch = resp.choices[1]
|
local ch = resp.choices[1]
|
||||||
if ch.message then
|
if type(ch.message) == "table" then
|
||||||
unified.content = ch.message.content or ""
|
unified.content = ch.message.content or ""
|
||||||
if ch.message.reasoning_content then
|
if ch.message.reasoning_content then
|
||||||
unified.reasoning_content = ch.message.reasoning_content
|
unified.reasoning_content = ch.message.reasoning_content
|
||||||
end
|
end
|
||||||
if ch.message.tool_calls then
|
if type(ch.message.tool_calls) == "table" then
|
||||||
local tcs = {}
|
local tcs = {}
|
||||||
for _, tc in ipairs(ch.message.tool_calls) do
|
for _, tc in ipairs(ch.message.tool_calls) do
|
||||||
local args_ok, args = pcall(json.decode, tc["function"].arguments)
|
local args_ok, args = pcall(json.decode, tc["function"].arguments)
|
||||||
|
|||||||
Reference in New Issue
Block a user