Skip to content

Commit

Permalink
saving all messages in chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Aayush-Agnihotri committed Dec 29, 2024
1 parent dd7c439 commit 7e751c4
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions src/app/chat/[chatId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ export default function ChatPage() {
const data = await response.json();
setMessages(data.messages);

if (data.messages && data.messages.length === 1) {
if (data.messages && data.messages.length == 1) {
initiateResponse(data.messages[data.messages.length - 1].content, []);
}
} catch (error) {
console.error(error);
}
};
fetchChat();
}, []);
});

// TODO: Add button to scroll to bottom of chat
useEffect(() => {
Expand All @@ -60,24 +60,24 @@ export default function ChatPage() {
}
}, [messages]);

// const saveMessage = async (message: string, files: string[], sender: string) => {
// const response = await fetch(`/api/chats/${chatId}`, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({
// content: message,
// images: files,
// timestamp: new Date().toISOString(),
// sender: sender,
// }),
// });
const saveMessage = async (message: string, images: string[], sender: string) => {
const response = await fetch(`/api/chats/${chatId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
content: message,
images: images,
timestamp: new Date().toISOString(),
sender: sender,
}),
});

// if (!response.ok) {
// throw new Error('Failed to create chat');
// }
// };
if (!response.ok) {
throw new Error('Failed to create chat');
}
};

const createChatCompletionRequestBody = (message: string, files: FileComponent[]) => {
const body: ChatCompletionRequest = {
Expand All @@ -91,8 +91,8 @@ export default function ChatPage() {
{
role: 'user',
content: message,
// images: files.map((file) => file.base64.split(',')[1]),
images: [],
images: files.map((file) => file.base64.split(',')[1]),
// images: [],
},
],
};
Expand Down Expand Up @@ -131,6 +131,7 @@ export default function ChatPage() {

const processStreamedResponse = async (reader: ReadableStreamDefaultReader) => {
const decoder = new TextDecoder('utf-8');
let completeMessage = '';
let buffer = '';
let done = false;

Expand All @@ -146,6 +147,7 @@ export default function ChatPage() {
if (line.trim()) {
try {
const response: ChatStreamCompletionResponse = JSON.parse(line);
completeMessage += response.message!.content;
updateMessages(response);
if (response.done) {
done = true;
Expand All @@ -162,11 +164,14 @@ export default function ChatPage() {
if (buffer.trim()) {
try {
const response: ChatStreamCompletionResponse = JSON.parse(buffer);
completeMessage += response.message!.content;
updateMessages(response);
} catch (error) {
displayError(error);
}
}

saveMessage(completeMessage, [], selectedModel);
};

const updateMessages = (response: ChatStreamCompletionResponse) => {
Expand Down Expand Up @@ -230,14 +235,18 @@ export default function ChatPage() {
id: `${messages.length}`,
chatId: chatId,
content: message,
// images: files.map((file) => file.base64),
images: [],
images: files.map((file) => file.base64),
// images: [],
timestamp: new Date().toLocaleString(),
sender: 'user',
};

setMessages((prevMessages) => [...prevMessages, userMessage]);
// saveMessage(message, files.map((file) => file.base64.split(',')[1]), 'user');
saveMessage(
message,
files.map((file) => file.base64.split(',')[1]),
'user',
);
initiateResponse(message, files);
};

Expand Down

0 comments on commit 7e751c4

Please sign in to comment.