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.

2.5 KB
Raw Blame
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { BrandedSelect } from "@/components/ui/select";
import { switchBrainProvider, useProviders } from "@/hooks/useProviders";
import { fill, useT } from "@/i18n";
import { useEventStore } from "@/store/events";

/**
 * Compact brain-provider switcher for the debug view. Pulls the list
 * dynamically from /api/providers — no more hardcoded names.
 */
export function ProviderSwitcher() {
  const t = useT();
  const { providers, refetch } = useProviders();
  const brainProviders = providers.filter(
    (p) => p.tier === "brain" && p.brain_switchable !== false,
  );
  const active = brainProviders.find((p) => p.active) ?? brainProviders[0];
  const [target, setTarget] = useState(active?.id ?? "");
  const [pending, setPending] = useState(false);
  const pushToast = useEventStore((s) => s.pushToast);

  if (!brainProviders.length) {
    return (
      <p className="text-xs text-muted-foreground">
        {t("provider_switcher.loading_hint")}
      </p>
    );
  }

  const choice = target || active?.id || brainProviders[0].id;

  async function handleApply() {
    if (!choice) return;
    setPending(true);
    try {
      await switchBrainProvider(choice);
      pushToast("success", fill(t("provider_switcher.switched_toast"), { provider: choice }));
      window.dispatchEvent(new CustomEvent("jarvis:brain-switched"));
      refetch();
    } catch (e) {
      pushToast("error", (e as Error).message);
    } finally {
      setPending(false);
    }
  }

  return (
    <div className="space-y-3">
      <label className="text-xs text-muted-foreground">
        {t("provider_switcher.active_brain")}
      </label>
      <BrandedSelect
        value={choice}
        onValueChange={setTarget}
        ariaLabel={t("provider_switcher.active_brain")}
        options={brainProviders.map((provider) => ({
          value: provider.id,
          label: `${provider.label}${
            provider.configured
              ? ""
              : ` — ${t("provider_switcher.no_credential")}`
          }`,
          disabled: !provider.configured,
        }))}
      />
      <Button onClick={handleApply} disabled={pending} className="w-full">
        {pending ? t("provider_switcher.switching") : t("common.apply")}
      </Button>
      {active && (
        <p className="text-xs text-muted-foreground">
          {t("provider_switcher.active")}: <code className="font-mono">{active.id}</code>
        </p>
      )}
    </div>
  );
}