2026-10-17 08:24:22 -07:00
|
|
|
import AppKit
|
|
|
|
|
import UserNotifications
|
|
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
|
final class Notifier: NSObject, UNUserNotificationCenterDelegate {
|
|
|
|
|
static let shared = Notifier()
|
|
|
|
|
weak var connection: Connection?
|
|
|
|
|
var showWindow: (() -> Void)?
|
|
|
|
|
private var pending: String?
|
|
|
|
|
|
|
|
|
|
func install() {
|
|
|
|
|
UNUserNotificationCenter.current().delegate = self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func open(_ taskId: String) {
|
|
|
|
|
NSApp.activate()
|
|
|
|
|
showWindow?()
|
|
|
|
|
pending = taskId
|
|
|
|
|
deliver()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func deliver() {
|
|
|
|
|
guard let taskId = pending, let store = connection?.store else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
pending = nil
|
|
|
|
|
if !taskId.isEmpty {
|
|
|
|
|
store.focusTask(taskId)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
|
|
|
|
|
[.banner, .list, .sound]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
|
|
|
|
|
let taskId = response.notification.request.content.userInfo["task_id"] as? String ?? ""
|
|
|
|
|
await MainActor.run {
|
|
|
|
|
open(taskId)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func wants(_ p: NotificationPrefs, _ item: InboxItem) -> Bool {
|
|
|
|
|
switch item.kind {
|
2026-10-17 14:41:04 -07:00
|
|
|
case .question, .clientRun: p.questions
|
2026-10-17 08:24:22 -07:00
|
|
|
case .sessionError: p.errors
|
|
|
|
|
case .rateLimited: p.rateLimits
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func itemText(_ item: InboxItem) -> String {
|
|
|
|
|
let p = item.payload.value
|
|
|
|
|
switch item.kind {
|
|
|
|
|
case .question: return str(p, "question") ?? ""
|
|
|
|
|
case .sessionError: return "Error: \(str(p, "error") ?? "")"
|
|
|
|
|
case .rateLimited: return "Rate limited"
|
2026-10-17 14:41:04 -07:00
|
|
|
case .clientRun: return "Run on your Mac: \(str(p, "why") ?? "")"
|
2026-10-17 08:24:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
|
private func show(_ store: Store, _ taskId: String, _ tag: String, _ text: String) {
|
|
|
|
|
let line = trimmed(text).split(separator: "\n", omittingEmptySubsequences: false).first.map(String.init) ?? ""
|
|
|
|
|
let content = UNMutableNotificationContent()
|
|
|
|
|
content.title = store.findTask(taskId).map { "byre · \($0.title)" } ?? "byre"
|
|
|
|
|
content.body = line.count > 200 ? "\(line.prefix(199))…" : line
|
|
|
|
|
content.sound = .default
|
|
|
|
|
content.userInfo = ["task_id": taskId]
|
|
|
|
|
let request = UNNotificationRequest(identifier: tag, content: content, trigger: nil)
|
|
|
|
|
Task {
|
|
|
|
|
let status = await UNUserNotificationCenter.current().notificationSettings().authorizationStatus
|
|
|
|
|
if status != .authorized && status != .provisional {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
do {
|
|
|
|
|
try await UNUserNotificationCenter.current().add(request)
|
|
|
|
|
} catch {
|
|
|
|
|
log.error("show notification: \(errorText(error), privacy: .public)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
|
func notifyInbox(_ store: Store, _ item: InboxItem) {
|
|
|
|
|
guard let me = store.me, wants(me.notificationPrefs, item) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
show(store, item.taskId, item.id, itemText(item))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
|
func notifyTurn(_ store: Store, _ ev: ByreEvent) {
|
|
|
|
|
guard let me = store.me, me.notificationPrefs.reports, ev.payload.value["summary"] as? Bool == true, let taskId = ev.taskId, !taskId.isEmpty else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
show(store, taskId, "turn-\(ev.seq)", str(ev.payload.value, "result") ?? "")
|
|
|
|
|
}
|