@hncode/hncode 0.8.1 → 0.8.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 +18 -0
- package/README_CN.md +14 -0
- package/bin/hncode +54 -8
- package/package.json +1 -1
- package/src/agent-task.js +356 -337
- package/src/agent.js +1465 -1405
- package/src/animate.js +56 -0
- package/src/colors.js +615 -593
- package/src/config.js +50 -7
- package/src/context-bar.js +280 -278
- package/src/doctor.js +4 -3
- package/src/file-tree.js +4 -3
- package/src/highlight.js +20 -6
- package/src/i18n.js +1432 -389
- package/src/image-row.js +11 -2
- package/src/index.js +218 -6
- package/src/keybindings.js +2 -1
- package/src/llm.js +146 -21
- package/src/log-file.js +112 -0
- package/src/markdown.js +646 -567
- package/src/mcp-server.js +409 -0
- package/src/mcp.js +279 -29
- package/src/memory-profile.js +19 -16
- package/src/migrate-index.js +175 -0
- package/src/migrate.js +392 -0
- package/src/panels.js +9 -7
- package/src/plan.js +7 -5
- package/src/plugin.js +37 -21
- package/src/registry-browser.js +17 -15
- package/src/rows-plan-bg.js +3 -2
- package/src/rows-status.js +20 -6
- package/src/rows-tool-result.js +242 -208
- package/src/screen-paint.js +153 -128
- package/src/session-recover.js +317 -0
- package/src/session.js +203 -13
- package/src/settings-schema.js +9 -0
- package/src/swarm-progress.js +6 -2
- package/src/tasks-browser.js +37 -36
- package/src/team-panel.js +12 -11
- package/src/term.js +57 -21
- package/src/text.js +254 -149
- package/src/tool-line.js +7 -2
- package/src/tool-result.js +400 -327
- package/src/tools/agent.js +7 -2
- package/src/tools/ask-user-question.js +1 -2
- package/src/tools/bash.js +446 -434
- package/src/tools/edit.js +653 -665
- package/src/tools/goal.js +130 -93
- package/src/tools/read.js +141 -133
- package/src/tools/tasks.js +214 -214
- package/src/tools/utils.js +143 -104
- package/src/tui.js +20887 -19942
package/src/migrate.js
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
// Export and import every piece of hncode state a person would want on another machine.
|
|
2
|
+
//
|
|
3
|
+
// WHAT TRAVELS AND WHAT DOES NOT
|
|
4
|
+
// ------------------------------
|
|
5
|
+
// The distinction is not size, it is MEANING. A session or a skill is something the user made; a paint log or a Chromium
|
|
6
|
+
// cache is something this machine produced on their behalf and can produce again. So the list is explicit rather than
|
|
7
|
+
// "everything under ~/.hncode", which would ship 174 MB of paint.log and 16 MB of browser profile.
|
|
8
|
+
//
|
|
9
|
+
// CROSS-MACHINE PATHS
|
|
10
|
+
// -------------------
|
|
11
|
+
// A session records `workspace` as an absolute path, and one saved on another machine points at a directory that does not
|
|
12
|
+
// exist here. The import therefore does not silently keep it: it either keeps the old path (for a machine with the same
|
|
13
|
+
// layout) or maps it onto a directory the user names. `tool-results` is dropped for the same reason — those files are
|
|
14
|
+
// referred to by absolute path from inside session histories, so copying the directory alone would leave every pointer
|
|
15
|
+
// aimed at a path that no longer exists.
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import os from 'node:os';
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import { execFileSync } from 'node:child_process';
|
|
20
|
+
import { INDEX_ENTRY, renderIndex, parseIndex, mapPath } from './migrate-index.js';
|
|
21
|
+
|
|
22
|
+
/** The root every path here is relative to. `HNCODE_HOME` wins, like everywhere else in the program. */
|
|
23
|
+
// The last four bytes of an archive. Latin-1 rather than UTF-8 so a reader does not have to agree with a decoder about
|
|
24
|
+
// how many bytes a character took — this is a tag, not text.
|
|
25
|
+
export const MARKER = 'HNB1';
|
|
26
|
+
|
|
27
|
+
export function hncodeHome() {
|
|
28
|
+
return process.env.HNCODE_HOME || path.join(process.env.USERPROFILE || process.env.HOME || '.', '.hncode');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* What a migration carries.
|
|
33
|
+
*
|
|
34
|
+
* `dir` entries are copied whole; `file` entries only when they exist. `config*.toml*` is matched by pattern so the
|
|
35
|
+
* rolling backups a user may want to come back to travel with the live file.
|
|
36
|
+
*/
|
|
37
|
+
export const PORTABLE = [
|
|
38
|
+
{ name: 'config.toml', kind: 'file', what: 'settings, providers, models' },
|
|
39
|
+
{ name: 'config.toml.backup', kind: 'file', what: 'previous settings' },
|
|
40
|
+
{ name: 'sessions', kind: 'dir', what: 'conversations' },
|
|
41
|
+
{ name: 'history.json', kind: 'file', what: 'prompt history' },
|
|
42
|
+
{ name: 'history', kind: 'dir', what: 'prompt history, per session' },
|
|
43
|
+
{ name: 'MEMORY.md', kind: 'file', what: 'agent memory' },
|
|
44
|
+
{ name: 'PERSONAL.md', kind: 'file', what: 'personal preferences' },
|
|
45
|
+
{ name: 'mcp.json', kind: 'file', what: 'MCP servers' },
|
|
46
|
+
{ name: 'skills', kind: 'dir', what: 'installed skills' },
|
|
47
|
+
{ name: 'plugins', kind: 'dir', what: 'installed plugins' },
|
|
48
|
+
{ name: 'teams', kind: 'dir', what: 'team rosters' },
|
|
49
|
+
{ name: 'file-history', kind: 'dir', what: 'snapshots for /undo' },
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
/** Entries that are deliberately left behind, with the reason, so a report can say why. */
|
|
53
|
+
export const EXCLUDED = [
|
|
54
|
+
{ name: 'browser-profile', why: 'the plugin browser\'s Chromium profile — machine-local, rebuilt on use' },
|
|
55
|
+
{ name: 'tool-results', why: 'spilled tool output, addressed by absolute paths from inside the sessions' },
|
|
56
|
+
{ name: '*.log', why: 'debug logs (paint.log alone is over 170 MB)' },
|
|
57
|
+
{ name: 'teleport', why: 'sessions already exported as standalone files' },
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
/** Every config backup this machine has, since `/reload` can restore one. */
|
|
61
|
+
function configBackups(home) {
|
|
62
|
+
try {
|
|
63
|
+
return fs.readdirSync(home)
|
|
64
|
+
.filter((f) => /^config\.toml\.(bak.*|backup)$/.test(f))
|
|
65
|
+
.map((f) => ({ name: f, kind: 'file', what: 'settings backup' }));
|
|
66
|
+
} catch { return []; }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Size of a file or a directory tree, in bytes, without following symlinks. */
|
|
70
|
+
export function sizeOf(p) {
|
|
71
|
+
let total = 0;
|
|
72
|
+
const walk = (q) => {
|
|
73
|
+
let st;
|
|
74
|
+
try { st = fs.lstatSync(q); } catch { return; }
|
|
75
|
+
if (st.isSymbolicLink()) return;
|
|
76
|
+
if (st.isDirectory()) {
|
|
77
|
+
let entries = [];
|
|
78
|
+
try { entries = fs.readdirSync(q); } catch { return; }
|
|
79
|
+
for (const e of entries) walk(path.join(q, e));
|
|
80
|
+
} else {
|
|
81
|
+
total += st.size;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
walk(p);
|
|
85
|
+
return total;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Human-readable bytes, at the precision a migration report needs. */
|
|
89
|
+
export function fmtBytes(n) {
|
|
90
|
+
const v = Number(n) || 0;
|
|
91
|
+
if (v < 1024) return `${v} B`;
|
|
92
|
+
if (v < 1024 * 1024) return `${(v / 1024).toFixed(0)} KB`;
|
|
93
|
+
if (v < 1024 * 1024 * 1024) return `${(v / 1024 / 1024).toFixed(1)} MB`;
|
|
94
|
+
return `${(v / 1024 / 1024 / 1024).toFixed(2)} GB`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* What an export WOULD include: one entry per portable item that exists, with its size.
|
|
99
|
+
*
|
|
100
|
+
* Returned rather than printed so a caller can show a summary before writing anything — a 190 MB archive is worth
|
|
101
|
+
* confirming.
|
|
102
|
+
*/
|
|
103
|
+
export function planExport(home = hncodeHome()) {
|
|
104
|
+
const items = [];
|
|
105
|
+
const all = [...PORTABLE, ...configBackups(home)];
|
|
106
|
+
const seen = new Set();
|
|
107
|
+
for (const item of all) {
|
|
108
|
+
if (seen.has(item.name)) continue;
|
|
109
|
+
seen.add(item.name);
|
|
110
|
+
const p = path.join(home, item.name);
|
|
111
|
+
let st;
|
|
112
|
+
try { st = fs.lstatSync(p); } catch { continue; }
|
|
113
|
+
if (item.kind === 'dir' && !st.isDirectory()) continue;
|
|
114
|
+
if (item.kind === 'file' && !st.isFile()) continue;
|
|
115
|
+
items.push({ ...item, size: sizeOf(p) });
|
|
116
|
+
}
|
|
117
|
+
return items;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Write a migration archive.
|
|
123
|
+
*
|
|
124
|
+
* FORMAT: a one-line JSON manifest followed by a newline, then every file's bytes back to back. No dependency, no
|
|
125
|
+
* compression, and the manifest is readable with `head -1` — which matters when the only thing available on the target
|
|
126
|
+
* machine is the hncode that is not installed yet.
|
|
127
|
+
*
|
|
128
|
+
* {"version":1,"createdAt":...,"home":...,"items":[{name,kind,size}],"files":[{path,size,offset}]}
|
|
129
|
+
* <bytes><bytes><bytes>…
|
|
130
|
+
*
|
|
131
|
+
* `offset` is absolute from the start of the file, so a reader can seek straight to one entry without walking the rest.
|
|
132
|
+
*
|
|
133
|
+
* @param {string} out path to write
|
|
134
|
+
* @param {object} [opts] `home`, `items` (from planExport), `onProgress(done, total, name)`
|
|
135
|
+
* @returns {{path: string, files: number, bytes: number, items: object[]}}
|
|
136
|
+
*/
|
|
137
|
+
/**
|
|
138
|
+
* Write a migration archive.
|
|
139
|
+
*
|
|
140
|
+
* FORMAT: the files' bytes back to back, then a JSON manifest, then an 8-byte little-endian length and the marker
|
|
141
|
+
* `HNB1`. The manifest goes LAST on purpose — its offsets are from the start of the file, so writing it first would
|
|
142
|
+
* mean knowing every size before a single byte was copied, and padding the numbers to a guessed width so the header
|
|
143
|
+
* length could not move. Writing it last makes both problems disappear.
|
|
144
|
+
*
|
|
145
|
+
* <file bytes><file bytes>…<manifest json><8-byte length><HNB1>
|
|
146
|
+
*
|
|
147
|
+
* @param {string} out path to write
|
|
148
|
+
* @param {object} [opts] `home`, `items` (from planExport), `onProgress(done, total, name)`
|
|
149
|
+
* @returns {{path: string, files: number, bytes: number, items: object[]}}
|
|
150
|
+
*/
|
|
151
|
+
export function writeArchive(out, opts = {}) {
|
|
152
|
+
const home = opts.home || hncodeHome();
|
|
153
|
+
const items = opts.items || planExport(home);
|
|
154
|
+
const files = [];
|
|
155
|
+
const collect = (abs, rel) => {
|
|
156
|
+
let st;
|
|
157
|
+
try { st = fs.lstatSync(abs); } catch { return; }
|
|
158
|
+
if (st.isSymbolicLink()) return;
|
|
159
|
+
if (st.isDirectory()) {
|
|
160
|
+
let entries = [];
|
|
161
|
+
try { entries = fs.readdirSync(abs); } catch { return; }
|
|
162
|
+
for (const e of entries) collect(path.join(abs, e), rel ? rel + '/' + e : e);
|
|
163
|
+
} else if (st.isFile()) {
|
|
164
|
+
files.push({ abs, rel, size: st.size });
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
for (const item of items) collect(path.join(home, item.name), item.name);
|
|
168
|
+
|
|
169
|
+
// THE INDEX TRAVELS AS AN ORDINARY ENTRY, so it can be extracted and edited with nothing but a text editor. It is
|
|
170
|
+
// built from the sessions that were just collected rather than from a second walk of the store.
|
|
171
|
+
const sessionIndex = [];
|
|
172
|
+
for (const f of files) {
|
|
173
|
+
const m = /^sessions\/([^/]+)\.json$/.exec(f.rel);
|
|
174
|
+
if (!m) continue;
|
|
175
|
+
let meta = { id: m[1], title: '', workspace: '' };
|
|
176
|
+
try {
|
|
177
|
+
// The file is small enough to read whole here — this is the export, not a frame.
|
|
178
|
+
const s = JSON.parse(fs.readFileSync(f.abs, 'utf8'));
|
|
179
|
+
meta = { id: s.id || m[1], title: s.title || '', workspace: s.workspace || '' };
|
|
180
|
+
} catch { /* a session that will not parse is still listed by id */ }
|
|
181
|
+
sessionIndex.push({ ...meta, file: f.rel });
|
|
182
|
+
}
|
|
183
|
+
const indexBody = Buffer.from(renderIndex({
|
|
184
|
+
home,
|
|
185
|
+
createdAt: opts.createdAt || Date.now(),
|
|
186
|
+
mapping: opts.mapping || [],
|
|
187
|
+
sessions: sessionIndex,
|
|
188
|
+
}), 'utf8');
|
|
189
|
+
files.unshift({ abs: null, rel: INDEX_ENTRY, size: indexBody.length, body: indexBody });
|
|
190
|
+
|
|
191
|
+
const fd = fs.openSync(out, 'w');
|
|
192
|
+
let offset = 0;
|
|
193
|
+
const records = [];
|
|
194
|
+
try {
|
|
195
|
+
const buf = Buffer.allocUnsafe(1024 * 1024);
|
|
196
|
+
for (let i = 0; i < files.length; i++) {
|
|
197
|
+
const f = files[i];
|
|
198
|
+
records.push({ path: f.rel, size: f.size, offset });
|
|
199
|
+
// A GENERATED ENTRY HAS ITS BYTES IN MEMORY. `index.yml` is not on disk to read from, so it is written
|
|
200
|
+
// straight from the buffer the caller handed over; everything else streams from its file.
|
|
201
|
+
if (f.body) {
|
|
202
|
+
fs.writeSync(fd, f.body, 0, f.body.length);
|
|
203
|
+
offset += f.body.length;
|
|
204
|
+
if (typeof opts.onProgress === 'function') opts.onProgress(i + 1, files.length, f.rel);
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const src = fs.openSync(f.abs, 'r');
|
|
209
|
+
try {
|
|
210
|
+
let left = f.size;
|
|
211
|
+
let pos = 0;
|
|
212
|
+
while (left > 0) {
|
|
213
|
+
const n = fs.readSync(src, buf, 0, Math.min(buf.length, left), pos);
|
|
214
|
+
if (n <= 0) break;
|
|
215
|
+
fs.writeSync(fd, buf, 0, n);
|
|
216
|
+
pos += n;
|
|
217
|
+
left -= n;
|
|
218
|
+
offset += n;
|
|
219
|
+
}
|
|
220
|
+
} finally {
|
|
221
|
+
fs.closeSync(src);
|
|
222
|
+
}
|
|
223
|
+
if (typeof opts.onProgress === 'function') opts.onProgress(i + 1, files.length, f.rel);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const manifest = Buffer.from(JSON.stringify({
|
|
227
|
+
version: 1,
|
|
228
|
+
createdAt: Date.now(),
|
|
229
|
+
home,
|
|
230
|
+
items: items.map((i) => ({ name: i.name, kind: i.kind, size: i.size })),
|
|
231
|
+
files: records,
|
|
232
|
+
}), 'utf8');
|
|
233
|
+
fs.writeSync(fd, manifest);
|
|
234
|
+
const tail = Buffer.alloc(12);
|
|
235
|
+
tail.writeBigUInt64LE(BigInt(manifest.length), 0);
|
|
236
|
+
tail.write(MARKER, 8, 'latin1');
|
|
237
|
+
fs.writeSync(fd, tail);
|
|
238
|
+
offset += manifest.length + tail.length;
|
|
239
|
+
} finally {
|
|
240
|
+
fs.closeSync(fd);
|
|
241
|
+
}
|
|
242
|
+
return { path: out, files: files.length, bytes: offset, items };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Read an archive's manifest without extracting anything.
|
|
247
|
+
*
|
|
248
|
+
* Used to SHOW the user what they are about to import, and to refuse a file that is not one of ours before anything is
|
|
249
|
+
* written over their state.
|
|
250
|
+
*/
|
|
251
|
+
/**
|
|
252
|
+
* Read an archive's manifest without extracting anything.
|
|
253
|
+
*
|
|
254
|
+
* The manifest is the last thing in the file, immediately before a 12-byte tail holding its length and the marker, so
|
|
255
|
+
* it is found by seeking to the end rather than by scanning.
|
|
256
|
+
*/
|
|
257
|
+
export function readArchiveManifest(from) {
|
|
258
|
+
const fd = fs.openSync(from, 'r');
|
|
259
|
+
try {
|
|
260
|
+
const size = fs.fstatSync(fd).size;
|
|
261
|
+
if (size < 12) throw new Error('not a hncode archive: too short');
|
|
262
|
+
const tail = Buffer.alloc(12);
|
|
263
|
+
fs.readSync(fd, tail, 0, 12, size - 12);
|
|
264
|
+
if (tail.subarray(8).toString('latin1') !== MARKER) throw new Error('not a hncode archive: the end marker is missing');
|
|
265
|
+
const len = Number(tail.readBigUInt64LE(0));
|
|
266
|
+
if (!Number.isFinite(len) || len <= 0 || len > size - 12) throw new Error('not a hncode archive: the manifest length is wrong');
|
|
267
|
+
const body = Buffer.alloc(len);
|
|
268
|
+
fs.readSync(fd, body, 0, len, size - 12 - len);
|
|
269
|
+
let m;
|
|
270
|
+
try { m = JSON.parse(body.toString('utf8')); } catch { throw new Error('not a hncode archive: the manifest is not JSON'); }
|
|
271
|
+
if (!m || m.version !== 1 || !Array.isArray(m.files)) throw new Error('not a hncode archive: unsupported version');
|
|
272
|
+
return { manifest: m };
|
|
273
|
+
} finally {
|
|
274
|
+
fs.closeSync(fd);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Extract an archive into a hncode home.
|
|
281
|
+
*
|
|
282
|
+
* SAFE BY DEFAULT. Nothing is overwritten unless `overwrite` is set: an existing session with the same id is kept and
|
|
283
|
+
* reported, because importing is often "bring my other machine's history in beside this one", not "replace it".
|
|
284
|
+
*
|
|
285
|
+
* PATH REWRITING. A session stores its `workspace` as an absolute path. Imported from another machine that path almost
|
|
286
|
+
* certainly does not exist here, and leaving it means every session opens onto a directory that is not there. Two
|
|
287
|
+
* choices, both explicit:
|
|
288
|
+
*
|
|
289
|
+
* `workspace: "/some/dir"` every imported session is pointed at that directory
|
|
290
|
+
* omitted the original paths are kept (right for a machine with the same layout)
|
|
291
|
+
*
|
|
292
|
+
* Only `workspace` is rewritten. Everything else a session holds — messages, tool arguments, todos — is conversation,
|
|
293
|
+
* and rewriting paths inside it would corrupt what the model was told.
|
|
294
|
+
*
|
|
295
|
+
* @param {string} from archive path
|
|
296
|
+
* @param {object} [opts] `home`, `overwrite`, `workspace`, `dryRun`, `onProgress(done, total, path)`
|
|
297
|
+
* @returns {{written: number, skipped: number, bytes: number, sessionsRewritten: number, errors: string[]}}
|
|
298
|
+
*/
|
|
299
|
+
export function readArchive(from, opts = {}) {
|
|
300
|
+
const home = opts.home || hncodeHome();
|
|
301
|
+
const { manifest } = readArchiveManifest(from);
|
|
302
|
+
const result = {
|
|
303
|
+
written: 0, skipped: 0, bytes: 0, sessionsRewritten: 0, errors: [],
|
|
304
|
+
indexFound: false, mapped: 0,
|
|
305
|
+
};
|
|
306
|
+
// `--workspace` still wins when given: it is the blunt "put everything here" the user typed on this command line.
|
|
307
|
+
const rewriteTo = typeof opts.workspace === 'string' && opts.workspace.trim() ? path.resolve(opts.workspace.trim()) : null;
|
|
308
|
+
|
|
309
|
+
const fd = fs.openSync(from, 'r');
|
|
310
|
+
const readEntry = (f) => {
|
|
311
|
+
const buf = Buffer.allocUnsafe(Math.max(1, f.size));
|
|
312
|
+
let read = 0;
|
|
313
|
+
while (read < f.size) {
|
|
314
|
+
const n = fs.readSync(fd, buf, read, f.size - read, f.offset + read);
|
|
315
|
+
if (n <= 0) break;
|
|
316
|
+
read += n;
|
|
317
|
+
}
|
|
318
|
+
return buf.subarray(0, read);
|
|
319
|
+
};
|
|
320
|
+
try {
|
|
321
|
+
// THE INDEX IS READ FIRST, because it decides where every session lands. It travels as an ordinary entry, so a
|
|
322
|
+
// person can extract it, correct the paths and put it back — or hand `--index <file>` an edited copy from outside.
|
|
323
|
+
let index = null;
|
|
324
|
+
if (opts.index !== false) {
|
|
325
|
+
const entry = manifest.files.find((f) => f.path === INDEX_ENTRY);
|
|
326
|
+
if (entry) {
|
|
327
|
+
try {
|
|
328
|
+
index = parseIndex(readEntry(entry).toString('utf8'));
|
|
329
|
+
result.indexFound = !!index;
|
|
330
|
+
} catch { /* a damaged index must not stop the import */ }
|
|
331
|
+
}
|
|
332
|
+
if (typeof opts.indexFile === 'string' && opts.indexFile.trim()) {
|
|
333
|
+
try { index = parseIndex(fs.readFileSync(path.resolve(opts.indexFile.trim()), 'utf8')); result.indexFound = !!index; }
|
|
334
|
+
catch (e) { result.errors.push(`could not read --index file: ${(e && e.message) || e}`); }
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const mapping = (index && index.mapping) || [];
|
|
338
|
+
|
|
339
|
+
for (const f of manifest.files) {
|
|
340
|
+
// A path in the manifest is data from a file, so it is checked before it is joined to anything: an entry holding
|
|
341
|
+
// `..` would otherwise write outside the hncode home.
|
|
342
|
+
const rel = String(f.path || '');
|
|
343
|
+
if (!rel || rel.startsWith('/') || rel.includes('..') || /^[A-Za-z]:/.test(rel)) {
|
|
344
|
+
result.errors.push(`refused an unsafe path: ${rel}`);
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
const dest = path.join(home, rel);
|
|
348
|
+
const isSession = /(^|\/)sessions\/[^/]+\.json$/.test(rel);
|
|
349
|
+
if (!opts.overwrite && fs.existsSync(dest)) { result.skipped++; continue; }
|
|
350
|
+
if (opts.dryRun) {
|
|
351
|
+
result.written++;
|
|
352
|
+
result.bytes += f.size;
|
|
353
|
+
if (isSession && (rewriteTo || mapping.length)) result.sessionsRewritten++;
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
358
|
+
const buf = readEntry(f);
|
|
359
|
+
// A SESSION IS REWRITTEN, NOT COPIED VERBATIM — its `workspace` is an absolute path from another machine.
|
|
360
|
+
if (isSession && (rewriteTo || mapping.length)) {
|
|
361
|
+
try {
|
|
362
|
+
const s = JSON.parse(buf.toString('utf8'));
|
|
363
|
+
if (s && typeof s === 'object') {
|
|
364
|
+
if (rewriteTo) {
|
|
365
|
+
s.workspace = rewriteTo;
|
|
366
|
+
if (s.cwd) s.cwd = rewriteTo;
|
|
367
|
+
} else {
|
|
368
|
+
const before = s.workspace;
|
|
369
|
+
if (s.workspace) s.workspace = mapPath(s.workspace, mapping);
|
|
370
|
+
if (s.cwd) s.cwd = mapPath(s.cwd, mapping);
|
|
371
|
+
if (s.workspace && s.workspace !== before) result.mapped++;
|
|
372
|
+
}
|
|
373
|
+
const out = Buffer.from(JSON.stringify(s), 'utf8');
|
|
374
|
+
fs.writeFileSync(dest, out);
|
|
375
|
+
result.written++;
|
|
376
|
+
result.bytes += out.length;
|
|
377
|
+
result.sessionsRewritten++;
|
|
378
|
+
if (typeof opts.onProgress === 'function') opts.onProgress(result.written, manifest.files.length, rel);
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
} catch { /* not JSON we can rewrite; fall through and copy it as-is */ }
|
|
382
|
+
}
|
|
383
|
+
fs.writeFileSync(dest, buf);
|
|
384
|
+
result.written++;
|
|
385
|
+
result.bytes += buf.length;
|
|
386
|
+
if (typeof opts.onProgress === 'function') opts.onProgress(result.written, manifest.files.length, rel);
|
|
387
|
+
}
|
|
388
|
+
} finally {
|
|
389
|
+
fs.closeSync(fd);
|
|
390
|
+
}
|
|
391
|
+
return result;
|
|
392
|
+
}
|
package/src/panels.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import { C } from './colors.js';
|
|
7
7
|
import { col, fitAnsi, wrapWords } from './text.js';
|
|
8
|
+
import { uiText } from './i18n.js';
|
|
8
9
|
|
|
9
10
|
// ---- title strip ----------------------------------------------------------
|
|
10
11
|
|
|
@@ -167,7 +168,7 @@ export function renderTodoPanel(state, w, hoverTop, dragTop) {
|
|
|
167
168
|
const out = [];
|
|
168
169
|
const ruleColor = dragTop ? C.scrollThumbActive : (hoverTop ? C.hover : C.border);
|
|
169
170
|
out.push(col('─'.repeat(w), ruleColor));
|
|
170
|
-
out.push(col(' Todo', C.cyan + C.bold));
|
|
171
|
+
out.push(col(uiText(' Todo'), C.cyan + C.bold));
|
|
171
172
|
const want = todoRowCount(state);
|
|
172
173
|
// CREATION ORDER, ALWAYS. Which rows fit is still the panel's decision — it takes the first `want` in order.
|
|
173
174
|
const rows = todos.slice(0, want);
|
|
@@ -177,10 +178,10 @@ export function renderTodoPanel(state, w, hoverTop, dragTop) {
|
|
|
177
178
|
const counts = { done: 0, in_progress: 0, pending: 0 };
|
|
178
179
|
for (const t of todos) if (!rows.includes(t)) counts[t.status] = (counts[t.status] || 0) + 1;
|
|
179
180
|
const dist = [['done', 'done'], ['in_progress', 'in progress'], ['pending', 'pending']]
|
|
180
|
-
.filter(([k]) => counts[k] > 0).map(([k, label]) => `${counts[k]} ${label}`).join(', ');
|
|
181
|
+
.filter(([k]) => counts[k] > 0).map(([k, label]) => `${counts[k]} ${uiText(label)}`).join(', ');
|
|
181
182
|
// The hint appears exactly when rows are hidden — which is exactly when Ctrl+T is useful — so this is where the key
|
|
182
183
|
// has to be discoverable.
|
|
183
|
-
out.push(col(`
|
|
184
|
+
out.push(col(` ${uiText(`… +${hidden} more`)}${dist ? ` (${dist})` : ''} ${uiText('· ctrl+t to expand · drag the top rule to resize')}`, C.gray));
|
|
184
185
|
}
|
|
185
186
|
return out;
|
|
186
187
|
}
|
|
@@ -231,7 +232,7 @@ export function renderQueuePanel(state, w, hoverTop, dragTop) {
|
|
|
231
232
|
const out = [];
|
|
232
233
|
const ruleColor = dragTop ? C.scrollThumbActive : (hoverTop ? C.hover : C.border);
|
|
233
234
|
out.push(col('─'.repeat(w), ruleColor));
|
|
234
|
-
out.push(col(' Queue', C.cyan + C.bold));
|
|
235
|
+
out.push(col(uiText(' Queue'), C.cyan + C.bold));
|
|
235
236
|
|
|
236
237
|
const want = queueRowCount(state);
|
|
237
238
|
const rows = queued.slice(0, want);
|
|
@@ -243,12 +244,13 @@ export function renderQueuePanel(state, w, hoverTop, dragTop) {
|
|
|
243
244
|
|
|
244
245
|
const hidden = queued.length - rows.length;
|
|
245
246
|
if (hidden > 0) {
|
|
247
|
+
const more = uiText(`… +${hidden} more`);
|
|
246
248
|
const hint = state.running
|
|
247
|
-
?
|
|
248
|
-
:
|
|
249
|
+
? `${more} · ${uiText('↑ to edit · ctrl-s to steer immediately')} · ${uiText('drag top rule to resize')}`
|
|
250
|
+
: `${more} · ${uiText('↑ to edit · will send now')} · ${uiText('drag top rule to resize')}`;
|
|
249
251
|
out.push(col(' ' + hint, C.gray));
|
|
250
252
|
} else {
|
|
251
|
-
const hint = state.running ? '↑ to edit · ctrl-s to steer immediately' : '↑ to edit · will send now';
|
|
253
|
+
const hint = state.running ? uiText('↑ to edit · ctrl-s to steer immediately') : uiText('↑ to edit · will send now');
|
|
252
254
|
out.push(col(' ' + hint, C.gray));
|
|
253
255
|
}
|
|
254
256
|
|
package/src/plan.js
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
// tolerant: a plan is free-form Markdown, so a missed file reference degrades to
|
|
18
18
|
// "fewer files listed", never to an error.
|
|
19
19
|
|
|
20
|
+
import { uiText, t, getUiLang } from './i18n.js';
|
|
20
21
|
import fs from 'node:fs';
|
|
21
22
|
import path from 'node:path';
|
|
22
23
|
|
|
@@ -113,16 +114,17 @@ export function reviewLines(plan, workspace) {
|
|
|
113
114
|
const stats = planStats(plan);
|
|
114
115
|
const files = filesReferenced(plan);
|
|
115
116
|
const lines = [];
|
|
116
|
-
lines.push(
|
|
117
|
+
lines.push(t(getUiLang(), 'plan.summary',
|
|
118
|
+
{ steps: stats.steps, lines: stats.lines, files: files.length }));
|
|
117
119
|
lines.push('');
|
|
118
120
|
if (files.length) {
|
|
119
|
-
lines.push('Files this plan will touch:');
|
|
121
|
+
lines.push(uiText('Files this plan will touch:'));
|
|
120
122
|
for (const f of files.slice(0, 40)) lines.push(` ${f}`);
|
|
121
|
-
if (files.length > 40) lines.push(
|
|
123
|
+
if (files.length > 40) lines.push(' ' + t(getUiLang(), 'plan.andMore', { n: files.length - 40 }));
|
|
122
124
|
} else {
|
|
123
|
-
lines.push('No specific files named in the plan.');
|
|
125
|
+
lines.push(uiText('No specific files named in the plan.'));
|
|
124
126
|
}
|
|
125
127
|
lines.push('');
|
|
126
|
-
lines.push('Enter = approve & execute e = edit the plan Esc = keep planning');
|
|
128
|
+
lines.push(uiText('Enter = approve & execute e = edit the plan Esc = keep planning'));
|
|
127
129
|
return lines;
|
|
128
130
|
}
|
package/src/plugin.js
CHANGED
|
@@ -684,29 +684,45 @@ export async function loadPlugins(dir, opts = {}) {
|
|
|
684
684
|
// Phase 1: import every file and read its metadata. Imports come first because the
|
|
685
685
|
// dependency order is declared in that metadata — the graph cannot be built before
|
|
686
686
|
// the modules are read.
|
|
687
|
+
//
|
|
688
|
+
// THE IMPORTS RUN IN PARALLEL, THEN THE LOOP BELOW CONSUMES THEM IN ORDER.
|
|
689
|
+
//
|
|
690
|
+
// Each file is independent, so importing them one at a time made startup pay the SUM of
|
|
691
|
+
// every plugin's module evaluation — measured at 165ms for eleven plugins, dominated by
|
|
692
|
+
// the largest one. The order that MATTERS is `inject`, applied in phase 2; the filesystem
|
|
693
|
+
// order only ever decided which warning was printed first.
|
|
694
|
+
//
|
|
695
|
+
// The console is redirected across the WHOLE batch rather than per import: the swap is
|
|
696
|
+
// global, so overlapping captures would restore each other's handlers and let plugin
|
|
697
|
+
// output land on the screen, which is the thing the capture exists to prevent.
|
|
698
|
+
const mods = new Map();
|
|
699
|
+
{
|
|
700
|
+
const restoreConsole = capturePluginConsole();
|
|
701
|
+
try {
|
|
702
|
+
await Promise.all(files.map(async ({ id: fid, full }) => {
|
|
703
|
+
// A query string is a DIFFERENT module instance to Node, which is exactly what
|
|
704
|
+
// a hot reload wants. Only on a fresh load: the startup path keeps the plain
|
|
705
|
+
// URL so nothing else can see two instances of a plugin.
|
|
706
|
+
const url = pathToFileURL(full).href + (opts.fresh ? `?v=${++loadSeq}` : '');
|
|
707
|
+
try {
|
|
708
|
+
mods.set(fid, await import(url));
|
|
709
|
+
} catch (e) {
|
|
710
|
+
// Recorded rather than thrown: one broken plugin must not stop the others, and
|
|
711
|
+
// the loop below reports the failure against the file it came from.
|
|
712
|
+
mods.set(fid, { __error: (e && e.message) || String(e) });
|
|
713
|
+
}
|
|
714
|
+
}));
|
|
715
|
+
} finally {
|
|
716
|
+
restoreConsole();
|
|
717
|
+
}
|
|
718
|
+
}
|
|
687
719
|
const candidates = [];
|
|
688
|
-
for (const { id: f
|
|
720
|
+
for (const { id: f } of files) {
|
|
689
721
|
try {
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
// A PLUGIN'S OWN console.log MUST NOT REACH THE SCREEN.
|
|
695
|
-
//
|
|
696
|
-
// The import below runs the plugin's top-level code, and `install(api)` runs more of it. Both are
|
|
697
|
-
// plugin author territory, where `console.log` is the first thing anyone reaches for — and while the
|
|
698
|
-
// TUI owns stdout, that write lands wherever the cursor happens to be, on top of the frame. It was
|
|
699
|
-
// most visible after `/reload`, which re-runs every plugin's load-time logging in one burst.
|
|
700
|
-
//
|
|
701
|
-
// Redirected to `pluginLogger`, which queues a notice the TUI already renders in its own transcript.
|
|
702
|
-
// The real console is restored immediately afterwards, so this covers exactly the window where
|
|
703
|
-
// plugin code runs and nothing else.
|
|
704
|
-
const restoreConsole = capturePluginConsole();
|
|
705
|
-
let mod;
|
|
706
|
-
try {
|
|
707
|
-
mod = await import(url);
|
|
708
|
-
} finally {
|
|
709
|
-
restoreConsole();
|
|
722
|
+
const mod = mods.get(f);
|
|
723
|
+
if (mod && mod.__error) {
|
|
724
|
+
notice('error', `${f} failed to load: ${mod.__error}`);
|
|
725
|
+
continue;
|
|
710
726
|
}
|
|
711
727
|
// `install(api)` may be the named export or live on the default export.
|
|
712
728
|
let installFn;
|
package/src/registry-browser.js
CHANGED
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
|
|
28
28
|
import { C } from './colors.js';
|
|
29
29
|
import { visualWidth } from './term.js';
|
|
30
|
+
import { uiText } from './i18n.js';
|
|
30
31
|
|
|
31
32
|
const ELLIPSIS = '\u2026';
|
|
32
33
|
const ESC = '\x1b';
|
|
@@ -190,11 +191,11 @@ export function visibleRowsForTab(state, tabId) {
|
|
|
190
191
|
|
|
191
192
|
export function renderRegistryBrowser(state, cols, rows) {
|
|
192
193
|
if (cols < MIN_WIDTH || rows < MIN_HEIGHT) {
|
|
193
|
-
const lines = [fitExactly(C.red +
|
|
194
|
+
const lines = [fitExactly(C.red + uiText(`Terminal too small (need >= ${MIN_WIDTH} x ${MIN_HEIGHT})`) + C.reset, cols)];
|
|
194
195
|
for (let i = 1; i < rows; i++) lines.push(' '.repeat(cols));
|
|
195
196
|
return lines;
|
|
196
197
|
}
|
|
197
|
-
const title = state.kind === 'skills' ? ' Skills' : ' Plugins';
|
|
198
|
+
const title = uiText(state.kind === 'skills' ? ' Skills' : ' Plugins');
|
|
198
199
|
const tabs = state.kind === 'skills' ? SKILL_TABS : PLUGIN_TABS;
|
|
199
200
|
const tabIdx = Math.max(0, tabs.findIndex((t) => t.id === state.tab));
|
|
200
201
|
const tab = tabs[tabIdx] || tabs[0];
|
|
@@ -216,7 +217,7 @@ export function renderRegistryBrowser(state, cols, rows) {
|
|
|
216
217
|
// The key hints stay visible while the fetch runs: the panel is usable during it
|
|
217
218
|
// (the local tabs are already loaded), so replacing the hints with a status message
|
|
218
219
|
// would hide the keys exactly when the user is most likely to press them.
|
|
219
|
-
const hint = ' / search
|
|
220
|
+
const hint = uiText(' / search · Tab switch · ↑↓ select · Enter install/remove · Esc close');
|
|
220
221
|
out.push(fitExactly(C.gray + hint + (state.busy ? C.gray + ` ${state.busy}` : '') + C.reset, cols));
|
|
221
222
|
out.push('');
|
|
222
223
|
// The search field, drawn whenever it is open or has text, so a filtered list always explains itself.
|
|
@@ -233,7 +234,8 @@ export function renderRegistryBrowser(state, cols, rows) {
|
|
|
233
234
|
let col = 0;
|
|
234
235
|
const segs = tabs.map((t) => {
|
|
235
236
|
const n = rowsForTab(state, t.id).length;
|
|
236
|
-
const
|
|
237
|
+
const lbl = uiText(t.label);
|
|
238
|
+
const text = t.id === tab.id ? `[${lbl} (${n})]` : ` ${lbl} (${n}) `;
|
|
237
239
|
const col0 = col;
|
|
238
240
|
col += visWidth(text) + 1; // +1 for the joining space
|
|
239
241
|
return { id: t.id, text, col0, col1: col0 + visWidth(text) - 1 };
|
|
@@ -259,11 +261,11 @@ export function renderRegistryBrowser(state, cols, rows) {
|
|
|
259
261
|
// "Loading" only applies to the tab that is actually waiting on the fetch; a local
|
|
260
262
|
// tab is empty because there is genuinely nothing there, and saying "Loading" for
|
|
261
263
|
// data that is already on screen reads as a stall.
|
|
262
|
-
const msg = (isRemoteTab && state.busy) ? ' Loading
|
|
264
|
+
const msg = (isRemoteTab && state.busy) ? uiText(' Loading…')
|
|
263
265
|
: (isRemoteTab && state.data && state.data.remoteError)
|
|
264
|
-
? ' Could not fetch from the repo: ' + state.data.remoteError
|
|
265
|
-
: isRemoteTab ? ' Nothing published in the repo yet.'
|
|
266
|
-
: ' Nothing here.';
|
|
266
|
+
? uiText(' Could not fetch from the repo: ') + state.data.remoteError
|
|
267
|
+
: isRemoteTab ? uiText(' Nothing published in the repo yet.')
|
|
268
|
+
: uiText(' Nothing here.');
|
|
267
269
|
out.push(C.gray + msg + C.reset);
|
|
268
270
|
}
|
|
269
271
|
// How many LINES each row occupies: its label plus its wrapped description.
|
|
@@ -294,14 +296,14 @@ export function renderRegistryBrowser(state, cols, rows) {
|
|
|
294
296
|
const isSel = i === sel;
|
|
295
297
|
const ptr = isSel ? C.cyan + '\u276f ' + C.reset : ' ';
|
|
296
298
|
const nameStyle = isSel ? (C.cyan + C.bold) : C.white;
|
|
297
|
-
const badge = r.status ? ' ' + statusColor(r.statusTone) + r.status + C.reset : '';
|
|
299
|
+
const badge = r.status ? ' ' + statusColor(r.statusTone) + uiText(r.status) + C.reset : '';
|
|
298
300
|
const dis = r.disabled ? C.gray : '';
|
|
299
301
|
const labelRow = out.length;
|
|
300
302
|
out.push(fitExactly(ptr + nameStyle + dis + r.label + C.reset + badge, cols));
|
|
301
303
|
// The whole label row is a click target: clicking selects the item (and a second click / the footer
|
|
302
304
|
// activates it), mirroring the tasks browser.
|
|
303
305
|
hits.push({ row: labelRow, col0: 0, col1: cols - 1, kind: 'regRow', index: i });
|
|
304
|
-
for (const line of wrapText(r.description, descW)) {
|
|
306
|
+
for (const line of wrapText(uiText(r.description), descW)) {
|
|
305
307
|
out.push(C.gray + ' ' + line + C.reset);
|
|
306
308
|
}
|
|
307
309
|
}
|
|
@@ -329,13 +331,13 @@ export function renderRegistryBrowser(state, cols, rows) {
|
|
|
329
331
|
// does there (and it is the only way to pick up a newer version).
|
|
330
332
|
const cur = all[sel];
|
|
331
333
|
const action = !cur ? ''
|
|
332
|
-
: cur.disabled ? 'Enter does nothing
|
|
334
|
+
: cur.disabled ? uiText('Enter does nothing — this entry has no SKILL.md')
|
|
333
335
|
: cur.action === 'reinstall-skill' || cur.action === 'reinstall-plugin'
|
|
334
|
-
? 'Enter reinstalls ' + cur.name + ' (replaces the local copy)'
|
|
336
|
+
? uiText('Enter reinstalls ') + cur.name + uiText(' (replaces the local copy)')
|
|
335
337
|
: cur.action === 'install-skill' || cur.action === 'install-plugin'
|
|
336
|
-
? 'Enter installs ' + cur.name + ' from the repository'
|
|
337
|
-
: cur.action.startsWith('remove') ? 'Enter removes the local copy of ' + cur.name
|
|
338
|
-
: 'loaded plugin
|
|
338
|
+
? uiText('Enter installs ') + cur.name + uiText(' from the repository')
|
|
339
|
+
: cur.action.startsWith('remove') ? uiText('Enter removes the local copy of ') + cur.name
|
|
340
|
+
: uiText('loaded plugin — switch to the On disk tab to remove it');
|
|
339
341
|
while (out.length < rows - 2) out.push('');
|
|
340
342
|
out.push(fitExactly(C.gray + ' ' + action + C.reset, cols));
|
|
341
343
|
out.push(rule);
|
package/src/rows-plan-bg.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import { C } from './colors.js';
|
|
7
7
|
import { col, fitAnsi, visualCol, wrapWords } from './text.js';
|
|
8
|
+
import { uiText } from './i18n.js';
|
|
8
9
|
import { renderMdText } from './markdown.js';
|
|
9
10
|
|
|
10
11
|
/**
|
|
@@ -24,7 +25,7 @@ export function planRows(msg, width) {
|
|
|
24
25
|
const full = (l, r) => bar(l + '─'.repeat(inner) + r);
|
|
25
26
|
// Header: the card's single column is captioned "Plan".
|
|
26
27
|
out.push({ text: full('╭', '╮'), ind: '', raw: true });
|
|
27
|
-
out.push({ text: bar('│') + fitAnsi(col(' Plan', C.cyan + C.bold), inner) + bar('│'), ind: '', raw: true });
|
|
28
|
+
out.push({ text: bar('│') + fitAnsi(col(' ' + uiText('Plan'), C.cyan + C.bold), inner) + bar('│'), ind: '', raw: true });
|
|
28
29
|
out.push({ text: bar('├' + '─'.repeat(inner) + '┤'), ind: '', raw: true });
|
|
29
30
|
const lines = String(msg.text || '').replace(/\r\n/g, '\n').split('\n');
|
|
30
31
|
while (lines.length && lines[lines.length - 1].trim() === '') lines.pop();
|
|
@@ -76,7 +77,7 @@ export function bgTaskRows(msg, width, expanded) {
|
|
|
76
77
|
}
|
|
77
78
|
if (lines.length > MAX) {
|
|
78
79
|
// Only reachable while COLLAPSED: expanded, `MAX` is the whole list and this cannot fire.
|
|
79
|
-
out.push({ text: col(`… ${lines.length - MAX} more lines, ctrl+o to expand
|
|
80
|
+
out.push({ text: col(uiText(`… ${lines.length - MAX} more lines, ctrl+o to expand`), C.gray), ind: indent, raw: true });
|
|
80
81
|
}
|
|
81
82
|
}
|
|
82
83
|
return out;
|