mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2025-05-02 17:07:13 +00:00
handle chat edge-case for response object in text field
This commit is contained in:
parent
466bf7dc9c
commit
3f5b2485d7
1 changed files with 18 additions and 2 deletions
|
@ -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>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue