Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Blank chat Live call: wait for held settings, cancel on a later start or Turns call
AdityaUmale531128a98951
diff --git a/src/components/CallView.start-request.test.ts b/src/components/CallView.start-request.test.tsindex ee6637666c..46cd2b3ac5 100644--- a/src/components/CallView.start-request.test.ts+++ b/src/components/CallView.start-request.test.ts@@ -93,6 +93,17 @@ describe("a call started from the composer's + menu", () => {expect(liveMedia()).toMatchObject({ phase: "starting", botId: "atlas", threadId: "made" });});+ it("never goes Live from a blank chat after a Turns call started and hung up while the conversation was being made", () => {+ render(newChatThreadId("atlas"));+ flushSync(() => requestCallStart("atlas", "live"));+ 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.tsxindex 3580e6d828..131445c385 100644--- a/src/components/CallView.tsx+++ b/src/components/CallView.tsx@@ -178,12 +178,18 @@ export function CallTargetButton({useEffect(() => () => dismissStartPrompt(targetId), [targetId]);+ // A Live call from a blank chat starts once its conversation is made; a later start or a Turns call cancels it.+ const liveRequest = useRef(0);+ useEffect(() => { if (onCall) liveRequest.current++; }, [onCall]);const start = (next: CallMode) => {setHelpOpen(false);setMenuOpen(false);+ const request = ++liveRequest.current;if (next === "live" && liveThreadId !== undefined) {onStart("live");- const live = (thread: string) => void startLiveCall({ botId: targetId, threadId: thread, provider: state.config?.live?.provider });+ const live = (thread: string) => {+ if (liveRequest.current === request) 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;diff --git a/src/state/new-chat.test.ts b/src/state/new-chat.test.tsindex 586100cde3..971b445b34 100644--- a/src/state/new-chat.test.ts+++ b/src/state/new-chat.test.ts@@ -39,6 +39,7 @@ let sends: Array<{ threadId?: string; text: string }>;let taskPatches: string[];let createAnswer: Promise<Response> | null;let switchAnswer: Promise<Response> | null;+let patchAnswer: Promise<Response> | null;const send = (text: string, extra: Partial<Extract<Action, { type: "send" }>> = {}) =>flushSync(() => dispatch({ type: "send", botId: "bot", threadId: DRAFT, text, sendId: `send-${text}`, ...extra }));@@ -50,6 +51,7 @@ beforeEach(async () => {taskPatches = [];createAnswer = null;switchAnswer = null;+ patchAnswer = null;const stored = new Map<string, string>();vi.stubGlobal("localStorage", {getItem: (key: string) => stored.get(key) ?? null, setItem: (key: string, value: string) => { stored.set(key, String(value)); },@@ -67,7 +69,10 @@ beforeEach(async () => {sends.push(body);return json({ threadId: body.threadId, message: { ...line(body.sendId), sendId: body.sendId } });}- if (/^\/api\/bots\/bot\/tasks\/[^/]+$/.test(path) && init?.method === "PATCH") taskPatches.push(path);+ if (/^\/api\/bots\/bot\/tasks\/[^/]+$/.test(path) && init?.method === "PATCH") {+ taskPatches.push(path);+ if (patchAnswer) return patchAnswer;+ }if (path === "/api/bots/bot/tasks/a") return json({ bot: bot("a", ["a"]) });if (path.startsWith("/api/bots/bot/tasks/b")) return switchAnswer ?? json({ bot: bot("b", ["a", "b"]) });if (path === "/api/instances") return json({ instances: [] });@@ -306,6 +311,21 @@ describe("a new conversation opens blank and is made by its first send", () => {expect(state.newChat).toBeNull();});+ it("answers a Live call only after the settings picked in the blank chat are saved", async () => {+ let saved!: (response: Response) => void;+ patchAnswer = new Promise((resolve) => { saved = resolve; });+ const made = vi.fn();+ openBlank();+ flushSync(() => dispatch({ type: "updateTask", botId: "bot", threadId: DRAFT, patch: { approvalMode: "ask" } }));+ flushSync(() => dispatch({ type: "makeNewChat", botId: "bot", threadId: DRAFT, onMade: made }));+ await vi.waitFor(() => expect(taskPatches).toEqual(["/api/bots/bot/tasks/made"]));+ await settle();+ expect(made).not.toHaveBeenCalled();++ saved(json({ bot: bot("made", ["a", "made"]) }));+ await vi.waitFor(() => expect(made).toHaveBeenCalledExactlyOnceWith("made"));+ });+it("makes one conversation for a Live call and a send that start together", async () => {const made = vi.fn();openBlank();diff --git a/src/state/new-chat.ts b/src/state/new-chat.tsindex 41e1b490bd..568fa835c8 100644--- a/src/state/new-chat.ts+++ b/src/state/new-chat.ts@@ -53,6 +53,8 @@ type Effects = {create: (botId: string, projectId: string | undefined) => Promise<{ bot: Bot }>;navigated: (botId: string) => void;showError: (error: unknown) => void;+ /** Settles once the conversation's held settings (approval mode, model) are saved, as a send waits for them. */+ settled: (botId: string, threadId: string) => Promise<unknown>;};/** The store's effects for the blank chat, answering true for an action they took. While a bot's blank chat is open, a@@ -86,7 +88,8 @@ export function createNewChat(effects: Effects) {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);+ conversationFor(action.botId)?.then((threadId) => effects.settled(action.botId, threadId).then(() => action.onMade(threadId)))+ .catch(effects.showError);return true;}if (action.type !== "send") {diff --git a/src/state/store.tsx b/src/state/store.tsxindex 5d4f5b2f00..bfb5fe26ad 100644--- a/src/state/store.tsx+++ b/src/state/store.tsx@@ -2095,7 +2095,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {const roomAnswer = roomAnswers(() => stateRef.current, rawDispatch);const olderPagesInFlight = new Set<string>();const newTaskOnce = createNewTaskOnce(), runNewChat = createNewChat({ state: () => store.getState(), rawDispatch, dispatch: (action) => wrapped(action), showError: (error) => showError(error),- navigated: (botId) => navigation.set(botId, (navigation.get(botId) ?? 0) + 1), create: (botId, projectId) => botPatchQueue.flush(botId).then(() => api<{ bot: Bot }>(`/api/bots/${botId}/tasks`, { method: "POST", body: JSON.stringify({ projectId }) })) });+ settled: (botId, threadId) => waitForExecutionSettings(stateRef.current.bots.filter((bot) => bot.id === botId), threadId), navigated: (botId) => navigation.set(botId, (navigation.get(botId) ?? 0) + 1), create: (botId, projectId) => botPatchQueue.flush(botId).then(() => api<{ bot: Bot }>(`/api/bots/${botId}/tasks`, { method: "POST", body: JSON.stringify({ projectId }) })) });let creatingBot = false;let noticeTimer: ReturnType<typeof setTimeout> | undefined;const showError = (e: unknown) => { if (isBotLimitRefusal(e)) return; if (isNoAiRefusal(e)) wrapped({ type: "notice", notice: { kind: "note", botName: null, text: e.message } }); else rawDispatch({ type: "error", message: e instanceof Error ? e.message : String(e) }); };