Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Address review: exchange attribution, fold under search, modal focus

AdityaUmale6ce573d3e0f7
diff --git a/src/components/ChatView.tsx b/src/components/ChatView.tsx
index cb73d115c4..58eb5005a9 100644
--- a/src/components/ChatView.tsx
+++ b/src/components/ChatView.tsx
@@ -336,7 +336,7 @@ const Bubble = memo(function Bubble({
const user = message.role === "user" && !peer;
const speaker = user ? otherSpeaker(message, speakerViewer ?? null) : null;
const ownBubbleStyle = speaker ? undefined : bubbleStyle;
- const [expanded, setExpanded] = useState(false);
+ const [expanded, setExpanded] = useState(false), [folded, setFolded] = useState(false);
const focusedSearch = focus?.messageId === message.id && Boolean(focus.matchText);
const [viewRaw, setViewRaw] = useState(false);
const speech = useSpeech();
@@ -390,6 +390,7 @@ const Bubble = memo(function Bubble({
const collapsible =
user && !webhookView && !expanded && (visibleText.length > USER_COLLAPSE_CHARS || visibleText.split("\n").length > USER_COLLAPSE_LINES);
useEffect(() => {
+ setFolded(false);
if (focusedSearch && (collapsible || peer)) setExpanded(true);
}, [focusedSearch, collapsible, Boolean(peer), focus?.nonce]);
@@ -402,8 +403,8 @@ const Bubble = memo(function Bubble({
}
// A long request from another bot is one line until opened.
- const peerLong = peer ? peerPreview(peer.body) : null;
- if (peer && peerLong && !expanded && !focusedSearch) return <div {...{ [MESSAGE_ROW]: "" }} className="group flex w-full flex-col items-start"><PeerLabel peer={peer} /><PeerCompactRow preview={peerLong} onOpen={() => setExpanded(true)} /></div>;
+ const peerLong = peer ? peerPreview(text) : null;
+ if (peer && peerLong && !expanded && (!focusedSearch || folded)) return <div {...{ [MESSAGE_ROW]: "" }} className="group flex w-full flex-col items-start"><PeerLabel peer={peer} /><PeerCompactRow preview={peerLong} onOpen={() => setExpanded(true)} /></div>;
const forks = user && versions ? versions : [message];
const versionIndex = forks.findIndex((v) => v.id === message.id);
const switchTo = (v: Message | undefined) => {
@@ -565,7 +566,7 @@ const Bubble = memo(function Bubble({
</span>
</div>
<ReactionChips threadId={threadId} message={message} bots={bots} align={user ? "end" : "start"} canReact={canReact} />
- {peer && expanded && peerLong && <PeerOpenedActions peer={peer} bots={bots} botId={botId} botName={botName} onFold={() => setExpanded(false)} />}
+ {peer && expanded && peerLong && <PeerOpenedActions peer={peer} bots={bots} botId={botId} botName={botName} onFold={() => { setExpanded(false); setFolded(true); }} />}
{forks.length > 1 && (
<div className="mt-1 flex items-center gap-0.5 pr-1 text-[12px] text-ink-secondary">
<button
@@ -685,7 +686,7 @@ const MessagesList = memo(function MessagesList({
return <NewMessagesDivider fading={unreadDividerFading} />;
};
return (
- <PeerMessagesContext value={messages}>
+ <PeerMessagesContext value={transcript}>
{items.map((item, i) => {
const previous = items[i - 1];
const prev = previous && (previous.kind === "message" ? previous.message : previous.messages.at(-1));
diff --git a/src/components/PeerExchange.test.ts b/src/components/PeerExchange.test.ts
index 6a222e1313..4636b6a840 100644
--- a/src/components/PeerExchange.test.ts
+++ b/src/components/PeerExchange.test.ts
@@ -38,6 +38,8 @@ describe("PeerExchangeView", () => {
onClose: () => {},
}));
expect(html).toContain('role="dialog"');
+ expect(html).toContain('tabindex="-1"');
+ expect(html).not.toContain("autofocus");
expect(html).toContain("Liaison and Helper");
expect(html).toContain('aria-label="Close"');
expect(html.indexOf("First request")).toBeLessThan(html.indexOf("Done with the first"));
diff --git a/src/components/PeerExchange.tsx b/src/components/PeerExchange.tsx
index 40adabd2e2..eab02ac7c9 100644
--- a/src/components/PeerExchange.tsx
+++ b/src/components/PeerExchange.tsx
@@ -1,8 +1,9 @@
// Another bot's long request reads as one line in the chat. It opens in place, and its back-and-forth with this bot opens beside the chat.
-import { createContext, use, useEffect, useMemo, useState } from "react";
+import { createContext, use, useMemo, useRef, useState } from "react";
import { createPortal } from "react-dom";
import { ChevronRight, MessagesSquare, X } from "lucide-react";
import { BotAvatar } from "./Avatar";
+import { useModalDialog } from "@/hooks/use-modal-dialog";
import { formatTime } from "@/lib/clock-time";
import { normalizeState } from "@/lib/mascot";
import { peerExchange, type ExchangeLine, type PeerLine } from "@/lib/peer-message";
@@ -78,13 +79,6 @@ export function PeerOpenedActions({ onFold, ...context }: PeerContext & { onFold
function PeerExchangePanel({ peer, bots, botId, botName, onClose }: PeerContext & { onClose: () => void }) {
const messages = use(PeerMessagesContext);
const lines = useMemo(() => peerExchange(messages, botId, peer), [messages, botId, peer.botId, peer.name]);
- useEffect(() => {
- const onKey = (event: KeyboardEvent) => {
- if (event.key === "Escape") onClose();
- };
- window.addEventListener("keydown", onKey);
- return () => window.removeEventListener("keydown", onKey);
- }, [onClose]);
return createPortal(
<PeerExchangeView lines={lines} peerName={peer.name} peerBot={authorOf(bots, peer)} botName={botName} self={bots.find((b) => b.id === botId)} onClose={onClose} />,
document.body,
@@ -100,12 +94,15 @@ export function PeerExchangeView({ lines, peerName, peerBot, botName, self, onCl
onClose: () => void;
}) {
const title = t("chat.peer.exchangeTitle", { peer: peerName, bot: botName });
+ // Keeps Tab inside the panel, closes on Escape and gives focus back to "View exchange".
+ const dialogRef = useRef<HTMLElement>(null);
+ useModalDialog(dialogRef, onClose);
return (
<div className="fixed inset-0 z-50 flex justify-end bg-black/40" onMouseDown={(event) => event.target === event.currentTarget && onClose()}>
- <aside role="dialog" aria-modal="true" aria-label={title} data-testid="peer-exchange" className="flex h-full w-[min(440px,100vw)] flex-col border-l border-hairline/50 bg-panel shadow-2xl shadow-black/50">
+ <aside ref={dialogRef} tabIndex={-1} role="dialog" aria-modal="true" aria-label={title} data-testid="peer-exchange" className="flex h-full w-[min(440px,100vw)] flex-col border-l border-hairline/50 bg-panel shadow-2xl shadow-black/50">
<header className="flex items-center justify-between gap-3 border-b border-hairline/40 px-4 py-3">
<h2 className="truncate text-[14px] font-medium text-ink">{title}</h2>
- <button type="button" autoFocus onClick={onClose} title={t("common.close")} aria-label={t("common.close")} className="rounded p-1 text-ink-secondary hover:bg-raised hover:text-ink">
+ <button type="button" onClick={onClose} title={t("common.close")} aria-label={t("common.close")} className="rounded p-1 text-ink-secondary hover:bg-raised hover:text-ink">
<X size={16} />
</button>
</header>
diff --git a/src/lib/peer-message.test.ts b/src/lib/peer-message.test.ts
index f64f2bd176..4ed4f160e9 100644
--- a/src/lib/peer-message.test.ts
+++ b/src/lib/peer-message.test.ts
@@ -124,6 +124,28 @@ describe("peerExchange", () => {
]);
});
+ it("files a reply under the request it names, even after another line", () => {
+ const messages = [
+ ask("p1", "Liaison", "L", "First request", 1),
+ { id: "u1", role: "user" as const, kind: "text" as const, text: "person in between", at: 2 },
+ { ...reply("b1", "Done with the first", 3), requestMessageId: "p1" },
+ ask("p2", "Other", "O", "Not this peer", 4),
+ { ...reply("b2", "late answer to Liaison", 5), requestMessageId: "p1" },
+ { ...reply("b3", "answer to Other", 6), requestMessageId: "p2" },
+ ];
+ expect(peerExchange(messages, "me", { botId: "L", name: "Liaison" }).map((line) => line.id)).toEqual(["p1", "b1", "b2"]);
+ expect(peerExchange(messages, "me", { botId: "O", name: "Other" }).map((line) => line.id)).toEqual(["p2", "b3"]);
+ });
+
+ it("leaves out another room member's reply", () => {
+ const messages = [
+ ask("p1", "Liaison", "L", "First request", 1),
+ { ...reply("m1", "member's own reply", 2), from: { botId: "M", name: "Member", color: "blue" as const } },
+ { ...reply("b1", "my answer", 3), from: { botId: "me", name: "Helper", color: "green" as const } },
+ ];
+ expect(peerExchange(messages, "me", { botId: "L", name: "Liaison" }).map((line) => line.id)).toEqual(["p1", "b1"]);
+ });
+
it("matches room requests from the same peer by name when no id is known", () => {
const messages = [
{ id: "r1", role: "bot" as const, kind: "text" as const, text: "Room ask", at: 1, from: { name: "Chief" }, roomRequest: { phase: "request" } },
diff --git a/src/lib/peer-message.ts b/src/lib/peer-message.ts
index 4d28cd123f..0fa9e7b839 100644
--- a/src/lib/peer-message.ts
+++ b/src/lib/peer-message.ts
@@ -67,7 +67,7 @@ export interface ExchangeLine {
at: number;
}
-type ExchangeMessage = Pick<Message, "id" | "role" | "kind" | "text" | "at" | "peerAsk" | "from" | "roomRequest">;
+type ExchangeMessage = Pick<Message, "id" | "role" | "kind" | "text" | "at" | "peerAsk" | "from" | "roomRequest" | "requestMessageId">;
/** One peer's requests in this thread and the bot's written answers to each, for the side view. */
export function peerExchange(
@@ -77,14 +77,23 @@ export function peerExchange(
): ExchangeLine[] {
const lines: ExchangeLine[] = [];
let answering = false;
+ const asked = new Set<string>();
for (const m of messages) {
const from = peerLine(m) ?? peerRequest(m, threadBotId);
if (from) {
answering = peer.botId && from.botId ? from.botId === peer.botId : from.name === peer.name;
- if (answering) lines.push({ id: m.id, from: "peer", text: from.body, at: m.at });
+ if (answering) {
+ asked.add(m.id);
+ lines.push({ id: m.id, from: "peer", text: from.body, at: m.at });
+ }
} else if (m.role === "user") {
answering = false;
- } else if (answering && m.role === "bot" && m.kind === "text" && m.text?.trim()) {
+ } else if (
+ m.role === "bot" && m.kind === "text" && m.text?.trim() &&
+ // Another room member's reply is not this bot's answer; a reply names its request when it can.
+ (!m.from || m.from.botId === threadBotId) &&
+ (m.requestMessageId ? asked.has(m.requestMessageId) : answering)
+ ) {
lines.push({ id: m.id, from: "bot", text: m.text, at: m.at });
}
}