-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathcopilot.lua
364 lines (327 loc) · 10.6 KB
/
copilot.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
local config = require("codecompanion.config")
local curl = require("plenary.curl")
local log = require("codecompanion.utils.log")
local openai = require("codecompanion.adapters.openai")
local utils = require("codecompanion.utils.adapters")
local _cache_expires
local _cache_file = vim.fn.tempname()
local _cached_adapter
local _cached_models
---@alias CopilotOAuthToken string|nil
local _oauth_token
---@alias CopilotToken {token: string, expires_at: number}|nil
local _github_token
--- Finds the configuration path
local function find_config_path()
if os.getenv("CODECOMPANION_TOKEN_PATH") then
return os.getenv("CODECOMPANION_TOKEN_PATH")
end
local path = vim.fs.normalize("$XDG_CONFIG_HOME")
if path and vim.fn.isdirectory(path) > 0 then
return path
elseif vim.fn.has("win32") > 0 then
path = vim.fs.normalize("~/AppData/Local")
if vim.fn.isdirectory(path) > 0 then
return path
end
else
path = vim.fs.normalize("~/.config")
if vim.fn.isdirectory(path) > 0 then
return path
end
end
end
---The function first attempts to load the token from the environment variables,
---specifically for GitHub Codespaces. If not found, it then attempts to load
---the token from configuration files located in the user's configuration path.
---@return CopilotOAuthToken
local function get_token()
if _oauth_token then
return _oauth_token
end
local token = os.getenv("GITHUB_TOKEN")
local codespaces = os.getenv("CODESPACES")
if token and codespaces then
return token
end
local config_path = find_config_path()
if not config_path then
return nil
end
local file_paths = {
config_path .. "/github-copilot/hosts.json",
config_path .. "/github-copilot/apps.json",
}
for _, file_path in ipairs(file_paths) do
if vim.uv.fs_stat(file_path) then
local userdata = vim.fn.readfile(file_path)
if vim.islist(userdata) then
userdata = table.concat(userdata, " ")
end
local userdata = vim.json.decode(userdata)
for key, value in pairs(userdata) do
if string.find(key, "github.com") then
return value.oauth_token
end
end
end
end
return nil
end
---Authorize the GitHub OAuth token
---@return CopilotToken
local function authorize_token()
if _github_token and _github_token.expires_at > os.time() then
log:trace("Reusing GitHub Copilot token")
return _github_token
end
log:debug("Authorizing GitHub Copilot token")
local request = curl.get("https://api.github.com/copilot_internal/v2/token", {
headers = {
Authorization = "Bearer " .. _oauth_token,
["Accept"] = "application/json",
},
insecure = config.adapters.opts.allow_insecure,
proxy = config.adapters.opts.proxy,
on_error = function(err)
log:error("Copilot Adapter: Token request error %s", err)
end,
})
_github_token = vim.json.decode(request.body)
return _github_token
end
---Get and authorize a GitHub Copilot token
---@return boolean success
local function get_and_authorize_token()
_oauth_token = get_token()
if not _oauth_token then
log:error("Copilot Adapter: No token found. Please refer to https://github.com/github/copilot.vim")
return false
end
_github_token = authorize_token()
if not _github_token or vim.tbl_isempty(_github_token) then
log:error("Copilot Adapter: Could not authorize your GitHub Copilot token")
return false
end
return true
end
---Reset the cached adapter
---@return nil
local function reset()
_cached_adapter = nil
end
---Get a list of available Copilot models
---@params self CodeCompanion.Adapter
---@params opts? table
---@return table
local function get_models(self, opts)
if _cached_models and _cache_expires and _cache_expires > os.time() then
return _cached_models
end
if not _cached_adapter then
if not self then
return {}
end
_cached_adapter = self
end
get_and_authorize_token()
local url = "https://api.githubcopilot.com"
local headers = vim.deepcopy(_cached_adapter.headers)
headers["Authorization"] = "Bearer " .. _github_token.token
local ok, response = pcall(function()
return curl.get(url .. "/models", {
sync = true,
headers = headers,
insecure = config.adapters.opts.allow_insecure,
proxy = config.adapters.opts.proxy,
})
end)
if not ok then
log:error("Could not get the Copilot models from " .. url .. "/models.\nError: %s", response)
return {}
end
local ok, json = pcall(vim.json.decode, response.body)
if not ok then
log:error("Error parsing the response from " .. url .. "/models.\nError: %s", response.body)
return {}
end
local models = {}
for _, model in ipairs(json.data) do
if model.model_picker_enabled and model.capabilities.type == "chat" then
local choice_opts = {}
-- streaming support
if model.capabilities.supports.streaming then
choice_opts.can_stream = true
end
models[model.id] = { opts = choice_opts }
end
end
_cached_models = models
_cache_expires = utils.refresh_cache(_cache_file, config.adapters.opts.cache_models_for)
return models
end
---@class Copilot.Adapter: CodeCompanion.Adapter
return {
name = "copilot",
formatted_name = "Copilot",
roles = {
llm = "assistant",
user = "user",
},
opts = {
stream = true,
},
features = {
text = true,
tokens = true,
vision = false,
},
url = "https://api.githubcopilot.com/chat/completions",
env = {
---@return string|nil
api_key = function()
return authorize_token().token
end,
},
headers = {
Authorization = "Bearer ${api_key}",
["Content-Type"] = "application/json",
["Copilot-Integration-Id"] = "vscode-chat",
["Editor-Version"] = "Neovim/" .. vim.version().major .. "." .. vim.version().minor .. "." .. vim.version().patch,
},
handlers = {
---Check for a token before starting the request
---@param self CodeCompanion.Adapter
---@return boolean
setup = function(self)
local model = self.schema.model.default
local choices = self.schema.model.choices
if type(model) == "function" then
model = model(self)
end
if type(choices) == "function" then
choices = choices(self)
end
local model_opts = choices[model]
if (self.opts and self.opts.stream) and (model_opts and model_opts.opts and model_opts.opts.can_stream) then
self.parameters.stream = true
else
self.parameters.stream = nil
end
return get_and_authorize_token()
end,
--- Use the OpenAI adapter for the bulk of the work
form_parameters = function(self, params, messages)
return openai.handlers.form_parameters(self, params, messages)
end,
form_messages = function(self, messages)
return openai.handlers.form_messages(self, messages)
end,
tokens = function(self, data)
if data and data ~= "" then
local data_mod = utils.clean_streamed_data(data)
local ok, json = pcall(vim.json.decode, data_mod, { luanil = { object = true } })
if ok then
if json.usage then
local total_tokens = json.usage.total_tokens or 0
local completion_tokens = json.usage.completion_tokens or 0
local prompt_tokens = json.usage.prompt_tokens or 0
local tokens = total_tokens > 0 and total_tokens or completion_tokens + prompt_tokens
log:trace("Tokens: %s", tokens)
return tokens
end
end
end
end,
chat_output = function(self, data)
return openai.handlers.chat_output(self, data)
end,
inline_output = function(self, data, context)
return openai.handlers.inline_output(self, data, context)
end,
on_exit = function(self, data)
reset()
return openai.handlers.on_exit(self, data)
end,
},
schema = {
---@type CodeCompanion.Schema
model = {
order = 1,
mapping = "parameters",
type = "enum",
desc = "ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API.",
---@type string|fun(): string
default = "gpt-4o",
choices = function(self)
return get_models(self)
end,
},
---@type CodeCompanion.Schema
reasoning_effort = {
order = 2,
mapping = "parameters",
type = "string",
optional = true,
default = "medium",
desc = "Constrains effort on reasoning for reasoning models. Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response.",
choices = {
"high",
"medium",
"low",
},
},
---@type CodeCompanion.Schema
temperature = {
order = 3,
mapping = "parameters",
type = "number",
default = 0,
condition = function(self)
local model = self.schema.model.default
if type(model) == "function" then
model = model()
end
return not vim.startswith(model, "o1")
end,
desc = "What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or top_p but not both.",
},
max_tokens = {
order = 4,
mapping = "parameters",
type = "integer",
default = 15000,
desc = "The maximum number of tokens to generate in the chat completion. The total length of input tokens and generated tokens is limited by the model's context length.",
},
---@type CodeCompanion.Schema
top_p = {
order = 5,
mapping = "parameters",
type = "number",
default = 1,
condition = function(self)
local model = self.schema.model.default
if type(model) == "function" then
model = model()
end
return not vim.startswith(model, "o1")
end,
desc = "An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both.",
},
---@type CodeCompanion.Schema
n = {
order = 6,
mapping = "parameters",
type = "number",
default = 1,
condition = function(self)
local model = self.schema.model.default
if type(model) == "function" then
model = model()
end
return not vim.startswith(model, "o1")
end,
desc = "How many chat completions to generate for each prompt.",
},
},
}