Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

24.4 KB
Raw Blame
import { useState, useEffect, useRef } from 'react';
import {
  Button,
  CircleHelpIcon,
  Dropdown,
  InfoHoverCard,
  Input,
  Label,
  SecretInput,
  Spinner,
  Switch,
  ESide,
  useToastContext,
} from '@librechat/client';
import type {
  TLangfuseConnectionStatus,
  TLangfuseConnectionTestErrorCode,
} from 'librechat-data-provider';
import type { TranslationKeys } from '~/hooks';
import {
  useGetStartupConfig,
  useGetLangfuseConnectionQuery,
  useUpdateLangfuseConnectionMutation,
  useTestLangfuseConnectionMutation,
  useUpdateLangfusePromptSyncMutation,
  useSyncLangfusePromptsMutation,
} from '~/data-provider';
import LangfusePromptsDialog from './LangfusePromptsDialog';
import { getPromptErrorMessage } from '~/utils';
import { useLocalize } from '~/hooks';

type ConnectionTestState = 'idle' | 'unverified' | 'checking' | 'connected' | 'failed';

function getStoredConnectionTestKey(status?: TLangfuseConnectionStatus): string | undefined {
  if (status?.configured !== true || !status.destination || !status.publicKey) {
    return undefined;
  }

  return [status.destination, status.publicKey].join('\u0000');
}

function getConnectionStatusLabelKey(state: ConnectionTestState): TranslationKeys {
  switch (state) {
    case 'checking':
      return 'com_ui_langfuse_status_checking';
    case 'connected':
      return 'com_ui_langfuse_status_connected';
    case 'failed':
      return 'com_ui_langfuse_status_failed';
    case 'unverified':
      return 'com_ui_langfuse_status_not_verified';
    case 'idle':
    default:
      return 'com_ui_langfuse_status_not_configured';
  }
}

function getConnectionTestErrorLabelKey(
  errorCode?: TLangfuseConnectionTestErrorCode,
): TranslationKeys {
  switch (errorCode) {
    case 'invalid_credentials':
      return 'com_ui_langfuse_test_invalid_credentials';
    case 'access_denied':
      return 'com_ui_langfuse_test_access_denied';
    case 'rate_limited':
      return 'com_ui_langfuse_test_rate_limited';
    case 'server_error':
      return 'com_ui_langfuse_test_server_error';
    case 'timeout':
      return 'com_ui_langfuse_test_timeout';
    case 'missing_secret':
      return 'com_ui_langfuse_test_missing_secret';
    case 'stored_secret_unavailable':
      return 'com_ui_langfuse_test_stored_secret_unavailable';
    case 'unexpected_response':
      return 'com_ui_langfuse_test_unexpected_response';
    case 'unreachable':
    default:
      return 'com_ui_langfuse_test_error';
  }
}

function getConnectionStatusDotClass(state: ConnectionTestState): string {
  switch (state) {
    case 'connected':
      return 'bg-status-success';
    case 'failed':
      return 'bg-status-error';
    case 'checking':
      return 'bg-status-warning';
    case 'idle':
    default:
      return 'border border-border-medium';
  }
}

function getDisplayPublicKey(publicKey: string): string {
  const trimmedPublicKey = publicKey.trim();
  if (trimmedPublicKey.length <= 12) {
    return trimmedPublicKey;
  }

  return `${trimmedPublicKey.slice(0, 6)}...${trimmedPublicKey.slice(-4)}`;
}

export default function LangfuseConnection() {
  const localize = useLocalize();
  const { showToast } = useToastContext();
  const { data: startupConfig } = useGetStartupConfig();
  // The server's `POST /prompts/sync` route requires `manage:prompts` in addition to the
  // `langfuse` config capability that gates this whole tab, so an admin can see the
  // connection without being able to run the sync.
  const canManagePrompts = startupConfig?.langfusePromptManageAccess === true;
  const {
    data: status,
    isLoading: isConnectionLoading,
    isError: isConnectionError,
    isFetching: isConnectionFetching,
    refetch: refetchConnection,
  } = useGetLangfuseConnectionQuery();
  const updateMutation = useUpdateLangfuseConnectionMutation();
  const testMutation = useTestLangfuseConnectionMutation();
  const promptSyncMutation = useUpdateLangfusePromptSyncMutation();
  const syncPromptsMutation = useSyncLangfusePromptsMutation();

  const [connectionStatus, setConnectionStatus] = useState<TLangfuseConnectionStatus>();
  const [destination, setDestination] = useState('');
  const [publicKey, setPublicKey] = useState('');
  const [secretKey, setSecretKey] = useState('');
  const [isEditingPublicKey, setIsEditingPublicKey] = useState(false);
  const [isEditingSecretKey, setIsEditingSecretKey] = useState(false);
  const [connectionTestState, setConnectionTestState] = useState<ConnectionTestState>('idle');
  const [connectionTestMessage, setConnectionTestMessage] = useState('');
  const [promptSyncError, setPromptSyncError] = useState(false);
  const [isPromptsDialogOpen, setIsPromptsDialogOpen] = useState(false);
  const autoTestedConnectionRef = useRef<string>();
  const connectionTestRequestRef = useRef(0);
  const publicKeyInputRef = useRef<HTMLInputElement>(null);
  const secretKeyInputRef = useRef<HTMLInputElement>(null);

  useEffect(() => {
    if (isEditingPublicKey) {
      publicKeyInputRef.current?.focus();
    }
  }, [isEditingPublicKey]);

  useEffect(() => {
    if (isEditingSecretKey) {
      secretKeyInputRef.current?.focus();
    }
  }, [isEditingSecretKey]);

  useEffect(() => {
    if (!status) {
      return;
    }
    setConnectionStatus(status);
  }, [status]);

  // Scoped to the two fields this syncs, not the whole `connectionStatus` object,
  // so an unrelated update (e.g. toggling prompt sync) cannot reset unsaved edits
  // to the destination or public key.
  useEffect(() => {
    setDestination(connectionStatus?.destination ?? '');
    setPublicKey(connectionStatus?.publicKey ?? '');
  }, [connectionStatus?.destination, connectionStatus?.publicKey]);

  const connectionAvailable = connectionStatus?.connectionAvailable !== false;
  const secretConfigured = connectionStatus?.configured === true;
  const destinations = connectionStatus?.destinations ?? [];
  const connectionDestinationAvailable = destinations.some(
    ({ key }) => key === connectionStatus?.destination,
  );
  const storedDestinationUnavailable =
    secretConfigured && Boolean(connectionStatus?.destination) && !connectionDestinationAvailable;
  const destinationOptions = [
    ...(storedDestinationUnavailable && connectionStatus?.destination
      ? [
          {
            value: connectionStatus.destination,
            label: `${connectionStatus.destination} - ${localize(
              'com_ui_langfuse_destination_unavailable',
            )}`,
          },
        ]
      : []),
    ...destinations.map(({ key, baseUrl }) => ({
      value: key,
      label: `${key} - ${baseUrl}`,
    })),
  ];
  const trimmedPublicKey = publicKey.trim();
  const trimmedSecretKey = secretKey.trim();
  const publicKeyInputVisible = !secretConfigured || isEditingPublicKey;
  const secretInputVisible = !secretConfigured || isEditingSecretKey;
  const displayPublicKey = getDisplayPublicKey(publicKey);
  const connectionCredentialsChanged =
    destination !== (connectionStatus?.destination ?? '') ||
    trimmedPublicKey !== (connectionStatus?.publicKey ?? '');
  const hasUnsavedChanges = connectionCredentialsChanged || trimmedSecretKey !== '';
  const isEditing =
    !secretConfigured || isEditingPublicKey || isEditingSecretKey || hasUnsavedChanges;
  const canSubmit =
    destination !== '' &&
    trimmedPublicKey !== '' &&
    ((!connectionCredentialsChanged && secretConfigured) || trimmedSecretKey !== '');
  const isSavingStatus = updateMutation.isLoading || promptSyncMutation.isLoading;
  const busy = testMutation.isLoading || isSavingStatus || syncPromptsMutation.isLoading;

  useEffect(() => {
    const storedConnectionTestKey = getStoredConnectionTestKey(connectionStatus);
    if (!connectionStatus || !connectionAvailable) {
      return;
    }
    if (!storedConnectionTestKey) {
      return;
    }

    if (!connectionStatus.destinations?.some(({ key }) => key === connectionStatus.destination)) {
      connectionTestRequestRef.current += 1;
      setConnectionTestState('failed');
      setConnectionTestMessage(localize('com_ui_langfuse_destination_removed'));
      return;
    }

    if (autoTestedConnectionRef.current === storedConnectionTestKey) {
      return;
    }

    autoTestedConnectionRef.current = storedConnectionTestKey;
    const requestId = ++connectionTestRequestRef.current;
    setConnectionTestState('checking');
    testMutation.mutate(
      {
        destination: connectionStatus.destination ?? '',
        publicKey: connectionStatus.publicKey ?? '',
      },
      {
        onSuccess: (result) => {
          if (requestId !== connectionTestRequestRef.current) {
            return;
          }
          setConnectionTestState(result.success ? 'connected' : 'failed');
          setConnectionTestMessage(
            result.success ? '' : localize(getConnectionTestErrorLabelKey(result.errorCode)),
          );
        },
        onError: () => {
          if (requestId !== connectionTestRequestRef.current) {
            return;
          }
          setConnectionTestState('failed');
          setConnectionTestMessage(localize('com_ui_langfuse_test_error'));
        },
      },
    );
  }, [connectionStatus, connectionAvailable, localize, testMutation]);

  const connectionStatusLabel =
    connectionTestState === 'failed' && connectionTestMessage !== ''
      ? connectionTestMessage
      : localize(getConnectionStatusLabelKey(connectionTestState));
  const connectionStatusDotClass = getConnectionStatusDotClass(connectionTestState);
  const connectionStatusTextClass =
    connectionTestState === 'failed' ? 'text-text-destructive' : 'text-text-secondary';
  const connectionStatusTitle =
    connectionTestState === 'failed' ? localize('com_ui_langfuse_status_failed_hover') : undefined;

  const handleSave = () => {
    const payload = {
      enabled: !secretConfigured || connectionStatus?.enabled === true,
      destination,
      publicKey: trimmedPublicKey,
      ...(trimmedSecretKey ? { secretKey: trimmedSecretKey } : {}),
    };

    connectionTestRequestRef.current += 1;
    updateMutation.mutate(payload, {
      onSuccess: (nextStatus) => {
        autoTestedConnectionRef.current = getStoredConnectionTestKey(nextStatus);
        setConnectionStatus(nextStatus);
        setConnectionTestState('connected');
        setConnectionTestMessage('');
        setSecretKey('');
        setIsEditingPublicKey(false);
        setIsEditingSecretKey(false);
        showToast({ message: localize('com_ui_langfuse_saved'), status: 'success' });
      },
      onError: () => {
        setConnectionTestState('failed');
        setConnectionTestMessage(localize('com_ui_langfuse_save_error'));
        showToast({ message: localize('com_ui_langfuse_save_error'), status: 'error' });
      },
    });
  };

  const handleDestinationChange = (nextDestination: string) => {
    setDestination(nextDestination);
    const requestId = ++connectionTestRequestRef.current;
    const credentialsChanged =
      nextDestination !== (connectionStatus?.destination ?? '') ||
      trimmedPublicKey !== (connectionStatus?.publicKey ?? '');

    if (secretConfigured && credentialsChanged) {
      setIsEditingSecretKey(true);
    }

    if (
      nextDestination === '' ||
      trimmedPublicKey === '' ||
      ((!secretConfigured || credentialsChanged) && trimmedSecretKey === '')
    ) {
      setConnectionTestState(credentialsChanged ? 'unverified' : 'idle');
      setConnectionTestMessage('');
      return;
    }

    setConnectionTestState('checking');
    setConnectionTestMessage('');
    testMutation.mutate(
      {
        destination: nextDestination,
        publicKey: trimmedPublicKey,
        ...(trimmedSecretKey ? { secretKey: trimmedSecretKey } : {}),
      },
      {
        onSuccess: (result) => {
          if (requestId !== connectionTestRequestRef.current) {
            return;
          }
          setConnectionTestState(result.success ? 'connected' : 'failed');
          setConnectionTestMessage(
            result.success ? '' : localize(getConnectionTestErrorLabelKey(result.errorCode)),
          );
        },
        onError: () => {
          if (requestId !== connectionTestRequestRef.current) {
            return;
          }
          setConnectionTestState('failed');
          setConnectionTestMessage(localize('com_ui_langfuse_test_error'));
        },
      },
    );
  };

  const handleEnabledChange = () => {
    if (!secretConfigured || !connectionStatus?.destination || !connectionStatus.publicKey) {
      return;
    }

    const nextEnabled = connectionStatus.enabled !== true;
    const requestId = ++connectionTestRequestRef.current;
    const saveEnabledState = () => {
      updateMutation.mutate(
        {
          enabled: nextEnabled,
          destination: connectionStatus.destination ?? '',
          publicKey: connectionStatus.publicKey ?? '',
        },
        {
          onSuccess: (nextStatus) => {
            if (requestId !== connectionTestRequestRef.current) {
              return;
            }
            autoTestedConnectionRef.current = getStoredConnectionTestKey(nextStatus);
            setConnectionStatus(nextStatus);
            showToast({ message: localize('com_ui_langfuse_saved'), status: 'success' });
          },
          onError: () => {
            if (requestId !== connectionTestRequestRef.current) {
              return;
            }
            showToast({ message: localize('com_ui_langfuse_save_error'), status: 'error' });
          },
        },
      );
    };

    saveEnabledState();
  };

  const handlePromptSyncChange = (nextEnabled: boolean) => {
    setPromptSyncError(false);
    // The dialog unmounts once the switch is off; reset `open` so re-enabling keeps it closed.
    if (!nextEnabled) {
      setIsPromptsDialogOpen(false);
    }
    promptSyncMutation.mutate(
      { enabled: nextEnabled },
      {
        onSuccess: (nextStatus) => {
          setConnectionStatus(nextStatus);
        },
        onError: () => {
          setPromptSyncError(true);
        },
      },
    );
  };

  const handleSyncPrompts = () => {
    syncPromptsMutation.mutate(undefined, {
      onSuccess: (result) => {
        if (result.complete === false) {
          showToast({
            message: localize('com_ui_langfuse_prompt_sync_incomplete', {
              0: result.created,
              1: result.unchanged,
              2: result.skipped,
            }),
            status: 'warning',
          });
          return;
        }
        showToast({
          message: localize('com_ui_langfuse_prompt_sync_result', {
            0: result.created,
            1: result.unchanged,
            2: result.skipped,
          }),
          status: 'success',
        });
      },
      onError: (error) => {
        showToast({ message: getPromptErrorMessage(error, localize), status: 'error' });
      },
    });
  };

  if (isConnectionLoading && connectionStatus == null) {
    return (
      <div
        data-testid="langfuse-connection-loading"
        className="border-border-light flex items-center justify-center rounded-xl border py-12"
      >
        <Spinner tone="secondary" className="h-6 w-6" />
        <span className="sr-only">{localize('com_ui_loading')}</span>
      </div>
    );
  }

  if (isConnectionError && connectionStatus == null) {
    return (
      <div className="border-border-light flex flex-col items-center gap-3 rounded-xl border px-6 py-10 text-center">
        <p className="text-text-secondary text-sm">{localize('com_ui_langfuse_load_error')}</p>
        <Button
          variant="outline"
          size="sm"
          onClick={() => refetchConnection()}
          disabled={isConnectionFetching}
        >
          {localize('com_ui_retry')}
        </Button>
      </div>
    );
  }

  return (
    <div className="flex flex-col gap-4">
      <div className="flex flex-wrap items-start justify-between gap-x-4 gap-y-2">
        <div className="flex min-w-0 flex-1 flex-wrap items-center gap-2">
          <div className="font-medium">{localize('com_ui_langfuse_title')}</div>
          <div className="border-brand-purple/40 bg-brand-purple/10 text-brand-purple rounded-full border px-2 py-0.5 text-xs font-medium">
            {localize('com_ui_beta')}
          </div>
          <InfoHoverCard side={ESide.Top} text={localize('com_ui_langfuse_beta_info')}>
            <span className="text-text-tertiary hover:bg-surface-hover hover:text-text-primary flex size-6 items-center justify-center rounded-md transition-colors">
              <CircleHelpIcon className="h-4 w-4" aria-hidden="true" />
            </span>
          </InfoHoverCard>
        </div>
        {connectionAvailable && (
          <div
            data-testid="langfuse-connection-status"
            className={`ms-auto flex max-w-full shrink-0 items-start justify-end gap-1.5 text-end text-xs sm:max-w-[50%] ${connectionStatusTextClass}`}
            aria-live="polite"
            title={connectionStatusTitle}
          >
            {connectionTestState === 'checking' ? (
              <Spinner className="h-3 w-3" />
            ) : (
              <span className={`mt-1 h-2 w-2 shrink-0 rounded-full ${connectionStatusDotClass}`} />
            )}
            <span>{connectionStatusLabel}</span>
          </div>
        )}
      </div>

      {connectionAvailable && (
        <>
          <div className="flex flex-col gap-1.5">
            <Label id="langfuse-destination-label">{localize('com_ui_langfuse_destination')}</Label>
            <Dropdown
              value={destination}
              label={destination === '' ? localize('com_ui_select') : ''}
              onChange={handleDestinationChange}
              options={destinationOptions}
              disabled={destinations.length === 0 || busy}
              className="w-full"
              triggerClassName="w-full"
              sameWidth
              testId="langfuse-destination"
              aria-labelledby="langfuse-destination-label"
            />
          </div>

          <div className="flex flex-col gap-1.5">
            <Label htmlFor="langfuse-public-token">{localize('com_ui_langfuse_public_key')}</Label>
            {secretConfigured && !isEditingPublicKey && (
              <button
                type="button"
                className="border-border-light hover:border-border-medium focus-visible:ring-ring-primary w-full rounded-lg border px-3 py-2 text-start focus-visible:ring-2 focus-visible:outline-hidden"
                aria-label={`${localize('com_ui_edit')} ${localize('com_ui_langfuse_public_key')}`}
                disabled={busy}
                onClick={() => setIsEditingPublicKey(true)}
              >
                <code className="text-text-primary block min-w-0 truncate font-mono text-sm">
                  {displayPublicKey}
                </code>
              </button>
            )}
            {publicKeyInputVisible && (
              <Input
                ref={publicKeyInputRef}
                id="langfuse-public-token"
                autoComplete="off"
                data-lpignore="true"
                data-1p-ignore="true"
                data-bwignore="true"
                data-form-type="other"
                value={publicKey}
                disabled={busy}
                placeholder="pk-lf-..."
                onChange={(e) => {
                  connectionTestRequestRef.current += 1;
                  const nextPublicKey = e.target.value;
                  setPublicKey(nextPublicKey);
                  if (
                    secretConfigured &&
                    nextPublicKey.trim() !== (connectionStatus?.publicKey ?? '')
                  ) {
                    setIsEditingSecretKey(true);
                  }
                  setConnectionTestState('unverified');
                  setConnectionTestMessage('');
                }}
              />
            )}
          </div>

          <div className="flex flex-col gap-1.5">
            <Label htmlFor="langfuse-private-token">{localize('com_ui_langfuse_secret_key')}</Label>
            {secretConfigured && !isEditingSecretKey && (
              <button
                type="button"
                className="border-border-light hover:border-border-medium focus-visible:ring-ring-primary w-full rounded-lg border px-3 py-2 text-start focus-visible:ring-2 focus-visible:outline-hidden"
                aria-label={`${localize('com_ui_edit')} ${localize('com_ui_langfuse_secret_key')}`}
                disabled={busy}
                onClick={() => setIsEditingSecretKey(true)}
              >
                <code className="text-text-primary block min-w-0 truncate font-mono text-sm">
                  {connectionStatus?.secretKeyPreview}
                </code>
              </button>
            )}
            {secretInputVisible && (
              <SecretInput
                ref={secretKeyInputRef}
                id="langfuse-private-token"
                autoComplete="off"
                data-lpignore="true"
                data-1p-ignore="true"
                data-bwignore="true"
                data-form-type="other"
                value={secretKey}
                disabled={busy}
                placeholder="sk-lf-..."
                onChange={(e) => {
                  connectionTestRequestRef.current += 1;
                  setSecretKey(e.target.value);
                  setConnectionTestState('unverified');
                  setConnectionTestMessage('');
                }}
              />
            )}
          </div>

          <div className="flex min-h-9 items-center justify-end gap-2">
            {isEditing ? (
              <Button variant="submit" disabled={!canSubmit || busy} onClick={handleSave}>
                {testMutation.isLoading ? (
                  <span className="flex items-center gap-2">
                    <Spinner className="h-4 w-4" />
                    {localize('com_ui_langfuse_testing')}
                  </span>
                ) : (
                  localize('com_ui_save')
                )}
              </Button>
            ) : (
              <Button
                variant={connectionStatus?.enabled === true ? 'outline' : 'submit'}
                disabled={
                  busy || (connectionStatus?.enabled !== true && !connectionDestinationAvailable)
                }
                onClick={handleEnabledChange}
              >
                {localize(
                  connectionStatus?.enabled === true
                    ? 'com_ui_langfuse_disable'
                    : 'com_ui_langfuse_enable',
                )}
              </Button>
            )}
          </div>
        </>
      )}

      {connectionStatus?.promptSync?.available === true && (
        <div className="border-border-light flex flex-col gap-2 border-t pt-4">
          <div className="flex items-center justify-between gap-4">
            <div className="flex flex-col gap-0.5">
              <Label id="langfuse-prompt-sync-label">
                {localize('com_ui_langfuse_prompt_sync')}
              </Label>
              <span className="text-text-secondary text-xs">
                {localize('com_ui_langfuse_prompt_sync_description')}
              </span>
            </div>
            <Switch
              checked={connectionStatus.promptSync.enabled}
              onCheckedChange={handlePromptSyncChange}
              disabled={isSavingStatus || syncPromptsMutation.isLoading}
              aria-labelledby="langfuse-prompt-sync-label"
            />
          </div>
          {promptSyncError && (
            <p className="text-text-destructive text-xs">
              {localize('com_ui_langfuse_prompt_sync_error')}
            </p>
          )}
          {connectionStatus.promptSync.enabled && (
            <div className="flex flex-wrap items-center gap-2">
              {canManagePrompts && (
                <Button variant="submit" size="sm" disabled={busy} onClick={handleSyncPrompts}>
                  {syncPromptsMutation.isLoading ? (
                    <span className="flex items-center gap-2">
                      <Spinner className="h-4 w-4" />
                      {localize('com_ui_langfuse_prompt_sync_running')}
                    </span>
                  ) : (
                    localize('com_ui_langfuse_prompt_sync_run')
                  )}
                </Button>
              )}
              <Button
                variant="outline"
                size="sm"
                disabled={busy}
                onClick={() => setIsPromptsDialogOpen(true)}
              >
                {localize('com_ui_langfuse_prompt_sync_open')}
              </Button>
            </div>
          )}
        </div>
      )}

      {connectionStatus?.promptSync?.enabled === true && (
        <LangfusePromptsDialog open={isPromptsDialogOpen} onOpenChange={setIsPromptsDialogOpen} />
      )}
    </div>
  );
}