Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

13.8 KB
Raw Blame
import { useCallback } from "react";
import { useShallow } from "zustand/react/shallow";
import { useTranslation } from "react-i18next";
import { Cloud, Key, Cpu, Network, Building2, ShieldCheck, AlertTriangle } from "../icons";
import {
  LLM_ENTERPRISE_POLICY_PROVIDER_IDS,
  LLM_POLICY_PROVIDER_IDS,
  useSettingsStore,
  selectPolicyEffectiveSettings,
  selectResolvedLLMConfig,
  setResolvedLLMConfig,
  type ResolvedLLMConfig,
} from "../../stores/settingsStore";
import { usePolicyModeOptions, usePolicySnapshot } from "../../hooks/usePolicy";
import { InferenceModeSelector } from "../ui/SettingsSection";
import type { InferenceModeOption } from "../ui/SettingsSection";
import ReasoningModelSelector from "../ReasoningModelSelector";
import EnterpriseSection from "../EnterpriseSection";
import OpenAICompatiblePanel from "../OpenAICompatiblePanel";
import { Toggle } from "../ui/toggle";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../ui/select";
import type { InferenceMode } from "../../types/electron";
import {
  INFERENCE_SCOPES,
  type InferenceScope,
  type InferenceScopeDefinition,
} from "../../config/inferenceScopes";
import { enterpriseProviderName } from "../../models/ModelRegistry";
import { AUTO_REASONING_EFFORT, isReasoningEffortSupported } from "../../models/reasoningEffort";
import { selectableReasoningEffort } from "../../services/ai/chatRequestBody";
import { offersThinkingToggle } from "../../services/ai/thinkingSuppression";
import { useManagedScopeResolution } from "../../stores/enterpriseIdentityStore";
import { modeSwitchPatch } from "../../utils/llmModeSwitch";
import { requestSignIn } from "../../utils/requestSignIn";
import TestConnectionButton from "../TestConnectionButton";
import { getEnterpriseCallSettings } from "../../services/ai/enterpriseSettings";
import { Button } from "../ui/button";

// Radix Select can't hold an empty value, so Auto ("") gets a sentinel.
const AUTO_REASONING_EFFORT_VALUE = "__auto__";

interface InferenceConfigEditorProps {
  scope: InferenceScope;
  onModeChange?: (mode: InferenceMode) => void;
  /** Restrict the selectable modes (e.g. vision override offers cloud/BYOK only). */
  allowedModes?: InferenceMode[];
}

export default function InferenceConfigEditor({
  scope,
  onModeChange,
  allowedModes,
}: InferenceConfigEditorProps) {
  const { t } = useTranslation();
  const policyState = usePolicySnapshot();
  const config = useSettingsStore(
    useShallow((settings): ResolvedLLMConfig => {
      const effective = selectPolicyEffectiveSettings(settings, policyState);
      const resolved = selectResolvedLLMConfig(effective, scope);
      const definition: InferenceScopeDefinition = INFERENCE_SCOPES[scope];
      // Inherited runtime defaults are not an explicit selection in an optional picker.
      return definition.optional
        ? { ...resolved, model: effective[definition.storeKeys.model] as string }
        : resolved;
    })
  );
  const isSignedIn = useSettingsStore((s) => s.isSignedIn);
  const keepLocalModelLoaded = useSettingsStore((s) => s.keepLocalModelLoaded);
  const setKeepLocalModelLoaded = useSettingsStore((s) => s.setKeepLocalModelLoaded);
  const enterpriseSetupMode = useSettingsStore((s) => s.enterpriseSetupMode);
  const setEnterpriseSetupMode = useSettingsStore((s) => s.setEnterpriseSetupMode);
  const managed = useManagedScopeResolution(scope, enterpriseSetupMode);
  const managedAvailable = useManagedScopeResolution(scope, "managed");

  const { modes, effectiveMode, isModeAllowed } = usePolicyModeOptions<InferenceModeOption>(
    (
      [
        {
          id: "openwhispr",
          label: t("settingsPage.aiModels.modes.openwhispr"),
          description: t("settingsPage.aiModels.modes.openwhisprDesc"),
          icon: <Cloud className="w-4 h-4" />,
          disabled: !isSignedIn,
          signInRequired: !isSignedIn,
        },
        {
          id: "providers",
          label: t("settingsPage.aiModels.modes.providers"),
          description: t("settingsPage.aiModels.modes.providersDesc"),
          icon: <Key className="w-4 h-4" />,
        },
        {
          id: "local",
          label: t("settingsPage.aiModels.modes.local"),
          description: t("settingsPage.aiModels.modes.localDesc"),
          icon: <Cpu className="w-4 h-4" />,
        },
        {
          id: "self-hosted",
          label: t("settingsPage.aiModels.modes.selfHosted"),
          description: t("settingsPage.aiModels.modes.selfHostedDesc"),
          icon: <Network className="w-4 h-4" />,
        },
        {
          id: "enterprise",
          label: t("settingsPage.aiModels.modes.enterprise"),
          description: t("settingsPage.aiModels.modes.enterpriseDesc"),
          icon: <Building2 className="w-4 h-4" />,
        },
      ] as InferenceModeOption[]
    ).filter((mode) => !allowedModes || allowedModes.includes(mode.id)),
    "llm",
    config.mode,
    {
      byokProviders: LLM_POLICY_PROVIDER_IDS,
      enterpriseProviders: LLM_ENTERPRISE_POLICY_PROVIDER_IDS,
    }
  );

  const setField = useCallback(
    <K extends keyof Omit<typeof config, "scope">>(field: K) =>
      (value: NonNullable<(typeof config)[K]>) => {
        setResolvedLLMConfig(scope, { [field]: value });
      },
    [scope]
  );

  const handleModeSelect = useCallback(
    (mode: InferenceMode) => {
      if (!isModeAllowed(mode)) return;
      if (mode === "openwhispr" && !isSignedIn) {
        requestSignIn();
        return;
      }
      if (mode === effectiveMode) return;

      setResolvedLLMConfig(
        scope,
        modeSwitchPatch({ provider: config.provider, model: config.model }, mode)
      );
      onModeChange?.(mode);
    },
    [scope, config.provider, config.model, effectiveMode, isSignedIn, onModeChange, isModeAllowed]
  );

  const setMode = setField("mode");
  const setProvider = setField("provider");
  const setModel = setField("model");

  const renderModelSelector = (mode?: "cloud" | "local") => (
    <ReasoningModelSelector
      reasoningModel={config.model}
      setReasoningModel={setModel}
      localReasoningProvider={config.provider}
      setLocalReasoningProvider={setProvider}
      cloudReasoningBaseUrl={config.cloudBaseUrl ?? ""}
      setCloudReasoningBaseUrl={setField("cloudBaseUrl")}
      customReasoningApiKey={config.customApiKey ?? ""}
      setCustomReasoningApiKey={setField("customApiKey")}
      setReasoningMode={setMode}
      mode={mode}
    />
  );

  const showThinkingToggle = offersThinkingToggle(effectiveMode, config.provider, config.model);

  const effortCapability =
    effectiveMode === "providers"
      ? selectableReasoningEffort(config.provider, config.model, config, config.cloudBaseUrl)
      : null;

  if (managed.kind === "error") {
    return (
      <div className="rounded-lg border border-destructive/30 bg-destructive/5 p-3" role="alert">
        <div className="flex items-start gap-2">
          <AlertTriangle className="mt-0.5 h-4 w-4 shrink-0 text-destructive" />
          <div>
            <p className="text-sm font-medium">
              {t("settingsPage.aiModels.managedEnterprise.errorTitle")}
            </p>
            <p className="mt-0.5 text-xs text-muted-foreground">
              {managed.messageKey ? t(managed.messageKey) : managed.message}
            </p>
          </div>
        </div>
      </div>
    );
  }

  if (managed.kind === "managed") {
    return (
      <div className="space-y-3 rounded-lg border border-primary/20 bg-primary/[0.03] p-3">
        <div className="flex items-start gap-2.5">
          <div className="rounded-md bg-primary/10 p-1.5 text-primary">
            <ShieldCheck className="h-4 w-4" />
          </div>
          <div className="min-w-0 flex-1">
            <p className="text-sm font-medium">
              {t("settingsPage.aiModels.managedEnterprise.title")}
            </p>
            <p className="mt-0.5 text-xs text-muted-foreground">
              {enterpriseProviderName(managed.provider)} ·{" "}
              <span className="font-mono">{managed.model}</span>
            </p>
            <p className="mt-1 text-xs text-muted-foreground">
              {t("settingsPage.aiModels.managedEnterprise.description")}
            </p>
          </div>
        </div>
        <div className="flex flex-wrap items-center gap-2 border-t border-border/70 pt-3">
          <TestConnectionButton
            provider={managed.provider}
            getConfig={() => ({
              ...getEnterpriseCallSettings(managed.provider, scope),
              model: managed.model,
            })}
          />
          {managed.mode !== "managed_required" && managed.allowManualSetup && (
            <Button
              type="button"
              variant="ghost"
              size="sm"
              onClick={() => setEnterpriseSetupMode("manual")}
            >
              {t("settingsPage.aiModels.managedEnterprise.usePersonalSetup")}
            </Button>
          )}
        </div>
      </div>
    );
  }

  return (
    <div className="space-y-3">
      {enterpriseSetupMode === "manual" && managedAvailable.kind === "managed" && (
        <div className="flex items-center justify-between gap-3 rounded-lg border bg-muted/30 p-3">
          <div className="min-w-0">
            <p className="text-sm font-medium">
              {t("settingsPage.aiModels.managedEnterprise.availableTitle")}
            </p>
            <p className="text-xs text-muted-foreground">
              {t("settingsPage.aiModels.managedEnterprise.availableDescription", {
                provider: enterpriseProviderName(managedAvailable.provider),
              })}
            </p>
          </div>
          <Button
            type="button"
            variant="outline"
            size="sm"
            className="shrink-0"
            onClick={() => setEnterpriseSetupMode("managed")}
          >
            {t("settingsPage.aiModels.managedEnterprise.useManaged")}
          </Button>
        </div>
      )}
      {modes.length > 1 && (
        <InferenceModeSelector
          modes={modes}
          activeMode={effectiveMode}
          onSelect={handleModeSelect}
        />
      )}

      {effectiveMode === "providers" && renderModelSelector("cloud")}
      {effectiveMode === "local" && renderModelSelector("local")}

      {effectiveMode === "self-hosted" && (
        <OpenAICompatiblePanel
          baseUrl={config.remoteUrl ?? ""}
          setBaseUrl={setField("remoteUrl")}
          apiKey={config.customApiKey ?? ""}
          setApiKey={setField("customApiKey")}
          model={config.model}
          setModel={setModel}
          baseUrlPlaceholder="http://192.168.1.126:11434/v1"
          helpExamples={
            <p className="text-xs text-muted-foreground">
              {t("reasoning.selfHosted.endpointHelp")}
            </p>
          }
        />
      )}

      {showThinkingToggle && (
        <div className="flex items-start justify-between gap-3 pt-1">
          <div className="flex-1 min-w-0">
            <h4 className="text-sm font-medium text-foreground">
              {t("reasoning.disableThinking.label")}
            </h4>
            <p className="text-xs text-muted-foreground">{t("reasoning.disableThinking.help")}</p>
          </div>
          <Toggle checked={config.disableThinking} onChange={setField("disableThinking")} />
        </div>
      )}

      {effortCapability && (
        <div className="flex items-start justify-between gap-3 pt-1">
          <div className="flex-1 min-w-0">
            <h4 className="text-sm font-medium text-foreground">{t("reasoning.effort.title")}</h4>
            <p className="text-xs text-muted-foreground">
              {effortCapability.defaultValue
                ? t("reasoning.effort.descriptionWithDefault", {
                    value: t(`reasoning.effort.options.${effortCapability.defaultValue}`),
                  })
                : t("reasoning.effort.description")}
            </p>
          </div>
          <Select
            value={
              isReasoningEffortSupported(effortCapability, config.reasoningEffort)
                ? config.reasoningEffort
                : AUTO_REASONING_EFFORT_VALUE
            }
            onValueChange={(value) =>
              setField("reasoningEffort")(
                value === AUTO_REASONING_EFFORT_VALUE ? AUTO_REASONING_EFFORT : value
              )
            }
          >
            <SelectTrigger className="h-7 w-44 shrink-0 text-xs rounded-lg px-2.5 [&>svg]:h-3 [&>svg]:w-3">
              <SelectValue />
            </SelectTrigger>
            <SelectContent>
              <SelectItem
                value={AUTO_REASONING_EFFORT_VALUE}
                className="text-xs py-1.5 ps-2.5 pe-7 rounded-md"
              >
                {t("reasoning.effort.auto")}
              </SelectItem>
              {effortCapability.options.map((effort) => (
                <SelectItem
                  key={effort}
                  value={effort}
                  className="text-xs py-1.5 ps-2.5 pe-7 rounded-md"
                >
                  {t(`reasoning.effort.options.${effort}`)}
                </SelectItem>
              ))}
            </SelectContent>
          </Select>
        </div>
      )}

      {effectiveMode === "local" && (
        <div className="flex items-start justify-between gap-3 pt-1">
          <div className="flex-1 min-w-0">
            <h4 className="text-sm font-medium text-foreground">
              {t("reasoning.keepModelLoaded.label")}
            </h4>
            <p className="text-xs text-muted-foreground">{t("reasoning.keepModelLoaded.help")}</p>
          </div>
          <Toggle checked={keepLocalModelLoaded} onChange={setKeepLocalModelLoaded} />
        </div>
      )}

      {effectiveMode === "enterprise" && (
        <EnterpriseSection
          currentProvider={config.provider}
          reasoningModel={config.model}
          setReasoningModel={setModel}
          setLocalReasoningProvider={setProvider}
        />
      )}
    </div>
  );
}