/q <prompt> holds prompts until the running turn ends and sends them one per turn; a band above the prompt lists, edits, reorders and pauses them

A Claude Code mod that queues prompts until Claude's running turn ends.
When you send a message while Claude is working, Claude Code delivers it into the running turn, between tool calls. Sometimes you want the opposite: a follow-up that waits until Claude has finished, then goes out as its own turn.
/q now write tests for what you just changed
/q <prompt>, or Tab once bound (below), queues a prompt. While a turn is running it confirms in a toast (Queued: 2 waiting.); while idle it sends straight away. /q leaves nothing in the transcript.Mods cannot see keys typed in the prompt box, so the two shortcuts come from Claude Code's own actions. Add them to ~/.claude/keybindings.json:
{
"bindings": [
{
"context": "Chat",
"bindings": {
"tab": "chat:queueSubmit",
"alt+up": "abovePrompt:focus"
}
},
{
"context": "Autocomplete",
"bindings": {
"tab": "autocomplete:accept"
}
}
]
}
Autocomplete entry keeps Tab accepting file, command and skill suggestions while their menu is open: without it, a Chat binding for Tab wins over the built-in autocomplete one and submits the prompt. The dim suggestion in an empty prompt box is taken with → instead: Claude Code drops an empty submit before any mod sees it, so Tab cannot do both./diff's file-list key, which this binding takes over while the prompt box has focus.In the band:
| Key | Does |
|---|---|
| ↑ / ↓ | move to the prompt before / after |
| Enter | steer the focused prompt into the running turn (send it, while idle) |
e | edit the focused prompt (Enter saves; saving it empty removes it) |
k / j | move the focused prompt up / down the queue |
x | remove the focused prompt |
p | pause / resume sending |
| Esc | back to the prompt box |
From inside Claude Code:
/plugin marketplace add gergesh/claude-queue-mod
/plugin install wait-queue@claude-queue-mod
Or run it from a checkout without installing:
git clone https://github.com/gergesh/claude-queue-mod
claude --plugin-dir ./claude-queue-mod
Mods (function-hook plugins) are an early-access Claude Code feature; the API may change between releases.
claude plugin validate . # what the engine would load or refuse
claude plugin test . # hooks/register.test.ts against the engine
tsc -p . # after Claude Code has loaded the mod once
Claude Code writes the API's type declarations into .claude-plugin/types/ each time it loads the mod, and tsconfig.json extends the config it writes there, so type-checking works once the mod has been loaded (with --plugin-dir, or as an installed plugin). That folder is not committed.
hooks/register.tsx: the modhooks/register.test.ts: its tests, on the terminal and desktop surfacestypes/index.d.ts: the session state the mod keeps ($.state)hooks/register.tsx 365 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register } from 'claude-code'
3
4import type { QueueItem } from '../types'
5
6// The engine delivers a prompt typed mid-turn into that turn even when the
7// person asked it to wait (`e.wait`, chat:queueSubmit). This mod holds such
8// prompts, and those given to `/q <prompt>`, sends them one per turn once the
9// session is idle, and lists them in a band above the prompt to steer into the
10// running turn, edit, reorder, remove or pause.
11
12const items = atom({ plugin: 'wait-queue', key: 'items' } as const, [])
13const editingId = atom({ plugin: 'wait-queue', key: 'editingId' } as const, null)
14const isPaused = atom({ plugin: 'wait-queue', key: 'isPaused' } as const, false)
15const steered = atom({ plugin: 'wait-queue', key: 'steered' } as const, [])
16const runningTurnId = atom({ plugin: 'wait-queue', key: 'runningTurnId' } as const, null)
17const isReleasing = atom({ plugin: 'wait-queue', key: 'isReleasing' } as const, false)
18
19const ROW = 'row-'
20const EDIT = 'edit-'
21const STEER_FRAME = '[The user sent this while you were working. Apply it to the current task now.]'
22const STEER_NUDGE = 'Continue the task, applying the instruction I just sent.'
23
24function isQueueCommand(text: string): boolean {
25 return /^\/q(\s|$)/.test(text.trim())
26}
27
28function truncate(text: string, width: number): string {
29 const line = text.replace(/\s+/g, ' ')
30 return line.length <= width ? line : `${line.slice(0, width - 1)}…`
31}
32
33function moveItem(list: QueueItem[], id: string, offset: -1 | 1): QueueItem[] {
34 const from = list.findIndex(item => item.id === id)
35 const item = list[from]
36 const to = from + offset
37 if (item === undefined || to < 0 || to >= list.length) return list
38 const rest = list.filter(other => other.id !== id)
39 return [...rest.slice(0, to), item, ...rest.slice(to)]
40}
41
42function showCount($: EngineInterface, count: number) {
43 $.ui.status(count === 0 ? undefined : `${count} queued`)
44}
45
46async function enqueue($: EngineInterface, text: string): Promise<number> {
47 const item: QueueItem = { id: crypto.randomUUID(), text }
48 const next = await update($, items, list => [...list, item])
49 showCount($, next.length)
50 return next.length
51}
52
53async function take($: EngineInterface, id: string): Promise<QueueItem | undefined> {
54 const item = (await read($, items)).find(one => one.id === id)
55 if (item === undefined) return undefined
56 const rest = await update($, items, list => list.filter(one => one.id !== id))
57 showCount($, rest.length)
58 return item
59}
60
61// Puts a prompt that did not get through back at the front of the queue.
62async function restore($: EngineInterface, item: QueueItem, reason: string) {
63 await update($, isReleasing, () => false)
64 const list = await update($, items, current => [item, ...current.filter(one => one.id !== item.id)])
65 showCount($, list.length)
66 $.ui.toast(`${reason}; it is back at the front of the queue.`)
67}
68
69// Sends a prompt as the person's own; one the engine refuses goes back in the queue.
70function submit($: EngineInterface, item: QueueItem) {
71 void $.prompt
72 .submit({ text: item.text, asUser: true })
73 .then(result => (result.drop === undefined ? undefined : restore($, item, `Not sent (${result.drop})`)))
74 .catch(() => restore($, item, 'Could not send'))
75}
76
77// Sends the head of the queue, unless the queue is paused, a prompt in it is
78// being edited, or one already went and its turn has not started.
79async function sendNext($: EngineInterface) {
80 if ((await read($, isPaused)) || (await read($, editingId)) !== null || (await read($, isReleasing))) return
81 const [first] = await read($, items)
82 if (first === undefined) return
83
84 await update($, isReleasing, () => true)
85 const item = await take($, first.id)
86 if (item !== undefined) submit($, item)
87}
88
89// Sends one queued prompt into the running turn, read at the model's next step;
90// while idle it goes as a turn of its own.
91async function steer($: EngineInterface, id: string) {
92 const isWorking = (await read($, runningTurnId)) !== null
93 const item = await take($, id)
94 if (item === undefined) return
95 if (!isWorking) {
96 submit($, item)
97 return
98 }
99
100 const appended = await $.session
101 .append({ message: { type: 'user', content: [{ type: 'text', text: `${STEER_FRAME}\n\n${item.text}` }] } })
102 .catch((error: unknown) => ({ deny: error instanceof Error ? error.message : String(error) }))
103 if (appended.deny !== undefined) {
104 await restore($, item, `Not steered (${appended.deny})`)
105 return
106 }
107
108 await update($, steered, list => [...list, { id: item.id, text: item.text, status: 'pending' as const }])
109 $.ui.toast('Steered into the current turn.')
110}
111
112export const register: Register = on => {
113 on('session.start', async ($, e, next) => {
114 // A reload or a new process starts with nothing in flight.
115 await update($, isReleasing, () => false)
116 await $.command.register({
117 name: 'q',
118 description: 'Queue a prompt to send after the current turn ends',
119 argumentHint: '<prompt>',
120 immediate: true,
121 })
122 return next(e)
123 })
124
125 on('command.run', { command: 'q' }, async ($, e) => {
126 // Answers in a toast, not text: /q leaves nothing in the transcript (see the
127 // CommandOutput and UserMessage hooks below).
128 const text = e.args.trim()
129 if (text === '') {
130 $.ui.toast('Usage: /q <prompt>. The queue panel above the prompt manages what is queued.')
131 return {}
132 }
133
134 const count = await enqueue($, text)
135 if ((await read($, runningTurnId)) === null) {
136 // A submit can't start inside this hook (it would wait on the command's own
137 // dispatch), so it goes out on the clock's next tick, once the command is done.
138 $.clock.after(0, () => void sendNext($))
139 $.ui.toast('Sending now.')
140 return {}
141 }
142 $.ui.toast(`Queued: ${count} waiting.`)
143 return {}
144 })
145
146 on('prompt.submit', async ($, e, next) => {
147 const isWaitingRequest =
148 e.wait && e.turnId !== undefined && e.origin.kind === 'composer' && e.attachments === undefined
149 if (!isWaitingRequest) return next(e)
150
151 const count = await enqueue($, e.text)
152 // The engine puts a dropped prompt back in the box once this hook answers;
153 // the queue holds it now, so the box is cleared on the clock's next tick,
154 // unless the person has already started typing something else.
155 $.clock.after(0, async () => {
156 if ((await $.prompt.read()).text.trim() === e.text.trim()) await $.prompt.fill({ text: '' })
157 })
158 return { drop: `Queued (${count} waiting).` }
159 })
160
161 on('turn.start', async ($, e, next) => {
162 const result = await next(e)
163 await update($, runningTurnId, () => result.turnId)
164 await update($, isReleasing, () => false)
165 return result
166 })
167
168 on('turn.step', async function* ($, e, next) {
169 // The step after a steer carries it to the model.
170 if (e.turnId === (await read($, runningTurnId)) && (await read($, steered)).some(one => one.status === 'pending')) {
171 await update($, steered, list => list.map(one => ({ ...one, status: 'read' as const })))
172 }
173 return yield* next(e)
174 })
175
176 on('turn.complete', async ($, e, next) => {
177 const result = await next(e)
178 if (e.agentId !== undefined) return result
179 if (e.turnId === (await read($, runningTurnId))) await update($, runningTurnId, () => null)
180
181 const hasUnreadSteer = (await read($, steered)).some(one => one.status === 'pending')
182 await update($, steered, () => [])
183
184 if (e.isAborted) {
185 // An interrupted turn pauses the queue; the person resumes or edits it in the band.
186 const waiting = (await read($, items)).length
187 if (waiting > 0) {
188 await update($, isPaused, () => true)
189 $.ui.toast(`Queue paused: turn interrupted (${waiting} waiting). Resume it from the panel.`)
190 }
191 return result
192 }
193
194 if (hasUnreadSteer) {
195 // The turn ended before the model read the steer: ask it to pick it up
196 // first; the queue goes on after that turn.
197 void $.prompt.submit({ text: STEER_NUDGE, asUser: true }).catch(() => $.ui.toast('Could not ask Claude to continue.'))
198 return result
199 }
200
201 await sendNext($)
202 return result
203 })
204
205 // /q runs are kept out of the transcript's drawing (the model still reads them).
206 on('ui.render', { component: 'CommandOutput' }, ($, e, next) =>
207 e.props.command === 'q' ? $.ui.resolve(e).Box({}) : next(e),
208 )
209
210 on('ui.render', { component: 'UserMessage' }, ($, e, next) =>
211 isQueueCommand(e.props.text) ? $.ui.resolve(e).Box({}) : next(e),
212 )
213
214 // The row the band's focus ring is on, so e / k / j / x act on it.
215 let focusedId: string | undefined
216
217 on('ui.focus', { component: 'AbovePrompt' }, ($, e, next) => {
218 if (e.plugin === $.plugin.name && e.element?.startsWith(ROW) === true) focusedId = e.element.slice(ROW.length)
219 return next(e)
220 })
221
222 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
223 const list = await read($, items)
224 const steering = await read($, steered)
225 if (e.props.hasSurvey || (list.length === 0 && steering.length === 0)) return next(e)
226
227 const paused = await read($, isPaused)
228 const editing = await read($, editingId)
229 const elements = $.ui.resolve(e)
230 const { Box, Button, Text } = elements
231 // A surface without text fields lists, steers, reorders and removes, but does not edit.
232 const Input = 'Input' in elements ? elements.Input : undefined
233
234 // Sending resumes on its own at the next turn's end; while idle it is nudged here.
235 const resumeIfIdle = async () => {
236 if ((await read($, runningTurnId)) === null) await sendNext($)
237 }
238 // Read at press time: moving the focus ring does not redraw the band.
239 const focusedRow = async () => {
240 const current = await read($, items)
241 return (current.find(item => item.id === focusedId) ?? current[current.length - 1])?.id
242 }
243 const moveFocused = async (offset: -1 | 1) => {
244 const id = await focusedRow()
245 if (id !== undefined) await update($, items, current => moveItem(current, id, offset))
246 }
247 const state = paused ? 'paused' : editing !== null ? 'paused while editing' : 'sends after this turn'
248 const width = Math.max(20, e.props.bodyColumns - 8)
249
250 return (
251 <Box flexDirection="column">
252 {steering.length > 0 && (
253 <Box key="steering" flexDirection="column">
254 {steering.map(one => (
255 <Text key={`steered-${one.id}`} color="cyan" wrap="truncate-end">
256 {one.status === 'pending' ? '↪ steering' : '✓ read'} · {truncate(one.text, width - 12)}
257 </Text>
258 ))}
259 </Box>
260 )}
261 {list.length > 0 && (
262 <Box flexDirection="row" gap={1}>
263 <Text bold>Queue</Text>
264 <Text dimColor>
265 {list.length} waiting · {state}
266 </Text>
267 <Button
268 key="pause"
269 label={paused ? 'Resume' : 'Pause'}
270 hotkey="p"
271 plain
272 dimColor
273 onPress={async () => {
274 await update($, isPaused, value => !value)
275 if (paused) await resumeIfIdle()
276 }}
277 />
278 <Button
279 key="clear"
280 label="Clear"
281 dimColor
282 onPress={async () => {
283 await update($, editingId, () => null)
284 await update($, items, () => [])
285 showCount($, 0)
286 }}
287 />
288 </Box>
289 )}
290 {list.map((item, index) =>
291 item.id === editing && Input !== undefined ? (
292 <Box key={`editing-${item.id}`} flexDirection="row" gap={1}>
293 <Input
294 key={`${EDIT}${item.id}`}
295 label={`${index + 1}.`}
296 value={item.text}
297 submitLabel="save"
298 autoFocus
299 onSubmit={async (value: string) => {
300 const text = value.trim()
301 await update($, items, current =>
302 text === ''
303 ? current.filter(one => one.id !== item.id)
304 : current.map(one => (one.id === item.id ? { ...one, text } : one)),
305 )
306 await update($, editingId, () => null)
307 await resumeIfIdle()
308 }}
309 />
310 <Button
311 key={`cancel-${item.id}`}
312 label="Cancel"
313 dimColor
314 onPress={async () => {
315 await update($, editingId, () => null)
316 await resumeIfIdle()
317 }}
318 />
319 </Box>
320 ) : (
321 <Button
322 key={`${ROW}${item.id}`}
323 label={truncate(`${index + 1}. ${item.text}`, width)}
324 plain
325 autoFocus={editing === null && index === list.length - 1 ? true : undefined}
326 onPress={() => steer($, item.id)}
327 />
328 ),
329 )}
330 {list.length > 0 && editing === null && (
331 <Box flexDirection="row" gap={2}>
332 <Text dimColor>enter: {e.props.isWorking ? 'steer now' : 'send now'}</Text>
333 {Input !== undefined && (
334 <Button
335 key="edit"
336 label="edit"
337 hotkey="e"
338 plain
339 dimColor
340 onPress={async () => {
341 const id = await focusedRow()
342 if (id !== undefined) await update($, editingId, () => id)
343 }}
344 />
345 )}
346 <Button key="move-up" label="move up" hotkey="k" plain dimColor onPress={() => moveFocused(-1)} />
347 <Button key="move-down" label="move down" hotkey="j" plain dimColor onPress={() => moveFocused(1)} />
348 <Button
349 key="remove"
350 label="remove"
351 hotkey="x"
352 plain
353 dimColor
354 onPress={async () => {
355 const id = await focusedRow()
356 if (id !== undefined) await take($, id)
357 }}
358 />
359 </Box>
360 )}
361 </Box>
362 )
363 })
364}
365types/index.d.ts 20 lines1export type QueueItem = { id: string; text: string }
2
3/** A queued prompt steered into the running turn: `pending` until the model's next step reads it. */
4export type SteeredItem = { id: string; text: string; status: 'pending' | 'read' }
5
6declare module 'claude-code' {
7 interface PluginState {
8 'wait-queue': {
9 items: QueueItem[]
10 editingId: string | null
11 isPaused: boolean
12 steered: SteeredItem[]
13 /** The main loop's running turn, or null while idle. */
14 runningTurnId: string | null
15 /** True from releasing a queued prompt until its turn starts, so only one goes at a time. */
16 isReleasing: boolean
17 }
18 }
19}
20