Skip to content

πŸ™Œ OpenHands: AI-Driven Development

7.5 KB
Raw Blame
import React from "react";
import { useTranslation } from "react-i18next";
import { AxiosError } from "axios";
import { Check } from "lucide-react";
import { BrandButton } from "#/components/features/settings/brand-button";
import { I18nKey } from "#/i18n/declaration";
import { cn } from "#/utils/utils";
import { useSaveSettings } from "#/hooks/mutation/use-save-settings";
import {
  ACP_PROVIDER_FALLBACK_ICON,
  ACP_PROVIDERS,
  buildAcpAgentSettingsDiff,
  getAcpProvidersForBackend,
} from "#/constants/acp-providers";
import type { BackendKind } from "#/api/backend-registry/types";
import { useActiveBackend } from "#/contexts/active-backend-context";
import {
  AgentBrandIcon,
  type AgentBrandIconKind,
} from "#/components/shared/agent-brand-icon";
import {
  displayErrorToast,
  displaySuccessToast,
} from "#/utils/custom-toast-handlers";
import { retrieveAxiosErrorMessage } from "#/utils/retrieve-axios-error-message";

export type OnboardingAgentId =
  | "openhands"
  | "claude-code"
  | "codex"
  | "gemini-cli"
  | "pi"
  | "opencode";

function getAgentOptionIcon(id: string): AgentBrandIconKind {
  if (id === "openhands") return "openhands";

  return (
    ACP_PROVIDERS.find(({ key }) => key === id)?.icon ??
    ACP_PROVIDER_FALLBACK_ICON
  );
}

export function AgentOptionIcon({ id, muted }: { id: string; muted: boolean }) {
  const icon = getAgentOptionIcon(id);

  // The OpenHands wordmark is wider than the square brand marks (24Γ—16 vs
  // 18Γ—18) and dims via opacity rather than a muted text colour β€” its paths
  // inherit ``currentColor`` so it stays white on the tile.
  if (icon === "openhands") {
    return (
      <AgentBrandIcon
        kind="openhands"
        size={16}
        className={cn("text-contrast", muted && "opacity-55")}
        data-testid="onboarding-agent-icon-openhands"
      />
    );
  }

  return (
    <AgentBrandIcon
      kind={icon}
      size={18}
      className={muted ? "text-muted" : "text-contrast"}
      data-testid={`onboarding-agent-icon-${icon}`}
    />
  );
}

interface AgentOption {
  id: OnboardingAgentId;
  label: string;
  descriptionKey: I18nKey;
}

// Onboarding tile list is *derived* from the ACP registry so adding a
// new provider (or changing a display name) only needs one edit in
// ``acp-providers.ts``. The OpenHands tile is the only synthetic
// entry β€” it isn't an ACP provider, just the canonical default.
function getAgentOptions(backendKind: BackendKind): AgentOption[] {
  return [
    {
      id: "openhands",
      label: "OpenHands",
      descriptionKey: I18nKey.ONBOARDING$AGENT_OPENHANDS_DESCRIPTION,
    },
    ...getAcpProvidersForBackend(backendKind).map<AgentOption>((provider) => ({
      id: provider.key as OnboardingAgentId,
      label: provider.display_name,
      descriptionKey: provider.description_key,
    })),
  ];
}

interface ChooseAgentStepProps {
  selectedAgentId: OnboardingAgentId;
  onSelect: (agentId: OnboardingAgentId) => void;
  onBack?: () => void;
  onNext: () => void;
}

export function ChooseAgentStep({
  selectedAgentId,
  onSelect,
  onBack,
  onNext,
}: ChooseAgentStepProps) {
  const { t } = useTranslation("openhands");
  const { mutate: saveSettings, isPending: isSaving } = useSaveSettings();
  const backendKind = useActiveBackend().backend.kind;
  const options = getAgentOptions(backendKind);
  const isSelectionOffered = options.some(({ id }) => id === selectedAgentId);

  // Switching to a backend that can't run the chosen agent (e.g. back to the
  // backend step, then to Cloud) must not leave a hidden tile selected.
  React.useEffect(() => {
    if (!isSelectionOffered) onSelect("openhands");
  }, [isSelectionOffered, onSelect]);

  const handleNext = () => {
    // The diff builder seeds the preferred default model (Vertex-safe for
    // Gemini) when none is passed.
    const diff = buildAcpAgentSettingsDiff(selectedAgentId);
    if (!diff) {
      // Unknown id (shouldn't be reachable through the UI). Advance
      // without writing β€” better to show the next step than block the
      // user behind a silent no-op.
      onNext();
      return;
    }

    saveSettings(
      { agent_settings_diff: diff },
      {
        onError: (error) => {
          const message = retrieveAxiosErrorMessage(error as AxiosError);
          displayErrorToast(message || t(I18nKey.ERROR$GENERIC));
        },
        onSuccess: () => {
          displaySuccessToast(t(I18nKey.SETTINGS$SAVED));
          onNext();
        },
      },
    );
  };

  return (
    <div
      data-testid="onboarding-step-choose-agent"
      className="flex flex-col gap-6"
    >
      <header className="flex flex-col gap-2">
        <h2 className="text-2xl font-medium text-contrast">
          {t(I18nKey.ONBOARDING$AGENT_TITLE)}
        </h2>
        <p className="text-sm text-muted">
          {t(I18nKey.ONBOARDING$AGENT_SUBTITLE)}
        </p>
      </header>

      <div
        role="radiogroup"
        aria-label={t(I18nKey.ONBOARDING$AGENT_TITLE)}
        className="flex flex-col gap-3"
      >
        {options.map((option) => {
          const isSelected = option.id === selectedAgentId;
          return (
            <button
              key={option.id}
              type="button"
              role="radio"
              aria-checked={isSelected}
              data-testid={`onboarding-agent-option-${option.id}`}
              data-selected={isSelected}
              onClick={() => onSelect(option.id)}
              className={cn(
                "flex items-start justify-between gap-4 rounded-xl border px-4 py-3 text-left transition-colors cursor-pointer",
                isSelected
                  ? "border-contrast/45 bg-contrast/[0.09] shadow-none hover:border-contrast/45 hover:bg-contrast/[0.09]"
                  : "border-contrast/30 bg-contrast/5 hover:border-contrast/40 hover:bg-contrast/[0.08]",
              )}
            >
              <div className="flex min-w-0 flex-1 flex-col gap-1">
                <div className="flex min-w-0 items-center gap-2">
                  <AgentOptionIcon id={option.id} muted={false} />
                  <span className="truncate text-base font-normal text-contrast">
                    {option.label}
                  </span>
                </div>
                <span className="text-xs text-muted">
                  {t(option.descriptionKey)}
                </span>
              </div>
              <div className="flex shrink-0 flex-col items-end gap-1">
                {isSelected ? (
                  <Check
                    width={18}
                    height={18}
                    className="mt-1 shrink-0 text-contrast"
                    aria-hidden
                  />
                ) : null}
              </div>
            </button>
          );
        })}
      </div>

      <div
        className={cn(
          "sticky bottom-0 flex items-center gap-2 bg-base-secondary pt-4 pb-7",
          onBack ? "justify-between" : "justify-end",
        )}
      >
        {onBack ? (
          <BrandButton
            testId="onboarding-agent-back"
            type="button"
            variant="secondary"
            onClick={onBack}
            isDisabled={isSaving}
          >
            {t(I18nKey.ONBOARDING$BACK)}
          </BrandButton>
        ) : null}
        <BrandButton
          testId="onboarding-agent-next"
          type="button"
          variant="primary"
          isDisabled={isSaving}
          onClick={handleNext}
        >
          {isSaving ? t(I18nKey.SETTINGS$SAVING) : t(I18nKey.ONBOARDING$NEXT)}
        </BrandButton>
      </div>
    </div>
  );
}