@hncode/hncode 0.8.2 → 0.8.4

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.
@@ -1,235 +1,251 @@
1
- // A tool result row: the `↳` block under the tool line.
2
- //
3
- // The shape is the same for every tool, and the parts differ:
4
- //
5
- // ● Used Edit (a.js) +1 -1 <- the tool line, counts only
6
- // ↳ 179 - old line <- ONE `↳` on the FIRST body row
7
- // 179 + new line
8
- // Edited a.js: replaced 1 occurrence(s). <- receipt LAST
9
- //
10
- // An Edit's DIFF is the body and carries the marker, with the receipt moved to the bottom; every other tool's output
11
- // IS the body and carries the marker on its first line.
12
-
13
- import { C } from './colors.js';
14
- import { col, expandTabs, fitAnsi, sanitizeText, visualCol, wrapWords } from './text.js';
15
- import { uiText } from './i18n.js';
16
- import { langForPath } from './highlight.js';
17
- import { paintCode } from './diff.js';
18
-
19
- export function toolResultRows(msg, width, expanded) {
20
- // The marker sits in the SAME COLUMN as the tool bullet `●` (columns 0-1), so `↳ ` starts at column 0 and the output
21
- // text is indented to the tool text column. ONE `↳` per result, however many lines it has.
22
- const pre = '↳ ';
23
- const cont = ' ';
24
- const bodyW = Math.max(1, width - visualCol(cont));
25
- // Strip escapes BEFORE splitting: a file or a command can emit raw cursor codes, and letting them through made the
26
- // terminal OBEY them — recolouring the transcript, resetting styles mid-row, clearing the screen over the composer.
27
- const rawLines = sanitizeText(msg.text).replace(/\r\n/g, '\n').split('\n');
28
- // A TAB counts as ZERO columns in our width maths but advances to the next tab stop in a real terminal. A row
29
- // containing one was therefore padded short and the terminal kept the old frame's glyphs in the gap — the "residue"
30
- // under tool output. Read rows are the exception: their first tab is the line-number separator and is handled in the
31
- // Read branch below, where the CONTENT half is expanded too.
32
- if (!msg.readPath) for (let k = 0; k < rawLines.length; k++) rawLines[k] = expandTabs(rawLines[k]);
33
- // Trim blank lines at BOTH ends: commands typically emit a trailing newline, and some tools emit a leading one. A
34
- // stray blank line would render as a lone "↳ " with no content.
35
- while (rawLines.length && rawLines[0].trim() === '') rawLines.shift();
36
- while (rawLines.length && rawLines[rawLines.length - 1].trim() === '') rawLines.pop();
37
- // COLLAPSED IS A GLANCE; EXPANDED IS A READ, NOT A DUMP.
38
- //
39
- // Expanded used to be `Infinity`, so Ctrl+O on a command that printed 3000 lines put all 3000 into the transcript: the
40
- // conversation above it scrolled away, the scrollbar became meaningless, and every frame afterwards paid to lay out
41
- // rows nobody would read. The cap here is what a person can actually scan; past that the output belongs in a file,
42
- // which is what the hint says.
43
- const MAX_RESULT_LINES = expanded ? 400 : 12;
44
- // The exit-code line is bookkeeping for the AGENT and is NEVER shown to the user — success or failure. A failure is
45
- // already legible from the RED result text below. `[error: …]` is kept: it carries the message, not just a code.
46
- const visible = rawLines.filter((ln) => !/^\[exit code:/.test(ln.trim()));
47
- const failed = msg.failed === true;
48
- // A silent Bash keeps the ONE empty output row it had while running. Only Bash has this area: an empty Read/Grep
49
- // result must not grow a phantom line merely because the command itself printed nothing.
50
- const shown = visible.length ? visible.slice(0, MAX_RESULT_LINES) : (msg.toolName === 'Bash' ? [''] : []);
51
-
52
- const diffRows = [];
53
- const receiptRows = [];
54
- const hasDiff = Array.isArray(msg.diff); // empty array => receipt-only (Write)
55
-
56
- if (hasDiff) {
57
- // Same collapse rule as elsewhere: a short preview by default, everything once Ctrl+O expands. 16 rows (not 12)
58
- // because a change renders as a PAIR: `- old` then `+ new`.
59
- const MAX = expanded ? 400 : 16;
60
- // Show the unified diff WITH context rows (ctx = dim) so a change is not two orphaned blocks; `gap` rows mark where
61
- // unchanged lines were elided.
62
- const total = msg.diff.length;
63
- const wNo = Math.max(1, ...msg.diff.map((d) => String(d.no || 0).length));
64
- // THE COMMENT STATE TRAVELS BETWEEN THE ROWS of one diff, so a block comment that opens on a context row and closes
65
- // on a changed one stays grey across both.
66
- let inComment = false;
67
- let emitted = 0;
68
- for (const d of msg.diff) {
69
- if (emitted >= MAX) break;
70
- if (d.type === 'gap') {
71
- diffRows.push({ text: col(' ⋮', C.gray), ind: cont, raw: true });
72
- emitted++;
73
- continue;
74
- }
75
- const no = String(d.no || 0).padStart(wNo);
76
- const mark = d.type === 'add' ? '+' : d.type === 'del' ? '-' : ' ';
77
- // TABS ARE EXPANDED FIRST: `visualCol` counts one as zero columns, so an indented row measured short and its
78
- // background stopped before the right edge — the strip of page colour seen on Go diffs.
79
- const text = expandTabs(sanitizeText(d.text));
80
-
81
- // The sign, line number and content share the line's addition/deletion background; syntax highlighting changes
82
- // only the foreground. The wash runs to the right edge so adjacent changed rows read as one block.
83
- const isAdd = d.type === 'add';
84
- const isDel = d.type === 'del';
85
- const lineBg = isAdd ? C.diffAddBg : isDel ? C.diffDelBg : '';
86
- const fg = isAdd ? C.diffAddFg : isDel ? C.diffDelFg : C.white;
87
- const lead = col(`${no} ${mark} `, lineBg ? fg + lineBg : fg);
88
-
89
- if (!lineBg) {
90
- // CONTEXT LINES ARE CODE TOO. They are most of a diff and the part a reader scans to place the change, so they
91
- // get the same highlighter, on the CODE SURFACE rather than the raw page — a changed block sits on one recessed
92
- // field. The +/- rows around it keep their green/red wash.
93
- const ctxPainted = paintCode(text, d.lang, '', inComment);
94
- inComment = ctxPainted.inComment;
95
- const ctxRow = (lead + ctxPainted.text).replace(/\x1b\[0m/g, `\x1b[0m${C.codeBg}`);
96
- diffRows.push({ text: C.codeBg + fitAnsi(ctxRow, bodyW) + C.bgReset, ind: cont, raw: true });
97
- emitted++;
98
- continue;
99
- }
100
-
101
- // Keep only the line-level wash and syntax foreground. Positional word pairing can highlight the wrong token when
102
- // lines are reordered or punctuation changes, so no word tint is applied.
103
- const painted = paintCode(text, d.lang, lineBg, inComment);
104
- inComment = painted.inComment;
105
- const used = visualCol(`${no} ${mark} `) + visualCol(painted.text);
106
- const pad = ' '.repeat(Math.max(0, bodyW - used));
107
- // The padding is INSIDE the wash, so it must not end with a bare reset: `col()` closes with ESC[0m, which would
108
- // drop the background on the last column and leave a strip of page colour at the right edge.
109
- diffRows.push({ text: lead + painted.text + lineBg + pad + '\x1b[39m', ind: cont, raw: true });
110
- emitted++;
111
- }
112
- if (total > emitted) {
113
- diffRows.push({
114
- // A DEAD-END Ctrl+O IS WORSE THAN NO HINT. Collapsed it expands; already expanded there is nothing left to
115
- // reveal, so the line says how much was withheld instead of inviting a key that would do nothing.
116
- text: col(expanded
117
- ? uiText(`… (${total - emitted} more lines not shown; press ctrl+e for the whole thing)`)
118
- : uiText(`… (${total - emitted} more lines, ctrl+o to expand)`), C.gray),
119
- ind: cont, raw: true,
120
- });
121
- }
122
- if (total === 0) diffRows.push({ text: col('(no changes)', C.gray), ind: cont, raw: true });
123
- // The receipt: one line per source line of the tool's own message.
124
- for (const ln of shown) {
125
- const color = failed ? C.red : C.gray;
126
- for (const t of (ln === '' ? [''] : wrapWords(ln, bodyW))) {
127
- receiptRows.push({ text: t, ind: cont, color });
128
- }
129
- }
130
- } else {
131
- // No diff: the tool's own output is the body.
132
- //
133
- // A READ RESULT IS SOURCE CODE, so its numbered lines are syntax-coloured — the file's own language, carried on the
134
- // message by the tool_result handler, which is the last place that still knows the path. The header and any error
135
- // keep the plain grey: those are the tool's own words, not the file's.
136
- const readLang = msg.readPath ? langForPath(msg.readPath) : null;
137
- // THE GUTTER IS ONE WIDTH FOR THE WHOLE BLOCK. The tool sends `N<TAB>text`, and the tab is what used to place the
138
- // code — so `1` and `10` started their text at different columns once the tab was expanded. Padding the NUMBER to
139
- // the widest one in this block keeps the digits left-aligned and the code aligned under itself.
140
- const numW = shown.reduce((w, ln) => {
141
- const m = /^(\d+)\t/.exec(ln);
142
- return m ? Math.max(w, m[1].length) : w;
143
- }, 0);
144
- let inComment = false;
145
- shown.forEach((ln, idx) => {
146
- const isErr = /^\[error:/.test(ln.trim());
147
- // A failed run is red throughout so it cannot be skimmed past.
148
- const color = isErr ? C.red : (failed ? C.red : C.gray);
149
- // `N<TAB>text` is the Read tool's own line format; only those rows are file content.
150
- const numbered = readLang && !isErr ? /^(\d+)\t([\s\S]*)$/.exec(ln) : null;
151
- if (numbered) {
152
- // A READ CAN START INSIDE A BLOCK COMMENT. The `/**` may sit above the first visible row, so no cross-row state
153
- // can tell the highlighter the first row is a comment — it coloured `of`, `switch`, `from` in a JSDoc paragraph
154
- // as keywords. A row that is plainly part of a block comment (` * …`, an opening `/**`, or the closing `*/`) is
155
- // painted as a comment on its own merits; the opener also seeds the rows that follow.
156
- const content = expandTabs(numbered[2]);
157
- const isBlockCommentRow = /^\s*(?:\/\*|\*|\*\/|\*\*)/.test(content);
158
- const painted = paintCode(content, readLang, '', inComment || isBlockCommentRow);
159
- if (/\/\*/.test(content) && !/\*\//.test(content)) inComment = true;
160
- else if (/\*\//.test(content)) inComment = false;
161
- // The separator is a SPACE: the number is padded to the block's widest, so every row's code starts in the same
162
- // column.
163
- const text = `${col(numbered[1].padStart(numW), C.white)} ${painted.text}`;
164
- // A Read row is TRUNCATED, not wrapped. Wrapping folded an overlong line onto a second row without a line
165
- // number, and that second row was exactly what a differential painter could not reconcile with the previous
166
- // frame: the leftover glyphs stayed under it as residue. One source line is one display row.
167
- //
168
- // The background has to survive the row's own resets: every syntax token closes with `ESC[0m`, and the padding
169
- // `fitAnsi` adds sits AFTER the last one. Re-asserting after each reset — the same trick `paintCode` uses for a
170
- // diff wash — keeps the surface on the padding too, so the block reads solid.
171
- const surfaced = text.replace(/\x1b\[0m/g, `\x1b[0m${C.codeBg}`);
172
- diffRows.push({ text: C.codeBg + fitAnsi(surfaced, bodyW) + C.bgReset, ind: idx === 0 ? pre : cont, raw: true });
173
- return;
174
- }
175
- const wrapped = ln === '' ? [''] : wrapWords(ln, bodyW);
176
- // BASH OUTPUT SITS ON THE TERMINAL SURFACE, like text inside a real terminal emulator: the darkest recess on
177
- // screen. Other tools keep the plain page background, so a command's output reads as "a terminal ran this" rather
178
- // than as an ordinary result.
179
- const termSurface = msg.toolName === 'Bash' || msg.toolName === 'Shell';
180
- wrapped.forEach((t, i) => {
181
- if (termSurface) {
182
- const surfaced = t.replace(/\x1b\[0m/g, `\x1b[0m${C.termBg}${color}`);
183
- // A FOREGROUND IS NOT OPTIONAL HERE.
184
- //
185
- // This set only the background, so the text fell back to the terminal's own default colour. Against the very
186
- // dark `termBg` that is readable on a light default and invisible on a dark one — which is exactly what a
187
- // dark-background terminal showed: a solid dark block with no words in it.
188
- //
189
- // The `\x1b[0m` inside the row closes EVERYTHING, background included, so both have to be re-asserted after
190
- // each one: the wash and the colour.
191
- diffRows.push({ text: C.termBg + color + fitAnsi(surfaced, bodyW) + C.bgReset, ind: idx === 0 && i === 0 ? pre : cont, raw: true });
192
- } else {
193
- diffRows.push({ text: t, ind: idx === 0 && i === 0 ? pre : cont, color });
194
- }
195
- });
196
- });
197
- if (visible.length > MAX_RESULT_LINES) {
198
- // THE HINT SAYS HOW TO SEE THE REST, and what "the rest" is depends on where we are.
199
- //
200
- // Collapsed: Ctrl+O expands — that is the whole point of the line.
201
- // Already expanded: there is no more to reveal here, so inviting another Ctrl+O would be a dead end. Say how much
202
- // was withheld and where it does live, which is the tool result the model was given (and, when the result was
203
- // spilled, the file it was written to).
204
- const rest = visible.length - MAX_RESULT_LINES;
205
- const text = expanded
206
- ? uiText(`… ${rest} more line${rest === 1 ? '' : 's'} not shown; ctrl+o collapses, ctrl+e shows all of it`)
207
- : uiText(`… ${rest} more lines, ctrl+o to expand`);
208
- diffRows.push({
209
- text,
210
- ind: cont,
211
- color: failed ? C.red : C.gray,
212
- });
213
- }
214
- }
215
-
216
- const out = [];
217
- for (const r of diffRows) out.push(r);
218
- for (const r of receiptRows) out.push(r);
219
- // Put the `↳` on the FIRST rendered row (diff or output) — never on the receipt, which sits last.
220
- if (out.length && out[0].ind === cont && Array.isArray(msg.diff) && msg.diff.length > 0) {
221
- out[0] = { ...out[0], ind: pre };
222
- }
223
- // The TOOL ROW may already have drawn this result's body — a Write streams the file's content up there — in which
224
- // case the receipt below is the whole result and must NOT open a second `↳` block:
225
- // ● Used Write (a.txt) 1.3KB
226
- // ↳ 1 <content> <- the body, marked on the tool row
227
- // ↳ File written: a.txt (1372 bytes) <- the stray second `↳`, now plain
228
- // One body, one marker. `bodyOnToolRow` is set by the tool_result handler, the only place that knows whether the tool
229
- // row had content to show.
230
- if (msg.bodyOnToolRow && out.length && out[0].ind === pre) {
231
- out[0] = { ...out[0], ind: cont };
232
- }
233
- // Nothing to show (empty output) -> render no row at all rather than a dangling "↳ ".
234
- return out;
235
- }
1
+ // A tool result row: the `↳` block under the tool line.
2
+ //
3
+ // The shape is the same for every tool, and the parts differ:
4
+ //
5
+ // ● Used Edit (a.js) +1 -1 <- the tool line, counts only
6
+ // ↳ 179 - old line <- ONE `↳` on the FIRST body row
7
+ // 179 + new line
8
+ // Edited a.js: replaced 1 occurrence(s). <- receipt LAST
9
+ //
10
+ // An Edit's DIFF is the body and carries the marker, with the receipt moved to the bottom; every other tool's output
11
+ // IS the body and carries the marker on its first line.
12
+
13
+ import { C } from './colors.js';
14
+ import { col, expandTabs, fitAnsi, sanitizeText, visualCol, wrapWords } from './text.js';
15
+ import { uiText } from './i18n.js';
16
+ import { langForPath } from './highlight.js';
17
+ import { paintCode } from './diff.js';
18
+
19
+ export function toolResultRows(msg, width, expanded) {
20
+ // The marker sits in the SAME COLUMN as the tool bullet `●` (columns 0-1), so `↳ ` starts at column 0 and the output
21
+ // text is indented to the tool text column. ONE `↳` per result, however many lines it has.
22
+ //
23
+ // The marker carries the STRUCTURAL colour, not the body's. A `↳` is a piece of chrome —
24
+ // it says "what follows belongs to the line above" — and it was inheriting the result's
25
+ // grey, which on a dark page made it a smudge in the margin rather than a marker. The
26
+ // plain text is kept for the comparisons below and for the width maths; the coloured one
27
+ // is what is drawn.
28
+ //
29
+ // IT CLOSES WITH THE ROW'S OWN COLOUR, NOT A RESET.
30
+ //
31
+ // The body colour is wrapped around the whole line by the caller (`color + text + reset`),
32
+ // so whatever this marker does to the foreground has to be undone before the body text
33
+ // follows. A `ESC[0m` closed the caller's colour too, and on a failed result the first line
34
+ // — the one carrying the marker — came out default grey while every line under it was red.
35
+ // Restating the body colour is the only form that survives either way.
36
+ const PRE = '↳ ';
37
+ const bodyFg = msg.failed ? C.red : C.gray;
38
+ const pre = C.marker + '↳ ' + bodyFg;
39
+ const cont = ' ';
40
+ const bodyW = Math.max(1, width - visualCol(cont));
41
+ // Strip escapes BEFORE splitting: a file or a command can emit raw cursor codes, and letting them through made the
42
+ // terminal OBEY them — recolouring the transcript, resetting styles mid-row, clearing the screen over the composer.
43
+ const rawLines = sanitizeText(msg.text).replace(/\r\n/g, '\n').split('\n');
44
+ // A TAB counts as ZERO columns in our width maths but advances to the next tab stop in a real terminal. A row
45
+ // containing one was therefore padded short and the terminal kept the old frame's glyphs in the gap — the "residue"
46
+ // under tool output. Read rows are the exception: their first tab is the line-number separator and is handled in the
47
+ // Read branch below, where the CONTENT half is expanded too.
48
+ if (!msg.readPath) for (let k = 0; k < rawLines.length; k++) rawLines[k] = expandTabs(rawLines[k]);
49
+ // Trim blank lines at BOTH ends: commands typically emit a trailing newline, and some tools emit a leading one. A
50
+ // stray blank line would render as a lone "↳ " with no content.
51
+ while (rawLines.length && rawLines[0].trim() === '') rawLines.shift();
52
+ while (rawLines.length && rawLines[rawLines.length - 1].trim() === '') rawLines.pop();
53
+ // COLLAPSED IS A GLANCE; EXPANDED IS A READ, NOT A DUMP.
54
+ //
55
+ // Expanded used to be `Infinity`, so Ctrl+O on a command that printed 3000 lines put all 3000 into the transcript: the
56
+ // conversation above it scrolled away, the scrollbar became meaningless, and every frame afterwards paid to lay out
57
+ // rows nobody would read. The cap here is what a person can actually scan; past that the output belongs in a file,
58
+ // which is what the hint says.
59
+ const MAX_RESULT_LINES = expanded ? 400 : 12;
60
+ // The exit-code line is bookkeeping for the AGENT and is NEVER shown to the user — success or failure. A failure is
61
+ // already legible from the RED result text below. `[error: …]` is kept: it carries the message, not just a code.
62
+ const visible = rawLines.filter((ln) => !/^\[exit code:/.test(ln.trim()));
63
+ const failed = msg.failed === true;
64
+ // A silent Bash keeps the ONE empty output row it had while running. Only Bash has this area: an empty Read/Grep
65
+ // result must not grow a phantom line merely because the command itself printed nothing.
66
+ const shown = visible.length ? visible.slice(0, MAX_RESULT_LINES) : (msg.toolName === 'Bash' ? [''] : []);
67
+
68
+ const diffRows = [];
69
+ const receiptRows = [];
70
+ const hasDiff = Array.isArray(msg.diff); // empty array => receipt-only (Write)
71
+
72
+ if (hasDiff) {
73
+ // Same collapse rule as elsewhere: a short preview by default, everything once Ctrl+O expands. 16 rows (not 12)
74
+ // because a change renders as a PAIR: `- old` then `+ new`.
75
+ const MAX = expanded ? 400 : 16;
76
+ // Show the unified diff WITH context rows (ctx = dim) so a change is not two orphaned blocks; `gap` rows mark where
77
+ // unchanged lines were elided.
78
+ const total = msg.diff.length;
79
+ const wNo = Math.max(1, ...msg.diff.map((d) => String(d.no || 0).length));
80
+ // THE COMMENT STATE TRAVELS BETWEEN THE ROWS of one diff, so a block comment that opens on a context row and closes
81
+ // on a changed one stays grey across both.
82
+ let inComment = false;
83
+ let emitted = 0;
84
+ for (const d of msg.diff) {
85
+ if (emitted >= MAX) break;
86
+ if (d.type === 'gap') {
87
+ diffRows.push({ text: col(' ⋮', C.gray), ind: cont, raw: true });
88
+ emitted++;
89
+ continue;
90
+ }
91
+ const no = String(d.no || 0).padStart(wNo);
92
+ const mark = d.type === 'add' ? '+' : d.type === 'del' ? '-' : ' ';
93
+ // TABS ARE EXPANDED FIRST: `visualCol` counts one as zero columns, so an indented row measured short and its
94
+ // background stopped before the right edge — the strip of page colour seen on Go diffs.
95
+ const text = expandTabs(sanitizeText(d.text));
96
+
97
+ // The sign, line number and content share the line's addition/deletion background; syntax highlighting changes
98
+ // only the foreground. The wash runs to the right edge so adjacent changed rows read as one block.
99
+ const isAdd = d.type === 'add';
100
+ const isDel = d.type === 'del';
101
+ const lineBg = isAdd ? C.diffAddBg : isDel ? C.diffDelBg : '';
102
+ const fg = isAdd ? C.diffAddFg : isDel ? C.diffDelFg : C.white;
103
+ const lead = col(`${no} ${mark} `, lineBg ? fg + lineBg : fg);
104
+
105
+ if (!lineBg) {
106
+ // CONTEXT LINES ARE CODE TOO. They are most of a diff and the part a reader scans to place the change, so they
107
+ // get the same highlighter, on the CODE SURFACE rather than the raw page — a changed block sits on one recessed
108
+ // field. The +/- rows around it keep their green/red wash.
109
+ const ctxPainted = paintCode(text, d.lang, '', inComment);
110
+ inComment = ctxPainted.inComment;
111
+ const ctxRow = (lead + ctxPainted.text).replace(/\x1b\[0m/g, `\x1b[0m${C.codeBg}`);
112
+ diffRows.push({ text: C.codeBg + fitAnsi(ctxRow, bodyW) + C.bgReset, ind: cont, raw: true });
113
+ emitted++;
114
+ continue;
115
+ }
116
+
117
+ // Keep only the line-level wash and syntax foreground. Positional word pairing can highlight the wrong token when
118
+ // lines are reordered or punctuation changes, so no word tint is applied.
119
+ const painted = paintCode(text, d.lang, lineBg, inComment);
120
+ inComment = painted.inComment;
121
+ const used = visualCol(`${no} ${mark} `) + visualCol(painted.text);
122
+ const pad = ' '.repeat(Math.max(0, bodyW - used));
123
+ // The padding is INSIDE the wash, so it must not end with a bare reset: `col()` closes with ESC[0m, which would
124
+ // drop the background on the last column and leave a strip of page colour at the right edge.
125
+ diffRows.push({ text: lead + painted.text + lineBg + pad + '\x1b[39m', ind: cont, raw: true });
126
+ emitted++;
127
+ }
128
+ if (total > emitted) {
129
+ diffRows.push({
130
+ // A DEAD-END Ctrl+O IS WORSE THAN NO HINT. Collapsed it expands; already expanded there is nothing left to
131
+ // reveal, so the line says how much was withheld instead of inviting a key that would do nothing.
132
+ text: col(expanded
133
+ ? uiText(`… (${total - emitted} more lines not shown; press ctrl+e for the whole thing)`)
134
+ : uiText(`… (${total - emitted} more lines, ctrl+o to expand)`), C.gray),
135
+ ind: cont, raw: true,
136
+ });
137
+ }
138
+ if (total === 0) diffRows.push({ text: col('(no changes)', C.gray), ind: cont, raw: true });
139
+ // The receipt: one line per source line of the tool's own message.
140
+ for (const ln of shown) {
141
+ const color = failed ? C.red : C.gray;
142
+ for (const t of (ln === '' ? [''] : wrapWords(ln, bodyW))) {
143
+ receiptRows.push({ text: t, ind: cont, color });
144
+ }
145
+ }
146
+ } else {
147
+ // No diff: the tool's own output is the body.
148
+ //
149
+ // A READ RESULT IS SOURCE CODE, so its numbered lines are syntax-coloured — the file's own language, carried on the
150
+ // message by the tool_result handler, which is the last place that still knows the path. The header and any error
151
+ // keep the plain grey: those are the tool's own words, not the file's.
152
+ const readLang = msg.readPath ? langForPath(msg.readPath) : null;
153
+ // THE GUTTER IS ONE WIDTH FOR THE WHOLE BLOCK. The tool sends `N<TAB>text`, and the tab is what used to place the
154
+ // code — so `1` and `10` started their text at different columns once the tab was expanded. Padding the NUMBER to
155
+ // the widest one in this block keeps the digits left-aligned and the code aligned under itself.
156
+ const numW = shown.reduce((w, ln) => {
157
+ const m = /^(\d+)\t/.exec(ln);
158
+ return m ? Math.max(w, m[1].length) : w;
159
+ }, 0);
160
+ let inComment = false;
161
+ shown.forEach((ln, idx) => {
162
+ const isErr = /^\[error:/.test(ln.trim());
163
+ // A failed run is red throughout so it cannot be skimmed past.
164
+ const color = isErr ? C.red : (failed ? C.red : C.gray);
165
+ // `N<TAB>text` is the Read tool's own line format; only those rows are file content.
166
+ const numbered = readLang && !isErr ? /^(\d+)\t([\s\S]*)$/.exec(ln) : null;
167
+ if (numbered) {
168
+ // A READ CAN START INSIDE A BLOCK COMMENT. The `/**` may sit above the first visible row, so no cross-row state
169
+ // can tell the highlighter the first row is a comment — it coloured `of`, `switch`, `from` in a JSDoc paragraph
170
+ // as keywords. A row that is plainly part of a block comment (` * …`, an opening `/**`, or the closing `*/`) is
171
+ // painted as a comment on its own merits; the opener also seeds the rows that follow.
172
+ const content = expandTabs(numbered[2]);
173
+ const isBlockCommentRow = /^\s*(?:\/\*|\*|\*\/|\*\*)/.test(content);
174
+ const painted = paintCode(content, readLang, '', inComment || isBlockCommentRow);
175
+ if (/\/\*/.test(content) && !/\*\//.test(content)) inComment = true;
176
+ else if (/\*\//.test(content)) inComment = false;
177
+ // The separator is a SPACE: the number is padded to the block's widest, so every row's code starts in the same
178
+ // column.
179
+ const text = `${col(numbered[1].padStart(numW), C.white)} ${painted.text}`;
180
+ // A Read row is TRUNCATED, not wrapped. Wrapping folded an overlong line onto a second row without a line
181
+ // number, and that second row was exactly what a differential painter could not reconcile with the previous
182
+ // frame: the leftover glyphs stayed under it as residue. One source line is one display row.
183
+ //
184
+ // The background has to survive the row's own resets: every syntax token closes with `ESC[0m`, and the padding
185
+ // `fitAnsi` adds sits AFTER the last one. Re-asserting after each reset — the same trick `paintCode` uses for a
186
+ // diff wash — keeps the surface on the padding too, so the block reads solid.
187
+ const surfaced = text.replace(/\x1b\[0m/g, `\x1b[0m${C.codeBg}`);
188
+ diffRows.push({ text: C.codeBg + fitAnsi(surfaced, bodyW) + C.bgReset, ind: idx === 0 ? pre : cont, raw: true });
189
+ return;
190
+ }
191
+ const wrapped = ln === '' ? [''] : wrapWords(ln, bodyW);
192
+ // BASH OUTPUT SITS ON THE TERMINAL SURFACE, like text inside a real terminal emulator: the darkest recess on
193
+ // screen. Other tools keep the plain page background, so a command's output reads as "a terminal ran this" rather
194
+ // than as an ordinary result.
195
+ const termSurface = msg.toolName === 'Bash' || msg.toolName === 'Shell';
196
+ wrapped.forEach((t, i) => {
197
+ if (termSurface) {
198
+ const surfaced = t.replace(/\x1b\[0m/g, `\x1b[0m${C.termBg}${color}`);
199
+ // A FOREGROUND IS NOT OPTIONAL HERE.
200
+ //
201
+ // This set only the background, so the text fell back to the terminal's own default colour. Against the very
202
+ // dark `termBg` that is readable on a light default and invisible on a dark one — which is exactly what a
203
+ // dark-background terminal showed: a solid dark block with no words in it.
204
+ //
205
+ // The `\x1b[0m` inside the row closes EVERYTHING, background included, so both have to be re-asserted after
206
+ // each one: the wash and the colour.
207
+ diffRows.push({ text: C.termBg + color + fitAnsi(surfaced, bodyW) + C.bgReset, ind: idx === 0 && i === 0 ? pre : cont, raw: true });
208
+ } else {
209
+ diffRows.push({ text: t, ind: idx === 0 && i === 0 ? pre : cont, color });
210
+ }
211
+ });
212
+ });
213
+ if (visible.length > MAX_RESULT_LINES) {
214
+ // THE HINT SAYS HOW TO SEE THE REST, and what "the rest" is depends on where we are.
215
+ //
216
+ // Collapsed: Ctrl+O expands — that is the whole point of the line.
217
+ // Already expanded: there is no more to reveal here, so inviting another Ctrl+O would be a dead end. Say how much
218
+ // was withheld and where it does live, which is the tool result the model was given (and, when the result was
219
+ // spilled, the file it was written to).
220
+ const rest = visible.length - MAX_RESULT_LINES;
221
+ const text = expanded
222
+ ? uiText(`… ${rest} more line${rest === 1 ? '' : 's'} not shown; ctrl+o collapses, ctrl+e shows all of it`)
223
+ : uiText(`… ${rest} more lines, ctrl+o to expand`);
224
+ diffRows.push({
225
+ text,
226
+ ind: cont,
227
+ color: failed ? C.red : C.gray,
228
+ });
229
+ }
230
+ }
231
+
232
+ const out = [];
233
+ for (const r of diffRows) out.push(r);
234
+ for (const r of receiptRows) out.push(r);
235
+ // Put the `↳` on the FIRST rendered row (diff or output) — never on the receipt, which sits last.
236
+ if (out.length && out[0].ind === cont && Array.isArray(msg.diff) && msg.diff.length > 0) {
237
+ out[0] = { ...out[0], ind: pre };
238
+ }
239
+ // The TOOL ROW may already have drawn this result's body — a Write streams the file's content up there — in which
240
+ // case the receipt below is the whole result and must NOT open a second `↳` block:
241
+ // ● Used Write (a.txt) 1.3KB
242
+ // ↳ 1 <content> <- the body, marked on the tool row
243
+ // ↳ File written: a.txt (1372 bytes) <- the stray second `↳`, now plain
244
+ // One body, one marker. `bodyOnToolRow` is set by the tool_result handler, the only place that knows whether the tool
245
+ // row had content to show.
246
+ if (msg.bodyOnToolRow && out.length && out[0].ind === pre) {
247
+ out[0] = { ...out[0], ind: cont };
248
+ }
249
+ // Nothing to show (empty output) -> render no row at all rather than a dangling "↳ ".
250
+ return out;
251
+ }
@@ -75,6 +75,10 @@ export const SETTINGS_SCHEMA = [
75
75
  // forgetting it left /settings offering fewer themes than /theme did.
76
76
  { key: 'theme', ...en('dark', THEME_NAMES),
77
77
  ui: { tab: 'appearance', group: 'Display', label: 'Theme', hint: 'Colour scheme. `auto` follows the terminal background' } },
78
+ // The language of the TERMINAL UI. Only what a person reads is translated; the system
79
+ // prompt and everything the model reads stay English.
80
+ { key: 'lang', ...en('en', ['en', 'zh']),
81
+ ui: { tab: 'appearance', group: 'Display', label: 'Interface language', hint: 'The language the terminal UI speaks. The model still reads English' } },
78
82
  { key: 'calm_mode', ...bool(false),
79
83
  ui: { tab: 'appearance', group: 'Display', label: 'Calm mode', hint: 'Fewer decorative flourishes; keeps the information, drops the animation' } },
80
84
  { key: 'reduced_motion', ...bool(false),
@@ -219,6 +223,11 @@ export const SETTINGS_SCHEMA = [
219
223
  ui: { tab: 'model', group: 'Thinking', label: 'Max output size', hint: 'Startup default for the reply length; 0 means fall back' } },
220
224
  { key: 'reasoning', ...bool(false),
221
225
  ui: { tab: 'model', group: 'Thinking', label: 'Reasoning by default', hint: 'Whether new sessions start with thinking on' } },
226
+ // Whether the model is told to wrap its reasoning in <|thinking|> tags. THREE states,
227
+ // not two: `auto` follows the model (a model with a reasoning channel does not need
228
+ // the tag, one without it does), and the other two decide by hand.
229
+ { key: 'thinking_tags', ...en('auto', ['auto', 'on', 'off']),
230
+ ui: { tab: 'model', group: 'Thinking', label: 'Inline reasoning tags', hint: 'Ask the model to wrap its reasoning in <|thinking|> tags. Auto: only for a model with no reasoning channel of its own' } },
222
231
  { key: 'plugins_dir', ...str(''),
223
232
  ui: { tab: 'advanced', group: 'MCP', label: 'Plugins directory', hint: 'Where plugins load from; defaults to ~/.hncode/plugins' } },
224
233
  { key: 'web_host', ...str('127.0.0.1'),
@@ -241,7 +241,7 @@ export function renderSwarmProgress(snapshot, width, opts = {}) {
241
241
  const lines = [];
242
242
 
243
243
  // ---- header: `─ Agent Swarm ─ desc ─ model ─────` ----
244
- const title = col('Agent Swarm', C.spring + C.bold);
244
+ const title = col(uiText('Agent Swarm'), C.spring + C.bold);
245
245
  const suffix = [];
246
246
  if (desc) suffix.push(col(' \u2500 ', C.spring) + col(desc, C.white));
247
247
  if (model) suffix.push(col(' \u2500 ', C.spring) + col(model, C.gray));