Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

8.6 KB
Raw Blame
import { useCallback, useEffect, useRef, useState } from 'react';

import { useT } from '../../../lib/i18n/I18nContext';
import { callCoreRpc } from '../../../services/coreRpcClient';
import { openUrl } from '../../../utils/openUrl';

// Connect/disconnect surface for OpenAI "Sign in with ChatGPT" (inference
// OAuth). The Rust core owns the flow via the `openhuman.inference_openai_oauth_*`
// RPCs; this component only drives them and reflects status. The same flow
// already ships in onboarding (`ApiKeysStep`); this brings it to AI settings so
// users can connect, re-check, or disconnect after onboarding.
//
// The flow is intentionally two-step (start -> user signs in -> paste the
// loopback redirect URL -> complete) because the system browser redirects to a
// local `http://127.0.0.1:1455/` callback the desktop app cannot read directly.

type OpenAiOAuthStatus = { connected: boolean; authMethod?: string | null };

interface OpenAiOAuthConnectProps {
  /** Prefix for `data-testid`s so onboarding and settings stay distinguishable. */
  testIdPrefix?: string;
  /** Notified whenever the connected state is established or cleared. */
  onConnectedChange?: (connected: boolean) => void;
  /** Runs only after the user explicitly completes the OAuth round-trip. */
  onCompleted?: () => Promise<void> | void;
  /** Runs after OAuth credentials are explicitly disconnected. */
  onDisconnected?: () => Promise<void> | void;
  /** When true, expose a "Disconnect" control once connected. */
  allowDisconnect?: boolean;
}

const OpenAiOAuthConnect = ({
  testIdPrefix = 'openai-oauth',
  onConnectedChange,
  onCompleted,
  onDisconnected,
  allowDisconnect = false,
}: OpenAiOAuthConnectProps) => {
  const { t } = useT();
  const [connected, setConnected] = useState(false);
  const [busy, setBusy] = useState(false);
  const [awaitingCallback, setAwaitingCallback] = useState(false);
  const [callbackUrl, setCallbackUrl] = useState('');
  const [error, setError] = useState<string | null>(null);
  const connectedRef = useRef(false);
  const statusRevision = useRef(0);
  const onConnectedChangeRef = useRef(onConnectedChange);
  onConnectedChangeRef.current = onConnectedChange;

  const applyConnected = useCallback((next: boolean) => {
    if (connectedRef.current === next) return;
    connectedRef.current = next;
    setConnected(next);
    onConnectedChangeRef.current?.(next);
  }, []);

  const refreshStatus = useCallback(async () => {
    const revision = statusRevision.current;
    try {
      const res = await callCoreRpc<{ result: OpenAiOAuthStatus }>({
        method: 'openhuman.inference_openai_oauth_status',
        params: {},
      });
      console.debug('[ai-settings:openai-oauth] status check succeeded', {
        connected: Boolean(res?.result?.connected),
      });
      if (revision === statusRevision.current) {
        applyConnected(Boolean(res?.result?.connected));
      }
    } catch {
      // Status is best-effort; a failed probe must not block the connect UI.
      console.debug('[ai-settings:openai-oauth] status check failed');
    }
  }, [applyConnected]);

  useEffect(() => {
    void refreshStatus();
  }, [refreshStatus]);

  const handleStart = async () => {
    // A status probe started before this explicit action must not overwrite it.
    statusRevision.current += 1;
    setBusy(true);
    setError(null);
    try {
      const res = await callCoreRpc<{ result: { authUrl: string } }>({
        method: 'openhuman.inference_openai_oauth_start',
        params: {},
      });
      const authUrl = res?.result?.authUrl?.trim();
      if (!authUrl) {
        throw new Error('missing authUrl');
      }
      await openUrl(authUrl);
      console.debug('[ai-settings:openai-oauth] browser opened');
      setAwaitingCallback(true);
    } catch {
      console.warn('[ai-settings:openai-oauth] start failed');
      setError(t('settings.ai.openaiOauthStartError'));
    } finally {
      setBusy(false);
    }
  };

  const handleComplete = async () => {
    const callback = callbackUrl.trim();
    if (!callback) {
      setError(t('settings.ai.openaiOauthCallbackRequired'));
      return;
    }
    setBusy(true);
    setError(null);
    try {
      await callCoreRpc({
        method: 'openhuman.inference_openai_oauth_complete',
        params: { callback_url: callback },
      });
      console.debug('[ai-settings:openai-oauth] completion succeeded');
      await onCompleted?.();
      setCallbackUrl('');
      setAwaitingCallback(false);
      applyConnected(true);
    } catch {
      // RPC errors can contain the callback URL, including its authorization code.
      console.warn('[ai-settings:openai-oauth] complete failed');
      setError(t('settings.ai.openaiOauthCompleteError'));
    } finally {
      setBusy(false);
    }
  };

  const handleDisconnect = async () => {
    setBusy(true);
    setError(null);
    try {
      await callCoreRpc({ method: 'openhuman.inference_openai_oauth_disconnect', params: {} });
      console.debug('[ai-settings:openai-oauth] disconnect succeeded');
      await onDisconnected?.();
      setAwaitingCallback(false);
      setCallbackUrl('');
      applyConnected(false);
    } catch {
      console.warn('[ai-settings:openai-oauth] disconnect failed');
      setError(t('settings.ai.openaiOauthDisconnectError'));
    } finally {
      setBusy(false);
    }
  };

  return (
    <div
      data-testid={`${testIdPrefix}-section`}
      className="flex flex-col gap-2 rounded-xl border border-line bg-surface-muted p-3">
      <div className="flex flex-wrap items-center justify-between gap-2">
        <span className="text-[11px] font-semibold uppercase tracking-wide text-content-muted">
          {t('settings.ai.openaiOauthTitle')}
        </span>
        {connected ? (
          <span
            data-testid={`${testIdPrefix}-connected`}
            className="text-xs font-medium text-sage-700 dark:text-sage-300">
            {t('settings.ai.openaiOauthConnected')}
          </span>
        ) : null}
      </div>
      <p className="text-xs text-content-muted">{t('settings.ai.openaiOauthDescription')}</p>

      {connected ? (
        allowDisconnect ? (
          <button
            type="button"
            data-testid={`${testIdPrefix}-disconnect`}
            disabled={busy}
            onClick={() => void handleDisconnect()}
            className="self-start rounded-lg border border-line-strong bg-surface px-3 py-2 text-xs font-medium text-content-secondary hover:bg-surface-hover disabled:opacity-50">
            {t('settings.ai.openaiOauthDisconnect')}
          </button>
        ) : null
      ) : (
        <>
          <button
            type="button"
            data-testid={`${testIdPrefix}-connect`}
            disabled={busy}
            onClick={() => void handleStart()}
            className="self-start rounded-lg border border-primary-500 bg-primary-50 dark:bg-primary-500/10 px-3 py-2 text-sm font-medium text-primary-700 dark:text-primary-300 hover:bg-primary-100 dark:hover:bg-primary-500/20 disabled:opacity-50">
            {busy ? t('settings.ai.openaiOauthOpening') : t('settings.ai.openaiOauthConnect')}
          </button>
          {awaitingCallback ? (
            <div className="flex flex-col gap-1.5">
              <p className="text-[11px] text-content-muted">
                {t('settings.ai.openaiOauthCallbackHint')}
              </p>
              <input
                aria-label={t('settings.ai.openaiOauthCallbackPlaceholder')}
                data-testid={`${testIdPrefix}-callback-input`}
                type="text"
                autoComplete="off"
                spellCheck={false}
                placeholder={t('settings.ai.openaiOauthCallbackPlaceholder')}
                value={callbackUrl}
                onChange={e => {
                  setCallbackUrl(e.target.value);
                  setError(null);
                }}
                className="rounded-lg border border-line-strong bg-surface px-3 py-2 text-xs text-content placeholder:text-content-muted focus:border-primary-500 focus:outline-none focus:ring-1 focus:ring-primary-500"
              />
              <button
                type="button"
                data-testid={`${testIdPrefix}-complete`}
                disabled={busy}
                onClick={() => void handleComplete()}
                className="self-start text-xs font-medium text-primary-600 dark:text-primary-400 underline disabled:opacity-50">
                {t('settings.ai.openaiOauthFinish')}
              </button>
            </div>
          ) : null}
        </>
      )}

      {error ? (
        <p data-testid={`${testIdPrefix}-error`} className="text-xs font-medium text-red-600">
          {error}
        </p>
      ) : null}
    </div>
  );
};

export default OpenAiOAuthConnect;