<!-- source-hash: 05b4580f6cd1fa50abcf196198e4487a -->
Extracts incomplete message state from the last historical assistant message to enable resumption of in-progress AI responses across realtime connection restarts.

## Key Components

### `extractIncompleteMessageState(lastMessage)`

**Parameters:** `lastMessage: ProcessedMessage | undefined`

**Returns:** `{ existingSegments?, pendingApprovals?, executingTools? } | undefined`

Returns `undefined` if the message is complete or not an assistant message with structured segments. Otherwise returns state needed to reinitialize the realtime chunk processor.

**Incomplete state detection by segment type:**

| Segment Type | Condition Flagged Incomplete |
|---|---|
| `tool_execution` | Status is `EXECUTING_TOOL` |
| `approval_request` | Status is `pending` with a `requestId` |
| `approval_batch` | Not `rejected` AND not all tool calls have `done` executions |
| `context_compaction` | Status is `started` |

## Usage Example

```typescript
import { extractIncompleteMessageState } from './extract-incomplete-message-state'

const lastMessage = conversationHistory.at(-1)
const incompleteState = extractIncompleteMessageState(lastMessage)

if (incompleteState) {
  realtimeProcessor.initialize({
    existingSegments: incompleteState.existingSegments,
    pendingApprovals: incompleteState.pendingApprovals,
    executingTools: incompleteState.executingTools,
  })
}
```

## Notes

- Only processes assistant messages with structured (`MessageSegment[]`) content — string content is ignored
- `pendingApprovals` and `executingTools` maps are omitted from the return value when empty, reducing unnecessary initialization overhead
- The `approval_batch` logic ensures post-approval `EXECUTED_TOOL` chunks can correctly merge into the batch segment via `applyExecutionToBatch`