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.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
import { useEffect, useRef, useState } from "react";
import { Download, ExternalLink, Loader2, LogIn } from "lucide-react";
import { Button } from "@/components/ui/button";
import { BrandedSelect } from "@/components/ui/select";
import { useT } from "@/i18n";
import {
cancelLoginFlow,
getLoginFlow,
startLoginFlow,
submitLoginFlowCode,
type AccountPlatformGroup,
type LoginFlowState,
} from "@/lib/agentAccountsApi";
import { requestConnect, spreadDelay } from "@/lib/connectBudget";
import { openExternalUrl } from "@/lib/openExternal";
import { recheckAgent } from "@/lib/agenticIdeApi";
import { useRestartApp } from "@/hooks/useRestartApp";
export function LiveSubscriptionAccount({
group,
accountId,
loading,
disabled,
voiceStatus,
onAccountChange,
onConnected,
}: {
group?: AccountPlatformGroup;
accountId: string;
loading: boolean;
disabled: boolean;
voiceStatus: string;
onAccountChange: (accountId: string) => void;
onConnected: () => void;
}) {
const t = useT();
const restart = useRestartApp();
const selectedId = accountId || group?.active_account || "";
const account = group?.accounts.find((entry) => entry.id === selectedId);
const connected = account?.connected && account.mode === "subscription";
const [flow, setFlow] = useState<LoginFlowState | null>(null);
const [busy, setBusy] = useState(false);
const [error, setError] = useState("");
const [code, setCode] = useState("");
const [cliMissing, setCliMissing] = useState(false);
const [installing, setInstalling] = useState(false);
const [installNeedsRestart, setInstallNeedsRestart] = useState(false);
const installController = useRef<AbortController | null>(null);
const flowRef = useRef<LoginFlowState | null>(null);
const onConnectedRef = useRef(onConnected);
onConnectedRef.current = onConnected;
const generation = useRef(0);
// Changing the voice account or leaving setup ends its pending login. A late
// response must never attach the previous account's flow to the new account.
useEffect(() => {
generation.current += 1;
setFlow(null);
setError("");
setCode("");
setBusy(false);
setCliMissing(false);
setInstalling(false);
setInstallNeedsRestart(false);
return () => {
generation.current += 1;
installController.current?.abort();
installController.current = null;
const current = flowRef.current;
flowRef.current = null;
if (current && !current.finished) {
void cancelLoginFlow(current.flow_id).catch(() => {
// The view is gone; the server also bounds the login lifetime.
});
}
};
}, [selectedId]);
function accept(next: LoginFlowState) {
flowRef.current = next;
setFlow(next);
if (next.status === "success") onConnectedRef.current();
}
useEffect(() => {
if (!flow || flow.finished) return;
let disposed = false;
const current = generation.current;
const cancel = requestConnect(() => {
void getLoginFlow(flow.flow_id).then((next) => {
if (disposed || generation.current !== current) return;
flowRef.current = next;
setFlow(next);
if (next.status === "success") onConnectedRef.current();
}).catch(() => {
if (disposed || generation.current !== current) return;
// A failed status read is terminal here; retry is an explicit action.
setError(t("live.subscription_login_failed"));
setFlow(null);
});
}, spreadDelay(1500));
return () => { disposed = true; cancel(); };
}, [flow, t]);
async function signIn(afterInstall = false, signal?: AbortSignal) {
if (!selectedId || busy) return;
const current = generation.current;
setBusy(true);
setError("");
try {
// This read verifies the private login-only helper without advertising it
// as a complete coding CLI. It never installs or starts an OAuth flow.
const response = await fetch("/api/live/login-helper", { cache: "no-store", signal });
if (generation.current !== current || signal?.aborted) return;
if (response.status === 404) {
// Older backends can still sign in through a full existing Codex CLI.
const existing = await recheckAgent("codex");
if (generation.current !== current || signal?.aborted) return;
if (existing.installed === false) {
setCliMissing(true);
setInstallNeedsRestart(true);
return;
}
if (existing.installed !== true) throw new Error("Invalid local CLI status");
} else {
if (!response.ok) throw new Error("Sign-in helper status unavailable");
const status = await response.json();
if (generation.current !== current || signal?.aborted) return;
if (status.status === "missing" && status.installed === false) {
setCliMissing(true);
setInstallNeedsRestart(false);
if (afterInstall) setError(t("live.subscription_install_failed"));
return;
}
if (status.status !== "ready" || status.installed !== true) {
throw new Error("Invalid sign-in helper status");
}
}
setCliMissing(false);
const next = await startLoginFlow(selectedId);
if (generation.current !== current || signal?.aborted) {
if (!next.finished) await cancelLoginFlow(next.flow_id);
return;
}
accept(next);
} catch {
if (generation.current === current) setError(t(afterInstall && signal?.aborted
? "live.subscription_install_failed" : "live.subscription_login_failed"));
} finally {
if (generation.current === current) setBusy(false);
}
}
async function install() {
if (busy || !selectedId || installController.current) return;
const current = generation.current;
const controller = new AbortController();
installController.current = controller;
setInstalling(true);
setInstallNeedsRestart(false);
setError("");
// The backend bounds download/verification to 180 seconds and cooperatively
// cancels its worker on disconnect. Keep a slightly longer UI safety bound.
const timeout = window.setTimeout(() => controller.abort(), 190_000);
try {
const response = await fetch("/api/live/login-helper", { method: "POST", signal: controller.signal });
if (generation.current !== current || controller.signal.aborted) return;
if (response.status === 404) {
setInstallNeedsRestart(true);
return;
}
if (!response.ok) throw new Error("Sign-in helper installation failed");
const result = await response.json();
if (result.status !== "ready") throw new Error("Sign-in helper is not ready");
if (generation.current !== current || controller.signal.aborted) return;
// Native provisioning needs no Node/npm. Reverify the private helper
// before starting the selected account's existing OAuth flow.
await signIn(true, controller.signal);
} catch {
if (generation.current === current) setError(t("live.subscription_install_failed"));
} finally {
window.clearTimeout(timeout);
if (installController.current === controller) installController.current = null;
if (generation.current === current) setInstalling(false);
}
}
function cancelInstall() {
generation.current += 1;
installController.current?.abort();
installController.current = null;
setInstalling(false);
setBusy(false);
setError("");
}
async function submit() {
if (!flow || !code.trim() || busy) return;
const current = generation.current;
setBusy(true);
setError("");
try {
const next = await submitLoginFlowCode(flow.flow_id, code.trim());
if (generation.current === current) { accept(next); setCode(""); }
} catch {
if (generation.current === current) setError(t("live.subscription_login_failed"));
} finally {
if (generation.current === current) setBusy(false);
}
}
async function cancel() {
if (!flow || busy) return;
generation.current += 1;
setBusy(true);
try {
await cancelLoginFlow(flow.flow_id);
flowRef.current = null;
setFlow(null);
} catch {
setError(t("live.subscription_login_failed"));
} finally {
setBusy(false);
}
}
return (
<div className="space-y-3">
{/* One settings row: the account and its state on the left, the
picker on the right. Only an unavailable voice says more. */}
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between sm:gap-8">
<div className="min-w-0 space-y-1">
<div className="text-sm font-medium text-foreground">{t("live.subscription_account")}</div>
<p role="status" className="inline-flex items-center gap-1.5 text-xs text-muted-foreground">
<span aria-hidden="true" className={connected ? "h-1.5 w-1.5 rounded-full bg-success" : "h-1.5 w-1.5 rounded-full bg-border-strong"} />
{loading ? t("live.loading") : connected
? t("live.subscription_connected") : t("live.subscription_sign_in_required")}
</p>
{connected && voiceStatus === "unavailable" ? (
<p className="text-xs text-warning">{t("live.subscription_voice_unavailable")}</p>
) : null}
</div>
<BrandedSelect
className="w-60"
value={accountId}
disabled={disabled || loading || busy || installing}
ariaLabel={t("live.subscription_account")}
onValueChange={onAccountChange}
options={[
{ value: "", label: t("live.subscription_active_account") },
...(group?.accounts ?? []).map((entry) => ({ value: entry.id, label: entry.label })),
]}
/>
</div>
{(!flow || flow.finished) && !connected ? (
cliMissing ? <div className="space-y-2">
<p role="status" className="text-xs leading-relaxed text-muted-foreground">
{t("live.subscription_install_required")}
</p>
{installing ? <div className="flex flex-wrap items-center gap-2">
<p role="status" className="flex items-center gap-2 text-xs text-muted-foreground">
<Loader2 className="h-3.5 w-3.5 animate-spin" />{t("live.subscription_installing")}
</p>
<Button type="button" variant="ghost" size="sm" onClick={cancelInstall}>{t("common.cancel")}</Button>
</div> : installNeedsRestart ? <div className="space-y-2">
<p role="status" className="text-xs text-muted-foreground">{t("settings_view.wake_word.restart_required")}</p>
<Button type="button" variant="outline" size="sm" disabled={restart.restarting}
onClick={() => void restart.restart()}>{restart.buttonLabel}</Button>
</div> : <Button type="button" variant="outline" size="sm"
disabled={disabled || busy || !selectedId} onClick={() => void install()}>
<Download />{t("live.subscription_install")}
</Button>}
{!installing ? <Button type="button" variant="ghost" size="sm" disabled={disabled || busy}
onClick={() => void signIn()}>{t("common.retry")}</Button> : null}
</div> : <Button type="button" variant="outline" size="sm" disabled={disabled || busy || !selectedId} onClick={() => void signIn()}>
{busy ? <Loader2 className="animate-spin" /> : <LogIn />}
{t("live.subscription_sign_in")}
</Button>
) : null}
{flow && !flow.finished ? (
<div className="space-y-2 border-t border-border pt-3">
<p role="status" className="flex items-center gap-2 text-xs text-muted-foreground">
<Loader2 className="h-3.5 w-3.5 animate-spin" />
{t("agent_accounts.flow.waiting_browser")}
</p>
{flow.url ? (
<Button type="button" variant="outline" size="sm" onClick={() => {
void openExternalUrl(flow.url!).then((opened) => {
if (!opened) setError(t("live.subscription_browser_failed"));
});
}}>
<ExternalLink />{t("agent_accounts.flow.open")}
</Button>
) : null}
{flow.code_expected ? (
<div className="flex flex-wrap items-end gap-2">
<label className="min-w-0 flex-1 space-y-1 text-xs">
<span>{t("agent_accounts.flow.code_placeholder")}</span>
<input value={code} onChange={(event) => setCode(event.target.value)} autoComplete="off" spellCheck={false}
className="w-full rounded-md border border-border bg-background px-3 py-2 text-sm text-foreground" />
</label>
<Button type="button" size="sm" disabled={busy || !code.trim()} onClick={() => void submit()}>{t("agent_accounts.flow.submit")}</Button>
</div>
) : null}
<Button type="button" variant="ghost" size="sm" disabled={busy} onClick={() => void cancel()}>{t("agent_accounts.flow.cancel")}</Button>
</div>
) : null}
{flow?.status === "failed" ? <p role="alert" className="text-xs text-destructive">{t("live.subscription_login_failed")}</p> : null}
{error ? <p role="alert" className="text-xs text-destructive">{error}</p> : null}
</div>
);
}