docs/solutions/performance-issues/2026-03-26-ai-streaming-preview-should-use-localized-rollback.md
Insert-mode AI preview was restoring and recommitting the whole editor value.
That kept chunk history out of undo, but it still deep-cloned the full document and used full-document setValue on accept and cancel. Large documents therefore paid a document-sized cost for a paragraph-sized preview.
Treat insert-mode preview as a localized top-level block range instead of a full-document snapshot.
tf.ai.beginPreview stores:
originalBlocks: the exact top-level blocks the preview overwritesselectionBefore: the original selectionWhen streaming starts from an empty paragraph, originalBlocks is that paragraph. When streaming inserts after existing content, originalBlocks is [].
streamInsertChunk tags preview-owned top-level blocks with aiPreview: true while keeping AI text leaves marked with the normal AI text prop.
That makes the live preview range discoverable without relying on drifting paths.
tf.ai.cancelPreview():
aiPreview rangeoriginalBlocksselectionBeforeAll of that runs in withoutSaving, so preview cancel still stays out of history.
tf.ai.acceptPreview():
aiPreview and AI text marks from that local clonewithoutSavingselectionBeforeThat produces a single undoable commit whose cost scales with the preview range, not the whole document.
The preview and the committed edit need different behavior:
The trick is not “snapshot everything once.” The trick is “remember only what this preview replaced, then commit only that region.”
That keeps:
setValue for insert-mode preview accept or cancel.