|
12 | 12 | startResponseTiming, |
13 | 13 | isTyping, |
14 | 14 | saveChatHistory, |
15 | | - retryLastUserMessage |
| 15 | + retryLastUserMessage, |
| 16 | + newChat |
16 | 17 | } from '$lib/stores/chat'; |
17 | 18 | import { generateChatResponse, webLLMService } from '$lib/utils/webllm'; |
18 | 19 | import { loadModelWithChatBubble } from '$lib/utils/model-loading'; |
|
253 | 254 | return; |
254 | 255 | } |
255 | 256 |
|
| 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 | +
|
256 | 308 | if (!$isModelLoaded) { |
257 | 309 | const waitingMessage: ChatMessageType = { |
258 | 310 | id: crypto.randomUUID(), |
|
543 | 595 | scrollToBottom(); |
544 | 596 | } catch (error) { |
545 | 597 | 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); |
551 | 616 | } finally { |
552 | 617 | isSubmitting = false; |
553 | 618 | isTyping.set(false); |
|
0 commit comments