diff --git a/frontend/src/pages/GeneralSettings/Chats/ChatRow/index.jsx b/frontend/src/pages/GeneralSettings/Chats/ChatRow/index.jsx index d487994e7e990c4dcd52a4e028d2a277ddcb0561..4866b57c06b049f25723c90505bef03ca41862bf 100644 --- a/frontend/src/pages/GeneralSettings/Chats/ChatRow/index.jsx +++ b/frontend/src/pages/GeneralSettings/Chats/ChatRow/index.jsx @@ -4,6 +4,22 @@ import System from "@/models/system"; import ModalWrapper from "@/components/ModalWrapper"; import { useModal } from "@/hooks/useModal"; +// Some LLMs may return a "valid" response that truncation fails to truncate because +// it stored an Object as opposed to a string for the `text` field. +function parseText(jsonResponse = "") { + try { + const json = JSON.parse(jsonResponse); + if (!json.hasOwnProperty("text")) + throw new Error('JSON response has no property "text".'); + return typeof json.text !== "string" + ? JSON.stringify(json.text) + : json.text; + } catch (e) { + console.error(e); + return "--failed to parse--"; + } +} + export default function ChatRow({ chat, onDelete }) { const { isOpen: isPromptOpen, @@ -47,7 +63,7 @@ export default function ChatRow({ chat, onDelete }) { onClick={openResponseModal} className="px-6 py-4 cursor-pointer transform transition-transform duration-200 hover:scale-105 hover:shadow-lg" > - {truncate(JSON.parse(chat.response)?.text, 40)} + {truncate(parseText(chat.response), 40)} </td> <td className="px-6 py-4">{chat.createdAt}</td> <td className="px-6 py-4 flex items-center gap-x-6"> @@ -64,7 +80,7 @@ export default function ChatRow({ chat, onDelete }) { </ModalWrapper> <ModalWrapper isOpen={isResponseOpen}> <TextPreview - text={JSON.parse(chat.response)?.text} + text={parseText(chat.response)} closeModal={closeResponseModal} /> </ModalWrapper>