Skip to content

Commit b76c04e

Browse files
authored
refactor(adapters): schema->parameters is cleaned up (olimorris#777)
1 parent 2c85026 commit b76c04e

File tree

3 files changed

+6
-19
lines changed

3 files changed

+6
-19
lines changed

lua/codecompanion/adapters/init.lua

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,9 @@ function Adapter.new(args)
120120
return setmetatable(args, { __index = Adapter })
121121
end
122122

123-
---TODO: Refactor this to return self so we can chain it
124123
---Get the default settings from the schema
125124
---@return table
126-
function Adapter:get_default_settings()
125+
function Adapter:make_from_schema()
127126
local settings = {}
128127

129128
for key, value in pairs(self.schema) do
@@ -201,9 +200,7 @@ end
201200
---@param settings? table
202201
---@return CodeCompanion.Adapter
203202
function Adapter:map_schema_to_params(settings)
204-
if not settings then
205-
settings = self:get_default_settings()
206-
end
203+
settings = settings or self:make_from_schema()
207204

208205
for k, v in pairs(settings) do
209206
local mapping = self.schema[k] and self.schema[k].mapping

lua/codecompanion/adapters/ollama.lua

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,6 @@ return {
8080
---@param messages table
8181
---@return table
8282
form_parameters = function(self, params, messages)
83-
if type(params.model) == "function" then
84-
params.model = params.model(self)
85-
end
86-
vim.iter(params.options):each(function(k, v)
87-
if type(v) == "function" then
88-
params[k] = v(self)
89-
end
90-
end)
9183
return params
9284
end,
9385

lua/codecompanion/strategies/chat/init.lua

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ local function ts_parse_settings(bufnr, adapter)
4949
-- If the user has disabled settings in the chat buffer, use the default settings
5050
if not config.display.chat.show_settings then
5151
if adapter then
52-
_cached_settings[bufnr] = adapter:get_default_settings()
52+
_cached_settings[bufnr] = adapter:make_from_schema()
5353
return _cached_settings[bufnr]
5454
end
5555
end
@@ -65,7 +65,7 @@ local function ts_parse_settings(bufnr, adapter)
6565
local end_line = -1
6666
if adapter then
6767
-- Account for the two YAML lines and the fact Tree-sitter is 0-indexed
68-
end_line = vim.tbl_count(adapter:get_default_settings()) + 2 - 1
68+
end_line = vim.tbl_count(adapter:make_from_schema()) + 2 - 1
6969
end
7070

7171
for _, matches, _ in query:iter_matches(root, bufnr, 0, end_line) do
@@ -368,10 +368,8 @@ end
368368
---@param settings table
369369
---@return self
370370
function Chat:apply_settings(settings)
371-
self.settings = settings or schema.get_default(self.adapter.schema, self.opts.settings)
372-
373-
-- Reset the cache
374-
_cached_settings[self.bufnr] = self.settings
371+
_cached_settings[self.bufnr] = settings
372+
self.settings = settings
375373

376374
return self
377375
end

0 commit comments

Comments
 (0)