Skip to content

Commit

Permalink
refactor: clean up client
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Feb 29, 2024
1 parent 5c534ba commit ceb7b25
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions lua/codecompanion/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,6 @@ local function close_request(bufnr, opts)
fire_autocmd("finished")
end

---@class CodeCompanion.Client
---@field secret_key string
---@field organization nil|string
---@field settings nil|table
local Client = {}

---@class CodeCompanion.ClientArgs
---@field secret_key string
---@field organization nil|string
---@field settings nil|table

---@param args CodeCompanion.ClientArgs
---@return CodeCompanion.Client
function Client.new(args)
return setmetatable({
secret_key = args.secret_key,
organization = args.organization,
settings = args.settings or schema.get_default(schema.static.client_settings, args.settings),
}, { __index = Client })
end

---@param client CodeCompanion.Client
---@return table<string, string>
local function headers(client)
Expand Down Expand Up @@ -92,11 +71,32 @@ local function parse_response(code, stdout)
return nil, data
end

---@class CodeCompanion.Client
---@field secret_key string
---@field organization nil|string
---@field settings nil|table
local Client = {}

---@class CodeCompanion.ClientArgs
---@field secret_key string
---@field organization nil|string
---@field settings nil|table

---@param args CodeCompanion.ClientArgs
---@return CodeCompanion.Client
function Client.new(args)
return setmetatable({
secret_key = args.secret_key,
organization = args.organization,
settings = args.settings or schema.get_default(schema.static.client_settings, args.settings),
}, { __index = Client })
end

---Call the OpenAI API but block the main loop until the response is received
---@param url string
---@param payload table
---@param cb fun(err: nil|string, response: nil|table)
function Client:call(url, payload, cb)
function Client:block_request(url, payload, cb)
cb = log:wrap_cb(cb, "Response error: %s")

local cmd = {
Expand Down Expand Up @@ -139,7 +139,7 @@ end
---@param bufnr number
---@param cb fun(err: nil|string, chunk: nil|table, done: nil|boolean) Will be called multiple times until done is true
---@return nil
function Client:stream_call(url, payload, bufnr, cb)
function Client:stream_request(url, payload, bufnr, cb)
cb = log:wrap_cb(cb, "Response error: %s")

local handler = self.settings.request({
Expand Down Expand Up @@ -220,7 +220,7 @@ end
---@param cb fun(err: nil|string, response: nil|table)
---@return nil
function Client:chat(args, cb)
return self:call(config.options.base_url .. "/v1/chat/completions", args, cb)
return self:block_request(config.options.base_url .. "/v1/chat/completions", args, cb)
end

---@param args CodeCompanion.ChatArgs
Expand All @@ -229,7 +229,7 @@ end
---@return nil
function Client:stream_chat(args, bufnr, cb)
args.stream = true
return self:stream_call(config.options.base_url .. "/v1/chat/completions", args, bufnr, cb)
return self:stream_request(config.options.base_url .. "/v1/chat/completions", args, bufnr, cb)
end

---@class args CodeCompanion.InlineArgs
Expand All @@ -238,7 +238,7 @@ end
---@return nil
function Client:inline(args, bufnr, cb)
args.stream = true
return self:stream_call(config.options.base_url .. "/v1/chat/completions", args, bufnr, cb)
return self:stream_request(config.options.base_url .. "/v1/chat/completions", args, bufnr, cb)
end

return Client

0 comments on commit ceb7b25

Please sign in to comment.