Show the Assistant thinking in a bubble with a spark
While a person's turn is answering, a thinking bubble sits at the end of the conversation on the Assistant's side, carrying "Thinking…" and then Equipo's activity lines; the status line above the field stays empty (it keeps "waiting" and the failure notices). Streamed words fill the bubble and the reply becomes it in place, crossfading and easing to its new size. Stop and a failure take it away; team_update turns stay quiet. Scripts off, the server's status line is unchanged. The approved mockup's WebGL spark runs round the bubble's edge: a canvas made only while it thinks (shaders compile in the first frame), DPR aware, pointer-events none, the loop ending once faded or out of sight. Without WebGL, or after a lost context, the bubble gets no-gl and the CSS sweep; with reduced motion, a still glow and no frames. The composer's focus is the approved edge: 2px teal to violet outside the field, drifting every 6s over a glow that breathes every 4.5s, in place of the outline (still under reduced motion; the system outline in forced colours). New colours are tokens in all three theme blocks. A turn that has ended stays ended: the send's own answer can arrive after the turn's events and would otherwise bring a bubble back. Tests that waited for "a coordinator line without .draft" now wait for a delivered reply; the fake Equipo can hold, stream, answer and fail turns.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
10 files changed,
+878
−40
internal/browsertest/ask_test.go+9 −5internal/browsertest/layout_test.go+1 −1internal/browsertest/light_test.go+4 −2internal/browsertest/mobile_test.go+1 −1internal/browsertest/scroll_test.go+5 −5internal/browsertest/thinking_test.go+327 −0internal/equipo/equipotest/chat.go+30 −14internal/equipo/equipotest/fake.go+1 −1internal/look/static/app.css+68 −1internal/look/static/ask.js+432 −10
diff --git a/internal/browsertest/ask_test.go b/internal/browsertest/ask_test.go| index b4f4aa0..f608b39 100644 |
| --- a/internal/browsertest/ask_test.go |
| +++ b/internal/browsertest/ask_test.go |
| @@ -295,7 +295,7 @@ func TestAskDockedOpenByDefault(t *testing.T) { |
| } |
| drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-text").value = "What can I do"`), |
| chromedp.Click(chromedp.CSS(`#ask-panel form.composer button`)), |
| - chromedp.Poll[chromedp.Void](`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.textContent.includes("What would you like to do?") && !m.classList.contains("draft"))`, |
| + chromedp.Poll[chromedp.Void](`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.textContent.includes("What would you like to do?") && m.dataset.id)`, |
| chromedp.WithPollingTimeout(10*time.Second))) |
| rig.Screen("#ask-panel", "the docked chat with one question answered") |
| if got := harness.Evaluate[int](rig, `document.querySelectorAll("#ask-panel .msg.person").length`); got != 1 { |
| @@ -500,10 +500,13 @@ func TestAskComposerSendAndStop(t *testing.T) { |
| } |
| // Sent by a click; while Equipo answers, Stop is where Send was, the |
| - // status line above the field, and no other Stop shows. |
| + // thinking bubble says so (the status line above the field is empty), |
| + // and no other Stop shows. |
| f.Hold = true |
| drive(t, rig, chromedp.Click(chromedp.CSS(send))) |
| - wait(`document.querySelectorAll("#ask-panel .msg.person").length === 1 && ` + shown(stop)) |
| + // (The turn's events can come before the send's answer, which clears |
| + // the field.) |
| + wait(`document.querySelectorAll("#ask-panel .msg.person").length === 1 && document.querySelector("#ask-text").value === "" && ` + shown(stop)) |
| if got := harness.Evaluate[string](rig, `(() => { |
| const b = document.querySelector(`+strconv.Quote(stop)+`), p = []; |
| if (b.getAttribute("aria-label") !== "Stop" || b.title !== "Stop") p.push("named " + b.getAttribute("aria-label") + "/" + b.title); |
| @@ -513,8 +516,9 @@ func TestAskComposerSendAndStop(t *testing.T) { |
| const others = [...document.querySelectorAll("#ask-panel button")].filter(x => x !== b && /Stop/.test(x.textContent) && x.checkVisibility()); |
| if (others.length) p.push(others.length + " other Stop buttons"); |
| const st = document.querySelector("#ask-panel .ask-status"); |
| - if (st.textContent !== "Thinking…") p.push("status " + st.textContent); |
| - if (st.getBoundingClientRect().bottom > document.querySelector("#ask-text").getBoundingClientRect().top + 0.5) p.push("status not above the field"); |
| + if (st.textContent !== "") p.push("status " + st.textContent); |
| + const bubble = document.querySelector("#ask-panel .thread > li:last-child.thinking"); |
| + if (!bubble || bubble.textContent !== "Thinking…") p.push("bubble " + (bubble && bubble.textContent)); |
| return p.join("; "); |
| })()`); got != "" { |
| t.Errorf("while answering: %s", got) |
diff --git a/internal/browsertest/layout_test.go b/internal/browsertest/layout_test.go| index e9fbc2c..ac6f903 100644 |
| --- a/internal/browsertest/layout_test.go |
| +++ b/internal/browsertest/layout_test.go |
| @@ -243,7 +243,7 @@ func TestAskDockLayout(t *testing.T) { |
| if harness.Evaluate[int](rig, `document.querySelectorAll("#ask-panel .msg.coordinator").length`) == 0 { |
| drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), "What can I do"), |
| chromedp.Click(chromedp.CSS(`#ask-panel form.composer button`)), |
| - chromedp.Poll[chromedp.Void](`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => !m.classList.contains("draft"))`, |
| + chromedp.Poll[chromedp.Void](`!document.querySelector("#ask-panel .thinking, #ask-panel .draft, #ask-panel .think-line") && [...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id)`, |
| chromedp.WithPollingTimeout(10*time.Second))) |
| } |
| if got := harness.Evaluate[string](rig, dockedJS); got != "" { |
diff --git a/internal/browsertest/light_test.go b/internal/browsertest/light_test.go| index 8521731..73aef3b 100644 |
| --- a/internal/browsertest/light_test.go |
| +++ b/internal/browsertest/light_test.go |
| @@ -283,7 +283,7 @@ func TestLightMode(t *testing.T) { |
| drive(t, rig, chromedp.Click(chromedp.CSS(`.base-btn`)), chromedp.Poll[chromedp.Void](settledJS, chromedp.WithPollingTimeout(3*time.Second)), |
| chromedp.SendKeys(chromedp.CSS(`#ask-text`), "What can I do"), |
| chromedp.Click(chromedp.CSS(`#ask-panel form.composer button`)), |
| - chromedp.Poll[chromedp.Void](`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => !m.classList.contains("draft"))`, |
| + chromedp.Poll[chromedp.Void](`!document.querySelector("#ask-panel .thinking, #ask-panel .draft, #ask-panel .think-line") && [...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id)`, |
| chromedp.WithPollingTimeout(10*time.Second)), |
| chromedp.Poll[chromedp.Void](`getComputedStyle(document.querySelector("#ask-panel")).transform === "none"`, chromedp.WithPollingTimeout(3*time.Second))) |
| checkContrast(t, rig, "the panel, light", "#ask-panel .msg.person", "#ask-panel .msg.coordinator", "#ask-panel .ask-head h2") |
| @@ -382,8 +382,10 @@ func TestThemesWithoutLightDark(t *testing.T) { |
| for _, page := range []struct{ path, wait string }{{"/", ".hello"}, {"/settings", "form.segmented"}} { |
| snap := func(theme string, old bool) []string { |
| t.Helper() |
| + // Focus out of the composer first: its glow breathes, and two |
| + // loads would catch it at different breaths. |
| drive(t, rig, chromedp.Navigate(rig.Origin+page.path), chromedp.WaitVisible(chromedp.CSS(page.wait)), |
| - chromedp.Evaluate[chromedp.Void](pauseJS)) |
| + chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`), chromedp.Evaluate[chromedp.Void](pauseJS)) |
| if theme != "" { |
| drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.documentElement.dataset.theme = `+strconv.Quote(theme))) |
| } |
diff --git a/internal/browsertest/mobile_test.go b/internal/browsertest/mobile_test.go| index 1c6d639..1de7f08 100644 |
| --- a/internal/browsertest/mobile_test.go |
| +++ b/internal/browsertest/mobile_test.go |
| @@ -140,7 +140,7 @@ func TestMobile(t *testing.T) { |
| // top, the composer on screen at the bottom, targets 44px. |
| drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), "What can I do"), |
| chromedp.Click(chromedp.CSS(`#ask-panel form.composer button`)), |
| - chromedp.Poll[chromedp.Void](`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => !m.classList.contains("draft"))`, |
| + chromedp.Poll[chromedp.Void](`!document.querySelector("#ask-panel .thinking, #ask-panel .draft, #ask-panel .think-line") && [...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id)`, |
| chromedp.WithPollingTimeout(10*time.Second)), |
| chromedp.Poll[chromedp.Void](`(() => { const r = document.querySelector("#ask-panel").getBoundingClientRect(); return r.left === 0 && r.top === 0 })()`, |
| chromedp.WithPollingTimeout(3*time.Second))) |
diff --git a/internal/browsertest/scroll_test.go b/internal/browsertest/scroll_test.go| index 5dbad46..2935901 100644 |
| --- a/internal/browsertest/scroll_test.go |
| +++ b/internal/browsertest/scroll_test.go |
| @@ -68,8 +68,8 @@ func TestAskScrollFollowsNewest(t *testing.T) { |
| t.Errorf("on opening: %s", got) |
| } |
| - // Send while the coordinator has not started: the person's bubble |
| - // is wholly above "Thinking…" and Stop. |
| + // Send while the coordinator has not started: the Assistant's |
| + // thinking bubble, under the person's, is wholly above Stop. |
| send := func(text string) { |
| t.Helper() |
| // Enter sends, as a person does on a keyboard; scrolled to the |
| @@ -79,8 +79,8 @@ func TestAskScrollFollowsNewest(t *testing.T) { |
| } |
| f.Hold = true |
| send("Can we fix a bug in Tito?") |
| - if _, err := chromedp.Run(rig.Context(), chromedp.Poll[chromedp.Void](`!document.querySelector("#ask-panel form.composer button.stop").hidden && document.querySelector("#ask-panel .ask-status").textContent !== ""`, chromedp.WithPollingTimeout(10*time.Second))); err != nil { |
| - t.Fatalf("no answering status: %v; person bubbles %d, status %q, field %q", err, |
| + if _, err := chromedp.Run(rig.Context(), chromedp.Poll[chromedp.Void](`!document.querySelector("#ask-panel form.composer button.stop").hidden && document.querySelector("#ask-panel .thread > li:last-child.thinking")`, chromedp.WithPollingTimeout(10*time.Second))); err != nil { |
| + t.Fatalf("no thinking bubble: %v; person bubbles %d, status %q, field %q", err, |
| harness.Evaluate[int](rig, `document.querySelectorAll("#ask-panel .msg.person").length`), |
| harness.Evaluate[string](rig, `document.querySelector("#ask-panel .ask-status").textContent`), |
| harness.Evaluate[string](rig, `document.querySelector("#ask-text").value`)) |
| @@ -94,7 +94,7 @@ func TestAskScrollFollowsNewest(t *testing.T) { |
| f.Hold = false |
| // Send: the log goes to the bottom, and stays there as the reply |
| - // streams in under "Thinking…". |
| + // streams into the thinking bubble. |
| send("What can I do") |
| wait(`(() => { const d = document.querySelector("#ask-panel .msg.draft"); return d && d.textContent.length > 120 })()`) |
| if got := harness.Evaluate[string](rig, lastVisible); got != "" { |
diff --git a/internal/browsertest/thinking_test.go b/internal/browsertest/thinking_test.go| new file mode 100644 |
| index 0000000..4f40a49 |
| --- /dev/null |
| +++ b/internal/browsertest/thinking_test.go |
| @@ -0,0 +1,327 @@ |
| +//go:build browser |
| + |
| +package browsertest |
| + |
| +import ( |
| + "fmt" |
| + "regexp" |
| + "strconv" |
| + "testing" |
| + "time" |
| + |
| + "github.com/chromedp/chromedp" |
| + |
| + "amadan.net/rastrillo/rastrillo/harness" |
| + |
| + "amadan.net/carlos/base/internal/equipo/equipotest" |
| +) |
| + |
| +// thinkingSel is the Assistant's thinking bubble: on its side of the |
| +// conversation, where its answer will appear. |
| +const thinkingSel = `#ask-panel .thread > li.msg.coordinator.thinking` |
| + |
| +// countFramesJS counts the page's requestAnimationFrame calls from now on, |
| +// in window.__raf: the spark's loop is the only thing in the chat that |
| +// asks for frames while it waits. (chromedp's Poll asks for frames too: |
| +// count from after the last one.) |
| +const countFramesJS = `(() => { window.__raf = 0; if (!window.__rafWrapped) { const r = window.requestAnimationFrame.bind(window); |
| + window.requestAnimationFrame = (cb) => { window.__raf++; return r(cb); }; window.__rafWrapped = true; } })()` |
| + |
| +// noGLJS makes this page's canvases refuse a WebGL context, as a browser |
| +// without WebGL does. |
| +const noGLJS = `(() => { const get = HTMLCanvasElement.prototype.getContext; |
| + HTMLCanvasElement.prototype.getContext = function (type, ...rest) { return /webgl/.test(type) ? null : get.call(this, type, ...rest); }; })()` |
| + |
| +type thinking struct { |
| + t *testing.T |
| + rig *harness.Rig |
| + f *equipotest.Fake |
| +} |
| + |
| +func (th thinking) wait(js string) { |
| + th.t.Helper() |
| + drive(th.t, th.rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second))) |
| +} |
| + |
| +func (th thinking) eval(js string) string { |
| + th.t.Helper() |
| + return harness.Evaluate[string](th.rig, js) |
| +} |
| + |
| +// send asks text and holds the turn running; it returns the turn's id, |
| +// once the bubble shows. |
| +func (th thinking) send(text string) string { |
| + th.t.Helper() |
| + th.f.Hold = true |
| + drive(th.t, th.rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), text+"\r")) |
| + th.wait(`!!document.querySelector(` + strconv.Quote(thinkingSel) + `) && !document.querySelector("#ask-panel form.composer button.stop").hidden`) |
| + m := regexp.MustCompile(`^/ask/turns/([^/]+)/cancel$`).FindStringSubmatch(th.eval(`document.querySelector("#ask-panel form.cancel").getAttribute("action")`)) |
| + if m == nil { |
| + th.t.Fatal("no turn to cancel while the bubble shows") |
| + } |
| + return m[1] |
| +} |
| + |
| +// statusJS is the status line above the field: its text, and whether it |
| +// takes any room. |
| +const statusJS = `(() => { const s = document.querySelector("#ask-panel .ask-status"); |
| + return JSON.stringify(s.textContent) + (s.getBoundingClientRect().height > 0 ? " shown" : " empty"); })()` |
| + |
| +// While the Assistant answers, its thinking bubble sits at the end of the |
| +// conversation carrying what it is doing, and the status line above the |
| +// field stays empty; a spark runs round the bubble's edge on a WebGL |
| +// canvas made for it. The bubble becomes the reply in place, streamed or |
| +// not; Stop and a failure take it away; a team_update turn shows none; |
| +// scripts off, the status line says it as before. |
| +func TestAskThinkingBubble(t *testing.T) { |
| + rig, f := askBase(t) |
| + openPanel(t, rig) |
| + th := thinking{t, rig, f} |
| + if got := th.eval(`document.querySelectorAll("#ask-panel canvas, #ask-panel .thinking").length + ""`); got != "0" { |
| + t.Fatalf("idle, the chat holds %s canvases or bubbles", got) |
| + } |
| + id := th.send("What can I do") |
| + drive(t, rig, chromedp.Evaluate[chromedp.Void](countFramesJS), chromedp.Sleep(300*time.Millisecond)) |
| + if got := th.eval(`(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `), list = b.parentElement, p = []; |
| + if (list.lastElementChild !== b) p.push("not at the end of the conversation"); |
| + if (document.querySelectorAll("#ask-panel .thinking").length !== 1) p.push(document.querySelectorAll("#ask-panel .thinking").length + " bubbles"); |
| + if (b.textContent !== "Thinking…") p.push("says " + JSON.stringify(b.textContent)); |
| + if (b.getBoundingClientRect().left - list.getBoundingClientRect().left > 20) p.push("not on the Assistant's side"); |
| + if (b.classList.contains("no-gl")) p.push("fell back to the CSS sweep"); |
| + const c = b.querySelector("canvas"); |
| + if (!c) return p.concat("no spark").join("; "); |
| + const dpr = Math.min(devicePixelRatio || 1, 2); |
| + if (Math.abs(c.width - Math.round((b.offsetWidth + 16) * dpr)) > 1 || Math.abs(c.height - Math.round((b.offsetHeight + 16) * dpr)) > 1) |
| + p.push("canvas " + c.width + "x" + c.height + " for a bubble " + b.offsetWidth + "x" + b.offsetHeight + " at " + dpr + "x"); |
| + if (getComputedStyle(c).pointerEvents !== "none") p.push("the canvas takes the pointer"); |
| + if (c.getAttribute("aria-hidden") !== "true") p.push("the canvas is not hidden from assistive tech"); |
| + if (!window.__raf) p.push("no frames: the spark is not running"); |
| + return p.join("; "); })()`); got != "" { |
| + t.Errorf("thinking: %s", got) |
| + } |
| + if got := th.eval(statusJS); got != `"" empty` { |
| + t.Errorf("thinking, the status line above the field is %s", got) |
| + } |
| + rig.Screen("#ask-panel", "the Assistant's thinking bubble") |
| + |
| + // Equipo's activity line shows in the bubble, not above the field. |
| + f.Emit("activity", map[string]string{"turn_id": id, "label": "Looking at what I can do…"}) |
| + th.wait(`document.querySelector(` + strconv.Quote(thinkingSel) + `)?.textContent === "Looking at what I can do…"`) |
| + if got := th.eval(statusJS); got != `"" empty` { |
| + t.Errorf("with an activity line, the status line is %s", got) |
| + } |
| + if n := th.eval(`document.querySelectorAll("#ask-panel .thinking").length + ""`); n != "1" { |
| + t.Errorf("with an activity line, %s bubbles", n) |
| + } |
| + |
| + // The reply, whole: the bubble becomes it, where it is. |
| + same := func(text string) { |
| + t.Helper() |
| + th.wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id && m.textContent === ` + strconv.Quote(text) + `) && |
| + !document.querySelector("#ask-panel .think-line")`) |
| + if got := th.eval(`(() => { const b = window.__b, p = [], r = b.getBoundingClientRect(); |
| + const all = [...document.querySelectorAll("#ask-panel .msg.coordinator")].filter(m => m.textContent === ` + strconv.Quote(text) + `); |
| + if (all.length !== 1) p.push(all.length + " messages say it"); |
| + if (all[0] !== b) p.push("drawn as a new element"); |
| + if (!b.isConnected || [...b.parentElement.children].indexOf(b) !== window.__at) p.push("moved from " + window.__at); |
| + if (Math.abs(r.left - window.__r.left) > 0.5 || (Math.abs(r.top - window.__r.top) > 1 && Math.abs(r.bottom - window.__r.bottom) > 1)) |
| + p.push("jumped from " + JSON.stringify(window.__r) + " to " + JSON.stringify(r)); |
| + if (b.matches(".thinking, .draft") || b.querySelector(".think-line")) p.push("still thinking: " + b.outerHTML); |
| + if (document.querySelector("#ask-panel .thinking")) p.push("a bubble still shows"); |
| + return p.join("; "); })()`); got != "" { |
| + t.Errorf("the reply %q: %s", text, got) |
| + } |
| + // The spark fades and its loop ends. |
| + th.wait(`!document.querySelector("#ask-panel canvas")`) |
| + drive(t, rig, chromedp.Evaluate[chromedp.Void](countFramesJS), chromedp.Sleep(300*time.Millisecond)) |
| + if n := harness.Evaluate[int](rig, `window.__raf`); n != 0 { |
| + t.Errorf("answered, the page asked for %d frames", n) |
| + } |
| + if got := th.eval(statusJS); got != `"" empty` { |
| + t.Errorf("answered, the status line is %s", got) |
| + } |
| + } |
| + mark := chromedp.Evaluate[chromedp.Void](`(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `); |
| + window.__b = b; window.__at = [...b.parentElement.children].indexOf(b); window.__r = b.getBoundingClientRect(); })()`) |
| + drive(t, rig, mark) |
| + f.Answer(id, "Here's what I can do.", false) |
| + same("Here's what I can do.") |
| + |
| + // Streamed: the words fill the bubble, which then becomes the reply. |
| + id = th.send("And what else?") |
| + drive(t, rig, mark) |
| + f.Answer(id, "Plan work, check on a project, or start a task.", true) |
| + same("Plan work, check on a project, or start a task.") |
| + |
| + // Scripts off, the status line says it, as before; back on, the bubble |
| + // is drawn again. |
| + said := th.eval(`document.querySelectorAll("#ask-panel .msg.coordinator").length + ""`) |
| + id = th.send("Build me a site") |
| + drive(t, rig, scriptsDisabled(true), chromedp.Navigate(rig.Origin+"/ask"), chromedp.WaitVisible(chromedp.CSS(`.ask-page textarea`))) |
| + if got := harness.Evaluate[string](rig, `(() => { const s = document.querySelector(".ask-page .ask-status"); |
| + return s.textContent + " " + s.checkVisibility() + " " + document.querySelectorAll(".thinking").length; })()`); got != "Thinking… true 0" { |
| + t.Errorf("scripts off, while answering: %s, want the status line and no bubble", got) |
| + } |
| + drive(t, rig, scriptsDisabled(false)) |
| + openPanel(t, rig) |
| + th.wait(`document.querySelector(` + strconv.Quote(thinkingSel) + `)?.textContent === "Thinking…"`) |
| + |
| + // Stop takes the bubble away, and its spark with it. |
| + drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| + th.wait(`!document.querySelector("#ask-panel .thinking") && document.querySelector("#ask-panel form.composer button.stop").hidden`) |
| + if got := th.eval(`document.querySelectorAll("#ask-panel canvas").length + " " + document.querySelectorAll("#ask-panel .msg.coordinator").length`); got != "0 "+said { |
| + t.Errorf("stopped: canvases and Assistant lines %s, want 0 %s", got, said) |
| + } |
| + if got := th.eval(statusJS); got != `"" empty` { |
| + t.Errorf("stopped, the status line is %s", got) |
| + } |
| + if f.Turn(id) != "cancelled" { |
| + t.Errorf("Stop left the turn %s", f.Turn(id)) |
| + } |
| + |
| + // A failure takes it away too, and says so where it always has. |
| + id = th.send("Try this") |
| + f.FailTurn(id) |
| + th.wait(`!document.querySelector("#ask-panel .thinking")`) |
| + if got := th.eval(statusJS); got != `"That didn't get an answer. Try again." shown` { |
| + t.Errorf("failed, the status line is %s", got) |
| + } |
| + if n := th.eval(`document.querySelectorAll("#ask-panel canvas").length + ""`); n != "0" { |
| + t.Errorf("failed, %s canvases", n) |
| + } |
| + |
| + // A team_update turn is quiet: no bubble. |
| + f.Hold = false |
| + quiet := f.EmitTurn("team_update", "running") |
| + f.Emit("activity", map[string]string{"turn_id": quiet, "label": "Looking at the job"}) |
| + f.Say("Have a look.") |
| + th.wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.textContent === "Have a look.")`) |
| + drive(t, rig, chromedp.Sleep(200*time.Millisecond)) |
| + if got := th.eval(`document.querySelectorAll("#ask-panel .thinking, #ask-panel canvas").length + " " + document.querySelector("#ask-panel form.composer button.stop").hidden`); got != "0 true" { |
| + t.Errorf("with a team_update turn running: bubbles and canvases, Stop hidden %s", got) |
| + } |
| + |
| + // An ordinary send, answered at once: the turn's events (and its first |
| + // words) can come before the send's own answer, which carries the turn |
| + // as it was queued. Once the reply is in, nothing is left thinking. |
| + drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), "What can I do\r")) |
| + th.wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id && m.textContent.includes("What would you like to do?"))`) |
| + drive(t, rig, chromedp.Sleep(800*time.Millisecond)) |
| + if got := th.eval(`document.querySelectorAll("#ask-panel .thinking, #ask-panel .draft, #ask-panel .think-line, #ask-panel canvas").length + " " + |
| + document.querySelector("#ask-panel form.composer button.stop").hidden + " " + JSON.stringify(document.querySelector("#ask-panel .ask-status").textContent)`); got != `0 true ""` { |
| + t.Errorf("answered at once: bubbles, drafts and canvases, Stop hidden, status %s", got) |
| + } |
| +} |
| + |
| +// With reduced motion the bubble glows, still: no spark, no frames. Without |
| +// WebGL, or when its context is lost, a CSS sweep runs round the edge |
| +// instead. |
| +func TestAskThinkingStillAndFallback(t *testing.T) { |
| + rig, f := askBase(t) |
| + t.Run("reduced motion", func(t *testing.T) { |
| + th := thinking{t, rig, f} |
| + drive(t, rig, reducedMotion("dark", true)) |
| + defer drive(t, rig, reducedMotion("dark", false)) |
| + openPanel(t, rig) |
| + th.send("What can I do") |
| + drive(t, rig, chromedp.Evaluate[chromedp.Void](countFramesJS), chromedp.Sleep(400*time.Millisecond)) |
| + if got := th.eval(`(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `), s = getComputedStyle(b, "::before"), p = []; |
| + if (b.querySelector("canvas")) p.push("a spark canvas"); |
| + if (window.__raf) p.push(window.__raf + " frames"); |
| + if (s.animationName !== "none") p.push("the edge moves: " + s.animationName); |
| + if (!(Number(s.opacity) > 0)) p.push("no glow on the edge"); |
| + return p.join("; "); })()`); got != "" { |
| + t.Errorf("reduced motion, thinking: %s", got) |
| + } |
| + drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| + th.wait(`!document.querySelector("#ask-panel .thinking")`) |
| + }) |
| + t.Run("no WebGL", func(t *testing.T) { |
| + th := thinking{t, rig, f} |
| + openPanel(t, rig) |
| + drive(t, rig, chromedp.Evaluate[chromedp.Void](noGLJS)) |
| + th.send("What can I do") |
| + th.wait(`document.querySelector(` + strconv.Quote(thinkingSel) + `).matches(".no-gl")`) |
| + if got := th.eval(`(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `), s = getComputedStyle(b, "::before"); |
| + return b.classList.contains("no-gl") + " " + !!b.querySelector("canvas") + " " + s.animationName; })()`); got != "true false sweep" { |
| + t.Errorf("without WebGL: fallback, canvas, edge animation %s; want true false sweep", got) |
| + } |
| + rig.Screen("#ask-panel", "the thinking bubble without WebGL") |
| + drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| + th.wait(`!document.querySelector("#ask-panel .thinking")`) |
| + }) |
| + t.Run("context lost", func(t *testing.T) { |
| + th := thinking{t, rig, f} |
| + openPanel(t, rig) |
| + th.send("What can I do") |
| + th.wait(`!!document.querySelector(` + strconv.Quote(thinkingSel+" canvas") + `)`) |
| + drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector(`+strconv.Quote(thinkingSel+" canvas")+`).getContext("webgl").getExtension("WEBGL_lose_context").loseContext()`)) |
| + th.wait(`(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `); return b.classList.contains("no-gl") && !b.querySelector("canvas"); })()`) |
| + drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| + th.wait(`!document.querySelector("#ask-panel .thinking")`) |
| + }) |
| +} |
| + |
| +// The composer's focus is a thin teal-to-violet edge round the field that |
| +// drifts, over a soft glow that breathes, in place of the outline; no |
| +// spark. With reduced motion both hold still. |
| +func TestAskComposerFocusEdge(t *testing.T) { |
| + rig, f := askBase(t) |
| + th := thinking{t, rig, f} |
| + openPanel(t, rig) |
| + edge := `(() => { const ta = document.querySelector("#ask-text"), s = getComputedStyle(ta), e = getComputedStyle(ta.parentElement, "::after"); |
| + return [document.activeElement === ta, e.opacity, s.outlineStyle, s.boxShadow !== "none", s.animationName, e.animationName, e.animationPlayState, |
| + /conic-gradient/.test(e.backgroundImage), e.paddingTop, e.top, document.querySelectorAll("#ask-panel form.composer canvas, #ask-panel form.composer .spark").length].join(" "); })()` |
| + drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-text").focus()`)) |
| + th.wait(`getComputedStyle(document.querySelector("#ask-text").parentElement, "::after").opacity === "1"`) |
| + if got, want := th.eval(edge), "true 1 none true breathe-glow edge-turn running true 2px -3px 0"; got != want { |
| + t.Errorf("focused: %s, want %s", got, want) |
| + } |
| + rig.Screen("#ask-panel", "the composer's focus edge") |
| + drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-text").blur()`)) |
| + th.wait(`getComputedStyle(document.querySelector("#ask-text").parentElement, "::after").opacity === "0"`) |
| + if got := th.eval(`getComputedStyle(document.querySelector("#ask-text")).boxShadow`); got != "none" { |
| + t.Errorf("blurred, the field's glow is %s", got) |
| + } |
| + |
| + drive(t, rig, reducedMotion("dark", true), chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-text").focus()`)) |
| + defer drive(t, rig, reducedMotion("dark", false)) |
| + th.wait(`getComputedStyle(document.querySelector("#ask-text").parentElement, "::after").opacity === "1"`) |
| + if got, want := th.eval(edge), "true 1 none true none none running true 2px -3px 0"; got != want { |
| + t.Errorf("reduced motion, focused: %s, want %s", got, want) |
| + } |
| +} |
| + |
| +// TestAskThinkingShots keeps the thinking bubble and the focus edge at |
| +// 1280 and 390, dark and light, in BASE_BROWSER_ARTIFACTS when it is set. |
| +func TestAskThinkingShots(t *testing.T) { |
| + rig, f := askBase(t) |
| + th := thinking{t, rig, f} |
| + for _, size := range []struct { |
| + name string |
| + view chromedp.Action[chromedp.Void] |
| + }{ |
| + {"1280", chromedp.EmulateViewport(1280, 800)}, |
| + {"390", chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))}, |
| + } { |
| + for _, scheme := range []string{"dark", "light"} { |
| + drive(t, rig, size.view, reducedMotion(scheme, false)) |
| + if size.name == "390" { |
| + drive(t, rig, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(`a.base-btn`)), |
| + chromedp.Click(chromedp.CSS(`a.base-btn`)), chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`)), |
| + chromedp.Poll[chromedp.Void](readJS, chromedp.WithPollingTimeout(5*time.Second)), chromedp.Sleep(500*time.Millisecond)) |
| + } else { |
| + openPanel(t, rig) |
| + } |
| + name := fmt.Sprintf("%s-%s", size.name, scheme) |
| + drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-text").focus()`), chromedp.Sleep(700*time.Millisecond)) |
| + viewportShot(t, rig, name+"-focus") |
| + id := th.send("Can you make me a website?") |
| + f.Emit("activity", map[string]string{"turn_id": id, "label": "Looking at what I can do…"}) |
| + drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`), chromedp.Sleep(1200*time.Millisecond)) |
| + viewportShot(t, rig, name+"-thinking") |
| + drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| + th.wait(`!document.querySelector("#ask-panel .thinking")`) |
| + } |
| + } |
| +} |
diff --git a/internal/equipo/equipotest/chat.go b/internal/equipo/equipotest/chat.go| index 62be430..222bb4b 100644 |
| --- a/internal/equipo/equipotest/chat.go |
| +++ b/internal/equipo/equipotest/chat.go |
| @@ -459,10 +459,34 @@ func (f *Fake) answerTurn(id, text string) { |
| if f.Hold { |
| return |
| } |
| - reply := f.Reply(text) |
| - words := strings.SplitAfter(reply, " ") |
| + f.Answer(id, f.Reply(text), true) |
| +} |
| + |
| +// Answer answers the running turn with id (one Hold kept running): with |
| +// deltas, the reply streamed in word by word first (Stream); then the |
| +// reply message, and the turn replied. |
| +func (f *Fake) Answer(id, reply string, deltas bool) { |
| + if deltas { |
| + f.Stream(id, reply) |
| + } |
| + f.cmu.Lock() |
| + defer f.cmu.Unlock() |
| + if f.turns[id].State != "running" { |
| + return |
| + } |
| + m := message{ID: f.next("m"), Revision: 1, Role: "assistant", Text: reply, TurnID: id, CreatedAt: time.Now().UTC()} |
| + f.messages = append(f.messages, m) |
| + f.emit("message", "message", m.ID, 1, id, m) |
| + t := f.turns[id] |
| + t.State, t.ReplyID, t.Revision, t.UpdatedAt = "replied", m.ID, t.Revision+1, time.Now().UTC() |
| + f.emit("turn", "turn", id, t.Revision, id, *t) |
| +} |
| + |
| +// 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) { |
| offset := 0 |
| - for _, part := range words { |
| + for _, part := range strings.SplitAfter(reply, " ") { |
| select { |
| case <-f.closing: |
| return |
| @@ -477,19 +501,11 @@ func (f *Fake) answerTurn(id, text string) { |
| f.cmu.Unlock() |
| offset += len(part) |
| } |
| - f.cmu.Lock() |
| - defer f.cmu.Unlock() |
| - if f.turns[id].State != "running" { |
| - return |
| - } |
| - m := message{ID: f.next("m"), Revision: 1, Role: "assistant", Text: reply, TurnID: id, CreatedAt: time.Now().UTC()} |
| - f.messages = append(f.messages, m) |
| - f.emit("message", "message", m.ID, 1, id, m) |
| - t := f.turns[id] |
| - t.State, t.ReplyID, t.Revision, t.UpdatedAt = "replied", m.ID, t.Revision+1, time.Now().UTC() |
| - f.emit("turn", "turn", id, t.Revision, id, *t) |
| } |
| +// FailTurn ends the turn with id unanswered: failed. |
| +func (f *Fake) FailTurn(id string) { f.setTurn(id, "failed", "") } |
| + |
| // WaitTurn puts the turn with id in the waiting state: saved, with nothing |
| // yet able to answer it. |
| func (f *Fake) WaitTurn(id string) { f.setTurn(id, "waiting", "") } |
diff --git a/internal/equipo/equipotest/fake.go b/internal/equipo/equipotest/fake.go| index 6d4adad..b3121e6 100644 |
| --- a/internal/equipo/equipotest/fake.go |
| +++ b/internal/equipo/equipotest/fake.go |
| @@ -46,7 +46,7 @@ type Fake struct { |
| URL string |
| Instance string |
| // Reply is what the Assistant answers to a message. Hold keeps every |
| - // new turn running until Finish, so a test can cancel one. |
| + // new turn running until Answer, so a test can cancel one. |
| Reply func(text string) string |
| Hold bool |
diff --git a/internal/look/static/app.css b/internal/look/static/app.css| index 135ae0e..30c86f6 100644 |
| --- a/internal/look/static/app.css |
| +++ b/internal/look/static/app.css |
| @@ -122,6 +122,16 @@ |
| --st-ask: #FFB066; |
| --st-stop: #F9A8D4; |
| --st-wash: .2; |
| + /* The composer's focus edge, teal into violet (each 3:1 on the panel, |
| + in both themes), the glow under it, and the thinking bubble's spark, |
| + which adds rose and the light's amber. */ |
| + --edge-a: #6EE7DC; |
| + --edge-b: #B49CFC; |
| + --edge-hi: #E6FFFB; |
| + --edge-glow-a: rgba(47,198,192,.5); |
| + --edge-glow-b: rgba(139,92,246,.45); |
| + --spk-rose: #F9A8D4; |
| + --spk-amber: #FFC48A; |
| } |
| :root[data-theme="light"] { |
| color-scheme: light; --sky-stop: 55%; --glass-lift: -22px; |
| @@ -152,7 +162,8 @@ |
| --theirs: rgba(255,255,255,.88); --theirs-edge: rgba(30,36,64,.1); --card: rgba(139,92,246,.1); |
| --card-edge: rgba(124,77,232,.35); --pill: rgba(255,255,255,.94); --pill-edge: rgba(30,36,64,.16); |
| --pill-shine: rgba(255,255,255,1); --pill-lift: rgba(40,46,90,.28); --st-ask: #C0651A; |
| - --st-stop: #C2366F; --st-wash: .13; |
| + --st-stop: #C2366F; --st-wash: .13; --edge-a: #0B7A72; --edge-b: #6A3CD6; --edge-hi: #2B58C8; |
| + --edge-glow-a: rgba(15,168,156,.36); --edge-glow-b: rgba(124,77,232,.32); --spk-rose: #C2366F; --spk-amber: #B8641C; |
| } |
| :root[data-theme="dark"] { color-scheme: dark; } |
| @supports (color: light-dark(white, black)) { |
| @@ -237,6 +248,13 @@ |
| --pill-lift: light-dark(rgba(40,46,90,.28), rgba(6,8,22,.55)); |
| --st-ask: light-dark(#C0651A, #FFB066); |
| --st-stop: light-dark(#C2366F, #F9A8D4); |
| + --edge-a: light-dark(#0B7A72, #6EE7DC); |
| + --edge-b: light-dark(#6A3CD6, #B49CFC); |
| + --edge-hi: light-dark(#2B58C8, #E6FFFB); |
| + --edge-glow-a: light-dark(rgba(15,168,156,.36), rgba(47,198,192,.5)); |
| + --edge-glow-b: light-dark(rgba(124,77,232,.32), rgba(139,92,246,.45)); |
| + --spk-rose: light-dark(#C2366F, #F9A8D4); |
| + --spk-amber: light-dark(#B8641C, #FFC48A); |
| } |
| /* The few values that are not colours follow the device too. */ |
| @media (prefers-color-scheme: light) { |
| @@ -633,6 +651,47 @@ aside.ask-panel { position: absolute; top: 16px; right: 16px; bottom: 16px; widt |
| .composer .send svg { fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; } |
| .composer .stop svg { fill: currentColor; } |
| @keyframes pop { from { opacity: 0; transform: scale(.6); } } |
| +/* The composer's focus, in place of the outline: a thin luminous edge |
| + outside the field, teal into violet, that fades in and drifts round (a |
| + turn every 6s), over a soft glow that breathes (4.5s). Only the glow |
| + breathes: the 2px edge stays at full strength, so it is a real focus |
| + indicator. (The masks are black: only their alpha counts.) */ |
| +@property --edge-turn { syntax: "<angle>"; inherits: false; initial-value: 0deg; } |
| +@property --breath { syntax: "<number>"; inherits: false; initial-value: 0; } |
| +.composer textarea.input:focus-visible { outline: none; animation: breathe-glow 2.25s cubic-bezier(.37,0,.63,1) infinite alternate; |
| + box-shadow: 0 0 calc(15px + 6px * var(--breath)) calc(-4px + 3px * var(--breath)) color-mix(in srgb, var(--edge-glow-a) calc(55% + 35% * var(--breath)), transparent), |
| + 0 0 calc(26px + 8px * var(--breath)) calc(-7px + 3px * var(--breath)) color-mix(in srgb, var(--edge-glow-b) calc(55% + 35% * var(--breath)), transparent); } |
| +@keyframes breathe-glow { to { --breath: 1; } } |
| +.composer .field::after { content: ""; position: absolute; inset: -3px; padding: 2px; border-radius: 13px; pointer-events: none; opacity: 0; transition: opacity .35s ease; |
| + -webkit-mask: linear-gradient(black 0 0) content-box, linear-gradient(black 0 0); -webkit-mask-composite: xor; |
| + mask: linear-gradient(black 0 0) content-box exclude, linear-gradient(black 0 0); |
| + animation: edge-turn 6s linear infinite paused; |
| + background: conic-gradient(from var(--edge-turn), var(--edge-a), var(--edge-hi) 9%, var(--edge-a) 20%, var(--edge-b) 50%, var(--edge-a)) border-box; } |
| +.composer .field:has(textarea:focus-visible)::after { opacity: 1; animation-play-state: running; } |
| +@keyframes edge-turn { to { --edge-turn: 360deg; } } |
| +/* Forced colours drop the glow and the edge: the system's outline instead. */ |
| +@media (forced-colors: active) { |
| + .composer textarea.input:focus-visible { outline: 2px solid CanvasText; outline-offset: 3px; } |
| + .composer .field::after { display: none; } |
| +} |
| +/* Thinking: the Assistant's bubble while it works on an answer. It sits |
| + where the answer will be, says what it is doing, and a small spark runs |
| + round its edge (a lap every 4s), its colour drifting through Base's |
| + palette, on a WebGL canvas over the edge that ask.js makes only while |
| + it thinks. Without WebGL (no-gl), a gradient sweeps round the edge |
| + instead; with reduced motion the edge just glows, still. */ |
| +@property --sweep { syntax: "<angle>"; inherits: false; initial-value: 0deg; } |
| +.msg.thinking, .msg.answered { position: relative; } |
| +.msg.thinking { color: var(--haze); } |
| +.msg .spark { position: absolute; left: -8px; top: -8px; width: calc(100% + 16px); height: calc(100% + 16px); pointer-events: none; } |
| +.msg.thinking::before, .msg.answered::before { content: ""; position: absolute; inset: 0; padding: 1.5px; border-radius: inherit; pointer-events: none; opacity: 0; |
| + transition: opacity .35s ease; |
| + -webkit-mask: linear-gradient(black 0 0) content-box, linear-gradient(black 0 0); -webkit-mask-composite: xor; |
| + mask: linear-gradient(black 0 0) content-box exclude, linear-gradient(black 0 0); |
| + background: conic-gradient(from var(--sweep), transparent 0 50%, color-mix(in srgb, var(--spk-amber) 60%, transparent) 64%, |
| + var(--spk-rose) 76%, var(--edge-b) 88%, var(--edge-a) 98%, var(--edge-hi)) border-box; } |
| +.msg.thinking.no-gl::before { opacity: 1; animation: sweep 4s linear infinite; } |
| +@keyframes sweep { to { --sweep: 360deg; } } |
| .ask-page { display: flex; flex-direction: column; padding: 22px 0 0; } |
| .ask-page .crumb { padding: 0 24px; } |
| .ask-page .ask-head { padding-left: 24px; } |
| @@ -733,6 +792,14 @@ main { min-height: 100dvh; } |
| .job[data-state="working"] .state-mark::after { transform: rotate(45deg); } |
| /* The job view just shows: no rise, no sheet sliding up. */ |
| .jobview[popover] { animation: none; } |
| + /* The focus edge holds still and doesn't breathe. The thinking bubble |
| + glows, still, with no spark. */ |
| + .composer .field::after, .composer textarea.input:focus-visible { animation: none; } |
| + .composer textarea.input:focus-visible { --breath: .6; } |
| + .msg .spark { display: none; } |
| + .msg.thinking { box-shadow: inset 0 0 0 1px var(--theirs-edge), 0 0 16px -3px var(--edge-glow-a), 0 0 26px -6px var(--edge-glow-b); } |
| + .msg.thinking::before, .msg.thinking.no-gl::before { opacity: .7; animation: none; |
| + background: linear-gradient(110deg, var(--edge-a), var(--edge-b) 50%, var(--spk-rose)) border-box; } |
| } |
| /* Scratchpad's shared note: a field that grows past one control height. */ |
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js| index dbc6a98..6d9c24c 100644 |
| --- a/internal/look/static/ask.js |
| +++ b/internal/look/static/ask.js |
| @@ -83,7 +83,8 @@ function start(root) { |
| return id ? thread.querySelector(`[${attr}="${CSS.escape(id)}"]`) : null; |
| } |
| - // A message, once: the Assistant's reply takes its streamed draft's place. |
| + // A message, once: the Assistant's reply takes its streamed draft's |
| + // place, or becomes its thinking bubble, where it is. |
| function addMessage(m) { |
| if (!m || !m.id || item("data-id", m.id)) return; |
| const li = document.createElement("li"); |
| @@ -91,22 +92,40 @@ function start(root) { |
| li.dataset.id = m.id; |
| if (m.role === "person") li.textContent = m.text; |
| else rich(li, m.text); |
| - const draft = m.role !== "person" && item("data-draft", m.turn_id); |
| - if (draft) draft.replaceWith(li); |
| + const draft = m.role !== "person" && (item("data-draft", m.turn_id) || bubbles.get(m.turn_id)); |
| + if (draft && draft.querySelector(":scope > .think-line")) become(draft, li); |
| + else if (draft) draft.replaceWith(li); |
| else thread.append(li); |
| scroll(); |
| } |
| + // A streamed word: the turn's thinking bubble, if it shows, holds the |
| + // draft. |
| function delta(d) { |
| let li = item("data-draft", d.turn_id); |
| - if (!li) { |
| + const bubble = !li && bubbles.get(d.turn_id); |
| + if (bubble && bubble.isConnected) { |
| + li = bubble; |
| + bubbles.delete(d.turn_id); |
| + const line = li.querySelector(":scope > .think-line"); |
| + for (const n of [line, li]) for (const a of n.getAnimations()) a.cancel(); |
| + line.style.display = ""; |
| + line.textContent = ""; |
| + li.classList.replace("thinking", "draft"); |
| + li.classList.add("answered"); |
| + delete li.dataset.thinking; |
| + li.dataset.draft = d.turn_id; |
| + li.setAttribute("aria-busy", "true"); |
| + spark.stop(li); |
| + } else if (!li) { |
| li = document.createElement("li"); |
| li.className = "msg coordinator draft"; |
| li.dataset.draft = d.turn_id; |
| li.setAttribute("aria-busy", "true"); |
| thread.append(li); |
| } |
| - if (d.offset === li.textContent.length) li.textContent += d.text; |
| + const box = li.querySelector(":scope > .think-line") || li; |
| + if (d.offset === box.textContent.length) box.textContent += d.text; |
| scroll(); |
| } |
| @@ -600,6 +619,393 @@ function start(root) { |
| }); |
| } |
| + // While the Assistant works on an answer, a small spark runs round its |
| + // thinking bubble's edge: a bright point with a short fading trail, its |
| + // colour drifting through Base's palette (teal, violet, rose, the |
| + // light's amber, mist), and now and then a faint crackle. It is drawn |
| + // with WebGL on a canvas over the edge, made only while a bubble thinks; |
| + // the loop ends once it has faded out, or the bubble has gone or is out |
| + // of sight. Without WebGL, or once its context is lost, the bubble gets |
| + // no-gl and the CSS sweep instead; with reduced motion, no spark at all |
| + // (the CSS glow alone). (The approved mockup's renderer, as it is.) |
| + const still = matchMedia("(prefers-reduced-motion: reduce)"); |
| + const lightDark = CSS.supports("color", "light-dark(white, black)"); |
| + function lightTheme() { |
| + const theme = document.documentElement.dataset.theme; |
| + return theme === "light" || (theme !== "dark" && lightDark && matchMedia("(prefers-color-scheme: light)").matches); |
| + } |
| + const spark = (() => { |
| + const cv = document.createElement("canvas"); |
| + cv.className = "spark"; |
| + cv.setAttribute("aria-hidden", "true"); |
| + const PAD = 8, LAP = 4000, CYCLE = 12000; |
| + const VS = "attribute vec2 p; void main() { gl_Position = vec4(p, 0.0, 1.0); }"; |
| + const FS = ` |
| + #ifdef GL_FRAGMENT_PRECISION_HIGH |
| + precision highp float; |
| + #else |
| + precision mediump float; |
| + #endif |
| + uniform vec2 u_size, u_half, u_hp; |
| + uniform vec4 u_rad; // corner radii: top-left, top-right, bottom-right, bottom-left |
| + uniform float u_head, u_len, u_dpr, u_time, u_alpha, u_flick, u_add, u_white, u_cyc; |
| + uniform vec3 u_pal[5]; |
| + uniform vec4 u_br[3]; |
| + const float PI = 3.14159265; |
| + float hash(float n) { return fract(sin(n) * 43758.5453); } |
| + float noise(float x) { float i = floor(x), f = fract(x); return mix(hash(i), hash(i + 1.0), f * f * (3.0 - 2.0 * f)); } |
| + float jag(float x) { float i = floor(x); return mix(hash(i), hash(i + 1.0), fract(x)); } |
| + vec3 pal(float x) { |
| + x = fract(x) * 5.0; float i = floor(x), f = fract(x); f = f * f * (3.0 - 2.0 * f); |
| + vec3 a = u_pal[0], b = u_pal[0]; |
| + for (int k = 0; k < 5; k++) { if (float(k) == i) a = u_pal[k]; if (float(k) == mod(i + 1.0, 5.0)) b = u_pal[k]; } |
| + return mix(a, b, f); |
| + } |
| + float qr(vec2 p) { return p.x > 0.0 ? (p.y < 0.0 ? u_rad.y : u_rad.z) : (p.y < 0.0 ? u_rad.x : u_rad.w); } |
| + float sdRR(vec2 p) { float r = qr(p); vec2 q = abs(p) - u_half + r; return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r; } |
| + // Distance along the edge, clockwise from the top edge's left end (y down), for the nearest edge point. |
| + float arcPos(vec2 p) { |
| + float tl = u_rad.x, tr = u_rad.y, br = u_rad.z, bl = u_rad.w, hx = u_half.x, hy = u_half.y; |
| + float T = 2.0 * hx - tl - tr, R = 2.0 * hy - tr - br, B = 2.0 * hx - br - bl, L = 2.0 * hy - bl - tl; |
| + float r = qr(p); vec2 q = abs(p) - (u_half - r); |
| + if (q.x > 0.0 && q.y > 0.0) { |
| + vec2 v = p - sign(p) * (u_half - r); float th = atan(v.y, v.x); |
| + if (p.x > 0.0 && p.y < 0.0) return T + r * (th + 0.5 * PI); |
| + if (p.x > 0.0) return T + 0.5 * PI * tr + R + r * th; |
| + if (p.y > 0.0) return T + 0.5 * PI * (tr + br) + R + B + r * (th - 0.5 * PI); |
| + return T + 0.5 * PI * (tr + br + bl) + R + B + L + r * (th + PI); |
| + } |
| + if (q.y > q.x) return p.y < 0.0 ? p.x + hx - tl : T + 0.5 * PI * (tr + br) + R + (hx - br - p.x); |
| + return p.x > 0.0 ? T + 0.5 * PI * tr + (p.y + hy - tr) : T + 0.5 * PI * (tr + br + bl) + R + B + (hy - bl - p.y); |
| + } |
| + void main() { |
| + vec2 p = gl_FragCoord.xy - 0.5 * u_size; p.y = -p.y; |
| + float sd = sdRR(p), d = abs(sd), s = arcPos(p); |
| + float inner = sd < 0.0 ? 0.5 : 1.0; // softer on the text side of the edge |
| + float delta = mod(u_head - s, u_len); |
| + float tl = min(u_len * 0.2, 110.0 * u_dpr); // trail length |
| + float k = clamp(delta / tl, 0.0, 1.0); |
| + float trail = pow(1.0 - k, 2.2) * smoothstep(0.0, 6.0 * u_dpr, delta) * (0.74 + 0.26 * noise(s / u_dpr * 0.4 - u_time * 26.0)); |
| + float w = 1.05 * u_dpr, gw = 3.6 * u_dpr; |
| + float line = trail * (exp(-d * d / (w * w)) + 0.42 * inner * exp(-d * d / (gw * gw))); |
| + float hd = length(p - u_hp); |
| + float hcore = exp(-hd * hd / (2.3 * 2.3 * u_dpr * u_dpr)); |
| + float head = hcore + 0.55 * inner * exp(-hd * hd / (4.6 * 4.6 * u_dpr * u_dpr)); |
| + float br = 0.0; |
| + for (int i = 0; i < 3; i++) { |
| + vec2 a = u_br[i].xy, e = u_br[i].zw, ab = e - a; |
| + float t = clamp(dot(p - a, ab) / max(dot(ab, ab), 1.0), 0.0, 1.0); |
| + vec2 n = normalize(vec2(-ab.y, ab.x) + 1e-5); |
| + vec2 c = a + ab * t + n * (jag(t * 4.0 + float(i) * 7.3 + floor(u_time * 24.0)) - 0.5) * 2.2 * u_dpr * t; |
| + float bd = length(p - c); |
| + br += (1.0 - t) * (exp(-bd * bd / (0.6 * 0.6 * u_dpr * u_dpr)) + 0.25 * exp(-bd / (2.4 * u_dpr))); |
| + } |
| + br *= u_flick; |
| + float I = clamp(line + head + br, 0.0, 1.0) * u_alpha; |
| + // The head carries the palette's current colour; the trail holds the colours it has just passed through. |
| + vec3 col = pal(u_cyc - 0.45 * (delta > 0.5 * u_len ? 0.0 : k)); |
| + col = mix(col, vec3(1.0), clamp(hcore * 1.2 + br * 0.5, 0.0, 1.0) * u_white); |
| + gl_FragColor = vec4(col * I, I * u_add); |
| + }`; |
| + // The WebGL context is made the first time the Assistant thinks, so a |
| + // page that never waits holds none. |
| + let gl = null, tried = false; |
| + const U = {}; |
| + function init() { |
| + tried = true; |
| + geo = null; |
| + gl = cv.getContext("webgl", { alpha: true, premultipliedAlpha: true, antialias: false, depth: false, stencil: false, powerPreference: "low-power" }); |
| + if (!gl) return; |
| + const sh = (type, src) => { |
| + const s = gl.createShader(type); |
| + gl.shaderSource(s, src); |
| + gl.compileShader(s); |
| + return gl.getShaderParameter(s, gl.COMPILE_STATUS) ? s : null; |
| + }; |
| + const vs = sh(gl.VERTEX_SHADER, VS), fs = sh(gl.FRAGMENT_SHADER, FS), prog = gl.createProgram(); |
| + if (vs && fs) { |
| + gl.attachShader(prog, vs); |
| + gl.attachShader(prog, fs); |
| + gl.linkProgram(prog); |
| + } |
| + if (!vs || !fs || !gl.getProgramParameter(prog, gl.LINK_STATUS)) { |
| + gl = null; |
| + return; |
| + } |
| + gl.useProgram(prog); |
| + gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer()); |
| + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW); |
| + const loc = gl.getAttribLocation(prog, "p"); |
| + gl.enableVertexAttribArray(loc); |
| + gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0); |
| + for (const n of ["u_size", "u_half", "u_hp", "u_rad", "u_head", "u_len", "u_dpr", "u_time", "u_alpha", "u_flick", "u_add", "u_white", "u_cyc", "u_pal", "u_br"]) { |
| + U[n] = gl.getUniformLocation(prog, n); |
| + } |
| + } |
| + cv.addEventListener("webglcontextlost", (e) => { |
| + e.preventDefault(); |
| + gl = null; |
| + const was = host; |
| + end(); |
| + if (was && on) was.classList.add("no-gl"); |
| + }); |
| + |
| + // The edge's centre line, in device pixels from the canvas centre, and |
| + // a point on it by distance (clockwise, y down). |
| + let host = null, geo = null, raf = 0, on = false, alpha = 0, last = 0, t0 = 0, nextCrackle = 0, crackle = null; |
| + function measure() { |
| + const dpr = Math.min(devicePixelRatio || 1, 2), w = host.offsetWidth, h = host.offsetHeight; |
| + if (geo && geo.w === w && geo.h === h && geo.dpr === dpr) return geo; |
| + cv.width = Math.round((w + 2 * PAD) * dpr); |
| + cv.height = Math.round((h + 2 * PAD) * dpr); |
| + const cs = getComputedStyle(host), hx = (w / 2 - 0.5) * dpr, hy = (h / 2 - 0.5) * dpr; |
| + const rad = ["borderTopLeftRadius", "borderTopRightRadius", "borderBottomRightRadius", "borderBottomLeftRadius"] |
| + .map((k) => Math.max(0.5, Math.min((parseFloat(cs[k]) || 0) - 0.5, w / 2 - 0.5, h / 2 - 0.5)) * dpr); |
| + const [tl, tr, br, bl] = rad, T = 2 * hx - tl - tr, R = 2 * hy - tr - br, B = 2 * hx - br - bl, L = 2 * hy - bl - tl; |
| + const arc = (cx, cy, r, from) => [Math.PI * r / 2, (u) => { |
| + const t = from + u / r; |
| + return [cx + r * Math.cos(t), cy + r * Math.sin(t), Math.cos(t), Math.sin(t)]; |
| + }]; |
| + const segs = [ |
| + [T, (u) => [-hx + tl + u, -hy, 0, -1]], arc(hx - tr, -hy + tr, tr, -Math.PI / 2), |
| + [R, (u) => [hx, -hy + tr + u, 1, 0]], arc(hx - br, hy - br, br, 0), |
| + [B, (u) => [hx - br - u, hy, 0, 1]], arc(-hx + bl, hy - bl, bl, Math.PI / 2), |
| + [L, (u) => [-hx, hy - bl - u, -1, 0]], arc(-hx + tl, -hy + tl, tl, Math.PI), |
| + ]; |
| + geo = { w, h, dpr, hx, hy, rad, segs, len: segs.reduce((n, [l]) => n + l, 0) }; |
| + gl.viewport(0, 0, cv.width, cv.height); |
| + return geo; |
| + } |
| + function pointAt(s) { |
| + s = ((s % geo.len) + geo.len) % geo.len; |
| + for (const [l, f] of geo.segs) { |
| + if (s <= l) return f(s); |
| + s -= l; |
| + } |
| + return geo.segs[0][1](0); |
| + } |
| + const hex = (h) => [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16) / 255); |
| + // Teal, violet, rose, the light beam's amber, pale mist: brighter on |
| + // dark glass, deeper on light so they read on white. |
| + const THEMES = { |
| + dark: { pal: ["#5EE3DA", "#A78BFA", "#F9A8D4", "#FFC48A", "#DCE4F5"].flatMap(hex), white: 0.75, add: 0.55 }, |
| + light: { pal: ["#0FA89C", "#7C4DE8", "#D23F8A", "#C9741F", "#5E6A96"].flatMap(hex), white: 0, add: 1 }, |
| + }; |
| + |
| + function end() { |
| + cancelAnimationFrame(raf); |
| + raf = 0; |
| + last = 0; |
| + alpha = 0; |
| + cv.remove(); |
| + } |
| + function frame(now) { |
| + raf = 0; |
| + if (!gl || still.matches || !host || !host.isConnected || !host.checkVisibility({ visibilityProperty: true })) { |
| + end(); |
| + return; |
| + } |
| + const dt = Math.min(now - (last || now), 100); |
| + last = now; |
| + alpha = on ? Math.min(1, alpha + dt / 260) : Math.max(0, alpha - dt / 360); |
| + if (!alpha && !on) { |
| + end(); // faded out: the loop ends here |
| + return; |
| + } |
| + const g = measure(); |
| + gl.clearColor(0, 0, 0, 0); |
| + gl.clear(gl.COLOR_BUFFER_BIT); |
| + const t = now - t0, head = ((t % LAP) / LAP) * g.len, [hx, hy] = pointAt(head); |
| + // Now and then, a crackle: two or three short jagged branches flick |
| + // out from the spark for a moment. |
| + if (now >= nextCrackle) { |
| + crackle = { at: now, b: Array.from({ length: 2 + (Math.random() < 0.5 ? 1 : 0) }, () => ({ ang: (Math.random() * 2 - 1) * 1.1, len: 3.5 + Math.random() * 3.5, back: Math.random() * 5 })) }; |
| + nextCrackle = now + 1200 + Math.random() * 1800; |
| + } |
| + const br = new Float32Array(12); |
| + let flick = 0; |
| + const ck = crackle ? now - crackle.at : Infinity; |
| + if (ck < 170) { |
| + flick = (1 - ck / 170) * (Math.floor(ck / 34) % 2 ? 0.35 : 1); |
| + crackle.b.forEach((b, i) => { |
| + const [sx, sy, snx, sny] = pointAt(head - b.back * g.dpr); |
| + const cs = Math.cos(b.ang), sn = Math.sin(b.ang), dx = snx * cs - sny * sn, dy = snx * sn + sny * cs, L = b.len * g.dpr; |
| + br.set([sx, sy, sx + dx * L, sy + dy * L], i * 4); |
| + }); |
| + } |
| + const th = THEMES[lightTheme() ? "light" : "dark"]; |
| + gl.uniform2f(U.u_size, cv.width, cv.height); |
| + gl.uniform2f(U.u_half, g.hx, g.hy); |
| + gl.uniform2f(U.u_hp, hx, hy); |
| + gl.uniform4fv(U.u_rad, g.rad); |
| + gl.uniform1f(U.u_head, head); |
| + gl.uniform1f(U.u_len, g.len); |
| + gl.uniform1f(U.u_dpr, g.dpr); |
| + gl.uniform1f(U.u_time, t / 1000); |
| + gl.uniform1f(U.u_alpha, alpha * alpha * (3 - 2 * alpha)); |
| + gl.uniform1f(U.u_flick, flick); |
| + gl.uniform1f(U.u_add, th.add); |
| + gl.uniform1f(U.u_white, th.white); |
| + gl.uniform1f(U.u_cyc, t / CYCLE); |
| + gl.uniform3fv(U.u_pal, th.pal); |
| + gl.uniform4fv(U.u_br, br); |
| + gl.drawArrays(gl.TRIANGLES, 0, 3); |
| + raf = requestAnimationFrame(frame); |
| + } |
| + function kick() { |
| + if (!on || still.matches || !host || !host.isConnected) return; |
| + // The shaders compile in the first frame, after the bubble has drawn. |
| + if (!tried) { |
| + if (!raf) raf = requestAnimationFrame(() => { |
| + raf = 0; |
| + init(); |
| + kick(); |
| + }); |
| + return; |
| + } |
| + if (!gl) { |
| + host.classList.add("no-gl"); |
| + return; |
| + } |
| + if (cv.parentNode !== host) { |
| + host.append(cv); |
| + geo = null; |
| + } |
| + if (!raf) raf = requestAnimationFrame(frame); |
| + } |
| + still.addEventListener("change", kick); |
| + return { |
| + // Start on a thinking bubble. |
| + start(el) { |
| + if (host !== el) { |
| + end(); |
| + host = el; |
| + } |
| + on = true; |
| + t0 = performance.now(); |
| + nextCrackle = t0 + 900 + Math.random() * 900; |
| + kick(); |
| + }, |
| + // Stop on el, if the spark is on it: it fades out, or ends at once |
| + // (now) when el is going away. |
| + stop(el, now) { |
| + if (host !== el) return; |
| + on = false; |
| + if (now) end(); |
| + }, |
| + }; |
| + })(); |
| + |
| + // The thinking bubble, one per turn being answered: on the Assistant's |
| + // side, at the end of the conversation, where its answer will appear. It |
| + // says what the Assistant is doing ("Thinking…", then Equipo's activity |
| + // lines); the streamed words fill it and the reply becomes it, in place; |
| + // Stop or a failure takes it away. Its words are in .think-line until |
| + // the reply has landed. Without scripts the status line says it instead. |
| + const bubbles = new Map(); |
| + function think(id, words) { |
| + const b = bubbles.get(id); |
| + if (b && b.isConnected) { |
| + if (words) say(b, words); |
| + return; |
| + } |
| + // Its words are already streaming in (a late, stale revision). |
| + if (item("data-draft", id)) return; |
| + const li = document.createElement("li"); |
| + li.className = "msg coordinator thinking"; |
| + li.dataset.thinking = id; |
| + li.append(span("think-line", words || "Thinking…")); |
| + thread.append(li); |
| + bubbles.set(id, li); |
| + spark.start(li); |
| + scroll(); |
| + } |
| + function say(b, words) { |
| + const line = b.querySelector(":scope > .think-line"); |
| + if (line && line.textContent !== words) swap(line, () => { line.textContent = words; }); |
| + } |
| + // drop takes turn id's bubble away, and its spark with it. |
| + function drop(id) { |
| + const b = bubbles.get(id); |
| + bubbles.delete(id); |
| + if (!b) return; |
| + spark.stop(b, true); |
| + b.remove(); |
| + } |
| + // unthink settles turn t's bubble as the turn ends. Replied, the bubble |
| + // waits a little for the reply (it may come just after); otherwise, or |
| + // if it never comes, it goes. |
| + function unthink(t) { |
| + const b = bubbles.get(t.id); |
| + if (!b) return; |
| + if (t.state === "replied" && !(t.reply_id && item("data-id", t.reply_id))) { |
| + setTimeout(() => { if (bubbles.get(t.id) === b) drop(t.id); }, 5000); |
| + return; |
| + } |
| + drop(t.id); |
| + } |
| + // become makes bubble b the reply li, in place: its spark fades, and a |
| + // bubble that was still thinking crossfades its words into the reply's. |
| + function become(b, li) { |
| + const turnID = b.dataset.thinking || b.dataset.draft; |
| + bubbles.delete(turnID); |
| + spark.stop(b); |
| + const thinking = b.classList.contains("thinking"); |
| + b.classList.remove("thinking", "draft"); |
| + b.classList.add("answered"); |
| + delete b.dataset.thinking; |
| + delete b.dataset.draft; |
| + b.removeAttribute("aria-busy"); |
| + b.dataset.id = li.dataset.id; |
| + const line = b.querySelector(":scope > .think-line"); |
| + const land = () => line.replaceChildren(...li.childNodes); |
| + const unwrap = () => { if (line.isConnected) line.replaceWith(...line.childNodes); }; |
| + if (thinking) swap(line, land, unwrap); |
| + else { |
| + for (const n of [line, b]) for (const a of n.getAnimations()) a.cancel(); |
| + land(); |
| + unwrap(); |
| + } |
| + } |
| + // swap changes the words in el, a bubble's line: the old ones fade out, |
| + // the bubble eases to its new size (the conversation following its foot |
| + // while it is pinned there), then the new ones fade in, and done runs. |
| + // A straight swap with reduced motion, or out of sight. |
| + function swap(el, apply, done) { |
| + const box = el.parentElement; |
| + for (const n of [el, box]) for (const a of n.getAnimations()) a.cancel(); |
| + el.style.display = ""; |
| + const finish = () => { if (done) done(); }; |
| + if (still.matches || !el.getClientRects().length) { |
| + apply(); |
| + finish(); |
| + return; |
| + } |
| + const out = el.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 150, easing: "ease-in", fill: "forwards" }); |
| + out.onfinish = () => { |
| + const w0 = box.offsetWidth, h0 = box.offsetHeight; |
| + apply(); |
| + const w1 = box.offsetWidth, h1 = box.offsetHeight; |
| + el.style.display = "none"; // out of layout while the box resizes, so it cannot overflow and nudge the scroll |
| + const grow = box.animate([{ width: w0 + "px", height: h0 + "px" }, { width: w1 + "px", height: h1 + "px" }], { duration: 220, easing: "cubic-bezier(.2,.8,.2,1)" }); |
| + const follow = () => { |
| + if (pinned) toBottom(); |
| + if (grow.playState === "running") requestAnimationFrame(follow); |
| + }; |
| + follow(); |
| + grow.onfinish = () => { |
| + el.style.display = ""; |
| + follow(); |
| + out.cancel(); |
| + el.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 260, easing: "ease-out" }).onfinish = finish; |
| + }; |
| + grow.oncancel = () => { |
| + el.style.display = ""; |
| + out.cancel(); |
| + }; |
| + }; |
| + } |
| + |
| const ended = (s) => s === "replied" || s === "failed" || s === "cancelled"; |
| // The field's end holds Stop while a turn runs, else Send once something |
| @@ -619,8 +1025,12 @@ function start(root) { |
| // never the turn in flight. Each turn's kind is kept for its activity. |
| const quietTurns = new Set(); |
| const isQuiet = (id) => quietTurns.has(id); |
| + // A turn that has ended stays ended: the send's own answer, carrying the |
| + // turn as it was queued, can come after the turn's events have ended it. |
| + const over = new Set(); |
| function setTurn(t, line) { |
| - if (t && !t.id) return; |
| + if (t && (!t.id || over.has(t.id))) return; |
| + if (t && ended(t.state)) over.add(t.id); |
| if (t && t.kind === "team_update") { |
| quietTurns.add(t.id); |
| if (ended(t.state)) settle(t); |
| @@ -630,11 +1040,18 @@ function start(root) { |
| turn = t; |
| cancel.action = `/ask/turns/${encodeURIComponent(t.id)}/cancel`; |
| paint(); |
| - status.textContent = line || (t.state === "waiting" |
| - ? "Ask Base can't answer right now. It will as soon as it can." |
| - : "Thinking…"); |
| + // Waiting is a notice, not an answer on its way: the status line |
| + // says it. Otherwise the thinking bubble does, and the line is empty. |
| + if (t.state === "waiting") { |
| + drop(t.id); |
| + status.textContent = line || "Ask Base can't answer right now. It will as soon as it can."; |
| + } else { |
| + status.textContent = ""; |
| + think(t.id, line); |
| + } |
| return; |
| } |
| + if (t) unthink(t); |
| if (t && turn && t.id !== turn.id) return; |
| turn = null; |
| paint(); |
| @@ -664,7 +1081,11 @@ function start(root) { |
| on("card", (e) => (e.data && e.data.kind === "job" ? addJob(e.data) : addCard(e.data))); |
| on("turn", (e) => setTurn(e.data)); |
| on("activity", (e) => { |
| - if (turn && e.data.turn_id === turn.id && !isQuiet(e.data.turn_id) && e.data.label) status.textContent = e.data.label; |
| + const id = e.data.turn_id, label = e.data.label; |
| + if (!label || isQuiet(id)) return; |
| + const b = bubbles.get(id); |
| + if (b && b.isConnected) say(b, label); |
| + else if (turn && id === turn.id) status.textContent = label; |
| }); |
| on("state", (e) => { |
| quiet(); |
| @@ -701,6 +1122,7 @@ function start(root) { |
| // An open job view stays open, with focus in it if it was. |
| const focused = viewing && item("data-job", viewing)?.querySelector(".jobview")?.contains(document.activeElement); |
| thread.replaceChildren(); |
| + bubbles.clear(); |
| pinned = true; |
| s.messages.forEach(addMessage); |
| for (const j of s.jobs || []) { |