Skip to content

Commit 1fde20c

Browse files
pettniPetter Nilsson
authored andcommitted
fix(adapter): variable substitution in 'raw'.
Pass adapter.raw through adapter:set_env_vars() to enable variable substitution. Also appends adapter.raw to the static raw arguments defined in the http client.
1 parent 9c3e01a commit 1fde20c

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

lua/codecompanion/http.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,16 @@ function Client:request(payload, actions, opts)
101101
table.insert(raw, "--no-buffer")
102102
end
103103

104+
if adapter.raw then
105+
vim.list_extend(raw, adapter:set_env_vars(adapter.raw))
106+
end
107+
104108
local request_opts = {
105109
url = adapter:set_env_vars(adapter.url),
106110
headers = adapter:set_env_vars(adapter.headers),
107111
insecure = config.adapters.opts.allow_insecure,
108112
proxy = config.adapters.opts.proxy,
109-
raw = adapter.raw or raw,
113+
raw = raw,
110114
body = body_file.filename or "",
111115
-- This is called when the request is finished. It will only ever be called
112116
-- once, even if the endpoint is streaming.

tests/test_http.lua

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
local h = require("tests.helpers")
2-
31
local assert = require("luassert")
4-
local codecompanion = require("codecompanion")
52
local stub = require("luassert.stub")
63

74
local Client
85

96
local adapter = {
107
name = "TestAdapter",
11-
url = "https://api.openai.com/v1/chat/completions",
8+
url = "https://api.openai.com/v1/${url_value}/completions",
9+
env = {
10+
url_value = "chat",
11+
header_value = "json",
12+
raw_value = "RAW_VALUE",
13+
},
1214
headers = {
13-
content_type = "application/json",
15+
content_type = "application/${header_value}",
1416
},
1517
parameters = {
1618
stream = true,
1719
},
20+
raw = {
21+
"--arg1-${raw_value}",
22+
"--arg2-${raw_value}",
23+
},
1824
handlers = {
1925
form_parameters = function()
2026
return {}
@@ -65,4 +71,31 @@ describe("Client", function()
6571

6672
assert.stub(mock_request).was_called(1)
6773
end)
74+
75+
it("substitutes variables", function()
76+
local mock_request = stub.new().returns(nil)
77+
78+
Client.static.opts = {
79+
post = { default = mock_request },
80+
encode = { default = stub.new().returns("{}") },
81+
}
82+
83+
adapter = require("codecompanion.adapters").new(adapter)
84+
85+
Client.new({ adapter = adapter }):request({}, { callback = stub.new() })
86+
87+
assert.stub(mock_request).was_called(1)
88+
local request_args = mock_request.calls[1].refs[1]
89+
90+
-- can substitute in 'address'
91+
assert.equal(request_args.url, "https://api.openai.com/v1/chat/completions")
92+
93+
-- can substitute in 'headers'
94+
assert.equal(request_args.headers.content_type, "application/json")
95+
96+
-- can substitute in 'raw'
97+
local raw_args = request_args.raw
98+
assert.equal(raw_args[#raw_args - 1], "--arg1-RAW_VALUE")
99+
assert.equal(raw_args[#raw_args], "--arg2-RAW_VALUE")
100+
end)
68101
end)

0 commit comments

Comments
 (0)