mirror of
https://github.com/NovaOSS/nova-betterchat.git
synced 2024-11-29 11:03:59 +01:00
error handling
This commit is contained in:
parent
8a9dac15f7
commit
f5c8bec819
|
@ -12,7 +12,12 @@ export const parseEventSource = (
|
||||||
.map((line) => line.replace(/^data: /, ''))
|
.map((line) => line.replace(/^data: /, ''))
|
||||||
.join('');
|
.join('');
|
||||||
if (jsonString === '[DONE]') return jsonString;
|
if (jsonString === '[DONE]') return jsonString;
|
||||||
else return JSON.parse(jsonString);
|
try {
|
||||||
|
const json = JSON.parse(jsonString);
|
||||||
|
return json;
|
||||||
|
} catch {
|
||||||
|
return '[ERROR]';
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import React from 'react';
|
import React, { useState } from 'react';
|
||||||
import ScrollToBottom from 'react-scroll-to-bottom';
|
import ScrollToBottom from 'react-scroll-to-bottom';
|
||||||
import useStore from '@store/store';
|
import useStore from '@store/store';
|
||||||
|
|
||||||
|
@ -24,6 +24,7 @@ const ChatContent = () => {
|
||||||
state.setMessages,
|
state.setMessages,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
const [error, setError] = useState<string>('');
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
const updatedMessages: MessageInterface[] = JSON.parse(
|
const updatedMessages: MessageInterface[] = JSON.parse(
|
||||||
|
@ -33,6 +34,7 @@ const ChatContent = () => {
|
||||||
setMessages(updatedMessages);
|
setMessages(updatedMessages);
|
||||||
let stream;
|
let stream;
|
||||||
|
|
||||||
|
try {
|
||||||
if (apiFree) {
|
if (apiFree) {
|
||||||
stream = await getChatCompletionStreamFree(messages);
|
stream = await getChatCompletionStreamFree(messages);
|
||||||
} else if (apiKey) {
|
} else if (apiKey) {
|
||||||
|
@ -45,14 +47,13 @@ const ChatContent = () => {
|
||||||
while (reading) {
|
while (reading) {
|
||||||
const { done, value } = await reader.read();
|
const { done, value } = await reader.read();
|
||||||
|
|
||||||
try {
|
|
||||||
const result = parseEventSource(new TextDecoder().decode(value));
|
const result = parseEventSource(new TextDecoder().decode(value));
|
||||||
|
|
||||||
if (result === '[DONE]' || done) {
|
if (result === '[DONE]' || done) {
|
||||||
reading = false;
|
reading = false;
|
||||||
} else {
|
} else {
|
||||||
const resultString = result.reduce((output: string, curr) => {
|
const resultString = result.reduce((output: string, curr) => {
|
||||||
if (curr === '[DONE]') return output;
|
if (typeof curr === 'string') return output;
|
||||||
else {
|
else {
|
||||||
const content = curr.choices[0].delta.content;
|
const content = curr.choices[0].delta.content;
|
||||||
if (content) output += content;
|
if (content) output += content;
|
||||||
|
@ -66,10 +67,15 @@ const ChatContent = () => {
|
||||||
updatedMessages[updatedMessages.length - 1].content += resultString;
|
updatedMessages[updatedMessages.length - 1].content += resultString;
|
||||||
setMessages(updatedMessages);
|
setMessages(updatedMessages);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
console.log((e as Error).message);
|
const err = (e as Error).message;
|
||||||
}
|
console.log(err);
|
||||||
}
|
setError(err);
|
||||||
|
setTimeout(() => {
|
||||||
|
setError(''), 10000;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -95,6 +101,12 @@ const ChatContent = () => {
|
||||||
))}
|
))}
|
||||||
<Message role={inputRole} content='' messageIndex={-1} sticky />
|
<Message role={inputRole} content='' messageIndex={-1} sticky />
|
||||||
|
|
||||||
|
{error !== '' && (
|
||||||
|
<div className='bg-red-600/50 p-2 rounded-sm w-3/5 mt-3 text-gray-900 dark:text-gray-300 text-sm'>
|
||||||
|
Invalid API key!
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className='text-center mt-6 flex justify-center gap-2'>
|
<div className='text-center mt-6 flex justify-center gap-2'>
|
||||||
<button
|
<button
|
||||||
className='btn relative btn-primary mt-2'
|
className='btn relative btn-primary mt-2'
|
||||||
|
|
|
@ -26,7 +26,11 @@ const Message = ({
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={`w-full border-b border-black/10 dark:border-gray-900/50 text-gray-800 dark:text-gray-100 group ${backgroundStyle[role]}`}
|
className={`w-full border-b border-black/10 dark:border-gray-900/50 text-gray-800 dark:text-gray-100 group ${backgroundStyle[role]}`}
|
||||||
key={Math.random()}
|
key={
|
||||||
|
messageIndex !== -1
|
||||||
|
? `${messageIndex}-${content}`
|
||||||
|
: 'sticky-message-text-area'
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<div className='text-base gap-4 md:gap-6 m-auto md:max-w-2xl lg:max-w-2xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0'>
|
<div className='text-base gap-4 md:gap-6 m-auto md:max-w-2xl lg:max-w-2xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0'>
|
||||||
<Avatar role={role} />
|
<Avatar role={role} />
|
||||||
|
|
|
@ -264,11 +264,12 @@ const EditView = ({
|
||||||
);
|
);
|
||||||
if (sticky) {
|
if (sticky) {
|
||||||
updatedMessages.push({ role: inputRole, content: _content });
|
updatedMessages.push({ role: inputRole, content: _content });
|
||||||
|
_setContent('');
|
||||||
} else {
|
} else {
|
||||||
updatedMessages[messageIndex].content = _content;
|
updatedMessages[messageIndex].content = _content;
|
||||||
|
setIsEdit(false);
|
||||||
}
|
}
|
||||||
setMessages(updatedMessages);
|
setMessages(updatedMessages);
|
||||||
setIsEdit(false);
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div className='flex items-center justify-center gap-2'>Save</div>
|
<div className='flex items-center justify-center gap-2'>Save</div>
|
||||||
|
|
Loading…
Reference in a new issue