@yanzc/claude-pro 0.9.0 → 0.9.2

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 (57) hide show
  1. package/README.md +342 -320
  2. package/package.json +1 -2
  3. package/src/agent.js +240 -62
  4. package/src/agents.js +3 -2
  5. package/src/api.js +83 -41
  6. package/src/browser/broker.js +367 -0
  7. package/src/browser/cdp.js +114 -0
  8. package/src/browser/render.js +242 -0
  9. package/src/browser/session.js +948 -0
  10. package/src/browser/snapshot.js +212 -0
  11. package/src/cache.js +1 -1
  12. package/src/checkpoints.js +1 -0
  13. package/src/commands.js +177 -71
  14. package/src/compaction.js +75 -0
  15. package/src/config.js +153 -153
  16. package/src/context.js +45 -18
  17. package/src/filestate.js +2 -1
  18. package/src/gateway.js +208 -164
  19. package/src/hooks.js +2 -2
  20. package/src/keepalive.js +1 -1
  21. package/src/main.js +52 -40
  22. package/src/mcp.js +2 -2
  23. package/src/peers.js +1 -10
  24. package/src/print.js +9 -3
  25. package/src/prompt-profiles.js +0 -6
  26. package/src/prompts.js +6 -62
  27. package/src/ratelimit.js +3 -5
  28. package/src/remote.js +26 -10
  29. package/src/restart.js +3 -0
  30. package/src/runtime.js +33 -10
  31. package/src/selfcheck.js +3 -3
  32. package/src/session.js +93 -3
  33. package/src/supervisor.js +10 -1
  34. package/src/tools/bash.js +19 -18
  35. package/src/tools/browser.js +365 -0
  36. package/src/tools/code.js +430 -0
  37. package/src/tools/effort.js +3 -4
  38. package/src/tools/files.js +92 -19
  39. package/src/tools/index.js +6 -0
  40. package/src/tools/notebook.js +11 -4
  41. package/src/tools/processes.js +274 -0
  42. package/src/tools/search.js +104 -21
  43. package/src/tools/web.js +316 -46
  44. package/src/trace.js +0 -4
  45. package/src/ui/ansi.js +92 -13
  46. package/src/ui/dialogs.js +16 -54
  47. package/src/ui/editor.js +231 -34
  48. package/src/ui/markdown.js +1 -2
  49. package/src/ui/render.js +1 -2
  50. package/src/ui/repl.js +246 -119
  51. package/src/ui/screen.js +139 -31
  52. package/src/updater.js +135 -165
  53. package/src/utils/clipboard.js +5 -3
  54. package/src/utils/paths.js +15 -17
  55. package/src/utils/shell.js +75 -21
  56. package/.env.example +0 -29
  57. package/src/verify.js +0 -57
package/src/ui/screen.js CHANGED
@@ -61,6 +61,19 @@ const MULTI_CLICK_MS = 450;
61
61
  const FIND_MATCH = '\x1b[30;43m';
62
62
  const FIND_CURRENT = '\x1b[30;48;5;208m';
63
63
  const FIND_MAX = 10_000;
64
+
65
+ // The text of a line's wrapped rows without colours, all of it and where each row starts in it (see scanFind). It is
66
+ // what paintRow walks through, so offsets in it are the offsets it counts.
67
+ function plainRows(rows) {
68
+ const plains = rows.map((r) => stripAnsi(r));
69
+ const starts = [];
70
+ for (let k = 0, at = 0; k < plains.length; at += plains[k].length, k++) starts.push(at);
71
+ return { text: plains.join(''), starts };
72
+ }
73
+ // A line that starts a part of the conversation: a tool call or a reply ("⏺ …"), a message of the user ("> …"), a
74
+ // command of bash mode ("! …"), a message of another session ("✉ …"). What a quote is from (see selectionQuote).
75
+ const HEADING = /^(?:⏺|>|!|✉) /;
76
+ const SOURCE_REACH = 5000; // lines looked back for it
64
77
  // A row that fills the rest of its width with its background (the user's messages, see renderUserMessage) ends in an
65
78
  // erase to the end of the line made while that background is on. Nothing may erase after it: an erase does not move
66
79
  // the cursor, so a second one, made after the colours are reset, would wipe the band out again from the same column.
@@ -80,6 +93,17 @@ function sliceCols(plain, from, to) {
80
93
  return out;
81
94
  }
82
95
 
96
+ // The text of a selection without the indentation the conversation is drawn with: the first line from where the
97
+ // selection starts, the others less the indentation they all have (a reply's, a tool result's, whose "⎿" counts as
98
+ // a space). Blank lines at either end go.
99
+ export function dedent(text) {
100
+ const lines = String(text).split('\n').map((l) => l.replace(/^(\s*)⎿(?=\s)/, '$1 ').trimEnd());
101
+ let cut = Infinity;
102
+ for (const l of lines.slice(1)) if (l) cut = Math.min(cut, /^ */.exec(l)[0].length);
103
+ if (cut === Infinity) cut = 0;
104
+ return [lines[0].trimStart(), ...lines.slice(1).map((l) => l.slice(cut))].join('\n').replace(/^\n+|\n+$/g, '');
105
+ }
106
+
83
107
  const before = (a, b) => a.line < b.line || (a.line === b.line && (a.sub < b.sub || (a.sub === b.sub && a.col <= b.col)));
84
108
  const MOUSE = /\x1b\[<(\d+);(\d+);(\d+)([Mm])/g;
85
109
 
@@ -99,7 +123,10 @@ export function mouseFilter(stdin, onMouse) {
99
123
  try { onMouse({ button: Number(b), x: Number(x), y: Number(y), release: kind === 'm' }); } catch { /* the listener's problem */ }
100
124
  return '';
101
125
  });
102
- const partial = /\x1b\[<[\d;]*$/.exec(text);
126
+ // the start of a report cut off at the end of the chunk, as far as ESC or ESC [ alone: passed on, those two let
127
+ // the rest of the report ("<0;35;12M") through as typed text. A chunk that is a lone ESC is the Esc key, passed
128
+ // on at once (readline holds it 500 ms already).
129
+ const partial = text === '\x1b' ? null : /\x1b(?:\[(?:<[\d;]*)?)?$/.exec(text);
103
130
  if (partial) {
104
131
  pending = partial[0];
105
132
  text = text.slice(0, partial.index);
@@ -175,7 +202,10 @@ export class Screen {
175
202
  this.dropped = 0; // fullscreen: lines dropped from the top of the history so far (the first one's number)
176
203
  this.selection = null; // fullscreen: {anchor, focus}, points {line, sub, col} of the conversation (history line, wrapped row, column)
177
204
  this.dragging = false;
205
+ this.dragFrom = null; // fullscreen: where a drag in the conversation started, {line, sub, col}
178
206
  this.lastClick = { at: 0, x: 0, y: 0, count: 0 };
207
+ this.inputBox = null; // fullscreen: where the input box's text is on screen, {top, rows, col} (see inputAt)
208
+ this.inputDrag = null; // fullscreen: the mouse button held down on the input box's text: the cell it is over, {x, y, moved}
179
209
  this.cursorRow = 0; // row of the terminal cursor, relative to the top of the live region
180
210
  this.staticQueue = [];
181
211
  this.history = []; // the most recent static lines, for repainting after a resize
@@ -249,7 +279,7 @@ export class Screen {
249
279
  }
250
280
 
251
281
  wrappedAt(i, cols) {
252
- if (this.wrapCols !== cols) { this.wrapped = []; this.wrapCols = cols; this.selection = null; } // its rows are other rows now
282
+ if (this.wrapCols !== cols) { this.wrapped = []; this.wrapCols = cols; this.selection = null; this.dragFrom = null; } // its rows are other rows now
253
283
  if (this.wrapped[i]) return this.wrapped[i];
254
284
  let rows = wrapLine(this.history[i], Math.max(1, cols - 1));
255
285
  // a band now wider than the window: each of its rows starts in its colours and fills the rest of the row
@@ -312,19 +342,21 @@ export class Screen {
312
342
  if (!sel && !hits) return e.text;
313
343
  const fill = e.text.includes(FILL);
314
344
  const base = fill ? LEAD.exec(e.text)?.[0] || '' : '';
315
- const style = (col) => {
345
+ const style = (col, off) => {
316
346
  if (sel && col >= sel[0] && col < sel[1]) return '\x1b[7m';
317
- const hit = hits?.find((h) => col >= h.from && col < h.to);
347
+ const hit = hits?.find((h) => off >= h.from && off < h.to); // a match's place in the row's text (see scanFind)
318
348
  return !hit ? base : hit.match === this.finding.matches[this.finding.index] ? FIND_CURRENT : FIND_MATCH;
319
349
  };
320
350
  let out = '';
321
351
  let open = '';
322
352
  let col = 0;
353
+ let off = 0;
323
354
  for (const g of graphemes(stripAnsi(e.text))) {
324
- const s = style(col);
355
+ const s = style(col, off);
325
356
  if (s !== open) { out += '\x1b[0m' + s; open = s; }
326
357
  out += g;
327
358
  col += graphemeWidth(g);
359
+ off += g.length;
328
360
  }
329
361
  if (fill) return out + '\x1b[0m' + base + FILL;
330
362
  return out + (open ? '\x1b[0m' : '');
@@ -416,7 +448,9 @@ export class Screen {
416
448
  }
417
449
 
418
450
  // The matches of f.query in history lines [from, to), newest first, entered in f.byRow; `rows`: the rows those
419
- // lines take (fromEnd counts from the end of line to - 1).
451
+ // lines take (fromEnd counts from the end of line to - 1). A match's place on a row is kept as offsets in the row's
452
+ // text, which paintRow counts as it draws: columns (stringWidth for every match) took most of the time of a key
453
+ // typed in the search, 130 ms over 20,000 lines. The rows' text without colours is kept with them (plainRows).
420
454
  scanFind(f, from, to, cols) {
421
455
  const re = new RegExp(f.query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'giu');
422
456
  const matches = [];
@@ -424,26 +458,25 @@ export class Screen {
424
458
  for (let line = to - 1; line >= from; line--) {
425
459
  const rows = this.wrappedAt(line, cols);
426
460
  if (matches.length < FIND_MAX) {
427
- const plains = rows.map((r) => stripAnsi(r));
428
- const text = plains.join('');
429
- const starts = [];
430
- for (let k = 0, at = 0; k < plains.length; at += plains[k].length, k++) starts.push(at);
431
- // offset -> {sub, col}: the row it falls on, and the column there (`end`: an end offset belongs to the row before)
432
- const place = (offset, end = false) => {
433
- let sub = plains.length - 1;
461
+ rows.plain ??= plainRows(rows);
462
+ const { text, starts } = rows.plain;
463
+ // offset -> the row it falls on (`end`: an end offset belongs to the row before)
464
+ const rowOf = (offset, end = false) => {
465
+ let sub = starts.length - 1;
434
466
  while (sub > 0 && (end ? starts[sub] >= offset : starts[sub] > offset)) sub--;
435
- return { sub, col: stringWidth(plains[sub].slice(0, offset - starts[sub])) };
467
+ return sub;
436
468
  };
437
469
  const found = [];
438
470
  for (const m of text.matchAll(re)) {
439
471
  if (!m[0]) continue;
440
- const a = place(m.index);
441
- const b = place(m.index + m[0].length, true);
442
- const match = { abs: this.dropped + line, offset: m.index, line, sub: a.sub, col: a.col, fromEnd: rowsAfter + rows.length - 1 - a.sub };
443
- for (let sub = a.sub; sub <= b.sub; sub++) {
472
+ const end = m.index + m[0].length;
473
+ const a = rowOf(m.index);
474
+ const b = rowOf(end, true);
475
+ const match = { abs: this.dropped + line, offset: m.index, line, sub: a, fromEnd: rowsAfter + rows.length - 1 - a };
476
+ for (let sub = a; sub <= b; sub++) {
444
477
  const k = `${line}:${sub}`;
445
478
  if (!f.byRow.has(k)) f.byRow.set(k, []);
446
- f.byRow.get(k).push({ from: sub === a.sub ? a.col : 0, to: sub === b.sub ? b.col : Infinity, match });
479
+ f.byRow.get(k).push({ from: sub === a ? m.index - starts[sub] : 0, to: sub === b ? end - starts[sub] : Infinity, match });
447
480
  }
448
481
  found.push(match);
449
482
  }
@@ -484,6 +517,24 @@ export class Screen {
484
517
  return lines.join('\n');
485
518
  }
486
519
 
520
+ // What is selected, to quote in a message (see App.quoteSelection): {text, source}. `text` without the indentation
521
+ // the conversation is drawn with (see dedent); `source` the line that starts the part of the conversation where the
522
+ // selection begins, a tool call, a reply or a message of the user (see HEADING), so that the model knows where the
523
+ // quote is from: '' when there is none, or when the quote starts with that line itself.
524
+ selectionQuote() {
525
+ if (!this.selection) return null;
526
+ const text = dedent(this.selectedText());
527
+ const [a] = this.ordered();
528
+ let source = '';
529
+ for (let i = Math.min(a.line, this.history.length - 1), n = 0; i >= 0 && n < SOURCE_REACH; i--, n++) {
530
+ const plain = stripAnsi(this.history[i]).trim();
531
+ if (HEADING.test(plain)) { source = plain; break; }
532
+ }
533
+ if (source && text.split('\n', 1)[0] === source) source = '';
534
+ if (source.length > 300) source = source.slice(0, 299) + '…';
535
+ return { text, source };
536
+ }
537
+
487
538
  clearSelection() {
488
539
  if (!this.selection) return false;
489
540
  this.selection = null;
@@ -492,6 +543,19 @@ export class Screen {
492
543
  return true;
493
544
  }
494
545
 
546
+ // The cell of the input box's text at screen cell (x, y), 1-based: {row, col}, the row among the rows of the text
547
+ // and the column in it (negative left of it). null when (x, y) is not on a row of the text shown; `clamp` (a drag
548
+ // that left the box): the start of its first row on screen above it, the end of the text below it.
549
+ inputAt(x, y, clamp = false) {
550
+ const b = this.inputBox;
551
+ if (!b) return null;
552
+ const first = Math.max(0, this.lastArea - b.top); // rows the conversation covers (the box taller than the screen)
553
+ const row = y - 1 - b.top;
554
+ if (row >= first && row < b.rows) return { row, col: x - 1 - b.col };
555
+ if (!clamp) return null;
556
+ return row < first ? { row: first, col: -1 } : { row: b.rows - 1, col: Infinity };
557
+ }
558
+
495
559
  // The point of the conversation at screen cell (x, y), 1-based: the nearest row that shows some of it.
496
560
  pointAt(x, y) {
497
561
  let i = Math.max(0, Math.min(this.lastArea - 1, y - 1));
@@ -519,7 +583,9 @@ export class Screen {
519
583
 
520
584
  // A mouse report (fullscreen). Mouse selection only highlights text; Ctrl+C copies it. Drag to select (the view
521
585
  // scrolls at its edges), double-click a word, triple-click a line; a click clears the selection; a click on the
522
- // "Jump to bottom" bar goes there.
586
+ // "Jump to bottom" bar goes there. On the input box's text it returns {input: {kind, row, col, count}}, for the
587
+ // editor (Editor.mouse): a press there, then a drag (once for each cell the mouse moves to) and the release, from
588
+ // wherever the mouse is by then.
523
589
  mouse({ button, x, y, release }) {
524
590
  if (!this.full || this.closed) return null;
525
591
  if (button & 64) return null; // the wheel: the caller scrolls
@@ -531,35 +597,62 @@ export class Screen {
531
597
  const c = this.lastClick;
532
598
  const count = now - c.at < MULTI_CLICK_MS && Math.abs(c.x - x) <= 1 && c.y === y ? c.count + 1 : 1;
533
599
  this.lastClick = { at: now, x, y, count };
600
+ this.inputDrag = null;
601
+ this.dragFrom = null;
534
602
  if (this.barRow !== null && y - 1 === this.barRow) { this.selection = null; this.scrollBy('bottom'); return null; }
535
- if (y - 1 >= this.lastArea) { this.clearSelection(); return null; } // the input box, the footer
603
+ const cell = this.inputAt(x, y);
604
+ if (cell) {
605
+ this.clearSelection();
606
+ this.dragging = false;
607
+ if (count === 1) this.inputDrag = { x, y, moved: false };
608
+ return { input: { kind: 'press', ...cell, count } };
609
+ }
610
+ if (y - 1 >= this.lastArea) { this.clearSelection(); return null; } // the input box's border, the footer
536
611
  const p = this.pointAt(x, y);
537
612
  if (!p) { this.clearSelection(); return null; }
538
613
  if (count >= 2) {
539
- const sel = this.expand(p, count >= 3);
540
- this.selection = sel;
614
+ this.selection = this.expand(p, count >= 3);
541
615
  this.dragging = false;
542
616
  this.draw();
543
617
  return null;
544
618
  }
545
- this.selection = { anchor: p, focus: { ...p }, moved: false };
619
+ // where a drag starts: the selection is made once the mouse moves, so that a click selects nothing (it showed
620
+ // a cell in reverse video while the button was down, and cleared the footer's message)
621
+ this.clearSelection();
622
+ this.dragFrom = p;
546
623
  this.dragging = true;
547
624
  return null;
548
625
  }
549
- if (!this.dragging || !this.selection) return null;
626
+ const d = this.inputDrag;
627
+ if (d) {
628
+ if (motion && d.x === x && d.y === y) return null; // still the same cell
629
+ const cell = this.inputAt(x, y, true);
630
+ if (release) {
631
+ this.inputDrag = null;
632
+ if (d.moved) this.lastClick.count = 0; // a drag: the next click starts a count of its own
633
+ return cell && { input: { kind: 'release', ...cell } };
634
+ }
635
+ if (!motion) return null;
636
+ Object.assign(d, { x, y, moved: true });
637
+ return cell && { input: { kind: 'drag', ...cell } };
638
+ }
639
+ if (!this.dragging || !this.dragFrom) return null;
550
640
  if (motion) {
551
641
  // past the top or the bottom of the conversation: it scrolls, and the selection goes on with it
552
642
  if (y <= 1) this.scrollBy(1);
553
643
  else if (y - 1 >= this.lastArea - 1 && this.scroll > 0) this.scrollBy(-1);
554
644
  const p = this.pointAt(x, y);
555
- if (p) { this.selection.focus = p; this.selection.moved = true; }
645
+ if (p) {
646
+ if (this.selection) this.selection.focus = p;
647
+ else this.selection = { anchor: { ...this.dragFrom }, focus: p };
648
+ }
556
649
  this.draw();
557
650
  return null;
558
651
  }
559
652
  // released
560
653
  this.dragging = false;
561
- if (!this.selection.moved) { this.clearSelection(); return null; } // a plain click
562
- this.lastClick.count = 0; // a drag: the next click starts a count of its own
654
+ this.dragFrom = null;
655
+ if (this.selection) this.lastClick.count = 0; // a drag: the next click starts a count of its own
563
656
  return null;
564
657
  }
565
658
 
@@ -596,11 +689,13 @@ export class Screen {
596
689
  const gap = rows - current;
597
690
  layout.rows.unshift(...Array(gap).fill(''));
598
691
  if (layout.cursorRow !== null) layout.cursorRow += gap;
692
+ if (layout.box) layout.box.top += gap;
599
693
  return layout;
600
694
  }
601
695
  if (current > rows) {
602
696
  const dropped = current - rows;
603
697
  layout.rows.splice(0, dropped);
698
+ if (layout.box) layout.box.top -= dropped;
604
699
  if (layout.cursorRow !== null) {
605
700
  layout.cursorRow -= dropped;
606
701
  if (layout.cursorRow < 0) layout.cursorRow = null;
@@ -624,6 +719,7 @@ export class Screen {
624
719
  }
625
720
  const area = Math.max(0, screenRows - layout.rows.length);
626
721
  this.lastArea = area;
722
+ this.inputBox = layout.box && { ...layout.box, top: area + layout.box.top };
627
723
  if (this.finding) this.refreshFind();
628
724
  const shown = this.view(area, cols);
629
725
  this.viewMap = shown;
@@ -708,11 +804,17 @@ export class Screen {
708
804
  for (const p of [this.selection.anchor, this.selection.focus]) p.line -= drop;
709
805
  if (this.selection.anchor.line < 0 || this.selection.focus.line < 0) this.selection = null;
710
806
  }
807
+ if (this.dragFrom) {
808
+ this.dragFrom.line -= drop;
809
+ if (this.dragFrom.line < 0) this.dragFrom = null;
810
+ }
711
811
  }
712
812
  return lines;
713
813
  }
714
814
 
715
- // The live region as rows (pre-wrapped to cols-1, capped to fit the viewport) and where the cursor goes.
815
+ // The live region as rows (pre-wrapped to cols-1, capped to fit the viewport) and where the cursor goes. `box`:
816
+ // where the input box's text is among the rows, {top, rows, col}, from what renderLive says (`input`: its first
817
+ // line, how many, the column the text starts at); null when its rows did not stay one row each.
716
818
  layoutLive(cols) {
717
819
  let live;
718
820
  try {
@@ -720,10 +822,12 @@ export class Screen {
720
822
  } catch (e) {
721
823
  live = { lines: [`\x1b[31mrender error: ${e.message}\x1b[0m`] };
722
824
  }
723
- const { lines = [], cursor = null } = live;
825
+ const { lines = [], cursor = null, input = null } = live;
724
826
  const rows = [];
827
+ const starts = []; // the row each line starts on
725
828
  let cursorRow = null, cursorCol = 0;
726
829
  lines.forEach((line, i) => {
830
+ starts.push(rows.length);
727
831
  const wrapped = wrapLine(cleanLine(line), cols - 1);
728
832
  if (cursor && cursor.row === i) {
729
833
  const col = cursor.col;
@@ -733,14 +837,18 @@ export class Screen {
733
837
  }
734
838
  rows.push(...wrapped);
735
839
  });
840
+ starts.push(rows.length);
841
+ const end = input ? starts[input.row + input.rows] : undefined;
842
+ const box = end !== undefined && end - starts[input.row] === input.rows ? { top: starts[input.row], rows: input.rows, col: input.col } : null;
736
843
  // Cap to the terminal height so there is always a way back to the top.
737
844
  const maxRows = Math.max(1, Math.min(this.height, this.out.rows || this.height) - 1);
738
845
  if (rows.length > maxRows) {
739
846
  const dropped = rows.length - maxRows;
740
847
  rows.splice(0, dropped);
741
848
  if (cursorRow !== null) cursorRow = Math.max(0, cursorRow - dropped);
849
+ if (box) box.top -= dropped;
742
850
  }
743
- return { rows, cursorRow, cursorCol };
851
+ return { rows, cursorRow, cursorCol, box };
744
852
  }
745
853
 
746
854
  // Escape sequence that leaves the cursor where the user types, and the bookkeeping for the next redraw.