Coerces a chat message's `content` field — either a plain string or a `MessageSegment[]` array — into a flat string suitable for wire transport to the LLM. ## Key Components - **`flattenAssistantContent(raw: unknown): string`** — Single exported function that normalizes assistant message content before serialization. Handles three cases: - Plain strings are returned as-is - Arrays are filtered to `text`-typed segments, joining their `.text` fields with `\n\n` - Null, undefined, or unknown shapes return `''` ## Why This Exists When `useChat` streams SSE responses, assistant messages are stored in React state as structured `MessageSegment[]` arrays (text, approval cards, tool executions, thinking blocks). A naive empty-string fallback for non-strings silently drops all context on subsequent turns — Anthropic receives `assistant: ""` and loses conversation history. This function preserves the LLM-visible text while intentionally skipping non-text segments (`approval_request`, `tool_execution`, `thinking`) to avoid bloating the request body. ## Usage Example ```typescript import { flattenAssistantContent } from './flatten-assistant-content' // Plain string — returned as-is flattenAssistantContent("Hello world") // → "Hello world" // Structured segment array — text segments joined flattenAssistantContent([ { type: 'thinking', content: '...' }, // skipped { type: 'text', text: 'Here is my answer.' }, // included { type: 'tool_execution', id: 'tu_1' }, // skipped { type: 'text', text: 'And a follow-up.' }, // included ]) // → "Here is my answer.\n\nAnd a follow-up." // Null / unknown — safe fallback flattenAssistantContent(null) // → "" ```