Skip to content

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

Blank chat: a Live call makes the conversation, then calls into it

AdityaUmale6ed18c8dfdad
diff --git a/src/components/CallView.start-request.test.ts b/src/components/CallView.start-request.test.ts
index 8ca2d861a3..ee6637666c 100644
--- a/src/components/CallView.start-request.test.ts
+++ b/src/components/CallView.start-request.test.ts
@@ -7,7 +7,7 @@ import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { AppState } from "@/state/store";
-const fixture = vi.hoisted(() => ({ liveCall: null as AppState["liveCall"] }));
+const fixture = vi.hoisted(() => ({ liveCall: null as AppState["liveCall"], dispatch: vi.fn() }));
vi.mock("@/state/store", async (importOriginal) => {
const original = await importOriginal<typeof import("@/state/store")>();
return {
@@ -21,7 +21,7 @@ vi.mock("@/state/store", async (importOriginal) => {
} as AppState["config"],
liveCall: fixture.liveCall,
},
- dispatch: vi.fn(),
+ dispatch: fixture.dispatch,
}),
};
});
@@ -35,17 +35,19 @@ const { requestCallStart } = await import("@/lib/call-start-request");
const { callMode, setCallMode } = await import("@/lib/call-mode");
const { currentCall, endCall, startCall } = await import("@/lib/call");
const { configureLiveMedia, liveMedia, resetLiveMedia } = await import("@/lib/live-call-media");
+const { newChatThreadId } = await import("@/state/new-chat");
let root: Root;
let container: HTMLDivElement;
const onStart = vi.fn();
-const render = () => flushSync(() => root.render(createElement(CallTargetButton, {
- targetId: "atlas", targetName: "Atlas", threadId: "thread-atlas", voices: ["v"],
+const render = (threadId = "thread-atlas") => flushSync(() => root.render(createElement(CallTargetButton, {
+ targetId: "atlas", targetName: "Atlas", threadId, voices: ["v"],
requireExplicitVoices: false, liveCapable: true, placement: "composer", fill: "#8057C8", onStart,
})));
beforeEach(() => {
fixture.liveCall = null;
+ fixture.dispatch.mockClear();
window.ogb = { speechStart: vi.fn(), speechStop: vi.fn(async () => {}) } as unknown as NonNullable<Window["ogb"]>;
configureLiveMedia({ getUserMedia: () => new Promise<MediaStream>(() => {}) });
container = document.createElement("div");
@@ -80,6 +82,17 @@ describe("a call started from the composer's + menu", () => {
expect(callMode()).toBe("live");
});
+ it("goes Live from a blank chat once the conversation it makes exists", () => {
+ render(newChatThreadId("atlas"));
+ flushSync(() => requestCallStart("atlas", "live"));
+ expect(onStart).toHaveBeenCalledWith("live");
+ expect(liveMedia().phase).toBe("idle");
+ expect(fixture.dispatch).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ type: "makeNewChat", botId: "atlas", threadId: newChatThreadId("atlas") }));
+
+ flushSync(() => fixture.dispatch.mock.calls[0][0].onMade("made"));
+ expect(liveMedia()).toMatchObject({ phase: "starting", botId: "atlas", threadId: "made" });
+ });
+
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 df5cb91b6e..3580e6d828 100644
--- a/src/components/CallView.tsx
+++ b/src/components/CallView.tsx
@@ -96,7 +96,7 @@ export function CallTargetButton({
const showThreads = useShowThreads();
const { capabilities, ready: capabilitiesReady } = useDesktopCapabilities();
const media = useLiveMedia();
- const liveThreadId = liveCapable && !isNewChatThread(threadId) ? threadId : undefined;
+ const liveThreadId = liveCapable ? threadId : undefined;
const canLive = liveThreadId !== undefined;
// Check Live first: startCall also marks the target as on a call.
const liveRunning = isLiveCallRunning(media.phase);
@@ -183,7 +183,9 @@ export function CallTargetButton({
setMenuOpen(false);
if (next === "live" && liveThreadId !== undefined) {
onStart("live");
- void startLiveCall({ botId: targetId, threadId: liveThreadId, provider: state.config?.live?.provider });
+ const live = (thread: string) => void startLiveCall({ botId: targetId, threadId: thread, provider: state.config?.live?.provider });
+ if (isNewChatThread(liveThreadId)) dispatch({ type: "makeNewChat", botId: targetId, threadId: liveThreadId, onMade: live });
+ else live(liveThreadId);
return;
}
if (!turnsReady) {
diff --git a/src/state/new-chat.test.ts b/src/state/new-chat.test.ts
index 89ff0b956b..586100cde3 100644
--- a/src/state/new-chat.test.ts
+++ b/src/state/new-chat.test.ts
@@ -293,4 +293,28 @@ describe("a new conversation opens blank and is made by its first send", () => {
expect(pepper().threadId).toBe("a");
expect(state.newChat?.botId).toBe("bot");
});
+
+ it("makes the conversation for a Live call started in the blank chat and answers with it", async () => {
+ const made = vi.fn();
+ openBlank();
+ flushSync(() => dispatch({ type: "makeNewChat", botId: "bot", threadId: DRAFT, onMade: made }));
+ await vi.waitFor(() => expect(made).toHaveBeenCalledExactlyOnceWith("made"));
+
+ expect(creates).toHaveLength(1);
+ expect(sends).toEqual([]);
+ expect(pepper().threadId).toBe("made");
+ expect(state.newChat).toBeNull();
+ });
+
+ it("makes one conversation for a Live call and a send that start together", async () => {
+ const made = vi.fn();
+ openBlank();
+ flushSync(() => dispatch({ type: "makeNewChat", botId: "bot", threadId: DRAFT, onMade: made }));
+ send("Hello");
+ await vi.waitFor(() => expect(sends).toHaveLength(1));
+
+ expect(creates).toHaveLength(1);
+ expect(made).toHaveBeenCalledExactlyOnceWith("made");
+ expect(sends[0]).toMatchObject({ threadId: "made", text: "Hello" });
+ });
});
diff --git a/src/state/new-chat.ts b/src/state/new-chat.ts
index 682220343b..41e1b490bd 100644
--- a/src/state/new-chat.ts
+++ b/src/state/new-chat.ts
@@ -4,6 +4,8 @@ import type { Action, AppState, Bot } from "./store";
/** A blank new chat with one bot: shown in place of its open conversation and made into one by the first send. */
export type NewChat = { session: number; botId: string; projectId?: string; held: HeldAction[] };
type HeldAction = Extract<Action, { type: "setModel" } | { type: "updateTask" }>;
+/** Makes the conversation a bot's blank chat stands for, as its first send would, and answers with its thread. */
+export type MakeNewChatAction = { type: "makeNewChat"; botId: string; threadId: string; onMade: (threadId: string) => void };
const THREAD_PREFIX = "new-chat-";
export const newChatThreadId = (botId: string) => `${THREAD_PREFIX}${botId}`;
@@ -56,7 +58,7 @@ type Effects = {
/** The store's effects for the blank chat, answering true for an action they took. While a bot's blank chat is open, a
* send to the conversation it covers goes into the blank chat too. The first send makes the conversation, once per blank
* chat however many arrive while it is being made, then each is sent into it. The conversation opens only if the person
- * is still in that blank chat when it is made. */
+ * is still in that blank chat when it is made. A Live call started in the blank chat makes it the same way. */
export function createNewChat(effects: Effects) {
const making = new Map<number, Promise<string>>(), heldWhileMaking = new Map<number, HeldAction[]>();
let sessions = 0;
@@ -82,13 +84,18 @@ export function createNewChat(effects: Effects) {
const state = effects.state(), open = state.newChat;
const intoBlank = action.type === "send" && open?.botId === action.botId && action.threadId === state.bots.find((bot) => bot.id === action.botId)?.threadId;
if (!intoBlank && !("threadId" in action && isNewChatThread(action.threadId))) return false;
+ const conversationFor = (botId: string) => open?.botId === botId ? making.get(open.session) ?? make(open) : null;
+ if (action.type === "makeNewChat") {
+ conversationFor(action.botId)?.then(action.onMade, effects.showError);
+ return true;
+ }
if (action.type !== "send") {
effects.rawDispatch(action);
const after = effects.state().newChat;
if (after && heldWhileMaking.has(after.session)) heldWhileMaking.set(after.session, after.held);
return true;
}
- const made = open?.botId === action.botId ? making.get(open.session) ?? make(open) : null;
+ const made = conversationFor(action.botId);
if (!made) {
action.onError?.();
return true;
diff --git a/src/state/store.tsx b/src/state/store.tsx
index 2b969ae112..5d4f5b2f00 100644
--- a/src/state/store.tsx
+++ b/src/state/store.tsx
@@ -65,7 +65,7 @@ import { useShowThreads } from "@/lib/thread-preferences";
import { speechOwner, spokenReplyStream } from "@/lib/spoken-reply-stream";
import { t } from "@/lib/i18n";
export { formatTime } from "@/lib/clock-time";
-import { createBotPatchQueue, flushWhenLeaving, keepaliveFits, type BotUpdatePatch } from "./bot-patch-queue"; import { createNewTaskOnce } from "./new-task-once"; import { createNewChat, reduceNewChat, type NewChat } from "./new-chat";
+import { createBotPatchQueue, flushWhenLeaving, keepaliveFits, type BotUpdatePatch } from "./bot-patch-queue"; import { createNewTaskOnce } from "./new-task-once"; import { createNewChat, reduceNewChat, type MakeNewChatAction, type NewChat } from "./new-chat";
import { liveCallFromFrame, nextLiveCallLookup } from "./live-call";
import { isNoAiRefusal, useChatErrorClear } from "./chat-error";
import { openLiveEvents, publishLiveFrame, publishMissedFrames, withoutScreenPixels } from "@/lib/live-events";
@@ -641,7 +641,7 @@ export type Action =
}
| { type: "deleteGroup"; groupId: string }
| { type: "newGroupTask"; groupId: string }
- | ForkPanelAction
+ | ForkPanelAction | MakeNewChatAction
| { type: "switchGroupTask"; groupId: string; threadId: string }
| { type: "renameGroupTask"; groupId: string; threadId: string; title: string }
| { type: "pinGroupTask"; groupId: string; threadId: string; pinned: boolean; title: string }
@@ -1749,7 +1749,7 @@ function reduceApp(state: AppState, action: Action): AppState {
case "markAllRoutineRunsSeen":
case "refreshTaskPermissions":
case "followBotModel":
- case "loadDataSheet":
+ case "loadDataSheet": case "makeNewChat":
return state;
case "dataSheet":
return {