Skip to content
Snippets Groups Projects
Commit 3f5b2485 authored by timothycarambat's avatar timothycarambat
Browse files

handle chat edge-case for response object in text field

parent 466bf7dc
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,22 @@ import System from "@/models/system"; ...@@ -4,6 +4,22 @@ import System from "@/models/system";
import ModalWrapper from "@/components/ModalWrapper"; import ModalWrapper from "@/components/ModalWrapper";
import { useModal } from "@/hooks/useModal"; 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 }) { export default function ChatRow({ chat, onDelete }) {
const { const {
isOpen: isPromptOpen, isOpen: isPromptOpen,
...@@ -47,7 +63,7 @@ export default function ChatRow({ chat, onDelete }) { ...@@ -47,7 +63,7 @@ export default function ChatRow({ chat, onDelete }) {
onClick={openResponseModal} onClick={openResponseModal}
className="px-6 py-4 cursor-pointer transform transition-transform duration-200 hover:scale-105 hover:shadow-lg" 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>
<td className="px-6 py-4">{chat.createdAt}</td> <td className="px-6 py-4">{chat.createdAt}</td>
<td className="px-6 py-4 flex items-center gap-x-6"> <td className="px-6 py-4 flex items-center gap-x-6">
...@@ -64,7 +80,7 @@ export default function ChatRow({ chat, onDelete }) { ...@@ -64,7 +80,7 @@ export default function ChatRow({ chat, onDelete }) {
</ModalWrapper> </ModalWrapper>
<ModalWrapper isOpen={isResponseOpen}> <ModalWrapper isOpen={isResponseOpen}>
<TextPreview <TextPreview
text={JSON.parse(chat.response)?.text} text={parseText(chat.response)}
closeModal={closeResponseModal} closeModal={closeResponseModal}
/> />
</ModalWrapper> </ModalWrapper>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment