telecoder 1.2.0 → 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 (197) hide show
  1. package/.env.example +142 -130
  2. package/CHANGELOG.md +683 -2
  3. package/README.md +36 -32
  4. package/dist/audio/transcribe.js +0 -9
  5. package/dist/bin/mcp-server.js +46 -61
  6. package/dist/bot/bot.js +162 -181
  7. package/dist/bot/command-catalog.js +263 -0
  8. package/dist/bot/deleted-threads.js +103 -0
  9. package/dist/bot/file-access.js +35 -26
  10. package/dist/bot/handlers/album.handler.js +128 -0
  11. package/dist/bot/handlers/{fork.handler.js → branch.handler.js} +267 -280
  12. package/dist/bot/handlers/command/access.js +1 -1
  13. package/dist/bot/handlers/command/admin.js +86 -110
  14. package/dist/bot/handlers/command/all-view.js +33 -0
  15. package/dist/bot/handlers/command/auto-approve.js +3 -3
  16. package/dist/bot/handlers/command/background.js +37 -0
  17. package/dist/bot/handlers/command/botname.js +76 -0
  18. package/dist/bot/handlers/command/btw.js +20 -69
  19. package/dist/bot/handlers/command/cleanup.js +329 -0
  20. package/dist/bot/handlers/command/model.js +40 -119
  21. package/dist/bot/handlers/command/prefs-scope.js +255 -90
  22. package/dist/bot/handlers/command/project.js +31 -109
  23. package/dist/bot/handlers/command/resume.js +8 -4
  24. package/dist/bot/handlers/command/session.js +74 -213
  25. package/dist/bot/handlers/command/settings.js +92 -447
  26. package/dist/bot/handlers/command/shared.js +32 -35
  27. package/dist/bot/handlers/command/tasks.js +263 -95
  28. package/dist/bot/handlers/command/thread.js +128 -0
  29. package/dist/bot/handlers/command/topic-store.js +14 -53
  30. package/dist/bot/handlers/command/topic.js +8 -61
  31. package/dist/bot/handlers/command/{cost.js → usage.js} +32 -59
  32. package/dist/bot/handlers/command-fallback.js +179 -0
  33. package/dist/bot/handlers/command.handler.js +96 -422
  34. package/dist/bot/handlers/{schedule.handler.js → loop.handler.js} +159 -73
  35. package/dist/bot/handlers/media-fallback.js +12 -7
  36. package/dist/bot/handlers/message/replies.js +10 -87
  37. package/dist/bot/handlers/message/shared.js +9 -31
  38. package/dist/bot/handlers/message/turn-notify.js +15 -49
  39. package/dist/bot/handlers/message/turn-runner.js +18 -103
  40. package/dist/bot/handlers/message.handler.js +59 -52
  41. package/dist/bot/handlers/newbot.handler.js +13 -4
  42. package/dist/bot/handlers/photo.handler.js +88 -129
  43. package/dist/bot/handlers/prompts.handler.js +11 -11
  44. package/dist/bot/handlers/running-note.js +2 -2
  45. package/dist/bot/handlers/session-guard.js +4 -4
  46. package/dist/bot/handlers/suggestion.handler.js +2 -2
  47. package/dist/bot/handlers/tree.handler.js +20 -20
  48. package/dist/bot/handlers/voice.handler.js +23 -85
  49. package/dist/bot/kept-threads.js +51 -0
  50. package/dist/bot/middleware/album-collector.js +69 -0
  51. package/dist/bot/middleware/auth.middleware.js +1 -1
  52. package/dist/bot/middleware/group-mention.middleware.js +11 -5
  53. package/dist/bot/middleware/queued-messages.js +143 -32
  54. package/dist/bot/middleware/stale-filter.js +0 -22
  55. package/dist/build-info.json +5 -5
  56. package/dist/claude/ask-user-form.js +517 -0
  57. package/dist/claude/ask-user.js +94 -26
  58. package/dist/claude/auto-topic-haiku.js +17 -0
  59. package/dist/claude/available-commands.js +22 -19
  60. package/dist/claude/background-hint.js +91 -0
  61. package/dist/claude/{fork-tree.js → branch-tree.js} +9 -9
  62. package/dist/claude/carry-over.js +0 -2
  63. package/dist/claude/ccr-shim.js +12 -10
  64. package/dist/claude/claude-auth.js +6 -10
  65. package/dist/claude/command-parser.js +49 -43
  66. package/dist/claude/cost-overlay-scraper.js +7 -2
  67. package/dist/claude/effort-level.js +5 -0
  68. package/dist/claude/enabled-plugins.js +12 -16
  69. package/dist/claude/in-flight-tracker.js +21 -0
  70. package/dist/claude/mcp-bridge.js +86 -58
  71. package/dist/claude/memory-guard.js +400 -0
  72. package/dist/claude/message-offsets.js +5 -5
  73. package/dist/claude/modal-relay.js +401 -34
  74. package/dist/claude/model-catalog.js +20 -3
  75. package/dist/claude/monitor-relay.js +130 -15
  76. package/dist/claude/native-commands.js +40 -0
  77. package/dist/claude/{pending-forks.js → pending-branches.js} +26 -26
  78. package/dist/claude/permission-gate.js +11 -10
  79. package/dist/claude/permission-mode.js +19 -45
  80. package/dist/claude/prewarm.js +70 -0
  81. package/dist/claude/pty-idle-reaper.js +96 -0
  82. package/dist/claude/pty-provider.js +473 -188
  83. package/dist/claude/pty-spawn-config.js +79 -32
  84. package/dist/claude/pty-watch.js +201 -0
  85. package/dist/claude/request-queue.js +7 -35
  86. package/dist/claude/rewind.js +111 -0
  87. package/dist/claude/scheduled-runner.js +1 -3
  88. package/dist/claude/scheduler.js +6 -2
  89. package/dist/claude/scope-guard.js +8 -3
  90. package/dist/claude/screen-marks.js +83 -0
  91. package/dist/claude/screen-render.js +96 -0
  92. package/dist/claude/session-history.js +4 -2
  93. package/dist/claude/session-jsonl.js +162 -15
  94. package/dist/claude/session-manager.js +29 -8
  95. package/dist/claude/session-move.js +70 -0
  96. package/dist/claude/shell-mode-gate.js +60 -0
  97. package/dist/claude/side-question.js +20 -17
  98. package/dist/claude/stage-images.js +5 -5
  99. package/dist/claude/tasks-overlay-scraper.js +143 -20
  100. package/dist/claude/thread-judge-haiku.js +88 -0
  101. package/dist/claude/tip-scraper.js +3 -4
  102. package/dist/claude/tui-modal.js +272 -27
  103. package/dist/claude/tui-state.js +74 -2
  104. package/dist/claude/turn-end.js +15 -1
  105. package/dist/claude/turn-prose.js +1 -1
  106. package/dist/claude/typed-answer.js +104 -0
  107. package/dist/claude/update-banner-relay.js +1 -1
  108. package/dist/cli/commands/doctor.js +48 -15
  109. package/dist/cli/commands/help.js +11 -2
  110. package/dist/cli/commands/init.js +16 -13
  111. package/dist/cli/commands/service.js +40 -8
  112. package/dist/cli/commands/start.js +10 -0
  113. package/dist/cli/commands/watch.js +322 -0
  114. package/dist/cli/dispatch.js +5 -0
  115. package/dist/cli/init/env-file.js +1 -1
  116. package/dist/cli/init/flow.js +114 -47
  117. package/dist/cli/init/prompter.js +8 -0
  118. package/dist/cli/init/telegram.js +35 -9
  119. package/dist/cli/service/render.js +3 -3
  120. package/dist/config.js +156 -131
  121. package/dist/index.js +107 -31
  122. package/dist/launcher.js +34 -7
  123. package/dist/providers/ccr-introspect.js +1 -8
  124. package/dist/providers/ccr-provider.js +19 -20
  125. package/dist/providers/claude-provider.js +4 -57
  126. package/dist/providers/prefs-sync.js +16 -59
  127. package/dist/providers/provider-router.js +24 -23
  128. package/dist/providers/spawn-settings.js +45 -0
  129. package/dist/providers/user-preferences.js +127 -19
  130. package/dist/restart-when-idle.js +27 -7
  131. package/dist/startup/auto-continue.js +2 -2
  132. package/dist/startup/auto-resume.js +3 -15
  133. package/dist/startup/notices.js +1 -1
  134. package/dist/startup/verify-token.js +4 -4
  135. package/dist/telegram/action-logger.js +3 -165
  136. package/dist/telegram/background-activity-card.js +1 -1
  137. package/dist/telegram/botname-settings.js +71 -125
  138. package/dist/telegram/client-options.js +6 -7
  139. package/dist/telegram/force-reply-tracker.js +55 -16
  140. package/dist/telegram/message-sender.js +92 -360
  141. package/dist/telegram/prompt-label.js +11 -5
  142. package/dist/telegram/streaming-turn.js +5 -7
  143. package/dist/telegram/table-lists.js +56 -0
  144. package/dist/telegram/task-list.js +4 -4
  145. package/dist/telegram/task-tracker.js +45 -126
  146. package/dist/telegram/terminal-renderer.js +1 -5
  147. package/dist/telegram/thread-titles.js +74 -2
  148. package/dist/telegram/threads-setup.js +1 -1
  149. package/dist/utils/admins.js +2 -2
  150. package/dist/utils/bot-icon.js +248 -0
  151. package/dist/utils/bot-token.js +8 -5
  152. package/dist/utils/build-info.js +3 -3
  153. package/dist/utils/caffeinate.js +6 -3
  154. package/dist/utils/claude-env.js +0 -1
  155. package/dist/utils/claude-version.js +2 -2
  156. package/dist/utils/config-home.js +3 -3
  157. package/dist/utils/download.js +41 -15
  158. package/dist/utils/exit-codes.js +11 -0
  159. package/dist/utils/format.js +3 -6
  160. package/dist/utils/home-project.js +1 -1
  161. package/dist/utils/idle-restart.js +1 -1
  162. package/dist/utils/install-mode.js +1 -1
  163. package/dist/utils/instance-config.js +1 -1
  164. package/dist/utils/instances.js +1 -1
  165. package/dist/utils/keyed-settings.js +2 -2
  166. package/dist/utils/memory-pressure.js +60 -0
  167. package/dist/utils/npm-version.js +2 -2
  168. package/dist/utils/resolve-claude-bin.js +13 -24
  169. package/dist/utils/respawn-backoff.js +9 -0
  170. package/dist/utils/stale-launcher.js +1 -1
  171. package/dist/utils/systemd.js +8 -8
  172. package/dist/utils/telegram-proxy.js +72 -0
  173. package/dist/utils/uploads-dir.js +27 -0
  174. package/dist/utils/verbosity.js +8 -8
  175. package/dist/utils/version-check.js +5 -5
  176. package/package.json +3 -4
  177. package/dist/bot/handlers/command/media.js +0 -197
  178. package/dist/bot/handlers/command/streaming-mode.js +0 -16
  179. package/dist/bot/handlers/message/throttle.js +0 -49
  180. package/dist/claude/agent-watchdog.js +0 -162
  181. package/dist/claude/agent.js +0 -1458
  182. package/dist/claude/held-query.js +0 -190
  183. package/dist/claude/mcp-tools.js +0 -354
  184. package/dist/claude/sdk-permission-gate.js +0 -77
  185. package/dist/claude/session-cost.js +0 -41
  186. package/dist/claude/system-prompt.js +0 -162
  187. package/dist/providers/sdk-provider.js +0 -37
  188. package/dist/telegram/telegraph-settings.js +0 -31
  189. package/dist/telegram/telegraph.js +0 -453
  190. package/dist/telegram/terminal-settings.js +0 -25
  191. package/dist/tts/tts-settings.js +0 -62
  192. package/dist/tts/tts.js +0 -245
  193. package/dist/tts/voice-reply.js +0 -78
  194. package/dist/utils/debug-agent.js +0 -117
  195. package/dist/utils/debug-watchdog.js +0 -121
  196. package/dist/utils/project-data-dir.js +0 -33
  197. package/dist/utils/reasoning-summary.js +0 -12
@@ -31,7 +31,14 @@
31
31
  * open — pressing it there on the belief that Esc always cancels is how a
32
32
  * probe session silently turned off auto-compact. Offer what the dialog says
33
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.
34
40
  */
41
+ import { FILLED, MUTED } from './screen-marks.js';
35
42
  /** The seam rule between the transcript and a dialog's body. */
36
43
  const RULE = /^\s*─{10,}\s*$/u;
37
44
  /**
@@ -61,6 +68,26 @@ const ROW_NUMBER = /^(\d{1,2})\.\s+(\S.*)$/u;
61
68
  * this the walk out from the cursor took it for one more row.
62
69
  */
63
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;
64
91
  /**
65
92
  * The group headings `/mcp` draws between its servers, in the rows' own
66
93
  * column: `Built-in MCPs (always available)`, `User MCPs (~/.claude.json)`,
@@ -75,8 +102,12 @@ const LIST_HEADING = /^\s*(?:[\w.-]+(?: [\w.-]+)* MCPs(?: \(.*\))?|claude\.ai)\s
75
102
  * `↑/↓ to select`, `s to use this session only` — a key, the word "to", and a
76
103
  * verb phrase. Keys are matched narrowly so a sentence in the body that
77
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`.
78
109
  */
79
- const KEY_HINT = /^(Enter(?:\/[^\s]+)?|Esc|Tab|Space|[a-z]|↑\/↓|←\/→|\/)\s+to\s+(.+)$/u;
110
+ const KEY_HINT = /^(Enter(?:\/[^\s]+)?|Esc|Tab|Space|[a-z]|↑\/↓|←\/→(?:\/tab)?|[↑↓]|\/)\s+to\s+(.+)$/u;
80
111
  /** How far up from the footer a dialog's body can reach. */
81
112
  const MAX_BODY_ROWS = 30;
82
113
  /**
@@ -103,23 +134,31 @@ const MAX_LABEL = 48;
103
134
  * The screen's non-blank rows, the bottom {@link MAX_BODY_ROWS} of them, and
104
135
  * for each whether a blank row sat above it. The parse reads only the rows;
105
136
  * the gaps are for showing the result, since they are how claude splits a
106
- * dialog into its title, command, reason and question.
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.
107
139
  */
108
- function bodyLines(screenText) {
140
+ function bodyLines(screenText, marks) {
109
141
  const lines = [];
110
142
  const gaps = [];
143
+ const styles = [];
144
+ const markRows = marks?.split('\n');
111
145
  let gap = false;
112
- for (const raw of screenText.split('\n')) {
146
+ screenText.split('\n').forEach((raw, row) => {
113
147
  const line = raw.trimEnd();
114
148
  if (!line.trim()) {
115
149
  gap = true;
116
- continue;
150
+ return;
117
151
  }
118
152
  lines.push(line);
119
153
  gaps.push(gap);
154
+ styles.push(markRows?.[row] ?? '');
120
155
  gap = false;
121
- }
122
- return { lines: lines.slice(-MAX_BODY_ROWS), gaps: gaps.slice(-MAX_BODY_ROWS) };
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
+ };
123
162
  }
124
163
  function collapse(text) {
125
164
  return text.replace(/\s+/g, ' ').trim();
@@ -170,6 +209,21 @@ function findListEnd(lines, width) {
170
209
  const index = seam + 1 + list.end + 1;
171
210
  return lines.length - index <= MAX_FOOTER_CHROME ? { index, hints: [] } : null;
172
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
+ }
173
227
  /**
174
228
  * Split the footer into its hints. Returns [] for a line that is prose rather
175
229
  * than chrome, which is what keeps a dialog we cannot drive from being offered
@@ -192,14 +246,18 @@ export function parseKeyHints(footer) {
192
246
  * with no input box means a screen we cannot drive — mid-render, or a dialog
193
247
  * shaped in a way this parser does not know — and the caller should relay it
194
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}).
195
253
  */
196
- export function parseModal(screenText) {
197
- const { lines, gaps } = bodyLines(screenText);
254
+ export function parseModal(screenText, marks) {
255
+ const { lines, gaps, styles } = bodyLines(screenText, marks);
198
256
  if (lines.length === 0)
199
257
  return null;
200
258
  // The terminal's width, near enough: the seam is drawn edge to edge.
201
259
  const width = Math.max(...lines.map((l) => l.length));
202
- const footer = findFooter(lines) ?? findListEnd(lines, width);
260
+ const footer = findFooter(lines) ?? findListEnd(lines, width) ?? findTabbedEnd(lines, styles);
203
261
  if (!footer)
204
262
  return null;
205
263
  const { hints, index: footerIndex } = footer;
@@ -230,6 +288,11 @@ export function parseModal(screenText) {
230
288
  }
231
289
  if (body.length === 0)
232
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;
233
296
  const { options, highlighted, more, rows, list } = extractOptions(body, width);
234
297
  // A cursor we could not resolve into rows is the one state that must not
235
298
  // produce buttons. The footer's `Enter to confirm` would then be the only
@@ -241,16 +304,109 @@ export function parseModal(screenText) {
241
304
  // The title is the first body row that isn't an option — dialogs lead with
242
305
  // their heading, and one that opens straight onto its list has none.
243
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;
244
313
  return {
245
- title: title ? clip(title, 120) : clip(body[0], 120),
314
+ title: strip ? clip(strip.title, 120) : title ? clip(title, 120) : clip(body[0], 120),
246
315
  options,
247
316
  highlighted,
248
317
  more,
249
318
  hints,
250
- body: body.join('\n'),
251
- parts: readParts(body, gaps.slice(seam + 1, footerIndex), list, width),
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),
252
324
  };
253
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
+ }
254
410
  /** Index of the last rule strictly above `before`, or -1 when there is none. */
255
411
  function lastRuleBefore(lines, before) {
256
412
  for (let i = before - 1; i >= 0; i--) {
@@ -272,8 +428,14 @@ function lastRuleBefore(lines, before) {
272
428
  * things are drawn in the rows' column without being rows, and are told
273
429
  * apart by what they say: the `↓ 92 more` of a list that scrolls, and the
274
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.
275
437
  */
276
- function rowRole(line, col) {
438
+ function rowRole(line, col, glyphCol) {
277
439
  if (RULE.test(line))
278
440
  return 'skip';
279
441
  const at = line.search(/\S/);
@@ -283,8 +445,52 @@ function rowRole(line, col) {
283
445
  return 'more';
284
446
  if (at === col)
285
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
+ }
286
451
  return at > col ? 'skip' : 'end';
287
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
+ }
288
494
  /**
289
495
  * The dialog's selectable rows, found from the cursor outwards.
290
496
  *
@@ -303,38 +509,53 @@ function extractOptions(body, width) {
303
509
  if (cursor < 0)
304
510
  return none;
305
511
  const match = CURSOR_ROW.exec(body[cursor]);
306
- const labelCol = match[1].length + 1 + match[2].length;
307
- const indexes = [];
512
+ const glyphCol = match[1].length;
513
+ const labelCol = glyphCol + 1 + match[2].length;
514
+ let indexes = [];
308
515
  // The list's first line, which is its `↑ 2 more` when it has one.
309
516
  let start = cursor;
310
517
  for (let i = cursor - 1; i >= 0; i--) {
311
- const role = rowRole(body[i], labelCol);
518
+ const role = rowRole(body[i], labelCol, glyphCol);
312
519
  if (role === 'end')
313
520
  break;
314
- if (role === 'row')
521
+ if (role === 'row' || role === 'edge')
315
522
  indexes.unshift(i);
523
+ if (role === 'edge')
524
+ noteEdge(more, body[i], glyphCol);
316
525
  if (role === 'more')
317
526
  more.above = Number(MORE_ROWS.exec(body[i])[2]);
318
527
  if (role !== 'skip')
319
528
  start = i;
320
529
  }
321
- // However many rows sit above it is where the cursor is — the arrow walk
322
- // counts rows, not screen lines, and the two differ wherever a description
323
- // or a rule sits between them.
324
- const highlighted = indexes.length;
325
530
  indexes.push(cursor);
326
531
  // Where this walk stops is where the list ends: what hangs under its last
327
532
  // row still belongs to that row.
328
533
  let stop = cursor + 1;
329
534
  for (; stop < body.length; stop++) {
330
- const role = rowRole(body[stop], labelCol);
535
+ const role = rowRole(body[stop], labelCol, glyphCol);
331
536
  if (role === 'end')
332
537
  break;
333
- if (role === 'row')
538
+ if (role === 'row' || role === 'edge')
334
539
  indexes.push(stop);
540
+ if (role === 'edge')
541
+ noteEdge(more, body[stop], glyphCol);
335
542
  if (role === 'more')
336
543
  more.below = Number(MORE_ROWS.exec(body[stop])[2]);
337
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);
338
559
  const options = [];
339
560
  const rows = new Set();
340
561
  indexes.forEach((i, k) => {
@@ -344,10 +565,15 @@ function extractOptions(body, width) {
344
565
  const own = numbered ? numbered[2] : text;
345
566
  const under = body.slice(i + 1, indexes[k + 1] ?? stop)
346
567
  .filter((l) => !RULE.test(l) && !MORE_ROWS.test(l) && !LIST_HEADING.test(l));
568
+ const detail = collapse(under.join(' '));
347
569
  options.push({
348
570
  ...(numbered ? { number: Number(numbered[1]) } : {}),
349
571
  label: clip(own, MAX_LABEL),
350
- ...readRow(body[i], own, under, width),
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)),
351
577
  index: options.length,
352
578
  });
353
579
  });
@@ -373,6 +599,17 @@ function readRow(row, own, under, width) {
373
599
  const detail = collapse([...beside, ...under.slice(n)].join(' '));
374
600
  return { text: collapse(label), ...(detail ? { detail } : {}) };
375
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
+ }
376
613
  /**
377
614
  * Read the body into what a chat can show, in the order claude drew it.
378
615
  *
@@ -397,7 +634,9 @@ function readParts(body, gaps, list, width) {
397
634
  open = null;
398
635
  continue;
399
636
  }
400
- if (RULE.test(line)) {
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)) {
401
640
  open = null;
402
641
  continue;
403
642
  }
@@ -411,7 +650,7 @@ function readParts(body, gaps, list, width) {
411
650
  open = { kind, lines: [], gapBefore: gaps[i] };
412
651
  parts.push(open);
413
652
  }
414
- open.lines.push(gutter ? gutter[1] : line);
653
+ open.lines.push(gutter ? gutter[1].replace(/\s*│$/u, '') : line);
415
654
  }
416
655
  // The dialog's own prose sits at one indent; what it quotes — a one-line
417
656
  // command, a URL — is indented under it. Only the prose gets rejoined: a
@@ -495,6 +734,12 @@ export function keystrokeFor(key) {
495
734
  return ' ';
496
735
  if (/^[a-z]$/.test(key))
497
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';
498
743
  // ↑/↓, ←/→ and / are navigation or search affordances, not answers.
499
744
  return null;
500
745
  }
@@ -118,6 +118,18 @@ const RULE = /^\s*─{10,}\s*$/u;
118
118
  */
119
119
  const INPUT_ROW = /^\s*(?:│\s*)?❯/u;
120
120
  const SIDE_BORDERED_INPUT = /│\s*❯/u;
121
+ /**
122
+ * The box's row in shell mode. A `!` typed or pasted first flips the editor:
123
+ * the `❯` gives way to the `!`, drawn apart from the command (`! echo hi` for a
124
+ * pasted `!echo hi`), and Enter runs the command itself. Only {@link
125
+ * inputBoxText} reads it. {@link hasInputBox} deliberately does not: a prompt
126
+ * pasted into a box in this state would run as a command.
127
+ *
128
+ * Anchored at the first column, where the box draws it. The footer under the
129
+ * box reads ` ! for shell mode` in this state, indented, and it sits right
130
+ * under a rule as well.
131
+ */
132
+ const SHELL_INPUT_ROW = /^!(?:\s|$)/u;
121
133
  /** Rows from the bottom that can hold the input box and its footer. */
122
134
  const INPUT_BOX_ROWS = 10;
123
135
  /**
@@ -308,6 +320,18 @@ export function newPastePlaceholder(known, screenText) {
308
320
  * use. Only the opening rule is required, not the closing one, because a
309
321
  * wrapped input pushes that several rows down.
310
322
  *
323
+ * A box in shell mode counts, with its `!` row in the `❯` row's place. It holds
324
+ * a `!command` of ours between the paste and the Enter, and not reading it was
325
+ * how every such command longer than a word was reported as never having
326
+ * reached the editor, with the command sitting in the box on screen.
327
+ *
328
+ * One more row wears the glyph under a rule: the first of the menu claude
329
+ * draws beneath the box while a command is typed. Being last, it was taken
330
+ * for the box, and what it says is the command's own name. So `/config` was
331
+ * found in it and went through, while a command typed by its alias was not:
332
+ * under `/allowed-tools` the menu reads `❯ /permissions (allowed-tools)`,
333
+ * and the alias was reported as never having reached the editor.
334
+ *
311
335
  * Undefined when no box is drawn at all — an overlay has replaced it, or the
312
336
  * TUI is still starting. That is not the same answer as an empty box, and a
313
337
  * caller about to send a keystroke needs to tell them apart.
@@ -317,14 +341,62 @@ export function inputBoxText(screenText) {
317
341
  let inputRow = -1;
318
342
  for (let i = 0; i < lines.length; i++) {
319
343
  const line = lines[i];
344
+ // The command menu's marked row has the glyph and a rule right above it
345
+ // as well: the rule is the box's own closing one.
346
+ if (inputRow === i - 2 && inputRow >= 0 && COMMAND_MENU_ROW.test(line))
347
+ continue;
320
348
  if (SIDE_BORDERED_INPUT.test(line)
321
- || (INPUT_ROW.test(line) && RULE.test(lines[i - 1] ?? '')))
349
+ || ((INPUT_ROW.test(line) || SHELL_INPUT_ROW.test(line)) && RULE.test(lines[i - 1] ?? '')))
322
350
  inputRow = i;
323
351
  }
324
352
  if (inputRow === -1)
325
- return undefined;
353
+ return boxAboveCommandMenu(screenText);
326
354
  return lines.slice(inputRow).join('\n');
327
355
  }
356
+ /**
357
+ * A row of the menu claude draws under the box while a slash command is being
358
+ * typed: the commands the text so far could mean, the likeliest marked.
359
+ *
360
+ * ❯ /config
361
+ * ────────────────────────────────────────
362
+ * ❯ /config Open settings
363
+ * /update-config Change settings: hooks, permissions, environment
364
+ * variables
365
+ *
366
+ * A command with other names lists them after its own:
367
+ * `❯ /permissions (allowed-tools) Manage allow and deny …`.
368
+ */
369
+ const COMMAND_MENU_ROW = /^\s{2}(?:❯ | {2})\/\S+(?: \([^)]*\))?(?:\s{2,}\S.*)?$/u;
370
+ /** The rest of a menu row's description, wrapped under it in the descriptions' column. */
371
+ const COMMAND_MENU_WRAP = /^\s{20,}\S/u;
372
+ /** Rows a command menu can run to. Measured: ten, for a `/config` that seven other commands also matched. */
373
+ const COMMAND_MENU_ROWS = 24;
374
+ /**
375
+ * The input box when a command menu has pushed it out of the bottom rows.
376
+ *
377
+ * The menu takes the footer's place and can be longer than {@link
378
+ * INPUT_BOX_ROWS}: `/config`, on a machine whose plugins add commands with
379
+ * "config" in their names, drew ten rows of it. The box was then above the
380
+ * rows read, so a command typed into it was reported as never having arrived,
381
+ * and `/config` could not be sent at all.
382
+ *
383
+ * Looking further up is only safe for this one shape, which is why it is
384
+ * spelled out: a `❯` row between two rules, and nothing under it but menu
385
+ * rows. A box from an earlier frame, still in the buffer above a dialog,
386
+ * has that dialog under it instead.
387
+ */
388
+ function boxAboveCommandMenu(screenText) {
389
+ const lines = tailLines(screenText, INPUT_BOX_ROWS + COMMAND_MENU_ROWS);
390
+ for (let i = lines.length - 3; i >= 1; i--) {
391
+ if (!INPUT_ROW.test(lines[i]) || !RULE.test(lines[i - 1]) || !RULE.test(lines[i + 1]))
392
+ continue;
393
+ const menu = lines.slice(i + 2);
394
+ const isMenu = COMMAND_MENU_ROW.test(menu[0])
395
+ && menu.every((line) => COMMAND_MENU_ROW.test(line) || COMMAND_MENU_WRAP.test(line));
396
+ return isMenu ? lines[i] : undefined;
397
+ }
398
+ return undefined;
399
+ }
328
400
  /** What a row break can add between two characters of one line: spacing and side borders. */
329
401
  const WRAP_FILLER = /[\s│]+/gu;
330
402
  /**
@@ -74,6 +74,15 @@ export function dialogResumeState(answeredAt, { generating, inflightTools, now,
74
74
  * like telecoder_ask_user long-polls for a button tap and the pty is silent
75
75
  * by design for as long as it waits, which is otherwise indistinguishable
76
76
  * from a finished turn.
77
+ *
78
+ * Nor is a screen that says finished proof of it. Claude can pause for a
79
+ * second or two in the middle of writing a reply with no spinner in the rows
80
+ * the check reads, and the reply reaches the log only once the whole message
81
+ * is in. A turn on claude 2.1.311 was ended that way 0.2s before claude wrote
82
+ * its reply and fired Stop: the log held the prompt and nothing after it, the
83
+ * chat got an empty "Done", and the answer was never sent. So without Stop,
84
+ * the log has to show the turn's end too, and only a long quiet — a Stop hook
85
+ * lost on its way — ends a turn that has neither.
77
86
  */
78
87
  export function turnHold(s) {
79
88
  // Stop is authoritative. All it waits for is the screen to settle.
@@ -86,6 +95,11 @@ export function turnHold(s) {
86
95
  }
87
96
  // Everything says finished, which is also how the moment after a dialog is
88
97
  // answered looks. That one is not finished.
89
- return s.resumingAfterDialog ? 'resuming' : null;
98
+ if (s.resumingAfterDialog)
99
+ return 'resuming';
100
+ // And how a pause in the middle of a reply looks.
101
+ if (!s.stopWaitOver && !s.turnEndedInLog())
102
+ return 'stop';
103
+ return null;
90
104
  }
91
105
  //# sourceMappingURL=turn-end.js.map
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Tracks assistant prose already delivered to Telegram *during* a PTY turn.
3
3
  *
4
- * PTY mode has no streaming text channel. The turn's prose is scraped from
4
+ * A pty has no streaming text channel. The turn's prose is scraped from
5
5
  * Claude Code's JSONL log at end-of-turn (readLastAssistantTurnText) and posted
6
6
  * as one message. That is fine right up until the model stops mid-turn to ask a
7
7
  * question: everything it wrote to explain the choice is still sitting