Show Connect Oficina as a button at the end of the reply that asked
An open connect card is now a filled button with the Oficina mark, "Connect Oficina", and a line naming what it unlocks, drawn at the end of the Assistant's newest reply instead of as a separate card with a plain link. The server render (/ask, the docked chat) and the panel's script place it the same way. Live, the relay passes the turn that raised a connect card (from the event's own turn_id, never the card's data). The panel holds the card while that turn's reply is on its way and puts it in the reply once it lands. If no words come for three seconds it shows on its own below the thinking bubble, and moves into the reply when it arrives. Once connected it becomes a quiet "Connected" line where it was; a fresh read no longer draws it (the chat's approved note says it once). The snapshot carries no turn for a card, so a fresh read can only put the button at the end of the newest reply; the script moves it there too when a later reply lands, so both agree. The new words are pending copy review (docs/copy-pending.md, Review 12).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
11 files changed,
+475
−6
docs/copy-pending.md+8 −0internal/browsertest/connect_button_test.go+97 −0internal/equipo/chat.go+13 −0internal/equipo/equipotest/chat.go+13 −0internal/look/static/app.css+19 −0internal/look/static/ask.js+124 −1internal/web/ask.go+78 −0internal/web/ask_relay.go+7 −2internal/web/relay_internal_test.go+25 −0internal/web/templates/partials/chat.html+6 −3internal/webtest/connect_offer_test.go+85 −0
diff --git a/docs/copy-pending.md b/docs/copy-pending.md| index 884d7db..775d48f 100644 |
| --- a/docs/copy-pending.md |
| +++ b/docs/copy-pending.md |
| @@ -409,3 +409,11 @@ Review 11 (approved, Paul, 2026-11-16, copy review arfnqquz6wks4vdyxptv6ditju): |
| | ask_oficina.go, partials/chat.html | APPROVED (Paul, 2026-11-16): the line after the person refused, or stopped before finishing | Oficina wasn't connected. | |
| | ask_oficina.go, partials/chat.html | APPROVED (Paul, 2026-11-16): the line after the connect link had expired | That link expired. Try again. | |
| | ask_oficina.go, partials/chat.html | APPROVED (Paul, 2026-11-16): the line when Oficina, or the way to it, could not be reached | Couldn't reach Oficina. Try again later. | |
| + |
| +Review 12 (pending): the Connect button in the chat. When the Assistant needs Oficina to do what the person asked, its reply ends with a button instead of a separate card with a plain link. The button goes to Base's own connect page in the same tab. Once the person has connected, the button turns into a quiet line in the same place. The reply's own words come from the Assistant (Equipo's copy). |
| + |
| +| File | Context | Text | |
| +|---|---|---| |
| +| partials/chat.html, ask.js | PENDING: the button at the end of the Assistant's reply, beside the Oficina mark | Connect Oficina | |
| +| partials/chat.html, ask.js | PENDING: the line under the button, naming what connecting unlocks | Calendar, Docs, Sheets and more | |
| +| ask.js | PENDING: what the button becomes once Oficina is connected, after a tick icon (proposed as "Connected ✓"; the tick is drawn, not typed) | Connected | |
diff --git a/internal/browsertest/connect_button_test.go b/internal/browsertest/connect_button_test.go| new file mode 100644 |
| index 0000000..c815814 |
| --- /dev/null |
| +++ b/internal/browsertest/connect_button_test.go |
| @@ -0,0 +1,97 @@ |
| +//go:build browser |
| + |
| +package browsertest |
| + |
| +import ( |
| + "strconv" |
| + "testing" |
| + "time" |
| + |
| + "github.com/chromedp/chromedp" |
| +) |
| + |
| +// offerJS says where connect card id is: "none" while it is not drawn, |
| +// "loose" on its own in the conversation (with its place among the |
| +// thread's lines and the thinking bubble's or draft's), or "in reply" once |
| +// it is the last thing in a settled reply that starts with reply, its |
| +// button going to Base's own connect page in this tab. |
| +func offerJS(id, reply string) string { |
| + return `(() => { const o = document.querySelector('#ask-panel [data-card=' + ` + strconv.Quote(strconv.Quote(id)) + ` + ']'); |
| + if (!o) return "none"; |
| + const li = o.closest("li.msg.coordinator"); |
| + if (!li) { const kids = [...document.querySelectorAll("#ask-panel .thread > li")]; |
| + const busy = kids.findIndex((k) => k.matches(".thinking, [data-draft]")); |
| + return "loose " + kids.indexOf(o) + "/" + busy + "/" + kids.length; } |
| + if (!li.dataset.id || li.matches(".thinking, [data-draft]")) return "in a draft"; |
| + if (li.lastElementChild !== o) return "not last in its reply"; |
| + if (!li.textContent.startsWith(` + strconv.Quote(reply) + `)) return "in another reply: " + li.textContent.slice(0, 40); |
| + const a = o.querySelector("a.connect-btn"); |
| + if (!a) return "no button"; |
| + if (a.hasAttribute("target") || new URL(a.href).pathname !== "/ask/oficina/connect") return "button goes " + a.href + " " + a.target; |
| + return "in reply"; })()` |
| +} |
| + |
| +// A connect card is a button at the end of the Assistant's reply that |
| +// raised it, never above it: its event comes while the reply streams, and |
| +// it waits for the reply to land. When no words come for a while it shows |
| +// on its own below the thinking bubble, and moves into the reply once it |
| +// lands. Once connected it is a quiet line in the same place. |
| +func TestAskConnectButtonFollowsItsReply(t *testing.T) { |
| + rig, f, _ := askBaseOn(t, nil, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))) |
| + th := thinking{t, rig, f} |
| + drive(t, rig, reducedMotion("dark", false), 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)) |
| + |
| + const reply = "Connect your Oficina apps and I'll show you this week." |
| + turn := th.send("What's on my calendar this week?") |
| + id := f.RaiseConnect(turn, rig.Origin+"/ask/oficina/connect") |
| + drive(t, rig, chromedp.Sleep(600*time.Millisecond)) |
| + if got := th.eval(offerJS(id, reply)); got != "none" { |
| + t.Errorf("while the reply is on its way the card is %q, want held (none)", got) |
| + } |
| + f.Stream(turn, reply) |
| + if got := th.eval(offerJS(id, reply)); got != "none" { |
| + t.Errorf("while the reply streams the card is %q, want held (none)", got) |
| + } |
| + f.Answer(turn, reply, false) |
| + th.wait(`!!document.querySelector('#ask-panel .thread > li.msg.coordinator[data-id]') && !document.querySelector("#ask-panel .thinking, #ask-panel [data-draft]")`) |
| + drive(t, rig, chromedp.Sleep(1500*time.Millisecond)) |
| + viewportShot(t, rig, "connect-390-reply") |
| + th.wait(offerJS(id, reply) + ` === "in reply"`) |
| + if got := th.eval(`[...document.querySelectorAll('#ask-panel .connect-offer')].map((o) => o.innerText.replace(/\s+/g, " ").trim()).join("|")`); got != "Connect Oficina Calendar, Docs, Sheets and more" { |
| + t.Errorf("the offer says %q", got) |
| + } |
| + if n := th.eval(`String(document.querySelectorAll("#ask-panel li.msg.card").length)`); n != "0" { |
| + t.Errorf("%s loose cards beside the offer, want none", n) |
| + } |
| + |
| + // Connected: the button goes, a quiet line takes its place. |
| + f.SetCard(id, "resolved", "") |
| + th.wait(`(() => { const o = document.querySelector('#ask-panel li.msg.coordinator > .connect-offer.connected'); return !!o && !o.querySelector("a") && o.innerText.trim() === "Connected"; })()`) |
| + viewportShot(t, rig, "connect-390-connected") |
| + |
| + // No words for a while: the card shows on its own after the thinking |
| + // bubble, the draft stays above it, and the reply takes it in. |
| + const again = "Connect your Oficina apps and I'll find you a free slot." |
| + turn = th.send("When am I free tomorrow?") |
| + id = f.RaiseConnect(turn, rig.Origin+"/ask/oficina/connect") |
| + th.wait(offerJS(id, again) + `.startsWith("loose")`) |
| + if got := th.eval(`(() => { const kids = [...document.querySelectorAll("#ask-panel .thread > li")]; return String(kids.at(-1).matches(".connect-offer") && kids.at(-2).matches(".thinking")); })()`); got != "true" { |
| + t.Errorf("without words the card is not right after the thinking bubble: %s", th.eval(offerJS(id, again))) |
| + } |
| + f.Stream(turn, again) |
| + if got := th.eval(`(() => { const kids = [...document.querySelectorAll("#ask-panel .thread > li")]; return String(kids.at(-1).matches(".connect-offer") && kids.at(-2).matches("[data-draft]")); })()`); got != "true" { |
| + t.Errorf("while words stream the card is not right below the draft: %s", th.eval(offerJS(id, again))) |
| + } |
| + f.Answer(turn, again, false) |
| + th.wait(offerJS(id, again) + ` === "in reply"`) |
| + // The first, connected, stays with its own reply. |
| + if n := th.eval(`String(document.querySelectorAll("#ask-panel .connect-offer.connected").length)`); n != "1" { |
| + t.Errorf("%s connected lines, want the first one kept", n) |
| + } |
| + viewportShot(t, rig, "connect-390-second") |
| + drive(t, rig, reducedMotion("light", false), chromedp.Sleep(300*time.Millisecond)) |
| + viewportShot(t, rig, "connect-390-second-light") |
| + noOverflow(t, rig, "the chat with a connect button") |
| +} |
diff --git a/internal/equipo/chat.go b/internal/equipo/chat.go| index f513dfe..18332e3 100644 |
| --- a/internal/equipo/chat.go |
| +++ b/internal/equipo/chat.go |
| @@ -64,6 +64,9 @@ type Card struct { |
| Job string `json:"job,omitempty"` |
| Detail string `json:"detail,omitempty"` |
| StartedAt *time.Time `json:"started_at,omitempty"` |
| + // Turn is the turn that raised a connect card, as its event said |
| + // (WithTurn); never read from the card's own fields. |
| + Turn string `json:"turn_id,omitempty"` |
| } |
| // Option is one answer a card offers: Label on the button, Value sent. |
| @@ -158,6 +161,16 @@ func (c Card) CleanAt(base string) (Card, bool) { |
| return out, true |
| } |
| +// WithTurn is a connect card with Turn set to turn, the turn its event |
| +// says raised it, when turn looks like an id (validJob). Every other card, |
| +// and a turn that does not look like one, is c as it is. |
| +func (c Card) WithTurn(turn string) Card { |
| + if c.Kind == CardConnect && validJob(turn) { |
| + c.Turn = turn |
| + } |
| + return c |
| +} |
| + |
| // validJob reports whether id is 1 to 128 of [A-Za-z0-9_-]. |
| func validJob(id string) bool { |
| if id == "" || len(id) > 128 { |
diff --git a/internal/equipo/equipotest/chat.go b/internal/equipo/equipotest/chat.go| index 2a9fbbd..2fc1a3b 100644 |
| --- a/internal/equipo/equipotest/chat.go |
| +++ b/internal/equipo/equipotest/chat.go |
| @@ -160,6 +160,19 @@ func (f *Fake) AskKind(kind, question string, options []equipo.Option, links []e |
| return c.ID |
| } |
| +// RaiseConnect puts an open connect card in the conversation, raised in |
| +// turn (its event carries the turn's id, as Equipo's does), with one link, |
| +// labelled Connect, to link. It returns the card's id. |
| +func (f *Fake) RaiseConnect(turn, link string) string { |
| + f.cmu.Lock() |
| + defer f.cmu.Unlock() |
| + c := &card{ID: f.next("c"), Revision: 1, Kind: equipo.CardConnect, State: "open", Question: "Connect your Oficina apps", |
| + Options: []equipo.Option{}, Links: []equipo.Link{{Label: "Connect", URL: link, Rel: equipo.LinkOficinaConnect}}} |
| + f.cards = append(f.cards, c) |
| + f.emit("card", "card", c.ID, c.Revision, turn, *c) |
| + return c.ID |
| +} |
| + |
| // AskJob puts a job card in the conversation, as Equipo does when the |
| // Assistant hands work on: working, getting started, linked to the job's |
| // page in Equipo. It returns the card's id. |
diff --git a/internal/look/static/app.css b/internal/look/static/app.css| index d231bfa..25aa5d5 100644 |
| --- a/internal/look/static/app.css |
| +++ b/internal/look/static/app.css |
| @@ -663,6 +663,25 @@ aside.ask-panel { position: absolute; top: 16px; right: 16px; bottom: 16px; widt |
| .job-cards > .msg.card { position: relative; margin-left: 22px; max-width: 340px; } |
| .job-cards > .msg.card::before { content: ""; position: absolute; left: -10px; top: -6px; bottom: 50%; width: 8px; |
| border-left: 2px solid var(--pill-edge); border-bottom: 2px solid var(--pill-edge); border-bottom-left-radius: 10px; } |
| +/* Connecting Oficina: a button at the end of the Assistant's reply that |
| + asked for it. Coloured glass, teal into violet (white on either is over |
| + 4.5:1, in both themes), the Oficina mark at its start, and under it a |
| + quiet line saying what it unlocks. Connected, a quiet line with a tick. |
| + On its own (no reply to end) it sits where a reply would. */ |
| +.connect-offer { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; margin-top: 12px; white-space: normal; } |
| +.connect-offer.loose { align-self: flex-start; margin-top: 0; padding-left: 4px; } |
| +.thread .connect-offer a.connect-btn { height: auto; min-height: 48px; display: inline-flex; align-items: center; gap: 10px; padding: 0 20px 0 8px; |
| + border: 0; border-radius: 999px; font-size: 15px; font-weight: 600; color: #FFFFFF; text-decoration: none; |
| + background: linear-gradient(120deg, rgba(10,122,116,.94), rgba(106,63,214,.94)); |
| + box-shadow: inset 0 0 0 1px rgba(255,255,255,.22), inset 0 1px 0 rgba(255,255,255,.35), 0 10px 22px -12px var(--edge-glow-b); |
| + transition: box-shadow .2s, filter .2s; } |
| +.thread .connect-offer a.connect-btn:hover { filter: brightness(1.08); |
| + box-shadow: inset 0 0 0 1px rgba(255,255,255,.4), inset 0 1px 0 rgba(255,255,255,.35), 0 12px 26px -10px var(--edge-glow-a); } |
| +.thread .connect-offer a.connect-btn:focus-visible { outline-offset: 3px; } |
| +.connect-mark { flex: none; width: 32px; height: 32px; } |
| +.connect-offer .connect-line { margin: 0 0 0 8px; color: var(--haze); font-size: 13.5px; line-height: 1.4; } |
| +.connect-offer .connect-done { margin: 0; display: inline-flex; align-items: center; gap: 6px; color: var(--haze); font-size: 14px; font-weight: 500; } |
| +.connect-done svg { width: 16px; height: 16px; fill: none; stroke: var(--focus); stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; } |
| .ask-empty, .ask-problem, .ask-notice { margin: 0; padding: 0 20px 12px; color: var(--haze); font-size: 14px; } |
| .ask-foot { display: flex; align-items: center; gap: 10px; padding: 0 16px; } |
| .ask-status { flex: 1; margin: 0; min-height: 1.5em; color: var(--haze); font-size: 14px; } |
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js| index 15457e5..ba5b1f9 100644 |
| --- a/internal/look/static/ask.js |
| +++ b/internal/look/static/ask.js |
| @@ -92,12 +92,14 @@ function start(root) { |
| const li = document.createElement("li"); |
| li.className = "msg " + (m.role === "person" ? "person" : "coordinator"); |
| li.dataset.id = m.id; |
| + if (m.role !== "person" && m.turn_id) li.dataset.turn = m.turn_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) || 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); |
| + if (m.role !== "person") reoffer(); |
| scroll(); |
| } |
| @@ -140,7 +142,8 @@ function start(root) { |
| li.className = "msg coordinator draft"; |
| li.dataset.draft = d.turn_id; |
| li.setAttribute("aria-busy", "true"); |
| - thread.append(li); |
| + // Above the turn's connect button, should it already show. |
| + thread.insertBefore(li, thread.querySelector(`:scope > li.connect-offer[data-turn="${CSS.escape(d.turn_id)}"]`)); |
| } |
| write(li, li.querySelector(":scope > .think-line") || li, d); |
| } |
| @@ -293,6 +296,10 @@ function start(root) { |
| const jobCards = new Map(); |
| function addCard(c) { |
| if (!c || !c.id) return; |
| + if (c.kind === "connect" && (offerHref(c) || offers.has(c.id) || item("data-card", c.id)?.matches(".connect-offer"))) { |
| + offer(c); |
| + return; |
| + } |
| const old = item("data-card", c.id); |
| if (!kinds.includes(c.kind) || (c.state !== "open" && !c.result)) { |
| if (old) old.remove(); |
| @@ -372,6 +379,116 @@ function start(root) { |
| scroll(); |
| } |
| + // An open connect card is a button, as the server draws it (the |
| + // ask-offer template): the Oficina mark and "Connect Oficina", going to |
| + // Base's own connect page in this tab, and a line saying what connecting |
| + // unlocks. It ends the Assistant's newest reply. One that comes while |
| + // the reply of the turn that raised it is on its way waits for that |
| + // reply to land (reoffer); if no words come for OFFER_WAIT, it shows on |
| + // its own below the thinking bubble, and moves into the reply once it |
| + // lands. Connected, it becomes a quiet line where it is; a fresh read no |
| + // longer draws it. |
| + const OFFER_WAIT = 3000; |
| + const offers = new Map(); // card id: the open connect card, drawn or waiting |
| + const waiting = new Map(); // card id: its timer, while it waits for its reply |
| + function offerHref(c) { |
| + for (const l of c.links || []) { |
| + const safe = l && l.rel === "oficina_connect" && typeof l.url === "string" && webURL(l.url); |
| + if (safe && sameTab(safe)) return safe; |
| + } |
| + return ""; |
| + } |
| + function offer(c) { |
| + const old = item("data-card", c.id), known = offers.get(c.id); |
| + if ((known && known.revision > c.revision) || (old && Number(old.dataset.revision) > c.revision)) return; |
| + if (c.state !== "open") { |
| + offers.delete(c.id); |
| + clearTimeout(waiting.get(c.id)); |
| + waiting.delete(c.id); |
| + if (!old) return; |
| + const done = document.createElement("p"); |
| + done.className = "connect-done"; |
| + const tick = icon(marks.done); |
| + tick.setAttribute("aria-hidden", "true"); |
| + done.append(tick, "Connected"); |
| + old.classList.add("connected"); |
| + old.dataset.revision = String(c.revision); |
| + old.replaceChildren(done); |
| + scroll(); |
| + return; |
| + } |
| + if (!offerHref(c)) return; |
| + const turnID = typeof c.turn_id === "string" && c.turn_id ? c.turn_id : known && known.turn_id; |
| + c = { ...c, turn_id: turnID || "" }; |
| + offers.set(c.id, c); |
| + if (onItsWay(c)) { |
| + if (!old) wait(c); |
| + return; |
| + } |
| + put(c); |
| + } |
| + // onItsWay is whether the reply of the turn that raised c is still to |
| + // land: the turn has not ended, or its words still stream. |
| + function onItsWay(c) { |
| + const id = c.turn_id; |
| + if (!id || thread.querySelector(`:scope > li.msg.coordinator[data-id][data-turn="${CSS.escape(id)}"]`)) return false; |
| + return !over.has(id) || bubbles.has(id) || !!item("data-draft", id); |
| + } |
| + function wait(c) { |
| + if (waiting.has(c.id)) return; |
| + waiting.set(c.id, setTimeout(() => { |
| + waiting.delete(c.id); |
| + const now = offers.get(c.id); |
| + if (!now) return; |
| + // Words are streaming: the reply is coming, wait for it. |
| + if (item("data-draft", now.turn_id) && onItsWay(now)) wait(now); |
| + else if (!item("data-card", now.id)) put(now); |
| + }, OFFER_WAIT)); |
| + } |
| + // reoffer puts every open connect card whose reply is no longer on its |
| + // way at the end of the newest reply (put). |
| + function reoffer() { |
| + for (const c of offers.values()) { |
| + if (onItsWay(c)) continue; |
| + clearTimeout(waiting.get(c.id)); |
| + waiting.delete(c.id); |
| + put(c); |
| + } |
| + } |
| + // put draws open connect card c at the end of the Assistant's newest |
| + // reply, or on its own at the end of the conversation (below the |
| + // thinking bubble) when there is none, or its reply is still to land. |
| + function put(c) { |
| + const old = item("data-card", c.id); |
| + const replies = thread.querySelectorAll(":scope > li.msg.coordinator[data-id]"); |
| + const reply = onItsWay(c) ? null : replies[replies.length - 1]; |
| + if (old && reply && old.tagName === "DIV") { |
| + if (old.parentElement !== reply || reply.lastElementChild !== old) reply.append(old); |
| + old.dataset.revision = String(c.revision); |
| + scroll(); |
| + return; |
| + } |
| + const focused = old && old.contains(document.activeElement); |
| + const el = document.createElement(reply ? "div" : "li"); |
| + el.className = reply ? "connect-offer" : "connect-offer loose"; |
| + el.dataset.card = c.id; |
| + el.dataset.revision = String(c.revision); |
| + if (c.turn_id) el.dataset.turn = c.turn_id; |
| + const a = document.createElement("a"); |
| + a.className = "btn connect-btn"; |
| + a.href = offerHref(c); |
| + const mark = document.createElement("img"); |
| + mark.className = "connect-mark"; |
| + mark.src = thread.dataset.mark || ""; |
| + mark.alt = ""; |
| + a.append(mark, "Connect Oficina"); |
| + el.append(a, para("connect-line", "Calendar, Docs, Sheets and more")); |
| + if (old) old.remove(); |
| + (reply || thread).append(el); |
| + if (focused) a.focus({ preventScroll: true }); |
| + scroll(); |
| + } |
| + |
| // A job's card that has ended, in its view, as the server draws it (the |
| // ask-jobcard template): the question and how it ended. |
| function endedCard(c) { |
| @@ -1136,6 +1253,7 @@ function start(root) { |
| delete b.dataset.draft; |
| b.removeAttribute("aria-busy"); |
| b.dataset.id = li.dataset.id; |
| + if (li.dataset.turn) b.dataset.turn = li.dataset.turn; |
| const line = b.querySelector(":scope > .think-line"); |
| const land = () => { unwiden(b); line.replaceChildren(...li.childNodes); }; |
| const unwrap = () => { if (line.isConnected) line.replaceWith(...line.childNodes); }; |
| @@ -1235,6 +1353,8 @@ function start(root) { |
| return; |
| } |
| if (t) unthink(t); |
| + // A turn that ended without its reply lets its connect button show. |
| + if (t) reoffer(); |
| if (t && turn && t.id !== turn.id) return; |
| turn = null; |
| paint(); |
| @@ -1313,6 +1433,9 @@ function start(root) { |
| thread.replaceChildren(); |
| bubbles.clear(); |
| jobCards.clear(); |
| + for (const timer of waiting.values()) clearTimeout(timer); |
| + waiting.clear(); |
| + offers.clear(); |
| glances.clear(); |
| waits.clear(); |
| pinned = true; |
diff --git a/internal/web/ask.go b/internal/web/ask.go| index 2a2d35d..6540eaf 100644 |
| --- a/internal/web/ask.go |
| +++ b/internal/web/ask.go |
| @@ -191,6 +191,75 @@ type askView struct { |
| // base is Base's own origin: the only place a connect-Oficina link may |
| // go and keep its rel. |
| base string |
| + // offerAt is the newest reply of the Assistant's, which ends with the |
| + // open connect cards' buttons (OffersAt); "" when there is none. |
| + offerAt string |
| +} |
| + |
| +// connectOffer is an open connect card as the chat draws it: a button, |
| +// going to Base's own connect page, at the end of the Assistant's newest |
| +// reply. The snapshot does not say which turn raised a card, so a fresh |
| +// read puts it after the newest reply; the panel's script does the same, |
| +// and holds one that comes while its turn's reply streams until it lands |
| +// (ask.js, offer). |
| +type connectOffer struct { |
| + ID string |
| + Revision int64 |
| + URL string |
| +} |
| + |
| +// offerOf is card c as a button: an open connect card with a link to |
| +// Base's own connect page (the rel CleanAt keeps for that alone). |
| +func offerOf(c equipo.Card) (connectOffer, bool) { |
| + if c.Kind != equipo.CardConnect || !c.Open() { |
| + return connectOffer{}, false |
| + } |
| + for _, l := range c.Links { |
| + if l.Rel == equipo.LinkOficinaConnect { |
| + return connectOffer{ID: c.ID, Revision: c.Revision, URL: l.URL}, true |
| + } |
| + } |
| + return connectOffer{}, false |
| +} |
| + |
| +func (v askView) offers() []connectOffer { |
| + var out []connectOffer |
| + for _, c := range v.Cards { |
| + if o, ok := offerOf(c); ok { |
| + out = append(out, o) |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// OffersAt are the buttons that end message id: the open connect cards', |
| +// when id is the Assistant's newest reply. |
| +func (v askView) OffersAt(id string) []connectOffer { |
| + if id == "" || id != v.offerAt { |
| + return nil |
| + } |
| + return v.offers() |
| +} |
| + |
| +// LooseOffers are the buttons drawn on their own, at the end of the |
| +// conversation: with no reply of the Assistant's to end, all of them. |
| +func (v askView) LooseOffers() []connectOffer { |
| + if v.offerAt != "" { |
| + return nil |
| + } |
| + return v.offers() |
| +} |
| + |
| +// LooseCards are the cards drawn as cards of their own: every card but |
| +// the connect cards drawn as buttons. |
| +func (v askView) LooseCards() []equipo.Card { |
| + var out []equipo.Card |
| + for _, c := range v.Cards { |
| + if _, ok := offerOf(c); !ok { |
| + out = append(out, c) |
| + } |
| + } |
| + return out |
| } |
| // jobView is a job card as the chat draws it, a pill, with the cards that |
| @@ -319,6 +388,9 @@ func (v *askView) fill(s equipo.Snapshot) { |
| p, ok := c.CleanAt(v.base) |
| switch { |
| case !ok || !p.Shown(): |
| + case p.Kind == equipo.CardConnect && !p.Open(): |
| + // Connected: a fresh read no longer offers it (the chat says |
| + // so once, oficinaNotes). |
| case p.Kind == equipo.CardJob: |
| if _, dup := jobs[p.ID]; !dup { |
| jobs[p.ID] = len(v.Jobs) |
| @@ -335,6 +407,12 @@ func (v *askView) fill(s equipo.Snapshot) { |
| v.Cards = append(v.Cards, c) |
| } |
| } |
| + for i := len(s.Messages) - 1; i >= 0; i-- { |
| + if s.Messages[i].Role != "person" { |
| + v.offerAt = s.Messages[i].ID |
| + break |
| + } |
| + } |
| for i := len(s.Turns) - 1; i >= 0; i-- { |
| if equipo.InFlight(s.Turns[i].State) && s.Turns[i].Kind != equipo.TurnTeamUpdate { |
| t := s.Turns[i] |
diff --git a/internal/web/ask_relay.go b/internal/web/ask_relay.go| index 72125c6..b6dab9e 100644 |
| --- a/internal/web/ask_relay.go |
| +++ b/internal/web/ask_relay.go |
| @@ -372,8 +372,9 @@ func (e sseEvent) frame(seen *turnsSeen) string { |
| return "" |
| } |
| var env struct { |
| - ID string `json:"id"` |
| - Data json.RawMessage `json:"data"` |
| + ID string `json:"id"` |
| + TurnID string `json:"turn_id"` |
| + Data json.RawMessage `json:"data"` |
| } |
| if err := json.Unmarshal([]byte(strings.Join(e.data, "\n")), &env); err != nil { |
| return "" |
| @@ -385,6 +386,10 @@ func (e sseEvent) frame(seen *turnsSeen) string { |
| } else { |
| data, ok = project(e.typ, env.Data, seen.base) |
| } |
| + if c, card := data.(equipo.Card); card { |
| + // A connect card waits for the reply of the turn that raised it. |
| + data = c.WithTurn(env.TurnID) |
| + } |
| if !ok { |
| return "" |
| } |
diff --git a/internal/web/relay_internal_test.go b/internal/web/relay_internal_test.go| index 48f5077..48fbbff 100644 |
| --- a/internal/web/relay_internal_test.go |
| +++ b/internal/web/relay_internal_test.go |
| @@ -249,3 +249,28 @@ func TestRelayGlanceFromSnapshot(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// A connect card passes with the turn that raised it, read from the |
| +// event's own turn_id (never from the card's data), so the panel can hold |
| +// it for that turn's reply. A turn id that is not one is dropped, and no |
| +// other kind of card carries one. |
| +func TestRelayConnectCardKeepsItsTurn(t *testing.T) { |
| + connect := func(cursor, turn string) string { |
| + return `{"cursor":` + cursor + `,"type":"card","resource":"card","id":"c_8","revision":1,"turn_id":"` + turn + `","data":{"id":"c_8","revision":1,"kind":"connect","state":"open","turn_id":"t_data","question":"Connect your Oficina apps","options":[],"links":[{"label":"Connect","url":"https://base.example.test/ask/oficina/connect","rel":"oficina_connect"}],"result":""}}` |
| + } |
| + outcome := `{"cursor":72,"type":"card","resource":"card","id":"c_9","revision":1,"turn_id":"t_7","data":{"id":"c_9","revision":1,"kind":"outcome","state":"open","question":"Go ahead?","options":[],"links":[],"result":""}}` |
| + seen := turnsSeen{base: "https://base.example.test"} |
| + out := relaySeeded(t, seen, strings.NewReader(frame("70", "card", connect("70", "t_7"))+frame("71", "card", connect("71", `t\"><x`))+frame("72", "card", outcome)), time.Second, func() {}) |
| + for _, want := range []string{ |
| + `id: 70` + "\nevent: card\n" + `data: {"type":"card","data":{"id":"c_8","revision":1,"kind":"connect","state":"open","question":"Connect your Oficina apps","options":[],"links":[{"label":"Connect","url":"https://base.example.test/ask/oficina/connect","rel":"oficina_connect"}],"result":"","turn_id":"t_7"}}`, |
| + `id: 71` + "\nevent: card\n" + `data: {"type":"card","data":{"id":"c_8","revision":1,"kind":"connect","state":"open","question":"Connect your Oficina apps","options":[],"links":[{"label":"Connect","url":"https://base.example.test/ask/oficina/connect","rel":"oficina_connect"}],"result":""}}`, |
| + `data: {"type":"card","data":{"id":"c_9","revision":1,"kind":"outcome","state":"open","question":"Go ahead?","options":[],"links":[],"result":""}}`, |
| + } { |
| + if !strings.Contains(out, want) { |
| + t.Errorf("relay lacks %s:\n%s", want, out) |
| + } |
| + } |
| + if strings.Contains(out, "t_data") { |
| + t.Errorf("the card's own turn_id passed:\n%s", out) |
| + } |
| +} |
diff --git a/internal/web/templates/partials/chat.html b/internal/web/templates/partials/chat.html| index 82407c3..c7a2e3c 100644 |
| --- a/internal/web/templates/partials/chat.html |
| +++ b/internal/web/templates/partials/chat.html |
| @@ -8,6 +8,8 @@ |
| {{- range .Links}}<p>{{if eq .Rel "oficina_connect"}}<a class="card-link" href="{{.URL}}">{{.Label}}</a>{{else}}<a class="card-link" href="{{.URL}}" target="_blank" rel="noopener">{{.Label}}</a>{{end}}</p>{{end}} |
| {{- if and .Open .Options}}<form class="choices" method="post" action="/ask/cards/{{.ID}}/answer"><input type="hidden" name="revision" value="{{.Revision}}"><input type="hidden" name="key" value="{{newKey}}">{{range .Options}}<button class="btn ghost" type="submit" name="choice" value="{{.Value}}">{{.Label}}</button>{{end}}</form>{{end}} |
| {{- with .Result}}<p class="card-state">{{.}}</p>{{end}}</li>{{end}} |
| +{{/* An open connect card, drawn as a button (ask.go, connectOffer): the Oficina mark and "Connect Oficina", going to Base's own connect page in this tab, and a line saying what connecting unlocks. It ends the Assistant's newest reply, or with none stands on its own. The panel's script draws the same (ask.js, offer). */}} |
| +{{define "ask-offer"}}<a class="btn connect-btn" href="{{.URL}}"><img class="connect-mark" src="{{asset "static/oficina/equipo.svg"}}" alt="">Connect Oficina</a><p class="connect-line">Calendar, Docs, Sheets and more</p>{{end}} |
| {{/* A job, a pill: its state's mark, the title and its status, and once there is something to see (ready, done) the link to it. The summary opens the job view (without scripts, in place). The cards that belong to the job hang under it while they wait on the person; once ended they are listed in its view. The panel's script draws the same (ask.js, addJob). */}} |
| {{define "ask-job"}}<li class="job" data-job="{{.ID}}" data-revision="{{.Revision}}" data-state="{{.State}}"> |
| <div class="pill"><details class="job-toggle"><summary class="pill-main" aria-label="{{.Question}}, {{.Result}}"><span class="state-mark" aria-hidden="true">{{template "ask-mark" .State}}</span><span class="pill-title">{{.Question}}</span><span class="pill-stage">{{.Result}}</span></summary>{{template "ask-jobview" .}}</details> |
| @@ -39,10 +41,11 @@ |
| <p>Ask Base isn't answering. Try again in a minute.</p> |
| </div> |
| <div class="ask-chat" data-when="ready norunner oldrunner"{{if not (.Shows "ready norunner oldrunner")}} hidden{{end}}> |
| -<div class="log"><ol class="thread" role="log" aria-label="Conversation"{{with .Zone}} data-zone="{{.}}"{{end}}> |
| -{{range .Messages}}{{if eq .Role "person"}}<li class="msg person" data-id="{{.ID}}">{{.Text}}</li>{{else}}<li class="msg coordinator" data-id="{{.ID}}">{{$.Rich .Text}}</li>{{end}} |
| +<div class="log"><ol class="thread" role="log" aria-label="Conversation"{{with .Zone}} data-zone="{{.}}"{{end}} data-mark="{{asset "static/oficina/equipo.svg"}}"> |
| +{{range .Messages}}{{if eq .Role "person"}}<li class="msg person" data-id="{{.ID}}">{{.Text}}</li>{{else}}<li class="msg coordinator" data-id="{{.ID}}">{{$.Rich .Text}}{{range $.OffersAt .ID}}<div class="connect-offer" data-card="{{.ID}}" data-revision="{{.Revision}}">{{template "ask-offer" .}}</div>{{end}}</li>{{end}} |
| {{end}}{{range .Jobs}}{{template "ask-job" .}} |
| -{{end}}{{range .Cards}}{{template "ask-card" .}} |
| +{{end}}{{range .LooseCards}}{{template "ask-card" .}} |
| +{{end}}{{range .LooseOffers}}<li class="connect-offer loose" data-card="{{.ID}}" data-revision="{{.Revision}}">{{template "ask-offer" .}}</li> |
| {{end}}</ol> |
| <button class="jump" type="button" hidden>Jump to latest</button></div> |
| <p class="ask-empty" data-empty{{if or .Messages .Jobs .Cards .Unread}} hidden{{end}}>Ask about your work, or ask Base to get something done.</p> |
diff --git a/internal/webtest/connect_offer_test.go b/internal/webtest/connect_offer_test.go| new file mode 100644 |
| index 0000000..710e676 |
| --- /dev/null |
| +++ b/internal/webtest/connect_offer_test.go |
| @@ -0,0 +1,85 @@ |
| +package webtest |
| + |
| +import ( |
| + "encoding/json" |
| + "regexp" |
| + "strings" |
| + "testing" |
| + |
| + "amadan.net/carlos/base/internal/equipo" |
| +) |
| + |
| +// replies are the Assistant's lines on a page, each whole, oldest first. |
| +func replies(page string) []string { |
| + return regexp.MustCompile(`(?s)<li class="msg coordinator" data-id="[^"]+">.*?</li>`).FindAllString(page, -1) |
| +} |
| + |
| +// An open connect card is a button at the end of the Assistant's newest |
| +// reply, after its words, never a bubble of its own above or below it: |
| +// "Connect Oficina" with the Oficina mark, going to Base's own connect |
| +// page in the same tab, and a line saying what it unlocks. |
| +func TestAskConnectOfferFollowsItsReply(t *testing.T) { |
| + a, f := newAskApp(t) |
| + a.connectEquipo(f) |
| + f.Say("Hello. What can I do?") |
| + id := f.RaiseConnect("t_1", testOrigin+"/ask/oficina/connect") |
| + f.Say("Connect your Oficina apps and I'll show you this week.") |
| + page := a.get("/ask").Body |
| + lines := replies(page) |
| + if len(lines) != 2 { |
| + t.Fatalf("%d replies:\n%s", len(lines), page) |
| + } |
| + if strings.Contains(lines[0], "connect-offer") { |
| + t.Errorf("the older reply has the offer:\n%s", lines[0]) |
| + } |
| + want := `Connect your Oficina apps and I'll show you this week.<div class="connect-offer" data-card="` + id + `" data-revision="1">` + |
| + `<a class="btn connect-btn" href="` + testOrigin + `/ask/oficina/connect"><img class="connect-mark" src="/static/oficina/equipo.` |
| + if !strings.Contains(lines[1], want) { |
| + t.Errorf("the reply lacks the offer after its words:\n%s", lines[1]) |
| + } |
| + if !strings.HasSuffix(lines[1], `alt="">Connect Oficina</a><p class="connect-line">Calendar, Docs, Sheets and more</p></div></li>`) { |
| + t.Errorf("the offer is not the end of the reply:\n%s", lines[1]) |
| + } |
| + if strings.Contains(page, "card-connect") || strings.Contains(page, "Connect your Oficina apps</p>") { |
| + t.Errorf("the card was drawn on its own as well:\n%s", page) |
| + } |
| + // The panel's script gets the card as it is, to place the same way. |
| + var st struct{ Cards []equipo.Card } |
| + if r := a.get("/ask/state"); json.Unmarshal([]byte(r.Body), &st) != nil || len(st.Cards) != 1 || st.Cards[0].ID != id || |
| + st.Cards[0].Kind != "connect" || len(st.Cards[0].Links) != 1 || st.Cards[0].Links[0].Rel != "oficina_connect" { |
| + t.Fatalf("state: %s", r.Body) |
| + } |
| + |
| + // A later reply takes it: the newest reply always ends with it. |
| + f.Say("Sure, anything else?") |
| + lines = replies(a.get("/ask").Body) |
| + if len(lines) != 3 || strings.Contains(lines[1], "connect-offer") || !strings.Contains(lines[2], `<div class="connect-offer" data-card="`+id+`"`) { |
| + t.Errorf("after another reply:\n%s", strings.Join(lines, "\n")) |
| + } |
| + |
| + // Connected: on a fresh read it is gone (the chat says so once). |
| + f.SetCard(id, "resolved", "") |
| + if page := a.get("/ask").Body; strings.Contains(page, "connect-offer") || strings.Contains(page, id) { |
| + t.Errorf("a connected card is still offered:\n%s", page) |
| + } |
| +} |
| + |
| +// With no reply of the Assistant's to go in, the offer is on its own at |
| +// the end of the conversation; a connect card whose link is not Base's |
| +// own connect page is a card like any other, opening in a new tab. |
| +func TestAskConnectOfferWithoutAReply(t *testing.T) { |
| + a, f := newAskApp(t) |
| + a.connectEquipo(f) |
| + id := f.RaiseConnect("t_1", testOrigin+"/ask/oficina/connect") |
| + page := a.get("/ask").Body |
| + if !regexp.MustCompile(`<li class="connect-offer loose" data-card="` + id + `" data-revision="1"><a class="btn connect-btn" href="` + |
| + regexp.QuoteMeta(testOrigin) + `/ask/oficina/connect"><img [^>]+>Connect Oficina</a><p class="connect-line">Calendar, Docs, Sheets and more</p></li>\s*</ol>`).MatchString(page) { |
| + t.Errorf("no loose offer at the end:\n%s", page) |
| + } |
| + other := f.RaiseConnect("t_2", "https://evil.example/ask/oficina/connect") |
| + page = a.get("/ask").Body |
| + card := cardItem(t, page, other) |
| + if !strings.Contains(card, `href="https://evil.example/ask/oficina/connect" target="_blank" rel="noopener">Connect</a>`) || strings.Contains(card, "connect-btn") { |
| + t.Errorf("a lookalike connect card got the button:\n%s", card) |
| + } |
| +} |