Skip to content

Commit 4f9ca1f

Browse files
committed
feat(computer-use): read app-command times without an offset as the Mac's local time
1 parent a10bdfd commit 4f9ca1f

4 files changed

Lines changed: 29 additions & 13 deletions

File tree

‎apps/desktop/native/computer-use/ComputerUse.swift‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func remindersStore() throws -> EKEventStore {
8686
}
8787
return EKEventStore()
8888
}
89-
/// A due date: `YYYY-MM-DD` is date-only; a date-time must be ISO 8601 with an explicit offset or `Z`,
90-
/// so the reminder never depends on the helper's time zone guess.
89+
/// A due date: `YYYY-MM-DD` is date-only; a date-time is ISO 8601, and one without an offset is this
90+
/// Mac's local time, as typing it into the app would be.
9191
func dueComponents(_ text: String) throws -> DateComponents {
9292
var calendar = Calendar(identifier: .gregorian)
9393
if text.range(of: #"^\d{4}-\d{2}-\d{2}$"#, options: .regularExpression) != nil {
@@ -96,9 +96,8 @@ func dueComponents(_ text: String) throws -> DateComponents {
9696
guard components.isValidDate else { throw ComputerError("invalid_arguments", "Due date is not a real date.", dispatchState: "not_started") }
9797
return DateComponents(year: parts[0], month: parts[1], day: parts[2])
9898
}
99-
let formatter = ISO8601DateFormatter(); formatter.formatOptions = [.withInternetDateTime]
100-
guard let date = formatter.date(from: text) else {
101-
throw ComputerError("invalid_arguments", "Due must be YYYY-MM-DD or an ISO 8601 date-time with an offset.", dispatchState: "not_started")
99+
guard let date = isoDateTime(text) else {
100+
throw ComputerError("invalid_arguments", "Due must be YYYY-MM-DD or an ISO 8601 date-time.", dispatchState: "not_started")
102101
}
103102
calendar.timeZone = .current
104103
return calendar.dateComponents([.year, .month, .day, .hour, .minute, .timeZone], from: date)
@@ -130,16 +129,25 @@ func contactStore() throws -> CNContactStore {
130129
}
131130
return CNContactStore()
132131
}
133-
/// An event boundary: an ISO 8601 date-time with an explicit offset, or `YYYY-MM-DD` for all-day events.
132+
/// An ISO 8601 date-time; without an offset it is this Mac's local time.
133+
func isoDateTime(_ text: String) -> Date? {
134+
let zoned = ISO8601DateFormatter(); zoned.formatOptions = [.withInternetDateTime]
135+
if let date = zoned.date(from: text) { return date }
136+
guard text.range(of: #"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2})?$"#, options: .regularExpression) != nil else { return nil }
137+
let local = DateFormatter(); local.locale = Locale(identifier: "en_US_POSIX"); local.timeZone = .current
138+
local.dateFormat = text.count == 16 ? "yyyy-MM-dd'T'HH:mm" : "yyyy-MM-dd'T'HH:mm:ss"
139+
local.isLenient = false
140+
return local.date(from: text)
141+
}
142+
/// An event boundary: an ISO 8601 date-time (local without an offset), or `YYYY-MM-DD` for all-day events.
134143
func eventDate(_ text: String?, _ field: String, allDay: Bool) throws -> Date {
135144
let text = try commandText(text, field, max: 64)
136145
if allDay {
137146
let parts = try dueComponents(text)
138147
guard parts.hour == nil, let date = Calendar.current.date(from: parts) else { throw invalidCommand("All-day \(field) must be YYYY-MM-DD.") }
139148
return date
140149
}
141-
let formatter = ISO8601DateFormatter(); formatter.formatOptions = [.withInternetDateTime]
142-
guard let date = formatter.date(from: text) else { throw invalidCommand("\(field) must be an ISO 8601 date-time with an offset.") }
150+
guard let date = isoDateTime(text) else { throw invalidCommand("\(field) must be an ISO 8601 date-time.") }
143151
return date
144152
}
145153
func runAppCommand(_ command: AppCommand) throws -> [String: Any] {

‎apps/desktop/native/computer-use/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Compile each architecture with `swiftc -parse-as-library -O -target arm64-apple-
88

99
Persistent newline-delimited JSON over stdin/stdout: `{id,method,params}` returns `{id,result}` or `{id,error:{code,message,dispatchState?}}`. The producer contract is `worker/packages/contracts/src/computer-use.ts` in mothership, copied into the desktop bridge by contract sync. `request_permission` is a local onboarding method, never a model tool; params are `{permission:"accessibility"|"screenCapture"|"reminders"|"calendars"|"contacts"}`. Status/preflight never triggers prompts; it reports `reminders`, `calendars` and `contacts` as `granted`, `denied` or `not_determined`.
1010

11-
`app_command` takes `{command:{name,...}}` and runs one allowlisted command through EventKit or Contacts, never Apple Events or scripts. Reads: `list_reminder_lists`, `list_calendars` and `list_contact_groups` return `{kind:"command",items:[{id,title,allowsModifications}]}`. Creates return `{kind:"command",created:{id,title,list}}`: `create_reminder` (`title`, `notes`, `due` as `YYYY-MM-DD` or ISO 8601 with an offset, `listId`, `priority` 0/1/5/9), `create_event` (`title`, `start`/`end` as ISO 8601 with offsets or `YYYY-MM-DD` with `allDay`, at most 31 days, `location`, `notes`, `calendarId`, `alertMinutesBefore`), `create_contact_group` (`title`; an existing group with that name is returned instead of a duplicate) and `create_contact` (`givenName`, `familyName`, `organization`, `jobTitle`, up to 5 `emails` and `phones`, `groupId`). Without the matching access a command fails with `reminders_permission_required`, `calendars_permission_required` or `contacts_permission_required` before touching anything. There is no update, delete or search. The desktop service confirms each create with the user before sending it.
11+
`app_command` takes `{command:{name,...}}` and runs one allowlisted command through EventKit or Contacts, never Apple Events or scripts. Reads: `list_reminder_lists`, `list_calendars` and `list_contact_groups` return `{kind:"command",items:[{id,title,allowsModifications}]}`. Creates return `{kind:"command",created:{id,title,list}}`: `create_reminder` (`title`, `notes`, `due` as `YYYY-MM-DD` or an ISO 8601 date-time, `listId`, `priority` 0/1/5/9), `create_event` (`title`, `start`/`end` as ISO 8601 date-times or `YYYY-MM-DD` with `allDay`, at most 31 days, `location`, `notes`, `calendarId`, `alertMinutesBefore`), `create_contact_group` (`title`; an existing group with that name is returned instead of a duplicate) and `create_contact` (`givenName`, `familyName`, `organization`, `jobTitle`, up to 5 `emails` and `phones`, `groupId`). A date-time without an offset is this Mac's local time, as typing it into the app would be. Without the matching access a command fails with `reminders_permission_required`, `calendars_permission_required` or `contacts_permission_required` before touching anything. There is no update, delete or search. The desktop service confirms each create with the user before sending it.
1212

1313
The helper observes up to 2000 accessibility nodes (depth 64, queue 4000, traversal deadline 8 seconds, each AX call timeout 250ms). Selected-window and focused-element ancestry are prioritized. The menu bar and menus are read in place, so an open menu's items and their submenus come first; items of closed menus are read last, capped at 80 menu nodes. Electron accessibility is enabled using its documented `AXManualAccessibility` attribute. The helper observes 100 windows and 1000 apps. Requests are bounded to 128 KiB during ingestion. Text/value input is capped at 32000 UTF-16 units. State references are per-process, per-app, expire after 60 seconds, and are consumed by every action that reaches the app; a request that fails before sending anything reports `dispatchState: "not_started"` and keeps them valid. An Accessibility failure counts as sent unless Accessibility rejected the request before the app saw it (unsupported attribute or action, illegal argument). Restarts invalidate all references. Screen captures exclude window shadows, preserve the selected window’s coordinate geometry, and are capped to 1600 pixels on the longest side and 8 MiB encoded. Complete JSON replies are capped at 16 MiB: oversized observations omit screenshots first, then truncate accessibility nodes, while preserving action outcomes. Screenshots are optional and failure is reported in `screenshotError` without discarding accessibility state. macOS 26 uses the screenshot-specific capture API; the macOS 14/15 API path includes only the selected window in a display filter and crops its exact frame. That older path requires the window to fit within one display and reports `capture_geometry_unavailable` otherwise.
1414

‎apps/desktop/native/computer-use/tests/NativeTests.swift‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ import AppKit
4040
precondition(dateOnly.year == 2027 && dateOnly.month == 5 && dateOnly.day == 4 && dateOnly.hour == nil)
4141
let timed = try dueComponents("2027-05-04T09:30:00Z")
4242
precondition(timed.hour != nil && timed.minute != nil)
43-
for due in ["2027-02-30", "2027-05-04T09:30:00", "May 4", "2027-5-4"] {
43+
let localDue = try dueComponents("2027-05-04T09:30")
44+
precondition(localDue.hour == 9 && localDue.minute == 30)
45+
for due in ["2027-02-30", "2027-05-04T25:30:00", "May 4", "2027-5-4"] {
4446
expectError("invalid_arguments") { _ = try dueComponents(due) }
4547
}
4648
expectError("unknown_command") { _ = try runAppCommand(AppCommand(name: "run_script")) }
4749
expectError("invalid_arguments") { _ = try runAppCommand(AppCommand(name: "create_reminder", title: " ")) }
4850
expectError("invalid_arguments") { _ = try runAppCommand(AppCommand(name: "create_reminder", title: "Pay rent", priority: 3)) }
49-
expectError("invalid_arguments") { _ = try eventDate("2027-06-09T07:15:00", "Start", allDay: false) }
51+
var local = Calendar(identifier: .gregorian); local.timeZone = .current
52+
precondition(local.dateComponents([.hour, .minute], from: try eventDate("2027-06-09T07:15:00", "Start", allDay: false)) == DateComponents(hour: 7, minute: 15))
53+
expectError("invalid_arguments") { _ = try eventDate("2027-06-09 07:15", "Start", allDay: false) }
5054
expectError("invalid_arguments") { _ = try eventDate("2027-06-09T07:15:00+01:00", "Start", allDay: true) }
5155
precondition(try eventDate("2027-06-09T07:15:00+01:00", "Start", allDay: false) == Date(timeIntervalSince1970: 1_812_521_700))
5256
expectError("invalid_arguments") { _ = try runAppCommand(AppCommand(name: "create_event", title: "Flight", start: "2027-06-09T08:10:00Z", end: "2027-06-09T07:15:00Z")) }

‎packages/desktop-bridge/src/computer-use.generated.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ const EventBoundary = z
236236
.string()
237237
.min(10)
238238
.max(64)
239-
.describe("ISO 8601 date-time with an explicit offset, or YYYY-MM-DD when allDay is true.");
239+
.describe(
240+
"ISO 8601 date-time, or YYYY-MM-DD when allDay is true. Without an offset it is this Mac's local time, as typing it into Calendar would be.",
241+
);
240242

241243
/** Typed app commands: one allowlisted create or list read through the OS's own data frameworks, never a script. */
242244
export const AppCommandSchema = z.discriminatedUnion("name", [
@@ -250,7 +252,9 @@ export const AppCommandSchema = z.discriminatedUnion("name", [
250252
.min(10)
251253
.max(64)
252254
.optional()
253-
.describe("YYYY-MM-DD for a date-only reminder, or an ISO 8601 date-time with an explicit offset."),
255+
.describe(
256+
"YYYY-MM-DD for a date-only reminder, or an ISO 8601 date-time (this Mac's local time without an offset).",
257+
),
254258
listId: z
255259
.string()
256260
.min(1)

0 commit comments

Comments
 (0)