forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
465 lines (421 loc) · 15.7 KB
/
AppDelegate.swift
File metadata and controls
465 lines (421 loc) · 15.7 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import Combine
import FileChangeChecker
import GitHubCopilotService
import LaunchAgentManager
import Logger
import Preferences
import Service
import ServiceManagement
import Status
import SwiftUI
import UpdateChecker
import UserDefaultsObserver
import UserNotifications
import XcodeInspector
import XPCShared
import GitHubCopilotViewModel
import StatusBarItemView
let bundleIdentifierBase = Bundle.main
.object(forInfoDictionaryKey: "BUNDLE_IDENTIFIER_BASE") as! String
let serviceIdentifier = bundleIdentifierBase + ".ExtensionService"
class ExtensionUpdateCheckerDelegate: UpdateCheckerDelegate {
func prepareForRelaunch(finish: @escaping () -> Void) {
Task {
await Service.shared.prepareForExit()
finish()
}
}
}
@main
class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
let service = Service.shared
var statusBarItem: NSStatusItem!
var extensionStatusItem: NSMenuItem!
var accountItem: NSMenuItem!
var authStatusItem: NSMenuItem!
var upSellItem: NSMenuItem!
var toggleCompletions: NSMenuItem!
var toggleIgnoreLanguage: NSMenuItem!
var openChat: NSMenuItem!
var signOutItem: NSMenuItem!
var xpcController: XPCController?
let updateChecker =
UpdateChecker(
hostBundle: Bundle(url: locateHostBundleURL(url: Bundle.main.bundleURL)),
checkerDelegate: ExtensionUpdateCheckerDelegate()
)
var xpcExtensionService: XPCExtensionService?
private var cancellables = Set<AnyCancellable>()
private var progressView: NSProgressIndicator?
func applicationDidFinishLaunching(_: Notification) {
if ProcessInfo.processInfo.environment["IS_UNIT_TEST"] == "YES" { return }
_ = XcodeInspector.shared
service.start()
AXIsProcessTrustedWithOptions([
kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true,
] as CFDictionary)
setupQuitOnUpdate()
setupQuitOnUserTerminated()
xpcController = .init()
Logger.service.info("XPC Service started.")
NSApp.setActivationPolicy(.accessory)
buildStatusBarMenu()
watchServiceStatus()
watchAXStatus()
watchAuthStatus()
setInitialStatusBarStatus()
UserDefaults.shared.set(false, for: \.clsWarningDismissedUntilRelaunch)
}
@objc func quit() {
Task { @MainActor in
await service.prepareForExit()
await xpcController?.quit()
NSApp.terminate(self)
}
}
@objc func openCopilotForXcode() {
let task = Process()
let appPath = locateHostBundleURL(url: Bundle.main.bundleURL)
task.launchPath = "/usr/bin/open"
task.arguments = [appPath.absoluteString]
task.launch()
task.waitUntilExit()
}
@objc func signIntoGitHub() {
Task { @MainActor in
let viewModel = GitHubCopilotViewModel.shared
// Don't trigger the shared viewModel's alert
do {
guard let signInResponse = try await viewModel.preSignIn() else {
return
}
NSApp.activate(ignoringOtherApps: true)
let alert = NSAlert()
alert.messageText = signInResponse.userCode
alert.informativeText = """
Please enter the above code in the GitHub website to authorize your \
GitHub account with Copilot for Xcode.
\(signInResponse.verificationURL.absoluteString)
"""
alert.addButton(withTitle: "Copy Code and Open")
alert.addButton(withTitle: "Cancel")
let response = alert.runModal()
if response == .alertFirstButtonReturn {
viewModel.signInResponse = signInResponse
viewModel.copyAndOpen()
}
} catch {
Logger.service.error("GitHub copilot view model Sign in fails: \(error)")
}
}
}
@objc func signOutGitHub() {
Task { @MainActor in
let viewModel = GitHubCopilotViewModel.shared
viewModel.signOut()
}
}
@objc func openGlobalChat() {
Task { @MainActor in
let serviceGUI = Service.shared.guiController
serviceGUI.openGlobalChat()
}
}
func setupQuitOnUpdate() {
Task {
guard let url = Bundle.main.executableURL else { return }
let checker = await FileChangeChecker(fileURL: url)
// If Xcode or Copilot for Xcode is made active, check if the executable of this program
// is changed. If changed, quit this program.
let sequence = NSWorkspace.shared.notificationCenter
.notifications(named: NSWorkspace.didActivateApplicationNotification)
for await notification in sequence {
try Task.checkCancellation()
guard let app = notification
.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication,
app.isUserOfService
else { continue }
guard await checker.checkIfChanged() else {
Logger.service.info("Extension Service is not updated, no need to quit.")
continue
}
Logger.service.info("Extension Service will quit.")
#if DEBUG
#else
quit()
#endif
}
}
}
func setupQuitOnUserTerminated() {
Task {
// Whenever Xcode or the host application quits, check if any of the two is running.
// If none, quit the XPC service.
let sequence = NSWorkspace.shared.notificationCenter
.notifications(named: NSWorkspace.didTerminateApplicationNotification)
for await notification in sequence {
try Task.checkCancellation()
guard UserDefaults.shared.value(for: \.quitXPCServiceOnXcodeAndAppQuit)
else { continue }
guard let app = notification
.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication,
app.isUserOfService
else { continue }
if NSWorkspace.shared.runningApplications.contains(where: \.isUserOfService) {
continue
}
quit()
}
}
}
func requestAccessoryAPIPermission() {
AXIsProcessTrustedWithOptions([
kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true,
] as NSDictionary)
}
@objc func checkForUpdate() {
guard let updateChecker = updateChecker else {
Logger.service.error("Unable to check for updates: updateChecker is nil.")
return
}
updateChecker.checkForUpdates()
}
func getXPCExtensionService() -> XPCExtensionService {
if let service = xpcExtensionService { return service }
let service = XPCExtensionService(logger: .service)
xpcExtensionService = service
return service
}
func watchServiceStatus() {
let notifications = NotificationCenter.default.notifications(named: .serviceStatusDidChange)
Task { [weak self] in
for await _ in notifications {
guard let self else { return }
self.updateStatusBarItem()
}
}
}
func watchAXStatus() {
let osNotifications = DistributedNotificationCenter.default().notifications(named: NSNotification.Name("com.apple.accessibility.api"))
Task { [weak self] in
for await _ in osNotifications {
guard let self else { return }
self.updateStatusBarItem()
}
}
}
func watchAuthStatus() {
let notifications = DistributedNotificationCenter.default().notifications(named: .authStatusDidChange)
Task { [weak self] in
for await _ in notifications {
guard let self else { return }
await self.forceAuthStatusCheck()
}
}
}
func setInitialStatusBarStatus() {
Task {
let authStatus = await Status.shared.getAuthStatus()
if authStatus == .unknown {
// temporarily kick off a language server instance to prime the initial auth status
await forceAuthStatusCheck()
}
updateStatusBarItem()
}
}
func forceAuthStatusCheck() async {
do {
let service = try GitHubCopilotService()
_ = try await service.checkStatus()
try await service.shutdown()
try await service.exit()
} catch {
Logger.service.error("Failed to read auth status: \(error)")
}
}
private func configureNotLoggedIn() {
self.accountItem.view = AccountItemView(
target: self,
action: #selector(signIntoGitHub)
)
self.authStatusItem.isHidden = true
self.upSellItem.isHidden = true
self.toggleCompletions.isHidden = true
self.toggleIgnoreLanguage.isHidden = true
self.openChat.isHidden = true
self.signOutItem.isHidden = true
}
private func configureLoggedIn(status: StatusResponse) {
self.accountItem.view = AccountItemView(
target: self,
action: nil,
userName: status.userName ?? ""
)
if !status.clsMessage.isEmpty {
self.authStatusItem.isHidden = false
let CLSMessageSummary = getCLSMessageSummary(status.clsMessage)
self.authStatusItem.title = CLSMessageSummary.summary
let submenu = NSMenu()
let attributedCLSErrorItem = NSMenuItem()
attributedCLSErrorItem.view = ErrorMessageView(
errorMessage: CLSMessageSummary.detail
)
submenu.addItem(attributedCLSErrorItem)
submenu.addItem(.separator())
submenu.addItem(
NSMenuItem(
title: "View Details on GitHub",
action: #selector(openGitHubDetailsLink),
keyEquivalent: ""
)
)
self.authStatusItem.submenu = submenu
self.authStatusItem.isEnabled = true
self.upSellItem.title = "Upgrade Now"
self.upSellItem.isHidden = false
self.upSellItem.isEnabled = true
} else {
self.authStatusItem.isHidden = true
self.upSellItem.isHidden = true
}
self.toggleCompletions.isHidden = false
self.toggleIgnoreLanguage.isHidden = false
self.openChat.isHidden = false
self.signOutItem.isHidden = false
}
private func configureNotAuthorized(status: StatusResponse) {
self.accountItem.view = AccountItemView(
target: self,
action: nil,
userName: status.userName ?? ""
)
self.authStatusItem.isHidden = false
self.authStatusItem.title = "No Subscription"
let submenu = NSMenu()
let attributedNotAuthorizedItem = NSMenuItem()
attributedNotAuthorizedItem.view = ErrorMessageView(
errorMessage: "GitHub Copilot features are disabled. Check your subscription to enable them."
)
attributedNotAuthorizedItem.isEnabled = true
submenu.addItem(attributedNotAuthorizedItem)
self.authStatusItem.submenu = submenu
self.authStatusItem.isEnabled = true
self.upSellItem.title = "Check Subscription Plans"
self.upSellItem.isHidden = false
self.upSellItem.isEnabled = true
self.toggleCompletions.isHidden = true
self.toggleIgnoreLanguage.isHidden = true
self.openChat.isHidden = true
self.signOutItem.isHidden = false
}
private func configureUnknown() {
self.accountItem.view = AccountItemView(
target: self,
action: nil,
userName: "Unknown User"
)
self.authStatusItem.isHidden = true
self.upSellItem.isHidden = true
self.toggleCompletions.isHidden = false
self.toggleIgnoreLanguage.isHidden = false
self.openChat.isHidden = false
self.signOutItem.isHidden = false
}
func updateStatusBarItem() {
Task { @MainActor in
let status = await Status.shared.getStatus()
self.statusBarItem.button?.image = status.icon.nsImage
switch status.authStatus {
case .notLoggedIn: configureNotLoggedIn()
case .loggedIn: configureLoggedIn(status: status)
case .notAuthorized: configureNotAuthorized(status: status)
case .unknown: configureUnknown()
}
if let message = status.message {
self.extensionStatusItem.title = message
self.extensionStatusItem.isHidden = false
self.extensionStatusItem.isEnabled = status.url != nil
} else {
self.extensionStatusItem.isHidden = true
}
self.markAsProcessing(status.inProgress)
}
}
func markAsProcessing(_ isProcessing: Bool) {
if !isProcessing {
// No longer in progress
progressView?.removeFromSuperview()
progressView = nil
return
}
if progressView != nil {
// Already in progress
return
}
let progress = NSProgressIndicator()
progress.style = .spinning
progress.sizeToFit()
progress.frame = statusBarItem.button?.bounds ?? .zero
progress.isIndeterminate = true
progress.startAnimation(nil)
statusBarItem.button?.addSubview(progress)
statusBarItem.button?.image = nil
progressView = progress
}
@objc func openGitHubDetailsLink() {
Task {
if let url = URL(string: "https://github.com/copilot") {
NSWorkspace.shared.open(url)
}
}
}
}
extension NSRunningApplication {
var isUserOfService: Bool {
[
"com.apple.dt.Xcode",
bundleIdentifierBase,
].contains(bundleIdentifier)
}
}
func locateHostBundleURL(url: URL) -> URL {
var nextURL = url
while nextURL.path != "/" {
nextURL = nextURL.deletingLastPathComponent()
if nextURL.lastPathComponent.hasSuffix(".app") {
return nextURL
}
}
let devAppURL = url
.deletingLastPathComponent()
.appendingPathComponent("GitHub Copilot for Xcode Dev.app")
return devAppURL
}
struct CLSMessage {
let summary: String
let detail: String
}
func extractDateFromCLSMessage(_ message: String) -> String? {
let pattern = #"until (\d{1,2}/\d{1,2}/\d{4}, \d{1,2}:\d{2}:\d{2} [AP]M)"#
if let range = message.range(of: pattern, options: .regularExpression) {
return String(message[range].dropFirst(6))
}
return nil
}
func getCLSMessageSummary(_ message: String) -> CLSMessage {
let summary: String
if message.contains("You've reached your monthly chat messages limit") {
summary = "Monthly Chat Limit Reached"
} else if message.contains("You've reached your monthly code completion limit") {
summary = "Monthly Completion Limit Reached"
} else {
summary = "CLS Error"
}
let detail: String
if let date = extractDateFromCLSMessage(message) {
detail = "Visit GitHub to check your usage and upgrade to Copilot Pro or wait until \(date) for your limit to reset."
} else {
detail = message
}
return CLSMessage(summary: summary, detail: detail)
}