Swadhin Biswas feat: implement mervelas cli engine, built-in tools, and reactive UI · 4/3/2026
0358986
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
import { feature } from 'bun:bundle'
import type { ContentBlockParam } from '@anthropic-ai/sdk/resources/messages.mjs'
import type { Permutations } from 'src/types/utils.js'
import { getSessionId } from '../bootstrap/state.js'
import type { AppState } from '../state/AppState.js'
import type {
QueueOperation,
QueueOperationMessage,
} from '../types/messageQueueTypes.js'
import type {
EditablePromptInputMode,
PromptInputMode,
QueuedCommand,
QueuePriority,
} from '../types/textInputTypes.js'
import type { PastedContent } from './config.js'
import { extractTextContent } from './messages.js'
import { objectGroupBy } from './objectGroupBy.js'
import { recordQueueOperation } from './sessionStorage.js'
import { createSignal } from './signal.js'
export type SetAppState = (f: (prev: AppState) => AppState) => void
// ============================================================================
// Logging helper
// ============================================================================
function logOperation(operation: QueueOperation, content?: string): void {
const sessionId = getSessionId()
const queueOp: QueueOperationMessage = {
type: 'queue-operation',
operation,
timestamp: new Date().toISOString(),
sessionId,
...(content !== undefined && { content }),
}
void recordQueueOperation(queueOp)
}
// ============================================================================
// Unified command queue (module-level, independent of React state)
//
// All commands — user input, task notifications, orphaned permissions — go
// through this single queue. React components subscribe via
// useSyncExternalStore (subscribeToCommandQueue / getCommandQueueSnapshot).
// Non-React code (print.ts streaming loop) reads directly via
// getCommandQueue() / getCommandQueueLength().
//
// Priority determines dequeue order: 'now' > 'next' > 'later'.
// Within the same priority, commands are processed FIFO.
// ============================================================================
const commandQueue: QueuedCommand[] = []
/** Frozen snapshot — recreated on every mutation for useSyncExternalStore. */
let snapshot: readonly QueuedCommand[] = Object.freeze([])
const queueChanged = createSignal()
function notifySubscribers(): void {
snapshot = Object.freeze([...commandQueue])
queueChanged.emit()
}
// ============================================================================
// useSyncExternalStore interface
// ============================================================================
/**
* Subscribe to command queue changes.
* Compatible with React's useSyncExternalStore.
*/
export const subscribeToCommandQueue = queueChanged.subscribe
/**
* Get current snapshot of the command queue.
* Compatible with React's useSyncExternalStore.
* Returns a frozen array that only changes reference on mutation.
*/
export function getCommandQueueSnapshot(): readonly QueuedCommand[] {
return snapshot
}
// ============================================================================
// Read operations (for non-React code)
// ============================================================================
/**
* Get a mutable copy of the current queue.
* Use for one-off reads where you need the actual commands.
*/
export function getCommandQueue(): QueuedCommand[] {
return [...commandQueue]
}
/**
* Get the current queue length without copying.
*/
export function getCommandQueueLength(): number {
return commandQueue.length
}
/**
* Check if there are commands in the queue.
*/
export function hasCommandsInQueue(): boolean {
return commandQueue.length > 0
}
/**
* Trigger a re-check by notifying subscribers.
* Use after async processing completes to ensure remaining commands
* are picked up by useSyncExternalStore consumers.
*/
export function recheckCommandQueue(): void {
if (commandQueue.length > 0) {
notifySubscribers()
}
}
// ============================================================================
// Write operations
// ============================================================================
/**
* Add a command to the queue.
* Used for user-initiated commands (prompt, bash, orphaned-permission).
* Defaults priority to 'next' (processed before task notifications).
*/
export function enqueue(command: QueuedCommand): void {
commandQueue.push({ ...command, priority: command.priority ?? 'next' })
notifySubscribers()
logOperation(
'enqueue',
typeof command.value === 'string' ? command.value : undefined,
)
}
/**
* Add a task notification to the queue.
* Convenience wrapper that defaults priority to 'later' so user input
* is never starved by system messages.
*/
export function enqueuePendingNotification(command: QueuedCommand): void {
commandQueue.push({ ...command, priority: command.priority ?? 'later' })
notifySubscribers()
logOperation(
'enqueue',
typeof command.value === 'string' ? command.value : undefined,
)
}
const PRIORITY_ORDER: Record<QueuePriority, number> = {
now: 0,
next: 1,
later: 2,
}
/**
* Remove and return the highest-priority command, or undefined if empty.
* Within the same priority level, commands are dequeued FIFO.
*
* An optional `filter` narrows the candidates: only commands for which the
* predicate returns `true` are considered. Non-matching commands stay in the
* queue untouched. This lets between-turn drains (SDK, REPL) restrict to
* main-thread commands (`cmd.agentId === undefined`) without restructuring
* the existing while-loop patterns.
*/
export function dequeue(
filter?: (cmd: QueuedCommand) => boolean,
): QueuedCommand | undefined {
if (commandQueue.length === 0) {
return undefined
}
// Find the first command with the highest priority (respecting filter)
let bestIdx = -1
let bestPriority = Infinity
for (let i = 0; i < commandQueue.length; i++) {
const cmd = commandQueue[i]!
if (filter && !filter(cmd)) continue
const priority = PRIORITY_ORDER[cmd.priority ?? 'next']
if (priority < bestPriority) {
bestIdx = i
bestPriority = priority
}
}
if (bestIdx === -1) return undefined
const [dequeued] = commandQueue.splice(bestIdx, 1)
notifySubscribers()
logOperation('dequeue')
return dequeued
}
/**
* Remove and return all commands from the queue.
* Logs a dequeue operation for each command.
*/
export function dequeueAll(): QueuedCommand[] {
if (commandQueue.length === 0) {
return []
}
const commands = [...commandQueue]
commandQueue.length = 0
notifySubscribers()
for (const _cmd of commands) {
logOperation('dequeue')
}
return commands
}
/**
* Return the highest-priority command without removing it, or undefined if empty.
* Accepts an optional `filter` — only commands passing the predicate are considered.
*/
export function peek(
filter?: (cmd: QueuedCommand) => boolean,
): QueuedCommand | undefined {
if (commandQueue.length === 0) {
return undefined
}
let bestIdx = -1
let bestPriority = Infinity
for (let i = 0; i < commandQueue.length; i++) {
const cmd = commandQueue[i]!
if (filter && !filter(cmd)) continue
const priority = PRIORITY_ORDER[cmd.priority ?? 'next']
if (priority < bestPriority) {
bestIdx = i
bestPriority = priority
}
}
if (bestIdx === -1) return undefined
return commandQueue[bestIdx]
}
/**
* Remove and return all commands matching a predicate, preserving priority order.
* Non-matching commands stay in the queue.
*/
export function dequeueAllMatching(
predicate: (cmd: QueuedCommand) => boolean,
): QueuedCommand[] {
const matched: QueuedCommand[] = []
const remaining: QueuedCommand[] = []
for (const cmd of commandQueue) {
if (predicate(cmd)) {
matched.push(cmd)
} else {
remaining.push(cmd)
}
}
if (matched.length === 0) {
return []
}
commandQueue.length = 0
commandQueue.push(...remaining)
notifySubscribers()
for (const _cmd of matched) {
logOperation('dequeue')
}
return matched
}
/**
* Remove specific commands from the queue by reference identity.
* Callers must pass the same object references that are in the queue
* (e.g. from getCommandsByMaxPriority). Logs a 'remove' operation for each.
*/
export function remove(commandsToRemove: QueuedCommand[]): void {
if (commandsToRemove.length === 0) {
return
}
const before = commandQueue.length
for (let i = commandQueue.length - 1; i >= 0; i--) {
if (commandsToRemove.includes(commandQueue[i]!)) {
commandQueue.splice(i, 1)
}
}
if (commandQueue.length !== before) {
notifySubscribers()
}
for (const _cmd of commandsToRemove) {
logOperation('remove')
}
}
/**
* Remove commands matching a predicate.
* Returns the removed commands.
*/
export function removeByFilter(
predicate: (cmd: QueuedCommand) => boolean,
): QueuedCommand[] {
const removed: QueuedCommand[] = []
for (let i = commandQueue.length - 1; i >= 0; i--) {
if (predicate(commandQueue[i]!)) {
removed.unshift(commandQueue.splice(i, 1)[0]!)
}
}
if (removed.length > 0) {
notifySubscribers()
for (const _cmd of removed) {
logOperation('remove')
}
}
return removed
}
/**
* Clear all commands from the queue.
* Used by ESC cancellation to discard queued notifications.
*/
export function clearCommandQueue(): void {
if (commandQueue.length === 0) {
return
}
commandQueue.length = 0
notifySubscribers()
}
/**
* Clear all commands and reset snapshot.
* Used for test cleanup.
*/
export function resetCommandQueue(): void {
commandQueue.length = 0
snapshot = Object.freeze([])
}
// ============================================================================
// Editable mode helpers
// ============================================================================
const NON_EDITABLE_MODES = new Set<PromptInputMode>([
'task-notification',
] satisfies Permutations<Exclude<PromptInputMode, EditablePromptInputMode>>)
export function isPromptInputModeEditable(
mode: PromptInputMode,
): mode is EditablePromptInputMode {
return !NON_EDITABLE_MODES.has(mode)
}
/**
* Whether this queued command can be pulled into the input buffer via UP/ESC.
* System-generated commands (proactive ticks, scheduled tasks, plan
* verification, channel messages) contain raw XML and must not leak into
* the user's input.
*/
export function isQueuedCommandEditable(cmd: QueuedCommand): boolean {
return isPromptInputModeEditable(cmd.mode) && !cmd.isMeta
}
/**
* Whether this queued command should render in the queue preview under the
* prompt. Superset of editable — channel messages show (so the keyboard user
* sees what arrived) but stay non-editable (raw XML).
*/
export function isQueuedCommandVisible(cmd: QueuedCommand): boolean {
if (
(feature('KAIROS') || feature('KAIROS_CHANNELS')) &&
cmd.origin?.kind === 'channel'
)
return true
return isQueuedCommandEditable(cmd)
}
/**
* Extract text from a queued command value.
* For strings, returns the string.
* For ContentBlockParam[], extracts text from text blocks.
*/
function extractTextFromValue(value: string | ContentBlockParam[]): string {
return typeof value === 'string' ? value : extractTextContent(value, '\n')
}
/**
* Extract images from ContentBlockParam[] and convert to PastedContent format.
* Returns empty array for string values or if no images found.
*/
function extractImagesFromValue(
value: string | ContentBlockParam[],
startId: number,
): PastedContent[] {
if (typeof value === 'string') {
return []
}
const images: PastedContent[] = []
let imageIndex = 0
for (const block of value) {
if (block.type === 'image' && block.source.type === 'base64') {
images.push({
id: startId + imageIndex,
type: 'image',
content: block.source.data,
mediaType: block.source.media_type,
filename: `image${imageIndex + 1}`,
})
imageIndex++
}
}
return images
}
export type PopAllEditableResult = {
text: string
cursorOffset: number
images: PastedContent[]
}
/**
* Pop all editable commands and combine them with current input for editing.
* Notification modes (task-notification) are left in the queue
* to be auto-processed later.
* Returns object with combined text, cursor offset, and images to restore.
* Returns undefined if no editable commands in queue.
*/
export function popAllEditable(
currentInput: string,
currentCursorOffset: number,
): PopAllEditableResult | undefined {
if (commandQueue.length === 0) {
return undefined
}
const { editable = [], nonEditable = [] } = objectGroupBy(
[...commandQueue],
cmd => (isQueuedCommandEditable(cmd) ? 'editable' : 'nonEditable'),
)
if (editable.length === 0) {
return undefined
}
// Extract text from queued commands (handles both strings and ContentBlockParam[])
const queuedTexts = editable.map(cmd => extractTextFromValue(cmd.value))
const newInput = [...queuedTexts, currentInput].filter(Boolean).join('\n')
// Calculate cursor offset: length of joined queued commands + 1 + current cursor offset
const cursorOffset = queuedTexts.join('\n').length + 1 + currentCursorOffset
// Extract images from queued commands
const images: PastedContent[] = []
let nextImageId = Date.now() // Use timestamp as base for unique IDs
for (const cmd of editable) {
// handlePromptSubmit queues images in pastedContents (value is a string).
// Preserve the original PastedContent id so imageStore lookups still work.
if (cmd.pastedContents) {
for (const content of Object.values(cmd.pastedContents)) {
if (content.type === 'image') {
images.push(content)
}
}
}
// Bridge/remote commands may embed images directly in ContentBlockParam[].
const cmdImages = extractImagesFromValue(cmd.value, nextImageId)
images.push(...cmdImages)
nextImageId += cmdImages.length
}
for (const command of editable) {
logOperation(
'popAll',
typeof command.value === 'string' ? command.value : undefined,
)
}
// Replace queue contents with only the non-editable commands
commandQueue.length = 0
commandQueue.push(...nonEditable)
notifySubscribers()
return { text: newInput, cursorOffset, images }
}
// ============================================================================
// Backward-compatible aliases (deprecated — prefer new names)
// ============================================================================
/** @deprecated Use subscribeToCommandQueue */
export const subscribeToPendingNotifications = subscribeToCommandQueue
/** @deprecated Use getCommandQueueSnapshot */
export function getPendingNotificationsSnapshot(): readonly QueuedCommand[] {
return snapshot
}
/** @deprecated Use hasCommandsInQueue */
export const hasPendingNotifications = hasCommandsInQueue
/** @deprecated Use getCommandQueueLength */
export const getPendingNotificationsCount = getCommandQueueLength
/** @deprecated Use recheckCommandQueue */
export const recheckPendingNotifications = recheckCommandQueue
/** @deprecated Use dequeue */
export function dequeuePendingNotification(): QueuedCommand | undefined {
return dequeue()
}
/** @deprecated Use resetCommandQueue */
export const resetPendingNotifications = resetCommandQueue
/** @deprecated Use clearCommandQueue */
export const clearPendingNotifications = clearCommandQueue
/**
* Get commands at or above a given priority level without removing them.
* Useful for mid-chain draining where only urgent items should be processed.
*
* Priority order: 'now' (0) > 'next' (1) > 'later' (2).
* Passing 'now' returns only now-priority commands; 'later' returns everything.
*/
export function getCommandsByMaxPriority(
maxPriority: QueuePriority,
): QueuedCommand[] {
const threshold = PRIORITY_ORDER[maxPriority]
return commandQueue.filter(
cmd => PRIORITY_ORDER[cmd.priority ?? 'next'] <= threshold,
)
}
/**
* Returns true if the command is a slash command that should be routed through
* processSlashCommand rather than sent to the model as text.
*
* Commands with `skipSlashCommands` (e.g. bridge/CCR messages) are NOT treated
* as slash commands — their text is meant for the model.
*/
export function isSlashCommand(cmd: QueuedCommand): boolean {
return (
typeof cmd.value === 'string' &&
cmd.value.trim().startsWith('/') &&
!cmd.skipSlashCommands
)
}