Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.6 KB
Raw Blame
'use client';

import { safeParseJSON } from '@lobechat/utils';
import { Flexbox, Highlighter, Icon, Text } from '@lobehub/ui';
import { cssVar } from '@lobehub/ui/stylex/cssVar.stylex';
import * as stylex from '@stylexjs/stylex';
import { AlertTriangle } from 'lucide-react';
import type { ErrorInfo, ReactNode } from 'react';
import { Component, memo, useMemo } from 'react';
import { createPortal } from 'react-dom';
import { useTranslation } from 'react-i18next';

import { useUserStore } from '@/store/user';
import { toolInterventionSelectors } from '@/store/user/selectors';

import ApprovalActions from '../Messages/AssistantGroup/Tool/Detail/Intervention/ApprovalActions';

const styles = stylex.create({
  description: {
    overflow: 'hidden',
    flex: '1',
    minWidth: '0',
    color: cssVar.colorTextSecondary,
    textOverflow: 'ellipsis',
    whiteSpace: 'nowrap',
  },
  icon: { flex: 'none', color: cssVar.colorWarning },
  notice: {
    display: 'flex',
    gap: '6px',
    alignItems: 'center',
    minWidth: '0',
    paddingBlock: '2px',
    fontSize: '12px',
    lineHeight: 1.5,
  },
  title: { flex: 'none', fontWeight: 500, color: cssVar.colorWarning },
});

interface UserInterventionFallbackProps {
  actionsPortalTarget?: HTMLDivElement | null;
  apiName: string;
  assistantGroupId?: string;
  identifier: string;
  requestArgs: string;
  toolCallId: string;
  toolMessageId: string;
}

interface UserInterventionErrorBoundaryProps extends UserInterventionFallbackProps {
  children: ReactNode;
}

interface UserInterventionErrorBoundaryState {
  hasError: boolean;
}

const formatRequestArgs = (requestArgs: string) => {
  const parsed = safeParseJSON<unknown>(requestArgs);

  if (parsed === undefined) return requestArgs.trim() || '{}';

  return JSON.stringify(parsed, null, 2);
};

const UserInterventionFallback = memo<UserInterventionFallbackProps>(
  ({
    actionsPortalTarget,
    apiName,
    assistantGroupId,
    identifier,
    requestArgs,
    toolCallId,
    toolMessageId,
  }) => {
    const { t } = useTranslation('chat');
    const approvalMode = useUserStore(toolInterventionSelectors.approvalMode);
    const json = useMemo(() => formatRequestArgs(requestArgs), [requestArgs]);
    const actions = (
      <Flexbox horizontal justify={'flex-end'}>
        <ApprovalActions
          apiName={apiName}
          approvalMode={approvalMode}
          assistantGroupId={assistantGroupId}
          identifier={identifier}
          messageId={toolMessageId}
          toolCallId={toolCallId}
        />
      </Flexbox>
    );

    return (
      <Flexbox gap={8}>
        <div {...stylex.props(styles.notice)}>
          <Icon className={stylex.props(styles.icon).className} icon={AlertTriangle} size={14} />
          <span {...stylex.props(styles.title)}>{t('tool.intervention.renderFallback.title')}</span>
          <span {...stylex.props(styles.description)}>
            {t('tool.intervention.renderFallback.description')}
          </span>
        </div>
        <Text fontSize={12} type="secondary">
          {identifier} / {apiName} · {t('tool.intervention.renderFallback.rawJson')}
        </Text>
        <Highlighter wrap actionIconSize="small" language="json" variant="borderless">
          {json}
        </Highlighter>
        {actionsPortalTarget ? createPortal(actions, actionsPortalTarget) : actions}
      </Flexbox>
    );
  },
);

UserInterventionFallback.displayName = 'UserInterventionFallback';

class UserInterventionErrorBoundary extends Component<
  UserInterventionErrorBoundaryProps,
  UserInterventionErrorBoundaryState
> {
  public state: UserInterventionErrorBoundaryState = { hasError: false };

  public static getDerivedStateFromError(): UserInterventionErrorBoundaryState {
    return { hasError: true };
  }

  public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    console.error('[UserInterventionErrorBoundary] Caught error in intervention render:', {
      apiName: this.props.apiName,
      componentStack: errorInfo.componentStack,
      error: error.message,
      identifier: this.props.identifier,
      toolCallId: this.props.toolCallId,
    });
  }

  public render() {
    if (this.state.hasError) {
      return (
        <UserInterventionFallback
          actionsPortalTarget={this.props.actionsPortalTarget}
          apiName={this.props.apiName}
          assistantGroupId={this.props.assistantGroupId}
          identifier={this.props.identifier}
          requestArgs={this.props.requestArgs}
          toolCallId={this.props.toolCallId}
          toolMessageId={this.props.toolMessageId}
        />
      );
    }

    return this.props.children;
  }
}

export default UserInterventionErrorBoundary;