@daogee/mdgraph 1.0.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 +25 -0
- package/LICENSE +21 -0
- package/README-ZH.md +203 -0
- package/README.md +203 -0
- package/agent-pack/README.md +26 -0
- package/agent-pack/host-examples.md +71 -0
- package/agent-pack/mcp-config.example.json +14 -0
- package/agent-pack/mdgraph-agent-instructions.md +21 -0
- package/agent-pack/prompts/relationship-trace.md +12 -0
- package/agent-pack/prompts/status-doctor.md +13 -0
- package/agent-pack/prompts/task-start-context.md +15 -0
- package/dist/analysis/doctor-scope.d.ts +8 -0
- package/dist/analysis/doctor-scope.js +109 -0
- package/dist/analysis/doctor.d.ts +148 -0
- package/dist/analysis/doctor.js +919 -0
- package/dist/analysis/status-freshness.d.ts +13 -0
- package/dist/analysis/status-freshness.js +55 -0
- package/dist/benchmark/benchmark.d.ts +118 -0
- package/dist/benchmark/benchmark.js +357 -0
- package/dist/bin/mdgraph.d.ts +2 -0
- package/dist/bin/mdgraph.js +877 -0
- package/dist/bundle/bundle.d.ts +66 -0
- package/dist/bundle/bundle.js +320 -0
- package/dist/config/limits.d.ts +24 -0
- package/dist/config/limits.js +24 -0
- package/dist/config/load-config.d.ts +14 -0
- package/dist/config/load-config.js +177 -0
- package/dist/db/connection.d.ts +15 -0
- package/dist/db/connection.js +145 -0
- package/dist/db/repositories.d.ts +159 -0
- package/dist/db/repositories.js +929 -0
- package/dist/db/schema.sql +110 -0
- package/dist/db/sqlite-adapter.d.ts +18 -0
- package/dist/db/sqlite-adapter.js +60 -0
- package/dist/diff/graph-diff.d.ts +41 -0
- package/dist/diff/graph-diff.js +334 -0
- package/dist/evaluation/retrieval-eval.d.ts +115 -0
- package/dist/evaluation/retrieval-eval.js +399 -0
- package/dist/export/diagram.d.ts +11 -0
- package/dist/export/diagram.js +41 -0
- package/dist/export/graphjson.d.ts +89 -0
- package/dist/export/graphjson.js +299 -0
- package/dist/export/markdown-index.d.ts +21 -0
- package/dist/export/markdown-index.js +89 -0
- package/dist/export/source-bridge.d.ts +22 -0
- package/dist/export/source-bridge.js +83 -0
- package/dist/extraction/entity-extractor.d.ts +12 -0
- package/dist/extraction/entity-extractor.js +153 -0
- package/dist/extraction/graph-builder.d.ts +2 -0
- package/dist/extraction/graph-builder.js +168 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +28 -0
- package/dist/indexer.d.ts +19 -0
- package/dist/indexer.js +100 -0
- package/dist/mcp/debug.d.ts +7 -0
- package/dist/mcp/debug.js +41 -0
- package/dist/mcp/server-instructions.d.ts +2 -0
- package/dist/mcp/server-instructions.js +40 -0
- package/dist/mcp/server.d.ts +28 -0
- package/dist/mcp/server.js +232 -0
- package/dist/mcp/tools.d.ts +31 -0
- package/dist/mcp/tools.js +506 -0
- package/dist/mcp/transport.d.ts +50 -0
- package/dist/mcp/transport.js +88 -0
- package/dist/parser/frontmatter.d.ts +10 -0
- package/dist/parser/frontmatter.js +191 -0
- package/dist/parser/links.d.ts +6 -0
- package/dist/parser/links.js +9 -0
- package/dist/parser/markdown-parser.d.ts +2 -0
- package/dist/parser/markdown-parser.js +267 -0
- package/dist/query/context-builder.d.ts +85 -0
- package/dist/query/context-builder.js +413 -0
- package/dist/query/search.d.ts +32 -0
- package/dist/query/search.js +237 -0
- package/dist/query/trace.d.ts +10 -0
- package/dist/query/trace.js +55 -0
- package/dist/reporting/report.d.ts +49 -0
- package/dist/reporting/report.js +143 -0
- package/dist/resolution/link-resolver.d.ts +17 -0
- package/dist/resolution/link-resolver.js +113 -0
- package/dist/scanner/file-scanner.d.ts +4 -0
- package/dist/scanner/file-scanner.js +144 -0
- package/dist/semantic/local-embedding.d.ts +4 -0
- package/dist/semantic/local-embedding.js +30 -0
- package/dist/semantic/status.d.ts +19 -0
- package/dist/semantic/status.js +70 -0
- package/dist/semantic/vector-codec.d.ts +3 -0
- package/dist/semantic/vector-codec.js +35 -0
- package/dist/types.d.ts +198 -0
- package/dist/types.js +15 -0
- package/dist/utils/bounded-json.d.ts +9 -0
- package/dist/utils/bounded-json.js +57 -0
- package/dist/utils/content-risk.d.ts +5 -0
- package/dist/utils/content-risk.js +20 -0
- package/dist/utils/fts.d.ts +3 -0
- package/dist/utils/fts.js +52 -0
- package/dist/utils/id.d.ts +2 -0
- package/dist/utils/id.js +8 -0
- package/dist/utils/path-safety.d.ts +4 -0
- package/dist/utils/path-safety.js +25 -0
- package/dist/utils/text.d.ts +7 -0
- package/dist/utils/text.js +36 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +11 -0
- package/dist/watcher/file-watcher.d.ts +11 -0
- package/dist/watcher/file-watcher.js +127 -0
- package/docs/EN/Agent_Integration.md +129 -0
- package/docs/EN/Architecture.md +135 -0
- package/docs/EN/Evaluation_Questions.md +68 -0
- package/docs/EN/Output_Contracts.md +274 -0
- package/docs/EN/Public_Contracts.md +88 -0
- package/docs/EN/Public_Contracts_1.0.md +234 -0
- package/docs/EN/Release_Checklist.md +82 -0
- package/docs/ZH/Agent_Integration.md +105 -0
- package/docs/ZH/Architecture.md +124 -0
- package/docs/ZH/Evaluation_Questions.md +66 -0
- package/docs/ZH/Output_Contracts.md +269 -0
- package/docs/ZH/Public_Contracts.md +81 -0
- package/docs/ZH/Public_Contracts_1.0.md +202 -0
- package/docs/ZH/Release_Checklist.md +83 -0
- package/package.json +94 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { asStringArray } from "../utils/text.js";
|
|
2
|
+
import { parse as parseYaml } from "yaml";
|
|
3
|
+
const documentKinds = new Set([
|
|
4
|
+
"spec",
|
|
5
|
+
"design",
|
|
6
|
+
"adr",
|
|
7
|
+
"api",
|
|
8
|
+
"runbook",
|
|
9
|
+
"incident",
|
|
10
|
+
"meeting",
|
|
11
|
+
"guide",
|
|
12
|
+
"memory",
|
|
13
|
+
"other"
|
|
14
|
+
]);
|
|
15
|
+
const trustTiers = new Set(["authored", "generated", "validated", "external", "untrusted"]);
|
|
16
|
+
export function parseFrontmatterBlock(raw) {
|
|
17
|
+
const text = stripBom(raw);
|
|
18
|
+
const openingLine = readLine(text, 0);
|
|
19
|
+
if (!isYamlDelimiter(openingLine.text)) {
|
|
20
|
+
return { data: {}, body: text, bodyLineOffset: 0, diagnostics: [] };
|
|
21
|
+
}
|
|
22
|
+
let offset = openingLine.nextOffset;
|
|
23
|
+
let lineNumber = 1;
|
|
24
|
+
while (offset < text.length) {
|
|
25
|
+
const line = readLine(text, offset);
|
|
26
|
+
lineNumber += 1;
|
|
27
|
+
if (isYamlDelimiter(line.text)) {
|
|
28
|
+
const yamlText = text.slice(openingLine.nextOffset, line.startOffset);
|
|
29
|
+
const parsed = parseYamlMapping(yamlText, 2);
|
|
30
|
+
const fieldLines = frontmatterFieldLines(yamlText, 2);
|
|
31
|
+
return {
|
|
32
|
+
data: parsed.data,
|
|
33
|
+
body: text.slice(line.nextOffset),
|
|
34
|
+
bodyLineOffset: lineNumber,
|
|
35
|
+
diagnostics: [...parsed.diagnostics, ...diagnoseFrontmatterFields(parsed.data, fieldLines)]
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
offset = line.nextOffset;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
data: {},
|
|
42
|
+
body: text,
|
|
43
|
+
bodyLineOffset: 0,
|
|
44
|
+
diagnostics: [{
|
|
45
|
+
code: "front_matter.unclosed",
|
|
46
|
+
message: "YAML front matter was opened but not closed.",
|
|
47
|
+
line: 1
|
|
48
|
+
}]
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export function normalizeFrontmatter(input) {
|
|
52
|
+
return {
|
|
53
|
+
...input,
|
|
54
|
+
id: stringField(input.id),
|
|
55
|
+
title: stringField(input.title),
|
|
56
|
+
type: documentKindField(input.type),
|
|
57
|
+
status: stringField(input.status),
|
|
58
|
+
tags: asStringArray(input.tags),
|
|
59
|
+
defines: asStringArray(input.defines),
|
|
60
|
+
depends_on: asStringArray(input.depends_on),
|
|
61
|
+
implements: asStringArray(input.implements),
|
|
62
|
+
supersedes: asStringArray(input.supersedes),
|
|
63
|
+
deprecated_by: asStringArray(input.deprecated_by),
|
|
64
|
+
source_refs: asStringArray(input.source_refs),
|
|
65
|
+
trust_tier: trustTierField(input.trust_tier)
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export function diagnoseFrontmatterFields(input, fieldLines = new Map()) {
|
|
69
|
+
const diagnostics = [];
|
|
70
|
+
for (const field of ["id", "title", "status"]) {
|
|
71
|
+
if (field in input && !stringField(input[field])) {
|
|
72
|
+
diagnostics.push(invalidFieldDiagnostic(field, "non-empty string", input[field], fieldLines));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if ("type" in input && !documentKindField(input.type)) {
|
|
76
|
+
diagnostics.push(invalidFieldDiagnostic("type", "known document kind string", input.type, fieldLines));
|
|
77
|
+
}
|
|
78
|
+
if ("trust_tier" in input && !trustTierField(input.trust_tier)) {
|
|
79
|
+
diagnostics.push(invalidFieldDiagnostic("trust_tier", "known trust tier string", input.trust_tier, fieldLines));
|
|
80
|
+
}
|
|
81
|
+
for (const field of ["tags", "defines", "depends_on", "implements", "supersedes", "deprecated_by", "source_refs"]) {
|
|
82
|
+
if (field in input && !isStringOrStringArray(input[field])) {
|
|
83
|
+
diagnostics.push(invalidFieldDiagnostic(field, "string or string array", input[field], fieldLines));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return diagnostics;
|
|
87
|
+
}
|
|
88
|
+
function parseYamlMapping(yamlText, line) {
|
|
89
|
+
if (!yamlText.trim()) {
|
|
90
|
+
return { data: {}, diagnostics: [] };
|
|
91
|
+
}
|
|
92
|
+
let parsed;
|
|
93
|
+
try {
|
|
94
|
+
parsed = parseYaml(yamlText);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
return {
|
|
98
|
+
data: {},
|
|
99
|
+
diagnostics: [{
|
|
100
|
+
code: "front_matter.invalid_yaml",
|
|
101
|
+
message: error instanceof Error ? error.message : "Invalid YAML front matter.",
|
|
102
|
+
line
|
|
103
|
+
}]
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
if (isRecord(parsed)) {
|
|
107
|
+
return { data: parsed, diagnostics: [] };
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
data: {},
|
|
111
|
+
diagnostics: [{
|
|
112
|
+
code: "front_matter.not_mapping",
|
|
113
|
+
message: "YAML front matter must be a mapping.",
|
|
114
|
+
line
|
|
115
|
+
}]
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function stripBom(value) {
|
|
119
|
+
return value.charCodeAt(0) === 0xfeff ? value.slice(1) : value;
|
|
120
|
+
}
|
|
121
|
+
function isYamlDelimiter(line) {
|
|
122
|
+
return /^---[ \t]*$/.test(line);
|
|
123
|
+
}
|
|
124
|
+
function frontmatterFieldLines(yamlText, startLine) {
|
|
125
|
+
const lines = new Map();
|
|
126
|
+
let lineNumber = startLine - 1;
|
|
127
|
+
for (const line of yamlText.split(/\r?\n/)) {
|
|
128
|
+
lineNumber += 1;
|
|
129
|
+
const match = /^([A-Za-z0-9_-]+)\s*:/.exec(line);
|
|
130
|
+
if (match && !lines.has(match[1])) {
|
|
131
|
+
lines.set(match[1], lineNumber);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return lines;
|
|
135
|
+
}
|
|
136
|
+
function readLine(input, startOffset) {
|
|
137
|
+
const newlineIndex = input.indexOf("\n", startOffset);
|
|
138
|
+
if (newlineIndex === -1) {
|
|
139
|
+
return { text: input.slice(startOffset).replace(/\r$/, ""), startOffset, nextOffset: input.length };
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
text: input.slice(startOffset, newlineIndex).replace(/\r$/, ""),
|
|
143
|
+
startOffset,
|
|
144
|
+
nextOffset: newlineIndex + 1
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function isRecord(value) {
|
|
148
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
149
|
+
}
|
|
150
|
+
function stringField(value) {
|
|
151
|
+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
|
152
|
+
}
|
|
153
|
+
function documentKindField(value) {
|
|
154
|
+
if (typeof value !== "string") {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
const normalized = value.trim().toLowerCase();
|
|
158
|
+
return documentKinds.has(normalized) ? normalized : undefined;
|
|
159
|
+
}
|
|
160
|
+
function trustTierField(value) {
|
|
161
|
+
if (typeof value !== "string") {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
const normalized = value.trim().toLowerCase();
|
|
165
|
+
return trustTiers.has(normalized) ? normalized : undefined;
|
|
166
|
+
}
|
|
167
|
+
function isStringOrStringArray(value) {
|
|
168
|
+
if (typeof value === "string" && value.trim()) {
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string" && item.trim());
|
|
172
|
+
}
|
|
173
|
+
function invalidFieldDiagnostic(field, expected, value, fieldLines) {
|
|
174
|
+
return {
|
|
175
|
+
code: "front_matter.invalid_field",
|
|
176
|
+
message: `Front matter field '${field}' must be ${expected}.`,
|
|
177
|
+
line: fieldLines.get(field) ?? 2,
|
|
178
|
+
field,
|
|
179
|
+
expected,
|
|
180
|
+
actual: describeValue(value)
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
function describeValue(value) {
|
|
184
|
+
if (Array.isArray(value)) {
|
|
185
|
+
return `array(${value.map(describeValue).join(",")})`;
|
|
186
|
+
}
|
|
187
|
+
if (value === null) {
|
|
188
|
+
return "null";
|
|
189
|
+
}
|
|
190
|
+
return typeof value;
|
|
191
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function parseWikiLinkTarget(value) {
|
|
2
|
+
const [targetAndAnchor, alias] = value.split("|").map((part) => part.trim());
|
|
3
|
+
const [target, anchor] = targetAndAnchor.split("#").map((part) => part.trim());
|
|
4
|
+
return {
|
|
5
|
+
target,
|
|
6
|
+
anchor: anchor || undefined,
|
|
7
|
+
alias: alias || undefined
|
|
8
|
+
};
|
|
9
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import remarkGfm from "remark-gfm";
|
|
4
|
+
import remarkParse from "remark-parse";
|
|
5
|
+
import { unified } from "unified";
|
|
6
|
+
import { PARSER_LIMITS } from "../config/limits.js";
|
|
7
|
+
import { contentHash, stableId } from "../utils/id.js";
|
|
8
|
+
import { assertInsideRoot } from "../utils/path-safety.js";
|
|
9
|
+
import { relativeUnixPath, slugifyHeading } from "../utils/text.js";
|
|
10
|
+
import { normalizeFrontmatter, parseFrontmatterBlock } from "./frontmatter.js";
|
|
11
|
+
import { parseWikiLinkTarget } from "./links.js";
|
|
12
|
+
export function parseMarkdownDocument(projectRoot, absolutePath) {
|
|
13
|
+
assertInsideRoot(projectRoot, absolutePath, "Markdown file path");
|
|
14
|
+
const raw = fs.readFileSync(absolutePath, "utf8");
|
|
15
|
+
const parsed = parseFrontmatter(raw);
|
|
16
|
+
const body = parsed.content;
|
|
17
|
+
const frontmatter = normalizeFrontmatter(parsed.data);
|
|
18
|
+
const frontmatterDiagnostics = parsed.diagnostics;
|
|
19
|
+
const relativePath = relativeUnixPath(projectRoot, absolutePath);
|
|
20
|
+
const id = stableId("document", frontmatter.id ?? relativePath.toLowerCase());
|
|
21
|
+
const tree = unified().use(remarkParse).use(remarkGfm).parse(body);
|
|
22
|
+
const bodyLineOffset = parsed.bodyLineOffset;
|
|
23
|
+
const bodyLines = body.split(/\r?\n/);
|
|
24
|
+
const headings = collectHeadings(tree, bodyLineOffset);
|
|
25
|
+
const sections = buildSections(id, headings, bodyLines, bodyLineOffset, frontmatter.title ?? path.basename(relativePath));
|
|
26
|
+
const title = frontmatter.title ?? firstH1(headings) ?? path.basename(relativePath, path.extname(relativePath));
|
|
27
|
+
const codeBlockRanges = collectCodeBlockRanges(tree, bodyLineOffset);
|
|
28
|
+
const inlineCodeRanges = collectInlineCodeRanges(tree, bodyLineOffset);
|
|
29
|
+
const markdownLinks = collectMarkdownLinks(tree, sections, bodyLineOffset);
|
|
30
|
+
const wikiLinks = collectWikiLinks(bodyLines, sections, bodyLineOffset, [...codeBlockRanges, ...inlineCodeRanges]);
|
|
31
|
+
const codeBlocks = collectCodeBlocks(tree, sections, bodyLineOffset);
|
|
32
|
+
const inlineCode = collectInlineCode(tree, sections, bodyLineOffset);
|
|
33
|
+
return {
|
|
34
|
+
id,
|
|
35
|
+
absolutePath,
|
|
36
|
+
relativePath,
|
|
37
|
+
title,
|
|
38
|
+
hash: contentHash(raw),
|
|
39
|
+
frontmatter,
|
|
40
|
+
frontmatterDiagnostics,
|
|
41
|
+
body,
|
|
42
|
+
sections,
|
|
43
|
+
markdownLinks,
|
|
44
|
+
wikiLinks,
|
|
45
|
+
codeBlocks,
|
|
46
|
+
inlineCode
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function parseFrontmatter(raw) {
|
|
50
|
+
const parsed = parseFrontmatterBlock(raw);
|
|
51
|
+
return { data: parsed.data, content: parsed.body, bodyLineOffset: parsed.bodyLineOffset, diagnostics: parsed.diagnostics };
|
|
52
|
+
}
|
|
53
|
+
function collectHeadings(tree, bodyLineOffset) {
|
|
54
|
+
const headings = [];
|
|
55
|
+
visit(tree, (node) => {
|
|
56
|
+
if (node.type !== "heading" || !node.depth || !node.position) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const bodyLine = node.position.start.line;
|
|
60
|
+
headings.push({
|
|
61
|
+
text: textOf(node),
|
|
62
|
+
depth: node.depth,
|
|
63
|
+
line: bodyLine + bodyLineOffset,
|
|
64
|
+
bodyLine
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
return headings.filter((heading) => heading.depth <= 4);
|
|
68
|
+
}
|
|
69
|
+
function buildSections(documentId, headings, bodyLines, bodyLineOffset, fallbackTitle) {
|
|
70
|
+
if (!headings.length) {
|
|
71
|
+
const heading = fallbackTitle || "Document";
|
|
72
|
+
return [
|
|
73
|
+
{
|
|
74
|
+
id: stableId("section", `${documentId}:root`),
|
|
75
|
+
anchor: "",
|
|
76
|
+
heading,
|
|
77
|
+
level: 1,
|
|
78
|
+
startLine: bodyLineOffset + 1,
|
|
79
|
+
endLine: bodyLineOffset + bodyLines.length,
|
|
80
|
+
content: bodyLines.join("\n")
|
|
81
|
+
}
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
const slugCounts = new Map();
|
|
85
|
+
return headings.map((heading, index) => {
|
|
86
|
+
const next = headings[index + 1];
|
|
87
|
+
const endBodyLine = next ? next.bodyLine - 1 : bodyLines.length;
|
|
88
|
+
const baseSlug = slugifyHeading(heading.text);
|
|
89
|
+
const count = slugCounts.get(baseSlug) ?? 0;
|
|
90
|
+
slugCounts.set(baseSlug, count + 1);
|
|
91
|
+
const anchor = count === 0 ? baseSlug : `${baseSlug}-${count + 1}`;
|
|
92
|
+
const content = bodyLines.slice(Math.max(heading.bodyLine - 1, 0), Math.max(endBodyLine, heading.bodyLine - 1)).join("\n");
|
|
93
|
+
return {
|
|
94
|
+
id: stableId("section", `${documentId}:${anchor}`),
|
|
95
|
+
anchor,
|
|
96
|
+
heading: heading.text,
|
|
97
|
+
level: heading.depth,
|
|
98
|
+
startLine: heading.line,
|
|
99
|
+
endLine: endBodyLine + bodyLineOffset,
|
|
100
|
+
content
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function collectMarkdownLinks(tree, sections, bodyLineOffset) {
|
|
105
|
+
const links = [];
|
|
106
|
+
visit(tree, (node) => {
|
|
107
|
+
if (node.type !== "link" || !node.url || !node.position) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const line = node.position.start.line + bodyLineOffset;
|
|
111
|
+
links.push({
|
|
112
|
+
text: textOf(node),
|
|
113
|
+
url: node.url,
|
|
114
|
+
title: node.title,
|
|
115
|
+
line,
|
|
116
|
+
sectionId: findSectionId(sections, line)
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
return links;
|
|
120
|
+
}
|
|
121
|
+
function collectWikiLinks(bodyLines, sections, bodyLineOffset, ignoredLineRanges) {
|
|
122
|
+
const wikiLinks = [];
|
|
123
|
+
const pattern = /\[\[([^\]]+)\]\]/g;
|
|
124
|
+
bodyLines.forEach((lineText, index) => {
|
|
125
|
+
const line = index + 1 + bodyLineOffset;
|
|
126
|
+
if (isIgnoredLine(line, ignoredLineRanges)) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
for (const match of lineText.matchAll(pattern)) {
|
|
130
|
+
const matchStartColumn = (match.index ?? 0) + 1;
|
|
131
|
+
const matchEndColumn = matchStartColumn + match[0].length;
|
|
132
|
+
if (isIgnoredRange(line, matchStartColumn, matchEndColumn, ignoredLineRanges)) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
const parsed = parseWikiLinkTarget(match[1]);
|
|
136
|
+
if (!parsed.target) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
wikiLinks.push({
|
|
140
|
+
raw: match[0],
|
|
141
|
+
target: parsed.target,
|
|
142
|
+
anchor: parsed.anchor,
|
|
143
|
+
alias: parsed.alias,
|
|
144
|
+
line,
|
|
145
|
+
sectionId: findSectionId(sections, line)
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
return wikiLinks;
|
|
150
|
+
}
|
|
151
|
+
function collectInlineCodeRanges(tree, bodyLineOffset) {
|
|
152
|
+
const ranges = [];
|
|
153
|
+
visit(tree, (node) => {
|
|
154
|
+
if (node.type !== "inlineCode" || !node.position) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
ranges.push({
|
|
158
|
+
start: node.position.start.line + bodyLineOffset,
|
|
159
|
+
end: node.position.end.line + bodyLineOffset,
|
|
160
|
+
startColumn: node.position.start.column,
|
|
161
|
+
endColumn: node.position.end.column
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
return ranges;
|
|
165
|
+
}
|
|
166
|
+
function collectCodeBlockRanges(tree, bodyLineOffset) {
|
|
167
|
+
const ranges = [];
|
|
168
|
+
visit(tree, (node) => {
|
|
169
|
+
if (node.type !== "code" || !node.position) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
ranges.push({
|
|
173
|
+
start: node.position.start.line + bodyLineOffset,
|
|
174
|
+
end: node.position.end.line + bodyLineOffset
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
return ranges;
|
|
178
|
+
}
|
|
179
|
+
function isIgnoredLine(line, ranges) {
|
|
180
|
+
return ranges.some((range) => range.startColumn === undefined && line >= range.start && line <= range.end);
|
|
181
|
+
}
|
|
182
|
+
function isIgnoredRange(line, startColumn, endColumn, ranges) {
|
|
183
|
+
return ranges.some((range) => {
|
|
184
|
+
if (line < range.start || line > range.end) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
if (range.startColumn === undefined || range.endColumn === undefined) {
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
if (range.start === range.end) {
|
|
191
|
+
return startColumn < range.endColumn && endColumn > range.startColumn;
|
|
192
|
+
}
|
|
193
|
+
if (line === range.start) {
|
|
194
|
+
return endColumn > range.startColumn;
|
|
195
|
+
}
|
|
196
|
+
if (line === range.end) {
|
|
197
|
+
return startColumn < range.endColumn;
|
|
198
|
+
}
|
|
199
|
+
return true;
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
function collectCodeBlocks(tree, sections, bodyLineOffset) {
|
|
203
|
+
const snippets = [];
|
|
204
|
+
visit(tree, (node) => {
|
|
205
|
+
if (node.type !== "code" || typeof node.value !== "string" || !node.position) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const line = node.position.start.line + bodyLineOffset;
|
|
209
|
+
snippets.push({ language: node.lang, value: node.value, line, sectionId: findSectionId(sections, line) });
|
|
210
|
+
});
|
|
211
|
+
return snippets;
|
|
212
|
+
}
|
|
213
|
+
function collectInlineCode(tree, sections, bodyLineOffset) {
|
|
214
|
+
const snippets = [];
|
|
215
|
+
visit(tree, (node) => {
|
|
216
|
+
if (node.type !== "inlineCode" || typeof node.value !== "string" || !node.position) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const line = node.position.start.line + bodyLineOffset;
|
|
220
|
+
snippets.push({ value: node.value, line, sectionId: findSectionId(sections, line) });
|
|
221
|
+
});
|
|
222
|
+
return snippets;
|
|
223
|
+
}
|
|
224
|
+
function firstH1(headings) {
|
|
225
|
+
return headings.find((heading) => heading.depth === 1)?.text;
|
|
226
|
+
}
|
|
227
|
+
function textOf(node) {
|
|
228
|
+
let text = "";
|
|
229
|
+
let visited = 0;
|
|
230
|
+
const stack = [{ node, depth: 0 }];
|
|
231
|
+
while (stack.length) {
|
|
232
|
+
const current = stack.pop();
|
|
233
|
+
assertTraversalBudget(++visited, current.depth);
|
|
234
|
+
if (typeof current.node.value === "string") {
|
|
235
|
+
text += current.node.value;
|
|
236
|
+
}
|
|
237
|
+
const children = current.node.children ?? [];
|
|
238
|
+
for (let index = children.length - 1; index >= 0; index -= 1) {
|
|
239
|
+
stack.push({ node: children[index], depth: current.depth + 1 });
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return text.trim();
|
|
243
|
+
}
|
|
244
|
+
function findSectionId(sections, line) {
|
|
245
|
+
return sections.find((section) => line >= section.startLine && line <= section.endLine)?.id ?? sections[0]?.id;
|
|
246
|
+
}
|
|
247
|
+
function visit(node, callback) {
|
|
248
|
+
let visited = 0;
|
|
249
|
+
const stack = [{ node, depth: 0 }];
|
|
250
|
+
while (stack.length) {
|
|
251
|
+
const current = stack.pop();
|
|
252
|
+
assertTraversalBudget(++visited, current.depth);
|
|
253
|
+
callback(current.node);
|
|
254
|
+
const children = current.node.children ?? [];
|
|
255
|
+
for (let index = children.length - 1; index >= 0; index -= 1) {
|
|
256
|
+
stack.push({ node: children[index], depth: current.depth + 1 });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
function assertTraversalBudget(nodes, depth) {
|
|
261
|
+
if (nodes > PARSER_LIMITS.maxAstNodes) {
|
|
262
|
+
throw new Error(`Markdown AST exceeds node budget (${PARSER_LIMITS.maxAstNodes}).`);
|
|
263
|
+
}
|
|
264
|
+
if (depth > PARSER_LIMITS.maxAstDepth) {
|
|
265
|
+
throw new Error(`Markdown AST exceeds depth budget (${PARSER_LIMITS.maxAstDepth}).`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { EdgeKind, MDGraphConfig, Provenance } from "../types.js";
|
|
2
|
+
import { GraphRepository } from "../db/repositories.js";
|
|
3
|
+
import { type SearchOptions } from "./search.js";
|
|
4
|
+
export declare const CONTEXT_PACKING_STRATEGY: "mmr-style-document-round-robin";
|
|
5
|
+
export interface ContextItem {
|
|
6
|
+
nodeId: string;
|
|
7
|
+
documentId: string;
|
|
8
|
+
sectionId?: string;
|
|
9
|
+
anchor?: string;
|
|
10
|
+
path: string;
|
|
11
|
+
title: string;
|
|
12
|
+
heading?: string;
|
|
13
|
+
lines?: {
|
|
14
|
+
start: number;
|
|
15
|
+
end: number;
|
|
16
|
+
};
|
|
17
|
+
reason: string;
|
|
18
|
+
matchedEntities: string[];
|
|
19
|
+
edgePath?: ContextEdgePathStep[];
|
|
20
|
+
sourceRefs?: ContextSourceRef[];
|
|
21
|
+
riskNotes?: string[];
|
|
22
|
+
content: string;
|
|
23
|
+
}
|
|
24
|
+
export interface ContextEdgePathStep {
|
|
25
|
+
fromId: string;
|
|
26
|
+
fromLabel: string;
|
|
27
|
+
edgeFromId: string;
|
|
28
|
+
edgeToId: string;
|
|
29
|
+
edgeKind: EdgeKind;
|
|
30
|
+
toId: string;
|
|
31
|
+
toLabel: string;
|
|
32
|
+
traversalDirection: "forward" | "reverse";
|
|
33
|
+
confidence: number;
|
|
34
|
+
provenance: Provenance;
|
|
35
|
+
}
|
|
36
|
+
export interface ContextSourceRef {
|
|
37
|
+
path: string;
|
|
38
|
+
edgeKind: Extract<EdgeKind, "IMPLEMENTS" | "REFERENCES_SOURCE">;
|
|
39
|
+
provenance: Provenance;
|
|
40
|
+
confidence: number;
|
|
41
|
+
}
|
|
42
|
+
export interface ContextAutoMode {
|
|
43
|
+
name: "auto" | "manual";
|
|
44
|
+
searchLimit: number;
|
|
45
|
+
maxDepth: number;
|
|
46
|
+
maxChars: number;
|
|
47
|
+
reason: string;
|
|
48
|
+
}
|
|
49
|
+
export interface ContextResult {
|
|
50
|
+
query: string;
|
|
51
|
+
maxChars: number;
|
|
52
|
+
usedChars: number;
|
|
53
|
+
knownFiles?: string[];
|
|
54
|
+
suggestedNextQueries?: string[];
|
|
55
|
+
mode?: ContextAutoMode;
|
|
56
|
+
items: ContextItem[];
|
|
57
|
+
debug?: ContextDebug;
|
|
58
|
+
}
|
|
59
|
+
export interface ContextDebug {
|
|
60
|
+
seedNodes: number;
|
|
61
|
+
visitedNodes: number;
|
|
62
|
+
expandedEdges: number;
|
|
63
|
+
skippedVisitedNodes: number;
|
|
64
|
+
skippedByNodeLimit: number;
|
|
65
|
+
skippedByDepth: number;
|
|
66
|
+
candidateCount: number;
|
|
67
|
+
directCandidates: number;
|
|
68
|
+
expandedCandidates: number;
|
|
69
|
+
packingStrategy: typeof CONTEXT_PACKING_STRATEGY;
|
|
70
|
+
packedItems: number;
|
|
71
|
+
packedUniqueDocuments: number;
|
|
72
|
+
packingDiversityRatio: number;
|
|
73
|
+
budgetTruncatedItems: number;
|
|
74
|
+
budgetSkippedItems: number;
|
|
75
|
+
}
|
|
76
|
+
export interface ContextBuildOptions {
|
|
77
|
+
debug?: boolean;
|
|
78
|
+
maxChars?: number;
|
|
79
|
+
knownFiles?: string[];
|
|
80
|
+
searchLimit?: number;
|
|
81
|
+
maxDepth?: number;
|
|
82
|
+
mode?: ContextAutoMode;
|
|
83
|
+
searchOptions?: SearchOptions;
|
|
84
|
+
}
|
|
85
|
+
export declare function buildContext(repository: GraphRepository, config: MDGraphConfig, query: string, options?: ContextBuildOptions): ContextResult;
|