Skip to content

The open alternative to Salesforce, designed for AI.

12.9 KB
Raw Blame
import { useLingui } from '@lingui/react/macro';
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { currentWorkspaceMembersState } from '@/auth/states/currentWorkspaceMembersState';
import { useAiChatSentMessageHandOff } from '@/ai/hooks/useAiChatSentMessageHandOff';
import { styled } from '@linaria/react';
import { type ReactNode } from 'react';

import { AgentChatFilePreview } from '@/ai/components/internal/AgentChatFilePreview';
import { AGENT_MESSAGE_ROLE } from '@/ai/constants/AgentMessageRole';

import { AiChatAssistantAvatar } from '@/ai/components/AiChatAssistantAvatar';
import { AiChatAssistantMessageRenderer } from '@/ai/components/AiChatAssistantMessageRenderer';
import { AiChatMessageHeader } from '@/ai/components/AiChatMessageHeader';
import { AiChatMessageReactionPicker } from '@/ai/components/AiChatMessageReactionPicker';
import { AiChatMessageReactions } from '@/ai/components/AiChatMessageReactions';
import { AiChatErrorRenderer } from '@/ai/components/AiChatErrorRenderer';
import { AiChatUserMessageCollapsibleContent } from '@/ai/components/AiChatUserMessageCollapsibleContent';
import { agentChatFirstUnreadMessageIdSelector } from '@/ai/states/selectors/agentChatFirstUnreadMessageIdSelector';
import { agentChatIsMessageBeforeFirstUserMessageFamilySelector } from '@/ai/states/selectors/agentChatIsMessageBeforeFirstUserMessageFamilySelector';
import { agentChatMessageFamilySelector } from '@/ai/states/selectors/agentChatMessageFamilySelector';
import { isAgentChatMessageOwn } from '@/ai/utils/isAgentChatMessageOwn';
import { getAgentChatSenderLabel } from '@/ai/utils/getAgentChatSenderLabel';
import { isSilentAgentChatMessage } from '@/ai/utils/isSilentAgentChatMessage';
import { getAgentChatMessageWorkDurationMs } from '@/ai/utils/getAgentChatMessageWorkDurationMs';
import { type AiChatError } from '@/ai/types/AiChatError';
import { LightCopyIconButton } from '@/ui/input/components/LightCopyIconButton';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';

import { isExtendedFileUIPart } from 'twenty-shared/ai';
import { isDefined, isNonEmptyArray } from 'twenty-shared/utils';
import { LabeledSeparator } from 'twenty-ui/components/layout';
import { Avatar } from 'twenty-ui/primitives/data-display';
import { themeCssVariables } from 'twenty-ui/theme';
import { dateLocaleState } from '@/localization/states/dateLocaleState';
import { beautifyPastDateRelativeToNow } from '~/utils/date-utils';
import { useAtomFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomFamilySelectorValue';
import { useIsFeatureEnabled } from '@/workspace/hooks/useIsFeatureEnabled';
import { FeatureFlagKey } from '~/generated-metadata/graphql';
import { getAbsoluteImageUrl } from '~/utils/image/getAbsoluteImageUrl';

const StyledMessageBubble = styled.div<{ isRightAligned: boolean }>`
  align-items: ${({ isRightAligned }) =>
    isRightAligned ? 'flex-end' : 'flex-start'};
  display: flex;
  flex-direction: column;
  position: relative;
  width: 100%;

  &:hover .message-actions,
  &:focus-within .message-actions {
    opacity: 1;
    pointer-events: auto;
  }
`;

const StyledMessageText = styled.div<{
  isUser?: boolean;
  isOtherMember?: boolean;
}>`
  background: ${({ isUser, isOtherMember }) => {
    if (isOtherMember) {
      return themeCssVariables.background.secondary;
    }

    if (isUser) {
      return themeCssVariables.background.tertiary;
    }

    return 'transparent';
  }};
  border: ${({ isOtherMember }) =>
    isOtherMember
      ? `1px solid ${themeCssVariables.border.color.medium}`
      : 'none'};
  border-radius: ${({ isUser }) =>
    isUser ? themeCssVariables.border.radius.lgRound : '0'};
  color: ${({ isUser }) =>
    isUser
      ? themeCssVariables.font.color.secondary
      : themeCssVariables.font.color.primary};
  corner-shape: round;
  font-weight: ${({ isUser }) => (isUser ? 500 : 400)};
  line-height: ${themeCssVariables.spacing[6]};
  max-width: 100%;
  overflow-wrap: break-word;
  padding: ${({ isUser }) =>
    isUser ? `0 ${themeCssVariables.spacing[2]}` : '0'};
  white-space: normal;
  width: ${({ isUser }) => (isUser ? 'fit-content' : '100%')};
  /* Pre-wrap on the container turns newlines between blocks into spacing; only code pre-wraps. */
  word-wrap: break-word;

  code {
    background: ${themeCssVariables.background.tertiary};
    border-radius: ${themeCssVariables.border.radius.sm};
    line-height: 1.4;
    max-width: 100%;
    overflow: auto;
    padding: 1px 3px;
    white-space: pre-wrap;
    word-wrap: break-word;
  }

  pre {
    background: ${themeCssVariables.background.tertiary};
    border-radius: ${themeCssVariables.border.radius.sm};
    max-width: 100%;
    overflow-x: auto;
    padding: ${themeCssVariables.spacing[2]};

    code {
      background: none;
      border-radius: 0;
      padding: 0;
    }
  }

  p {
    line-height: inherit;
    margin-block: ${({ isUser }) =>
      isUser ? '0' : themeCssVariables.spacing[1]};
  }

  blockquote {
    border-left: 3px solid ${themeCssVariables.border.color.medium};
    color: ${themeCssVariables.font.color.secondary};
    margin: ${themeCssVariables.spacing[2]} 0;
    padding-left: ${themeCssVariables.spacing[2]};
  }
`;

const StyledMessageFooter = styled.div<{
  isSpreadOut: boolean;
  isRightAligned: boolean;
}>`
  align-items: center;
  color: ${themeCssVariables.font.color.secondary};
  display: flex;
  flex-direction: ${({ isRightAligned }) =>
    isRightAligned ? 'row-reverse' : 'row'};
  flex-wrap: wrap;
  font-size: ${themeCssVariables.font.size.sm};
  gap: ${themeCssVariables.spacing[1]};
  justify-content: ${({ isSpreadOut }) =>
    isSpreadOut ? 'space-between' : 'flex-start'};
  margin-top: ${themeCssVariables.spacing[2]};
  width: 100%;
`;

const StyledMessageActions = styled.div<{ isRightAligned: boolean }>`
  align-items: center;
  display: flex;
  flex-direction: ${({ isRightAligned }) =>
    isRightAligned ? 'row-reverse' : 'row'};
  gap: ${themeCssVariables.spacing[1]};
  opacity: 0;
  pointer-events: none;
  transition: opacity calc(${themeCssVariables.animation.duration.normal} * 1s)
    ease-in-out;

  @media (hover: none) {
    opacity: 1;
    pointer-events: auto;
  }
`;

const StyledSender = styled.div`
  color: ${themeCssVariables.font.color.secondary};
  font-size: ${themeCssVariables.font.size.sm};
  margin-bottom: ${themeCssVariables.spacing[1]};
`;

const StyledMessageTimestamp = styled.span`
  color: ${themeCssVariables.font.color.light};
`;

const StyledMessageContainer = styled.div<{ isUser?: boolean }>`
  max-width: 100%;
  min-width: 0;
  width: ${({ isUser }) => (isUser ? 'fit-content' : '100%')};
`;

const StyledFilesContainer = styled.div`
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: ${themeCssVariables.spacing[2]};
  margin-top: ${themeCssVariables.spacing[2]};
`;

type AiChatUserMessageTextProps = {
  messageId: string;
  text: string;
  children: ReactNode;
  isOtherMember: boolean;
};

const AiChatUserMessageText = ({
  messageId,
  text,
  children,
  isOtherMember,
}: AiChatUserMessageTextProps) => {
  const sentMessageHandOffRef = useAiChatSentMessageHandOff(messageId);

  return (
    <StyledMessageText
      isUser
      isOtherMember={isOtherMember}
      ref={sentMessageHandOffRef}
    >
      <AiChatUserMessageCollapsibleContent text={text}>
        {children}
      </AiChatUserMessageCollapsibleContent>
    </StyledMessageText>
  );
};

type AiChatMessageProps = {
  messageId: string;
  isLastMessageStreaming?: boolean;
  error?: AiChatError;
};

export const AiChatMessage = ({
  messageId,
  isLastMessageStreaming = false,
  error,
}: AiChatMessageProps) => {
  const { t } = useLingui();
  const currentWorkspaceMember = useAtomStateValue(currentWorkspaceMemberState);
  const currentWorkspaceMembers = useAtomStateValue(
    currentWorkspaceMembersState,
  );
  const agentChatMessage = useAtomFamilySelectorValue(
    agentChatMessageFamilySelector,
    { messageId },
  );

  const isMessageBeforeFirstUserMessage = useAtomFamilySelectorValue(
    agentChatIsMessageBeforeFirstUserMessageFamilySelector,
    { messageId },
  );

  const { localeCatalog } = useAtomStateValue(dateLocaleState);
  const agentChatFirstUnreadMessageId = useAtomStateValue(
    agentChatFirstUnreadMessageIdSelector,
  );
  const isAiChatInboxEnabled = useIsFeatureEnabled(
    FeatureFlagKey.IS_AI_CHAT_INBOX_ENABLED,
  );

  if (
    !isDefined(agentChatMessage) ||
    (isSilentAgentChatMessage(agentChatMessage) && !isDefined(error))
  ) {
    return null;
  }

  const senderId = agentChatMessage.metadata?.senderWorkspaceMemberId;
  const sender = currentWorkspaceMembers.find(
    (member) => isDefined(senderId) && member.id === senderId,
  );
  const isOwnMessage = isAgentChatMessageOwn({
    message: agentChatMessage,
    currentWorkspaceMemberId: currentWorkspaceMember?.id,
    isSenderlessUserMessageOwn: !isAiChatInboxEnabled,
  });
  const senderLabel = isDefined(senderId)
    ? getAgentChatSenderLabel({
        sender,
        isCurrentWorkspaceMember: isOwnMessage,
      })
    : t`Unknown sender`;
  const isUser = agentChatMessage.role === AGENT_MESSAGE_ROLE.USER;
  const isRightAligned = isAiChatInboxEnabled ? isOwnMessage : isUser;
  const shouldMirrorMessageActions = isAiChatInboxEnabled && isOwnMessage;
  const createdAt = agentChatMessage.metadata?.createdAt ?? new Date();
  const hasHeader = isAiChatInboxEnabled && !isOwnMessage;
  const shouldShowError =
    isDefined(error) && agentChatMessage.role === AGENT_MESSAGE_ROLE.ASSISTANT;

  const fileParts = agentChatMessage.parts.filter(isExtendedFileUIPart);
  const messageText =
    agentChatMessage.parts.find((part) => part.type === 'text')?.text ?? '';
  const messageContent = (
    <AiChatAssistantMessageRenderer
      isUserMessage={isUser}
      isLastMessageStreaming={isLastMessageStreaming}
      messageParts={agentChatMessage.parts}
      hasError={shouldShowError}
      shouldHideThinkingSteps={isMessageBeforeFirstUserMessage}
      workDurationMs={getAgentChatMessageWorkDurationMs({
        metadata: agentChatMessage.metadata,
        isStreaming: isLastMessageStreaming,
      })}
    />
  );

  return (
    <>
      {isAiChatInboxEnabled && agentChatFirstUnreadMessageId === messageId && (
        <LabeledSeparator
          labelPosition="end"
          tone="danger"
          hasStartMarker
        >{t`New`}</LabeledSeparator>
      )}
      <StyledMessageBubble isRightAligned={isRightAligned}>
        {hasHeader && (isUser || isDefined(senderId)) && (
          <AiChatMessageHeader
            avatar={
              <Avatar
                src={getAbsoluteImageUrl(sender?.avatarUrl)}
                colorSeed={senderId ?? messageId}
                name={senderLabel}
                size="sm"
                shape="circle"
              />
            }
            name={senderLabel}
            createdAt={createdAt}
          />
        )}
        {hasHeader && !isUser && !isDefined(senderId) && (
          <AiChatMessageHeader
            avatar={<AiChatAssistantAvatar />}
            name={t`Assistant`}
            createdAt={createdAt}
          />
        )}
        {!isAiChatInboxEnabled && isUser && isDefined(senderId) && (
          <StyledSender>{senderLabel}</StyledSender>
        )}
        <StyledMessageContainer isUser={isUser}>
          {isUser ? (
            <AiChatUserMessageText
              messageId={messageId}
              text={messageText}
              isOtherMember={isAiChatInboxEnabled && !isOwnMessage}
            >
              {messageContent}
            </AiChatUserMessageText>
          ) : (
            <StyledMessageText>{messageContent}</StyledMessageText>
          )}
          {isNonEmptyArray(fileParts) && (
            <StyledFilesContainer>
              {fileParts.map((file) => (
                <AgentChatFilePreview key={file.filename} file={file} />
              ))}
            </StyledFilesContainer>
          )}
          {shouldShowError && <AiChatErrorRenderer error={error} />}
        </StyledMessageContainer>
        {isNonEmptyArray(agentChatMessage.parts) && (
          <StyledMessageFooter
            isSpreadOut={!isAiChatInboxEnabled}
            isRightAligned={shouldMirrorMessageActions}
          >
            {isAiChatInboxEnabled && (
              <AiChatMessageReactions messageId={messageId} />
            )}
            {!hasHeader && (
              <StyledMessageTimestamp>
                {beautifyPastDateRelativeToNow(createdAt, localeCatalog)}
              </StyledMessageTimestamp>
            )}
            <StyledMessageActions
              className="message-actions"
              isRightAligned={shouldMirrorMessageActions}
            >
              {isAiChatInboxEnabled && !isLastMessageStreaming && (
                <AiChatMessageReactionPicker messageId={messageId} />
              )}
              <LightCopyIconButton copyText={messageText} />
            </StyledMessageActions>
          </StyledMessageFooter>
        )}
      </StyledMessageBubble>
    </>
  );
};