@ontology-kit/okb 0.2.0 → 0.3.0-alpha.133
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/dist/check.js +21 -2
- package/dist/checks.js +23 -0
- package/dist/cli.js +11 -819
- package/dist/clidocs.d.ts +0 -2
- package/dist/clidocs.js +5 -27
- package/dist/command-table.d.ts +144 -0
- package/dist/command-table.js +85 -0
- package/dist/commands/documents.d.ts +3 -0
- package/dist/commands/documents.js +250 -0
- package/dist/commands/getting-started.d.ts +3 -0
- package/dist/commands/getting-started.js +135 -0
- package/dist/commands/housekeeping.d.ts +3 -0
- package/dist/commands/housekeeping.js +203 -0
- package/dist/commands/modeling.d.ts +3 -0
- package/dist/commands/modeling.js +467 -0
- package/dist/commands/planning.d.ts +3 -0
- package/dist/commands/planning.js +251 -0
- package/dist/commands/reviewing.d.ts +3 -0
- package/dist/commands/reviewing.js +234 -0
- package/dist/commands/skills.d.ts +3 -0
- package/dist/commands/skills.js +53 -0
- package/dist/commands.d.ts +10 -22
- package/dist/commands.js +15 -716
- package/dist/cqanswer.d.ts +51 -0
- package/dist/cqanswer.js +76 -0
- package/dist/export-changes.d.ts +40 -0
- package/dist/export-changes.js +115 -0
- package/dist/export-csv.d.ts +17 -0
- package/dist/export-csv.js +188 -0
- package/dist/export-eval-cases.d.ts +38 -0
- package/dist/export-eval-cases.js +40 -0
- package/dist/export.d.ts +33 -2
- package/dist/export.js +69 -11
- package/dist/help.d.ts +11 -2
- package/dist/help.js +51 -64
- package/dist/ops/admin.d.ts +1 -1
- package/dist/ops/admin.js +2 -1
- package/dist/ops/classes.js +2 -2
- package/dist/ops/common.d.ts +12 -2
- package/dist/ops/common.js +3 -0
- package/dist/ops/instances.js +2 -2
- package/dist/ops/links.d.ts +1 -1
- package/dist/ops/links.js +2 -1
- package/dist/ops/scope.d.ts +9 -2
- package/dist/ops/scope.js +19 -3
- package/dist/ops/slots.js +6 -6
- package/dist/provenance.d.ts +1 -1
- package/dist/provenance.js +3 -2
- package/dist/release.d.ts +4 -0
- package/dist/release.js +22 -3
- package/dist/render.d.ts +85 -1
- package/dist/render.js +244 -1
- package/dist/runner.d.ts +3 -0
- package/dist/runner.js +84 -0
- package/dist/skills.d.ts +19 -3
- package/dist/skills.js +129 -13
- package/dist/verifynote.d.ts +4 -3
- package/dist/verifynote.js +3 -2
- package/dist/workflows.d.ts +283 -0
- package/dist/workflows.js +270 -0
- package/meta-kb/data/edges.json +25 -0
- package/meta-kb/data/manifest.json +4 -4
- package/meta-kb/data/nodes.json +39 -2
- package/package.json +4 -3
- package/schema/node.schema.json +22 -1
- package/skills/build-domain-kb-from-docs/SKILL.md +2 -2
- package/skills/ontology-coach/SKILL.md +3 -3
- package/skills/ontology-maintainer/SKILL.md +2 -2
- package/skills/ontology-review/SKILL.md +2 -2
- package/skills/ontology-signoff/SKILL.md +2 -2
- package/templates/okb-check.yml +17 -1
package/dist/cli.js
CHANGED
|
@@ -1,829 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/** okb: command-line interface. Run `okb help` for usage. */
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
import { changedPageSet, changesPage, contextPage } from "./reviewdocs.js";
|
|
14
|
-
import { buildReview } from "./review.js";
|
|
15
|
-
import * as prov from "./provenance.js";
|
|
16
|
-
import { computeStatus } from "./status.js";
|
|
17
|
-
import { HELP } from "./help.js";
|
|
18
|
-
import { PACKAGE_VERSION } from "./version.js";
|
|
19
|
-
import { runCheck } from "./check.js";
|
|
20
|
-
import { renderResync, resync } from "./resync.js";
|
|
21
|
-
import { diffMarkdown, diffOntologies } from "./diff.js";
|
|
22
|
-
import { loadSide } from "./ontology-ref.js";
|
|
23
|
-
import { cutRelease, verifyRelease } from "./release.js";
|
|
24
|
-
import { installSkills, skillsStatus } from "./skills.js";
|
|
25
|
-
const str = { type: "string" };
|
|
26
|
-
const strs = { type: "string", multiple: true };
|
|
27
|
-
const bool = { type: "boolean" };
|
|
28
|
-
const GLOBAL = { C: { type: "string", short: "C" }, json: bool };
|
|
29
|
-
function parse(argv, options = {}) {
|
|
30
|
-
const { values, positionals } = parseArgs({ args: argv, options: { ...GLOBAL, ...options }, allowPositionals: true, strict: true });
|
|
31
|
-
return { v: values, p: positionals };
|
|
32
|
-
}
|
|
33
|
-
const num = (x, flag) => {
|
|
34
|
-
if (x === undefined)
|
|
35
|
-
return undefined;
|
|
36
|
-
if (!/^\d+$/.test(x))
|
|
37
|
-
throw new OkbError(`${flag} needs a whole number (0 or more).`);
|
|
38
|
-
return Number(x);
|
|
39
|
-
};
|
|
40
|
-
const tri = (x) => (x === undefined ? undefined : ["true", "yes", "1"].includes(x.toLowerCase()));
|
|
41
|
-
/** `okb review` lists this many prompts per rule in the terminal; --json has them all. */
|
|
42
|
-
const REVIEW_PROMPTS_SHOWN = 12;
|
|
43
|
-
const TIP = /^(Tip|Add a|Attach|Decide|Say which|List the)/;
|
|
44
|
-
function say(notes) {
|
|
45
|
-
for (const n of notes)
|
|
46
|
-
console.log(TIP.test(n) ? c.dim(" " + n) : n);
|
|
47
|
-
}
|
|
48
|
-
/** Under --json print what `data` builds; otherwise run the human-readable printer. */
|
|
49
|
-
function output(v, data, human) {
|
|
50
|
-
if (v.json)
|
|
51
|
-
console.log(JSON.stringify(data(), null, 2));
|
|
52
|
-
else
|
|
53
|
-
human();
|
|
54
|
-
}
|
|
55
|
-
function load(v) {
|
|
56
|
-
return Ontology.load(v.C ?? ".");
|
|
57
|
-
}
|
|
58
|
-
/** Load the folder, change it, save it and print the notes, all with the folder's lock held. */
|
|
59
|
-
function mutate(v, fn) {
|
|
60
|
-
Ontology.edit(v.C ?? ".", (ont) => {
|
|
61
|
-
const notes = ont.tick(() => fn(ont));
|
|
62
|
-
ont.save();
|
|
63
|
-
say(notes);
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
async function main(argvIn) {
|
|
67
|
-
// Accept -C <folder> anywhere, including before the command.
|
|
68
|
-
const argv = [...argvIn];
|
|
69
|
-
if (argv[0] === "-C" && argv.length > 2)
|
|
70
|
-
argv.push(...argv.splice(0, 2));
|
|
71
|
-
if (argv.includes("--help") || argv.includes("-h")) {
|
|
72
|
-
console.log(HELP);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
// "property" and "relationship" are everyday names for Ontology 101's two kinds of slot.
|
|
76
|
-
if (argv[0] === "property")
|
|
77
|
-
argv[0] = "slot";
|
|
78
|
-
if (argv[0] === "relationship") {
|
|
79
|
-
argv[0] = "slot";
|
|
80
|
-
if (argv[1] === "add")
|
|
81
|
-
argv.push("--type", "Instance");
|
|
82
|
-
if (argv[1] === "property")
|
|
83
|
-
argv[0] = "edgeprop";
|
|
84
|
-
for (let i = 2; i < argv.length; i++) {
|
|
85
|
-
if (argv[i] === "--from")
|
|
86
|
-
argv[i] = "--on";
|
|
87
|
-
else if (argv[i] === "--to")
|
|
88
|
-
argv[i] = "--range";
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
const [cmd, ...rest] = argv;
|
|
92
|
-
const sub = rest[0];
|
|
93
|
-
switch (cmd) {
|
|
94
|
-
case undefined:
|
|
95
|
-
case "help":
|
|
96
|
-
console.log(HELP);
|
|
97
|
-
return;
|
|
98
|
-
case "version":
|
|
99
|
-
case "--version":
|
|
100
|
-
console.log(`okb ${PACKAGE_VERSION} · meta-KB ${MetaKB.get().version}`);
|
|
101
|
-
return;
|
|
102
|
-
case "init": {
|
|
103
|
-
const { v, p } = parse(rest, { name: str });
|
|
104
|
-
if (!p[0])
|
|
105
|
-
throw new OkbError('Usage: okb init <folder> --name "My Ontology"');
|
|
106
|
-
const name = v.name ?? p[0];
|
|
107
|
-
const ont = ops.createOntology(p[0], name);
|
|
108
|
-
ont.manifest.metaKbVersion = MetaKB.get().version;
|
|
109
|
-
ont.save();
|
|
110
|
-
console.log(`${SYMBOL.ok} Created ontology '${name}' in ${ont.root}\n`);
|
|
111
|
-
console.log(stepGuide(MetaKB.get().step(1)));
|
|
112
|
-
console.log(c.dim(`\nNext: cd ${p[0]} && okb scope --domain "..." --purpose "..." --users "..."`));
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
case "scope": {
|
|
116
|
-
const { v } = parse(rest, { name: str, domain: str, purpose: str, users: strs, maintainers: strs, "out-of-scope": strs, kind: str });
|
|
117
|
-
const touched = ["name", "domain", "purpose", "users", "maintainers", "out-of-scope", "kind"].some((k) => v[k] !== undefined);
|
|
118
|
-
const ont = touched
|
|
119
|
-
? Ontology.edit(v.C ?? ".", (o) => {
|
|
120
|
-
ops.setScope(o, { name: v.name, domain: v.domain, purpose: v.purpose, users: v.users, maintainers: v.maintainers, outOfScope: v["out-of-scope"], kind: v.kind });
|
|
121
|
-
o.save();
|
|
122
|
-
return o;
|
|
123
|
-
})
|
|
124
|
-
: load(v);
|
|
125
|
-
const m = ont.meta;
|
|
126
|
-
const row = (k, val) => console.log(` ${c.dim(k.padEnd(13))} ${Array.isArray(val) ? val.join(", ") || c.yellow("—") : val || c.yellow("—")}`);
|
|
127
|
-
return output(v, () => m, () => {
|
|
128
|
-
console.log(c.bold(m.name));
|
|
129
|
-
row("domain", m.domain);
|
|
130
|
-
row("purpose", m.purpose);
|
|
131
|
-
row("users", m.users);
|
|
132
|
-
row("maintainers", m.maintainers);
|
|
133
|
-
row("out of scope", m.outOfScope);
|
|
134
|
-
row("kind", m.kind);
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
case "convention": {
|
|
138
|
-
const { v } = parse(rest, { "class-case": str, "slot-case": str, "instance-case": str, number: str, "slot-affix": str });
|
|
139
|
-
mutate(v, (ont) => {
|
|
140
|
-
const notes = ops.setConventions(ont, { classCase: v["class-case"], slotCase: v["slot-case"], instanceCase: v["instance-case"], classNumber: v.number, slotAffix: v["slot-affix"] });
|
|
141
|
-
const cv = ont.conventions;
|
|
142
|
-
return [`Naming convention: classes ${cv.classCase} (${cv.classNumber}) · slots ${cv.slotCase}${cv.slotAffix !== "none" ? ` (${cv.slotAffix})` : ""} · instances ${cv.instanceCase ?? "free text"}`, ...notes];
|
|
143
|
-
});
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
case "cq": {
|
|
147
|
-
if (sub === "add") {
|
|
148
|
-
const { v, p } = parse(rest.slice(1));
|
|
149
|
-
if (!p.length)
|
|
150
|
-
throw new OkbError('Usage: okb cq add "Your question?"');
|
|
151
|
-
return mutate(v, (ont) => p.flatMap((t) => ops.addCQ(ont, t)));
|
|
152
|
-
}
|
|
153
|
-
if (sub === "link" || sub === "unlink") {
|
|
154
|
-
const { v, p } = parse(rest.slice(1));
|
|
155
|
-
if (p.length < 2)
|
|
156
|
-
throw new OkbError(`Usage: okb cq ${sub} <question number> <Class|slot|instance>...`);
|
|
157
|
-
return mutate(v, (ont) => ops.linkCQ(ont, p[0], p.slice(1), sub === "unlink"));
|
|
158
|
-
}
|
|
159
|
-
if (sub === "set") {
|
|
160
|
-
const { v, p } = parse(rest.slice(1), { text: str, status: str });
|
|
161
|
-
return mutate(v, (ont) => ops.setCQ(ont, p[0], { text: v.text, status: v.status }));
|
|
162
|
-
}
|
|
163
|
-
if (sub === "remove") {
|
|
164
|
-
const { v, p } = parse(rest.slice(1));
|
|
165
|
-
return mutate(v, (ont) => ops.remove(ont, p[0], ["CompetencyQuestion"]));
|
|
166
|
-
}
|
|
167
|
-
const { v } = parse(sub === "list" ? rest.slice(1) : rest);
|
|
168
|
-
const ont = load(v);
|
|
169
|
-
const qs = ont.numbered("CompetencyQuestion");
|
|
170
|
-
return output(v, () => qs.map((q) => ({ ...q, needs: ont.targets(q.id, "NEEDS") })), () => {
|
|
171
|
-
if (!qs.length)
|
|
172
|
-
return void console.log(c.dim('No competency questions yet. okb cq add "..."'));
|
|
173
|
-
for (const q of qs) {
|
|
174
|
-
const needs = ont.targets(q.id, "NEEDS").map((n) => ont.label(n));
|
|
175
|
-
const num = ont.displayNumber(q);
|
|
176
|
-
console.log(`${c.bold(String(num).padEnd(3))} ${q.text} ${c.dim(`[${q.status ?? "draft"}]`)}`);
|
|
177
|
-
console.log(c.dim(` needs: ${needs.length ? needs.join(", ") : "(not linked yet: okb cq link " + num + " <Class|slot>...)"}`));
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
case "term": {
|
|
182
|
-
if (sub === "add") {
|
|
183
|
-
const { v, p } = parse(rest.slice(1), { note: str });
|
|
184
|
-
if (!p.length)
|
|
185
|
-
throw new OkbError("Usage: okb term add <term> [<term>...] (quote multi-word terms)");
|
|
186
|
-
return mutate(v, (ont) => ops.addTerms(ont, p, v.note));
|
|
187
|
-
}
|
|
188
|
-
if (sub === "set") {
|
|
189
|
-
const { v, p } = parse(rest.slice(1), { as: str, node: str, note: str });
|
|
190
|
-
if (!p[0] || !v.as)
|
|
191
|
-
throw new OkbError("Usage: okb term set <term> --as class|slot|instance|value|synonym|out-of-scope [--node Name]");
|
|
192
|
-
return mutate(v, (ont) => ops.setTerm(ont, p[0], v.as, v.node, v.note));
|
|
193
|
-
}
|
|
194
|
-
const { v } = parse(sub === "list" ? rest.slice(1) : rest);
|
|
195
|
-
const ont = load(v);
|
|
196
|
-
const ts = ont.ofType("Term");
|
|
197
|
-
return output(v, () => ts, () => {
|
|
198
|
-
const groups = new Map();
|
|
199
|
-
for (const t of ts) {
|
|
200
|
-
const d = t.disposition ?? "undecided";
|
|
201
|
-
const became = ont.targets(t.id, "BECAME").map((x) => ont.label(x));
|
|
202
|
-
groups.set(d, [...(groups.get(d) ?? []), t.text + (became.length ? c.dim(` → ${became.join(", ")}`) : "")]);
|
|
203
|
-
}
|
|
204
|
-
for (const [d, items] of groups)
|
|
205
|
-
console.log(`${c.bold(d.padEnd(13))} ${items.join(" · ")}`);
|
|
206
|
-
if (!ts.length)
|
|
207
|
-
console.log(c.dim("No terms yet. okb term add wine grape winery ..."));
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
case "reuse": {
|
|
211
|
-
if (sub === "add") {
|
|
212
|
-
const { v } = parse(rest.slice(1), { name: str, url: str, decision: str, notes: str });
|
|
213
|
-
if (!v.name || !v.decision)
|
|
214
|
-
throw new OkbError("Usage: okb reuse add --name ... --decision reuse|adapt|reference|rejected [--url ...] [--notes ...]");
|
|
215
|
-
return mutate(v, (ont) => ops.addReuse(ont, { name: v.name, url: v.url, decision: v.decision, notes: v.notes }));
|
|
216
|
-
}
|
|
217
|
-
if (sub === "none") {
|
|
218
|
-
const { v } = parse(rest.slice(1), { why: str });
|
|
219
|
-
if (!v.why)
|
|
220
|
-
throw new OkbError('Usage: okb reuse none --why "what you looked at and why nothing fit"');
|
|
221
|
-
return mutate(v, (ont) => ops.reuseNone(ont, v.why));
|
|
222
|
-
}
|
|
223
|
-
const { v } = parse(sub === "list" ? rest.slice(1) : rest);
|
|
224
|
-
const ont = load(v);
|
|
225
|
-
for (const r of ont.ofType("ReusedOntology"))
|
|
226
|
-
console.log(`${c.bold(r.name)} ${c.dim(`[${r.decision}]`)} ${r.url ?? ""} ${r.notes ? "\n " + r.notes : ""}`);
|
|
227
|
-
if (!ont.ofType("ReusedOntology").length)
|
|
228
|
-
console.log(c.dim(ont.meta.reuseReviewed ? "Reviewed: nothing reused." : "Not reviewed yet."));
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
case "class": {
|
|
232
|
-
const rest2 = rest.slice(1);
|
|
233
|
-
if (sub === "add") {
|
|
234
|
-
const { v, p } = parse(rest2, { parent: strs, desc: str, synonym: strs, abstract: bool, terminological: bool, force: bool });
|
|
235
|
-
if (!p[0])
|
|
236
|
-
throw new OkbError("Usage: okb class add <Name> [--parent P] [--desc ...]");
|
|
237
|
-
return mutate(v, (ont) => ops.addClass(ont, p[0], { parents: v.parent, description: v.desc, synonyms: v.synonym, abstract: v.abstract, terminological: v.terminological, force: v.force }));
|
|
238
|
-
}
|
|
239
|
-
if (sub === "set") {
|
|
240
|
-
const { v, p } = parse(rest2, { desc: str, "add-parent": strs, "remove-parent": strs, synonym: strs, abstract: str, terminological: str });
|
|
241
|
-
if (!p[0])
|
|
242
|
-
throw new OkbError("Usage: okb class set <Class> [--desc ...] [--add-parent P] [--remove-parent P] [--synonym S] [--abstract true|false]");
|
|
243
|
-
return mutate(v, (ont) => ops.setClass(ont, p[0], { description: v.desc, addParents: v["add-parent"], removeParents: v["remove-parent"], synonyms: v.synonym, abstract: tri(v.abstract), terminological: tri(v.terminological) }));
|
|
244
|
-
}
|
|
245
|
-
if (sub === "disjoint") {
|
|
246
|
-
const { v, p } = parse(rest2);
|
|
247
|
-
return mutate(v, (ont) => ops.disjoint(ont, p));
|
|
248
|
-
}
|
|
249
|
-
if (sub === "fix") {
|
|
250
|
-
const { v, p } = parse(rest2);
|
|
251
|
-
if (p.length < 2)
|
|
252
|
-
throw new OkbError("Usage: okb class fix <Class> slot=value ...");
|
|
253
|
-
return mutate(v, (ont) => ops.assign(ont, ont.find(p[0], "Class").id, p.slice(1), "fixed"));
|
|
254
|
-
}
|
|
255
|
-
if (sub === "default") {
|
|
256
|
-
const { v, p } = parse(rest2);
|
|
257
|
-
if (p.length < 2)
|
|
258
|
-
throw new OkbError("Usage: okb class default <Class> slot=value ...");
|
|
259
|
-
return mutate(v, (ont) => ops.classDefault(ont, p[0], p.slice(1)));
|
|
260
|
-
}
|
|
261
|
-
if (sub === "restrict") {
|
|
262
|
-
const { v, p } = parse(rest2, { values: strs, min: str, max: str, range: strs, clear: bool });
|
|
263
|
-
if (p.length < 2)
|
|
264
|
-
throw new OkbError("Usage: okb class restrict <Class> <slot> [--values a,b] [--min N] [--max N] [--range C] [--clear]");
|
|
265
|
-
return mutate(v, (ont) => ops.restrict(ont, p[0], p[1], { values: v.values, min: num(v.min, "--min"), max: num(v.max, "--max"), range: v.range, clear: v.clear }));
|
|
266
|
-
}
|
|
267
|
-
throw new OkbError("Usage: okb class add|set|disjoint|fix|default|restrict ... (okb help)");
|
|
268
|
-
}
|
|
269
|
-
case "slot": {
|
|
270
|
-
const rest2 = rest.slice(1);
|
|
271
|
-
const slotOpts = { on: strs, off: strs, type: str, values: strs, range: strs, "add-range": strs, "remove-range": strs, card: str, min: str, max: str, default: str, desc: str, force: bool, "on-rules": bool, "off-rules": bool };
|
|
272
|
-
const toOpts = (v) => ({
|
|
273
|
-
on: v.on, off: v.off, type: v.type, values: v.values, range: v.range, addRange: v["add-range"], removeRange: v["remove-range"],
|
|
274
|
-
card: v.card, min: num(v.min, "--min"), max: num(v.max, "--max"), default: v.default, description: v.desc, force: v.force, onRules: v["on-rules"], offRules: v["off-rules"],
|
|
275
|
-
});
|
|
276
|
-
if (sub === "add") {
|
|
277
|
-
const { v, p } = parse(rest2, slotOpts);
|
|
278
|
-
if (!p[0])
|
|
279
|
-
throw new OkbError("Usage: okb slot add <name> --on <Class> --type <Type> [--card single|multiple] ...");
|
|
280
|
-
return mutate(v, (ont) => ops.addSlot(ont, p[0], toOpts(v)));
|
|
281
|
-
}
|
|
282
|
-
if (sub === "set") {
|
|
283
|
-
const { v, p } = parse(rest2, slotOpts);
|
|
284
|
-
if (!p[0])
|
|
285
|
-
throw new OkbError("Usage: okb slot set <slot> [--on C] [--type T] [--values a,b] [--range C] [--card single|multiple] ...");
|
|
286
|
-
return mutate(v, (ont) => ops.updateSlot(ont, p[0], toOpts(v)));
|
|
287
|
-
}
|
|
288
|
-
if (sub === "inverse") {
|
|
289
|
-
const { v, p } = parse(rest2);
|
|
290
|
-
if (p.length !== 2)
|
|
291
|
-
throw new OkbError("Usage: okb slot inverse <slotA> <slotB>");
|
|
292
|
-
return mutate(v, (ont) => ops.inverse(ont, p[0], p[1]));
|
|
293
|
-
}
|
|
294
|
-
throw new OkbError("Usage: okb slot add|set|inverse ... (okb help)");
|
|
295
|
-
}
|
|
296
|
-
case "instance": {
|
|
297
|
-
const rest2 = rest.slice(1);
|
|
298
|
-
if (sub === "add") {
|
|
299
|
-
const { v, p } = parse(rest2, { of: strs, desc: str });
|
|
300
|
-
if (!p[0])
|
|
301
|
-
throw new OkbError('Usage: okb instance add "Name" --of <Class> [slot=value ...]');
|
|
302
|
-
return mutate(v, (ont) => ops.addInstance(ont, p[0], { of: v.of, description: v.desc, assignments: p.slice(1) }));
|
|
303
|
-
}
|
|
304
|
-
if (sub === "set") {
|
|
305
|
-
const { v, p } = parse(rest2, { of: strs, desc: str });
|
|
306
|
-
if (!p[0])
|
|
307
|
-
throw new OkbError('Usage: okb instance set "Name" slot=value ... [--of <Class>]');
|
|
308
|
-
return mutate(v, (ont) => {
|
|
309
|
-
const notes = [
|
|
310
|
-
...(v.of?.length ? ops.setInstanceClasses(ont, p[0], v.of) : []),
|
|
311
|
-
...(p.length > 1 ? ops.assign(ont, ont.find(p[0], "Instance").id, p.slice(1)) : []),
|
|
312
|
-
];
|
|
313
|
-
if (v.desc !== undefined) {
|
|
314
|
-
notes.push(...ops.setInstanceDescription(ont, p[0], v.desc));
|
|
315
|
-
}
|
|
316
|
-
return notes;
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
throw new OkbError("Usage: okb instance add|set ... (okb help)");
|
|
320
|
-
}
|
|
321
|
-
case "decision": {
|
|
322
|
-
if (sub === "add") {
|
|
323
|
-
const { v } = parse(rest.slice(1), { title: str, decision: str, question: str, why: str, alt: strs, about: strs, waives: strs });
|
|
324
|
-
if (!v.title || !v.decision)
|
|
325
|
-
throw new OkbError('Usage: okb decision add --title "..." --decision "..." --why "..." [--about X] [--waives rule-id]');
|
|
326
|
-
return mutate(v, (ont) => ops.addDecision(ont, { title: v.title, decision: v.decision, question: v.question, rationale: v.why, alternatives: v.alt, about: v.about, waives: v.waives }));
|
|
327
|
-
}
|
|
328
|
-
const { v } = parse(sub === "list" ? rest.slice(1) : rest);
|
|
329
|
-
const ont = load(v);
|
|
330
|
-
const ds = ont.numbered("DesignDecision");
|
|
331
|
-
return output(v, () => ds, () => {
|
|
332
|
-
for (const d of ds) {
|
|
333
|
-
console.log(`${c.bold(String(ont.displayNumber(d)).padEnd(3))} ${d.title}`);
|
|
334
|
-
console.log(` ${d.decision}${d.rationale ? c.dim(" · why: " + d.rationale) : ""}`);
|
|
335
|
-
const about = ont.targets(d.id, "ABOUT").map((x) => ont.label(x));
|
|
336
|
-
if (about.length || d.metaRules)
|
|
337
|
-
console.log(c.dim(` about: ${about.join(", ") || "—"}${d.metaRules ? " · explains: " + d.metaRules.join(", ") : ""}`));
|
|
338
|
-
}
|
|
339
|
-
if (!ds.length)
|
|
340
|
-
console.log(c.dim("No design decisions recorded yet."));
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
case "rename": {
|
|
344
|
-
const { v, p } = parse(rest, { force: bool });
|
|
345
|
-
if (p.length !== 2)
|
|
346
|
-
throw new OkbError("Usage: okb rename <old name> <new name>");
|
|
347
|
-
return mutate(v, (ont) => ops.rename(ont, p[0], p[1], v.force));
|
|
348
|
-
}
|
|
349
|
-
case "remove": {
|
|
350
|
-
const { v, p } = parse(rest, { type: str });
|
|
351
|
-
if (!p[0])
|
|
352
|
-
throw new OkbError("Usage: okb remove <name> [--type Class|Slot|Instance|Term|...]");
|
|
353
|
-
return mutate(v, (ont) => ops.remove(ont, p[0], v.type ? [v.type] : undefined));
|
|
354
|
-
}
|
|
355
|
-
case "show": {
|
|
356
|
-
const { v, p } = parse(rest);
|
|
357
|
-
const ont = load(v);
|
|
358
|
-
const n = ont.find(p[0] ?? "ontology");
|
|
359
|
-
return output(v, () => ({ node: n, out: ont.edges.filter((e) => e.from === n.id), in: ont.edges.filter((e) => e.to === n.id) }), () => console.log(show(ont, n)));
|
|
360
|
-
}
|
|
361
|
-
case "tree": {
|
|
362
|
-
const { v } = parse(rest, { instances: bool });
|
|
363
|
-
console.log(tree(load(v), v.instances));
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
case "diagram": {
|
|
367
|
-
const { v } = parse(rest, { out: str });
|
|
368
|
-
const md = mermaid(load(v));
|
|
369
|
-
if (v.out) {
|
|
370
|
-
writeFileSync(resolve(v.out), md + "\n");
|
|
371
|
-
console.log(`Wrote ${v.out}. Open it in GitHub, VS Code (Markdown preview), or mermaid.live.`);
|
|
372
|
-
}
|
|
373
|
-
else
|
|
374
|
-
console.log(md);
|
|
375
|
-
return;
|
|
376
|
-
}
|
|
377
|
-
case "validate": {
|
|
378
|
-
const { v } = parse(rest, { all: bool, why: bool, rule: strs, strict: bool });
|
|
379
|
-
const ont = load(v);
|
|
380
|
-
const { findings, hiddenLater } = runChecks(ont, { all: v.all, only: v.rule }, { pages: await loadSourcePages(ont) });
|
|
381
|
-
output(v, () => ({ step: ont.step, metaKbVersion: MetaKB.get().version, hiddenLater, findings }), () => {
|
|
382
|
-
console.log(formatFindings(findings, ont, { all: v.all, verbose: v.why }));
|
|
383
|
-
if (hiddenLater)
|
|
384
|
-
console.log(c.dim(`(${hiddenLater} warning(s)/hint(s) from later steps are hidden until you get there; okb validate --all shows them. Errors are never hidden.)`));
|
|
385
|
-
});
|
|
386
|
-
const open = findings.filter((f) => !f.explainedBy);
|
|
387
|
-
const fail = open.some((f) => f.severity === "error") || (v.strict && open.some((f) => f.severity === "warning"));
|
|
388
|
-
process.exitCode = fail ? 1 : 0;
|
|
389
|
-
return;
|
|
390
|
-
}
|
|
391
|
-
case "status": {
|
|
392
|
-
const { v } = parse(rest);
|
|
393
|
-
const ont = load(v);
|
|
394
|
-
const { findings } = runChecks(ont, { all: true }, { pages: await loadSourcePages(ont) });
|
|
395
|
-
const st = computeStatus(ont, findings);
|
|
396
|
-
return output(v, () => ({ currentStep: ont.step, ...st }), () => {
|
|
397
|
-
const meta = MetaKB.get();
|
|
398
|
-
const shown = findings.filter((f) => !f.explainedBy && (f.severity === "error" || (meta.rule(f.rule).fromStep ?? 1) <= ont.step));
|
|
399
|
-
const count = (sev) => shown.filter((f) => f.severity === sev).length;
|
|
400
|
-
console.log(c.bold(ont.meta.name) + c.dim(` · ${ont.ofType("Class").length} classes · ${ont.ofType("Slot").length} slots · ${ont.ofType("Instance").length} instances · ${ont.ofType("CompetencyQuestion").length} questions`));
|
|
401
|
-
console.log("");
|
|
402
|
-
for (const s of st.steps) {
|
|
403
|
-
const here = s.order === ont.step ? c.cyan(" ◀ you are here") : "";
|
|
404
|
-
const mark = s.complete ? SYMBOL.ok : s.order === ont.step ? c.cyan("▶") : SYMBOL.todo;
|
|
405
|
-
console.log(`${mark} ${c.bold(`${s.order}. ${s.name}`)}${here}`);
|
|
406
|
-
if (s.order === ont.step || (!s.complete && s.order < ont.step)) {
|
|
407
|
-
for (const it of s.items)
|
|
408
|
-
console.log(` ${it.done === null ? SYMBOL.ask : it.done ? SYMBOL.ok : SYMBOL.todo} ${it.text}`);
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
console.log("");
|
|
412
|
-
console.log(`Validator (steps 1–${ont.step}): ${SYMBOL.error} ${count("error")} ${SYMBOL.warning} ${count("warning")} ${SYMBOL.info} ${count("info")} ${c.dim("okb validate for details")}`);
|
|
413
|
-
const cur = st.steps.find((s) => s.order === ont.step);
|
|
414
|
-
if (cur.complete && ont.step < st.steps.length)
|
|
415
|
-
console.log(c.green(`\nStep ${ont.step} looks done. When you're happy with it: okb step next`));
|
|
416
|
-
else if (st.suggested < ont.step)
|
|
417
|
-
console.log(c.yellow(`\nStep ${st.suggested} still has open items. It's fine to go back: okb step ${st.suggested}`));
|
|
418
|
-
else
|
|
419
|
-
console.log(c.dim(`\nSee the guide for this step: okb step`));
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
case "step": {
|
|
423
|
-
const { v, p } = parse(rest);
|
|
424
|
-
const meta = MetaKB.get();
|
|
425
|
-
if (p[0]) {
|
|
426
|
-
const { target, leaving } = Ontology.edit(v.C ?? ".", (ont) => {
|
|
427
|
-
const target = p[0] === "next" ? ont.step + 1 : p[0] === "prev" ? ont.step - 1 : Number(p[0]);
|
|
428
|
-
if (!meta.step(target))
|
|
429
|
-
throw new OkbError(`There is no step ${p[0]}. Steps are 1–${meta.steps.length}.`);
|
|
430
|
-
const st = computeStatus(ont);
|
|
431
|
-
const leaving = st.steps.find((s) => s.order === ont.step);
|
|
432
|
-
ont.manifest.currentStep = target;
|
|
433
|
-
ont.save();
|
|
434
|
-
return { target, leaving };
|
|
435
|
-
});
|
|
436
|
-
if (target > leaving.order && !leaving.complete) {
|
|
437
|
-
console.log(c.yellow(`Moving on with step ${leaving.order} unfinished (${leaving.items.filter((i) => i.done === false).map((i) => i.text).join("; ")}). That's allowed; the method is iterative.`));
|
|
438
|
-
}
|
|
439
|
-
console.log(c.cyan(`Now on step ${target}.`) + c.dim(` (To read a step's guide without moving: okb explain ${target})`) + "\n");
|
|
440
|
-
console.log(stepGuide(meta.step(target)));
|
|
441
|
-
return;
|
|
442
|
-
}
|
|
443
|
-
let order = 1;
|
|
444
|
-
try {
|
|
445
|
-
order = load(v).step;
|
|
446
|
-
}
|
|
447
|
-
catch {
|
|
448
|
-
/* no ontology here: show step 1 */
|
|
449
|
-
}
|
|
450
|
-
console.log(stepGuide(meta.step(order)));
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
case "explain": {
|
|
454
|
-
const { v, p } = parse(rest, { search: str });
|
|
455
|
-
const meta = MetaKB.get();
|
|
456
|
-
if (v.search) {
|
|
457
|
-
const hits = meta.search(v.search.replace(/[-_]/g, " "));
|
|
458
|
-
if (!hits.length)
|
|
459
|
-
return void console.log(`Nothing mentions '${v.search}'.`);
|
|
460
|
-
for (const h of hits)
|
|
461
|
-
console.log(`${(h.type === "Rule" ? h.key : h.id).padEnd(40)} ${c.dim(h.type)} ${h.type === "Rule" ? h.statement.slice(0, 60) : h.name}`);
|
|
462
|
-
return;
|
|
463
|
-
}
|
|
464
|
-
if (!p.length)
|
|
465
|
-
return void console.log(listExplainable());
|
|
466
|
-
const q = p.join(" ");
|
|
467
|
-
const hits = meta.resolve(q);
|
|
468
|
-
return output(v, () => hits.map((h) => ({ ...h, citations: meta.citations(h.id), rationale: h.type === "Rule" ? meta.rationale(h) : undefined })), () => {
|
|
469
|
-
if (!hits.length)
|
|
470
|
-
return void console.log(`Nothing called '${q}'. Try: okb explain --search ${q}`);
|
|
471
|
-
console.log(explain(hits[0]));
|
|
472
|
-
if (hits.length > 1)
|
|
473
|
-
console.log(c.dim(`\nAlso matches: ${hits.slice(1, 6).map(explainName).join(", ")}`));
|
|
474
|
-
});
|
|
475
|
-
}
|
|
476
|
-
case "review": {
|
|
477
|
-
// The judgment rules a script can't check, turned into concrete questions about
|
|
478
|
-
// this ontology. Used by the ontology-review skill.
|
|
479
|
-
const { v } = parse(rest, { all: bool });
|
|
480
|
-
const ont = load(v);
|
|
481
|
-
const items = buildReview(ont, v.all);
|
|
482
|
-
const classes = () => ont.ofType("Class").map((cl) => ({
|
|
483
|
-
id: cl.id, name: cl.name, description: cl.description, parents: ont.parents(cl.id).map((x) => ont.label(x)),
|
|
484
|
-
children: ont.children(cl.id).map((x) => ont.label(x)), ownSlots: ont.ownSlots(cl.id).map((s) => ont.label(s)),
|
|
485
|
-
abstract: cl.abstract, terminological: cl.terminological, instances: ont.sources(cl.id, "INSTANCE_OF").length,
|
|
486
|
-
}));
|
|
487
|
-
return output(v, () => ({ step: ont.step, scope: ont.meta, judgmentRules: items, classes: classes(), decisions: ont.ofType("DesignDecision") }), () => {
|
|
488
|
-
console.log(c.bold(v.all ? "Review: what only a person can judge (all rules)" : `Review: what only a person can judge (step ${ont.step} and anything already visible)`) + "\n");
|
|
489
|
-
for (const it of items) {
|
|
490
|
-
console.log(`${SYMBOL.ask} ${c.bold(it.rule)} ${c.dim(`(${it.modality.replace("_", " ")})`)} ${it.question}`);
|
|
491
|
-
for (const f of it.flags)
|
|
492
|
-
console.log(` ${SYMBOL.warning} ${f}`);
|
|
493
|
-
for (const x of it.prompts.slice(0, REVIEW_PROMPTS_SHOWN))
|
|
494
|
-
console.log(` · ${x}`);
|
|
495
|
-
if (it.prompts.length > REVIEW_PROMPTS_SHOWN)
|
|
496
|
-
console.log(c.dim(` · ...and ${it.prompts.length - REVIEW_PROMPTS_SHOWN} more (okb review --json)`));
|
|
497
|
-
if (it.source)
|
|
498
|
-
console.log(c.dim(` ${it.source}`));
|
|
499
|
-
console.log("");
|
|
500
|
-
}
|
|
501
|
-
console.log(c.dim("Record answers that needed thought (or that someone might question later): okb decision add ..."));
|
|
502
|
-
});
|
|
503
|
-
}
|
|
504
|
-
case "edgeprop": {
|
|
505
|
-
const { v, p } = parse(rest.slice(1), { type: str, values: strs, required: bool, desc: str, remove: bool });
|
|
506
|
-
if (p.length !== 2)
|
|
507
|
-
throw new OkbError("Usage: okb relationship property <relationship> <name> --type String|Integer|Float|Boolean|Enumerated [--values a,b] [--required] [--desc ...] [--remove]");
|
|
508
|
-
return mutate(v, (ont) => ops.declareEdgeProperty(ont, p[0], p[1], { type: v.type, values: v.values, required: v.required, description: v.desc, remove: v.remove }));
|
|
509
|
-
}
|
|
510
|
-
case "link": {
|
|
511
|
-
const { v, p } = parse(rest, { rule: str, clear: strs });
|
|
512
|
-
if (p.length < 3)
|
|
513
|
-
throw new OkbError('Usage: okb link <from> <relationship> <to> [name=value ...] [--rule <rule id>] [--clear name]');
|
|
514
|
-
return mutate(v, (ont) => ops.link(ont, p[0], p[1], p[2], { props: p.slice(3), rule: v.rule, clear: v.clear }));
|
|
515
|
-
}
|
|
516
|
-
case "unlink": {
|
|
517
|
-
const { v, p } = parse(rest);
|
|
518
|
-
if (p.length !== 3)
|
|
519
|
-
throw new OkbError("Usage: okb unlink <from> <relationship> <to>");
|
|
520
|
-
return mutate(v, (ont) => ops.unlink(ont, p[0], p[1], p[2]));
|
|
521
|
-
}
|
|
522
|
-
case "export": {
|
|
523
|
-
const { v } = parse(rest, { format: str, out: str, "with-schema": bool, "no-inherited": bool, "no-rules": bool, "include-unverified": bool });
|
|
524
|
-
const ont = load(v);
|
|
525
|
-
const fmt = v.format ?? (v.out?.endsWith(".cypher") || v.out?.endsWith(".cql") ? "cypher" : "json");
|
|
526
|
-
if (!["json", "cypher"].includes(fmt))
|
|
527
|
-
throw new OkbError("--format must be json or cypher.");
|
|
528
|
-
const g = exportGraph(ont, { inherited: !v["no-inherited"], schema: v["with-schema"], rules: !v["no-rules"], unverified: v["include-unverified"] });
|
|
529
|
-
const text = fmt === "cypher" ? toCypher(g) : JSON.stringify(g, null, 2) + "\n";
|
|
530
|
-
if (v.out) {
|
|
531
|
-
writeFileSync(resolve(v.out), text);
|
|
532
|
-
console.log(`Wrote ${v.out}: ${g.nodes.length} nodes, ${g.relationships.length} relationships (${fmt}).`);
|
|
533
|
-
for (const n of g.notes)
|
|
534
|
-
console.log(c.dim(" " + n));
|
|
535
|
-
}
|
|
536
|
-
else
|
|
537
|
-
process.stdout.write(text);
|
|
538
|
-
return;
|
|
539
|
-
}
|
|
540
|
-
case "source": {
|
|
541
|
-
const rest2 = rest.slice(1);
|
|
542
|
-
if (sub === "add") {
|
|
543
|
-
const { v, p } = parse(rest2, { title: str, url: str, "repo-url": str, id: str });
|
|
544
|
-
if (!p[0])
|
|
545
|
-
throw new OkbError("Usage: okb source add <file.md|file.pdf> [--title ...] [--repo-url <GitHub file URL>] [--url <rendered page URL>]");
|
|
546
|
-
return mutate(v, (ont) => prov.addSource(ont, p[0], { title: v.title, url: v.url, repoUrl: v["repo-url"], id: v.id }));
|
|
547
|
-
}
|
|
548
|
-
if (sub === "refresh") {
|
|
549
|
-
const { v, p } = parse(rest2, { all: bool, record: bool, "dry-run": bool, strict: bool });
|
|
550
|
-
if (!v.all)
|
|
551
|
-
return mutate(v, (ont) => prov.refreshSource(ont, p[0]));
|
|
552
|
-
if (p.length)
|
|
553
|
-
throw new OkbError("--all re-checks every source; leave out the source name (or drop --all).");
|
|
554
|
-
const ont = load(v);
|
|
555
|
-
const pages = await loadSourcePages(ont);
|
|
556
|
-
const o = { write: !v["dry-run"], record: !!v.record && !v["dry-run"] };
|
|
557
|
-
let report = resync(ont, pages, { write: false, record: false });
|
|
558
|
-
// Take the lock only when something has to be written.
|
|
559
|
-
if (o.write && (report.moved.length || o.record)) {
|
|
560
|
-
report = Ontology.edit(v.C ?? ".", (locked) => {
|
|
561
|
-
const r = locked.tick(() => resync(locked, pages, o));
|
|
562
|
-
locked.save();
|
|
563
|
-
return r;
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
|
-
output(v, () => report, () => say(renderResync(report, { dryRun: !o.write })));
|
|
567
|
-
// 1 when the KB no longer matches its documents (a quote is gone or a source can't be read); --strict also when blocks need work.
|
|
568
|
-
if (!report.ok || (v.strict && report.worklist.length))
|
|
569
|
-
process.exitCode = 1;
|
|
570
|
-
return;
|
|
571
|
-
}
|
|
572
|
-
if (sub === "outline") {
|
|
573
|
-
const { v, p } = parse(rest2, { quotable: bool, problems: bool });
|
|
574
|
-
const ont = load(v);
|
|
575
|
-
const src = ont.find(p[0], "Source");
|
|
576
|
-
if (src.format !== "markdown")
|
|
577
|
-
throw new OkbError("outline works on markdown sources.");
|
|
578
|
-
const doc = prov.readMarkdownSource(ont, src);
|
|
579
|
-
const quoted = new Map();
|
|
580
|
-
for (const id of ont.sources(src.id, "PART_OF")) {
|
|
581
|
-
const l = ont.get(id);
|
|
582
|
-
if (l?.type !== "SourceLocation" || l.startLine === undefined)
|
|
583
|
-
continue;
|
|
584
|
-
const { startLine, endLine } = l;
|
|
585
|
-
for (let n = startLine; n <= (endLine ?? startLine); n++)
|
|
586
|
-
quoted.set(n, [...(quoted.get(n) ?? []), id]);
|
|
587
|
-
}
|
|
588
|
-
const blocks = doc.blocks.filter((b) => !v.quotable || b.quotable);
|
|
589
|
-
const outline = () => ({
|
|
590
|
-
source: src.id, title: doc.title, frontmatter: doc.frontmatter, problems: doc.problems,
|
|
591
|
-
blocks: blocks.map((b) => ({ index: b.index, kind: b.kind, headingPath: b.headingPath, lines: [b.startLine, b.endLine], quotable: b.quotable, note: b.note, calloutType: b.calloutType, row: b.row, text: b.kind === "code" ? undefined : b.text, alreadyQuoted: [...new Set(Array.from({ length: b.endLine - b.startLine + 1 }, (_, k) => quoted.get(b.startLine + k) ?? []).flat())] })),
|
|
592
|
-
});
|
|
593
|
-
return output(v, outline, () => {
|
|
594
|
-
console.log(c.bold(`${doc.title ?? src.id}`) + c.dim(` ${src.localPath} · ${doc.blocks.length} blocks, ${doc.blocks.filter((b) => b.quotable).length} quotable`));
|
|
595
|
-
if (doc.problems.length) {
|
|
596
|
-
console.log(c.yellow(`\n${doc.problems.length} problem(s) in the document:`));
|
|
597
|
-
for (const pr of doc.problems)
|
|
598
|
-
console.log(c.yellow(` line ${pr.line}: ${pr.message}`));
|
|
599
|
-
}
|
|
600
|
-
if (v.problems)
|
|
601
|
-
return;
|
|
602
|
-
let lastPath = "";
|
|
603
|
-
for (const b of blocks) {
|
|
604
|
-
const pth = b.headingPath.join(" > ");
|
|
605
|
-
if (pth !== lastPath) {
|
|
606
|
-
console.log("\n" + c.bold(pth || "(top)"));
|
|
607
|
-
lastPath = pth;
|
|
608
|
-
}
|
|
609
|
-
const mark = !b.quotable ? c.dim("·") : quoted.has(b.startLine) ? SYMBOL.ok : SYMBOL.todo;
|
|
610
|
-
const where = `L${b.startLine}${b.endLine > b.startLine ? `-${b.endLine}` : ""}`.padEnd(9);
|
|
611
|
-
const label = b.kind === "code" ? c.dim(`code (${b.lang || "text"}), not quotable`) : b.kind === "comment" ? c.dim("comment") : b.kind === "frontmatter" ? c.dim("frontmatter") : (b.calloutType ? c.cyan(`[${b.calloutType}] `) : "") + b.text.slice(0, 110) + (b.text.length > 110 ? "…" : "");
|
|
612
|
-
console.log(` ${mark} ${c.dim(where)} ${c.dim(b.kind.padEnd(10))} ${label}${b.note?.startsWith("malformed") ? c.yellow(" ⚠ " + b.note) : ""}`);
|
|
613
|
-
}
|
|
614
|
-
console.log(c.dim(`\n${SYMBOL.ok} quoted ${SYMBOL.todo} not quoted yet · not quotable · okb quote add ${src.id} "..."`));
|
|
615
|
-
});
|
|
616
|
-
}
|
|
617
|
-
const { v } = parse(sub === "list" ? rest2 : rest);
|
|
618
|
-
const ont = load(v);
|
|
619
|
-
for (const s of ont.ofType("Source")) {
|
|
620
|
-
console.log(`${c.bold(s.id)} ${s.title} ${c.dim(`(${s.format ?? "?"}, ${ont.sources(s.id, "PART_OF").length} quotes)`)} ${c.dim(s.localPath ?? s.url ?? "")}`);
|
|
621
|
-
}
|
|
622
|
-
if (!ont.ofType("Source").length)
|
|
623
|
-
console.log(c.dim("No sources yet. okb source add <file>"));
|
|
624
|
-
return;
|
|
625
|
-
}
|
|
626
|
-
case "quote": {
|
|
627
|
-
if (sub === "add") {
|
|
628
|
-
const { v, p } = parse(rest.slice(1), { cites: strs, line: str, "allow-code": bool });
|
|
629
|
-
if (p.length < 2)
|
|
630
|
-
throw new OkbError('Usage: okb quote add <source> "verbatim text" [--cites <node>] [--line N]');
|
|
631
|
-
const notes = await Ontology.edit(v.C ?? ".", async (ont) => {
|
|
632
|
-
const notes = await prov.addQuote(ont, p[0], p.slice(1).join(" "), { cites: v.cites, line: num(v.line, "--line"), allowCode: v["allow-code"] });
|
|
633
|
-
ont.save();
|
|
634
|
-
return notes;
|
|
635
|
-
});
|
|
636
|
-
return void say(notes);
|
|
637
|
-
}
|
|
638
|
-
const { v, p } = parse(sub === "list" ? rest.slice(1) : rest);
|
|
639
|
-
const ont = load(v);
|
|
640
|
-
const locs = ont.ofType("SourceLocation").filter((l) => !p[0] || ont.targets(l.id, "PART_OF").includes(ont.find(p[0], "Source").id));
|
|
641
|
-
return output(v, () => locs.map((l) => ({ ...l, citedBy: ont.sources(l.id, "CITES") })), () => {
|
|
642
|
-
for (const l of locs) {
|
|
643
|
-
const by = ont.sources(l.id, "CITES").map((x) => ont.label(x));
|
|
644
|
-
console.log(`${c.bold(l.id)} ${c.dim(`${l.locator}${l.lines ? `, line ${l.lines}` : ""}`)}\n "${l.quote}"${by.length ? c.dim(`\n cited by: ${by.join(", ")}`) : ""}`);
|
|
645
|
-
}
|
|
646
|
-
});
|
|
647
|
-
}
|
|
648
|
-
case "rule": {
|
|
649
|
-
if (sub === "add") {
|
|
650
|
-
const { v } = parse(rest.slice(1), { statement: str, modality: str, governs: strs, cites: strs, name: str, authored: bool, value: strs, refines: strs, adds: str });
|
|
651
|
-
if (!v.statement || !v.modality)
|
|
652
|
-
throw new OkbError('Usage: okb rule add --statement "..." --modality MUST|SHOULD|MAY|MUST_NOT|SHOULD_NOT --cites <quote id> [--governs <Class|slot>] [--authored] [--value slot=value ...] [--refines <rule> [--adds "..."]]');
|
|
653
|
-
return mutate(v, (ont) => prov.addRule(ont, { statement: v.statement, modality: v.modality, governs: v.governs, cites: v.cites, name: v.name, authored: v.authored, values: v.value, refines: v.refines, adds: v.adds }));
|
|
654
|
-
}
|
|
655
|
-
if (sub === "refines") {
|
|
656
|
-
const { v, p } = parse(rest.slice(1), { adds: str, remove: bool });
|
|
657
|
-
if (p.length !== 2)
|
|
658
|
-
throw new OkbError('Usage: okb rule refines <rule> <broader rule> [--adds "..."] [--remove]');
|
|
659
|
-
return mutate(v, (ont) => prov.refine(ont, p[0], p[1], { adds: v.adds, remove: v.remove }));
|
|
660
|
-
}
|
|
661
|
-
const { v } = parse(sub === "list" ? rest.slice(1) : rest);
|
|
662
|
-
const ont = load(v);
|
|
663
|
-
for (const r of ont.ofType("Rule")) {
|
|
664
|
-
const st = r.verification?.status ?? "unverified";
|
|
665
|
-
console.log(`${c.bold(r.id)} ${c.dim(`[${r.modality} · ${r.authored ? "authored · " : ""}${st}${r.extractionConfidence ? " · " + r.extractionConfidence : ""}]`)}\n ${r.statement}${c.dim(`\n governs: ${ont.targets(r.id, "GOVERNS").map((x) => ont.label(x)).join(", ") || "—"} · cites: ${ont.targets(r.id, "CITES").join(", ")}`)}`);
|
|
666
|
-
}
|
|
667
|
-
return;
|
|
668
|
-
}
|
|
669
|
-
case "cite": {
|
|
670
|
-
const { v, p } = parse(rest);
|
|
671
|
-
if (p.length !== 2)
|
|
672
|
-
throw new OkbError("Usage: okb cite <node> <quote id> (attach an existing quote; see okb quote list)");
|
|
673
|
-
return mutate(v, (ont) => prov.cite(ont, p[0], p[1]));
|
|
674
|
-
}
|
|
675
|
-
case "fulfills": {
|
|
676
|
-
const { v, p } = parse(rest, { remove: bool });
|
|
677
|
-
if (p.length !== 2)
|
|
678
|
-
throw new OkbError("Usage: okb fulfills <instance> <rule> [--remove]");
|
|
679
|
-
return mutate(v, (ont) => prov.fulfill(ont, p[0], p[1], { remove: v.remove }));
|
|
680
|
-
}
|
|
681
|
-
case "verify": {
|
|
682
|
-
const { v, p } = parse(rest, { status: str, corrected: str, confidence: str, note: str, ref: str, approve: bool });
|
|
683
|
-
if (!p[0])
|
|
684
|
-
throw new OkbError('Usage: okb verify <node> --status SUPPORTED|OVERREACH|UNSUPPORTED [--corrected "..."] [--confidence verbatim|paraphrased] [--note "..."] [--ref <url>] · okb verify <node> --approve');
|
|
685
|
-
return mutate(v, (ont) => prov.verify(ont, p[0], { status: v.status, corrected: v.corrected, confidence: v.confidence, note: v.note, ref: v.ref, approve: v.approve }));
|
|
686
|
-
}
|
|
687
|
-
case "drift": {
|
|
688
|
-
const { v, p } = parse(rest);
|
|
689
|
-
const onts = (p.length ? p : ["."]).map((d) => Ontology.load(d));
|
|
690
|
-
const merged = Ontology.fromData(onts.flatMap((o) => o.nodes.filter((n) => n.type !== "Ontology")), onts.flatMap((o) => o.edges));
|
|
691
|
-
const hits = duplicateQuotes(merged);
|
|
692
|
-
return output(v, () => hits, () => {
|
|
693
|
-
if (!hits.length)
|
|
694
|
-
return void console.log(`${SYMBOL.ok} No near-duplicate quotes across different sources.`);
|
|
695
|
-
for (const h of hits)
|
|
696
|
-
console.log(`${SYMBOL.info} ${h.message}`);
|
|
697
|
-
});
|
|
698
|
-
}
|
|
699
|
-
case "release": {
|
|
700
|
-
if (sub === "verify") {
|
|
701
|
-
const { v, p } = parse(rest.slice(1));
|
|
702
|
-
if (p.length)
|
|
703
|
-
throw new OkbError("Usage: okb release verify (checks the folder against its manifest.json; read-only)");
|
|
704
|
-
const r = verifyRelease(resolve(v.C ?? "."));
|
|
705
|
-
output(v, () => r, () => {
|
|
706
|
-
if (r.ok)
|
|
707
|
-
return void console.log(`${SYMBOL.ok} ${r.id} ${r.version} matches its manifest (${r.checked} files).`);
|
|
708
|
-
console.log(`${SYMBOL.error} ${r.id} ${r.version} doesn't match its manifest:`);
|
|
709
|
-
for (const pr of r.problems)
|
|
710
|
-
console.log(` - ${pr}`);
|
|
711
|
-
console.log(c.dim(" If the changes are intended, cut a new release: okb release <version>. If not, restore the files (git checkout)."));
|
|
712
|
-
});
|
|
713
|
-
process.exitCode = r.ok ? 0 : 1;
|
|
714
|
-
return;
|
|
715
|
-
}
|
|
716
|
-
const { v, p } = parse(rest);
|
|
717
|
-
if (p.length !== 1)
|
|
718
|
-
throw new OkbError("Usage: okb release <version> · okb release verify (version is semver, like 1.0.0)");
|
|
719
|
-
const dir = v.C ?? ".";
|
|
720
|
-
const result = await Ontology.edit(dir, async (ont) => {
|
|
721
|
-
const findings = runChecks(ont, { all: false }, { pages: await loadSourcePages(ont) }).findings;
|
|
722
|
-
return cutRelease(ont, p[0], findings);
|
|
723
|
-
});
|
|
724
|
-
output(v, () => ({ version: result.version, previous: result.previous ?? null, manifest: result.manifest }), () => say(result.notes));
|
|
725
|
-
return;
|
|
726
|
-
}
|
|
727
|
-
case "pin": {
|
|
728
|
-
const { v, p } = parse(rest);
|
|
729
|
-
if (p.length)
|
|
730
|
-
throw new OkbError("Usage: okb pin (records the installed okb version in okb.json)");
|
|
731
|
-
Ontology.edit(v.C ?? ".", (ont) => {
|
|
732
|
-
ont.manifest.okbVersion = PACKAGE_VERSION;
|
|
733
|
-
ont.save();
|
|
734
|
-
});
|
|
735
|
-
console.log(`${SYMBOL.ok} Pinned okb ${PACKAGE_VERSION} in okb.json. okb check warns anyone with an older okb, and the CI workflow installs this version.`);
|
|
736
|
-
return;
|
|
737
|
-
}
|
|
738
|
-
case "check": {
|
|
739
|
-
const { v, p } = parse(rest);
|
|
740
|
-
if (p.length)
|
|
741
|
-
throw new OkbError("Usage: okb check [--json] (checks the current folder; use -C <folder> for another)");
|
|
742
|
-
const ont = load(v);
|
|
743
|
-
const report = await runCheck(ont, PACKAGE_VERSION);
|
|
744
|
-
output(v, () => report, () => console.log(formatCheck(report, ont.meta.name)));
|
|
745
|
-
process.exitCode = report.ok ? 0 : 1;
|
|
746
|
-
return;
|
|
747
|
-
}
|
|
748
|
-
case "diff": {
|
|
749
|
-
const { v, p } = parse(rest);
|
|
750
|
-
if (p.length < 1 || p.length > 2)
|
|
751
|
-
throw new OkbError("Usage: okb diff <old> [<new>] (each a folder or a git ref; with one, <new> is the current folder)");
|
|
752
|
-
const here = v.C ?? ".";
|
|
753
|
-
const a = loadSide(p[0], here);
|
|
754
|
-
const b = p[1] ? loadSide(p[1], here) : { ont: Ontology.load(here), name: "working folder" };
|
|
755
|
-
const d = diffOntologies(a.ont, b.ont, a.name, b.name);
|
|
756
|
-
return output(v, () => d, () => process.stdout.write(diffMarkdown(d)));
|
|
757
|
-
}
|
|
758
|
-
case "docs": {
|
|
759
|
-
const DOCS_USAGE = "Usage: okb docs --out <folder> · okb docs --out <folder> --changed <ref> [--hops N] · okb docs --out <folder> --context";
|
|
760
|
-
const { v, p } = parse(rest, { out: str, changed: str, hops: str, context: bool });
|
|
761
|
-
if (p.length || !v.out)
|
|
762
|
-
throw new OkbError(DOCS_USAGE);
|
|
763
|
-
const out = resolve(v.out);
|
|
764
|
-
const done = (files, what, hint, extra = {}) => output(v, () => ({ out: v.out, files, ...extra }), () => console.log(`${SYMBOL.ok} Wrote ${what} to ${v.out}${hint}.`));
|
|
765
|
-
if (v.context && (v.changed !== undefined || v.hops !== undefined))
|
|
766
|
-
throw new OkbError("--context is its own view of every rule; it doesn't combine with --changed or --hops.");
|
|
767
|
-
if (v.hops !== undefined && v.changed === undefined)
|
|
768
|
-
throw new OkbError("--hops adds neighbors to a --changed view; give --changed <ref> too.");
|
|
769
|
-
if (v.context) {
|
|
770
|
-
const ont = load(v);
|
|
771
|
-
return done(writePages(out, { "rules-in-context.md": contextPage(ont, await loadSourcePages(ont)) }), "rules-in-context.md", "");
|
|
772
|
-
}
|
|
773
|
-
if (v.changed !== undefined) {
|
|
774
|
-
const hops = num(v.hops, "--hops") ?? 0;
|
|
775
|
-
const ont = load(v);
|
|
776
|
-
const old = loadSide(v.changed, v.C ?? ".");
|
|
777
|
-
const { pages, changes } = changedPageSet(kbDocs(old.ont), kbDocs(ont), hops);
|
|
778
|
-
const after = kbDocs(ont);
|
|
779
|
-
const report = resync(ont, await loadSourcePages(ont), { write: false, record: false });
|
|
780
|
-
const files = Object.fromEntries(pages.map((n) => [n, after[n]]));
|
|
781
|
-
files["changes.md"] = changesPage({ against: v.changed, name: ont.meta.name, changes, hops, resync: report });
|
|
782
|
-
const stale = report.gone.length + report.moved.length + report.worklist.length;
|
|
783
|
-
return done(writeChangedPages(out, files), `${pages.length} changed ${pages.length === 1 ? "page" : "pages"} and changes.md (${stale} source ${stale === 1 ? "change" : "changes"} not yet in the KB)`, " (start at changes.md)", changes);
|
|
784
|
-
}
|
|
785
|
-
const files = writeKbDocs(load(v), out);
|
|
786
|
-
return done(files, `${files.length} pages`, " (start at index.md)");
|
|
787
|
-
}
|
|
788
|
-
case "skills": {
|
|
789
|
-
const SKILLS_USAGE = "Usage: okb skills install [--copy] [--dir <folder>] [--force] · okb skills status [--dir <folder>]";
|
|
790
|
-
if (sub !== "install" && sub !== "status")
|
|
791
|
-
throw new OkbError(SKILLS_USAGE);
|
|
792
|
-
const { v, p } = parse(rest.slice(1), sub === "install" ? { copy: bool, dir: str, force: bool } : { dir: str });
|
|
793
|
-
if (p.length)
|
|
794
|
-
throw new OkbError(SKILLS_USAGE);
|
|
795
|
-
const opts = { dir: v.dir, metaVersion: MetaKB.get().version };
|
|
796
|
-
if (sub === "install") {
|
|
797
|
-
const { dir, lines } = installSkills({ ...opts, copy: v.copy, force: v.force });
|
|
798
|
-
if (v.json)
|
|
799
|
-
return void console.log(JSON.stringify({ dir, lines }, null, 2));
|
|
800
|
-
console.log(`${SYMBOL.ok} Skills in ${dir}`);
|
|
801
|
-
for (const l of lines)
|
|
802
|
-
console.log(` ${l}`);
|
|
803
|
-
console.log(c.dim("\n Start a new Claude Code or Cursor session to load them."));
|
|
804
|
-
return;
|
|
805
|
-
}
|
|
806
|
-
const { dir, skills } = skillsStatus(opts);
|
|
807
|
-
const good = (s) => s === "current";
|
|
808
|
-
if (!skills.every((s) => good(s.state)))
|
|
809
|
-
process.exitCode = 1;
|
|
810
|
-
return output(v, () => ({ dir, metaKbVersion: opts.metaVersion, skills }), () => {
|
|
811
|
-
console.log(c.bold(`Skills in ${dir}`) + c.dim(` (okb meta-KB ${opts.metaVersion})`));
|
|
812
|
-
for (const s of skills)
|
|
813
|
-
console.log(` ${good(s.state) ? SYMBOL.ok : s.state === "missing" ? SYMBOL.todo : SYMBOL.warning} ${s.name.padEnd(26)} ${s.detail}`);
|
|
814
|
-
if (!skills.every((s) => good(s.state)))
|
|
815
|
-
console.log(c.dim("\n Fix it with: okb skills install"));
|
|
816
|
-
});
|
|
817
|
-
}
|
|
818
|
-
default:
|
|
819
|
-
throw new OkbError(`Unknown command '${cmd}'. Run okb help.`);
|
|
820
|
-
}
|
|
3
|
+
import { OkbError } from "@ontology-kit/reader";
|
|
4
|
+
import { COMMANDS } from "./commands.js";
|
|
5
|
+
import { asksForHelp, isParseError, leadingGlobals } from "./command-table.js";
|
|
6
|
+
import { runCommand } from "./runner.js";
|
|
7
|
+
import { SYMBOL } from "./render.js";
|
|
8
|
+
async function main(argv) {
|
|
9
|
+
// Global options (-C, --json) may come before the command; they also work after it.
|
|
10
|
+
const { globals, rest } = leadingGlobals(argv);
|
|
11
|
+
const words = asksForHelp(rest) ? ["help"] : rest;
|
|
12
|
+
await runCommand(COMMANDS, words, globals);
|
|
821
13
|
}
|
|
822
14
|
try {
|
|
823
15
|
await main(process.argv.slice(2));
|
|
824
16
|
}
|
|
825
17
|
catch (e) {
|
|
826
|
-
if (e instanceof OkbError ||
|
|
18
|
+
if (e instanceof OkbError || isParseError(e)) {
|
|
827
19
|
console.error(`${SYMBOL.error} ${e.message}`);
|
|
828
20
|
process.exitCode = 2;
|
|
829
21
|
}
|