@daogee/mdgraph 1.0.0 → 1.2.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/CHANGELOG.md +51 -0
- package/README-ZH.md +66 -4
- package/README.md +84 -22
- package/agent-pack/README.md +2 -0
- package/agent-pack/mdgraph-agent-instructions.md +3 -1
- package/agent-pack/prompts/wiki-authoring.md +12 -0
- package/agent-pack/skills/wiki-authoring/SKILL.md +38 -0
- package/agent-pack/skills/wiki-authoring/agents/openai.yaml +4 -0
- package/dist/analysis/status-freshness.d.ts +8 -1
- package/dist/analysis/status-freshness.js +19 -6
- package/dist/bin/mdgraph.js +260 -25
- package/dist/config/limits.d.ts +2 -0
- package/dist/config/limits.js +3 -1
- package/dist/config/load-config.js +8 -2
- package/dist/db/connection.js +17 -1
- package/dist/db/repositories.d.ts +55 -4
- package/dist/db/repositories.js +458 -11
- package/dist/db/sqlite-adapter.js +8 -2
- package/dist/evaluation/context-packing-evaluation.d.ts +29 -0
- package/dist/evaluation/context-packing-evaluation.js +134 -0
- package/dist/evaluation/entity-extraction-evaluation.d.ts +38 -0
- package/dist/evaluation/entity-extraction-evaluation.js +118 -0
- package/dist/evaluation/historical-baseline.d.ts +70 -0
- package/dist/evaluation/historical-baseline.js +278 -0
- package/dist/evaluation/related-documents-evaluation.d.ts +26 -0
- package/dist/evaluation/related-documents-evaluation.js +163 -0
- package/dist/evaluation/retrieval-eval.d.ts +25 -5
- package/dist/evaluation/retrieval-eval.js +79 -7
- package/dist/evaluation/structured-query-evaluation.d.ts +27 -0
- package/dist/evaluation/structured-query-evaluation.js +156 -0
- package/dist/extraction/entity-extractor.js +42 -22
- package/dist/extraction/graph-builder.js +2 -18
- package/dist/index.d.ts +24 -4
- package/dist/index.js +12 -4
- package/dist/indexer.js +80 -4
- package/dist/mcp/server.d.ts +4 -0
- package/dist/mcp/server.js +21 -11
- package/dist/mcp/tools.d.ts +5 -1
- package/dist/mcp/tools.js +116 -18
- package/dist/parser/markdown-parser.js +2 -0
- package/dist/query/context-builder.d.ts +32 -2
- package/dist/query/context-builder.js +191 -12
- package/dist/query/knowledge-card.d.ts +90 -0
- package/dist/query/knowledge-card.js +676 -0
- package/dist/query/search.d.ts +5 -1
- package/dist/query/search.js +85 -16
- package/dist/query/structured-query-executor.d.ts +28 -0
- package/dist/query/structured-query-executor.js +138 -0
- package/dist/query/structured-query.d.ts +50 -0
- package/dist/query/structured-query.js +390 -0
- package/dist/relationships/derive-related.d.ts +86 -0
- package/dist/relationships/derive-related.js +322 -0
- package/dist/reporting/report.js +2 -2
- package/dist/semantic/embed-chunks.d.ts +2 -0
- package/dist/semantic/embed-chunks.js +31 -0
- package/dist/semantic/ollama-provider.d.ts +6 -0
- package/dist/semantic/ollama-provider.js +136 -0
- package/dist/semantic/provider-registry.d.ts +8 -0
- package/dist/semantic/provider-registry.js +66 -0
- package/dist/semantic/provider.d.ts +35 -0
- package/dist/semantic/provider.js +29 -0
- package/dist/semantic/status.d.ts +6 -1
- package/dist/semantic/status.js +49 -3
- package/dist/types.d.ts +7 -2
- package/dist/types.js +2 -1
- package/dist/utils/path-safety.d.ts +3 -0
- package/dist/utils/path-safety.js +27 -0
- package/dist/watcher/file-watcher.d.ts +4 -0
- package/dist/watcher/file-watcher.js +151 -17
- package/dist/watcher/watch-health.d.ts +34 -0
- package/dist/watcher/watch-health.js +75 -0
- package/dist/wiki/wiki-dependencies.d.ts +20 -0
- package/dist/wiki/wiki-dependencies.js +45 -0
- package/dist/wiki/wiki-domains.d.ts +13 -0
- package/dist/wiki/wiki-domains.js +55 -0
- package/dist/wiki/wiki-evidence.d.ts +27 -0
- package/dist/wiki/wiki-evidence.js +112 -0
- package/dist/wiki/wiki-plan.d.ts +121 -0
- package/dist/wiki/wiki-plan.js +616 -0
- package/dist/wiki/wiki-status.d.ts +67 -0
- package/dist/wiki/wiki-status.js +451 -0
- package/docs/EN/Agent_Integration.md +15 -5
- package/docs/EN/Architecture.md +52 -16
- package/docs/EN/Evaluation_Questions.md +35 -5
- package/docs/EN/Knowledge_Cards_and_Wiki_Workflow.md +97 -0
- package/docs/EN/Operations.md +111 -0
- package/docs/EN/Output_Contracts.md +83 -10
- package/docs/EN/Public_Contracts.md +25 -16
- package/docs/EN/Public_Contracts_1.0.md +32 -18
- package/docs/EN/README.md +24 -0
- package/docs/EN/Release_Checklist.md +27 -13
- package/docs/EN/Retrieval_and_Context.md +118 -0
- package/docs/EN/Structured_Query_and_Relationships.md +111 -0
- package/docs/ZH/Agent_Integration.md +15 -5
- package/docs/ZH/Architecture.md +52 -21
- package/docs/ZH/Evaluation_Questions.md +36 -6
- package/docs/ZH/Knowledge_Cards_and_Wiki_Workflow.md +99 -0
- package/docs/ZH/Operations.md +111 -0
- package/docs/ZH/Output_Contracts.md +83 -10
- package/docs/ZH/Public_Contracts.md +24 -15
- package/docs/ZH/Public_Contracts_1.0.md +31 -17
- package/docs/ZH/README.md +24 -0
- package/docs/ZH/Release_Checklist.md +27 -13
- package/docs/ZH/Retrieval_and_Context.md +118 -0
- package/docs/ZH/Structured_Query_and_Relationships.md +111 -0
- package/package.json +50 -19
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import { EDGE_WEIGHTS } from "../types.js";
|
|
2
|
+
export const STRUCTURED_QUERY_DEFAULT_LIMIT = 50;
|
|
3
|
+
export const STRUCTURED_QUERY_MAX_LIMIT = 500;
|
|
4
|
+
const STRUCTURED_QUERY_MAX_LENGTH = 10_000;
|
|
5
|
+
const STRUCTURED_QUERY_MAX_TOKENS = 256;
|
|
6
|
+
const STRUCTURED_QUERY_MAX_DEPTH = 24;
|
|
7
|
+
const STRUCTURED_QUERY_MAX_PREDICATES = 64;
|
|
8
|
+
const DOCUMENT_KINDS = new Set(["spec", "design", "adr", "api", "runbook", "incident", "meeting", "guide", "memory", "other"]);
|
|
9
|
+
const TRUST_TIERS = new Set(["authored", "generated", "validated", "external", "untrusted"]);
|
|
10
|
+
export const STRUCTURED_QUERY_HEALTH_VALUES = [
|
|
11
|
+
"dead_link",
|
|
12
|
+
"orphan",
|
|
13
|
+
"stale_source_ref",
|
|
14
|
+
"missing_definition",
|
|
15
|
+
"weakly_linked",
|
|
16
|
+
"possible_contradiction",
|
|
17
|
+
"content_risk"
|
|
18
|
+
];
|
|
19
|
+
export class StructuredQuerySyntaxError extends Error {
|
|
20
|
+
offset;
|
|
21
|
+
name = "StructuredQuerySyntaxError";
|
|
22
|
+
constructor(message, offset) {
|
|
23
|
+
super(`${message} at offset ${offset}`);
|
|
24
|
+
this.offset = offset;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function tokenizeStructuredQuery(input) {
|
|
28
|
+
if (input.length > STRUCTURED_QUERY_MAX_LENGTH) {
|
|
29
|
+
throw new StructuredQuerySyntaxError(`Query exceeds ${STRUCTURED_QUERY_MAX_LENGTH} characters`, STRUCTURED_QUERY_MAX_LENGTH);
|
|
30
|
+
}
|
|
31
|
+
const tokens = [];
|
|
32
|
+
let offset = 0;
|
|
33
|
+
while (offset < input.length) {
|
|
34
|
+
const character = input[offset];
|
|
35
|
+
if (/\s/u.test(character)) {
|
|
36
|
+
offset += 1;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (character === "(") {
|
|
40
|
+
tokens.push({ kind: "left_paren", value: character, offset });
|
|
41
|
+
offset += 1;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (character === ")") {
|
|
45
|
+
tokens.push({ kind: "right_paren", value: character, offset });
|
|
46
|
+
offset += 1;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (character === ",") {
|
|
50
|
+
tokens.push({ kind: "comma", value: character, offset });
|
|
51
|
+
offset += 1;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (character === "\"" || character === "'") {
|
|
55
|
+
const token = quotedToken(input, offset, character);
|
|
56
|
+
tokens.push(token.token);
|
|
57
|
+
offset = token.nextOffset;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const twoCharacters = input.slice(offset, offset + 2);
|
|
61
|
+
if (twoCharacters === "!=" || twoCharacters === "!~" || twoCharacters === "<=" || twoCharacters === ">=") {
|
|
62
|
+
tokens.push({ kind: "operator", value: twoCharacters, offset });
|
|
63
|
+
offset += 2;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (character === "=" || character === ":" || character === "~" || character === "<" || character === ">") {
|
|
67
|
+
tokens.push({ kind: "operator", value: character === ":" ? "=" : character, offset });
|
|
68
|
+
offset += 1;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const start = offset;
|
|
72
|
+
while (offset < input.length && !/[\s(),=!~<>:'"]/u.test(input[offset])) {
|
|
73
|
+
offset += 1;
|
|
74
|
+
}
|
|
75
|
+
if (offset === start) {
|
|
76
|
+
throw new StructuredQuerySyntaxError(`Unexpected character '${character}'`, offset);
|
|
77
|
+
}
|
|
78
|
+
tokens.push({ kind: "word", value: input.slice(start, offset), offset: start });
|
|
79
|
+
if (tokens.length > STRUCTURED_QUERY_MAX_TOKENS) {
|
|
80
|
+
throw new StructuredQuerySyntaxError(`Query exceeds ${STRUCTURED_QUERY_MAX_TOKENS} tokens`, offset);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (tokens.length > STRUCTURED_QUERY_MAX_TOKENS) {
|
|
84
|
+
throw new StructuredQuerySyntaxError(`Query exceeds ${STRUCTURED_QUERY_MAX_TOKENS} tokens`, input.length);
|
|
85
|
+
}
|
|
86
|
+
return tokens;
|
|
87
|
+
}
|
|
88
|
+
export function parseStructuredQuery(input) {
|
|
89
|
+
const parser = new StructuredQueryParser(tokenizeStructuredQuery(input));
|
|
90
|
+
const ast = parser.parse();
|
|
91
|
+
validateStructuredQuery(ast);
|
|
92
|
+
return ast;
|
|
93
|
+
}
|
|
94
|
+
export function validateStructuredQuery(ast) {
|
|
95
|
+
let predicates = 0;
|
|
96
|
+
visitStructuredExpression(ast.expression, (predicate) => {
|
|
97
|
+
predicates += 1;
|
|
98
|
+
if (predicates > STRUCTURED_QUERY_MAX_PREDICATES) {
|
|
99
|
+
throw new StructuredQuerySyntaxError(`Query exceeds ${STRUCTURED_QUERY_MAX_PREDICATES} predicates`, 0);
|
|
100
|
+
}
|
|
101
|
+
validatePredicate(predicate);
|
|
102
|
+
});
|
|
103
|
+
if (!Number.isSafeInteger(ast.limit) || ast.limit <= 0 || ast.limit > STRUCTURED_QUERY_MAX_LIMIT) {
|
|
104
|
+
throw new StructuredQuerySyntaxError(`LIMIT must be between 1 and ${STRUCTURED_QUERY_MAX_LIMIT}`, 0);
|
|
105
|
+
}
|
|
106
|
+
if (ast.orderBy.length > 3) {
|
|
107
|
+
throw new StructuredQuerySyntaxError("ORDER BY supports at most 3 fields", 0);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
export function structuredQueryUsesHealth(expression) {
|
|
111
|
+
let usesHealth = false;
|
|
112
|
+
visitStructuredExpression(expression, (predicate) => {
|
|
113
|
+
usesHealth ||= predicate.field === "health";
|
|
114
|
+
});
|
|
115
|
+
return usesHealth;
|
|
116
|
+
}
|
|
117
|
+
export function structuredQueryPredicates(expression) {
|
|
118
|
+
const predicates = [];
|
|
119
|
+
visitStructuredExpression(expression, (predicate) => predicates.push(predicate));
|
|
120
|
+
return predicates;
|
|
121
|
+
}
|
|
122
|
+
export function formatStructuredQueryExpression(expression) {
|
|
123
|
+
switch (expression.kind) {
|
|
124
|
+
case "predicate":
|
|
125
|
+
return `${expression.field} ${expression.operator} ${JSON.stringify(expression.value)}`;
|
|
126
|
+
case "not":
|
|
127
|
+
return `NOT (${formatStructuredQueryExpression(expression.expression)})`;
|
|
128
|
+
case "and":
|
|
129
|
+
case "or":
|
|
130
|
+
return `(${formatStructuredQueryExpression(expression.left)} ${expression.kind.toUpperCase()} ${formatStructuredQueryExpression(expression.right)})`;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function quotedToken(input, start, quote) {
|
|
134
|
+
let value = "";
|
|
135
|
+
let offset = start + 1;
|
|
136
|
+
while (offset < input.length) {
|
|
137
|
+
const character = input[offset];
|
|
138
|
+
if (character === quote) {
|
|
139
|
+
return { token: { kind: "string", value, offset: start }, nextOffset: offset + 1 };
|
|
140
|
+
}
|
|
141
|
+
if (character === "\\") {
|
|
142
|
+
const escaped = input[offset + 1];
|
|
143
|
+
if (escaped === undefined) {
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
const escapes = { n: "\n", r: "\r", t: "\t", "\\": "\\", "\"": "\"", "'": "'" };
|
|
147
|
+
value += escapes[escaped] ?? escaped;
|
|
148
|
+
offset += 2;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
value += character;
|
|
152
|
+
offset += 1;
|
|
153
|
+
}
|
|
154
|
+
throw new StructuredQuerySyntaxError("Unterminated quoted string", start);
|
|
155
|
+
}
|
|
156
|
+
class StructuredQueryParser {
|
|
157
|
+
tokens;
|
|
158
|
+
index = 0;
|
|
159
|
+
depth = 0;
|
|
160
|
+
constructor(tokens) {
|
|
161
|
+
this.tokens = tokens;
|
|
162
|
+
}
|
|
163
|
+
parse() {
|
|
164
|
+
if (!this.tokens.length) {
|
|
165
|
+
throw new StructuredQuerySyntaxError("Query is empty", 0);
|
|
166
|
+
}
|
|
167
|
+
const expression = this.parseOr();
|
|
168
|
+
const orderBy = this.consumeKeyword("ORDER") ? this.parseOrderBy() : [];
|
|
169
|
+
const limit = this.consumeKeyword("LIMIT") ? this.parseLimit() : STRUCTURED_QUERY_DEFAULT_LIMIT;
|
|
170
|
+
const remaining = this.peek();
|
|
171
|
+
if (remaining) {
|
|
172
|
+
throw new StructuredQuerySyntaxError(`Unexpected token '${remaining.value}'`, remaining.offset);
|
|
173
|
+
}
|
|
174
|
+
return { expression, orderBy, limit };
|
|
175
|
+
}
|
|
176
|
+
parseOr() {
|
|
177
|
+
let expression = this.parseAnd();
|
|
178
|
+
while (this.consumeKeyword("OR")) {
|
|
179
|
+
expression = { kind: "or", left: expression, right: this.parseAnd() };
|
|
180
|
+
}
|
|
181
|
+
return expression;
|
|
182
|
+
}
|
|
183
|
+
parseAnd() {
|
|
184
|
+
let expression = this.parseUnary();
|
|
185
|
+
while (this.consumeKeyword("AND")) {
|
|
186
|
+
expression = { kind: "and", left: expression, right: this.parseUnary() };
|
|
187
|
+
}
|
|
188
|
+
return expression;
|
|
189
|
+
}
|
|
190
|
+
parseUnary() {
|
|
191
|
+
if (this.consumeKeyword("NOT")) {
|
|
192
|
+
this.depth += 1;
|
|
193
|
+
if (this.depth > STRUCTURED_QUERY_MAX_DEPTH) {
|
|
194
|
+
throw new StructuredQuerySyntaxError(`Query nesting exceeds ${STRUCTURED_QUERY_MAX_DEPTH}`, this.peek()?.offset ?? this.endOffset());
|
|
195
|
+
}
|
|
196
|
+
const expression = this.parseUnary();
|
|
197
|
+
this.depth -= 1;
|
|
198
|
+
return { kind: "not", expression };
|
|
199
|
+
}
|
|
200
|
+
if (this.peek()?.kind === "left_paren") {
|
|
201
|
+
const opening = this.take();
|
|
202
|
+
this.depth += 1;
|
|
203
|
+
if (this.depth > STRUCTURED_QUERY_MAX_DEPTH) {
|
|
204
|
+
throw new StructuredQuerySyntaxError(`Query nesting exceeds ${STRUCTURED_QUERY_MAX_DEPTH}`, opening.offset);
|
|
205
|
+
}
|
|
206
|
+
const expression = this.parseOr();
|
|
207
|
+
const closing = this.take();
|
|
208
|
+
if (closing?.kind !== "right_paren") {
|
|
209
|
+
throw new StructuredQuerySyntaxError("Expected ')'", closing?.offset ?? opening.offset);
|
|
210
|
+
}
|
|
211
|
+
this.depth -= 1;
|
|
212
|
+
return expression;
|
|
213
|
+
}
|
|
214
|
+
return this.parsePredicate();
|
|
215
|
+
}
|
|
216
|
+
parsePredicate() {
|
|
217
|
+
const fieldToken = this.take();
|
|
218
|
+
if (fieldToken?.kind !== "word") {
|
|
219
|
+
throw new StructuredQuerySyntaxError("Expected a query field", fieldToken?.offset ?? this.endOffset());
|
|
220
|
+
}
|
|
221
|
+
const operatorToken = this.take();
|
|
222
|
+
if (operatorToken?.kind !== "operator") {
|
|
223
|
+
throw new StructuredQuerySyntaxError(`Expected an operator after '${fieldToken.value}'`, operatorToken?.offset ?? this.endOffset());
|
|
224
|
+
}
|
|
225
|
+
const valueToken = this.take();
|
|
226
|
+
if (!valueToken || (valueToken.kind !== "word" && valueToken.kind !== "string")) {
|
|
227
|
+
throw new StructuredQuerySyntaxError(`Expected a value after '${operatorToken.value}'`, valueToken?.offset ?? this.endOffset());
|
|
228
|
+
}
|
|
229
|
+
const field = normalizeField(fieldToken.value, fieldToken.offset);
|
|
230
|
+
return {
|
|
231
|
+
kind: "predicate",
|
|
232
|
+
field,
|
|
233
|
+
operator: operatorToken.value,
|
|
234
|
+
value: normalizePredicateValue(field, valueToken.value)
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
parseOrderBy() {
|
|
238
|
+
const by = this.take();
|
|
239
|
+
if (!isKeyword(by, "BY")) {
|
|
240
|
+
throw new StructuredQuerySyntaxError("Expected BY after ORDER", by?.offset ?? this.endOffset());
|
|
241
|
+
}
|
|
242
|
+
const orderBy = [];
|
|
243
|
+
while (true) {
|
|
244
|
+
const fieldToken = this.take();
|
|
245
|
+
if (fieldToken?.kind !== "word") {
|
|
246
|
+
throw new StructuredQuerySyntaxError("Expected an ORDER BY field", fieldToken?.offset ?? this.endOffset());
|
|
247
|
+
}
|
|
248
|
+
const field = normalizeField(fieldToken.value, fieldToken.offset);
|
|
249
|
+
if (!isSortField(field)) {
|
|
250
|
+
throw new StructuredQuerySyntaxError(`Field '${field}' cannot be sorted`, fieldToken.offset);
|
|
251
|
+
}
|
|
252
|
+
const directionToken = this.peek();
|
|
253
|
+
const direction = isKeyword(directionToken, "ASC") || isKeyword(directionToken, "DESC")
|
|
254
|
+
? this.take().value.toLowerCase()
|
|
255
|
+
: "asc";
|
|
256
|
+
orderBy.push({ field, direction });
|
|
257
|
+
if (this.peek()?.kind !== "comma") {
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
this.take();
|
|
261
|
+
}
|
|
262
|
+
return orderBy;
|
|
263
|
+
}
|
|
264
|
+
parseLimit() {
|
|
265
|
+
const token = this.take();
|
|
266
|
+
if (token?.kind !== "word" || !/^\d+$/u.test(token.value)) {
|
|
267
|
+
throw new StructuredQuerySyntaxError("Expected a positive integer after LIMIT", token?.offset ?? this.endOffset());
|
|
268
|
+
}
|
|
269
|
+
return Number(token.value);
|
|
270
|
+
}
|
|
271
|
+
consumeKeyword(keyword) {
|
|
272
|
+
if (!isKeyword(this.peek(), keyword)) {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
this.index += 1;
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
peek() {
|
|
279
|
+
return this.tokens[this.index];
|
|
280
|
+
}
|
|
281
|
+
take() {
|
|
282
|
+
const token = this.peek();
|
|
283
|
+
this.index += token ? 1 : 0;
|
|
284
|
+
return token;
|
|
285
|
+
}
|
|
286
|
+
endOffset() {
|
|
287
|
+
const last = this.tokens.at(-1);
|
|
288
|
+
return last ? last.offset + last.value.length : 0;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function validatePredicate(predicate) {
|
|
292
|
+
if (!predicate.value || predicate.value.length > 1_000) {
|
|
293
|
+
throw new StructuredQuerySyntaxError("Predicate values must contain 1 to 1000 characters", 0);
|
|
294
|
+
}
|
|
295
|
+
const allowed = allowedOperators(predicate.field);
|
|
296
|
+
if (!allowed.has(predicate.operator)) {
|
|
297
|
+
throw new StructuredQuerySyntaxError(`Operator '${predicate.operator}' is not valid for '${predicate.field}'`, 0);
|
|
298
|
+
}
|
|
299
|
+
if ((predicate.field === "updated" || predicate.field === "indexed") && !validDateValue(predicate.value)) {
|
|
300
|
+
throw new StructuredQuerySyntaxError(`Field '${predicate.field}' requires YYYY-MM-DD or an ISO timestamp`, 0);
|
|
301
|
+
}
|
|
302
|
+
if (predicate.field === "type" && !DOCUMENT_KINDS.has(predicate.value)) {
|
|
303
|
+
throw new StructuredQuerySyntaxError(`Unknown document type '${predicate.value}'`, 0);
|
|
304
|
+
}
|
|
305
|
+
if (predicate.field === "trust" && !TRUST_TIERS.has(predicate.value)) {
|
|
306
|
+
throw new StructuredQuerySyntaxError(`Unknown trust tier '${predicate.value}'`, 0);
|
|
307
|
+
}
|
|
308
|
+
if (predicate.field === "edge" && !edgeKind(predicate.value)) {
|
|
309
|
+
throw new StructuredQuerySyntaxError(`Unknown edge kind '${predicate.value}'`, 0);
|
|
310
|
+
}
|
|
311
|
+
if (predicate.field === "health" && !STRUCTURED_QUERY_HEALTH_VALUES.includes(predicate.value.toLowerCase())) {
|
|
312
|
+
throw new StructuredQuerySyntaxError(`Unknown health value '${predicate.value}'`, 0);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
function normalizePredicateValue(field, value) {
|
|
316
|
+
if (field === "edge") {
|
|
317
|
+
return value.toUpperCase();
|
|
318
|
+
}
|
|
319
|
+
if (field === "health" || field === "type" || field === "trust") {
|
|
320
|
+
return value.toLowerCase();
|
|
321
|
+
}
|
|
322
|
+
return value;
|
|
323
|
+
}
|
|
324
|
+
function normalizeField(value, offset) {
|
|
325
|
+
const normalized = value.trim().toLowerCase();
|
|
326
|
+
const aliases = {
|
|
327
|
+
type: "type",
|
|
328
|
+
status: "status",
|
|
329
|
+
trust: "trust",
|
|
330
|
+
trust_tier: "trust",
|
|
331
|
+
path: "path",
|
|
332
|
+
title: "title",
|
|
333
|
+
updated: "updated",
|
|
334
|
+
updated_at: "updated",
|
|
335
|
+
indexed: "indexed",
|
|
336
|
+
indexed_at: "indexed",
|
|
337
|
+
tag: "tag",
|
|
338
|
+
tags: "tag",
|
|
339
|
+
edge: "edge",
|
|
340
|
+
health: "health"
|
|
341
|
+
};
|
|
342
|
+
if (aliases[normalized]) {
|
|
343
|
+
return aliases[normalized];
|
|
344
|
+
}
|
|
345
|
+
if (normalized.startsWith("edge.")) {
|
|
346
|
+
const kind = normalized.slice("edge.".length).toUpperCase();
|
|
347
|
+
if (edgeKind(kind)) {
|
|
348
|
+
return `edge.${kind}`;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
throw new StructuredQuerySyntaxError(`Unknown query field '${value}'`, offset);
|
|
352
|
+
}
|
|
353
|
+
function edgeKind(value) {
|
|
354
|
+
return Object.prototype.hasOwnProperty.call(EDGE_WEIGHTS, value.toUpperCase());
|
|
355
|
+
}
|
|
356
|
+
function allowedOperators(field) {
|
|
357
|
+
if (field === "updated" || field === "indexed") {
|
|
358
|
+
return new Set(["=", "!=", "<", "<=", ">", ">="]);
|
|
359
|
+
}
|
|
360
|
+
if (field === "edge" || field === "health") {
|
|
361
|
+
return new Set(["=", "!="]);
|
|
362
|
+
}
|
|
363
|
+
return new Set(["=", "!=", "~", "!~"]);
|
|
364
|
+
}
|
|
365
|
+
function validDateValue(value) {
|
|
366
|
+
if (/^\d{4}-\d{2}-\d{2}$/u.test(value)) {
|
|
367
|
+
const parsed = new Date(`${value}T00:00:00.000Z`);
|
|
368
|
+
return !Number.isNaN(parsed.getTime()) && parsed.toISOString().slice(0, 10) === value;
|
|
369
|
+
}
|
|
370
|
+
return !Number.isNaN(Date.parse(value)) && /^\d{4}-\d{2}-\d{2}T/u.test(value);
|
|
371
|
+
}
|
|
372
|
+
function isSortField(field) {
|
|
373
|
+
return field === "path" || field === "title" || field === "type" || field === "status"
|
|
374
|
+
|| field === "trust" || field === "updated" || field === "indexed";
|
|
375
|
+
}
|
|
376
|
+
function isKeyword(token, keyword) {
|
|
377
|
+
return token?.kind === "word" && token.value.toUpperCase() === keyword;
|
|
378
|
+
}
|
|
379
|
+
function visitStructuredExpression(expression, visit) {
|
|
380
|
+
if (expression.kind === "predicate") {
|
|
381
|
+
visit(expression);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
if (expression.kind === "not") {
|
|
385
|
+
visitStructuredExpression(expression.expression, visit);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
visitStructuredExpression(expression.left, visit);
|
|
389
|
+
visitStructuredExpression(expression.right, visit);
|
|
390
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { GraphRepository } from "../db/repositories.js";
|
|
2
|
+
import type { MDGraphConfig } from "../types.js";
|
|
3
|
+
export declare const RELATED_TO_ALGORITHM_VERSION = "document-chunk-reciprocal-v1";
|
|
4
|
+
export declare const RELATED_TO_QUALITY_GATE = "semantic-relatedness-v1";
|
|
5
|
+
export declare const RELATED_TO_DEFAULT_THRESHOLD = 0.86;
|
|
6
|
+
export declare const RELATED_TO_MIN_THRESHOLD = 0.75;
|
|
7
|
+
export declare const RELATED_TO_DEFAULT_MAX_NEIGHBORS = 3;
|
|
8
|
+
export declare const RELATED_TO_DEFAULT_MIN_EVIDENCE = 2;
|
|
9
|
+
export declare const RELATED_TO_MAX_DOCUMENT_PAIRS = 250000;
|
|
10
|
+
export declare const RELATED_TO_MAX_VECTOR_COMPARISONS = 2000000;
|
|
11
|
+
export declare const RELATED_TO_MAX_COMPONENT_COMPARISONS = 100000000;
|
|
12
|
+
export declare const RELATED_TO_MAX_CHUNKS_PER_DOCUMENT = 12;
|
|
13
|
+
export interface DeriveRelatedRelationshipsOptions {
|
|
14
|
+
threshold?: number;
|
|
15
|
+
maxNeighbors?: number;
|
|
16
|
+
minEvidence?: number;
|
|
17
|
+
dryRun?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface DerivedRelationshipEvidence {
|
|
20
|
+
fromChunkId: string;
|
|
21
|
+
fromSectionId?: string;
|
|
22
|
+
toChunkId: string;
|
|
23
|
+
toSectionId?: string;
|
|
24
|
+
similarity: number;
|
|
25
|
+
}
|
|
26
|
+
export interface DerivedRelationship {
|
|
27
|
+
fromDocumentId: string;
|
|
28
|
+
fromPath: string;
|
|
29
|
+
toDocumentId: string;
|
|
30
|
+
toPath: string;
|
|
31
|
+
confidence: number;
|
|
32
|
+
evidenceSections: DerivedRelationshipEvidence[];
|
|
33
|
+
}
|
|
34
|
+
export interface DerivedRelationshipReport {
|
|
35
|
+
algorithmVersion: typeof RELATED_TO_ALGORITHM_VERSION;
|
|
36
|
+
generatedAt: string;
|
|
37
|
+
provider: string;
|
|
38
|
+
model: string;
|
|
39
|
+
dimensions: number;
|
|
40
|
+
options: {
|
|
41
|
+
threshold: number;
|
|
42
|
+
maxNeighbors: number;
|
|
43
|
+
minEvidence: number;
|
|
44
|
+
dryRun: boolean;
|
|
45
|
+
};
|
|
46
|
+
qualityGate: {
|
|
47
|
+
id: typeof RELATED_TO_QUALITY_GATE;
|
|
48
|
+
passed: true;
|
|
49
|
+
checks: {
|
|
50
|
+
freshIndex: true;
|
|
51
|
+
semanticModelProvider: true;
|
|
52
|
+
completeVectorCoverage: true;
|
|
53
|
+
thresholdFloor: true;
|
|
54
|
+
multipleIndependentEvidenceSections: true;
|
|
55
|
+
reciprocalTopK: true;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
corpus: {
|
|
59
|
+
documents: number;
|
|
60
|
+
eligibleDocuments: number;
|
|
61
|
+
chunks: number;
|
|
62
|
+
vectors: number;
|
|
63
|
+
invalidVectors: number;
|
|
64
|
+
sampledChunks: number;
|
|
65
|
+
documentsWithSampledChunkTruncation: number;
|
|
66
|
+
documentPairsEvaluated: number;
|
|
67
|
+
centroidComponentComparisons: number;
|
|
68
|
+
evidenceVectorComparisons: number;
|
|
69
|
+
evidenceComponentComparisons: number;
|
|
70
|
+
};
|
|
71
|
+
relationships: DerivedRelationship[];
|
|
72
|
+
relationshipPairs: number;
|
|
73
|
+
directedEdges: number;
|
|
74
|
+
mutation: {
|
|
75
|
+
dryRun: boolean;
|
|
76
|
+
removed: number;
|
|
77
|
+
inserted: number;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export type DerivedRelationshipErrorCode = "embedding_disabled" | "provider_not_semantic" | "stale_index" | "incomplete_vector_coverage" | "invalid_options" | "corpus_too_large";
|
|
81
|
+
export declare class DerivedRelationshipError extends Error {
|
|
82
|
+
readonly code: DerivedRelationshipErrorCode;
|
|
83
|
+
readonly name = "DerivedRelationshipError";
|
|
84
|
+
constructor(code: DerivedRelationshipErrorCode, message: string);
|
|
85
|
+
}
|
|
86
|
+
export declare function deriveRelatedRelationships(repository: GraphRepository, projectRoot: string, config: MDGraphConfig, options?: DeriveRelatedRelationshipsOptions): Promise<DerivedRelationshipReport>;
|