@yanzc/claude-pro 0.8.0 → 0.9.1

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 -318
  2. package/package.json +1 -2
  3. package/src/agent.js +80 -14
  4. package/src/agents.js +3 -2
  5. package/src/api.js +39 -34
  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 +169 -67
  14. package/src/compaction.js +75 -0
  15. package/src/config.js +145 -144
  16. package/src/context.js +45 -18
  17. package/src/filestate.js +3 -2
  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 +7 -63
  27. package/src/ratelimit.js +2 -4
  28. package/src/remote.js +76 -14
  29. package/src/runtime.js +33 -10
  30. package/src/selfcheck.js +3 -3
  31. package/src/session.js +93 -3
  32. package/src/supervisor.js +10 -1
  33. package/src/tools/bash.js +18 -17
  34. package/src/tools/browser.js +365 -0
  35. package/src/tools/code.js +430 -0
  36. package/src/tools/effort.js +3 -4
  37. package/src/tools/files.js +368 -45
  38. package/src/tools/index.js +9 -1
  39. package/src/tools/notebook.js +11 -4
  40. package/src/tools/processes.js +274 -0
  41. package/src/tools/search.js +33 -8
  42. package/src/tools/web.js +316 -46
  43. package/src/trace.js +0 -4
  44. package/src/ui/ansi.js +92 -13
  45. package/src/ui/dialogs.js +16 -54
  46. package/src/ui/editor.js +231 -34
  47. package/src/ui/markdown.js +1 -2
  48. package/src/ui/render.js +1 -2
  49. package/src/ui/repl.js +335 -101
  50. package/src/ui/screen.js +139 -31
  51. package/src/updater.js +135 -165
  52. package/src/utils/clipboard.js +5 -3
  53. package/src/utils/dirs.js +136 -0
  54. package/src/utils/encoding.js +23 -2
  55. package/src/utils/paths.js +15 -17
  56. package/src/utils/shell.js +66 -11
  57. package/.env.example +0 -29
package/src/ui/dialogs.js CHANGED
@@ -1,5 +1,5 @@
1
- // Modal dialogs rendered in the live region: selection menus (permission prompts,
2
- // plan approval, questions, session picker) and a free-text prompt.
1
+ // Modal dialogs rendered in the live region: selection menus (questions, the model and session pickers, /rewind),
2
+ // with a typed answer of their own when they offer "Other".
3
3
 
4
4
  import { c, padEnd, stringWidth, truncate, wordWrap } from './ansi.js';
5
5
  import { Editor } from './editor.js';
@@ -17,52 +17,10 @@ function box(lines, width, color) {
17
17
  return out;
18
18
  }
19
19
 
20
- // A question answered with typed text: Enter submits, Esc cancels (shift/alt+Enter starts a new line when
21
- // `multiline`). On the remote page it is a text box ({input: {placeholder, value, multiline}}, see App.remoteDialog).
22
- export class TextDialog {
23
- constructor({ title, body = [], question, placeholder = '', value = '', multiline = false, color = c.blue, onResolve, cancelValue = null }) {
24
- this.title = title;
25
- this.body = body;
26
- this.question = question;
27
- this.color = color;
28
- this.onResolve = onResolve;
29
- this.cancelValue = cancelValue;
30
- this.input = { placeholder, multiline };
31
- this.editor = new Editor();
32
- if (value) this.editor.set(value, value.length);
33
- }
34
-
35
- get value() { return this.editor.text; }
36
-
37
- resolve(value) {
38
- const cb = this.onResolve;
39
- this.onResolve = null;
40
- cb?.(value);
41
- }
42
-
43
- handleKey(str, key = {}) {
44
- if (key.name === 'escape') { this.resolve(this.cancelValue); return true; }
45
- if (key.name === 'return' && !key.meta && !key.shift) { this.resolve(this.editor.text); return true; }
46
- if (key.name === 'return' && !this.input.multiline) return true;
47
- this.editor.handleKey(str, key);
48
- return true; // modal: everything else is the editor's
49
- }
50
-
51
- render(width) {
52
- const lines = [];
53
- if (this.title) lines.push(c.bold(this.color(this.title)), '');
54
- lines.push(...this.body);
55
- if (this.body.length) lines.push('');
56
- if (this.question) lines.push(this.question, '');
57
- const inner = Math.max(20, width - 1) - 4;
58
- const r = this.editor.render(width - 4, { placeholder: this.input.placeholder || 'Type the answer' });
59
- const top = lines.length + 1; // +1 for the box's top border
60
- lines.push(...r.lines.map((l) => truncate(l, inner)));
61
- const cursor = r.cursor ? { row: top + r.cursor.row, col: 2 + r.cursor.col } : null;
62
- const out = box(lines, width, this.color);
63
- out.push(c.dim(` Enter to submit${this.input.multiline ? ' · shift+Enter for a new line' : ''} · Esc to cancel`));
64
- return { lines: out, cursor };
65
- }
20
+ // The rows `lines` take inside box(), which wraps the long ones.
21
+ function boxRows(lines, width) {
22
+ const inner = Math.max(20, width - 1) - 4;
23
+ return lines.reduce((n, l) => n + (stringWidth(l) > inner ? wordWrap(l, inner).length : 1), 0);
66
24
  }
67
25
 
68
26
  export class SelectDialog {
@@ -93,8 +51,8 @@ export class SelectDialog {
93
51
  if (this.other) {
94
52
  if (key.name === 'escape') { this.other = null; return true; }
95
53
  if (key.name === 'return' && !key.meta) {
96
- const text = this.other.text.trim();
97
- if (text) this.resolve(this.multi ? [...[...this.checked].map((i) => this.options[i].value), text] : text);
54
+ const text = this.other.expanded().trim(); // a long paste is a placeholder in the text
55
+ if (text) this.resolve(this.multi ? [...this.checkedValues(), text] : text);
98
56
  return true;
99
57
  }
100
58
  return this.other.handleKey(str, key);
@@ -124,14 +82,18 @@ export class SelectDialog {
124
82
  return true;
125
83
  }
126
84
  if (this.multi) {
127
- const values = this.checked.size ? [...this.checked].sort().map((i) => this.options[i].value) : [opt.value];
128
- this.resolve(values);
85
+ this.resolve(this.checked.size ? this.checkedValues() : [opt.value]);
129
86
  } else {
130
87
  this.resolve(opt.value);
131
88
  }
132
89
  return true;
133
90
  }
134
91
 
92
+ // The checked options' values, in the order they are shown (a plain sort() put option 10 before option 2).
93
+ checkedValues() {
94
+ return [...this.checked].sort((a, b) => a - b).map((i) => this.options[i].value);
95
+ }
96
+
135
97
  render(width, maxHeight = 40) {
136
98
  const lines = [];
137
99
  if (this.title) lines.push(c.bold(this.color(this.title)), '');
@@ -141,7 +103,7 @@ export class SelectDialog {
141
103
  const inner = Math.max(20, width - 1) - 4;
142
104
  // Show a window of options around the selection when they don't all fit.
143
105
  const perOption = this.options.some((o) => o.description) ? 2 : 1;
144
- const room = Math.max(3, Math.floor((maxHeight - lines.length - 4 - (this.other ? 5 : 0)) / perOption));
106
+ const room = Math.max(3, Math.floor((maxHeight - boxRows(lines, width) - 4 - (this.other ? 5 : 0)) / perOption));
145
107
  let first = 0;
146
108
  if (this.options.length > room) first = Math.min(Math.max(0, this.selected - Math.floor(room / 2)), this.options.length - room);
147
109
  const last = Math.min(this.options.length, first + room);
@@ -159,7 +121,7 @@ export class SelectDialog {
159
121
  if (this.other) {
160
122
  lines.push('');
161
123
  const r = this.other.render(width - 4, { placeholder: 'Type your answer, Enter to submit, Esc to go back' });
162
- const top = lines.length + 1; // +1 for the box's top border
124
+ const top = boxRows(lines, width) + 1; // the rows above it once box() has wrapped the long ones, and its top border
163
125
  lines.push(...r.lines.map((l) => truncate(l, inner)));
164
126
  if (r.cursor) cursor = { row: top + r.cursor.row, col: 2 + r.cursor.col };
165
127
  }
package/src/ui/editor.js CHANGED
@@ -4,6 +4,26 @@ import { c, graphemes, graphemeWidth, stringWidth, truncate, padEnd } from './an
4
4
 
5
5
  const PASTE_THRESHOLD_CHARS = 800;
6
6
  const PASTE_THRESHOLD_LINES = 8;
7
+ const QUOTE_PREVIEW_LINES = 3; // of a quote, shown in the transcript (see quotePreview)
8
+
9
+ // What the model gets for a quote (see Editor.insertQuote): the text, and the line of the conversation it is under.
10
+ export function quoteBlock(text, source = '') {
11
+ return `<quote${source ? ` source="${source.replace(/"/g, '&quot;')}"` : ''}>\n${text}\n</quote>`;
12
+ }
13
+
14
+ // How a quote shows in the transcript: where it is from, then its first lines, each behind a bar.
15
+ export function quotePreview(text, source = '') {
16
+ const lines = text.split('\n');
17
+ const shown = lines.slice(0, QUOTE_PREVIEW_LINES).map((l) => `│ ${l.length > 200 ? `${l.slice(0, 199)}…` : l}`);
18
+ if (lines.length > QUOTE_PREVIEW_LINES) shown.push(`│ … +${lines.length - QUOTE_PREVIEW_LINES} lines`);
19
+ return [...(source ? [`Quoting ${source}${/[::]$/.test(source) ? '' : ':'}`] : []), ...shown].join('\n');
20
+ }
21
+
22
+ // A message as it was sent, each quote in it (see quoteBlock) as the transcript shows it (see quotePreview): for a
23
+ // conversation shown again, resumed or on the web page.
24
+ export function shownQuotes(text) {
25
+ return String(text).replace(/<quote(?: source="([^"]*)")?>\n([\s\S]*?)\n<\/quote>/g, (_, source = '', body) => quotePreview(body, source.replace(/&quot;/g, '"')));
26
+ }
7
27
 
8
28
  // "╭─ left ───── right ─╮", the parts cut short to fit (the start of the directory goes first).
9
29
  export function topBorder(boxW, color, title) {
@@ -54,9 +74,10 @@ function wordLeft(text, i) {
54
74
  }
55
75
 
56
76
  // How `body` is shown in a box `textW` columns wide: its rows, each with the cursor stops on it ({i: index in
57
- // body, col}). A long line wraps onto more rows; every index has exactly one stop (one where a row wraps is at the
58
- // start of the next row, the end of a line is where its newline is). The input box draws these rows, and up and
59
- // down move between them, so that they move through a long wrapped line too.
77
+ // body, col, g: the character there}; no g: a newline or the end). A long line wraps onto more rows; every index
78
+ // has exactly one stop (one where a row wraps is at the start of the next row, the end of a line is where its
79
+ // newline is). The input box draws these rows, and up and down move between them, so that they move through a
80
+ // long wrapped line too.
60
81
  export function layoutRows(body, textW) {
61
82
  const row = () => ({ text: '', width: 0, stops: [] });
62
83
  const rows = [row()];
@@ -69,7 +90,7 @@ export function layoutRows(body, textW) {
69
90
  } else {
70
91
  const w = graphemeWidth(g);
71
92
  if (r.width && r.width + w > textW) { r = row(); rows.push(r); }
72
- r.stops.push({ i: idx, col: r.width });
93
+ r.stops.push({ i: idx, col: r.width, g });
73
94
  r.text += g === '\t' ? ' ' : g;
74
95
  r.width += w;
75
96
  }
@@ -81,6 +102,9 @@ export function layoutRows(body, textW) {
81
102
  return rows;
82
103
  }
83
104
 
105
+ // A history entry's text: an entry is the text, or {text, parts} when it had placeholders (see Editor.submit).
106
+ export const historyText = (entry) => (typeof entry === 'string' ? entry : entry?.text);
107
+
84
108
  function wordRight(text, i) {
85
109
  let j = i;
86
110
  while (j < text.length && /\s/.test(text[j])) j++;
@@ -105,15 +129,17 @@ export class Editor {
105
129
  this.pasteCounter = 0;
106
130
  this.images = new Map(); // "[Image #1]" -> {media_type, data}
107
131
  this.imageCounter = 0;
132
+ this.quotes = new Map(); // "[Quote #1 +3 lines]" -> how it shows in the transcript (its text is in `pastes`)
133
+ this.quoteCounter = 0;
108
134
  this.imageFromPath = imageFromPath; // (path) => image | null, for pasted/dropped image files
109
- this.pasting = false;
110
- this.pasteBuf = '';
111
135
  this.pendingSubmit = null;
112
136
  this.lastEscape = 0;
113
137
  this.suggestionsDismissedFor = null;
114
138
  this.onDoubleEscape = onDoubleEscape;
115
139
  this.textW = 76; // the width of the text in the box, as last drawn: up and down move by the rows shown
116
140
  this.goalCol = null; // the column up and down aim for, kept while they are pressed in a row
141
+ this.selection = null; // {from, to}: text selected with the mouse (see mouse), indexes into `text`
142
+ this.press = null; // the mouse button held down on the text: {at, moved}
117
143
  }
118
144
 
119
145
  get mode() {
@@ -125,15 +151,115 @@ export class Editor {
125
151
  set(text, cursor = text.length) {
126
152
  this.text = text;
127
153
  this.cursor = Math.max(0, Math.min(cursor, text.length));
154
+ this.selection = null;
128
155
  this.refreshSuggestions();
129
156
  this.onChange?.();
130
157
  }
131
158
 
159
+ // ------------------------------------------------------------ the mouse (fullscreen)
160
+ //
161
+ // As in Claude Code: a click on the text puts the cursor there. A drag selects from the cell it started in to the
162
+ // one it is over, both included (as in the conversation, see Screen.mouse); a double click selects a word, a
163
+ // triple click the line. A selection is there to be copied (ctrl+c, see App.onKey): any other key clears it and
164
+ // then does what it does, so typing does not replace it (there is no undo to bring the text back).
165
+
166
+ // The place in the text under a cell of the rows shown (see place): `row` among them, `col` the column in the
167
+ // text. {i, end}: the index of the character drawn in that cell, or, for a cell past the text of the row, of the
168
+ // row's end (end: true), where a click there puts the cursor.
169
+ at(row, col) {
170
+ const { rows, offset } = this.place();
171
+ const r = rows[Math.max(0, Math.min(rows.length - 1, (this.viewTop || 0) + row))]; // the rows shown start at viewTop
172
+ const x = Math.max(0, col);
173
+ for (const st of r.stops) {
174
+ if (st.g !== undefined && x >= st.col && x < st.col + graphemeWidth(st.g)) return { i: offset + st.i, end: false };
175
+ }
176
+ const tail = r.stops[r.stops.length - 1]; // the newline, the end, or (a row that wraps) its last character
177
+ return { i: offset + tail.i + (tail.g?.length ?? 0), end: true };
178
+ }
179
+
180
+ // A mouse event on the text (see Screen.inputAt): {kind: 'press' | 'drag' | 'release', row, col, count}.
181
+ mouse({ kind, row, col, count = 1 }) {
182
+ const at = this.at(row, col);
183
+ if (kind === 'press') {
184
+ this.press = null;
185
+ this.selection = null;
186
+ if (count >= 3) this.selection = this.lineRange(at);
187
+ else if (count === 2) this.selection = this.wordRange(at);
188
+ else this.press = { at, moved: false };
189
+ } else if (!this.press) {
190
+ return;
191
+ } else if (kind === 'drag') {
192
+ const a = this.press.at;
193
+ this.press.moved = true;
194
+ // the cells in order; of two at the same index, the one past the end of a row comes first (it holds nothing)
195
+ const [lo, hi] = at.i < a.i || (at.i === a.i && at.end) ? [at, a] : [a, at];
196
+ const to = hi.end ? hi.i : nextBoundary(this.text, hi.i);
197
+ this.selection = to > lo.i ? { from: lo.i, to } : null;
198
+ } else { // released: without a drag, a click
199
+ const click = !this.press.moved;
200
+ this.press = null;
201
+ if (click) { this.goalCol = null; this.set(this.text, at.i); return; }
202
+ }
203
+ this.onChange?.();
204
+ }
205
+
206
+ // The word (what is between spaces, as in the conversation) under `at`, or null.
207
+ wordRange(at) {
208
+ const t = this.text;
209
+ const offset = this.mode === 'prompt' ? 0 : 1;
210
+ const word = (ch) => ch !== undefined && !/\s/.test(ch);
211
+ if (at.end || !word(t[at.i])) return null;
212
+ let from = at.i;
213
+ let to = at.i;
214
+ while (from > offset && word(t[from - 1])) from--;
215
+ while (to < t.length && word(t[to])) to++;
216
+ return { from, to };
217
+ }
218
+
219
+ // The line `at` is on, without its newline, or null when it is empty.
220
+ lineRange(at) {
221
+ const from = Math.max(this.mode === 'prompt' ? 0 : 1, lineStartOf(this.text, at.i));
222
+ const nl = this.text.indexOf('\n', at.i);
223
+ const to = nl < 0 ? this.text.length : nl;
224
+ return to > from ? { from, to } : null;
225
+ }
226
+
227
+ // The text selected, pasted text in it as what its placeholder stands for (see expanded).
228
+ selectedText() {
229
+ if (!this.selection) return '';
230
+ let t = this.text.slice(this.selection.from, this.selection.to);
231
+ for (const [label, content] of this.pastes) t = t.split(label).join(content);
232
+ return t;
233
+ }
234
+
235
+ clearSelection() {
236
+ if (!this.selection && !this.press) return false;
237
+ this.selection = null;
238
+ this.press = null;
239
+ this.onChange?.();
240
+ return true;
241
+ }
242
+
243
+ // A row of the rows shown (see layoutRows) with what is selected in reverse video, its newline as a space.
244
+ paintRow(row, offset) {
245
+ const { from, to } = this.selection;
246
+ let out = '';
247
+ let on = false;
248
+ for (const st of row.stops) {
249
+ const sel = offset + st.i >= from && offset + st.i < to;
250
+ if (st.g === undefined && !sel) continue; // a newline, drawn as a space when selected (the end of the text never is)
251
+ if (sel !== on) { out += sel ? '\x1b[7m' : '\x1b[27m'; on = sel; }
252
+ out += st.g === undefined ? (row.width < this.textW ? ' ' : '') : st.g === '\t' ? ' ' : st.g;
253
+ }
254
+ return on ? out + '\x1b[27m' : out;
255
+ }
256
+
132
257
  clear() {
133
258
  this.set('');
134
259
  this.pastes = new Map();
135
260
  this.images = new Map();
136
261
  this.imageCounter = 0;
262
+ this.quotes = new Map();
137
263
  this.historyIndex = null;
138
264
  }
139
265
 
@@ -141,19 +267,31 @@ export class Editor {
141
267
  // with its pasted text and images, even after other messages were sent meanwhile.
142
268
  discard() {
143
269
  if (this.text.trim()) {
144
- this.cleared = { text: this.text, cursor: this.cursor, pastes: this.pastes, images: this.images, imageCounter: this.imageCounter };
270
+ this.cleared = { text: this.text, cursor: this.cursor, pastes: this.pastes, images: this.images, imageCounter: this.imageCounter, quotes: this.quotes };
145
271
  }
146
272
  this.clear();
147
273
  }
148
274
 
149
- // The rows the text is shown in (see layoutRows), and the row and column of the cursor among them.
275
+ // The rows the text is shown in (see layoutRows), and the row and column of the cursor among them. The rows are
276
+ // laid out again only when the text or the width changed: this runs for every frame (a 3000-line draft took 64 ms
277
+ // a frame, laid out and searched whole), and the cursor's row is found by bisection.
150
278
  place() {
151
279
  const offset = this.mode === 'prompt' ? 0 : 1; // the ! or # is drawn as the prompt sign, not as text
152
- const rows = layoutRows(this.text.slice(offset), this.textW);
280
+ const L = this.layout;
281
+ let rows;
282
+ if (L && L.text === this.text && L.textW === this.textW && L.offset === offset) rows = L.rows;
283
+ else {
284
+ rows = layoutRows(this.text.slice(offset), this.textW);
285
+ this.layout = { text: this.text, textW: this.textW, offset, rows };
286
+ }
153
287
  const at = Math.max(0, this.cursor - offset);
154
- let r = 0;
288
+ let r = 0; // the last row that starts at or before the cursor (every row has a stop)
289
+ for (let hi = rows.length - 1; r < hi;) {
290
+ const mid = (r + hi + 1) >> 1;
291
+ if (rows[mid].stops[0].i <= at) r = mid; else hi = mid - 1;
292
+ }
155
293
  let col = 0;
156
- rows.forEach((row, ri) => { for (const st of row.stops) if (st.i <= at) { r = ri; col = st.col; } });
294
+ for (const st of rows[r].stops) if (st.i <= at) col = st.col;
157
295
  return { rows, r, col, offset };
158
296
  }
159
297
 
@@ -169,6 +307,23 @@ export class Editor {
169
307
  return true;
170
308
  }
171
309
 
310
+ // What the draft is made of besides its text, for the next process of a restart: the pasted text, images and
311
+ // quotes its placeholders stand for (those still in it), and the numbers the next ones get. Without them the draft
312
+ // came back as bare placeholders, and "[Pasted text #1 +40 lines]" was what the model got.
313
+ draftParts() {
314
+ const inText = (m) => [...m].filter(([label]) => this.text.includes(label));
315
+ return { pastes: inText(this.pastes), images: inText(this.images), quotes: inText(this.quotes), pasteCounter: this.pasteCounter, imageCounter: this.imageCounter, quoteCounter: this.quoteCounter };
316
+ }
317
+
318
+ restoreDraft(text, cursor, parts) {
319
+ const pairs = (v) => (Array.isArray(v) ? v.filter((p) => Array.isArray(p) && typeof p[0] === 'string' && p[1] != null) : []);
320
+ this.pastes = new Map(pairs(parts?.pastes));
321
+ this.images = new Map(pairs(parts?.images));
322
+ this.quotes = new Map(pairs(parts?.quotes));
323
+ for (const key of ['pasteCounter', 'imageCounter', 'quoteCounter']) if (Number.isFinite(parts?.[key])) this[key] = parts[key];
324
+ this.set(text, cursor);
325
+ }
326
+
172
327
  restoreCleared() {
173
328
  const d = this.cleared;
174
329
  if (!d || this.text || this.historyIndex !== null) return false;
@@ -176,10 +331,28 @@ export class Editor {
176
331
  this.pastes = d.pastes;
177
332
  this.images = d.images;
178
333
  this.imageCounter = d.imageCounter;
334
+ this.quotes = d.quotes || new Map();
179
335
  this.set(d.text, d.cursor);
180
336
  return true;
181
337
  }
182
338
 
339
+ // A part of the conversation quoted in the message (see App.quoteSelection), as ChatGPT's "Ask ChatGPT" does:
340
+ // "[Quote #1 +12 lines]" on a line of its own, the cursor on the line after it, where the question goes. What is
341
+ // sent has the text itself in a <quote>, with the line of the conversation it is under as its source (see
342
+ // quoteBlock), so that the model knows which part is meant; the transcript shows its first lines (see displayed).
343
+ // Deleted like a paste: one backspace takes the whole placeholder.
344
+ insertQuote(text, source = '') {
345
+ const lines = text.split('\n').length;
346
+ const label = `[Quote #${++this.quoteCounter} +${lines} line${lines === 1 ? '' : 's'}]`;
347
+ this.pastes.set(label, quoteBlock(text, source));
348
+ this.quotes.set(label, quotePreview(text, source));
349
+ const before = this.text.slice(0, this.cursor);
350
+ const newline = this.text.slice(this.cursor).startsWith('\n');
351
+ this.insert((before && !before.endsWith('\n') ? '\n' : '') + label + (newline ? '' : '\n'));
352
+ if (newline) this.set(this.text, this.cursor + 1);
353
+ return label;
354
+ }
355
+
183
356
  insertImage(image) {
184
357
  const label = `[Image #${++this.imageCounter}]`;
185
358
  this.images.set(label, image);
@@ -218,6 +391,13 @@ export class Editor {
218
391
  return t;
219
392
  }
220
393
 
394
+ // The text as the transcript shows it: as expanded(), but each quote as its first lines (see quotePreview).
395
+ displayed() {
396
+ let t = this.text;
397
+ for (const [label, content] of this.pastes) t = t.split(label).join(this.quotes.get(label) ?? content);
398
+ return t;
399
+ }
400
+
221
401
  refreshSuggestions() {
222
402
  if (!this.getSuggestions || this.suggestionsDismissedFor === this.text) { this.suggestions = null; return; }
223
403
  const s = this.getSuggestions(this.text, this.cursor);
@@ -240,11 +420,17 @@ export class Editor {
240
420
  submit() {
241
421
  const value = this.expanded();
242
422
  const raw = this.text;
423
+ // `display`: how the message shows in the transcript, when that is not `value` (a quote shows its first lines)
424
+ const display = [...this.quotes.keys()].some((label) => raw.includes(label)) ? this.displayed() : undefined;
243
425
  // Only images whose placeholder is still in the text are sent.
244
426
  const images = [...this.images].filter(([label]) => raw.includes(label)).map(([, img]) => img);
245
- if (raw.trim() && this.history[this.history.length - 1] !== raw) this.history.push(raw);
427
+ // The history keeps what the placeholders stand for: up brought back "[Pasted text #1 +20 lines]" alone, and
428
+ // sending it again sent those words.
429
+ const parts = this.draftParts();
430
+ const entry = parts.pastes.length || parts.images.length || parts.quotes.length ? { text: raw, parts } : raw;
431
+ if (raw.trim() && historyText(this.history[this.history.length - 1]) !== raw) this.history.push(entry);
246
432
  this.clear();
247
- this.onSubmit?.(value, raw, images);
433
+ this.onSubmit?.(value, raw, images, display, typeof entry === 'string' ? null : parts);
248
434
  }
249
435
 
250
436
  // Returns true when the key was consumed.
@@ -255,14 +441,7 @@ export class Editor {
255
441
  this.pendingSubmit = null;
256
442
  this.insert('\n');
257
443
  }
258
- if (key.name === 'paste-start') { this.pasting = true; this.pasteBuf = ''; return true; }
259
- if (key.name === 'paste-end') { this.pasting = false; this.insertPaste(this.pasteBuf); this.pasteBuf = ''; return true; }
260
- if (this.pasting) {
261
- if (key.name === 'return' || key.name === 'enter') this.pasteBuf += '\n';
262
- else if (str) this.pasteBuf += str;
263
- return true;
264
- }
265
-
444
+ // A bracketed paste never gets here: App.onKey takes it whole and calls insertPaste.
266
445
  const name = key.name;
267
446
  if (!(name === 'up' || name === 'down' || (key.ctrl && (name === 'p' || name === 'n')))) this.goalCol = null;
268
447
  if (name === 'return' && !key.meta && !key.shift) {
@@ -374,13 +553,15 @@ export class Editor {
374
553
  return false;
375
554
  }
376
555
 
556
+ // Up and down through the history. The draft is kept with what its placeholders stand for, since an entry brings
557
+ // its own (see submit), and comes back whole after the newest entry.
377
558
  historyPrev() {
378
559
  this.goalCol = null;
379
560
  if (!this.history.length) return true;
380
- if (this.historyIndex === null) { this.draft = this.text; this.historyIndex = this.history.length; }
561
+ if (this.historyIndex === null) { this.draft = { text: this.text, cursor: this.cursor, parts: this.draftParts() }; this.historyIndex = this.history.length; }
381
562
  if (this.historyIndex > 0) {
382
563
  this.historyIndex--;
383
- this.set(this.history[this.historyIndex]);
564
+ this.recall(this.history[this.historyIndex]);
384
565
  }
385
566
  return true;
386
567
  }
@@ -391,16 +572,24 @@ export class Editor {
391
572
  this.historyIndex++;
392
573
  if (this.historyIndex >= this.history.length) {
393
574
  this.historyIndex = null;
394
- this.set(this.draft);
575
+ this.restoreDraft(this.draft.text, this.draft.cursor, this.draft.parts);
395
576
  } else {
396
- this.set(this.history[this.historyIndex]);
577
+ this.recall(this.history[this.historyIndex]);
397
578
  }
398
579
  return true;
399
580
  }
400
581
 
582
+ recall(entry) {
583
+ if (typeof entry === 'string') this.restoreDraft(entry, entry.length, null);
584
+ else this.restoreDraft(entry.text, entry.text.length, entry.parts);
585
+ }
586
+
401
587
  // Render as a rounded box. Returns {lines, cursor:{row,col}}.
402
588
  // `title`: {left, right}, shown in the top border (left: the directory; right: the session's name).
403
- render(width, { placeholder = '', active = true, borderColor, title = null } = {}) {
589
+ // `maxRows`: the most rows of text to show. A longer text shows a window of them that follows the cursor (from
590
+ // viewTop), and the bottom border says how many rows are above and below it: the whole box used to be drawn and
591
+ // the screen cut its top off, so that the cursor stood on a row that was not the one typed in.
592
+ render(width, { placeholder = '', active = true, borderColor, title = null, maxRows = Infinity } = {}) {
404
593
  const boxW = Math.max(20, width - 1);
405
594
  const textW = boxW - 6;
406
595
  const mode = this.mode;
@@ -408,19 +597,27 @@ export class Editor {
408
597
  const promptCh = mode === 'bash' ? '!' : mode === 'memory' ? '#' : '>';
409
598
  const body = mode === 'prompt' ? this.text : this.text.slice(1);
410
599
  this.textW = textW;
411
- const { rows: laid, r, col } = this.place();
412
- const rows = laid.map((row) => row.text);
413
- const cur = { row: r, col };
600
+ const { rows: laid, r, col, offset } = this.place();
601
+ const shown = Math.max(1, Math.min(laid.length, Math.floor(maxRows)));
602
+ let top = Math.min(this.viewTop || 0, laid.length - shown); // the window moves only as far as the cursor needs
603
+ if (r < top) top = r;
604
+ else if (r >= top + shown) top = r - shown + 1;
605
+ this.viewTop = top = Math.max(0, top);
414
606
 
415
607
  const lines = [topBorder(boxW, color, title)];
416
- rows.forEach((r, i) => {
417
- let content = r;
608
+ for (let i = top; i < top + shown; i++) {
609
+ let content = laid[i].text;
418
610
  if (!body && i === 0 && placeholder) content = c.dim(truncate(placeholder, textW));
611
+ else if (this.selection) content = this.paintRow(laid[i], offset);
419
612
  const lead = i === 0 ? (mode === 'prompt' ? promptCh : color(promptCh)) + ' ' : ' ';
420
613
  lines.push(color('│') + ' ' + lead + padEnd(content, textW) + ' ' + color('│'));
421
- });
422
- lines.push(color('╰' + '─'.repeat(boxW - 2) + '╯'));
423
- return { lines, cursor: active ? { row: cur.row + 1, col: 4 + cur.col } : null };
614
+ }
615
+ const below = laid.length - top - shown;
616
+ const hidden = [top ? `↑ ${top} more rows` : '', below ? `↓ ${below} more rows` : ''].filter(Boolean).join(' · ');
617
+ const label = hidden && boxW > stringWidth(hidden) + 6 ? ` ${hidden} ` : '';
618
+ lines.push(color('╰─') + c.dim(label) + color('─'.repeat(boxW - 3 - stringWidth(label)) + '╯'));
619
+ // `text`: where the text is in `lines` (its first row, how many, the column it starts at), for the mouse
620
+ return { lines, cursor: active ? { row: r - top + 1, col: 4 + col } : null, text: { row: 1, rows: shown, col: 4 } };
424
621
  }
425
622
 
426
623
  renderSuggestions(width) {
@@ -2,7 +2,7 @@
2
2
  // rendered immediately (tables are buffered until they end) and the in-progress line
3
3
  // can be previewed separately.
4
4
 
5
- import { c, stringWidth, wordWrap, wrapLine, padEnd } from './ansi.js';
5
+ import { c, stringWidth, wordWrap, wrapLine } from './ansi.js';
6
6
 
7
7
  const KEYWORDS = new Set(('abstract as async await break case catch class const continue debugger def default del delete do elif else enum export extends ' +
8
8
  'false final finally fn for from func function go if impl import in instanceof interface is lambda let loop match mod module mut new nil none null ' +
@@ -246,4 +246,3 @@ export function renderMarkdown(text, opts) {
246
246
  return [...s.write(text), ...s.end()];
247
247
  }
248
248
 
249
- export { padEnd };
package/src/ui/render.js CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  import { c, colorEnabled, stringWidth, truncate, padEnd, wordWrap, wrapLine } from './ansi.js';
4
4
  import { displayPath } from '../utils/paths.js';
5
- import { renderMarkdown } from './markdown.js';
6
5
 
7
6
  export const DOT = '⏺';
8
7
  const RESULT_PREFIX = ' ⎿ ';
@@ -45,7 +44,7 @@ function numberWidth(hunks) {
45
44
 
46
45
  export function renderDiff(preview, width, maxLines = 40) {
47
46
  const out = [];
48
- const w = Math.max(20, width - RESULT_INDENT.length - 1);
47
+ const w = Math.max(20, width - 1); // `width` is what is left beside the result's indentation already
49
48
  if (preview.kind === 'create') {
50
49
  const nw = String(preview.lines.length).length;
51
50
  preview.lines.slice(0, maxLines).forEach((line, i) => {