@hncode/hncode 0.7.3 → 0.7.5
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 +24 -2
- package/README_CN.md +24 -2
- package/package.json +1 -1
- package/src/agent.js +10 -5
- package/src/banner.js +222 -0
- package/src/colors.js +50 -1
- package/src/config.js +46 -2
- package/src/highlight.js +432 -0
- package/src/hooks.js +44 -7
- package/src/i18n.js +5 -5
- package/src/registry-browser.js +1 -1
- package/src/settings-schema.js +4 -0
- package/src/team-panel.js +1 -1
- package/src/term.js +971 -764
- package/src/tools/edit.js +63 -12
- package/src/tools/memory.js +92 -80
- package/src/tui.js +22251 -21521
- package/src/wordlist.js +6 -0
package/src/tools/edit.js
CHANGED
|
@@ -47,7 +47,12 @@ number (Read prints '12' then a tab, then the indented code, so you send the lea
|
|
|
47
47
|
COUNT FIRST: in line-range mode new_string must have exactly end_line - start_line + 1 lines, and every line
|
|
48
48
|
you write must be one you are replacing. A line outside the range is not deleted and survives as a duplicate.
|
|
49
49
|
|
|
50
|
-
Omitting new_string entirely is an error — pass "" to delete
|
|
50
|
+
Omitting new_string entirely is an error — pass "" to delete.
|
|
51
|
+
|
|
52
|
+
AFTER A SUCCESSFUL EDIT, the result quotes the file around what you wrote — five lines either side, in the same
|
|
53
|
+
numbered form Read uses. So editing a region you have just edited does NOT need another Read of it: the previous
|
|
54
|
+
Edit already shows you those lines as they are on disk now. Read again when you need a part of the file the edit
|
|
55
|
+
did not show, or when a different tool or the user has changed the file since.`,
|
|
51
56
|
parameters: {
|
|
52
57
|
type: 'object',
|
|
53
58
|
properties: {
|
|
@@ -225,13 +230,13 @@ Omitting new_string entirely is an error — pass "" to delete.`,
|
|
|
225
230
|
ensureDir(p);
|
|
226
231
|
fs.writeFileSync(p, updated, 'utf8');
|
|
227
232
|
} catch (e) { return `Error writing ${args.path}: ${e.message}`; }
|
|
228
|
-
//
|
|
229
|
-
//
|
|
230
|
-
// model has never seen. Regenerating trust from the arguments would let it edit again on top of a change
|
|
231
|
-
// it only THINKS it knows. It reads the file instead.
|
|
233
|
+
// The returned excerpt is read from disk after the write. Refresh the snapshot for precisely those returned
|
|
234
|
+
// lines, so a follow-up edit inside the excerpt needs no separate Read; regions outside it remain untrusted.
|
|
232
235
|
const removed = end - start + 1;
|
|
233
|
-
if (ctx) ctx.lastEditDiff = buildEditDiff({ fileLines: lines, oldText: replacedText, newText: inserted.join('\n'), startLine: start });
|
|
234
|
-
|
|
236
|
+
if (ctx) ctx.lastEditDiff = buildEditDiff({ fileLines: lines, oldText: replacedText, newText: inserted.join('\n'), startLine: start, filePath: p });
|
|
237
|
+
const written = Math.max(1, inserted.length);
|
|
238
|
+
const excerpt = readAfterEdit(p, start, written, ctx);
|
|
239
|
+
return `Edited ${args.path}: replaced lines ${start}-${end} (${removed} line${removed === 1 ? '' : 's'}) with ${inserted.length} line${inserted.length === 1 ? '' : 's'}.\n\n${excerpt}`.trimEnd();
|
|
235
240
|
}
|
|
236
241
|
|
|
237
242
|
// ---- SUBSTRING mode (old_string -> new_string) --------------------------
|
|
@@ -364,9 +369,8 @@ Omitting new_string entirely is an error — pass "" to delete.`,
|
|
|
364
369
|
ensureDir(p);
|
|
365
370
|
fs.writeFileSync(p, updated, 'utf8');
|
|
366
371
|
} catch (e) { return `Error writing ${args.path}: ${e.message}`; }
|
|
367
|
-
//
|
|
368
|
-
//
|
|
369
|
-
// Read of them.
|
|
372
|
+
// Only the post-edit excerpt returned below refreshes the Read snapshot. An unrelated part of the file is not
|
|
373
|
+
// made trusted merely because this replacement succeeded.
|
|
370
374
|
const occurrences = replace_all ? count : 1;
|
|
371
375
|
// Publish the diff for the UI (same contract as the line-range path above).
|
|
372
376
|
// `startLine` comes from the actual match offset, so a snippet that starts
|
|
@@ -379,9 +383,17 @@ Omitting new_string entirely is an error — pass "" to delete.`,
|
|
|
379
383
|
oldText: old_string,
|
|
380
384
|
newText: new_string,
|
|
381
385
|
startLine,
|
|
386
|
+
filePath: p,
|
|
382
387
|
});
|
|
383
388
|
}
|
|
384
|
-
|
|
389
|
+
// Quote the result the same way the line-range path does, so neither mode leaves the model guessing.
|
|
390
|
+
const editedAt = matchAt >= 0 ? normContent.slice(0, matchAt).split('\n').length : 1;
|
|
391
|
+
// replace_all may change distant copies too. One excerpt cannot vouch for them, so do not authorize another
|
|
392
|
+
// edit based on an incomplete view; the caller must Read the desired region after a bulk replacement.
|
|
393
|
+
const writtenLines = normalizeText(new_string).split('\n').length;
|
|
394
|
+
const excerpt = readAfterEdit(p, editedAt, writtenLines, replace_all ? null : ctx);
|
|
395
|
+
if (replace_all && ctx && ctx.readPool) ctx.readPool.delete(p);
|
|
396
|
+
return `Edited ${args.path}: replaced ${occurrences} occurrence(s).\n\n${excerpt}`.trimEnd();
|
|
385
397
|
},
|
|
386
398
|
};
|
|
387
399
|
|
|
@@ -410,7 +422,43 @@ function leadingWs(s) {
|
|
|
410
422
|
* `fileLines` is the content BEFORE the edit, for the replaced region's START LINE to be meaningful:
|
|
411
423
|
* `startLine` is a line number in that same file.
|
|
412
424
|
*/
|
|
413
|
-
|
|
425
|
+
/** How many lines of the file to show on each side of a freshly edited region. */
|
|
426
|
+
const READ_AFTER_CONTEXT = 5;
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* The file around the region an edit just wrote, in the same `N<TAB>text` form the Read tool uses.
|
|
430
|
+
*
|
|
431
|
+
* WHY THE EDIT RETURNS IT: the result used to be one sentence — "replaced lines 5-7" — which tells the model what
|
|
432
|
+
* it asked for but not what the file now says. Every follow-up edit therefore began with a Read of the same lines,
|
|
433
|
+
* a whole round trip whose only purpose was to learn something the edit already knew.
|
|
434
|
+
*
|
|
435
|
+
* The excerpt is read from disk after the write, then precisely that region is registered in the Read pool. This
|
|
436
|
+
* makes the advertised "no second Read" path real without trusting the edit arguments or any unseen file region.
|
|
437
|
+
*
|
|
438
|
+
* Returns '' when the post-write read fails, leaving the old snapshot unchanged.
|
|
439
|
+
*/
|
|
440
|
+
function readAfterEdit(filePath, firstLine, lineCount, ctx) {
|
|
441
|
+
let text;
|
|
442
|
+
try { text = fs.readFileSync(filePath, 'utf8'); } catch { return ''; }
|
|
443
|
+
const lines = normalizeText(text).split('\n');
|
|
444
|
+
if (lines.length > 1 && lines[lines.length - 1] === '') lines.pop();
|
|
445
|
+
const total = lines.length;
|
|
446
|
+
const start = Math.max(1, firstLine - READ_AFTER_CONTEXT);
|
|
447
|
+
const end = Math.min(total, firstLine + Math.max(1, lineCount) - 1 + READ_AFTER_CONTEXT);
|
|
448
|
+
if (ctx && ctx.readPool) {
|
|
449
|
+
// The file changed, so every older region for this path is invalid; only the lines actually returned below
|
|
450
|
+
// may authorize a follow-up edit. Keep the same normalized hashing convention as Read.
|
|
451
|
+
ctx.readPool.set(filePath, {
|
|
452
|
+
fileHash: hashStr(text), lines: total,
|
|
453
|
+
regions: [{ start, end, hash: hashLines(lines, start, end) }],
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
const out = [];
|
|
457
|
+
for (let i = start; i <= end; i++) out.push(`${i}\t${lines[i - 1]}`);
|
|
458
|
+
return out.join('\n');
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function buildEditDiff({ fileLines, oldText, newText, startLine, filePath }) {
|
|
414
462
|
const lines = Array.isArray(fileLines) ? fileLines : [];
|
|
415
463
|
const before = [];
|
|
416
464
|
const after = [];
|
|
@@ -442,6 +490,9 @@ function buildEditDiff({ fileLines, oldText, newText, startLine }) {
|
|
|
442
490
|
old: normalizeText(oldText),
|
|
443
491
|
new: normalizeText(newText),
|
|
444
492
|
startLine,
|
|
493
|
+
// The file the change belongs to, so the viewer can pick a syntax highlighter from its extension. Without it
|
|
494
|
+
// the diff had no way to know whether it was looking at JavaScript or a Makefile.
|
|
495
|
+
path: filePath || '',
|
|
445
496
|
context: { before, after },
|
|
446
497
|
};
|
|
447
498
|
}
|
package/src/tools/memory.js
CHANGED
|
@@ -1,80 +1,92 @@
|
|
|
1
|
-
// Memory tool — the agent records a durable note for FUTURE sessions.
|
|
2
|
-
//
|
|
3
|
-
// Distinct from /personal: that file is the USER's standing preferences and the
|
|
4
|
-
// agent must never write it. This tool writes MEMORY.md, which is the agent's own
|
|
5
|
-
// scratchpad, injected into the system prompt on later turns (see config.js's
|
|
6
|
-
// readMemory). The point is to stop rediscovering the same facts every session —
|
|
7
|
-
// build quirks, a command that works here, a decision the user already made.
|
|
8
|
-
|
|
9
|
-
import { appendMemory, readMemoryRaw, memoryFile } from '../config.js';
|
|
10
|
-
|
|
11
|
-
export const spec = {
|
|
12
|
-
name: 'Memory',
|
|
13
|
-
description: `Save a durable note for FUTURE sessions,
|
|
14
|
-
|
|
15
|
-
Use it for something still true next week that you would otherwise rediscover: a build or test command that
|
|
16
|
-
works here, a layout quirk, a convention the user asked for, a decision already made, a gotcha that cost a
|
|
17
|
-
debugging cycle.
|
|
18
|
-
|
|
19
|
-
Not for: what is already in AGENTS.md or the code (read it — that is the source); the current task or its
|
|
20
|
-
progress (that is TodoList; this file outlives the task); secrets, tokens, credentials, or anything the user
|
|
21
|
-
asked you to keep quiet; one-off details.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
out
|
|
58
|
-
out.push(`
|
|
59
|
-
out.push(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
1
|
+
// Memory tool — the agent records a durable note for FUTURE sessions.
|
|
2
|
+
//
|
|
3
|
+
// Distinct from /personal: that file is the USER's standing preferences and the
|
|
4
|
+
// agent must never write it. This tool writes MEMORY.md, which is the agent's own
|
|
5
|
+
// scratchpad, injected into the system prompt on later turns (see config.js's
|
|
6
|
+
// readMemory). The point is to stop rediscovering the same facts every session —
|
|
7
|
+
// build quirks, a command that works here, a decision the user already made.
|
|
8
|
+
|
|
9
|
+
import { appendMemory, removeMemory, readMemoryRaw, memoryFile } from '../config.js';
|
|
10
|
+
|
|
11
|
+
export const spec = {
|
|
12
|
+
name: 'Memory',
|
|
13
|
+
description: `Save a durable note for FUTURE sessions, list what is saved, or remove one that is no longer true.
|
|
14
|
+
|
|
15
|
+
Use it for something still true next week that you would otherwise rediscover: a build or test command that
|
|
16
|
+
works here, a layout quirk, a convention the user asked for, a decision already made, a gotcha that cost a
|
|
17
|
+
debugging cycle.
|
|
18
|
+
|
|
19
|
+
Not for: what is already in AGENTS.md or the code (read it — that is the source); the current task or its
|
|
20
|
+
progress (that is TodoList; this file outlives the task); secrets, tokens, credentials, or anything the user
|
|
21
|
+
asked you to keep quiet; one-off details.
|
|
22
|
+
|
|
23
|
+
Remove a note whose rule has stopped being true — a stale rule keeps steering later sessions and is worse than no
|
|
24
|
+
rule at all. Pass enough of its text to identify it; every note containing that text is dropped.
|
|
25
|
+
|
|
26
|
+
One short note per call, in the user's language. Prefer several small notes over one long entry.
|
|
27
|
+
project (default) follows the repository; global applies to every workspace.`,
|
|
28
|
+
parameters: {
|
|
29
|
+
type: 'object',
|
|
30
|
+
properties: {
|
|
31
|
+
action: {
|
|
32
|
+
type: 'string',
|
|
33
|
+
enum: ['add', 'list', 'remove'],
|
|
34
|
+
description: 'add a note, list what is stored, or remove one. Defaults to add.',
|
|
35
|
+
},
|
|
36
|
+
note: {
|
|
37
|
+
type: 'string',
|
|
38
|
+
description: 'The note to save when adding; when removing, any text that identifies it.',
|
|
39
|
+
},
|
|
40
|
+
scope: {
|
|
41
|
+
type: 'string',
|
|
42
|
+
enum: ['project', 'global'],
|
|
43
|
+
description: 'Which memory to write to. Defaults to project.',
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
required: [],
|
|
47
|
+
},
|
|
48
|
+
async execute(args, ctx) {
|
|
49
|
+
// A malformed argument must not throw: an uncaught error here aborts the turn.
|
|
50
|
+
const action = String(args.action || 'add').toLowerCase();
|
|
51
|
+
const scope = String(args.scope || 'project').toLowerCase() === 'global' ? 'global' : 'project';
|
|
52
|
+
const workspace = (ctx && (ctx.workspace || ctx.cwd)) || process.cwd();
|
|
53
|
+
|
|
54
|
+
if (action === 'list') {
|
|
55
|
+
const project = readMemoryRaw('project', workspace).trim();
|
|
56
|
+
const global = readMemoryRaw('global', workspace).trim();
|
|
57
|
+
const out = [];
|
|
58
|
+
out.push(`project (${memoryFile('project', workspace)}):`);
|
|
59
|
+
out.push(project || ' (empty)');
|
|
60
|
+
out.push('');
|
|
61
|
+
out.push(`global (${memoryFile('global', workspace)}):`);
|
|
62
|
+
out.push(global || ' (empty)');
|
|
63
|
+
return out.join('\n');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (action === 'remove') {
|
|
67
|
+
// REMOVAL IS BY MATCHING TEXT, not by index: the caller knows what a note SAYS, and re-reading the file to
|
|
68
|
+
// find its line number is a step that can go stale between the read and the write.
|
|
69
|
+
const needle = String(args.note == null ? '' : args.note).trim();
|
|
70
|
+
if (!needle) return 'Error: `note` is required when action is "remove" — pass text from the note.';
|
|
71
|
+
const { removed, kept } = removeMemory(scope, workspace, needle);
|
|
72
|
+
if (!removed) return `No ${scope} note matched ${JSON.stringify(needle)}; nothing removed.`;
|
|
73
|
+
return `Removed ${removed} ${scope} note(s); ${kept} left standing.`;
|
|
74
|
+
}
|
|
75
|
+
if (action !== 'add') {
|
|
76
|
+
return `Error: unknown action ${JSON.stringify(action)}; use "add", "list" or "remove".`;
|
|
77
|
+
}
|
|
78
|
+
const note = String(args.note == null ? '' : args.note).trim();
|
|
79
|
+
if (!note) return 'Error: `note` is required when action is "add".';
|
|
80
|
+
// Guard against the file quietly becoming a transcript. The tool is for durable
|
|
81
|
+
// facts, and a pasted log is neither durable nor a fact.
|
|
82
|
+
if (note.length > 600) {
|
|
83
|
+
return `Error: the note is ${note.length} characters; keep it under 600 (save a summary, not a dump).`;
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
const file = appendMemory(scope, workspace, note);
|
|
87
|
+
return `Saved to ${scope} memory (${file}).`;
|
|
88
|
+
} catch (e) {
|
|
89
|
+
return `Error writing memory: ${e.message}`;
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
};
|