-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathQuotaNotifier.swift
More file actions
257 lines (233 loc) · 9.49 KB
/
Copy pathQuotaNotifier.swift
File metadata and controls
257 lines (233 loc) · 9.49 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
import AppKit
import Combine
import Foundation
import Logger
import NotificationCenterCoordinator
import Status
import UserNotifications
public struct QuotaSnapshotNotificationParams: Hashable, Codable {
public var quota: Int
public var used: Int
public var percentRemaining: Double
public var overageUsed: Int
public var overageEnabled: Bool
public var resetDate: String
public var unlimited: Bool
}
public struct QuotaChangeParams: Codable {
public var chat: QuotaSnapshotNotificationParams?
public var completions: QuotaSnapshotNotificationParams?
public var premiumInteractions: QuotaSnapshotNotificationParams?
public var copilotPlan: String?
public var canUpgradePlan: Bool?
enum CodingKeys: String, CodingKey {
case chat
case completions
case premiumInteractions = "premium_interactions"
case copilotPlan
case canUpgradePlan
}
}
public struct QuotaWarningParams: Hashable, Codable {
public var title: String
public var message: String
public var severity: String // "warning" or "info"
public var chat: QuotaSnapshotNotificationParams?
public var completions: QuotaSnapshotNotificationParams?
public var premiumInteractions: QuotaSnapshotNotificationParams?
public var copilotPlan: String?
public var canUpgradePlan: Bool?
enum CodingKeys: String, CodingKey {
case title
case message
case severity
case chat
case completions
case premiumInteractions = "premium_interactions"
case copilotPlan
case canUpgradePlan
}
}
public protocol QuotaNotifier {
func handleQuotaChange(_ params: QuotaChangeParams)
func handleQuotaWarning(_ params: QuotaWarningParams)
}
public class QuotaNotifierImpl: NSObject, QuotaNotifier {
public static let shared = QuotaNotifierImpl()
private static let enableUsageActionIdentifier = "quotaEnableUsageAction"
private static let increaseBudgetActionIdentifier = "quotaIncreaseBudgetAction"
private static let upgradeActionIdentifier = "quotaUpgradeAction"
private static let categoryNone = "quotaWarning_none"
private static let categoryUpgrade = "quotaWarning_upgrade"
private static let categoryEnableUsage = "quotaWarning_enableUsage"
private static let categoryEnableUsageUpgrade = "quotaWarning_enableUsage_upgrade"
private static let categoryIncreaseBudget = "quotaWarning_increaseBudget"
private static let categoryIncreaseBudgetUpgrade = "quotaWarning_increaseBudget_upgrade"
private var areCategoriesRegistered = false
private override init() {
super.init()
}
private func registerCategoriesIfNeeded() {
guard !areCategoriesRegistered else { return }
areCategoriesRegistered = true
let enableUsageAction = UNNotificationAction(
identifier: Self.enableUsageActionIdentifier,
title: "Enable additional usage",
options: [.foreground]
)
let increaseBudgetAction = UNNotificationAction(
identifier: Self.increaseBudgetActionIdentifier,
title: "Increase budget",
options: [.foreground]
)
let upgradeAction = UNNotificationAction(
identifier: Self.upgradeActionIdentifier,
title: "Upgrade Plan",
options: [.foreground]
)
let handler: (UNNotificationResponse) -> Void = { response in
switch response.actionIdentifier {
case Self.enableUsageActionIdentifier, Self.increaseBudgetActionIdentifier:
NSWorkspace.shared.open(URL(string: QuotaFormatting.manageOverageURL)!)
case Self.upgradeActionIdentifier:
NSWorkspace.shared.open(URL(string: QuotaFormatting.upgradePlanURL)!)
default:
break
}
}
let definitions: [(String, [UNNotificationAction])] = [
(Self.categoryNone, []),
(Self.categoryUpgrade, [upgradeAction]),
(Self.categoryEnableUsage, [enableUsageAction]),
(Self.categoryEnableUsageUpgrade, [enableUsageAction, upgradeAction]),
(Self.categoryIncreaseBudget, [increaseBudgetAction]),
(Self.categoryIncreaseBudgetUpgrade, [increaseBudgetAction, upgradeAction]),
]
for (id, actions) in definitions {
let category = UNNotificationCategory(
identifier: id,
actions: actions,
intentIdentifiers: [],
options: []
)
NotificationCenterCoordinator.shared.register(
category: category,
handler: handler,
for: id
)
}
}
private func notificationCategoryID(for actions: [WarningAction]) -> String {
let manageURL = QuotaFormatting.manageOverageURL
let upgradeURL = QuotaFormatting.upgradePlanURL
let manageAction = actions.first { $0.url.absoluteString == manageURL }
let hasUpgrade = actions.contains { $0.url.absoluteString == upgradeURL }
switch (manageAction?.title, hasUpgrade) {
case (nil, false): return Self.categoryNone
case (nil, true): return Self.categoryUpgrade
case ("Increase budget", false): return Self.categoryIncreaseBudget
case ("Increase budget", true): return Self.categoryIncreaseBudgetUpgrade
case (_, false): return Self.categoryEnableUsage
case (_, true): return Self.categoryEnableUsageUpgrade
}
}
public func handleQuotaChange(_ params: QuotaChangeParams) {
Task {
guard var quotaInfo = await Status.shared.getQuotaInfo() else { return }
if let chat = params.chat {
quotaInfo.chat = QuotaSnapshot(from: chat)
}
if let completions = params.completions {
quotaInfo.completions = QuotaSnapshot(from: completions)
}
if let premium = params.premiumInteractions {
quotaInfo.premiumInteractions = QuotaSnapshot(from: premium)
}
if let plan = params.copilotPlan {
quotaInfo.copilotPlan = plan
}
if let canUpgradePlan = params.canUpgradePlan {
quotaInfo.canUpgradePlan = canUpgradePlan
}
let resetDate = params.chat?.resetDate
?? params.completions?.resetDate
?? params.premiumInteractions?.resetDate
if let date = resetDate {
quotaInfo.resetDate = date
}
await Status.shared.updateQuotaInfo(quotaInfo)
}
}
public func handleQuotaWarning(_ params: QuotaWarningParams) {
Task { @MainActor in
let quotaInfo = await Status.shared.getQuotaInfo()
let actions = buildWarningActions(params: params, quotaInfo: quotaInfo)
let isCompletionsWarning = params.message.localizedCaseInsensitiveContains("completions")
if !isCompletionsWarning {
WarningStateManager.shared.setWarning(WarningContent(
message: params.message,
severity: params.severity,
actions: actions
))
}
await NotificationCenterCoordinator.shared.setupIfNeeded()
self.registerCategoriesIfNeeded()
await sendAppleNotification(params, categoryID: notificationCategoryID(for: actions))
}
}
private func buildWarningActions(
params: QuotaWarningParams,
quotaInfo: GitHubCopilotQuotaInfo?
) -> [WarningAction] {
let overageEnabled = params.premiumInteractions?.overageEnabled
?? quotaInfo?.premiumInteractions?.overagePermitted
?? false
let canUpgrade = params.canUpgradePlan ?? quotaInfo?.isUpgradePlanAllowed ?? true
let overageAction = WarningAction(
title: overageEnabled ? "Increase budget" : "Enable additional usage",
url: URL(string: QuotaFormatting.manageOverageURL)!
)
let upgradeAction = WarningAction(
title: "Upgrade plan",
url: URL(string: QuotaFormatting.upgradePlanURL)!
)
var actions: [WarningAction] = []
if quotaInfo?.isPaidIndividual ?? false {
actions.append(overageAction)
}
if canUpgrade {
actions.append(upgradeAction)
}
return actions
}
@MainActor
private func sendAppleNotification(_ params: QuotaWarningParams, categoryID: String) async {
let content = UNMutableNotificationContent()
content.title = params.title
content.body = params.message
content.sound = .default
content.categoryIdentifier = categoryID
let request = UNNotificationRequest(
identifier: "quotaWarning",
content: content,
trigger: nil
)
do {
try await UNUserNotificationCenter.current().add(request)
} catch {
Logger.gitHubCopilot.error("Failed to show quota warning notification: \(error)")
}
}
}
private extension QuotaSnapshot {
init(from params: QuotaSnapshotNotificationParams) {
self.init(
percentRemaining: Float(params.percentRemaining),
unlimited: params.unlimited,
overagePermitted: params.overageEnabled,
overageCount: Float(params.overageUsed),
entitlement: Double(params.quota),
quotaRemaining: Double(params.quota - params.used)
)
}
}