@hncode/hncode 0.8.2 → 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.
@@ -78,12 +78,12 @@ function singleLine(t) { return String(t == null ? '' : t).replace(/\s+/g, ' ').
78
78
  function relativeTime(ts) {
79
79
  if (!ts || !Number.isFinite(ts) || ts <= 0) return '';
80
80
  const d = Math.floor(Math.max(0, Date.now() - ts) / 1000);
81
- if (d < 60) return 'just now';
81
+ if (d < 60) return uiText('just now');
82
82
  const m = Math.floor(d / 60);
83
- if (m < 60) return m + 'm ago';
83
+ if (m < 60) return m + uiText('m ago');
84
84
  const h = Math.floor(m / 60);
85
- if (h < 24) return h + 'h ago';
86
- return Math.floor(h / 24) + 'd ago';
85
+ if (h < 24) return h + uiText('h ago');
86
+ return Math.floor(h / 24) + uiText('d ago');
87
87
  }
88
88
 
89
89
  export function visibleTasks(tasks, filter) {
@@ -147,22 +147,22 @@ export function renderTasksBrowser(state, cols, rows) {
147
147
  // gets a clickable span. `c` tracks the column so the spans land exactly on the
148
148
  // rendered words — the original drew `N/esc cancel` as one run, so the two keys
149
149
  // share the single "cancel" label and are measured as one span.
150
- const prefix = ' ' + C.orange + C.bold + 'Stop' + C.reset + ' ' + C.white + state.pendingStop + C.reset + '? ';
150
+ const prefix = ' ' + C.orange + C.bold + uiText('Stop') + C.reset + ' ' + C.white + state.pendingStop + C.reset + '? ';
151
151
  let c = visWidth(prefix);
152
- const yPart = key('Y') + ' ' + dim('confirm');
152
+ const yPart = key('Y') + ' ' + dim(uiText('confirm'));
153
153
  footerHits.push({ keyWord: 'Y', label: 'confirm', action: 'confirmStop', col0: c, col1: c + visWidth(yPart) - 1 });
154
154
  c += visWidth(yPart) + 2;
155
- const nPart = key('N') + dim('/') + key('esc') + ' ' + dim('cancel');
155
+ const nPart = key('N') + dim('/') + key('esc') + ' ' + dim(uiText('cancel'));
156
156
  footerHits.push({ keyWord: 'N/esc', label: 'cancel', action: 'cancelStop', col0: c, col1: c + visWidth(nPart) - 1 });
157
157
  footer = fitExactly(prefix + yPart + ' ' + nPart + ' ', cols);
158
158
  } else {
159
159
  const specs = [
160
- ['\u2191\u2193', 'select', 'select'],
161
- ['Enter/O', 'output', 'openOutput'],
162
- ['S', 'stop', 'requestStop'],
163
- ['R', 'refresh', 'refresh'],
164
- ['Tab', 'filter', 'toggleFilter'],
165
- ['Q/Esc', 'close', 'close'],
160
+ ['↑↓', uiText('select'), 'select'],
161
+ ['Enter/O', uiText('output'), 'openOutput'],
162
+ ['S', uiText('stop'), 'requestStop'],
163
+ ['R', uiText('refresh'), 'refresh'],
164
+ ['Tab', uiText('filter'), 'toggleFilter'],
165
+ ['Q/Esc', uiText('close'), 'close'],
166
166
  ];
167
167
  const parts = [];
168
168
  let cursor = 1;
@@ -238,34 +238,34 @@ export function renderTasksBrowser(state, cols, rows) {
238
238
 
239
239
  const detailLines = [];
240
240
  if (!task) {
241
- detailLines.push(C.gray + 'Select a task from the list.' + C.reset);
241
+ detailLines.push(C.gray + uiText('Select a task from the list.') + C.reset);
242
242
  } else {
243
243
  const label = (s) => C.gray + s.padEnd(14) + C.reset;
244
244
  const val = (s) => C.white + String(s) + C.reset;
245
- detailLines.push(label('Task ID:') + val(task.taskId));
246
- detailLines.push(label('Kind:') + val(task.kind));
245
+ detailLines.push(label(uiText('Task ID:')) + val(task.taskId));
246
+ detailLines.push(label(uiText('Kind:')) + val(task.kind));
247
247
  detailLines.push(label(uiText('Status:')) + statusColorToken(task.status) + uiText(STATUS_LABEL[task.status] || task.status) + C.reset);
248
- detailLines.push(label('Description:') + val(singleLine(task.description) || '-'));
248
+ detailLines.push(label(uiText('Description:')) + val(singleLine(task.description) || '-'));
249
249
  if (task.kind === 'process') {
250
- if (task.command && task.command !== task.description) detailLines.push(label('Command:') + val(singleLine(task.command)));
251
- if (task.pid) detailLines.push(label('Pid:') + C.gray + task.pid + C.reset);
252
- if (task.exitCode != null) detailLines.push(label('Exit code:') + C.gray + task.exitCode + C.reset);
250
+ if (task.command && task.command !== task.description) detailLines.push(label(uiText('Command:')) + val(singleLine(task.command)));
251
+ if (task.pid) detailLines.push(label(uiText('Pid:')) + C.gray + task.pid + C.reset);
252
+ if (task.exitCode != null) detailLines.push(label(uiText('Exit code:')) + C.gray + task.exitCode + C.reset);
253
253
  }
254
254
  if (task.kind === 'agent') {
255
- if (task.agentId) detailLines.push(label('Agent ID:') + val(task.agentId));
256
- if (task.subagentType) detailLines.push(label('Agent type:') + val(task.subagentType));
257
- if (task.model) detailLines.push(label('Model:') + val(task.model));
258
- if (task.thinkingEffort) detailLines.push(label('Effort:') + val(task.thinkingEffort));
255
+ if (task.agentId) detailLines.push(label(uiText('Agent ID:')) + val(task.agentId));
256
+ if (task.subagentType) detailLines.push(label(uiText('Agent type:')) + val(task.subagentType));
257
+ if (task.model) detailLines.push(label(uiText('Model:')) + val(task.model));
258
+ if (task.thinkingEffort) detailLines.push(label(uiText('Effort:')) + val(task.thinkingEffort));
259
259
  }
260
260
  if (task.kind === 'question') {
261
- detailLines.push(label('Questions:') + C.gray + task.questionCount + C.reset);
262
- if (task.toolCallId) detailLines.push(label('Tool call:') + C.gray + task.toolCallId + C.reset);
261
+ detailLines.push(label(uiText('Questions:')) + C.gray + task.questionCount + C.reset);
262
+ if (task.toolCallId) detailLines.push(label(uiText('Tool call:')) + C.gray + task.toolCallId + C.reset);
263
263
  }
264
264
  const timing = task.status === 'running'
265
265
  ? 'running ' + relativeTime(task.startedAt)
266
266
  : (task.endedAt ? 'finished ' + relativeTime(task.endedAt) : '');
267
- if (timing) detailLines.push(label('Time:') + C.gray + timing + C.reset);
268
- if (task.stopReason) detailLines.push(label('Reason:') + C.gray + singleLine(task.stopReason) + C.reset);
267
+ if (timing) detailLines.push(label(uiText('Time:')) + C.gray + timing + C.reset);
268
+ if (task.stopReason) detailLines.push(label(uiText('Reason:')) + C.gray + singleLine(task.stopReason) + C.reset);
269
269
  }
270
270
  while (detailLines.length < Math.max(0, detailH - 2)) detailLines.push('');
271
271
  const detailFrame = renderFrame('Detail', detailLines, rightW, detailH);
package/src/term.js CHANGED
@@ -218,30 +218,53 @@ const AMBIGUOUS = [
218
218
  [0x2103, 0x2103], [0x2105, 0x2105], [0x2109, 0x2109], [0x2113, 0x2113],
219
219
  [0x2116, 0x2116], [0x2121, 0x2122], [0x2126, 0x2126], [0x212B, 0x212B],
220
220
  [0x2153, 0x2154], [0x215B, 0x215E], [0x2160, 0x216B], [0x2170, 0x2179],
221
- [0x2189, 0x2189], [0x2190, 0x2199], [0x21B8, 0x21B9], [0x21D2, 0x21D2],
222
- [0x21D4, 0x21D4], [0x21E7, 0x21E7], [0x2200, 0x2200], [0x2202, 0x2203],
223
- [0x2207, 0x2208], [0x220B, 0x220B], [0x220F, 0x220F], [0x2211, 0x2211],
224
- [0x2215, 0x2215], [0x221A, 0x221A], [0x221D, 0x2220], [0x2223, 0x2223],
225
- [0x2225, 0x2225], [0x2227, 0x222C], [0x222E, 0x222E], [0x2234, 0x2237],
226
- [0x223C, 0x223D], [0x2248, 0x2248], [0x224C, 0x224C], [0x2252, 0x2252],
227
- [0x2260, 0x2261], [0x2264, 0x2267], [0x226A, 0x226B], [0x226E, 0x226F],
228
- [0x2282, 0x2283], [0x2286, 0x2287], [0x2295, 0x2295], [0x2299, 0x2299],
229
- [0x22A5, 0x22A5], [0x22BF, 0x22BF], [0x2312, 0x2312], [0x2460, 0x24E9],
230
- [0x24EB, 0x254B], [0x2550, 0x2573], [0x2580, 0x258F], [0x2592, 0x2595],
231
- [0x25A0, 0x25A1], [0x25A3, 0x25A9], [0x25B2, 0x25B3], [0x25B6, 0x25B7],
232
- [0x25BC, 0x25BD], [0x25C0, 0x25C1], [0x25C6, 0x25C8], [0x25CB, 0x25CB],
233
- [0x25CE, 0x25D1], [0x25E2, 0x25E5], [0x25EF, 0x25EF], [0x2605, 0x2606],
234
- [0x2609, 0x2609], [0x260E, 0x260F], [0x261C, 0x261C], [0x261E, 0x261E],
235
- [0x2640, 0x2640], [0x2642, 0x2642], [0x2660, 0x2661], [0x2663, 0x2665],
236
- [0x2667, 0x266A], [0x266C, 0x266D], [0x266F, 0x266F], [0x269E, 0x269F],
237
- [0x26C6, 0x26CD], [0x26CF, 0x26D3], [0x26D5, 0x26E1], [0x26E3, 0x26E3],
238
- [0x26E8, 0x26E9], [0x26EB, 0x26F1], [0x26F4, 0x26F4], [0x26F6, 0x26F9],
239
- [0x26FB, 0x26FC], [0x26FE, 0x26FF], [0x273D, 0x273D], [0x2761, 0x2761],
240
- [0x2776, 0x277F], [0x2B56, 0x2B59], [0x3248, 0x324F], [0xE000, 0xF8FF],
241
- [0xFE00, 0xFE0F], [0xFFFD, 0xFFFD], [0x1F100, 0x1F10A],
221
+ [0x2189, 0x2189], [0x2190, 0x2199], [0x219A, 0x219B], [0x219E, 0x21A0],
222
+ [0x21A3, 0x21A3], [0x21A6, 0x21A6], [0x21A9, 0x21AA], [0x21AE, 0x21AE],
223
+ [0x21B0, 0x21B1], [0x21B3, 0x21B4], [0x21B6, 0x21B9], [0x21B8, 0x21B9],
224
+ [0x21BC, 0x21BD], [0x21C0, 0x21C1], [0x21C4, 0x21C5], [0x21C7, 0x21C9],
225
+ [0x21CB, 0x21CC], [0x21CE, 0x21CF], [0x21D2, 0x21D2], [0x21D4, 0x21D4],
226
+ [0x21DA, 0x21DB], [0x21DD, 0x21DD], [0x21E0, 0x21E1], [0x21E4, 0x21E5],
227
+ [0x21E7, 0x21E7], [0x21EA, 0x21ED], [0x21F4, 0x21F4], [0x2200, 0x2200],
228
+ [0x2202, 0x2203], [0x2207, 0x2208], [0x220B, 0x220B], [0x220F, 0x220F],
229
+ [0x2211, 0x2211], [0x2215, 0x2215], [0x221A, 0x221A], [0x221D, 0x2220],
230
+ [0x2223, 0x2223], [0x2225, 0x2225], [0x2227, 0x222C], [0x222E, 0x222E],
231
+ [0x2234, 0x2237], [0x223C, 0x223D], [0x2248, 0x2248], [0x224C, 0x224C],
232
+ [0x2252, 0x2252], [0x2260, 0x2261], [0x2264, 0x2267], [0x226A, 0x226B],
233
+ [0x226E, 0x226F], [0x2282, 0x2283], [0x2286, 0x2287], [0x2295, 0x2295],
234
+ [0x2299, 0x2299], [0x22A5, 0x22A5], [0x22BF, 0x22BF], [0x2312, 0x2312],
235
+ [0x2460, 0x24E9], [0x24EB, 0x254B], [0x2550, 0x2573], [0x2580, 0x258F],
236
+ [0x2592, 0x2595], [0x25A0, 0x25A1], [0x25A3, 0x25A9], [0x25B2, 0x25B3],
237
+ [0x25B6, 0x25B7], [0x25BC, 0x25BD], [0x25C0, 0x25C1], [0x25C6, 0x25C8],
238
+ [0x25CB, 0x25CB], [0x25CE, 0x25D1], [0x25E2, 0x25E5], [0x25EF, 0x25EF],
239
+ [0x2605, 0x2606], [0x2609, 0x2609], [0x260E, 0x260F], [0x261C, 0x261C],
240
+ [0x261E, 0x261E], [0x2640, 0x2640], [0x2642, 0x2642], [0x2660, 0x2661],
241
+ [0x2663, 0x2665], [0x2667, 0x266A], [0x266C, 0x266D], [0x266F, 0x266F],
242
+ [0x269E, 0x269F], [0x26C6, 0x26CD], [0x26CF, 0x26D3], [0x26D5, 0x26E1],
243
+ [0x26E3, 0x26E3], [0x26E8, 0x26E9], [0x26EB, 0x26F1], [0x26F4, 0x26F4],
244
+ [0x26F6, 0x26F9], [0x26FB, 0x26FC], [0x26FE, 0x26FF], [0x273D, 0x273D],
245
+ [0x2761, 0x2761], [0x2776, 0x277F], [0x2B56, 0x2B59], [0x3248, 0x324F],
246
+ [0xE000, 0xF8FF], [0xFE00, 0xFE0F], [0xFFFD, 0xFFFD], [0x1F100, 0x1F10A],
242
247
  [0x1F110, 0x1F12D], [0x1F130, 0x1F169], [0x1F170, 0x1F19A],
243
248
  ];
244
249
 
250
+ // EAW=Ambiguous code points that every terminal still draws ONE column wide.
251
+ //
252
+ // Box drawing and block elements exist to tile: a `─` that was two columns wide could not
253
+ // meet its neighbour, so terminals draw them at one regardless of locale. Unicode classes
254
+ // them Ambiguous anyway, and honouring that measured every box and panel twice as wide as
255
+ // the space it was given — the frames they compose came apart.
256
+ //
257
+ // The three typographic marks are the same story for a different reason: `…`, `·`, `•` are
258
+ // used here for truncation and as separators, INSIDE single-column layouts. A truncated
259
+ // title measured two columns wide overflowed the panel it was cut to fit, and the panel's
260
+ // own border was then one column short on that row.
261
+ const AMBIGUOUS_ALWAYS_NARROW = [
262
+ [0x00B7, 0x00B7], // ·
263
+ [0x2022, 0x2022], // •
264
+ [0x2026, 0x2026], // …
265
+ [0x2500, 0x259F], // box drawing and block elements
266
+ ];
267
+
245
268
  /**
246
269
  * Should EAW=Ambiguous characters measure 2 columns?
247
270
  *
@@ -302,6 +325,19 @@ export function charWidth(ch) {
302
325
  if (isZeroWidth(c)) return 0;
303
326
  if (isWideByDefault(c)) return 2;
304
327
  if (inRanges(c, WIDE)) return 2;
328
+ // EAST ASIAN AMBIGUOUS: 1 column normally, 2 on a CJK terminal.
329
+ //
330
+ // `detectAmbiguousWide` and the AMBIGUOUS table both existed and NEITHER was consulted
331
+ // here, so every ambiguous character measured 1 while a Chinese terminal drew it as 2.
332
+ // The visible damage was the `↳` gutter: the arrow is ambiguous, so the marker measured
333
+ // a column narrower than the terminal drew it and the rows under it started a column
334
+ // early — a ragged block, and only on a CJK machine.
335
+ //
336
+ // BOX DRAWING AND BLOCK ELEMENTS ARE EXCLUDED, although Unicode classes them Ambiguous.
337
+ // Every terminal draws `─ │ ╭ █` one column wide regardless of locale — they exist to
338
+ // tile — and measuring them as two made every box and panel twice as wide as the space
339
+ // it was given, which broke the frames they compose.
340
+ if (isAmbiguousWide() && !inRanges(c, AMBIGUOUS_ALWAYS_NARROW) && inRanges(c, AMBIGUOUS)) return 2;
305
341
  return 1;
306
342
  }
307
343
 
package/src/text.js CHANGED
@@ -153,6 +153,68 @@ export function fitAnsi(s, w) {
153
153
  * A word longer than the line is hard-split BY VISUAL COLUMNS, not by `.length`: CJK characters occupy two cells, so
154
154
  * slicing by length emitted lines twice as wide as the box and let the terminal wrap them.
155
155
  */
156
+ /**
157
+ * Wrap a STYLED string to `width` VISIBLE columns.
158
+ *
159
+ * The order matters: inline markdown is rendered FIRST and the result is wrapped second.
160
+ * Wrapping the source first broke every marker that straddled a line break — a `**bold**`
161
+ * split across two lines matched no pattern, so the asterisks were printed verbatim. That
162
+ * showed up as literal `**` in the middle of a sentence, most often on a table cell and on
163
+ * a long paragraph in a narrow terminal.
164
+ *
165
+ * ANSI is carried through untouched: a colour sequence is never cut in half, and the width
166
+ * is measured on what the terminal will actually show.
167
+ */
168
+ export function wrapStyled(str, width) {
169
+ const src = String(str == null ? '' : str);
170
+ const limit = Math.max(1, width | 0);
171
+ const out = [];
172
+ // SPLIT INTO TOKENS: escape sequences, spaces, and everything else.
173
+ const tokens = [];
174
+ let i = 0;
175
+ while (i < src.length) {
176
+ if (src[i] === ESC) {
177
+ const m = /^\x1b\[[0-9;?]*[a-zA-Z]/.exec(src.slice(i))
178
+ || /^\x1b\[[0-9;?]* [a-zA-Z]/.exec(src.slice(i))
179
+ || /^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/.exec(src.slice(i));
180
+ if (m) { tokens.push({ esc: true, text: m[0], w: 0 }); i += m[0].length; continue; }
181
+ }
182
+ const cp = src.codePointAt(i);
183
+ const ch = String.fromCodePoint(cp);
184
+ if (ch === ' ') { tokens.push({ space: true, text: ' ', w: 1 }); i += ch.length; continue; }
185
+ tokens.push({ text: ch, w: visualWidth(ch) });
186
+ i += ch.length;
187
+ }
188
+
189
+ let line = '';
190
+ let lineW = 0;
191
+ const pushLine = () => { if (line) out.push(line); line = ''; lineW = 0; };
192
+
193
+ for (let t = 0; t < tokens.length; t++) {
194
+ const tok = tokens[t];
195
+ if (tok.space) {
196
+ // A space at the start of a line is dropped: it is the gap the wrap left behind.
197
+ if (lineW === 0) continue;
198
+ if (lineW + tok.w > limit) { pushLine(); continue; }
199
+ line += tok.text; lineW += tok.w;
200
+ continue;
201
+ }
202
+ if (tok.esc) { line += tok.text; continue; }
203
+ if (lineW + tok.w > limit) {
204
+ // The word does not fit: break before it. The escape sequences that opened the
205
+ // styling stay on this line and are closed by the reset the caller appended, so the
206
+ // colour does not bleed into the next one.
207
+ pushLine();
208
+ line += tok.text; lineW = tok.w;
209
+ continue;
210
+ }
211
+ line += tok.text; lineW += tok.w;
212
+ }
213
+ pushLine();
214
+ if (!out.length) out.push('');
215
+ return out;
216
+ }
217
+
156
218
  export function wrapWords(str, width) {
157
219
  width = Math.max(1, width | 0);
158
220
  const out = [];
@@ -58,7 +58,12 @@ export function listSubagents() {
58
58
  function systemPromptFor(type, cfg, systemPrompt) {
59
59
  const profile = SUBAGENT_TYPES[type] || SUBAGENT_TYPES[DEFAULT_SUBAGENT_TYPE];
60
60
  const base = (cfg.systemPrompt && String(cfg.systemPrompt).trim()) || systemPrompt;
61
- return `${profile.prompt}\n\n${base}`;
61
+ // The inline-reasoning instruction, for a model with no reasoning channel of its own —
62
+ // the same rule the main agent applies (see needsThinkingTags).
63
+ const tags = needsThinkingTags(cfg)
64
+ ? `\n\n${THINKING_TAGS_INSTRUCTION}`
65
+ : '';
66
+ return `${profile.prompt}\n\n${base}${tags}`;
62
67
  }
63
68
 
64
69
  // Run a subagent to completion. `history` seeds its messages (fork). Returns
@@ -70,7 +75,7 @@ function systemPromptFor(type, cfg, systemPrompt) {
70
75
  async function runSubagent({ cfg, type, prompt, history, signal, onEvent, onProgress, onStep, agentId, onAgent }) {
71
76
  // The parent-signal listener (registered below, removed when the run settles).
72
77
  let onSubagentAbort = null;
73
- const { Agent, SYSTEM_PROMPT } = await import('../agent.js');
78
+ const { Agent, SYSTEM_PROMPT, THINKING_TAGS_INSTRUCTION, needsThinkingTags } = await import('../agent.js');
74
79
  const subCfg = {
75
80
  ...cfg,
76
81
  systemPrompt: systemPromptFor(type, cfg, SYSTEM_PROMPT),