|
| 1 | +// TODO: uncomment when API changes are added |
| 2 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | +import React, { useEffect, useRef, useState } from 'react'; |
| 5 | +import { useRecoilValue } from 'recoil'; |
| 6 | +import { FlexBox, Icon, Input } from '@ui5/webcomponents-react'; |
| 7 | +import { initialPromptState } from 'components/KymaCompanion/state/initalPromptAtom'; |
| 8 | +import Message from './messages/Message'; |
| 9 | +import Bubbles from './messages/Bubbles'; |
| 10 | +import ErrorMessage from './messages/ErrorMessage'; |
| 11 | +import { sessionIDState } from 'components/KymaCompanion/state/sessionIDAtom'; |
| 12 | +import { clusterState } from 'state/clusterAtom'; |
| 13 | +import { authDataState } from 'state/authDataAtom'; |
| 14 | +import './Chat.scss'; |
| 15 | + |
| 16 | +interface MessageType { |
| 17 | + author: 'user' | 'ai'; |
| 18 | + messageChunks: { step: string; result: string }[]; |
| 19 | + isLoading: boolean; |
| 20 | + suggestions?: any[]; |
| 21 | +} |
| 22 | + |
| 23 | +export default function Chat() { |
| 24 | + const { t } = useTranslation(); |
| 25 | + const containerRef = useRef<HTMLDivElement | null>(null); |
| 26 | + const [inputValue, setInputValue] = useState<string>(''); |
| 27 | + const [chatHistory, setChatHistory] = useState<MessageType[]>([]); |
| 28 | + const [errorOccured, setErrorOccured] = useState<boolean>(false); |
| 29 | + const initialPrompt = useRecoilValue<string>(initialPromptState); |
| 30 | + const sessionID = useRecoilValue<string>(sessionIDState); |
| 31 | + const cluster = useRecoilValue<any>(clusterState); |
| 32 | + const authData = useRecoilValue<any>(authDataState); |
| 33 | + |
| 34 | + const addMessage = ({ author, messageChunks, isLoading }: MessageType) => { |
| 35 | + setChatHistory(prevItems => |
| 36 | + prevItems.concat({ author, messageChunks, isLoading }), |
| 37 | + ); |
| 38 | + }; |
| 39 | + |
| 40 | + const handleChatResponse = (response: any) => { |
| 41 | + const isLoading = response?.step !== 'output'; |
| 42 | + if (!isLoading) { |
| 43 | + // TODO: uncomment when API changes are added |
| 44 | + /*getFollowUpQuestions({ sessionID, handleFollowUpQuestions, clusterUrl: cluster.currentContext.cluster.cluster.server, token: authData.token, certificateAuthorityData: cluster.currentContext.cluster.cluster['certificate-authority-data']});*/ |
| 45 | + } |
| 46 | + setChatHistory(prevMessages => { |
| 47 | + const [latestMessage] = prevMessages.slice(-1); |
| 48 | + return prevMessages.slice(0, -1).concat({ |
| 49 | + author: 'ai', |
| 50 | + messageChunks: latestMessage.messageChunks.concat(response), |
| 51 | + isLoading, |
| 52 | + }); |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + const handleFollowUpQuestions = (questions: any) => { |
| 57 | + setChatHistory(prevMessages => { |
| 58 | + const [latestMessage] = prevMessages.slice(-1); |
| 59 | + return prevMessages |
| 60 | + .slice(0, -1) |
| 61 | + .concat({ ...latestMessage, suggestions: questions }); |
| 62 | + }); |
| 63 | + }; |
| 64 | + |
| 65 | + const handleError = () => { |
| 66 | + setErrorOccured(true); |
| 67 | + setChatHistory(prevItems => prevItems.slice(0, -2)); |
| 68 | + }; |
| 69 | + |
| 70 | + const sendPrompt = (prompt: string) => { |
| 71 | + setErrorOccured(false); |
| 72 | + addMessage({ |
| 73 | + author: 'user', |
| 74 | + messageChunks: [{ step: 'output', result: prompt }], |
| 75 | + isLoading: false, |
| 76 | + }); |
| 77 | + // TODO: uncomment when API changes are added |
| 78 | + /*getChatResponse({ prompt, handleChatResponse, handleError, sessionID, clusterUrl: cluster.currentContext.cluster.cluster.server, token: authData.token, certificateAuthorityData: cluster.currentContext.cluster.cluster['certificate-authority-data'], });*/ |
| 79 | + addMessage({ author: 'ai', messageChunks: [], isLoading: true }); |
| 80 | + }; |
| 81 | + |
| 82 | + const onSubmitInput = () => { |
| 83 | + if (inputValue.length === 0) return; |
| 84 | + const prompt = inputValue; |
| 85 | + setInputValue(''); |
| 86 | + sendPrompt(prompt); |
| 87 | + }; |
| 88 | + |
| 89 | + const scrollToBottom = () => { |
| 90 | + if (containerRef?.current?.lastChild) |
| 91 | + (containerRef.current.lastChild as HTMLElement).scrollIntoView({ |
| 92 | + behavior: 'smooth', |
| 93 | + block: 'start', |
| 94 | + }); |
| 95 | + }; |
| 96 | + |
| 97 | + useEffect(() => { |
| 98 | + if (chatHistory.length === 0) sendPrompt(initialPrompt); |
| 99 | + // eslint-disable-next-line |
| 100 | + }, []); |
| 101 | + |
| 102 | + useEffect(() => { |
| 103 | + const delay = errorOccured ? 500 : 0; |
| 104 | + setTimeout(() => { |
| 105 | + scrollToBottom(); |
| 106 | + }, delay); |
| 107 | + }, [chatHistory, errorOccured]); |
| 108 | + |
| 109 | + return ( |
| 110 | + <FlexBox |
| 111 | + direction="Column" |
| 112 | + justifyContent="SpaceBetween" |
| 113 | + className="chat-container" |
| 114 | + > |
| 115 | + <div className="chat-list sap-margin-tiny" ref={containerRef}> |
| 116 | + {chatHistory.map((message, index) => { |
| 117 | + return message.author === 'ai' ? ( |
| 118 | + <React.Fragment key={index}> |
| 119 | + <Message |
| 120 | + className="left-aligned" |
| 121 | + messageChunks={message.messageChunks} |
| 122 | + isLoading={message.isLoading} |
| 123 | + /> |
| 124 | + {index === chatHistory.length - 1 && !message.isLoading && ( |
| 125 | + <Bubbles |
| 126 | + onClick={sendPrompt} |
| 127 | + suggestions={message.suggestions} |
| 128 | + /> |
| 129 | + )} |
| 130 | + </React.Fragment> |
| 131 | + ) : ( |
| 132 | + <Message |
| 133 | + key={index} |
| 134 | + className="right-aligned" |
| 135 | + messageChunks={message.messageChunks} |
| 136 | + isLoading={message.isLoading} |
| 137 | + /> |
| 138 | + ); |
| 139 | + })} |
| 140 | + {errorOccured && ( |
| 141 | + <ErrorMessage |
| 142 | + errorOnInitialMessage={chatHistory.length === 0} |
| 143 | + resendInitialPrompt={() => sendPrompt(initialPrompt)} |
| 144 | + /> |
| 145 | + )} |
| 146 | + </div> |
| 147 | + <div className="sap-margin-x-tiny"> |
| 148 | + <Input |
| 149 | + className="full-width" |
| 150 | + disabled={chatHistory[chatHistory.length - 1]?.isLoading} |
| 151 | + placeholder={t('kyma-companion.placeholder')} |
| 152 | + value={inputValue} |
| 153 | + icon={<Icon name="paper-plane" onClick={onSubmitInput} />} |
| 154 | + onKeyDown={e => e.key === 'Enter' && onSubmitInput()} |
| 155 | + onInput={e => setInputValue(e.target.value)} |
| 156 | + /> |
| 157 | + </div> |
| 158 | + </FlexBox> |
| 159 | + ); |
| 160 | +} |
0 commit comments