@hmharness/cli 0.14.10 → 0.14.12

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 (3) hide show
  1. package/dist/tui.d.ts +51 -9
  2. package/dist/tui.js +167 -24
  3. package/package.json +1 -1
package/dist/tui.d.ts CHANGED
@@ -64,6 +64,29 @@ export declare function splitMouseReport(data: string): {
64
64
  data: string;
65
65
  pending: string;
66
66
  };
67
+ /**
68
+ * Bracketed-paste body cleanup: strip the 200~/201~ markers and any escape
69
+ * sequences (pasted terminal controls must not leak into the input line),
70
+ * and collapse newlines to single spaces - a multi-line paste becomes ONE
71
+ * reviewable input instead of auto-submitting each line as its own command.
72
+ * Capped so a runaway paste cannot balloon the input field.
73
+ */
74
+ export declare function sanitizePaste(raw: string): string;
75
+ /**
76
+ * /copy clipboard candidates for the platform/session: Wayland first when
77
+ * WAYLAND_DISPLAY is set (xclip does not exist on a pure wl session), then
78
+ * X11's xclip with xsel as fallback. Empty when nothing plausible applies.
79
+ * The caller tries them in order until one spawns; the final "output
80
+ * follows" degrade stays for systems with no clipboard utility at all
81
+ * (e.g. the KaihongOS guest terminal).
82
+ */
83
+ export declare function clipboardCandidates(platform: string, env: {
84
+ WAYLAND_DISPLAY?: string;
85
+ DISPLAY?: string;
86
+ }): Array<{
87
+ bin: string;
88
+ args: string[];
89
+ }>;
67
90
  export declare class TuiRuntime {
68
91
  private entries;
69
92
  private dirty;
@@ -97,19 +120,26 @@ export declare class TuiRuntime {
97
120
  private modeTag;
98
121
  /** rows for the `/model ` picker (configured providers first, set by driver) */
99
122
  private modelChoices;
100
- /** Wheel/click handling: SGR mouse reporting is ALWAYS on (enabled with
101
- * ?1000h+?1006h at startup). Cross-terminal wheel support has to come
102
- * from explicit reports: only some terminals translate the wheel to
103
- * arrow keys on the alternate screen, so the wheel would silently do
104
- * nothing on the others (conhost, several Linux terminals, the
105
- * KaihongOS terminal). The wheel drives the transcript (or the open
106
- * palette's selection); clicks only pick rows while a palette is open.
107
- * Native select/copy stays available via Shift+click, the standard
108
- * bypass in mainstream terminals. */
123
+ /** Wheel/click handling (T23, settled design): capture (?1000h+?1006h)
124
+ * is OFF by default - click-drag must stay the terminal's NATIVE
125
+ * selection, always-on capture took that away on every system. The
126
+ * wheel is covered by ?1007 (alternate scroll, enabled at startup):
127
+ * the terminal itself translates wheel -> arrow keys without touching
128
+ * clicks, and the existing arrow path scrolls the transcript. Capture
129
+ * turns on only while a palette is open (clicks choose rows, wheel
130
+ * drives selection) or when the user forces it with /mouse for
131
+ * terminals that honour neither wheel translation nor ?1007 (legacy
132
+ * conhost); Shift+drag still selects on mainstream terminals then. */
109
133
  private mouseReported;
134
+ /** /mouse force flag (persisted as config.json tui.mouse): full-time
135
+ * capture for terminals without any wheel translation */
136
+ private forceMouse;
110
137
  /** tail of an SGR mouse report split across stdin chunks (fast wheel
111
138
  * bursts); prepended to the next chunk so parseWheel sees the sequence */
112
139
  private mousePending;
140
+ /** bracketed-paste accumulator: non-null while the 201~ end marker has
141
+ * not arrived yet; everything buffered is literal insert-on-completion */
142
+ private pasteBuf;
113
143
  /** screen row of each visible palette item (SGR click hit-testing); the
114
144
  * render loop records row = frame.length (1-based) as it pushes rows */
115
145
  private paletteClickRows;
@@ -185,6 +215,15 @@ export declare class TuiRuntime {
185
215
  frameText: string;
186
216
  };
187
217
  setModeTag(tag: string): void;
218
+ /** Capture is on ONLY while a palette is open or /mouse forced it -
219
+ * native selection must stay native everywhere else; ?1007 covers
220
+ * the wheel without any capture (T23). */
221
+ private syncMouseReporting;
222
+ /** /mouse (T23): force full-time capture for terminals that honour
223
+ * neither wheel->arrow translation nor ?1007. While forced, Shift+drag
224
+ * is the mainstream-terminal escape hatch back to selection. */
225
+ setMouseForced(on: boolean): void;
226
+ isMouseForced(): boolean;
188
227
  /** The palette data source: `/model ` opens the model picker, otherwise
189
228
  * slash commands. (/resume submits straight through to the driver, which
190
229
  * opens the Codex-style full-frame picker - resume-picker.ts.) Rows are
@@ -253,6 +292,9 @@ export declare class TuiRuntime {
253
292
  clearScreen(): void;
254
293
  private totalLines;
255
294
  private onKey;
295
+ /** insert literal (sanitized paste) text at the caret - the same path
296
+ * printable typing takes, so caret/history/palette side-effects match */
297
+ private insertText;
256
298
  render(): void;
257
299
  private flushFrame;
258
300
  }
package/dist/tui.js CHANGED
@@ -111,6 +111,7 @@ export const COMMANDS = [
111
111
  { name: '/new', key: 'cmdNew' },
112
112
  { name: '/fork', key: 'cmdFork' },
113
113
  { name: '/copy', key: 'cmdCopy' },
114
+ { name: '/mouse', key: 'cmdMouse' },
114
115
  { name: '/plan', key: 'cmdPlan' },
115
116
  { name: '/goal', key: 'cmdGoal' },
116
117
  { name: '/usage', key: 'cmdUsage' },
@@ -243,6 +244,43 @@ export function splitMouseReport(data) {
243
244
  ? { data, pending: '' }
244
245
  : { data: data.slice(0, cut), pending: tail };
245
246
  }
247
+ /**
248
+ * Bracketed-paste body cleanup: strip the 200~/201~ markers and any escape
249
+ * sequences (pasted terminal controls must not leak into the input line),
250
+ * and collapse newlines to single spaces - a multi-line paste becomes ONE
251
+ * reviewable input instead of auto-submitting each line as its own command.
252
+ * Capped so a runaway paste cannot balloon the input field.
253
+ */
254
+ export function sanitizePaste(raw) {
255
+ return raw
256
+ .replace(/\x1b\[[0-9;?]*[a-zA-Z~]/g, '')
257
+ .replace(/\x1b/g, '')
258
+ .replace(/\r\n|\r|\n/g, ' ')
259
+ .replace(/[ \t]+$/, '')
260
+ .slice(0, 262144);
261
+ }
262
+ /**
263
+ * /copy clipboard candidates for the platform/session: Wayland first when
264
+ * WAYLAND_DISPLAY is set (xclip does not exist on a pure wl session), then
265
+ * X11's xclip with xsel as fallback. Empty when nothing plausible applies.
266
+ * The caller tries them in order until one spawns; the final "output
267
+ * follows" degrade stays for systems with no clipboard utility at all
268
+ * (e.g. the KaihongOS guest terminal).
269
+ */
270
+ export function clipboardCandidates(platform, env) {
271
+ if (platform === 'win32')
272
+ return [{ bin: 'clip', args: [] }];
273
+ if (platform === 'darwin')
274
+ return [{ bin: 'pbcopy', args: [] }];
275
+ const out = [];
276
+ if (env.WAYLAND_DISPLAY)
277
+ out.push({ bin: 'wl-copy', args: [] });
278
+ if (env.DISPLAY) {
279
+ out.push({ bin: 'xclip', args: ['-selection', 'clipboard'] });
280
+ out.push({ bin: 'xsel', args: ['--clipboard', '--input'] });
281
+ }
282
+ return out;
283
+ }
246
284
  export class TuiRuntime {
247
285
  entries = [];
248
286
  dirty = true;
@@ -276,19 +314,26 @@ export class TuiRuntime {
276
314
  modeTag = '';
277
315
  /** rows for the `/model ` picker (configured providers first, set by driver) */
278
316
  modelChoices = [];
279
- /** Wheel/click handling: SGR mouse reporting is ALWAYS on (enabled with
280
- * ?1000h+?1006h at startup). Cross-terminal wheel support has to come
281
- * from explicit reports: only some terminals translate the wheel to
282
- * arrow keys on the alternate screen, so the wheel would silently do
283
- * nothing on the others (conhost, several Linux terminals, the
284
- * KaihongOS terminal). The wheel drives the transcript (or the open
285
- * palette's selection); clicks only pick rows while a palette is open.
286
- * Native select/copy stays available via Shift+click, the standard
287
- * bypass in mainstream terminals. */
288
- mouseReported = true;
317
+ /** Wheel/click handling (T23, settled design): capture (?1000h+?1006h)
318
+ * is OFF by default - click-drag must stay the terminal's NATIVE
319
+ * selection, always-on capture took that away on every system. The
320
+ * wheel is covered by ?1007 (alternate scroll, enabled at startup):
321
+ * the terminal itself translates wheel -> arrow keys without touching
322
+ * clicks, and the existing arrow path scrolls the transcript. Capture
323
+ * turns on only while a palette is open (clicks choose rows, wheel
324
+ * drives selection) or when the user forces it with /mouse for
325
+ * terminals that honour neither wheel translation nor ?1007 (legacy
326
+ * conhost); Shift+drag still selects on mainstream terminals then. */
327
+ mouseReported = false;
328
+ /** /mouse force flag (persisted as config.json tui.mouse): full-time
329
+ * capture for terminals without any wheel translation */
330
+ forceMouse = false;
289
331
  /** tail of an SGR mouse report split across stdin chunks (fast wheel
290
332
  * bursts); prepended to the next chunk so parseWheel sees the sequence */
291
333
  mousePending = '';
334
+ /** bracketed-paste accumulator: non-null while the 201~ end marker has
335
+ * not arrived yet; everything buffered is literal insert-on-completion */
336
+ pasteBuf = null;
292
337
  /** screen row of each visible palette item (SGR click hit-testing); the
293
338
  * render loop records row = frame.length (1-based) as it pushes rows */
294
339
  paletteClickRows = [];
@@ -316,9 +361,11 @@ export class TuiRuntime {
316
361
  // ?1l forces DECCKM OFF so arrow keys arrive as CSI (\x1b[A) even if a
317
362
  // previous program left the terminal in application cursor mode - in
318
363
  // that mode arrows arrive as SS3 (\x1bOA) and would be silently dropped.
319
- // SGR mouse reporting on from the first frame: the wheel must scroll on
320
- // every terminal, not only the ones that map wheel->arrows themselves.
321
- stdout.write('\x1b[?1049h\x1b[?25l\x1b[2J\x1b[?1l\x1b[?1000h\x1b[?1006h');
364
+ // ?1007 alternate scroll: the terminal translates the wheel to arrow
365
+ // keys itself - no click capture, native selection stays native.
366
+ // Bracketed paste (?2004h): multi-line pastes arrive delimited and
367
+ // insert as one reviewable input instead of auto-submitting per line.
368
+ stdout.write('\x1b[?1049h\x1b[?25l\x1b[2J\x1b[?1l\x1b[?1007h\x1b[?2004h');
322
369
  stdin.setRawMode?.(true);
323
370
  stdin.resume();
324
371
  stdin.setEncoding('utf8');
@@ -566,6 +613,27 @@ export class TuiRuntime {
566
613
  this.modeTag = tag;
567
614
  this.dirty = true;
568
615
  }
616
+ /** Capture is on ONLY while a palette is open or /mouse forced it -
617
+ * native selection must stay native everywhere else; ?1007 covers
618
+ * the wheel without any capture (T23). */
619
+ syncMouseReporting() {
620
+ const want = this.forceMouse || this.panelItems(this.input).length > 0;
621
+ if (want === this.mouseReported)
622
+ return;
623
+ this.mouseReported = want;
624
+ stdout.write(want ? '\x1b[?1000h\x1b[?1006h' : '\x1b[?1000l\x1b[?1006l');
625
+ }
626
+ /** /mouse (T23): force full-time capture for terminals that honour
627
+ * neither wheel->arrow translation nor ?1007. While forced, Shift+drag
628
+ * is the mainstream-terminal escape hatch back to selection. */
629
+ setMouseForced(on) {
630
+ this.forceMouse = on;
631
+ this.syncMouseReporting();
632
+ this.dirty = true;
633
+ }
634
+ isMouseForced() {
635
+ return this.forceMouse;
636
+ }
569
637
  /** The palette data source: `/model ` opens the model picker, otherwise
570
638
  * slash commands. (/resume submits straight through to the driver, which
571
639
  * opens the Codex-style full-frame picker - resume-picker.ts.) Rows are
@@ -597,7 +665,7 @@ export class TuiRuntime {
597
665
  clearInterval(this.spinnerTimer);
598
666
  // ?1l restores default CSI cursor keys; reporting off whatever the
599
667
  // modal state was
600
- stdout.write((this.mouseReported ? '\x1b[?1000l\x1b[?1006l' : '') + '\x1b[?1l\x1b[?25h\x1b[?1049l');
668
+ stdout.write((this.mouseReported ? '\x1b[?1000l\x1b[?1006l' : '') + '\x1b[?2004l\x1b[?1007l' + '\x1b[?1l\x1b[?25h\x1b[?1049l');
601
669
  stdin.setRawMode?.(false);
602
670
  stdin.pause();
603
671
  }
@@ -1055,6 +1123,43 @@ export class TuiRuntime {
1055
1123
  if (!split.data)
1056
1124
  return;
1057
1125
  data = split.data;
1126
+ // bracketed paste (T22): buffer until the 201~ end marker, then insert
1127
+ // the sanitized body at the caret; keys/bytes outside the markers keep
1128
+ // flowing through the normal path (recursion depth stays 1: indexOf
1129
+ // anchors on the FIRST 200~ so the outer chunk cannot contain another)
1130
+ if (this.pasteBuf !== null) {
1131
+ this.pasteBuf += data;
1132
+ const end = this.pasteBuf.indexOf('\x1b[201~');
1133
+ if (end !== -1) {
1134
+ const done = this.pasteBuf;
1135
+ this.pasteBuf = null;
1136
+ this.insertText(sanitizePaste(done));
1137
+ const after = done.slice(end + 6);
1138
+ if (after)
1139
+ this.onKey(after);
1140
+ }
1141
+ else if (this.pasteBuf.length > 1048576) {
1142
+ this.pasteBuf = null; // runaway paste: drop, never balloon the input
1143
+ }
1144
+ return;
1145
+ }
1146
+ const ps = data.indexOf('\x1b[200~');
1147
+ if (ps !== -1) {
1148
+ const before = data.slice(0, ps);
1149
+ if (before)
1150
+ this.onKey(before);
1151
+ this.pasteBuf = data.slice(ps + 6);
1152
+ const end = this.pasteBuf.indexOf('\x1b[201~');
1153
+ if (end !== -1) {
1154
+ const done = this.pasteBuf;
1155
+ this.pasteBuf = null;
1156
+ this.insertText(sanitizePaste(done));
1157
+ const after = done.slice(end + 6);
1158
+ if (after)
1159
+ this.onKey(after);
1160
+ }
1161
+ return;
1162
+ }
1058
1163
  // SS3 application-mode arrows (\x1bOA…H): terminals left in DECCKM by a
1059
1164
  // previous program send these; normalize to the CSI forms this UI
1060
1165
  // matches so navigation never silently dies
@@ -1367,6 +1472,17 @@ export class TuiRuntime {
1367
1472
  // '@' opens the file palette (B2); refreshAtPal() is a no-op otherwise
1368
1473
  this.refreshAtPal();
1369
1474
  }
1475
+ /** insert literal (sanitized paste) text at the caret - the same path
1476
+ * printable typing takes, so caret/history/palette side-effects match */
1477
+ insertText(text) {
1478
+ if (!text)
1479
+ return;
1480
+ this.input = this.input.slice(0, this.caret) + text + this.input.slice(this.caret);
1481
+ this.caret += text.length;
1482
+ this.cmdIdx = 0;
1483
+ this.dirty = true;
1484
+ this.refreshAtPal();
1485
+ }
1370
1486
  /* ---------------- rendering ---------------- */
1371
1487
  render() {
1372
1488
  if (!this.running || !this.dirty)
@@ -1395,8 +1511,9 @@ export class TuiRuntime {
1395
1511
  this.flushFrame(frame, H);
1396
1512
  return;
1397
1513
  }
1514
+ // capture follows palette/force state (see syncMouseReporting, T23);
1398
1515
  // click rows are re-recorded every frame because screen positions move
1399
- // (mouse reporting is always on - see the mouseReported field note)
1516
+ this.syncMouseReporting();
1400
1517
  this.paletteClickRows.length = 0;
1401
1518
  const spin = '⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'[this.spinnerFrame] ?? ' ';
1402
1519
  // Header = pure identity strip: logo · model · cwd · skills (+ mode tag
@@ -1797,6 +1914,10 @@ export async function tui(yes, noWeb = false, opts = {}) {
1797
1914
  }
1798
1915
  };
1799
1916
  applyStatusline();
1917
+ // T23: persisted /mouse force (config.json tui.mouse) - for terminals
1918
+ // that honour neither wheel->arrow translation nor ?1007
1919
+ if (cfg.tui?.mouse)
1920
+ rt.setMouseForced(true);
1800
1921
  // Ctrl+T overlay source: transcript + FULL (folded-away) tool outputs (B5)
1801
1922
  rt.setOverlaySource(() => {
1802
1923
  const out = [];
@@ -2066,21 +2187,43 @@ export async function tui(yes, noWeb = false, opts = {}) {
2066
2187
  rt.addText('(no AI output yet to copy)', 'dim');
2067
2188
  return;
2068
2189
  }
2069
- try {
2070
- const { spawn } = await import('node:child_process');
2071
- const bin = process.platform === 'win32' ? 'clip'
2072
- : process.platform === 'darwin' ? 'pbcopy'
2073
- : 'xclip';
2074
- const args = bin === 'xclip' ? ['-selection', 'clipboard'] : [];
2190
+ const { spawn } = await import('node:child_process');
2191
+ const tryBin = (bin, args) => new Promise((res) => {
2075
2192
  const child = spawn(bin, args, { windowsHide: true, stdio: ['pipe', 'ignore', 'ignore'] });
2193
+ child.on('error', () => res(false));
2194
+ child.stdin.on('error', () => { });
2076
2195
  child.stdin.write(lastAiText);
2077
2196
  child.stdin.end();
2078
- await new Promise((r) => child.on('close', r));
2079
- rt.addText(GREEN('✓') + ' ' + t.cmdCopied, 'plain');
2197
+ child.on('close', (code) => res(code === 0));
2198
+ });
2199
+ // candidate chain (T16 复案): Wayland wl-copy before X11 xclip/xsel -
2200
+ // a single hard-coded xclip is dead on a pure wl session (Omarchy)
2201
+ let copied = false;
2202
+ for (const c of clipboardCandidates(process.platform, process.env)) {
2203
+ if (await tryBin(c.bin, c.args)) {
2204
+ copied = true;
2205
+ break;
2206
+ }
2080
2207
  }
2081
- catch {
2208
+ if (copied)
2209
+ rt.addText(GREEN('✓') + ' ' + t.cmdCopied, 'plain');
2210
+ else
2082
2211
  rt.addText('(clipboard tool unavailable — output follows)\n' + lastAiText.slice(0, 2000), 'dim');
2212
+ return;
2213
+ }
2214
+ if (line === '/mouse') {
2215
+ // T23: full-time capture toggle for terminals without wheel
2216
+ // translation (?1007 already covers the wheel on mainstream ones);
2217
+ // while forced, Shift+drag is the selection escape hatch
2218
+ const next = !rt.isMouseForced();
2219
+ rt.setMouseForced(next);
2220
+ try {
2221
+ const { patchConfig } = await import('@hmharness/kernel');
2222
+ const merged = { ...(cfg.tui ?? {}), mouse: next };
2223
+ cfg = await patchConfig({ tui: merged });
2083
2224
  }
2225
+ catch { /* runtime toggle applied even when persisting fails */ }
2226
+ rt.addText(next ? t.cmdMouseOn : t.cmdMouseOff, 'plain');
2084
2227
  return;
2085
2228
  }
2086
2229
  if (line === '/plan' || line.startsWith('/plan ')) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/cli",
3
- "version": "0.14.10",
3
+ "version": "0.14.12",
4
4
  "description": "hmharness command line: one-shot tasks, an interactive REPL, a fullscreen TUI, the web frontend, and direct tool invocation.",
5
5
  "type": "module",
6
6
  "main": "dist/main.js",