@hncode/hncode 0.8.6 → 1.0.0
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 +30 -0
- package/README_CN.md +27 -0
- package/package.json +1 -1
- package/src/agent.js +172 -43
- package/src/bootstrap.js +63 -56
- package/src/ci.js +247 -247
- package/src/colors.js +5 -1
- package/src/commands.js +121 -0
- package/src/composer.js +31 -1
- package/src/config.js +177 -45
- package/src/context.js +29 -0
- package/src/files.js +32 -0
- package/src/fx.js +19 -2
- package/src/i18n.js +180 -131
- package/src/index.js +558 -77
- package/src/permission-gate.js +103 -0
- package/src/plugin.js +221 -2
- package/src/prompt-presets.js +36 -0
- package/src/protocol.js +184 -0
- package/src/registry-browser.js +14 -3
- package/src/remote.js +117 -5
- package/src/schedule.js +148 -11
- package/src/serve.js +613 -0
- package/src/session-client.js +148 -0
- package/src/session-host.js +52 -0
- package/src/session.js +26 -3
- package/src/settings-schema.js +2 -2
- package/src/status-line.js +48 -0
- package/src/toml.js +18 -4
- package/src/tool-status.js +45 -0
- package/src/tools/bash.js +43 -24
- package/src/tools/edit.js +10 -5
- package/src/tools/web-search.js +8 -5
- package/src/tools/write.js +28 -3
- package/src/tui.js +448 -1380
- package/src/ws.js +248 -0
- package/src/session-hub.js +0 -445
- package/src/web-auth.js +0 -98
- package/src/web-daemon-cli.js +0 -63
- package/src/web-daemon-client.js +0 -203
- package/src/web-daemon.js +0 -894
- package/src/web-live.js +0 -250
- package/src/web-public/app.css +0 -1524
- package/src/web-public/app.js +0 -2969
- package/src/web-public/complete.js +0 -154
- package/src/web-public/fonts/JetBrainsMono-Bold.woff2 +0 -0
- package/src/web-public/fonts/JetBrainsMono-Regular.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-100-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-101-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-102-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-103-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-104-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-105-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-106-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-107-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-108-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-109-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-110-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-111-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-112-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-113-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-114-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-115-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-116-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-117-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-118-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-119-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-21-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-22-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-23-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-24-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-25-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-26-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-27-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-28-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-29-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-30-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-31-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-32-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-33-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-34-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-35-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-36-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-37-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-38-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-39-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-4-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-40-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-41-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-42-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-43-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-44-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-45-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-46-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-47-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-48-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-49-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-5-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-50-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-51-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-52-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-53-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-54-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-55-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-56-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-57-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-58-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-59-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-6-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-60-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-61-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-62-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-63-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-64-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-65-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-66-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-67-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-68-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-69-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-70-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-71-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-72-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-73-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-74-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-75-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-76-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-77-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-78-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-79-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-80-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-81-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-82-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-83-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-84-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-85-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-86-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-87-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-88-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-89-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-90-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-91-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-97-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-98-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-99-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc.css +0 -775
- package/src/web-public/fonts.css +0 -31
- package/src/web-public/home.css +0 -222
- package/src/web-public/home.html +0 -33
- package/src/web-public/home.js +0 -160
- package/src/web-public/i18n.js +0 -983
- package/src/web-public/index.html +0 -196
- package/src/web-public/login.css +0 -123
- package/src/web-public/login.html +0 -25
- package/src/web-public/login.js +0 -46
- package/src/web-public/settings.js +0 -290
- package/src/web-ws.js +0 -226
- package/src/web.js +0 -430
package/README.md
CHANGED
|
@@ -88,6 +88,36 @@ hncode control prompt "run the tests" # queue a prompt on the running session
|
|
|
88
88
|
hncode control interrupt
|
|
89
89
|
```
|
|
90
90
|
|
|
91
|
+
### GUI window
|
|
92
|
+
|
|
93
|
+
The same CLI drives an Electron window, for the things a terminal cannot do: a
|
|
94
|
+
conversation rail, images in the transcript, a conversation saved as a PNG, and
|
|
95
|
+
dictation into the composer.
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
cd hncode-gui
|
|
99
|
+
npm install
|
|
100
|
+
npm start
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
It is not a second implementation. Every turn is the same
|
|
104
|
+
`hncode -p … --output-format stream-json` child; the window renders its event
|
|
105
|
+
stream, and the settings it writes are the same `config.toml` the terminal reads.
|
|
106
|
+
|
|
107
|
+
| | |
|
|
108
|
+
|---|---|
|
|
109
|
+
| `Ctrl+M` | dictate — hold to talk, tap to toggle |
|
|
110
|
+
| `Ctrl+K` | focus the composer |
|
|
111
|
+
| `Ctrl+N` | new conversation |
|
|
112
|
+
| `Ctrl+F` | find inside the conversation |
|
|
113
|
+
| `Ctrl+G` | type into a command that is waiting for input |
|
|
114
|
+
| `Ctrl+B` | move that command to the background |
|
|
115
|
+
| `Ctrl+R` | reload the window and its config |
|
|
116
|
+
| `?` | the full list |
|
|
117
|
+
|
|
118
|
+
Tab reaches every control, the rail is walked with the arrow keys and opened with
|
|
119
|
+
Enter, and the model menu takes arrows and Enter as well.
|
|
120
|
+
|
|
91
121
|
---
|
|
92
122
|
|
|
93
123
|
## Installation
|
package/README_CN.md
CHANGED
|
@@ -67,6 +67,33 @@ npx @hncode/hncode
|
|
|
67
67
|
|
|
68
68
|
---
|
|
69
69
|
|
|
70
|
+
## 桌面窗口
|
|
71
|
+
|
|
72
|
+
同一个命令行也带一个 Electron 窗口,用来做终端做不到的事:会话侧栏、对话里的图片、把整段对话存成 PNG、以及直接对输入框说话。
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
cd hncode-gui
|
|
76
|
+
npm install
|
|
77
|
+
npm start
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
它不是第二套实现。每一次对话仍然是同一个 `hncode -p … --output-format stream-json` 子进程,窗口只是渲染它的事件流;窗口写的设置就是终端读的那份 `config.toml`。
|
|
81
|
+
|
|
82
|
+
| 按键 | 作用 |
|
|
83
|
+
|---|---|
|
|
84
|
+
| `Ctrl+M` | 语音输入,长按说话、单击切换 |
|
|
85
|
+
| `Ctrl+K` | 聚焦输入框 |
|
|
86
|
+
| `Ctrl+N` | 新建会话 |
|
|
87
|
+
| `Ctrl+F` | 在对话里查找 |
|
|
88
|
+
| `Ctrl+G` | 给正在等输入的命令打字 |
|
|
89
|
+
| `Ctrl+B` | 把该命令转到后台 |
|
|
90
|
+
| `Ctrl+R` | 重新加载窗口和配置 |
|
|
91
|
+
| `?` | 完整列表 |
|
|
92
|
+
|
|
93
|
+
Tab 能到每个控件,侧栏用方向键走、回车打开,模型菜单也一样。
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
70
97
|
## 文档
|
|
71
98
|
|
|
72
99
|
本文件只是简要介绍。**完整文档**(所有命令、配置项、架构说明、插件与开发指南)请见 Wiki:
|
package/package.json
CHANGED
package/src/agent.js
CHANGED
|
@@ -7,6 +7,7 @@ import { tools, combinedTools, getTool, llmTools } from './tools/index.js';
|
|
|
7
7
|
import { validateToolArgs } from './tools/validate-args.js';
|
|
8
8
|
import { DELEGATION_TOOLS } from './subagent-types.js';
|
|
9
9
|
import { estimateMessagesTokens, estimateRequestOverhead, estimateTokens, measureToolDefTokens, messageTokens, TokenLedger, forgetMessageTokens } from './term.js';
|
|
10
|
+
import { contextBreakdown } from './context.js';
|
|
10
11
|
import { runHooks, runPatch, runPatchSync } from './plugin.js';
|
|
11
12
|
import { loadHooks, runShellHooks } from './hooks.js';
|
|
12
13
|
import {
|
|
@@ -22,6 +23,21 @@ function wrapSystemReminder(content) {
|
|
|
22
23
|
return `<system-reminder>\n${String(content).trim()}\n</system-reminder>`;
|
|
23
24
|
}
|
|
24
25
|
|
|
26
|
+
/**
|
|
27
|
+
* An error as text, for anything that crosses a process boundary as JSON.
|
|
28
|
+
*
|
|
29
|
+
* `JSON.stringify(new Error('boom'))` is `{}`: `message` and `stack` are own but not enumerable,
|
|
30
|
+
* so a real reason became an empty object by the time a front end saw it, and the window printed
|
|
31
|
+
* "[object Object]" where the diagnosis should have been.
|
|
32
|
+
*/
|
|
33
|
+
function errorText(err) {
|
|
34
|
+
if (err == null) return '';
|
|
35
|
+
if (typeof err === 'string') return err;
|
|
36
|
+
if (typeof err.message === 'string' && err.message) return err.message;
|
|
37
|
+
// A provider that rejected with a bare object still has something worth reading in it.
|
|
38
|
+
try { return JSON.stringify(err); } catch { return String(err); }
|
|
39
|
+
}
|
|
40
|
+
|
|
25
41
|
// ---- token accounting -------------------------------------------------------
|
|
26
42
|
// The provider reports the numbers; this folds one report into a running total.
|
|
27
43
|
// A field the provider did not send is left as it was, because "not reported" and
|
|
@@ -80,6 +96,37 @@ const COMPACTION_INSTRUCTION = [
|
|
|
80
96
|
].join('\n');
|
|
81
97
|
// Trailing note: the model should RESUME, not answer the summary.
|
|
82
98
|
|
|
99
|
+
/**
|
|
100
|
+
* The clock, as one line: the LOCAL time first, with its zone, then the same instant in UTC.
|
|
101
|
+
*
|
|
102
|
+
* LOCAL FIRST BECAUSE THAT IS THE ONE THE PERSON MEANS. "What time is it" and "run it at nine"
|
|
103
|
+
* are about the clock on the wall, and a model told only UTC answers the wrong question — a
|
|
104
|
+
* machine at UTC+8 was told 10:52 while its owner's clock said 18:52. UTC follows because it is
|
|
105
|
+
* unambiguous: a zone abbreviation is not, and DST makes an offset lie for half the year.
|
|
106
|
+
*
|
|
107
|
+
* The zone NAME comes from the runtime rather than being guessed from the offset, so the model
|
|
108
|
+
* gets `Asia/Shanghai` instead of a number it would have to interpret.
|
|
109
|
+
*/
|
|
110
|
+
export function timeStamp(now = new Date()) {
|
|
111
|
+
const zone = (() => {
|
|
112
|
+
try { return Intl.DateTimeFormat().resolvedOptions().timeZone || ""; }
|
|
113
|
+
catch { return ""; }
|
|
114
|
+
})();
|
|
115
|
+
// `sv-SE` is the locale whose date format is already `YYYY-MM-DD HH:MM:SS`, so the result is
|
|
116
|
+
// sortable and unambiguous without a hand-rolled formatter.
|
|
117
|
+
let local = "";
|
|
118
|
+
try {
|
|
119
|
+
local = now.toLocaleString("sv-SE", { hour12: false }).replace(",", "");
|
|
120
|
+
} catch { local = now.toISOString().slice(0, 19).replace("T", " "); }
|
|
121
|
+
const offsetMin = -now.getTimezoneOffset();
|
|
122
|
+
const sign = offsetMin >= 0 ? "+" : "-";
|
|
123
|
+
const oh = String(Math.floor(Math.abs(offsetMin) / 60)).padStart(2, "0");
|
|
124
|
+
const om = String(Math.abs(offsetMin) % 60).padStart(2, "0");
|
|
125
|
+
const off = `${sign}${oh}:${om}`;
|
|
126
|
+
return `${local} ${off}${zone ? ` (${zone})` : ""} | UTC ${now.toISOString().slice(0, 19).replace("T", " ")}`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
83
130
|
// A summariser can answer with tool-call syntax even when no tools were offered — the format is in its
|
|
84
131
|
// training data, and a model interrupted mid-workflow reaches for it. Nothing was called, so the text is
|
|
85
132
|
// noise, and left in a handoff summary the next turn reads it as a step that happened.
|
|
@@ -328,52 +375,41 @@ export const SYSTEM_PROMPT = `You are hncode, a coding agent in the user's termi
|
|
|
328
375
|
You work inside a workspace directory using the tools provided.
|
|
329
376
|
|
|
330
377
|
Tools:
|
|
331
|
-
- Read a file before editing it
|
|
332
|
-
- Prefer Edit over Write for existing
|
|
333
|
-
-
|
|
334
|
-
|
|
335
|
-
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
fix is to remove the duplicated line, never to restate another one.
|
|
378
|
+
- Read a file before editing it. Never guess its contents.
|
|
379
|
+
- Prefer Edit over Write for an existing file.
|
|
380
|
+
- One edit at a time: an edit shifts the lines below it, so re-read before the next one rather than
|
|
381
|
+
trusting line numbers from a moment ago.
|
|
382
|
+
- A failed tool is information. Read the error, change the approach. Two failures on the same goal
|
|
383
|
+
means stop and report the blocker.
|
|
384
|
+
- Independent calls may run in parallel. Dependent ones never.
|
|
385
|
+
- A successful edit needs no confirmation read.
|
|
340
386
|
|
|
341
387
|
Acting:
|
|
342
|
-
-
|
|
343
|
-
|
|
344
|
-
- Carry the task through
|
|
345
|
-
|
|
346
|
-
-
|
|
347
|
-
|
|
348
|
-
no drive-by fixes.
|
|
349
|
-
- Read the neighbours before you write: match the naming, error handling and comment density you see.
|
|
350
|
-
- Do not re-read a file after editing it, and do not re-run a search you already have results for.
|
|
351
|
-
A successful edit tool call already proves it worked.
|
|
388
|
+
- Assume the user wants the work done, unless they are explicitly asking for a plan or a question.
|
|
389
|
+
Implement rather than describe.
|
|
390
|
+
- Carry the task through to verification in this turn. Do not stop at analysis or at a list of next steps.
|
|
391
|
+
- New project: use your judgement on structure. Existing codebase: touch only what the task needs.
|
|
392
|
+
- Read the neighbours first and match their naming, error handling and comment density.
|
|
393
|
+
- Do not re-run a search whose results you already have.
|
|
352
394
|
|
|
353
395
|
Scope:
|
|
354
|
-
- Do only what was asked.
|
|
355
|
-
-
|
|
356
|
-
-
|
|
357
|
-
-
|
|
358
|
-
|
|
359
|
-
(git reset --hard, git checkout --) unless the user asked for it. If you notice an unexpected change
|
|
360
|
-
you did not make, stop and ask.
|
|
396
|
+
- Do only what was asked. No refactors, renames or drive-by fixes to unrelated code.
|
|
397
|
+
- Unrelated bugs and broken tests: mention them, do not fix them.
|
|
398
|
+
- Ask one short question when the request is genuinely ambiguous; otherwise state your assumption and go.
|
|
399
|
+
- The worktree may be dirty. Never revert changes you did not make, and never run a destructive git
|
|
400
|
+
command unless asked. If you find a change you did not make, stop and ask.
|
|
361
401
|
|
|
362
402
|
Verifying:
|
|
363
|
-
- Run the narrowest check that exercises
|
|
364
|
-
- In auto/yolo mode, run tests and lint
|
|
365
|
-
-
|
|
366
|
-
|
|
367
|
-
- Do not add tests to a codebase that has none. Do not add a formatter that the project did not configure.
|
|
368
|
-
- If you could not verify something, say so plainly. Do not imply a test passed when you did not run it.
|
|
403
|
+
- Run the narrowest check that exercises the change first, then widen.
|
|
404
|
+
- In auto/yolo mode, run tests and lint without asking. In ask mode, say what you would run.
|
|
405
|
+
- Do not add tests to a codebase that has none, or a formatter it never configured.
|
|
406
|
+
- If you could not verify something, say so. Never imply a test passed when you did not run it.
|
|
369
407
|
|
|
370
408
|
Finishing:
|
|
371
|
-
- Never stop silently after a tool call
|
|
372
|
-
- Report
|
|
373
|
-
-
|
|
374
|
-
-
|
|
375
|
-
- If context is running low, call /compact to shrink history.
|
|
376
|
-
`;
|
|
409
|
+
- Never stop silently after a tool call. End every turn with a written answer in the user's language.
|
|
410
|
+
- Report what changed and how it was verified, briefly. No filler, no restating the request.
|
|
411
|
+
- Cite file:line for code you refer to.
|
|
412
|
+
- If context is running low, call /compact.`;
|
|
377
413
|
|
|
378
414
|
// THE INLINE-REASONING INSTRUCTION, AND WHEN IT IS NEEDED AT ALL.
|
|
379
415
|
//
|
|
@@ -390,6 +426,38 @@ Finishing:
|
|
|
390
426
|
// It is kept OUT of SYSTEM_PROMPT so that the prompt stays static — a static prefix is what
|
|
391
427
|
// providers cache, and a suffix that changes with the model would break that caching for
|
|
392
428
|
// every request that follows it.
|
|
429
|
+
/**
|
|
430
|
+
* Plan mode: what the model is asked to PRODUCE.
|
|
431
|
+
*
|
|
432
|
+
* The tool filter alone only removes its write tools. Without this the model investigates and
|
|
433
|
+
* then answers in prose — a plan nobody can approve, because it is not a block. Both front ends
|
|
434
|
+
* run the same words, so a plan drafted from the window and one drafted in the terminal read
|
|
435
|
+
* the same and are parsed by the same code.
|
|
436
|
+
*/
|
|
437
|
+
export const PLAN_MODE_INSTRUCTION =
|
|
438
|
+
'PLAN MODE is ON. You are in a read-only planning phase.\n'
|
|
439
|
+
+ '- The write tools (Write, Edit, Bash, ...) are NOT available. Investigate with\n'
|
|
440
|
+
+ ' the read-only tools you do have, then produce a PLAN.\n'
|
|
441
|
+
+ '- Do NOT try to make the changes. Your job this turn is the plan, not the code.\n'
|
|
442
|
+
+ '- End your reply with the plan wrapped in <|plan|> and </|plan|> tags, on their\n'
|
|
443
|
+
+ ' own lines. Everything between them is shown to the user verbatim and is what\n'
|
|
444
|
+
+ ' they approve. Only the LAST such block is used.\n'
|
|
445
|
+
+ '- Inside the block, use short Markdown: a heading per step, and bullets for the\n'
|
|
446
|
+
+ ' files to touch and why. Be concrete enough that the plan can be executed as\n'
|
|
447
|
+
+ ' written — name files, functions and commands.\n'
|
|
448
|
+
+ '- Keep any text OUTSIDE the tags to a one-line summary of what you found.\n'
|
|
449
|
+
+ '- Once the plan is approved the mode turns off and you get the write tools, so\n'
|
|
450
|
+
+ ' the plan must stand on its own.\n'
|
|
451
|
+
+ '\n'
|
|
452
|
+
+ 'Example shape:\n'
|
|
453
|
+
+ '<|plan|>\n'
|
|
454
|
+
+ '## Add the /personal command\n'
|
|
455
|
+
+ '- `src/config.js`: add readPersonalPrompt() merging global + project files.\n'
|
|
456
|
+
+ '- `src/tui.js`: register /personal, inject the text each turn.\n'
|
|
457
|
+
+ '## Verify\n'
|
|
458
|
+
+ '- Write a temp workspace, check both scopes merge in order.\n'
|
|
459
|
+
+ '</|plan|>';
|
|
460
|
+
|
|
393
461
|
export const THINKING_TAGS_INSTRUCTION = `Reasoning:
|
|
394
462
|
- Put ALL of your internal reasoning inside <|thinking|>...<|/thinking|> tags, and always emit BOTH the
|
|
395
463
|
opening and the closing tag. Do not omit the tags, do not abbreviate them, and do not leave the block
|
|
@@ -828,7 +896,11 @@ const filtered = (Array.isArray(cfg.toolFilter) && cfg.toolFilter.length
|
|
|
828
896
|
this.onEvent({ type: 'aborted' });
|
|
829
897
|
break;
|
|
830
898
|
case 'error':
|
|
831
|
-
|
|
899
|
+
// AN ERROR CROSSES A PROCESS BOUNDARY AS JSON, and `JSON.stringify(new Error('…'))` is
|
|
900
|
+
// `{}` — the reason was in `.message`, which is not enumerable. Every front end then had
|
|
901
|
+
// an empty object to report, and the window printed "[object Object]". The text is
|
|
902
|
+
// flattened here, once, where it still exists.
|
|
903
|
+
this.onEvent({ type: 'error', error: errorText(e.error) });
|
|
832
904
|
break;
|
|
833
905
|
}
|
|
834
906
|
};
|
|
@@ -1015,6 +1087,8 @@ const filtered = (Array.isArray(cfg.toolFilter) && cfg.toolFilter.length
|
|
|
1015
1087
|
// Drop it: the next turn falls back to the local estimate until the provider reports again, instead of
|
|
1016
1088
|
// reading a stale figure and compacting a history that was just compacted.
|
|
1017
1089
|
this.ctxReportedTokens = 0;
|
|
1090
|
+
// The breakdown describes the same old history, so it goes with the figure.
|
|
1091
|
+
this.ctxBreakdown = null;
|
|
1018
1092
|
this.onEvent({
|
|
1019
1093
|
type: 'compacted', before, after: usedNow, kept,
|
|
1020
1094
|
dropped: droppedCount, summary,
|
|
@@ -1068,7 +1142,12 @@ const filtered = (Array.isArray(cfg.toolFilter) && cfg.toolFilter.length
|
|
|
1068
1142
|
// Live context gauge: report the estimated size of the request about to be
|
|
1069
1143
|
// sent, so the TUI's context readout is refreshed at every step rather than
|
|
1070
1144
|
// only once the whole turn ends.
|
|
1071
|
-
|
|
1145
|
+
const breakdown = contextBreakdown({ messages: this.messages }, this.ctx);
|
|
1146
|
+
// KEPT FOR THE SESSION RECORD. The window shows the breakdown when a panel is opened, and
|
|
1147
|
+
// a conversation reopened from the rail has no live event to read it from — the numbers
|
|
1148
|
+
// were computed on every step and then thrown away with the process.
|
|
1149
|
+
this.ctxBreakdown = breakdown;
|
|
1150
|
+
this.onEvent({ type: 'context', tokens: usedNow, max: maxCtx, breakdown });
|
|
1072
1151
|
|
|
1073
1152
|
// Inject any user steering messages typed during the previous step.
|
|
1074
1153
|
// They are appended right after the last assistant/tool round so the
|
|
@@ -1104,6 +1183,19 @@ const filtered = (Array.isArray(cfg.toolFilter) && cfg.toolFilter.length
|
|
|
1104
1183
|
// message array being sent (and may mutate it); `onAfterResponse` fires once
|
|
1105
1184
|
// the stream has been consumed.
|
|
1106
1185
|
await runHooks('onBeforeRequest', this.messages, this.cfg);
|
|
1186
|
+
// THE TIME GOES ON THE LAST USER MESSAGE, AT SEND TIME ONLY.
|
|
1187
|
+
//
|
|
1188
|
+
// The model has no clock: "yesterday", "the file changed this morning" and "run it again
|
|
1189
|
+
// tomorrow" are all unanswerable without one, and a session resumed a week later reads as
|
|
1190
|
+
// if no time passed. Stamping it onto the STORED message would change the text every time
|
|
1191
|
+
// the history is resent, which breaks the provider's prefix cache and writes a different
|
|
1192
|
+
// past into the session file on every turn. So the stamp is added to a COPY that goes out
|
|
1193
|
+
// and nothing else.
|
|
1194
|
+
//
|
|
1195
|
+
// THE CLOCK IS ADDED BY THE FRONT END, not here. Each of them assembles the request in its
|
|
1196
|
+
// own way — the terminal from `state`, the window from the resident server — and the clock
|
|
1197
|
+
// has to be part of the PROMPT, not a message in the conversation. As a message it showed up
|
|
1198
|
+
// in the transcript as something the person had said, and the model quoted it back.
|
|
1107
1199
|
await this.llm.request(this.messages, handle, cacheEnd);
|
|
1108
1200
|
await runHooks('onAfterRequest', this.messages, this.cfg);
|
|
1109
1201
|
|
|
@@ -1129,6 +1221,15 @@ const filtered = (Array.isArray(cfg.toolFilter) && cfg.toolFilter.length
|
|
|
1129
1221
|
// Not part of the answer, never rendered from here: it is the provider's own
|
|
1130
1222
|
// accounting of its reasoning, replayed verbatim on the next request.
|
|
1131
1223
|
thoughtBlocks: hasThoughtBlocks ? thoughtBlocks : undefined,
|
|
1224
|
+
// THE REASONING ITSELF, kept so a window can still show it after the turn.
|
|
1225
|
+
//
|
|
1226
|
+
// It used to travel only as live `think` events, and the transcript persisted none of
|
|
1227
|
+
// it: the card was built from a stream that had already happened, so reopening the
|
|
1228
|
+
// session — or finishing the turn, which re-reads the file — left nothing to draw and
|
|
1229
|
+
// the reasoning vanished exactly when the reader wanted to check it. The terminal shows
|
|
1230
|
+
// its own block from the same events and has the same gap; this is the field that closes
|
|
1231
|
+
// it for both. `thinking` is not sent to the model (see toWire in the provider layer).
|
|
1232
|
+
thinking: thought && thought.trim() ? thought : undefined,
|
|
1132
1233
|
};
|
|
1133
1234
|
// Mixin seam: llmResponse - plugins may rewrite the assistant text before it is saved.
|
|
1134
1235
|
try { const out = await runPatch('llmResponse', { content: assistantMsg.content, toolCalls: assistantMsg.toolCalls }, (c) => c); if (out) { if (out.content !== undefined) assistantMsg.content = out.content; if (out.toolCalls !== undefined) assistantMsg.toolCalls = out.toolCalls; } } catch (e) { console.error('[hncode-plugin] llmResponse error:', e.message); }
|
|
@@ -1369,11 +1470,16 @@ const filtered = (Array.isArray(cfg.toolFilter) && cfg.toolFilter.length
|
|
|
1369
1470
|
const forModel = out.editDiff
|
|
1370
1471
|
? { ...out.result, text: mediaResultText(out.result) + formatEditDiffForModel(out.editDiff) }
|
|
1371
1472
|
: out.result;
|
|
1372
|
-
this.messages.push({
|
|
1473
|
+
this.messages.push({
|
|
1474
|
+
role: 'tool', toolCallId: tc.id, content: forModel,
|
|
1475
|
+
// Same reason as the branch below: the record carries the diff so a reopened card can draw it.
|
|
1476
|
+
...(out.editDiff ? { editDiff: out.editDiff } : {}),
|
|
1477
|
+
});
|
|
1373
1478
|
this.onEvent({
|
|
1374
1479
|
type: 'tool_result', id: tc.id, name: tc.name,
|
|
1375
1480
|
content: out.result._receipt || out.result.text || '[media]',
|
|
1376
1481
|
editDiff: out.editDiff,
|
|
1482
|
+
media: out.result && out.result.media ? out.result.media : undefined,
|
|
1377
1483
|
});
|
|
1378
1484
|
return;
|
|
1379
1485
|
}
|
|
@@ -1387,7 +1493,16 @@ const filtered = (Array.isArray(cfg.toolFilter) && cfg.toolFilter.length
|
|
|
1387
1493
|
let _toolContent = shaped.content;
|
|
1388
1494
|
// Mixin seam: toolResult (sync) - plugins may rewrite what the model receives for this call.
|
|
1389
1495
|
try { const out = runPatchSync('toolResult', { name: tc.name, content: shaped.content }, (c) => c); if (out && out.content !== undefined) _toolContent = out.content; } catch (e) { console.error('[hncode-plugin] toolResult error:', e.message); }
|
|
1390
|
-
|
|
1496
|
+
// THE DIFF IS PART OF THE RECORD, NOT ONLY OF THE EVENT.
|
|
1497
|
+
//
|
|
1498
|
+
// `editDiff` was forwarded to the UI and never stored, so a reopened conversation had the tool's
|
|
1499
|
+
// receipt text and no diff — every Edit card in history drew a paragraph instead of the change, and
|
|
1500
|
+
// the window could not draw what it had no data for. The model does not read this field; it is here
|
|
1501
|
+
// so the transcript can be rebuilt as it was seen.
|
|
1502
|
+
this.messages.push({
|
|
1503
|
+
role: 'tool', toolCallId: tc.id, content: _toolContent,
|
|
1504
|
+
...(out.editDiff ? { editDiff: out.editDiff } : {}),
|
|
1505
|
+
});
|
|
1391
1506
|
// Remember where the newest tool result landed: a trim must never elide the
|
|
1392
1507
|
// output the model was handed in the step it is about to reason about.
|
|
1393
1508
|
this._lastToolResultIndex = this.messages.length - 1;
|
|
@@ -1434,7 +1549,7 @@ const filtered = (Array.isArray(cfg.toolFilter) && cfg.toolFilter.length
|
|
|
1434
1549
|
|
|
1435
1550
|
// Report the context after this step's tool results were appended: that is
|
|
1436
1551
|
// exactly what the next request will carry, so the gauge grows with history.
|
|
1437
|
-
this.onEvent({ type: 'context', tokens: this.usedTokens(), max: maxCtx });
|
|
1552
|
+
this.onEvent({ type: 'context', tokens: this.usedTokens(), max: maxCtx, breakdown: contextBreakdown({ messages: this.messages }, this.ctx) });
|
|
1438
1553
|
|
|
1439
1554
|
}
|
|
1440
1555
|
|
|
@@ -1463,3 +1578,17 @@ const filtered = (Array.isArray(cfg.toolFilter) && cfg.toolFilter.length
|
|
|
1463
1578
|
{ messages: this.messages, stopped: this.stopRequested, prompt: this._turnPrompt }, this.cfg.workspace);
|
|
1464
1579
|
}
|
|
1465
1580
|
}
|
|
1581
|
+
|
|
1582
|
+
/**
|
|
1583
|
+
* The env block, for stripping it out of text that is about to be saved.
|
|
1584
|
+
*
|
|
1585
|
+
* The clock is appended to the user's message so the model receives it with the line it
|
|
1586
|
+
* describes, which means the message as SENT is not the message as STORED. This is the one
|
|
1587
|
+
* pattern both front ends use to tell them apart.
|
|
1588
|
+
*/
|
|
1589
|
+
export const ENV_BLOCK_RE = /\n\n<env>Current time: [\s\S]*?<\/env>/g;
|
|
1590
|
+
|
|
1591
|
+
/** `text` with any env block removed. */
|
|
1592
|
+
export function stripEnvBlock(text) {
|
|
1593
|
+
return typeof text === 'string' ? text.replace(ENV_BLOCK_RE, '') : text;
|
|
1594
|
+
}
|
package/src/bootstrap.js
CHANGED
|
@@ -1,56 +1,63 @@
|
|
|
1
|
-
// Bootstrap: install the patch loader and load plugins BEFORE the rest of
|
|
2
|
-
// hncode's modules are imported.
|
|
3
|
-
//
|
|
4
|
-
// WHY A SEPARATE MODULE
|
|
5
|
-
// ---------------------
|
|
6
|
-
// Symbol-level patching (patch.js) rewrites a target module's source as it is
|
|
7
|
-
// loaded, so the loader MUST be installed and the plugins MUST have registered
|
|
8
|
-
// their targets before those modules are imported. If any patched module is
|
|
9
|
-
// imported first, it is served from cache and the patch is silently skipped.
|
|
10
|
-
//
|
|
11
|
-
// hncode's real work lives in modules (agent.js, llm.js, tools/*) that index.js
|
|
12
|
-
// imports at the top, so index.js cannot load plugins itself — by the time it ran
|
|
13
|
-
// loadPlugins(), those modules would already be cached. This module runs first
|
|
14
|
-
// (see bin/hncode) and does the ordering that makes patching possible.
|
|
15
|
-
//
|
|
16
|
-
// It is deliberately tiny and dependency-light: it only needs config (for the
|
|
17
|
-
// plugin directory) plus plugin.js / patch.js. Everything else stays lazy.
|
|
18
|
-
//
|
|
19
|
-
// FAILURE POSTURE: bootstrap must NEVER stop hncode from starting. A missing
|
|
20
|
-
// Node API, a malformed config, or a broken plugin directory degrades to "no
|
|
21
|
-
// patches / no plugins", never to "hncode won't launch".
|
|
22
|
-
|
|
23
|
-
// Set once plugins have been loaded (by bootstrap or by index.js), so index.js
|
|
24
|
-
// does not load them a second time and trip "tool already registered".
|
|
25
|
-
let pluginsLoaded = false;
|
|
26
|
-
export function pluginsAlreadyLoaded() { return pluginsLoaded; }
|
|
27
|
-
export function markPluginsLoaded() { pluginsLoaded = true; }
|
|
28
|
-
|
|
29
|
-
export async function bootstrap({ loadPlugins = true } = {}) {
|
|
30
|
-
// Patch loader first, but never let it break startup: installPatchLoader is
|
|
31
|
-
// already non-throwing, and we belt-and-brace with try/catch here.
|
|
32
|
-
try {
|
|
33
|
-
const { installPatchLoader } = await import('./patch.js');
|
|
34
|
-
installPatchLoader();
|
|
35
|
-
} catch (e) {
|
|
36
|
-
console.error('[hncode] patch loader unavailable, continuing without it:', e && e.message);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (!loadPlugins) return { pluginDir: null, loaded: [] };
|
|
40
|
-
|
|
41
|
-
try {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
1
|
+
// Bootstrap: install the patch loader and load plugins BEFORE the rest of
|
|
2
|
+
// hncode's modules are imported.
|
|
3
|
+
//
|
|
4
|
+
// WHY A SEPARATE MODULE
|
|
5
|
+
// ---------------------
|
|
6
|
+
// Symbol-level patching (patch.js) rewrites a target module's source as it is
|
|
7
|
+
// loaded, so the loader MUST be installed and the plugins MUST have registered
|
|
8
|
+
// their targets before those modules are imported. If any patched module is
|
|
9
|
+
// imported first, it is served from cache and the patch is silently skipped.
|
|
10
|
+
//
|
|
11
|
+
// hncode's real work lives in modules (agent.js, llm.js, tools/*) that index.js
|
|
12
|
+
// imports at the top, so index.js cannot load plugins itself — by the time it ran
|
|
13
|
+
// loadPlugins(), those modules would already be cached. This module runs first
|
|
14
|
+
// (see bin/hncode) and does the ordering that makes patching possible.
|
|
15
|
+
//
|
|
16
|
+
// It is deliberately tiny and dependency-light: it only needs config (for the
|
|
17
|
+
// plugin directory) plus plugin.js / patch.js. Everything else stays lazy.
|
|
18
|
+
//
|
|
19
|
+
// FAILURE POSTURE: bootstrap must NEVER stop hncode from starting. A missing
|
|
20
|
+
// Node API, a malformed config, or a broken plugin directory degrades to "no
|
|
21
|
+
// patches / no plugins", never to "hncode won't launch".
|
|
22
|
+
|
|
23
|
+
// Set once plugins have been loaded (by bootstrap or by index.js), so index.js
|
|
24
|
+
// does not load them a second time and trip "tool already registered".
|
|
25
|
+
let pluginsLoaded = false;
|
|
26
|
+
export function pluginsAlreadyLoaded() { return pluginsLoaded; }
|
|
27
|
+
export function markPluginsLoaded() { pluginsLoaded = true; }
|
|
28
|
+
|
|
29
|
+
export async function bootstrap({ loadPlugins = true, surface = 'tui' } = {}) {
|
|
30
|
+
// Patch loader first, but never let it break startup: installPatchLoader is
|
|
31
|
+
// already non-throwing, and we belt-and-brace with try/catch here.
|
|
32
|
+
try {
|
|
33
|
+
const { installPatchLoader } = await import('./patch.js');
|
|
34
|
+
installPatchLoader();
|
|
35
|
+
} catch (e) {
|
|
36
|
+
console.error('[hncode] patch loader unavailable, continuing without it:', e && e.message);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!loadPlugins) return { pluginDir: null, loaded: [] };
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
// WHICH SURFACE THE PLUGINS ARE LOADING INTO, set before anything else can matter.
|
|
43
|
+
//
|
|
44
|
+
// A plugin reads `api.surface` during install() to decide whether to register a rail row or a
|
|
45
|
+
// status-bar widget, so the answer has to be in place before the first install runs. It is set
|
|
46
|
+
// even when there is no plugin directory: `setPluginSurface` is cheap, and leaving it at the
|
|
47
|
+
// default would make a later `/reload` inherit the wrong surface.
|
|
48
|
+
const { setPluginSurface, loadPlugins: load } = await import('./plugin.js');
|
|
49
|
+
setPluginSurface(surface);
|
|
50
|
+
const { resolveConfig } = await import('./config.js');
|
|
51
|
+
const cfg = resolveConfig();
|
|
52
|
+
if (!cfg.pluginDir) return { pluginDir: null, loaded: [] };
|
|
53
|
+
|
|
54
|
+
const loaded = await load(cfg.pluginDir);
|
|
55
|
+
markPluginsLoaded();
|
|
56
|
+
return { pluginDir: cfg.pluginDir, loaded };
|
|
57
|
+
} catch (e) {
|
|
58
|
+
// A broken plugin dir/config must not stop hncode. Report and carry on.
|
|
59
|
+
console.error('[hncode] plugin bootstrap failed, continuing without plugins:', e && e.message);
|
|
60
|
+
markPluginsLoaded();
|
|
61
|
+
return { pluginDir: null, loaded: [], error: e };
|
|
62
|
+
}
|
|
63
|
+
}
|