Skip to content

Commit 1dcf077

Browse files
more ui changes with collapsable messages and doc previews
1 parent 30b0294 commit 1dcf077

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

src/lib/components/ChatInterface.svelte

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
startResponseTiming,
1313
isTyping,
1414
saveChatHistory,
15-
retryLastUserMessage
15+
retryLastUserMessage,
16+
newChat
1617
} from '$lib/stores/chat';
1718
import { generateChatResponse, webLLMService } from '$lib/utils/webllm';
1819
import { loadModelWithChatBubble } from '$lib/utils/model-loading';
@@ -253,6 +254,57 @@
253254
return;
254255
}
255256
257+
// Delete previous messages command
258+
if (trimmedInput === '/delete-previous' || trimmedInput === 'delete-previous') {
259+
// Add user message to chat history before clearing input
260+
const userMessage: ChatMessageType = {
261+
id: crypto.randomUUID(),
262+
role: 'user',
263+
content: messageInput.trim(),
264+
timestamp: Date.now()
265+
};
266+
addMessage(userMessage);
267+
268+
messageInput = '';
269+
270+
// Remove the second-to-last message if it exists
271+
const messages = $currentMessages;
272+
if (messages.length >= 2) {
273+
const secondToLastMessage = messages[messages.length - 2];
274+
removeMessageById(secondToLastMessage.id);
275+
276+
addMessage({
277+
id: crypto.randomUUID(),
278+
role: 'assistant',
279+
content: '✅ Deleted the previous message. You can now continue the conversation with more context space.',
280+
timestamp: Date.now()
281+
});
282+
} else {
283+
addMessage({
284+
id: crypto.randomUUID(),
285+
role: 'assistant',
286+
content: '❌ No previous message to delete.',
287+
timestamp: Date.now()
288+
});
289+
}
290+
return;
291+
}
292+
293+
// New chat command
294+
if (trimmedInput === '/new-chat' || trimmedInput === 'new-chat') {
295+
newChat();
296+
297+
addMessage({
298+
id: crypto.randomUUID(),
299+
role: 'assistant',
300+
content: '✅ Started a new chat. Previous conversation history has been cleared.',
301+
timestamp: Date.now()
302+
});
303+
304+
messageInput = '';
305+
return;
306+
}
307+
256308
if (!$isModelLoaded) {
257309
const waitingMessage: ChatMessageType = {
258310
id: crypto.randomUUID(),
@@ -543,11 +595,24 @@
543595
scrollToBottom();
544596
} catch (error) {
545597
console.error('Error generating response:', error);
546-
updateLastMessage(
547-
'Sorry, I encountered an error while processing your request. Please try again.',
548-
undefined,
549-
true
550-
);
598+
599+
// Check if error might be due to context length
600+
const isContextError = error instanceof Error &&
601+
(error.message.includes('context') ||
602+
error.message.includes('token') ||
603+
error.message.includes('length') ||
604+
$currentMessages.length > 10);
605+
606+
let errorMessage = '❌ **Error Processing Request**\n\nSorry, I encountered an error while processing your request. Please try again.';
607+
608+
if (isContextError) {
609+
errorMessage += '\n\n💡 **Context may be full** - Try one of these options:\n\n';
610+
errorMessage += '• [Delete previous messages and continue](cmd:delete-previous)\n';
611+
errorMessage += '• [Start a new chat](cmd:new-chat)\n';
612+
errorMessage += '• Use the close button (✕) on messages to remove them manually';
613+
}
614+
615+
updateLastMessage(errorMessage, undefined, true);
551616
} finally {
552617
isSubmitting = false;
553618
isTyping.set(false);

0 commit comments

Comments
 (0)