@mnmal-ai/substrate-tui 1.0.0-alpha.8 → 1.0.0-beta.1
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 +23 -1
- package/dist/{app-D0ccrk1L.js → app-h4FA6Urt.js} +1078 -142
- package/dist/bin/substrate-tui.js +4 -4
- package/dist/{deps-WzqsHHiC.js → deps-CMPHUv70.js} +7 -3
- package/dist/{terminal-XfKFiD7r.js → terminal-4hKQ29CS.js} +8 -3
- package/dist/{terminal-CVX2CKWu.js → terminal-BAd8sH3d.js} +1 -1
- package/package.json +10 -10
|
@@ -1,31 +1,160 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { i as isKeyRejection, t as adminCapabilityOf } from "./session-C1D5Ermr.js";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
|
-
import { AdminBackfillError, AuthError, CURRENT_MARK, NetworkError, STEP_MARKS, adminClaimCapabilities, capabilitiesFor, checkRevocable, checkUpdatable, currentStep, decodeList, describeGrant, diagnose, encodeGrant, flattenGrant, grantEntriesConfer, isActiveAdminKeyRow, isEncryptedKey, makeTokenProvider, parseGrantEntries, planOnboarding, resolveAdminCredential, tokenAudience } from "@mnmal-ai/substrate/client";
|
|
5
|
-
import { createApp, defineModule, sanitise } from "@mnmal-ai/surface-core";
|
|
4
|
+
import { AdminBackfillError, AuthError, CURRENT_MARK, NetworkError, STEP_MARKS, adminClaimCapabilities, capabilitiesFor, checkRevocable, checkUpdatable, currentStep, decodeList, describeGrant, diagnose, encodeGrant, flattenGrant, grantEntriesConfer, isActiveAdminKeyRow, isEncryptedKey, makeTokenProvider, parseGrantEntries, planOnboarding, resolveAdminCredential, substrateFetch, tokenAudience } from "@mnmal-ai/substrate/client";
|
|
5
|
+
import { createApp, defineModule, listPager, sanitise } from "@mnmal-ai/surface-core";
|
|
6
6
|
import { marathon } from "@mnmal-ai/surface-theme";
|
|
7
|
-
import { complete } from "@mnmal-ai/substrate-nl/complete";
|
|
8
|
-
import { Panel, cellWidth, padCells, sparkline, truncateCells } from "@mnmal-ai/surface-kit";
|
|
9
7
|
import { Box, Text } from "ink";
|
|
8
|
+
import { createElement } from "react";
|
|
9
|
+
import { Panel, cellWidth, padCells, sparkline, truncateCells } from "@mnmal-ai/surface-kit";
|
|
10
10
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
11
|
+
import { complete } from "@mnmal-ai/substrate-nl/complete";
|
|
11
12
|
import { readFile } from "node:fs/promises";
|
|
12
13
|
import { createHash, createPublicKey } from "node:crypto";
|
|
13
14
|
import { homedir } from "node:os";
|
|
14
15
|
import { readFileSync } from "node:fs";
|
|
15
16
|
|
|
17
|
+
//#region src/lines.tsx
|
|
18
|
+
/** Lines as a column of `Text`, each in its tone: a joined string view has no tone to give them. */
|
|
19
|
+
function linesView(lines, theme) {
|
|
20
|
+
return /* @__PURE__ */ jsx(Box, {
|
|
21
|
+
flexDirection: "column",
|
|
22
|
+
children: lines.map((l, i) => /* @__PURE__ */ jsx(Text, {
|
|
23
|
+
...l.tone !== void 0 ? { color: theme.color[l.tone] } : {},
|
|
24
|
+
children: l.text
|
|
25
|
+
}, i))
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/admin/mutate.ts
|
|
31
|
+
const KEY = /^[A-Za-z_][\w-]*\/[A-Za-z_]\w*$/;
|
|
32
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
33
|
+
const line$3 = (text, tone) => ({
|
|
34
|
+
text: sanitise(text),
|
|
35
|
+
...tone !== void 0 ? { tone } : {}
|
|
36
|
+
});
|
|
37
|
+
/** JSON, one Line per printed line: what was sent or received, verbatim. */
|
|
38
|
+
const jsonLines = (v, tone) => JSON.stringify(v, null, 2).split("\n").map((t) => line$3(` ${t}`, tone));
|
|
39
|
+
/** The typed text, parsed into a frame whose every key is `<ns>/<mutation>`, or why not. */
|
|
40
|
+
function parseMutateFrame(text) {
|
|
41
|
+
let v;
|
|
42
|
+
try {
|
|
43
|
+
v = JSON.parse(text);
|
|
44
|
+
} catch (err) {
|
|
45
|
+
return { error: `not JSON: ${err instanceof Error ? err.message : String(err)}` };
|
|
46
|
+
}
|
|
47
|
+
if (!isObject(v) || Object.keys(v).length === 0) return { error: "a mutate frame is a non-empty object: { \"<ns>/<mutation>\": { \"params\": … } }" };
|
|
48
|
+
for (const [k, entry] of Object.entries(v)) {
|
|
49
|
+
if (!KEY.test(k)) return { error: `'${k}' is not a qualified <ns>/<mutation> key` };
|
|
50
|
+
const entries = Array.isArray(entry) ? entry : [entry];
|
|
51
|
+
if (entries.length === 0) return { error: `'${k}' is an empty batch` };
|
|
52
|
+
if (!entries.every(isObject)) return { error: `'${k}' must be a { params, fields } object or an array of them` };
|
|
53
|
+
}
|
|
54
|
+
return { frame: v };
|
|
55
|
+
}
|
|
56
|
+
/** The text the operator types to send it: the frame's keys, space-separated. */
|
|
57
|
+
const confirmOf = (frame) => Object.keys(frame).join(" ");
|
|
58
|
+
/** A dry description: which mutation, how many entries, which ids and which params. Sends nothing. */
|
|
59
|
+
function describeMutate(frame, kid) {
|
|
60
|
+
const out = [];
|
|
61
|
+
for (const [k, entry] of Object.entries(frame)) {
|
|
62
|
+
const entries = Array.isArray(entry) ? entry : [entry];
|
|
63
|
+
out.push(line$3(`${k} · ${entries.length === 1 ? "1 entry" : `${entries.length} entries, one transaction`}`));
|
|
64
|
+
for (const e of entries) {
|
|
65
|
+
const params = isObject(e.params) ? e.params : {};
|
|
66
|
+
const id = params.id;
|
|
67
|
+
const rest = Object.keys(params).filter((p) => p !== "id");
|
|
68
|
+
out.push(line$3(` ${id !== void 0 ? `row ${String(id)}` : "no id"} · ${rest.length > 0 ? `params ${rest.join(", ")}` : "no other params"}`, "dim"));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
out.push(line$3(`signed as admin ${kid}; nothing has been sent`, "warn"));
|
|
72
|
+
out.push(line$3("frame:"), ...jsonLines(frame, "dim"));
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
/** The envelope as received: `errors` first, then each asked-for key absent from `data`, then `data`. */
|
|
76
|
+
function envelopeLines(frame, env) {
|
|
77
|
+
const errors = env.errors ?? [];
|
|
78
|
+
const data = env.data ?? {};
|
|
79
|
+
const absent = Object.keys(frame).filter((k) => !(k in data));
|
|
80
|
+
const lines = [];
|
|
81
|
+
if (errors.length > 0) lines.push(line$3("errors:", "error"), ...jsonLines(errors, "error"));
|
|
82
|
+
for (const k of absent) lines.push(line$3(`${k}: absent from data — not applied`, "error"));
|
|
83
|
+
lines.push(line$3("data:"), ...jsonLines(data));
|
|
84
|
+
return {
|
|
85
|
+
lines,
|
|
86
|
+
ok: errors.length === 0 && absent.length === 0
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/admin/refusal.ts
|
|
92
|
+
/** A server refusal as the server said it: `code: message`, sanitised, never retried. A chunked backfill wraps its failure; the cause is the refusal. */
|
|
93
|
+
function refusalOf(err) {
|
|
94
|
+
const e = err instanceof AdminBackfillError ? err.cause : err;
|
|
95
|
+
const known = e instanceof NetworkError || e instanceof AuthError;
|
|
96
|
+
const code = known ? e.code ?? String(e.status ?? "error") : "error";
|
|
97
|
+
const message = (known ? e.serverMessage : void 0) ?? (e instanceof Error ? e.message : String(e));
|
|
98
|
+
return {
|
|
99
|
+
code: sanitise(code),
|
|
100
|
+
message: sanitise(message),
|
|
101
|
+
...known && e.body !== void 0 ? { body: e.body } : {}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
16
106
|
//#region src/admin/module.ts
|
|
17
107
|
/**
|
|
18
|
-
* `/admin unlock|lock|status`.
|
|
108
|
+
* `/admin unlock|lock|status|mutate`. `mutate` asks for a raw mutate frame, previews it, and sends
|
|
109
|
+
* it signed as admin once its keys are typed (mutate.ts); locked, it is refused. Unlock resolves the admin credential the way
|
|
19
110
|
* `substrate-admin` does, asks for a passphrase on a masked panel when the key is encrypted, and
|
|
20
111
|
* verifies the identity with `diagnose` before the session holds anything: a key that is not an
|
|
21
112
|
* admin fails here, not at the first write.
|
|
22
113
|
*/
|
|
23
114
|
const STATUS_ID = "admin";
|
|
115
|
+
const lockedLine = {
|
|
116
|
+
text: "admin mutate refused: admin is locked — /admin unlock",
|
|
117
|
+
tone: "error"
|
|
118
|
+
};
|
|
24
119
|
function badge(deps, now) {
|
|
25
120
|
const s = deps.admin.state();
|
|
26
121
|
if (s.kind !== "unlocked") return null;
|
|
27
122
|
return `admin ${s.kid} · ${Math.max(0, Math.ceil((s.idleUntil - now) / 6e4))}m`;
|
|
28
123
|
}
|
|
124
|
+
function mutateView(s, theme, size) {
|
|
125
|
+
const w = Math.max(1, size.columns - 2);
|
|
126
|
+
const target = s.frame === null ? "" : sanitise(confirmOf(s.frame));
|
|
127
|
+
const head = { text: `${theme.glyph.title} admin mutate${target ? ` › ${target}` : ""} · ${s.stage}` };
|
|
128
|
+
const body = [...s.preview, ...s.result];
|
|
129
|
+
const foot = [];
|
|
130
|
+
if (s.stage === "frame") foot.push({ text: "paste the mutate frame as JSON, then enter · esc closes" });
|
|
131
|
+
if (s.stage === "preview") foot.push({ text: `type ${target} then enter to send · esc closes` });
|
|
132
|
+
if (s.stage === "applying") foot.push({
|
|
133
|
+
text: "sending…",
|
|
134
|
+
tone: "dim"
|
|
135
|
+
});
|
|
136
|
+
if (s.stage === "done" || s.stage === "refused") foot.push({
|
|
137
|
+
text: "esc closes",
|
|
138
|
+
tone: "dim"
|
|
139
|
+
});
|
|
140
|
+
if (s.error !== null) foot.push({
|
|
141
|
+
text: sanitise(s.error),
|
|
142
|
+
tone: "error"
|
|
143
|
+
});
|
|
144
|
+
const room = Math.max(0, size.rows - 1 - foot.length);
|
|
145
|
+
const shown = body.length <= room ? body : [...body.slice(0, Math.max(0, room - 1)), {
|
|
146
|
+
text: `… ${body.length - room + 1} more`,
|
|
147
|
+
tone: "dim"
|
|
148
|
+
}];
|
|
149
|
+
return linesView([
|
|
150
|
+
head,
|
|
151
|
+
...shown,
|
|
152
|
+
...foot
|
|
153
|
+
].map((l) => ({
|
|
154
|
+
...l,
|
|
155
|
+
text: truncateCells(l.text, w)
|
|
156
|
+
})), theme);
|
|
157
|
+
}
|
|
29
158
|
/** The first check that answered no, as one printable line with its fix. */
|
|
30
159
|
function refusal$1(r) {
|
|
31
160
|
const no = r.checks.find((c) => c.status === "no");
|
|
@@ -41,6 +170,8 @@ function adminModule(deps, io = {}) {
|
|
|
41
170
|
let generation = 0;
|
|
42
171
|
/** Verifications awaiting diagnose, so `/admin lock` can say it cancelled one. */
|
|
43
172
|
let pending = 0;
|
|
173
|
+
/** Bumped by each `/admin mutate`, so a result lands only in the panel that sent it. */
|
|
174
|
+
let mutateRun = 0;
|
|
44
175
|
/** Verify, then hold. Resolves to null on success, undefined when abandoned, else the refusal line. */
|
|
45
176
|
const verifyAndUnlock = async (getAuthorization, label, fallbackKid, getToken) => {
|
|
46
177
|
const mine = generation;
|
|
@@ -86,10 +217,24 @@ function adminModule(deps, io = {}) {
|
|
|
86
217
|
name: "admin",
|
|
87
218
|
summary: "unlock or lock the admin identity",
|
|
88
219
|
args: [{
|
|
89
|
-
name: "unlock|lock|status",
|
|
220
|
+
name: "unlock|lock|status|mutate",
|
|
90
221
|
optional: true
|
|
91
222
|
}],
|
|
92
223
|
async run([action], ctx) {
|
|
224
|
+
if (action === "mutate") {
|
|
225
|
+
if (!deps.admin.unlocked()) return ctx.print("admin mutate refused: admin is locked — /admin unlock", "error");
|
|
226
|
+
mutateRun += 1;
|
|
227
|
+
ctx.setDraft("");
|
|
228
|
+
ctx.open("mutate", {
|
|
229
|
+
run: mutateRun,
|
|
230
|
+
stage: "frame",
|
|
231
|
+
frame: null,
|
|
232
|
+
preview: [],
|
|
233
|
+
result: [],
|
|
234
|
+
error: null
|
|
235
|
+
});
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
93
238
|
if (action === "lock") {
|
|
94
239
|
generation += 1;
|
|
95
240
|
if (!deps.admin.unlocked()) return ctx.print(pending > 0 ? "admin unlock cancelled" : "admin is already locked", "dim");
|
|
@@ -132,54 +277,133 @@ function adminModule(deps, io = {}) {
|
|
|
132
277
|
if (why !== null && why !== void 0) ctx.print(sanitise(why), "error");
|
|
133
278
|
}
|
|
134
279
|
}],
|
|
135
|
-
panels: {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
280
|
+
panels: {
|
|
281
|
+
mutate: {
|
|
282
|
+
view: ({ state, theme, size }) => mutateView(state, theme, size),
|
|
283
|
+
submit: (s, typed, ctx) => {
|
|
284
|
+
if (s.stage === "frame") {
|
|
285
|
+
const st = deps.admin.state();
|
|
286
|
+
if (st.kind !== "unlocked") return {
|
|
287
|
+
...s,
|
|
288
|
+
error: "admin is locked — /admin unlock"
|
|
289
|
+
};
|
|
290
|
+
const parsed = parseMutateFrame(typed);
|
|
291
|
+
if ("error" in parsed) return {
|
|
292
|
+
...s,
|
|
293
|
+
error: parsed.error
|
|
294
|
+
};
|
|
295
|
+
const { frame } = parsed;
|
|
296
|
+
return {
|
|
297
|
+
...s,
|
|
298
|
+
stage: "preview",
|
|
299
|
+
frame,
|
|
300
|
+
preview: describeMutate(frame, st.kid),
|
|
301
|
+
error: null
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
if (s.stage !== "preview" || s.frame === null) return void 0;
|
|
305
|
+
if (typed !== confirmOf(s.frame)) return {
|
|
306
|
+
...s,
|
|
307
|
+
error: "doesn't match"
|
|
308
|
+
};
|
|
309
|
+
const st = deps.admin.state();
|
|
310
|
+
if (st.kind !== "unlocked") return {
|
|
311
|
+
...s,
|
|
312
|
+
stage: "refused",
|
|
313
|
+
error: null,
|
|
314
|
+
result: [lockedLine]
|
|
315
|
+
};
|
|
316
|
+
const { frame, run } = s;
|
|
317
|
+
const by = `(admin: ${st.kid})`;
|
|
318
|
+
const keys = sanitise(confirmOf(frame));
|
|
319
|
+
const ours = (fn) => ctx.update("mutate", (x) => x.run === run ? fn(x) : x);
|
|
320
|
+
deps.admin.run(({ client }) => client.mutate(frame)).then((env) => {
|
|
321
|
+
const r = envelopeLines(frame, env);
|
|
322
|
+
ctx.print(r.ok ? `✓ admin mutate ${keys} applied ${by}` : `✗ admin mutate ${keys} refused or partial — see errors ${by}`, r.ok ? "ok" : "error");
|
|
323
|
+
ours((x) => ({
|
|
324
|
+
...x,
|
|
325
|
+
stage: r.ok ? "done" : "refused",
|
|
326
|
+
result: r.lines
|
|
327
|
+
}));
|
|
328
|
+
}, (err) => {
|
|
329
|
+
const r = refusalOf(err);
|
|
330
|
+
ctx.print(`✗ admin mutate ${keys}: ${r.code}: ${r.message} ${by}`, "error");
|
|
331
|
+
ours((x) => ({
|
|
332
|
+
...x,
|
|
333
|
+
stage: "refused",
|
|
334
|
+
result: [{
|
|
335
|
+
text: `${r.code}: ${r.message}`,
|
|
336
|
+
tone: "error"
|
|
337
|
+
}]
|
|
338
|
+
}));
|
|
339
|
+
});
|
|
340
|
+
return {
|
|
341
|
+
...s,
|
|
342
|
+
stage: "applying",
|
|
343
|
+
error: null
|
|
344
|
+
};
|
|
345
|
+
}
|
|
145
346
|
},
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
347
|
+
passphrase: {
|
|
348
|
+
mask: true,
|
|
349
|
+
view: ({ state, theme }) => [
|
|
350
|
+
`${theme.glyph.title} admin unlock · ${sanitise(state.kid)}`,
|
|
351
|
+
`passphrase for ${sanitise(state.keyPath)}${state.busy ? " · checking…" : ""}`,
|
|
352
|
+
...state.error !== null ? [sanitise(state.error)] : [],
|
|
353
|
+
" type it and press enter · esc cancels"
|
|
354
|
+
].join("\n"),
|
|
355
|
+
escape: () => {
|
|
356
|
+
generation += 1;
|
|
357
|
+
},
|
|
358
|
+
submit: (state, line, ctx) => {
|
|
359
|
+
if (state.busy) return void 0;
|
|
360
|
+
let getToken;
|
|
361
|
+
try {
|
|
362
|
+
getToken = makeTokenProvider({
|
|
363
|
+
keyPath: state.keyPath,
|
|
364
|
+
kid: state.kid
|
|
365
|
+
}, {
|
|
366
|
+
capabilities: adminClaimCapabilities(deps.adminEnv),
|
|
367
|
+
passphrase: line
|
|
368
|
+
});
|
|
369
|
+
} catch {
|
|
370
|
+
return {
|
|
371
|
+
...state,
|
|
372
|
+
error: "wrong passphrase, or the key is unreadable"
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
verifyAndUnlock(() => `Bearer ${getToken()}`, state.keyPath, state.kid, getToken).then((why) => {
|
|
376
|
+
if (why === void 0) return;
|
|
377
|
+
if (why === null) ctx.close();
|
|
378
|
+
else ctx.update("passphrase", (s) => ({
|
|
379
|
+
...s,
|
|
380
|
+
busy: false,
|
|
381
|
+
error: why
|
|
382
|
+
}));
|
|
156
383
|
});
|
|
157
|
-
} catch {
|
|
158
384
|
return {
|
|
159
385
|
...state,
|
|
160
|
-
|
|
386
|
+
busy: true,
|
|
387
|
+
error: null
|
|
161
388
|
};
|
|
162
389
|
}
|
|
163
|
-
verifyAndUnlock(() => `Bearer ${getToken()}`, state.keyPath, state.kid, getToken).then((why) => {
|
|
164
|
-
if (why === void 0) return;
|
|
165
|
-
if (why === null) ctx.close();
|
|
166
|
-
else ctx.update("passphrase", (s) => ({
|
|
167
|
-
...s,
|
|
168
|
-
busy: false,
|
|
169
|
-
error: why
|
|
170
|
-
}));
|
|
171
|
-
});
|
|
172
|
-
return {
|
|
173
|
-
...state,
|
|
174
|
-
busy: true,
|
|
175
|
-
error: null
|
|
176
|
-
};
|
|
177
390
|
}
|
|
178
|
-
}
|
|
391
|
+
},
|
|
179
392
|
init(ctx) {
|
|
180
393
|
const show = () => ctx.status(STATUS_ID, badge(deps, Date.now()), "warn");
|
|
181
394
|
const off = deps.admin.onChange((s) => {
|
|
182
395
|
show();
|
|
396
|
+
if (s.kind === "locked") {
|
|
397
|
+
let stage = null;
|
|
398
|
+
ctx.update("mutate", (x) => {
|
|
399
|
+
stage = x.stage;
|
|
400
|
+
return x;
|
|
401
|
+
});
|
|
402
|
+
if (stage !== null) {
|
|
403
|
+
ctx.close();
|
|
404
|
+
ctx.print(stage === "applying" ? "admin mutate closed — admin locked; the write in flight reports its outcome below" : "admin mutate closed — admin locked", "warn");
|
|
405
|
+
}
|
|
406
|
+
}
|
|
183
407
|
if (s.kind === "locked" && s.reason !== null) ctx.print(sanitise(s.reason.startsWith("admin locked") ? s.reason : `admin locked: ${s.reason}`), "warn");
|
|
184
408
|
});
|
|
185
409
|
const tickTimer = setInterval(show, 3e4);
|
|
@@ -216,6 +440,185 @@ function adapt(draft, cursor, s) {
|
|
|
216
440
|
};
|
|
217
441
|
}
|
|
218
442
|
|
|
443
|
+
//#endregion
|
|
444
|
+
//#region src/ask/present.ts
|
|
445
|
+
/** Keys that would reach or shadow Object.prototype; refused as type and field names. */
|
|
446
|
+
const RESERVED = /* @__PURE__ */ new Set([
|
|
447
|
+
"__proto__",
|
|
448
|
+
"constructor",
|
|
449
|
+
"prototype"
|
|
450
|
+
]);
|
|
451
|
+
const dict = () => Object.create(null);
|
|
452
|
+
/** `m[k]` when `k` is m's own key. */
|
|
453
|
+
const own$1 = (m, k) => Object.hasOwn(m, k) ? m[k] : void 0;
|
|
454
|
+
const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
455
|
+
const validCurrency = (c) => {
|
|
456
|
+
try {
|
|
457
|
+
new Intl.NumberFormat("en-US", {
|
|
458
|
+
style: "currency",
|
|
459
|
+
currency: c
|
|
460
|
+
});
|
|
461
|
+
return true;
|
|
462
|
+
} catch {
|
|
463
|
+
return false;
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
var Bad = class extends Error {};
|
|
467
|
+
function entry$1(v, where) {
|
|
468
|
+
if (typeof v === "string" && v !== "") return { field: v };
|
|
469
|
+
if (isObj(v) && typeof v.field === "string" && v.field !== "" && (v.template === void 0 || typeof v.template === "string") && (v.label === void 0 || typeof v.label === "string")) return {
|
|
470
|
+
field: v.field,
|
|
471
|
+
...v.template !== void 0 ? { template: v.template } : {},
|
|
472
|
+
...v.label !== void 0 ? { label: v.label } : {}
|
|
473
|
+
};
|
|
474
|
+
throw new Bad(`${where} is not a field name or { field, template?, label? }`);
|
|
475
|
+
}
|
|
476
|
+
/** Why a field cannot be shown, or undefined when it can. A crossRef walk (`card.cardName`) is not carried by NL answer rows. */
|
|
477
|
+
const unusable = (field) => RESERVED.has(field) ? "is a reserved name" : field.includes(".") ? "crosses a reference" : void 0;
|
|
478
|
+
/** The entry, or undefined (noted) when its field cannot be shown. */
|
|
479
|
+
function usable(e, t, where, problems) {
|
|
480
|
+
const why = unusable(e.field);
|
|
481
|
+
if (why === void 0) return e;
|
|
482
|
+
problems.push({
|
|
483
|
+
type: t,
|
|
484
|
+
text: `${where}: ${e.field} ${why}; left out`
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
function rowView(t, v, problems) {
|
|
488
|
+
if (!isObj(v)) throw new Bad(`rows.${t} is not an object`);
|
|
489
|
+
const at = (k) => `rows.${t}.${k}`;
|
|
490
|
+
const list = (k) => {
|
|
491
|
+
const raw = v[k];
|
|
492
|
+
if (raw === void 0) return [];
|
|
493
|
+
if (!Array.isArray(raw)) throw new Bad(`${at(k)} is not a list`);
|
|
494
|
+
return raw.map((x, i) => usable(entry$1(x, `${at(k)}[${i}]`), t, at(k), problems)).filter((x) => x !== void 0);
|
|
495
|
+
};
|
|
496
|
+
const one = (k) => v[k] === void 0 ? void 0 : usable(entry$1(v[k], at(k)), t, at(k), problems);
|
|
497
|
+
const title = one("title");
|
|
498
|
+
const value = one("value");
|
|
499
|
+
return {
|
|
500
|
+
...title !== void 0 ? { title } : {},
|
|
501
|
+
tags: list("tags"),
|
|
502
|
+
...value !== void 0 ? { value } : {},
|
|
503
|
+
details: list("details")
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
function parseTui(raw) {
|
|
507
|
+
const problems = [];
|
|
508
|
+
const types = dict();
|
|
509
|
+
if (raw === void 0) return {
|
|
510
|
+
types,
|
|
511
|
+
problems
|
|
512
|
+
};
|
|
513
|
+
const whole = (why) => ({
|
|
514
|
+
types,
|
|
515
|
+
problems: [{ text: `tui ignored: ${why}` }]
|
|
516
|
+
});
|
|
517
|
+
if (!isObj(raw)) return whole("not an object");
|
|
518
|
+
const currency = raw.currency ?? "USD";
|
|
519
|
+
if (typeof currency !== "string" || !validCurrency(currency)) return whole(`currency ${JSON.stringify(currency)} is not an ISO 4217 code`);
|
|
520
|
+
if (raw.formats !== void 0 && !isObj(raw.formats)) return whole("formats is not an object");
|
|
521
|
+
if (raw.rows !== void 0 && !isObj(raw.rows)) return whole("rows is not an object");
|
|
522
|
+
const dead = /* @__PURE__ */ new Set();
|
|
523
|
+
const view = (t) => {
|
|
524
|
+
if (!Object.hasOwn(types, t)) types[t] = {
|
|
525
|
+
currency,
|
|
526
|
+
formats: dict()
|
|
527
|
+
};
|
|
528
|
+
return types[t];
|
|
529
|
+
};
|
|
530
|
+
for (const [key, fmt] of Object.entries(raw.formats ?? {})) {
|
|
531
|
+
const dot = key.indexOf(".");
|
|
532
|
+
const t = key.slice(0, dot);
|
|
533
|
+
if (dot <= 0 || dot === key.length - 1 || RESERVED.has(t)) {
|
|
534
|
+
problems.push({ text: `formats.${key}: key is not '<Type>.<field>'; left out` });
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
const field = key.slice(dot + 1);
|
|
538
|
+
const why = unusable(field);
|
|
539
|
+
if (why !== void 0) {
|
|
540
|
+
problems.push({
|
|
541
|
+
type: t,
|
|
542
|
+
text: `formats.${key}: ${field} ${why}; left out`
|
|
543
|
+
});
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
if (typeof fmt !== "string") {
|
|
547
|
+
problems.push({
|
|
548
|
+
type: t,
|
|
549
|
+
text: `${t} ignored: formats.${key} is not a string`
|
|
550
|
+
});
|
|
551
|
+
dead.add(t);
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
view(t).formats[field] = fmt;
|
|
555
|
+
}
|
|
556
|
+
for (const [t, spec] of Object.entries(raw.rows ?? {})) {
|
|
557
|
+
if (RESERVED.has(t)) {
|
|
558
|
+
problems.push({ text: `rows.${t}: ${t} is a reserved name; left out` });
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
try {
|
|
562
|
+
view(t).row = rowView(t, spec, problems);
|
|
563
|
+
} catch (e) {
|
|
564
|
+
if (!(e instanceof Bad)) throw e;
|
|
565
|
+
problems.push({
|
|
566
|
+
type: t,
|
|
567
|
+
text: `${t} ignored: ${e.message}`
|
|
568
|
+
});
|
|
569
|
+
dead.add(t);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
for (const t of dead) delete types[t];
|
|
573
|
+
return {
|
|
574
|
+
types,
|
|
575
|
+
problems
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
const pad2 = (n) => String(n).padStart(2, "0");
|
|
579
|
+
const localDay = (d) => `${d.getFullYear()}-${pad2(d.getMonth() + 1)}-${pad2(d.getDate())}`;
|
|
580
|
+
const DECIMAL = /^\s*-?\d+(\.\d+)?\s*$/;
|
|
581
|
+
const NUMERIC = /* @__PURE__ */ new Set([
|
|
582
|
+
"money",
|
|
583
|
+
"int",
|
|
584
|
+
"number",
|
|
585
|
+
"pct"
|
|
586
|
+
]);
|
|
587
|
+
/** Whether a format reads its value as a number. */
|
|
588
|
+
const isNumericFormat = (fmt) => fmt !== void 0 && NUMERIC.has(fmt);
|
|
589
|
+
/** `v` under `fmt`, unsanitised; raw `String(v)` when the format is unknown or does not fit the value. */
|
|
590
|
+
function formatValue(v, fmt, currency) {
|
|
591
|
+
const raw = String(v);
|
|
592
|
+
if (fmt === void 0) return raw;
|
|
593
|
+
if (NUMERIC.has(fmt)) {
|
|
594
|
+
const n = typeof v === "number" ? v : typeof v === "string" && DECIMAL.test(v) ? Number(v) : NaN;
|
|
595
|
+
if (!Number.isFinite(n)) return raw;
|
|
596
|
+
if (fmt === "money") return new Intl.NumberFormat("en-US", {
|
|
597
|
+
style: "currency",
|
|
598
|
+
currency,
|
|
599
|
+
signDisplay: "negative"
|
|
600
|
+
}).format(n);
|
|
601
|
+
if (fmt === "int") return new Intl.NumberFormat("en-US", {
|
|
602
|
+
maximumFractionDigits: 0,
|
|
603
|
+
signDisplay: "negative"
|
|
604
|
+
}).format(n);
|
|
605
|
+
const num = new Intl.NumberFormat("en-US", {
|
|
606
|
+
maximumFractionDigits: 2,
|
|
607
|
+
signDisplay: "negative"
|
|
608
|
+
}).format(n);
|
|
609
|
+
return fmt === "pct" ? `${num}%` : num;
|
|
610
|
+
}
|
|
611
|
+
if (fmt === "date" || fmt === "datetime") {
|
|
612
|
+
if (typeof v !== "string" && typeof v !== "number") return raw;
|
|
613
|
+
if (fmt === "date" && typeof v === "string" && /^\d{4}-\d{2}-\d{2}$/.test(v)) return v;
|
|
614
|
+
const d = new Date(v);
|
|
615
|
+
if (Number.isNaN(d.getTime())) return raw;
|
|
616
|
+
if (fmt === "date" && d.getTime() % 864e5 === 0) return d.toISOString().slice(0, 10);
|
|
617
|
+
return fmt === "date" ? localDay(d) : `${localDay(d)} ${pad2(d.getHours())}:${pad2(d.getMinutes())}`;
|
|
618
|
+
}
|
|
619
|
+
return raw;
|
|
620
|
+
}
|
|
621
|
+
|
|
219
622
|
//#endregion
|
|
220
623
|
//#region src/ask/render.tsx
|
|
221
624
|
const KIND = {
|
|
@@ -224,6 +627,7 @@ const KIND = {
|
|
|
224
627
|
recall: "substrate.recall",
|
|
225
628
|
clarify: "substrate.clarify",
|
|
226
629
|
refuse: "substrate.refuse",
|
|
630
|
+
unknown: "substrate.unknown",
|
|
227
631
|
error: "substrate.error"
|
|
228
632
|
};
|
|
229
633
|
const str = (v) => v === void 0 || v === null ? "" : sanitise(String(v));
|
|
@@ -232,9 +636,10 @@ const errorLines = (errors) => (errors ?? []).map((e) => {
|
|
|
232
636
|
const x = e;
|
|
233
637
|
return sanitise(`${x.code ?? "error"}${x.message !== void 0 ? `: ${x.message}` : ""}`);
|
|
234
638
|
});
|
|
639
|
+
const droppedLines = (dropped) => (dropped ?? []).map((d) => sanitise(`dropped "${d.span}": ${d.reason}`));
|
|
235
640
|
function outcomeChips(o) {
|
|
236
641
|
if (o.kind !== "rows") return [];
|
|
237
|
-
const rejected =
|
|
642
|
+
const rejected = o.data === void 0;
|
|
238
643
|
return [
|
|
239
644
|
{
|
|
240
645
|
label: `▸ ${frameKey(o)}`,
|
|
@@ -245,11 +650,76 @@ function outcomeChips(o) {
|
|
|
245
650
|
tone: o.source === "handoff" ? "warn" : "dim"
|
|
246
651
|
},
|
|
247
652
|
{
|
|
248
|
-
label: rejected ? "rejected" : `${o.data.length} rows`,
|
|
653
|
+
label: rejected ? "rejected" : o.aggregate !== void 0 ? "aggregate" : typeof o.count === "number" ? `count ${o.count}` : `${Array.isArray(o.data) ? o.data.length : 0} rows`,
|
|
249
654
|
tone: rejected ? "error" : "dim"
|
|
250
655
|
}
|
|
251
656
|
];
|
|
252
657
|
}
|
|
658
|
+
/** The first label-like field a row has: the usual handles, then any `…Name` / `…Title` string. */
|
|
659
|
+
const LABELS = [
|
|
660
|
+
"title",
|
|
661
|
+
"synopsis",
|
|
662
|
+
"name",
|
|
663
|
+
"topic",
|
|
664
|
+
"subject"
|
|
665
|
+
];
|
|
666
|
+
function labelField(r) {
|
|
667
|
+
const own = LABELS.find((k) => typeof r[k] === "string" && r[k] !== "");
|
|
668
|
+
if (own !== void 0) return own;
|
|
669
|
+
return Object.keys(r).find((k) => /(Name|Title)$/.test(k) && typeof r[k] === "string" && r[k] !== "");
|
|
670
|
+
}
|
|
671
|
+
/** Fields the question filtered or ordered on, in the order they appear in the frame. */
|
|
672
|
+
function askedFields(frame) {
|
|
673
|
+
const params = Object.values(frame)[0]?.params ?? {};
|
|
674
|
+
const out = [];
|
|
675
|
+
const add = (k) => {
|
|
676
|
+
if (!out.includes(k)) out.push(k);
|
|
677
|
+
};
|
|
678
|
+
const walk = (w) => {
|
|
679
|
+
if (w === null || typeof w !== "object") return;
|
|
680
|
+
for (const [k, v] of Object.entries(w)) if (k === "AND" || k === "OR" || k === "NOT") for (const c of Array.isArray(v) ? v : [v]) walk(c);
|
|
681
|
+
else if (!k.startsWith("_")) add(k);
|
|
682
|
+
};
|
|
683
|
+
for (const o of params.orderBy ?? []) for (const k of Object.keys(o)) add(k);
|
|
684
|
+
walk(params.where);
|
|
685
|
+
return out;
|
|
686
|
+
}
|
|
687
|
+
/** A row's walked values (`_agg.<name>`, such as a hop ordering's) as fields beside its own. */
|
|
688
|
+
const withWalked = (r) => {
|
|
689
|
+
const a = own$1(r, "_agg");
|
|
690
|
+
return a !== null && typeof a === "object" && !Array.isArray(a) ? {
|
|
691
|
+
...a,
|
|
692
|
+
...r
|
|
693
|
+
} : r;
|
|
694
|
+
};
|
|
695
|
+
/** `name value` for each walked value a row carries. */
|
|
696
|
+
const walkedDetails = (r) => {
|
|
697
|
+
const a = own$1(r, "_agg");
|
|
698
|
+
if (a === null || typeof a !== "object" || Array.isArray(a)) return [];
|
|
699
|
+
return Object.entries(a).filter(([, v]) => !empty(v)).map(([k, v]) => `${sanitise(k)} ${str(v)}`);
|
|
700
|
+
};
|
|
701
|
+
const empty = (v) => v === null || v === void 0 || v === "" || typeof v === "object";
|
|
702
|
+
/** A row's own field as the view formats it, sanitised; '' when empty. */
|
|
703
|
+
const shown = (r, field, view) => {
|
|
704
|
+
const v = own$1(r, field);
|
|
705
|
+
return empty(v) ? "" : view === void 0 ? str(v) : sanitise(formatValue(v, own$1(view.formats, field), view.currency));
|
|
706
|
+
};
|
|
707
|
+
/** An entry's cell: its template around the formatted value; '' when the value is empty. */
|
|
708
|
+
const cell$1 = (r, e, view) => {
|
|
709
|
+
const v = shown(r, e.field, view);
|
|
710
|
+
return v === "" || e.template === void 0 ? v : sanitise(e.template.replaceAll("{}", () => v));
|
|
711
|
+
};
|
|
712
|
+
/** Width of a right-aligned value column: the widest value, capped at a third of the line. */
|
|
713
|
+
const valueWidth = (values, columns) => Math.min(Math.max(0, ...values.map(cellWidth)), Math.max(8, Math.floor(columns / 3)));
|
|
714
|
+
const SKIP = /* @__PURE__ */ new Set(["id", "shortId"]);
|
|
715
|
+
/** `field=value` for a row's scalar fields: the asked-about ones first, then the rest. */
|
|
716
|
+
function detailList(r, asked, label, view) {
|
|
717
|
+
const scalar = (k) => {
|
|
718
|
+
const v = r[k];
|
|
719
|
+
return v !== null && v !== void 0 && v !== "" && typeof v !== "object";
|
|
720
|
+
};
|
|
721
|
+
return [...asked.filter((k) => k in r), ...Object.keys(r).filter((k) => !asked.includes(k))].filter((k) => !SKIP.has(k) && k !== label && scalar(k)).map((k) => `${sanitise(k)}=${shown(r, k, view)}`);
|
|
722
|
+
}
|
|
253
723
|
const COLS = {
|
|
254
724
|
id: 6,
|
|
255
725
|
status: 10,
|
|
@@ -258,9 +728,183 @@ const COLS = {
|
|
|
258
728
|
minTitle: 12
|
|
259
729
|
};
|
|
260
730
|
const titleWidth = (columns) => Math.max(COLS.minTitle, columns - (COLS.id + COLS.status + COLS.project + COLS.gaps));
|
|
731
|
+
const FN_LABEL = {
|
|
732
|
+
avg: "average",
|
|
733
|
+
sum: "total",
|
|
734
|
+
min: "min",
|
|
735
|
+
max: "max"
|
|
736
|
+
};
|
|
737
|
+
const aggValue = (v, view, field) => v === null || v === void 0 ? "—" : view !== void 0 && field !== void 0 && own$1(view.formats, field) !== void 0 ? sanitise(formatValue(v, own$1(view.formats, field), view.currency)) : typeof v === "number" && !Number.isInteger(v) ? String(Math.abs(v) >= .01 ? Math.round(v * 100) / 100 : Number(v.toPrecision(3))) : str(v);
|
|
738
|
+
/**
|
|
739
|
+
* A declared type's list. Wide: per row a dim ordinal, the bold title, dim tag chips and the value
|
|
740
|
+
* right-aligned in one column down the list, then an optional dim `·`-joined details line.
|
|
741
|
+
* Stacked, when the title would get less than max(20, 40%) of line 1: the title alone, then
|
|
742
|
+
* `tags · value`, then details. One layout for the whole list, so it stays aligned. Rows truncate.
|
|
743
|
+
*/
|
|
744
|
+
function declaredRows(list, row, view, theme, columns) {
|
|
745
|
+
const ordW = String(list.length).length;
|
|
746
|
+
const prefixW = 1 + ordW + 2;
|
|
747
|
+
const indent = " ".repeat(prefixW + 2);
|
|
748
|
+
const detailW = Math.max(1, columns - indent.length);
|
|
749
|
+
const values = list.map((r) => row.value === void 0 ? "" : cell$1(r, row.value, view));
|
|
750
|
+
const valW = row.value === void 0 ? 0 : valueWidth(values.map((v) => v || "—"), columns);
|
|
751
|
+
const leftW = Math.max(1, columns - prefixW - (valW > 0 ? valW + 2 : 0));
|
|
752
|
+
const items = list.map((r) => {
|
|
753
|
+
const f = row.title?.field ?? labelField(r);
|
|
754
|
+
return {
|
|
755
|
+
title: (f === void 0 ? "" : cell$1(r, row.title ?? { field: f }, view)) || "—",
|
|
756
|
+
tags: row.tags.map((e) => cell$1(r, e, view)).filter((t) => t !== ""),
|
|
757
|
+
det: declaredDetails(r, row, view)
|
|
758
|
+
};
|
|
759
|
+
});
|
|
760
|
+
const wide = leftW - Math.max(0, ...items.map((x) => x.tags.length > 0 ? cellWidth(` ${x.tags.join(" ")}`) : 0)) >= Math.max(20, Math.ceil(columns * .4));
|
|
761
|
+
const ordinal = (i) => /* @__PURE__ */ jsx(Text, {
|
|
762
|
+
color: theme.color.dim,
|
|
763
|
+
children: ` ${padCells(String(i + 1), ordW, true)} `
|
|
764
|
+
});
|
|
765
|
+
const details = (det) => det.length > 0 ? /* @__PURE__ */ jsx(Text, {
|
|
766
|
+
wrap: "truncate",
|
|
767
|
+
color: theme.color.dim,
|
|
768
|
+
children: indent + truncateCells(det.join(" · "), detailW)
|
|
769
|
+
}) : null;
|
|
770
|
+
return items.map(({ title, tags, det }, i) => {
|
|
771
|
+
const value = values[i] ?? "";
|
|
772
|
+
if (!wide) {
|
|
773
|
+
const second = [...tags, value].filter((x) => x !== "");
|
|
774
|
+
return /* @__PURE__ */ jsxs(Box, {
|
|
775
|
+
flexDirection: "column",
|
|
776
|
+
children: [
|
|
777
|
+
/* @__PURE__ */ jsxs(Text, {
|
|
778
|
+
wrap: "truncate",
|
|
779
|
+
children: [ordinal(i), /* @__PURE__ */ jsx(Text, {
|
|
780
|
+
bold: true,
|
|
781
|
+
color: theme.color.text,
|
|
782
|
+
children: truncateCells(title, Math.max(1, columns - prefixW))
|
|
783
|
+
})]
|
|
784
|
+
}),
|
|
785
|
+
second.length > 0 ? /* @__PURE__ */ jsx(Text, {
|
|
786
|
+
wrap: "truncate",
|
|
787
|
+
color: theme.color.dim,
|
|
788
|
+
children: indent + truncateCells(second.join(" · "), detailW)
|
|
789
|
+
}) : null,
|
|
790
|
+
details(det)
|
|
791
|
+
]
|
|
792
|
+
}, i);
|
|
793
|
+
}
|
|
794
|
+
const titleCell = truncateCells(title, leftW);
|
|
795
|
+
const room = leftW - cellWidth(titleCell);
|
|
796
|
+
const rest = tags.length > 0 && room > 0 ? ` ${tags.join(" ")}` : "";
|
|
797
|
+
const pad = valW > 0;
|
|
798
|
+
const restCell = pad ? padCells(rest, room) : truncateCells(rest, room);
|
|
799
|
+
return /* @__PURE__ */ jsxs(Box, {
|
|
800
|
+
flexDirection: "column",
|
|
801
|
+
children: [/* @__PURE__ */ jsxs(Text, {
|
|
802
|
+
wrap: "truncate",
|
|
803
|
+
children: [
|
|
804
|
+
ordinal(i),
|
|
805
|
+
/* @__PURE__ */ jsx(Text, {
|
|
806
|
+
bold: true,
|
|
807
|
+
color: theme.color.text,
|
|
808
|
+
children: titleCell
|
|
809
|
+
}),
|
|
810
|
+
/* @__PURE__ */ jsx(Text, {
|
|
811
|
+
color: theme.color.dim,
|
|
812
|
+
children: restCell
|
|
813
|
+
}),
|
|
814
|
+
pad ? /* @__PURE__ */ jsx(Text, {
|
|
815
|
+
color: theme.color.text,
|
|
816
|
+
children: ` ${padCells(truncateCells(value || "—", valW), valW, true)}`
|
|
817
|
+
}) : null
|
|
818
|
+
]
|
|
819
|
+
}), details(det)]
|
|
820
|
+
}, i);
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
/** Below this width a grouped aggregate puts each value on the line under its key. */
|
|
824
|
+
const STACK_GROUPS_AT = 40;
|
|
261
825
|
const rows = ({ data, theme, size }) => {
|
|
262
826
|
const titleCols = titleWidth(size.columns);
|
|
263
827
|
const o = data;
|
|
828
|
+
const view = o.tui;
|
|
829
|
+
const lost = droppedLines(o.dropped).map((l, i) => /* @__PURE__ */ jsx(Text, {
|
|
830
|
+
color: theme.color.warn,
|
|
831
|
+
children: ` ${l}`
|
|
832
|
+
}, `d${i}`));
|
|
833
|
+
const agg = o.aggregate;
|
|
834
|
+
if (agg !== void 0) {
|
|
835
|
+
const head = `${Object.hasOwn(FN_LABEL, agg.fn) ? FN_LABEL[agg.fn] : str(agg.fn)} ${str(agg.field)}`;
|
|
836
|
+
const errs = errorLines(o.errors).map((l, i) => /* @__PURE__ */ jsx(Text, {
|
|
837
|
+
color: theme.color.error,
|
|
838
|
+
children: ` ${l}`
|
|
839
|
+
}, `e${i}`));
|
|
840
|
+
if (agg.groups === void 0) return /* @__PURE__ */ jsxs(Box, {
|
|
841
|
+
flexDirection: "column",
|
|
842
|
+
children: [
|
|
843
|
+
/* @__PURE__ */ jsx(Text, {
|
|
844
|
+
color: theme.color.text,
|
|
845
|
+
children: `${head} = ${aggValue(agg.value, view, agg.field)}`
|
|
846
|
+
}),
|
|
847
|
+
errs,
|
|
848
|
+
lost
|
|
849
|
+
]
|
|
850
|
+
});
|
|
851
|
+
const keyCols = Math.min(30, Math.max(COLS.minTitle, Math.floor(size.columns / 3)));
|
|
852
|
+
const groups = agg.groups.filter((g) => g.key === null || g.value !== null);
|
|
853
|
+
const values = groups.map((g) => aggValue(g.value, view, agg.field));
|
|
854
|
+
const valW = valueWidth(values, size.columns);
|
|
855
|
+
return /* @__PURE__ */ jsxs(Box, {
|
|
856
|
+
flexDirection: "column",
|
|
857
|
+
children: [
|
|
858
|
+
/* @__PURE__ */ jsx(Text, {
|
|
859
|
+
color: theme.color.dim,
|
|
860
|
+
children: `${head} by ${str(agg.by)}`
|
|
861
|
+
}),
|
|
862
|
+
groups.length === 0 ? /* @__PURE__ */ jsx(Text, {
|
|
863
|
+
color: theme.color.dim,
|
|
864
|
+
children: " no groups"
|
|
865
|
+
}) : null,
|
|
866
|
+
groups.map((g, i) => size.columns <= STACK_GROUPS_AT ? /* @__PURE__ */ jsxs(Box, {
|
|
867
|
+
flexDirection: "column",
|
|
868
|
+
children: [/* @__PURE__ */ jsx(Text, {
|
|
869
|
+
wrap: "truncate",
|
|
870
|
+
color: theme.color.dimMid,
|
|
871
|
+
children: ` ${truncateCells(g.key === null ? "(none)" : str(g.key), Math.max(1, size.columns - 2))}`
|
|
872
|
+
}), /* @__PURE__ */ jsx(Text, {
|
|
873
|
+
wrap: "truncate",
|
|
874
|
+
color: theme.color.text,
|
|
875
|
+
children: ` ${truncateCells(values[i] ?? "", Math.max(1, size.columns - 4))}`
|
|
876
|
+
})]
|
|
877
|
+
}, i) : /* @__PURE__ */ jsxs(Text, {
|
|
878
|
+
wrap: "truncate",
|
|
879
|
+
children: [
|
|
880
|
+
" ",
|
|
881
|
+
/* @__PURE__ */ jsx(Text, {
|
|
882
|
+
color: theme.color.dimMid,
|
|
883
|
+
children: padCells(g.key === null ? "(none)" : str(g.key), keyCols)
|
|
884
|
+
}),
|
|
885
|
+
" ",
|
|
886
|
+
/* @__PURE__ */ jsx(Text, {
|
|
887
|
+
color: theme.color.text,
|
|
888
|
+
children: padCells(truncateCells(values[i] ?? "", valW), valW, true)
|
|
889
|
+
})
|
|
890
|
+
]
|
|
891
|
+
}, i)),
|
|
892
|
+
agg.truncated === true ? /* @__PURE__ */ jsx(Text, {
|
|
893
|
+
color: theme.color.dim,
|
|
894
|
+
children: " …more groups"
|
|
895
|
+
}) : null,
|
|
896
|
+
errs,
|
|
897
|
+
lost
|
|
898
|
+
]
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
if (!Array.isArray(o.data) && typeof o.count === "number") return /* @__PURE__ */ jsxs(Box, {
|
|
902
|
+
flexDirection: "column",
|
|
903
|
+
children: [/* @__PURE__ */ jsx(Text, {
|
|
904
|
+
color: theme.color.text,
|
|
905
|
+
children: o.count
|
|
906
|
+
}), lost]
|
|
907
|
+
});
|
|
264
908
|
if (!Array.isArray(o.data)) return /* @__PURE__ */ jsxs(Box, {
|
|
265
909
|
flexDirection: "column",
|
|
266
910
|
children: [/* @__PURE__ */ jsxs(Text, {
|
|
@@ -272,33 +916,85 @@ const rows = ({ data, theme, size }) => {
|
|
|
272
916
|
}, i))]
|
|
273
917
|
});
|
|
274
918
|
const list = o.data;
|
|
275
|
-
if (list.length === 0) return /* @__PURE__ */ jsx(Text, {
|
|
919
|
+
if (list.length === 0 && lost.length === 0) return /* @__PURE__ */ jsx(Text, {
|
|
276
920
|
color: theme.color.dim,
|
|
277
921
|
children: "no rows"
|
|
278
922
|
});
|
|
279
|
-
return /* @__PURE__ */
|
|
923
|
+
if (view?.row !== void 0) return /* @__PURE__ */ jsxs(Box, {
|
|
924
|
+
flexDirection: "column",
|
|
925
|
+
children: [declaredRows(list, view.row, view, theme, size.columns), lost]
|
|
926
|
+
});
|
|
927
|
+
if (!list.some((r) => "status" in r || "project" in r)) {
|
|
928
|
+
const asked = askedFields(o.frame);
|
|
929
|
+
const labelCols = Math.min(40, Math.max(COLS.minTitle, Math.floor(size.columns / 3)));
|
|
930
|
+
const detailCols = Math.max(COLS.minTitle, size.columns - (COLS.id + labelCols + 4));
|
|
931
|
+
return /* @__PURE__ */ jsxs(Box, {
|
|
932
|
+
flexDirection: "column",
|
|
933
|
+
children: [list.map((row, i) => {
|
|
934
|
+
const r = withWalked(row);
|
|
935
|
+
const label = labelField(r);
|
|
936
|
+
return /* @__PURE__ */ jsxs(Text, { children: [
|
|
937
|
+
/* @__PURE__ */ jsx(Text, {
|
|
938
|
+
color: theme.color.dim,
|
|
939
|
+
children: padCells(str(r.shortId ?? r.id), COLS.id, true)
|
|
940
|
+
}),
|
|
941
|
+
" ",
|
|
942
|
+
/* @__PURE__ */ jsx(Text, {
|
|
943
|
+
color: theme.color.text,
|
|
944
|
+
children: padCells(label !== void 0 ? shown(r, label, view) : "", labelCols)
|
|
945
|
+
}),
|
|
946
|
+
" ",
|
|
947
|
+
/* @__PURE__ */ jsx(Text, {
|
|
948
|
+
color: theme.color.dimMid,
|
|
949
|
+
children: padCells(detailList(r, asked, label, view).join(" "), detailCols)
|
|
950
|
+
})
|
|
951
|
+
] }, i);
|
|
952
|
+
}), lost]
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
return /* @__PURE__ */ jsxs(Box, {
|
|
280
956
|
flexDirection: "column",
|
|
281
|
-
children:
|
|
282
|
-
/* @__PURE__ */ jsx(Text, {
|
|
957
|
+
children: [
|
|
958
|
+
list.length === 0 ? /* @__PURE__ */ jsx(Text, {
|
|
283
959
|
color: theme.color.dim,
|
|
284
|
-
children:
|
|
285
|
-
}),
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
960
|
+
children: "no rows"
|
|
961
|
+
}) : null,
|
|
962
|
+
list.map((r, i) => /* @__PURE__ */ jsxs(Text, { children: [
|
|
963
|
+
/* @__PURE__ */ jsx(Text, {
|
|
964
|
+
color: theme.color.dim,
|
|
965
|
+
children: padCells(str(r.shortId ?? r.id), COLS.id, true)
|
|
966
|
+
}),
|
|
967
|
+
" ",
|
|
968
|
+
/* @__PURE__ */ jsx(Text, {
|
|
969
|
+
color: theme.color.text,
|
|
970
|
+
children: padCells(str(r.title ?? r.synopsis ?? r.name ?? r.topic ?? r.subject), titleCols)
|
|
971
|
+
}),
|
|
972
|
+
" ",
|
|
973
|
+
/* @__PURE__ */ jsx(Text, {
|
|
974
|
+
color: theme.color.info,
|
|
975
|
+
children: padCells(str(r.status), COLS.status)
|
|
976
|
+
}),
|
|
977
|
+
" ",
|
|
978
|
+
/* @__PURE__ */ jsx(Text, {
|
|
979
|
+
color: theme.color.dimMid,
|
|
980
|
+
children: padCells(str(r.project), COLS.project)
|
|
981
|
+
})
|
|
982
|
+
] }, i)),
|
|
983
|
+
lost
|
|
984
|
+
]
|
|
985
|
+
});
|
|
986
|
+
};
|
|
987
|
+
const unknown = ({ data, theme }) => {
|
|
988
|
+
const o = data;
|
|
989
|
+
return /* @__PURE__ */ jsxs(Box, {
|
|
990
|
+
flexDirection: "column",
|
|
991
|
+
children: [/* @__PURE__ */ jsx(Text, {
|
|
992
|
+
color: theme.color.warn,
|
|
993
|
+
children: "? unknown: not answered, because part of it was dropped"
|
|
994
|
+
}), droppedLines(o.dropped).map((l, i) => /* @__PURE__ */ jsx(Text, {
|
|
995
|
+
color: theme.color.warn,
|
|
996
|
+
children: ` ${l}`
|
|
997
|
+
}, i))]
|
|
302
998
|
});
|
|
303
999
|
};
|
|
304
1000
|
const recall = ({ data, theme }) => {
|
|
@@ -386,8 +1082,148 @@ const renderers = {
|
|
|
386
1082
|
[KIND.recall]: recall,
|
|
387
1083
|
[KIND.clarify]: clarify,
|
|
388
1084
|
[KIND.refuse]: refuse,
|
|
1085
|
+
[KIND.unknown]: unknown,
|
|
389
1086
|
[KIND.error]: error
|
|
390
1087
|
};
|
|
1088
|
+
/** A declared row's details: its walked values, then each declared detail, labelled when numeric. */
|
|
1089
|
+
function declaredDetails(r, row, view) {
|
|
1090
|
+
return [...walkedDetails(r), ...row.details.map((e) => {
|
|
1091
|
+
const v = cell$1(r, e, view);
|
|
1092
|
+
if (v === "") return "";
|
|
1093
|
+
const label = e.label ?? (typeof own$1(r, e.field) === "number" || isNumericFormat(own$1(view.formats, e.field)) ? e.field : void 0);
|
|
1094
|
+
return label === void 0 ? v : `${sanitise(label)} ${v}`;
|
|
1095
|
+
})].filter((d) => d !== "");
|
|
1096
|
+
}
|
|
1097
|
+
/**
|
|
1098
|
+
* A rows answer as a list for the pager, or undefined when it is not a plain list of rows (an
|
|
1099
|
+
* aggregate, a count, a rejected frame). Declared rows keep their title, tags, details and value;
|
|
1100
|
+
* the rest get a label and the asked-about fields, then the others, as `field=value`.
|
|
1101
|
+
*/
|
|
1102
|
+
function rowsList(o) {
|
|
1103
|
+
if (!Array.isArray(o.data) || o.aggregate !== void 0 || o.count !== void 0) return void 0;
|
|
1104
|
+
const view = o.tui;
|
|
1105
|
+
const asked = askedFields(o.frame);
|
|
1106
|
+
const rows = o.data.map((raw, i) => {
|
|
1107
|
+
const r = withWalked(raw);
|
|
1108
|
+
const key = typeof raw.id === "string" ? raw.id : String(i);
|
|
1109
|
+
const decl = view?.row;
|
|
1110
|
+
if (view !== void 0 && decl !== void 0) {
|
|
1111
|
+
const f = decl.title?.field ?? labelField(r);
|
|
1112
|
+
const value = decl.value === void 0 ? "" : cell$1(r, decl.value, view);
|
|
1113
|
+
return {
|
|
1114
|
+
key,
|
|
1115
|
+
name: (f === void 0 ? "" : cell$1(r, decl.title ?? { field: f }, view)) || "—",
|
|
1116
|
+
details: [...decl.tags.map((e) => cell$1(r, e, view)).filter((t) => t !== ""), ...declaredDetails(r, decl, view)],
|
|
1117
|
+
...value !== "" ? { value } : {}
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
const label = labelField(r);
|
|
1121
|
+
return {
|
|
1122
|
+
key,
|
|
1123
|
+
name: (label !== void 0 ? shown(r, label, view) : "") || str(r.shortId ?? r.id) || "—",
|
|
1124
|
+
details: detailList(r, asked, label, view)
|
|
1125
|
+
};
|
|
1126
|
+
});
|
|
1127
|
+
const notes = [...errorLines(o.errors), ...droppedLines(o.dropped)];
|
|
1128
|
+
return {
|
|
1129
|
+
heading: frameKey(o),
|
|
1130
|
+
rows,
|
|
1131
|
+
...notes.length > 0 ? { notes } : {}
|
|
1132
|
+
};
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
//#endregion
|
|
1136
|
+
//#region src/ask/list.ts
|
|
1137
|
+
/** Rows `r` lists: recall's hits about the same idea, the row itself left out. */
|
|
1138
|
+
const RELATED_K = 20;
|
|
1139
|
+
const META = [
|
|
1140
|
+
"status",
|
|
1141
|
+
"area",
|
|
1142
|
+
"project"
|
|
1143
|
+
];
|
|
1144
|
+
const text = (v) => typeof v === "string" ? v : "";
|
|
1145
|
+
const scalar = (v) => v !== null && v !== void 0 && v !== "" && typeof v !== "object";
|
|
1146
|
+
const nameOf = (row) => typeof row.name === "string" ? row.name : row.name.map((s) => s.text).join("");
|
|
1147
|
+
/** A frame's rows; a refused frame throws the server's reason rather than reading as empty. */
|
|
1148
|
+
async function rowsOf(deps, key, frame, signal) {
|
|
1149
|
+
const env = await deps.query({ [key]: frame }, signal);
|
|
1150
|
+
if (!Object.hasOwn(env.data, key)) throw new Error(env.errors?.[0]?.message ?? `${key} was refused`);
|
|
1151
|
+
const v = env.data[key];
|
|
1152
|
+
return (Array.isArray(v) ? v : [v]).filter((x) => x !== null && typeof x === "object");
|
|
1153
|
+
}
|
|
1154
|
+
/**
|
|
1155
|
+
* Enter and `r` on an NL answer's rows. `open` reads the row's record whole (every scalar field
|
|
1156
|
+
* the schema has, else the fields the answer carried); `related` asks recall, over the same type
|
|
1157
|
+
* and namespace, for rows about the same idea as the record's label and body.
|
|
1158
|
+
*/
|
|
1159
|
+
function listHooks(deps, ns, type, fields, answered, signal) {
|
|
1160
|
+
const records = /* @__PURE__ */ new Map();
|
|
1161
|
+
const record = (row) => {
|
|
1162
|
+
const known = records.get(row.key);
|
|
1163
|
+
if (known !== void 0) return known;
|
|
1164
|
+
const seen = answered.get(row.key) ?? {};
|
|
1165
|
+
if (typeof seen.id !== "string") return Promise.resolve(seen);
|
|
1166
|
+
const names = fields !== void 0 ? Object.entries(fields).filter(([, f]) => f.kind === "scalar" && f.type !== "json").map(([k]) => k) : Object.keys(seen).filter((k) => scalar(seen[k]));
|
|
1167
|
+
const read = rowsOf(deps, `${ns}/${type}`, {
|
|
1168
|
+
params: {
|
|
1169
|
+
where: { id: { eq: row.key } },
|
|
1170
|
+
limit: 1
|
|
1171
|
+
},
|
|
1172
|
+
fields: Object.fromEntries(["id", ...names].map((k) => [k, true]))
|
|
1173
|
+
}, signal).then((rows) => {
|
|
1174
|
+
const r = rows[0];
|
|
1175
|
+
if (r === void 0) throw new Error(`${type} ${row.key} is gone`);
|
|
1176
|
+
return r;
|
|
1177
|
+
});
|
|
1178
|
+
read.catch(() => records.delete(row.key));
|
|
1179
|
+
records.set(row.key, read);
|
|
1180
|
+
return read;
|
|
1181
|
+
};
|
|
1182
|
+
return {
|
|
1183
|
+
async open(row) {
|
|
1184
|
+
const r = await record(row);
|
|
1185
|
+
const label = labelField(r);
|
|
1186
|
+
const meta = META.map((k) => text(r[k])).filter((v) => v !== "");
|
|
1187
|
+
const prose = [text(r.body), text(r.notes)].filter((v) => v !== "");
|
|
1188
|
+
const body = prose.length > 0 ? prose.join("\n\n") : Object.keys(r).filter((k) => k !== "id" && k !== label && !META.some((m) => m === k) && scalar(r[k])).map((k) => `${k}: ${String(r[k])}`).join("\n");
|
|
1189
|
+
return {
|
|
1190
|
+
title: label !== void 0 ? text(r[label]) : nameOf(row),
|
|
1191
|
+
...meta.length > 0 ? { meta: meta.join(" · ") } : {},
|
|
1192
|
+
...body !== "" ? { body } : {}
|
|
1193
|
+
};
|
|
1194
|
+
},
|
|
1195
|
+
async related(row) {
|
|
1196
|
+
const r = await record(row);
|
|
1197
|
+
const label = labelField(r);
|
|
1198
|
+
const title = label !== void 0 ? text(r[label]) : nameOf(row);
|
|
1199
|
+
const rows = (await rowsOf(deps, "recall/Hit", {
|
|
1200
|
+
params: {
|
|
1201
|
+
to: [title, text(r.body)].filter((v) => v !== "").join("\n") || nameOf(row),
|
|
1202
|
+
namespace: ns,
|
|
1203
|
+
type,
|
|
1204
|
+
k: 21
|
|
1205
|
+
},
|
|
1206
|
+
fields: {
|
|
1207
|
+
id: true,
|
|
1208
|
+
score: true,
|
|
1209
|
+
row: true
|
|
1210
|
+
}
|
|
1211
|
+
}, signal)).filter((h) => h.id !== row.key).slice(0, 20).map((h) => {
|
|
1212
|
+
const hr = h.row !== null && typeof h.row === "object" ? h.row : {};
|
|
1213
|
+
const l = labelField(hr);
|
|
1214
|
+
return {
|
|
1215
|
+
key: String(h.id),
|
|
1216
|
+
name: (l !== void 0 ? text(hr[l]) : "") || String(h.id),
|
|
1217
|
+
...typeof h.score === "number" ? { value: h.score.toFixed(2) } : {}
|
|
1218
|
+
};
|
|
1219
|
+
});
|
|
1220
|
+
return {
|
|
1221
|
+
heading: `related to ${title}`,
|
|
1222
|
+
rows
|
|
1223
|
+
};
|
|
1224
|
+
}
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
391
1227
|
|
|
392
1228
|
//#endregion
|
|
393
1229
|
//#region src/ask/module.ts
|
|
@@ -409,15 +1245,48 @@ function explain(err, endpoint) {
|
|
|
409
1245
|
if (e.status === 401 || e.status === 403) return `not authorised (${e.status}) — check SUBSTRATE_AGENT_KEY_ID / SUBSTRATE_AGENT_KEY_PATH`;
|
|
410
1246
|
return e.message ?? String(err);
|
|
411
1247
|
}
|
|
412
|
-
|
|
1248
|
+
/** What the server accepts for `asOf`: a date-time with an explicit offset. */
|
|
1249
|
+
const ISO_WITH_OFFSET = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})$/;
|
|
1250
|
+
/**
|
|
1251
|
+
* The ask module. A plain list of rows goes to `pager`, whose module the app must also register;
|
|
1252
|
+
* the default is for an app that registers it from here, as the tests do.
|
|
1253
|
+
*/
|
|
1254
|
+
function askModule(deps, pager = listPager()) {
|
|
413
1255
|
let namespace = deps.namespace;
|
|
414
1256
|
let pack = null;
|
|
415
1257
|
let forceType;
|
|
1258
|
+
/** The clock relative phrases are read against; undefined = the server's. */
|
|
1259
|
+
let asOf;
|
|
416
1260
|
let last = { lines: ["no question asked yet"] };
|
|
417
1261
|
let lastPackNote = "";
|
|
1262
|
+
/** The namespace's `annotations.tui`; empty until read, and when it cannot be. */
|
|
1263
|
+
let present = {
|
|
1264
|
+
types: {},
|
|
1265
|
+
problems: []
|
|
1266
|
+
};
|
|
1267
|
+
/** The namespace's types, for reading a listed row's record whole; empty until read. */
|
|
1268
|
+
let types = {};
|
|
418
1269
|
const loadPack = (ctx) => {
|
|
419
1270
|
pack = null;
|
|
420
1271
|
const ns = namespace;
|
|
1272
|
+
present = {
|
|
1273
|
+
types: {},
|
|
1274
|
+
problems: []
|
|
1275
|
+
};
|
|
1276
|
+
types = {};
|
|
1277
|
+
deps.getJson(`/substrate/v1/schema/${encodeURIComponent(ns)}`, ctx.signal).then((s) => {
|
|
1278
|
+
if (ns !== namespace) return;
|
|
1279
|
+
try {
|
|
1280
|
+
const t = s?.types;
|
|
1281
|
+
if (t !== null && typeof t === "object") types = t;
|
|
1282
|
+
present = parseTui(s?.annotations?.tui);
|
|
1283
|
+
} catch {
|
|
1284
|
+
present = {
|
|
1285
|
+
types: {},
|
|
1286
|
+
problems: [{ text: "tui ignored: unreadable" }]
|
|
1287
|
+
};
|
|
1288
|
+
}
|
|
1289
|
+
}, () => {});
|
|
421
1290
|
deps.nl.pack(ns, { signal: ctx.signal }).then((p) => {
|
|
422
1291
|
if (ns === namespace) pack = p;
|
|
423
1292
|
}, (err) => {
|
|
@@ -428,6 +1297,24 @@ function askModule(deps) {
|
|
|
428
1297
|
ctx.print(note, "dim");
|
|
429
1298
|
});
|
|
430
1299
|
};
|
|
1300
|
+
/** A rows outcome with its frame type's declared view, when the namespace declares one. */
|
|
1301
|
+
const withView = (o) => {
|
|
1302
|
+
const [ns, type] = (Object.keys(o.frame)[0] ?? "").split("/");
|
|
1303
|
+
const view = ns === namespace && type !== void 0 ? own$1(present.types, type) : void 0;
|
|
1304
|
+
return view !== void 0 ? {
|
|
1305
|
+
...o,
|
|
1306
|
+
tui: view
|
|
1307
|
+
} : o;
|
|
1308
|
+
};
|
|
1309
|
+
/** A rows answer as a list the pager pages, with Enter and `r` wired to the row's type. */
|
|
1310
|
+
const asList = (o, ctx) => {
|
|
1311
|
+
const list = rowsList(o);
|
|
1312
|
+
if (list === void 0) return void 0;
|
|
1313
|
+
const [ns = namespace, type = ""] = (Object.keys(o.frame)[0] ?? "").split("/");
|
|
1314
|
+
const answered = new Map(o.data.map((r, i) => [typeof r.id === "string" ? r.id : String(i), r]));
|
|
1315
|
+
const fields = ns === namespace ? own$1(types, type)?.fields : void 0;
|
|
1316
|
+
return pager.present(list, listHooks(deps, ns, type, fields, answered, ctx.signal));
|
|
1317
|
+
};
|
|
431
1318
|
const pick = (i) => (s, ctx) => {
|
|
432
1319
|
const opt = s.options[i];
|
|
433
1320
|
if (opt === void 0) return void 0;
|
|
@@ -440,23 +1327,41 @@ function askModule(deps) {
|
|
|
440
1327
|
loadPack(ctx);
|
|
441
1328
|
ctx.emit("substrate/ns", { namespace });
|
|
442
1329
|
},
|
|
443
|
-
commands: [
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
1330
|
+
commands: [
|
|
1331
|
+
{
|
|
1332
|
+
name: "ns",
|
|
1333
|
+
summary: "switch the namespace questions go to",
|
|
1334
|
+
args: [{ name: "namespace" }],
|
|
1335
|
+
run([ns], ctx) {
|
|
1336
|
+
if (ns === void 0 || ns === "") return ctx.print(`namespace: ${namespace}`);
|
|
1337
|
+
namespace = ns;
|
|
1338
|
+
loadPack(ctx);
|
|
1339
|
+
ctx.emit("substrate/ns", { namespace });
|
|
1340
|
+
ctx.print(`namespace → ${namespace}`);
|
|
1341
|
+
}
|
|
1342
|
+
},
|
|
1343
|
+
{
|
|
1344
|
+
name: "asof",
|
|
1345
|
+
summary: "read \"yesterday\", \"past 2 days\" … as of a date-time (ISO-8601 with an offset); off for the server clock",
|
|
1346
|
+
args: [{ name: "datetime|off" }],
|
|
1347
|
+
run([v], ctx) {
|
|
1348
|
+
if (v === void 0 || v === "") return ctx.print(`asOf: ${asOf ?? "server clock"}`);
|
|
1349
|
+
if (v === "off") {
|
|
1350
|
+
asOf = void 0;
|
|
1351
|
+
return ctx.print("asOf → server clock");
|
|
1352
|
+
}
|
|
1353
|
+
if (!ISO_WITH_OFFSET.test(v) || Number.isNaN(Date.parse(v))) return ctx.print("asOf needs an ISO-8601 date-time with an offset, e.g. 2026-10-24T23:59:59-05:00");
|
|
1354
|
+
asOf = v;
|
|
1355
|
+
ctx.print(`asOf → ${asOf}`);
|
|
1356
|
+
}
|
|
1357
|
+
},
|
|
1358
|
+
{
|
|
1359
|
+
name: "frame",
|
|
1360
|
+
summary: "show the last answer's query frame, evidence and judge trail",
|
|
1361
|
+
args: [],
|
|
1362
|
+
run: (_a, ctx) => ctx.open("frame", last)
|
|
453
1363
|
}
|
|
454
|
-
|
|
455
|
-
name: "frame",
|
|
456
|
-
summary: "show the last answer's query frame, evidence and judge trail",
|
|
457
|
-
args: [],
|
|
458
|
-
run: (_a, ctx) => ctx.open("frame", last)
|
|
459
|
-
}],
|
|
1364
|
+
],
|
|
460
1365
|
panels: {
|
|
461
1366
|
clarify: {
|
|
462
1367
|
view: ({ state }) => [
|
|
@@ -491,11 +1396,13 @@ function askModule(deps) {
|
|
|
491
1396
|
namespace,
|
|
492
1397
|
question: text,
|
|
493
1398
|
project: deps.project,
|
|
1399
|
+
...asOf !== void 0 ? { asOf } : {},
|
|
494
1400
|
...type !== void 0 ? { type } : {}
|
|
495
1401
|
}, { signal })) switch (ev.event) {
|
|
496
1402
|
case "parsed": {
|
|
497
1403
|
const key = ev.data.frame !== void 0 ? Object.keys(ev.data.frame)[0] : void 0;
|
|
498
1404
|
lines.push(`parse: ${ev.data.status}${ev.data.reason !== void 0 ? ` — ${ev.data.reason}` : ""}`);
|
|
1405
|
+
if (ev.data.asOf !== void 0) lines.push(`asOf: ${ev.data.asOf}`);
|
|
499
1406
|
if (ev.data.frame !== void 0) lines.push(JSON.stringify(ev.data.frame, null, 2));
|
|
500
1407
|
for (const e of ev.data.evidence) lines.push(` ${e.span} → ${e.role} (${e.detail})`);
|
|
501
1408
|
yield {
|
|
@@ -525,18 +1432,28 @@ function askModule(deps) {
|
|
|
525
1432
|
case "recall":
|
|
526
1433
|
case "clarify":
|
|
527
1434
|
case "refuse":
|
|
1435
|
+
case "unknown": {
|
|
1436
|
+
const answered = ev.event === "rows" ? Object.keys(ev.data.frame)[0]?.split("/")[1] : void 0;
|
|
1437
|
+
for (const p of present.problems) if (p.type === void 0 || p.type === answered) lines.push(`tui: ${p.text}`);
|
|
528
1438
|
last = { lines };
|
|
529
1439
|
if (ev.event === "clarify" && ev.data.options.length > 0) ctx.open("clarify", ev.data);
|
|
530
1440
|
else ctx.close();
|
|
1441
|
+
const data = ev.event === "rows" ? withView(ev.data) : ev.data;
|
|
1442
|
+
const listed = ev.event === "rows" ? asList(data, ctx) : void 0;
|
|
1443
|
+
const chips = outcomeChips(ev.data);
|
|
531
1444
|
yield {
|
|
532
1445
|
phase: "final",
|
|
533
|
-
reply: {
|
|
1446
|
+
reply: listed !== void 0 ? {
|
|
1447
|
+
...listed,
|
|
1448
|
+
chips
|
|
1449
|
+
} : {
|
|
534
1450
|
kind: `substrate.${ev.event}`,
|
|
535
|
-
data
|
|
536
|
-
chips
|
|
1451
|
+
data,
|
|
1452
|
+
chips
|
|
537
1453
|
}
|
|
538
1454
|
};
|
|
539
1455
|
return;
|
|
1456
|
+
}
|
|
540
1457
|
case "done": return;
|
|
541
1458
|
}
|
|
542
1459
|
} catch (err) {
|
|
@@ -1291,7 +2208,7 @@ function doctorModule(deps, io = {}) {
|
|
|
1291
2208
|
const deadline = new AbortController();
|
|
1292
2209
|
const timer = setTimeout(() => deadline.abort(), deadlineMs);
|
|
1293
2210
|
const signal = AbortSignal.any([ctx.signal, deadline.signal]);
|
|
1294
|
-
const fetchWith = ((url, init) =>
|
|
2211
|
+
const fetchWith = ((url, init) => substrateFetch(String(url), {
|
|
1295
2212
|
...init,
|
|
1296
2213
|
signal
|
|
1297
2214
|
}));
|
|
@@ -1471,34 +2388,6 @@ function doctorModule(deps, io = {}) {
|
|
|
1471
2388
|
});
|
|
1472
2389
|
}
|
|
1473
2390
|
|
|
1474
|
-
//#endregion
|
|
1475
|
-
//#region src/admin/refusal.ts
|
|
1476
|
-
/** A server refusal as the server said it: `code: message`, sanitised, never retried. A chunked backfill wraps its failure; the cause is the refusal. */
|
|
1477
|
-
function refusalOf(err) {
|
|
1478
|
-
const e = err instanceof AdminBackfillError ? err.cause : err;
|
|
1479
|
-
const known = e instanceof NetworkError || e instanceof AuthError;
|
|
1480
|
-
const code = known ? e.code ?? String(e.status ?? "error") : "error";
|
|
1481
|
-
const message = (known ? e.serverMessage : void 0) ?? (e instanceof Error ? e.message : String(e));
|
|
1482
|
-
return {
|
|
1483
|
-
code: sanitise(code),
|
|
1484
|
-
message: sanitise(message),
|
|
1485
|
-
...known && e.body !== void 0 ? { body: e.body } : {}
|
|
1486
|
-
};
|
|
1487
|
-
}
|
|
1488
|
-
|
|
1489
|
-
//#endregion
|
|
1490
|
-
//#region src/lines.tsx
|
|
1491
|
-
/** Lines as a column of `Text`, each in its tone: a joined string view has no tone to give them. */
|
|
1492
|
-
function linesView(lines, theme) {
|
|
1493
|
-
return /* @__PURE__ */ jsx(Box, {
|
|
1494
|
-
flexDirection: "column",
|
|
1495
|
-
children: lines.map((l, i) => /* @__PURE__ */ jsx(Text, {
|
|
1496
|
-
...l.tone !== void 0 ? { color: theme.color[l.tone] } : {},
|
|
1497
|
-
children: l.text
|
|
1498
|
-
}, i))
|
|
1499
|
-
});
|
|
1500
|
-
}
|
|
1501
|
-
|
|
1502
2391
|
//#endregion
|
|
1503
2392
|
//#region src/keys/grant.ts
|
|
1504
2393
|
const SHORT = {
|
|
@@ -2073,6 +2962,14 @@ function editView(s, glyph, size) {
|
|
|
2073
2962
|
fit(" ↑↓←→ move · space toggle · + add namespace · enter review · esc discard")
|
|
2074
2963
|
];
|
|
2075
2964
|
}
|
|
2965
|
+
/** Every keys panel: a one-column inset on a raised ground, set apart from the input band. */
|
|
2966
|
+
const KEYS_CHROME = {
|
|
2967
|
+
inset: {
|
|
2968
|
+
x: 1,
|
|
2969
|
+
y: 0
|
|
2970
|
+
},
|
|
2971
|
+
ground: marathon.color.raised
|
|
2972
|
+
};
|
|
2076
2973
|
function editorPanels(deps) {
|
|
2077
2974
|
const act = (fn) => (s, ctx) => {
|
|
2078
2975
|
ctx.setDraft("");
|
|
@@ -2088,6 +2985,7 @@ function editorPanels(deps) {
|
|
|
2088
2985
|
}));
|
|
2089
2986
|
return {
|
|
2090
2987
|
edit: {
|
|
2988
|
+
...KEYS_CHROME,
|
|
2091
2989
|
view: ({ state, theme, size }) => linesView(toned(editView(state, theme.glyph, size)), theme),
|
|
2092
2990
|
keymap: [
|
|
2093
2991
|
{
|
|
@@ -2167,6 +3065,7 @@ function editorPanels(deps) {
|
|
|
2167
3065
|
} : void 0
|
|
2168
3066
|
},
|
|
2169
3067
|
prompt: {
|
|
3068
|
+
...KEYS_CHROME,
|
|
2170
3069
|
view: ({ state, theme }) => linesView(toned(promptView(state, theme.glyph)), theme),
|
|
2171
3070
|
submit: (s, line, ctx) => {
|
|
2172
3071
|
const text = line.trim();
|
|
@@ -2265,6 +3164,7 @@ function editorPanels(deps) {
|
|
|
2265
3164
|
}
|
|
2266
3165
|
},
|
|
2267
3166
|
review: {
|
|
3167
|
+
...KEYS_CHROME,
|
|
2268
3168
|
view: ({ state, theme, armed, size }) => linesView(toned(reviewView(state, theme.glyph, armed, size)), theme),
|
|
2269
3169
|
confirm: [{
|
|
2270
3170
|
key: "y",
|
|
@@ -2473,33 +3373,43 @@ function editorPanels(deps) {
|
|
|
2473
3373
|
//#endregion
|
|
2474
3374
|
//#region src/keys/module.ts
|
|
2475
3375
|
/**
|
|
2476
|
-
* `/keys`: the active key table
|
|
3376
|
+
* `/keys`: the active key table; `r` toggles revoked keys in, dimmed with their revocation time
|
|
3377
|
+
* and read-only. Unlocked it reads as the admin identity: Enter edits a
|
|
2477
3378
|
* row's grant (editor.tsx), `x` revokes it, `i` mints an invite and `n` registers a new key, each
|
|
2478
3379
|
* through a review. Locked, it reads as the agent, which a 403 turns into "admin required", and
|
|
2479
3380
|
* Enter shows a row's grant read-only. Locking closes the table and every editor panel.
|
|
2480
3381
|
*/
|
|
3382
|
+
const isRevoked = (r) => typeof r.revokedAt === "string";
|
|
3383
|
+
/** The rows the editor flows judge against: a revoked key holds no grant. */
|
|
3384
|
+
const live = (rows) => rows.filter((r) => !isRevoked(r));
|
|
2481
3385
|
/** Opens the table and reads it; `select` is a kid whose row takes the cursor once the read lands. */
|
|
2482
|
-
async function loadKeys(ctx, deps, select) {
|
|
3386
|
+
async function loadKeys(ctx, deps, select, revoked = false) {
|
|
2483
3387
|
const source = deps.admin.unlocked() ? "admin" : "agent";
|
|
2484
3388
|
ctx.open("keys", {
|
|
2485
3389
|
source,
|
|
2486
3390
|
rows: null,
|
|
2487
3391
|
note: null,
|
|
2488
3392
|
cursor: 0,
|
|
2489
|
-
detail: false
|
|
3393
|
+
detail: false,
|
|
3394
|
+
revoked
|
|
2490
3395
|
});
|
|
3396
|
+
const filter = revoked ? "all" : "active";
|
|
2491
3397
|
try {
|
|
2492
|
-
const rows = source === "admin" ? await deps.admin.run(({ client }) => client.admin.keys({
|
|
3398
|
+
const rows = source === "admin" ? await deps.admin.run(({ client }) => client.admin.keys({
|
|
3399
|
+
signal: ctx.signal,
|
|
3400
|
+
filter
|
|
3401
|
+
})) : await deps.keys(ctx.signal, filter);
|
|
2493
3402
|
const at = select === void 0 ? -1 : rows.findIndex((r) => r.kid === select);
|
|
2494
3403
|
const cursor = Math.max(0, at);
|
|
2495
|
-
|
|
3404
|
+
const mine = (s) => s.source === source && s.revoked === revoked;
|
|
3405
|
+
ctx.update("keys", (s) => mine(s) ? {
|
|
2496
3406
|
...s,
|
|
2497
3407
|
rows,
|
|
2498
3408
|
cursor
|
|
2499
3409
|
} : s);
|
|
2500
3410
|
} catch (err) {
|
|
2501
3411
|
const note = source === "admin" && err instanceof AuthError && err.code !== void 0 && err.code !== "forbidden" ? `could not read keys: ${err.code}: ${err.serverMessage ?? err.message}` : err instanceof AuthError ? "admin required — /admin unlock" : `could not read keys: ${err instanceof Error ? err.message : String(err)}`;
|
|
2502
|
-
ctx.update("keys", (s) => s.source === source ? {
|
|
3412
|
+
ctx.update("keys", (s) => s.source === source && s.revoked === revoked ? {
|
|
2503
3413
|
...s,
|
|
2504
3414
|
rows: null,
|
|
2505
3415
|
note
|
|
@@ -2520,9 +3430,11 @@ function keysModule(deps) {
|
|
|
2520
3430
|
}],
|
|
2521
3431
|
panels: {
|
|
2522
3432
|
keys: {
|
|
3433
|
+
...KEYS_CHROME,
|
|
2523
3434
|
view: ({ state, theme, size }) => {
|
|
2524
3435
|
const w = Math.max(1, size.columns - 2);
|
|
2525
|
-
const
|
|
3436
|
+
const gone = state.rows === null ? 0 : state.rows.filter(isRevoked).length;
|
|
3437
|
+
const count = state.rows === null ? "" : ` · ${state.rows.length - gone} active${state.revoked ? ` · ${gone} revoked` : ""}`;
|
|
2526
3438
|
const head = `${theme.glyph.title} keys${count} · ${state.source}`;
|
|
2527
3439
|
if (state.note !== null) return [head, ` ${sanitise(state.note)}`].join("\n");
|
|
2528
3440
|
if (state.rows === null) return [head, " reading…"].join("\n");
|
|
@@ -2530,6 +3442,7 @@ function keysModule(deps) {
|
|
|
2530
3442
|
if (state.detail && pick !== void 0) return [
|
|
2531
3443
|
head,
|
|
2532
3444
|
truncateCells(sanitise(` ${pick.kid} ${pick.name} ${pick.algorithm}`), w),
|
|
3445
|
+
...isRevoked(pick) ? [truncateCells(sanitise(` revoked ${pick.revokedAt}`), w)] : [],
|
|
2533
3446
|
...describeGrant(pick.namespaces, pick.capabilities).map((g) => truncateCells(sanitise(` ${g}`), w)),
|
|
2534
3447
|
" backspace back · esc close"
|
|
2535
3448
|
].join("\n");
|
|
@@ -2537,16 +3450,23 @@ function keysModule(deps) {
|
|
|
2537
3450
|
const nameW = Math.min(24, Math.max(4, ...state.rows.map((r) => r.name.length)));
|
|
2538
3451
|
const max = Math.max(1, size.rows - 3);
|
|
2539
3452
|
const start = Math.max(0, Math.min(state.cursor - max + 1, state.rows.length - max));
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
3453
|
+
const lines = state.rows.slice(start, start + max).map((r, k) => {
|
|
3454
|
+
const mark = start + k === state.cursor ? theme.glyph.cursor : " ";
|
|
3455
|
+
const grant = compactGrant(r.namespaces, r.capabilities);
|
|
3456
|
+
const gone = isRevoked(r);
|
|
3457
|
+
const text = truncateCells(sanitise(`${mark} ${r.kid.padEnd(kidW)} ${r.name.padEnd(nameW)} ${gone ? `revoked ${r.revokedAt} ` : ""}${grant}`), w);
|
|
3458
|
+
return gone ? {
|
|
3459
|
+
text,
|
|
3460
|
+
tone: "dim"
|
|
3461
|
+
} : { text };
|
|
3462
|
+
});
|
|
3463
|
+
const toggle = state.revoked ? "r hide revoked" : "r show revoked";
|
|
3464
|
+
return linesView([
|
|
3465
|
+
{ text: head },
|
|
3466
|
+
...lines,
|
|
3467
|
+
{ text: truncateCells(` ${GRANT_LEGEND}`, w) },
|
|
3468
|
+
{ text: state.source === "admin" && deps.admin.unlocked() ? ` ↑↓/jk move · enter edit · x revoke · i invite · n register · ${toggle} · esc close` : ` ↑↓/jk move · enter grant · ${toggle} · esc close` }
|
|
3469
|
+
], theme);
|
|
2550
3470
|
},
|
|
2551
3471
|
keymap: [
|
|
2552
3472
|
{
|
|
@@ -2584,6 +3504,10 @@ function keysModule(deps) {
|
|
|
2584
3504
|
{
|
|
2585
3505
|
key: "x",
|
|
2586
3506
|
act: "revoke"
|
|
3507
|
+
},
|
|
3508
|
+
{
|
|
3509
|
+
key: "r",
|
|
3510
|
+
act: "toggleRevoked"
|
|
2587
3511
|
}
|
|
2588
3512
|
],
|
|
2589
3513
|
on: {
|
|
@@ -2591,11 +3515,12 @@ function keysModule(deps) {
|
|
|
2591
3515
|
down: move(1),
|
|
2592
3516
|
in: (s, ctx) => {
|
|
2593
3517
|
if (s.rows === null || s.rows.length === 0) return void 0;
|
|
2594
|
-
|
|
3518
|
+
const row = s.rows[s.cursor];
|
|
3519
|
+
if (deps.admin.unlocked() && s.source === "admin" && !isRevoked(row)) {
|
|
2595
3520
|
openEditor(ctx, deps, {
|
|
2596
3521
|
kind: "update",
|
|
2597
|
-
row
|
|
2598
|
-
}, s.rows);
|
|
3522
|
+
row
|
|
3523
|
+
}, live(s.rows));
|
|
2599
3524
|
return;
|
|
2600
3525
|
}
|
|
2601
3526
|
return {
|
|
@@ -2607,6 +3532,10 @@ function keysModule(deps) {
|
|
|
2607
3532
|
...s,
|
|
2608
3533
|
detail: false
|
|
2609
3534
|
} : void 0,
|
|
3535
|
+
toggleRevoked: (s, ctx) => {
|
|
3536
|
+
if (s.detail || s.rows === null && s.note === null) return void 0;
|
|
3537
|
+
loadKeys(ctx, deps, s.rows?.[s.cursor]?.kid, !s.revoked);
|
|
3538
|
+
},
|
|
2610
3539
|
register: (s, ctx) => {
|
|
2611
3540
|
if (!(deps.admin.unlocked() && s.source === "admin" && s.rows !== null && !s.detail)) return;
|
|
2612
3541
|
ctx.setDraft("");
|
|
@@ -2615,17 +3544,18 @@ function keysModule(deps) {
|
|
|
2615
3544
|
label: "kid to register",
|
|
2616
3545
|
error: null,
|
|
2617
3546
|
back: null,
|
|
2618
|
-
table: s.rows
|
|
3547
|
+
table: live(s.rows)
|
|
2619
3548
|
});
|
|
2620
3549
|
},
|
|
2621
3550
|
invite: (s, ctx) => {
|
|
2622
3551
|
if (!(deps.admin.unlocked() && s.source === "admin" && s.rows !== null && !s.detail)) return;
|
|
2623
|
-
openEditor(ctx, deps, { kind: "invite" }, s.rows);
|
|
3552
|
+
openEditor(ctx, deps, { kind: "invite" }, live(s.rows));
|
|
2624
3553
|
},
|
|
2625
3554
|
revoke: (s, ctx) => {
|
|
2626
3555
|
if (!(deps.admin.unlocked() && s.source === "admin" && s.rows !== null && !s.detail)) return;
|
|
2627
3556
|
const row = s.rows[s.cursor];
|
|
2628
|
-
if (row === void 0) return void 0;
|
|
3557
|
+
if (row === void 0 || isRevoked(row)) return void 0;
|
|
3558
|
+
const table = live(s.rows);
|
|
2629
3559
|
ctx.setDraft("");
|
|
2630
3560
|
switchTo(ctx, deps, "review", {
|
|
2631
3561
|
flow: {
|
|
@@ -2636,13 +3566,13 @@ function keysModule(deps) {
|
|
|
2636
3566
|
next: null,
|
|
2637
3567
|
verdict: revokeVerdict({
|
|
2638
3568
|
kid: row.kid,
|
|
2639
|
-
table
|
|
3569
|
+
table,
|
|
2640
3570
|
adminCapability: adminCapabilityOf(deps.adminEnv).adminCapability ?? "admin"
|
|
2641
3571
|
}),
|
|
2642
3572
|
stage: "review",
|
|
2643
3573
|
error: null,
|
|
2644
3574
|
mismatch: false,
|
|
2645
|
-
table
|
|
3575
|
+
table
|
|
2646
3576
|
});
|
|
2647
3577
|
}
|
|
2648
3578
|
}
|
|
@@ -3670,13 +4600,15 @@ const substrateTheme = {
|
|
|
3670
4600
|
}
|
|
3671
4601
|
};
|
|
3672
4602
|
function buildModules(deps, opts = {}) {
|
|
4603
|
+
const pager = listPager();
|
|
3673
4604
|
return [
|
|
3674
4605
|
statusModule(deps, opts),
|
|
3675
4606
|
adminModule(deps),
|
|
3676
4607
|
doctorModule(deps, opts.doctorIntervalMs !== void 0 ? { intervalMs: opts.doctorIntervalMs } : {}),
|
|
3677
4608
|
keysModule(deps),
|
|
3678
4609
|
surgeryModule(deps),
|
|
3679
|
-
askModule(deps),
|
|
4610
|
+
askModule(deps, pager),
|
|
4611
|
+
pager.module,
|
|
3680
4612
|
schemaModule(deps),
|
|
3681
4613
|
dashModule(deps, opts.dashIntervalMs !== void 0 ? { intervalMs: opts.dashIntervalMs } : {})
|
|
3682
4614
|
];
|
|
@@ -3688,6 +4620,10 @@ function substrateAppOptions(deps, opts = {}) {
|
|
|
3688
4620
|
defaultTurn: "substrate.ask:ask",
|
|
3689
4621
|
band: true,
|
|
3690
4622
|
gutter: 2,
|
|
4623
|
+
entries: { answer: ({ entry, body, theme }) => createElement(Box, {
|
|
4624
|
+
flexDirection: "column",
|
|
4625
|
+
marginBottom: 1
|
|
4626
|
+
}, entry.mark !== null ? createElement(Text, { color: theme.color.accent }, `↩ ⟨${entry.mark}⟩`) : null, body) },
|
|
3691
4627
|
anchored: true,
|
|
3692
4628
|
terminal: false
|
|
3693
4629
|
};
|