* Extract plain text from a replayed SDKUserMessage NDJSON line. Returns the * trimmed text if this looks like a real human-authored message, otherwise * undefined so the caller keeps waiting for the first real message.
( msg: Record<string, unknown>, )
| 267 | * undefined so the caller keeps waiting for the first real message. |
| 268 | */ |
| 269 | function extractUserMessageText( |
| 270 | msg: Record<string, unknown>, |
| 271 | ): string | undefined { |
| 272 | // Skip tool-result user messages (wrapped subagent results) and synthetic |
| 273 | // caveat messages — neither is human-authored. |
| 274 | if (msg.parent_tool_use_id != null || msg.isSynthetic || msg.isReplay) |
| 275 | return undefined |
| 276 | |
| 277 | const message = msg.message as Record<string, unknown> | undefined |
| 278 | const content = message?.content |
| 279 | let text: string | undefined |
| 280 | if (typeof content === 'string') { |
| 281 | text = content |
| 282 | } else if (Array.isArray(content)) { |
| 283 | for (const block of content) { |
| 284 | if ( |
| 285 | block && |
| 286 | typeof block === 'object' && |
| 287 | (block as Record<string, unknown>).type === 'text' |
| 288 | ) { |
| 289 | text = (block as Record<string, unknown>).text as string | undefined |
| 290 | break |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | text = text?.trim() |
| 295 | return text ? text : undefined |
| 296 | } |
| 297 | |
| 298 | /** Build a short preview of tool input for debug logging. */ |
| 299 | function inputPreview(input: Record<string, unknown>): string { |