Skip to content

Your lightweight open-source personal agent.

11.7 KB
Raw Blame
import {
  ModelsIcon,
  WebSearchIcon,
  ImageGenerationIcon,
  VoiceIcon,
} from "@/components/icons/product-icons";
import { useContext, useEffect, type Dispatch, type SetStateAction } from "react";
import {
  ChevronRight,
  type LucideIcon,
} from "lucide-react";
import { useTranslation } from "react-i18next";

import { HostNavigationContext } from "@/components/remote/HostSwitcher";
import { StarLink } from "@/components/StarPrompt";
import { AboutSettingsView } from "./AboutSettingsView";
import { AppearanceSettingsView } from "./AppearanceSettingsView";
import { VersionCheckRowView, type VersionInfoProps } from "./VersionCheckRowView";
import { DEFAULT_TRANSCRIPTION_SETTINGS } from "@/components/settings/capabilities/TranscriptionSettings";
import type { SettingsSectionKey } from "@/components/settings/contracts";
import { settingsProviderConfigured } from "@/components/settings/shared/ModelControls";
import {
  SettingsGroup,
  SettingsSectionTitle,
} from "@/components/settings/shared/SettingsControls";
import { TokenUsageCard } from "@/components/settings/TokenUsageCard";
import { useLogoFallback } from "@/hooks/useLogoFallback";
import type { ThemePreference } from "@/hooks/useTheme";
import type {
  LocalPreferences,
} from "@/lib/local-preferences";
import { providerBrand, providerDisplayLabel } from "@/lib/provider-brand";
import type { SettingsPayload } from "@/lib/types";
import { cn } from "@/lib/utils";
import { useUpdateFlow } from "@/providers/UpdateFlowProvider";
import { useVersionCheck } from "@/providers/VersionCheckProvider";

export function OverviewSettings({
  settings,
  onSelectSection,
  showBrandLogos,
}: {
  settings: SettingsPayload;
  onSelectSection: (section: SettingsSectionKey) => void;
  showBrandLogos: boolean;
}) {
  const { t } = useTranslation();
  const tx = (key: string, fallback: string) => t(key, { defaultValue: fallback });
  const activePresetName = settings.agent.model_preset;
  const activePreset =
    activePresetName && activePresetName !== "default"
      ? settings.model_presets.find((preset) => preset.name === activePresetName)?.name ??
        activePresetName
      : null;
  const activeProvider = settings.agent.resolved_provider ?? settings.agent.provider;
  const activeProviderConfigured = settingsProviderConfigured(settings, activeProvider);
  const activeProviderLabel = providerDisplayLabel(settings.providers, activeProvider);
  const activeModelValue = activeProviderConfigured
    ? settings.agent.model
    : tx("settings.values.notConfigured", "Not configured");
  const activeModelCaption = activeProviderConfigured
    ? [activeProvider, activePreset].filter(Boolean).join(" ")
    : activeProviderLabel || settings.agent.model
      ? [activeProviderLabel, settings.agent.model].filter(Boolean).join(" ")
      : tx("settings.byok.noConfiguredProviders", "No configured providers");
  const webStatus = settings.web.enable
    ? tx("settings.values.enabled", "Enabled")
    : tx("settings.values.disabled", "Disabled");
  const imageStatus = settings.image_generation.enabled
    ? tx("settings.values.enabled", "Enabled")
    : tx("settings.values.disabled", "Disabled");
  const transcription = settings.transcription ?? DEFAULT_TRANSCRIPTION_SETTINGS;
  const voiceStatus = transcription.enabled
    ? tx("settings.values.enabled", "Enabled")
    : tx("settings.values.disabled", "Disabled");
  return (
    <div className="settings-stack">
      <section className="rounded-panel bg-settings-surface p-6">
        <TokenUsageCard usage={settings.usage} timeZone={settings.agent.timezone} />
      </section>

      <section>
        <SettingsSectionTitle>{tx("settings.sections.ai", "AI")}</SettingsSectionTitle>
        <SettingsGroup>
          <OverviewListRow
            icon={ModelsIcon}
            valueLogoProvider={activeProvider}
            title={tx("settings.overview.model", "Current model")}
            value={activeModelValue}
            caption={activeModelCaption}
            showBrandLogos={showBrandLogos}
            onClick={() => onSelectSection("models")}
          />
        </SettingsGroup>
      </section>

      <section>
        <SettingsSectionTitle>{tx("settings.sections.capabilities", "Capabilities")}</SettingsSectionTitle>
        <SettingsGroup>
          <OverviewListRow
            icon={WebSearchIcon}
            valueLogoProvider={settings.web_search.provider}
            title={tx("settings.overview.webSearch", "Web search")}
            value={webStatus}
            showBrandLogos={showBrandLogos}
            onClick={() => onSelectSection("browser")}
          />
          <OverviewListRow
            icon={ImageGenerationIcon}
            valueLogoProvider={settings.image_generation.provider}
            title={tx("settings.overview.imageGeneration", "Image generation")}
            value={imageStatus}
            showBrandLogos={showBrandLogos}
            onClick={() => onSelectSection("image")}
          />
          <OverviewListRow
            icon={VoiceIcon}
            valueLogoProvider={transcription.provider}
            title={tx("settings.overview.voiceInput", "Voice input")}
            value={voiceStatus}
            showBrandLogos={showBrandLogos}
            onClick={() => onSelectSection("voice")}
          />
        </SettingsGroup>
      </section>

    </div>
  );
}

export function AboutSettings({ settings, onRequestUpdate: requestUpdate }: { settings: SettingsPayload; onRequestUpdate?: () => void }) {
  const hostPicker = useContext(HostNavigationContext);
  const flow = useUpdateFlow();
  const onRequestUpdate = requestUpdate ?? flow.open;
  const currentVersion = settings.version?.current;
  const currentCommit = settings.version?.commit;
  const environment = settings.environment;
  const reportIssueUrl = new URL("https://github.com/HKUDS/nanobot/issues/new");
  reportIssueUrl.searchParams.set("template", "bug_report.yml");
  const reportedVersion = [currentVersion, currentCommit && `(commit ${currentCommit})`]
    .filter(Boolean).join(" ");
  if (reportedVersion) reportIssueUrl.searchParams.set("version", reportedVersion);
  reportIssueUrl.searchParams.set("channel", "WebSocket");
  if (environment) {
    reportIssueUrl.searchParams.set("python_version", environment.python_version);
    const osName = environment.os === "Darwin" ? "macOS" : environment.os;
    const osVersion = [osName, environment.os_version].filter(Boolean).join(" ");
    reportIssueUrl.searchParams.set("os", environment.architecture
      ? `${osVersion} (${environment.architecture})` : osVersion);
  }
  if (settings.agent.model) reportIssueUrl.searchParams.set("model", settings.agent.model);
  const provider = settings.agent.resolved_provider || settings.agent.provider;
  if (provider) reportIssueUrl.searchParams.set("llm_provider", provider);
  reportIssueUrl.searchParams.set("browser", navigator.userAgent);
  reportIssueUrl.searchParams.set("connection", hostPicker?.kind === "embedded"
    ? "Remote host" : hostPicker ? "Local host" : "Direct WebUI");
  return <AboutSettingsView reportIssueUrl={reportIssueUrl.href}
    versionCheck={<VersionCheckRow currentVersion={currentVersion} currentCommit={currentCommit}
      onRequestUpdate={onRequestUpdate} hasPendingUpdate={flow.pending} />}
    starLink={<StarLink />} />;
}

function VersionCheckRow({ currentVersion, currentCommit, onRequestUpdate, hasPendingUpdate }: VersionInfoProps) {
  const { checking, result, check, releaseStatus, canChangeChannel,
    changingChannel, channelError, changeChannel, installationMethod } = useVersionCheck();
  useEffect(() => { void check(); }, [check]);

  return <VersionCheckRowView currentVersion={currentVersion} currentCommit={currentCommit}
    checking={checking} result={result} onCheck={() => void check()}
    onRequestUpdate={onRequestUpdate} hasPendingUpdate={hasPendingUpdate}
    installationMethod={installationMethod}
    releaseStatus={releaseStatus} canChangeChannel={canChangeChannel} changingChannel={changingChannel}
    channelError={channelError} onChangeChannel={changeChannel} />;
}

export function AppearanceSettings({ themePreference, onChangeTheme, localPrefs, onChangeLocalPrefs }: {
  themePreference: ThemePreference;
  onChangeTheme: (value: ThemePreference) => void;
  localPrefs: LocalPreferences;
  onChangeLocalPrefs: Dispatch<SetStateAction<LocalPreferences>>;
}) {
  return <AppearanceSettingsView themePreference={themePreference} onChangeTheme={onChangeTheme}
    localPrefs={localPrefs} onChangeLocalPrefs={onChangeLocalPrefs} />;
}

function OverviewRowIcon({
  icon: Icon,
}: {
  icon: LucideIcon;
}) {
  return (
    <span className="grid h-9 w-9 shrink-0 place-items-center rounded-control bg-muted text-foreground/82 transition-colors dark:bg-muted/70">
      <Icon className="h-4 w-4" aria-hidden />
    </span>
  );
}

function OverviewValueLogo({
  provider,
  showBrandLogos,
}: {
  provider: string | null | undefined;
  showBrandLogos: boolean;
}) {
  const brand = provider ? providerBrand(provider) : null;
  const { logoUrl, logoLoaded, onLogoError, onLogoLoad } = useLogoFallback(brand?.logoUrls);
  const isLogoTile = brand?.logoLayout === "tile" && logoUrl === brand.logoUrl;

  if (!provider || !showBrandLogos || !brand) return null;

  if (logoUrl) {
    return (
      <span
        data-testid={`overview-logo-${provider}`}
        className={cn(
          "grid h-5 w-5 shrink-0 place-items-center overflow-hidden rounded-md",
          logoLoaded ? (isLogoTile ? "bg-transparent" : "bg-white") : "bg-muted",
        )}
        aria-hidden
      >
        <span
          className={cn(
            "col-start-1 row-start-1 grid h-full w-full place-items-center rounded-md text-[7.5px] font-semibold text-white",
            logoLoaded ? "opacity-0" : "opacity-100",
          )}
          style={{ backgroundColor: brand.color }}
        >
          {brand.initials}
        </span>
        <img
          src={logoUrl}
          alt=""
          decoding="async"
          loading="lazy"
          referrerPolicy="no-referrer"
          draggable={false}
          className={cn(
            "col-start-1 row-start-1 object-contain",
            isLogoTile ? "h-5 w-5" : "h-3.5 w-3.5",
            logoLoaded ? "opacity-100" : "opacity-0",
          )}
          onLoad={onLogoLoad}
          onError={onLogoError}
        />
      </span>
    );
  }

  return (
    <span
      data-testid={`overview-logo-fallback-${provider}`}
      className="grid h-5 w-5 shrink-0 place-items-center rounded-md text-[7.5px] font-semibold text-white"
      style={{ backgroundColor: brand.color }}
      aria-hidden
    >
      {brand.initials}
    </span>
  );
}

function OverviewListRow({
  icon: Icon,
  valueLogoProvider,
  title,
  value,
  caption,
  showBrandLogos = false,
  onClick,
}: {
  icon: LucideIcon;
  valueLogoProvider?: string | null;
  title: string;
  value: string;
  caption?: string;
  showBrandLogos?: boolean;
  onClick: () => void;
}) {
  return (
    <button
      type="button"
      onClick={onClick}
      className="group flex min-h-[68px] w-full select-none items-center gap-3 px-4 py-3.5 text-left transition-colors settings-hover sm:px-5"
    >
      <OverviewRowIcon icon={Icon} />
      <span className="min-w-0 flex-1">
        <span className="block text-[14px] font-medium leading-5 text-foreground">{title}</span>
        {caption ? <span className="sr-only">{caption}</span> : null}
      </span>
      <span className="ml-auto flex min-w-0 max-w-[48%] items-center gap-2">
        <OverviewValueLogo provider={valueLogoProvider} showBrandLogos={showBrandLogos} />
        <span className="truncate text-right text-[13px] leading-5 text-muted-foreground">
          {value}
        </span>
        <ChevronRight
          className="h-4 w-4 shrink-0 text-muted-foreground/60 transition-transform group-hover:translate-x-0.5"
          aria-hidden
        />
      </span>
    </button>
  );
}