Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(live): hold the Live provider while a key save is pending
Nathaniel9454c8191962
diff --git a/src/components/LiveCallSettings.tsx b/src/components/LiveCallSettings.tsxindex 6ef0edf1e4..ddf5589728 100644--- a/src/components/LiveCallSettings.tsx+++ b/src/components/LiveCallSettings.tsx@@ -17,6 +17,8 @@ export function LiveCallSettings({ onClose }: { onClose: () => void }) {const live: LiveSettings = state.config?.live ?? { configured: false, voice: "", readTypedReplies: true, idleMinutes: 5 };const [saving, setSaving] = useState(false);const [changingKey, setChangingKey] = useState(false);+ const [keySaving, setKeySaving] = useState(false);+ const [providerSaving, setProviderSaving] = useState(false);const [error, setError] = useState<string | null>(null);const typedHintId = useId();// Stable, so this callback ref runs once when the popover mounts, not on every save.@@ -70,7 +72,7 @@ export function LiveCallSettings({ onClose }: { onClose: () => void }) {onClose();}}>- <LiveProviderSettings />+ <LiveProviderSettings locked={keySaving} onSaving={setProviderSaving} />{!fish && <><label className="flex items-center justify-between gap-2"><span className="text-ink-secondary">{t("call.live.voice")}</span>@@ -128,7 +130,7 @@ export function LiveCallSettings({ onClose }: { onClose: () => void }) {<div className="-mt-2 text-[12px] text-ink-tertiary">{t(live.keySource === "openai" ? "call.live.keyFromApiKeys" : "call.live.needsKey")} <KeysLink id="openai" purpose="liveCalls" /></div>)}{/* Keyed by provider: a key typed for one provider is never saved for another. */}- {changingKey && <LiveKeySetup key={state.config?.live?.provider ?? "openai"} compact onSaved={() => setChangingKey(false)} />}+ {changingKey && <LiveKeySetup key={state.config?.live?.provider ?? "openai"} compact locked={providerSaving} onSaving={setKeySaving} onSaved={() => setChangingKey(false)} />}<div className="text-[12px] text-ink-tertiary">{liveDisclosure({ cloudHome: state.config?.cloudHome === true, azure, fish })} {t(copy.cost)}</div>{error && <div className="text-[12px] text-danger">{error}</div>}</div>diff --git a/src/components/LiveKeyNeeded.tsx b/src/components/LiveKeyNeeded.tsxindex e6f82fcd59..7b53b38129 100644--- a/src/components/LiveKeyNeeded.tsx+++ b/src/components/LiveKeyNeeded.tsx@@ -1,4 +1,4 @@-import { useEffect, useId } from "react";+import { useEffect, useId, useState } from "react";import { KeyRound } from "lucide-react";import { liveDisclosure, liveKeyCopy } from "@/lib/call-mode";@@ -23,14 +23,22 @@ export function LiveKeyNeeded() {<KeyRound size={16} className="mt-0.5 shrink-0 text-ink-secondary" aria-hidden="true" /><h2 id={titleId} className="text-[14px] font-medium leading-[1.4] text-ink">{t(copy.needs)}</h2></div>- <LiveProviderSettings />+ <ProviderAndKey provider={live?.provider ?? "openai"} keyForm={keyForm} />{(azure || fish) && <DismissWhenReady ready={live?.configured === true} />}- {keyForm && <LiveKeySetup key={state.config?.live?.provider ?? "openai"} compact onSaved={() => {}} />}{!azure && !fish && <button type="button" className="ui-button ui-button-primary self-start" onClick={() => { dismissLiveNotice(); dispatch(openKeySettings("openai", "liveCalls")); }}>{t("call.live.openApiKeys")}</button>}<p className="text-[12px] leading-[1.45] text-ink-tertiary">{!keyForm && `${liveDisclosure({ cloudHome: state.config?.cloudHome === true, azure, fish })} `}{t(copy.cost)}</p></section>;}+function ProviderAndKey({ provider, keyForm }: { provider: string; keyForm: boolean }) {+ const [keySaving, setKeySaving] = useState(false);+ const [providerSaving, setProviderSaving] = useState(false);+ return <>+ <LiveProviderSettings locked={keySaving} onSaving={setProviderSaving} />+ {keyForm && <LiveKeySetup key={provider} compact locked={providerSaving} onSaving={setKeySaving} onSaved={() => {}} />}+ </>;+}+function DismissWhenReady({ ready }: { ready: boolean }) {useEffect(() => {if (ready) dismissLiveNotice();diff --git a/src/components/LiveKeySetup.tsx b/src/components/LiveKeySetup.tsxindex e07f3c8b78..6435a1afdf 100644--- a/src/components/LiveKeySetup.tsx+++ b/src/components/LiveKeySetup.tsx@@ -5,7 +5,16 @@ import { liveDisclosure, liveKeyCopy } from "@/lib/call-mode";import { cn } from "@/lib/cn";import { t } from "@/lib/i18n";-export function LiveKeySetup({ onSaved, compact = false }: { onSaved: () => void; compact?: boolean }) {+/**+ * The server binds a key to the provider set when the key arrives, so the provider select is held while a save is+ * pending (`onSaving`), and no key is sent while a provider change is (`locked`).+ */+export function LiveKeySetup({ onSaved, onSaving, locked = false, compact = false }: {+ onSaved: () => void;+ onSaving?: (saving: boolean) => void;+ locked?: boolean;+ compact?: boolean;+}) {const { state, dispatch } = useStore();const provider = state.config?.live?.provider;const azure = provider === "azure";@@ -21,8 +30,9 @@ export function LiveKeySetup({ onSaved, compact = false }: { onSaved: () => void}, []);const save = async () => {const value = key.trim();- if (!value || saving) return;+ if (!value || saving || locked) return;setSaving(true);+ onSaving?.(true);setError(null);try {const status: ConfigStatus = window.ogb?.setCredential@@ -37,6 +47,7 @@ export function LiveKeySetup({ onSaved, compact = false }: { onSaved: () => voidif (mounted.current) setError(saveError instanceof Error ? saveError.message : String(saveError));} finally {if (mounted.current) setSaving(false);+ onSaving?.(false);}};return (@@ -66,7 +77,7 @@ export function LiveKeySetup({ onSaved, compact = false }: { onSaved: () => void{error && <div className="text-[12px] text-danger">{error}</div>}<buttontype="submit"- disabled={!key.trim() || saving}+ disabled={!key.trim() || saving || locked}className="self-start rounded-lg bg-accent px-3 py-1.5 text-[13px] font-medium text-white hover:brightness-110 disabled:opacity-50">{saving ? t("call.live.keySaving") : compact ? t("common.save") : t("call.live.keySaveAndCall")}diff --git a/src/components/LiveProviderSettings.tsx b/src/components/LiveProviderSettings.tsxindex 0e6f329264..38a181ce56 100644--- a/src/components/LiveProviderSettings.tsx+++ b/src/components/LiveProviderSettings.tsx@@ -9,7 +9,8 @@ type ProviderPatch = Partial<Pick<LiveSettings, "provider" | "azureEndpoint" | "const FIELD = "w-full rounded-lg border border-hairline bg-inset px-2.5 py-1.5 text-[13px] text-ink outline-none focus:border-accent/60";-export function LiveProviderSettings() {+/** `locked` holds the provider while a key save is pending; `onSaving` reports a pending provider change. */+export function LiveProviderSettings({ locked = false, onSaving }: { locked?: boolean; onSaving?: (saving: boolean) => void } = {}) {const { state, dispatch } = useStore();const live: LiveSettings = state.config?.live ?? { configured: false, voice: "", readTypedReplies: true, idleMinutes: 5 };const provider: LiveProvider = live.provider ?? "openai";@@ -23,6 +24,7 @@ export function LiveProviderSettings() {const save = async (patch: ProviderPatch) => {setSaving(true);+ onSaving?.(true);setError(null);try {const body = await api<{ live: LiveSettings }>("/api/live/settings", { method: "PATCH", body: JSON.stringify(patch) });@@ -31,6 +33,7 @@ export function LiveProviderSettings() {setError(saveError instanceof Error ? saveError.message : String(saveError));} finally {setSaving(false);+ onSaving?.(false);}};@@ -41,7 +44,7 @@ export function LiveProviderSettings() {<Selectsize="sm"value={provider}- disabled={saving}+ disabled={saving || locked}onChange={(event) => void save({ provider: event.target.value as LiveProvider })}><option value="openai">{t("call.live.provider.openai")}</option>diff --git a/src/components/keys/KeysPage.dom.test.ts b/src/components/keys/KeysPage.dom.test.tsindex aaa26257fd..3f3d80d960 100644--- a/src/components/keys/KeysPage.dom.test.ts+++ b/src/components/keys/KeysPage.dom.test.ts@@ -59,6 +59,12 @@ function type(input: HTMLInputElement, value: string) {});}+async function answered(reply: () => void) {+ reply();+ await new Promise((resolve) => setTimeout(resolve, 0));+ flushSync(() => {});+}+function paste(input: HTMLInputElement, text: string) {const event = new Event("paste", { bubbles: true, cancelable: true });Object.defineProperty(event, "clipboardData", { value: { getData: () => text } });@@ -160,6 +166,33 @@ describe("the keys page", () => {expect(host.querySelector("[data-keys-empty]")!.textContent).toBe("No keys match “zzz”.");});+ it("holds the Live provider while a key save is pending, and the key while a provider change is", async () => {+ const live = { configured: false, provider: "fish", fishAgentId: "agent_1", voice: "", readTypedReplies: true, idleMinutes: 5 };+ setConfig({ ...fx.config, live });+ let answer: (value: unknown) => void = () => {};+ fx.api.mockImplementation(() => new Promise((resolve) => { answer = resolve; }));+ render(() => createElement(LiveKeyNeeded, { key: "notice" }));+ const provider = () => host.querySelector<HTMLSelectElement>("select")!;+ const saveKey = () => host.querySelector<HTMLInputElement>('input[type="password"]')!.closest("form")!.querySelector<HTMLButtonElement>('button[type="submit"]')!;+ type(host.querySelector<HTMLInputElement>('input[type="password"]')!, "fish-key");+ flushSync(() => saveKey().click());+ expect(fx.api).toHaveBeenCalledWith("/api/config", expect.objectContaining({ method: "PUT" }));+ expect(provider().disabled).toBe(true);+ await answered(() => answer({ ...fx.config, live: { ...live, keySource: "live" } }));+ expect(provider().disabled).toBe(false);++ setConfig({ ...fx.config, live });+ flushSync(() => {+ provider().value = "openai";+ provider().dispatchEvent(new Event("change", { bubbles: true }));+ });+ expect(fx.api).toHaveBeenLastCalledWith("/api/live/settings", expect.objectContaining({ method: "PATCH" }));+ type(host.querySelector<HTMLInputElement>('input[type="password"]')!, "fish-key");+ expect(saveKey().disabled).toBe(true);+ await answered(() => answer({ live }));+ expect(saveKey().disabled).toBe(false);+ });+it("lands the live-call notice's button on the OpenAI row in Voice and calls, focused and lit", async () => {render(() => createElement(LiveKeyNeeded, { key: "notice" }), () => createElement(KeysPage, { key: "page" }));expect(expanded("voice")).toBe("false");