From 0deae8597a78f9da9417a85052625bf0a9a796f8 Mon Sep 17 00:00:00 2001 From: Shx Guo Date: Tue, 20 Feb 2024 22:48:17 +0800 Subject: [PATCH] Support Copilot.vim 1.17.0 to 1.19.x --- .../GitHubCopilotRequest.swift | 72 +++++++++++++------ 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/Tool/Sources/GitHubCopilotService/GitHubCopilotRequest.swift b/Tool/Sources/GitHubCopilotService/GitHubCopilotRequest.swift index 744077ba..5fcba1c7 100644 --- a/Tool/Sources/GitHubCopilotService/GitHubCopilotRequest.swift +++ b/Tool/Sources/GitHubCopilotService/GitHubCopilotRequest.swift @@ -49,7 +49,6 @@ public struct GitHubCopilotCodeSuggestion: Codable, Equatable { public var displayText: String } - enum GitHubCopilotRequest { struct SetEditorInfo: GitHubCopilotRequestType { struct Response: Codable {} @@ -80,40 +79,61 @@ enum GitHubCopilotRequest { ]) } } - - var editorConfiguration: JSONValue? { + + var http: JSONValue? { var dict: [String: JSONValue] = [:] - if let networkProxy { - dict["http"] = networkProxy + let host = UserDefaults.shared.value(for: \.gitHubCopilotProxyHost) + if host.isEmpty { return nil } + var port = UserDefaults.shared.value(for: \.gitHubCopilotProxyPort) + if port.isEmpty { port = "80" } + let username = UserDefaults.shared.value(for: \.gitHubCopilotProxyUsername) + let password = UserDefaults.shared.value(for: \.gitHubCopilotProxyPassword) + let strictSSL = UserDefaults.shared.value(for: \.gitHubCopilotUseStrictSSL) + + let url = if !username.isEmpty { + "http://\(username):\(password)@\(host):\(port)" + } else { + "http://\(host):\(port)" } - + + dict["proxy"] = .string(url) + dict["proxyStrictSSL"] = .bool(strictSSL) + + if dict.isEmpty { return nil } + + return .hash(dict) + } + + var editorConfiguration: JSONValue? { + var dict: [String: JSONValue] = [:] + dict["http"] = http + let enterpriseURI = UserDefaults.shared.value(for: \.gitHubCopilotEnterpriseURI) if !enterpriseURI.isEmpty { dict["github-enterprise"] = .hash([ - "uri": .string(enterpriseURI) + "uri": .string(enterpriseURI), ]) } - + if dict.isEmpty { return nil } return .hash(dict) } - var request: ClientRequest { - if let editorConfiguration { - return .custom("setEditorInfo", .hash([ - "editorInfo": .hash([ - "name": "Xcode", - "version": "", - ]), - "editorPluginInfo": .hash([ - "name": "Copilot for Xcode", - "version": "", - ]), - "editorConfiguration": editorConfiguration, - ])) + var authProvider: JSONValue? { + var dict: [String: JSONValue] = [:] + let enterpriseURI = UserDefaults.shared.value(for: \.gitHubCopilotEnterpriseURI) + if !enterpriseURI.isEmpty { + dict["github-enterprise"] = .hash([ + "url": .string(enterpriseURI), + ]) } - return .custom("setEditorInfo", .hash([ + if dict.isEmpty { return nil } + return .hash(dict) + } + + var request: ClientRequest { + var dict: [String: JSONValue] = [ "editorInfo": .hash([ "name": "Xcode", "version": "", @@ -122,7 +142,13 @@ enum GitHubCopilotRequest { "name": "Copilot for Xcode", "version": "", ]), - ])) + ] + + dict["editorConfiguration"] = editorConfiguration + dict["authProvider"] = authProvider + dict["networkProxy"] = networkProxy + + return .custom("setEditorInfo", .hash(dict)) } }