(input: QueueInput)
| 57 | // Ordinary prompts submitted during an ordinary active turn remain local and |
| 58 | // are exposed by the footer for edit/removal until their turn begins. |
| 59 | export async function runPromptQueue(input: QueueInput): Promise<void> { |
| 60 | const stop = defer<{ type: "closed" }>() |
| 61 | const done = defer() |
| 62 | const state: State = { |
| 63 | queue: [], |
| 64 | queued: [], |
| 65 | closed: input.footer.isClosed, |
| 66 | } |
| 67 | let draining: Promise<void> | undefined |
| 68 | |
| 69 | const emit = (next: FooterEvent, row: Record<string, unknown>) => { |
| 70 | input.trace?.write("ui.patch", row) |
| 71 | input.footer.event(next) |
| 72 | } |
| 73 | |
| 74 | const syncQueue = () => { |
| 75 | const queue = state.queue.length |
| 76 | emit({ type: "queue", queue }, { queue }) |
| 77 | emit( |
| 78 | { |
| 79 | type: "queued.prompts", |
| 80 | prompts: [...state.queued], |
| 81 | }, |
| 82 | { queued: state.queued.length }, |
| 83 | ) |
| 84 | } |
| 85 | |
| 86 | const removeLocalQueued = (queued: FooterQueuedPrompt) => { |
| 87 | if (!state.queued.includes(queued)) return |
| 88 | state.queued = state.queued.filter((item) => item !== queued) |
| 89 | syncQueue() |
| 90 | } |
| 91 | |
| 92 | const finish = () => { |
| 93 | if (!state.closed || draining) { |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | done.resolve() |
| 98 | } |
| 99 | |
| 100 | const close = () => { |
| 101 | if (state.closed) { |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | state.closed = true |
| 106 | state.queue.length = 0 |
| 107 | state.queued.length = 0 |
| 108 | state.ctrl?.abort() |
| 109 | stop.resolve({ type: "closed" }) |
| 110 | finish() |
| 111 | } |
| 112 | |
| 113 | const drain = () => { |
| 114 | if (draining || state.closed || state.queue.length === 0) { |
| 115 | return |
| 116 | } |
no test coverage detected