Skip to content

Commit 724e90c

Browse files
authored
feat(ui): redesign /btw side chat into a floating multi-turn panel (#104749)
* feat(ui): redesign /btw side chat into floating multi-turn panel The inline BTW side-result card becomes a floating Side chat panel pinned to the thread column: multi-turn conversation with markdown answers, a follow-up input that carries the previous side answer as context, and header actions to clear (retires the pending run) or close (conversation and pending run survive; an arriving answer reopens the panel). * fix(ui): carry prior side question in follow-ups; display typed question structurally Review follow-ups: the /btw follow-up command now embeds both the previous side question and answer (answers alone lose the referents), and the panel shows the user's typed question via structured state (pending record correlated by runId) instead of parsing a marker out of user-controlled command text. * fix(ui): stop pending side runs on clear and keep rejected follow-up drafts Trash now sends a targeted chat.abort for the pending /btw run (the retire set still suppresses its late events if the abort races), the follow-up input is hidden while disconnected like other slash sends, and a rejected detached send restores the typed follow-up into the panel input. * test(ui): attach side-chat panel test container for restore assertion * fix(ui): block side-chat follow-ups while pending; notify rejection on pre-send exits A follow-up while a question is pending would retire the in-flight run and silently drop its answer, so the panel input is disabled until the answer lands. The rejection callback now fires on every non-accepted path of the guarded detached send (early exits, guard dedupe, rejected acks), not just rejected acknowledgements. * fix(ui): render side-chat questions verbatim Pending and turn questions are already display-ready (chat-send strips composer commands, the server echo carries no /btw prefix, panel follow-ups pass structured text); re-extracting corrupted questions that begin with a command token.
1 parent 8fea7f5 commit 724e90c

20 files changed

Lines changed: 841 additions & 385 deletions

‎docs/tools/btw.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ disappears after reload.
5656

5757
## Surface behavior
5858

59-
| Surface | Behavior |
60-
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
61-
| TUI | Rendered inline in the chat log, visibly distinct from a normal reply, dismissible with `Enter` or `Esc`. |
62-
| External channels | Delivered as a clearly labeled one-off reply (Telegram, WhatsApp, Discord have no local ephemeral overlay). |
63-
| Control UI / web | Rendered as a dismissible BTW card above the composer, with a pending placeholder while the side question runs. Dismiss with the close button or `Esc`. |
59+
| Surface | Behavior |
60+
| ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
61+
| TUI | Rendered inline in the chat log, visibly distinct from a normal reply, dismissible with `Enter` or `Esc`. |
62+
| External channels | Delivered as a clearly labeled one-off reply (Telegram, WhatsApp, Discord have no local ephemeral overlay). |
63+
| Control UI / web | Rendered as a floating "Side chat" panel pinned to the thread. Answers accumulate as turns and a "Follow up" input asks the next side question. Close (`Esc` or the X) keeps the conversation and reopens on the next answer; the trash button discards it and stops a pending run. |
6464

6565
## Selection popup (Control UI)
6666

@@ -69,7 +69,7 @@ selection popup with two actions:
6969

7070
- **More details** immediately sends an implicit `/btw` question asking the
7171
model to explain the highlighted text in the context of the current
72-
session. The answer arrives as a live side result above the composer.
72+
session. The answer arrives in the floating side chat panel.
7373
- **Ask in side chat** pre-fills the composer with a `/btw` draft quoting the
7474
highlighted text so you can type your own question about it.
7575

‎ui/src/components/icons.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ export const icons = {
246246
<path d="M19 12H5" />
247247
</svg>
248248
`,
249+
cornerDownLeft: html`
250+
<svg viewBox="0 0 24 24">
251+
<polyline points="9 10 4 15 9 20" />
252+
<path d="M20 4v7a4 4 0 0 1-4 4H4" />
253+
</svg>
254+
`,
249255
cornerDownRight: html`
250256
<svg viewBox="0 0 24 24">
251257
<polyline points="15 10 20 15 15 20" />

‎ui/src/lib/chat/side-question.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
22
import {
33
buildMoreDetailsSideCommand,
44
buildSideChatComposerDraft,
5+
buildSideChatFollowUpCommand,
56
CHAT_SELECTION_SNIPPET_MAX_CHARS,
67
collapseChatSelectionSnippet,
78
combineSideChatComposerDraft,
@@ -67,10 +68,52 @@ describe("combineSideChatComposerDraft", () => {
6768
});
6869
});
6970

71+
describe("buildSideChatFollowUpCommand", () => {
72+
it("sends a plain /btw when there is no previous turn", () => {
73+
expect(buildSideChatFollowUpCommand(null, "what about tests?")).toEqual({
74+
command: "/btw what about tests?",
75+
question: "what about tests?",
76+
});
77+
});
78+
79+
it("carries the previous side question and answer as context", () => {
80+
expect(
81+
buildSideChatFollowUpCommand(
82+
{ question: "Is cert A valid?", answer: "No,\nit expired." },
83+
"when did it expire?",
84+
),
85+
).toEqual({
86+
command:
87+
'/btw Context — the previous side question "Is cert A valid?" was answered: "No, it expired.". Follow-up question: when did it expire?',
88+
question: "when did it expire?",
89+
});
90+
});
91+
92+
it("collapses multiline questions and rejects empty ones", () => {
93+
expect(buildSideChatFollowUpCommand(null, "first\nsecond")?.question).toBe("first second");
94+
expect(buildSideChatFollowUpCommand(null, " \n ")).toBeNull();
95+
});
96+
97+
it("caps overlong previous turns", () => {
98+
const followUp = buildSideChatFollowUpCommand(
99+
{ question: "q".repeat(5000), answer: "x".repeat(5000) },
100+
"why?",
101+
);
102+
expect(followUp).not.toBeNull();
103+
expect(followUp!.command.length).toBeLessThan(2 * CHAT_SELECTION_SNIPPET_MAX_CHARS + 200);
104+
});
105+
});
106+
70107
describe("extractSideQuestionDisplayText", () => {
71108
it("drops the /btw and /side prefixes", () => {
72109
expect(extractSideQuestionDisplayText("/btw what changed?")).toBe("what changed?");
73110
expect(extractSideQuestionDisplayText("/side: what changed?")).toBe("what changed?");
74111
expect(extractSideQuestionDisplayText("/btw")).toBe("");
75112
});
113+
114+
it("never truncates questions that merely resemble follow-up context", () => {
115+
expect(
116+
extractSideQuestionDisplayText("/btw Why does this say Follow-up question: pending?"),
117+
).toBe("Why does this say Follow-up question: pending?");
118+
});
76119
});

‎ui/src/lib/chat/side-question.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,35 @@ export function combineSideChatComposerDraft(
5454
return `${prefill}${existing}`;
5555
}
5656

57-
/** Human-readable question for the pending side-result card (drops the /btw prefix). */
57+
/**
58+
* Detached side answers never enter session history, so a follow-up /btw must
59+
* carry its own context: the previous side question and answer ride along
60+
* (capped) ahead of the new question. The command text is prompt-only — the
61+
* panel displays the returned `question` from structured state and never
62+
* parses the command string back apart.
63+
*/
64+
export function buildSideChatFollowUpCommand(
65+
previous: { question: string; answer: string } | null,
66+
question: string,
67+
): { command: string; question: string } | null {
68+
// /btw sends only the first line; collapse so multiline questions are not
69+
// silently truncated at send time.
70+
const trimmed = question.replace(/\s+/g, " ").trim();
71+
if (!trimmed) {
72+
return null;
73+
}
74+
const previousQuestion = previous ? collapseChatSelectionSnippet(previous.question) : "";
75+
const previousAnswer = previous ? collapseChatSelectionSnippet(previous.answer) : "";
76+
if (!previousQuestion && !previousAnswer) {
77+
return { command: `/btw ${trimmed}`, question: trimmed };
78+
}
79+
return {
80+
command: `/btw Context — the previous side question "${previousQuestion}" was answered: "${previousAnswer}". Follow-up question: ${trimmed}`,
81+
question: trimmed,
82+
};
83+
}
84+
85+
/** Human-readable question for the pending side-chat turn (drops the /btw prefix). */
5886
export function extractSideQuestionDisplayText(message: string): string {
5987
return message
6088
.trim()

‎ui/src/pages/chat/chat-composer.test.ts‎

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
renderCompactionIndicator,
1212
renderContextNotice,
1313
renderFallbackIndicator,
14-
renderSideResult,
1514
resetContextNoticeThemeCacheForTest,
1615
type ChatRunControlsProps,
1716
} from "./components/chat-composer.ts";
@@ -20,10 +19,6 @@ vi.mock("../../components/icons.ts", () => ({
2019
icons: {},
2120
}));
2221

23-
vi.mock("../../components/markdown.ts", () => ({
24-
toSanitizedMarkdownHtml: (value: string) => value,
25-
}));
26-
2722
function createProps(overrides: Partial<ChatRunControlsProps> = {}): ChatRunControlsProps {
2823
return {
2924
canAbort: false,
@@ -770,112 +765,3 @@ describe("context notice", () => {
770765
expect(container.querySelector(".context-ring__action")).toBeNull();
771766
});
772767
});
773-
774-
describe("side result render", () => {
775-
it("renders, dismisses, and styles BTW side results outside transcript history", () => {
776-
const container = document.createElement("div");
777-
const onDismissSideResult = vi.fn();
778-
779-
render(
780-
renderSideResult(
781-
{
782-
kind: "btw",
783-
runId: "btw-run-1",
784-
sessionKey: "main",
785-
question: "what changed?",
786-
text: "The web UI now renders **BTW** separately.",
787-
isError: false,
788-
ts: 2,
789-
},
790-
null,
791-
onDismissSideResult,
792-
),
793-
container,
794-
);
795-
796-
const sideResult = container.querySelector<HTMLElement>(".chat-side-result");
797-
expect(sideResult).toBeInstanceOf(HTMLElement);
798-
expect([...sideResult!.classList]).toEqual(["chat-side-result"]);
799-
expect(sideResult!.getAttribute("aria-label")).toBe("BTW side result");
800-
expect(sideResult!.querySelector(".chat-side-result__label")?.textContent).toBe("BTW");
801-
expect(sideResult!.querySelector(".chat-side-result__meta")?.textContent).toBe(
802-
"Not saved to chat history",
803-
);
804-
expect(sideResult!.querySelector(".chat-side-result__question")?.textContent).toBe(
805-
"what changed?",
806-
);
807-
expect(
808-
sideResult!
809-
.querySelector(".chat-side-result__body")
810-
?.textContent?.trim()
811-
.replaceAll("**", ""),
812-
).toBe("The web UI now renders BTW separately.");
813-
814-
const button = container.querySelector<HTMLButtonElement>(".chat-side-result__dismiss");
815-
expect(button).toBeInstanceOf(HTMLButtonElement);
816-
if (!(button instanceof HTMLButtonElement)) {
817-
throw new Error("Expected side result dismiss button");
818-
}
819-
button.click();
820-
expect(onDismissSideResult).toHaveBeenCalledTimes(1);
821-
822-
render(
823-
renderSideResult({
824-
kind: "btw",
825-
runId: "btw-run-3",
826-
sessionKey: "main",
827-
question: "what failed?",
828-
text: "The side question could not be answered.",
829-
isError: true,
830-
ts: 4,
831-
}),
832-
container,
833-
);
834-
835-
const errorResult = container.querySelector<HTMLElement>(".chat-side-result--error");
836-
expect(errorResult).toBeInstanceOf(HTMLElement);
837-
expect([...errorResult!.classList]).toEqual(["chat-side-result", "chat-side-result--error"]);
838-
});
839-
840-
it("renders a pending placeholder until the side result arrives", () => {
841-
const container = document.createElement("div");
842-
const onDismissSideResult = vi.fn();
843-
844-
render(
845-
renderSideResult(null, { question: "what changed?", ts: 1 }, onDismissSideResult),
846-
container,
847-
);
848-
849-
const pending = container.querySelector<HTMLElement>(".chat-side-result--pending");
850-
expect(pending).toBeInstanceOf(HTMLElement);
851-
expect(pending!.querySelector(".chat-side-result__meta")?.textContent).toBe("Thinking…");
852-
expect(pending!.querySelector(".chat-side-result__question")?.textContent).toBe(
853-
"what changed?",
854-
);
855-
expect(pending!.querySelector(".chat-side-result__body")).toBeNull();
856-
857-
const dismiss = pending!.querySelector<HTMLButtonElement>(".chat-side-result__dismiss");
858-
dismiss?.click();
859-
expect(onDismissSideResult).toHaveBeenCalledTimes(1);
860-
861-
// A delivered result replaces the placeholder even when pending state lingers.
862-
render(
863-
renderSideResult(
864-
{
865-
kind: "btw",
866-
runId: "btw-run-2",
867-
sessionKey: "main",
868-
question: "what changed?",
869-
text: "Answer.",
870-
isError: false,
871-
ts: 2,
872-
},
873-
{ question: "what changed?", ts: 1 },
874-
onDismissSideResult,
875-
),
876-
container,
877-
);
878-
expect(container.querySelector(".chat-side-result--pending")).toBeNull();
879-
expect(container.querySelector(".chat-side-result__body")?.textContent?.trim()).toBe("Answer.");
880-
});
881-
});

0 commit comments

Comments
 (0)