@yanzc/claude-pro 0.11.0 → 0.13.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.
- package/README.md +56 -36
- package/package.json +1 -1
- package/src/agent.js +4 -3
- package/src/browser/session.js +21 -1
- package/src/commands.js +198 -33
- package/src/config.js +17 -2
- package/src/gateway.js +33 -0
- package/src/live.js +148 -0
- package/src/main.js +5 -3
- package/src/peers.js +41 -4
- package/src/providers.js +25 -6
- package/src/remote.js +82 -268
- package/src/runtime.js +36 -11
- package/src/selfcheck.js +3 -3
- package/src/session.js +128 -8
- package/src/supervisor.js +1 -1
- package/src/tools/bash.js +21 -12
- package/src/tools/browser.js +14 -8
- package/src/tools/code.js +18 -11
- package/src/tools/restart.js +1 -1
- package/src/ui/dialogs.js +23 -30
- package/src/ui/editor.js +65 -169
- package/src/ui/repl.js +467 -474
- package/src/ui/screen.js +47 -674
- package/src/updater.js +5 -4
- package/src/web/api.js +881 -0
- package/src/web/app.css +530 -0
- package/src/web/app.js +2188 -0
- package/src/web/data.js +436 -0
- package/src/web/host.js +332 -0
- package/src/web/login.html +67 -0
- package/src/web/logo.svg +1 -0
- package/src/web/page.html +44 -0
- package/src/web/remote-access.js +232 -0
- package/src/web/server.js +476 -0
package/src/ui/editor.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// The prompt input
|
|
1
|
+
// The prompt input: multi-line editing, history, paste handling and Tab completion.
|
|
2
2
|
|
|
3
|
-
import { c,
|
|
3
|
+
import { c, graphemes, graphemeWidth, stringWidth, truncate, padEnd } from './ansi.js';
|
|
4
4
|
|
|
5
5
|
const PASTE_THRESHOLD_CHARS = 800;
|
|
6
6
|
const PASTE_THRESHOLD_LINES = 8;
|
|
@@ -25,29 +25,6 @@ export function shownQuotes(text) {
|
|
|
25
25
|
return String(text).replace(/<quote(?: source="([^"]*)")?>\n([\s\S]*?)\n<\/quote>/g, (_, source = '', body) => quotePreview(body, source.replace(/"/g, '"')));
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
// "╭─ left ───── right ─╮", the parts cut short to fit (the start of the directory goes first).
|
|
29
|
-
export function topBorder(boxW, color, title) {
|
|
30
|
-
const plain = () => color(glyphs.topLeft + '─'.repeat(boxW - 2) + glyphs.topRight);
|
|
31
|
-
if (!title || (!title.left && !title.right)) return plain();
|
|
32
|
-
const room = boxW - 6; // the corners, a dash at each end and a space each side of a part
|
|
33
|
-
const right = title.right ? truncate(title.right, Math.max(0, Math.floor(room / 3))).replace(/\x1b\[0m$/, '') : '';
|
|
34
|
-
const rightPart = right ? `${glyphs.lineGap}${right} ` : '';
|
|
35
|
-
const rightW = stringWidth(rightPart);
|
|
36
|
-
let left = title.left || '';
|
|
37
|
-
const leftRoom = room - rightW - (right && left ? 1 : 0) - 2;
|
|
38
|
-
if (leftRoom < 4) left = '';
|
|
39
|
-
else if (stringWidth(left) > leftRoom) {
|
|
40
|
-
const chars = [...left];
|
|
41
|
-
while (chars.length && stringWidth(chars.join('')) > leftRoom - 1) chars.shift();
|
|
42
|
-
left = '…' + chars.join('');
|
|
43
|
-
}
|
|
44
|
-
if (!left && !right) return plain();
|
|
45
|
-
const leftPart = left ? ` ${left} ` : '';
|
|
46
|
-
const dashes = boxW - 4 - stringWidth(leftPart) - stringWidth(rightPart);
|
|
47
|
-
if (dashes < 1) return plain();
|
|
48
|
-
return color(glyphs.topLeft + '─') + c.dim(leftPart) + color('─'.repeat(dashes)) + (right ? c.blue(rightPart) : '') + color('─' + glyphs.topRight);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
28
|
// Start of the line the cursor is on. (lastIndexOf with a negative start still looks at index 0, so a
|
|
52
29
|
// text that begins with a newline made the cursor at 0 appear to be on its second line.)
|
|
53
30
|
function lineStartOf(text, cursor) {
|
|
@@ -134,12 +111,10 @@ export class Editor {
|
|
|
134
111
|
this.imageFromPath = imageFromPath; // (path) => image | null, for pasted/dropped image files
|
|
135
112
|
this.pendingSubmit = null;
|
|
136
113
|
this.lastEscape = 0;
|
|
137
|
-
this.suggestionsDismissedFor = null;
|
|
138
114
|
this.onDoubleEscape = onDoubleEscape;
|
|
139
115
|
this.textW = 76; // the width of the text in the box, as last drawn: up and down move by the rows shown
|
|
140
116
|
this.goalCol = null; // the column up and down aim for, kept while they are pressed in a row
|
|
141
|
-
this.
|
|
142
|
-
this.press = null; // the mouse button held down on the text: {at, moved}
|
|
117
|
+
this.completing = false; // Tab opened the completion list: it follows what is typed until it is closed
|
|
143
118
|
}
|
|
144
119
|
|
|
145
120
|
get mode() {
|
|
@@ -151,110 +126,12 @@ export class Editor {
|
|
|
151
126
|
set(text, cursor = text.length) {
|
|
152
127
|
this.text = text;
|
|
153
128
|
this.cursor = Math.max(0, Math.min(cursor, text.length));
|
|
154
|
-
this.
|
|
155
|
-
this.refreshSuggestions();
|
|
129
|
+
if (this.completing) this.refreshSuggestions();
|
|
156
130
|
this.onChange?.();
|
|
157
131
|
}
|
|
158
132
|
|
|
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
|
-
|
|
257
133
|
clear() {
|
|
134
|
+
this.closeSuggestions();
|
|
258
135
|
this.set('');
|
|
259
136
|
this.pastes = new Map();
|
|
260
137
|
this.images = new Map();
|
|
@@ -336,23 +213,6 @@ export class Editor {
|
|
|
336
213
|
return true;
|
|
337
214
|
}
|
|
338
215
|
|
|
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
|
-
|
|
356
216
|
insertImage(image) {
|
|
357
217
|
const label = `[Image #${++this.imageCounter}]`;
|
|
358
218
|
this.images.set(label, image);
|
|
@@ -398,13 +258,47 @@ export class Editor {
|
|
|
398
258
|
return t;
|
|
399
259
|
}
|
|
400
260
|
|
|
261
|
+
// The completion list again for the text as it is now (while it is open: see complete). It closes when nothing
|
|
262
|
+
// matches any more.
|
|
401
263
|
refreshSuggestions() {
|
|
402
|
-
if (!this.
|
|
403
|
-
const s = this.getSuggestions(this.text, this.cursor);
|
|
264
|
+
if (!this.completing || !this.getSuggestions) return;
|
|
265
|
+
const s = this.getSuggestions(this.text, this.cursor, { quiet: true });
|
|
404
266
|
this.suggestions = s && s.items.length ? s : null;
|
|
267
|
+
if (!this.suggestions) this.completing = false;
|
|
405
268
|
if (!this.suggestions || this.selected >= this.suggestions.items.length) this.selected = 0;
|
|
406
269
|
}
|
|
407
270
|
|
|
271
|
+
closeSuggestions() {
|
|
272
|
+
this.completing = false;
|
|
273
|
+
this.suggestions = null;
|
|
274
|
+
this.selected = 0;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Tab, as in a shell: one match is put in at once; several put in what they all start with and open the list,
|
|
278
|
+
// which Tab and down then go through (Enter takes the one selected, Esc closes it).
|
|
279
|
+
complete() {
|
|
280
|
+
if (this.suggestions) { this.selected = (this.selected + 1) % this.suggestions.items.length; this.onChange?.(); return; }
|
|
281
|
+
const s = this.getSuggestions?.(this.text, this.cursor);
|
|
282
|
+
// nothing (yet: the file list may still be loading, see App's FileIndex, which then completes again)
|
|
283
|
+
this.lastMiss = s?.items.length ? null : { text: this.text, cursor: this.cursor, at: Date.now() };
|
|
284
|
+
if (!s?.items.length) return;
|
|
285
|
+
const typed = this.text.slice(s.from, s.to);
|
|
286
|
+
// what starts with what is typed counts first ("/he": /help, not /bashes, which only contains it)
|
|
287
|
+
const starting = s.items.filter((item) => item.value.toLowerCase().startsWith(typed.toLowerCase()));
|
|
288
|
+
if (starting.length && starting.length < s.items.length) s.items = starting;
|
|
289
|
+
if (s.items.length === 1) { this.suggestions = s; this.selected = 0; this.acceptSuggestion(); return; }
|
|
290
|
+
let common = s.items[0].value;
|
|
291
|
+
for (const item of s.items) while (!item.value.toLowerCase().startsWith(common.toLowerCase())) common = common.slice(0, -1);
|
|
292
|
+
this.completing = true;
|
|
293
|
+
if (common.length > typed.length && common.toLowerCase().startsWith(typed.toLowerCase())) {
|
|
294
|
+
this.set(this.text.slice(0, s.from) + common + this.text.slice(s.to), s.from + common.length); // reopens the list
|
|
295
|
+
} else {
|
|
296
|
+
this.suggestions = s;
|
|
297
|
+
this.selected = 0;
|
|
298
|
+
this.onChange?.();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
408
302
|
acceptSuggestion() {
|
|
409
303
|
const s = this.suggestions;
|
|
410
304
|
if (!s) return null;
|
|
@@ -412,8 +306,8 @@ export class Editor {
|
|
|
412
306
|
const before = this.text.slice(0, s.from);
|
|
413
307
|
const after = this.text.slice(s.to);
|
|
414
308
|
const value = item.value + (item.noSpace ? '' : ' ');
|
|
309
|
+
this.closeSuggestions();
|
|
415
310
|
this.set(before + value + after.replace(/^ /, ''), before.length + value.length);
|
|
416
|
-
this.suggestions = null;
|
|
417
311
|
return item;
|
|
418
312
|
}
|
|
419
313
|
|
|
@@ -463,11 +357,16 @@ export class Editor {
|
|
|
463
357
|
return true;
|
|
464
358
|
}
|
|
465
359
|
if (name === 'tab' && !key.shift) {
|
|
466
|
-
|
|
360
|
+
this.complete();
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
if (name === 'tab' && this.suggestions) { // shift+tab: back up the list
|
|
364
|
+
this.selected = (this.selected - 1 + this.suggestions.items.length) % this.suggestions.items.length;
|
|
365
|
+
this.onChange?.();
|
|
467
366
|
return true;
|
|
468
367
|
}
|
|
469
368
|
if (name === 'escape') {
|
|
470
|
-
if (this.
|
|
369
|
+
if (this.completing) { this.closeSuggestions(); this.onChange?.(); return true; }
|
|
471
370
|
const now = Date.now();
|
|
472
371
|
const twice = key.meta || now - this.lastEscape < 800; // meta: ESC ESC arriving in one chunk
|
|
473
372
|
if (twice && this.text) { this.discard(); this.lastEscape = 0; return true; }
|
|
@@ -584,42 +483,37 @@ export class Editor {
|
|
|
584
483
|
else this.restoreDraft(entry.text, entry.text.length, entry.parts);
|
|
585
484
|
}
|
|
586
485
|
|
|
587
|
-
//
|
|
588
|
-
//
|
|
486
|
+
// Draw the input without a box: "> " before its first row, two spaces before the others (the ! or # of bash and
|
|
487
|
+
// memory mode take the place of ">"). Returns {lines, cursor: {row, col}}.
|
|
589
488
|
// `maxRows`: the most rows of text to show. A longer text shows a window of them that follows the cursor (from
|
|
590
|
-
// viewTop), and
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
const boxW = Math.max(20, width - 1);
|
|
594
|
-
const textW = boxW - 6;
|
|
489
|
+
// viewTop), and a line under it says how many rows are above and below.
|
|
490
|
+
render(width, { placeholder = '', active = true, maxRows = Infinity } = {}) {
|
|
491
|
+
const textW = Math.max(10, width - 1 - 2);
|
|
595
492
|
const mode = this.mode;
|
|
596
|
-
const color =
|
|
493
|
+
const color = mode === 'bash' ? c.magenta : mode === 'memory' ? c.blue : c.claude;
|
|
597
494
|
const promptCh = mode === 'bash' ? '!' : mode === 'memory' ? '#' : '>';
|
|
598
495
|
const body = mode === 'prompt' ? this.text : this.text.slice(1);
|
|
599
496
|
this.textW = textW;
|
|
600
|
-
const { rows: laid, r, col
|
|
497
|
+
const { rows: laid, r, col } = this.place();
|
|
601
498
|
const shown = Math.max(1, Math.min(laid.length, Math.floor(maxRows)));
|
|
602
499
|
let top = Math.min(this.viewTop || 0, laid.length - shown); // the window moves only as far as the cursor needs
|
|
603
500
|
if (r < top) top = r;
|
|
604
501
|
else if (r >= top + shown) top = r - shown + 1;
|
|
605
502
|
this.viewTop = top = Math.max(0, top);
|
|
606
503
|
|
|
607
|
-
const lines = [
|
|
504
|
+
const lines = [];
|
|
608
505
|
for (let i = top; i < top + shown; i++) {
|
|
609
506
|
let content = laid[i].text;
|
|
610
507
|
if (!body && i === 0 && placeholder) content = c.dim(truncate(placeholder, textW));
|
|
611
|
-
|
|
612
|
-
const lead = i === 0 ? (mode === 'prompt' ? promptCh : color(promptCh)) + ' ' : ' ';
|
|
613
|
-
lines.push(color('│') + ' ' + lead + padEnd(content, textW) + ' ' + color('│'));
|
|
508
|
+
lines.push((i === 0 ? color(promptCh) + ' ' : ' ') + content);
|
|
614
509
|
}
|
|
615
510
|
const below = laid.length - top - shown;
|
|
616
511
|
const hidden = [top ? `↑ ${top} more rows` : '', below ? `↓ ${below} more rows` : ''].filter(Boolean).join(' · ');
|
|
617
|
-
|
|
618
|
-
lines
|
|
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 } };
|
|
512
|
+
if (hidden) lines.push(c.dim(` ${hidden}`));
|
|
513
|
+
return { lines, cursor: active ? { row: r - top, col: 2 + col } : null };
|
|
621
514
|
}
|
|
622
515
|
|
|
516
|
+
// The completion list (Tab): a window of 8 items around the selected one.
|
|
623
517
|
renderSuggestions(width) {
|
|
624
518
|
if (!this.suggestions) return [];
|
|
625
519
|
const items = this.suggestions.items;
|
|
@@ -627,11 +521,13 @@ export class Editor {
|
|
|
627
521
|
const start = Math.max(0, Math.min(this.selected - Math.floor(max / 2), items.length - max));
|
|
628
522
|
const shown = items.slice(start, start + max);
|
|
629
523
|
const labelW = Math.min(30, Math.max(...shown.map((i) => stringWidth(i.label))) + 2);
|
|
630
|
-
|
|
524
|
+
const lines = shown.map((item, k) => {
|
|
631
525
|
const sel = start + k === this.selected;
|
|
632
|
-
const label = padEnd(truncate(item.label, labelW), labelW);
|
|
526
|
+
const label = padEnd(truncate(item.label, labelW - 2), labelW);
|
|
633
527
|
const desc = item.description ? truncate(item.description, Math.max(10, width - labelW - 6)) : '';
|
|
634
528
|
return sel ? ' ' + c.permission(label) + c.permission(desc) : ' ' + c.gray(label) + c.dim(desc);
|
|
635
529
|
});
|
|
530
|
+
if (items.length > max) lines.push(c.dim(` ${this.selected + 1}/${items.length} · tab/↓ next · ⏎ choose · esc close`));
|
|
531
|
+
return lines;
|
|
636
532
|
}
|
|
637
533
|
}
|