From f7e91eccc11b21bc42bc4a6aac8946938d8ff416 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Mon, 8 Sep 2025 22:01:14 +0530 Subject: [PATCH] feat(cli): message queing --- extensions/cli/src/stream/messageQueue.ts | 57 +++++++++++++++++++++++ extensions/cli/src/ui/UserInput.tsx | 27 +++++------ extensions/cli/src/ui/hooks/useChat.ts | 13 ++++++ 3 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 extensions/cli/src/stream/messageQueue.ts diff --git a/extensions/cli/src/stream/messageQueue.ts b/extensions/cli/src/stream/messageQueue.ts new file mode 100644 index 000000000..d91f2d398 --- /dev/null +++ b/extensions/cli/src/stream/messageQueue.ts @@ -0,0 +1,57 @@ +import { InputHistory } from "../util/inputHistory.js"; +import { logger } from "../util/logger.js"; + +export interface QueuedMessage { + message: string; + imageMap?: Map; + timestamp: number; +} + +export interface MessageProcessor { + (message: string, imageMap?: Map): Promise; +} + +/** + * A queue to store messages that need to be processed later + */ +class MessageQueue { + private queue: QueuedMessage[] = []; + private inputHistory: InputHistory | undefined; + + async enqueueMessage( + message: string, + imageMap?: Map, + inputHistory?: InputHistory, + ): Promise { + this.inputHistory = inputHistory; + + const queuedMessage: QueuedMessage = { + message, + imageMap, + timestamp: Date.now(), + }; + + this.queue.push(queuedMessage); + logger.debug("MessageQueue: Message queued", { + queueLength: this.queue.length, + }); + + return true; + } + + public getLatestMessage() { + if (this.queue.length === 0) return undefined; + const latestMessage = this.queue.shift(); + logger.debug("MessageQueue: Message dequeued", { + queueLength: this.queue.length, + }); + this.inputHistory?.addEntry(latestMessage!.message); + return latestMessage; + } + + getQueueLength(): number { + return this.queue.length; + } +} + +export const messageQueue = new MessageQueue(); diff --git a/extensions/cli/src/ui/UserInput.tsx b/extensions/cli/src/ui/UserInput.tsx index 4b5deb5bc..97b04949a 100644 --- a/extensions/cli/src/ui/UserInput.tsx +++ b/extensions/cli/src/ui/UserInput.tsx @@ -9,6 +9,7 @@ import type { PermissionMode } from "../permissions/types.js"; import type { FileIndexServiceState } from "../services/FileIndexService.js"; import { SERVICE_NAMES, serviceContainer } from "../services/index.js"; import { modeService } from "../services/ModeService.js"; +import { messageQueue } from "../stream/messageQueue.js"; import { InputHistory } from "../util/inputHistory.js"; import { FileSearchUI } from "./FileSearchUI.js"; @@ -515,7 +516,7 @@ const UserInput: React.FC = ({ } // Normal Enter behavior - submit if there's content - if ((textBuffer.text.trim() || wasInterrupted) && !isWaitingForResponse) { + if (textBuffer.text.trim() || wasInterrupted) { // Get images before expanding paste blocks const imageMap = textBuffer.getAllImages(); @@ -523,14 +524,19 @@ const UserInput: React.FC = ({ textBuffer.expandAllPasteBlocks(); const submittedText = textBuffer.text.trim(); - // Only add to history if there's actual text (not when resuming) - if (submittedText) { + if (isWaitingForResponse) { + // Process message later when LLM has responded + void messageQueue.enqueueMessage( + submittedText, + imageMap, + inputHistory, + ); + } else { + // Submit with images inputHistory.addEntry(submittedText); + onSubmit(submittedText, imageMap); } - // Submit with images - onSubmit(submittedText, imageMap); - textBuffer.clear(); setInputText(""); setCursorPosition(0); @@ -647,15 +653,6 @@ const UserInput: React.FC = ({ return; } - // Allow typing during streaming, but block submission - if (!inputMode) { - // Block only Enter key submission during streaming - if (key.return && !key.shift) { - return; - } - // Allow all other input (typing, navigation, etc.) - } - // Handle slash command navigation if (showSlashCommands) { if (handleSlashCommandNavigation(key)) { diff --git a/extensions/cli/src/ui/hooks/useChat.ts b/extensions/cli/src/ui/hooks/useChat.ts index 9b9cc5871..ed8fa2cd4 100644 --- a/extensions/cli/src/ui/hooks/useChat.ts +++ b/extensions/cli/src/ui/hooks/useChat.ts @@ -11,6 +11,7 @@ import { updateSessionHistory, } from "../../session.js"; import { handleSlashCommands } from "../../slashCommands.js"; +import { messageQueue } from "../../stream/messageQueue.js"; import { telemetryService } from "../../telemetry/telemetryService.js"; import { formatError } from "../../util/formatError.js"; import { logger } from "../../util/logger.js"; @@ -306,6 +307,16 @@ export function useChat({ setIsWaitingForResponse(false); setResponseStartTime(null); setInputMode(true); + + // Check if there are queued messages and process them after a microtask delay + // This ensures the GUI state has been updated before processing the next message + const latestQueuedMessage = messageQueue.getLatestMessage()?.message; + if (latestQueuedMessage) { + logger.debug("processing queued message", { latestQueuedMessage }); + await new Promise((resolve) => setTimeout(resolve, 100)); + // Note: InputHistory entry was already added when the message was first submitted + await handleUserMessage(latestQueuedMessage); + } } }; @@ -484,6 +495,8 @@ export function useChat({ const newHistory = [...currentChatHistory, newUserMessage]; setChatHistory(newHistory); + logger.debug("debug1 streaming started again"); + // Execute the streaming response await executeStreamingResponse(newHistory, currentCompactionIndex); };