telecoder 0.0.0-stage → 1.3.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 (235) hide show
  1. package/.env.example +375 -0
  2. package/CHANGELOG.md +1867 -0
  3. package/LICENSE +22 -0
  4. package/README.md +130 -2
  5. package/dist/audio/transcribe.js +41 -0
  6. package/dist/bin/mcp-server.js +363 -0
  7. package/dist/bin/tool-result.js +22 -0
  8. package/dist/bot/access/group-access.js +147 -0
  9. package/dist/bot/bot.js +573 -0
  10. package/dist/bot/command-catalog.js +263 -0
  11. package/dist/bot/deleted-threads.js +103 -0
  12. package/dist/bot/file-access.js +150 -0
  13. package/dist/bot/handlers/album.handler.js +128 -0
  14. package/dist/bot/handlers/branch.handler.js +1096 -0
  15. package/dist/bot/handlers/command/access.js +407 -0
  16. package/dist/bot/handlers/command/admin.js +803 -0
  17. package/dist/bot/handlers/command/all-view.js +33 -0
  18. package/dist/bot/handlers/command/auto-approve.js +138 -0
  19. package/dist/bot/handlers/command/background.js +37 -0
  20. package/dist/bot/handlers/command/botname.js +76 -0
  21. package/dist/bot/handlers/command/btw.js +175 -0
  22. package/dist/bot/handlers/command/cleanup.js +329 -0
  23. package/dist/bot/handlers/command/model.js +226 -0
  24. package/dist/bot/handlers/command/prefs-scope.js +362 -0
  25. package/dist/bot/handlers/command/project.js +458 -0
  26. package/dist/bot/handlers/command/resume.js +325 -0
  27. package/dist/bot/handlers/command/session.js +299 -0
  28. package/dist/bot/handlers/command/settings.js +657 -0
  29. package/dist/bot/handlers/command/shared.js +207 -0
  30. package/dist/bot/handlers/command/tasks.js +681 -0
  31. package/dist/bot/handlers/command/thread.js +128 -0
  32. package/dist/bot/handlers/command/topic-store.js +74 -0
  33. package/dist/bot/handlers/command/topic.js +27 -0
  34. package/dist/bot/handlers/command/usage.js +382 -0
  35. package/dist/bot/handlers/command-fallback.js +179 -0
  36. package/dist/bot/handlers/command.handler.js +446 -0
  37. package/dist/bot/handlers/loop.handler.js +389 -0
  38. package/dist/bot/handlers/media-fallback.js +126 -0
  39. package/dist/bot/handlers/message/replies.js +56 -0
  40. package/dist/bot/handlers/message/shared.js +95 -0
  41. package/dist/bot/handlers/message/turn-notify.js +135 -0
  42. package/dist/bot/handlers/message/turn-runner.js +56 -0
  43. package/dist/bot/handlers/message.handler.js +114 -0
  44. package/dist/bot/handlers/newbot.handler.js +230 -0
  45. package/dist/bot/handlers/photo.handler.js +286 -0
  46. package/dist/bot/handlers/prompts.handler.js +343 -0
  47. package/dist/bot/handlers/running-note.js +44 -0
  48. package/dist/bot/handlers/session-guard.js +78 -0
  49. package/dist/bot/handlers/suggestion.handler.js +84 -0
  50. package/dist/bot/handlers/tree.handler.js +480 -0
  51. package/dist/bot/handlers/view.js +41 -0
  52. package/dist/bot/handlers/voice.handler.js +147 -0
  53. package/dist/bot/kept-threads.js +51 -0
  54. package/dist/bot/middleware/admin-only.js +49 -0
  55. package/dist/bot/middleware/album-collector.js +69 -0
  56. package/dist/bot/middleware/auth.middleware.js +122 -0
  57. package/dist/bot/middleware/group-mention.middleware.js +145 -0
  58. package/dist/bot/middleware/group-role.middleware.js +75 -0
  59. package/dist/bot/middleware/message-batcher.js +87 -0
  60. package/dist/bot/middleware/queued-messages.js +1082 -0
  61. package/dist/bot/middleware/stale-filter.js +9 -0
  62. package/dist/build-info.json +8 -0
  63. package/dist/claude/ask-user-form.js +517 -0
  64. package/dist/claude/ask-user.js +363 -0
  65. package/dist/claude/auto-approve.js +225 -0
  66. package/dist/claude/auto-topic-haiku.js +143 -0
  67. package/dist/claude/available-commands.js +156 -0
  68. package/dist/claude/background-hint.js +91 -0
  69. package/dist/claude/branch-tree.js +393 -0
  70. package/dist/claude/carry-over.js +25 -0
  71. package/dist/claude/ccr-shim.js +48 -0
  72. package/dist/claude/charter-judge.js +214 -0
  73. package/dist/claude/claude-auth.js +117 -0
  74. package/dist/claude/command-parser.js +206 -0
  75. package/dist/claude/cost-overlay-scraper.js +134 -0
  76. package/dist/claude/effort-level.js +5 -0
  77. package/dist/claude/enabled-plugins.js +52 -0
  78. package/dist/claude/in-flight-tracker.js +82 -0
  79. package/dist/claude/ipc-server.js +192 -0
  80. package/dist/claude/mcp-bridge.js +481 -0
  81. package/dist/claude/memory-guard.js +400 -0
  82. package/dist/claude/message-offsets.js +88 -0
  83. package/dist/claude/modal-relay.js +817 -0
  84. package/dist/claude/model-catalog.js +102 -0
  85. package/dist/claude/monitor-relay.js +712 -0
  86. package/dist/claude/native-commands.js +40 -0
  87. package/dist/claude/pending-branches.js +93 -0
  88. package/dist/claude/pending-suggestions.js +50 -0
  89. package/dist/claude/permission-gate.js +338 -0
  90. package/dist/claude/permission-mode.js +152 -0
  91. package/dist/claude/poll-user.js +51 -0
  92. package/dist/claude/prewarm.js +70 -0
  93. package/dist/claude/project-commands.js +69 -0
  94. package/dist/claude/prompt-hold.js +124 -0
  95. package/dist/claude/prompt-suggestion-scraper.js +89 -0
  96. package/dist/claude/pty-idle-reaper.js +96 -0
  97. package/dist/claude/pty-provider.js +2499 -0
  98. package/dist/claude/pty-spawn-config.js +343 -0
  99. package/dist/claude/pty-stream.js +363 -0
  100. package/dist/claude/pty-watch.js +201 -0
  101. package/dist/claude/request-queue.js +107 -0
  102. package/dist/claude/rewind.js +111 -0
  103. package/dist/claude/scheduled-runner.js +89 -0
  104. package/dist/claude/scheduler.js +241 -0
  105. package/dist/claude/scope-guard.js +279 -0
  106. package/dist/claude/screen-marks.js +83 -0
  107. package/dist/claude/screen-render.js +96 -0
  108. package/dist/claude/secret-request.js +576 -0
  109. package/dist/claude/session-history.js +288 -0
  110. package/dist/claude/session-jsonl.js +1067 -0
  111. package/dist/claude/session-manager.js +216 -0
  112. package/dist/claude/session-move.js +70 -0
  113. package/dist/claude/session-scan.js +270 -0
  114. package/dist/claude/setup-token.js +285 -0
  115. package/dist/claude/shell-mode-gate.js +60 -0
  116. package/dist/claude/side-call-options.js +45 -0
  117. package/dist/claude/side-question.js +252 -0
  118. package/dist/claude/stage-images.js +62 -0
  119. package/dist/claude/subagent-tools.js +12 -0
  120. package/dist/claude/synthetic-ctx.js +51 -0
  121. package/dist/claude/tasks-overlay-scraper.js +217 -0
  122. package/dist/claude/thread-judge-haiku.js +88 -0
  123. package/dist/claude/tip-scraper.js +77 -0
  124. package/dist/claude/tui-modal.js +746 -0
  125. package/dist/claude/tui-state.js +456 -0
  126. package/dist/claude/turn-end.js +105 -0
  127. package/dist/claude/turn-prose.js +64 -0
  128. package/dist/claude/typed-answer.js +104 -0
  129. package/dist/claude/update-banner-relay.js +70 -0
  130. package/dist/cli/commands/doctor.js +338 -0
  131. package/dist/cli/commands/help.js +34 -0
  132. package/dist/cli/commands/init.js +126 -0
  133. package/dist/cli/commands/service.js +710 -0
  134. package/dist/cli/commands/start.js +63 -0
  135. package/dist/cli/commands/version.js +11 -0
  136. package/dist/cli/commands/watch.js +322 -0
  137. package/dist/cli/dispatch.js +84 -0
  138. package/dist/cli/init/env-file.js +99 -0
  139. package/dist/cli/init/flow.js +705 -0
  140. package/dist/cli/init/prompter.js +124 -0
  141. package/dist/cli/init/telegram.js +120 -0
  142. package/dist/cli/service/render.js +212 -0
  143. package/dist/cli.js +18 -0
  144. package/dist/config-report.js +107 -0
  145. package/dist/config.js +525 -0
  146. package/dist/index.js +443 -0
  147. package/dist/launcher.js +960 -0
  148. package/dist/node-version-check.js +27 -0
  149. package/dist/providers/ccr-health.js +119 -0
  150. package/dist/providers/ccr-introspect.js +142 -0
  151. package/dist/providers/ccr-provider.js +59 -0
  152. package/dist/providers/claude-provider.js +10 -0
  153. package/dist/providers/prefs-sync.js +121 -0
  154. package/dist/providers/project-favorites.js +74 -0
  155. package/dist/providers/provider-router.js +128 -0
  156. package/dist/providers/provider-switch.js +91 -0
  157. package/dist/providers/spawn-settings.js +45 -0
  158. package/dist/providers/types.js +8 -0
  159. package/dist/providers/user-preferences.js +322 -0
  160. package/dist/restart-when-idle.js +173 -0
  161. package/dist/startup/auto-continue.js +67 -0
  162. package/dist/startup/auto-resume.js +161 -0
  163. package/dist/startup/notices.js +120 -0
  164. package/dist/startup/shared.js +44 -0
  165. package/dist/startup/verify-token.js +31 -0
  166. package/dist/telegram/access-request.js +153 -0
  167. package/dist/telegram/action-logger.js +522 -0
  168. package/dist/telegram/admin-mention.js +107 -0
  169. package/dist/telegram/background-activity-card.js +502 -0
  170. package/dist/telegram/botname-settings.js +282 -0
  171. package/dist/telegram/button-text.js +26 -0
  172. package/dist/telegram/client-options.js +26 -0
  173. package/dist/telegram/deduplication.js +33 -0
  174. package/dist/telegram/entities.js +295 -0
  175. package/dist/telegram/force-reply-tracker.js +96 -0
  176. package/dist/telegram/lettered-options.js +87 -0
  177. package/dist/telegram/markdown.js +362 -0
  178. package/dist/telegram/message-sender.js +1019 -0
  179. package/dist/telegram/prompt-label.js +148 -0
  180. package/dist/telegram/prompt-suggestion.js +54 -0
  181. package/dist/telegram/send.js +123 -0
  182. package/dist/telegram/streaming-turn.js +103 -0
  183. package/dist/telegram/suggestions-settings.js +30 -0
  184. package/dist/telegram/table-lists.js +56 -0
  185. package/dist/telegram/task-list.js +367 -0
  186. package/dist/telegram/task-tracker.js +128 -0
  187. package/dist/telegram/terminal-renderer.js +667 -0
  188. package/dist/telegram/thread-titles.js +262 -0
  189. package/dist/telegram/threads-setup.js +117 -0
  190. package/dist/utils/admins.js +143 -0
  191. package/dist/utils/agent-timer.js +55 -0
  192. package/dist/utils/atomic-write.js +24 -0
  193. package/dist/utils/bot-icon.js +248 -0
  194. package/dist/utils/bot-token.js +77 -0
  195. package/dist/utils/bounded-map.js +55 -0
  196. package/dist/utils/build-info.js +57 -0
  197. package/dist/utils/caffeinate.js +53 -0
  198. package/dist/utils/claude-env.js +53 -0
  199. package/dist/utils/claude-version.js +48 -0
  200. package/dist/utils/config-home.js +62 -0
  201. package/dist/utils/download.js +137 -0
  202. package/dist/utils/exit-codes.js +59 -0
  203. package/dist/utils/file-type.js +149 -0
  204. package/dist/utils/format.js +78 -0
  205. package/dist/utils/home-project.js +30 -0
  206. package/dist/utils/host-stall.js +51 -0
  207. package/dist/utils/id-list.js +44 -0
  208. package/dist/utils/idle-restart.js +92 -0
  209. package/dist/utils/install-mode.js +63 -0
  210. package/dist/utils/instance-config.js +148 -0
  211. package/dist/utils/instances.js +95 -0
  212. package/dist/utils/json-store.js +140 -0
  213. package/dist/utils/keyed-settings.js +62 -0
  214. package/dist/utils/legacy-env.js +35 -0
  215. package/dist/utils/memory-pressure.js +60 -0
  216. package/dist/utils/npm-version.js +134 -0
  217. package/dist/utils/prefs-relay.js +107 -0
  218. package/dist/utils/proc-children.js +102 -0
  219. package/dist/utils/resolve-bin.js +86 -0
  220. package/dist/utils/resolve-claude-bin.js +52 -0
  221. package/dist/utils/respawn-backoff.js +28 -0
  222. package/dist/utils/sanitize.js +91 -0
  223. package/dist/utils/session-key.js +37 -0
  224. package/dist/utils/stale-launcher.js +109 -0
  225. package/dist/utils/state-dir-migration.js +92 -0
  226. package/dist/utils/systemd.js +126 -0
  227. package/dist/utils/telegram-proxy.js +72 -0
  228. package/dist/utils/uploads-dir.js +27 -0
  229. package/dist/utils/user-roster.js +275 -0
  230. package/dist/utils/verbosity.js +118 -0
  231. package/dist/utils/version-check.js +166 -0
  232. package/dist/utils/workspace-guard.js +44 -0
  233. package/dist/worker-restart.js +141 -0
  234. package/instances.json.example +41 -0
  235. package/package.json +75 -4
@@ -0,0 +1,746 @@
1
+ /**
2
+ * Reading a blocking dialog off Claude Code's TUI.
3
+ *
4
+ * A pty has no one at the keyboard, so every dialog claude opens is a dead
5
+ * end: the input box is gone, the prompt we paste is swallowed by a list that
6
+ * ignores it, and whatever we press next lands on whichever option happens to
7
+ * be highlighted. The bot's answer is to put the dialog in the chat and let a
8
+ * person tap — which needs the dialog read off the screen first.
9
+ *
10
+ * What follows is a parser for the two shapes claude actually draws, measured
11
+ * against a live 120x40 pty:
12
+ *
13
+ * ❯ /model ← the command that opened it, scrolled up
14
+ * ────────────────────────────── ← the seam between transcript and body
15
+ * Select model ← title
16
+ * Switch between Claude models… ← blurb
17
+ * ❯ 1. Default (recommended) ✔ ← options, the glyph marking the cursor
18
+ * 2. Opus (1M context)
19
+ * Enter to set as default · s to use this session only · Esc to cancel
20
+ *
21
+ * the same list without the numbers, which is how the trust dialog draws its
22
+ * `No, exit` / `Yes, I trust this folder`, and dialogs with no list at all
23
+ * (`/tasks`, `/config`) where the footer's key hints are the only affordances
24
+ * there are. WebFetch's permission prompt is the odd one out the other way: a
25
+ * numbered list and no footer at all, so there the list has to mark where the
26
+ * dialog ends ({@link findListEnd}).
27
+ *
28
+ * The footer is parsed rather than assumed, because the keys do not mean the
29
+ * same thing twice. `/model` closes on Esc; `/tasks` closes on Esc; `/config`
30
+ * reads `Esc to clear`, where Esc empties the filter box and leaves the dialog
31
+ * open — pressing it there on the belief that Esc always cancels is how a
32
+ * probe session silently turned off auto-compact. Offer what the dialog says
33
+ * it offers, and nothing else.
34
+ *
35
+ * Two things a dialog says in colour alone, and plain text loses: which of its
36
+ * tabs is open, and whether its list is the part taking keys. Those come from
37
+ * a second reading of the screen, its marks (screen-marks.ts), which the
38
+ * parse takes when the caller has one. Without it a dialog reads as it always
39
+ * did, with no tabs and a list assumed to be listening.
40
+ */
41
+ import { FILLED, MUTED } from './screen-marks.js';
42
+ /** The seam rule between the transcript and a dialog's body. */
43
+ const RULE = /^\s*─{10,}\s*$/u;
44
+ /**
45
+ * The dashed rules an edit prompt draws above and below its diff. What sits
46
+ * between a pair is file content, and is shown exactly as drawn.
47
+ */
48
+ const FENCE = /^\s*╌{10,}\s*$/u;
49
+ /** A row in claude's quote gutter — how a Bash prompt draws its command. */
50
+ const GUTTER = /^\s*│ ?(.*)$/u;
51
+ /**
52
+ * How far short of the terminal's edge claude's prose wraps. Dialogs are
53
+ * padded, so a row breaks a column or two early: `/model`'s blurb broke at a
54
+ * row its next word would only have taken to column 119 of 120.
55
+ */
56
+ const WRAP_SLACK = 4;
57
+ /**
58
+ * The cursor's row: indent, the glyph, a gap, then the label. The capture
59
+ * groups give the column its label starts at, which is what finds the rows
60
+ * beside it.
61
+ */
62
+ const CURSOR_ROW = /^(\s*)❯(\s+)(\S.*)$/u;
63
+ /** A leading `1. `, when the dialog numbers its rows. Many do not. */
64
+ const ROW_NUMBER = /^(\d{1,2})\.\s+(\S.*)$/u;
65
+ /**
66
+ * What a list too long for the screen draws past the rows it shows: `↑ 2 more`
67
+ * above them, `↓ 92 more` below. It sits in the rows' own column, so without
68
+ * this the walk out from the cursor took it for one more row.
69
+ */
70
+ const MORE_ROWS = /^\s*([↑↓])\s+(\d+)\s+more\s*$/u;
71
+ /**
72
+ * A text field: a rounded box one row high, drawn the dialog's full width.
73
+ *
74
+ * ╭──────────────────────────────╮
75
+ * │ Enter the path to the directory │
76
+ * ╰──────────────────────────────╯
77
+ *
78
+ * Measured on `/add-dir`, `/export` → Save to file and `/config`, Claude Code
79
+ * 2.1.326. A box with more than one row inside is drawn content, not a field.
80
+ */
81
+ const FIELD_TOP = /^\s*╭─{10,}/u;
82
+ const FIELD_BOTTOM = /^\s*╰─{10,}/u;
83
+ const FIELD_ROW = /^\s*│ ?(.*?)\s*│?\s*$/u;
84
+ /**
85
+ * The heading of `/rewind`'s first screen, the list of prompts to go back to.
86
+ * Its rows come in pairs, see {@link foldRewindRows}.
87
+ */
88
+ const REWIND_TITLE = /^\s*Rewind\s*$/u;
89
+ /** The row that stands for "where the conversation is now". It has no second line. */
90
+ const REWIND_CURRENT = /^\(current\)$/u;
91
+ /**
92
+ * The group headings `/mcp` draws between its servers, in the rows' own
93
+ * column: `Built-in MCPs (always available)`, `User MCPs (~/.claude.json)`,
94
+ * `claude.ai` — the labels Claude Code 2.1.310's list gives its groups. Its
95
+ * cursor skips them. Read as rows, the server list's heading became a button
96
+ * that could only fail, and a second group's heading would put every row
97
+ * below it one arrow further off than it is.
98
+ */
99
+ const LIST_HEADING = /^\s*(?:[\w.-]+(?: [\w.-]+)* MCPs(?: \(.*\))?|claude\.ai)\s*$/u;
100
+ /**
101
+ * A footer hint. Claude writes these as `Enter to confirm`, `Esc to cancel`,
102
+ * `↑/↓ to select`, `s to use this session only` — a key, the word "to", and a
103
+ * verb phrase. Keys are matched narrowly so a sentence in the body that
104
+ * happens to contain " to " cannot pose as one.
105
+ *
106
+ * A lone arrow is a key of its own. A dialog with tabs has more than one
107
+ * place its keys can go, and names the way between them: `↓ to select` under
108
+ * `/permissions`' tabs, `↑ to tabs` and `↓ to return` in `/config`.
109
+ */
110
+ const KEY_HINT = /^(Enter(?:\/[^\s]+)?|Esc|Tab|Space|[a-z]|↑\/↓|←\/→(?:\/tab)?|[↑↓]|\/)\s+to\s+(.+)$/u;
111
+ /** How far up from the footer a dialog's body can reach. */
112
+ const MAX_BODY_ROWS = 30;
113
+ /**
114
+ * How many lines of chrome may sit *below* the footer.
115
+ *
116
+ * The footer is not reliably the last thing on screen. Claude draws its status
117
+ * bar under a dialog — `⏵⏵ bypass permissions on (shift+tab to cycle)` — and
118
+ * adds notices there too, and every one of those pushed the hints off the last
119
+ * line and made this parser answer null. The caller reads null as "a screen we
120
+ * cannot drive", so a trust dialog with a status bar under it timed out after
121
+ * 180 seconds instead of being offered as two buttons.
122
+ *
123
+ * Bounded rather than open-ended: the footer has to still be near the bottom,
124
+ * or a hint line from the transcript scrolled above could pose as one.
125
+ */
126
+ const MAX_FOOTER_CHROME = 3;
127
+ /**
128
+ * Longest option label we keep. Telegram caps a button at 64 bytes and the
129
+ * ask-user contract at 60 chars; claude pads its rows out to the full 120
130
+ * columns with a trailing description, so most need clipping.
131
+ */
132
+ const MAX_LABEL = 48;
133
+ /**
134
+ * The screen's non-blank rows, the bottom {@link MAX_BODY_ROWS} of them, and
135
+ * for each whether a blank row sat above it. The parse reads only the rows;
136
+ * the gaps are for showing the result, since they are how claude splits a
137
+ * dialog into its title, command, reason and question. `styles` is each kept
138
+ * row's marks, empty strings when the caller had none to give.
139
+ */
140
+ function bodyLines(screenText, marks) {
141
+ const lines = [];
142
+ const gaps = [];
143
+ const styles = [];
144
+ const markRows = marks?.split('\n');
145
+ let gap = false;
146
+ screenText.split('\n').forEach((raw, row) => {
147
+ const line = raw.trimEnd();
148
+ if (!line.trim()) {
149
+ gap = true;
150
+ return;
151
+ }
152
+ lines.push(line);
153
+ gaps.push(gap);
154
+ styles.push(markRows?.[row] ?? '');
155
+ gap = false;
156
+ });
157
+ return {
158
+ lines: lines.slice(-MAX_BODY_ROWS),
159
+ gaps: gaps.slice(-MAX_BODY_ROWS),
160
+ styles: styles.slice(-MAX_BODY_ROWS),
161
+ };
162
+ }
163
+ function collapse(text) {
164
+ return text.replace(/\s+/g, ' ').trim();
165
+ }
166
+ function clip(text, max) {
167
+ const collapsed = collapse(text);
168
+ // trimEnd after slicing: a cut that lands on a space would otherwise render
169
+ // the gap before the ellipsis, which reads as a missing word.
170
+ return collapsed.length > max ? `${collapsed.slice(0, max - 1).trimEnd()}…` : collapsed;
171
+ }
172
+ /**
173
+ * Find the footer: the lowest line that reads as key hints, allowing for the
174
+ * status chrome claude draws beneath a dialog. Returns null when the bottom of
175
+ * the screen holds no hints at all.
176
+ */
177
+ function findFooter(lines) {
178
+ const floor = Math.max(0, lines.length - 1 - MAX_FOOTER_CHROME);
179
+ for (let i = lines.length - 1; i >= floor; i--) {
180
+ const hints = parseKeyHints(lines[i]);
181
+ if (hints.length > 0)
182
+ return { index: i, hints };
183
+ }
184
+ return null;
185
+ }
186
+ /**
187
+ * Where a dialog with no footer ends, standing in for {@link findFooter}.
188
+ *
189
+ * WebFetch's permission prompt has none. It ends on its last row, and the one
190
+ * key it names is written into that row — `3. No, and tell Claude what to do
191
+ * differently (esc)` — so a parser that insisted on a footer never saw it,
192
+ * and a turn that asked to fetch a page sat on the dialog, unrelayed, until
193
+ * the hard ceiling. What marks the end instead is the list itself: numbered
194
+ * rows with the cursor on one, under the seam, reaching the bottom of the
195
+ * screen. The glyph's other haunts don't fit that shape — echoed prompts sit
196
+ * above the seam, and the input box has a rule of its own beneath it.
197
+ *
198
+ * Numbered rows only, because that is how claude draws a list meant for
199
+ * picking from, and it keeps this to the shape that was measured. The index is
200
+ * just past the list, where a footer would be; there are no hints to go with it.
201
+ */
202
+ function findListEnd(lines, width) {
203
+ const seam = lastRuleBefore(lines, lines.length);
204
+ if (seam < 0)
205
+ return null;
206
+ const { options, list } = extractOptions(lines.slice(seam + 1), width);
207
+ if (!list || options.length < 2 || options.some((o) => o.number === undefined))
208
+ return null;
209
+ const index = seam + 1 + list.end + 1;
210
+ return lines.length - index <= MAX_FOOTER_CHROME ? { index, hints: [] } : null;
211
+ }
212
+ /**
213
+ * Where a tab with no footer ends: the bottom of the screen.
214
+ *
215
+ * The Settings dialog's Stats tab draws a chart and names no key at all. Read
216
+ * as no dialog, it was a screen the chat could open (`/stats`, or a tap on
217
+ * the tab) and never leave. Its row of tabs, with one of them open, says it is
218
+ * a dialog, and the tabs are the way out of it: every other one has a button,
219
+ * and the tabs it leads to name their Esc.
220
+ */
221
+ function findTabbedEnd(lines, styles) {
222
+ const seam = lastRuleBefore(lines, lines.length);
223
+ if (seam < 0 || seam + 2 >= lines.length)
224
+ return null;
225
+ return readTabs(lines[seam + 1], styles[seam + 1]) ? { index: lines.length, hints: [] } : null;
226
+ }
227
+ /**
228
+ * Split the footer into its hints. Returns [] for a line that is prose rather
229
+ * than chrome, which is what keeps a dialog we cannot drive from being offered
230
+ * buttons that do nothing.
231
+ */
232
+ export function parseKeyHints(footer) {
233
+ const hints = [];
234
+ for (const part of footer.split('·')) {
235
+ const match = KEY_HINT.exec(part.trim());
236
+ if (match)
237
+ hints.push({ key: match[1], action: clip(match[2], 40) });
238
+ }
239
+ return hints;
240
+ }
241
+ /**
242
+ * Read the dialog covering the input box, or null when the screen holds none.
243
+ *
244
+ * The caller is expected to have established that the input box is absent
245
+ * ({@link hasInputBox}); this only answers what is there instead. A null here
246
+ * with no input box means a screen we cannot drive — mid-render, or a dialog
247
+ * shaped in a way this parser does not know — and the caller should relay it
248
+ * verbatim rather than press anything.
249
+ *
250
+ * `marks` is the screen's second reading (screen-marks.ts), for the tabs and
251
+ * the resting cursor. With it, one more screen reads as a dialog: a tab that
252
+ * draws no footer ({@link findTabbedEnd}).
253
+ */
254
+ export function parseModal(screenText, marks) {
255
+ const { lines, gaps, styles } = bodyLines(screenText, marks);
256
+ if (lines.length === 0)
257
+ return null;
258
+ // The terminal's width, near enough: the seam is drawn edge to edge.
259
+ const width = Math.max(...lines.map((l) => l.length));
260
+ const footer = findFooter(lines) ?? findListEnd(lines, width) ?? findTabbedEnd(lines, styles);
261
+ if (!footer)
262
+ return null;
263
+ const { hints, index: footerIndex } = footer;
264
+ // Body starts below the seam rule when there is one; a dialog that redrew
265
+ // the whole screen has no seam, and everything we kept is its body.
266
+ let seam = lastRuleBefore(lines, footerIndex);
267
+ let body = lines.slice(seam + 1, footerIndex);
268
+ // A rule can also sit *inside* a dialog. Auto mode draws one between its
269
+ // numbered choices and a trailing "Chat about this", which left a body of a
270
+ // single line with no cursor in it — five options parsed as none, and the
271
+ // only button on offer was a bare `Enter to select` committing the row
272
+ // nobody had been shown. Reach back past a rule like that.
273
+ //
274
+ // Only when what's left below it is numbered, though. The transcript above
275
+ // a dialog carries the echoed command wearing this same glyph (`❯ /tasks`),
276
+ // and reaching past the real seam would swallow it as an option. Claude
277
+ // numbers a list exactly when its rows are selectable, so that is the line
278
+ // between a continued list and a dialog that simply has no rows.
279
+ if (seam > 0 && body.length > 0
280
+ && !body.some((line) => CURSOR_ROW.test(line))
281
+ && body.every((line) => ROW_NUMBER.test(line.trimStart()))) {
282
+ const outer = lastRuleBefore(lines, seam);
283
+ const extended = lines.slice(outer + 1, footerIndex);
284
+ if (extended.some((line) => CURSOR_ROW.test(line))) {
285
+ seam = outer;
286
+ body = extended;
287
+ }
288
+ }
289
+ if (body.length === 0)
290
+ return null;
291
+ const bodyStyles = styles.slice(seam + 1, footerIndex);
292
+ // Read the rows with only the live cursor's glyph on them.
293
+ const cursors = readCursors(body, bodyStyles);
294
+ const drawn = body;
295
+ body = cursors.body;
296
+ const { options, highlighted, more, rows, list } = extractOptions(body, width);
297
+ // A cursor we could not resolve into rows is the one state that must not
298
+ // produce buttons. The footer's `Enter to confirm` would then be the only
299
+ // thing on offer, and Enter commits whatever the cursor is on — which is
300
+ // exactly how the trust dialog, whose default row is `No, exit`, got
301
+ // answered with an exit. Better to relay the screen and press nothing.
302
+ if (options.length === 0 && body.some((l) => l.includes('❯')))
303
+ return null;
304
+ // The title is the first body row that isn't an option — dialogs lead with
305
+ // their heading, and one that opens straight onto its list has none.
306
+ const title = body.find((_, i) => !rows.has(i));
307
+ const field = readField(body);
308
+ // A row of tabs is the dialog's first line, and its first word the heading.
309
+ const strip = rows.has(0) ? null : readTabs(body[0], bodyStyles[0]);
310
+ const shown = strip
311
+ ? [body[0].slice(0, indentOf(body[0])) + strip.title, ...body.slice(1)]
312
+ : body;
313
+ return {
314
+ title: strip ? clip(strip.title, 120) : title ? clip(title, 120) : clip(body[0], 120),
315
+ options,
316
+ highlighted,
317
+ more,
318
+ hints,
319
+ ...(strip ? { tabs: strip.tabs } : {}),
320
+ ...(cursors.resting && highlighted >= 0 ? { resting: true } : {}),
321
+ ...(field ? { field } : {}),
322
+ body: drawn.join('\n'),
323
+ parts: readParts(shown, gaps.slice(seam + 1, footerIndex), list, width),
324
+ };
325
+ }
326
+ /**
327
+ * A dialog's row of tabs, read off its first line: the heading, then each
328
+ * tab's name, two or more spaces apart, with the open one filled in.
329
+ *
330
+ * Permissions Recently blocked Allow Ask Deny Auto mode Workspace
331
+ * #######
332
+ *
333
+ * Measured on `/permissions`, the Settings dialog (`/status`, `/config`,
334
+ * `/usage`) and `/plugin`, Claude Code 2.1.326. The fill covers the name and
335
+ * a space either side, in the dialog's colour while the tabs take the keys
336
+ * and in reverse video while something below them does.
337
+ *
338
+ * Null unless exactly one stretch of the line is filled and it is one of the
339
+ * names. Without the marks there is no telling which tab is open, and a row
340
+ * of tabs with no open one is a heading like any other.
341
+ */
342
+ export function readTabs(line, marks) {
343
+ if (!marks)
344
+ return null;
345
+ const filled = [];
346
+ for (let i = 0; i < line.length; i++) {
347
+ if (marks[i] !== FILLED)
348
+ continue;
349
+ const last = filled[filled.length - 1];
350
+ if (last && last.end === i)
351
+ last.end = i + 1;
352
+ else
353
+ filled.push({ start: i, end: i + 1 });
354
+ }
355
+ if (filled.length !== 1)
356
+ return null;
357
+ const [title, ...names] = line.trim().split(/\s{2,}/);
358
+ if (!title || names.length < 2)
359
+ return null;
360
+ const active = names.indexOf(line.slice(filled[0].start, filled[0].end).trim());
361
+ return active < 0 ? null : { title, tabs: { names, active } };
362
+ }
363
+ /**
364
+ * The row of tabs alone, for watching a walk along them. A step is checked
365
+ * the moment its tab is drawn open, before what the tab holds has loaded.
366
+ */
367
+ export function tabsOn(screenText, marks) {
368
+ const { lines, styles } = bodyLines(screenText, marks);
369
+ const seam = lastRuleBefore(lines, lines.length);
370
+ const first = lines[seam + 1];
371
+ return first === undefined ? null : readTabs(first, styles[seam + 1])?.tabs ?? null;
372
+ }
373
+ /**
374
+ * The body with only the live cursor's glyph left on it, and whether that
375
+ * cursor is resting.
376
+ *
377
+ * A dialog with two lists draws a `❯` in each. `/memory` has its Auto-memory
378
+ * switch above its list of files, and whichever of the two isn't taking keys
379
+ * keeps its glyph, in grey:
380
+ *
381
+ * ❯ Auto-memory on ← grey while the list has the keys
382
+ * ❯ User instructions Saved in …
383
+ * Project instructions …
384
+ *
385
+ * Read as text, that is two cursors, and the walk out from the first one
386
+ * stopped at the second: one row offered out of four. With the grey glyph
387
+ * blanked, its row is a row like the others, and ↑ from the top of the list
388
+ * does reach it.
389
+ *
390
+ * A grey glyph with no other on screen is the dialog's one cursor all the
391
+ * same, and stays. It marks a list that isn't taking keys just now, see
392
+ * {@link TuiModal.resting}.
393
+ */
394
+ function readCursors(body, styles) {
395
+ const glyphs = body.flatMap((line, row) => {
396
+ const match = CURSOR_ROW.exec(line);
397
+ return match ? [{ row, col: match[1].length }] : [];
398
+ });
399
+ const muted = glyphs.filter(({ row, col }) => styles[row]?.[col] === MUTED);
400
+ if (muted.length === 0)
401
+ return { body, resting: false };
402
+ if (muted.length === glyphs.length)
403
+ return { body, resting: glyphs.length === 1 };
404
+ const out = [...body];
405
+ for (const { row, col } of muted) {
406
+ out[row] = `${out[row].slice(0, col)} ${out[row].slice(col + 1)}`;
407
+ }
408
+ return { body: out, resting: false };
409
+ }
410
+ /** Index of the last rule strictly above `before`, or -1 when there is none. */
411
+ function lastRuleBefore(lines, before) {
412
+ for (let i = before - 1; i >= 0; i--) {
413
+ if (RULE.test(lines[i]))
414
+ return i;
415
+ }
416
+ return -1;
417
+ }
418
+ /**
419
+ * What `line` is, to a list whose rows begin at `col`.
420
+ *
421
+ * The three answers are the shape of every list claude draws. A row starts
422
+ * exactly at the column its siblings do. Anything starting *deeper* hangs off
423
+ * the row above — auto mode gives each choice a sentence of explanation
424
+ * underneath, and a walk that stopped at the first of those found one option
425
+ * where there were five. A rule can sit inside a list too, so it is stepped
426
+ * over rather than treated as the end. Anything starting *shallower* is the
427
+ * title, the blurb or prose, and that really is the end of the list. Two
428
+ * things are drawn in the rows' column without being rows, and are told
429
+ * apart by what they say: the `↓ 92 more` of a list that scrolls, and the
430
+ * headings of one in groups.
431
+ *
432
+ * One shape starts shallower and is a row all the same: a list that shows a
433
+ * window of its rows marks the window's edge with a bare arrow where the
434
+ * cursor would be. `/rewind` draws five of its six choices, the last as
435
+ * `↓ 5. Summarize up to here`. Taken for the end of the list, that row was
436
+ * never offered, and the one hidden past it could not be reached at all.
437
+ */
438
+ function rowRole(line, col, glyphCol) {
439
+ if (RULE.test(line))
440
+ return 'skip';
441
+ const at = line.search(/\S/);
442
+ if (at < 0)
443
+ return 'skip';
444
+ if (MORE_ROWS.test(line))
445
+ return 'more';
446
+ if (at === col)
447
+ return LIST_HEADING.test(line) ? 'skip' : 'row';
448
+ if (at === glyphCol && /[↑↓]/u.test(line[at]) && line.slice(at + 1).search(/\S/) === col - at - 1) {
449
+ return 'edge';
450
+ }
451
+ return at > col ? 'skip' : 'end';
452
+ }
453
+ /** Note the window edge a row marks: the list goes on past it, by an amount it doesn't give. */
454
+ function noteEdge(more, line, glyphCol) {
455
+ if (line[glyphCol] === '↑')
456
+ more.above = Math.max(more.above, 1);
457
+ else
458
+ more.below = Math.max(more.below, 1);
459
+ more.uncounted = true;
460
+ }
461
+ /**
462
+ * The rows of `/rewind`'s list of prompts that are entries, out of every line
463
+ * drawn in the rows' column.
464
+ *
465
+ * Each prompt there takes two lines, both in that column: the prompt, and
466
+ * under it what going back would do to the code.
467
+ *
468
+ * reply with just the number 4
469
+ * No code changes
470
+ * ❯ Use the Edit tool to append the line 'tail' to a.txt
471
+ * a.txt +1
472
+ * (current)
473
+ *
474
+ * Read as five rows, every second button was a line nothing can select, and
475
+ * the cursor was counted twice as far from each prompt as it is, so the walk
476
+ * to a prompt ended on another one. The second lines say too many things to
477
+ * tell by their text (`a.txt +1 -1`, `3 files changed +10 -2`, `Code changes
478
+ * unknown`, `No code restore`), so they are told by position: the list draws
479
+ * whole entries, the first line of one is its prompt, and only `(current)`,
480
+ * which is always last, has no second line.
481
+ *
482
+ * Null when that puts the cursor on a second line, which means the list was
483
+ * not drawn the way this reads it.
484
+ */
485
+ function foldRewindRows(body, indexes, cursor, col) {
486
+ const entries = [];
487
+ for (let k = 0; k < indexes.length; k++) {
488
+ entries.push(indexes[k]);
489
+ if (!REWIND_CURRENT.test(body[indexes[k]].slice(col).trim()))
490
+ k++;
491
+ }
492
+ return entries.includes(cursor) ? entries : null;
493
+ }
494
+ /**
495
+ * The dialog's selectable rows, found from the cursor outwards.
496
+ *
497
+ * Claude does not number them consistently — `/model` draws `❯ 1. Default`,
498
+ * the trust dialog draws a bare `❯ No, exit` — so a pattern keyed on the
499
+ * number misses the dialogs that matter most. What every list does share is
500
+ * layout: the rows are the run of lines whose text starts in the same column
501
+ * as the cursor's own label, and nothing else on the screen lines up there.
502
+ * Walking out from the cursor until that alignment breaks picks up the rows
503
+ * and stops at the blurb above and the footer below.
504
+ */
505
+ function extractOptions(body, width) {
506
+ const more = { above: 0, below: 0 };
507
+ const none = { options: [], highlighted: -1, more, rows: new Set(), list: null };
508
+ const cursor = body.findIndex((l) => CURSOR_ROW.test(l));
509
+ if (cursor < 0)
510
+ return none;
511
+ const match = CURSOR_ROW.exec(body[cursor]);
512
+ const glyphCol = match[1].length;
513
+ const labelCol = glyphCol + 1 + match[2].length;
514
+ let indexes = [];
515
+ // The list's first line, which is its `↑ 2 more` when it has one.
516
+ let start = cursor;
517
+ for (let i = cursor - 1; i >= 0; i--) {
518
+ const role = rowRole(body[i], labelCol, glyphCol);
519
+ if (role === 'end')
520
+ break;
521
+ if (role === 'row' || role === 'edge')
522
+ indexes.unshift(i);
523
+ if (role === 'edge')
524
+ noteEdge(more, body[i], glyphCol);
525
+ if (role === 'more')
526
+ more.above = Number(MORE_ROWS.exec(body[i])[2]);
527
+ if (role !== 'skip')
528
+ start = i;
529
+ }
530
+ indexes.push(cursor);
531
+ // Where this walk stops is where the list ends: what hangs under its last
532
+ // row still belongs to that row.
533
+ let stop = cursor + 1;
534
+ for (; stop < body.length; stop++) {
535
+ const role = rowRole(body[stop], labelCol, glyphCol);
536
+ if (role === 'end')
537
+ break;
538
+ if (role === 'row' || role === 'edge')
539
+ indexes.push(stop);
540
+ if (role === 'edge')
541
+ noteEdge(more, body[stop], glyphCol);
542
+ if (role === 'more')
543
+ more.below = Number(MORE_ROWS.exec(body[stop])[2]);
544
+ }
545
+ // `/rewind` numbers its second screen, the choices, and not its first, the
546
+ // prompts — and only the first draws two lines to a row.
547
+ const paired = REWIND_TITLE.test(body[0] ?? '')
548
+ && !indexes.some((i) => ROW_NUMBER.test(body[i].slice(labelCol)));
549
+ if (paired) {
550
+ const entries = foldRewindRows(body, indexes, cursor, labelCol);
551
+ if (!entries)
552
+ return none;
553
+ indexes = entries;
554
+ }
555
+ // However many rows sit above it is where the cursor is — the arrow walk
556
+ // counts rows, not screen lines, and the two differ wherever a description
557
+ // or a rule sits between them.
558
+ const highlighted = indexes.indexOf(cursor);
559
+ const options = [];
560
+ const rows = new Set();
561
+ indexes.forEach((i, k) => {
562
+ rows.add(i);
563
+ const text = i === cursor ? match[3] : body[i].slice(labelCol);
564
+ const numbered = ROW_NUMBER.exec(text);
565
+ const own = numbered ? numbered[2] : text;
566
+ const under = body.slice(i + 1, indexes[k + 1] ?? stop)
567
+ .filter((l) => !RULE.test(l) && !MORE_ROWS.test(l) && !LIST_HEADING.test(l));
568
+ const detail = collapse(under.join(' '));
569
+ options.push({
570
+ ...(numbered ? { number: Number(numbered[1]) } : {}),
571
+ label: clip(own, MAX_LABEL),
572
+ // A rewind entry's second line is never the rest of its first, however
573
+ // close to the edge a long prompt was cut.
574
+ ...(paired
575
+ ? { text: collapse(own), ...(detail ? { detail } : {}) }
576
+ : readRow(body[i], own, under, width)),
577
+ index: options.length,
578
+ });
579
+ });
580
+ return { options, highlighted, more, rows, list: { start, end: stop - 1 } };
581
+ }
582
+ /**
583
+ * A row's full text, and what the dialog says about it.
584
+ *
585
+ * `under` is whatever hangs beneath the row before the next one: the rest of
586
+ * a label claude wrapped at the edge, then any description — auto writes a
587
+ * sentence under each choice. `/model` puts its description beside the row
588
+ * instead, a wide gap away, which is what splitting on a run of spaces finds.
589
+ */
590
+ function readRow(row, own, under, width) {
591
+ let text = own;
592
+ let last = row;
593
+ let n = 0;
594
+ while (n < under.length && wrapsOnto(last, under[n], width)) {
595
+ text += ` ${under[n].trim()}`;
596
+ last = under[n++];
597
+ }
598
+ const [label, ...beside] = text.trim().split(/\s{2,}/);
599
+ const detail = collapse([...beside, ...under.slice(n)].join(' '));
600
+ return { text: collapse(label), ...(detail ? { detail } : {}) };
601
+ }
602
+ /** The dialog's text field and what it shows, when it draws one. See {@link FIELD_TOP}. */
603
+ function readField(body) {
604
+ for (let i = 0; i + 2 < body.length; i++) {
605
+ if (!FIELD_TOP.test(body[i]) || !FIELD_BOTTOM.test(body[i + 2]))
606
+ continue;
607
+ const row = FIELD_ROW.exec(body[i + 1]);
608
+ if (row)
609
+ return { value: row[1].trim() };
610
+ }
611
+ return undefined;
612
+ }
613
+ /**
614
+ * Read the body into what a chat can show, in the order claude drew it.
615
+ *
616
+ * Blank rows split it into parts, as they split the dialog on screen, and so
617
+ * does any change between prose and drawn content — a Bash prompt's command
618
+ * sits straight under its title, with nothing but its gutter to set it off.
619
+ */
620
+ function readParts(body, gaps, list, width) {
621
+ const parts = [];
622
+ let open = null;
623
+ let fenced = false;
624
+ for (let i = 0; i < body.length; i++) {
625
+ const line = body[i];
626
+ if (list && i >= list.start && i <= list.end) {
627
+ if (i === list.start)
628
+ parts.push({ kind: 'options', gapBefore: gaps[i] });
629
+ open = null;
630
+ continue;
631
+ }
632
+ if (FENCE.test(line)) {
633
+ fenced = !fenced;
634
+ open = null;
635
+ continue;
636
+ }
637
+ // A box's borders are drawing. What's inside it reads as quoted, like a
638
+ // command in its gutter: the box's sides are that same bar.
639
+ if (RULE.test(line) || FIELD_TOP.test(line) || FIELD_BOTTOM.test(line)) {
640
+ open = null;
641
+ continue;
642
+ }
643
+ const gutter = fenced ? null : GUTTER.exec(line);
644
+ const kind = fenced || gutter ? 'verbatim' : 'text';
645
+ if (open?.kind === 'verbatim' && kind === 'verbatim' && gaps[i]) {
646
+ // A blank row inside drawn content is part of the content.
647
+ open.lines.push('');
648
+ }
649
+ else if (!open || open.kind !== kind || gaps[i]) {
650
+ open = { kind, lines: [], gapBefore: gaps[i] };
651
+ parts.push(open);
652
+ }
653
+ open.lines.push(gutter ? gutter[1].replace(/\s*│$/u, '') : line);
654
+ }
655
+ // The dialog's own prose sits at one indent; what it quotes — a one-line
656
+ // command, a URL — is indented under it. Only the prose gets rejoined: a
657
+ // command whose last row ends near the edge would otherwise swallow the
658
+ // description under it, and show the user a command they aren't approving.
659
+ const base = Math.min(...parts.flatMap((p) => (p.kind === 'text' ? p.lines.map(indentOf) : [])));
660
+ return parts.map((part) => {
661
+ if (part.kind === 'text')
662
+ return { ...part, lines: unwrap(part.lines, width, base) };
663
+ if (part.kind === 'verbatim')
664
+ return { ...part, lines: dedent(part.lines) };
665
+ return part;
666
+ });
667
+ }
668
+ /**
669
+ * Undo claude's wraps at the terminal edge. It breaks prose where the width
670
+ * runs out, and a phone then wraps both halves again, stranding the odd word
671
+ * on a line of its own. A row carries on into the next exactly when the next
672
+ * one's first word would not have fitted on it.
673
+ */
674
+ function unwrap(lines, width, indent) {
675
+ const out = [];
676
+ lines.forEach((line, i) => {
677
+ const prose = i > 0 && indentOf(lines[i - 1]) === indent && indentOf(line) === indent;
678
+ if (prose && wrapsOnto(lines[i - 1], line, width))
679
+ out[out.length - 1] += ` ${line.trim()}`;
680
+ else
681
+ out.push(line.trim());
682
+ });
683
+ return out;
684
+ }
685
+ function indentOf(line) {
686
+ return line.search(/\S/);
687
+ }
688
+ /** True when claude broke `row` at the edge and `next` is the rest of it. */
689
+ function wrapsOnto(row, next, width) {
690
+ const word = next.trim().split(/\s/, 1)[0];
691
+ return row.length + 1 + word.length > width - WRAP_SLACK;
692
+ }
693
+ /** Strip the indentation every line shares — the dialog's, not the content's. */
694
+ function dedent(lines) {
695
+ const depth = Math.min(...lines.filter((l) => l.trim()).map(indentOf));
696
+ return Number.isFinite(depth) ? lines.map((l) => l.slice(depth)) : lines;
697
+ }
698
+ /**
699
+ * The keystrokes that answer `modal` with the option at `index`.
700
+ *
701
+ * Arrow keys rather than the row's digit: not every dialog numbers its rows,
702
+ * and the ones that do don't all take the number as a hotkey, whereas ↑/↓ move
703
+ * the cursor in all of them. Moving is also checkable — the caller re-reads the
704
+ * screen and confirms the glyph landed on the intended row before committing —
705
+ * where a digit is a blind press.
706
+ *
707
+ * Returns null when the cursor's position is unknown, since every step from an
708
+ * unknown start is a guess.
709
+ */
710
+ export function arrowsTo(modal, index) {
711
+ if (modal.highlighted < 0)
712
+ return null;
713
+ if (index < 0 || index >= modal.options.length)
714
+ return null;
715
+ const distance = index - modal.highlighted;
716
+ return Array.from({ length: Math.abs(distance) }, () => (distance > 0 ? '\x1b[B' : '\x1b[A'));
717
+ }
718
+ /**
719
+ * The byte a footer hint's key sends. Null for keys that don't answer the
720
+ * dialog, which are no use as a button.
721
+ *
722
+ * Tab is one of those, though claude's footers name it. A permission prompt's
723
+ * `Tab to amend` turns the highlighted row into a text field — `Yes, and tell
724
+ * Claude what to do next` — and waits for a note there, which a button cannot
725
+ * type. Pressing it only ever changed the dialog without closing it, and the
726
+ * turn then failed on a dialog that was still open.
727
+ */
728
+ export function keystrokeFor(key) {
729
+ if (/^Enter/.test(key))
730
+ return '\r';
731
+ if (key === 'Esc')
732
+ return '\x1b';
733
+ if (key === 'Space')
734
+ return ' ';
735
+ if (/^[a-z]$/.test(key))
736
+ return key;
737
+ // `↓ to select`, under a dialog's tabs, is the one way into its list: there
738
+ // is no cursor on any row until it is pressed, so no row can be offered.
739
+ // Its opposite, `↑ to tabs`, is no use as a button. What it leads to is
740
+ // ←/→, and a tab's own button makes that whole trip.
741
+ if (key === '↓')
742
+ return '\x1b[B';
743
+ // ↑/↓, ←/→ and / are navigation or search affordances, not answers.
744
+ return null;
745
+ }
746
+ //# sourceMappingURL=tui-modal.js.map