|
| 1 | +import { z } from 'zod' |
| 2 | +import { desktopToolCallIdSchema } from '@/lib/api/contracts/desktop-tool-authorization' |
| 3 | +import { defineRouteContract } from '@/lib/api/contracts/types' |
| 4 | + |
| 5 | +/** |
| 6 | + * The desktop background executor's wire protocol. The Electron main process calls every route |
| 7 | + * here with the cookie of the Better Auth session it registered under; Sim refuses any other |
| 8 | + * session for that device. |
| 9 | + */ |
| 10 | + |
| 11 | +/** The install id the desktop app generates once and keeps in its user data. */ |
| 12 | +const desktopDeviceIdSchema = z.string().uuid('Device ID must be a UUID') |
| 13 | + |
| 14 | +const desktopDeviceCapabilitiesSchema = z.object({ |
| 15 | + executor: z.number().int().min(1).max(1000), |
| 16 | + browser: z.boolean(), |
| 17 | + terminal: z.boolean(), |
| 18 | + localFiles: z.boolean(), |
| 19 | +}) |
| 20 | + |
| 21 | +const registerDesktopDeviceBodySchema = z.object({ |
| 22 | + deviceId: desktopDeviceIdSchema, |
| 23 | + name: z.string().trim().min(1, 'Device name is required').max(128, 'Device name is too long'), |
| 24 | + appVersion: z.string().trim().min(1, 'App version is required').max(64), |
| 25 | + platform: z.string().trim().min(1, 'Platform is required').max(64), |
| 26 | + capabilities: desktopDeviceCapabilitiesSchema, |
| 27 | +}) |
| 28 | +export type RegisterDesktopDeviceBody = z.input<typeof registerDesktopDeviceBodySchema> |
| 29 | + |
| 30 | +/** |
| 31 | + * `enabled: false` is the kill switch: the device keeps finishing calls on runs already bound to |
| 32 | + * it, but no new turn binds to it. |
| 33 | + */ |
| 34 | +export const registerDesktopDeviceResponseSchema = z.object({ |
| 35 | + enabled: z.boolean(), |
| 36 | + protocolVersion: z.number().int().min(1), |
| 37 | + leaseMs: z.number().int().positive(), |
| 38 | + leaseRenewMs: z.number().int().positive(), |
| 39 | + pickupGraceMs: z.number().int().positive(), |
| 40 | + activeReconcileMs: z.number().int().positive(), |
| 41 | + idleReconcileMs: z.number().int().positive(), |
| 42 | +}) |
| 43 | + |
| 44 | +export const registerDesktopDeviceContract = defineRouteContract({ |
| 45 | + method: 'POST', |
| 46 | + path: '/api/desktop/devices', |
| 47 | + body: registerDesktopDeviceBodySchema, |
| 48 | + response: { mode: 'json', schema: registerDesktopDeviceResponseSchema }, |
| 49 | + error: z.object({ error: z.string() }), |
| 50 | +}) |
| 51 | + |
| 52 | +const desktopInboxQuerySchema = z.object({ deviceId: desktopDeviceIdSchema }) |
| 53 | + |
| 54 | +const desktopInboxCallSchema = z.object({ |
| 55 | + kind: z.literal('call'), |
| 56 | + toolCallId: z.string().min(1), |
| 57 | + toolName: z.string().min(1), |
| 58 | + chatId: z.string().min(1), |
| 59 | + workspaceId: z.string().nullable(), |
| 60 | + createdAt: z.string(), |
| 61 | +}) |
| 62 | + |
| 63 | +const desktopInboxApprovalSchema = z.object({ |
| 64 | + kind: z.literal('approval_needed'), |
| 65 | + toolCallId: z.string().min(1), |
| 66 | + toolName: z.string().min(1), |
| 67 | + chatId: z.string().min(1), |
| 68 | + chatTitle: z.string().nullable(), |
| 69 | + workspaceId: z.string().nullable(), |
| 70 | + /** A short description of what the call would do, such as the command to run. */ |
| 71 | + summary: z.string().nullable(), |
| 72 | +}) |
| 73 | + |
| 74 | +const desktopInboxCancelSchema = z.object({ |
| 75 | + kind: z.literal('cancel'), |
| 76 | + toolCallId: z.string().min(1), |
| 77 | +}) |
| 78 | + |
| 79 | +/** Ordered by when each call was persisted, which is the order the executor claims them in. */ |
| 80 | +const desktopInboxItemSchema = z.discriminatedUnion('kind', [ |
| 81 | + desktopInboxCallSchema, |
| 82 | + desktopInboxApprovalSchema, |
| 83 | + desktopInboxCancelSchema, |
| 84 | +]) |
| 85 | +export type DesktopInboxItem = z.output<typeof desktopInboxItemSchema> |
| 86 | + |
| 87 | +export const desktopInboxResponseSchema = z.object({ |
| 88 | + items: z.array(desktopInboxItemSchema), |
| 89 | + /** True while any run bound to this device is active, so the device pulls at the active cadence. */ |
| 90 | + hasActiveRun: z.boolean(), |
| 91 | +}) |
| 92 | + |
| 93 | +export const listDesktopInboxContract = defineRouteContract({ |
| 94 | + method: 'GET', |
| 95 | + path: '/api/desktop/inbox', |
| 96 | + query: desktopInboxQuerySchema, |
| 97 | + response: { mode: 'json', schema: desktopInboxResponseSchema }, |
| 98 | + error: z.object({ error: z.string() }), |
| 99 | +}) |
| 100 | + |
| 101 | +/** |
| 102 | + * Server-sent events: `inbox_changed` with `{ reason: 'call' | 'approval' | 'cancel' }`, plus |
| 103 | + * heartbeat comments and a `rotate` event before the server closes a long-lived stream. Events |
| 104 | + * are hints; the device re-reads `GET /api/desktop/inbox` on each one. While the stream is open |
| 105 | + * the device counts as online. |
| 106 | + */ |
| 107 | +export const desktopInboxStreamContract = defineRouteContract({ |
| 108 | + method: 'GET', |
| 109 | + path: '/api/desktop/inbox/stream', |
| 110 | + query: desktopInboxQuerySchema, |
| 111 | + response: { mode: 'stream' }, |
| 112 | +}) |
| 113 | + |
| 114 | +const claimDesktopToolBodySchema = z.object({ |
| 115 | + deviceId: desktopDeviceIdSchema, |
| 116 | + toolCallId: desktopToolCallIdSchema, |
| 117 | +}) |
| 118 | +export type ClaimDesktopToolBody = z.input<typeof claimDesktopToolBodySchema> |
| 119 | + |
| 120 | +export const claimDesktopToolResponseSchema = z.object({ |
| 121 | + toolName: z.string().min(1), |
| 122 | + args: z.record(z.string(), z.unknown()), |
| 123 | + chatId: z.string().min(1), |
| 124 | + workspaceId: z.string().nullable(), |
| 125 | + /** Presented on every lease renewal and on completion; it fences out stale owners. */ |
| 126 | + executionToken: z.string().min(1), |
| 127 | + leaseExpiresAt: z.string(), |
| 128 | +}) |
| 129 | + |
| 130 | +export const claimDesktopToolContract = defineRouteContract({ |
| 131 | + method: 'POST', |
| 132 | + path: '/api/desktop/tool/claim', |
| 133 | + body: claimDesktopToolBodySchema, |
| 134 | + response: { mode: 'json', schema: claimDesktopToolResponseSchema }, |
| 135 | + error: z.object({ error: z.string() }), |
| 136 | +}) |
| 137 | + |
| 138 | +const renewDesktopToolLeaseBodySchema = z.object({ |
| 139 | + deviceId: desktopDeviceIdSchema, |
| 140 | + toolCallId: desktopToolCallIdSchema, |
| 141 | + executionToken: z.string().min(1).max(128), |
| 142 | +}) |
| 143 | +export type RenewDesktopToolLeaseBody = z.input<typeof renewDesktopToolLeaseBodySchema> |
| 144 | + |
| 145 | +export const renewDesktopToolLeaseResponseSchema = z.object({ leaseExpiresAt: z.string() }) |
| 146 | + |
| 147 | +/** A 410 means the call was stopped, settled, or its lease lapsed: cancel the local action. */ |
| 148 | +export const renewDesktopToolLeaseContract = defineRouteContract({ |
| 149 | + method: 'POST', |
| 150 | + path: '/api/desktop/tool/lease', |
| 151 | + body: renewDesktopToolLeaseBodySchema, |
| 152 | + response: { mode: 'json', schema: renewDesktopToolLeaseResponseSchema }, |
| 153 | + error: z.object({ error: z.string() }), |
| 154 | +}) |
| 155 | + |
| 156 | +const completeDesktopToolBodySchema = z.object({ |
| 157 | + deviceId: desktopDeviceIdSchema, |
| 158 | + toolCallId: desktopToolCallIdSchema, |
| 159 | + executionToken: z.string().min(1).max(128), |
| 160 | + status: z.enum(['success', 'error', 'cancelled']), |
| 161 | + message: z.string().max(10_000).optional(), |
| 162 | + data: z.unknown().optional(), |
| 163 | +}) |
| 164 | +export type CompleteDesktopToolBody = z.input<typeof completeDesktopToolBodySchema> |
| 165 | + |
| 166 | +/** |
| 167 | + * Every 200 is an acknowledgement the device's outbox can drop the result on. `recorded`: this |
| 168 | + * request settled the call. `duplicate`: an earlier request with the same token already did. |
| 169 | + * `superseded`: Sim settled the call first (Stop, or a lapsed lease), and `status` is what the |
| 170 | + * model received. |
| 171 | + */ |
| 172 | +export const completeDesktopToolResponseSchema = z.object({ |
| 173 | + outcome: z.enum(['recorded', 'duplicate', 'superseded']), |
| 174 | + status: z.enum(['completed', 'failed', 'cancelled']), |
| 175 | +}) |
| 176 | + |
| 177 | +export const completeDesktopToolContract = defineRouteContract({ |
| 178 | + method: 'POST', |
| 179 | + path: '/api/desktop/tool/complete', |
| 180 | + body: completeDesktopToolBodySchema, |
| 181 | + response: { mode: 'json', schema: completeDesktopToolResponseSchema }, |
| 182 | + error: z.object({ error: z.string() }), |
| 183 | +}) |
0 commit comments