agent-whisker 0.1.0 → 0.2.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 +12 -3
- package/README.zh-CN.md +12 -3
- package/package.json +3 -2
- package/packages/antigravity-plugin/package.json +1 -1
- package/packages/antigravity-plugin/plugin.json +1 -1
- package/packages/claude-plugin/.claude-plugin/plugin.json +1 -1
- package/packages/claude-plugin/.codex-plugin/plugin.json +1 -1
- package/packages/claude-plugin/README.md +11 -1
- package/packages/claude-plugin/README.zh-CN.md +4 -1
- package/packages/claude-plugin/bin/taskstate +145 -4
- package/packages/claude-plugin/hooks/codex.json +8 -8
- package/packages/claude-plugin/package.json +1 -1
- package/packages/cli/README.md +76 -3
- package/packages/cli/README.zh-CN.md +76 -3
- package/packages/cli/package.json +1 -1
- package/packages/cli/src/command.js +20 -3
- package/packages/cli/src/commands/catalog.js +26 -7
- package/packages/cli/src/commands/doctor.js +31 -27
- package/packages/cli/src/commands/exclude.js +156 -0
- package/packages/cli/src/commands/extract.js +4 -2
- package/packages/cli/src/commands/index-command.js +1 -1
- package/packages/cli/src/commands/index.js +4 -0
- package/packages/cli/src/commands/install.js +12 -1
- package/packages/cli/src/commands/nudge.js +39 -37
- package/packages/cli/src/commands/read.js +2 -1
- package/packages/cli/src/commands/search.js +33 -3
- package/packages/cli/src/commands/sessions.js +20 -12
- package/packages/cli/src/commands/status.js +6 -3
- package/packages/cli/src/commands/tui.js +62 -0
- package/packages/cli/src/commands/version.js +4 -4
- package/packages/cli/src/exclusion.js +88 -0
- package/packages/cli/src/install/codex-plugin.js +6 -5
- package/packages/cli/src/install/external.js +48 -37
- package/packages/cli/src/install/handlers.js +4 -3
- package/packages/cli/src/install/mcp.js +3 -3
- package/packages/cli/src/install/steps.js +43 -15
- package/packages/cli/src/install/wrapper.js +28 -5
- package/packages/cli/src/main.js +9 -12
- package/packages/cli/src/node-version.js +25 -0
- package/packages/cli/src/run.js +25 -5
- package/packages/cli/src/tui/app.js +1430 -0
- package/packages/cli/src/tui/data/call.js +83 -0
- package/packages/cli/src/tui/data/poll.js +24 -0
- package/packages/cli/src/tui/glyphs.js +93 -0
- package/packages/cli/src/tui/i18n.js +342 -0
- package/packages/cli/src/tui/model.js +251 -0
- package/packages/cli/src/tui/pages/common.js +71 -0
- package/packages/cli/src/tui/pages/library.js +154 -0
- package/packages/cli/src/tui/pages/sessions.js +96 -0
- package/packages/cli/src/tui/pages/system.js +124 -0
- package/packages/cli/src/tui/pages/tasks.js +297 -0
- package/packages/cli/src/tui/run.js +216 -0
- package/packages/cli/src/tui/term/keys.js +152 -0
- package/packages/cli/src/tui/term/screen.js +88 -0
- package/packages/cli/src/tui/term/term.js +157 -0
- package/packages/cli/src/tui/term/width.js +290 -0
- package/packages/cli/src/tui/ui/widgets.js +247 -0
- package/packages/core/README.md +1 -1
- package/packages/core/README.zh-CN.md +2 -2
- package/packages/core/package.json +1 -1
- package/packages/core/src/catalog.js +13 -1
- package/packages/core/src/clairic.js +1 -1
- package/packages/core/src/convert.js +1 -1
- package/packages/core/src/db.js +6 -1
- package/packages/core/src/host.js +104 -0
- package/packages/core/src/index.js +2 -2
- package/packages/core/src/library-read.js +12 -8
- package/packages/core/src/nudge.js +34 -1
- package/packages/core/src/schema.js +17 -0
- package/packages/core/src/sessions-read.js +23 -11
- package/packages/dsh-plugin/package.json +1 -1
- package/packages/herdr-plugin/herdr-plugin.toml +1 -1
- package/packages/herdr-plugin/package.json +1 -1
- package/packages/mcp/README.md +5 -5
- package/packages/mcp/README.zh-CN.md +5 -5
- package/packages/mcp/package.json +1 -1
- package/packages/pi-extension/README.md +1 -1
- package/packages/pi-extension/README.zh-CN.md +1 -1
- package/packages/pi-extension/package.json +1 -1
- package/packages/pi-extension/src/extension.ts +3 -3
- package/packages/service/package.json +1 -1
- package/packages/service/src/index.js +1 -1
- package/packages/service/src/runtime.js +4 -0
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// The interface's widgets, each a pure function from state to rows: a list whose viewport counts every row an item
|
|
2
|
+
// takes (group headings and wrapped lines included, so the selected item can never scroll out of sight), the key-hint
|
|
3
|
+
// bar packed by priority, the confirmation bar, a one-line text input, and joining two panes side by side.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
import { displayWidth, fit, sliceColumns, wrap } from "../term/width.js";
|
|
7
|
+
|
|
8
|
+
/** A row of spans cut or padded to width columns (the encoder does it too; panes need it before joining). */
|
|
9
|
+
export function fitRow(row , width , ambiguous ) {
|
|
10
|
+
const out = [];
|
|
11
|
+
let col = 0;
|
|
12
|
+
for (const span of row) {
|
|
13
|
+
if (col >= width) break;
|
|
14
|
+
const w = displayWidth(span.text, ambiguous);
|
|
15
|
+
if (col + w <= width) {
|
|
16
|
+
out.push(span);
|
|
17
|
+
col += w;
|
|
18
|
+
} else {
|
|
19
|
+
const cut = sliceColumns(span.text, 0, width - col, ambiguous);
|
|
20
|
+
out.push({ text: fit(cut, width - col, ambiguous, false), ...(span.style ? { style: span.style } : {}) });
|
|
21
|
+
col = width;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (col < width) out.push({ text: " ".repeat(width - col) });
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A horizontal rule of glyph exactly width columns wide (a glyph drawn two columns wide is repeated half as often). */
|
|
29
|
+
export function rule(glyph , width , ambiguous ) {
|
|
30
|
+
const each = Math.max(1, displayWidth(glyph, ambiguous));
|
|
31
|
+
const count = Math.max(0, Math.floor(width / each));
|
|
32
|
+
return glyph.repeat(count) + " ".repeat(Math.max(0, width - count * each));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A row's spans with style added to each (the selection's inverse). */
|
|
36
|
+
function styled(row , style ) {
|
|
37
|
+
return row.map((span) => ({ text: span.text, style: { ...span.style, ...style } }));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A row of one span. */
|
|
41
|
+
export function line(text , style ) {
|
|
42
|
+
return [style ? { text, style } : { text }];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Text wrapped to width, each line a row in style, indented by indent spaces. */
|
|
46
|
+
export function wrapped(text , width , ambiguous , style , indent = 0) {
|
|
47
|
+
const room = Math.max(1, width - indent);
|
|
48
|
+
return wrap(text, room, ambiguous).map((piece) => line(" ".repeat(indent) + piece, style));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** One list item: its rows (the first is the one the cursor marks), and whether it can be selected (a heading cannot). */
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
/** Where a list's viewport starts: kept in the page's state so moving the cursor does not jump the view. */
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The rows of a list in height rows: items from viewport.top (moved so the selected item is wholly visible), with
|
|
59
|
+
* `↑ N more` and `↓ N more` rows taking a line each when items lie above or below. The selected item's rows are drawn
|
|
60
|
+
* inverse with the cursor glyph. Updates viewport.top.
|
|
61
|
+
*/
|
|
62
|
+
export function listRows(
|
|
63
|
+
items ,
|
|
64
|
+
selected ,
|
|
65
|
+
height ,
|
|
66
|
+
width ,
|
|
67
|
+
viewport ,
|
|
68
|
+
glyphs ,
|
|
69
|
+
paint ,
|
|
70
|
+
t ,
|
|
71
|
+
) {
|
|
72
|
+
if (height <= 0) return [];
|
|
73
|
+
const sizes = items.map((item) => item.rows.length);
|
|
74
|
+
const total = sizes.reduce((a, b) => a + b, 0);
|
|
75
|
+
const marked = (index ) =>
|
|
76
|
+
items[index]?.rows.map((row, n) => {
|
|
77
|
+
const lead = index === selected && n === 0 ? glyphs.cursor : " ";
|
|
78
|
+
const full = fitRow([{ text: lead }, ...row], width, paint.ambiguous);
|
|
79
|
+
return index === selected ? styled(full, { inverse: true }) : full;
|
|
80
|
+
}) ?? [];
|
|
81
|
+
if (total <= height) {
|
|
82
|
+
viewport.top = 0;
|
|
83
|
+
const out = items.flatMap((_, index) => marked(index));
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
// The first item not shown when the view starts at top: a line for `↑ more` when top > 0, and a line kept for
|
|
87
|
+
// `↓ more` after each item that is not the last.
|
|
88
|
+
const fits = (top ) => {
|
|
89
|
+
let used = top > 0 ? 1 : 0;
|
|
90
|
+
let index = top;
|
|
91
|
+
while (index < items.length) {
|
|
92
|
+
const need = (sizes[index] ?? 0) + (index + 1 < items.length ? 1 : 0);
|
|
93
|
+
if (used + need > height) break;
|
|
94
|
+
used += sizes[index] ?? 0;
|
|
95
|
+
index++;
|
|
96
|
+
}
|
|
97
|
+
return Math.max(index, Math.min(top + 1, items.length));
|
|
98
|
+
};
|
|
99
|
+
let top = Math.min(Math.max(0, viewport.top), Math.max(0, items.length - 1));
|
|
100
|
+
if (selected >= 0) {
|
|
101
|
+
if (selected < top) top = selected;
|
|
102
|
+
while (top < selected && fits(top) <= selected) top++;
|
|
103
|
+
}
|
|
104
|
+
// Fill from the bottom when the end is reached, so no empty rows show below the last item.
|
|
105
|
+
while (top > 0 && fits(top - 1) >= items.length) top--;
|
|
106
|
+
viewport.top = top;
|
|
107
|
+
const end = fits(top);
|
|
108
|
+
const out = [];
|
|
109
|
+
if (top > 0) out.push(fitRow(line(` ${t("moreAbove", top)}`, { dim: true }), width, paint.ambiguous));
|
|
110
|
+
for (let index = top; index < end; index++) out.push(...marked(index));
|
|
111
|
+
if (end < items.length)
|
|
112
|
+
out.push(fitRow(line(` ${t("moreBelow", items.length - end)}`, { dim: true }), width, paint.ambiguous));
|
|
113
|
+
return out.slice(0, height);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Moves a selection by delta among selectable items, without wrapping around; -1 when none is selectable. */
|
|
117
|
+
export function moveSelection(items , selected , delta ) {
|
|
118
|
+
const selectable = items.map((item, index) => (item.selectable ? index : -1)).filter((index) => index >= 0);
|
|
119
|
+
if (selectable.length === 0) return -1;
|
|
120
|
+
const at = selectable.indexOf(selected);
|
|
121
|
+
if (at < 0) return selectable[0] ?? -1;
|
|
122
|
+
const next = Math.min(selectable.length - 1, Math.max(0, at + delta));
|
|
123
|
+
return selectable[next] ?? -1;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The first selectable index at or after selected (clamped), so a list that shrank keeps a valid cursor. */
|
|
127
|
+
export function clampSelection(items , selected ) {
|
|
128
|
+
if (items[selected]?.selectable) return selected;
|
|
129
|
+
for (let index = Math.min(selected, items.length - 1); index >= 0; index--)
|
|
130
|
+
if (items[index]?.selectable) return index;
|
|
131
|
+
return items.findIndex((item) => item.selectable);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** A key hint: the key as shown, its label, and its priority (higher stays longer on a narrow screen). */
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The hint bar: hints in their given order, keeping the highest priorities that fit width columns; when any is left
|
|
139
|
+
* out, `?more` ends the bar. Never wider than width.
|
|
140
|
+
*/
|
|
141
|
+
export function hintBar(hints , width , ambiguous , moreLabel ) {
|
|
142
|
+
const cell = (hint ) => `${hint.key} ${hint.label}`;
|
|
143
|
+
const widthOf = (chosen , more ) =>
|
|
144
|
+
chosen.reduce((sum, hint) => sum + displayWidth(cell(hint), ambiguous) + 2, 1) +
|
|
145
|
+
(more ? displayWidth(`? ${moreLabel}`, ambiguous) : 0);
|
|
146
|
+
const byPrio = [...hints].sort((a, b) => b.prio - a.prio);
|
|
147
|
+
const chosen = new Set ();
|
|
148
|
+
for (const hint of byPrio) {
|
|
149
|
+
const next = [...chosen, hint];
|
|
150
|
+
const more = next.length < hints.length;
|
|
151
|
+
if (widthOf(next, more) <= width) chosen.add(hint);
|
|
152
|
+
}
|
|
153
|
+
const spans = [{ text: " " }];
|
|
154
|
+
for (const hint of hints) {
|
|
155
|
+
if (!chosen.has(hint)) continue;
|
|
156
|
+
spans.push({ text: hint.key, style: { bold: true } }, { text: ` ${hint.label} `, style: { dim: true } });
|
|
157
|
+
}
|
|
158
|
+
if (chosen.size < hints.length)
|
|
159
|
+
spans.push({ text: "?", style: { bold: true } }, { text: ` ${moreLabel}`, style: { dim: true } });
|
|
160
|
+
return fitRow(spans, width, ambiguous);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Shell quoting for the confirmation bar's command: a word of safe characters as is, anything else in single quotes. */
|
|
164
|
+
export function shellQuote(argv ) {
|
|
165
|
+
return argv
|
|
166
|
+
.map((word) => (/^[A-Za-z0-9_/.:=@%+,~-]+$/u.test(word) ? word : `'${word.replace(/'/gu, `'\\''`)}'`))
|
|
167
|
+
.join(" ");
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** A one-line text input: its text and the cursor's position in clusters (code units of text, at cluster bounds). */
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
/** The input after a key: printable characters and pastes insert (control characters dropped), and editing keys. */
|
|
174
|
+
export function editInput(input , key ) {
|
|
175
|
+
const { text, cursor } = input;
|
|
176
|
+
const insert = (more ) => {
|
|
177
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: pasted control characters are dropped.
|
|
178
|
+
const clean = more.replace(/[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]+/gu, " ");
|
|
179
|
+
return { text: text.slice(0, cursor) + clean + text.slice(cursor), cursor: cursor + clean.length };
|
|
180
|
+
};
|
|
181
|
+
if (key.paste !== undefined) return insert(key.paste);
|
|
182
|
+
const prev = () => {
|
|
183
|
+
const chars = [...text.slice(0, cursor)];
|
|
184
|
+
return cursor - (chars.at(-1)?.length ?? 0);
|
|
185
|
+
};
|
|
186
|
+
const next = () => cursor + ([...text.slice(cursor)][0]?.length ?? 0);
|
|
187
|
+
if (key.ctrl) {
|
|
188
|
+
if (key.name === "a") return { text, cursor: 0 };
|
|
189
|
+
if (key.name === "e") return { text, cursor: text.length };
|
|
190
|
+
if (key.name === "u") return { text: text.slice(cursor), cursor: 0 };
|
|
191
|
+
if (key.name === "w") {
|
|
192
|
+
const head = text.slice(0, cursor).replace(/\S+\s*$/u, "");
|
|
193
|
+
return { text: head + text.slice(cursor), cursor: head.length };
|
|
194
|
+
}
|
|
195
|
+
return input;
|
|
196
|
+
}
|
|
197
|
+
if (key.name === "backspace") {
|
|
198
|
+
const at = prev();
|
|
199
|
+
return { text: text.slice(0, at) + text.slice(cursor), cursor: at };
|
|
200
|
+
}
|
|
201
|
+
if (key.name === "delete") return { text: text.slice(0, cursor) + text.slice(next()), cursor };
|
|
202
|
+
if (key.name === "left") return { text, cursor: prev() };
|
|
203
|
+
if (key.name === "right") return { text, cursor: next() };
|
|
204
|
+
if (key.name === "home") return { text, cursor: 0 };
|
|
205
|
+
if (key.name === "end") return { text, cursor: text.length };
|
|
206
|
+
if (key.name === "space") return insert(" ");
|
|
207
|
+
// A C1 or bidirectional control typed alone becomes a space in insert, as a pasted one does.
|
|
208
|
+
if (!key.alt && [...key.name].length === 1 && key.name >= " ") return insert(key.name);
|
|
209
|
+
return input;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** An input drawn in width columns after a prompt, the cursor as an inverse cell, scrolled to keep it in view. */
|
|
213
|
+
export function inputRow(prompt , input , width , ambiguous ) {
|
|
214
|
+
const room = Math.max(1, width - displayWidth(prompt, ambiguous) - 1);
|
|
215
|
+
const before = input.text.slice(0, input.cursor);
|
|
216
|
+
const at = [...input.text.slice(input.cursor)][0] ?? " ";
|
|
217
|
+
const after = input.text.slice(input.cursor + (at === " " && input.cursor >= input.text.length ? 0 : at.length));
|
|
218
|
+
let head = before;
|
|
219
|
+
while (displayWidth(head, ambiguous) + displayWidth(at, ambiguous) > room && head !== "")
|
|
220
|
+
head = [...head].slice(1).join("");
|
|
221
|
+
return fitRow(
|
|
222
|
+
[{ text: prompt, style: { bold: true } }, { text: head }, { text: at, style: { inverse: true } }, { text: after }],
|
|
223
|
+
width,
|
|
224
|
+
ambiguous,
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Two panes side by side, left then a separator then right, each row exactly width columns. */
|
|
229
|
+
export function besideRows(
|
|
230
|
+
left ,
|
|
231
|
+
right ,
|
|
232
|
+
leftWidth ,
|
|
233
|
+
rightWidth ,
|
|
234
|
+
height ,
|
|
235
|
+
sep ,
|
|
236
|
+
ambiguous ,
|
|
237
|
+
) {
|
|
238
|
+
const out = [];
|
|
239
|
+
for (let r = 0; r < height; r++) {
|
|
240
|
+
out.push([
|
|
241
|
+
...fitRow(left[r] ?? [], leftWidth, ambiguous),
|
|
242
|
+
{ text: sep, style: { dim: true } },
|
|
243
|
+
...fitRow(right[r] ?? [], rightWidth, ambiguous),
|
|
244
|
+
]);
|
|
245
|
+
}
|
|
246
|
+
return out;
|
|
247
|
+
}
|
package/packages/core/README.md
CHANGED
|
@@ -4,7 +4,7 @@ English · [简体中文](README.zh-CN.md)
|
|
|
4
4
|
|
|
5
5
|
Core owns the local store: identity, journal, blobs, reducers, scope and tier checks, ingestion, document and session search, and derived index rebuilds. Its only runtime dependency is `jieba-wasm` (WebAssembly CJK segmentation), and it does not load any agent framework. Session adapters parse local files; library search contacts a reranking endpoint only when one is configured and reranking is enabled.
|
|
6
6
|
|
|
7
|
-
Other packages use the public API in `src/index.ts` to open and query the store. Only core appends journal events. Retained session history uses a separate authoritative database. `nudgeStore({ home, nowMs })` updates the file returned by `nudgePath(home)` without opening the store or taking a database lock. `store.ingestFile` picks the file's document by the [identity rules](../../docs/architecture.md#storage-and-retrieval) and returns `{ docId, version, status, removed, label }`: `status` is `added`, `moved`, `updated` or `unchanged`, `removed` says that the document is removed (ingest never restores it), and `label` is what catalog and search show as `source`.
|
|
7
|
+
Other packages use the public API in `src/index.ts` to open and query the store. Only core appends journal events. Retained session history uses a separate authoritative database. `nudgeStore({ home, nowMs })` updates the file returned by `nudgePath(home)` without opening the store or taking a database lock. `storeStamp(home)` returns a fingerprint string made from `lstat` of `truth/journal.db`, `truth/journal.db-wal`, `truth/sessions.db`, `truth/sessions.db-wal` and `run/nudge`, without opening the store or taking a lock; an interface compares two of them to decide whether to load again. `Host` has an optional, lazy `tty` field, which only `tui` uses; the MCP server's stdio does not touch it. `store.ingestFile` picks the file's document by the [identity rules](../../docs/architecture.md#storage-and-retrieval) and returns `{ docId, version, status, removed, label }`: `status` is `added`, `moved`, `updated` or `unchanged`, `removed` says that the document is removed (ingest never restores it), and `label` is what catalog and search show as `source`.
|
|
8
8
|
|
|
9
9
|
The journal has three streams, `library`, `registry` and `trace`; the first two have reducers, and `store.state` reads projects and documents. Append refuses an event of any other stream. A data directory is a store when it holds the file `store.lock` and the directory `maintenance/`, which are checked without a lock (they are the lock and the queue of maintenance requests), and `truth/`, which is checked and opened only while the shared lock on `store.lock` is held. `probeStore({ home })` checks the directory and the first two with lstat alone, for callers that must not wait for a lock. `initCreatesStore(home)` says, reading only, whether `initStore` would create a store there (the path does not exist or is an empty directory); the CLI, MCP and the service advise `agent-whisker init` only where it is true and pass the `NotAStore` message on elsewhere. `openStore` rebuilds older derived files and refuses newer ones with `StoreTooNew`; the rules are under [storage compatibility](#storage-compatibility). A damaged database is `Corrupt`. A file or directory of the store that cannot be opened, read or written (its permissions, a full disk, I/O), at open, in a transaction or as a blob under `truth/blobs`, is `StorageFailed`, with details `{ file, sqlite }`, SQLite's extended code or the errno code; its message keeps the system's and names no command, and nothing is rebuilt, deleted or retried because of it. See [architecture](../../docs/architecture.md) for the storage layout.
|
|
10
10
|
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
[English](README.md) · 简体中文
|
|
4
4
|
|
|
5
|
-
core 负责本地存储:存储的身份标识、日志、blob、reducer、作用域与层级检查、导入、文档与会话检索,以及派生索引的重建。它唯一的运行时依赖是 `jieba-wasm`(WebAssembly
|
|
5
|
+
core 负责本地存储:存储的身份标识、日志、blob、reducer、作用域与层级检查、导入、文档与会话检索,以及派生索引的重建。它唯一的运行时依赖是 `jieba-wasm`(WebAssembly 中日韩分词),也不加载任何 agent 框架。会话适配器解析本机文件;只有配置了重排服务并且开启了重排时,文档检索才会联系它。
|
|
6
6
|
|
|
7
|
-
其他包通过 `src/index.ts` 中的公开 API 打开和查询存储。只有 core 向日志追加事件。留存的会话历史使用另一个单独的权威数据库。`nudgeStore({ home, nowMs })` 更新 `nudgePath(home)` 返回的文件,既不打开存储,也不获取数据库锁。`store.ingestFile` 按[身份规则](../../docs/architecture.zh-CN.md#存储与检索)确定文件对应的文档,返回 `{ docId, version, status, removed, label }`:`status` 是 `added`、`moved`、`updated` 或 `unchanged`;`removed` 表示该文档已被移除(导入从不恢复它);`label` 是目录和检索结果里显示为 `source` 的内容。
|
|
7
|
+
其他包通过 `src/index.ts` 中的公开 API 打开和查询存储。只有 core 向日志追加事件。留存的会话历史使用另一个单独的权威数据库。`nudgeStore({ home, nowMs })` 更新 `nudgePath(home)` 返回的文件,既不打开存储,也不获取数据库锁。`storeStamp(home)` 只对 `truth/journal.db`、`truth/journal.db-wal`、`truth/sessions.db`、`truth/sessions.db-wal` 和 `run/nudge` 做 `lstat`,返回一个指纹字符串,不打开存储,也不加锁;界面比较前后两个指纹,判断是否需要重新加载。`Host` 有一个可选的惰性字段 `tty`,只有 `tui` 用到它;MCP 服务的 stdio 不受影响。`store.ingestFile` 按[身份规则](../../docs/architecture.zh-CN.md#存储与检索)确定文件对应的文档,返回 `{ docId, version, status, removed, label }`:`status` 是 `added`、`moved`、`updated` 或 `unchanged`;`removed` 表示该文档已被移除(导入从不恢复它);`label` 是目录和检索结果里显示为 `source` 的内容。
|
|
8
8
|
|
|
9
9
|
日志有三个流:`library`、`registry` 和 `trace`;前两个有 reducer,`store.state` 读取项目和文档。追加事件时,其他任何流的事件都会被拒绝。一个数据目录要成为存储,需要有文件 `store.lock` 和目录 `maintenance/`,它们不加锁就会被检查(它们就是锁本身和维护请求的队列);还要有 `truth/`,它只在持有 `store.lock` 的共享锁时才被检查和打开。`probeStore({ home })` 只用 lstat 检查数据目录和前两者,供不能等待锁的调用方使用。`initCreatesStore(home)` 只读地判断 `initStore` 能否在那里创建存储(路径不存在,或是一个空目录);CLI、MCP 和服务只在它为真时建议 `agent-whisker init`,其他情况原样转述 `NotAStore` 的消息。`openStore` 重建较旧的派生文件,遇到较新的派生文件则以 `StoreTooNew` 拒绝;规则见[存储兼容性](#存储兼容性)。损坏的数据库是 `Corrupt`。存储中的文件或目录无法打开、读取或写入时(权限、磁盘已满、I/O 错误),无论在打开时、在事务中还是作为 `truth/blobs` 下的 blob,都是 `StorageFailed`,details 为 `{ file, sqlite }`,即 SQLite 的扩展错误码或 errno 码;它的消息保留系统给出的原文,不提任何命令,也不会因此重建、删除或重试任何东西。存储布局见[架构](../../docs/architecture.zh-CN.md)。
|
|
10
10
|
|
|
@@ -127,6 +127,12 @@ const CATALOG_DEFAULT_LIMIT = 50;
|
|
|
127
127
|
|
|
128
128
|
|
|
129
129
|
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
130
136
|
|
|
131
137
|
|
|
132
138
|
/** One catalog entry. */
|
|
@@ -175,6 +181,7 @@ const CATALOG_FIELDS = new Set([
|
|
|
175
181
|
"title",
|
|
176
182
|
"folder",
|
|
177
183
|
"countBy",
|
|
184
|
+
"removed",
|
|
178
185
|
]);
|
|
179
186
|
|
|
180
187
|
/** checkCatalogQuery result: defaults filled, after split into two parts (null if omitted), folder in stored form. */
|
|
@@ -187,6 +194,8 @@ const CATALOG_FIELDS = new Set([
|
|
|
187
194
|
|
|
188
195
|
|
|
189
196
|
|
|
197
|
+
|
|
198
|
+
|
|
190
199
|
|
|
191
200
|
|
|
192
201
|
|
|
@@ -264,7 +273,10 @@ function checkCatalogQuery(query ) {
|
|
|
264
273
|
throw invalidArgument("countBy must be kind, folder or both");
|
|
265
274
|
const ignored =
|
|
266
275
|
countBy === undefined ? [] : (["limit", "after"] ).filter((name) => fields[name] !== undefined);
|
|
276
|
+
if (fields.removed !== undefined && typeof fields.removed !== "boolean")
|
|
277
|
+
throw invalidArgument("removed must be a boolean");
|
|
267
278
|
return {
|
|
279
|
+
removed: fields.removed === true,
|
|
268
280
|
scopes,
|
|
269
281
|
maxTier,
|
|
270
282
|
docKinds,
|
|
@@ -353,7 +365,7 @@ const FOLDER_SQL = "agent_whisker_folder";
|
|
|
353
365
|
*/
|
|
354
366
|
function catalogFilter(query , windows ) {
|
|
355
367
|
const params = [query.maxTier, ...query.scopes];
|
|
356
|
-
let sql = `WHERE d.max_tier = ? AND d.deleted = 0 AND d.scope IN (${placeholders(query.scopes.length)})`;
|
|
368
|
+
let sql = `WHERE d.max_tier = ? AND d.deleted = ${query.removed ? 1 : 0} AND d.scope IN (${placeholders(query.scopes.length)})`;
|
|
357
369
|
if (query.docKinds !== undefined) {
|
|
358
370
|
sql += ` AND d.doc_kind IN (${placeholders(query.docKinds.length)})`;
|
|
359
371
|
params.push(...query.docKinds);
|
|
@@ -349,7 +349,6 @@ export async function extractFromFiles(
|
|
|
349
349
|
host ,
|
|
350
350
|
input ,
|
|
351
351
|
) {
|
|
352
|
-
const extraction = request(host, input);
|
|
353
352
|
const files = input.paths.map((given) => {
|
|
354
353
|
const path = localFile(resolve(host.cwd, given));
|
|
355
354
|
if (!path) throw new StoreError("MissingSource", "an extraction input file does not exist");
|
|
@@ -357,6 +356,7 @@ export async function extractFromFiles(
|
|
|
357
356
|
});
|
|
358
357
|
if (input.pages && files.filter(({ ext }) => ext === ".pdf").length !== 1)
|
|
359
358
|
throw invalidArgument("pages requires exactly one PDF input");
|
|
359
|
+
const extraction = request(host, input);
|
|
360
360
|
for (const { path, ext } of files) {
|
|
361
361
|
const mime = IMAGE_MIME.get(ext);
|
|
362
362
|
if (mime) extraction.image(path, mime);
|
|
@@ -120,7 +120,7 @@ function runConverter(file , spec , program , ar
|
|
|
120
120
|
const kind = spec.extensions.join(", ");
|
|
121
121
|
throw new StoreError(
|
|
122
122
|
"NotAvailable",
|
|
123
|
-
`${program} is not installed (or could not be started), so ${kind} files cannot be ingested; install
|
|
123
|
+
`${program} is not installed (or could not be started), so ${kind} files cannot be ingested; install ${program === "pdftotext" ? "Poppler (poppler on Homebrew, poppler-utils on Debian/Ubuntu)" : "pandoc"} and ingest again`,
|
|
124
124
|
{ details: { program } },
|
|
125
125
|
);
|
|
126
126
|
}
|
package/packages/core/src/db.js
CHANGED
|
@@ -29,6 +29,7 @@ import {
|
|
|
29
29
|
JOURNAL_SCHEMA,
|
|
30
30
|
KEPT_SESSIONS_SCHEMA,
|
|
31
31
|
LOCK_SCHEMA,
|
|
32
|
+
missingFtsSupport,
|
|
32
33
|
readIdentity,
|
|
33
34
|
|
|
34
35
|
SESSIONS_SCHEMA,
|
|
@@ -353,6 +354,8 @@ function quickCheck(db , file ) {
|
|
|
353
354
|
*/
|
|
354
355
|
function mapOpenError(error , file ) {
|
|
355
356
|
if (error instanceof StoreError) return error;
|
|
357
|
+
const unavailable = missingFtsSupport(error);
|
|
358
|
+
if (unavailable) return unavailable;
|
|
356
359
|
const code = sqliteErrcode(error);
|
|
357
360
|
if (code === undefined) return fileSystemFailed(error, file) ?? error;
|
|
358
361
|
if (code === SQLITE_BUSY) {
|
|
@@ -1032,7 +1035,7 @@ export function createDatabaseFile(path , schema , fill
|
|
|
1032
1035
|
db.exec("COMMIT");
|
|
1033
1036
|
} catch (error) {
|
|
1034
1037
|
rollbackQuietly(db);
|
|
1035
|
-
throw error;
|
|
1038
|
+
throw missingFtsSupport(error) ?? error;
|
|
1036
1039
|
}
|
|
1037
1040
|
} finally {
|
|
1038
1041
|
db.close();
|
|
@@ -1296,6 +1299,8 @@ export function withImmediateTransaction (db , fn , mapEr
|
|
|
1296
1299
|
*/
|
|
1297
1300
|
export function derivedDbError(error , file ) {
|
|
1298
1301
|
if (error instanceof StoreError) return error;
|
|
1302
|
+
const unavailable = missingFtsSupport(error);
|
|
1303
|
+
if (unavailable) return unavailable;
|
|
1299
1304
|
const code = sqliteErrcode(error);
|
|
1300
1305
|
if (code === SQLITE_BUSY) {
|
|
1301
1306
|
return new StoreError("Busy", `${file} is locked by another process; try again`, {
|
|
@@ -26,6 +26,7 @@ import { invalidEvent } from "./errors.js";
|
|
|
26
26
|
* - fetch(url, init): sends an HTTP request, see HostFetch. The process's only way out to the network; only
|
|
27
27
|
* reranking (and doctor's rerank check) uses it.
|
|
28
28
|
* - out(text), err(text): write to stdout and stderr verbatim, with no added newline.
|
|
29
|
+
* - tty: the interactive terminal, optional and lazy (HostTty). Only `agent-whisker tui` uses it.
|
|
29
30
|
*/
|
|
30
31
|
|
|
31
32
|
|
|
@@ -51,6 +52,7 @@ import { invalidEvent } from "./errors.js";
|
|
|
51
52
|
|
|
52
53
|
|
|
53
54
|
|
|
55
|
+
|
|
54
56
|
|
|
55
57
|
|
|
56
58
|
|
|
@@ -77,6 +79,41 @@ import { invalidEvent } from "./errors.js";
|
|
|
77
79
|
|
|
78
80
|
|
|
79
81
|
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Host.tty: the interactive terminal on the process's stdin and stdout, for `agent-whisker tui` alone.
|
|
88
|
+
*
|
|
89
|
+
* Lazy by contract: creating it touches nothing, and only calling one of its methods reaches process.stdin,
|
|
90
|
+
* process.stdout or process.on. `agent-whisker mcp` speaks JSON-RPC over the same stdin and stdout and never calls
|
|
91
|
+
* these methods, so its streams are never put in raw mode, decoded or paused by this object. Every method is a plain
|
|
92
|
+
* function (no getter), so copying a Host with `{ ...host }` (runCli does) calls none of them.
|
|
93
|
+
*
|
|
94
|
+
* - isTTY(): whether both stdin and stdout are terminals.
|
|
95
|
+
* - size(): the terminal's columns and rows (80×24 when the terminal does not say).
|
|
96
|
+
* - write(bytes): writes to stdout as is; a write the terminal refuses (EIO, EPIPE after a hang-up) is dropped.
|
|
97
|
+
* - setRaw(on): raw mode on stdin (no echo, no line editing, Ctrl+C arrives as byte 3).
|
|
98
|
+
* - onInput(fn): stdin decoded as UTF-8, a character split across chunks kept whole; resumes stdin, and the returned
|
|
99
|
+
* function removes the listener and pauses stdin again so the process can end.
|
|
100
|
+
* - onResize(fn): stdout's resize event (Node emits it on SIGWINCH, and by polling on Windows).
|
|
101
|
+
* - onSignal(signal, fn): a listener for SIGTERM, SIGHUP, SIGINT or SIGCONT; returns its removal.
|
|
102
|
+
* - onFatal(fn): fn runs on an uncaught exception or unhandled rejection, and on the process's exit event, so a
|
|
103
|
+
* terminal can be restored before the error is printed; after an uncaught error the listener is removed and the
|
|
104
|
+
* error thrown again on the next turn, so the process still ends as Node ends it (code 1, the error on stderr).
|
|
105
|
+
* - suspend(): stops the process as Ctrl+Z does in a shell (SIGTSTP to itself); a SIGCONT listener sees the resume.
|
|
106
|
+
*/
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
80
117
|
|
|
81
118
|
|
|
82
119
|
/**
|
|
@@ -186,6 +223,7 @@ export function systemHost() {
|
|
|
186
223
|
for (const name of WITHHELD_ENV) delete childEnv[name];
|
|
187
224
|
const host = {
|
|
188
225
|
platform: platformOf(process.platform),
|
|
226
|
+
...(process.getuid ? { uid: process.getuid() } : {}),
|
|
189
227
|
env,
|
|
190
228
|
homeDir: homedir(),
|
|
191
229
|
cwd: process.cwd(),
|
|
@@ -234,10 +272,76 @@ export function systemHost() {
|
|
|
234
272
|
err: (text) => {
|
|
235
273
|
process.stderr.write(text);
|
|
236
274
|
},
|
|
275
|
+
tty: systemTty(),
|
|
237
276
|
};
|
|
238
277
|
return Object.freeze(host);
|
|
239
278
|
}
|
|
240
279
|
|
|
280
|
+
/**
|
|
281
|
+
* The real HostTty over process.stdin and process.stdout. Creating it reads nothing; see HostTty for each method.
|
|
282
|
+
* Writes go through process.stdout.write, which is synchronous on a terminal on every platform Node supports.
|
|
283
|
+
*/
|
|
284
|
+
function systemTty() {
|
|
285
|
+
const tty = {
|
|
286
|
+
isTTY: () => process.stdin.isTTY === true && process.stdout.isTTY === true,
|
|
287
|
+
size: () => ({ cols: process.stdout.columns || 80, rows: process.stdout.rows || 24 }),
|
|
288
|
+
write(bytes) {
|
|
289
|
+
try {
|
|
290
|
+
process.stdout.write(bytes);
|
|
291
|
+
} catch {
|
|
292
|
+
// A terminal that hung up (EIO, EPIPE): nothing more can be shown on it.
|
|
293
|
+
}
|
|
294
|
+
},
|
|
295
|
+
setRaw(on) {
|
|
296
|
+
if (typeof process.stdin.setRawMode === "function") process.stdin.setRawMode(on);
|
|
297
|
+
},
|
|
298
|
+
onInput(fn) {
|
|
299
|
+
const decoder = new TextDecoder("utf-8");
|
|
300
|
+
const listener = (chunk ) => {
|
|
301
|
+
const text = typeof chunk === "string" ? chunk : decoder.decode(chunk, { stream: true });
|
|
302
|
+
if (text !== "") fn(text);
|
|
303
|
+
};
|
|
304
|
+
process.stdin.on("data", listener);
|
|
305
|
+
process.stdin.resume();
|
|
306
|
+
return () => {
|
|
307
|
+
process.stdin.off("data", listener);
|
|
308
|
+
process.stdin.pause();
|
|
309
|
+
};
|
|
310
|
+
},
|
|
311
|
+
onResize(fn) {
|
|
312
|
+
process.stdout.on("resize", fn);
|
|
313
|
+
return () => process.stdout.off("resize", fn);
|
|
314
|
+
},
|
|
315
|
+
onSignal(signal, fn) {
|
|
316
|
+
process.on(signal, fn);
|
|
317
|
+
return () => process.off(signal, fn);
|
|
318
|
+
},
|
|
319
|
+
onFatal(fn) {
|
|
320
|
+
const fatal = (error ) => {
|
|
321
|
+
remove();
|
|
322
|
+
fn(error);
|
|
323
|
+
setImmediate(() => {
|
|
324
|
+
throw error;
|
|
325
|
+
});
|
|
326
|
+
};
|
|
327
|
+
const exit = () => fn(null);
|
|
328
|
+
const remove = () => {
|
|
329
|
+
process.off("uncaughtException", fatal);
|
|
330
|
+
process.off("unhandledRejection", fatal);
|
|
331
|
+
process.off("exit", exit);
|
|
332
|
+
};
|
|
333
|
+
process.on("uncaughtException", fatal);
|
|
334
|
+
process.on("unhandledRejection", fatal);
|
|
335
|
+
process.on("exit", exit);
|
|
336
|
+
return remove;
|
|
337
|
+
},
|
|
338
|
+
suspend: () => {
|
|
339
|
+
process.kill(process.pid, "SIGTSTP");
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
return Object.freeze(tty);
|
|
343
|
+
}
|
|
344
|
+
|
|
241
345
|
/** Maps process.platform to Host's three platforms: darwin and win32 as is, anything else (linux, freebsd, aix, ...) as linux. */
|
|
242
346
|
function platformOf(platform ) {
|
|
243
347
|
return platform === "darwin" || platform === "win32" ? platform : "linux";
|
|
@@ -23,14 +23,14 @@ export { EXTRACTOR_PROGRAMS, INGEST_EXTENSIONS } from "./extractors.js";
|
|
|
23
23
|
export { isoTime, oneLine } from "./format.js";
|
|
24
24
|
|
|
25
25
|
export { GREP_DEFAULT_LIMIT, GREP_MAX_CONTEXT } from "./grep.js";
|
|
26
|
-
|
|
26
|
+
|
|
27
27
|
export { envValue, findProgram, resolveHome, systemHost } from "./host.js";
|
|
28
28
|
|
|
29
29
|
export { tierOfId } from "./ids.js";
|
|
30
30
|
|
|
31
31
|
|
|
32
32
|
|
|
33
|
-
export { nudgePath, nudgeStore } from "./nudge.js";
|
|
33
|
+
export { nudgePath, nudgeStore, storeStamp } from "./nudge.js";
|
|
34
34
|
|
|
35
35
|
|
|
36
36
|
export { reclassifyDocumentKinds } from "./reclassify.js";
|
|
@@ -23,8 +23,8 @@ import { checkBudget, checkIntegerFrom, checkQueryObject, firstLocator, growWind
|
|
|
23
23
|
// Constants and types
|
|
24
24
|
// ---------------------------------------------------------------------------
|
|
25
25
|
|
|
26
|
-
/**
|
|
27
|
-
const VERSION_PATTERN = /^[0-9a-f]{64}$/;
|
|
26
|
+
/** Accepted explicit document version: full hash or cursor-length prefix. */
|
|
27
|
+
const VERSION_PATTERN = /^(?:[0-9a-f]{12}|[0-9a-f]{64})$/;
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* readSection maxBytes: default 16384, no maximum, for the same reason as the catalog limit. A smaller value than 4,
|
|
@@ -539,7 +539,7 @@ function checkOffset(value , what ) {
|
|
|
539
539
|
|
|
540
540
|
/**
|
|
541
541
|
* Validate readSection arguments, choose the locator used, and return a copy with defaults filled.
|
|
542
|
-
* Throws InvalidArgument for non-plain objects, extra fields, invalid scopes/docId/maxTier, supplied version not
|
|
542
|
+
* Throws InvalidArgument for non-plain objects, extra fields, invalid scopes/docId/maxTier, supplied version not a full hash or a cursor-length prefix,
|
|
543
543
|
* supplied section not a non-negative safe integer, supplied start/end not integers, supplied page not a safe integer
|
|
544
544
|
* >= 1, supplied chunk/before/after not non-negative safe integers, supplied from not a next as readSection writes it
|
|
545
545
|
* (parseNext), version not starting with the prefix from carries, maxBytes not a safe integer, or supplied outline or
|
|
@@ -560,7 +560,7 @@ function checkReadSectionQuery(query ) {
|
|
|
560
560
|
const maxTier = checkTier(fields.maxTier);
|
|
561
561
|
const version = fields.version;
|
|
562
562
|
if (version !== undefined && (typeof version !== "string" || !VERSION_PATTERN.test(version))) {
|
|
563
|
-
throw invalidArgument("version must be 64 lowercase hexadecimal
|
|
563
|
+
throw invalidArgument("version must be a full 64-character lowercase hexadecimal hash or its 12-character prefix");
|
|
564
564
|
}
|
|
565
565
|
const section = fields.section;
|
|
566
566
|
if (section !== undefined && (typeof section !== "number" || !Number.isSafeInteger(section) || section < 0)) {
|
|
@@ -672,16 +672,18 @@ function pinVersion(db , query ) {
|
|
|
672
672
|
const doc = documentRow.get(query.maxTier, scope, query.docId);
|
|
673
673
|
if (doc === undefined) continue;
|
|
674
674
|
let version ;
|
|
675
|
-
|
|
676
|
-
|
|
675
|
+
const prefix = query.version ?? query.from?.version;
|
|
676
|
+
if (query.version !== undefined && query.version.length !== NEXT_VERSION_CHARS) version = query.version;
|
|
677
|
+
else if (prefix !== undefined) {
|
|
677
678
|
// At most two rows tell none, one and several apart; the primary key keeps this to the document's own versions.
|
|
678
679
|
const named = db
|
|
679
680
|
.prepare(
|
|
680
681
|
`SELECT version FROM versions
|
|
681
682
|
WHERE max_tier = ? AND scope = ? AND doc_id = ? AND substr(version, 1, ?) = ? LIMIT 2`,
|
|
682
683
|
)
|
|
683
|
-
.all(query.maxTier, scope, query.docId, NEXT_VERSION_CHARS,
|
|
684
|
-
if (named.length !== 1)
|
|
684
|
+
.all(query.maxTier, scope, query.docId, NEXT_VERSION_CHARS, prefix);
|
|
685
|
+
if (named.length !== 1)
|
|
686
|
+
return { missing: named.length > 1 ? "ambiguous" : query.version ? "version" : "from", scope };
|
|
685
687
|
version = String(named[0]?.version);
|
|
686
688
|
} else version = doc.current_version === null ? null : String(doc.current_version);
|
|
687
689
|
if (version === null) return { missing: "no-version", scope };
|
|
@@ -711,6 +713,8 @@ function lookupError(query , lookup
|
|
|
711
713
|
return fromInvalid(`document ${query.docId} has no version starting ${query.from?.version}`);
|
|
712
714
|
}
|
|
713
715
|
if (lookup.missing === "ambiguous") {
|
|
716
|
+
if (query.version)
|
|
717
|
+
return invalidArgument(`version ${query.version} starts several versions; give the full 64-character version`);
|
|
714
718
|
return invalidArgument(
|
|
715
719
|
`from names version ${query.from?.version}…, which starts several versions of document ${query.docId}; give version too`,
|
|
716
720
|
);
|