Changesets · Branches · Tags · Compare
| Changeset | 78599bd09fb681bc527f0a34aa611fc86ec081213182adc05e443e01d5772a0f |
|---|---|
| Author | Nate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-09-23 23:39 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-09-23 23:39 -07:00 |
| Parents | 0e7d07cb2a28 |
8 files changed
modified extensions/cli/src/commands/serve.helpers.ts +0 −2
| @@ -119,9 +119,7 @@ | ||
| 119 | 119 | model: any; |
| 120 | 120 | isProcessing: boolean; |
| 121 | 121 | lastActivity: number; |
| 122 | - messageQueue: string[]; | |
| 123 | 122 | currentAbortController: AbortController | null; |
| 124 | - shouldInterrupt: boolean; | |
| 125 | 123 | serverRunning: boolean; |
| 126 | 124 | pendingPermission: PendingPermission | null; |
| 127 | 125 | systemMessage?: string; |
modified extensions/cli/src/commands/serve.ts +15 −21
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | ModelServiceState, |
| 18 | 18 | } from "../services/types.js"; |
| 19 | 19 | import { createSession, getSessionPersistenceSnapshot } from "../session.js"; |
| 20 | +import { messageQueue } from "../stream/messageQueue.js"; | |
| 20 | 21 | import { constructSystemMessage } from "../systemMessage.js"; |
| 21 | 22 | import { telemetryService } from "../telemetry/telemetryService.js"; |
| 22 | 23 | import { gracefulExit } from "../util/exit.js"; |
| @@ -138,9 +139,7 @@ | ||
| 138 | 139 | model, |
| 139 | 140 | isProcessing: false, |
| 140 | 141 | lastActivity: Date.now(), |
| 141 | - messageQueue: [], | |
| 142 | 142 | currentAbortController: null, |
| 143 | - shouldInterrupt: false, | |
| 144 | 143 | serverRunning: true, |
| 145 | 144 | pendingPermission: null, |
| 146 | 145 | }; |
| @@ -185,7 +184,7 @@ | ||
| 185 | 184 | res.json({ |
| 186 | 185 | session: state.session, // Return session directly instead of converting |
| 187 | 186 | isProcessing: state.isProcessing, |
| 188 | - messageQueueLength: state.messageQueue.length, | |
| 187 | + messageQueueLength: messageQueue.getQueueLength(), | |
| 189 | 188 | pendingPermission: state.pendingPermission, |
| 190 | 189 | }); |
| 191 | 190 | }); |
| @@ -200,17 +199,11 @@ | ||
| 200 | 199 | } |
| 201 | 200 | |
| 202 | 201 | // Queue the message |
| 203 | - state.messageQueue.push(message); | |
| 204 | - | |
| 205 | - // If currently processing, set interrupt flag | |
| 206 | - if (state.isProcessing && state.currentAbortController) { | |
| 207 | - state.shouldInterrupt = true; | |
| 208 | - } | |
| 202 | + await messageQueue.enqueueMessage(message); | |
| 209 | 203 | |
| 210 | 204 | res.json({ |
| 211 | 205 | queued: true, |
| 212 | - position: state.messageQueue.length, | |
| 213 | - willInterrupt: state.shouldInterrupt, | |
| 206 | + position: messageQueue.getQueueLength(), | |
| 214 | 207 | }); |
| 215 | 208 | |
| 216 | 209 | // Process messages if not already processing |
| @@ -303,9 +296,6 @@ | ||
| 303 | 296 | state.currentAbortController.abort(); |
| 304 | 297 | } |
| 305 | 298 | |
| 306 | - // Clear the message queue | |
| 307 | - state.messageQueue = []; | |
| 308 | - | |
| 309 | 299 | // Clean up intervals |
| 310 | 300 | if (inactivityChecker) { |
| 311 | 301 | clearInterval(inactivityChecker); |
| @@ -325,7 +315,7 @@ | ||
| 325 | 315 | setTimeout(handleExitResponse, 100); |
| 326 | 316 | }); |
| 327 | 317 | |
| 328 | - const server = app.listen(port, () => { | |
| 318 | + const server = app.listen(port, async () => { | |
| 329 | 319 | console.log(chalk.green(`Server started on http://localhost:${port}`)); |
| 330 | 320 | console.log(chalk.dim("Endpoints:")); |
| 331 | 321 | console.log(chalk.dim(" GET /state - Get current agent state")); |
| @@ -354,7 +344,7 @@ | ||
| 354 | 344 | // If initial prompt provided, queue it for processing |
| 355 | 345 | if (actualPrompt) { |
| 356 | 346 | console.log(chalk.dim("\nProcessing initial prompt...")); |
| 357 | - state.messageQueue.push(actualPrompt); | |
| 347 | + await messageQueue.enqueueMessage(actualPrompt); | |
| 358 | 348 | processMessages(state, llmApi); |
| 359 | 349 | } |
| 360 | 350 | }); |
| @@ -383,10 +373,14 @@ | ||
| 383 | 373 | |
| 384 | 374 | async function processMessages(state: ServerState, llmApi: any) { |
| 385 | 375 | let processedMessage = false; |
| 386 | - while (state.messageQueue.length > 0 && state.serverRunning) { | |
| 387 | - const userMessage = state.messageQueue.shift()!; | |
| 376 | + while (state.serverRunning) { | |
| 377 | + const queuedMessage = messageQueue.getNextMessage(); | |
| 378 | + if (!queuedMessage) { | |
| 379 | + break; | |
| 380 | + } | |
| 381 | + | |
| 382 | + const userMessage = queuedMessage.message; | |
| 388 | 383 | state.isProcessing = true; |
| 389 | - state.shouldInterrupt = false; | |
| 390 | 384 | state.lastActivity = Date.now(); |
| 391 | 385 | processedMessage = true; |
| 392 | 386 | |
| @@ -410,7 +404,7 @@ | ||
| 410 | 404 | state, |
| 411 | 405 | llmApi, |
| 412 | 406 | state.currentAbortController, |
| 413 | - () => state.shouldInterrupt, | |
| 407 | + () => false, | |
| 414 | 408 | ); |
| 415 | 409 | |
| 416 | 410 | // No direct persistence here; ChatHistoryService handles persistence when appropriate |
| @@ -443,7 +437,7 @@ | ||
| 443 | 437 | if ( |
| 444 | 438 | processedMessage && |
| 445 | 439 | state.serverRunning && |
| 446 | - state.messageQueue.length === 0 | |
| 440 | + messageQueue.getQueueLength() === 0 | |
| 447 | 441 | ) { |
| 448 | 442 | await storageSyncService.markAgentStatusUnread(); |
| 449 | 443 | } |
modified extensions/cli/src/hubLoader.test.ts +45 −2
| @@ -1,12 +1,15 @@ | ||
| 1 | 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | 2 | |
| 3 | -import { | |
| 3 | +import * as hubLoader from "./hubLoader.js"; | |
| 4 | + | |
| 5 | +const { | |
| 4 | 6 | loadPackageFromHub, |
| 5 | 7 | mcpProcessor, |
| 6 | 8 | modelProcessor, |
| 7 | 9 | promptProcessor, |
| 8 | 10 | ruleProcessor, |
| 9 | -} from "./hubLoader.js"; | |
| 11 | + processRule, | |
| 12 | +} = hubLoader; | |
| 10 | 13 | |
| 11 | 14 | // Store the original fetch before any mocking |
| 12 | 15 | const originalFetch = global.fetch; |
| @@ -136,6 +139,46 @@ | ||
| 136 | 139 | }); |
| 137 | 140 | }); |
| 138 | 141 | |
| 142 | + describe("processRule", () => { | |
| 143 | + beforeEach(() => { | |
| 144 | + // Ensure mocks are set up properly for processRule tests | |
| 145 | + global.fetch = mockFetch; | |
| 146 | + }); | |
| 147 | + | |
| 148 | + it("should treat multi-line strings as inline rules", async () => { | |
| 149 | + const loadRuleSpy = vi | |
| 150 | + .spyOn(hubLoader, "loadRuleFromHub") | |
| 151 | + .mockResolvedValue("hub-content"); | |
| 152 | + | |
| 153 | + const multilineRule = "Line one\nEnsure /tmp is ignored\nFinal line"; | |
| 154 | + const result = await processRule(multilineRule); | |
| 155 | + | |
| 156 | + expect(result).toBe(multilineRule); | |
| 157 | + expect(loadRuleSpy).not.toHaveBeenCalled(); | |
| 158 | + | |
| 159 | + loadRuleSpy.mockRestore(); | |
| 160 | + }); | |
| 161 | + | |
| 162 | + it("should still treat owner/package strings as hub slugs", async () => { | |
| 163 | + // Mock fetch to return a proper response for this test | |
| 164 | + const JSZip = (await import("jszip")).default; | |
| 165 | + const zip = new JSZip(); | |
| 166 | + zip.file("rule.md", "hub-content"); | |
| 167 | + const zipBuffer = await zip.generateAsync({ type: "arraybuffer" }); | |
| 168 | + | |
| 169 | + mockFetch.mockResolvedValueOnce({ | |
| 170 | + ok: true, | |
| 171 | + arrayBuffer: () => Promise.resolve(zipBuffer), | |
| 172 | + } as Response); | |
| 173 | + | |
| 174 | + const slug = "owner/package"; | |
| 175 | + const result = await processRule(slug); | |
| 176 | + | |
| 177 | + expect(result).toBe("hub-content"); | |
| 178 | + expect(mockFetch).toHaveBeenCalled(); | |
| 179 | + }); | |
| 180 | + }); | |
| 181 | + | |
| 139 | 182 | describe("processors", () => { |
| 140 | 183 | it("should define rule processor correctly", () => { |
| 141 | 184 | expect(ruleProcessor.type).toBe("rule"); |
modified extensions/cli/src/hubLoader.ts +35 −18
| @@ -3,6 +3,11 @@ | ||
| 3 | 3 | import { env } from "./env.js"; |
| 4 | 4 | import { logger } from "./util/logger.js"; |
| 5 | 5 | |
| 6 | +/** | |
| 7 | + * Pattern to match valid hub slugs (owner/package format) | |
| 8 | + */ | |
| 9 | +const HUB_SLUG_PATTERN = /^[A-Za-z0-9._-]+\/[A-Za-z0-9._-]+$/; | |
| 10 | + | |
| 6 | 11 | /** |
| 7 | 12 | * Hub package type definitions |
| 8 | 13 | */ |
| @@ -220,28 +225,23 @@ | ||
| 220 | 225 | * Process a rule specification - supports file paths, hub slugs, or direct content |
| 221 | 226 | */ |
| 222 | 227 | export async function processRule(ruleSpec: string): Promise<string> { |
| 223 | - // If it looks like a hub slug (contains / but doesn't start with . or /) | |
| 224 | - if ( | |
| 225 | - ruleSpec.includes("/") && | |
| 226 | - !ruleSpec.startsWith(".") && | |
| 227 | - !ruleSpec.startsWith("/") | |
| 228 | - ) { | |
| 229 | - return await loadRuleFromHub(ruleSpec); | |
| 230 | - } | |
| 231 | - | |
| 232 | - // If it looks like a file path | |
| 233 | - if ( | |
| 234 | - ruleSpec.startsWith(".") || | |
| 235 | - ruleSpec.startsWith("/") || | |
| 236 | - ruleSpec.includes("/") || | |
| 237 | - ruleSpec.includes("\\") || | |
| 238 | - /\.[a-zA-Z]+$/.test(ruleSpec) // Has file extension at the end | |
| 239 | - ) { | |
| 228 | + const trimmedRuleSpec = ruleSpec.trim(); | |
| 229 | + const hasNewline = /[\r\n]/.test(ruleSpec); | |
| 230 | + | |
| 231 | + // If it looks like a file path (single line, typical path indicators) | |
| 232 | + const looksLikePath = | |
| 233 | + !hasNewline && | |
| 234 | + (trimmedRuleSpec.startsWith(".") || | |
| 235 | + trimmedRuleSpec.startsWith("/") || | |
| 236 | + trimmedRuleSpec.includes("\\") || | |
| 237 | + /\.[a-zA-Z]+$/.test(trimmedRuleSpec)); | |
| 238 | + | |
| 239 | + if (looksLikePath) { | |
| 240 | 240 | const fs = await import("fs"); |
| 241 | 241 | const path = await import("path"); |
| 242 | 242 | |
| 243 | 243 | try { |
| 244 | - const absolutePath = path.resolve(ruleSpec); | |
| 244 | + const absolutePath = path.resolve(trimmedRuleSpec); | |
| 245 | 245 | if (!fs.existsSync(absolutePath)) { |
| 246 | 246 | throw new Error(`Rule file not found: ${ruleSpec}`); |
| 247 | 247 | } |
| @@ -253,6 +253,23 @@ | ||
| 253 | 253 | } |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | + // Check if it might be a hub slug (contains "/" and is a single line) | |
| 257 | + if (!hasNewline && trimmedRuleSpec.includes("/")) { | |
| 258 | + const parts = trimmedRuleSpec.split("/"); | |
| 259 | + | |
| 260 | + // If it's exactly 2 parts and matches hub slug pattern, treat as hub slug | |
| 261 | + if (parts.length === 2 && HUB_SLUG_PATTERN.test(trimmedRuleSpec)) { | |
| 262 | + return await loadRuleFromHub(trimmedRuleSpec); | |
| 263 | + } | |
| 264 | + | |
| 265 | + // If it has more than 2 parts, it's an invalid hub slug | |
| 266 | + if (parts.length > 2) { | |
| 267 | + throw new Error( | |
| 268 | + `Invalid hub slug format. Expected "owner/package", got: ${trimmedRuleSpec}`, | |
| 269 | + ); | |
| 270 | + } | |
| 271 | + } | |
| 272 | + | |
| 256 | 273 | // Otherwise, treat it as direct string content |
| 257 | 274 | return ruleSpec; |
| 258 | 275 | } |
modified extensions/cli/src/stream/messageQueue.test.ts +1 −32
| @@ -1,4 +1,4 @@ | ||
| 1 | -import { beforeEach, describe, expect, it, vi } from "vitest"; | |
| 1 | +import { describe, expect, it, vi } from "vitest"; | |
| 2 | 2 | |
| 3 | 3 | vi.mock("../util/logger.js", () => ({ |
| 4 | 4 | logger: { |
| @@ -16,11 +16,6 @@ | ||
| 16 | 16 | import { messageQueue } from "./messageQueue.js"; |
| 17 | 17 | |
| 18 | 18 | describe("messageQueue", () => { |
| 19 | - beforeEach(() => { | |
| 20 | - // Drain any leftover items from previous tests | |
| 21 | - while (messageQueue.getAllQueuedMessages()) {} | |
| 22 | - }); | |
| 23 | - | |
| 24 | 19 | it("enqueues and emits messageQueued", async () => { |
| 25 | 20 | const event = new Promise<any>((resolve) => |
| 26 | 21 | messageQueue.once("messageQueued", resolve), |
| @@ -33,30 +28,4 @@ | ||
| 33 | 28 | const queued = await event; |
| 34 | 29 | expect(queued.message).toBe("hello world"); |
| 35 | 30 | }); |
| 36 | - | |
| 37 | - it("combines messages, merges image maps, writes history, and clears queue", async () => { | |
| 38 | - const bufA = Buffer.from("A"); | |
| 39 | - const bufB = Buffer.from("B"); | |
| 40 | - | |
| 41 | - const img1 = new Map<string, Buffer>([["a", bufA]]); | |
| 42 | - const img2 = new Map<string, Buffer>([["b", bufB]]); | |
| 43 | - | |
| 44 | - const history = { addEntry: vi.fn() } as any; | |
| 45 | - | |
| 46 | - await messageQueue.enqueueMessage("one", img1); | |
| 47 | - await messageQueue.enqueueMessage("two", img2, history); | |
| 48 | - | |
| 49 | - const combined = messageQueue.getAllQueuedMessages(); | |
| 50 | - expect(combined).toBeTruthy(); | |
| 51 | - expect(combined!.message).toBe("one\ntwo"); | |
| 52 | - expect(combined!.imageMap).toBeDefined(); | |
| 53 | - expect(combined!.imageMap!.get("a")).toEqual(bufA); | |
| 54 | - expect(combined!.imageMap!.get("b")).toEqual(bufB); | |
| 55 | - | |
| 56 | - // queue should be cleared | |
| 57 | - expect(messageQueue.getQueueLength()).toBe(0); | |
| 58 | - | |
| 59 | - // input history should receive the combined message | |
| 60 | - expect(history.addEntry).toHaveBeenCalledWith("one\ntwo"); | |
| 61 | - }); | |
| 62 | 31 | }); |
modified extensions/cli/src/stream/messageQueue.ts +9 −41
| @@ -1,12 +1,13 @@ | ||
| 1 | 1 | import { EventEmitter } from "events"; |
| 2 | 2 | |
| 3 | -import { InputHistory } from "../util/inputHistory.js"; | |
| 3 | +import type { InputHistory } from "../util/inputHistory.js"; | |
| 4 | 4 | import { logger } from "../util/logger.js"; |
| 5 | 5 | |
| 6 | 6 | export interface QueuedMessage { |
| 7 | 7 | message: string; |
| 8 | 8 | imageMap?: Map<string, Buffer>; |
| 9 | 9 | timestamp: number; |
| 10 | + history?: InputHistory; | |
| 10 | 11 | } |
| 11 | 12 | |
| 12 | 13 | export interface MessageProcessor { |
| @@ -18,7 +19,6 @@ | ||
| 18 | 19 | */ |
| 19 | 20 | class MessageQueue extends EventEmitter { |
| 20 | 21 | private queue: QueuedMessage[] = []; |
| 21 | - private inputHistory: InputHistory | undefined; | |
| 22 | 22 | |
| 23 | 23 | constructor() { |
| 24 | 24 | super(); |
| @@ -27,14 +27,13 @@ | ||
| 27 | 27 | async enqueueMessage( |
| 28 | 28 | message: string, |
| 29 | 29 | imageMap?: Map<string, Buffer>, |
| 30 | - inputHistory?: InputHistory, | |
| 30 | + history?: InputHistory, | |
| 31 | 31 | ): Promise<boolean> { |
| 32 | - this.inputHistory = inputHistory; | |
| 33 | - | |
| 34 | 32 | const queuedMessage: QueuedMessage = { |
| 35 | 33 | message, |
| 36 | 34 | imageMap, |
| 37 | 35 | timestamp: Date.now(), |
| 36 | + history, | |
| 38 | 37 | }; |
| 39 | 38 | |
| 40 | 39 | this.queue.push(queuedMessage); |
| @@ -48,42 +47,11 @@ | ||
| 48 | 47 | return true; |
| 49 | 48 | } |
| 50 | 49 | |
| 51 | - public getAllQueuedMessages() { | |
| 52 | - if (this.queue.length === 0) return undefined; | |
| 53 | - | |
| 54 | - // Combine all queued messages into one | |
| 55 | - const combinedMessage = this.queue.map((msg) => msg.message).join("\n"); | |
| 56 | - const combinedImageMap = new Map<string, Buffer>(); | |
| 57 | - | |
| 58 | - // Merge all image maps | |
| 59 | - for (const queuedMsg of this.queue) { | |
| 60 | - if (queuedMsg.imageMap) { | |
| 61 | - for (const [key, value] of queuedMsg.imageMap) { | |
| 62 | - combinedImageMap.set(key, value); | |
| 63 | - } | |
| 64 | - } | |
| 65 | - } | |
| 66 | - | |
| 67 | - const result = { | |
| 68 | - message: combinedMessage, | |
| 69 | - imageMap: combinedImageMap.size > 0 ? combinedImageMap : undefined, | |
| 70 | - timestamp: Date.now(), | |
| 71 | - }; | |
| 72 | - | |
| 73 | - // Add to input history and clear the queue | |
| 74 | - this.inputHistory?.addEntry(combinedMessage); | |
| 75 | - this.queue = []; | |
| 76 | - | |
| 77 | - logger.debug("MessageQueue: All messages dequeued and combined", { | |
| 78 | - combinedLength: combinedMessage.length, | |
| 79 | - queueLength: this.queue.length, | |
| 80 | - }); | |
| 81 | - | |
| 82 | - return result; | |
| 83 | - } | |
| 84 | - | |
| 85 | - public getLatestMessage() { | |
| 86 | - return this.getAllQueuedMessages(); | |
| 50 | + /** | |
| 51 | + * Dequeues and returns the next message to be processed (FIFO - oldest first) | |
| 52 | + */ | |
| 53 | + public getNextMessage(): QueuedMessage | undefined { | |
| 54 | + return this.queue.shift(); | |
| 87 | 55 | } |
| 88 | 56 | |
| 89 | 57 | getQueueLength(): number { |
modified extensions/cli/src/ui/UserInput.tsx +5 −7
| @@ -548,16 +548,14 @@ | ||
| 548 | 548 | textBuffer.expandAllPasteBlocks(); |
| 549 | 549 | const submittedText = textBuffer.text.trim(); |
| 550 | 550 | |
| 551 | - if (isWaitingForResponse || isCompacting) { | |
| 551 | + inputHistory.addEntry(submittedText); | |
| 552 | + | |
| 553 | + // We don't queue, we just send in remote mode because the server handles queueing | |
| 554 | + if (!isRemoteMode && (isWaitingForResponse || isCompacting)) { | |
| 552 | 555 | // Process message later when LLM has responded or compaction is complete |
| 553 | - void messageQueue.enqueueMessage( | |
| 554 | - submittedText, | |
| 555 | - imageMap, | |
| 556 | - inputHistory, | |
| 557 | - ); | |
| 556 | + void messageQueue.enqueueMessage(submittedText, imageMap); | |
| 558 | 557 | } else { |
| 559 | 558 | // Submit with images |
| 560 | - inputHistory.addEntry(submittedText); | |
| 561 | 559 | onSubmit(submittedText, imageMap); |
| 562 | 560 | } |
| 563 | 561 |
modified extensions/cli/src/ui/hooks/useChat.ts +2 −2
| @@ -337,13 +337,13 @@ | ||
| 337 | 337 | |
| 338 | 338 | // Check if there are queued messages and process them after a microtask delay |
| 339 | 339 | // This ensures the GUI state has been updated before processing the next message |
| 340 | - const queuedMessageData = messageQueue.getLatestMessage(); | |
| 340 | + const queuedMessageData = messageQueue.getNextMessage(); | |
| 341 | 341 | if (queuedMessageData) { |
| 342 | 342 | const { message: latestQueuedMessage, imageMap } = queuedMessageData; |
| 343 | 343 | logger.debug("processing queued message", { latestQueuedMessage }); |
| 344 | 344 | |
| 345 | 345 | // Clear queued messages from display since they're about to be processed |
| 346 | - // Note: messageQueue.getLatestMessage() already cleared the actual queue | |
| 346 | + // Note: messageQueue.getNextMessage() already cleared the actual queue | |
| 347 | 347 | setQueuedMessages([]); |
| 348 | 348 | |
| 349 | 349 | await new Promise((resolve) => setTimeout(resolve, 100)); // add timeout for react to render the tui |