UPDATED cache in suggestions

This commit is contained in:
redesyef 2024-09-25 16:06:28 -05:00
parent dcb7c54800
commit 7a887f59bc
5 changed files with 70 additions and 21 deletions

View file

@ -167,7 +167,7 @@ const useSocket = (
'Failed to connect to the server. Please try again later.',
);
}
}, 10000);
}, 1000);
ws.onopen = () => {
console.log('[DEBUG] open');
@ -280,6 +280,8 @@ const ChatWindow = ({ id }: { id?: string }) => {
const [isMessagesLoaded, setIsMessagesLoaded] = useState(false);
const [notFound, setNotFound] = useState(false);
const [cache, setCache] = useState(false);
const [newRequest, setNewRequest] = useState(false);
useEffect(() => {
if (
@ -361,6 +363,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
]);
const messageHandler = async (e: MessageEvent) => {
setNewRequest(true);
const data = JSON.parse(e.data);
if (data.type === 'error') {
@ -371,6 +374,9 @@ const ChatWindow = ({ id }: { id?: string }) => {
if (data.type === 'sources') {
sources = data.data;
if (data.cache) {
setCache(true);
}
if (typeof sources === 'string') {
sources = JSON.parse(data.data);
added = false;
@ -490,12 +496,39 @@ const ChatWindow = ({ id }: { id?: string }) => {
sendMessage(message.content);
};
const getSuggestionsWithPreviewInfo = async () => {
if (
messages[1].role === 'assistant' &&
messages[1].sources &&
messages[1].sources.length > 0 &&
!messages[1].suggestions
) {
const suggestions = await getSuggestions(messagesRef.current);
setMessages((prev) =>
prev.map((msg) => {
if (msg.messageId === messages[1].messageId) {
return { ...msg, suggestions: suggestions };
}
return msg;
}),
);
}
};
useEffect(() => {
if (isReady && initialMessage) {
sendMessage(initialMessage);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isReady, initialMessage]);
useEffect(() => {
if (
(messages.length > 1 && cache) ||
(messages.length > 1 && !newRequest)
) {
getSuggestionsWithPreviewInfo();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [messages]);
if (hasError) {
return (

View file

@ -1,22 +1,31 @@
import { Message } from '@/components/ChatWindow';
export const getSuggestions = async (chatHisory: Message[]) => {
const chatModel = localStorage.getItem('chatModel');
const chatModelProvider = localStorage.getItem('chatModelProvider');
export const getSuggestions = async (chatHistory: Message[]) => {
try {
const chatModel = localStorage.getItem('chatModel');
const chatModelProvider = localStorage.getItem('chatModelProvider');
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/suggestions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
chat_history: chatHisory,
chat_model: chatModel,
chat_model_provider: chatModelProvider,
}),
});
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/suggestions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
chat_history: chatHistory,
chat_model: chatModel,
chat_model_provider: chatModelProvider,
}),
});
const data = (await res.json()) as { suggestions: string[] };
if (!res.ok) {
throw new Error(`Error: ${res.status} ${res.statusText}`);
}
return data.suggestions;
const data = (await res.json()) as { suggestions: string[] };
return data.suggestions;
} catch (error) {
console.error('Error fetching suggestions:', error);
return [];
}
};