Repository: jackMort/ChatGPT.nvim
File: lua/chatgpt/api.lua (Lua)

Code before:
end

local function loadConfigFromCommand(command, configName, optionName)
  local cmd = splitCommandIntoTable(command)
  job
    :new({
      command = cmd[1],
      args = vim.list_slice(cmd, 2, #cmd),
      on_exit = function(j, exit_code)
        if exit_code ~= 0 then
          logger.warn("Config '" .. optionName .. "' did not return a value when executed")
          return
        end
        Api[configName] = j:result()[1]:gsub("%s+$", "")
      end,
    })
    :start()
end

local function loadConfigFromEnv(envName, configName)
  Api[configName] = os.getenv(envName)
  if not Api[configName] then
    logger.warn(envName .. " environment variable not set")
    return
  end
  Api[configName] = Api[configName]:gsub("%s+$", "")
end

local function loadApiConfig(envName, configName, optionName, defaultValue)
  loadConfigFromEnv(envName, configName)
  if not Api[configName] then
    if Config.options[optionName] ~= nil and Config.options[optionName] ~= "" then
      loadConfigFromCommand(Config.options[optionName], configName, optionName)
    else
      logger.warn(envName .. " environment variable not set")
      return
    end
  end
  if Api[configName] ~= nil and Api[configName] ~= "" then
    Api[configName] = Api[configName]:gsub("%s+$", "")
  elseif defaultValue ~= nil and defaultValue ~= "" then
    Api[configName] = defaultValue
  end
end

function Api.setup()
  loadApiConfig("OPENAI_API_HOST", "OPENAI_API_HOST", "api_host_cmd", "api.openai.com")
  Api.COMPLETIONS_URL = "https://" .. Api.OPENAI_API_HOST .. "/v1/completions"
  Api.CHAT_COMPLETIONS_URL = "https://" .. Api.OPENAI_API_HOST .. "/v1/chat/completions"
  Api.EDITS_URL = "https://" .. Api.OPENAI_API_HOST .. "/v1/edits"


Review comment (suggestion):
`  local variable = os.getenv(envName)
  if not variable then
    return
  end
  Api[configName] = variable:gsub("%s+$", "")`

Code after:
end

local function loadConfigFromCommand(command, optionName, callback, defaultValue)
  local cmd = splitCommandIntoTable(command)
  job
    :new({
      command = cmd[1],
      args = vim.list_slice(cmd, 2, #cmd),
      on_exit = function(j, exit_code)
        if exit_code ~= 0 then
          logger.warn("Config '" .. optionName .. "' did not return a value when executed")
          return
        end
        local value = j:result()[1]:gsub("%s+$", "")
        if value ~= nil and value ~= "" then
          callback(value)
        elseif defaultValue ~= nil and defaultValue ~= "" then
          callback(defaultValue)
        end
      end,
    })
    :start()
end

local function loadConfigFromEnv(envName, configName)
  local variable = os.getenv(envName)
  if not variable then
    return
  end
  Api[configName] = variable:gsub("%s+$", "")
end

local function loadApiConfig(envName, configName, optionName, callback, defaultValue)
  loadConfigFromEnv(envName, configName)
  if not Api[configName] then
    if Config.options[optionName] ~= nil and Config.options[optionName] ~= "" then
      loadConfigFromCommand(Config.options[optionName], optionName, callback, defaultValue)
    else
      logger.warn(envName .. " environment variable not set")
      return
    end
  end
end

function Api.setup()
  loadApiConfig("OPENAI_API_HOST", "OPENAI_API_HOST", "api_host_cmd", function(value)
    Api.OPENAI_API_HOST = value
    Api.COMPLETIONS_URL = "https://" .. Api.OPENAI_API_HOST .. "/v1/completions"
    Api.CHAT_COMPLETIONS_URL = "https://" .. Api.OPENAI_API_HOST .. "/v1/chat/completions"
    Api.EDITS_URL = "https://" .. Api.OPENAI_API_HOST .. "/v1/edits"
  end, "api.openai.com")