Skip to content

Commit 47ecb4f

Browse files
committed
fix(computer-use): confirm trimmed app-command values and flag truncated lists
1 parent 17dd8c2 commit 47ecb4f

4 files changed

Lines changed: 24 additions & 8 deletions

File tree

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ func eventDate(_ text: String?, _ field: String, allDay: Bool) throws -> Date {
162162
func runAppCommand(_ command: AppCommand) throws -> [String: Any] {
163163
switch command.name {
164164
case "list_reminder_lists":
165-
let lists = try remindersStore().calendars(for: .reminder).prefix(200).map { list -> [String: Any] in
165+
let allLists = try remindersStore().calendars(for: .reminder)
166+
let lists = allLists.prefix(200).map { list -> [String: Any] in
166167
["id": list.calendarIdentifier, "title": String(list.title.prefix(1024)), "allowsModifications": list.allowsContentModifications]
167168
}
168-
return ["kind": "command", "command": command.name, "items": lists]
169+
return ["kind": "command", "command": command.name, "items": lists, "truncated": allLists.count > 200]
169170
case "create_reminder":
170171
let title = (command.title ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
171172
guard !title.isEmpty, title.count <= 1024 else { throw ComputerError("invalid_arguments", "A reminder needs a title of at most 1024 characters.", dispatchState: "not_started") }
@@ -192,10 +193,11 @@ func runAppCommand(_ command: AppCommand) throws -> [String: Any] {
192193
try store.save(reminder, commit: true)
193194
return ["kind": "command", "command": command.name, "created": ["id": reminder.calendarItemIdentifier, "title": title, "list": list.title]]
194195
case "list_calendars":
195-
let calendars = try calendarStore().calendars(for: .event).prefix(200).map { calendar -> [String: Any] in
196+
let allCalendars = try calendarStore().calendars(for: .event)
197+
let calendars = allCalendars.prefix(200).map { calendar -> [String: Any] in
196198
["id": calendar.calendarIdentifier, "title": String(calendar.title.prefix(1024)), "allowsModifications": calendar.allowsContentModifications]
197199
}
198-
return ["kind": "command", "command": command.name, "items": calendars]
200+
return ["kind": "command", "command": command.name, "items": calendars, "truncated": allCalendars.count > 200]
199201
case "create_event":
200202
let title = try commandText(command.title, "Title")
201203
let allDay = command.allDay ?? false
@@ -230,10 +232,11 @@ func runAppCommand(_ command: AppCommand) throws -> [String: Any] {
230232
try store.save(event, span: .thisEvent, commit: true)
231233
return ["kind": "command", "command": command.name, "created": ["id": event.eventIdentifier ?? event.calendarItemIdentifier, "title": title, "list": calendar.title]]
232234
case "list_contact_groups":
233-
let groups = try contactStore().groups(matching: nil).prefix(200).map { group -> [String: Any] in
235+
let allGroups = try contactStore().groups(matching: nil)
236+
let groups = allGroups.prefix(200).map { group -> [String: Any] in
234237
["id": group.identifier, "title": String(group.name.prefix(1024)), "allowsModifications": true]
235238
}
236-
return ["kind": "command", "command": command.name, "items": groups]
239+
return ["kind": "command", "command": command.name, "items": groups, "truncated": allGroups.count > 200]
237240
case "create_contact_group":
238241
let name = try commandText(command.title, "Group name")
239242
let store = try contactStore()

‎apps/desktop/src/main/computer-use/service.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ describe('native computer use authority and lifecycle', () => {
137137
})
138138
expect(native.request.mock.calls.map(([method]) => method)).not.toContain('app_command')
139139
expect(confirmAppCommand.mock.calls[0]?.[0].summary).toContain('Priority: High')
140+
await expect(
141+
service.execute('padded', 'chat', {
142+
action: 'app_command',
143+
command: { name: 'create_reminder', title: ' Pay rent ' },
144+
})
145+
).resolves.toMatchObject({ kind: 'command' })
146+
expect(confirmAppCommand.mock.calls[1]?.[0].summary.split('\n')[0]).toBe('Reminder: Pay rent')
140147

141148
await expect(service.execute('confirmed', 'chat', create)).resolves.toMatchObject({
142149
kind: 'command',

‎apps/desktop/src/main/computer-use/service.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ function workBundleId(input: ComputerUseWithAppCommandsInput): string | undefine
7070

7171
/** The exact fields a write will save, for the user to confirm; null for a read, which needs none. */
7272
function writeSummary(command: AppCommand): string | null {
73+
// Trimmed exactly as the helper trims before saving, so the user confirms the saved values.
7374
const lines = (entries: Array<[string, string | number | undefined]>) =>
7475
entries
76+
.map(([label, value]) => [label, typeof value === 'string' ? value.trim() : value] as const)
7577
.filter(([, value]) => value !== undefined && value !== '')
7678
.map(([label, value]) => `${label}: ${value}`)
7779
.join('\n')
@@ -107,8 +109,8 @@ function writeSummary(command: AppCommand): string | null {
107109
['Contact', [command.givenName, command.familyName].filter(Boolean).join(' ') || undefined],
108110
['Organization', command.organization],
109111
['Job title', command.jobTitle],
110-
['Emails', command.emails?.join(', ')],
111-
['Phones', command.phones?.join(', ')],
112+
['Emails', command.emails?.map((email) => email.trim()).join(', ')],
113+
['Phones', command.phones?.map((phone) => phone.trim()).join(', ')],
112114
['Group', command.groupId],
113115
])
114116
default:

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,10 @@ export const ComputerUseCommandResultSchema = z
425425
})
426426
.optional(),
427427
items: z.array(CommandItem).max(200).optional(),
428+
truncated: z
429+
.boolean()
430+
.optional()
431+
.describe("True when more than 200 items exist and only the first 200 are listed."),
428432
})
429433
.refine(
430434
(result) =>

0 commit comments

Comments
 (0)