({
chat,
chatMessages,
documents,
citations,
onJumpToPage,
}: {
chat: ChatResponse;
chatMessages: ChatMessageResponse[];
documents?: DocumentsWithIndexingStatus[];
citations?: CitationResponse[];
onJumpToPage?: (docId: string, pageIndex: number) => void;
})
| 150 | }; |
| 151 | |
| 152 | export function Chat({ |
| 153 | chat, |
| 154 | chatMessages, |
| 155 | documents, |
| 156 | citations, |
| 157 | onJumpToPage, |
| 158 | }: { |
| 159 | chat: ChatResponse; |
| 160 | chatMessages: ChatMessageResponse[]; |
| 161 | documents?: DocumentsWithIndexingStatus[]; |
| 162 | citations?: CitationResponse[]; |
| 163 | onJumpToPage?: (docId: string, pageIndex: number) => void; |
| 164 | }) { |
| 165 | const chatId = Id.from<ChatResponse>(chat.id); |
| 166 | |
| 167 | const [title, setTitle] = useState<string | undefined>(DEFAULT_CHAT_TITLE); |
| 168 | const [isTitleGenerating, setIsTitleGenerating] = useState<boolean>(false); |
| 169 | const [isProcessingDocuments, setIsProcessingDocuments] = |
| 170 | useState<boolean>(false); |
| 171 | const [numberOfProcessedDocuments, setNumberOfProcessedDocuments] = |
| 172 | useState(0); |
| 173 | const [processingDocumentName, setProcessingDocumentName] = useState(""); |
| 174 | const [processingDocumentStatus, setProcessingDocumentStatus] = useState(""); |
| 175 | const [processingDocumentsError, setProcessingDocumentsError] = useState(""); |
| 176 | const [messageCitations, setMessageCitations] = useState< |
| 177 | Map<string /* messageId */, CitationResponse[]> |
| 178 | >(toCitationMap(citations)); |
| 179 | const formRef = useRef(null); |
| 180 | const messagesRef = useRef<Message[]>([]); |
| 181 | const { |
| 182 | messages, |
| 183 | setMessages, |
| 184 | input, |
| 185 | handleInputChange, |
| 186 | handleSubmit, |
| 187 | append, |
| 188 | isLoading, |
| 189 | } = useChat({ |
| 190 | id: chat.id, |
| 191 | api: postChatMessagesGenerateApiPath(chatId), |
| 192 | onFinish: async (message) => { |
| 193 | if (message.role !== "assistant") { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | // Fetch latest message from backend to get the correct message id |
| 198 | const { response: chatMessageResponses } = await get< |
| 199 | ChatMessageResponse[] |
| 200 | >( |
| 201 | getChatMessagesApiPath({ |
| 202 | chatId: chatId, |
| 203 | ordering: { |
| 204 | orderBy: "createdAt", |
| 205 | order: "desc", |
| 206 | }, |
| 207 | pagination: { |
| 208 | page: 1, |
| 209 | pageSize: 1, |
nothing calls this directly
no test coverage detected