-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
148 lines (121 loc) · 6.06 KB
/
Copy pathscript.js
File metadata and controls
148 lines (121 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
document.addEventListener('DOMContentLoaded', () => {
const chatMessages = document.getElementById('chat-messages');
const userInput = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
// Function to add a message to the chat
function addMessage(message, isUser = false) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${isUser ? 'user' : 'bot'}`;
const messageContent = document.createElement('div');
messageContent.className = 'message-content';
// Convert newlines to <br> tags for proper paragraph formatting
const formattedMessage = message
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\n/g, '<br>');
messageContent.innerHTML = formattedMessage;
messageDiv.appendChild(messageContent);
chatMessages.appendChild(messageDiv);
// Scroll to the bottom of the chat
chatMessages.scrollTop = chatMessages.scrollHeight;
return messageContent; // Return the content element for streaming updates
}
// Function to handle sending a message
async function sendMessage() {
const message = userInput.value.trim();
if (message === '') return;
// Add user message to chat
addMessage(message, true);
userInput.value = '';
// Create bot message container with loading indicator
const botDiv = document.createElement('div');
botDiv.className = 'message bot';
const botContent = document.createElement('div');
botContent.className = 'message-content';
botContent.textContent = 'Typing...';
botDiv.appendChild(botContent);
chatMessages.appendChild(botDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
let fullResponse = '';
// Send the message to the server with streaming response
fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message }),
}).then(response => {
if (!response.ok) {
throw new Error('Failed to get response');
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
// Function to read stream chunks
function readChunk() {
return reader.read().then(({ value, done }) => {
if (done) {
console.log('Stream complete');
return;
}
const chunk = decoder.decode(value, { stream: true });
// Process the chunk (which might contain multiple SSE messages)
const lines = chunk.split('\n\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
try {
const data = JSON.parse(line.substring(6));
if (data.content) {
// If this is the first chunk, clear the "Typing..." text
if (fullResponse === '') {
botContent.innerHTML = '';
}
// Append new content
fullResponse += data.content;
// Format with paragraph breaks
const formattedResponse = fullResponse
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\n/g, '<br>');
botContent.innerHTML = formattedResponse;
chatMessages.scrollTop = chatMessages.scrollHeight;
}
if (data.error) {
console.error('Error in stream:', data.error);
botContent.innerHTML = 'Sorry, I encountered an error. Please try again.';
return; // Stop reading chunks
}
if (data.done) {
console.log('Stream done');
return; // Stop reading chunks
}
} catch (error) {
console.error('Error parsing SSE data:', error);
}
}
}
// Continue reading chunks
return readChunk();
}).catch(error => {
console.error('Error reading stream:', error);
if (fullResponse === '') {
botContent.innerHTML = 'Sorry, I encountered an error. Please try again.';
}
});
}
// Start reading the stream
return readChunk();
}).catch(error => {
console.error('Fetch error:', error);
botContent.innerHTML = 'Sorry, I encountered an error. Please try again.';
});
}
// Event listeners
sendButton.addEventListener('click', sendMessage);
userInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
});