Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

11.8 KB
Raw Blame
// 供给方式 card for a backend detail page (frame 02). Two radio options — 中枢模式
// Hub (推荐·默认; supply managed on the Models page, native config untouched) and
// 直连模式 Direct (legacy behavior preserved). When a native config is detected in
// Direct mode, a strip offers a one-click 导入中枢 into the migration dialog.
// Switches mode via PATCH /agents/<backend>/mode (never silent, plan §4).
import * as React from 'react';
import { Link } from 'react-router-dom';
import { ArrowDownToLine, ArrowRight, CheckCircle2, Info } from 'lucide-react';
import { useTranslation } from 'react-i18next';

import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import { cn } from '@/lib/utils';
import { getBackendUiMeta } from '@/lib/agentBackends';
import { createAgentCollectionReadAuthority } from './collectionReadAuthority';
import { useToast } from '@/context/ToastContext';
import { modelsApi } from './modelsApi';
import { MigrationDialog } from './MigrationDialog';
import { resumeGatewayAdoption } from './gatewayAdoption';
import { connectOutcome, isSupplyWarning } from './sufficiency';
import type { AgentBackend, AgentMode, AgentSupply, MigrationItem } from './types';

const RadioDot: React.FC<{ selected: boolean }> = ({ selected }) => (
  <span
    className={cn(
      'mt-0.5 flex size-[18px] shrink-0 items-center justify-center rounded-full border-2 transition-colors',
      selected ? 'border-mint' : 'border-border-strong',
    )}
  >
    {selected && <span className="size-2 rounded-full bg-mint" />}
  </span>
);

const OptionCard: React.FC<{
  selected: boolean;
  disabled?: boolean;
  onSelect: () => void;
  title: React.ReactNode;
  description: string;
  headerRight?: React.ReactNode;
  children?: React.ReactNode;
}> = ({ selected, disabled, onSelect, title, description, headerRight, children }) => (
  <div
    role="radio"
    aria-checked={selected}
    tabIndex={disabled ? -1 : 0}
    onClick={() => !disabled && onSelect()}
    onKeyDown={(e) => {
      if (disabled) return;
      // Only act when the radio row itself is focused — Enter/Space on a nested
      // control (the Models-page link / Import button) must drive that control,
      // not switch the supply mode.
      if (e.target !== e.currentTarget) return;
      if (e.key === ' ' || e.key === 'Enter') {
        e.preventDefault();
        onSelect();
      }
    }}
    className={cn(
      'flex cursor-pointer flex-col gap-3 rounded-xl border p-4 transition-colors',
      selected ? 'border-mint/50 bg-mint-soft/40' : 'border-border hover:border-border-strong',
      disabled && 'cursor-default opacity-70',
    )}
  >
    <div className="flex items-start gap-3">
      <RadioDot selected={selected} />
      <div className="flex min-w-0 flex-1 flex-col gap-1.5">
        <div className="flex items-center justify-between gap-3">
          <div className="flex items-center gap-2">{title}</div>
          {headerRight}
        </div>
        <p className="text-[13px] leading-relaxed text-muted">{description}</p>
      </div>
    </div>
    {children}
  </div>
);

export const BackendSupplyModeCard: React.FC<{ backend: AgentBackend }> = ({ backend }) => {
  const { t } = useTranslation();
  const { showToast } = useToast();
  const supportsDirect = getBackendUiMeta(backend).capabilities.supports_cli;

  const [agent, setAgent] = React.useState<AgentSupply | null>(null);
  const [detected, setDetected] = React.useState<MigrationItem[]>([]);
  const [switching, setSwitching] = React.useState<AgentMode | null>(null);
  /** Why the migration dialog is open: offered on the way to the gateway, or
   *  asked for from the Direct strip. Only the first switches when declined. */
  const [migrateOpen, setMigrateOpen] = React.useState<'switch' | 'import' | null>(null);
  const [agentReads] = React.useState(() => createAgentCollectionReadAuthority(modelsApi));
  const aliveRef = React.useRef(true);
  React.useEffect(() => {
    aliveRef.current = true;
    return () => {
      aliveRef.current = false;
    };
  }, []);

  const load = React.useCallback(async () => {
    try {
      const agents = await agentReads.readValue();
      if (aliveRef.current) setAgent(agents.find((a) => a.backend === backend) ?? null);
    } catch {
      /* card simply stays hidden if the hub is unreachable */
    }
  }, [agentReads, backend]);

  const scan = React.useCallback(async () => {
    if (!supportsDirect) return;
    try {
      const s = await modelsApi.scanMigration();
      if (aliveRef.current) setDetected(s.items.filter((i) => i.backend === backend));
    } catch {
      /* detect strip is best-effort */
    }
  }, [backend, supportsDirect]);

  React.useEffect(() => {
    void load();
    void scan();
  }, [load, scan]);

  const setMode = async (mode: AgentMode, offerMigration = true) => {
    if (!supportsDirect || !agent || agent.mode === mode || switching) return;
    setSwitching(mode);
    try {
      const next = mode === 'hub'
        ? await (async () => {
            if (offerMigration) {
              const result = await resumeGatewayAdoption(modelsApi, agentReads, backend);
              if (!result.ok) throw new Error(result.failure.reason);
              if (result.candidates.length > 0) {
                if (aliveRef.current) setMigrateOpen('switch');
                return null;
              }
            }
            return modelsApi.setAgentMode(backend, 'hub');
          })()
        : await modelsApi.setAgentMode(backend, mode);
      if (!aliveRef.current || !next) return;
      setAgent(next);
      if (mode === 'direct') {
        showToast(t('settings.models.supplyMode.switchedDirect') as string, 'success');
      } else {
        // Same rule as the Models page's 接入中枢 button, and the same strings for
        // every non-ok outcome — this surface used to keep its own paraphrase of
        // 「still on Direct」, which is how one wrong promise became two.
        // `null` sources: this card holds no source inventory, so a null grade is
        // 「did not check」 (indeterminate) rather than 「fine」. It falls to the
        // neutral 已切换到中枢 — a statement about the mode, which is the only thing
        // this surface actually observed.
        const outcome = connectOutcome(next, null);
        if (outcome === 'failed') showToast(t('settings.models.supplyMode.switchFailed') as string, 'error');
        else if (isSupplyWarning(outcome)) showToast(t(`settings.models.supply.${outcome}`) as string, 'warning');
        else showToast(t('settings.models.supplyMode.switchedHub') as string, 'success');
      }
    } catch {
      if (aliveRef.current) showToast(t('settings.models.supplyMode.switchFailed') as string, 'error');
    } finally {
      if (aliveRef.current) setSwitching(null);
    }
  };

  if (!agent) return null;

  const mode = agent.mode;
  const hubOutcome = connectOutcome(agent, null);
  // The strip advertises an actionable import row. A blocked-only native
  // configuration is still handled by the Direct → Hub migration dialog.
  const importable = detected.filter((i) => i.proposed_action === 'import');
  const detectItem = importable.find((i) => i.kind === 'api_key' || i.kind === 'opencode_provider') ?? importable[0] ?? null;

  return (
    <Card>
      <CardContent className="flex flex-col gap-4 p-6">
        <div className="flex items-center justify-between gap-3">
          <h2 className="text-[15px] font-semibold text-foreground">{t('settings.models.supplyMode.title')}</h2>
          <Badge variant={mode === 'hub' ? 'success' : 'secondary'} className="gap-1.5 rounded-full px-3 py-1.5 text-[12px]">
            {mode === 'hub' && <CheckCircle2 className="size-3.5" />}
            {t(mode === 'hub' ? 'settings.models.supplyMode.currentHub' : 'settings.models.supplyMode.currentDirect')}
          </Badge>
        </div>

        <OptionCard
          selected={mode === 'hub'}
          disabled={switching !== null}
          onSelect={() => void setMode('hub')}
          title={
            <>
              <span className="text-[15px] font-semibold text-foreground">{t('settings.models.supplyMode.hub.title')}</span>
              <Badge variant="success" className="rounded-md px-2 py-0.5 text-[10px] font-semibold">
                {t('settings.models.supplyMode.hub.recommended')}
              </Badge>
            </>
          }
          headerRight={
            <Link
              to="/settings/models"
              onClick={(e) => e.stopPropagation()}
              className="model-hub-action-mint inline-flex shrink-0 items-center gap-1 text-[13px] font-medium transition-colors"
            >
              {t('settings.models.supplyMode.hub.openModels')}
              <ArrowRight className="size-3.5" />
            </Link>
          }
          description={t('settings.models.supplyMode.hub.description', {
            backend: getBackendUiMeta(backend).label,
          }) as string}
        >
          {/* The banner asks the supply question. `fixHint` is appended only
              where a human can actually do something; a cooling source needs
              no instructions. */}
          {mode === 'hub' && isSupplyWarning(hubOutcome) && (
            <div className="flex items-start gap-2 rounded-lg border border-gold/40 bg-gold/[0.08] px-3.5 py-2.5 text-[12px] leading-relaxed text-foreground">
              <Info className="model-hub-ink-gold mt-0.5 size-3.5 shrink-0" />
              <span>
                {hubOutcome === 'noSources' || hubOutcome === 'interrupted'
                  ? t('settings.models.supplyMode.warningWithFix', {
                    warning: t(`settings.models.supply.${hubOutcome}`),
                    fixHint: t('settings.models.supplyMode.fixHint'),
                  })
                  : t(`settings.models.supply.${hubOutcome}`)}
              </span>
            </div>
          )}
        </OptionCard>

        {supportsDirect && <OptionCard
          selected={mode === 'direct'}
          disabled={switching !== null}
          onSelect={() => void setMode('direct')}
          title={<span className="text-[15px] font-semibold text-foreground">{t('settings.models.supplyMode.direct.title')}</span>}
          description={t('settings.models.supplyMode.direct.description', {
            backend: getBackendUiMeta(backend).label,
          }) as string}
        >
          {mode === 'direct' && detectItem && (
            <div className="flex items-center justify-between gap-3 rounded-lg border border-gold/40 bg-gold/[0.08] px-3.5 py-2.5">
              <span className="flex min-w-0 items-center gap-2 text-[12px] leading-relaxed text-foreground">
                <Info className="model-hub-ink-gold size-3.5 shrink-0" />
                <span className="truncate">
                  {t('settings.models.supplyMode.direct.detected', { detail: detectItem.masked_detail })}
                </span>
              </span>
              <Button
                variant="outline"
                size="sm"
                className="shrink-0"
                onClick={(e) => {
                  e.stopPropagation();
                  setMigrateOpen('import');
                }}
              >
                <ArrowDownToLine className="size-3.5" />
                {t('settings.models.supplyMode.direct.import')}
              </Button>
            </div>
          )}
        </OptionCard>}
      </CardContent>

      {/* Migration is optional: declining it still makes the requested switch. */}
      {supportsDirect && <MigrationDialog
        open={migrateOpen !== null}
        eligible={(item) => item.backend === backend}
        onDecline={migrateOpen === 'switch' ? () => void setMode('hub', false) : undefined}
        onClose={() => setMigrateOpen(null)}
        onApplied={() => {
          setMigrateOpen(null);
          void load();
          void scan();
        }}
      />}
    </Card>
  );
};