All files / src/components/assistant AiAssistantComponent.tsx

56.41% Statements 22/39
21.42% Branches 3/14
26.66% Functions 4/15
63.63% Lines 21/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149                                                                                                1x 1x 1x 1x               1x   4x 4x 4x 4x   4x                             4x   4x 1x             4x         4x             4x               4x             4x 4x 4x                       4x                                
import React, { ReactElement, useCallback, useEffect } from "react";
import "@chatui/core/es/styles/index.less";
import "@chatui/core/dist/index.css";
import { ChatBox, Message, ReceivedMessage } from "../chat/ChatBox";
import { getUserId } from "@utils/TokenUtils";
import {
  AiAssistantConversationProps,
  ChatMessageProps,
  getAiAssistantConversation,
  reAskAiAssistant,
  setConversationBarrier,
} from "./FetchUtils";
import "./AiAssistant.less";
import AiAssistantNavbar from "./AiAssistantNavbar";
import AiAssistantChatMessage, { getAvatarDataOfUser } from "./AiAssistantChatMessage";
import Chat, { MessageProps } from "@chatui/core";
import { useTranslation } from "react-i18next";
import { LargeSpin } from "../icons";
import { autoFocusTextarea } from "@utils/BrowserUtils";
import { DisplayMode } from "@props/RecordProps";
import { useSlashCommand } from "@utils/hooks";
 
export interface AssistantMessageContent {
 
  type: 'ask' | 'reply';
}
 
export interface AssistantAskMessageContent extends AssistantMessageContent {
  type: 'ask';
  ask: string;
}
 
export interface AssistantReplyMessageContent extends AssistantMessageContent {
  type: 'reply';
  status: 'Success' | 'Failed';
}
 
export interface AssistantSuccessReplyMessageContent extends AssistantReplyMessageContent {
  status: 'Success';
  reply: string;
}
 
export interface AssistantFailedReplyMessageContent extends AssistantReplyMessageContent {
  status: 'Failed';
  errMsg: string;
  forbiddenRetry: boolean;
}
 
export const isAssistantAskMessageContent = (content?: AssistantMessageContent): content is AssistantAskMessageContent => content?.type === 'ask';
export const isAssistantReplyMessageContent = (content?: AssistantMessageContent): content is AssistantReplyMessageContent => content?.type === 'reply';
export const isAssistantSuccessReplyMessageContent = (content?: AssistantReplyMessageContent): content is AssistantSuccessReplyMessageContent => content?.status === 'Success';
export const isAssistantFailedReplyMessageContent = (content?: AssistantReplyMessageContent): content is AssistantFailedReplyMessageContent => content?.status === 'Failed';
 
export interface AiAssistantComponentProps {
  zIndex: number;
  setDisplay: (display: DisplayMode) => void;
  enable: boolean;
}
 
const AiAssistantComponent = (props: AiAssistantComponentProps): ReactElement => {
 
  const { zIndex, setDisplay, enable } = props;
  const [conversation, setConversation] = React.useState<AiAssistantConversationProps | undefined>();
  const userId = Number.parseInt(getUserId() ?? "");
  const { t } = useTranslation();
 
  const handleSetConversationBarrier = useCallback((): void => {
    if (!conversation) {
      return;
    }
    setConversationBarrier(conversation.id).then((res) => {
      setConversation({
        ...conversation,
        extInfo: {
          ...conversation.extInfo,
          barrierIndex: res.barrierMsgIndex,
        }
      });
    });
  }, [conversation]);
 
  const { processSlashCommand, isSlashCommand } = useSlashCommand(handleSetConversationBarrier, setDisplay);
 
  useEffect(() => {
    enable && getAiAssistantConversation().then((res) => {
      setConversation(res);
      // warmup assistant avatar data
      getAvatarDataOfUser(res.extInfo.assistantUserId);
    });
  }, [enable]);
 
  const handleSend = useCallback((type: string, content: string): AssistantAskMessageContent => ({
    type: "ask",
    ask: content,
  }), []);
 
  const reAsk = useCallback((message: ReceivedMessage<AssistantFailedReplyMessageContent>): Promise<ChatMessageProps> | undefined => {
    if (conversation) {
      return reAskAiAssistant(conversation.id, message.index);
    }
    return undefined;
  }, [conversation]);
 
  const renderMessageContent = useCallback((msg: Message<AssistantMessageContent>): React.ReactNode =>
    <AiAssistantChatMessage
      key={msg.uuid}
      msg={msg}
      reAsk={reAsk}
      barrierIndex={conversation?.extInfo?.barrierIndex}
    />, [conversation, reAsk]);
 
  const renderNavbar = (): ReactElement => (<AiAssistantNavbar
    zIndex={zIndex}
    handleSetConversationBarrier={handleSetConversationBarrier}
    loading={!conversation}
    setDisplay={setDisplay}
  />);
 
  Eif (!enable) {
    autoFocusTextarea("Composer-input");
    return (<Chat
      renderNavbar={renderNavbar}
      onSend={(type, content) => {
        isSlashCommand(content) && processSlashCommand(content);
      }}
      placeholder={t("assistant:Type a message...")}
      messages={[{
        _id: 1,
        type: "text",
        content: t("assistant:Usage instructions"),
      }] as Array<MessageProps>}
      renderMessageContent={(message: MessageProps) =>
        <div dangerouslySetInnerHTML={{ __html: message.content }} />
      } />);
  }
 
  return (!conversation) ? <LargeSpin /> : (<ChatBox
    userId={userId}
    conversationId={conversation.id}
    renderNavbar={renderNavbar}
    renderMessageContent={renderMessageContent}
    handleSend={handleSend}
    handleSetConversationBarrier={handleSetConversationBarrier}
    setDisplay={setDisplay}
  />);
};
 
export default AiAssistantComponent;