flavor-code 1.2.13 → 1.2.14
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 +3 -0
- package/README.zh-CN.md +3 -0
- package/dist/{app-Z3YWFIBB.js → app-UJAGMKD5.js} +2 -2
- package/dist/astgraph/db.mjs +237 -237
- package/dist/astgraph/extract.mjs +239 -239
- package/dist/astgraph/flavor-plugin.json +25 -25
- package/dist/astgraph/grammars.mjs +68 -68
- package/dist/astgraph/index.js +301 -301
- package/dist/astgraph/indexer.mjs +175 -175
- package/dist/astgraph/query.mjs +165 -165
- package/dist/astgraph/resolve.mjs +93 -93
- package/dist/astgraph/vendor/tree-sitter.js +3980 -3980
- package/dist/astgraph/vendor/zod/LICENSE +21 -21
- package/dist/astgraph/vendor/zod/index.js +4 -4
- package/dist/astgraph/vendor/zod/locales/index.js +1 -1
- package/dist/astgraph/vendor/zod/locales/package.json +7 -7
- package/dist/astgraph/vendor/zod/package.json +135 -135
- package/dist/astgraph/vendor/zod/v4/classic/checks.js +1 -1
- package/dist/astgraph/vendor/zod/v4/classic/coerce.js +17 -17
- package/dist/astgraph/vendor/zod/v4/classic/compat.js +31 -31
- package/dist/astgraph/vendor/zod/v4/classic/errors.js +48 -48
- package/dist/astgraph/vendor/zod/v4/classic/external.js +20 -20
- package/dist/astgraph/vendor/zod/v4/classic/from-json-schema.js +599 -599
- package/dist/astgraph/vendor/zod/v4/classic/index.js +4 -4
- package/dist/astgraph/vendor/zod/v4/classic/iso.js +30 -30
- package/dist/astgraph/vendor/zod/v4/classic/package.json +7 -7
- package/dist/astgraph/vendor/zod/v4/classic/parse.js +15 -15
- package/dist/astgraph/vendor/zod/v4/classic/schemas.js +1395 -1395
- package/dist/astgraph/vendor/zod/v4/core/api.js +1087 -1087
- package/dist/astgraph/vendor/zod/v4/core/checks.js +575 -575
- package/dist/astgraph/vendor/zod/v4/core/core.js +78 -78
- package/dist/astgraph/vendor/zod/v4/core/doc.js +35 -35
- package/dist/astgraph/vendor/zod/v4/core/errors.js +185 -185
- package/dist/astgraph/vendor/zod/v4/core/index.js +16 -16
- package/dist/astgraph/vendor/zod/v4/core/json-schema-generator.js +95 -95
- package/dist/astgraph/vendor/zod/v4/core/json-schema-processors.js +601 -601
- package/dist/astgraph/vendor/zod/v4/core/json-schema.js +1 -1
- package/dist/astgraph/vendor/zod/v4/core/package.json +7 -7
- package/dist/astgraph/vendor/zod/v4/core/parse.js +93 -93
- package/dist/astgraph/vendor/zod/v4/core/regexes.js +139 -139
- package/dist/astgraph/vendor/zod/v4/core/registries.js +51 -51
- package/dist/astgraph/vendor/zod/v4/core/schemas.js +2239 -2239
- package/dist/astgraph/vendor/zod/v4/core/standard-schema.js +1 -1
- package/dist/astgraph/vendor/zod/v4/core/to-json-schema.js +448 -448
- package/dist/astgraph/vendor/zod/v4/core/util.js +674 -674
- package/dist/astgraph/vendor/zod/v4/core/versions.js +5 -5
- package/dist/astgraph/vendor/zod/v4/index.js +3 -3
- package/dist/astgraph/vendor/zod/v4/locales/ar.js +106 -106
- package/dist/astgraph/vendor/zod/v4/locales/az.js +105 -105
- package/dist/astgraph/vendor/zod/v4/locales/be.js +156 -156
- package/dist/astgraph/vendor/zod/v4/locales/bg.js +120 -120
- package/dist/astgraph/vendor/zod/v4/locales/ca.js +107 -107
- package/dist/astgraph/vendor/zod/v4/locales/cs.js +111 -111
- package/dist/astgraph/vendor/zod/v4/locales/da.js +115 -115
- package/dist/astgraph/vendor/zod/v4/locales/de.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/el.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/en.js +113 -113
- package/dist/astgraph/vendor/zod/v4/locales/eo.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/es.js +132 -132
- package/dist/astgraph/vendor/zod/v4/locales/fa.js +114 -114
- package/dist/astgraph/vendor/zod/v4/locales/fi.js +112 -112
- package/dist/astgraph/vendor/zod/v4/locales/fr-CA.js +107 -107
- package/dist/astgraph/vendor/zod/v4/locales/fr.js +125 -125
- package/dist/astgraph/vendor/zod/v4/locales/he.js +214 -214
- package/dist/astgraph/vendor/zod/v4/locales/hr.js +122 -122
- package/dist/astgraph/vendor/zod/v4/locales/hu.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/hy.js +147 -147
- package/dist/astgraph/vendor/zod/v4/locales/id.js +106 -106
- package/dist/astgraph/vendor/zod/v4/locales/index.js +52 -52
- package/dist/astgraph/vendor/zod/v4/locales/is.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/it.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/ja.js +107 -107
- package/dist/astgraph/vendor/zod/v4/locales/ka.js +112 -112
- package/dist/astgraph/vendor/zod/v4/locales/kh.js +5 -5
- package/dist/astgraph/vendor/zod/v4/locales/km.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/ko.js +111 -111
- package/dist/astgraph/vendor/zod/v4/locales/lt.js +203 -203
- package/dist/astgraph/vendor/zod/v4/locales/mk.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/ms.js +107 -107
- package/dist/astgraph/vendor/zod/v4/locales/nl.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/no.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/ota.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/package.json +7 -7
- package/dist/astgraph/vendor/zod/v4/locales/pl.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/ps.js +114 -114
- package/dist/astgraph/vendor/zod/v4/locales/pt.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/ro.js +119 -119
- package/dist/astgraph/vendor/zod/v4/locales/ru.js +156 -156
- package/dist/astgraph/vendor/zod/v4/locales/sl.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/sv.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/ta.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/th.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/tr.js +105 -105
- package/dist/astgraph/vendor/zod/v4/locales/ua.js +5 -5
- package/dist/astgraph/vendor/zod/v4/locales/uk.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/ur.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/uz.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/vi.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/yo.js +107 -107
- package/dist/astgraph/vendor/zod/v4/locales/zh-CN.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/zh-TW.js +107 -107
- package/dist/astgraph/vendor/zod/v4/mini/checks.js +1 -1
- package/dist/astgraph/vendor/zod/v4/mini/coerce.js +22 -22
- package/dist/astgraph/vendor/zod/v4/mini/external.js +14 -14
- package/dist/astgraph/vendor/zod/v4/mini/index.js +3 -3
- package/dist/astgraph/vendor/zod/v4/mini/iso.js +34 -34
- package/dist/astgraph/vendor/zod/v4/mini/package.json +7 -7
- package/dist/astgraph/vendor/zod/v4/mini/parse.js +1 -1
- package/dist/astgraph/vendor/zod/v4/mini/schemas.js +961 -961
- package/dist/astgraph/vendor/zod/v4/package.json +7 -7
- package/dist/{chunk-Y5MZDG7I.js → chunk-4YXDGGIC.js} +4611 -3754
- package/dist/{chunk-XFCJXRJ2.js → chunk-IG4Z23CL.js} +6 -0
- package/dist/{chunk-G6Q2Y2A7.js → chunk-KL6TR2KG.js} +1 -1
- package/dist/cli.js +5 -5
- package/dist/config/schema.d.ts +6 -0
- package/dist/desktop/main.js +4900 -4038
- package/dist/desktop-renderer/assets/{index-CCnSpff0.js → index-CcD0D6x0.js} +1 -1
- package/dist/desktop-renderer/index.html +1 -1
- package/dist/evolve/loader.d.ts +21 -0
- package/dist/evolve/service.d.ts +37 -0
- package/dist/evolve/store.d.ts +91 -0
- package/dist/hooks/types.d.ts +5 -1
- package/dist/{load-6ZMEBHSG.js → load-GKA7P6RJ.js} +1 -1
- package/dist/plugins/host.d.ts +9 -0
- package/dist/plugins/types.d.ts +1 -0
- package/dist/sdk/index.js +3 -3
- package/dist/tools/shell.d.ts +13 -0
- package/dist/ui/commands.d.ts +5 -2
- package/dist/ui/session.d.ts +1 -0
- package/package.json +1 -1
- package//346/212/200/346/234/257/346/226/271/346/241/210/346/212/245/345/221/212.md +303 -1
package/dist/astgraph/query.mjs
CHANGED
|
@@ -1,165 +1,165 @@
|
|
|
1
|
-
// astgraph query layer — anchor search, K-hop traversal, blast radius and
|
|
2
|
-
// subgraph context assembly. All coordinates are 1-based to match Read/editor use.
|
|
3
|
-
|
|
4
|
-
const NODE_FIELDS = "id, kind, name, qualified_name, file_path, language, start_line, end_line, signature, docstring, is_exported, is_async";
|
|
5
|
-
|
|
6
|
-
function mapNode(row) {
|
|
7
|
-
if (row === undefined) return undefined;
|
|
8
|
-
return {
|
|
9
|
-
id: row.id, kind: row.kind, name: row.name, qualifiedName: row.qualified_name,
|
|
10
|
-
filePath: row.file_path, language: row.language,
|
|
11
|
-
startLine: row.start_line, endLine: row.end_line,
|
|
12
|
-
signature: row.signature ?? undefined, docstring: row.docstring ?? undefined,
|
|
13
|
-
isExported: row.is_exported === 1, isAsync: row.is_async === 1,
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** Fetch one node by id, or undefined. */
|
|
18
|
-
export function getNode(db, id) {
|
|
19
|
-
return mapNode(db.prepare(`SELECT ${NODE_FIELDS} FROM nodes WHERE id = ?`).get(id));
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** Split camelCase/PascalCase/snake_case identifiers into lowercase word segments. */
|
|
23
|
-
export function identifierSegments(name) {
|
|
24
|
-
return name
|
|
25
|
-
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
26
|
-
.split(/[^A-Za-z0-9]+/)
|
|
27
|
-
.filter(Boolean)
|
|
28
|
-
.map((segment) => segment.toLowerCase());
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function queryWords(query) {
|
|
32
|
-
return query
|
|
33
|
-
.toLowerCase()
|
|
34
|
-
.split(/[^a-z0-9]+/)
|
|
35
|
-
.filter((word) => word.length > 0);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Anchor search: FTS5 first (prefix-matched terms), then identifier-segment
|
|
40
|
-
* matching so natural-language words ("order cancel") hit camelCase symbols.
|
|
41
|
-
* Returns scored node records, best first.
|
|
42
|
-
*/
|
|
43
|
-
export function search(db, query, { limit = 20 } = {}) {
|
|
44
|
-
const words = queryWords(query);
|
|
45
|
-
if (words.length === 0) return [];
|
|
46
|
-
|
|
47
|
-
const scored = new Map();
|
|
48
|
-
|
|
49
|
-
// FTS pass — each query word as a prefix term.
|
|
50
|
-
try {
|
|
51
|
-
const match = words.map((word) => `${word}*`).join(" ");
|
|
52
|
-
const rows = db.prepare(`
|
|
53
|
-
SELECT n.${NODE_FIELDS.split(", ").join(", n.")}, rank
|
|
54
|
-
FROM nodes_fts
|
|
55
|
-
JOIN nodes n ON n.rowid = nodes_fts.rowid
|
|
56
|
-
WHERE nodes_fts MATCH ?
|
|
57
|
-
ORDER BY rank LIMIT ?
|
|
58
|
-
`).all(match, limit * 2);
|
|
59
|
-
for (const row of rows) scored.set(row.id, { node: mapNode(row), score: 100 - Math.min(50, Math.max(0, row.rank)) });
|
|
60
|
-
} catch { /* Invalid MATCH syntax falls through to segment matching. */ }
|
|
61
|
-
|
|
62
|
-
// Segment pass — words of the query appear as camelCase segments of node names.
|
|
63
|
-
const candidates = db.prepare(`SELECT ${NODE_FIELDS} FROM nodes`).all();
|
|
64
|
-
for (const row of candidates) {
|
|
65
|
-
const segments = identifierSegments(row.name).concat(identifierSegments(row.qualified_name));
|
|
66
|
-
const matched = words.filter((word) => segments.some((segment) => segment === word || segment.startsWith(word)));
|
|
67
|
-
if (matched.length === 0) continue;
|
|
68
|
-
const score = matched.length * 25 + (matched.length === words.length ? 20 : 0);
|
|
69
|
-
const existing = scored.get(row.id);
|
|
70
|
-
if (existing === undefined || existing.score < score) {
|
|
71
|
-
scored.set(row.id, { node: mapNode(row), score });
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return [...scored.values()]
|
|
76
|
-
.sort((a, b) => b.score - a.score)
|
|
77
|
-
.slice(0, limit)
|
|
78
|
-
.map((entry) => entry.node);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Nodes that call the target (reverse call edges). */
|
|
82
|
-
export function callers(db, id) {
|
|
83
|
-
return db.prepare(`
|
|
84
|
-
SELECT DISTINCT n.${NODE_FIELDS.split(", ").join(", n.")}
|
|
85
|
-
FROM edges e JOIN nodes n ON n.id = e.source
|
|
86
|
-
WHERE e.target = ? AND e.kind IN ('calls', 'imports')
|
|
87
|
-
`).all(id).map(mapNode);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** Nodes the target calls (forward call edges). */
|
|
91
|
-
export function callees(db, id) {
|
|
92
|
-
return db.prepare(`
|
|
93
|
-
SELECT DISTINCT n.${NODE_FIELDS.split(", ").join(", n.")}
|
|
94
|
-
FROM edges e JOIN nodes n ON n.id = e.target
|
|
95
|
-
WHERE e.source = ? AND e.kind IN ('calls', 'imports')
|
|
96
|
-
`).all(id).map(mapNode);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* K-hop blast radius from a node.
|
|
101
|
-
* direction "up" — who depends on me (callers chain)
|
|
102
|
-
* direction "down" — what I depend on (callees chain)
|
|
103
|
-
* direction "both" — union
|
|
104
|
-
*/
|
|
105
|
-
export function impact(db, id, { hops = 2, direction = "up", limit = 200 } = {}) {
|
|
106
|
-
const visited = new Map();
|
|
107
|
-
const frontier = [id];
|
|
108
|
-
for (let hop = 1; hop <= hops && frontier.length > 0; hop += 1) {
|
|
109
|
-
const next = [];
|
|
110
|
-
for (const current of frontier) {
|
|
111
|
-
let rows = [];
|
|
112
|
-
if (direction === "up" || direction === "both") {
|
|
113
|
-
rows = rows.concat(db.prepare("SELECT source AS other FROM edges WHERE target = ?").all(current));
|
|
114
|
-
}
|
|
115
|
-
if (direction === "down" || direction === "both") {
|
|
116
|
-
rows = rows.concat(db.prepare("SELECT target AS other FROM edges WHERE source = ?").all(current));
|
|
117
|
-
}
|
|
118
|
-
for (const row of rows) {
|
|
119
|
-
if (row.other === id || visited.has(row.other)) continue;
|
|
120
|
-
visited.set(row.other, hop);
|
|
121
|
-
next.push(row.other);
|
|
122
|
-
if (visited.size >= limit) break;
|
|
123
|
-
}
|
|
124
|
-
if (visited.size >= limit) break;
|
|
125
|
-
}
|
|
126
|
-
frontier.length = 0;
|
|
127
|
-
frontier.push(...next);
|
|
128
|
-
}
|
|
129
|
-
const nodes = [...visited.entries()]
|
|
130
|
-
.map(([nodeId, hop]) => ({ ...getNode(db, nodeId), hop }))
|
|
131
|
-
.filter((node) => node.name !== undefined)
|
|
132
|
-
.sort((a, b) => a.hop - b.hop);
|
|
133
|
-
return { origin: getNode(db, id), nodes, hops };
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Subgraph context assembly: the anchor plus its K-hop callers and callees,
|
|
138
|
-
* rendered as precise file:start-end read ranges for the agent.
|
|
139
|
-
*/
|
|
140
|
-
export function subgraphContext(db, id, { hops = 1, limit = 50 } = {}) {
|
|
141
|
-
const origin = getNode(db, id);
|
|
142
|
-
if (origin === undefined) return { anchor: undefined, context: [] };
|
|
143
|
-
|
|
144
|
-
const result = impact(db, id, { hops, direction: "both", limit });
|
|
145
|
-
const seen = new Map([[origin.filePath, origin]]);
|
|
146
|
-
const entries = [origin, ...result.nodes];
|
|
147
|
-
for (const node of entries) {
|
|
148
|
-
const existing = seen.get(node.filePath);
|
|
149
|
-
if (existing === undefined || node.startLine < existing.startLine) {
|
|
150
|
-
// Prefer the widest node per file only when ranges are identical; otherwise
|
|
151
|
-
// keep each node individually below.
|
|
152
|
-
seen.set(node.filePath, node);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const context = entries
|
|
157
|
-
.map((node) => ({
|
|
158
|
-
nodeId: node.id, name: node.name, kind: node.kind,
|
|
159
|
-
filePath: node.filePath, startLine: node.startLine, endLine: node.endLine,
|
|
160
|
-
signature: node.signature, docstring: node.docstring,
|
|
161
|
-
}))
|
|
162
|
-
.sort((a, b) => a.filePath.localeCompare(b.filePath) || a.startLine - b.startLine);
|
|
163
|
-
|
|
164
|
-
return { anchor: origin, context };
|
|
165
|
-
}
|
|
1
|
+
// astgraph query layer — anchor search, K-hop traversal, blast radius and
|
|
2
|
+
// subgraph context assembly. All coordinates are 1-based to match Read/editor use.
|
|
3
|
+
|
|
4
|
+
const NODE_FIELDS = "id, kind, name, qualified_name, file_path, language, start_line, end_line, signature, docstring, is_exported, is_async";
|
|
5
|
+
|
|
6
|
+
function mapNode(row) {
|
|
7
|
+
if (row === undefined) return undefined;
|
|
8
|
+
return {
|
|
9
|
+
id: row.id, kind: row.kind, name: row.name, qualifiedName: row.qualified_name,
|
|
10
|
+
filePath: row.file_path, language: row.language,
|
|
11
|
+
startLine: row.start_line, endLine: row.end_line,
|
|
12
|
+
signature: row.signature ?? undefined, docstring: row.docstring ?? undefined,
|
|
13
|
+
isExported: row.is_exported === 1, isAsync: row.is_async === 1,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Fetch one node by id, or undefined. */
|
|
18
|
+
export function getNode(db, id) {
|
|
19
|
+
return mapNode(db.prepare(`SELECT ${NODE_FIELDS} FROM nodes WHERE id = ?`).get(id));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Split camelCase/PascalCase/snake_case identifiers into lowercase word segments. */
|
|
23
|
+
export function identifierSegments(name) {
|
|
24
|
+
return name
|
|
25
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
26
|
+
.split(/[^A-Za-z0-9]+/)
|
|
27
|
+
.filter(Boolean)
|
|
28
|
+
.map((segment) => segment.toLowerCase());
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function queryWords(query) {
|
|
32
|
+
return query
|
|
33
|
+
.toLowerCase()
|
|
34
|
+
.split(/[^a-z0-9]+/)
|
|
35
|
+
.filter((word) => word.length > 0);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Anchor search: FTS5 first (prefix-matched terms), then identifier-segment
|
|
40
|
+
* matching so natural-language words ("order cancel") hit camelCase symbols.
|
|
41
|
+
* Returns scored node records, best first.
|
|
42
|
+
*/
|
|
43
|
+
export function search(db, query, { limit = 20 } = {}) {
|
|
44
|
+
const words = queryWords(query);
|
|
45
|
+
if (words.length === 0) return [];
|
|
46
|
+
|
|
47
|
+
const scored = new Map();
|
|
48
|
+
|
|
49
|
+
// FTS pass — each query word as a prefix term.
|
|
50
|
+
try {
|
|
51
|
+
const match = words.map((word) => `${word}*`).join(" ");
|
|
52
|
+
const rows = db.prepare(`
|
|
53
|
+
SELECT n.${NODE_FIELDS.split(", ").join(", n.")}, rank
|
|
54
|
+
FROM nodes_fts
|
|
55
|
+
JOIN nodes n ON n.rowid = nodes_fts.rowid
|
|
56
|
+
WHERE nodes_fts MATCH ?
|
|
57
|
+
ORDER BY rank LIMIT ?
|
|
58
|
+
`).all(match, limit * 2);
|
|
59
|
+
for (const row of rows) scored.set(row.id, { node: mapNode(row), score: 100 - Math.min(50, Math.max(0, row.rank)) });
|
|
60
|
+
} catch { /* Invalid MATCH syntax falls through to segment matching. */ }
|
|
61
|
+
|
|
62
|
+
// Segment pass — words of the query appear as camelCase segments of node names.
|
|
63
|
+
const candidates = db.prepare(`SELECT ${NODE_FIELDS} FROM nodes`).all();
|
|
64
|
+
for (const row of candidates) {
|
|
65
|
+
const segments = identifierSegments(row.name).concat(identifierSegments(row.qualified_name));
|
|
66
|
+
const matched = words.filter((word) => segments.some((segment) => segment === word || segment.startsWith(word)));
|
|
67
|
+
if (matched.length === 0) continue;
|
|
68
|
+
const score = matched.length * 25 + (matched.length === words.length ? 20 : 0);
|
|
69
|
+
const existing = scored.get(row.id);
|
|
70
|
+
if (existing === undefined || existing.score < score) {
|
|
71
|
+
scored.set(row.id, { node: mapNode(row), score });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return [...scored.values()]
|
|
76
|
+
.sort((a, b) => b.score - a.score)
|
|
77
|
+
.slice(0, limit)
|
|
78
|
+
.map((entry) => entry.node);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Nodes that call the target (reverse call edges). */
|
|
82
|
+
export function callers(db, id) {
|
|
83
|
+
return db.prepare(`
|
|
84
|
+
SELECT DISTINCT n.${NODE_FIELDS.split(", ").join(", n.")}
|
|
85
|
+
FROM edges e JOIN nodes n ON n.id = e.source
|
|
86
|
+
WHERE e.target = ? AND e.kind IN ('calls', 'imports')
|
|
87
|
+
`).all(id).map(mapNode);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Nodes the target calls (forward call edges). */
|
|
91
|
+
export function callees(db, id) {
|
|
92
|
+
return db.prepare(`
|
|
93
|
+
SELECT DISTINCT n.${NODE_FIELDS.split(", ").join(", n.")}
|
|
94
|
+
FROM edges e JOIN nodes n ON n.id = e.target
|
|
95
|
+
WHERE e.source = ? AND e.kind IN ('calls', 'imports')
|
|
96
|
+
`).all(id).map(mapNode);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* K-hop blast radius from a node.
|
|
101
|
+
* direction "up" — who depends on me (callers chain)
|
|
102
|
+
* direction "down" — what I depend on (callees chain)
|
|
103
|
+
* direction "both" — union
|
|
104
|
+
*/
|
|
105
|
+
export function impact(db, id, { hops = 2, direction = "up", limit = 200 } = {}) {
|
|
106
|
+
const visited = new Map();
|
|
107
|
+
const frontier = [id];
|
|
108
|
+
for (let hop = 1; hop <= hops && frontier.length > 0; hop += 1) {
|
|
109
|
+
const next = [];
|
|
110
|
+
for (const current of frontier) {
|
|
111
|
+
let rows = [];
|
|
112
|
+
if (direction === "up" || direction === "both") {
|
|
113
|
+
rows = rows.concat(db.prepare("SELECT source AS other FROM edges WHERE target = ?").all(current));
|
|
114
|
+
}
|
|
115
|
+
if (direction === "down" || direction === "both") {
|
|
116
|
+
rows = rows.concat(db.prepare("SELECT target AS other FROM edges WHERE source = ?").all(current));
|
|
117
|
+
}
|
|
118
|
+
for (const row of rows) {
|
|
119
|
+
if (row.other === id || visited.has(row.other)) continue;
|
|
120
|
+
visited.set(row.other, hop);
|
|
121
|
+
next.push(row.other);
|
|
122
|
+
if (visited.size >= limit) break;
|
|
123
|
+
}
|
|
124
|
+
if (visited.size >= limit) break;
|
|
125
|
+
}
|
|
126
|
+
frontier.length = 0;
|
|
127
|
+
frontier.push(...next);
|
|
128
|
+
}
|
|
129
|
+
const nodes = [...visited.entries()]
|
|
130
|
+
.map(([nodeId, hop]) => ({ ...getNode(db, nodeId), hop }))
|
|
131
|
+
.filter((node) => node.name !== undefined)
|
|
132
|
+
.sort((a, b) => a.hop - b.hop);
|
|
133
|
+
return { origin: getNode(db, id), nodes, hops };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Subgraph context assembly: the anchor plus its K-hop callers and callees,
|
|
138
|
+
* rendered as precise file:start-end read ranges for the agent.
|
|
139
|
+
*/
|
|
140
|
+
export function subgraphContext(db, id, { hops = 1, limit = 50 } = {}) {
|
|
141
|
+
const origin = getNode(db, id);
|
|
142
|
+
if (origin === undefined) return { anchor: undefined, context: [] };
|
|
143
|
+
|
|
144
|
+
const result = impact(db, id, { hops, direction: "both", limit });
|
|
145
|
+
const seen = new Map([[origin.filePath, origin]]);
|
|
146
|
+
const entries = [origin, ...result.nodes];
|
|
147
|
+
for (const node of entries) {
|
|
148
|
+
const existing = seen.get(node.filePath);
|
|
149
|
+
if (existing === undefined || node.startLine < existing.startLine) {
|
|
150
|
+
// Prefer the widest node per file only when ranges are identical; otherwise
|
|
151
|
+
// keep each node individually below.
|
|
152
|
+
seen.set(node.filePath, node);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const context = entries
|
|
157
|
+
.map((node) => ({
|
|
158
|
+
nodeId: node.id, name: node.name, kind: node.kind,
|
|
159
|
+
filePath: node.filePath, startLine: node.startLine, endLine: node.endLine,
|
|
160
|
+
signature: node.signature, docstring: node.docstring,
|
|
161
|
+
}))
|
|
162
|
+
.sort((a, b) => a.filePath.localeCompare(b.filePath) || a.startLine - b.startLine);
|
|
163
|
+
|
|
164
|
+
return { anchor: origin, context };
|
|
165
|
+
}
|
|
@@ -1,93 +1,93 @@
|
|
|
1
|
-
// astgraph import resolution — maps module specifiers to workspace files and
|
|
2
|
-
// turns pending unresolved references into concrete graph edges.
|
|
3
|
-
|
|
4
|
-
import { posix } from "node:path";
|
|
5
|
-
|
|
6
|
-
/** Candidate extensions tried for extensionless imports, in priority order. */
|
|
7
|
-
const EXTENSION_CANDIDATES = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"];
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Rewrite a `.js`/`.jsx` specifier to a TypeScript candidate (ESM-style TS code
|
|
11
|
-
* frequently imports "./util.js" while the source file is util.ts).
|
|
12
|
-
*/
|
|
13
|
-
function rebaseExtension(specifier) {
|
|
14
|
-
if (specifier.endsWith(".js")) return `${specifier.slice(0, -3)}.ts`;
|
|
15
|
-
if (specifier.endsWith(".jsx")) return `${specifier.slice(0, -4)}.tsx`;
|
|
16
|
-
if (specifier.endsWith(".mjs")) return `${specifier.slice(0, -4)}.mts`;
|
|
17
|
-
if (specifier.endsWith(".cjs")) return `${specifier.slice(0, -4)}.cts`;
|
|
18
|
-
return undefined;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function normalizePath(fromFile, specifier) {
|
|
22
|
-
const fromDir = posix.dirname(fromFile.split(/[/\\]/).join("/"));
|
|
23
|
-
return posix.normalize(posix.join(fromDir, specifier.split(/[/\\]/).join("/")));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Resolve an import specifier to a workspace-relative file path.
|
|
28
|
-
* Only relative specifiers are resolved; bare package imports return undefined.
|
|
29
|
-
* `exists(path)` is a predicate over workspace-relative paths.
|
|
30
|
-
*/
|
|
31
|
-
export function resolveImportPath(fromFile, specifier, exists) {
|
|
32
|
-
if (specifier === undefined) return undefined;
|
|
33
|
-
if (!specifier.startsWith("./") && !specifier.startsWith("../") && !specifier.startsWith("/")) return undefined;
|
|
34
|
-
const base = normalizePath(fromFile, specifier);
|
|
35
|
-
|
|
36
|
-
const direct = exists(base) ? base : undefined;
|
|
37
|
-
if (direct !== undefined) return direct;
|
|
38
|
-
|
|
39
|
-
const rebased = rebaseExtension(specifier);
|
|
40
|
-
if (rebased !== undefined) {
|
|
41
|
-
const candidate = normalizePath(fromFile, rebased);
|
|
42
|
-
if (exists(candidate)) return candidate;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
for (const ext of EXTENSION_CANDIDATES) {
|
|
46
|
-
const candidate = base + ext;
|
|
47
|
-
if (exists(candidate)) return candidate;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
for (const ext of EXTENSION_CANDIDATES) {
|
|
51
|
-
const candidate = posix.join(base, `index${ext}`);
|
|
52
|
-
if (exists(candidate)) return candidate;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Kept for API symmetry with codegraph-style resolution; thin wrapper. */
|
|
59
|
-
export function resolveModulePath(fromFile, specifier, exists) {
|
|
60
|
-
return resolveImportPath(fromFile, specifier, exists);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Match pending references against the exported nodes of their target files.
|
|
65
|
-
*
|
|
66
|
-
* @param {Array<object>} refs unresolved_refs rows (from the database)
|
|
67
|
-
* @param {Map<string, Array<{id: string, name: string, isExported: boolean}>>} nodesByFile
|
|
68
|
-
* @param {(path: string) => boolean} exists workspace-relative file predicate
|
|
69
|
-
* @returns {{ edges: Array<object>, resolvedIds: number[] }}
|
|
70
|
-
*/
|
|
71
|
-
export function resolveRefs(refs, nodesByFile, exists) {
|
|
72
|
-
const edges = [];
|
|
73
|
-
const resolvedIds = [];
|
|
74
|
-
|
|
75
|
-
for (const ref of refs) {
|
|
76
|
-
if (ref.moduleSpecifier === undefined) continue;
|
|
77
|
-
const targetFile = resolveImportPath(ref.filePath, ref.moduleSpecifier, exists);
|
|
78
|
-
if (targetFile === undefined) continue;
|
|
79
|
-
const candidates = nodesByFile.get(targetFile) ?? [];
|
|
80
|
-
const match = candidates.find((node) => node.name === ref.referenceName && node.isExported);
|
|
81
|
-
if (match === undefined) continue;
|
|
82
|
-
|
|
83
|
-
const kind = ref.referenceKind === "import"
|
|
84
|
-
? "imports"
|
|
85
|
-
: ref.referenceKind === "extends" || ref.referenceKind === "implements"
|
|
86
|
-
? ref.referenceKind
|
|
87
|
-
: "calls";
|
|
88
|
-
edges.push({ source: ref.fromNodeId, target: match.id, kind, line: ref.line, col: ref.col });
|
|
89
|
-
resolvedIds.push(ref.id);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return { edges, resolvedIds };
|
|
93
|
-
}
|
|
1
|
+
// astgraph import resolution — maps module specifiers to workspace files and
|
|
2
|
+
// turns pending unresolved references into concrete graph edges.
|
|
3
|
+
|
|
4
|
+
import { posix } from "node:path";
|
|
5
|
+
|
|
6
|
+
/** Candidate extensions tried for extensionless imports, in priority order. */
|
|
7
|
+
const EXTENSION_CANDIDATES = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"];
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Rewrite a `.js`/`.jsx` specifier to a TypeScript candidate (ESM-style TS code
|
|
11
|
+
* frequently imports "./util.js" while the source file is util.ts).
|
|
12
|
+
*/
|
|
13
|
+
function rebaseExtension(specifier) {
|
|
14
|
+
if (specifier.endsWith(".js")) return `${specifier.slice(0, -3)}.ts`;
|
|
15
|
+
if (specifier.endsWith(".jsx")) return `${specifier.slice(0, -4)}.tsx`;
|
|
16
|
+
if (specifier.endsWith(".mjs")) return `${specifier.slice(0, -4)}.mts`;
|
|
17
|
+
if (specifier.endsWith(".cjs")) return `${specifier.slice(0, -4)}.cts`;
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function normalizePath(fromFile, specifier) {
|
|
22
|
+
const fromDir = posix.dirname(fromFile.split(/[/\\]/).join("/"));
|
|
23
|
+
return posix.normalize(posix.join(fromDir, specifier.split(/[/\\]/).join("/")));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Resolve an import specifier to a workspace-relative file path.
|
|
28
|
+
* Only relative specifiers are resolved; bare package imports return undefined.
|
|
29
|
+
* `exists(path)` is a predicate over workspace-relative paths.
|
|
30
|
+
*/
|
|
31
|
+
export function resolveImportPath(fromFile, specifier, exists) {
|
|
32
|
+
if (specifier === undefined) return undefined;
|
|
33
|
+
if (!specifier.startsWith("./") && !specifier.startsWith("../") && !specifier.startsWith("/")) return undefined;
|
|
34
|
+
const base = normalizePath(fromFile, specifier);
|
|
35
|
+
|
|
36
|
+
const direct = exists(base) ? base : undefined;
|
|
37
|
+
if (direct !== undefined) return direct;
|
|
38
|
+
|
|
39
|
+
const rebased = rebaseExtension(specifier);
|
|
40
|
+
if (rebased !== undefined) {
|
|
41
|
+
const candidate = normalizePath(fromFile, rebased);
|
|
42
|
+
if (exists(candidate)) return candidate;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const ext of EXTENSION_CANDIDATES) {
|
|
46
|
+
const candidate = base + ext;
|
|
47
|
+
if (exists(candidate)) return candidate;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
for (const ext of EXTENSION_CANDIDATES) {
|
|
51
|
+
const candidate = posix.join(base, `index${ext}`);
|
|
52
|
+
if (exists(candidate)) return candidate;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Kept for API symmetry with codegraph-style resolution; thin wrapper. */
|
|
59
|
+
export function resolveModulePath(fromFile, specifier, exists) {
|
|
60
|
+
return resolveImportPath(fromFile, specifier, exists);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Match pending references against the exported nodes of their target files.
|
|
65
|
+
*
|
|
66
|
+
* @param {Array<object>} refs unresolved_refs rows (from the database)
|
|
67
|
+
* @param {Map<string, Array<{id: string, name: string, isExported: boolean}>>} nodesByFile
|
|
68
|
+
* @param {(path: string) => boolean} exists workspace-relative file predicate
|
|
69
|
+
* @returns {{ edges: Array<object>, resolvedIds: number[] }}
|
|
70
|
+
*/
|
|
71
|
+
export function resolveRefs(refs, nodesByFile, exists) {
|
|
72
|
+
const edges = [];
|
|
73
|
+
const resolvedIds = [];
|
|
74
|
+
|
|
75
|
+
for (const ref of refs) {
|
|
76
|
+
if (ref.moduleSpecifier === undefined) continue;
|
|
77
|
+
const targetFile = resolveImportPath(ref.filePath, ref.moduleSpecifier, exists);
|
|
78
|
+
if (targetFile === undefined) continue;
|
|
79
|
+
const candidates = nodesByFile.get(targetFile) ?? [];
|
|
80
|
+
const match = candidates.find((node) => node.name === ref.referenceName && node.isExported);
|
|
81
|
+
if (match === undefined) continue;
|
|
82
|
+
|
|
83
|
+
const kind = ref.referenceKind === "import"
|
|
84
|
+
? "imports"
|
|
85
|
+
: ref.referenceKind === "extends" || ref.referenceKind === "implements"
|
|
86
|
+
? ref.referenceKind
|
|
87
|
+
: "calls";
|
|
88
|
+
edges.push({ source: ref.fromNodeId, target: match.id, kind, line: ref.line, col: ref.col });
|
|
89
|
+
resolvedIds.push(ref.id);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return { edges, resolvedIds };
|
|
93
|
+
}
|