diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/rich-markdown-editor.tsx b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/rich-markdown-editor.tsx index 61d23c429b..afe14867be 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/rich-markdown-editor.tsx +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/rich-markdown-editor.tsx @@ -158,6 +158,14 @@ export function LoadedRichMarkdownEditor({ const [initialContent] = useState(() => streamingAtMountRef.current ? '' : parseMarkdownToDoc(splitFrontmatter(content).body) ) + /** + * The body currently shown in the editor: seeded from a settled mount, updated on local edits (via + * onUpdate) and on each streamed sync. The streaming tick reveals a chunk only when it extends this, + * so an agent rewrite holds the current content instead of collapsing to a partial result. + */ + const lastSyncedBodyRef = useRef( + streamingAtMountRef.current ? null : splitFrontmatter(content).body + ) const onChangeRef = useRef(onChange) onChangeRef.current = onChange const onSaveShortcutRef = useRef(onSaveShortcut) @@ -263,13 +271,12 @@ export function LoadedRichMarkdownEditor({ }, onUpdate: ({ editor }) => { const md = postProcessSerializedMarkdown(editor.getMarkdown()) + lastSyncedBodyRef.current = md onChangeRef.current(applyFrontmatter(settledRef.current?.frontmatter ?? '', md)) }, }) editorInstanceRef.current = editor - const lastSyncedBodyRef = useRef(null) - const wasStreamingRef = useRef(streamingAtMountRef.current) const pendingStreamBodyRef = useRef(null) @@ -291,6 +298,12 @@ export function LoadedRichMarkdownEditor({ streamRafRef.current = null return } + const shownBody = lastSyncedBodyRef.current + const extendsShown = shownBody === null || pending.startsWith(shownBody) + if (!extendsShown) { + streamRafRef.current = null + return + } if ( pending.length > STREAM_REPARSE_THROTTLE_THRESHOLD && performance.now() - lastStreamParseAtRef.current < STREAM_REPARSE_THROTTLE_MS @@ -303,7 +316,7 @@ export function LoadedRichMarkdownEditor({ lastStreamParseAtRef.current = performance.now() const el = containerRef.current const pinnedToBottom = el ? el.scrollHeight - el.scrollTop - el.clientHeight < 80 : false - editor.setEditable(false) + if (editor.isEditable) editor.setEditable(false) editor.commands.setContent(parseMarkdownToDoc(pending), { contentType: 'json', emitUpdate: false,