feat(cli): message queing

This commit is contained in:
uinstinct 2025-09-08 22:01:14 +05:30 • committed by Ting-Wai To
parent 069c033ad6
commit f7e91eccc1
3 changed files with 82 additions and 15 deletions

View file

@ -0,0 +1,57 @@
import { InputHistory } from "../util/inputHistory.js";
import { logger } from "../util/logger.js";
export interface QueuedMessage {
message: string;
imageMap?: Map<string, Buffer>;
timestamp: number;
}
export interface MessageProcessor {
(message: string, imageMap?: Map<string, Buffer>): Promise<void>;
}
/**
* 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<string, Buffer>,
inputHistory?: InputHistory,
): Promise<boolean> {
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();

View file

@ -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<UserInputProps> = ({
}
// 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<UserInputProps> = ({
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<UserInputProps> = ({
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)) {

View file

@ -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);
};