Skip to content

Commit 73dbc0e

Browse files
authored
refactor: remove deprecated setEditorInfo API calls and follow new SDK (#406)
1 parent af15584 commit 73dbc0e

File tree

3 files changed

+54
-105
lines changed

3 files changed

+54
-105
lines changed

lua/copilot/api.lua

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,20 @@ end
3333

3434
---@alias copilot_editor_info { name: string, version: string }
3535
---@alias copilot_editor_plugin_info { name: string, version: string }
36-
---@alias copilot_auth_provider { url: string }
37-
---@alias copilot_network_proxy { host: string, port: integer, username?: string, password?: string, rejectUnauthorized?: boolean }
38-
---@alias copilot_set_editor_info_params { editorInfo: copilot_editor_info, editorPluginInfo: copilot_editor_plugin_info, editorConfiguration: copilot_editor_configuration, networkProxy?: copilot_network_proxy, authProvider?: copilot_auth_provider }
3936

40-
---@param params copilot_set_editor_info_params
41-
---@return any|nil err
42-
---@return nil data
43-
---@return table ctx
44-
function mod.set_editor_info(client, params, callback)
45-
return mod.request(client, "setEditorInfo", params, callback)
46-
end
37+
---@alias copilot_settings_http { proxy: string, proxyStrictSSL: boolean, proxyKerberosServicePrincipal?: string }
38+
---@alias github_settings_telemetry { telemetryLevel: string }
39+
---@alias copilot_settings_github-enterprise { uri: string }
40+
---@alias copilot_settings { http?: copilot_settings_http, telemetry: github_settings_telemetry, github-enterprise?: copilot_settings_github-enterprise }
4741

48-
---@alias copilot_editor_configuration { enableAutoCompletions: boolean, disabledLanguages: string[] }
49-
---@alias copilot_notify_change_configuration_params { settings: copilot_editor_configuration }
42+
---@alias copilot_workspace_selected_completion_model { selectedCompletionModel: string }
43+
---@alias copilot_workspace_copilot { copilot: copilot_workspace_copilot }
44+
---@alias copilot_workspace_configuration { enableAutoCompletions: boolean, disabledLanguages: string[], github: copilot_workspace_configuration }
45+
---@alias copilot_workspace_configurations { settings: copilot_workspace_configuration }
5046

51-
---@param params copilot_notify_change_configuration_params
47+
---@param params copilot_workspace_configurations
5248
function mod.notify_change_configuration(client, params)
53-
return mod.notify(client, "notifyChangeConfiguration", params)
49+
return mod.notify(client, "workspace/didChangeConfiguration", params)
5450
end
5551

5652
---@alias copilot_nofify_set_trace_params { value: 'off'|'messages'|'verbose' }

lua/copilot/client.lua

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,32 @@ local function prepare_client_config(overrides)
231231
end
232232

233233
local editor_info = util.get_editor_info()
234+
local provider_url = config.get("auth_provider_url") --[[@as string|nil]]
235+
local proxy_uri = vim.g.copilot_proxy
236+
237+
local settings = { ---@type copilot_settings
238+
telemetry = { ---@type github_settings_telemetry
239+
telemetryLevel = "all",
240+
},
241+
}
242+
243+
if proxy_uri then
244+
vim.tbl_extend("force", settings, {
245+
http = { ---@type copilot_settings_http
246+
proxy = proxy_uri,
247+
proxyStrictSSL = vim.g.copilot_proxy_strict_ssl or false,
248+
proxyKerberosServicePrincipal = nil,
249+
},
250+
})
251+
end
252+
253+
if provider_url then
254+
vim.tbl_extend("force", settings, {
255+
["github-enterprise"] = { ---@type copilot_settings_github-enterprise
256+
uri = provider_url,
257+
},
258+
})
259+
end
234260

235261
-- LSP config, not to be confused with config.lua
236262
return vim.tbl_deep_extend("force", {
@@ -250,26 +276,16 @@ local function prepare_client_config(overrides)
250276
end
251277

252278
vim.schedule(function()
253-
local set_editor_info_params = util.get_editor_info() --[[@as copilot_set_editor_info_params]]
254-
set_editor_info_params.editorConfiguration = util.get_editor_configuration()
255-
set_editor_info_params.networkProxy = util.get_network_proxy()
256-
local provider_url = config.get("auth_provider_url")
257-
set_editor_info_params.authProvider = provider_url and {
258-
url = provider_url,
259-
} or nil
260-
261-
logger.debug("data for setEditorInfo LSP call", set_editor_info_params)
262-
api.set_editor_info(client, set_editor_info_params, function(err)
263-
if err then
264-
logger.error(string.format("setEditorInfo failure: %s", err))
265-
end
266-
end)
267-
logger.trace("setEditorInfo has been called")
279+
local configurations = util.get_workspace_configurations()
280+
api.notify_change_configuration(client, configurations)
281+
logger.trace("workspace configuration", configurations)
268282

283+
-- to activate tracing if we want it
269284
local logger_conf = config.get("logger") --[[@as copilot_config_logging]]
270285
local trace_params = { value = logger_conf.trace_lsp } --[[@as copilot_nofify_set_trace_params]]
271286
api.notify_set_trace(client, trace_params)
272287

288+
-- prevent requests to copilot prior to being initialized
273289
M.initialized = true
274290
end)
275291
end,
@@ -289,13 +305,11 @@ local function prepare_client_config(overrides)
289305
end,
290306
handlers = get_handlers(),
291307
init_options = {
292-
copilotIntegrationId = "vscode-chat",
293-
-- Fix LSP warning: editorInfo and editorPluginInfo will soon be required in initializationOptions
294-
-- We are sending these twice for the time being as it will become required here and we get a warning if not set.
295-
-- However if not passed in setEditorInfo, that one returns an error.
308+
copilotIntegrationId = "vscode-chat", -- can be safely removed with copilot v1.291
296309
editorInfo = editor_info.editorInfo,
297310
editorPluginInfo = editor_info.editorPluginInfo,
298311
},
312+
settings = settings,
299313
workspace_folders = workspace_folders,
300314
trace = config.get("trace") or "off",
301315
}, overrides)

lua/copilot/util.lua

Lines changed: 11 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local config = require("copilot.config")
22
local logger = require("copilot.logger")
3-
local unpack = unpack or table.unpack
43

54
local M = {}
65

@@ -186,8 +185,8 @@ M.get_completion_params = function(opts)
186185
return M.get_doc_params(opts)
187186
end
188187

189-
---@return copilot_editor_configuration
190-
function M.get_editor_configuration()
188+
---@return copilot_workspace_configurations
189+
function M.get_workspace_configurations()
191190
local conf = config.get() --[[@as copilot_config]]
192191

193192
local filetypes = vim.deepcopy(conf.filetypes) --[[@as table<string, boolean>]]
@@ -205,77 +204,17 @@ function M.get_editor_configuration()
205204
table.sort(disabled_filetypes)
206205

207206
return {
208-
github = {
209-
copilot = {
210-
selectedCompletionModel = copilot_model,
207+
settings = {
208+
github = {
209+
copilot = {
210+
selectedCompletionModel = copilot_model,
211+
},
211212
},
213+
enableAutoCompletions = not not (conf.panel.enabled or conf.suggestion.enabled),
214+
disabledLanguages = vim.tbl_map(function(ft)
215+
return { languageId = ft }
216+
end, disabled_filetypes),
212217
},
213-
enableAutoCompletions = not not (conf.panel.enabled or conf.suggestion.enabled),
214-
disabledLanguages = vim.tbl_map(function(ft)
215-
return { languageId = ft }
216-
end, disabled_filetypes),
217-
}
218-
end
219-
220-
---@param str string
221-
local function url_decode(str)
222-
return vim.fn.substitute(str, [[%\(\x\x\)]], [[\=iconv(nr2char("0x".submatch(1)), "utf-8", "latin1")]], "g")
223-
end
224-
225-
---@return copilot_network_proxy|nil
226-
function M.get_network_proxy()
227-
local proxy_uri = vim.g.copilot_proxy
228-
229-
if type(proxy_uri) ~= "string" then
230-
return
231-
end
232-
233-
proxy_uri = string.gsub(proxy_uri, "^[^:]+://", "")
234-
235-
---@type string|nil, string|nil
236-
local user_pass, host_port = unpack(vim.split(proxy_uri, "@", { plain = true, trimempty = true }))
237-
238-
if not host_port then
239-
host_port = user_pass --[[@as string]]
240-
user_pass = nil
241-
end
242-
243-
local query_string
244-
host_port, query_string = unpack(vim.split(host_port, "?", { plain = true, trimempty = true }))
245-
246-
local rejectUnauthorized = vim.g.copilot_proxy_strict_ssl
247-
248-
if query_string then
249-
local query_params = vim.split(query_string, "&", { plain = true, trimempty = true })
250-
for _, query_param in ipairs(query_params) do
251-
local strict_ssl = string.match(query_param, "strict_?ssl=(.*)")
252-
253-
if string.find(strict_ssl, "^[1t]") then
254-
rejectUnauthorized = true
255-
break
256-
end
257-
258-
if string.find(strict_ssl, "^[0f]") then
259-
rejectUnauthorized = false
260-
break
261-
end
262-
end
263-
end
264-
265-
local host, port = unpack(vim.split(host_port, ":", { plain = true, trimempty = true }))
266-
local username, password
267-
268-
if user_pass then
269-
username, password = unpack(vim.split(user_pass, ":", { plain = true, trimempty = true }))
270-
username, password = username and url_decode(username), password and url_decode(password)
271-
end
272-
273-
return {
274-
host = host,
275-
port = tonumber(port or 80),
276-
username = username,
277-
password = password,
278-
rejectUnauthorized = rejectUnauthorized,
279218
}
280219
end
281220

0 commit comments

Comments
 (0)