@ontology-kit/okb 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.md +26 -2
- package/bin/okb.js +8 -0
- package/dist/checks.d.ts +33 -0
- package/dist/checks.js +966 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +718 -0
- package/dist/clidocs.d.ts +6 -0
- package/dist/clidocs.js +90 -0
- package/dist/clock.d.ts +2 -0
- package/dist/clock.js +16 -0
- package/dist/commands.d.ts +28 -0
- package/dist/commands.js +622 -0
- package/dist/export.d.ts +54 -0
- package/dist/export.js +290 -0
- package/dist/families.d.ts +20 -0
- package/dist/families.js +51 -0
- package/dist/help.d.ts +2 -0
- package/dist/help.js +57 -0
- package/dist/markdown.d.ts +60 -0
- package/dist/markdown.js +310 -0
- package/dist/metakb.d.ts +164 -0
- package/dist/metakb.js +130 -0
- package/dist/naming.d.ts +20 -0
- package/dist/naming.js +140 -0
- package/dist/ops/admin.d.ts +16 -0
- package/dist/ops/admin.js +119 -0
- package/dist/ops/classes.d.ts +21 -0
- package/dist/ops/classes.js +105 -0
- package/dist/ops/common.d.ts +6 -0
- package/dist/ops/common.js +4 -0
- package/dist/ops/instances.d.ts +8 -0
- package/dist/ops/instances.js +70 -0
- package/dist/ops/links.d.ts +9 -0
- package/dist/ops/links.js +85 -0
- package/dist/ops/naming.d.ts +4 -0
- package/dist/ops/naming.js +70 -0
- package/dist/ops/scope.d.ts +29 -0
- package/dist/ops/scope.js +124 -0
- package/dist/ops/slots.d.ts +31 -0
- package/dist/ops/slots.js +186 -0
- package/dist/ops/values.d.ts +20 -0
- package/dist/ops/values.js +151 -0
- package/dist/ops.d.ts +14 -0
- package/dist/ops.js +14 -0
- package/dist/provenance.d.ts +53 -0
- package/dist/provenance.js +296 -0
- package/dist/quotes.d.ts +14 -0
- package/dist/quotes.js +104 -0
- package/dist/render.d.ts +30 -0
- package/dist/render.js +413 -0
- package/dist/review.d.ts +10 -0
- package/dist/review.js +145 -0
- package/dist/site.d.ts +6 -0
- package/dist/site.js +6 -0
- package/dist/skills.d.ts +52 -0
- package/dist/skills.js +120 -0
- package/dist/status.d.ts +18 -0
- package/dist/status.js +59 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.js +5 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +7 -0
- package/meta-kb/data/edges.json +3172 -0
- package/meta-kb/data/manifest.json +19 -0
- package/meta-kb/data/nodes.json +3546 -0
- package/package.json +71 -3
- package/skills/build-domain-kb-from-docs/SKILL.md +87 -0
- package/skills/ontology-coach/SKILL.md +100 -0
- package/skills/ontology-review/SKILL.md +66 -0
package/dist/markdown.js
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small, dependency-light reader for documentation markdown (CommonMark + GFM
|
|
3
|
+
* tables + YAML frontmatter). It isn't a full renderer; it splits a doc into
|
|
4
|
+
* *quotable blocks*, each with:
|
|
5
|
+
*
|
|
6
|
+
* - the heading path it sits under ("Accessibility > Touch Target Size")
|
|
7
|
+
* - its line range in the file (for GitHub deep links)
|
|
8
|
+
* - its plain text as a reader sees it: link and image text kept, URLs dropped,
|
|
9
|
+
* emphasis and code ticks removed, HTML tags and comments removed
|
|
10
|
+
* - whether it's quotable. Code examples and HTML comments aren't: an example
|
|
11
|
+
* shows usage, it doesn't state a rule.
|
|
12
|
+
*
|
|
13
|
+
* Tables are split into one block per row, with the column headers kept, so a prop
|
|
14
|
+
* can be cited on its own ("Component API > PrimaryButton > Props > row: size").
|
|
15
|
+
* Rows whose cell count doesn't match the header are flagged, because they usually
|
|
16
|
+
* mean an unescaped "|" inside a type union (`"start" | "end"`).
|
|
17
|
+
*/
|
|
18
|
+
import { parse as parseYaml } from "yaml";
|
|
19
|
+
import { normalize } from "./quotes.js";
|
|
20
|
+
export function inlineToText(s) {
|
|
21
|
+
const codes = [];
|
|
22
|
+
let t = s.replace(/`+([^`]*?)`+/g, (_m, c) => {
|
|
23
|
+
codes.push(c);
|
|
24
|
+
return `\u0000${codes.length - 1}\u0000`;
|
|
25
|
+
});
|
|
26
|
+
t = t
|
|
27
|
+
.replace(/<!--[\s\S]*?-->/g, "")
|
|
28
|
+
.replace(/<br\s*\/?>/gi, "\n")
|
|
29
|
+
.replace(/!\[([^\]]*)\]\([^)]*\)/g, "$1") // images → alt text
|
|
30
|
+
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1") // links → link text
|
|
31
|
+
.replace(/\[([^\]]*)\]\[[^\]]*\]/g, "$1") // reference links
|
|
32
|
+
.replace(/<(https?:[^>]+)>/g, "$1") // autolinks
|
|
33
|
+
.replace(/\{@link\s+([^}\s]+)\s*\}/g, "$1") // JSDoc {@link x}
|
|
34
|
+
.replace(/<\/?[A-Za-z][^>]*>/g, "") // HTML / JSX tags
|
|
35
|
+
.replace(/(\*\*|__)(.+?)\1/g, "$2")
|
|
36
|
+
.replace(/(^|[^\w*])[*_]([^*_\n]+)[*_](?=[^\w*]|$)/g, "$1$2")
|
|
37
|
+
.replace(/~~(.+?)~~/g, "$1")
|
|
38
|
+
.replace(/\\([\\`*_{}[\]()#+\-.!|])/g, "$1");
|
|
39
|
+
t = t.replace(/\u0000(\d+)\u0000/g, (_m, i) => codes[Number(i)]);
|
|
40
|
+
return t.replace(/[ \t]+/g, " ").trim();
|
|
41
|
+
}
|
|
42
|
+
/** Split a GFM table row into cells, ignoring pipes inside code spans or escaped. */
|
|
43
|
+
export function splitRow(line) {
|
|
44
|
+
const s = line.trim().replace(/^\|/, "").replace(/\|$/, "");
|
|
45
|
+
const cells = [];
|
|
46
|
+
let cur = "";
|
|
47
|
+
let tick = 0;
|
|
48
|
+
for (let i = 0; i < s.length; i++) {
|
|
49
|
+
const ch = s[i];
|
|
50
|
+
if (ch === "\\" && s[i + 1] === "|") {
|
|
51
|
+
cur += "|";
|
|
52
|
+
i++;
|
|
53
|
+
}
|
|
54
|
+
else if (ch === "`") {
|
|
55
|
+
let n = 1;
|
|
56
|
+
while (s[i + n] === "`")
|
|
57
|
+
n++;
|
|
58
|
+
tick = tick === 0 ? n : tick === n ? 0 : tick;
|
|
59
|
+
cur += "`".repeat(n);
|
|
60
|
+
i += n - 1;
|
|
61
|
+
}
|
|
62
|
+
else if (ch === "|" && tick === 0) {
|
|
63
|
+
cells.push(cur.trim());
|
|
64
|
+
cur = "";
|
|
65
|
+
}
|
|
66
|
+
else
|
|
67
|
+
cur += ch;
|
|
68
|
+
}
|
|
69
|
+
cells.push(cur.trim());
|
|
70
|
+
return cells;
|
|
71
|
+
}
|
|
72
|
+
const isTableSep = (l) => /^\s*\|?\s*:?-{3,}:?\s*(\|\s*:?-{3,}:?\s*)*\|?\s*$/.test(l);
|
|
73
|
+
const isListItem = (l) => /^(\s{0,3})([-*+]|\d+[.)])\s+/.test(l);
|
|
74
|
+
const isHeading = (l) => /^#{1,6}\s+/.test(l);
|
|
75
|
+
const isFence = (l) => /^\s*(```|~~~)/.test(l);
|
|
76
|
+
export function slugHeading(text) {
|
|
77
|
+
return inlineToText(text).toLowerCase().replace(/[^\p{L}\p{N}\s-]/gu, "").trim().replace(/\s/g, "-");
|
|
78
|
+
}
|
|
79
|
+
export function parseMarkdown(src) {
|
|
80
|
+
const lines = src.replace(/\r\n?/g, "\n").split("\n");
|
|
81
|
+
const blocks = [];
|
|
82
|
+
const headings = [];
|
|
83
|
+
const problems = [];
|
|
84
|
+
const stack = [];
|
|
85
|
+
const path = () => stack.map((h) => h.text);
|
|
86
|
+
let frontmatter = null;
|
|
87
|
+
const push = (b) => blocks.push({ ...b, index: blocks.length, headingPath: path() });
|
|
88
|
+
let i = 0;
|
|
89
|
+
if (lines[0]?.trim() === "---") {
|
|
90
|
+
const end = lines.findIndex((l, j) => j > 0 && l.trim() === "---");
|
|
91
|
+
if (end > 0) {
|
|
92
|
+
const raw = lines.slice(1, end).join("\n");
|
|
93
|
+
try {
|
|
94
|
+
frontmatter = parseYaml(raw) ?? {};
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
problems.push({ line: 1, message: `Frontmatter isn't valid YAML: ${e.message}` });
|
|
98
|
+
}
|
|
99
|
+
push({ kind: "frontmatter", startLine: 1, endLine: end + 1, raw, text: raw, quotable: true, note: "metadata" });
|
|
100
|
+
i = end + 1;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
while (i < lines.length) {
|
|
104
|
+
const line = lines[i];
|
|
105
|
+
const ln = i + 1;
|
|
106
|
+
if (!line.trim()) {
|
|
107
|
+
i++;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
// headings
|
|
111
|
+
const hm = line.match(/^(#{1,6})\s+(.*?)\s*#*\s*$/);
|
|
112
|
+
if (hm) {
|
|
113
|
+
const level = hm[1].length;
|
|
114
|
+
const text = inlineToText(hm[2]);
|
|
115
|
+
while (stack.length && stack[stack.length - 1].level >= level)
|
|
116
|
+
stack.pop();
|
|
117
|
+
stack.push({ level, text });
|
|
118
|
+
headings.push({ level, text, line: ln, path: path() });
|
|
119
|
+
i++;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
// fenced code
|
|
123
|
+
if (isFence(line)) {
|
|
124
|
+
const fence = line.trim().slice(0, 3);
|
|
125
|
+
const lang = line.trim().slice(3).trim();
|
|
126
|
+
let j = i + 1;
|
|
127
|
+
while (j < lines.length && !lines[j].trim().startsWith(fence))
|
|
128
|
+
j++;
|
|
129
|
+
const raw = lines.slice(i, Math.min(j + 1, lines.length)).join("\n");
|
|
130
|
+
push({ kind: "code", startLine: ln, endLine: Math.min(j + 1, lines.length), raw, text: lines.slice(i + 1, j).join("\n"), lang, quotable: false, note: "code example: shows usage but doesn't state a rule; cite the prose or the component source instead" });
|
|
131
|
+
i = j + 1;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
// HTML comments
|
|
135
|
+
if (line.trim().startsWith("<!--")) {
|
|
136
|
+
let j = i;
|
|
137
|
+
while (j < lines.length && !lines[j].includes("-->"))
|
|
138
|
+
j++;
|
|
139
|
+
push({ kind: "comment", startLine: ln, endLine: j + 1, raw: lines.slice(i, j + 1).join("\n"), text: "", quotable: false, note: "HTML comment (not shown to readers)" });
|
|
140
|
+
i = j + 1;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
// tables
|
|
144
|
+
if (line.trim().startsWith("|") && i + 1 < lines.length && isTableSep(lines[i + 1])) {
|
|
145
|
+
const columns = splitRow(line).map(inlineToText);
|
|
146
|
+
let j = i + 2;
|
|
147
|
+
while (j < lines.length && lines[j].trim().startsWith("|")) {
|
|
148
|
+
const cellsRaw = splitRow(lines[j]);
|
|
149
|
+
const cells = cellsRaw.map(inlineToText);
|
|
150
|
+
const key = cells[0] ?? "";
|
|
151
|
+
const text = columns.map((c, k) => `${c}: ${cells[k] ?? ""}`).join("; ") + (cells.length > columns.length ? `; ${cells.slice(columns.length).join("; ")}` : "");
|
|
152
|
+
const block = {
|
|
153
|
+
kind: "table-row", startLine: j + 1, endLine: j + 1, raw: lines[j], text, quotable: true, row: { columns, cells, key },
|
|
154
|
+
};
|
|
155
|
+
if (cells.length !== columns.length) {
|
|
156
|
+
block.note = `malformed row: ${cells.length} cells for ${columns.length} columns (often an unescaped "|" in a type union; write \\| inside the cell)`;
|
|
157
|
+
problems.push({ line: j + 1, message: `Table row '${key}' has ${cells.length} cells for ${columns.length} columns.` });
|
|
158
|
+
}
|
|
159
|
+
push(block);
|
|
160
|
+
j++;
|
|
161
|
+
}
|
|
162
|
+
i = j;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
// blockquotes / callouts
|
|
166
|
+
if (line.trimStart().startsWith(">")) {
|
|
167
|
+
let j = i;
|
|
168
|
+
const body = [];
|
|
169
|
+
while (j < lines.length && lines[j].trimStart().startsWith(">")) {
|
|
170
|
+
body.push(lines[j].trimStart().replace(/^>\s?/, ""));
|
|
171
|
+
j++;
|
|
172
|
+
}
|
|
173
|
+
const text = inlineToText(body.join(" "));
|
|
174
|
+
const ct = text.match(/^(Caution|Note|Warning|Tip|Important|Info|Danger)\b:?/i);
|
|
175
|
+
push({ kind: "callout", startLine: ln, endLine: j, raw: lines.slice(i, j).join("\n"), text, quotable: true, calloutType: ct?.[1] });
|
|
176
|
+
i = j;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
// list items (each item is its own block; nested items too)
|
|
180
|
+
if (isListItem(line)) {
|
|
181
|
+
let j = i + 1;
|
|
182
|
+
const indent = line.match(/^\s*/)[0].length;
|
|
183
|
+
while (j < lines.length && lines[j].trim() && !isListItem(lines[j]) && !isHeading(lines[j]) && !isFence(lines[j]) && lines[j].match(/^\s*/)[0].length > indent)
|
|
184
|
+
j++;
|
|
185
|
+
const raw = lines.slice(i, j).join("\n");
|
|
186
|
+
const body = lines.slice(i, j).map((l, k) => (k === 0 ? l.replace(/^\s*([-*+]|\d+[.)])\s+/, "") : l.trim())).join(" ");
|
|
187
|
+
push({ kind: "list-item", startLine: ln, endLine: j, raw, text: inlineToText(body), quotable: true });
|
|
188
|
+
i = j;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
// standalone image
|
|
192
|
+
if (/^\s*!\[[^\]]*\]\([^)]*\)\s*$/.test(line)) {
|
|
193
|
+
push({ kind: "image", startLine: ln, endLine: ln, raw: line, text: inlineToText(line), quotable: false, note: "image (only its alt text is available)" });
|
|
194
|
+
i++;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
// paragraph (until blank line or a new block starts)
|
|
198
|
+
let j = i + 1;
|
|
199
|
+
while (j < lines.length && lines[j].trim() && !isHeading(lines[j]) && !isFence(lines[j]) && !isListItem(lines[j]) && !lines[j].trimStart().startsWith(">") && !lines[j].trim().startsWith("|"))
|
|
200
|
+
j++;
|
|
201
|
+
const raw = lines.slice(i, j).join("\n");
|
|
202
|
+
const isHtml = /^\s*<[A-Za-z]/.test(line) && !inlineToText(raw);
|
|
203
|
+
push({ kind: isHtml ? "html" : "paragraph", startLine: ln, endLine: j, raw, text: inlineToText(raw.replace(/\n/g, " ")), quotable: !isHtml });
|
|
204
|
+
i = j;
|
|
205
|
+
}
|
|
206
|
+
const title = typeof frontmatter?.title === "string" ? frontmatter.title : headings.find((h) => h.level === 1)?.text;
|
|
207
|
+
return { frontmatter, title, headings, blocks, lines, problems };
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Find a quote in a parsed doc. Matching is on normalized plain text (letters and
|
|
211
|
+
* digits only), so it tolerates markdown syntax, line wrapping and punctuation, but
|
|
212
|
+
* not changed words. "..." marks a gap. A quote may span consecutive blocks.
|
|
213
|
+
* Returns every place it occurs.
|
|
214
|
+
*/
|
|
215
|
+
export function locate(doc, quote) {
|
|
216
|
+
const pieces = quote.split(/\.\.\.|…/).map(normalize).filter(Boolean);
|
|
217
|
+
if (!pieces.length)
|
|
218
|
+
return [];
|
|
219
|
+
// Search plain text first; fall back to raw text (for quotes copied with markup).
|
|
220
|
+
for (const field of ["text", "raw"]) {
|
|
221
|
+
let joined = "";
|
|
222
|
+
const spans = [];
|
|
223
|
+
for (const b of doc.blocks) {
|
|
224
|
+
if (b.kind === "comment")
|
|
225
|
+
continue;
|
|
226
|
+
const n = normalize(b[field]);
|
|
227
|
+
spans.push({ block: b, start: joined.length, end: joined.length + n.length });
|
|
228
|
+
joined += n;
|
|
229
|
+
}
|
|
230
|
+
const hits = [];
|
|
231
|
+
let from = 0;
|
|
232
|
+
for (;;) {
|
|
233
|
+
const start = joined.indexOf(pieces[0], from);
|
|
234
|
+
if (start < 0)
|
|
235
|
+
break;
|
|
236
|
+
let ok = true;
|
|
237
|
+
let end = start + pieces[0].length;
|
|
238
|
+
for (const p of pieces.slice(1)) {
|
|
239
|
+
const k = joined.indexOf(p, end);
|
|
240
|
+
if (k < 0) {
|
|
241
|
+
ok = false;
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
end = k + p.length;
|
|
245
|
+
}
|
|
246
|
+
if (!ok)
|
|
247
|
+
break;
|
|
248
|
+
const covered = spans.filter((s) => s.end > start && s.start < end).map((s) => s.block);
|
|
249
|
+
hits.push(toHit(doc, covered, quote, field));
|
|
250
|
+
from = start + 1;
|
|
251
|
+
}
|
|
252
|
+
if (hits.length)
|
|
253
|
+
return dedupe(hits);
|
|
254
|
+
}
|
|
255
|
+
return [];
|
|
256
|
+
}
|
|
257
|
+
function dedupe(hits) {
|
|
258
|
+
const seen = new Set();
|
|
259
|
+
return hits.filter((h) => {
|
|
260
|
+
const k = `${h.startLine}-${h.endLine}`;
|
|
261
|
+
if (seen.has(k))
|
|
262
|
+
return false;
|
|
263
|
+
seen.add(k);
|
|
264
|
+
return true;
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
/** Narrow the block range to the lines that actually contain the quote's start and end. */
|
|
268
|
+
function toHit(doc, blocks, quote, field) {
|
|
269
|
+
const first = blocks[0];
|
|
270
|
+
const last = blocks[blocks.length - 1];
|
|
271
|
+
let startLine = first.startLine;
|
|
272
|
+
let endLine = last.endLine;
|
|
273
|
+
const q = normalize(quote.split(/\.\.\.|…/)[0]);
|
|
274
|
+
const qEnd = normalize(quote.split(/\.\.\.|…/).pop());
|
|
275
|
+
const lineNorm = (n) => normalize(field === "text" ? inlineToText(doc.lines[n - 1] ?? "") : doc.lines[n - 1] ?? "");
|
|
276
|
+
const head = q.slice(0, 12);
|
|
277
|
+
const tail = qEnd.slice(-12);
|
|
278
|
+
for (let n = first.startLine; n <= first.endLine; n++) {
|
|
279
|
+
if (head && lineNorm(n).includes(head)) {
|
|
280
|
+
startLine = n;
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
for (let n = last.endLine; n >= last.startLine; n--) {
|
|
285
|
+
if (tail && lineNorm(n).includes(tail)) {
|
|
286
|
+
endLine = n;
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (endLine < startLine)
|
|
291
|
+
endLine = startLine;
|
|
292
|
+
const headingPath = first.headingPath;
|
|
293
|
+
let locator = headingPath.join(" > ") || "(top of document)";
|
|
294
|
+
if (first.kind === "table-row" && blocks.length === 1)
|
|
295
|
+
locator += ` > row: ${first.row.key}`;
|
|
296
|
+
if (first.kind === "frontmatter")
|
|
297
|
+
locator = "frontmatter";
|
|
298
|
+
if (first.kind === "callout" && first.calloutType)
|
|
299
|
+
locator += ` > ${first.calloutType} callout`;
|
|
300
|
+
return { blocks, headingPath, startLine, endLine, quotable: blocks.every((b) => b.quotable), locator };
|
|
301
|
+
}
|
|
302
|
+
/** GitHub-style links for a quote: source lines (?plain=1#Lx-Ly) and rendered heading anchor. */
|
|
303
|
+
export function deepLinks(hit, repoUrl, pageUrl) {
|
|
304
|
+
const lines = hit.startLine === hit.endLine ? `L${hit.startLine}` : `L${hit.startLine}-L${hit.endLine}`;
|
|
305
|
+
const heading = hit.headingPath[hit.headingPath.length - 1];
|
|
306
|
+
return {
|
|
307
|
+
sourceLink: repoUrl ? `${repoUrl}${repoUrl.includes("?") ? "&" : "?"}plain=1#${lines}` : undefined,
|
|
308
|
+
pageLink: pageUrl ? (heading ? `${pageUrl}#${slugHeading(heading)}` : pageUrl) : undefined,
|
|
309
|
+
};
|
|
310
|
+
}
|
package/dist/metakb.d.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { type GraphEdge, type Modality } from "@ontology-kit/format";
|
|
2
|
+
import { type Severity } from "./types.js";
|
|
3
|
+
export declare const META_DIR: string;
|
|
4
|
+
export interface MetaInfo {
|
|
5
|
+
type: "MetaKB";
|
|
6
|
+
id: string;
|
|
7
|
+
version: string;
|
|
8
|
+
formatVersion: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface MetaSource {
|
|
12
|
+
type: "Source";
|
|
13
|
+
id: string;
|
|
14
|
+
title: string;
|
|
15
|
+
shortName: string;
|
|
16
|
+
authors: string;
|
|
17
|
+
year: number;
|
|
18
|
+
url?: string;
|
|
19
|
+
venue?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface MetaLocation {
|
|
22
|
+
type: "SourceLocation";
|
|
23
|
+
id: string;
|
|
24
|
+
section: string;
|
|
25
|
+
/** A PDF page, or a range like "14-15". */
|
|
26
|
+
page: number | string;
|
|
27
|
+
quote: string;
|
|
28
|
+
deepLink?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface MetaPrinciple {
|
|
31
|
+
type: "Principle";
|
|
32
|
+
id: string;
|
|
33
|
+
name: string;
|
|
34
|
+
statement: string;
|
|
35
|
+
plainLanguage: string;
|
|
36
|
+
}
|
|
37
|
+
export interface MetaConcept {
|
|
38
|
+
type: "Concept";
|
|
39
|
+
id: string;
|
|
40
|
+
name: string;
|
|
41
|
+
aliases?: string[];
|
|
42
|
+
definition: string;
|
|
43
|
+
plainLanguage: string;
|
|
44
|
+
example?: string;
|
|
45
|
+
inThisToolkit?: string;
|
|
46
|
+
origin: "source" | "operational";
|
|
47
|
+
}
|
|
48
|
+
export interface MetaRule {
|
|
49
|
+
type: "Rule";
|
|
50
|
+
id: string;
|
|
51
|
+
key: string;
|
|
52
|
+
modality: Modality;
|
|
53
|
+
basis: "direct" | "interpretive" | "operational";
|
|
54
|
+
check: "mechanical" | "heuristic" | "judgment";
|
|
55
|
+
severity: Severity;
|
|
56
|
+
severityReason?: string;
|
|
57
|
+
fromStep: number;
|
|
58
|
+
statement: string;
|
|
59
|
+
plainLanguage: string;
|
|
60
|
+
/** The question the ontology-review skill asks (judgment rules). */
|
|
61
|
+
review?: string;
|
|
62
|
+
fix: string;
|
|
63
|
+
}
|
|
64
|
+
export interface MetaRationale {
|
|
65
|
+
type: "Rationale";
|
|
66
|
+
id: string;
|
|
67
|
+
explanation: string;
|
|
68
|
+
}
|
|
69
|
+
/** One yes/no question in a decision guide. "continue" means go to the next question. */
|
|
70
|
+
export interface DecisionTest {
|
|
71
|
+
ask: string;
|
|
72
|
+
ifYes: string;
|
|
73
|
+
ifNo: string;
|
|
74
|
+
}
|
|
75
|
+
export interface MetaDecision {
|
|
76
|
+
type: "Decision";
|
|
77
|
+
id: string;
|
|
78
|
+
name: string;
|
|
79
|
+
plainLanguage: string;
|
|
80
|
+
whenYouFaceIt: string;
|
|
81
|
+
tests: DecisionTest[];
|
|
82
|
+
note?: string;
|
|
83
|
+
wine: string;
|
|
84
|
+
}
|
|
85
|
+
/** A step's "done when" item. `check` names a function in STEP_CHECKS (src/status.ts); without one it's a judgment call. */
|
|
86
|
+
export interface DoneWhen {
|
|
87
|
+
text: string;
|
|
88
|
+
check?: string;
|
|
89
|
+
}
|
|
90
|
+
export interface MetaStep {
|
|
91
|
+
type: "Step";
|
|
92
|
+
id: string;
|
|
93
|
+
order: number;
|
|
94
|
+
name: string;
|
|
95
|
+
goal: string;
|
|
96
|
+
whyItMatters: string;
|
|
97
|
+
guidingQuestions: string[];
|
|
98
|
+
outputs: string[];
|
|
99
|
+
doneWhen: DoneWhen[];
|
|
100
|
+
tips: string[];
|
|
101
|
+
wine: string;
|
|
102
|
+
commands: string[];
|
|
103
|
+
}
|
|
104
|
+
export interface MetaNodeType {
|
|
105
|
+
type: "NodeType";
|
|
106
|
+
id: string;
|
|
107
|
+
name: string;
|
|
108
|
+
origin: "ontology101" | "operational";
|
|
109
|
+
required: string[];
|
|
110
|
+
description: string;
|
|
111
|
+
}
|
|
112
|
+
export interface MetaEdgeType {
|
|
113
|
+
type: "EdgeType";
|
|
114
|
+
id: string;
|
|
115
|
+
name: string;
|
|
116
|
+
origin: "ontology101" | "operational";
|
|
117
|
+
fromTypes: string[];
|
|
118
|
+
toTypes: string[];
|
|
119
|
+
props: string[];
|
|
120
|
+
/** Edge properties that may be present (String-valued); `props` are the required ones. */
|
|
121
|
+
optionalProps: string[];
|
|
122
|
+
/** Part of the provenance and rule layer (quotes, sources, rules) rather than the ontology itself. */
|
|
123
|
+
provenance: boolean;
|
|
124
|
+
description: string;
|
|
125
|
+
}
|
|
126
|
+
export type MetaNode = MetaInfo | MetaSource | MetaLocation | MetaPrinciple | MetaConcept | MetaRule | MetaRationale | MetaDecision | MetaStep | MetaNodeType | MetaEdgeType;
|
|
127
|
+
export type MetaType = MetaNode["type"];
|
|
128
|
+
export type MetaOf<T extends MetaType> = Extract<MetaNode, {
|
|
129
|
+
type: T;
|
|
130
|
+
}>;
|
|
131
|
+
/** What `okb explain` can show. */
|
|
132
|
+
export type Explainable = MetaConcept | MetaPrinciple | MetaDecision | MetaStep | MetaRule;
|
|
133
|
+
/** How `okb explain` lists a node: a rule by its key, anything else by its name. */
|
|
134
|
+
export declare function explainName(n: Explainable): string;
|
|
135
|
+
export declare class MetaKB {
|
|
136
|
+
private static instance;
|
|
137
|
+
nodes: MetaNode[];
|
|
138
|
+
edges: GraphEdge[];
|
|
139
|
+
byId: Map<string, MetaNode>;
|
|
140
|
+
rules: Map<string, MetaRule>;
|
|
141
|
+
steps: MetaStep[];
|
|
142
|
+
version: string;
|
|
143
|
+
constructor(dataDir?: string);
|
|
144
|
+
static get(): MetaKB;
|
|
145
|
+
targets(id: string, type: string): string[];
|
|
146
|
+
sources(id: string, type: string): string[];
|
|
147
|
+
ofType<T extends MetaType>(type: T): MetaOf<T>[];
|
|
148
|
+
/** A node the build guarantees exists (meta-kb/build.ts checks referential integrity). */
|
|
149
|
+
node<T extends MetaType>(id: string, type: T): MetaOf<T>;
|
|
150
|
+
rule(key: string): MetaRule;
|
|
151
|
+
/** SourceLocations a node is grounded in (following Rule -> Rationale -> CITES). */
|
|
152
|
+
citations(id: string): MetaLocation[];
|
|
153
|
+
citeLine(loc: MetaLocation): string;
|
|
154
|
+
rationale(rule: MetaRule): string;
|
|
155
|
+
step(order: number): MetaStep | undefined;
|
|
156
|
+
stepRules(order: number): MetaRule[];
|
|
157
|
+
formatRegistry(): {
|
|
158
|
+
nodeTypes: Map<string, MetaNodeType>;
|
|
159
|
+
edgeTypes: Map<string, MetaEdgeType>;
|
|
160
|
+
};
|
|
161
|
+
/** What `okb explain <query>` refers to (best matches first). */
|
|
162
|
+
resolve(query: string): Explainable[];
|
|
163
|
+
search(text: string): Explainable[];
|
|
164
|
+
}
|
package/dist/metakb.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/** Read access to the compiled meta-KB (meta-kb/data/*.json). */
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import * as naming from "./naming.js";
|
|
6
|
+
import {} from "@ontology-kit/format";
|
|
7
|
+
import {} from "./types.js";
|
|
8
|
+
export const META_DIR = join(dirname(dirname(fileURLToPath(import.meta.url))), "meta-kb", "data");
|
|
9
|
+
const EXPLAINABLE = new Set(["Concept", "Principle", "Decision", "Step", "Rule"]);
|
|
10
|
+
const isExplainable = (n) => n !== undefined && EXPLAINABLE.has(n.type);
|
|
11
|
+
/** How `okb explain` lists a node: a rule by its key, anything else by its name. */
|
|
12
|
+
export function explainName(n) {
|
|
13
|
+
return n.type === "Rule" ? n.key : n.name;
|
|
14
|
+
}
|
|
15
|
+
/** The words a node can be looked up by in `okb explain`. */
|
|
16
|
+
function explainNames(n) {
|
|
17
|
+
const names = [n.id.split(".").slice(1).join(".")];
|
|
18
|
+
if (n.type !== "Rule")
|
|
19
|
+
names.push(n.name);
|
|
20
|
+
if (n.type === "Rule")
|
|
21
|
+
names.push(n.key);
|
|
22
|
+
if (n.type === "Concept")
|
|
23
|
+
names.push(...(n.aliases ?? []));
|
|
24
|
+
return names.filter(Boolean);
|
|
25
|
+
}
|
|
26
|
+
export class MetaKB {
|
|
27
|
+
static instance;
|
|
28
|
+
nodes;
|
|
29
|
+
edges;
|
|
30
|
+
byId;
|
|
31
|
+
rules;
|
|
32
|
+
steps;
|
|
33
|
+
version;
|
|
34
|
+
constructor(dataDir = META_DIR) {
|
|
35
|
+
this.nodes = JSON.parse(readFileSync(join(dataDir, "nodes.json"), "utf8"));
|
|
36
|
+
this.edges = JSON.parse(readFileSync(join(dataDir, "edges.json"), "utf8"));
|
|
37
|
+
this.byId = new Map(this.nodes.map((n) => [n.id, n]));
|
|
38
|
+
this.rules = new Map(this.ofType("Rule").map((n) => [n.key, n]));
|
|
39
|
+
this.steps = this.ofType("Step").sort((a, b) => a.order - b.order);
|
|
40
|
+
this.version = this.node("metakb", "MetaKB").version;
|
|
41
|
+
}
|
|
42
|
+
static get() {
|
|
43
|
+
MetaKB.instance ??= new MetaKB();
|
|
44
|
+
return MetaKB.instance;
|
|
45
|
+
}
|
|
46
|
+
targets(id, type) {
|
|
47
|
+
return this.edges.filter((e) => e.from === id && e.type === type).map((e) => e.to);
|
|
48
|
+
}
|
|
49
|
+
sources(id, type) {
|
|
50
|
+
return this.edges.filter((e) => e.to === id && e.type === type).map((e) => e.from);
|
|
51
|
+
}
|
|
52
|
+
ofType(type) {
|
|
53
|
+
return this.nodes.filter((n) => n.type === type);
|
|
54
|
+
}
|
|
55
|
+
/** A node the build guarantees exists (meta-kb/build.ts checks referential integrity). */
|
|
56
|
+
node(id, type) {
|
|
57
|
+
const n = this.byId.get(id);
|
|
58
|
+
if (n?.type !== type)
|
|
59
|
+
throw new Error(`meta-KB: expected ${type} '${id}'. Rebuild it with pnpm build:meta.`);
|
|
60
|
+
return n;
|
|
61
|
+
}
|
|
62
|
+
rule(key) {
|
|
63
|
+
const r = this.rules.get(key);
|
|
64
|
+
if (!r)
|
|
65
|
+
throw new Error(`Unknown meta-KB rule: ${key}`);
|
|
66
|
+
return r;
|
|
67
|
+
}
|
|
68
|
+
/** SourceLocations a node is grounded in (following Rule -> Rationale -> CITES). */
|
|
69
|
+
citations(id) {
|
|
70
|
+
let ids = [...this.targets(id, "CITES"), ...this.targets(id, "DEFINED_IN")];
|
|
71
|
+
for (const rat of this.targets(id, "JUSTIFIED_BY"))
|
|
72
|
+
ids = ids.concat(this.targets(rat, "CITES"));
|
|
73
|
+
return ids.map((i) => this.byId.get(i)).filter((n) => n?.type === "SourceLocation");
|
|
74
|
+
}
|
|
75
|
+
citeLine(loc) {
|
|
76
|
+
const src = this.byId.get(this.targets(loc.id, "PART_OF")[0]);
|
|
77
|
+
const name = src?.type === "Source" ? (src.shortName ?? src.title) : "?";
|
|
78
|
+
return `${name} ${loc.section}, p.${loc.page}`;
|
|
79
|
+
}
|
|
80
|
+
rationale(rule) {
|
|
81
|
+
const rat = this.targets(rule.id, "JUSTIFIED_BY")[0];
|
|
82
|
+
return rat ? this.node(rat, "Rationale").explanation : "";
|
|
83
|
+
}
|
|
84
|
+
step(order) {
|
|
85
|
+
return this.steps.find((s) => s.order === order);
|
|
86
|
+
}
|
|
87
|
+
stepRules(order) {
|
|
88
|
+
const s = this.step(order);
|
|
89
|
+
return s ? this.targets(s.id, "APPLIES").map((r) => this.node(r, "Rule")) : [];
|
|
90
|
+
}
|
|
91
|
+
formatRegistry() {
|
|
92
|
+
return {
|
|
93
|
+
nodeTypes: new Map(this.ofType("NodeType").map((n) => [n.name, n])),
|
|
94
|
+
edgeTypes: new Map(this.ofType("EdgeType").map((n) => [n.name, n])),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/** What `okb explain <query>` refers to (best matches first). */
|
|
98
|
+
resolve(query) {
|
|
99
|
+
const q = query.trim();
|
|
100
|
+
const rule = this.rules.get(q);
|
|
101
|
+
if (rule)
|
|
102
|
+
return [rule];
|
|
103
|
+
const byId = this.byId.get(q);
|
|
104
|
+
if (isExplainable(byId))
|
|
105
|
+
return [byId];
|
|
106
|
+
const step = /^\d+$/.test(q) ? this.step(Number(q)) : undefined;
|
|
107
|
+
if (step)
|
|
108
|
+
return [step];
|
|
109
|
+
const k = naming.key(q);
|
|
110
|
+
if (!k)
|
|
111
|
+
return [];
|
|
112
|
+
const exact = [];
|
|
113
|
+
const partial = [];
|
|
114
|
+
for (const n of this.nodes) {
|
|
115
|
+
if (!isExplainable(n))
|
|
116
|
+
continue;
|
|
117
|
+
const keys = explainNames(n).flatMap((x) => [naming.key(x), ...x.split("/").map(naming.key)]);
|
|
118
|
+
if (keys.includes(k))
|
|
119
|
+
exact.push(n);
|
|
120
|
+
else if (keys.some((x) => x.includes(k)))
|
|
121
|
+
partial.push(n);
|
|
122
|
+
}
|
|
123
|
+
return exact.length ? exact : partial;
|
|
124
|
+
}
|
|
125
|
+
search(text) {
|
|
126
|
+
const norm = (s) => s.toLowerCase().replace(/[^a-z0-9]+/g, " ").trim();
|
|
127
|
+
const k = norm(text);
|
|
128
|
+
return this.nodes.filter((n) => isExplainable(n) && norm(JSON.stringify(n)).includes(k));
|
|
129
|
+
}
|
|
130
|
+
}
|
package/dist/naming.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Name styles, conversion, and singular/plural guessing. */
|
|
2
|
+
import { type Conventions } from "@ontology-kit/format";
|
|
3
|
+
export { fold, key, rawTokens, relType, slug, stripAccents, tokens } from "@ontology-kit/format";
|
|
4
|
+
export declare const CLASS_STYLES: string[];
|
|
5
|
+
export declare const SLOT_STYLES: string[];
|
|
6
|
+
export declare const DEFAULT_CONVENTIONS: Conventions;
|
|
7
|
+
/** "a Winery" / "an Author" */
|
|
8
|
+
export declare const an: (w: string) => string;
|
|
9
|
+
export declare function convert(name: string, style: string): string;
|
|
10
|
+
export declare function matches(name: string, style: string | null | undefined): boolean;
|
|
11
|
+
export declare function isPlural(word: string): boolean;
|
|
12
|
+
export declare function singularize(word: string): string;
|
|
13
|
+
export declare function pluralize(word: string): string;
|
|
14
|
+
export declare function headWord(name: string): string;
|
|
15
|
+
/** Apply fn to the last word of name, preserving the rest of the name's layout. */
|
|
16
|
+
export declare function withHead(name: string, fn: (w: string) => string): string;
|
|
17
|
+
/** Apply the slot affix convention to a proposed slot name. */
|
|
18
|
+
export declare function applySlotAffix(name: string, conv: Conventions): string;
|
|
19
|
+
/** The name a class/slot should have under the conventions (for "did you mean" hints). */
|
|
20
|
+
export declare function conventionalName(name: string, kind: "Class" | "Slot", conv: Conventions): string;
|