@@ -211,6 +211,12 @@ const BLANK_LINE_PATTERN = /^[ \t]*$/;
211211// nested items count. The trailing space is required, so a partial `-` or
212212// `1.` never matches before the model finishes the marker.
213213const LIST_ITEM_START_PATTERN = /^[ \t]*(?:[-*+]|\d{1,9}[.)])[ \t]/;
214+ // A section title: an ATX heading, or a line of only bold text, which models
215+ // often use as a heading.
216+ const SECTION_TITLE_PATTERN = /^ {0,3}(?:#{1,6}(?:[ \t]|$)|\*\*(?:[^*]|\*(?!\*))+\*\*:?$)/;
217+ // An unindented ATX heading ends the paragraph or list above it, even with no
218+ // blank line between them. A bold line would continue the paragraph instead.
219+ const TOP_LEVEL_HEADING_PATTERN = /^#{1,6}(?:[ \t]|$)/;
214220
215221/**
216222 * Splits buffered assistant text at the last blank line, closing code fence,
@@ -221,17 +227,26 @@ const LIST_ITEM_START_PATTERN = /^[ \t]*(?:[-*+]|\d{1,9}[.)])[ \t]/;
221227 * never leaks; a list item start is the one lookahead that may sit on the
222228 * partial line, since tight lists have no blank lines between items and would
223229 * otherwise land all at once.
230+ *
231+ * A section title holds the boundary until a content line follows it, so a
232+ * title never lands alone and waits above a block that is still streaming.
224233 */
225234export function splitBufferedAssistantText(text: string): { ready: string; rest: string } {
226235 let openFence: { marker: string; indent: number } | null = null;
227236 let boundary = -1;
228237 let lineStart = 0;
238+ let titleAwaitingContent = false;
229239 for (;;) {
230240 const newline = text.indexOf("\n", lineStart);
231241 const line = text
232242 .slice(lineStart, newline === -1 ? text.length : newline)
233243 .replace(/[ \t\r]+$/, "");
234- if (openFence === null && lineStart > 0 && LIST_ITEM_START_PATTERN.test(line)) {
244+ if (
245+ openFence === null &&
246+ lineStart > 0 &&
247+ !titleAwaitingContent &&
248+ LIST_ITEM_START_PATTERN.test(line)
249+ ) {
235250 boundary = lineStart;
236251 }
237252 if (newline === -1) {
@@ -243,6 +258,7 @@ export function splitBufferedAssistantText(text: string): { ready: string; rest:
243258 const marker = fenceMatch[2]!;
244259 if (openFence === null) {
245260 openFence = { marker, indent };
261+ titleAwaitingContent = false;
246262 } else if (
247263 marker[0] === openFence.marker[0] &&
248264 marker.length >= openFence.marker.length &&
@@ -254,7 +270,14 @@ export function splitBufferedAssistantText(text: string): { ready: string; rest:
254270 boundary = newline + 1;
255271 }
256272 } else if (openFence === null && BLANK_LINE_PATTERN.test(line) && lineStart > 0) {
257- boundary = newline + 1;
273+ if (!titleAwaitingContent) {
274+ boundary = newline + 1;
275+ }
276+ } else if (openFence === null) {
277+ if (lineStart > 0 && !titleAwaitingContent && TOP_LEVEL_HEADING_PATTERN.test(line)) {
278+ boundary = lineStart;
279+ }
280+ titleAwaitingContent = SECTION_TITLE_PATTERN.test(line);
258281 }
259282 lineStart = newline + 1;
260283 }
0 commit comments