Skip to content

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

Blank chat Live call: keep the pending start per bot so a composer remount keeps it

AdityaUmale55a13267fcc0
diff --git a/src/components/CallView.start-request.test.ts b/src/components/CallView.start-request.test.ts
index 2389636ae8..cc661a6eca 100644
--- a/src/components/CallView.start-request.test.ts
+++ b/src/components/CallView.start-request.test.ts
@@ -44,6 +44,10 @@ const render = (threadId = "thread-atlas") => flushSync(() => root.render(create
targetId: "atlas", targetName: "Atlas", threadId, voices: ["v"],
requireExplicitVoices: false, liveCapable: true, placement: "composer", fill: "#8057C8", onStart,
})));
+const remount = () => {
+ flushSync(() => root.unmount());
+ root = createRoot(container);
+};
beforeEach(() => {
fixture.liveCall = null;
@@ -125,6 +129,32 @@ describe("a call started from the composer's + menu", () => {
expect(liveMedia().phase).toBe("idle");
});
+ it("waits for the blank chat's Live start when the made conversation remounts the button before Live is pressed again", () => {
+ render(newChatThreadId("atlas"));
+ flushSync(() => requestCallStart("atlas", "live"));
+ remount();
+ render("made");
+ flushSync(() => requestCallStart("atlas", "live"));
+ expect(liveMedia().phase).toBe("idle");
+ expect(fixture.dispatch).toHaveBeenCalledOnce();
+
+ flushSync(() => fixture.dispatch.mock.calls[0][0].onMade("made"));
+ expect(liveMedia()).toMatchObject({ phase: "starting", botId: "atlas", threadId: "made" });
+ });
+
+ it("never goes Live from a blank chat after a Turns call started on the remounted button", () => {
+ render(newChatThreadId("atlas"));
+ flushSync(() => requestCallStart("atlas", "live"));
+ remount();
+ render("made");
+ flushSync(() => requestCallStart("atlas", "turns"));
+ expect(currentCall()).toBe("atlas");
+ flushSync(() => endCall("atlas"));
+
+ flushSync(() => fixture.dispatch.mock.calls[0][0].onMade("made"));
+ expect(liveMedia().phase).toBe("idle");
+ });
+
it("is ignored for another bot, and while this one is already on a call", () => {
flushSync(() => requestCallStart("juniper", "turns"));
expect(currentCall()).toBeNull();
diff --git a/src/components/CallView.tsx b/src/components/CallView.tsx
index 6fc9e2a02f..b8835e1d52 100644
--- a/src/components/CallView.tsx
+++ b/src/components/CallView.tsx
@@ -38,6 +38,15 @@ type Phase = "listening" | "sending" | "working" | "speaking";
* composer's own button: two buttons answering it would start two calls. */
export type CallButtonPlacement = "header" | "composer" | "panel";
+/** Each bot's Live start from a blank chat, kept outside the button: making the conversation changes the chat's thread,
+ * which remounts the composer and the button inside it. */
+const liveStarts = new Map<string, { request: number; waiting: number }>();
+function liveStartOf(botId: string): { request: number; waiting: number } {
+ let pending = liveStarts.get(botId);
+ if (!pending) liveStarts.set(botId, (pending = { request: 0, waiting: 0 }));
+ return pending;
+}
+
/** `fill` (Simple composer): a round button in that colour with its glyph in `ink`, and no chevron; the + menu picks the
* call mode. */
/** `inverse` (phone composer): a round ink-coloured button with an app-coloured glyph: white on Midnight, dark on Meadow. */
@@ -180,23 +189,22 @@ export function CallTargetButton({
// A Live call from a blank chat starts once its conversation is made and its held settings are saved; a later start
// or a Turns call cancels it, and pressing Live again meanwhile waits for that same start.
- const liveRequest = useRef(0);
- const waitingLive = useRef(0);
- useEffect(() => { if (onCall) liveRequest.current++; }, [onCall]);
+ useEffect(() => { if (onCall) liveStartOf(targetId).request++; }, [onCall, targetId]);
const start = (next: CallMode) => {
setHelpOpen(false);
setMenuOpen(false);
- if (next === "live" && waitingLive.current && waitingLive.current === liveRequest.current) return;
- const request = ++liveRequest.current;
+ const pending = liveStartOf(targetId);
+ if (next === "live" && pending.waiting && pending.waiting === pending.request) return;
+ const request = ++pending.request;
if (next === "live" && liveThreadId !== undefined) {
onStart("live");
const live = (thread: string) => {
- if (waitingLive.current === request) waitingLive.current = 0;
- if (liveRequest.current === request) void startLiveCall({ botId: targetId, threadId: thread, provider: state.config?.live?.provider });
+ if (pending.waiting === request) pending.waiting = 0;
+ if (pending.request === request) void startLiveCall({ botId: targetId, threadId: thread, provider: state.config?.live?.provider });
};
if (isNewChatThread(liveThreadId)) {
- waitingLive.current = request;
- const failed = () => { if (waitingLive.current === request) waitingLive.current = 0; };
+ pending.waiting = request;
+ const failed = () => { if (pending.waiting === request) pending.waiting = 0; };
dispatch({ type: "makeNewChat", botId: targetId, threadId: liveThreadId, onMade: live, onError: failed });
} else live(liveThreadId);
return;