Skip to content

The open alternative to Salesforce, designed for AI.

2.3 KB
Raw Blame
import { styled } from '@linaria/react';
import { Suspense, lazy } from 'react';

import { type DynamicToolUIPart, getToolName, type ToolUIPart } from 'ai';
import { themeCssVariables } from 'twenty-ui/theme';

import { ThinkingToolStepRow } from '@/ai/components/ThinkingToolStepRow';
import { type ToolInput } from '@/ai/types/ToolInput';
import { isThinkingStepPartActive } from '@/ai/utils/isThinkingStepPartActive';
import { unwrapToolInput } from '@/ai/utils/tool-display/unwrapToolInput';
import { FrontComponentSkeletonLoader } from '@/front-components/components/FrontComponentSkeletonLoader';

const FrontComponentRenderer = lazy(() =>
  import('@/front-components/components/FrontComponentRenderer').then(
    (module) => ({ default: module.FrontComponentRenderer }),
  ),
);

const StyledContainer = styled.div`
  background: ${themeCssVariables.background.secondary};
  border: 1px solid ${themeCssVariables.border.color.medium};
  border-radius: ${themeCssVariables.border.radius.md};
  overflow: hidden;
`;

type AiChatToolWidgetProps = {
  toolPart: ToolUIPart | DynamicToolUIPart;
  frontComponentId: string;
  isStreaming: boolean;
};

export const AiChatToolWidget = ({
  toolPart,
  frontComponentId,
  isStreaming,
}: AiChatToolWidgetProps) => {
  // execute_tool wraps the real tool, and the widget belongs to the dispatched one.
  const { toolName, toolInput } = unwrapToolInput({
    input: toolPart.input as ToolInput,
    toolName: getToolName(toolPart),
  });

  const toolCall = {
    toolCallId: toolPart.toolCallId,
    toolName,
    status: toolPart.state,
    input: toolInput as Record<string, unknown> | undefined,
    output:
      toolPart.state === 'output-available'
        ? (toolPart.output as Record<string, unknown> | undefined)
        : undefined,
    errorText:
      toolPart.state === 'output-error' ? toolPart.errorText : undefined,
  };

  return (
    <StyledContainer>
      <Suspense fallback={<FrontComponentSkeletonLoader />}>
        <FrontComponentRenderer
          frontComponentId={frontComponentId}
          toolCall={toolCall}
          loadingFallback={<FrontComponentSkeletonLoader />}
          unavailableFallback={
            <ThinkingToolStepRow
              part={toolPart}
              isActive={isThinkingStepPartActive(toolPart, isStreaming)}
            />
          }
        />
      </Suspense>
    </StyledContainer>
  );
};