From 875643ce5bc4d24d923ed4adab8bf9a50f03a99a Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Wed, 6 Mar 2024 11:41:23 +0100 Subject: [PATCH] feat: Add support for proxy and allow_insecure and update migration Signed-off-by: Tomas Slusny --- MIGRATION.md | 5 ++--- README.md | 2 ++ lua/CopilotChat/config.lua | 4 ++++ lua/CopilotChat/copilot.lua | 25 +++++++++++++++++++++---- lua/CopilotChat/init.lua | 2 +- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index c50016a3..37b11a7f 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -19,7 +19,6 @@ Removed or changed params that you pass to `setup`: - `show_help` was removed (the help is now always shown as virtual text, and not intrusive) - `disable_extra_info` was renamed to `show_user_selection` - `hide_system_prompt` was renamed to `show_system_prompt` -- `proxy` does not work at the moment (waiting for change in plenary.nvim), if you are behind corporate proxy you can look at something like [vpn-slice](https://github.com/dlenski/vpn-slice) - `language` was removed and is now part of `selection` as `selection.filetype` ## Command changes @@ -68,12 +67,12 @@ For further reference, you can view @jellydn's [configuration](https://github.co ## TODO -- [ ] For proxy support, this is needed: https://github.com/nvim-lua/plenary.nvim/pull/559 +- [x] For proxy support, this is needed: https://github.com/nvim-lua/plenary.nvim/pull/559 - [ ] Delete rest of the python code? Or finish rewriting in place then delete - [x] Check for curl availability with health check - [x] Add folds logic from python, maybe? Not sure if this is even needed - [ ] Finish rewriting the authentication request if needed or just keep relying on copilot.vim/lua - [x] Properly get token file path, atm it only supports Linux (easy fix) - [x] Update README and stuff -- [ ] Add token count from tiktoken support to extra_info +- [x] Add token count from tiktoken support to extra_info - [x] Add test and fix failed test in CI diff --git a/README.md b/README.md index fc8dae2e..d50d89c0 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,8 @@ Also see [here](/lua/CopilotChat/config.lua): model = 'gpt-4', -- GPT model to use temperature = 0.1, -- GPT temperature context = 'manual', -- Context to use, 'buffers', 'buffer' or 'manual' + proxy = nil, -- [protocol://]host[:port] Use this proxy + allow_insecure = false, -- Allow insecure server connections debug = false, -- Enable debug logging show_user_selection = true, -- Shows user selection in chat show_system_prompt = false, -- Shows system prompt in chat diff --git a/lua/CopilotChat/config.lua b/lua/CopilotChat/config.lua index 9cd9b76f..a3b1fb32 100644 --- a/lua/CopilotChat/config.lua +++ b/lua/CopilotChat/config.lua @@ -47,6 +47,8 @@ local select = require('CopilotChat.select') ---@field model string? ---@field temperature number? ---@field context string? +---@field proxy string? +---@field allow_insecure boolean? ---@field debug boolean? ---@field show_user_selection boolean? ---@field show_system_prompt boolean? @@ -64,6 +66,8 @@ return { model = 'gpt-4', -- GPT model to use, 'gpt-3.5-turbo' or 'gpt-4' temperature = 0.1, -- GPT temperature context = 'manual', -- Context to use, 'buffers', 'buffer' or 'manual' + proxy = nil, -- [protocol://]host[:port] Use this proxy + allow_insecure = false, -- Allow insecure server connections debug = false, -- Enable debug logging show_user_selection = true, -- Shows user selection in chat show_system_prompt = false, -- Shows system prompt in chat diff --git a/lua/CopilotChat/copilot.lua b/lua/CopilotChat/copilot.lua index 3d7c43d6..ef3eb198 100644 --- a/lua/CopilotChat/copilot.lua +++ b/lua/CopilotChat/copilot.lua @@ -219,7 +219,7 @@ local function generate_headers(token, sessionid, machineid) } end -local function authenticate(github_token) +local function authenticate(github_token, proxy, allow_insecure) local url = 'https://api.github.com/copilot_internal/v2/token' local headers = { authorization = 'token ' .. github_token, @@ -229,7 +229,11 @@ local function authenticate(github_token) ['user-agent'] = 'GitHubCopilotChat/0.12.2023120701', } - local response = curl.get(url, { headers = headers }) + local response = curl.get(url, { + headers = headers, + proxy = proxy, + insecure = allow_insecure, + }) if response.status ~= 200 then return nil, response.status @@ -239,7 +243,9 @@ local function authenticate(github_token) return token, nil end -local Copilot = class(function(self) +local Copilot = class(function(self, proxy, allow_insecure) + self.proxy = proxy + self.allow_insecure = allow_insecure self.github_token = get_cached_token() self.history = {} self.token = nil @@ -265,7 +271,7 @@ function Copilot:check_auth(on_error) not self.token or (self.token.expires_at and self.token.expires_at <= math.floor(os.time())) then local sessionid = uuid() .. tostring(math.floor(os.time() * 1000)) - local token, err = authenticate(self.github_token) + local token, err = authenticate(self.github_token, self.proxy, self.allow_insecure) if err then local msg = 'Failed to authenticate: ' .. tostring(err) log.error(msg) @@ -366,6 +372,15 @@ function Copilot:ask(prompt, opts) .post(url, { headers = headers, body = body, + proxy = self.proxy, + insecure = self.allow_insecure, + on_error = function(err) + err = vim.inspect(err) + log.error('Failed to get response: ' .. err) + if on_error then + on_error(err) + end + end, stream = function(err, line) if err then log.error('Failed to stream response: ' .. tostring(err)) @@ -471,6 +486,8 @@ function Copilot:embed(inputs, opts) curl.post(url, { headers = headers, body = body, + proxy = self.proxy, + insecure = self.allow_insecure, on_error = function(err) err = vim.inspect(err) log.error('Failed to get response: ' .. err) diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index bd3e00ab..9627763a 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -402,7 +402,7 @@ end ---@param config CopilotChat.config|nil function M.setup(config) M.config = vim.tbl_deep_extend('force', default_config, config or {}) - state.copilot = Copilot() + state.copilot = Copilot(M.config.proxy, M.config.allow_insecure) state.chat = Chat(plugin_name, function(bufnr) if M.config.mappings.complete then vim.keymap.set('i', M.config.mappings.complete, complete, { buffer = bufnr })