Skip to content

Commit 384fa3f

Browse files
committed
fix(computer-use): accept fractional-second app-command times and bound contact titles
1 parent 484f75f commit 384fa3f

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,17 @@ func localGregorian() -> Calendar {
136136
}
137137
/// An ISO 8601 date-time; without an offset it is this Mac's local time.
138138
func isoDateTime(_ text: String) -> Date? {
139-
let zoned = ISO8601DateFormatter(); zoned.formatOptions = [.withInternetDateTime]
140-
if let date = zoned.date(from: text) { return date }
141-
guard text.range(of: #"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2})?$"#, options: .regularExpression) != nil else { return nil }
139+
for options: ISO8601DateFormatter.Options in [[.withInternetDateTime], [.withInternetDateTime, .withFractionalSeconds]] {
140+
let zoned = ISO8601DateFormatter(); zoned.formatOptions = options
141+
if let date = zoned.date(from: text) { return date }
142+
}
143+
// Fractions of a second do not matter to a calendar item, so a local time drops them.
144+
guard text.range(of: #"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d{1,9})?)?$"#, options: .regularExpression) != nil else { return nil }
145+
let whole = String(text.prefix(19))
142146
let local = DateFormatter(); local.locale = Locale(identifier: "en_US_POSIX"); local.timeZone = .current
143-
local.dateFormat = text.count == 16 ? "yyyy-MM-dd'T'HH:mm" : "yyyy-MM-dd'T'HH:mm:ss"
147+
local.dateFormat = whole.count == 16 ? "yyyy-MM-dd'T'HH:mm" : "yyyy-MM-dd'T'HH:mm:ss"
144148
local.isLenient = false
145-
return local.date(from: text)
149+
return local.date(from: whole)
146150
}
147151
/// An event boundary: an ISO 8601 date-time (local without an offset), or `YYYY-MM-DD` for all-day events.
148152
func eventDate(_ text: String?, _ field: String, allDay: Bool) throws -> Date {
@@ -272,7 +276,8 @@ func runAppCommand(_ command: AppCommand) throws -> [String: Any] {
272276
inputDispatched = true
273277
try store.execute(request)
274278
let display = [given, family].compactMap { $0 }.joined(separator: " ")
275-
return ["kind": "command", "command": command.name, "created": ["id": contact.identifier, "title": display.isEmpty ? organization ?? "" : display, "list": group?.name ?? "Contacts"]]
279+
let title = String((display.isEmpty ? organization ?? "" : display).prefix(1024))
280+
return ["kind": "command", "command": command.name, "created": ["id": contact.identifier, "title": title, "list": String((group?.name ?? "Contacts").prefix(1024))]]
276281
default:
277282
throw ComputerError("unknown_command", "Unknown app command.", dispatchState: "not_started")
278283
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ import AppKit
5252
let floating = try eventDate("2027-06-09T07:15:00", "Start", allDay: false)
5353
precondition(local.dateComponents([.hour, .minute], from: floating) == DateComponents(hour: 7, minute: 15))
5454
expectError("invalid_arguments") { _ = try eventDate("2027-06-09 07:15", "Start", allDay: false) }
55+
let fractional = try eventDate("2027-06-09T06:15:00.000Z", "Start", allDay: false)
56+
precondition(fractional == Date(timeIntervalSince1970: 1_812_521_700))
57+
let localFraction = try eventDate("2027-06-09T07:15:00.250", "Start", allDay: false)
58+
precondition(local.dateComponents([.hour, .minute], from: localFraction) == DateComponents(hour: 7, minute: 15))
5559
expectError("invalid_arguments") { _ = try eventDate("2027-06-09T07:15:00+01:00", "Start", allDay: true) }
5660
let zoned = try eventDate("2027-06-09T07:15:00+01:00", "Start", allDay: false)
5761
precondition(zoned == Date(timeIntervalSince1970: 1_812_521_700))

0 commit comments

Comments
 (0)