-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(ai): buffer streaming preamble text before code fence appears #8911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -241,6 +241,15 @@ class CellCreationStream { | |||||||||
| stream(chunk: TextDeltaChunk) { | ||||||||||
| const delta = chunk.delta; | ||||||||||
| this.buffer += delta; | ||||||||||
|
|
||||||||||
| // If no code fence has appeared yet and no cells have been created, | ||||||||||
| // buffer the content and wait. This prevents conversational preamble | ||||||||||
| // (e.g. "I'll create a cell that...") from becoming a Python cell. | ||||||||||
| // Once a fence appears, codeToCells will correctly extract only the code. | ||||||||||
| if (!this.buffer.includes("```") && this.createdCells.length === 0) { | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const completionCells = codeToCells(this.buffer); | ||||||||||
|
|
||||||||||
| // As incoming chunks are appended to the buffer, | ||||||||||
|
|
@@ -282,8 +291,19 @@ class CellCreationStream { | |||||||||
| } | ||||||||||
|
|
||||||||||
| stop() { | ||||||||||
| // If the stream ended with buffered content but no cells were created | ||||||||||
| // (model returned code without fences), create a cell from the buffer. | ||||||||||
| if (this.buffer.trim() && this.createdCells.length === 0) { | ||||||||||
| const completionCells = codeToCells(this.buffer); | ||||||||||
| for (const cell of completionCells) { | ||||||||||
| const newCellId = this.onCreateCell(cell.code); | ||||||||||
| this.createdCells.push({ cellId: newCellId, cell }); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| // Clear all state | ||||||||||
| this.buffer = ""; | ||||||||||
|
||||||||||
| this.buffer = ""; | |
| this.buffer = ""; | |
| this.createdCells = []; | |
| this.hasMarimoImport = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — applied in 2d784fc. CellCreationStream is single-use (new instance per text-start), so this is purely defensive, but it makes the comment accurate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test covered the case where the first chunk is a partial fence, which got replaced. Would we add that scenario back?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added! The new test (
should buffer partial fence and create cell when fence completes) covers the split-fence scenario: first chunk has just ``````, second chunk completes the fence — no premature cell creation.