@mfdaves/okf-mcp 0.3.2-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +504 -0
- package/bin/okf-mcp.js +13 -0
- package/okf/bundles/okf-mcp/distribution/reference-bundle.md +42 -0
- package/okf/bundles/okf-mcp/index.md +17 -0
- package/okf/bundles/okf-mcp/interfaces/http-authoring-api.md +26 -0
- package/okf/bundles/okf-mcp/interfaces/mcp-tools.md +34 -0
- package/okf/bundles/okf-mcp/overview/okf-mcp.md +28 -0
- package/okf/bundles/okf-mcp/policies/authoring-safety.md +38 -0
- package/okf/bundles/okf-mcp/runtime/file-concept-store.md +30 -0
- package/okf/bundles/okf-mcp/runtime/indexer.md +34 -0
- package/okf/bundles/okf-mcp/runtime/mcp-server.md +32 -0
- package/okf/bundles/okf-mcp/specs/concept-format.md +32 -0
- package/okf/bundles/okf-mcp/workflows/concept-authoring.md +30 -0
- package/okf/bundles/okf-mcp/workflows/concept-update.md +30 -0
- package/okf.project.yaml +5 -0
- package/package.json +55 -0
- package/src/authoring.js +334 -0
- package/src/cli.js +427 -0
- package/src/graph.js +278 -0
- package/src/http-server.js +137 -0
- package/src/index.js +15 -0
- package/src/indexer.js +437 -0
- package/src/mcp-server.js +816 -0
- package/src/parser.js +364 -0
- package/src/plugins.js +209 -0
- package/src/project.js +235 -0
- package/src/remote.js +179 -0
- package/src/search.js +179 -0
- package/src/store.js +341 -0
- package/src/validation.js +117 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const http = require("http");
|
|
4
|
+
const { ConceptAuthoringService } = require("./authoring");
|
|
5
|
+
const { FileConceptStore } = require("./store");
|
|
6
|
+
|
|
7
|
+
function sendJson(res, status, value) {
|
|
8
|
+
const text = JSON.stringify(value, null, 2);
|
|
9
|
+
res.writeHead(status, {
|
|
10
|
+
"content-type": "application/json; charset=utf-8",
|
|
11
|
+
"content-length": Buffer.byteLength(text),
|
|
12
|
+
});
|
|
13
|
+
res.end(text);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function readBody(req, limit) {
|
|
17
|
+
const maxBytes = limit || 1024 * 1024;
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
const chunks = [];
|
|
20
|
+
let total = 0;
|
|
21
|
+
req.on("data", (chunk) => {
|
|
22
|
+
total += chunk.length;
|
|
23
|
+
if (total > maxBytes) {
|
|
24
|
+
reject(new Error("Request body exceeds byte limit."));
|
|
25
|
+
req.destroy();
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
chunks.push(chunk);
|
|
29
|
+
});
|
|
30
|
+
req.on("end", () => {
|
|
31
|
+
const text = Buffer.concat(chunks).toString("utf8");
|
|
32
|
+
if (!text.trim()) {
|
|
33
|
+
resolve({});
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
resolve(JSON.parse(text));
|
|
38
|
+
} catch (error) {
|
|
39
|
+
reject(new Error("Request body must be valid JSON."));
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
req.on("error", reject);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function requireWriteAuth(req, token) {
|
|
47
|
+
if (!token) {
|
|
48
|
+
throw new Error("OKF_WRITE_TOKEN is required for write endpoints.");
|
|
49
|
+
}
|
|
50
|
+
const header = req.headers.authorization || "";
|
|
51
|
+
if (header !== `Bearer ${token}`) {
|
|
52
|
+
const error = new Error("Unauthorized.");
|
|
53
|
+
error.statusCode = 401;
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function createHttpHandler(service, options) {
|
|
59
|
+
const writeToken = options && options.writeToken;
|
|
60
|
+
return async function handle(req, res) {
|
|
61
|
+
const url = new URL(req.url, "http://localhost");
|
|
62
|
+
try {
|
|
63
|
+
if (req.method === "GET" && url.pathname === "/health") {
|
|
64
|
+
sendJson(res, 200, { ok: true });
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (req.method === "GET" && url.pathname === "/v1/bundles") {
|
|
68
|
+
sendJson(res, 200, service.store.getBundles().map((bundle) => ({
|
|
69
|
+
id: bundle.id,
|
|
70
|
+
include: bundle.include || [],
|
|
71
|
+
exclude: bundle.exclude || [],
|
|
72
|
+
})));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (req.method === "POST" && url.pathname === "/v1/concepts/validate") {
|
|
76
|
+
sendJson(res, 200, service.validateConcept(await readBody(req)));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (req.method === "POST" && url.pathname === "/v1/concepts/suggest-path") {
|
|
80
|
+
sendJson(res, 200, service.suggestConceptPath(await readBody(req)));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (req.method === "POST" && url.pathname === "/v1/proposals/update") {
|
|
84
|
+
requireWriteAuth(req, writeToken);
|
|
85
|
+
sendJson(res, 200, await service.proposeUpdate(await readBody(req)));
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (req.method === "POST" && url.pathname === "/v1/proposals") {
|
|
89
|
+
requireWriteAuth(req, writeToken);
|
|
90
|
+
sendJson(res, 200, await service.proposeConcept(await readBody(req)));
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (req.method === "GET" && url.pathname === "/v1/proposals") {
|
|
94
|
+
sendJson(res, 200, await service.listProposals({
|
|
95
|
+
bundle: url.searchParams.get("bundle") || "",
|
|
96
|
+
status: url.searchParams.get("status") || "",
|
|
97
|
+
}));
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const proposalMatch = url.pathname.match(/^\/v1\/proposals\/([^/]+)(?:\/(accept|reject))?$/);
|
|
101
|
+
if (proposalMatch && req.method === "GET" && !proposalMatch[2]) {
|
|
102
|
+
sendJson(res, 200, await service.getProposal({ proposalId: proposalMatch[1] }));
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (proposalMatch && req.method === "POST" && proposalMatch[2] === "accept") {
|
|
106
|
+
requireWriteAuth(req, writeToken);
|
|
107
|
+
sendJson(res, 200, await service.acceptProposal({ proposalId: proposalMatch[1] }));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (proposalMatch && req.method === "POST" && proposalMatch[2] === "reject") {
|
|
111
|
+
requireWriteAuth(req, writeToken);
|
|
112
|
+
const body = await readBody(req);
|
|
113
|
+
sendJson(res, 200, await service.rejectProposal({ proposalId: proposalMatch[1], reason: body.reason || "" }));
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
sendJson(res, 404, { error: "Not found." });
|
|
117
|
+
} catch (error) {
|
|
118
|
+
sendJson(res, error.statusCode || 400, { error: error.message || String(error) });
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function runHttpServer(options) {
|
|
124
|
+
const config = options || {};
|
|
125
|
+
const store = config.store || FileConceptStore.fromProject(config.projectPath, { proposalRoot: config.proposalRoot });
|
|
126
|
+
const service = config.service || new ConceptAuthoringService(store);
|
|
127
|
+
const server = http.createServer(createHttpHandler(service, { writeToken: config.writeToken }));
|
|
128
|
+
const host = config.host || "127.0.0.1";
|
|
129
|
+
const port = config.port === undefined || config.port === "" ? 8765 : Number(config.port);
|
|
130
|
+
await new Promise((resolve) => server.listen(port, host, resolve));
|
|
131
|
+
return { server, service, url: `http://${host}:${server.address().port}` };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = {
|
|
135
|
+
createHttpHandler,
|
|
136
|
+
runHttpServer,
|
|
137
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
...require("./parser"),
|
|
5
|
+
...require("./indexer"),
|
|
6
|
+
...require("./search"),
|
|
7
|
+
...require("./graph"),
|
|
8
|
+
...require("./project"),
|
|
9
|
+
...require("./plugins"),
|
|
10
|
+
...require("./remote"),
|
|
11
|
+
...require("./authoring"),
|
|
12
|
+
...require("./store"),
|
|
13
|
+
...require("./http-server"),
|
|
14
|
+
...require("./mcp-server"),
|
|
15
|
+
};
|
package/src/indexer.js
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const { normalizeSlashes, parseMarkdownFile, parseMarkdownText, safeRelativePath } = require("./parser");
|
|
6
|
+
const { DEFAULT_RELATION_TYPES, loadProjectConfig } = require("./project");
|
|
7
|
+
const { fetchRemoteBundles } = require("./remote");
|
|
8
|
+
const { validateIndex } = require("./validation");
|
|
9
|
+
|
|
10
|
+
function sanitizeBundleId(value, fallback) {
|
|
11
|
+
const raw = String(value || fallback || "bundle").trim();
|
|
12
|
+
return raw.replace(/[^A-Za-z0-9_.-]+/g, "-").replace(/^-+|-+$/g, "") || "bundle";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function parseBundleArg(arg, index) {
|
|
16
|
+
if (arg && typeof arg === "object") {
|
|
17
|
+
return {
|
|
18
|
+
id: sanitizeBundleId(arg.id, `bundle-${index + 1}`),
|
|
19
|
+
root: arg.remote ? "" : path.resolve(arg.root),
|
|
20
|
+
remote: Boolean(arg.remote),
|
|
21
|
+
remoteSource: arg.remoteSource || null,
|
|
22
|
+
documents: Array.isArray(arg.documents) ? arg.documents : [],
|
|
23
|
+
include: Array.isArray(arg.include) ? arg.include : [],
|
|
24
|
+
exclude: Array.isArray(arg.exclude) ? arg.exclude : [],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
const text = String(arg || "");
|
|
28
|
+
const eq = text.indexOf("=");
|
|
29
|
+
if (eq > 0) {
|
|
30
|
+
return {
|
|
31
|
+
id: sanitizeBundleId(text.slice(0, eq), `bundle-${index + 1}`),
|
|
32
|
+
root: path.resolve(text.slice(eq + 1)),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const root = path.resolve(text);
|
|
36
|
+
return {
|
|
37
|
+
id: sanitizeBundleId(path.basename(root), `bundle-${index + 1}`),
|
|
38
|
+
root,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function uniqueBundleIds(bundles) {
|
|
43
|
+
return bundles.map((bundle) => {
|
|
44
|
+
const base = sanitizeBundleId(bundle.id);
|
|
45
|
+
return {
|
|
46
|
+
id: base,
|
|
47
|
+
root: bundle.remote ? "" : path.resolve(bundle.root),
|
|
48
|
+
remote: Boolean(bundle.remote),
|
|
49
|
+
remoteSource: bundle.remoteSource || null,
|
|
50
|
+
documents: Array.isArray(bundle.documents) ? bundle.documents : [],
|
|
51
|
+
include: bundle.include || [],
|
|
52
|
+
exclude: bundle.exclude || [],
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function escapeRegex(value) {
|
|
58
|
+
return String(value).replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function patternToRegex(pattern) {
|
|
62
|
+
const text = normalizeSlashes(pattern);
|
|
63
|
+
let source = "";
|
|
64
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
65
|
+
const char = text[index];
|
|
66
|
+
if (char === "*") {
|
|
67
|
+
if (text[index + 1] === "*") {
|
|
68
|
+
source += ".*";
|
|
69
|
+
index += 1;
|
|
70
|
+
} else {
|
|
71
|
+
source += "[^/]*";
|
|
72
|
+
}
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
source += escapeRegex(char);
|
|
76
|
+
}
|
|
77
|
+
return new RegExp(`^${source}$`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function matchesPattern(relativePath, pattern) {
|
|
81
|
+
const normalizedPath = normalizeSlashes(relativePath);
|
|
82
|
+
const normalizedPattern = normalizeSlashes(pattern);
|
|
83
|
+
if (!normalizedPattern) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if (normalizedPath === normalizedPattern) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
if (normalizedPattern.endsWith("/")) {
|
|
90
|
+
return normalizedPath.startsWith(normalizedPattern);
|
|
91
|
+
}
|
|
92
|
+
if (normalizedPattern.includes("*")) {
|
|
93
|
+
return patternToRegex(normalizedPattern).test(normalizedPath);
|
|
94
|
+
}
|
|
95
|
+
return normalizedPath.startsWith(`${normalizedPattern}/`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function bundleAllowsPath(bundle, relativePath) {
|
|
99
|
+
const include = Array.isArray(bundle.include) ? bundle.include : [];
|
|
100
|
+
const exclude = Array.isArray(bundle.exclude) ? bundle.exclude : [];
|
|
101
|
+
if (include.length && !include.some((pattern) => matchesPattern(relativePath, pattern))) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if (exclude.some((pattern) => matchesPattern(relativePath, pattern))) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function walkMarkdown(root) {
|
|
111
|
+
const out = [];
|
|
112
|
+
if (!fs.existsSync(root)) {
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
const entries = fs.readdirSync(root, { withFileTypes: true });
|
|
116
|
+
entries.forEach((entry) => {
|
|
117
|
+
const full = path.join(root, entry.name);
|
|
118
|
+
if (entry.isDirectory()) {
|
|
119
|
+
out.push.apply(out, walkMarkdown(full));
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (entry.isFile() && entry.name.toLowerCase().endsWith(".md")) {
|
|
123
|
+
out.push(full);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return out.sort();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function resolveLinkPath(bundle, fromPath, href) {
|
|
130
|
+
const clean = String(href || "").split("#")[0];
|
|
131
|
+
if (!clean || clean.startsWith("http://") || clean.startsWith("https://") || clean.startsWith("mailto:")) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
if (bundle.remote) {
|
|
135
|
+
const resolvedPath = clean.startsWith("/")
|
|
136
|
+
? path.posix.normalize(clean.slice(1))
|
|
137
|
+
: path.posix.normalize(path.posix.join(path.posix.dirname(fromPath), clean));
|
|
138
|
+
if (!resolvedPath || resolvedPath === "." || resolvedPath.startsWith("../") || resolvedPath === "..") {
|
|
139
|
+
return {
|
|
140
|
+
outsideRoot: true,
|
|
141
|
+
path: normalizeSlashes(clean),
|
|
142
|
+
uri: null,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
outsideRoot: false,
|
|
147
|
+
path: resolvedPath,
|
|
148
|
+
uri: `okf://${bundle.id}/${resolvedPath}`,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const base = clean.startsWith("/")
|
|
152
|
+
? path.join(bundle.root, clean.slice(1))
|
|
153
|
+
: path.resolve(path.dirname(path.join(bundle.root, fromPath)), clean);
|
|
154
|
+
const relativePath = safeRelativePath(bundle.root, base);
|
|
155
|
+
if (relativePath === null) {
|
|
156
|
+
return {
|
|
157
|
+
outsideRoot: true,
|
|
158
|
+
path: normalizeSlashes(clean),
|
|
159
|
+
uri: null,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
outsideRoot: false,
|
|
164
|
+
path: relativePath,
|
|
165
|
+
uri: `okf://${bundle.id}/${relativePath}`,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function relationFrom(value, doc) {
|
|
170
|
+
if (typeof value === "string") {
|
|
171
|
+
const parts = value.trim().split(/\s+/);
|
|
172
|
+
if (parts.length === 1) {
|
|
173
|
+
return { type: "related_to", target: parts[0], source: doc.uri };
|
|
174
|
+
}
|
|
175
|
+
return { type: parts[0], target: parts.slice(1).join(" "), source: doc.uri };
|
|
176
|
+
}
|
|
177
|
+
if (value && typeof value === "object") {
|
|
178
|
+
return {
|
|
179
|
+
type: value.type || value.kind || "related_to",
|
|
180
|
+
target: value.target || value.to || "",
|
|
181
|
+
source: doc.uri,
|
|
182
|
+
label: value.label || value.text || "",
|
|
183
|
+
description: value.description || "",
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
return { type: "related_to", target: "", source: doc.uri };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function buildIndex(bundleArgs, options) {
|
|
190
|
+
const config = options || {};
|
|
191
|
+
const bundles = uniqueBundleIds((bundleArgs || []).map(parseBundleArg));
|
|
192
|
+
const allowedRelationTypes = new Set((config.relationTypes || DEFAULT_RELATION_TYPES).map(String));
|
|
193
|
+
const documents = [];
|
|
194
|
+
const concepts = [];
|
|
195
|
+
const reserved = [];
|
|
196
|
+
const warnings = [];
|
|
197
|
+
const errors = [];
|
|
198
|
+
const externalReferences = new Map();
|
|
199
|
+
const seenBundleIds = new Set();
|
|
200
|
+
|
|
201
|
+
bundles.forEach((bundle) => {
|
|
202
|
+
if (seenBundleIds.has(bundle.id)) {
|
|
203
|
+
errors.push({ code: "duplicate_bundle_id", bundle: bundle.id, message: "Duplicate bundle id." });
|
|
204
|
+
}
|
|
205
|
+
seenBundleIds.add(bundle.id);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
bundles.forEach((bundle) => {
|
|
209
|
+
if (bundle.remote) {
|
|
210
|
+
(bundle.documents || []).forEach((remoteDoc) => {
|
|
211
|
+
const relativePath = normalizeSlashes(remoteDoc.path);
|
|
212
|
+
if (!bundleAllowsPath(bundle, relativePath)) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
const doc = parseMarkdownText(bundle, relativePath, String(remoteDoc.text || ""), remoteDoc.source || relativePath);
|
|
217
|
+
documents.push(doc);
|
|
218
|
+
if (doc.reserved) {
|
|
219
|
+
reserved.push(doc);
|
|
220
|
+
} else if (doc.valid) {
|
|
221
|
+
concepts.push(doc);
|
|
222
|
+
}
|
|
223
|
+
warnings.push.apply(warnings, doc.warnings.map((warning) => Object.assign({ bundle: doc.bundle }, warning)));
|
|
224
|
+
} catch (error) {
|
|
225
|
+
errors.push({
|
|
226
|
+
code: "parse_error",
|
|
227
|
+
bundle: bundle.id,
|
|
228
|
+
path: relativePath,
|
|
229
|
+
message: error.message,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (!fs.existsSync(bundle.root)) {
|
|
236
|
+
errors.push({ code: "missing_bundle_root", bundle: bundle.id, path: bundle.root, message: "Bundle root does not exist." });
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
walkMarkdown(bundle.root).forEach((filePath) => {
|
|
240
|
+
const relativePath = safeRelativePath(bundle.root, filePath) || normalizeSlashes(filePath);
|
|
241
|
+
if (!bundleAllowsPath(bundle, relativePath)) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
try {
|
|
245
|
+
const doc = parseMarkdownFile(bundle, filePath);
|
|
246
|
+
documents.push(doc);
|
|
247
|
+
if (doc.reserved) {
|
|
248
|
+
reserved.push(doc);
|
|
249
|
+
} else if (doc.valid) {
|
|
250
|
+
concepts.push(doc);
|
|
251
|
+
}
|
|
252
|
+
warnings.push.apply(warnings, doc.warnings.map((warning) => Object.assign({ bundle: doc.bundle }, warning)));
|
|
253
|
+
} catch (error) {
|
|
254
|
+
errors.push({
|
|
255
|
+
code: "parse_error",
|
|
256
|
+
bundle: bundle.id,
|
|
257
|
+
path: relativePath,
|
|
258
|
+
message: error.message,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
const byUri = new Map();
|
|
265
|
+
documents.forEach((doc) => {
|
|
266
|
+
if (byUri.has(doc.uri)) {
|
|
267
|
+
errors.push({ code: "duplicate_uri", uri: doc.uri, message: "Duplicate OKF URI." });
|
|
268
|
+
}
|
|
269
|
+
byUri.set(doc.uri, doc);
|
|
270
|
+
if (doc.pathUri !== doc.uri) {
|
|
271
|
+
if (byUri.has(doc.pathUri)) {
|
|
272
|
+
errors.push({ code: "duplicate_uri", uri: doc.pathUri, message: "Duplicate OKF path URI." });
|
|
273
|
+
} else {
|
|
274
|
+
byUri.set(doc.pathUri, doc);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const edges = [];
|
|
280
|
+
documents.forEach((doc) => {
|
|
281
|
+
const bundle = bundles.find((entry) => entry.id === doc.bundle);
|
|
282
|
+
doc.links.forEach((link) => {
|
|
283
|
+
const resolved = resolveLinkPath(bundle, doc.path, link.href);
|
|
284
|
+
if (!resolved) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (resolved.outsideRoot) {
|
|
288
|
+
warnings.push({
|
|
289
|
+
code: "link_outside_root",
|
|
290
|
+
bundle: doc.bundle,
|
|
291
|
+
path: doc.path,
|
|
292
|
+
href: link.href,
|
|
293
|
+
message: "Markdown link resolves outside bundle root.",
|
|
294
|
+
});
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
const target = byUri.get(resolved.uri);
|
|
298
|
+
const edge = {
|
|
299
|
+
source: doc.uri,
|
|
300
|
+
target: target ? target.uri : resolved.uri,
|
|
301
|
+
kind: "markdown_link",
|
|
302
|
+
text: link.text || "",
|
|
303
|
+
href: link.href,
|
|
304
|
+
broken: !target,
|
|
305
|
+
};
|
|
306
|
+
edges.push(edge);
|
|
307
|
+
if (!target) {
|
|
308
|
+
warnings.push({
|
|
309
|
+
code: "broken_link",
|
|
310
|
+
bundle: doc.bundle,
|
|
311
|
+
path: doc.path,
|
|
312
|
+
href: link.href,
|
|
313
|
+
target: resolved.path,
|
|
314
|
+
message: "Markdown link target does not exist in bundle.",
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
doc.relations.map((relation) => relationFrom(relation, doc)).forEach((relation) => {
|
|
319
|
+
const type = String(relation.type || "related_to");
|
|
320
|
+
const targetUri = String(relation.target || "").trim();
|
|
321
|
+
if (!targetUri) {
|
|
322
|
+
errors.push({ code: "missing_relation_target", bundle: doc.bundle, path: doc.path, relationType: type, message: "Relation has no target." });
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
if (!allowedRelationTypes.has(type)) {
|
|
326
|
+
errors.push({ code: "invalid_relation_type", bundle: doc.bundle, path: doc.path, relationType: type, message: `Unsupported relation type: ${type}` });
|
|
327
|
+
}
|
|
328
|
+
const target = byUri.get(targetUri);
|
|
329
|
+
const isOkfTarget = targetUri.startsWith("okf://");
|
|
330
|
+
const isExternal = !isOkfTarget;
|
|
331
|
+
if (isExternal) {
|
|
332
|
+
externalReferences.set(targetUri, { uri: targetUri, kind: "external" });
|
|
333
|
+
}
|
|
334
|
+
if (isOkfTarget && !target) {
|
|
335
|
+
errors.push({ code: "broken_relation", bundle: doc.bundle, path: doc.path, target: targetUri, relationType: type, message: "Relation target does not exist." });
|
|
336
|
+
}
|
|
337
|
+
edges.push({
|
|
338
|
+
source: doc.uri,
|
|
339
|
+
target: target ? target.uri : targetUri,
|
|
340
|
+
kind: "relation",
|
|
341
|
+
relationType: type,
|
|
342
|
+
text: relation.label || type,
|
|
343
|
+
description: relation.description || "",
|
|
344
|
+
broken: isOkfTarget && !target,
|
|
345
|
+
external: isExternal,
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
const index = {
|
|
351
|
+
bundles,
|
|
352
|
+
documents,
|
|
353
|
+
concepts,
|
|
354
|
+
reserved,
|
|
355
|
+
relationTypes: Array.from(allowedRelationTypes),
|
|
356
|
+
externalReferences: Array.from(externalReferences.values()),
|
|
357
|
+
warnings,
|
|
358
|
+
errors,
|
|
359
|
+
edges,
|
|
360
|
+
byUri,
|
|
361
|
+
};
|
|
362
|
+
return attachValidation(index);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function buildProjectIndex(projectPath) {
|
|
366
|
+
const project = loadProjectConfig(projectPath);
|
|
367
|
+
const index = buildIndex(project.bundles, { relationTypes: project.relationTypes });
|
|
368
|
+
return attachProject(index, project);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async function loadProjectBundles(projectPath, options) {
|
|
372
|
+
const project = loadProjectConfig(projectPath);
|
|
373
|
+
const remoteBundles = await fetchRemoteBundles(project.remoteBundles, options || {});
|
|
374
|
+
return {
|
|
375
|
+
project,
|
|
376
|
+
bundles: project.bundles.concat(remoteBundles),
|
|
377
|
+
remoteBundles,
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
async function buildProjectIndexAsync(projectPath, options) {
|
|
382
|
+
const loaded = await loadProjectBundles(projectPath, options);
|
|
383
|
+
const index = buildIndex(loaded.bundles, { relationTypes: loaded.project.relationTypes });
|
|
384
|
+
return attachProject(index, loaded.project);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function attachProject(index, project) {
|
|
388
|
+
if (!project) {
|
|
389
|
+
return index;
|
|
390
|
+
}
|
|
391
|
+
index.errors.unshift.apply(index.errors, project.errors || []);
|
|
392
|
+
index.project = {
|
|
393
|
+
name: project.project,
|
|
394
|
+
path: project.path,
|
|
395
|
+
root: project.root,
|
|
396
|
+
plugins: project.plugins,
|
|
397
|
+
remoteBundles: project.remoteBundles,
|
|
398
|
+
};
|
|
399
|
+
return attachValidation(index);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function attachValidation(index) {
|
|
403
|
+
const validation = validateIndex(index);
|
|
404
|
+
index.conformant = validation.conformant;
|
|
405
|
+
index.validForProject = validation.validForProject;
|
|
406
|
+
index.valid = validation.valid;
|
|
407
|
+
index.diagnostics = validation.diagnostics;
|
|
408
|
+
return index;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function conceptSummary(doc) {
|
|
412
|
+
return {
|
|
413
|
+
uri: doc.uri,
|
|
414
|
+
bundle: doc.bundle,
|
|
415
|
+
path: doc.path,
|
|
416
|
+
pathUri: doc.pathUri,
|
|
417
|
+
type: doc.type,
|
|
418
|
+
title: doc.title,
|
|
419
|
+
description: doc.description,
|
|
420
|
+
tags: doc.tags,
|
|
421
|
+
aliases: doc.aliases,
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
module.exports = {
|
|
426
|
+
buildIndex,
|
|
427
|
+
buildProjectIndex,
|
|
428
|
+
buildProjectIndexAsync,
|
|
429
|
+
attachProject,
|
|
430
|
+
conceptSummary,
|
|
431
|
+
bundleAllowsPath,
|
|
432
|
+
loadProjectBundles,
|
|
433
|
+
parseBundleArg,
|
|
434
|
+
resolveLinkPath,
|
|
435
|
+
sanitizeBundleId,
|
|
436
|
+
validateIndex,
|
|
437
|
+
};
|