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.
Files changed (137) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +31 -0
  3. package/README.md +499 -8
  4. package/dist/cli/desktopShortcut.d.ts +22 -0
  5. package/dist/cli/desktopShortcut.js +157 -0
  6. package/dist/cli/desktopShortcut.js.map +1 -0
  7. package/dist/cli/index.d.ts +13 -0
  8. package/dist/cli/index.js +846 -0
  9. package/dist/cli/index.js.map +1 -0
  10. package/dist/client/http.d.ts +143 -0
  11. package/dist/client/http.js +581 -0
  12. package/dist/client/http.js.map +1 -0
  13. package/dist/core/dialogs.d.ts +96 -0
  14. package/dist/core/dialogs.js +226 -0
  15. package/dist/core/dialogs.js.map +1 -0
  16. package/dist/core/echo.d.ts +71 -0
  17. package/dist/core/echo.js +249 -0
  18. package/dist/core/echo.js.map +1 -0
  19. package/dist/core/find.d.ts +34 -0
  20. package/dist/core/find.js +41 -0
  21. package/dist/core/find.js.map +1 -0
  22. package/dist/core/keys.d.ts +22 -0
  23. package/dist/core/keys.js +80 -0
  24. package/dist/core/keys.js.map +1 -0
  25. package/dist/core/manager.d.ts +96 -0
  26. package/dist/core/manager.js +271 -0
  27. package/dist/core/manager.js.map +1 -0
  28. package/dist/core/nativePty.d.ts +73 -0
  29. package/dist/core/nativePty.js +161 -0
  30. package/dist/core/nativePty.js.map +1 -0
  31. package/dist/core/policy.d.ts +61 -0
  32. package/dist/core/policy.js +143 -0
  33. package/dist/core/policy.js.map +1 -0
  34. package/dist/core/promptDetector.d.ts +109 -0
  35. package/dist/core/promptDetector.js +897 -0
  36. package/dist/core/promptDetector.js.map +1 -0
  37. package/dist/core/reasoning.d.ts +265 -0
  38. package/dist/core/reasoning.js +933 -0
  39. package/dist/core/reasoning.js.map +1 -0
  40. package/dist/core/regexBudget.d.ts +64 -0
  41. package/dist/core/regexBudget.js +103 -0
  42. package/dist/core/regexBudget.js.map +1 -0
  43. package/dist/core/renderer.d.ts +127 -0
  44. package/dist/core/renderer.js +316 -0
  45. package/dist/core/renderer.js.map +1 -0
  46. package/dist/core/selection.d.ts +6 -0
  47. package/dist/core/selection.js +48 -0
  48. package/dist/core/selection.js.map +1 -0
  49. package/dist/core/session.d.ts +565 -0
  50. package/dist/core/session.js +1450 -0
  51. package/dist/core/session.js.map +1 -0
  52. package/dist/core/types.d.ts +316 -0
  53. package/dist/core/types.js +71 -0
  54. package/dist/core/types.js.map +1 -0
  55. package/dist/core/waiter.d.ts +94 -0
  56. package/dist/core/waiter.js +310 -0
  57. package/dist/core/waiter.js.map +1 -0
  58. package/dist/core/windowsConsoleHost.d.ts +147 -0
  59. package/dist/core/windowsConsoleHost.js +588 -0
  60. package/dist/core/windowsConsoleHost.js.map +1 -0
  61. package/dist/core/windowsPtyStart.d.ts +161 -0
  62. package/dist/core/windowsPtyStart.js +236 -0
  63. package/dist/core/windowsPtyStart.js.map +1 -0
  64. package/dist/daemon/audit.d.ts +23 -0
  65. package/dist/daemon/audit.js +51 -0
  66. package/dist/daemon/audit.js.map +1 -0
  67. package/dist/daemon/auth.d.ts +197 -0
  68. package/dist/daemon/auth.js +366 -0
  69. package/dist/daemon/auth.js.map +1 -0
  70. package/dist/daemon/config.d.ts +119 -0
  71. package/dist/daemon/config.js +568 -0
  72. package/dist/daemon/config.js.map +1 -0
  73. package/dist/daemon/folderPicker.d.ts +27 -0
  74. package/dist/daemon/folderPicker.js +115 -0
  75. package/dist/daemon/folderPicker.js.map +1 -0
  76. package/dist/daemon/index.d.ts +7 -0
  77. package/dist/daemon/index.js +88 -0
  78. package/dist/daemon/index.js.map +1 -0
  79. package/dist/daemon/launchProfiles.d.ts +155 -0
  80. package/dist/daemon/launchProfiles.js +554 -0
  81. package/dist/daemon/launchProfiles.js.map +1 -0
  82. package/dist/daemon/liveness.d.ts +36 -0
  83. package/dist/daemon/liveness.js +71 -0
  84. package/dist/daemon/liveness.js.map +1 -0
  85. package/dist/daemon/logger.d.ts +7 -0
  86. package/dist/daemon/logger.js +29 -0
  87. package/dist/daemon/logger.js.map +1 -0
  88. package/dist/daemon/meta.d.ts +9 -0
  89. package/dist/daemon/meta.js +24 -0
  90. package/dist/daemon/meta.js.map +1 -0
  91. package/dist/daemon/paths.d.ts +85 -0
  92. package/dist/daemon/paths.js +154 -0
  93. package/dist/daemon/paths.js.map +1 -0
  94. package/dist/daemon/screenshot.d.ts +144 -0
  95. package/dist/daemon/screenshot.js +402 -0
  96. package/dist/daemon/screenshot.js.map +1 -0
  97. package/dist/daemon/server.d.ts +453 -0
  98. package/dist/daemon/server.js +3954 -0
  99. package/dist/daemon/server.js.map +1 -0
  100. package/dist/daemon/sessionFeed.d.ts +48 -0
  101. package/dist/daemon/sessionFeed.js +101 -0
  102. package/dist/daemon/sessionFeed.js.map +1 -0
  103. package/dist/daemon/terminalReports.d.ts +38 -0
  104. package/dist/daemon/terminalReports.js +84 -0
  105. package/dist/daemon/terminalReports.js.map +1 -0
  106. package/dist/daemon/viewerLauncher.d.ts +226 -0
  107. package/dist/daemon/viewerLauncher.js +562 -0
  108. package/dist/daemon/viewerLauncher.js.map +1 -0
  109. package/dist/daemon/viewerSync.d.ts +38 -0
  110. package/dist/daemon/viewerSync.js +83 -0
  111. package/dist/daemon/viewerSync.js.map +1 -0
  112. package/dist/mcp/config.d.ts +50 -0
  113. package/dist/mcp/config.js +95 -0
  114. package/dist/mcp/config.js.map +1 -0
  115. package/dist/mcp/index.d.ts +10 -0
  116. package/dist/mcp/index.js +78 -0
  117. package/dist/mcp/index.js.map +1 -0
  118. package/dist/mcp/tools.d.ts +66 -0
  119. package/dist/mcp/tools.js +638 -0
  120. package/dist/mcp/tools.js.map +1 -0
  121. package/dist/viewer/dashboard-folders.js +20 -0
  122. package/dist/viewer/dashboard-layout.js +52 -0
  123. package/dist/viewer/dashboard.css +67 -0
  124. package/dist/viewer/dashboard.html +44 -0
  125. package/dist/viewer/dashboard.js +1518 -0
  126. package/dist/viewer/icon-16.png +0 -0
  127. package/dist/viewer/icon-180.png +0 -0
  128. package/dist/viewer/icon-32.png +0 -0
  129. package/dist/viewer/icon-512.png +0 -0
  130. package/dist/viewer/icon.ico +0 -0
  131. package/dist/viewer/icon.svg +66 -0
  132. package/dist/viewer/index.html +61 -0
  133. package/dist/viewer/pane.js +736 -0
  134. package/dist/viewer/viewer.css +95 -0
  135. package/dist/viewer/viewer.js +84 -0
  136. package/package.json +66 -7
  137. package/index.js +0 -4
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Permission dialogs that a TUI draws on its own screen, recognised by a
3
+ * signature rather than by the shapes `promptDetector.ts` reads.
4
+ *
5
+ * Some agent CLIs ask for permission in a dialog drawn inside their own
6
+ * interface: a title and the request, with the choices on ONE row, as buttons.
7
+ * OpenCode draws `△ Permission required` with `Allow once Always allow
8
+ * Reject` under it, and MiMo Code, built from OpenCode, draws the same dialog
9
+ * with `Allow always` in the middle, or with two choices.
10
+ * None of the chooser shapes fits a row of buttons, and a heuristic loose
11
+ * enough to read one would read every row of words two spaces apart.
12
+ *
13
+ * So a dialog is described as data: a pattern for its title and one for its
14
+ * row of choices. A live dialog is drawn at the bottom of the screen, so the
15
+ * row of choices is looked for among the last few lines with anything but
16
+ * the dialog's own border on them, and the title in the lines above it. A
17
+ * framed copy of the dialog near the end of the screen, with no more than
18
+ * `below` lines of other text under it, is still read as one: it is the
19
+ * same characters. The built-in list covers the programs above, and
20
+ * requires the border they draw down the left of the dialog, which a diff, a
21
+ * file listing or a transcript quoting the dialog's words does not
22
+ * reproduce. Config `prompts.dialogs` adds more, so a new program's dialog
23
+ * can be recognised without a release. A recognised dialog is a prompt like
24
+ * any other: the session shows as waiting, `terminal_wait prompt:true`
25
+ * returns it and the stall alert fires.
26
+ *
27
+ * Patterns are matched against one line at a time, so a pattern that does
28
+ * not start with `^` matches anywhere in a line; OpenCode draws a sidebar on
29
+ * the same rows as the dialog, after it. Each signature's search runs under
30
+ * the regular-expression time budget (`regexBudget.ts`): a pattern from
31
+ * config is text a person wrote, and one that runs past the budget is
32
+ * reported and set aside for a minute (`SET_ASIDE_MS`) rather than tried on
33
+ * every scan. It is tried again after that, built-in ones included, as an
34
+ * agent signature is (`reasoning.ts`): an ordinary pattern can run past a
35
+ * wall-clock budget once on a loaded machine.
36
+ */
37
+ import type { DetectedPrompt } from './promptDetector.js';
38
+ /** One program's dialog, as data. The shape of a `prompts.dialogs` entry. */
39
+ export interface DialogSignature {
40
+ /** Which program draws it, for the person reading the list. */
41
+ name: string;
42
+ /** A regular expression for the dialog's title, matched against one line. */
43
+ title: string;
44
+ /**
45
+ * A regular expression for the row of choices, matched against one line
46
+ * below the title. What it matches, less any leading border, split at runs
47
+ * of two or more spaces, is the list of options.
48
+ */
49
+ choices: string;
50
+ /** How many lines above the choices the title may sit. Default 40. */
51
+ within?: number;
52
+ /**
53
+ * How many lines with anything but the dialog's border on them may sit
54
+ * below the choices. Default 4.
55
+ */
56
+ below?: number;
57
+ }
58
+ export declare const DEFAULT_DIALOG_WITHIN = 40;
59
+ export declare const DEFAULT_DIALOG_BELOW = 4;
60
+ /** Built-in signatures, checked before the chooser shapes. */
61
+ export declare const BUILT_IN_DIALOGS: readonly DialogSignature[];
62
+ interface CompiledDialog {
63
+ name: string;
64
+ source: string;
65
+ title: RegExp;
66
+ choices: RegExp;
67
+ within: number;
68
+ below: number;
69
+ /**
70
+ * When the signature last ran past its budget. It is set aside for
71
+ * `SET_ASIDE_MS` after that, then tried again.
72
+ */
73
+ overBudgetAt: number | null;
74
+ /** Whether it is one of `BUILT_IN_DIALOGS`, which no config can change. */
75
+ builtIn: boolean;
76
+ }
77
+ /**
78
+ * Compile signatures, setting aside any that cannot be used and saying why,
79
+ * in words a person can act on. `builtIn` marks the list as `BUILT_IN_DIALOGS`.
80
+ */
81
+ export declare function compileDialogs(list: readonly unknown[], builtIn?: boolean): {
82
+ dialogs: CompiledDialog[];
83
+ problems: string[];
84
+ /** The positions in `list` of the entries that compiled. */
85
+ usable: number[];
86
+ };
87
+ /**
88
+ * Recognise the built-in dialogs plus `extra`, the entries from config, and
89
+ * name the one to tell, with whether the signature is built in, when a
90
+ * signature runs past its budget (the daemon logs it). Returns what was wrong
91
+ * with `extra`; the entries that are fine are used.
92
+ */
93
+ export declare function useDialogSignatures(extra: readonly unknown[], onOverBudget?: (name: string, builtIn: boolean) => void): string[];
94
+ /** A recognised dialog on screen, or null. `now` is the time of the scan. */
95
+ export declare function dialogPrompt(raw: readonly string[], dialogs?: readonly CompiledDialog[], now?: number): DetectedPrompt | null;
96
+ export {};
@@ -0,0 +1,226 @@
1
+ /**
2
+ * Permission dialogs that a TUI draws on its own screen, recognised by a
3
+ * signature rather than by the shapes `promptDetector.ts` reads.
4
+ *
5
+ * Some agent CLIs ask for permission in a dialog drawn inside their own
6
+ * interface: a title and the request, with the choices on ONE row, as buttons.
7
+ * OpenCode draws `△ Permission required` with `Allow once Always allow
8
+ * Reject` under it, and MiMo Code, built from OpenCode, draws the same dialog
9
+ * with `Allow always` in the middle, or with two choices.
10
+ * None of the chooser shapes fits a row of buttons, and a heuristic loose
11
+ * enough to read one would read every row of words two spaces apart.
12
+ *
13
+ * So a dialog is described as data: a pattern for its title and one for its
14
+ * row of choices. A live dialog is drawn at the bottom of the screen, so the
15
+ * row of choices is looked for among the last few lines with anything but
16
+ * the dialog's own border on them, and the title in the lines above it. A
17
+ * framed copy of the dialog near the end of the screen, with no more than
18
+ * `below` lines of other text under it, is still read as one: it is the
19
+ * same characters. The built-in list covers the programs above, and
20
+ * requires the border they draw down the left of the dialog, which a diff, a
21
+ * file listing or a transcript quoting the dialog's words does not
22
+ * reproduce. Config `prompts.dialogs` adds more, so a new program's dialog
23
+ * can be recognised without a release. A recognised dialog is a prompt like
24
+ * any other: the session shows as waiting, `terminal_wait prompt:true`
25
+ * returns it and the stall alert fires.
26
+ *
27
+ * Patterns are matched against one line at a time, so a pattern that does
28
+ * not start with `^` matches anywhere in a line; OpenCode draws a sidebar on
29
+ * the same rows as the dialog, after it. Each signature's search runs under
30
+ * the regular-expression time budget (`regexBudget.ts`): a pattern from
31
+ * config is text a person wrote, and one that runs past the budget is
32
+ * reported and set aside for a minute (`SET_ASIDE_MS`) rather than tried on
33
+ * every scan. It is tried again after that, built-in ones included, as an
34
+ * agent signature is (`reasoning.ts`): an ordinary pattern can run past a
35
+ * wall-clock budget once on a loaded machine.
36
+ */
37
+ import { RegexBudgetError, SET_ASIDE_MS, withRegexBudget } from './regexBudget.js';
38
+ export const DEFAULT_DIALOG_WITHIN = 40;
39
+ export const DEFAULT_DIALOG_BELOW = 4;
40
+ const MAX_DIALOG_WITHIN = 100;
41
+ const MAX_DIALOG_BELOW = 20;
42
+ /** Built-in signatures, checked before the chooser shapes. */
43
+ export const BUILT_IN_DIALOGS = [
44
+ {
45
+ // OpenCode offers three choices, the middle one "Always allow"; MiMo
46
+ // Code, built from it, calls that one "Allow always", and leaves it out
47
+ // when the command deletes files.
48
+ name: 'OpenCode and MiMo Code',
49
+ title: '^ *┃ +△ Permission required',
50
+ choices: '^ *┃ +Allow once {2,}(?:(?:Always allow|Allow always) {2,})?Reject',
51
+ },
52
+ ];
53
+ function wholeNumber(value, max) {
54
+ return typeof value === 'number' && Number.isInteger(value) && value >= 1 && value <= max;
55
+ }
56
+ /**
57
+ * Compile signatures, setting aside any that cannot be used and saying why,
58
+ * in words a person can act on. `builtIn` marks the list as `BUILT_IN_DIALOGS`.
59
+ */
60
+ export function compileDialogs(list, builtIn = false) {
61
+ const dialogs = [];
62
+ const problems = [];
63
+ const usable = [];
64
+ list.forEach((entry, index) => {
65
+ const at = `prompts.dialogs[${String(index)}]`;
66
+ if (typeof entry !== 'object' || entry === null) {
67
+ problems.push(`${at} must be an object with name, title and choices: ignored`);
68
+ return;
69
+ }
70
+ const { name, title, choices, within, below } = entry;
71
+ if (typeof name !== 'string' || name === '') {
72
+ problems.push(`${at}.name must be a non-empty string: ignored`);
73
+ return;
74
+ }
75
+ if (typeof title !== 'string' || title === '' || typeof choices !== 'string' || choices === '') {
76
+ problems.push(`${at} (${name}) needs title and choices as non-empty regular expressions: ignored`);
77
+ return;
78
+ }
79
+ if (within !== undefined && !wholeNumber(within, MAX_DIALOG_WITHIN)) {
80
+ problems.push(`${at} (${name}).within must be a whole number from 1 to ${String(MAX_DIALOG_WITHIN)}: ignored`);
81
+ return;
82
+ }
83
+ if (below !== undefined && !wholeNumber(below, MAX_DIALOG_BELOW) && below !== 0) {
84
+ problems.push(`${at} (${name}).below must be a whole number from 0 to ${String(MAX_DIALOG_BELOW)}: ignored`);
85
+ return;
86
+ }
87
+ try {
88
+ dialogs.push({
89
+ name,
90
+ source: `${title} / ${choices}`,
91
+ title: new RegExp(title, 'u'),
92
+ choices: new RegExp(choices, 'u'),
93
+ within: within ?? DEFAULT_DIALOG_WITHIN,
94
+ below: below ?? DEFAULT_DIALOG_BELOW,
95
+ overBudgetAt: null,
96
+ builtIn,
97
+ });
98
+ usable.push(index);
99
+ }
100
+ catch (error) {
101
+ problems.push(`${at} (${name}) has a pattern that does not compile (${String(error)}): ignored`);
102
+ }
103
+ });
104
+ return { dialogs, problems, usable };
105
+ }
106
+ let active = compileDialogs(BUILT_IN_DIALOGS, true).dialogs;
107
+ let reportOverBudget = () => undefined;
108
+ /**
109
+ * Recognise the built-in dialogs plus `extra`, the entries from config, and
110
+ * name the one to tell, with whether the signature is built in, when a
111
+ * signature runs past its budget (the daemon logs it). Returns what was wrong
112
+ * with `extra`; the entries that are fine are used.
113
+ */
114
+ export function useDialogSignatures(extra, onOverBudget = () => undefined) {
115
+ const builtIn = compileDialogs(BUILT_IN_DIALOGS, true).dialogs;
116
+ const { dialogs, problems } = compileDialogs(extra);
117
+ active = [...builtIn, ...dialogs];
118
+ reportOverBudget = onOverBudget;
119
+ return problems;
120
+ }
121
+ function normalise(text) {
122
+ return text.replace(/\s+/g, ' ').trim();
123
+ }
124
+ /** Leading whitespace and box-drawing borders (U+2500 to U+257F). */
125
+ const LEADING_BORDER = /^[\s─-╿]+/u;
126
+ /** A recognised dialog on screen, or null. `now` is the time of the scan. */
127
+ export function dialogPrompt(raw, dialogs = active, now = Date.now()) {
128
+ for (const dialog of dialogs) {
129
+ if (dialog.overBudgetAt !== null && now - dialog.overBudgetAt < SET_ASIDE_MS)
130
+ continue;
131
+ let found = null;
132
+ try {
133
+ found = withRegexBudget(dialog.source, () => matchDialog(raw, dialog));
134
+ }
135
+ catch (error) {
136
+ if (!(error instanceof RegexBudgetError))
137
+ throw error;
138
+ // Set aside rather than tried on every scan, and reported each time it
139
+ // trips: a person reading daemon.log sees whether a signature keeps
140
+ // running long or tripped once under load.
141
+ dialog.overBudgetAt = now;
142
+ reportOverBudget(dialog.name, dialog.builtIn);
143
+ }
144
+ if (found !== null)
145
+ return found;
146
+ }
147
+ return null;
148
+ }
149
+ /** Text that is not whitespace or box drawing. */
150
+ const TEXT = /[^\s─-╿]/u;
151
+ /** How many rows of bare border, at most, may sit under the choices besides `below`. */
152
+ const MAX_BORDER_ROWS = 10;
153
+ function matchDialog(raw, dialog) {
154
+ const content = [];
155
+ raw.forEach((line, index) => {
156
+ if (line.trim() !== '')
157
+ content.push(index);
158
+ });
159
+ // The row of choices of a live dialog is near the bottom: at most `below`
160
+ // lines with content under it. A row that holds nothing but the dialog's
161
+ // own border is not counted: a narrow screen can put a wrapped key hint, a
162
+ // status line and a bare border row under the buttons. A row of choices
163
+ // with no title above it in reach is not a dialog, and the next one up is
164
+ // tried.
165
+ for (let i = content.length - 1; i >= Math.max(0, content.length - dialog.below - 1 - MAX_BORDER_ROWS); i--) {
166
+ const c = content[i];
167
+ const bottom = dialog.choices.exec(raw[c] ?? '');
168
+ if (bottom === null)
169
+ continue;
170
+ const lead = (raw[c] ?? '').slice(0, bottom.index + Math.max(0, bottom[0].search(TEXT)));
171
+ const border = new Set(lead.replace(/[^─-╿]/gu, ''));
172
+ const under = content
173
+ .slice(i + 1)
174
+ .filter((u) => [...(raw[u] ?? '').replace(/\s/gu, '')].some((ch) => !border.has(ch)));
175
+ if (under.length > dialog.below)
176
+ continue;
177
+ // The nearest title above: a copy of the title further down the screen
178
+ // is not this dialog's.
179
+ for (let t = c - 1; t >= Math.max(0, c - dialog.within); t--) {
180
+ const titleLine = raw[t] ?? '';
181
+ const title = dialog.title.exec(titleLine);
182
+ if (title === null)
183
+ continue;
184
+ const titleText = title[0].replace(LEADING_BORDER, '');
185
+ const question = normalise(titleText.replace(/^[^\p{L}\p{N}]+/u, ''));
186
+ const options = bottom[0].replace(LEADING_BORDER, '').trim().split(/\s{2,}/);
187
+ // The border the dialog draws down its left: the box drawing in front
188
+ // of the title's text.
189
+ const frame = [...new Set(titleLine.slice(0, title.index + title[0].length - titleText.length))].filter((ch) => /[─-╿]/u.test(ch));
190
+ const request = requestText(raw.slice(t + 1, c), frame);
191
+ return {
192
+ question,
193
+ options,
194
+ signature: [dialog.name, question, request, ...options].join(' | '),
195
+ // The title down to the row of choices, the request between them included.
196
+ rows: [t, c],
197
+ basis: 'frame',
198
+ };
199
+ }
200
+ }
201
+ return null;
202
+ }
203
+ /**
204
+ * What the dialog asks about: the text of every row between its title and
205
+ * its choices, each without the dialog's own border in front of it (`frame`,
206
+ * the box drawing in front of the title), with each run of whitespace as one
207
+ * space. Other box drawing is text: a `─` that a wrap leaves at the start of
208
+ * a row is part of the request. It keeps two requests apart that draw the
209
+ * same title and buttons, so answering one does not mark the next as already
210
+ * answered.
211
+ *
212
+ * Nothing between the title and the choices is left out, a sidebar drawn on
213
+ * the same rows included. Leaving text out is what can make two different
214
+ * requests one signature, and then the second is taken as already answered
215
+ * and a wait for a prompt never returns it. The cost runs the other way and
216
+ * is milder: a resize, or a change in text drawn beside the dialog, makes the
217
+ * same dialog a new signature, so the stall alert's clock starts again, and
218
+ * a dialog just answered may be offered once more if that happens before it
219
+ * closes. A request the program wraps at a word boundary keeps its signature
220
+ * at any width when nothing is drawn beside it.
221
+ */
222
+ function requestText(rows, frame) {
223
+ const border = new RegExp(`^[\\s${frame.join('')}]+`, 'u');
224
+ return normalise(rows.map((line) => line.replace(border, '')).join(' '));
225
+ }
226
+ //# sourceMappingURL=dialogs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dialogs.js","sourceRoot":"","sources":["../../src/core/dialogs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAwBnF,MAAM,CAAC,MAAM,qBAAqB,GAAG,EAAE,CAAC;AACxC,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,CAAC;AACtC,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAC9B,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAE5B,8DAA8D;AAC9D,MAAM,CAAC,MAAM,gBAAgB,GAA+B;IAC1D;QACE,qEAAqE;QACrE,wEAAwE;QACxE,kCAAkC;QAClC,IAAI,EAAE,wBAAwB;QAC9B,KAAK,EAAE,6BAA6B;QACpC,OAAO,EAAE,oEAAoE;KAC9E;CACF,CAAC;AAkBF,SAAS,WAAW,CAAC,KAAc,EAAE,GAAW;IAC9C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,GAAG,CAAC;AAC5F,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,IAAwB,EAAE,OAAO,GAAG,KAAK;IAMtE,MAAM,OAAO,GAAqB,EAAE,CAAC;IACrC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,IAAI,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;QAC5B,MAAM,EAAE,GAAG,mBAAmB,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC;QAC/C,IAAI,OAAO,KAAK,KAAK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AAC,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC;gBACnE,2EAA2E;gBAC3E,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;gBACZ,KAAK,EAAE,OAAO;aACf,CAAC;QACJ,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAS,WAAW,CAAC,IAAuB,EAAE,KAAwB;IACpE,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,QAAQ,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC3D,OAAO,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAC3E,CAAC"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Locate the echo of typed input on a rendered screen.
3
+ *
4
+ * `echoEnd` is the guard behind `matchAfterInput`; `screenShows` is the
5
+ * check behind `terminal_type`'s verify.
6
+ *
7
+ * A text or regex wait that describes
8
+ * the program's expected OUTPUT matches the echo of the caller's own input
9
+ * first, within a millisecond, because the natural thing to wait for is
10
+ * exactly what you asked the program to produce (`PY_FILES\s*=` matches a
11
+ * submitted prompt that mentions it). Ignoring matches at or before the echo
12
+ * removes the class instead of restating the warning in prose.
13
+ *
14
+ * Pure function; no I/O.
15
+ */
16
+ /**
17
+ * Offset in `screen` just past the LAST occurrence of the echo of `input`,
18
+ * or null when no echo can be placed: control-only input (a bare Enter, an
19
+ * arrow key), a printable head too short to be distinctive or a screen
20
+ * that does not show it (hidden input, scrolled away, re-rendered beyond
21
+ * recognition).
22
+ *
23
+ * Whitespace and box-drawing characters are ignored on both sides because
24
+ * TUIs re-wrap a submitted prompt across rows with their own indentation
25
+ * and border (`│ > the prompt…` / `│ continued`), the same reason
26
+ * `screenShows` ignores them. The LAST occurrence is the
27
+ * echo because it is the most recent rendering of that text: a transcript
28
+ * shows the submitted prompt once, and anything printed before it is
29
+ * history.
30
+ */
31
+ export declare function echoEnd(screen: string, input: string): number | null;
32
+ /**
33
+ * Whether `text` is on `screen`, read the way a TUI draws typed text: wrapped
34
+ * at the width of its input box. Whitespace is ignored on both sides, so a
35
+ * line break or a continuation indent inside the text does not hide it.
36
+ *
37
+ * An input box may also draw a border down its edges (OpenCode's `┃` on the
38
+ * left, Grok's `│` on the right) and a side panel on the same rows (OpenCode
39
+ * on a wide screen), and both land between the wrapped pieces. So the rows are
40
+ * also read one at a time. A row's part of the text ends:
41
+ * - at the end of the row, or before a border that closes it;
42
+ * - or before a gap of at least two spaces that leads to more text: a panel
43
+ * set apart by space. A single space, or box drawing after the gap, is
44
+ * part of what the row shows (a table's columns, two panes), not the end of
45
+ * the box.
46
+ *
47
+ * The text then carries on at the start of the next row, after its border and
48
+ * an indent of at most MAX_INDENT spaces. Box drawing anywhere else is text:
49
+ * typed box drawing must be on screen.
50
+ *
51
+ * A gap of two spaces cannot tell a side panel from a table's next column, so
52
+ * what was already on screen must not make up the typed text's wrapped
53
+ * pieces: `before` is the screen as it was just before the text was typed,
54
+ * and each row's piece must be new where it stands: at least one of its
55
+ * characters is new there, or text that stood just after it is gone (typed
56
+ * text replacing a placeholder that began with the same word). Typed text
57
+ * does one or the other where it lands; a table whose clock ticks, an earlier
58
+ * message beside a changing panel or a panel's labels do neither. With no
59
+ * `before`, everything counts as new. What this cannot tell
60
+ * apart is new output elsewhere that happens to hold the same words at the
61
+ * ends of its rows, such as old lines scrolling up. The plain check above,
62
+ * whitespace ignored across the whole screen, reads every row, as it always
63
+ * has.
64
+ *
65
+ * Reading the rows costs a few characters per place the text could start.
66
+ * A screen built against the text (its first character everywhere, with gaps)
67
+ * could make it far more, and the daemon reads the screen on its event loop,
68
+ * so the reading stops after MAX_STEPS characters and says the text is not
69
+ * there.
70
+ */
71
+ export declare function screenShows(screen: string, text: string, before?: string): boolean;
@@ -0,0 +1,249 @@
1
+ /**
2
+ * Locate the echo of typed input on a rendered screen.
3
+ *
4
+ * `echoEnd` is the guard behind `matchAfterInput`; `screenShows` is the
5
+ * check behind `terminal_type`'s verify.
6
+ *
7
+ * A text or regex wait that describes
8
+ * the program's expected OUTPUT matches the echo of the caller's own input
9
+ * first, within a millisecond, because the natural thing to wait for is
10
+ * exactly what you asked the program to produce (`PY_FILES\s*=` matches a
11
+ * submitted prompt that mentions it). Ignoring matches at or before the echo
12
+ * removes the class instead of restating the warning in prose.
13
+ *
14
+ * Pure function; no I/O.
15
+ */
16
+ /** How much of the input's printable head to look for. */
17
+ const NEEDLE_MAX_CHARS = 60;
18
+ /** Shorter than this is not distinctive enough to place on a screen. */
19
+ const NEEDLE_MIN_CHARS = 3;
20
+ /**
21
+ * Offset in `screen` just past the LAST occurrence of the echo of `input`,
22
+ * or null when no echo can be placed: control-only input (a bare Enter, an
23
+ * arrow key), a printable head too short to be distinctive or a screen
24
+ * that does not show it (hidden input, scrolled away, re-rendered beyond
25
+ * recognition).
26
+ *
27
+ * Whitespace and box-drawing characters are ignored on both sides because
28
+ * TUIs re-wrap a submitted prompt across rows with their own indentation
29
+ * and border (`│ > the prompt…` / `│ continued`), the same reason
30
+ * `screenShows` ignores them. The LAST occurrence is the
31
+ * echo because it is the most recent rendering of that text: a transcript
32
+ * shows the submitted prompt once, and anything printed before it is
33
+ * history.
34
+ */
35
+ export function echoEnd(screen, input) {
36
+ const needle = squash(printableHead(input)).slice(0, NEEDLE_MAX_CHARS);
37
+ if (needle.length < NEEDLE_MIN_CHARS)
38
+ return null;
39
+ // Squash the screen the same way, keeping a map back to original offsets.
40
+ const kept = [];
41
+ let squashed = '';
42
+ for (let i = 0; i < screen.length; i++) {
43
+ const ch = screen[i] ?? '';
44
+ if (CHROME.test(ch))
45
+ continue;
46
+ squashed += ch;
47
+ kept.push(i);
48
+ }
49
+ const at = squashed.lastIndexOf(needle);
50
+ if (at === -1)
51
+ return null;
52
+ return (kept[at + needle.length - 1] ?? 0) + 1;
53
+ }
54
+ /**
55
+ * Whether `text` is on `screen`, read the way a TUI draws typed text: wrapped
56
+ * at the width of its input box. Whitespace is ignored on both sides, so a
57
+ * line break or a continuation indent inside the text does not hide it.
58
+ *
59
+ * An input box may also draw a border down its edges (OpenCode's `┃` on the
60
+ * left, Grok's `│` on the right) and a side panel on the same rows (OpenCode
61
+ * on a wide screen), and both land between the wrapped pieces. So the rows are
62
+ * also read one at a time. A row's part of the text ends:
63
+ * - at the end of the row, or before a border that closes it;
64
+ * - or before a gap of at least two spaces that leads to more text: a panel
65
+ * set apart by space. A single space, or box drawing after the gap, is
66
+ * part of what the row shows (a table's columns, two panes), not the end of
67
+ * the box.
68
+ *
69
+ * The text then carries on at the start of the next row, after its border and
70
+ * an indent of at most MAX_INDENT spaces. Box drawing anywhere else is text:
71
+ * typed box drawing must be on screen.
72
+ *
73
+ * A gap of two spaces cannot tell a side panel from a table's next column, so
74
+ * what was already on screen must not make up the typed text's wrapped
75
+ * pieces: `before` is the screen as it was just before the text was typed,
76
+ * and each row's piece must be new where it stands: at least one of its
77
+ * characters is new there, or text that stood just after it is gone (typed
78
+ * text replacing a placeholder that began with the same word). Typed text
79
+ * does one or the other where it lands; a table whose clock ticks, an earlier
80
+ * message beside a changing panel or a panel's labels do neither. With no
81
+ * `before`, everything counts as new. What this cannot tell
82
+ * apart is new output elsewhere that happens to hold the same words at the
83
+ * ends of its rows, such as old lines scrolling up. The plain check above,
84
+ * whitespace ignored across the whole screen, reads every row, as it always
85
+ * has.
86
+ *
87
+ * Reading the rows costs a few characters per place the text could start.
88
+ * A screen built against the text (its first character everywhere, with gaps)
89
+ * could make it far more, and the daemon reads the screen on its event loop,
90
+ * so the reading stops after MAX_STEPS characters and says the text is not
91
+ * there.
92
+ */
93
+ export function screenShows(screen, text, before = '') {
94
+ const needle = text.replace(/\s+/g, '');
95
+ if (needle === '')
96
+ return true;
97
+ if (screen.replace(/\s+/g, '').includes(needle))
98
+ return true;
99
+ // Every character the text needs must be on screen somewhere.
100
+ for (const ch of new Set(needle))
101
+ if (!screen.includes(ch))
102
+ return false;
103
+ const lines = screen.split('\n');
104
+ const wrapped = {
105
+ lines,
106
+ earlier: before.split('\n'),
107
+ rows: new Map(),
108
+ needle,
109
+ failed: new Uint8Array((lines.length + 1) * (needle.length + 1)),
110
+ steps: 0,
111
+ };
112
+ const first = needle.charAt(0);
113
+ for (let row = 0; row < lines.length; row++) {
114
+ const line = lines[row] ?? '';
115
+ if (line === wrapped.earlier[row])
116
+ continue;
117
+ for (let col = line.indexOf(first); col !== -1; col = line.indexOf(first, col + 1)) {
118
+ if (wrappedMatch(wrapped, row, col, 0))
119
+ return true;
120
+ if (wrapped.steps > MAX_STEPS)
121
+ return false;
122
+ }
123
+ }
124
+ return false;
125
+ }
126
+ /** The widest indent after a continuation row's border. */
127
+ const MAX_INDENT = 4;
128
+ /** How many characters `screenShows` reads row by row before it gives up. */
129
+ const MAX_STEPS = 1_000_000;
130
+ function layout(wrapped, row) {
131
+ const known = wrapped.rows.get(row);
132
+ if (known !== undefined)
133
+ return known;
134
+ const line = wrapped.lines[row];
135
+ if (line === undefined || line === wrapped.earlier[row])
136
+ return null;
137
+ const space = new Uint8Array(line.length);
138
+ const box = new Uint8Array(line.length);
139
+ for (let i = 0; i < line.length; i++) {
140
+ const ch = line.charAt(i);
141
+ if (/\s/.test(ch))
142
+ space[i] = 1;
143
+ else if (BOX.test(ch))
144
+ box[i] = 1;
145
+ }
146
+ let start = 0;
147
+ let indent = 0;
148
+ for (; start < line.length && (space[start] === 1 || box[start] === 1); start++) {
149
+ indent = box[start] === 1 ? 0 : indent + 1;
150
+ }
151
+ let end = line.length;
152
+ while (end > start && (space[end - 1] === 1 || box[end - 1] === 1))
153
+ end--;
154
+ const found = { line, space, start, indent, end };
155
+ wrapped.rows.set(row, found);
156
+ return found;
157
+ }
158
+ /**
159
+ * Match the rest of the needle, from `matched` on, starting at
160
+ * `rows[row][col]`. Where the row's part of the text may end (see
161
+ * `screenShows`), both readings are tried: carry on along the row, or carry
162
+ * on at the start of the next one. A row the match carries on to must add to
163
+ * it, starting with its first character, and each row's piece must be new
164
+ * where it stands (see `screenShows`).
165
+ */
166
+ function wrappedMatch(wrapped, row, col, matched) {
167
+ const found = layout(wrapped, row);
168
+ if (found === null)
169
+ return false;
170
+ const { line, space, end } = found;
171
+ const { needle } = wrapped;
172
+ const earlier = wrapped.earlier[row] ?? '';
173
+ let added = false;
174
+ // Whether a character of this row's piece is new where it stands.
175
+ let fresh = false;
176
+ let pieceEnd = col;
177
+ let gap = 0;
178
+ // A piece whose characters all stood there before is still new when text
179
+ // that stood just after it, up to `to`, is gone: typed text replacing a
180
+ // placeholder that began with the same word.
181
+ const isNew = (to) => fresh || vacated(line, space, earlier, pieceEnd, to);
182
+ for (let c = col; c < end; c++) {
183
+ if (++wrapped.steps > MAX_STEPS)
184
+ return false;
185
+ if (space[c] === 1) {
186
+ gap++;
187
+ continue;
188
+ }
189
+ const ch = line.charAt(c);
190
+ if (gap >= 2 && added && !BOX.test(ch) && isNew(c) && continuesBelow(wrapped, row, matched))
191
+ return true;
192
+ if (ch !== needle.charAt(matched))
193
+ return false;
194
+ matched++;
195
+ added = true;
196
+ if (earlier.charAt(c) !== ch)
197
+ fresh = true;
198
+ pieceEnd = c + 1;
199
+ gap = 0;
200
+ if (matched === needle.length) {
201
+ let next = pieceEnd;
202
+ while (next < line.length && space[next] === 1)
203
+ next++;
204
+ return isNew(next < line.length ? next : Math.max(line.length, earlier.length));
205
+ }
206
+ }
207
+ return added && isNew(Math.max(line.length, earlier.length)) && continuesBelow(wrapped, row, matched);
208
+ }
209
+ /** Whether `earlier` had text at a position from `from` to `to` that is blank in `line` now. */
210
+ function vacated(line, space, earlier, from, to) {
211
+ for (let i = from; i < to; i++) {
212
+ const was = earlier.charAt(i);
213
+ if (was === '' || /\s/.test(was))
214
+ continue;
215
+ if (i >= line.length || space[i] === 1)
216
+ return true;
217
+ }
218
+ return false;
219
+ }
220
+ /** Whether the needle carries on from `matched` at the start of the row below. */
221
+ function continuesBelow(wrapped, row, matched) {
222
+ const key = (row + 1) * (wrapped.needle.length + 1) + matched;
223
+ if (wrapped.failed[key] === 1)
224
+ return false;
225
+ const below = layout(wrapped, row + 1);
226
+ if (below !== null && below.indent <= MAX_INDENT && wrappedMatch(wrapped, row + 1, below.start, matched)) {
227
+ return true;
228
+ }
229
+ wrapped.failed[key] = 1;
230
+ return false;
231
+ }
232
+ /** The first run of printable text in the input, escape sequences removed. */
233
+ function printableHead(input) {
234
+ const stripped = input.replace(
235
+ // CSI (arrows, function keys, modes), SS3 and any other two-byte escape.
236
+ // eslint-disable-next-line no-control-regex
237
+ /\x1b(\[[0-9;?<>=]*[@-~]|O[A-Za-z]|.)/g, '');
238
+ // eslint-disable-next-line no-control-regex
239
+ const runs = stripped.split(/[\x00-\x1f\x7f]+/).filter((run) => run.trim() !== '');
240
+ return runs[0] ?? '';
241
+ }
242
+ /** Whitespace and the box-drawing block (U+2500 to U+257F): layout, not text. */
243
+ const CHROME = /[\s─-╿]/;
244
+ /** The box-drawing block alone. */
245
+ const BOX = /[─-╿]/;
246
+ function squash(value) {
247
+ return value.replace(/[\s─-╿]+/g, '');
248
+ }
249
+ //# sourceMappingURL=echo.js.map