shellpuppet 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +31 -0
- package/README.md +499 -8
- package/dist/cli/desktopShortcut.d.ts +22 -0
- package/dist/cli/desktopShortcut.js +157 -0
- package/dist/cli/desktopShortcut.js.map +1 -0
- package/dist/cli/index.d.ts +13 -0
- package/dist/cli/index.js +846 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/client/http.d.ts +143 -0
- package/dist/client/http.js +581 -0
- package/dist/client/http.js.map +1 -0
- package/dist/core/dialogs.d.ts +96 -0
- package/dist/core/dialogs.js +226 -0
- package/dist/core/dialogs.js.map +1 -0
- package/dist/core/echo.d.ts +71 -0
- package/dist/core/echo.js +249 -0
- package/dist/core/echo.js.map +1 -0
- package/dist/core/find.d.ts +34 -0
- package/dist/core/find.js +41 -0
- package/dist/core/find.js.map +1 -0
- package/dist/core/keys.d.ts +22 -0
- package/dist/core/keys.js +80 -0
- package/dist/core/keys.js.map +1 -0
- package/dist/core/manager.d.ts +96 -0
- package/dist/core/manager.js +271 -0
- package/dist/core/manager.js.map +1 -0
- package/dist/core/nativePty.d.ts +73 -0
- package/dist/core/nativePty.js +161 -0
- package/dist/core/nativePty.js.map +1 -0
- package/dist/core/policy.d.ts +61 -0
- package/dist/core/policy.js +143 -0
- package/dist/core/policy.js.map +1 -0
- package/dist/core/promptDetector.d.ts +109 -0
- package/dist/core/promptDetector.js +897 -0
- package/dist/core/promptDetector.js.map +1 -0
- package/dist/core/reasoning.d.ts +265 -0
- package/dist/core/reasoning.js +933 -0
- package/dist/core/reasoning.js.map +1 -0
- package/dist/core/regexBudget.d.ts +64 -0
- package/dist/core/regexBudget.js +103 -0
- package/dist/core/regexBudget.js.map +1 -0
- package/dist/core/renderer.d.ts +127 -0
- package/dist/core/renderer.js +316 -0
- package/dist/core/renderer.js.map +1 -0
- package/dist/core/selection.d.ts +6 -0
- package/dist/core/selection.js +48 -0
- package/dist/core/selection.js.map +1 -0
- package/dist/core/session.d.ts +565 -0
- package/dist/core/session.js +1450 -0
- package/dist/core/session.js.map +1 -0
- package/dist/core/types.d.ts +316 -0
- package/dist/core/types.js +71 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/waiter.d.ts +94 -0
- package/dist/core/waiter.js +310 -0
- package/dist/core/waiter.js.map +1 -0
- package/dist/core/windowsConsoleHost.d.ts +147 -0
- package/dist/core/windowsConsoleHost.js +588 -0
- package/dist/core/windowsConsoleHost.js.map +1 -0
- package/dist/core/windowsPtyStart.d.ts +161 -0
- package/dist/core/windowsPtyStart.js +236 -0
- package/dist/core/windowsPtyStart.js.map +1 -0
- package/dist/daemon/audit.d.ts +23 -0
- package/dist/daemon/audit.js +51 -0
- package/dist/daemon/audit.js.map +1 -0
- package/dist/daemon/auth.d.ts +197 -0
- package/dist/daemon/auth.js +366 -0
- package/dist/daemon/auth.js.map +1 -0
- package/dist/daemon/config.d.ts +119 -0
- package/dist/daemon/config.js +568 -0
- package/dist/daemon/config.js.map +1 -0
- package/dist/daemon/folderPicker.d.ts +27 -0
- package/dist/daemon/folderPicker.js +115 -0
- package/dist/daemon/folderPicker.js.map +1 -0
- package/dist/daemon/index.d.ts +7 -0
- package/dist/daemon/index.js +88 -0
- package/dist/daemon/index.js.map +1 -0
- package/dist/daemon/launchProfiles.d.ts +155 -0
- package/dist/daemon/launchProfiles.js +554 -0
- package/dist/daemon/launchProfiles.js.map +1 -0
- package/dist/daemon/liveness.d.ts +36 -0
- package/dist/daemon/liveness.js +71 -0
- package/dist/daemon/liveness.js.map +1 -0
- package/dist/daemon/logger.d.ts +7 -0
- package/dist/daemon/logger.js +29 -0
- package/dist/daemon/logger.js.map +1 -0
- package/dist/daemon/meta.d.ts +9 -0
- package/dist/daemon/meta.js +24 -0
- package/dist/daemon/meta.js.map +1 -0
- package/dist/daemon/paths.d.ts +85 -0
- package/dist/daemon/paths.js +154 -0
- package/dist/daemon/paths.js.map +1 -0
- package/dist/daemon/screenshot.d.ts +144 -0
- package/dist/daemon/screenshot.js +402 -0
- package/dist/daemon/screenshot.js.map +1 -0
- package/dist/daemon/server.d.ts +453 -0
- package/dist/daemon/server.js +3954 -0
- package/dist/daemon/server.js.map +1 -0
- package/dist/daemon/sessionFeed.d.ts +48 -0
- package/dist/daemon/sessionFeed.js +101 -0
- package/dist/daemon/sessionFeed.js.map +1 -0
- package/dist/daemon/terminalReports.d.ts +38 -0
- package/dist/daemon/terminalReports.js +84 -0
- package/dist/daemon/terminalReports.js.map +1 -0
- package/dist/daemon/viewerLauncher.d.ts +226 -0
- package/dist/daemon/viewerLauncher.js +562 -0
- package/dist/daemon/viewerLauncher.js.map +1 -0
- package/dist/daemon/viewerSync.d.ts +38 -0
- package/dist/daemon/viewerSync.js +83 -0
- package/dist/daemon/viewerSync.js.map +1 -0
- package/dist/mcp/config.d.ts +50 -0
- package/dist/mcp/config.js +95 -0
- package/dist/mcp/config.js.map +1 -0
- package/dist/mcp/index.d.ts +10 -0
- package/dist/mcp/index.js +78 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/tools.d.ts +66 -0
- package/dist/mcp/tools.js +638 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/viewer/dashboard-folders.js +20 -0
- package/dist/viewer/dashboard-layout.js +52 -0
- package/dist/viewer/dashboard.css +67 -0
- package/dist/viewer/dashboard.html +44 -0
- package/dist/viewer/dashboard.js +1518 -0
- package/dist/viewer/icon-16.png +0 -0
- package/dist/viewer/icon-180.png +0 -0
- package/dist/viewer/icon-32.png +0 -0
- package/dist/viewer/icon-512.png +0 -0
- package/dist/viewer/icon.ico +0 -0
- package/dist/viewer/icon.svg +66 -0
- package/dist/viewer/index.html +61 -0
- package/dist/viewer/pane.js +736 -0
- package/dist/viewer/viewer.css +95 -0
- package/dist/viewer/viewer.js +84 -0
- package/package.json +66 -7
- package/index.js +0 -4
|
@@ -0,0 +1,897 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Permission-prompt detection over rendered screen text. An agent CLI
|
|
3
|
+
* stuck on a permission prompt should raise an alert instead of waiting for
|
|
4
|
+
* the next manual snapshot, because a prompt only serviced when a human or
|
|
5
|
+
* manager happens to look costs tens of idle minutes per prompt.
|
|
6
|
+
*
|
|
7
|
+
* This is a HEURISTIC over untrusted screen content. It decides only whether
|
|
8
|
+
* the screen LOOKS LIKE it is waiting on a human choice; it never decides the
|
|
9
|
+
* answer. False negatives cost what the status quo already costs (a wait for
|
|
10
|
+
* the next snapshot); false positives cost one spurious notification. Both
|
|
11
|
+
* are acceptable, so the patterns favour the shapes real agent CLIs draw.
|
|
12
|
+
*
|
|
13
|
+
* Calibrated against real screens from three CLIs (the fixtures are
|
|
14
|
+
* verbatim): Qoder (`❯` marker, tight option blocks), Codex (`›`/`>` markers,
|
|
15
|
+
* LONG WRAPPED option text between numbered rows and `Press enter to
|
|
16
|
+
* confirm or esc to cancel` footers) and Grok (radio rows `1 (○)` / `2 (•)`
|
|
17
|
+
* inside a `│` box border, NO dot after the number; its folder-trust question
|
|
18
|
+
* puts each option's key after it, `Yes, proceed y`). It also reads
|
|
19
|
+
* pointer-only choosers with unnumbered rows (inquirer.js, and Claude Code's
|
|
20
|
+
* own folder-trust prompt), letter-keyed menus, `(Y)es/(N)o` and bracketed
|
|
21
|
+
* letter lists, and the steps `@clack/prompts` draws for the scaffolding
|
|
22
|
+
* wizards of create-vite, `sv create` and others: a select, a multi-select or
|
|
23
|
+
* a confirm under a `◆` headline, or `▲` while the step shows a validation
|
|
24
|
+
* error. MiMo Code's folder-trust question is a clack select too. Two rules
|
|
25
|
+
* keep out FALSE positives: a numbered findings list an agent printed above
|
|
26
|
+
* its composer, under a line that happens to end in a question mark; and a
|
|
27
|
+
* message Claude Code echoes into its transcript or holds in its composer,
|
|
28
|
+
* whose `❯` and aligned lines look like a pointer chooser under a question
|
|
29
|
+
* from an earlier message.
|
|
30
|
+
*
|
|
31
|
+
* A permission dialog drawn with its choices as one row of buttons
|
|
32
|
+
* (OpenCode, MiMo Code) is recognised by its signature (`dialogs.ts`),
|
|
33
|
+
* which config can extend. Their numbered question menu is recognised by
|
|
34
|
+
* its fixed controls and custom-answer choice here.
|
|
35
|
+
*/
|
|
36
|
+
import { dialogPrompt } from './dialogs.js';
|
|
37
|
+
/** A plain Muse heading can precede the moment its keys become active. */
|
|
38
|
+
export const MUSE_MENU_SETTLE_MS = 1_500;
|
|
39
|
+
/**
|
|
40
|
+
* A numbered option row, in either of the two shapes real CLIs draw. Dot
|
|
41
|
+
* form: `❯ 1. Allow once` / `› 1. Yes, proceed (y)` / `>1. Sign in
|
|
42
|
+
* with ChatGPT` (Codex draws the sign-in marker TIGHT against the digit) /
|
|
43
|
+
* `2. Don't trust and exit`. Radio form: `│ 1 (○) Yes, and don't ask
|
|
44
|
+
* again` / `│ 2 (•) Yes, proceed` (Grok: box border, no dot, the radio IS
|
|
45
|
+
* the selection state). The capture group is the option number, used to keep
|
|
46
|
+
* wrap-interleaved rows in one block.
|
|
47
|
+
*/
|
|
48
|
+
const OPTION_ROW = /^\s*(?:│\s*)?(?:[❯›]\s*|>\s*)?(\d+)(?:\.\s+|\s+\([○•●]\)\s+)\S/;
|
|
49
|
+
/**
|
|
50
|
+
* A letter-keyed row: `[Y] Yes`, `(a) Apply the change`,
|
|
51
|
+
* optionally pointed. The capture is the key, used to keep a block to rows
|
|
52
|
+
* with DISTINCT keys, because a markdown checkbox list repeats `[x]`.
|
|
53
|
+
*/
|
|
54
|
+
const LETTER_ROW = /^\s*(?:│\s*)?(?:[❯›]\s*|>\s*)?[[(]([a-z])[\])]\s+\S/i;
|
|
55
|
+
/**
|
|
56
|
+
* An option with its key after it, as Grok's folder-trust question draws
|
|
57
|
+
* them: `Yes, proceed y`. The label, two or more spaces, then a single
|
|
58
|
+
* letter ending the line. Captures indent, label and key.
|
|
59
|
+
*/
|
|
60
|
+
const KEY_HINT_ROW = /^(\s*)(\S.*?)\s{2,}([a-z])$/i;
|
|
61
|
+
/**
|
|
62
|
+
* A pointer against plain text: `❯ npm`, `> minimal`. It is the
|
|
63
|
+
* inquirer.js shape, and Claude Code's own folder-trust prompt. Captures
|
|
64
|
+
* indent, pointer and text. `›` is deliberately absent: Codex draws it on
|
|
65
|
+
* its composer line, and every `›` chooser seen so far numbers its rows.
|
|
66
|
+
*/
|
|
67
|
+
const POINTER_ROW = /^(\s*)([❯>])\s+(\S.*)$/;
|
|
68
|
+
/**
|
|
69
|
+
* Focus markers TUIs draw on the selected option: `❯`/`›` anywhere,
|
|
70
|
+
* line-leading `>` only before a numbered or letter-keyed row (tight or
|
|
71
|
+
* spaced) or a FILLED radio `(•)`/`(●)`, Grok's selected-row marker.
|
|
72
|
+
*/
|
|
73
|
+
const MARKER_LINE = /[❯›]|^\s*(?:│\s*)?>\s*(?:\d+\.|[[(][a-z][\])])|\([•●]\)/i;
|
|
74
|
+
/**
|
|
75
|
+
* A step headline as `@clack/prompts` draws it: the step glyph, two spaces,
|
|
76
|
+
* the message. Only a step still waiting matches: `◆`, or `▲` while it shows
|
|
77
|
+
* a validation error. Clack redraws a step `◇` once it is answered and `■`
|
|
78
|
+
* once it is cancelled, and its log lines reuse the step glyphs
|
|
79
|
+
* (`◆ Project created`), so a headline counts only with choice rows under it.
|
|
80
|
+
* A step with no message of its own is the glyph alone: MiMo Code asks its
|
|
81
|
+
* folder-trust question in a message it logs above such a step.
|
|
82
|
+
*/
|
|
83
|
+
const CLACK_HEADLINE = /^[◆▲](?: {2}(\S.*))?$/;
|
|
84
|
+
/** The first line of a message clack logs: `● Accessing workspace:`. */
|
|
85
|
+
const CLACK_LOG = /^[●◆◇▲■] {2}(\S.*)$/;
|
|
86
|
+
/**
|
|
87
|
+
* A choice row in the `│` gutter clack draws down the left of a step. A
|
|
88
|
+
* select draws radios (`●` for the selected option, `○` for the rest), a
|
|
89
|
+
* confirm draws both radios on one row (`● Yes / ○ No`) and a multi-select
|
|
90
|
+
* draws checkboxes (`◼` checked, `◻` not). The focused checkbox differs only
|
|
91
|
+
* in colour, which screen text does not carry.
|
|
92
|
+
*/
|
|
93
|
+
const CLACK_ROW = /^│ {2}[●○◻◼] \S/;
|
|
94
|
+
/** A line in clack's gutter: a choice row, wrapped text, a footer or the bare bar. */
|
|
95
|
+
const CLACK_GUTTER = /^│(?: |$)/;
|
|
96
|
+
/** The line that closes a clack step, which may carry a validation message. */
|
|
97
|
+
const CLACK_BAR_END = /^└(?: |$)/;
|
|
98
|
+
/**
|
|
99
|
+
* The rest of a validation message, on the lines under the `└` that carries
|
|
100
|
+
* it: clack indents them three spaces, to line up with the message. An input
|
|
101
|
+
* box is not one, whatever its indent: neither its pointer (`❯ Ask
|
|
102
|
+
* anything`) nor its border.
|
|
103
|
+
*/
|
|
104
|
+
const VALIDATION_REST = /^ {3}[^\s❯›>─-╿]/;
|
|
105
|
+
/**
|
|
106
|
+
* Classic single-line confirmations: `Continue? (y/n)`, `Proceed [y/N]:`
|
|
107
|
+
* and the spelled-out `(Y)es/(N)o` / `[Y]es / [N]o` forms.
|
|
108
|
+
*/
|
|
109
|
+
const YES_NO_TAIL = /(\(y\/n\)|\[y\/n\]|\(yes\/no\)|\(y\)es\s*[/,|]?\s*\(n\)o|\[y\]es\s*[/,|]?\s*\[n\]o)\s*:?\s*$/i;
|
|
110
|
+
/**
|
|
111
|
+
* A bracketed letter list ending the line: `[y,n,q,a,d,e,?]?` as git's
|
|
112
|
+
* hunk staging draws it, `(y/N/a)`. Only a list that offers both y and n
|
|
113
|
+
* counts (see `isYesNo`): `(a/b)` in prose is a ratio, not a question.
|
|
114
|
+
*/
|
|
115
|
+
const LETTER_LIST_TAIL = /[[(]\s*([a-z?](?:\s*[/,|]\s*[a-z?])+)\s*[\])]\s*[?:]?\s*$/i;
|
|
116
|
+
/**
|
|
117
|
+
* `Press enter to continue` / `…to confirm or esc to cancel`: the footer
|
|
118
|
+
* both Codex prompt classes draw, and a wait state in its own right on the
|
|
119
|
+
* onboarding screens that have no option list at all. `Enter to confirm ·
|
|
120
|
+
* Esc to cancel`, without "press", is Claude Code's.
|
|
121
|
+
*/
|
|
122
|
+
const PRESS_ENTER = /(?:press\s+)?\benter to (confirm|continue|select)/i;
|
|
123
|
+
/**
|
|
124
|
+
* Lines that mean the program is WORKING, not waiting. Spinner verbs with
|
|
125
|
+
* their ellipsis only. A bare `esc to cancel` must NOT count as busy, since
|
|
126
|
+
* Codex's `Press enter to confirm or esc to cancel` footer sits BELOW the
|
|
127
|
+
* option block and would veto the very prompt it belongs to. `Waiting for
|
|
128
|
+
* response…` is Grok's response spinner: it appears BELOW a just-answered
|
|
129
|
+
* option block while the block is still in the tail, and without the veto
|
|
130
|
+
* the stale block would go on alerting until it scrolled away. The ellipsis
|
|
131
|
+
* is REQUIRED, matching the verbs: plain prose that happens to say
|
|
132
|
+
* "waiting for response" must not veto a live prompt (a false negative here
|
|
133
|
+
* is an unalerted stall, the costly direction). Claude Code's spinner is busy
|
|
134
|
+
* whatever verb it shows: a spinner glyph at the start of the line, the verb
|
|
135
|
+
* or the task in hand, the ellipsis and often the time so far
|
|
136
|
+
* (`✻ Calculating… (14s · thinking)`). The line it leaves when a turn ends
|
|
137
|
+
* has no ellipsis (`✻ Cooked for 19m 12s · done 20:31`).
|
|
138
|
+
*/
|
|
139
|
+
const BUSY_LINE = /(generating|thinking|running|working|waiting for response)(\.{3}|…)|^\s*[·✢✳✶✻✽*] [^…]+…(?: \(.*\))?\s*$/i;
|
|
140
|
+
/** Horizontal rules and box-drawing rows (U+2500 to U+257F) carry no text content. */
|
|
141
|
+
const RULE_LINE = /^[\s─-╿-]*$/;
|
|
142
|
+
/**
|
|
143
|
+
* How far up from the bottom of the screen a live prompt can plausibly sit.
|
|
144
|
+
* Codex's wrapped option text plus footers span a dozen lines on their own,
|
|
145
|
+
* so this is generous. The busy veto below the block does the real work of
|
|
146
|
+
* rejecting scrollback.
|
|
147
|
+
*/
|
|
148
|
+
const TAIL_WINDOW = 20;
|
|
149
|
+
/**
|
|
150
|
+
* How many non-rule lines above the option block the question search may
|
|
151
|
+
* inspect. Codex separates its `Would you like to run…?` headline from the
|
|
152
|
+
* options with the full command text, so one line is not enough; the search
|
|
153
|
+
* still only ACCEPTS question-shaped lines, so a bigger budget cannot
|
|
154
|
+
* misattribute ordinary transcript text.
|
|
155
|
+
*/
|
|
156
|
+
const QUESTION_SEARCH_BUDGET = 8;
|
|
157
|
+
/**
|
|
158
|
+
* Grok's permission question row: two spaces, the bar, two spaces, `>`,
|
|
159
|
+
* then a space. With or without a question mark, that row is the question
|
|
160
|
+
* the box is asking. The composer is `│ >` with one space and does not match.
|
|
161
|
+
*/
|
|
162
|
+
const GROK_BOX_QUESTION = /^ {2}│ {2}> /;
|
|
163
|
+
/** Bar text in that same box: two spaces, the bar, two spaces, a word. */
|
|
164
|
+
const GROK_BAR_TEXT = /^ {2}│ {2}\S/;
|
|
165
|
+
/** The bare bar row that closes a command off from the reasoning above it. */
|
|
166
|
+
const GROK_BARE_BAR = /^ {2}│\s*$/;
|
|
167
|
+
/**
|
|
168
|
+
* A line that starts Grok's reasoning. The command walk does not see the
|
|
169
|
+
* signature. The thinking header is itself bar text, so the walk would
|
|
170
|
+
* otherwise take it; the spinner is not bar text, and a run below it is
|
|
171
|
+
* still closed off by it.
|
|
172
|
+
*/
|
|
173
|
+
const GROK_REASONING_START = /^ {2}│ {2}♦ Thinking|^ {3}[|/\\-] \S/;
|
|
174
|
+
/** A `$` on a command row, or a `:` continuation of one. */
|
|
175
|
+
const GROK_COMMAND_MARK = /\$|^ {2}│ {2}:/;
|
|
176
|
+
/** The fixed controls at the foot of OpenCode and MiMo Code's question menu. */
|
|
177
|
+
const QUESTION_MENU_FOOTER = /^ *┃ {2}↑↓ select {2}enter submit {2}esc dismiss\s*$/u;
|
|
178
|
+
const QUESTION_MENU_OPTION = /^ *┃ {2}([1-4])\. (\S.*)$/u;
|
|
179
|
+
const QUESTION_MENU_DESCRIPTION = /^ *┃ {5}\S/u;
|
|
180
|
+
const QUESTION_MENU_TEXT = /^ *┃ {2}(\S.*)$/u;
|
|
181
|
+
const QUESTION_MENU_BAR = /^ *┃\s*$/u;
|
|
182
|
+
function contentLines(raw) {
|
|
183
|
+
return raw
|
|
184
|
+
.map((text, index) => ({ text: text.trimEnd(), index }))
|
|
185
|
+
.filter((line) => line.text.trim() !== '');
|
|
186
|
+
}
|
|
187
|
+
function normalise(text) {
|
|
188
|
+
return text.replace(/\s+/g, ' ').trim();
|
|
189
|
+
}
|
|
190
|
+
function stripMarkers(text) {
|
|
191
|
+
return normalise(text.replace(/^[\s│❯›>]+/, ''));
|
|
192
|
+
}
|
|
193
|
+
/** A single-line confirmation tail. See YES_NO_TAIL and LETTER_LIST_TAIL. */
|
|
194
|
+
function isYesNo(text) {
|
|
195
|
+
if (YES_NO_TAIL.test(text))
|
|
196
|
+
return true;
|
|
197
|
+
const list = LETTER_LIST_TAIL.exec(text);
|
|
198
|
+
if (list === null)
|
|
199
|
+
return false;
|
|
200
|
+
const letters = (list[1] ?? '').toLowerCase().split(/[\s/,|]+/);
|
|
201
|
+
return letters.includes('y') && letters.includes('n');
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Decide whether the screen is showing a prompt that waits on a human.
|
|
205
|
+
* Returns null when the screen looks busy or shows no prompt shape.
|
|
206
|
+
*/
|
|
207
|
+
export const SELECTED_OPTION_MARKER = '⟦SP:selected⟧ ';
|
|
208
|
+
export function detectPrompt(screen) {
|
|
209
|
+
const original = screen.split('\n');
|
|
210
|
+
const plain = original.map((line) => line.replace(/^⟦SP:selected⟧ /, ''));
|
|
211
|
+
const prompt = detectPlainPrompt(plain.join('\n'));
|
|
212
|
+
if (prompt?.optionRows === undefined)
|
|
213
|
+
return prompt;
|
|
214
|
+
const selected = prompt.optionRows.flatMap((row, index) => original[row]?.startsWith(SELECTED_OPTION_MARKER) ? [index] : []);
|
|
215
|
+
return selected.length === 1 ? { ...prompt, selectedOption: selected[0] } : prompt;
|
|
216
|
+
}
|
|
217
|
+
function detectPlainPrompt(screen) {
|
|
218
|
+
const raw = screen.split('\n');
|
|
219
|
+
const lines = contentLines(raw);
|
|
220
|
+
if (lines.length === 0)
|
|
221
|
+
return null;
|
|
222
|
+
const tail = lines.slice(-TAIL_WINDOW);
|
|
223
|
+
const muse = musePrompt(raw, tail);
|
|
224
|
+
if (muse !== null)
|
|
225
|
+
return muse;
|
|
226
|
+
const questionMenu = openCodeQuestionMenu(raw, lines);
|
|
227
|
+
if (questionMenu !== null)
|
|
228
|
+
return questionMenu;
|
|
229
|
+
// A dialog a program draws on its own screen, recognised by its signature
|
|
230
|
+
// (`dialogs.ts`): the most specific test there is, so it goes first.
|
|
231
|
+
const dialog = dialogPrompt(raw);
|
|
232
|
+
if (dialog !== null)
|
|
233
|
+
return dialog;
|
|
234
|
+
// A clack step draws its own frame, and the frame alone says whether the
|
|
235
|
+
// step is waiting.
|
|
236
|
+
const clack = clackPrompt(tail, raw);
|
|
237
|
+
if (clack !== null)
|
|
238
|
+
return clack;
|
|
239
|
+
// Four chooser shapes, most specific first. A block that is found but
|
|
240
|
+
// not LIVE (a numbered list in the transcript, say) does not end the
|
|
241
|
+
// search: the shapes below it, and the single-line tails after, may be.
|
|
242
|
+
const blocks = [
|
|
243
|
+
numberedBlock(tail),
|
|
244
|
+
letterBlock(tail),
|
|
245
|
+
keyHintBlock(tail),
|
|
246
|
+
pointerBlock(tail, raw),
|
|
247
|
+
];
|
|
248
|
+
for (const block of blocks) {
|
|
249
|
+
if (block === null)
|
|
250
|
+
continue;
|
|
251
|
+
const prompt = livePrompt(block, tail, raw);
|
|
252
|
+
if (prompt !== null)
|
|
253
|
+
return prompt;
|
|
254
|
+
}
|
|
255
|
+
// No live option block: a y/n tail on the LAST content line is still a prompt.
|
|
256
|
+
const last = lines[lines.length - 1];
|
|
257
|
+
if (isYesNo(last.text) && !BUSY_LINE.test(last.text)) {
|
|
258
|
+
const question = normalise(last.text);
|
|
259
|
+
return { question, options: [], signature: question, rows: [last.index, last.index], basis: 'frame' };
|
|
260
|
+
}
|
|
261
|
+
// A bare press-enter wait (the Codex onboarding screens): the last content
|
|
262
|
+
// line asks for enter and nothing below it can contradict that.
|
|
263
|
+
if (PRESS_ENTER.test(last.text)) {
|
|
264
|
+
const question = normalise(last.text);
|
|
265
|
+
return { question, options: [], signature: question, rows: [last.index, last.index], basis: 'frame' };
|
|
266
|
+
}
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
function museQuestion(text) {
|
|
270
|
+
return /^Would .+ like to run the following command\?$/.test(text) || text === 'Do you trust this workspace?';
|
|
271
|
+
}
|
|
272
|
+
/** Muse's approval and trust menus use ⟩ with numbered rows and no composer. */
|
|
273
|
+
function musePrompt(raw, tail) {
|
|
274
|
+
const selected = [...tail].reverse().find((line) => /^⟩ [1-3](?:\. | {2})/.test(line.text));
|
|
275
|
+
if (selected === undefined)
|
|
276
|
+
return null;
|
|
277
|
+
const marker = /^⟩ ([1-3])(\. | {2})/.exec(selected.text);
|
|
278
|
+
if (marker === null)
|
|
279
|
+
return null;
|
|
280
|
+
const selectedOption = Number(marker[1]) - 1;
|
|
281
|
+
const approval = marker[2] === '. ';
|
|
282
|
+
const first = selected.index - selectedOption;
|
|
283
|
+
// Muse omits the workspace-wide choice for script blocks and some commands.
|
|
284
|
+
// The second row identifies that form even when the heading stays plain.
|
|
285
|
+
const twoOptionApproval = approval && /^(?:⟩ | {2})2\. Abort the entire command\s*\(esc\)/.test(raw[first + 1] ?? '');
|
|
286
|
+
const count = approval && !twoOptionApproval ? 3 : 2;
|
|
287
|
+
if (selectedOption >= count)
|
|
288
|
+
return null;
|
|
289
|
+
const options = [];
|
|
290
|
+
const optionRows = [];
|
|
291
|
+
for (let offset = 0; offset < count; offset++) {
|
|
292
|
+
const row = first + offset;
|
|
293
|
+
const text = (raw[row] ?? '').trimEnd();
|
|
294
|
+
const prefix = `${offset === selectedOption ? '⟩ ' : ' '}${String(offset + 1)}${approval ? '. ' : ' '}`;
|
|
295
|
+
if (!text.startsWith(prefix) || text.slice(prefix.length).trim() === '')
|
|
296
|
+
return null;
|
|
297
|
+
options.push(normalise(text.replace(/^⟩ /, '')));
|
|
298
|
+
optionRows.push(row);
|
|
299
|
+
}
|
|
300
|
+
// Walk the visible menu itself: a long wrapped command can put the heading
|
|
301
|
+
// beyond the tail and more than ten rows above the first choice. The first
|
|
302
|
+
// transcript or composer boundary prevents borrowing an answered question.
|
|
303
|
+
let question;
|
|
304
|
+
for (let index = first - 1; index >= 0 && first - index <= 60; index--) {
|
|
305
|
+
const text = (raw[index] ?? '').trimEnd();
|
|
306
|
+
if (museQuestion(text)) {
|
|
307
|
+
question = { text, index };
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
if (/^\s*(?:[◆◇◈]|❯(?:\s|$)|[─━═]{2,})/u.test(text))
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
if (question === undefined)
|
|
314
|
+
return null;
|
|
315
|
+
if (approval !== question.text.startsWith('Would '))
|
|
316
|
+
return null;
|
|
317
|
+
// A quoted menu inside an answer has no new Muse input border before it.
|
|
318
|
+
const before = raw.slice(0, question.index);
|
|
319
|
+
let border = -1;
|
|
320
|
+
for (let index = before.length - 1; index >= 0; index--) {
|
|
321
|
+
if (/^\s*── .*Voice input/.test(before[index])) {
|
|
322
|
+
border = index;
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
if (before.slice(border + 1).some((line) => /^\s*◆/.test(line)))
|
|
327
|
+
return null;
|
|
328
|
+
const stage = approval ? raw.slice(question.index, first).find((line) => /^ {2}Stage \d+\/\d+$/.test(line.trimEnd())) : undefined;
|
|
329
|
+
// Only this menu's hint and footer, blank rows and Muse's status footer
|
|
330
|
+
// belong below its options. Other output means the menu is historical.
|
|
331
|
+
if (raw.slice(optionRows[optionRows.length - 1] + 1).some((line) => {
|
|
332
|
+
const text = line.trimEnd();
|
|
333
|
+
return text.trim() !== '' && !/^Change approval defaults in \/permissions$/.test(text) &&
|
|
334
|
+
(approval || !/^Use Up\/Down or 1\/2, then Enter\. Esc quits\.$/.test(text)) &&
|
|
335
|
+
!/^\s*[─━═]{2,}$/.test(text) &&
|
|
336
|
+
!/^ {2}\S[^·]* · [^·]+ · [^·]+ · Launch overrides$/u.test(text);
|
|
337
|
+
}))
|
|
338
|
+
return null;
|
|
339
|
+
return {
|
|
340
|
+
question: question.text,
|
|
341
|
+
options,
|
|
342
|
+
selectedOption,
|
|
343
|
+
optionRows,
|
|
344
|
+
signature: [question.text, stage ?? '', ...options].join(' | '),
|
|
345
|
+
rows: [question.index, optionRows[optionRows.length - 1]],
|
|
346
|
+
basis: 'marker',
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
/** Tracks the visible menu block, independently of activity above it. */
|
|
350
|
+
export class MusePromptSettler {
|
|
351
|
+
menu = null;
|
|
352
|
+
since = 0;
|
|
353
|
+
detect(screen, now = Date.now(), styled) {
|
|
354
|
+
const prompt = detectPrompt(screen);
|
|
355
|
+
if (prompt === null) {
|
|
356
|
+
this.menu = null;
|
|
357
|
+
return null;
|
|
358
|
+
}
|
|
359
|
+
if (!prompt.question.startsWith('Would ') || !prompt.signature.includes('Stage ') ||
|
|
360
|
+
prompt.optionRows === undefined) {
|
|
361
|
+
this.menu = null;
|
|
362
|
+
return prompt;
|
|
363
|
+
}
|
|
364
|
+
const atRow = styled?.[prompt.rows[0]];
|
|
365
|
+
const heading = atRow?.text === prompt.question ? atRow :
|
|
366
|
+
[...(styled ?? [])].reverse().find((line) => line.text === prompt.question);
|
|
367
|
+
const bold = heading !== undefined && heading.spans.some((span) => span.bold && span.to - span.from >= prompt.question.length / 2);
|
|
368
|
+
if (bold) {
|
|
369
|
+
this.menu = null;
|
|
370
|
+
return prompt;
|
|
371
|
+
}
|
|
372
|
+
// The heading to the last option row: a status footer below may tick.
|
|
373
|
+
const block = screen.split('\n').slice(prompt.rows[0], prompt.rows[1] + 1).join('\n');
|
|
374
|
+
if (block !== this.menu) {
|
|
375
|
+
this.menu = block;
|
|
376
|
+
this.since = now;
|
|
377
|
+
}
|
|
378
|
+
return now - this.since >= MUSE_MENU_SETTLE_MS ? prompt : null;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
/** A live left-ruled question menu with its fixed footer and custom-answer choice. */
|
|
382
|
+
function openCodeQuestionMenu(raw, lines) {
|
|
383
|
+
const footer = [...lines].reverse().find((line) => QUESTION_MENU_FOOTER.test(line.text));
|
|
384
|
+
if (footer === undefined)
|
|
385
|
+
return null;
|
|
386
|
+
const after = lines.filter((line) => line.index > footer.index);
|
|
387
|
+
// The programme may draw a status line below the menu, but another input
|
|
388
|
+
// box or a resumed transcript means this menu is no longer waiting.
|
|
389
|
+
if (after.length > 2 || after.some((line) => BUSY_LINE.test(line.text) || /[╭╰│❯›]/u.test(line.text)))
|
|
390
|
+
return null;
|
|
391
|
+
let cursor = footer.index - 1;
|
|
392
|
+
while (QUESTION_MENU_BAR.test(raw[cursor] ?? ''))
|
|
393
|
+
cursor--;
|
|
394
|
+
const options = [];
|
|
395
|
+
for (let count = 0; count < 4 && cursor >= 0; count++) {
|
|
396
|
+
while (QUESTION_MENU_DESCRIPTION.test(raw[cursor] ?? ''))
|
|
397
|
+
cursor--;
|
|
398
|
+
const row = QUESTION_MENU_OPTION.exec(raw[cursor] ?? '');
|
|
399
|
+
if (row === null)
|
|
400
|
+
break;
|
|
401
|
+
options.unshift(row[2]);
|
|
402
|
+
cursor--;
|
|
403
|
+
}
|
|
404
|
+
// The walk above collects option rows from the bottom. Their numbers must
|
|
405
|
+
// be consecutive, with the final row offering the custom answer.
|
|
406
|
+
if (options.length < 2 || options.length > 4 || options.at(-1) !== 'Type your own answer')
|
|
407
|
+
return null;
|
|
408
|
+
const firstOption = cursor + 1;
|
|
409
|
+
const numbered = raw.slice(firstOption, footer.index).map((line) => QUESTION_MENU_OPTION.exec(line)?.[1]);
|
|
410
|
+
if (numbered.filter((value) => value !== undefined).join(',') !== options.map((_, i) => String(i + 1)).join(','))
|
|
411
|
+
return null;
|
|
412
|
+
if (!QUESTION_MENU_BAR.test(raw[cursor] ?? ''))
|
|
413
|
+
return null;
|
|
414
|
+
while (QUESTION_MENU_BAR.test(raw[cursor] ?? ''))
|
|
415
|
+
cursor--;
|
|
416
|
+
const question = [];
|
|
417
|
+
while (cursor >= 0 && question.length < 8) {
|
|
418
|
+
const row = QUESTION_MENU_TEXT.exec(raw[cursor] ?? '');
|
|
419
|
+
if (row === null || QUESTION_MENU_OPTION.test(raw[cursor] ?? ''))
|
|
420
|
+
break;
|
|
421
|
+
question.unshift(row[1]);
|
|
422
|
+
cursor--;
|
|
423
|
+
}
|
|
424
|
+
if (question.length === 0)
|
|
425
|
+
return null;
|
|
426
|
+
const first = cursor + 1;
|
|
427
|
+
return {
|
|
428
|
+
question: normalise(question.join(' ')),
|
|
429
|
+
options,
|
|
430
|
+
optionRows: raw.slice(firstOption, footer.index).map((line, index) => QUESTION_MENU_OPTION.test(line) ? firstOption + index : -1).filter((index) => index >= 0),
|
|
431
|
+
signature: normalise(raw.slice(first, footer.index + 1).join(' ')),
|
|
432
|
+
rows: [first, footer.index],
|
|
433
|
+
basis: 'footer',
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* A clack step waiting on its answer: a `◆` or `▲` headline, choice rows in
|
|
438
|
+
* the gutter under it and the `└` that closes the step. The headline alone
|
|
439
|
+
* says whether the step waits, because clack redraws a step once it is
|
|
440
|
+
* answered or cancelled. A waiting step is also the last thing drawn: anything
|
|
441
|
+
* under its `└` means nothing is waiting on it, as when a wizard is killed with
|
|
442
|
+
* its question still on screen and the shell's output follows, or when an agent
|
|
443
|
+
* prints a step-shaped diagram above its own input box. Only the rest of a
|
|
444
|
+
* validation message may sit there, directly under the `└` that carries it and
|
|
445
|
+
* indented as clack indents it (VALIDATION_REST).
|
|
446
|
+
*/
|
|
447
|
+
function clackPrompt(tail, raw) {
|
|
448
|
+
const text = (index) => (raw[index] ?? '').trimEnd();
|
|
449
|
+
let lastRow = null;
|
|
450
|
+
for (const line of tail) {
|
|
451
|
+
if (CLACK_ROW.test(line.text))
|
|
452
|
+
lastRow = line;
|
|
453
|
+
}
|
|
454
|
+
if (lastRow === null)
|
|
455
|
+
return null;
|
|
456
|
+
// Up the gutter to the headline: every line of a step is in the gutter,
|
|
457
|
+
// wrapped message and option text included.
|
|
458
|
+
let top = lastRow.index;
|
|
459
|
+
while (CLACK_GUTTER.test(text(top - 1)))
|
|
460
|
+
top--;
|
|
461
|
+
const headline = CLACK_HEADLINE.exec(text(top - 1));
|
|
462
|
+
if (headline === null)
|
|
463
|
+
return null;
|
|
464
|
+
// Down past the footer to the `└`. On a narrow screen the terminal wraps
|
|
465
|
+
// the end of the footer out of the gutter onto a line of its own.
|
|
466
|
+
let end = lastRow.index + 1;
|
|
467
|
+
while (text(end) !== '' && !CLACK_BAR_END.test(text(end)))
|
|
468
|
+
end++;
|
|
469
|
+
if (!CLACK_BAR_END.test(text(end)))
|
|
470
|
+
return null;
|
|
471
|
+
if (raw.slice(lastRow.index + 1).some((line) => BUSY_LINE.test(line)))
|
|
472
|
+
return null;
|
|
473
|
+
let after = end + 1;
|
|
474
|
+
if (/^└ {2}\S/.test(text(end)))
|
|
475
|
+
while (VALIDATION_REST.test(text(after)))
|
|
476
|
+
after++;
|
|
477
|
+
if (raw.slice(after).some((line) => line.trim() !== ''))
|
|
478
|
+
return null;
|
|
479
|
+
// The message runs from the headline down to the first row, less the
|
|
480
|
+
// `...` clack draws where it has scrolled rows out of view. A step with no
|
|
481
|
+
// message of its own is asked by the message logged just above it.
|
|
482
|
+
const message = headline[1] === undefined ? loggedAbove(raw, top - 1) : [headline[1]];
|
|
483
|
+
const options = [];
|
|
484
|
+
const optionRows = [];
|
|
485
|
+
let radios = 0;
|
|
486
|
+
let selected = 0;
|
|
487
|
+
for (let index = top; index <= lastRow.index; index++) {
|
|
488
|
+
if (!CLACK_ROW.test(text(index))) {
|
|
489
|
+
const wrapped = text(index).slice(1).trim();
|
|
490
|
+
if (options.length === 0 && wrapped !== '...')
|
|
491
|
+
message.push(wrapped);
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
// A confirm draws both of its choices on one row.
|
|
495
|
+
for (const choice of text(index).slice(3).split(/ \/ (?=[●○] )/)) {
|
|
496
|
+
const glyph = choice.charAt(0);
|
|
497
|
+
if (glyph === '●' || glyph === '○')
|
|
498
|
+
radios++;
|
|
499
|
+
if (glyph === '●')
|
|
500
|
+
selected++;
|
|
501
|
+
options.push(normalise(choice.slice(2)));
|
|
502
|
+
optionRows.push(index);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
// A select has exactly one option selected, and so has a confirm.
|
|
506
|
+
if (radios > 0 && selected !== 1)
|
|
507
|
+
return null;
|
|
508
|
+
// What moves with the selection stays out of the signature: the glyphs,
|
|
509
|
+
// and the hint clack draws in brackets beside the focused or checked row.
|
|
510
|
+
const withoutHint = (option) => option.replace(/ \(.*$/, '');
|
|
511
|
+
const question = normalise(message.join(' ')) || withoutHint(options[0] ?? '');
|
|
512
|
+
return {
|
|
513
|
+
question,
|
|
514
|
+
options,
|
|
515
|
+
optionRows,
|
|
516
|
+
signature: [question, ...options.map(withoutHint)].join(' | '),
|
|
517
|
+
// From the headline to the bar end, a validation message under it included.
|
|
518
|
+
rows: [top - 1, after - 1],
|
|
519
|
+
basis: 'frame',
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* The message clack logged directly above the line at `index`: its first
|
|
524
|
+
* line (`● Accessing workspace:`) and the lines in the gutter under it.
|
|
525
|
+
* Empty when what is above is not a logged message.
|
|
526
|
+
*
|
|
527
|
+
* A line longer than the terminal is wide goes on at the start of the next
|
|
528
|
+
* row, out of the gutter. Such a row is joined to the row above it as it
|
|
529
|
+
* stands, since the break can fall inside a word, but only when the row above
|
|
530
|
+
* fills the screen's width as far as the screen shows it (its longest line, or
|
|
531
|
+
* one short where the break trimmed a space). Any other line out of the gutter
|
|
532
|
+
* is not part of the message.
|
|
533
|
+
*/
|
|
534
|
+
function loggedAbove(raw, index) {
|
|
535
|
+
const width = Math.max(...raw.map((line) => line.trimEnd().length));
|
|
536
|
+
const lines = [];
|
|
537
|
+
let wrapped = '';
|
|
538
|
+
for (let at = index - 1; at >= 0; at--) {
|
|
539
|
+
const line = (raw[at] ?? '').trimEnd();
|
|
540
|
+
const first = CLACK_LOG.exec(line);
|
|
541
|
+
if (first !== null)
|
|
542
|
+
return [`${first[1] ?? ''}${wrapped}`, ...lines];
|
|
543
|
+
if (CLACK_GUTTER.test(line)) {
|
|
544
|
+
lines.unshift(`${line.slice(1).trim()}${wrapped}`);
|
|
545
|
+
wrapped = '';
|
|
546
|
+
continue;
|
|
547
|
+
}
|
|
548
|
+
if (line.trim() === '')
|
|
549
|
+
return [];
|
|
550
|
+
if ((raw[at - 1] ?? '').trimEnd().length < width - 1)
|
|
551
|
+
return [];
|
|
552
|
+
wrapped = `${line}${wrapped}`;
|
|
553
|
+
}
|
|
554
|
+
return [];
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Numbered option rows anywhere in the tail, wrap-tolerant: Codex wraps a
|
|
558
|
+
* long option's text across several lines, so consecutive-row detection
|
|
559
|
+
* would never see its blocks. Rows whose numbers never
|
|
560
|
+
* DECREASE belong to one chooser; a decrease means a new list started and
|
|
561
|
+
* only the newest can be live.
|
|
562
|
+
*/
|
|
563
|
+
function numberedBlock(tail) {
|
|
564
|
+
let rows = [];
|
|
565
|
+
let previousNumber = 0;
|
|
566
|
+
for (const line of tail) {
|
|
567
|
+
const match = OPTION_ROW.exec(line.text);
|
|
568
|
+
if (match === null)
|
|
569
|
+
continue;
|
|
570
|
+
const number = Number(match[1]);
|
|
571
|
+
if (number < previousNumber)
|
|
572
|
+
rows = [];
|
|
573
|
+
previousNumber = number;
|
|
574
|
+
rows.push({ line, option: stripMarkers(line.text) });
|
|
575
|
+
}
|
|
576
|
+
return rows.length >= 2 ? { rows, kind: 'numbered' } : null;
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Letter-keyed rows: `[Y] Yes` / `[N] No` / `[A] Always`. One block is rows
|
|
580
|
+
* with DISTINCT keys no more than a few lines apart; a repeated key (a
|
|
581
|
+
* checkbox list's `[x]`, `[x]`) or a gap starts over, keeping the newest.
|
|
582
|
+
*/
|
|
583
|
+
function letterBlock(tail) {
|
|
584
|
+
let rows = [];
|
|
585
|
+
let keys = new Set();
|
|
586
|
+
let lastIndex = -1;
|
|
587
|
+
for (const line of tail) {
|
|
588
|
+
const match = LETTER_ROW.exec(line.text);
|
|
589
|
+
if (match === null)
|
|
590
|
+
continue;
|
|
591
|
+
const key = (match[1] ?? '').toLowerCase();
|
|
592
|
+
if ((lastIndex !== -1 && line.index - lastIndex > 3) || keys.has(key)) {
|
|
593
|
+
rows = [];
|
|
594
|
+
keys = new Set();
|
|
595
|
+
}
|
|
596
|
+
keys.add(key);
|
|
597
|
+
lastIndex = line.index;
|
|
598
|
+
rows.push({ line, option: stripMarkers(line.text) });
|
|
599
|
+
}
|
|
600
|
+
return rows.length >= 2 ? { rows, kind: 'letter' } : null;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Options with their keys after them, on adjacent rows that share an indent
|
|
604
|
+
* and a key column: Grok's folder-trust question. Plenty of ordinary lines
|
|
605
|
+
* end in a single letter, so the block must look like a yes/no chooser: each
|
|
606
|
+
* key is the first letter of its label, as a key hint is, and the keys offer
|
|
607
|
+
* both y and n. A table of y/n flags fails the first test and a list of
|
|
608
|
+
* keyboard shortcuts the second.
|
|
609
|
+
*/
|
|
610
|
+
function keyHintBlock(tail) {
|
|
611
|
+
let rows = [];
|
|
612
|
+
let keys = [];
|
|
613
|
+
let layout = '';
|
|
614
|
+
let lastIndex = -1;
|
|
615
|
+
for (const line of tail) {
|
|
616
|
+
const match = KEY_HINT_ROW.exec(line.text);
|
|
617
|
+
if (match === null)
|
|
618
|
+
continue;
|
|
619
|
+
const label = match[2] ?? '';
|
|
620
|
+
const key = (match[3] ?? '').toLowerCase();
|
|
621
|
+
if (label.charAt(0).toLowerCase() !== key)
|
|
622
|
+
continue;
|
|
623
|
+
// Rows of one chooser line up on both the indent and the key's column.
|
|
624
|
+
const indent = (match[1] ?? '').length;
|
|
625
|
+
const keyColumn = line.text.length - 1;
|
|
626
|
+
const lineLayout = `${String(indent)}:${String(keyColumn)}`;
|
|
627
|
+
if (line.index !== lastIndex + 1 || lineLayout !== layout) {
|
|
628
|
+
rows = [];
|
|
629
|
+
keys = [];
|
|
630
|
+
}
|
|
631
|
+
layout = lineLayout;
|
|
632
|
+
lastIndex = line.index;
|
|
633
|
+
rows.push({ line, option: normalise(line.text) });
|
|
634
|
+
keys.push(key);
|
|
635
|
+
}
|
|
636
|
+
return keys.includes('y') && keys.includes('n') ? { rows, kind: 'key-hint' } : null;
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* A pointer against unnumbered rows: the LAST pointer row in the tail plus
|
|
640
|
+
* the rows ADJACENT to it whose text starts in the same column. That is the
|
|
641
|
+
* shape inquirer.js and Claude Code's folder-trust prompt draw. Adjacent
|
|
642
|
+
* means adjacent: a blank line or a rule ends the block, which is what keeps
|
|
643
|
+
* a composer line (`❯ Try "…"`) apart from the status lines drawn under its
|
|
644
|
+
* rule. Claude Code's idle screen would otherwise read as a three-option
|
|
645
|
+
* chooser.
|
|
646
|
+
*/
|
|
647
|
+
function pointerBlock(tail, raw) {
|
|
648
|
+
let pointer = null;
|
|
649
|
+
for (const line of tail) {
|
|
650
|
+
const match = POINTER_ROW.exec(line.text);
|
|
651
|
+
if (match === null)
|
|
652
|
+
continue;
|
|
653
|
+
const text = match[3] ?? '';
|
|
654
|
+
if (!/[\p{L}\p{N}]/u.test(text))
|
|
655
|
+
continue; // `│ > │`: a box, not a choice
|
|
656
|
+
pointer = { line, textCol: line.text.length - text.length };
|
|
657
|
+
}
|
|
658
|
+
if (pointer === null)
|
|
659
|
+
return null;
|
|
660
|
+
const { textCol } = pointer;
|
|
661
|
+
const sibling = (index) => {
|
|
662
|
+
const text = (raw[index] ?? '').trimEnd();
|
|
663
|
+
if (text.length <= textCol)
|
|
664
|
+
return null;
|
|
665
|
+
if (text.slice(0, textCol).trim() !== '')
|
|
666
|
+
return null;
|
|
667
|
+
if (/\s/.test(text.charAt(textCol)))
|
|
668
|
+
return null;
|
|
669
|
+
if (POINTER_ROW.test(text))
|
|
670
|
+
return null;
|
|
671
|
+
return { text, index };
|
|
672
|
+
};
|
|
673
|
+
const above = [];
|
|
674
|
+
for (let i = pointer.line.index - 1; i >= 0; i--) {
|
|
675
|
+
const line = sibling(i);
|
|
676
|
+
if (line === null)
|
|
677
|
+
break;
|
|
678
|
+
above.unshift(line);
|
|
679
|
+
}
|
|
680
|
+
const below = [];
|
|
681
|
+
for (let i = pointer.line.index + 1; i < raw.length; i++) {
|
|
682
|
+
const line = sibling(i);
|
|
683
|
+
if (line === null)
|
|
684
|
+
break;
|
|
685
|
+
below.push(line);
|
|
686
|
+
}
|
|
687
|
+
const all = [...above, pointer.line, ...below];
|
|
688
|
+
if (all.length < 2)
|
|
689
|
+
return null;
|
|
690
|
+
return { rows: all.map((line) => ({ line, option: stripMarkers(line.text) })), kind: 'pointer' };
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Where an older Grok box's command starts. The command is plain bar text
|
|
694
|
+
* above the radios. The walk may cross the one bare bar between that text
|
|
695
|
+
* and the radios. It stops at the next bare bar, or at a line that starts
|
|
696
|
+
* Grok's reasoning (`♦ Thinking`, or the spinner). The run is taken only
|
|
697
|
+
* when a line in it is command evidence: the box's `│ > ` question row,
|
|
698
|
+
* a `$`, or a `:` continuation. Otherwise the run is the same shape as
|
|
699
|
+
* reasoning, and the radios stay the start.
|
|
700
|
+
*/
|
|
701
|
+
function commandAboveRadios(raw, firstOption) {
|
|
702
|
+
const text = (index) => (raw[index] ?? '').trimEnd();
|
|
703
|
+
if (!/^ {2}│ {2}\d+ \([○•●]\) /.test(text(firstOption)))
|
|
704
|
+
return firstOption;
|
|
705
|
+
const above = firstOption - 1;
|
|
706
|
+
// The one bare bar between the command and the radios may be crossed.
|
|
707
|
+
// Text sitting directly on the radios is the command itself.
|
|
708
|
+
const gap = GROK_BARE_BAR.test(text(above));
|
|
709
|
+
let cursor = gap ? above - 1 : above;
|
|
710
|
+
let start = firstOption;
|
|
711
|
+
let sawText = false;
|
|
712
|
+
let sawEvidence = false;
|
|
713
|
+
while (cursor >= 0 && GROK_BAR_TEXT.test(text(cursor)) && !GROK_REASONING_START.test(text(cursor))) {
|
|
714
|
+
sawText = true;
|
|
715
|
+
if (GROK_BOX_QUESTION.test(text(cursor)) || GROK_COMMAND_MARK.test(text(cursor)))
|
|
716
|
+
sawEvidence = true;
|
|
717
|
+
start = cursor;
|
|
718
|
+
cursor--;
|
|
719
|
+
}
|
|
720
|
+
if (!sawText || !sawEvidence)
|
|
721
|
+
return firstOption;
|
|
722
|
+
if (GROK_BARE_BAR.test(text(cursor)) || GROK_REASONING_START.test(text(cursor)))
|
|
723
|
+
return start;
|
|
724
|
+
return firstOption;
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Whether a block is a prompt the program is WAITING on, and if so the
|
|
728
|
+
* prompt as the consumer should see it.
|
|
729
|
+
*/
|
|
730
|
+
function livePrompt(block, tail, raw) {
|
|
731
|
+
const { rows, kind } = block;
|
|
732
|
+
const blockStart = rows[0].line.index;
|
|
733
|
+
const blockEnd = rows[rows.length - 1].line.index;
|
|
734
|
+
// The program drawing BUSY output below the options means it moved on:
|
|
735
|
+
// the block is history. (The `Press enter…` footer below the block is
|
|
736
|
+
// the opposite: it confirms the prompt is live.)
|
|
737
|
+
const below = tail.filter((line) => line.index > blockEnd);
|
|
738
|
+
if (below.some((line) => BUSY_LINE.test(line.text)))
|
|
739
|
+
return null;
|
|
740
|
+
const hasMarker = tail.some((line) => line.index >= blockStart && line.index <= blockEnd && MARKER_LINE.test(line.text));
|
|
741
|
+
const hasPressEnter = below.some((line) => PRESS_ENTER.test(line.text));
|
|
742
|
+
const nothingBelow = below.every((line) => RULE_LINE.test(line.text));
|
|
743
|
+
// A question mark above the bare bar that closes an older Grok command is
|
|
744
|
+
// reasoning. The command below that bar is claimed on its own, further down.
|
|
745
|
+
const commandStart = commandAboveRadios(raw, blockStart);
|
|
746
|
+
let question = findQuestion(tail, blockStart);
|
|
747
|
+
if (question !== null && commandStart < blockStart && question.index < commandStart)
|
|
748
|
+
question = null;
|
|
749
|
+
let live;
|
|
750
|
+
let basis;
|
|
751
|
+
if (kind === 'key-hint') {
|
|
752
|
+
// The key hints are the block's own markers; the question above must
|
|
753
|
+
// frame it.
|
|
754
|
+
live = question !== null;
|
|
755
|
+
basis = 'frame';
|
|
756
|
+
}
|
|
757
|
+
else if (kind === 'letter') {
|
|
758
|
+
// Distinct letter keys are the block. The weak signal the filter
|
|
759
|
+
// declines is a numbered list with nothing below it, which is also how
|
|
760
|
+
// reasoning draws a list. A letter menu is not that.
|
|
761
|
+
live = hasMarker || hasPressEnter || (question !== null && nothingBelow);
|
|
762
|
+
basis = 'frame';
|
|
763
|
+
}
|
|
764
|
+
else if (kind === 'pointer') {
|
|
765
|
+
// The pointer is the block's own marker, so it proves nothing by itself:
|
|
766
|
+
// Claude Code draws the same `❯` on each message in its transcript and on
|
|
767
|
+
// the draft in its composer. An enter-to-confirm footer below frames the
|
|
768
|
+
// block, or a question drawn with it, directly above with no blank line
|
|
769
|
+
// between, as inquirer draws it. A transcript sets each message apart
|
|
770
|
+
// with a blank line, so a question above an echoed message belongs to an
|
|
771
|
+
// earlier one. What follows the choices cannot decide: inquirer draws a
|
|
772
|
+
// description of the focused choice and a key-help line there.
|
|
773
|
+
live = hasPressEnter || (question !== null && drawnTogether(tail, question.index, blockStart));
|
|
774
|
+
basis = hasPressEnter ? 'footer' : 'marker';
|
|
775
|
+
}
|
|
776
|
+
else if (hasMarker) {
|
|
777
|
+
// A focused-option marker or a press-enter footer is the strongest
|
|
778
|
+
// signal a selector is live. Without either, a question-shaped headline
|
|
779
|
+
// counts only when the block ENDS the screen: a program blocked on a
|
|
780
|
+
// choice draws nothing after its options, while an agent that has
|
|
781
|
+
// finished a turn draws its composer under the numbered list it printed.
|
|
782
|
+
live = true;
|
|
783
|
+
basis = 'marker';
|
|
784
|
+
}
|
|
785
|
+
else if (hasPressEnter) {
|
|
786
|
+
live = true;
|
|
787
|
+
basis = 'footer';
|
|
788
|
+
}
|
|
789
|
+
else {
|
|
790
|
+
live = question !== null && nothingBelow;
|
|
791
|
+
basis = 'question';
|
|
792
|
+
}
|
|
793
|
+
if (!live)
|
|
794
|
+
return null;
|
|
795
|
+
const options = rows.map((row) => row.option);
|
|
796
|
+
const headline = question?.text ?? options[0];
|
|
797
|
+
// The signature must survive the selection MOVING (arrow keys flip which
|
|
798
|
+
// radio is filled). The prompt is still the same prompt, and resetting
|
|
799
|
+
// the stall clock on every selection change would let a held prompt
|
|
800
|
+
// evade the threshold forever. Normalise filled radios to hollow in the
|
|
801
|
+
// signature only; `options` keeps the live state for the consumer.
|
|
802
|
+
const stable = (text) => text.replace(/\([•●]\)/g, '(○)');
|
|
803
|
+
// From the question, when one is found, down to the last option row:
|
|
804
|
+
// what a program draws between the two belongs to the question too.
|
|
805
|
+
// With no question, an older Grok box still owns the command text a bare
|
|
806
|
+
// bar, or a reasoning start, closes off above its radios, when that text
|
|
807
|
+
// carries command evidence.
|
|
808
|
+
let start = question?.index ?? blockStart;
|
|
809
|
+
if (question === null)
|
|
810
|
+
start = Math.min(start, commandStart);
|
|
811
|
+
return {
|
|
812
|
+
question: headline,
|
|
813
|
+
options,
|
|
814
|
+
optionRows: rows.map((row) => row.line.index),
|
|
815
|
+
signature: [headline, ...options].map(stable).join(' | '),
|
|
816
|
+
rows: [start, blockEnd],
|
|
817
|
+
basis,
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* Whether the prompt on screen is the one the caller has ALREADY ANSWERED.
|
|
822
|
+
* A supervision loop is wait-for-prompt → answer → wait, and the second wait
|
|
823
|
+
* can look before the program has repainted; it must not return the
|
|
824
|
+
* question just answered as a new one.
|
|
825
|
+
*
|
|
826
|
+
* The guard remembers the prompt showing when input was last written and
|
|
827
|
+
* treats it as stale until an observation has seen the screen without it, or
|
|
828
|
+
* with a different prompt. An observation is a wait tick, a snapshot or the
|
|
829
|
+
* daemon's stall scan. So a prompt with the SAME text drawn later is
|
|
830
|
+
* new only once the earlier one was seen to go; a program that ignored the
|
|
831
|
+
* input keeps showing a stale prompt, and the wait says so rather than
|
|
832
|
+
* guessing.
|
|
833
|
+
*/
|
|
834
|
+
export class StalePromptGuard {
|
|
835
|
+
signatureAtInput = null;
|
|
836
|
+
cleared = false;
|
|
837
|
+
/** Input is being written; `screen` is what the program showed at that moment. */
|
|
838
|
+
noteInput(screen) {
|
|
839
|
+
this.signatureAtInput = detectPrompt(screen)?.signature ?? null;
|
|
840
|
+
this.cleared = false;
|
|
841
|
+
}
|
|
842
|
+
/** A look at the screen found this prompt (or none, when null). */
|
|
843
|
+
observe(signature) {
|
|
844
|
+
if (this.signatureAtInput !== null && signature !== this.signatureAtInput) {
|
|
845
|
+
this.cleared = true;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
/** True while `signature` is the answered prompt, not yet seen to go away. */
|
|
849
|
+
isStale(signature) {
|
|
850
|
+
return this.signatureAtInput !== null && signature === this.signatureAtInput && !this.cleared;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* A question-shaped line above the option block: skips rules and box borders,
|
|
855
|
+
* accepts a line that asks something or names a request ("Permission
|
|
856
|
+
* Required", "Allow this command to run?", "Do you trust…", "Would you
|
|
857
|
+
* like…"). Codex puts the command text between the headline and the
|
|
858
|
+
* options, so the search inspects up to QUESTION_SEARCH_BUDGET non-rule
|
|
859
|
+
* lines. It only ever RETURNS a question-shaped line, so the budget cannot
|
|
860
|
+
* misattribute ordinary transcript text. A question mark anywhere counts,
|
|
861
|
+
* not only at the end: Claude Code's trust prompt asks "…one you trust?
|
|
862
|
+
* (Like your own code…" and inquirer's `? Select a template: (Use arrow keys)`
|
|
863
|
+
* leads with it. Returns the line, its text normalised.
|
|
864
|
+
*/
|
|
865
|
+
function findQuestion(tail, blockStart) {
|
|
866
|
+
const above = tail.filter((line) => line.index < blockStart);
|
|
867
|
+
let inspected = 0;
|
|
868
|
+
for (let i = above.length - 1; i >= 0 && inspected < QUESTION_SEARCH_BUDGET; i--) {
|
|
869
|
+
const line = above[i];
|
|
870
|
+
if (RULE_LINE.test(line.text))
|
|
871
|
+
continue;
|
|
872
|
+
inspected++;
|
|
873
|
+
// Only Grok's exact box prefix is chrome. A question may itself begin
|
|
874
|
+
// with `>` or a box character, so leave everything after that prefix.
|
|
875
|
+
const cleaned = normalise(GROK_BOX_QUESTION.test(line.text)
|
|
876
|
+
? line.text.replace(GROK_BOX_QUESTION, '')
|
|
877
|
+
: line.text);
|
|
878
|
+
if (GROK_BOX_QUESTION.test(line.text) ||
|
|
879
|
+
/\?/.test(cleaned) ||
|
|
880
|
+
/permission required/i.test(cleaned) ||
|
|
881
|
+
/^allow\b/i.test(cleaned) ||
|
|
882
|
+
/do you trust/i.test(cleaned) ||
|
|
883
|
+
/^would you like\b/i.test(cleaned)) {
|
|
884
|
+
return { text: cleaned, index: line.index };
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
return null;
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* Whether two lines are drawn together: every line between them has content.
|
|
891
|
+
* Blank lines are the only lines between two lines of the tail that the tail
|
|
892
|
+
* leaves out.
|
|
893
|
+
*/
|
|
894
|
+
function drawnTogether(tail, upper, lower) {
|
|
895
|
+
return tail.filter((line) => line.index > upper && line.index < lower).length === lower - upper - 1;
|
|
896
|
+
}
|
|
897
|
+
//# sourceMappingURL=promptDetector.js.map
|