@mnmal-ai/substrate-tui 1.0.0-alpha.15 → 1.0.0-alpha.17

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.
@@ -2,7 +2,7 @@
2
2
  import { i as isKeyRejection, t as adminCapabilityOf } from "./session-C1D5Ermr.js";
3
3
  import { resolve } from "node:path";
4
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";
5
+ import { createApp, defineModule, listPager, sanitise } from "@mnmal-ai/surface-core";
6
6
  import { marathon } from "@mnmal-ai/surface-theme";
7
7
  import { Box, Text } from "ink";
8
8
  import { createElement } from "react";
@@ -457,7 +457,7 @@ function askedFields(frame) {
457
457
  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);
458
458
  else if (!k.startsWith("_")) add(k);
459
459
  };
460
- for (const o of params.orderBy ?? []) for (const k of Object.keys(o)) add(k.replace(/^\$/, ""));
460
+ for (const o of params.orderBy ?? []) for (const k of Object.keys(o)) add(k);
461
461
  walk(params.where);
462
462
  return out;
463
463
  }
@@ -490,12 +490,12 @@ const cell$1 = (r, e, view) => {
490
490
  const valueWidth = (values, columns) => Math.min(Math.max(0, ...values.map(cellWidth)), Math.max(8, Math.floor(columns / 3)));
491
491
  const SKIP = /* @__PURE__ */ new Set(["id", "shortId"]);
492
492
  /** `field=value` for a row's scalar fields: the asked-about ones first, then the rest. */
493
- function details(r, asked, label, view) {
493
+ function detailList(r, asked, label, view) {
494
494
  const scalar = (k) => {
495
495
  const v = r[k];
496
496
  return v !== null && v !== void 0 && v !== "" && typeof v !== "object";
497
497
  };
498
- 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)}`).join(" ");
498
+ 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)}`);
499
499
  }
500
500
  const COLS = {
501
501
  id: 6,
@@ -531,12 +531,7 @@ function declaredRows(list, row, view, theme, columns) {
531
531
  return {
532
532
  title: (f === void 0 ? "" : cell$1(r, row.title ?? { field: f }, view)) || "—",
533
533
  tags: row.tags.map((e) => cell$1(r, e, view)).filter((t) => t !== ""),
534
- det: [...walkedDetails(r), ...row.details.map((e) => {
535
- const v = cell$1(r, e, view);
536
- if (v === "") return "";
537
- const label = e.label ?? (typeof own$1(r, e.field) === "number" || isNumericFormat(own$1(view.formats, e.field)) ? e.field : void 0);
538
- return label === void 0 ? v : `${sanitise(label)} ${v}`;
539
- })].filter((d) => d !== "")
534
+ det: declaredDetails(r, row, view)
540
535
  };
541
536
  });
542
537
  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));
@@ -728,7 +723,7 @@ const rows = ({ data, theme, size }) => {
728
723
  " ",
729
724
  /* @__PURE__ */ jsx(Text, {
730
725
  color: theme.color.dimMid,
731
- children: padCells(details(r, asked, label, view), detailCols)
726
+ children: padCells(detailList(r, asked, label, view).join(" "), detailCols)
732
727
  })
733
728
  ] }, i);
734
729
  }), lost]
@@ -867,6 +862,145 @@ const renderers = {
867
862
  [KIND.unknown]: unknown,
868
863
  [KIND.error]: error
869
864
  };
865
+ /** A declared row's details: its walked values, then each declared detail, labelled when numeric. */
866
+ function declaredDetails(r, row, view) {
867
+ return [...walkedDetails(r), ...row.details.map((e) => {
868
+ const v = cell$1(r, e, view);
869
+ if (v === "") return "";
870
+ const label = e.label ?? (typeof own$1(r, e.field) === "number" || isNumericFormat(own$1(view.formats, e.field)) ? e.field : void 0);
871
+ return label === void 0 ? v : `${sanitise(label)} ${v}`;
872
+ })].filter((d) => d !== "");
873
+ }
874
+ /**
875
+ * A rows answer as a list for the pager, or undefined when it is not a plain list of rows (an
876
+ * aggregate, a count, a rejected frame). Declared rows keep their title, tags, details and value;
877
+ * the rest get a label and the asked-about fields, then the others, as `field=value`.
878
+ */
879
+ function rowsList(o) {
880
+ if (!Array.isArray(o.data) || o.aggregate !== void 0 || o.count !== void 0) return void 0;
881
+ const view = o.tui;
882
+ const asked = askedFields(o.frame);
883
+ const rows = o.data.map((raw, i) => {
884
+ const r = withWalked(raw);
885
+ const key = typeof raw.id === "string" ? raw.id : String(i);
886
+ const decl = view?.row;
887
+ if (view !== void 0 && decl !== void 0) {
888
+ const f = decl.title?.field ?? labelField(r);
889
+ const value = decl.value === void 0 ? "" : cell$1(r, decl.value, view);
890
+ return {
891
+ key,
892
+ name: (f === void 0 ? "" : cell$1(r, decl.title ?? { field: f }, view)) || "—",
893
+ details: [...decl.tags.map((e) => cell$1(r, e, view)).filter((t) => t !== ""), ...declaredDetails(r, decl, view)],
894
+ ...value !== "" ? { value } : {}
895
+ };
896
+ }
897
+ const label = labelField(r);
898
+ return {
899
+ key,
900
+ name: (label !== void 0 ? shown(r, label, view) : "") || str(r.shortId ?? r.id) || "—",
901
+ details: detailList(r, asked, label, view)
902
+ };
903
+ });
904
+ const notes = [...errorLines(o.errors), ...droppedLines(o.dropped)];
905
+ return {
906
+ heading: frameKey(o),
907
+ rows,
908
+ ...notes.length > 0 ? { notes } : {}
909
+ };
910
+ }
911
+
912
+ //#endregion
913
+ //#region src/ask/list.ts
914
+ /** Rows `r` lists: recall's hits about the same idea, the row itself left out. */
915
+ const RELATED_K = 20;
916
+ const META = [
917
+ "status",
918
+ "area",
919
+ "project"
920
+ ];
921
+ const text = (v) => typeof v === "string" ? v : "";
922
+ const scalar = (v) => v !== null && v !== void 0 && v !== "" && typeof v !== "object";
923
+ const nameOf = (row) => typeof row.name === "string" ? row.name : row.name.map((s) => s.text).join("");
924
+ /** A frame's rows; a refused frame throws the server's reason rather than reading as empty. */
925
+ async function rowsOf(deps, key, frame, signal) {
926
+ const env = await deps.query({ [key]: frame }, signal);
927
+ if (!Object.hasOwn(env.data, key)) throw new Error(env.errors?.[0]?.message ?? `${key} was refused`);
928
+ const v = env.data[key];
929
+ return (Array.isArray(v) ? v : [v]).filter((x) => x !== null && typeof x === "object");
930
+ }
931
+ /**
932
+ * Enter and `r` on an NL answer's rows. `open` reads the row's record whole (every scalar field
933
+ * the schema has, else the fields the answer carried); `related` asks recall, over the same type
934
+ * and namespace, for rows about the same idea as the record's label and body.
935
+ */
936
+ function listHooks(deps, ns, type, fields, answered, signal) {
937
+ const records = /* @__PURE__ */ new Map();
938
+ const record = (row) => {
939
+ const known = records.get(row.key);
940
+ if (known !== void 0) return known;
941
+ const seen = answered.get(row.key) ?? {};
942
+ if (typeof seen.id !== "string") return Promise.resolve(seen);
943
+ 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]));
944
+ const read = rowsOf(deps, `${ns}/${type}`, {
945
+ params: {
946
+ where: { id: { eq: row.key } },
947
+ limit: 1
948
+ },
949
+ fields: Object.fromEntries(["id", ...names].map((k) => [k, true]))
950
+ }, signal).then((rows) => {
951
+ const r = rows[0];
952
+ if (r === void 0) throw new Error(`${type} ${row.key} is gone`);
953
+ return r;
954
+ });
955
+ read.catch(() => records.delete(row.key));
956
+ records.set(row.key, read);
957
+ return read;
958
+ };
959
+ return {
960
+ async open(row) {
961
+ const r = await record(row);
962
+ const label = labelField(r);
963
+ const meta = META.map((k) => text(r[k])).filter((v) => v !== "");
964
+ const prose = [text(r.body), text(r.notes)].filter((v) => v !== "");
965
+ 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");
966
+ return {
967
+ title: label !== void 0 ? text(r[label]) : nameOf(row),
968
+ ...meta.length > 0 ? { meta: meta.join(" · ") } : {},
969
+ ...body !== "" ? { body } : {}
970
+ };
971
+ },
972
+ async related(row) {
973
+ const r = await record(row);
974
+ const label = labelField(r);
975
+ const title = label !== void 0 ? text(r[label]) : nameOf(row);
976
+ const rows = (await rowsOf(deps, "recall/Hit", {
977
+ params: {
978
+ to: [title, text(r.body)].filter((v) => v !== "").join("\n") || nameOf(row),
979
+ namespace: ns,
980
+ type,
981
+ k: 21
982
+ },
983
+ fields: {
984
+ id: true,
985
+ score: true,
986
+ row: true
987
+ }
988
+ }, signal)).filter((h) => h.id !== row.key).slice(0, 20).map((h) => {
989
+ const hr = h.row !== null && typeof h.row === "object" ? h.row : {};
990
+ const l = labelField(hr);
991
+ return {
992
+ key: String(h.id),
993
+ name: (l !== void 0 ? text(hr[l]) : "") || String(h.id),
994
+ ...typeof h.score === "number" ? { value: h.score.toFixed(2) } : {}
995
+ };
996
+ });
997
+ return {
998
+ heading: `related to ${title}`,
999
+ rows
1000
+ };
1001
+ }
1002
+ };
1003
+ }
870
1004
 
871
1005
  //#endregion
872
1006
  //#region src/ask/module.ts
@@ -890,7 +1024,11 @@ function explain(err, endpoint) {
890
1024
  }
891
1025
  /** What the server accepts for `asOf`: a date-time with an explicit offset. */
892
1026
  const ISO_WITH_OFFSET = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})$/;
893
- function askModule(deps) {
1027
+ /**
1028
+ * The ask module. A plain list of rows goes to `pager`, whose module the app must also register;
1029
+ * the default is for an app that registers it from here, as the tests do.
1030
+ */
1031
+ function askModule(deps, pager = listPager()) {
894
1032
  let namespace = deps.namespace;
895
1033
  let pack = null;
896
1034
  let forceType;
@@ -903,6 +1041,8 @@ function askModule(deps) {
903
1041
  types: {},
904
1042
  problems: []
905
1043
  };
1044
+ /** The namespace's types, for reading a listed row's record whole; empty until read. */
1045
+ let types = {};
906
1046
  const loadPack = (ctx) => {
907
1047
  pack = null;
908
1048
  const ns = namespace;
@@ -910,9 +1050,12 @@ function askModule(deps) {
910
1050
  types: {},
911
1051
  problems: []
912
1052
  };
1053
+ types = {};
913
1054
  deps.getJson(`/substrate/v1/schema/${encodeURIComponent(ns)}`, ctx.signal).then((s) => {
914
1055
  if (ns !== namespace) return;
915
1056
  try {
1057
+ const t = s?.types;
1058
+ if (t !== null && typeof t === "object") types = t;
916
1059
  present = parseTui(s?.annotations?.tui);
917
1060
  } catch {
918
1061
  present = {
@@ -940,6 +1083,15 @@ function askModule(deps) {
940
1083
  tui: view
941
1084
  } : o;
942
1085
  };
1086
+ /** A rows answer as a list the pager pages, with Enter and `r` wired to the row's type. */
1087
+ const asList = (o, ctx) => {
1088
+ const list = rowsList(o);
1089
+ if (list === void 0) return void 0;
1090
+ const [ns = namespace, type = ""] = (Object.keys(o.frame)[0] ?? "").split("/");
1091
+ const answered = new Map(o.data.map((r, i) => [typeof r.id === "string" ? r.id : String(i), r]));
1092
+ const fields = ns === namespace ? own$1(types, type)?.fields : void 0;
1093
+ return pager.present(list, listHooks(deps, ns, type, fields, answered, ctx.signal));
1094
+ };
943
1095
  const pick = (i) => (s, ctx) => {
944
1096
  const opt = s.options[i];
945
1097
  if (opt === void 0) return void 0;
@@ -1063,12 +1215,18 @@ function askModule(deps) {
1063
1215
  last = { lines };
1064
1216
  if (ev.event === "clarify" && ev.data.options.length > 0) ctx.open("clarify", ev.data);
1065
1217
  else ctx.close();
1218
+ const data = ev.event === "rows" ? withView(ev.data) : ev.data;
1219
+ const listed = ev.event === "rows" ? asList(data, ctx) : void 0;
1220
+ const chips = outcomeChips(ev.data);
1066
1221
  yield {
1067
1222
  phase: "final",
1068
- reply: {
1223
+ reply: listed !== void 0 ? {
1224
+ ...listed,
1225
+ chips
1226
+ } : {
1069
1227
  kind: `substrate.${ev.event}`,
1070
- data: ev.event === "rows" ? withView(ev.data) : ev.data,
1071
- chips: outcomeChips(ev.data)
1228
+ data,
1229
+ chips
1072
1230
  }
1073
1231
  };
1074
1232
  return;
@@ -2609,6 +2767,14 @@ function editView(s, glyph, size) {
2609
2767
  fit(" ↑↓←→ move · space toggle · + add namespace · enter review · esc discard")
2610
2768
  ];
2611
2769
  }
2770
+ /** Every keys panel: a one-column inset on a raised ground, set apart from the input band. */
2771
+ const KEYS_CHROME = {
2772
+ inset: {
2773
+ x: 1,
2774
+ y: 0
2775
+ },
2776
+ ground: marathon.color.raised
2777
+ };
2612
2778
  function editorPanels(deps) {
2613
2779
  const act = (fn) => (s, ctx) => {
2614
2780
  ctx.setDraft("");
@@ -2624,6 +2790,7 @@ function editorPanels(deps) {
2624
2790
  }));
2625
2791
  return {
2626
2792
  edit: {
2793
+ ...KEYS_CHROME,
2627
2794
  view: ({ state, theme, size }) => linesView(toned(editView(state, theme.glyph, size)), theme),
2628
2795
  keymap: [
2629
2796
  {
@@ -2703,6 +2870,7 @@ function editorPanels(deps) {
2703
2870
  } : void 0
2704
2871
  },
2705
2872
  prompt: {
2873
+ ...KEYS_CHROME,
2706
2874
  view: ({ state, theme }) => linesView(toned(promptView(state, theme.glyph)), theme),
2707
2875
  submit: (s, line, ctx) => {
2708
2876
  const text = line.trim();
@@ -2801,6 +2969,7 @@ function editorPanels(deps) {
2801
2969
  }
2802
2970
  },
2803
2971
  review: {
2972
+ ...KEYS_CHROME,
2804
2973
  view: ({ state, theme, armed, size }) => linesView(toned(reviewView(state, theme.glyph, armed, size)), theme),
2805
2974
  confirm: [{
2806
2975
  key: "y",
@@ -3056,6 +3225,7 @@ function keysModule(deps) {
3056
3225
  }],
3057
3226
  panels: {
3058
3227
  keys: {
3228
+ ...KEYS_CHROME,
3059
3229
  view: ({ state, theme, size }) => {
3060
3230
  const w = Math.max(1, size.columns - 2);
3061
3231
  const count = state.rows === null ? "" : ` · ${state.rows.length} active`;
@@ -4206,13 +4376,15 @@ const substrateTheme = {
4206
4376
  }
4207
4377
  };
4208
4378
  function buildModules(deps, opts = {}) {
4379
+ const pager = listPager();
4209
4380
  return [
4210
4381
  statusModule(deps, opts),
4211
4382
  adminModule(deps),
4212
4383
  doctorModule(deps, opts.doctorIntervalMs !== void 0 ? { intervalMs: opts.doctorIntervalMs } : {}),
4213
4384
  keysModule(deps),
4214
4385
  surgeryModule(deps),
4215
- askModule(deps),
4386
+ askModule(deps, pager),
4387
+ pager.module,
4216
4388
  schemaModule(deps),
4217
4389
  dashModule(deps, opts.dashIntervalMs !== void 0 ? { intervalMs: opts.dashIntervalMs } : {})
4218
4390
  ];
@@ -51,8 +51,8 @@ async function main() {
51
51
  }
52
52
  Object.assign(process.env, colorDepthEnv(process.env));
53
53
  const [{ createTui }, { makeDeps }, { prepareTerminal }, { terminalOptions }] = await Promise.all([
54
- import("../app-CQnYeMdE.js"),
55
- import("../deps-WzqsHHiC.js"),
54
+ import("../app-DacCAxOg.js"),
55
+ import("../deps-P8sDloE9.js"),
56
56
  import("@mnmal-ai/surface-core/pure"),
57
57
  import("../terminal-BAd8sH3d.js")
58
58
  ]);
@@ -42,6 +42,7 @@ function makeDeps(config, fetchImpl = fetch, env = process.env) {
42
42
  namespace: config.namespace,
43
43
  nl: client.nl,
44
44
  federatedSchema: () => client.federatedSchema(),
45
+ query: (frame, signal) => client.query(frame, sig(signal)),
45
46
  metrics: (signal) => client.metrics(sig(signal)),
46
47
  errors: (signal) => admin.unlocked() ? admin.run(({ client: c }) => c.errors(sig(signal)), { touch: false }) : client.errors(sig(signal)),
47
48
  admin,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mnmal-ai/substrate-tui",
3
- "version": "1.0.0-alpha.15",
3
+ "version": "1.0.0-alpha.17",
4
4
  "description": "Terminal UI for Substrate: ask in natural language, browse the schema, see the service status.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,17 +20,17 @@
20
20
  "access": "public"
21
21
  },
22
22
  "dependencies": {
23
- "@mnmal-ai/substrate": "1.0.0-alpha.15",
24
- "@mnmal-ai/substrate-nl": "1.0.0-alpha.15",
25
- "@mnmal-ai/surface-core": "0.16.0",
26
- "@mnmal-ai/surface-kit": "0.16.0",
27
- "@mnmal-ai/surface-theme": "0.16.0",
23
+ "@mnmal-ai/substrate": "1.0.0-alpha.17",
24
+ "@mnmal-ai/substrate-nl": "1.0.0-alpha.17",
25
+ "@mnmal-ai/surface-core": "0.17.1",
26
+ "@mnmal-ai/surface-kit": "0.17.1",
27
+ "@mnmal-ai/surface-theme": "0.17.1",
28
28
  "ink": "^8.0.0",
29
29
  "react": "^19.3.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@biomejs/biome": "2.6.4",
33
- "@mnmal-ai/substrate-config": "1.0.0-alpha.15",
33
+ "@mnmal-ai/substrate-config": "1.0.0-alpha.17",
34
34
  "@types/node": "^26.6.4",
35
35
  "@types/react": "^19.2.0",
36
36
  "ink-testing-library": "^4.0.0",
@@ -40,7 +40,7 @@
40
40
  "vitest": "^5.0.3"
41
41
  },
42
42
  "engines": {
43
- "node": ">=26.12.1"
43
+ "node": ">=26.13.1"
44
44
  },
45
45
  "scripts": {
46
46
  "build": "tsdown",