@yanzc/claude-pro 0.9.1 → 0.9.3
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 +10 -7
- package/package.json +1 -1
- package/src/agent.js +161 -49
- package/src/api.js +44 -7
- package/src/commands.js +9 -5
- package/src/config.js +22 -12
- package/src/main.js +1 -1
- package/src/prompts.js +1 -1
- package/src/ratelimit.js +1 -1
- package/src/restart.js +3 -0
- package/src/runtime.js +10 -4
- package/src/session.js +47 -34
- package/src/tools/bash.js +1 -1
- package/src/tools/search.js +82 -24
- package/src/ui/ansi.js +11 -2
- package/src/ui/repl.js +61 -15
- package/src/ui/screen.js +75 -22
- package/src/utils/shell.js +9 -10
- package/src/verify.js +0 -57
package/src/ui/repl.js
CHANGED
|
@@ -21,7 +21,7 @@ import { CONFIG_DIR, VERSION, APP_NAME, INSTALL_DIR } from '../config.js';
|
|
|
21
21
|
import { gatewayLogin, loadGatewayModels, normalizeGatewayUrl, formatUsd } from '../gateway.js';
|
|
22
22
|
import { TOPIC_TITLE_SYSTEM, topicTitlePrompt } from '../prompts.js';
|
|
23
23
|
import { buildCommands, modelChoices, effortChoices } from '../commands.js';
|
|
24
|
-
import { canRestart, writeRestartState, runSelfCheck, watchSource, restartNote, RESTART_EXIT_CODE, MAX_RESTARTS, RESTART_WINDOW_MS } from '../restart.js';
|
|
24
|
+
import { canRestart, writeRestartState, runSelfCheck, watchSource, restartNote, reloadTiming, RESTART_EXIT_CODE, MAX_RESTARTS, RESTART_WINDOW_MS } from '../restart.js';
|
|
25
25
|
import { resolvePath, displayPath } from '../utils/paths.js';
|
|
26
26
|
import { listDirectory, recentDirectories } from '../utils/dirs.js';
|
|
27
27
|
import { ripgrepPath } from '../utils/shell.js';
|
|
@@ -142,18 +142,38 @@ function appendHistory(cwd, text, parts = null) {
|
|
|
142
142
|
} catch { /* ignore */ }
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
+
// The files of the working directory, for the completion after an `@`. Listed when an `@` is first typed, not at
|
|
146
|
+
// the start, and never more than MAX_FILES of them: the listing is stopped there. (It ran at every start and
|
|
147
|
+
// restart and went through the whole directory before keeping the first 50,000: in a home directory of 1.3
|
|
148
|
+
// million files that was 10 seconds of ripgrep on every core and 45 MB, for every session open there, each time
|
|
149
|
+
// the program's source changed.) `onLoaded` is called when a listing is in, so that the completion is drawn again.
|
|
150
|
+
const MAX_FILES = 50_000;
|
|
145
151
|
class FileIndex {
|
|
146
|
-
constructor(cwd, getConfig = () => null
|
|
152
|
+
constructor(cwd, getConfig = () => null, onLoaded = () => {}) {
|
|
153
|
+
Object.assign(this, { cwd, getConfig, onLoaded, files: [], at: 0, took: 0, loading: false });
|
|
154
|
+
}
|
|
147
155
|
refresh() {
|
|
148
|
-
|
|
156
|
+
// listed again after 30 seconds, or later where the listing itself takes long
|
|
157
|
+
if (this.loading || Date.now() - this.at < Math.max(30_000, this.took * 30)) return;
|
|
149
158
|
this.loading = true;
|
|
150
|
-
const
|
|
159
|
+
const started = Date.now();
|
|
160
|
+
const done = (list) => {
|
|
161
|
+
Object.assign(this, { files: list.slice(0, MAX_FILES), at: Date.now(), took: Date.now() - started, loading: false });
|
|
162
|
+
this.onLoaded();
|
|
163
|
+
};
|
|
151
164
|
const rg = ripgrepPath(this.getConfig());
|
|
152
165
|
if (rg) {
|
|
153
166
|
const child = spawn(rg, ['--files', '--hidden', '--glob', '!.git'], { cwd: this.cwd, windowsHide: true });
|
|
154
167
|
let out = '';
|
|
168
|
+
let lines = 0;
|
|
155
169
|
child.stdout.setEncoding('utf8'); // a Chinese file name can be split between two chunks
|
|
156
|
-
child.stdout.on('data', (d) => {
|
|
170
|
+
child.stdout.on('data', (d) => {
|
|
171
|
+
if (lines >= MAX_FILES) return;
|
|
172
|
+
out += d;
|
|
173
|
+
for (let i = d.indexOf('\n'); i !== -1; i = d.indexOf('\n', i + 1)) lines++;
|
|
174
|
+
if (lines >= MAX_FILES) child.kill();
|
|
175
|
+
});
|
|
176
|
+
// (stopped in the middle of a name: the list is cut to MAX_FILES whole ones in done)
|
|
157
177
|
child.on('close', () => done(out.split(/\r?\n/).filter(Boolean).map((f) => f.replace(/\\/g, '/'))));
|
|
158
178
|
child.on('error', () => done([]));
|
|
159
179
|
} else {
|
|
@@ -162,7 +182,7 @@ class FileIndex {
|
|
|
162
182
|
try {
|
|
163
183
|
for await (const f of fs.promises.glob('**/*', { cwd: this.cwd, exclude: (p) => /(^|[\\/])(node_modules|\.git)$/.test(p) })) {
|
|
164
184
|
list.push(f.replace(/\\/g, '/'));
|
|
165
|
-
if (list.length >
|
|
185
|
+
if (list.length > MAX_FILES) break;
|
|
166
186
|
}
|
|
167
187
|
} catch { /* ignore */ }
|
|
168
188
|
done(list);
|
|
@@ -248,6 +268,8 @@ export class App {
|
|
|
248
268
|
this.restarts = []; // when this session was restarted lately (carried over by each restart)
|
|
249
269
|
this.restartRequest = null; // {reason, then, continueTurn}: restart as soon as nothing is running
|
|
250
270
|
this.sourceChanged = null; // the program's own source changed since it was last checked
|
|
271
|
+
this.sourceChangedInTurn = false; // ...while a turn of this session ran: its own work, most likely
|
|
272
|
+
this.sourceQuietAt = 0; // a change made elsewhere is loaded once the source has been left alone until then
|
|
251
273
|
this.reloading = false;
|
|
252
274
|
this.skipAutoReload = false;
|
|
253
275
|
this.peers = null; // this session as seen by the other sessions on the machine (see peers.js)
|
|
@@ -267,7 +289,7 @@ export class App {
|
|
|
267
289
|
this.remotePeersLoading = null;
|
|
268
290
|
this.remotePeersTimer = null;
|
|
269
291
|
this.cost = 0; // what this conversation's requests have cost so far (USD at list prices): the footer and /cost
|
|
270
|
-
this.fileIndex = new FileIndex(cwd, () => this.runtime?.config);
|
|
292
|
+
this.fileIndex = new FileIndex(cwd, () => this.runtime?.config, () => { this.editor?.refreshSuggestions(); this.screen?.schedule(); });
|
|
271
293
|
this.store = new SessionStore(cwd);
|
|
272
294
|
SessionStore.onError = (e, store) => this.notice(`Could not save the conversation to ${store.file} (${e.code || e.message}): it is written again whole before a restart, but /resume may miss what was not saved.`, 'warn');
|
|
273
295
|
// What the terminal prints goes to the remote page too, except what it is told about as events.
|
|
@@ -365,7 +387,6 @@ export class App {
|
|
|
365
387
|
else if (!gatewayLogin(this.config.gatewayUrl)) {
|
|
366
388
|
Promise.resolve(this.commands.get('login').run('')).catch((e) => this.logError(e)).finally(() => this.screen.schedule());
|
|
367
389
|
}
|
|
368
|
-
this.fileIndex.refresh();
|
|
369
390
|
this.startSourceWatch();
|
|
370
391
|
if (!this.driven) {
|
|
371
392
|
// Updates are deliberately manual: /update checks npm and installs the selected release.
|
|
@@ -453,6 +474,7 @@ export class App {
|
|
|
453
474
|
this.exiting = true;
|
|
454
475
|
this.controller?.abort();
|
|
455
476
|
clearInterval(this.spinnerTimer);
|
|
477
|
+
clearTimeout(this.reloadTimer);
|
|
456
478
|
this.sourceWatch?.close();
|
|
457
479
|
shutdownBackgroundShells();
|
|
458
480
|
shutdownInterpreters();
|
|
@@ -881,7 +903,7 @@ export class App {
|
|
|
881
903
|
// the same pattern and message; this holds when it does not (another gateway, or one that misses them).
|
|
882
904
|
if (TERMINAL_ONLY.test(value)) throw new Error('结束、重启会话和更新 claude-pro 只能在终端里操作');
|
|
883
905
|
if (this.exiting) throw new Error('The session is ending.');
|
|
884
|
-
if (this.isTurnRunning() && value.trim().startsWith('/')) throw new Error('Commands wait until the current task is done (or stopped).');
|
|
906
|
+
if (this.isTurnRunning() && value.trim().startsWith('/') && !this.modelSwitchAtPause(value.trim())) throw new Error('Commands wait until the current task is done (or stopped).');
|
|
885
907
|
if (this.dialogs.length && !this.isTurnRunning()) throw new Error('A question is open: answer it first.');
|
|
886
908
|
const queued = this.isTurnRunning();
|
|
887
909
|
this.onSubmit(value, value, pictures);
|
|
@@ -1128,12 +1150,18 @@ export class App {
|
|
|
1128
1150
|
});
|
|
1129
1151
|
}
|
|
1130
1152
|
|
|
1131
|
-
// Follow the program's own source:
|
|
1132
|
-
// a
|
|
1153
|
+
// Follow the program's own source: a change to it is loaded by a restart, if the new code starts. A change made
|
|
1154
|
+
// while a turn of this session runs is its own work, and is loaded when the turn is over. One that arrives while
|
|
1155
|
+
// the session is idle was made elsewhere (another session working on the program, an editor), one step of a
|
|
1156
|
+
// change that is not finished: it is loaded once the source has been left alone for a few minutes. (Loaded at
|
|
1157
|
+
// once, every edit of one session made each of the other open sessions check and restart, into a half-made
|
|
1158
|
+
// change: 8 of them, a dozen times in a day's work.)
|
|
1133
1159
|
startSourceWatch() {
|
|
1134
1160
|
if (!canRestart()) return;
|
|
1135
1161
|
this.sourceWatch = watchSource(INSTALL_DIR, (file) => {
|
|
1136
1162
|
this.sourceChanged = file || true;
|
|
1163
|
+
if (this.isTurnRunning()) this.sourceChangedInTurn = true;
|
|
1164
|
+
else this.sourceQuietAt = Date.now() + reloadTiming.quietMs + Math.random() * reloadTiming.spreadMs; // (not all sessions at the same moment)
|
|
1137
1165
|
// another window (or npm) installed a new version: say so, and load it once this session is idle. In a
|
|
1138
1166
|
// source checkout a newer version in package.json is a release being made, not an install.
|
|
1139
1167
|
const disk = installedVersion();
|
|
@@ -1150,8 +1178,18 @@ export class App {
|
|
|
1150
1178
|
if (!this.sourceChanged || this.updating || this.reloading || this.isTurnRunning() || this.exiting || this.restartRequest || this.dialogs.length || this.queue.length) return;
|
|
1151
1179
|
if (this.skipAutoReload) { this.skipAutoReload = false; return; } // the user just interrupted: not now
|
|
1152
1180
|
if (this.restartBlockers().length) return; // looked at again whenever the program is next idle
|
|
1181
|
+
clearTimeout(this.reloadTimer);
|
|
1182
|
+
const wait = this.updateRestart || this.sourceChangedInTurn ? 0 : this.sourceQuietAt - Date.now(); // (an installed version is complete)
|
|
1183
|
+
if (wait > 0) {
|
|
1184
|
+
this.reloadTimer = setTimeout(() => this.maybeAutoReload(), wait + 50);
|
|
1185
|
+
this.reloadTimer.unref?.();
|
|
1186
|
+
this.screen.schedule();
|
|
1187
|
+
return;
|
|
1188
|
+
}
|
|
1153
1189
|
const changed = this.sourceChanged;
|
|
1190
|
+
const inTurn = this.sourceChangedInTurn;
|
|
1154
1191
|
this.sourceChanged = null;
|
|
1192
|
+
this.sourceChangedInTurn = false;
|
|
1155
1193
|
this.reloading = true;
|
|
1156
1194
|
this.flash(this.updateRestart ? `Updated to ${this.updateRestart}: checking it starts, then restarting…` : 'Source changed: checking the new code…');
|
|
1157
1195
|
let check;
|
|
@@ -1161,7 +1199,7 @@ export class App {
|
|
|
1161
1199
|
this.notice(`The program's source changed but the new code does not start, so it was not restarted:\n${check.output.split('\n').slice(0, 12).join('\n')}\nFix it; the restart follows by itself (or run /restart).`, 'warn');
|
|
1162
1200
|
return;
|
|
1163
1201
|
}
|
|
1164
|
-
if (this.isTurnRunning() || this.dialogs.length) { this.sourceChanged = changed; return; } // something started meanwhile: later
|
|
1202
|
+
if (this.isTurnRunning() || this.dialogs.length) { this.sourceChanged = changed; this.sourceChangedInTurn = inTurn; return; } // something started meanwhile: later
|
|
1165
1203
|
const r = this.requestRestart({ reason: this.updateRestart ? `updated to ${APP_NAME} ${this.updateRestart}` : 'source changed', force: true });
|
|
1166
1204
|
// refused (too many restarts in a row): say so, or later changes would go unloaded without a word
|
|
1167
1205
|
if (!r.ok) this.notice(`The program's source changed, but it was not restarted to load it: ${r.message.replace(/ That looks like a loop:.*$/, '')} A change after that window loads it, or /restart once it has passed.`, 'warn');
|
|
@@ -1536,7 +1574,7 @@ export class App {
|
|
|
1536
1574
|
});
|
|
1537
1575
|
const items = [...peers, ...fileItems];
|
|
1538
1576
|
if (!items.length) {
|
|
1539
|
-
if (!q && m.index === 0 && !m[1] && !quiet) this.flash(this.remote ? 'No file or session is available here or online at the gateway' : 'No file or other session is available on this computer (/login to the gateway reaches your other computers)');
|
|
1577
|
+
if (!q && m.index === 0 && !m[1] && !quiet && !this.fileIndex.loading) this.flash(this.remote ? 'No file or session is available here or online at the gateway' : 'No file or other session is available on this computer (/login to the gateway reaches your other computers)');
|
|
1540
1578
|
return null;
|
|
1541
1579
|
}
|
|
1542
1580
|
return { items, from: cursor - q.length - (quoted ? 2 : 1), to: cursor };
|
|
@@ -1575,7 +1613,7 @@ export class App {
|
|
|
1575
1613
|
if (this.screen.scrolledUp) this.screen.scrollBy('bottom'); // what was sent, and what comes of it, is in view
|
|
1576
1614
|
appendHistory(this.cwd, raw, parts);
|
|
1577
1615
|
if (this.sendTypedToPeer(text)) return;
|
|
1578
|
-
if (this.isTurnRunning()) {
|
|
1616
|
+
if (this.isTurnRunning() && !this.modelSwitchAtPause(text)) {
|
|
1579
1617
|
if (text.startsWith('/')) { this.notice('Wait for the current task to finish (or press Esc) before running commands.', 'warn'); return; }
|
|
1580
1618
|
this.queue.push({ text: value, images, ...(display ? { display } : {}) });
|
|
1581
1619
|
this.screen.schedule();
|
|
@@ -1601,6 +1639,13 @@ export class App {
|
|
|
1601
1639
|
this.startTurn(value, { display });
|
|
1602
1640
|
}
|
|
1603
1641
|
|
|
1642
|
+
// /model while the turn's request is paused for a rate limit: the one command that runs in a turn. The request
|
|
1643
|
+
// then goes on with the model chosen instead of waiting hours for the limit of the other (the user had to stop
|
|
1644
|
+
// the turn, change the model and say "go on" for that).
|
|
1645
|
+
modelSwitchAtPause(text) {
|
|
1646
|
+
return /^\/model(?:\s|$)/i.test(text) && this.agent.pausedForLimit;
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1604
1649
|
// `@name message`: the message goes to that session, as a task for it. A session on another computer of the
|
|
1605
1650
|
// gateway account: `@machine/name message`, or `@machine message` when that computer runs one session. A known
|
|
1606
1651
|
// file wins only when no session matches; use `@./name` to force a file when the names collide.
|
|
@@ -2192,7 +2237,7 @@ export class App {
|
|
|
2192
2237
|
// Say it once; later waits of the same pause only move the countdown (unless the reset moved a lot).
|
|
2193
2238
|
const prev = this.rateWait;
|
|
2194
2239
|
this.rateWait = { resumeAt: ev.resumeAt, label: ev.label, polling: ev.polling, overloaded: Boolean(ev.overloaded) };
|
|
2195
|
-
if (shouldAnnounce(prev, ev)) this.notice(`${describePause(ev)} Esc cancels.`, 'warn');
|
|
2240
|
+
if (shouldAnnounce(prev, ev)) this.notice(`${describePause(ev)} Esc cancels${!ev.overloaded && this.agent.pauseControl ? '; /model goes on with another model at once' : ''}.`, 'warn');
|
|
2196
2241
|
this.screen.schedule();
|
|
2197
2242
|
}
|
|
2198
2243
|
|
|
@@ -2506,6 +2551,7 @@ export class App {
|
|
|
2506
2551
|
if (!flash) {
|
|
2507
2552
|
if (this.updating) left += c.dim(' · ') + c.blue(`⇣ updating ${APP_NAME} to ${this.updating}…`);
|
|
2508
2553
|
else if (this.updateRestart && this.sourceChanged && !this.restartRequest) left += c.dim(' · ') + c.blue(`↻ ${this.updateRestart} installed: restarts ${this.working() ? 'when the work is done' : 'now'}`);
|
|
2554
|
+
else if (this.sourceChanged && !this.sourceChangedInTurn && !this.restartRequest && !this.reloading && !this.working() && this.sourceQuietAt > Date.now()) left += c.dim(' · ') + c.dim('↻ source changed: restarts once it is left alone (/restart: now)');
|
|
2509
2555
|
else if (this.pendingUpdate) left += c.dim(' · ') + c.dim(`update ${this.pendingUpdate} ready · ${this.updateWaitingFor ? `installs when @${this.updateWaitingFor.split(',')[0]} is idle` : 'installs when idle'}`);
|
|
2510
2556
|
}
|
|
2511
2557
|
const cfg = this.config;
|
package/src/ui/screen.js
CHANGED
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
// of a restart) goes on right below the output.
|
|
30
30
|
|
|
31
31
|
import { PassThrough } from 'node:stream';
|
|
32
|
-
import { ESC, wrapLine, termWidth, truncate, stringWidth, stripAnsi, graphemes, graphemeWidth } from './ansi.js';
|
|
32
|
+
import { ESC, wrapLine, termWidth, truncate, stringWidth, stripAnsi, graphemes, graphemeWidth, setTerminalWidths } from './ansi.js';
|
|
33
33
|
|
|
34
34
|
const SYNC_START = '\x1b[?2026h';
|
|
35
35
|
const SYNC_END = '\x1b[?2026l';
|
|
@@ -136,32 +136,54 @@ export function mouseFilter(stdin, onMouse) {
|
|
|
136
136
|
});
|
|
137
137
|
return out;
|
|
138
138
|
}
|
|
139
|
-
const CPR = /\x1b\[(\d+);(\d+)R
|
|
139
|
+
const CPR = /\x1b\[(\d+);(\d+)R/g; // the terminal's answer to DSR 6: row;column
|
|
140
|
+
const WIDTH_PROBES = process.platform === 'win32' ? Array.from('·…↑↓←→↻⇣⏎◷⇄•◦⏺⎿✻✢✳✶✽✉⚠') : [];
|
|
140
141
|
|
|
141
|
-
// Ask the terminal where the cursor is (DSR 6): resolves with its row (1-based), or null when it does not answer
|
|
142
|
-
//
|
|
143
|
-
//
|
|
142
|
+
// Ask the terminal where the cursor is (DSR 6): resolves with its row (1-based), or null when it does not answer.
|
|
143
|
+
// On Windows, also measure the UI symbols whose width varies between conhosts/fonts. All probes start at column
|
|
144
|
+
// one, fit on this row, and are erased before restoring the cursor. Run before anything else reads raw `stdin`;
|
|
145
|
+
// keys typed meanwhile are put back byte for byte, without the terminal's replies.
|
|
144
146
|
export function queryCursorRow(stdin, out, timeout = 300) {
|
|
145
147
|
return new Promise((resolve) => {
|
|
146
148
|
let buf = Buffer.alloc(0);
|
|
147
|
-
let timer = null;
|
|
148
|
-
const
|
|
149
|
+
let timer = null, probing = false, finished = false;
|
|
150
|
+
const replies = () => [...buf.toString('latin1').matchAll(CPR)];
|
|
151
|
+
const done = () => {
|
|
152
|
+
if (finished) return;
|
|
153
|
+
finished = true;
|
|
149
154
|
clearTimeout(timer);
|
|
150
155
|
stdin.off('data', onData);
|
|
151
156
|
stdin.pause();
|
|
152
|
-
const
|
|
153
|
-
const
|
|
157
|
+
const matches = replies();
|
|
158
|
+
const first = matches[0];
|
|
159
|
+
if (probing) {
|
|
160
|
+
const widths = matches.slice(1).map((m, i) => [WIDTH_PROBES[i], Number(m[2]) - 1]);
|
|
161
|
+
if (matches.length === WIDTH_PROBES.length + 1 && matches.every((m) => m[1] === first[1]) && widths.every(([, w]) => w === 1 || w === 2)) setTerminalWidths(widths);
|
|
162
|
+
try { out.write('\r' + ESC.right(Number(first[2]) - 1) + ESC.showCursor + SYNC_END); } catch { /* terminal closed */ }
|
|
163
|
+
}
|
|
164
|
+
// latin1 keeps a one-to-one mapping of bytes, including keys/pastes split in the middle of a UTF-8 character.
|
|
165
|
+
const rest = Buffer.from(buf.toString('latin1').replace(CPR, ''), 'latin1');
|
|
154
166
|
if (rest.length) stdin.unshift(rest);
|
|
155
|
-
resolve(
|
|
167
|
+
resolve(first ? Number(first[1]) : null);
|
|
156
168
|
};
|
|
157
169
|
const onData = (d) => {
|
|
158
170
|
buf = Buffer.concat([buf, Buffer.isBuffer(d) ? d : Buffer.from(String(d), 'utf8')]);
|
|
159
|
-
const
|
|
160
|
-
if (
|
|
171
|
+
const matches = replies();
|
|
172
|
+
if (!matches.length) return;
|
|
173
|
+
if (!WIDTH_PROBES.length || (out.columns && out.columns < 3)) { done(); return; }
|
|
174
|
+
if (probing) {
|
|
175
|
+
if (matches.length >= WIDTH_PROBES.length + 1) done();
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
probing = true;
|
|
179
|
+
clearTimeout(timer);
|
|
180
|
+
timer = setTimeout(done, timeout);
|
|
181
|
+
const probes = WIDTH_PROBES.map((char) => '\r' + char + '\x1b[6n').join('');
|
|
182
|
+
try { out.write(SYNC_START + ESC.hideCursor + probes + '\r\x1b[K'); } catch { done(); }
|
|
161
183
|
};
|
|
162
|
-
timer = setTimeout(
|
|
184
|
+
timer = setTimeout(done, timeout);
|
|
163
185
|
stdin.on('data', onData);
|
|
164
|
-
try { out.write('\x1b[6n'); } catch { done(
|
|
186
|
+
try { out.write('\x1b[6n'); } catch { done(); }
|
|
165
187
|
});
|
|
166
188
|
}
|
|
167
189
|
|
|
@@ -186,6 +208,7 @@ export class Screen {
|
|
|
186
208
|
// pin: keep the live region on the last rows of the screen (see the top of this file).
|
|
187
209
|
constructor(out = process.stdout, { pin = true } = {}) {
|
|
188
210
|
this.out = out;
|
|
211
|
+
this.dimensions = this.readSize();
|
|
189
212
|
this.pin = pin;
|
|
190
213
|
this.pinned = null; // rows from the top of the live region to the bottom of the screen; null: not known
|
|
191
214
|
this.full = false; // fullscreen: see the top of this file
|
|
@@ -220,6 +243,12 @@ export class Screen {
|
|
|
220
243
|
this.titleInherited = false; // the process before this one in the session already saved the shell's title
|
|
221
244
|
this.exitTitle = '';
|
|
222
245
|
out.on('resize', () => this.onResize());
|
|
246
|
+
// RDP display changes can leave Node's columns/rows cache behind. Check the native size even while idle;
|
|
247
|
+
// finding a change takes the same debounced full-repaint path as a normal resize event.
|
|
248
|
+
if (process.platform === 'win32' && out.isTTY && out._handle?.getWindowSize) {
|
|
249
|
+
this.sizeTimer = setInterval(() => this.checkSize(), 250);
|
|
250
|
+
this.sizeTimer.unref();
|
|
251
|
+
}
|
|
223
252
|
}
|
|
224
253
|
|
|
225
254
|
// Keep the terminal's title (the label of its tab or window) following `provider()`. `exitTitle` is what
|
|
@@ -242,8 +271,28 @@ export class Screen {
|
|
|
242
271
|
return (first ? TITLE_PUSH : '') + `\x1b]0;${text}\x07`;
|
|
243
272
|
}
|
|
244
273
|
|
|
245
|
-
|
|
246
|
-
|
|
274
|
+
readSize() {
|
|
275
|
+
const fallback = [this.out.columns || this.dimensions?.[0] || termWidth(), this.out.rows || this.dimensions?.[1] || 30];
|
|
276
|
+
if (process.platform === 'win32' && this.out.isTTY && this.out._handle?.getWindowSize) {
|
|
277
|
+
try {
|
|
278
|
+
const size = [0, 0];
|
|
279
|
+
if (!this.out._handle.getWindowSize(size) && size.every((n) => Number.isInteger(n) && n > 0)) return size;
|
|
280
|
+
} catch { /* closed or unsupported handle: keep the last valid size */ }
|
|
281
|
+
return this.dimensions || fallback;
|
|
282
|
+
}
|
|
283
|
+
return fallback;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
checkSize() {
|
|
287
|
+
if (this.closed) return false;
|
|
288
|
+
const size = this.readSize();
|
|
289
|
+
if (size[0] === this.dimensions[0] && size[1] === this.dimensions[1]) return false;
|
|
290
|
+
this.onResize(size);
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
get width() { return Math.max(20, this.dimensions[0]); }
|
|
295
|
+
get height() { return Math.max(10, this.dimensions[1]); }
|
|
247
296
|
// True from a size change until the repaint that follows it.
|
|
248
297
|
get resizing() { return this.resizeTimer !== null; }
|
|
249
298
|
|
|
@@ -707,9 +756,10 @@ export class Screen {
|
|
|
707
756
|
// Draw the screen: the conversation above, the live region on the last rows, the cursor in the input box.
|
|
708
757
|
draw({ full = false } = {}) {
|
|
709
758
|
if (this.closed || !this.full) return;
|
|
759
|
+
if (this.checkSize() || this.resizing) return;
|
|
710
760
|
if (this.scheduled) { clearTimeout(this.scheduled); this.scheduled = null; }
|
|
711
761
|
const cols = this.width;
|
|
712
|
-
const screenRows = Math.max(1, this.
|
|
762
|
+
const screenRows = Math.max(1, this.dimensions[1]);
|
|
713
763
|
if (!this.scroll) this.scrollLiveRows = null;
|
|
714
764
|
else if (full) this.scrollLiveRows = null; // a resize changes wrapping; capture the new live layout below
|
|
715
765
|
let layout = this.layoutLive(cols);
|
|
@@ -841,7 +891,7 @@ export class Screen {
|
|
|
841
891
|
const end = input ? starts[input.row + input.rows] : undefined;
|
|
842
892
|
const box = end !== undefined && end - starts[input.row] === input.rows ? { top: starts[input.row], rows: input.rows, col: input.col } : null;
|
|
843
893
|
// Cap to the terminal height so there is always a way back to the top.
|
|
844
|
-
const maxRows = Math.max(1,
|
|
894
|
+
const maxRows = Math.max(1, this.dimensions[1] - 1);
|
|
845
895
|
if (rows.length > maxRows) {
|
|
846
896
|
const dropped = rows.length - maxRows;
|
|
847
897
|
rows.splice(0, dropped);
|
|
@@ -864,8 +914,9 @@ export class Screen {
|
|
|
864
914
|
return buf;
|
|
865
915
|
}
|
|
866
916
|
|
|
867
|
-
onResize() {
|
|
917
|
+
onResize(size = this.readSize()) {
|
|
868
918
|
if (this.closed) return;
|
|
919
|
+
this.dimensions = size;
|
|
869
920
|
const now = Date.now();
|
|
870
921
|
if (!this.resizeSince) this.resizeSince = now;
|
|
871
922
|
clearTimeout(this.resizeTimer);
|
|
@@ -876,7 +927,7 @@ export class Screen {
|
|
|
876
927
|
settleResize() {
|
|
877
928
|
this.resizeTimer = null;
|
|
878
929
|
this.resizeSince = 0;
|
|
879
|
-
this.repaint();
|
|
930
|
+
if (!this.checkSize()) this.repaint();
|
|
880
931
|
}
|
|
881
932
|
|
|
882
933
|
// Write the whole viewport again after a resize: the newest static lines that fit, then the live region,
|
|
@@ -888,6 +939,7 @@ export class Screen {
|
|
|
888
939
|
// What was on screen above our output (a short session: the shell that started us) is overwritten.
|
|
889
940
|
repaint() {
|
|
890
941
|
if (this.closed) return;
|
|
942
|
+
this.dimensions = this.readSize();
|
|
891
943
|
if (this.full) {
|
|
892
944
|
clearTimeout(this.resizeTimer);
|
|
893
945
|
this.resizeTimer = null;
|
|
@@ -901,7 +953,7 @@ export class Screen {
|
|
|
901
953
|
this.resizeTimer = null;
|
|
902
954
|
this.resizeSince = 0;
|
|
903
955
|
const cols = this.width;
|
|
904
|
-
const screenRows = Math.max(1, this.
|
|
956
|
+
const screenRows = Math.max(1, this.dimensions[1]);
|
|
905
957
|
this.takeStatic();
|
|
906
958
|
const layout = this.layoutLive(cols);
|
|
907
959
|
const room = Math.max(0, screenRows - layout.rows.length);
|
|
@@ -933,7 +985,7 @@ export class Screen {
|
|
|
933
985
|
|
|
934
986
|
flush() {
|
|
935
987
|
if (this.closed) return;
|
|
936
|
-
if (this.resizing) return; // the repaint after the resize writes everything that is waiting
|
|
988
|
+
if (this.checkSize() || this.resizing) return; // the repaint after the resize writes everything that is waiting
|
|
937
989
|
if (this.full) { this.takeStatic(); this.draw(); return; }
|
|
938
990
|
if (this.scheduled) { clearTimeout(this.scheduled); this.scheduled = null; }
|
|
939
991
|
const cols = this.width;
|
|
@@ -1004,6 +1056,7 @@ export class Screen {
|
|
|
1004
1056
|
this.flush();
|
|
1005
1057
|
}
|
|
1006
1058
|
this.closed = true;
|
|
1059
|
+
clearInterval(this.sizeTimer);
|
|
1007
1060
|
clearTimeout(this.resizeTimer);
|
|
1008
1061
|
// Give the tab back its old label: a terminal with a title stack restores it, and one without is
|
|
1009
1062
|
// left with a neutral one rather than a title that still looks like a program that is running.
|
package/src/utils/shell.js
CHANGED
|
@@ -21,17 +21,16 @@ export function findShell(config) {
|
|
|
21
21
|
const candidates = [];
|
|
22
22
|
if (isWin) {
|
|
23
23
|
if (config?.env.CLAUDE_CODE_GIT_BASH_PATH) candidates.push(config.env.CLAUDE_CODE_GIT_BASH_PATH);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
24
|
+
// Where git is on the PATH, looked up here (`where git` in a process of its own took 75 ms of every start):
|
|
25
|
+
// ...\Git\cmd\git.exe or ...\Git\bin\git.exe or ...\Git\mingw64\bin\git.exe
|
|
26
|
+
for (const entry of (process.env.PATH || '').split(path.delimiter).filter(Boolean)) {
|
|
27
|
+
if (!['git.exe', 'git.cmd'].some((name) => fs.existsSync(path.join(entry, name)))) continue;
|
|
28
|
+
let dir = entry;
|
|
29
|
+
for (let i = 0; i < 3; i++) {
|
|
30
|
+
candidates.push(path.join(dir, 'bin', 'bash.exe'));
|
|
31
|
+
dir = path.dirname(dir);
|
|
33
32
|
}
|
|
34
|
-
}
|
|
33
|
+
}
|
|
35
34
|
candidates.push('C:\\Program Files\\Git\\bin\\bash.exe', 'C:\\Program Files (x86)\\Git\\bin\\bash.exe');
|
|
36
35
|
for (const c of candidates) {
|
|
37
36
|
if (c && fs.existsSync(c)) return (shellCache = { path: c, kind: 'bash', name: 'bash (Git Bash)' });
|
package/src/verify.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
// Discover a conservative project verification command when the user has not configured one.
|
|
2
|
-
|
|
3
|
-
import fs from 'node:fs';
|
|
4
|
-
import path from 'node:path';
|
|
5
|
-
|
|
6
|
-
function exists(cwd, name) {
|
|
7
|
-
try { return fs.statSync(path.join(cwd, name)).isFile(); } catch { return false; }
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function directoryExists(cwd, name) {
|
|
11
|
-
try { return fs.statSync(path.join(cwd, name)).isDirectory(); } catch { return false; }
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function readJson(file) {
|
|
15
|
-
try { return JSON.parse(fs.readFileSync(file, 'utf8')); } catch { return null; }
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function packageRunner(cwd) {
|
|
19
|
-
if (exists(cwd, 'pnpm-lock.yaml')) return 'pnpm';
|
|
20
|
-
if (exists(cwd, 'yarn.lock')) return 'yarn';
|
|
21
|
-
return 'npm';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function meaningfulTestScript(script) {
|
|
25
|
-
if (typeof script !== 'string' || !script.trim()) return false;
|
|
26
|
-
// Skip npm init's placeholder because it intentionally exits with an error.
|
|
27
|
-
return !/(?:error:\s*)?no test specified/i.test(script);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function packageCommand(cwd) {
|
|
31
|
-
const pkg = readJson(path.join(cwd, 'package.json'));
|
|
32
|
-
const scripts = pkg?.scripts;
|
|
33
|
-
if (!scripts || typeof scripts !== 'object') return null;
|
|
34
|
-
const runner = packageRunner(cwd);
|
|
35
|
-
if (meaningfulTestScript(scripts.test)) return runner === 'npm' ? 'npm test' : `${runner} test`;
|
|
36
|
-
for (const name of ['check', 'verify', 'lint']) {
|
|
37
|
-
if (typeof scripts[name] === 'string' && scripts[name].trim()) return `${runner} run ${name}`;
|
|
38
|
-
}
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function makefileCommand(cwd) {
|
|
43
|
-
if (!exists(cwd, 'Makefile')) return null;
|
|
44
|
-
try {
|
|
45
|
-
const text = fs.readFileSync(path.join(cwd, 'Makefile'), 'utf8');
|
|
46
|
-
return /^(?:\.PHONY\s+)?test\s*(?::|;)/m.test(text) ? 'make test' : null;
|
|
47
|
-
} catch { return null; }
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function discoverVerifyCommand(cwd) {
|
|
51
|
-
const root = path.resolve(cwd || process.cwd());
|
|
52
|
-
return packageCommand(root)
|
|
53
|
-
|| (exists(root, 'pyproject.toml') && (directoryExists(root, 'tests') || directoryExists(root, 'test')) ? 'python -m pytest -q' : null)
|
|
54
|
-
|| (exists(root, 'Cargo.toml') ? 'cargo test' : null)
|
|
55
|
-
|| (exists(root, 'go.mod') ? 'go test ./...' : null)
|
|
56
|
-
|| makefileCommand(root);
|
|
57
|
-
}
|