The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | AgentMessage, |
| 3 | AgentOptions, |
| 4 | InteractionUpdate, |
| 5 | RunResult, |
| 6 | SDKUserMessage, |
| 7 | SendOptions, |
| 8 | SteerAckOutcome, |
| 9 | } from "@cursor/sdk"; |
| 10 | import type { OrchestrationV2ProviderSession, ThreadId } from "@t3tools/contracts"; |
| 11 | |
| 12 | import type { |
| 13 | CursorAgentSdkOpenInput, |
| 14 | CursorAgentSdkProtocolLogEvent, |
| 15 | CursorAgentSdkTextGenerateInput, |
| 16 | CursorContextUsage, |
| 17 | } from "./CursorAgentSdk.ts"; |
| 18 | import { type CursorSdkErrorKind, getCursorSdkErrorKind, tagCursorSdkError } from "./sdk.ts"; |
| 19 | |
| 20 | export type SerializedBackgroundShellEvent = |
| 21 | | { |
| 22 | readonly type: "started"; |
| 23 | readonly id: string; |
| 24 | readonly pid: number | undefined; |
| 25 | readonly command: string; |
| 26 | } |
| 27 | | { |
| 28 | readonly type: "ended"; |
| 29 | readonly id: string; |
| 30 | readonly pid: number | undefined; |
| 31 | readonly command: string; |
| 32 | readonly stopped: boolean; |
| 33 | readonly exitCode: number | undefined; |
| 34 | readonly outputPath: string | undefined; |
| 35 | }; |
| 36 | |
| 37 | export interface SerializedWorkerError { |
| 38 | readonly name: string; |
| 39 | readonly message: string; |
| 40 | readonly stack?: string; |
| 41 | readonly code?: string; |
| 42 | readonly status?: number; |
| 43 | readonly sdkErrorKind?: CursorSdkErrorKind; |
| 44 | readonly cause?: SerializedWorkerError; |
| 45 | } |
| 46 | |
| 47 | export interface SerializedRunnerFailure { |
| 48 | readonly method: string; |
| 49 | readonly cause: SerializedWorkerError; |
| 50 | } |
| 51 | |
| 52 | export type SerializedRunResult = Omit<RunResult, "error"> & { |
| 53 | readonly error?: SerializedWorkerError | undefined; |
| 54 | }; |
| 55 | |
| 56 | export type CursorWorkerRequest = |
| 57 | | { |
| 58 | readonly id: number; |
| 59 | readonly type: "open"; |
| 60 | readonly operation: CursorAgentSdkOpenInput["operation"]; |
| 61 | readonly agentId?: string; |
| 62 | readonly storeCwd?: string; |
| 63 | readonly options: AgentOptions; |
| 64 | readonly threadId: ThreadId; |
| 65 | readonly providerSessionId: OrchestrationV2ProviderSession["id"]; |
| 66 | } |
| 67 | | { |
| 68 | readonly id: number; |
| 69 | readonly type: "send"; |
| 70 | readonly sessionId: number; |
| 71 | readonly message: string | SDKUserMessage; |
| 72 | readonly options?: Omit<SendOptions, "onDelta">; |
| 73 | readonly summarize?: boolean; |
| 74 | readonly resume?: boolean; |
| 75 | readonly listenBackgroundShells?: boolean; |
| 76 | } |
| 77 | | { |
| 78 | readonly id: number; |
| 79 | readonly type: "stopBackgroundShell"; |
| 80 | readonly sessionId: number; |
| 81 | readonly shellId: string; |
| 82 | } |
| 83 | | { |
| 84 | readonly id: number; |
| 85 | readonly type: "steer"; |
| 86 | readonly sendId: number; |
| 87 | readonly text: string; |
| 88 | } |
| 89 | | { |
| 90 | readonly id: number; |
| 91 | readonly type: "cancel"; |
| 92 | readonly sendId: number; |
| 93 | } |
| 94 | | { |
| 95 | readonly id: number; |
| 96 | readonly type: "listMessages"; |
| 97 | readonly sessionId: number; |
| 98 | } |
| 99 | | { |
| 100 | readonly id: number; |
| 101 | readonly type: "readContextUsage"; |
| 102 | readonly sessionId: number; |
| 103 | } |
| 104 | | { |
| 105 | readonly id: number; |
| 106 | readonly type: "runCheckpointed"; |
| 107 | readonly sessionId: number; |
| 108 | readonly runId?: string; |
| 109 | } |
| 110 | | { |
| 111 | readonly id: number; |
| 112 | readonly type: "rewindAfterRun"; |
| 113 | readonly sessionId: number; |
| 114 | readonly runId: string; |
| 115 | } |
| 116 | | { |
| 117 | readonly id: number; |
| 118 | readonly type: "closeSession"; |
| 119 | readonly sessionId: number; |
| 120 | } |
| 121 | | { |
| 122 | readonly id: number; |
| 123 | readonly type: "cancelTextGenerate"; |
| 124 | readonly textGenerateId: number; |
| 125 | } |
| 126 | | { |
| 127 | /** Closes every session and exits the worker. */ |
| 128 | readonly id: number; |
| 129 | readonly type: "close"; |
| 130 | } |
| 131 | | ({ |
| 132 | readonly id: number; |
| 133 | readonly type: "textGenerate"; |
| 134 | } & CursorAgentSdkTextGenerateInput); |
| 135 | |
| 136 | export type CursorWorkerResponsePayload = |
| 137 | | { readonly agentId: string } |
| 138 | | { readonly runId: string; readonly agentId: string } |
| 139 | | SteerAckOutcome |
| 140 | | ReadonlyArray<AgentMessage> |
| 141 | | CursorContextUsage |
| 142 | | SerializedRunResult |
| 143 | | boolean |
| 144 | | null; |
| 145 | |
| 146 | export type CursorWorkerMessage = |
| 147 | | { |
| 148 | readonly type: "response"; |
| 149 | readonly id: number; |
| 150 | readonly ok: true; |
| 151 | readonly result: CursorWorkerResponsePayload; |
| 152 | } |
| 153 | | { |
| 154 | readonly type: "response"; |
| 155 | readonly id: number; |
| 156 | readonly ok: false; |
| 157 | readonly error: SerializedRunnerFailure; |
| 158 | } |
| 159 | | { |
| 160 | readonly type: "delta"; |
| 161 | readonly sendId: number; |
| 162 | readonly update: InteractionUpdate; |
| 163 | } |
| 164 | | { |
| 165 | readonly type: "runDone"; |
| 166 | readonly sendId: number; |
| 167 | readonly ok: true; |
| 168 | readonly result: SerializedRunResult; |
| 169 | } |
| 170 | | { |
| 171 | readonly type: "runDone"; |
| 172 | readonly sendId: number; |
| 173 | readonly ok: false; |
| 174 | readonly error: SerializedRunnerFailure; |
| 175 | } |
| 176 | | { |
| 177 | readonly type: "protocolLog"; |
| 178 | readonly providerSessionId: OrchestrationV2ProviderSession["id"]; |
| 179 | readonly sessionId?: number; |
| 180 | readonly event: CursorAgentSdkProtocolLogEvent; |
| 181 | } |
| 182 | | { |
| 183 | readonly type: "backgroundShell"; |
| 184 | readonly sessionId: number; |
| 185 | readonly event: SerializedBackgroundShellEvent; |
| 186 | }; |
| 187 | |
| 188 | const MAX_ERROR_CAUSE_DEPTH = 8; |
| 189 | |
| 190 | function safeGetProperty(target: object, key: string): unknown { |
| 191 | try { |
| 192 | return Reflect.get(target, key); |
| 193 | } catch { |
| 194 | return undefined; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | function safeErrorString(value: unknown): string { |
| 199 | if (typeof value === "string") return value; |
| 200 | try { |
| 201 | return String(value); |
| 202 | } catch { |
| 203 | return "[Unserializable error]"; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | export function serializeWorkerError( |
| 208 | cause: unknown, |
| 209 | depth = 0, |
| 210 | seen = new Set<object>(), |
| 211 | ): SerializedWorkerError { |
| 212 | if (typeof cause === "object" && cause !== null) { |
| 213 | if (seen.has(cause)) { |
| 214 | return { name: "Error", message: "[Circular error cause]" }; |
| 215 | } |
| 216 | seen.add(cause); |
| 217 | const rawName = safeGetProperty(cause, "name"); |
| 218 | const rawMessage = safeGetProperty(cause, "message"); |
| 219 | const rawStack = safeGetProperty(cause, "stack"); |
| 220 | const rawCode = safeGetProperty(cause, "code"); |
| 221 | const rawStatus = safeGetProperty(cause, "status"); |
| 222 | const rawCause = depth < MAX_ERROR_CAUSE_DEPTH ? safeGetProperty(cause, "cause") : undefined; |
| 223 | let sdkErrorKind: CursorSdkErrorKind | undefined; |
| 224 | try { |
| 225 | sdkErrorKind = getCursorSdkErrorKind(cause); |
| 226 | } catch { |
| 227 | sdkErrorKind = undefined; |
| 228 | } |
| 229 | return { |
| 230 | name: typeof rawName === "string" && rawName.length > 0 ? rawName : "Error", |
| 231 | message: typeof rawMessage === "string" ? rawMessage : safeErrorString(cause), |
| 232 | ...(typeof rawStack === "string" ? { stack: rawStack } : {}), |
| 233 | ...(typeof rawCode === "string" ? { code: rawCode } : {}), |
| 234 | ...(typeof rawStatus === "number" ? { status: rawStatus } : {}), |
| 235 | ...(sdkErrorKind === undefined ? {} : { sdkErrorKind }), |
| 236 | ...(rawCause === undefined ? {} : { cause: serializeWorkerError(rawCause, depth + 1, seen) }), |
| 237 | }; |
| 238 | } |
| 239 | return { |
| 240 | name: "Error", |
| 241 | message: safeErrorString(cause), |
| 242 | }; |
| 243 | } |
| 244 | |
| 245 | export function deserializeWorkerError(serialized: SerializedWorkerError): Error { |
| 246 | const error = new Error( |
| 247 | serialized.message, |
| 248 | serialized.cause === undefined |
| 249 | ? undefined |
| 250 | : { cause: deserializeWorkerError(serialized.cause) }, |
| 251 | ); |
| 252 | error.name = serialized.name; |
| 253 | if (serialized.stack !== undefined) { |
| 254 | error.stack = serialized.stack; |
| 255 | } |
| 256 | if (serialized.code !== undefined) { |
| 257 | Reflect.set(error, "code", serialized.code); |
| 258 | } |
| 259 | if (serialized.status !== undefined) { |
| 260 | Reflect.set(error, "status", serialized.status); |
| 261 | } |
| 262 | if (serialized.sdkErrorKind !== undefined) { |
| 263 | tagCursorSdkError(error, serialized.sdkErrorKind); |
| 264 | } |
| 265 | return error; |
| 266 | } |
| 267 | |
| 268 | export function serializeRunResult(result: RunResult): SerializedRunResult { |
| 269 | if (result.error === undefined) { |
| 270 | return result as SerializedRunResult; |
| 271 | } |
| 272 | return { |
| 273 | ...result, |
| 274 | error: serializeWorkerError(result.error), |
| 275 | }; |
| 276 | } |
| 277 | |
| 278 | export function deserializeRunResult(result: SerializedRunResult): RunResult { |
| 279 | if (result.error === undefined) { |
| 280 | return result as RunResult; |
| 281 | } |
| 282 | return { |
| 283 | ...result, |
| 284 | error: deserializeWorkerError(result.error), |
| 285 | } as RunResult; |
| 286 | } |
| 287 | |
| 288 | function jsonReplacer(_key: string, value: unknown): unknown { |
| 289 | if (typeof value === "bigint") { |
| 290 | return value.toString(); |
| 291 | } |
| 292 | if (value instanceof Uint8Array) { |
| 293 | return Buffer.from(value.buffer, value.byteOffset, value.byteLength).toString("base64"); |
| 294 | } |
| 295 | return value; |
| 296 | } |
| 297 | |
| 298 | export function encodeWorkerFrame(frame: CursorWorkerRequest | CursorWorkerMessage): string { |
| 299 | return `${JSON.stringify(frame, jsonReplacer)}\n`; |
| 300 | } |
| 301 | |
| 302 | export function decodeWorkerFrame(line: string): unknown { |
| 303 | return JSON.parse(line); |
| 304 | } |