Skip to content

Commit 7b9b42d

Browse files
committed
feat(adapters): support extra fields in OpenAI adapter
1 parent 534aa24 commit 7b9b42d

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

doc/configuration/adapters.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,40 @@ require("codecompanion").setup({
380380
}),
381381
```
382382

383+
## Setup: OpenRouter with Reasoning Output
384+
385+
```lua
386+
require("codecompanion").setup({
387+
adapters = {
388+
http = {
389+
openrouter = function()
390+
return require("codecompanion.adapters").extend("openai_compatible", {
391+
env = {
392+
url = "https://openrouter.ai/api",
393+
api_key = "OPENROUTER_API_KEY",
394+
chat_url = "/v1/chat/completions",
395+
},
396+
handlers = {
397+
parse_extra = function(self, data)
398+
local extra = data.extra
399+
if extra and extra.reasoning then
400+
data.output.reasoning = { content = extra.reasoning }
401+
data.output.content = nil
402+
end
403+
return data
404+
end,
405+
},
406+
})
407+
end,
408+
},
409+
},
410+
strategies = {
411+
chat = {
412+
adapter = "openrouter",
413+
},
414+
inline = {
415+
adapter = "openrouter",
416+
},
417+
},
418+
})
419+
```

lua/codecompanion/adapters/http/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ local function get_handler(adapter, name)
5050
parse_chat = "chat_output",
5151
parse_inline = "inline_output",
5252
parse_tokens = "tokens",
53+
parse_extra = "parse_extra",
5354

5455
-- tools
5556
format_calls = "format_tool_calls",

lua/codecompanion/adapters/http/openai.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
local adapter_utils = require("codecompanion.utils.adapters")
22
local log = require("codecompanion.utils.log")
3+
local CONSTANTS = {
4+
STANDARD_MESSAGE_FIELDS = {
5+
-- fields that are defined in the standard openai chat-completion API (inc. streaming and non-streaming)
6+
"content",
7+
"function_call",
8+
"refusal",
9+
"role",
10+
"tool_calls",
11+
"annotations",
12+
"audio",
13+
},
14+
}
15+
16+
---@param obj table?
17+
---@return table?
18+
local function find_extra_fields(obj)
19+
if obj == nil then
20+
return nil
21+
end
22+
local extra = {}
23+
vim.iter(obj):each(function(k, v)
24+
if not vim.list_contains(CONSTANTS.STANDARD_MESSAGE_FIELDS, k) then
25+
extra[k] = v
26+
end
27+
end)
28+
if not vim.tbl_isempty(extra) then
29+
return extra
30+
end
31+
end
332

433
---@class CodeCompanion.HTTPAdapter.OpenAI: CodeCompanion.HTTPAdapter
534
return {
@@ -257,6 +286,7 @@ return {
257286
role = delta.role,
258287
content = delta.content,
259288
},
289+
extra = find_extra_fields(delta),
260290
}
261291
end,
262292

lua/codecompanion/strategies/chat/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,12 @@ function Chat:_submit_http(payload)
10091009
end
10101010

10111011
local result = adapters.call_handler(adapter, "parse_chat", data, tools)
1012+
1013+
local parse_extra = adapters.get_handler(adapter, "parse_extra")
1014+
if result and result.extra and type(parse_extra) == "function" then
1015+
result = parse_extra(adapter, result)
1016+
end
1017+
10121018
if result and result.status then
10131019
self.status = result.status
10141020
if self.status == CONSTANTS.STATUS_SUCCESS then

0 commit comments

Comments
 (0)