Format the Assistant's reply as it streams, not after it ends
Each streamed word redraws the text so far through rich(), the renderer the
settled reply uses, in one swap. What the end could still change is held
back until it closes, as Equipo's drafts do: a last line that is only a list
marker, a link on its way (its words show, not "[" or "]("), and an emphasis
opener with no closer yet (plain words until it closes). The thinking bubble
and glance are unchanged.
Browser test streams bold, a link and a list item split across deltas and
checks no "**", "[" or "](" shows, and the settled reply equals the last
draft and the same reply sent whole. equipotest gains StreamParts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+118
−3
internal/browsertest/thinking_test.go+50 −0internal/equipo/equipotest/chat.go+5 −2internal/look/static/ask.js+63 −1
diff --git a/internal/browsertest/thinking_test.go b/internal/browsertest/thinking_test.go| index 47348f0..641260c 100644 |
| --- a/internal/browsertest/thinking_test.go |
| +++ b/internal/browsertest/thinking_test.go |
| @@ -6,6 +6,7 @@ import ( |
| "fmt" |
| "regexp" |
| "strconv" |
| + "strings" |
| "testing" |
| "time" |
| @@ -677,3 +678,52 @@ func TestAskThinkingHoldsSteady(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// A streamed reply shows formatted as it arrives: bold, a link and a list |
| +// item split across deltas never show a "**", "[" or "](" mid-stream (an |
| +// opener with no closer yet is plain words, a link on its way shows its |
| +// words), and the reply that settles is drawn as the same reply that |
| +// came whole. |
| +func TestAskStreamFormatsAsItArrives(t *testing.T) { |
| + rig, f := askBase(t) |
| + openPanel(t, rig) |
| + th := thinking{t, rig, f} |
| + const text = "Here is **bold text** and a [guide page](https://example.com/guide) too.\n- first *item* here\n- second [more](https://example.com/more)\n" |
| + parts := []string{"Here is *", "*bo", "ld te", "xt**", " and a [gu", "ide page", "](http", "s://example.com/gu", "ide) too.\n-", " first *it", "em* here\n", "- sec", "ond [more]", "(https://example.com/more)", "\n"} |
| + if strings.Join(parts, "") != text { |
| + t.Fatal("the test's pieces are not its text") |
| + } |
| + id := th.send("Say it formatted") |
| + th.eval(`(() => { window.__seen = []; window.__bad = []; |
| + const look = () => { for (const d of document.querySelectorAll("#ask-panel .msg.draft")) { |
| + const t = d.textContent; window.__seen.push(d.innerHTML); |
| + if (/\*\*|\[|\]\(/.test(t)) window.__bad.push(t); } }; |
| + new MutationObserver(look).observe(document.querySelector("#ask-panel"), { childList: true, subtree: true, characterData: true }); |
| + return "ok"; })()`) |
| + f.StreamParts(id, parts) |
| + th.wait(`document.querySelector("#ask-panel .msg.draft strong") && document.querySelector("#ask-panel .msg.draft li:last-child a[href$=\"/more\"]")`) |
| + if got := th.eval(`JSON.stringify(window.__bad)`); got != "[]" { |
| + t.Errorf("markup showed while streaming: %s", got) |
| + } |
| + if got := th.eval(`window.__seen.length + ""`); got == "0" { |
| + t.Error("nothing was drawn as it streamed") |
| + } |
| + last := th.eval(`(document.querySelector("#ask-panel .msg.draft > .think-line") || document.querySelector("#ask-panel .msg.draft")).innerHTML`) |
| + f.Answer(id, text, false) |
| + th.wait(`document.querySelector("#ask-panel .msg.coordinator[data-id] strong")`) |
| + streamed := th.eval(`(() => { const m = [...document.querySelectorAll("#ask-panel .msg.coordinator")].find(m => m.dataset.id && m.querySelector("strong")); return (m.querySelector(":scope > .think-line") || m).innerHTML })()`) |
| + if streamed != last { |
| + t.Errorf("the settled reply differs from the last draft:\n%s\n%s", streamed, last) |
| + } |
| + // The same reply that was never streamed. |
| + id2 := th.send("Say it again") |
| + f.Answer(id2, text, false) |
| + th.wait(`document.querySelectorAll("#ask-panel .msg.coordinator[data-id] strong").length === 2`) |
| + whole := th.eval(`(() => { const m = [...document.querySelectorAll("#ask-panel .msg.coordinator")].filter(m => m.dataset.id && m.querySelector("strong"))[1]; return (m.querySelector(":scope > .think-line") || m).innerHTML })()`) |
| + if streamed != whole { |
| + t.Errorf("streamed and whole replies differ:\n%s\n%s", streamed, whole) |
| + } |
| + if !strings.Contains(whole, "<strong>bold text</strong>") || !strings.Contains(whole, `href="https://example.com/guide"`) || strings.Count(whole, "<li>") != 2 { |
| + t.Errorf("the reply is not formatted: %s", whole) |
| + } |
| +} |
diff --git a/internal/equipo/equipotest/chat.go b/internal/equipo/equipotest/chat.go| index 6158a9d..f58b1c7 100644 |
| --- a/internal/equipo/equipotest/chat.go |
| +++ b/internal/equipo/equipotest/chat.go |
| @@ -525,9 +525,12 @@ func (f *Fake) Answer(id, reply string, deltas bool) { |
| // Stream streams reply into the running turn with id, word by word, as |
| // Equipo's deltas: each at its offset in UTF-8 bytes. It sends no reply. |
| -func (f *Fake) Stream(id, reply string) { |
| +func (f *Fake) Stream(id, reply string) { f.StreamParts(id, strings.SplitAfter(reply, " ")) } |
| + |
| +// StreamParts is Stream in the pieces given, wherever they split the text. |
| +func (f *Fake) StreamParts(id string, parts []string) { |
| offset := 0 |
| - for _, part := range strings.SplitAfter(reply, " ") { |
| + for _, part := range parts { |
| select { |
| case <-f.closing: |
| return |
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js| index 169b72b..862b0cd 100644 |
| --- a/internal/look/static/ask.js |
| +++ b/internal/look/static/ask.js |
| @@ -166,11 +166,73 @@ function start(root) { |
| function write(li, box, d) { |
| const at = drafted.get(li) || 0; |
| if (d.offset === at && typeof d.text === "string") { |
| - box.textContent += d.text; |
| + const text = (drafting.get(li) || "") + d.text; |
| + drafting.set(li, text); |
| drafted.set(li, at + utf8.encode(d.text).length); |
| + draw(box, text); |
| } |
| scroll(); |
| } |
| + // The draft is drawn as the reply will be: all the text so far through |
| + // rich, in one go (no frame between clearing and filling), so a list is |
| + // a list and bold is bold as it arrives. What the next words could still |
| + // change stays out until they come (steady), as Equipo's drafts do. |
| + const drafting = new WeakMap(); |
| + function draw(box, text) { |
| + const next = document.createElement("div"); |
| + rich(next, steady(text)); |
| + box.replaceChildren(...next.childNodes); |
| + } |
| + // A link on its way at the end of a line: its "[", words, and "]" or |
| + // "](" with as much of the address as has come. |
| + const openLink = /\[([^\[\]\n]*)(?:\](?:\([^()\s]*)?)?$/; |
| + // A last line that is only a list marker so far. |
| + const markerOnly = /^(?:[-*]|\d{1,3}[.)]?)?[ \t]*$/; |
| + // steady is text without what its end leaves open: a last line that is |
| + // only the start of a list item; a link on its way, down to its words; |
| + // and an emphasis opener (**, * or _) with no closer yet, which shows as |
| + // plain words until it closes. Only the last line can be open: no |
| + // emphasis or link crosses a line. |
| + function steady(text) { |
| + text = text.replace(/\r\n?/g, "\n").replaceAll("\0", ""); |
| + const at = text.lastIndexOf("\n") + 1; |
| + let line = text.slice(at); |
| + if (markerOnly.test(line)) return text.slice(0, at); |
| + line = line.replace(openLink, "$1"); |
| + // Between links, as emphasis reads it. |
| + let out = "", last = 0; |
| + for (const m of line.matchAll(linkRE)) { |
| + out += unopened(line.slice(last, m.index)) + m[0]; |
| + last = m.index + m[0].length; |
| + } |
| + return text.slice(0, at) + out + unopened(line.slice(last)); |
| + } |
| + // unopened is text without the emphasis openers emphasis would leave |
| + // as they are for want of a closer: a marker followed by a non-space |
| + // (or by the end), the way emphasis opens one. |
| + function unopened(text) { |
| + let out = "", last = 0; |
| + for (let i = 0; i < text.length; ) { |
| + const c = text[i]; |
| + const run = c === "*" && text[i + 1] === "*" ? 2 : 1; |
| + if (run === 2) { |
| + const j = text.indexOf("**", i + 2); |
| + if (j >= 0 && emphasized(text.slice(i + 2, j))) { i = j + 2; continue; } |
| + } |
| + if (c === "*" || (c === "_" && (i === 0 || !wordUnit(text.charCodeAt(i - 1))))) { |
| + if (run === 1) { |
| + const j = text.indexOf(c, i + 1); |
| + if (j >= 0 && emphasized(text.slice(i + 1, j)) && (c === "*" || j + 1 === text.length || !wordUnit(text.charCodeAt(j + 1)))) { i = j + 1; continue; } |
| + } |
| + if (i + run === text.length || !space(text[i + run])) { |
| + out += text.slice(last, i); |
| + last = i + run; |
| + } |
| + } |
| + i += run; |
| + } |
| + return out + text.slice(last); |
| + } |
| // An Assistant's line, in the little Markdown it may write (plan |
| // decision D16): runs of "- " or "* " lines are a bulleted list, runs of |