Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

16.0 KB
Raw Blame
/**
 * A single remote host's page (CHOO-1809).
 *
 * A host is not a settings row — it has an ongoing lifecycle: reach it, work
 * out what it needs, install those things one at a time, and later manage the
 * agent types running on it. It gets its own route so that lifecycle has
 * somewhere to live and somewhere to grow.
 *
 * Presented as a catalogue of what is on the host rather than a checklist of
 * steps: prerequisites and agent types, each a row with its status, each
 * opening a sheet with the detail and the actions. The same language the
 * agents settings page uses, so a host reads like the rest of the product.
 *
 * The page does no probing of its own on open. Reachability comes from the
 * central model (CHOO-1682/1780) and the setup plan is pushed from the main
 * process, so opening this page costs nothing and cannot disagree with the
 * rest of the app.
 */

import { useQuery } from '@tanstack/react-query';
import { ArrowLeft, RefreshCw } from 'lucide-react';
import { observer } from 'mobx-react-lite';
import { useEffect, useMemo, useRef, useState } from 'react';
import type { GuardResult, ViewDefinition } from '@renderer/app/view-registry';
import { PageHeader } from '@renderer/lib/components/page-header';
import { ProviderConnectionStatus } from '@renderer/lib/components/provider-connection-status';
import { failureText } from '@renderer/lib/errors/describe-failure';
import { rpc } from '@renderer/lib/ipc';
import { useNavigate, useParams } from '@renderer/lib/layout/navigation-provider';
import { Button } from '@renderer/lib/ui/button';
import { Spinner } from '@renderer/lib/ui/spinner';
import { StatusBadge } from '@renderer/lib/ui/status-badge';
import {
  asAgentProviderId,
  isValidProviderId,
} from '@shared/core/providers/agent-provider-registry';
import { deriveHostStatus } from '@shared/core/remote-hosts/host-status';
import { isHostBlocked } from '@shared/core/remote-hosts/reachability';
import { switchServersStore } from '../../switch-servers/switch-servers-store';
import { workspacesStore } from '../../workspaces/workspaces-store';
import { hostReachabilityStore } from '../host-reachability-store';
import { hostSetupStore } from '../host-setup-store';
import { HostUnreachablePanel } from '../host-unreachable-panel';
import { LoadExistingAgentsSection } from '../load-existing-agents-section';
import { SetupDetailSheet, type SheetTarget } from '../setup/setup-detail-sheet';
import {
  AgentTypeRowItem,
  PrerequisiteIcon,
  PrerequisiteRow,
  SectionLabel,
} from '../setup/setup-rows';
import { groupPlanSteps, needsFirstCheck } from '../setup/step-presentation';
import {
  useHostSetupPlan,
  useInstallSetupStep,
  usePrepareSetup,
  useRecheckSetup,
  useRecheckSetupStep,
  useSkipSetupStep,
  useUpdateSetupStep,
} from '../setup/use-host-setup';
import { consumeJustAddedHost, REMOTE_HOSTS_QUERY_KEY } from './remote-hosts-view';

function useSshHost(): string {
  return useParams('remoteHost').params.sshHost;
}

export const RemoteHostMainPanel = observer(function RemoteHostMainPanel() {
  const sshHost = useSshHost();
  const [justAdded] = useState(() => consumeJustAddedHost(sshHost));
  const { navigate } = useNavigate();

  const hosts = useQuery({
    queryKey: REMOTE_HOSTS_QUERY_KEY,
    queryFn: () => rpc.remoteHosts.listHosts(),
  });
  const host = hosts.data?.find((candidate) => candidate.sshHost === sshHost);

  const plan = useHostSetupPlan(sshHost);
  const prepare = usePrepareSetup(sshHost);
  const recheck = useRecheckSetup(sshHost);
  const skip = useSkipSetupStep(sshHost);
  const installStep = useInstallSetupStep(sshHost);
  const recheckStep = useRecheckSetupStep(sshHost);
  const updateStep = useUpdateSetupStep(sshHost);

  const [sheetTarget, setSheetTarget] = useState<SheetTarget | null>(null);
  const reachability = hostReachabilityStore.get(sshHost);
  const blocked = isHostBlocked(reachability);

  useEffect(() => {
    void hostReachabilityStore.hydrate();
  }, []);

  // Rebuild the plan whenever the page opens, not only when none exists.
  //
  // Which steps a plan *has* comes from the local plugin registry and costs no
  // SSH at all; only their states come from the host, and those are merged
  // forward. Building only when the plan was absent meant a host onboarded
  // before an agent type shipped never grew a row for it — Codex was simply
  // missing from that host's page for good, while appearing on hosts added
  // later. Rebuilding is free, so there is no reason to skip it.
  //
  // Still gated on reachability: probing a host the model already knows is down
  // would only mislabel every prerequisite as missing.
  const { mutate: startPrepare } = prepare;
  useEffect(() => {
    if (blocked || plan.isLoading) return;
    startPrepare();
  }, [blocked, plan.isLoading, startPrepare]);

  // A step nobody has looked at yet has no state worth showing: probe the
  // host once when the page opens with any, rather than leaving every row
  // blank until someone presses Re-check. Steps already observed keep their
  // last result; re-checking those stays a deliberate click.
  const { mutate: startRecheck } = recheck;
  const autoChecked = useRef(false);
  const unobserved = needsFirstCheck(plan.data ?? null);
  useEffect(() => {
    if (autoChecked.current || blocked || !prepare.isSuccess || !unobserved) return;
    autoChecked.current = true;
    startRecheck();
  }, [blocked, prepare.isSuccess, unobserved, startRecheck]);

  // Resolve the Switch server for this host: prefer a managed server on this
  // host, then the active server, then the first available. Read the
  // observables outside useMemo so MobX tracks them and React sees them change.
  const servers = switchServersStore.servers;
  const activeServer = switchServersStore.activeServer;
  const serverId = useMemo(() => {
    const managed = servers.find((s) => s.sshHost === sshHost && s.managed);
    if (managed) return managed.id;
    return activeServer?.id ?? servers[0]?.id ?? null;
  }, [sshHost, servers, activeServer]);
  // Which of that server's workspaces the discovery below asks. `GET /agents`
  // answers for one tenant, so without it the list would be whichever workspace
  // the session last selected.
  const workspaceId = workspacesStore.idOnServerInScope(serverId);

  const status = deriveHostStatus(reachability, plan.data ?? null);
  const { prerequisites, agentTypes } = useMemo(
    () => groupPlanSteps(plan.data ?? null),
    [plan.data]
  );
  const busy =
    prepare.isPending ||
    recheck.isPending ||
    installStep.isPending ||
    recheckStep.isPending ||
    updateStep.isPending;
  const installingStepId = installStep.isPending ? (installStep.variables ?? null) : null;
  const updatingStepId = updateStep.isPending ? (updateStep.variables ?? null) : null;
  const recheckingStepId = recheckStep.isPending ? (recheckStep.variables ?? null) : null;
  const currentStepId = plan.data?.currentStepId ?? null;

  // Read inside render so mobx tracks it: the line changes several times a
  // second while an install runs, and nothing else re-renders on that.
  const activityFor = (stepId: string) => hostSetupStore.activityFor(sshHost, stepId);

  // Keep an open sheet in step with pushed plan updates, so a row's detail
  // advances while a run is in flight instead of freezing at the state it had
  // when it was opened.
  const liveTarget = useMemo((): SheetTarget | null => {
    if (!sheetTarget) return null;
    if (sheetTarget.kind === 'prerequisite') {
      const step = prerequisites.find((s) => s.id === sheetTarget.step.id);
      return step ? { kind: 'prerequisite', step } : null;
    }
    const row = agentTypes.find((r) => r.agentId === sheetTarget.row.agentId);
    return row ? { kind: 'agent-type', row } : null;
  }, [sheetTarget, prerequisites, agentTypes]);

  // The workspace gives every main panel a fixed, `overflow-hidden` box, so a
  // view that does not scroll itself simply clips. This page grows without
  // bound — the gh device-flow terminal alone is tall enough to push the agent
  // types past the bottom of the window with no way to reach them.
  return (
    <div className="relative z-10 flex min-h-0 flex-1 flex-col overflow-auto bg-background">
      <div className="space-y-6 px-8 pb-10">
        <PageHeader
          sticky
          title={host?.name ?? sshHost}
          description={`Remote host · ${sshHost}. Auth uses your SSH agent — Switch Console stores no credentials.`}
          back={
            <Button
              size="sm"
              variant="ghost"
              className="-ml-2"
              onClick={() => navigate('settings', { tab: 'remote-hosts' })}
            >
              <ArrowLeft className="size-4" /> All hosts
            </Button>
          }
        >
          <div className="flex items-center gap-2">
            {!blocked && (
              <span className="flex items-center gap-2">
                <StatusBadge tone={status.tone}>{status.label}</StatusBadge>
                {status.readinessKnown && status.total > 0 && (
                  <span className="text-xs text-foreground-muted">
                    {status.done} of {status.total} prerequisites
                  </span>
                )}
              </span>
            )}
            {!blocked && (
              <Button
                size="sm"
                variant="outline"
                disabled={busy}
                onClick={() => recheck.mutate()}
                aria-label="Re-check this host"
              >
                <RefreshCw className={`size-3.5 ${recheck.isPending ? 'animate-spin' : ''}`} />
                Re-check
              </Button>
            )}
          </div>
        </PageHeader>

        {blocked ? (
          <HostUnreachablePanel reachability={reachability} />
        ) : (
          <>
            {/*
            An operation that stops because the host went away is reported as
            exactly that, rather than being blamed on whichever step it touched.
          */}
            {installStep.isError && (
              <p className="text-xs text-destructive">
                {failureText(installStep.error, 'Could not install.')}
              </p>
            )}
            {updateStep.isError && (
              <p className="text-xs text-destructive">
                {failureText(updateStep.error, 'Could not update.')}
              </p>
            )}
            {recheck.isError && (
              <p className="text-xs text-destructive">
                {failureText(recheck.error, 'Could not check this host.')}
              </p>
            )}
            {prepare.isError && (
              <p className="text-xs text-destructive">
                {failureText(prepare.error, 'Could not work out what this host needs.')}
              </p>
            )}

            {plan.isLoading ? (
              <div className="flex items-center gap-2 text-sm text-foreground-muted">
                <Spinner /> Loading…
              </div>
            ) : prerequisites.length === 0 && agentTypes.length === 0 ? (
              <p className="text-sm text-foreground-muted">
                Nothing known about this host yet. Re-check to see what it has.
              </p>
            ) : (
              <div className="flex flex-col">
                {prerequisites.length > 0 && (
                  <section>
                    <SectionLabel count={prerequisites.length}>Prerequisites</SectionLabel>
                    {prerequisites.map((step) => (
                      <div key={step.id} className="w-full py-0.5">
                        <PrerequisiteRow
                          step={step}
                          isCurrent={step.id === currentStepId}
                          installing={installingStepId === step.id}
                          updating={updatingStepId === step.id}
                          rechecking={recheckingStepId === step.id}
                          hostBusy={busy}
                          activity={activityFor(step.id)}
                          onInstall={() => installStep.mutate(step.id)}
                          onUpdate={() => updateStep.mutate(step.id)}
                          onRecheck={() => recheckStep.mutate(step.id)}
                          onOpen={() => setSheetTarget({ kind: 'prerequisite', step })}
                        />
                      </div>
                    ))}
                  </section>
                )}

                {agentTypes.length > 0 && (
                  <section className="pt-2">
                    <SectionLabel count={agentTypes.length}>Agent types</SectionLabel>
                    {agentTypes.map((row) => (
                      <div key={row.agentId} className="w-full py-0.5">
                        <AgentTypeRowItem
                          row={row}
                          currentStepId={currentStepId}
                          installingStepId={installingStepId}
                          updatingStepId={updatingStepId}
                          recheckingStepId={recheckingStepId}
                          hostBusy={busy}
                          activityFor={activityFor}
                          onInstall={(stepId) => installStep.mutate(stepId)}
                          onUpdate={(stepId) => updateStep.mutate(stepId)}
                          onRecheck={(stepId) => recheckStep.mutate(stepId)}
                          onOpen={() => setSheetTarget({ kind: 'agent-type', row })}
                          signIn={
                            // Installed is not usable: a session also needs
                            // the CLI signed in on this host.
                            !blocked && isValidProviderId(row.agentId) ? (
                              <ProviderConnectionStatus
                                providerId={asAgentProviderId(row.agentId)}
                                sshHost={sshHost}
                                dir=""
                                compact
                              />
                            ) : null
                          }
                        />
                      </div>
                    ))}
                  </section>
                )}

                {serverId && workspaceId && (
                  <LoadExistingAgentsSection
                    sshHost={sshHost}
                    serverId={serverId}
                    workspaceId={workspaceId}
                    initiallyOpen={justAdded}
                  />
                )}
              </div>
            )}

            <SetupDetailSheet
              target={liveTarget}
              sshHost={sshHost}
              icon={
                liveTarget?.kind === 'prerequisite' ? (
                  <PrerequisiteIcon step={liveTarget.step} size={24} />
                ) : null
              }
              activityFor={activityFor}
              onClose={() => setSheetTarget(null)}
              onInstall={(stepId) => installStep.mutate(stepId)}
              installingStepId={installingStepId}
              hostBusy={busy}
              onSkip={(stepId) => skip.mutate(stepId)}
              skippingStepId={skip.isPending ? (skip.variables ?? null) : null}
            />
          </>
        )}
      </div>
    </div>
  );
});

export const remoteHostView = {
  WrapView: ({ children }: { children: React.ReactNode; sshHost: string }) => <>{children}</>,
  // No titlebar slot: the page header already names the host and repeats the
  // alias underneath it, so a third copy in the title bar was only noise.
  MainPanel: RemoteHostMainPanel,
  // A host is somewhere agents run, not something a server owns: its page says
  // the same things with no server registered as with ten.
  worksWithoutServer: true,
  canActivate: (params: unknown): GuardResult => {
    // Params can come from a snapshot written by an older build, so validate
    // rather than trust: a view with no host to show has nothing to render.
    const sshHost =
      typeof params === 'object' && params !== null
        ? (params as { sshHost?: unknown }).sshHost
        : undefined;
    if (typeof sshHost !== 'string' || sshHost.length === 0) {
      return { ok: false, redirect: 'settings', params: { tab: 'remote-hosts' } };
    }
    return { ok: true };
  },
} satisfies ViewDefinition<{ sshHost: string }>;