Skip to content

Commit 9712e95

Browse files
committed
Allow setting GitHub Copilot proxy
1 parent 2f0ab62 commit 9712e95

File tree

4 files changed

+104
-13
lines changed

4 files changed

+104
-13
lines changed

Core/Sources/GitHubCopilotService/GitHubCopilotRequest.swift

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import SuggestionModel
21
import Foundation
32
import JSONRPC
43
import LanguageServerProtocol
4+
import SuggestionModel
55

66
struct GitHubCopilotDoc: Codable {
77
var source: String
@@ -26,8 +26,49 @@ enum GitHubCopilotRequest {
2626
struct SetEditorInfo: GitHubCopilotRequestType {
2727
struct Response: Codable {}
2828

29+
var networkProxy: JSONValue? {
30+
let host = UserDefaults.shared.value(for: \.gitHubCopilotProxyHost)
31+
if host.isEmpty { return nil }
32+
var port = UserDefaults.shared.value(for: \.gitHubCopilotProxyPort)
33+
if port.isEmpty { port = "80" }
34+
let username = UserDefaults.shared.value(for: \.gitHubCopilotProxyUsername)
35+
if username.isEmpty {
36+
return .hash([
37+
"host": .string(host),
38+
"port": .number(Double(Int(port) ?? 80)),
39+
"rejectUnauthorized": .bool(UserDefaults.shared
40+
.value(for: \.gitHubCopilotUseStrictSSL)),
41+
])
42+
} else {
43+
return .hash([
44+
"host": .string(host),
45+
"port": .number(Double(Int(port) ?? 80)),
46+
"rejectUnauthorized": .bool(UserDefaults.shared
47+
.value(for: \.gitHubCopilotUseStrictSSL)),
48+
"username": .string(username),
49+
"password": .string(UserDefaults.shared
50+
.value(for: \.gitHubCopilotProxyPassword)),
51+
52+
])
53+
}
54+
}
55+
2956
var request: ClientRequest {
30-
.custom("setEditorInfo", .hash([
57+
if let networkProxy {
58+
return .custom("setEditorInfo", .hash([
59+
"editorInfo": .hash([
60+
"name": "Xcode",
61+
"version": "",
62+
]),
63+
"editorPluginInfo": .hash([
64+
"name": "Copilot for Xcode",
65+
"version": "",
66+
]),
67+
"networkProxy": networkProxy,
68+
]))
69+
}
70+
71+
return .custom("setEditorInfo", .hash([
3172
"editorInfo": .hash([
3273
"name": "Xcode",
3374
"version": "",
@@ -171,3 +212,4 @@ enum GitHubCopilotRequest {
171212
}
172213
}
173214
}
215+

Core/Sources/GitHubCopilotService/GitHubCopilotService.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,13 @@ public class GitHubCopilotBaseService {
165165

166166
self.server = server
167167
localProcessServer = localServer
168+
169+
Task {
170+
try await server.sendRequest(GitHubCopilotRequest.SetEditorInfo())
171+
}
168172
}
173+
174+
169175

170176
public static func createFoldersIfNeeded() throws -> (
171177
applicationSupportURL: URL,
@@ -211,9 +217,6 @@ public final class GitHubCopilotAuthService: GitHubCopilotBaseService,
211217
public init() throws {
212218
let home = FileManager.default.homeDirectoryForCurrentUser
213219
try super.init(projectRootURL: home)
214-
Task {
215-
try? await server.sendRequest(GitHubCopilotRequest.SetEditorInfo())
216-
}
217220
}
218221

219222
public func checkStatus() async throws -> GitHubCopilotAccountStatus {

Core/Sources/HostApp/AccountSettings/CopilotView.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ struct CopilotView: View {
1313
@AppStorage(\.runNodeWith) var runNodeWith
1414
@AppStorage("username") var username: String = ""
1515
@AppStorage(\.gitHubCopilotVerboseLog) var gitHubCopilotVerboseLog
16+
@AppStorage(\.gitHubCopilotProxyHost) var gitHubCopilotProxyHost
17+
@AppStorage(\.gitHubCopilotProxyPort) var gitHubCopilotProxyPort
18+
@AppStorage(\.gitHubCopilotProxyUsername) var gitHubCopilotProxyUsername
19+
@AppStorage(\.gitHubCopilotProxyPassword) var gitHubCopilotProxyPassword
20+
@AppStorage(\.gitHubCopilotUseStrictSSL) var gitHubCopilotUseStrictSSL
1621

1722
init() {}
1823
}
@@ -182,9 +187,9 @@ struct CopilotView: View {
182187
uninstallButton
183188
}
184189
}
185-
190+
186191
Text("Language Server Version: \(version ?? "Loading..")")
187-
192+
188193
Text("Status: \(status?.description ?? "Loading..")")
189194

190195
HStack(alignment: .center) {
@@ -226,6 +231,24 @@ struct CopilotView: View {
226231
Form {
227232
Toggle("Verbose Log", isOn: $settings.gitHubCopilotVerboseLog)
228233
}
234+
235+
Divider()
236+
237+
Form {
238+
TextField(text: $settings.gitHubCopilotProxyHost, prompt: Text("xxx.xxx.xxx.xxx, leave it blank to disable proxy.")) {
239+
Text("Proxy Host")
240+
}
241+
TextField(text: $settings.gitHubCopilotProxyPort, prompt: Text("80")) {
242+
Text("Proxy Port")
243+
}
244+
TextField(text: $settings.gitHubCopilotProxyUsername) {
245+
Text("Proxy Username")
246+
}
247+
SecureField(text: $settings.gitHubCopilotProxyPassword) {
248+
Text("Proxy Password")
249+
}
250+
Toggle("Proxy Strict SSL", isOn: $settings.gitHubCopilotUseStrictSSL)
251+
}
229252
}
230253
Spacer()
231254
}.onAppear {

Tool/Sources/Preferences/Keys.swift

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ public extension UserDefaultPreferenceKeys {
122122
.init(defaultValue: false, key: "GitHubCopilotVerboseLog")
123123
}
124124

125+
var gitHubCopilotProxyHost: PreferenceKey<String> {
126+
.init(defaultValue: "", key: "GitHubCopilotProxyHost")
127+
}
128+
129+
var gitHubCopilotProxyPort: PreferenceKey<String> {
130+
.init(defaultValue: "", key: "GitHubCopilotProxyPort")
131+
}
132+
133+
var gitHubCopilotUseStrictSSL: PreferenceKey<Bool> {
134+
.init(defaultValue: true, key: "GitHubCopilotUseStrictSSL")
135+
}
136+
137+
var gitHubCopilotProxyUsername: PreferenceKey<String> {
138+
.init(defaultValue: "", key: "GitHubCopilotProxyUsername")
139+
}
140+
141+
var gitHubCopilotProxyPassword: PreferenceKey<String> {
142+
.init(defaultValue: "", key: "GitHubCopilotProxyPassword")
143+
}
144+
125145
var nodePath: PreferenceKey<String> {
126146
.init(defaultValue: "", key: "NodePath")
127147
}
@@ -245,7 +265,7 @@ public extension UserDefaultPreferenceKeys {
245265
key: "DefaultChatSystemPrompt"
246266
)
247267
}
248-
268+
249269
var chatSearchPluginMaxIterations: PreferenceKey<Int> {
250270
.init(defaultValue: 3, key: "ChatSearchPluginMaxIterations")
251271
}
@@ -257,9 +277,12 @@ public extension UserDefaultPreferenceKeys {
257277
var bingSearchSubscriptionKey: PreferenceKey<String> {
258278
.init(defaultValue: "", key: "BingSearchSubscriptionKey")
259279
}
260-
280+
261281
var bingSearchEndpoint: PreferenceKey<String> {
262-
.init(defaultValue: "https://api.bing.microsoft.com/v7.0/search/", key: "BingSearchEndpoint")
282+
.init(
283+
defaultValue: "https://api.bing.microsoft.com/v7.0/search/",
284+
key: "BingSearchEndpoint"
285+
)
263286
}
264287
}
265288

@@ -326,15 +349,15 @@ public extension UserDefaultPreferenceKeys {
326349
var triggerActionWithAccessibilityAPI: FeatureFlag {
327350
.init(defaultValue: true, key: "FeatureFlag-TriggerActionWithAccessibilityAPI")
328351
}
329-
352+
330353
var animationACrashSuggestion: FeatureFlag {
331354
.init(defaultValue: true, key: "FeatureFlag-AnimationACrashSuggestion")
332355
}
333-
356+
334357
var animationBCrashSuggestion: FeatureFlag {
335358
.init(defaultValue: true, key: "FeatureFlag-AnimationBCrashSuggestion")
336359
}
337-
360+
338361
var animationCCrashSuggestion: FeatureFlag {
339362
.init(defaultValue: true, key: "FeatureFlag-AnimationCCrashSuggestion")
340363
}

0 commit comments

Comments
 (0)