|
1 | 1 | import AppKit |
2 | 2 | import ApplicationServices |
| 3 | +import EventKit |
3 | 4 | import ScreenCaptureKit |
4 | 5 |
|
5 | 6 | nonisolated(unsafe) var cancellationRequested: sig_atomic_t = 0 |
@@ -39,6 +40,85 @@ struct Parameters: Decodable { |
39 | 40 | var button: String? |
40 | 41 | var clickCount: Int? |
41 | 42 | var accessibilityAction: String? |
| 43 | + var command: AppCommand? |
| 44 | +} |
| 45 | +/// One allowlisted app command. Commands go through EventKit, never Apple Events or scripts, so they |
| 46 | +/// cannot reach a shell; each is a typed create or a list read, with no update, delete or search. |
| 47 | +struct AppCommand: Decodable { |
| 48 | + var name: String |
| 49 | + var title: String? |
| 50 | + var notes: String? |
| 51 | + var due: String? |
| 52 | + var listId: String? |
| 53 | + var priority: Int? |
| 54 | +} |
| 55 | +/// The event store for reminders, available only once the user has granted full Reminders access. |
| 56 | +/// Checking never prompts; the grant happens from settings, outside the tool call. |
| 57 | +func remindersStore() throws -> EKEventStore { |
| 58 | + guard EKEventStore.authorizationStatus(for: .reminder) == .fullAccess else { |
| 59 | + throw ComputerError("reminders_permission_required", "Allow Reminders access for Mothership in System Settings, then retry.", dispatchState: "not_started") |
| 60 | + } |
| 61 | + return EKEventStore() |
| 62 | +} |
| 63 | +/// A due date: `YYYY-MM-DD` is date-only; a date-time must be ISO 8601 with an explicit offset or `Z`, |
| 64 | +/// so the reminder never depends on the helper's time zone guess. |
| 65 | +func dueComponents(_ text: String) throws -> DateComponents { |
| 66 | + var calendar = Calendar(identifier: .gregorian) |
| 67 | + if text.range(of: #"^\d{4}-\d{2}-\d{2}$"#, options: .regularExpression) != nil { |
| 68 | + let parts = text.split(separator: "-").compactMap { Int($0) } |
| 69 | + let components = DateComponents(calendar: calendar, year: parts[0], month: parts[1], day: parts[2]) |
| 70 | + guard components.isValidDate else { throw ComputerError("invalid_arguments", "Due date is not a real date.", dispatchState: "not_started") } |
| 71 | + return DateComponents(year: parts[0], month: parts[1], day: parts[2]) |
| 72 | + } |
| 73 | + let formatter = ISO8601DateFormatter(); formatter.formatOptions = [.withInternetDateTime] |
| 74 | + guard let date = formatter.date(from: text) else { |
| 75 | + throw ComputerError("invalid_arguments", "Due must be YYYY-MM-DD or an ISO 8601 date-time with an offset.", dispatchState: "not_started") |
| 76 | + } |
| 77 | + calendar.timeZone = .current |
| 78 | + return calendar.dateComponents([.year, .month, .day, .hour, .minute, .timeZone], from: date) |
| 79 | +} |
| 80 | +/// Reminders access as the status reports it, read without prompting. |
| 81 | +func remindersAuthorization() -> String { |
| 82 | + switch EKEventStore.authorizationStatus(for: .reminder) { |
| 83 | + case .fullAccess: return "granted" |
| 84 | + case .notDetermined: return "not_determined" |
| 85 | + default: return "denied" |
| 86 | + } |
| 87 | +} |
| 88 | +func runAppCommand(_ command: AppCommand) throws -> [String: Any] { |
| 89 | + switch command.name { |
| 90 | + case "list_reminder_lists": |
| 91 | + let lists = try remindersStore().calendars(for: .reminder).prefix(200).map { list -> [String: Any] in |
| 92 | + ["id": list.calendarIdentifier, "title": String(list.title.prefix(1024)), "allowsModifications": list.allowsContentModifications] |
| 93 | + } |
| 94 | + return ["kind": "command", "command": command.name, "items": lists] |
| 95 | + case "create_reminder": |
| 96 | + let title = (command.title ?? "").trimmingCharacters(in: .whitespacesAndNewlines) |
| 97 | + guard !title.isEmpty, title.count <= 1024 else { throw ComputerError("invalid_arguments", "A reminder needs a title of at most 1024 characters.", dispatchState: "not_started") } |
| 98 | + guard (command.notes?.count ?? 0) <= 8000 else { throw ComputerError("invalid_arguments", "Notes are limited to 8000 characters.", dispatchState: "not_started") } |
| 99 | + guard [0, 1, 5, 9].contains(command.priority ?? 0) else { throw ComputerError("invalid_arguments", "Priority must be 0 (none), 1 (high), 5 (medium) or 9 (low).", dispatchState: "not_started") } |
| 100 | + let due = try command.due.map(dueComponents) |
| 101 | + let store = try remindersStore() |
| 102 | + let list: EKCalendar |
| 103 | + if let listId = command.listId { |
| 104 | + guard let found = store.calendar(withIdentifier: listId), found.allowedEntityTypes.contains(.reminder) else { |
| 105 | + throw ComputerError("list_unavailable", "No reminder list has this id; call list_reminder_lists.", dispatchState: "not_started") |
| 106 | + } |
| 107 | + list = found |
| 108 | + } else { |
| 109 | + guard let fallback = store.defaultCalendarForNewReminders() else { throw ComputerError("list_unavailable", "Reminders has no default list; pass listId.", dispatchState: "not_started") } |
| 110 | + list = fallback |
| 111 | + } |
| 112 | + guard list.allowsContentModifications else { throw ComputerError("list_read_only", "This reminder list is read-only.", dispatchState: "not_started") } |
| 113 | + let reminder = EKReminder(eventStore: store) |
| 114 | + reminder.calendar = list; reminder.title = title; reminder.notes = command.notes |
| 115 | + reminder.priority = command.priority ?? 0; reminder.dueDateComponents = due |
| 116 | + inputDispatched = true |
| 117 | + try store.save(reminder, commit: true) |
| 118 | + return ["kind": "command", "command": command.name, "created": ["id": reminder.calendarItemIdentifier, "title": title, "list": list.title]] |
| 119 | + default: |
| 120 | + throw ComputerError("unknown_command", "Unknown app command.", dispatchState: "not_started") |
| 121 | + } |
42 | 122 | } |
43 | 123 | struct Request: Decodable { let id: String; let method: String; let params: Parameters } |
44 | 124 | func required<T>(_ value: T?, _ name: String) throws -> T { |
@@ -798,11 +878,15 @@ final class Driver { |
798 | 878 | let options = [kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true] as CFDictionary |
799 | 879 | _ = AXIsProcessTrustedWithOptions(options) |
800 | 880 | case "screenCapture": _ = CGRequestScreenCaptureAccess() |
| 881 | + case "reminders": _ = try? await EKEventStore().requestFullAccessToReminders() |
801 | 882 | default: throw ComputerError("invalid_arguments", "Unknown permission.") |
802 | 883 | } |
803 | | - return ["kind": "status", "platform": "darwin", "accessibility": AXIsProcessTrusted(), "screenRecording": CGPreflightScreenCaptureAccess()] |
804 | | - case "status": return ["kind": "status", "platform": "darwin", "accessibility": AXIsProcessTrusted(), "screenRecording": CGPreflightScreenCaptureAccess()] |
| 884 | + return ["kind": "status", "platform": "darwin", "accessibility": AXIsProcessTrusted(), "screenRecording": CGPreflightScreenCaptureAccess(), "reminders": remindersAuthorization()] |
| 885 | + case "status": return ["kind": "status", "platform": "darwin", "accessibility": AXIsProcessTrusted(), "screenRecording": CGPreflightScreenCaptureAccess(), "reminders": remindersAuthorization()] |
805 | 886 | case "list_apps": return ["kind": "apps", "apps": try discoverApps()] |
| 887 | + case "app_command": |
| 888 | + inputDispatched = false |
| 889 | + return try runAppCommand(required(p.command, "command")) |
806 | 890 | case "activate_app": |
807 | 891 | try requireAccessibility() |
808 | 892 | let app = try await appFor(p.bundleId, launch: true) |
|
0 commit comments