Skip to content

Commit 4d06669

Browse files
committed
chore: wip backup
1 parent 5512e89 commit 4d06669

File tree

2 files changed

+93
-14
lines changed

2 files changed

+93
-14
lines changed

lua/gp/config.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ local config = {
3838
-- secret = os.getenv("AZURE_API_KEY"),
3939
},
4040
copilot = {
41-
endpoint = "https://copilot-proxy.githubusercontent.com/v1/chat/completions",
41+
endpoint = "https://api.githubcopilot.com/chat/completions",
4242
secret = {
4343
"bash",
4444
"-c",
45-
"cat ~/.config/github-copilot/hosts.json | sed -e 's/.*oauth_token...//;s/\".*//",
45+
"cat ~/.config/github-copilot/hosts.json | sed -e 's/.*oauth_token...//;s/\".*//'",
4646
},
4747
},
4848
ollama = {

lua/gp/init.lua

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,51 @@ M.append_selection = function(params, origin_buf, target_buf)
655655
vim.api.nvim_buf_set_lines(target_buf, last_content_line, -1, false, lines)
656656
end
657657

658+
function M.refresh_copilot_bearer()
659+
if not M.providers.copilot or not M.providers.copilot.secret then
660+
return
661+
end
662+
local secret = M.providers.copilot.secret
663+
664+
local bearer = M._state.copilot_bearer or {}
665+
if bearer.token and bearer.expires_at and bearer.expires_at > os.time() then
666+
return
667+
end
668+
669+
local curl_params = vim.deepcopy(M.config.curl_params or {})
670+
local args = {
671+
"-s",
672+
"-v",
673+
"https://api.github.com/copilot_internal/v2/token",
674+
"-H",
675+
"Content-Type: application/json",
676+
"-H",
677+
"accept: */*",
678+
"-H",
679+
"authorization: token " .. secret,
680+
"-H",
681+
"editor-version: vscode/1.85.1",
682+
"-H",
683+
"editor-plugin-version: copilot-chat/0.12.2023120701",
684+
"-H",
685+
"user-agent: GitHubCopilotChat/0.12.2023120701",
686+
}
687+
688+
for _, arg in ipairs(args) do
689+
table.insert(curl_params, arg)
690+
end
691+
692+
M._H.process(nil, "curl", curl_params, function(code, signal, stdout, stderr)
693+
if code ~= 0 then
694+
M.error(string.format("Copilot bearer resolve exited: %d, %d", code, signal))
695+
return
696+
end
697+
698+
M._state.copilot_bearer = vim.json.decode(stdout)
699+
M.refresh_state()
700+
end, nil, nil)
701+
end
702+
658703
-- setup function
659704
M._setup_called = false
660705
---@param opts table | nil # table with options
@@ -674,12 +719,12 @@ M.setup = function(opts)
674719
M.config = vim.deepcopy(config)
675720

676721
-- merge nested tables
677-
local mergeTables = { "hooks", "agents", "image_agents" }
722+
local mergeTables = { "hooks", "agents", "image_agents", "providers" }
678723
for _, tbl in ipairs(mergeTables) do
679724
M[tbl] = M[tbl] or {}
680725
---@diagnostic disable-next-line: param-type-mismatch
681726
for k, v in pairs(M.config[tbl]) do
682-
if tbl == "hooks" then
727+
if tbl == "hooks" or tbl == "providers" then
683728
M[tbl][k] = v
684729
elseif tbl == "agents" or tbl == "image_agents" then
685730
M[tbl][v.name] = v
@@ -689,7 +734,7 @@ M.setup = function(opts)
689734

690735
opts[tbl] = opts[tbl] or {}
691736
for k, v in pairs(opts[tbl]) do
692-
if tbl == "hooks" then
737+
if tbl == "hooks" or tbl == "providers" then
693738
M[tbl][k] = v
694739
elseif tbl == "agents" or tbl == "image_agents" then
695740
M[tbl][v.name] = v
@@ -753,6 +798,13 @@ M.setup = function(opts)
753798
end
754799
end
755800

801+
-- remove invalid providers
802+
for name, provider in pairs(M.providers) do
803+
if type(provider) ~= "table" or not provider.endpoint then
804+
M.providers[name] = nil
805+
end
806+
end
807+
756808
-- prepare agent completions
757809
M._chat_agents = {}
758810
M._command_agents = {}
@@ -828,9 +880,29 @@ M.setup = function(opts)
828880
M.error("curl is not installed, run :checkhealth gp")
829881
end
830882

831-
if type(M.config.openai_api_key) == "table" then
883+
for name, _ in pairs(M.providers) do
884+
M.resolve_secret(name)
885+
end
886+
887+
-- M.resolve_secret(M.config, "openai_api_key")
888+
-- M.valid_api_key()
889+
end
890+
891+
---@provider string # provider name
892+
function M.resolve_secret(provider)
893+
local post_process = function(name)
894+
local p = M.providers[name]
895+
if p.secret and type(p.secret) == "string" then
896+
p.secret = p.secret:gsub("^%s*(.-)%s*$", "%1")
897+
end
898+
899+
M.refresh_copilot_bearer()
900+
end
901+
902+
local secret = M.providers[provider].secret
903+
if secret and type(secret) == "table" then
832904
---@diagnostic disable-next-line: param-type-mismatch
833-
local copy = vim.deepcopy(M.config.openai_api_key)
905+
local copy = vim.deepcopy(secret)
834906
---@diagnostic disable-next-line: param-type-mismatch
835907
local cmd = table.remove(copy, 1)
836908
local args = copy
@@ -840,18 +912,23 @@ M.setup = function(opts)
840912
local content = stdout_data:match("^%s*(.-)%s*$")
841913
if not string.match(content, "%S") then
842914
M.warning(
843-
"response from the config.openai_api_key command "
844-
.. vim.inspect(M.config.openai_api_key)
915+
"response from the config.providers."
916+
.. provider
917+
.. ".secret command "
918+
.. vim.inspect(secret)
845919
.. " is empty"
846920
)
847921
return
848922
end
849-
M.config.openai_api_key = content
923+
M.providers[provider].secret = content
924+
post_process(provider)
850925
else
851926
M.warning(
852-
"config.openai_api_key command "
853-
.. vim.inspect(M.config.openai_api_key)
854-
.. " to retrieve openai_api_key failed:\ncode: "
927+
"config.providers."
928+
.. provider
929+
.. ".secret command "
930+
.. vim.inspect(secret)
931+
.. " to retrieve the secret failed:\ncode: "
855932
.. code
856933
.. ", signal: "
857934
.. signal
@@ -863,7 +940,7 @@ M.setup = function(opts)
863940
end
864941
end)
865942
else
866-
M.valid_api_key()
943+
post_process(provider)
867944
end
868945
end
869946

@@ -903,6 +980,8 @@ M.refresh_state = function()
903980
M._state.image_agent = M._image_agents[1]
904981
end
905982

983+
M._state.copilot_bearer = M._state.copilot_bearer or state.copilot_bearer or nil
984+
906985
M.table_to_file(M._state, state_file)
907986

908987
M.prepare_commands()

0 commit comments

Comments
 (0)