MCPcopy Create free account
hub / github.com/WJX20/claude-code / processLine

Method processLine

src/cli/structuredIO.ts:338–469  ·  view source on GitHub ↗
(
    line: string,
  )

Source from the content-addressed store, hash-verified

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

Callers 1

readMethod · 0.95

Calls 14

jsonParseFunction · 0.85
logForDebuggingFunction · 0.85
notifyCommandLifecycleFunction · 0.85
keysMethod · 0.80
entriesMethod · 0.80
deleteMethod · 0.80
rejectMethod · 0.80
resolveMethod · 0.80
exitWithMessageFunction · 0.70
errorMethod · 0.65

Tested by

no test coverage detected