From fc33179adce9960e24b905c7ed04a6d2709e25d1 Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 29 Sep 2025 17:17:02 +0700 Subject: [PATCH] wip queue messages while the model works --- src/components/Shakespeare/ChatPane.tsx | 181 +++++++++++++++++++++--- src/hooks/useMessageQueue.ts | 34 +++++ 2 files changed, 197 insertions(+), 18 deletions(-) create mode 100644 src/hooks/useMessageQueue.ts diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index 02721a19..cc23ac43 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -6,15 +6,17 @@ import { Textarea } from '@/components/ui/textarea'; import { Skeleton } from '@/components/ui/skeleton'; import { CircularProgress } from '@/components/ui/circular-progress'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'; +import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { FileAttachment } from '@/components/ui/file-attachment'; import { OnboardingDialog } from '@/components/OnboardingDialog'; -import { Square, Loader2, ChevronDown, ArrowUp } from 'lucide-react'; +import { Square, Loader2, ChevronDown, ArrowUp, Plus, X } from 'lucide-react'; import { useAISettings } from '@/hooks/useAISettings'; import { useFS } from '@/hooks/useFS'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useKeepAlive } from '@/hooks/useKeepAlive'; import { useAIChat } from '@/hooks/useAIChat'; import { useProviderModels } from '@/hooks/useProviderModels'; +import { useMessageQueue } from '@/hooks/useMessageQueue'; import { ModelSelector } from '@/components/ModelSelector'; import { AIMessageItem } from '@/components/AIMessageItem'; import { TextEditorViewTool } from '@/lib/tools/TextEditorViewTool'; @@ -84,6 +86,10 @@ export const ChatPane = forwardRef(({ const [showOnboarding, setShowOnboarding] = useState(false); const [isStuck, setIsStuck] = useState(false); const [aiError, setAIError] = useState(null); + const [showQueueModal, setShowQueueModal] = useState(false); + + // Message queue for handling messages while AI is working + const { queuedMessages, addToQueue, clearQueue, removeFromQueue, hasQueuedMessages, queueLength } = useMessageQueue(); // Determine which error to show - console error takes priority over AI errors const displayError = consoleError || aiError; @@ -232,6 +238,64 @@ export const ChatPane = forwardRef(({ // Use external loading state if provided, otherwise use internal state const isLoading = externalIsLoading !== undefined ? externalIsLoading : internalIsLoading; + // Process queued messages when AI becomes available + const processQueuedMessages = useCallback(async () => { + if (isLoading || !isConfigured || !providerModel.trim() || queuedMessages.length === 0) return; + + const messagesToProcess = [...queuedMessages]; + // Clear the queue first to prevent re-processing + clearQueue(); + + const modelToUse = providerModel.trim(); + addRecentlyUsedModel(modelToUse); + + // Send each queued message individually + for (const queuedMessage of messagesToProcess) { + const contentParts: Array = []; + + // Add text content if present + if (queuedMessage.content.trim()) { + contentParts.push({ + type: 'text', + text: queuedMessage.content.trim() + }); + } + + // Process attached files for this message + if (queuedMessage.attachedFiles.length > 0) { + const filePromises = queuedMessage.attachedFiles.map(async (file) => { + try { + const savedPath = await saveFileToTmp(fs, file); + return `Added file: ${savedPath}`; + } catch (error) { + console.error('Failed to save file:', error); + return `Failed to save file: ${file.name}`; + } + }); + + const fileResults = await Promise.all(filePromises); + fileResults.forEach(fileResult => { + contentParts.push({ + type: 'text', + text: fileResult + }); + }); + } + + // Only send if there's content + if (contentParts.length > 0) { + const messageContent = contentParts.length === 1 ? contentParts[0].text : contentParts; + await sendMessage(messageContent, modelToUse); + } + } + }, [addRecentlyUsedModel, clearQueue, fs, isConfigured, isLoading, providerModel, queuedMessages, sendMessage]); + + useEffect(() => { + if (!isLoading && hasQueuedMessages) { + processQueuedMessages(); + } + }, [isLoading, hasQueuedMessages, processQueuedMessages]); + // Calculate context usage percentage const currentModel = useMemo(() => { if (!providerModel.trim()) return null; @@ -384,8 +448,6 @@ export const ChatPane = forwardRef(({ e.stopPropagation(); setIsDragOver(false); - if (isLoading || !providerModel.trim()) return; - const files = Array.from(e.dataTransfer.files); if (files.length === 0) return; @@ -394,7 +456,15 @@ export const ChatPane = forwardRef(({ }; const send = useCallback(async (input: string, attachedFiles: File[]) => { - if ((!input.trim() && attachedFiles.length === 0) || isLoading) return; + if ((!input.trim() && attachedFiles.length === 0)) return; + + // If AI is loading, add to queue instead of sending immediately + if (isLoading) { + addToQueue(input, attachedFiles); + setInput(''); + setAttachedFiles([]); + return; + } // If AI is not configured, show onboarding dialog if (!isConfigured) { @@ -452,7 +522,7 @@ export const ChatPane = forwardRef(({ // Send as text parts if we have multiple parts, otherwise send as string for simplicity const messageContent = contentParts.length === 1 ? contentParts[0].text : contentParts; await sendMessage(messageContent, modelToUse); - }, [addRecentlyUsedModel, fs, isConfigured, isLoading, providerModel, sendMessage]); + }, [addRecentlyUsedModel, addToQueue, fs, isConfigured, isLoading, providerModel, sendMessage]); const handleSend = useCallback(async () => { await send(input, attachedFiles); @@ -662,11 +732,11 @@ export const ChatPane = forwardRef(({ !isConfigured ? t('askToAddFeatures') : providerModel.trim() - ? t('askToAddFeatures') - : t('selectModelFirst') + ? t('askToAddFeatures') + : t('selectModelFirst') } className="flex-1 resize-none border-0 bg-transparent px-4 py-3 text-sm focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:text-muted-foreground" - disabled={isLoading || (isConfigured && !providerModel.trim())} + disabled={isConfigured && !providerModel.trim()} rows={1} aria-label="Chat message input" style={{ @@ -680,6 +750,16 @@ export const ChatPane = forwardRef(({ }} /> + {/* Queued Messages Indicator */} + {hasQueuedMessages && ( + + )} + {/* Bottom Controls Row */}
{/* File Attachment */} @@ -687,7 +767,7 @@ export const ChatPane = forwardRef(({ onFileSelect={handleFileSelect} onFileRemove={handleFileRemove} selectedFiles={attachedFiles} - disabled={isLoading} + disabled={false} multiple={true} /> @@ -756,21 +836,86 @@ export const ChatPane = forwardRef(({ ) : ( - + + + + + + {hasQueuedMessages && ( + +

{queueLength} message{queueLength > 1 ? 's' : ''} queued

+
+ )} +
+
)}
+ {/* Queue Management Modal */} + + + + Queued Messages + +
+ {queuedMessages.map((message, index) => ( +
+
+

+ {message.content || No text content} +

+ {message.attachedFiles.length > 0 && ( +

+ {message.attachedFiles.length} file{message.attachedFiles.length > 1 ? 's' : ''} attached +

+ )} +
+ +
+ ))} + {queuedMessages.length === 0 && ( +

No messages queued

+ )} +
+ + +
+
+
+
+ {/* Onboarding Dialog */} >([]); + + const addToQueue = useCallback((content: string, attachedFiles: File[] = []) => { + if (!content.trim() && attachedFiles.length === 0) return; + setQueuedMessages(prev => [...prev, { content: content.trim(), attachedFiles }]); + }, []); + + const clearQueue = useCallback(() => { + setQueuedMessages([]); + }, []); + + const removeFromQueue = useCallback((index: number) => { + setQueuedMessages(prev => prev.filter((_, i) => i !== index)); + }, []); + + const hasQueuedMessages = queuedMessages.length > 0; + const queueLength = queuedMessages.length; + + return { + queuedMessages, + addToQueue, + clearQueue, + removeFromQueue, + hasQueuedMessages, + queueLength + }; +}