flavor-code 1.4.0-beta.1 → 1.4.0-beta.3
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/dist/agent/loop.d.ts +3 -0
- package/dist/{app-C5ZQUOKJ.js → app-H7RYDMD3.js} +6 -6
- package/dist/{chunk-FOTMP4ZS.js → chunk-3BHKW5A7.js} +1 -1
- package/dist/{chunk-ODRWR4KK.js → chunk-IQAZ42TM.js} +1 -1
- package/dist/{chunk-URUVFECH.js → chunk-KBHWS7M6.js} +4 -4
- package/dist/{chunk-6MNOZ2TX.js → chunk-NIKCPCJG.js} +559 -377
- package/dist/{chunk-3WCDZ6ZL.js → chunk-ZJCAQXA7.js} +2 -1
- package/dist/{claude-ink-LP32NSX5.js → claude-ink-ECZ6QF3P.js} +2 -2
- package/dist/cli-main.js +7 -7
- package/dist/desktop/main.js +626 -443
- package/dist/desktop-renderer/assets/{index-CqRIr-hz.js → index-CYd5JkTR.js} +2 -2
- package/dist/desktop-renderer/assets/{interactive-terminal-C4o7MNPO.js → interactive-terminal-j52Ty3QO.js} +1 -1
- package/dist/desktop-renderer/index.html +1 -1
- package/dist/git/service.d.ts +3 -3
- package/dist/git/tools.d.ts +1 -1
- package/dist/harness/local.d.ts +2 -0
- package/dist/init/project.d.ts +0 -6
- package/dist/jobs/registry.d.ts +1 -1
- package/dist/{load-V2UIYYQI.js → load-SLSL3IWR.js} +1 -1
- package/dist/sdk/index.js +4 -4
- package/dist/tools/lsp.d.ts +13 -10
- package/dist/tools/runtime.d.ts +2 -1
- package/dist/tools/search.d.ts +1 -0
- package/dist/tools/types.d.ts +1 -1
- package/dist/utils/abort.d.ts +4 -0
- package/dist/utils/execFileNoThrow.d.ts +1 -0
- package/dist/utils/process-tree.d.ts +2 -0
- package/package.json +2 -2
- package//346/212/200/346/234/257/346/226/271/346/241/210/346/212/245/345/221/212.md +7 -7
- package/dist/astgraph/db.mjs +0 -237
- package/dist/astgraph/extract.mjs +0 -239
- package/dist/astgraph/flavor-plugin.json +0 -25
- package/dist/astgraph/grammars.mjs +0 -68
- package/dist/astgraph/index.js +0 -301
- package/dist/astgraph/indexer.mjs +0 -175
- package/dist/astgraph/query.mjs +0 -165
- package/dist/astgraph/resolve.mjs +0 -93
- package/dist/astgraph/vendor/tree-sitter-javascript.wasm +0 -0
- package/dist/astgraph/vendor/tree-sitter-tsx.wasm +0 -0
- package/dist/astgraph/vendor/tree-sitter-typescript.wasm +0 -0
- package/dist/astgraph/vendor/tree-sitter.js +0 -3980
- package/dist/astgraph/vendor/tree-sitter.wasm +0 -0
- package/dist/astgraph/vendor/zod/LICENSE +0 -21
- package/dist/astgraph/vendor/zod/index.js +0 -4
- package/dist/astgraph/vendor/zod/locales/index.js +0 -1
- package/dist/astgraph/vendor/zod/locales/package.json +0 -7
- package/dist/astgraph/vendor/zod/package.json +0 -135
- package/dist/astgraph/vendor/zod/v4/classic/checks.js +0 -1
- package/dist/astgraph/vendor/zod/v4/classic/coerce.js +0 -17
- package/dist/astgraph/vendor/zod/v4/classic/compat.js +0 -31
- package/dist/astgraph/vendor/zod/v4/classic/errors.js +0 -48
- package/dist/astgraph/vendor/zod/v4/classic/external.js +0 -20
- package/dist/astgraph/vendor/zod/v4/classic/from-json-schema.js +0 -599
- package/dist/astgraph/vendor/zod/v4/classic/index.js +0 -4
- package/dist/astgraph/vendor/zod/v4/classic/iso.js +0 -30
- package/dist/astgraph/vendor/zod/v4/classic/package.json +0 -7
- package/dist/astgraph/vendor/zod/v4/classic/parse.js +0 -15
- package/dist/astgraph/vendor/zod/v4/classic/schemas.js +0 -1395
- package/dist/astgraph/vendor/zod/v4/core/api.js +0 -1087
- package/dist/astgraph/vendor/zod/v4/core/checks.js +0 -575
- package/dist/astgraph/vendor/zod/v4/core/core.js +0 -78
- package/dist/astgraph/vendor/zod/v4/core/doc.js +0 -35
- package/dist/astgraph/vendor/zod/v4/core/errors.js +0 -185
- package/dist/astgraph/vendor/zod/v4/core/index.js +0 -16
- package/dist/astgraph/vendor/zod/v4/core/json-schema-generator.js +0 -95
- package/dist/astgraph/vendor/zod/v4/core/json-schema-processors.js +0 -601
- package/dist/astgraph/vendor/zod/v4/core/json-schema.js +0 -1
- package/dist/astgraph/vendor/zod/v4/core/package.json +0 -7
- package/dist/astgraph/vendor/zod/v4/core/parse.js +0 -93
- package/dist/astgraph/vendor/zod/v4/core/regexes.js +0 -139
- package/dist/astgraph/vendor/zod/v4/core/registries.js +0 -51
- package/dist/astgraph/vendor/zod/v4/core/schemas.js +0 -2239
- package/dist/astgraph/vendor/zod/v4/core/standard-schema.js +0 -1
- package/dist/astgraph/vendor/zod/v4/core/to-json-schema.js +0 -448
- package/dist/astgraph/vendor/zod/v4/core/util.js +0 -674
- package/dist/astgraph/vendor/zod/v4/core/versions.js +0 -5
- package/dist/astgraph/vendor/zod/v4/index.js +0 -3
- package/dist/astgraph/vendor/zod/v4/locales/ar.js +0 -106
- package/dist/astgraph/vendor/zod/v4/locales/az.js +0 -105
- package/dist/astgraph/vendor/zod/v4/locales/be.js +0 -156
- package/dist/astgraph/vendor/zod/v4/locales/bg.js +0 -120
- package/dist/astgraph/vendor/zod/v4/locales/ca.js +0 -107
- package/dist/astgraph/vendor/zod/v4/locales/cs.js +0 -111
- package/dist/astgraph/vendor/zod/v4/locales/da.js +0 -115
- package/dist/astgraph/vendor/zod/v4/locales/de.js +0 -108
- package/dist/astgraph/vendor/zod/v4/locales/el.js +0 -109
- package/dist/astgraph/vendor/zod/v4/locales/en.js +0 -113
- package/dist/astgraph/vendor/zod/v4/locales/eo.js +0 -109
- package/dist/astgraph/vendor/zod/v4/locales/es.js +0 -132
- package/dist/astgraph/vendor/zod/v4/locales/fa.js +0 -114
- package/dist/astgraph/vendor/zod/v4/locales/fi.js +0 -112
- package/dist/astgraph/vendor/zod/v4/locales/fr-CA.js +0 -107
- package/dist/astgraph/vendor/zod/v4/locales/fr.js +0 -125
- package/dist/astgraph/vendor/zod/v4/locales/he.js +0 -214
- package/dist/astgraph/vendor/zod/v4/locales/hr.js +0 -122
- package/dist/astgraph/vendor/zod/v4/locales/hu.js +0 -108
- package/dist/astgraph/vendor/zod/v4/locales/hy.js +0 -147
- package/dist/astgraph/vendor/zod/v4/locales/id.js +0 -106
- package/dist/astgraph/vendor/zod/v4/locales/index.js +0 -52
- package/dist/astgraph/vendor/zod/v4/locales/is.js +0 -109
- package/dist/astgraph/vendor/zod/v4/locales/it.js +0 -108
- package/dist/astgraph/vendor/zod/v4/locales/ja.js +0 -107
- package/dist/astgraph/vendor/zod/v4/locales/ka.js +0 -112
- package/dist/astgraph/vendor/zod/v4/locales/kh.js +0 -5
- package/dist/astgraph/vendor/zod/v4/locales/km.js +0 -110
- package/dist/astgraph/vendor/zod/v4/locales/ko.js +0 -111
- package/dist/astgraph/vendor/zod/v4/locales/lt.js +0 -203
- package/dist/astgraph/vendor/zod/v4/locales/mk.js +0 -109
- package/dist/astgraph/vendor/zod/v4/locales/ms.js +0 -107
- package/dist/astgraph/vendor/zod/v4/locales/nl.js +0 -110
- package/dist/astgraph/vendor/zod/v4/locales/no.js +0 -108
- package/dist/astgraph/vendor/zod/v4/locales/ota.js +0 -109
- package/dist/astgraph/vendor/zod/v4/locales/package.json +0 -7
- package/dist/astgraph/vendor/zod/v4/locales/pl.js +0 -109
- package/dist/astgraph/vendor/zod/v4/locales/ps.js +0 -114
- package/dist/astgraph/vendor/zod/v4/locales/pt.js +0 -108
- package/dist/astgraph/vendor/zod/v4/locales/ro.js +0 -119
- package/dist/astgraph/vendor/zod/v4/locales/ru.js +0 -156
- package/dist/astgraph/vendor/zod/v4/locales/sl.js +0 -109
- package/dist/astgraph/vendor/zod/v4/locales/sv.js +0 -110
- package/dist/astgraph/vendor/zod/v4/locales/ta.js +0 -110
- package/dist/astgraph/vendor/zod/v4/locales/th.js +0 -110
- package/dist/astgraph/vendor/zod/v4/locales/tr.js +0 -105
- package/dist/astgraph/vendor/zod/v4/locales/ua.js +0 -5
- package/dist/astgraph/vendor/zod/v4/locales/uk.js +0 -108
- package/dist/astgraph/vendor/zod/v4/locales/ur.js +0 -110
- package/dist/astgraph/vendor/zod/v4/locales/uz.js +0 -110
- package/dist/astgraph/vendor/zod/v4/locales/vi.js +0 -108
- package/dist/astgraph/vendor/zod/v4/locales/yo.js +0 -107
- package/dist/astgraph/vendor/zod/v4/locales/zh-CN.js +0 -109
- package/dist/astgraph/vendor/zod/v4/locales/zh-TW.js +0 -107
- package/dist/astgraph/vendor/zod/v4/mini/checks.js +0 -1
- package/dist/astgraph/vendor/zod/v4/mini/coerce.js +0 -22
- package/dist/astgraph/vendor/zod/v4/mini/external.js +0 -14
- package/dist/astgraph/vendor/zod/v4/mini/index.js +0 -3
- package/dist/astgraph/vendor/zod/v4/mini/iso.js +0 -34
- package/dist/astgraph/vendor/zod/v4/mini/package.json +0 -7
- package/dist/astgraph/vendor/zod/v4/mini/parse.js +0 -1
- package/dist/astgraph/vendor/zod/v4/mini/schemas.js +0 -961
- package/dist/astgraph/vendor/zod/v4/package.json +0 -7
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
// astgraph indexer — full and incremental project indexing.
|
|
2
|
-
// Walks the workspace, extracts symbols per file (content-hash incremental),
|
|
3
|
-
// then resolves cross-file references into graph edges.
|
|
4
|
-
|
|
5
|
-
import { createHash } from "node:crypto";
|
|
6
|
-
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
7
|
-
import { relative, sep } from "node:path";
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
deleteEdgesForFile,
|
|
11
|
-
deleteFileRecord,
|
|
12
|
-
deleteUnresolvedRefsForFile,
|
|
13
|
-
getFileRecord,
|
|
14
|
-
insertEdge,
|
|
15
|
-
insertUnresolvedRef,
|
|
16
|
-
replaceNodes,
|
|
17
|
-
setMetadata,
|
|
18
|
-
upsertFileRecord,
|
|
19
|
-
} from "./db.mjs";
|
|
20
|
-
import { extract } from "./extract.mjs";
|
|
21
|
-
import { languageForFile, SUPPORTED_LANGUAGES } from "./grammars.mjs";
|
|
22
|
-
import { resolveImportPath, resolveRefs } from "./resolve.mjs";
|
|
23
|
-
|
|
24
|
-
/** Directory names never indexed, regardless of depth. */
|
|
25
|
-
const EXCLUDED_DIRS = new Set([
|
|
26
|
-
"node_modules", "dist", "build", "out", "coverage", "release", ".git",
|
|
27
|
-
".flavor", ".claude", ".worktrees", ".cache", ".idea", ".vscode", "vendor",
|
|
28
|
-
]);
|
|
29
|
-
|
|
30
|
-
function toPosix(path) {
|
|
31
|
-
return path.split(sep).join("/");
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Walk the workspace and return code files as workspace-relative posix paths. */
|
|
35
|
-
export function listCodeFiles(workspace) {
|
|
36
|
-
const files = [];
|
|
37
|
-
const walk = (dir) => {
|
|
38
|
-
let entries;
|
|
39
|
-
try { entries = readdirSync(dir, { withFileTypes: true }); }
|
|
40
|
-
catch { return; }
|
|
41
|
-
for (const entry of entries) {
|
|
42
|
-
const full = `${dir}/${entry.name}`;
|
|
43
|
-
if (entry.isDirectory()) {
|
|
44
|
-
if (!EXCLUDED_DIRS.has(entry.name)) walk(full);
|
|
45
|
-
} else if (entry.isFile() && languageForFile(entry.name) !== undefined) {
|
|
46
|
-
files.push(toPosix(relative(workspace, full)));
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
walk(workspace);
|
|
51
|
-
return files.sort();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** SHA-256 content hash used for incremental detection. */
|
|
55
|
-
export function contentHash(content) {
|
|
56
|
-
return createHash("sha256").update(content).digest("hex");
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async function indexOneFile(db, workspace, relativePath) {
|
|
60
|
-
const content = readFileSync(`${workspace}/${relativePath}`, "utf8");
|
|
61
|
-
const language = languageForFile(relativePath);
|
|
62
|
-
const { nodes, edges, refs } = await extract(content, relativePath, language);
|
|
63
|
-
|
|
64
|
-
deleteEdgesForFile(db, relativePath);
|
|
65
|
-
deleteUnresolvedRefsForFile(db, relativePath);
|
|
66
|
-
replaceNodes(db, relativePath, nodes);
|
|
67
|
-
for (const edge of edges) insertEdge(db, edge);
|
|
68
|
-
for (const ref of refs) {
|
|
69
|
-
insertUnresolvedRef(db, {
|
|
70
|
-
fromNodeId: ref.fromNodeId, referenceName: ref.referenceName,
|
|
71
|
-
referenceKind: ref.referenceKind, line: ref.line, col: ref.col,
|
|
72
|
-
filePath: ref.filePath, moduleSpecifier: ref.moduleSpecifier,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
let size;
|
|
76
|
-
try { size = statSync(`${workspace}/${relativePath}`).size; } catch { size = content.length; }
|
|
77
|
-
upsertFileRecord(db, {
|
|
78
|
-
path: relativePath, contentHash: contentHash(content), language, size, nodeCount: nodes.length,
|
|
79
|
-
});
|
|
80
|
-
return { nodeCount: nodes.length };
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Resolve every pending unresolved_ref whose module specifier points at an indexed file. */
|
|
84
|
-
function resolvePass(db, workspace) {
|
|
85
|
-
const rows = db.prepare(`
|
|
86
|
-
SELECT id, from_node_id, reference_name, reference_kind, line, col, file_path, module_specifier
|
|
87
|
-
FROM unresolved_refs WHERE status = 'pending' AND module_specifier IS NOT NULL
|
|
88
|
-
`).all();
|
|
89
|
-
if (rows.length === 0) return 0;
|
|
90
|
-
|
|
91
|
-
const nodesByFile = new Map();
|
|
92
|
-
for (const file of db.prepare("SELECT DISTINCT file_path FROM nodes").all()) {
|
|
93
|
-
nodesByFile.set(file.file_path, db.prepare(
|
|
94
|
-
"SELECT id, name, is_exported FROM nodes WHERE file_path = ?",
|
|
95
|
-
).all(file.file_path).map((row) => ({ id: row.id, name: row.name, isExported: row.is_exported === 1 })));
|
|
96
|
-
}
|
|
97
|
-
const exists = (path) => {
|
|
98
|
-
try { statSync(`${workspace}/${path}`); return true; } catch { return false; }
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const refs = rows.map((row) => ({
|
|
102
|
-
id: row.id, fromNodeId: row.from_node_id, referenceName: row.reference_name,
|
|
103
|
-
referenceKind: row.reference_kind, line: row.line, col: row.col,
|
|
104
|
-
filePath: row.file_path, moduleSpecifier: row.module_specifier,
|
|
105
|
-
}));
|
|
106
|
-
// Import statements live outside any function, so their refs carry no from-node.
|
|
107
|
-
// Attribute them to a node that actually uses the binding (same file + name);
|
|
108
|
-
// unused imports resolve silently without an edge.
|
|
109
|
-
for (const ref of refs) {
|
|
110
|
-
if (ref.fromNodeId !== null || ref.referenceKind !== "import") continue;
|
|
111
|
-
const user = refs.find((other) => other.referenceKind !== "import"
|
|
112
|
-
&& other.referenceName === ref.referenceName
|
|
113
|
-
&& other.filePath === ref.filePath
|
|
114
|
-
&& other.fromNodeId !== null);
|
|
115
|
-
ref.fromNodeId = user?.fromNodeId ?? null;
|
|
116
|
-
}
|
|
117
|
-
const { edges, resolvedIds } = resolveRefs(refs.filter((ref) => ref.fromNodeId !== null), nodesByFile, exists);
|
|
118
|
-
for (const edge of edges) insertEdge(db, edge);
|
|
119
|
-
if (resolvedIds.length > 0) {
|
|
120
|
-
db.exec("BEGIN");
|
|
121
|
-
try {
|
|
122
|
-
const mark = db.prepare("DELETE FROM unresolved_refs WHERE id = ?");
|
|
123
|
-
for (const id of resolvedIds) mark.run(id);
|
|
124
|
-
db.exec("COMMIT");
|
|
125
|
-
} catch (error) { db.exec("ROLLBACK"); throw error; }
|
|
126
|
-
}
|
|
127
|
-
return resolvedIds.length;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Index (or re-index) a workspace into the graph database.
|
|
132
|
-
*
|
|
133
|
-
* Options:
|
|
134
|
-
* onlyPaths — restrict the pass to these workspace-relative paths (hook-driven sync).
|
|
135
|
-
* Disappeared files are still pruned; everything else is left untouched.
|
|
136
|
-
* Returns { files, indexed, skipped, removed, resolved }.
|
|
137
|
-
*/
|
|
138
|
-
export async function indexProject(workspace, { db, onlyPaths } = {}) {
|
|
139
|
-
const restrict = onlyPaths === undefined ? undefined : new Set(onlyPaths.map(toPosix));
|
|
140
|
-
const diskFiles = listCodeFiles(workspace).filter((path) => restrict === undefined || restrict.has(path));
|
|
141
|
-
|
|
142
|
-
let indexed = 0;
|
|
143
|
-
let skipped = 0;
|
|
144
|
-
for (const path of diskFiles) {
|
|
145
|
-
const current = readFileSync(`${workspace}/${path}`, "utf8");
|
|
146
|
-
const hash = contentHash(current);
|
|
147
|
-
const record = getFileRecord(db, path);
|
|
148
|
-
if (record !== undefined && record.contentHash === hash) { skipped += 1; continue; }
|
|
149
|
-
await indexOneFile(db, workspace, path);
|
|
150
|
-
indexed += 1;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Prune files that no longer exist on disk.
|
|
154
|
-
let removed = 0;
|
|
155
|
-
const known = db.prepare("SELECT path FROM files").all();
|
|
156
|
-
const diskSet = new Set(diskFiles);
|
|
157
|
-
for (const row of known) {
|
|
158
|
-
if (restrict !== undefined && !restrict.has(row.path)) continue;
|
|
159
|
-
if (!diskSet.has(row.path)) {
|
|
160
|
-
deleteEdgesForFile(db, row.path);
|
|
161
|
-
deleteUnresolvedRefsForFile(db, row.path);
|
|
162
|
-
replaceNodes(db, row.path, []);
|
|
163
|
-
deleteFileRecord(db, row.path);
|
|
164
|
-
removed += 1;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const resolved = resolvePass(db, workspace);
|
|
169
|
-
setMetadata(db, "last_index", JSON.stringify({ at: Date.now(), files: diskFiles.length, indexed, removed }));
|
|
170
|
-
|
|
171
|
-
return { files: diskFiles.length, indexed, skipped, removed, resolved };
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/** Exposed for diagnostics. */
|
|
175
|
-
export const INDEXER_LANGUAGES = SUPPORTED_LANGUAGES;
|
package/dist/astgraph/query.mjs
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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
|
-
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|