(
line: string,
)
| 345 | } |
| 346 | |
| 347 | private async processLine( |
| 348 | line: string, |
| 349 | ): Promise<StdinMessage | SDKMessage | undefined> { |
| 350 | // Skip empty lines (e.g. from double newlines in piped stdin) |
| 351 | if (!line) { |
| 352 | return undefined |
| 353 | } |
| 354 | try { |
| 355 | const message = normalizeControlMessageKeys(jsonParse(line)) as |
| 356 | | StdinMessage |
| 357 | | SDKMessage |
| 358 | if (message.type === 'keep_alive') { |
| 359 | // Silently ignore keep-alive messages |
| 360 | return undefined |
| 361 | } |
| 362 | if (message.type === 'update_environment_variables') { |
| 363 | // Apply environment variable updates directly to process.env. |
| 364 | // Used by bridge session runner for auth token refresh |
| 365 | // (CLAUDE_CODE_SESSION_ACCESS_TOKEN) which must be readable |
| 366 | // by the REPL process itself, not just child Bash commands. |
| 367 | const variables = message.variables ?? {} |
| 368 | const keys = Object.keys(variables) |
| 369 | for (const [key, value] of Object.entries(variables)) { |
| 370 | process.env[key] = value |
| 371 | } |
| 372 | logForDebugging( |
| 373 | `[structuredIO] applied update_environment_variables: ${keys.join(', ')}`, |
| 374 | ) |
| 375 | return undefined |
| 376 | } |
| 377 | if (message.type === 'control_response') { |
| 378 | // Close lifecycle for every control_response, including duplicates |
| 379 | // and orphans — orphans don't yield to print.ts's main loop, so this |
| 380 | // is the only path that sees them. uuid is server-injected into the |
| 381 | // payload. |
| 382 | const uuid = |
| 383 | 'uuid' in message && typeof message.uuid === 'string' |
| 384 | ? message.uuid |
| 385 | : undefined |
| 386 | if (uuid) { |
| 387 | notifyCommandLifecycle(uuid, 'completed') |
| 388 | } |
| 389 | const resp = message.response as { |
| 390 | request_id: string |
| 391 | subtype: string |
| 392 | response?: Record<string, unknown> |
| 393 | error?: string |
| 394 | } |
| 395 | const request = this.pendingRequests.get(resp.request_id) |
| 396 | if (!request) { |
| 397 | // Check if this tool_use was already resolved through the normal |
| 398 | // permission flow. Duplicate control_response deliveries (e.g. from |
| 399 | // WebSocket reconnects) arrive after the original was handled, and |
| 400 | // re-processing them would push duplicate assistant messages into |
| 401 | // the conversation, causing API 400 errors. |
| 402 | const responsePayload = |
| 403 | resp.subtype === 'success' ? resp.response : undefined |
| 404 | const toolUseID = responsePayload?.toolUseID |
no test coverage detected