@mfdaves/okf-mcp 0.5.0 → 0.6.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/README.md +86 -20
- package/okf/bundles/okf-mcp/interfaces/mcp-tools.md +7 -3
- package/okf/bundles/okf-mcp/overview/okf-mcp.md +1 -1
- package/okf/bundles/okf-mcp/policies/authoring-safety.md +7 -1
- package/okf/bundles/okf-mcp/runtime/file-concept-store.md +4 -2
- package/okf/bundles/okf-mcp/runtime/mcp-server.md +1 -1
- package/okf/bundles/okf-mcp/workflows/concept-authoring.md +9 -3
- package/package.json +3 -2
- package/server.json +2 -2
- package/src/cli.js +63 -1
- package/src/index.js +1 -0
- package/src/indexer.js +40 -6
- package/src/live-authoring.js +945 -0
- package/src/mcp-server.js +171 -2
- package/src/search-index.js +83 -0
- package/src/search.js +35 -42
- package/src/store.js +10 -1
- package/src/v02.js +1 -0
package/src/mcp-server.js
CHANGED
|
@@ -18,9 +18,11 @@ const {
|
|
|
18
18
|
validateIndex,
|
|
19
19
|
} = require("./indexer");
|
|
20
20
|
const { searchConcepts } = require("./search");
|
|
21
|
+
const { MAX_QUERY_CHARACTERS, MAX_QUERY_TERMS } = require("./search-index");
|
|
21
22
|
const { exportGraph, findPaths, getGraph, getNeighbors, getSubgraph, graphSummary } = require("./graph");
|
|
22
23
|
const { fetchGitHubBundle, fetchRemoteBundles, sanitizeRemoteId } = require("./remote");
|
|
23
24
|
const { ConceptAuthoringService } = require("./authoring");
|
|
25
|
+
const { LiveAuthoringService, MAX_CHANGES } = require("./live-authoring");
|
|
24
26
|
const { FileConceptStore } = require("./store");
|
|
25
27
|
const { loadProjectConfig } = require("./project");
|
|
26
28
|
const {
|
|
@@ -115,6 +117,107 @@ function objectParameter(description, extra) {
|
|
|
115
117
|
}, extra || {});
|
|
116
118
|
}
|
|
117
119
|
|
|
120
|
+
function relationParameter(description) {
|
|
121
|
+
return {
|
|
122
|
+
type: "object",
|
|
123
|
+
description,
|
|
124
|
+
additionalProperties: false,
|
|
125
|
+
properties: {
|
|
126
|
+
type: nonEmptyStringParameter("Typed relation name."),
|
|
127
|
+
target: nonEmptyStringParameter("Relation target URI or bundle-local path."),
|
|
128
|
+
label: stringParameter("Optional human-readable relation label."),
|
|
129
|
+
description: stringParameter("Optional relation explanation."),
|
|
130
|
+
},
|
|
131
|
+
required: ["type", "target"],
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function sourceParameter(description) {
|
|
136
|
+
return {
|
|
137
|
+
description,
|
|
138
|
+
anyOf: [
|
|
139
|
+
nonEmptyStringParameter("Source resource URI or bundle-local path."),
|
|
140
|
+
objectParameter("Structured OKF source entry."),
|
|
141
|
+
],
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function collectionPatchParameter(description, item) {
|
|
146
|
+
return {
|
|
147
|
+
type: "object",
|
|
148
|
+
description,
|
|
149
|
+
additionalProperties: false,
|
|
150
|
+
properties: {
|
|
151
|
+
add: { type: "array", items: item },
|
|
152
|
+
remove: { type: "array", items: item },
|
|
153
|
+
},
|
|
154
|
+
anyOf: [{ required: ["add"] }, { required: ["remove"] }],
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const LIVE_CREATE_CHANGE = {
|
|
159
|
+
type: "object",
|
|
160
|
+
additionalProperties: false,
|
|
161
|
+
properties: {
|
|
162
|
+
op: { type: "string", const: "create", description: "Create a new concept." },
|
|
163
|
+
path: nonEmptyStringParameter("Optional safe bundle-relative Markdown path; derived from type and title when omitted."),
|
|
164
|
+
type: nonEmptyStringParameter("Concept type."),
|
|
165
|
+
title: nonEmptyStringParameter("Concept title."),
|
|
166
|
+
description: stringParameter("Optional concept description."),
|
|
167
|
+
body: stringParameter("Optional Markdown body; defaults to an H1 using the title."),
|
|
168
|
+
tags: stringArrayParameter("Initial tags.", { nonEmptyItems: true }),
|
|
169
|
+
sources: {
|
|
170
|
+
type: "array",
|
|
171
|
+
description: "Initial source resources or structured source entries.",
|
|
172
|
+
items: sourceParameter("Source entry."),
|
|
173
|
+
},
|
|
174
|
+
relations: {
|
|
175
|
+
type: "array",
|
|
176
|
+
description: "Initial typed relationships.",
|
|
177
|
+
items: relationParameter("Typed relation."),
|
|
178
|
+
},
|
|
179
|
+
metadata: objectParameter("Optional extension frontmatter; managed concept fields are rejected."),
|
|
180
|
+
},
|
|
181
|
+
required: ["op", "type", "title"],
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const LIVE_UPDATE_CHANGE = {
|
|
185
|
+
type: "object",
|
|
186
|
+
additionalProperties: false,
|
|
187
|
+
properties: {
|
|
188
|
+
op: { type: "string", const: "update", description: "Update an existing concept in place." },
|
|
189
|
+
uri: nonEmptyStringParameter("Canonical or compatible URI of the existing concept."),
|
|
190
|
+
title: nonEmptyStringParameter("Replacement title."),
|
|
191
|
+
description: stringParameter("Replacement description."),
|
|
192
|
+
body: stringParameter("Replacement Markdown body."),
|
|
193
|
+
tags: collectionPatchParameter(
|
|
194
|
+
"Tags to add and/or remove; additions win when a tag appears in both lists.",
|
|
195
|
+
nonEmptyStringParameter("Tag."),
|
|
196
|
+
),
|
|
197
|
+
sources: collectionPatchParameter(
|
|
198
|
+
"Sources to add/replace and/or remove by id or resource.",
|
|
199
|
+
sourceParameter("Source entry or selector."),
|
|
200
|
+
),
|
|
201
|
+
relations: collectionPatchParameter(
|
|
202
|
+
"Relations to add/replace and/or remove by exact type plus target.",
|
|
203
|
+
relationParameter("Typed relation."),
|
|
204
|
+
),
|
|
205
|
+
metadata: objectParameter("Extension frontmatter fields to add or replace; managed fields are rejected."),
|
|
206
|
+
removeMetadataKeys: stringArrayParameter("Extension frontmatter keys to remove; managed fields are rejected.", { nonEmptyItems: true }),
|
|
207
|
+
},
|
|
208
|
+
required: ["op", "uri"],
|
|
209
|
+
anyOf: [
|
|
210
|
+
{ required: ["title"] },
|
|
211
|
+
{ required: ["description"] },
|
|
212
|
+
{ required: ["body"] },
|
|
213
|
+
{ required: ["tags"] },
|
|
214
|
+
{ required: ["sources"] },
|
|
215
|
+
{ required: ["relations"] },
|
|
216
|
+
{ required: ["metadata"] },
|
|
217
|
+
{ required: ["removeMetadataKeys"] },
|
|
218
|
+
],
|
|
219
|
+
};
|
|
220
|
+
|
|
118
221
|
function defineTool(description, annotations, properties, required, schemaExtras) {
|
|
119
222
|
return {
|
|
120
223
|
description,
|
|
@@ -138,7 +241,7 @@ const TOOL_DEFINITIONS = {
|
|
|
138
241
|
"List compact OKF concept summaries with optional bundle, type, tag, and text filters.",
|
|
139
242
|
READ_ONLY,
|
|
140
243
|
{
|
|
141
|
-
query: stringParameter(
|
|
244
|
+
query: stringParameter(`Optional all-term text query matched against concept metadata and content; maximum ${MAX_QUERY_TERMS} terms.`, { maxLength: MAX_QUERY_CHARACTERS }),
|
|
142
245
|
bundle: nonEmptyStringParameter("Limit results to this bundle id."),
|
|
143
246
|
type: nonEmptyStringParameter("Limit results to this exact concept type."),
|
|
144
247
|
tag: nonEmptyStringParameter("Limit results to concepts containing this tag."),
|
|
@@ -168,7 +271,7 @@ const TOOL_DEFINITIONS = {
|
|
|
168
271
|
"Search OKF concepts and return ranked summaries using text and structured filters.",
|
|
169
272
|
READ_ONLY,
|
|
170
273
|
{
|
|
171
|
-
query: stringParameter(
|
|
274
|
+
query: stringParameter(`All-term text query matched against concept metadata and content; maximum ${MAX_QUERY_TERMS} terms.`, { maxLength: MAX_QUERY_CHARACTERS }),
|
|
172
275
|
bundle: nonEmptyStringParameter("Limit results to this bundle id."),
|
|
173
276
|
types: stringArrayParameter("Limit results to any of these concept types.", { nonEmptyItems: true }),
|
|
174
277
|
tagsAny: stringArrayParameter("Require at least one of these tags.", { nonEmptyItems: true }),
|
|
@@ -411,6 +514,22 @@ const TOOL_DEFINITIONS = {
|
|
|
411
514
|
},
|
|
412
515
|
["proposalId"],
|
|
413
516
|
),
|
|
517
|
+
okf_apply_changes: defineTool(
|
|
518
|
+
"Validate and atomically apply one or more structured concept creates or updates to one local bundle.",
|
|
519
|
+
DESTRUCTIVE_WRITE,
|
|
520
|
+
{
|
|
521
|
+
bundle: nonEmptyStringParameter("Writable bundle id; optional when exactly one local root is configured."),
|
|
522
|
+
message: stringParameter("Optional Git commit summary when automatic commits are enabled."),
|
|
523
|
+
changes: {
|
|
524
|
+
type: "array",
|
|
525
|
+
description: "One through 100 concept create or update operations applied as one batch.",
|
|
526
|
+
minItems: 1,
|
|
527
|
+
maxItems: MAX_CHANGES,
|
|
528
|
+
items: { oneOf: [LIVE_CREATE_CHANGE, LIVE_UPDATE_CHANGE] },
|
|
529
|
+
},
|
|
530
|
+
},
|
|
531
|
+
["changes"],
|
|
532
|
+
),
|
|
414
533
|
get_graph: defineTool(
|
|
415
534
|
"Return a bounded set of OKF graph nodes and edges with optional concept filters.",
|
|
416
535
|
READ_ONLY,
|
|
@@ -512,6 +631,9 @@ const AUTHORING_MUTATION_TOOL_NAMES = new Set([
|
|
|
512
631
|
const COMPUTATION_AUTHORING_TOOL_NAMES = new Set([
|
|
513
632
|
"okf_propose_attested_computation",
|
|
514
633
|
]);
|
|
634
|
+
const LIVE_WRITE_TOOL_NAMES = new Set([
|
|
635
|
+
"okf_apply_changes",
|
|
636
|
+
]);
|
|
515
637
|
const RUNTIME_REMOTE_TOOL_NAMES = new Set([
|
|
516
638
|
"load_remote_bundle",
|
|
517
639
|
]);
|
|
@@ -546,6 +668,9 @@ function toolEnabled(state, name) {
|
|
|
546
668
|
&& state.allowComputationAuthoring,
|
|
547
669
|
);
|
|
548
670
|
}
|
|
671
|
+
if (LIVE_WRITE_TOOL_NAMES.has(name)) {
|
|
672
|
+
return Boolean(state && state.allowWrite && state.liveAuthoringService);
|
|
673
|
+
}
|
|
549
674
|
if (RUNTIME_REMOTE_TOOL_NAMES.has(name)) {
|
|
550
675
|
return Boolean(state && state.allowRuntimeRemoteLoad);
|
|
551
676
|
}
|
|
@@ -718,6 +843,13 @@ function requireAuthoring(state) {
|
|
|
718
843
|
return state.authoringService;
|
|
719
844
|
}
|
|
720
845
|
|
|
846
|
+
function requireLiveAuthoring(state) {
|
|
847
|
+
if (!state.allowWrite || !state.liveAuthoringService) {
|
|
848
|
+
throw new Error("Live OKF authoring is not configured. Start the server with --write and --actor.");
|
|
849
|
+
}
|
|
850
|
+
return state.liveAuthoringService;
|
|
851
|
+
}
|
|
852
|
+
|
|
721
853
|
function rebuildStateIndex(state) {
|
|
722
854
|
const index = buildIndex(
|
|
723
855
|
state.localBundleArgs.concat(state.remoteBundles),
|
|
@@ -911,6 +1043,18 @@ async function callTool(state, name, args) {
|
|
|
911
1043
|
return jsonContent(await expectedToolOperation(
|
|
912
1044
|
() => requireAuthoring(state).rejectProposal(args),
|
|
913
1045
|
));
|
|
1046
|
+
case "okf_apply_changes": {
|
|
1047
|
+
const result = await expectedToolOperation(
|
|
1048
|
+
() => requireLiveAuthoring(state).applyChanges(args, {
|
|
1049
|
+
additionalBundles: state.remoteBundles,
|
|
1050
|
+
}),
|
|
1051
|
+
{ translateTypeError: true },
|
|
1052
|
+
);
|
|
1053
|
+
if (result.applied) {
|
|
1054
|
+
rebuildStateIndex(state);
|
|
1055
|
+
}
|
|
1056
|
+
return jsonContent(result, { isError: result.applied === false });
|
|
1057
|
+
}
|
|
914
1058
|
case "get_graph":
|
|
915
1059
|
return jsonContent(getGraph(index, args));
|
|
916
1060
|
case "get_neighbors":
|
|
@@ -993,6 +1137,8 @@ function createState(bundleArgs, options) {
|
|
|
993
1137
|
relationTypes,
|
|
994
1138
|
project,
|
|
995
1139
|
authoringService,
|
|
1140
|
+
liveAuthoringService: options && options.liveAuthoringService,
|
|
1141
|
+
allowWrite: Boolean(options && options.allowWrite),
|
|
996
1142
|
allowAuthoring: Boolean(options && options.allowAuthoring),
|
|
997
1143
|
allowRuntimeRemoteLoad: Boolean(options && options.allowRuntimeRemoteLoad),
|
|
998
1144
|
allowComputationAuthoring: Boolean(options && options.allowComputationAuthoring),
|
|
@@ -1011,6 +1157,9 @@ async function createStateAsync(bundleArgs, options) {
|
|
|
1011
1157
|
const localBundleArgs = (loaded.bundles || []).filter((bundle) => !bundle.remote);
|
|
1012
1158
|
const store = options.authoringStore || FileConceptStore.fromProject(options.projectPath, { proposalRoot: options.proposalRoot });
|
|
1013
1159
|
const authoringService = options.authoringService || new ConceptAuthoringService(store);
|
|
1160
|
+
const liveAuthoringService = options.liveAuthoringService || (options.allowWrite
|
|
1161
|
+
? new LiveAuthoringService(store, { actor: options.actor, gitCommit: options.gitCommit })
|
|
1162
|
+
: null);
|
|
1014
1163
|
const initialIndex = attachProject(
|
|
1015
1164
|
buildIndex(localBundleArgs.concat(initialRemoteBundles), {
|
|
1016
1165
|
relationTypes: loaded.project.relationTypes,
|
|
@@ -1023,6 +1172,8 @@ async function createStateAsync(bundleArgs, options) {
|
|
|
1023
1172
|
initialRemoteBundles,
|
|
1024
1173
|
relationTypes: loaded.project.relationTypes,
|
|
1025
1174
|
authoringService,
|
|
1175
|
+
liveAuthoringService,
|
|
1176
|
+
allowWrite: options.allowWrite,
|
|
1026
1177
|
allowAuthoring: options.allowAuthoring,
|
|
1027
1178
|
allowRuntimeRemoteLoad: options.allowRuntimeRemoteLoad,
|
|
1028
1179
|
allowComputationAuthoring: options.allowComputationAuthoring,
|
|
@@ -1036,6 +1187,9 @@ async function createStateAsync(bundleArgs, options) {
|
|
|
1036
1187
|
strictLinks: options.strictLinks,
|
|
1037
1188
|
});
|
|
1038
1189
|
const authoringService = options.authoringService || new ConceptAuthoringService(store);
|
|
1190
|
+
const liveAuthoringService = options.liveAuthoringService || (options.allowWrite
|
|
1191
|
+
? new LiveAuthoringService(store, { actor: options.actor, gitCommit: options.gitCommit })
|
|
1192
|
+
: null);
|
|
1039
1193
|
const localBundleArgs = store.getBundles();
|
|
1040
1194
|
const remoteBundles = await fetchRemoteBundles(options.remoteBundles || []);
|
|
1041
1195
|
const initialIndex = attachProject(buildIndex(localBundleArgs.concat(remoteBundles), {
|
|
@@ -1048,6 +1202,8 @@ async function createStateAsync(bundleArgs, options) {
|
|
|
1048
1202
|
initialRemoteBundles: remoteBundles,
|
|
1049
1203
|
relationTypes: store.getRelationTypes(),
|
|
1050
1204
|
authoringService,
|
|
1205
|
+
liveAuthoringService,
|
|
1206
|
+
allowWrite: options.allowWrite,
|
|
1051
1207
|
allowAuthoring: options.allowAuthoring,
|
|
1052
1208
|
allowRuntimeRemoteLoad: options.allowRuntimeRemoteLoad,
|
|
1053
1209
|
allowComputationAuthoring: options.allowComputationAuthoring,
|
|
@@ -1058,6 +1214,17 @@ async function createStateAsync(bundleArgs, options) {
|
|
|
1058
1214
|
}
|
|
1059
1215
|
const remoteBundles = await fetchRemoteBundles((options && options.remoteBundles) || []);
|
|
1060
1216
|
const localBundleArgs = (bundleArgs || []).slice();
|
|
1217
|
+
const liveAuthoringService = options && options.liveAuthoringService
|
|
1218
|
+
? options.liveAuthoringService
|
|
1219
|
+
: options && options.allowWrite && options.authoringService && options.authoringService.store
|
|
1220
|
+
? new LiveAuthoringService(options.authoringService.store, {
|
|
1221
|
+
actor: options.actor,
|
|
1222
|
+
gitCommit: options.gitCommit,
|
|
1223
|
+
})
|
|
1224
|
+
: null;
|
|
1225
|
+
if (options && options.allowWrite && !liveAuthoringService) {
|
|
1226
|
+
throw new Error("Live OKF authoring requires --root or --project and a configured actor.");
|
|
1227
|
+
}
|
|
1061
1228
|
return createState(localBundleArgs, {
|
|
1062
1229
|
initialIndex: buildIndex(localBundleArgs.concat(remoteBundles), {
|
|
1063
1230
|
strictLinks: options && options.strictLinks,
|
|
@@ -1065,6 +1232,8 @@ async function createStateAsync(bundleArgs, options) {
|
|
|
1065
1232
|
}),
|
|
1066
1233
|
initialRemoteBundles: remoteBundles,
|
|
1067
1234
|
authoringService: options && options.authoringService,
|
|
1235
|
+
liveAuthoringService,
|
|
1236
|
+
allowWrite: options && options.allowWrite,
|
|
1068
1237
|
allowAuthoring: options && options.allowAuthoring,
|
|
1069
1238
|
allowRuntimeRemoteLoad: options && options.allowRuntimeRemoteLoad,
|
|
1070
1239
|
allowComputationAuthoring: options && options.allowComputationAuthoring,
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const MiniSearch = require("minisearch");
|
|
4
|
+
|
|
5
|
+
const MAX_QUERY_CHARACTERS = 512;
|
|
6
|
+
const MAX_QUERY_TERMS = 16;
|
|
7
|
+
const SEARCH_FIELDS = Object.freeze([
|
|
8
|
+
"title",
|
|
9
|
+
"type",
|
|
10
|
+
"tags",
|
|
11
|
+
"aliases",
|
|
12
|
+
"description",
|
|
13
|
+
"path",
|
|
14
|
+
"body",
|
|
15
|
+
]);
|
|
16
|
+
const SEARCH_BOOSTS = Object.freeze({
|
|
17
|
+
title: 8,
|
|
18
|
+
type: 5,
|
|
19
|
+
tags: 4,
|
|
20
|
+
aliases: 4,
|
|
21
|
+
description: 3,
|
|
22
|
+
path: 2,
|
|
23
|
+
body: 1,
|
|
24
|
+
});
|
|
25
|
+
const SEARCH_INDEXES = new WeakMap();
|
|
26
|
+
const tokenize = MiniSearch.getDefault("tokenize");
|
|
27
|
+
const processTerm = MiniSearch.getDefault("processTerm");
|
|
28
|
+
|
|
29
|
+
function queryTerms(query) {
|
|
30
|
+
return tokenize(String(query || ""))
|
|
31
|
+
.map((term) => processTerm(term))
|
|
32
|
+
.filter((term) => typeof term === "string" && term.length > 0);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function normalizeSearchQuery(value) {
|
|
36
|
+
const query = String(value || "").trim();
|
|
37
|
+
if (Array.from(query).length > MAX_QUERY_CHARACTERS) {
|
|
38
|
+
throw new TypeError(`Search query must not exceed ${MAX_QUERY_CHARACTERS} characters.`);
|
|
39
|
+
}
|
|
40
|
+
const terms = queryTerms(query);
|
|
41
|
+
if (terms.length > MAX_QUERY_TERMS) {
|
|
42
|
+
throw new TypeError(`Search query must not exceed ${MAX_QUERY_TERMS} terms.`);
|
|
43
|
+
}
|
|
44
|
+
return { query, terms };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function searchDocument(doc) {
|
|
48
|
+
return {
|
|
49
|
+
id: doc.uri,
|
|
50
|
+
title: String(doc.title || ""),
|
|
51
|
+
type: String(doc.type || ""),
|
|
52
|
+
tags: (doc.tags || []).join(" "),
|
|
53
|
+
aliases: (doc.aliases || []).join(" "),
|
|
54
|
+
description: String(doc.description || ""),
|
|
55
|
+
path: String(doc.path || ""),
|
|
56
|
+
body: String(doc.body || ""),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function prepareSearchIndex(index) {
|
|
61
|
+
let searchIndex = SEARCH_INDEXES.get(index);
|
|
62
|
+
if (searchIndex) {
|
|
63
|
+
return searchIndex;
|
|
64
|
+
}
|
|
65
|
+
searchIndex = new MiniSearch({
|
|
66
|
+
fields: SEARCH_FIELDS,
|
|
67
|
+
idField: "id",
|
|
68
|
+
searchOptions: {
|
|
69
|
+
boost: SEARCH_BOOSTS,
|
|
70
|
+
combineWith: "AND",
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
searchIndex.addAll(index.concepts.map(searchDocument));
|
|
74
|
+
SEARCH_INDEXES.set(index, searchIndex);
|
|
75
|
+
return searchIndex;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
module.exports = {
|
|
79
|
+
MAX_QUERY_CHARACTERS,
|
|
80
|
+
MAX_QUERY_TERMS,
|
|
81
|
+
normalizeSearchQuery,
|
|
82
|
+
prepareSearchIndex,
|
|
83
|
+
};
|
package/src/search.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const { conceptSummary } = require("./indexer");
|
|
4
4
|
const { normalizeV02Signals } = require("./v02");
|
|
5
5
|
const { resolveConcept } = require("./indexer");
|
|
6
|
+
const { normalizeSearchQuery, prepareSearchIndex } = require("./search-index");
|
|
6
7
|
|
|
7
8
|
function lower(value) {
|
|
8
9
|
return String(value || "").toLowerCase();
|
|
@@ -34,22 +35,30 @@ function docText(doc) {
|
|
|
34
35
|
doc.path,
|
|
35
36
|
doc.tags.join(" "),
|
|
36
37
|
doc.aliases.join(" "),
|
|
37
|
-
JSON.stringify(doc.frontmatter),
|
|
38
38
|
doc.body,
|
|
39
39
|
].join("\n");
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
function snippetFor(doc,
|
|
43
|
-
if (!
|
|
42
|
+
function snippetFor(doc, terms) {
|
|
43
|
+
if (!terms.length) {
|
|
44
44
|
return "";
|
|
45
45
|
}
|
|
46
46
|
const text = docText(doc).replace(/\s+/g, " ");
|
|
47
|
-
const
|
|
47
|
+
const normalized = lower(text);
|
|
48
|
+
let idx = -1;
|
|
49
|
+
let matchedLength = 0;
|
|
50
|
+
terms.forEach((term) => {
|
|
51
|
+
const candidate = normalized.indexOf(lower(term));
|
|
52
|
+
if (candidate !== -1 && (idx === -1 || candidate < idx)) {
|
|
53
|
+
idx = candidate;
|
|
54
|
+
matchedLength = String(term).length;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
48
57
|
if (idx === -1) {
|
|
49
58
|
return "";
|
|
50
59
|
}
|
|
51
60
|
const start = Math.max(0, idx - 50);
|
|
52
|
-
return text.slice(start, idx +
|
|
61
|
+
return text.slice(start, idx + matchedLength + 80).trim();
|
|
53
62
|
}
|
|
54
63
|
|
|
55
64
|
function linkedSets(index) {
|
|
@@ -166,50 +175,34 @@ function applyFilters(index, options) {
|
|
|
166
175
|
});
|
|
167
176
|
}
|
|
168
177
|
|
|
169
|
-
function
|
|
170
|
-
|
|
171
|
-
return 0;
|
|
172
|
-
}
|
|
173
|
-
const q = lower(query);
|
|
174
|
-
let score = 0;
|
|
175
|
-
if (lower(doc.title).includes(q)) {
|
|
176
|
-
score += 8;
|
|
177
|
-
}
|
|
178
|
-
if (lower(doc.type).includes(q)) {
|
|
179
|
-
score += 5;
|
|
180
|
-
}
|
|
181
|
-
if (doc.tags.some((tag) => lower(tag).includes(q))) {
|
|
182
|
-
score += 4;
|
|
183
|
-
}
|
|
184
|
-
if (doc.aliases.some((alias) => lower(alias).includes(q))) {
|
|
185
|
-
score += 4;
|
|
186
|
-
}
|
|
187
|
-
if (lower(doc.description).includes(q)) {
|
|
188
|
-
score += 3;
|
|
189
|
-
}
|
|
190
|
-
if (lower(doc.path).includes(q)) {
|
|
191
|
-
score += 2;
|
|
192
|
-
}
|
|
193
|
-
if (lower(doc.body).includes(q)) {
|
|
194
|
-
score += 1;
|
|
195
|
-
}
|
|
196
|
-
return score;
|
|
178
|
+
function compareDocs(left, right) {
|
|
179
|
+
return left.path.localeCompare(right.path) || left.uri.localeCompare(right.uri);
|
|
197
180
|
}
|
|
198
181
|
|
|
199
182
|
function searchConcepts(index, options) {
|
|
200
183
|
const config = options || {};
|
|
201
|
-
const query =
|
|
184
|
+
const { query, terms } = normalizeSearchQuery(config.query);
|
|
202
185
|
const limit = Math.max(1, Math.min(Number(config.limit || 25), 250));
|
|
203
186
|
const offset = Math.max(0, Number(config.offset || 0));
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
score: scoreDoc(doc, query),
|
|
207
|
-
}));
|
|
187
|
+
const filteredDocs = applyFilters(index, config);
|
|
188
|
+
let results;
|
|
208
189
|
if (query) {
|
|
209
|
-
|
|
210
|
-
|
|
190
|
+
if (!terms.length) {
|
|
191
|
+
results = [];
|
|
192
|
+
} else {
|
|
193
|
+
const docsByUri = new Map(filteredDocs.map((doc) => [doc.uri, doc]));
|
|
194
|
+
const allowedUris = new Set(docsByUri.keys());
|
|
195
|
+
results = prepareSearchIndex(index).search(query, {
|
|
196
|
+
filter: (result) => allowedUris.has(String(result.id)),
|
|
197
|
+
}).map((result) => ({
|
|
198
|
+
doc: docsByUri.get(String(result.id)),
|
|
199
|
+
score: result.score,
|
|
200
|
+
})).filter((entry) => Boolean(entry.doc));
|
|
201
|
+
results.sort((a, b) => b.score - a.score || compareDocs(a.doc, b.doc));
|
|
202
|
+
}
|
|
211
203
|
} else {
|
|
212
|
-
results.
|
|
204
|
+
results = filteredDocs.map((doc) => ({ doc, score: 0 }));
|
|
205
|
+
results.sort((a, b) => compareDocs(a.doc, b.doc));
|
|
213
206
|
}
|
|
214
207
|
const page = results.slice(offset, offset + limit).map((entry) => {
|
|
215
208
|
const summary = conceptSummary(entry.doc);
|
|
@@ -225,7 +218,7 @@ function searchConcepts(index, options) {
|
|
|
225
218
|
}
|
|
226
219
|
return Object.assign(summary, {
|
|
227
220
|
score: entry.score,
|
|
228
|
-
snippet: snippetFor(entry.doc,
|
|
221
|
+
snippet: snippetFor(entry.doc, terms),
|
|
229
222
|
});
|
|
230
223
|
});
|
|
231
224
|
return {
|
package/src/store.js
CHANGED
|
@@ -169,6 +169,7 @@ class FileConceptStore {
|
|
|
169
169
|
throw new Error("Proposal root cannot be a symbolic link.");
|
|
170
170
|
}
|
|
171
171
|
this.proposalLocks = new Map();
|
|
172
|
+
this.writeQueue = Promise.resolve();
|
|
172
173
|
}
|
|
173
174
|
|
|
174
175
|
static fromProject(projectPath, options) {
|
|
@@ -357,6 +358,12 @@ class FileConceptStore {
|
|
|
357
358
|
}
|
|
358
359
|
}
|
|
359
360
|
|
|
361
|
+
async withWriteLock(operation) {
|
|
362
|
+
const current = this.writeQueue.then(operation, operation);
|
|
363
|
+
this.writeQueue = current.catch(() => {});
|
|
364
|
+
return current;
|
|
365
|
+
}
|
|
366
|
+
|
|
360
367
|
listProposalFiles() {
|
|
361
368
|
const root = this.proposalDirectory("", false);
|
|
362
369
|
if (!fs.existsSync(root)) {
|
|
@@ -454,7 +461,9 @@ class FileConceptStore {
|
|
|
454
461
|
}
|
|
455
462
|
|
|
456
463
|
async acceptProposal(id, authoringService, options) {
|
|
457
|
-
return this.
|
|
464
|
+
return this.withWriteLock(() => (
|
|
465
|
+
this.withProposalLock(id, (canonicalId) => this.acceptProposalUnlocked(canonicalId, authoringService, options))
|
|
466
|
+
));
|
|
458
467
|
}
|
|
459
468
|
|
|
460
469
|
async acceptProposalUnlocked(id, authoringService, options) {
|