Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(voice): retry realtime discovery after cold boot

Ruben Lütkea60ded5d0c95
diff --git a/jarvis/ui/web/frontend/src/hooks/useVoiceMode.test.tsx b/jarvis/ui/web/frontend/src/hooks/useVoiceMode.test.tsx
new file mode 100644
index 0000000000..95e1ee2499
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/hooks/useVoiceMode.test.tsx
@@ -0,0 +1,58 @@
+import { cleanup, renderHook, waitFor } from "@testing-library/react";
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import type { ReactNode } from "react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import { useVoiceMode } from "./useVoiceMode";
+
+function response(available: boolean): Response {
+ return {
+ ok: true,
+ json: async () => ({
+ mode: "realtime",
+ realtime_available: available,
+ requires_webrtc_offer: true,
+ active_provider: available ? "codex-subscription-realtime" : null,
+ active_provider_label: available ? "ChatGPT subscription (Codex)" : null,
+ active_model: available ? "gpt-realtime-1.5" : null,
+ session_active: false,
+ active_session_mode: null,
+ active_session_provider: "",
+ active_session_model: "",
+ transitioning: false,
+ }),
+ } as Response;
+}
+
+afterEach(() => {
+ cleanup();
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+});
+
+describe("useVoiceMode realtime discovery", () => {
+ it("rechecks a transient cold-boot unavailable result", async () => {
+ const fetchMock = vi
+ .fn()
+ .mockResolvedValueOnce(response(false))
+ .mockResolvedValue(response(true));
+ vi.stubGlobal("fetch", fetchMock);
+ const client = new QueryClient({
+ defaultOptions: { queries: { retry: false } },
+ });
+ const wrapper = ({ children }: { children: ReactNode }) => (
+ <QueryClientProvider client={client}>{children}</QueryClientProvider>
+ );
+
+ const { result } = renderHook(() => useVoiceMode(), { wrapper });
+ await waitFor(() => expect(result.current.statusKnown).toBe(true));
+ expect(result.current.realtimeAvailable).toBe(false);
+
+ await waitFor(
+ () => expect(result.current.realtimeAvailable).toBe(true),
+ { timeout: 2_500 },
+ );
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+ expect(fetchMock.mock.calls[1]?.[1]).toMatchObject({ cache: "no-store" });
+ });
+});
diff --git a/jarvis/ui/web/frontend/src/hooks/useVoiceMode.ts b/jarvis/ui/web/frontend/src/hooks/useVoiceMode.ts
index 0467a6de3e..61b14bb845 100644
--- a/jarvis/ui/web/frontend/src/hooks/useVoiceMode.ts
+++ b/jarvis/ui/web/frontend/src/hooks/useVoiceMode.ts
@@ -1,4 +1,4 @@
-import { useEffect, useMemo } from "react";
+import { useEffect, useMemo, useRef } from "react";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { useEventStore } from "@/store/events";
@@ -19,12 +19,45 @@ type VoiceModeResp = {
transitioning: boolean;
};
+const REALTIME_DISCOVERY_RETRY_WINDOW_MS = 5 * 60_000;
+const REALTIME_DISCOVERY_RETRY_MAX_MS = 10_000;
+
export function useVoiceMode() {
const qc = useQueryClient();
const events = useEventStore((state) => state.events);
+ const realtimeDiscoveryStartedAt = useRef<number | null>(null);
const q = useQuery<VoiceModeResp>({
queryKey: ["voice-mode"],
- queryFn: async () => (await fetch("/api/settings/voice-mode")).json(),
+ queryFn: async ({ signal }) =>
+ (
+ await fetch("/api/settings/voice-mode", {
+ cache: "no-store",
+ signal,
+ })
+ ).json(),
+ refetchInterval: (query) => {
+ const data = query.state.data;
+ const discoveryPending =
+ data?.mode === "realtime" && data.realtime_available === false;
+ if (!discoveryPending) {
+ realtimeDiscoveryStartedAt.current = null;
+ return false;
+ }
+ const now = Date.now();
+ realtimeDiscoveryStartedAt.current ??= now;
+ if (
+ now - realtimeDiscoveryStartedAt.current >=
+ REALTIME_DISCOVERY_RETRY_WINDOW_MS
+ ) {
+ return false;
+ }
+ // Codex discovery can briefly report unavailable while its isolated
+ // login/runtime probe is still in flight during cold boot. Retry quickly
+ // once, then back off while a browser OAuth flow is still open.
+ const exponent = Math.max(0, query.state.dataUpdateCount - 1);
+ return Math.min(1_000 * 2 ** exponent, REALTIME_DISCOVERY_RETRY_MAX_MS);
+ },
+ refetchIntervalInBackground: false,
});
const m = useMutation({
mutationFn: async (mode: string) => {