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,239 +0,0 @@
|
|
|
1
|
-
// astgraph TypeScript/JavaScript structural extractor.
|
|
2
|
-
// Walks the tree-sitter syntax tree and produces:
|
|
3
|
-
// nodes — functions, classes, methods, interfaces, type aliases, variables
|
|
4
|
-
// edges — intra-file calls / extends / implements
|
|
5
|
-
// refs — cross-file references (imports + unresolved calls/heritage) resolved later
|
|
6
|
-
//
|
|
7
|
-
// Lines are 1-based in all outputs so they match editor/Read tool coordinates.
|
|
8
|
-
|
|
9
|
-
import { parseSource } from "./grammars.mjs";
|
|
10
|
-
|
|
11
|
-
const DECLARATION_KINDS = new Map([
|
|
12
|
-
["function_declaration", "function"],
|
|
13
|
-
["class_declaration", "class"],
|
|
14
|
-
["interface_declaration", "interface"],
|
|
15
|
-
["type_alias_declaration", "type"],
|
|
16
|
-
]);
|
|
17
|
-
|
|
18
|
-
/** Extract the doc text from a `/** ... */` or `// ...` comment node. */
|
|
19
|
-
function docstringFrom(commentNode) {
|
|
20
|
-
if (commentNode === null || commentNode.type !== "comment") return undefined;
|
|
21
|
-
const text = commentNode.text;
|
|
22
|
-
if (text.startsWith("/**") || text.startsWith("/*!")) {
|
|
23
|
-
return text
|
|
24
|
-
.replace(/^\/\*+[!*]?/, "")
|
|
25
|
-
.replace(/\*+\/$/, "")
|
|
26
|
-
.split("\n")
|
|
27
|
-
.map((line) => line.replace(/^\s*\*+\s?/, "").trim())
|
|
28
|
-
.filter((line) => line.length > 0)
|
|
29
|
-
.join("\n") || undefined;
|
|
30
|
-
}
|
|
31
|
-
if (text.startsWith("//")) return text.replace(/^\/\/+\s?/, "").trim() || undefined;
|
|
32
|
-
return undefined;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function firstLine(text, limit = 240) {
|
|
36
|
-
const line = text.split("\n")[0].trim();
|
|
37
|
-
return line.length > limit ? `${line.slice(0, limit)}…` : line;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function signatureOf(node) {
|
|
41
|
-
const text = node.text;
|
|
42
|
-
const brace = text.indexOf("{");
|
|
43
|
-
const head = brace >= 0 ? text.slice(0, brace) : text.split("\n")[0];
|
|
44
|
-
return firstLine(head.replace(/\s+/g, " "));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function nameOf(node) {
|
|
48
|
-
const nameNode = node.childForFieldName("name");
|
|
49
|
-
return nameNode === null ? undefined : nameNode.text;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function isAsync(node) {
|
|
53
|
-
return node.children.some((child) => child.type === "async");
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export async function extract(source, filePath, language) {
|
|
57
|
-
const tree = await parseSource(source, language);
|
|
58
|
-
|
|
59
|
-
/** @type {Array<object>} */
|
|
60
|
-
const nodes = [];
|
|
61
|
-
/** @type {Array<object>} */
|
|
62
|
-
const edges = [];
|
|
63
|
-
/** @type {Array<object>} */
|
|
64
|
-
const refs = [];
|
|
65
|
-
/** Map local symbol name → node id (methods registered as name only for call matching). */
|
|
66
|
-
const localNames = new Map();
|
|
67
|
-
/** Map imported binding name → module specifier. */
|
|
68
|
-
const imports = new Map();
|
|
69
|
-
/** Map declaration AST node id → node record, for enclosing-scope lookup.
|
|
70
|
-
* Keyed by numeric node.id because web-tree-sitter returns fresh node object
|
|
71
|
-
* instances on every navigation (parent/childForFieldName), so object identity
|
|
72
|
-
* cannot be used as a Map key. */
|
|
73
|
-
const scopeByNodeId = new Map();
|
|
74
|
-
|
|
75
|
-
const makeNode = (astNode, kind, name, qualifiedName, exported, extra = {}) => {
|
|
76
|
-
// The docstring lives on the top-level statement's preceding sibling (comments
|
|
77
|
-
// attach to program/export_statement, not to the inner declaration node).
|
|
78
|
-
const docSource = extra.docSource ?? astNode;
|
|
79
|
-
const record = {
|
|
80
|
-
id: `${filePath}#${qualifiedName}`,
|
|
81
|
-
kind,
|
|
82
|
-
name,
|
|
83
|
-
qualifiedName,
|
|
84
|
-
filePath,
|
|
85
|
-
language,
|
|
86
|
-
startLine: astNode.startPosition.row + 1,
|
|
87
|
-
endLine: astNode.endPosition.row + 1,
|
|
88
|
-
signature: signatureOf(astNode),
|
|
89
|
-
docstring: docstringFrom(docSource.previousSibling),
|
|
90
|
-
isExported: exported,
|
|
91
|
-
isAsync: extra.isAsync ?? false,
|
|
92
|
-
...extra,
|
|
93
|
-
};
|
|
94
|
-
delete record.docSource;
|
|
95
|
-
nodes.push(record);
|
|
96
|
-
scopeByNodeId.set(astNode.id, record);
|
|
97
|
-
if (!localNames.has(name)) localNames.set(name, record.id);
|
|
98
|
-
return record;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const collectDeclaration = (statement, exported) => {
|
|
102
|
-
const inner = statement.type === "export_statement" ? statement.namedChildren.find((child) => DECLARATION_KINDS.has(child.type) || child.type === "lexical_declaration" || child.type === "abstract_class_declaration") : statement;
|
|
103
|
-
const target = inner ?? statement;
|
|
104
|
-
const kind = DECLARATION_KINDS.get(target.type);
|
|
105
|
-
if (kind !== undefined) {
|
|
106
|
-
const name = nameOf(target);
|
|
107
|
-
if (name !== undefined) {
|
|
108
|
-
// Comments attach to the top-level statement (export_statement / declaration),
|
|
109
|
-
// so the docstring is read from the statement's preceding sibling.
|
|
110
|
-
makeNode(target, kind, name, name, exported, {
|
|
111
|
-
...(kind === "function" ? { isAsync: isAsync(target) } : {}),
|
|
112
|
-
docSource: statement,
|
|
113
|
-
});
|
|
114
|
-
if (target.type === "class_declaration" || target.type === "abstract_class_declaration") collectClassMembers(target, exported);
|
|
115
|
-
collectHeritage(target, exported);
|
|
116
|
-
}
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
if (target.type === "lexical_declaration") {
|
|
120
|
-
for (const declarator of target.namedChildren.filter((child) => child.type === "variable_declarator")) {
|
|
121
|
-
const name = declarator.childForFieldName("name")?.text;
|
|
122
|
-
const value = declarator.childForFieldName("value");
|
|
123
|
-
if (name === undefined || value === null) continue;
|
|
124
|
-
const isFunction = value.type === "arrow_function" || value.type === "function_expression" || value.type === "function";
|
|
125
|
-
makeNode(declarator, isFunction ? "function" : "variable", name, name, exported, {
|
|
126
|
-
isAsync: isFunction && value.children.some((child) => child.type === "async"),
|
|
127
|
-
docSource: statement,
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const collectClassMembers = (classNode, classExported) => {
|
|
134
|
-
const className = nameOf(classNode);
|
|
135
|
-
const body = classNode.childForFieldName("body");
|
|
136
|
-
if (body === null) return;
|
|
137
|
-
for (const member of body.namedChildren) {
|
|
138
|
-
if (member.type !== "method_definition") continue;
|
|
139
|
-
const name = member.childForFieldName("name")?.text;
|
|
140
|
-
if (name === undefined) continue;
|
|
141
|
-
const qualified = `${className}.${name}`;
|
|
142
|
-
makeNode(member, "method", name, qualified, classExported, { isAsync: isAsync(member) });
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
const collectHeritage = (classNode, _exported) => {
|
|
147
|
-
const heritage = classNode.namedChildren.find((child) => child.type === "class_heritage");
|
|
148
|
-
if (heritage === undefined) return;
|
|
149
|
-
const sourceName = nameOf(classNode);
|
|
150
|
-
if (sourceName === undefined) return;
|
|
151
|
-
const sourceId = `${filePath}#${sourceName}`;
|
|
152
|
-
for (const clause of heritage.namedChildren) {
|
|
153
|
-
const kind = clause.type === "extends_clause" ? "extends" : clause.type === "implements_clause" ? "implements" : undefined;
|
|
154
|
-
if (kind === undefined) continue;
|
|
155
|
-
for (const typeNode of clause.namedChildren.filter((child) => child.type === "type_identifier" || child.type === "identifier")) {
|
|
156
|
-
const typeName = typeNode.text;
|
|
157
|
-
if (localNames.has(typeName)) {
|
|
158
|
-
edges.push({ source: sourceId, target: localNames.get(typeName), kind, line: typeNode.startPosition.row + 1, col: typeNode.startPosition.column });
|
|
159
|
-
} else {
|
|
160
|
-
refs.push({
|
|
161
|
-
fromNodeId: sourceId, referenceName: typeName, referenceKind: kind,
|
|
162
|
-
line: typeNode.startPosition.row + 1, col: typeNode.startPosition.column,
|
|
163
|
-
filePath, moduleSpecifier: undefined,
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
// Pass 1 — top-level declarations and imports.
|
|
171
|
-
for (const statement of tree.rootNode.namedChildren) {
|
|
172
|
-
if (statement.type === "import_statement") {
|
|
173
|
-
const sourceNode = statement.childForFieldName("source");
|
|
174
|
-
const moduleSpecifier = sourceNode === null ? undefined : sourceNode.text.replace(/^["']|["']$/g, "");
|
|
175
|
-
for (const clause of statement.namedChildren.filter((child) => child.type === "import_clause")) {
|
|
176
|
-
for (const named of clause.namedChildren.filter((child) => child.type === "named_imports")) {
|
|
177
|
-
for (const specifier of named.namedChildren.filter((child) => child.type === "import_specifier")) {
|
|
178
|
-
const nameNode = specifier.childForFieldName("name");
|
|
179
|
-
if (nameNode === null) continue;
|
|
180
|
-
const importedName = nameNode.text;
|
|
181
|
-
imports.set(importedName, moduleSpecifier);
|
|
182
|
-
refs.push({
|
|
183
|
-
fromNodeId: null, referenceName: importedName, referenceKind: "import",
|
|
184
|
-
line: nameNode.startPosition.row + 1, col: nameNode.startPosition.column,
|
|
185
|
-
filePath, moduleSpecifier,
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
const defaultName = clause.namedChildren.find((child) => child.type === "identifier");
|
|
190
|
-
if (defaultName !== undefined) {
|
|
191
|
-
imports.set(defaultName.text, moduleSpecifier);
|
|
192
|
-
refs.push({
|
|
193
|
-
fromNodeId: null, referenceName: defaultName.text, referenceKind: "import",
|
|
194
|
-
line: defaultName.startPosition.row + 1, col: defaultName.startPosition.column,
|
|
195
|
-
filePath, moduleSpecifier,
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
continue;
|
|
200
|
-
}
|
|
201
|
-
collectDeclaration(statement, statement.type === "export_statement");
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// Pass 2 — call expressions anywhere in the file.
|
|
205
|
-
const enclosingScope = (node) => {
|
|
206
|
-
let cursor = node.parent;
|
|
207
|
-
while (cursor !== null) {
|
|
208
|
-
const record = scopeByNodeId.get(cursor.id);
|
|
209
|
-
if (record !== undefined) return record;
|
|
210
|
-
cursor = cursor.parent;
|
|
211
|
-
}
|
|
212
|
-
return undefined;
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
const visitCalls = (node) => {
|
|
216
|
-
if (node.type === "call_expression") {
|
|
217
|
-
const callee = node.childForFieldName("function");
|
|
218
|
-
if (callee !== null && callee.type === "identifier") {
|
|
219
|
-
const name = callee.text;
|
|
220
|
-
const scope = enclosingScope(node);
|
|
221
|
-
if (scope !== undefined) {
|
|
222
|
-
if (localNames.has(name) && localNames.get(name) !== scope.id) {
|
|
223
|
-
edges.push({ source: scope.id, target: localNames.get(name), kind: "calls", line: callee.startPosition.row + 1, col: callee.startPosition.column });
|
|
224
|
-
} else if (!localNames.has(name)) {
|
|
225
|
-
refs.push({
|
|
226
|
-
fromNodeId: scope.id, referenceName: name, referenceKind: "call",
|
|
227
|
-
line: callee.startPosition.row + 1, col: callee.startPosition.column,
|
|
228
|
-
filePath, moduleSpecifier: imports.get(name),
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
for (const child of node.namedChildren) visitCalls(child);
|
|
235
|
-
};
|
|
236
|
-
visitCalls(tree.rootNode);
|
|
237
|
-
|
|
238
|
-
return { nodes, edges, refs, imports: Object.fromEntries(imports) };
|
|
239
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "astgraph",
|
|
3
|
-
"version": "0.1.0",
|
|
4
|
-
"apiVersion": "1",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"permissions": [],
|
|
7
|
-
"contributes": {
|
|
8
|
-
"commands": [
|
|
9
|
-
{ "name": "ast" }
|
|
10
|
-
],
|
|
11
|
-
"tools": [
|
|
12
|
-
{ "name": "ast_search" },
|
|
13
|
-
{ "name": "ast_callers" },
|
|
14
|
-
{ "name": "ast_callees" },
|
|
15
|
-
{ "name": "ast_impact" },
|
|
16
|
-
{ "name": "ast_context" }
|
|
17
|
-
],
|
|
18
|
-
"skillRoots": [],
|
|
19
|
-
"modelAdapters": [],
|
|
20
|
-
"hooks": [
|
|
21
|
-
{ "name": "SessionStart" },
|
|
22
|
-
{ "name": "PostToolUse" }
|
|
23
|
-
]
|
|
24
|
-
}
|
|
25
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
// astgraph grammar loading — web-tree-sitter WASM runtime, vendored under ./vendor.
|
|
2
|
-
// Resolves the vendor directory relative to this module so it works both from the
|
|
3
|
-
// source tree (src/init/astgraph) and the copied plugin root (.flavor/plugins/astgraph).
|
|
4
|
-
|
|
5
|
-
import { dirname, join } from "node:path";
|
|
6
|
-
import { fileURLToPath } from "node:url";
|
|
7
|
-
|
|
8
|
-
import { Language, Parser } from "./vendor/tree-sitter.js";
|
|
9
|
-
|
|
10
|
-
const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
|
|
11
|
-
export const VENDOR_DIR = join(MODULE_DIR, "vendor");
|
|
12
|
-
|
|
13
|
-
const GRAMMAR_FILES = {
|
|
14
|
-
typescript: "tree-sitter-typescript.wasm",
|
|
15
|
-
tsx: "tree-sitter-tsx.wasm",
|
|
16
|
-
javascript: "tree-sitter-javascript.wasm",
|
|
17
|
-
jsx: "tree-sitter-javascript.wasm",
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export const SUPPORTED_LANGUAGES = Object.keys(GRAMMAR_FILES);
|
|
21
|
-
|
|
22
|
-
export function languageForFile(path) {
|
|
23
|
-
const lower = path.toLowerCase();
|
|
24
|
-
if (lower.endsWith(".tsx")) return "tsx";
|
|
25
|
-
if (lower.endsWith(".ts") || lower.endsWith(".mts") || lower.endsWith(".cts")) return "typescript";
|
|
26
|
-
if (lower.endsWith(".jsx")) return "jsx";
|
|
27
|
-
if (lower.endsWith(".js") || lower.endsWith(".mjs") || lower.endsWith(".cjs")) return "javascript";
|
|
28
|
-
return undefined;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
let initialized = false;
|
|
32
|
-
let initPromise;
|
|
33
|
-
const languageCache = new Map();
|
|
34
|
-
const parserCache = new Map();
|
|
35
|
-
|
|
36
|
-
/** Initialize the WASM runtime once. Safe to call repeatedly. */
|
|
37
|
-
export async function initGrammars() {
|
|
38
|
-
if (initialized) return;
|
|
39
|
-
initPromise ??= Parser.init({ locateFile: (name) => join(VENDOR_DIR, name) }).then(() => {
|
|
40
|
-
initialized = true;
|
|
41
|
-
});
|
|
42
|
-
await initPromise;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Load (and cache) a grammar for one of the supported languages. */
|
|
46
|
-
export async function loadLanguage(language) {
|
|
47
|
-
const file = GRAMMAR_FILES[language];
|
|
48
|
-
if (file === undefined) throw new Error(`astgraph: unsupported language "${language}"`);
|
|
49
|
-
await initGrammars();
|
|
50
|
-
let languageInstance = languageCache.get(language);
|
|
51
|
-
if (languageInstance === undefined) {
|
|
52
|
-
languageInstance = await Language.load(join(VENDOR_DIR, file));
|
|
53
|
-
languageCache.set(language, languageInstance);
|
|
54
|
-
}
|
|
55
|
-
return languageInstance;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Parse source text and return the syntax tree. */
|
|
59
|
-
export async function parseSource(source, language) {
|
|
60
|
-
const languageInstance = await loadLanguage(language);
|
|
61
|
-
let parser = parserCache.get(language);
|
|
62
|
-
if (parser === undefined) {
|
|
63
|
-
parser = new Parser();
|
|
64
|
-
parser.setLanguage(languageInstance);
|
|
65
|
-
parserCache.set(language, parser);
|
|
66
|
-
}
|
|
67
|
-
return parser.parse(source);
|
|
68
|
-
}
|
package/dist/astgraph/index.js
DELETED
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
// astgraph flavor-code plugin — codegraph-style code graph for precise navigation.
|
|
2
|
-
//
|
|
3
|
-
// Registers:
|
|
4
|
-
// command /ast init | sync | status | search | impact | callers | callees | context | help
|
|
5
|
-
// tools ast_search / ast_callers / ast_callees / ast_impact / ast_context
|
|
6
|
-
// hooks SessionStart (cache workspace), PostToolUse (incremental sync after edits)
|
|
7
|
-
//
|
|
8
|
-
// Heavy lifting is lazy: activation only registers handlers so it stays well
|
|
9
|
-
// inside the host's activation timeout; WASM grammars and the database load on
|
|
10
|
-
// first use.
|
|
11
|
-
|
|
12
|
-
import { existsSync } from "node:fs";
|
|
13
|
-
import { isAbsolute, join, relative } from "node:path";
|
|
14
|
-
|
|
15
|
-
import { z } from "./vendor/zod/index.js";
|
|
16
|
-
|
|
17
|
-
const ALLOW = Object.freeze({ decision: "allow" });
|
|
18
|
-
|
|
19
|
-
function workspaceFrom(event) {
|
|
20
|
-
const workspace = event?.payload?.workspace;
|
|
21
|
-
if (typeof workspace === "string" && workspace.length > 0) return workspace;
|
|
22
|
-
return undefined;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function dbPath(root) {
|
|
26
|
-
return join(root, ".flavor", "astgraph", "index.db");
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
async function mod(name) {
|
|
30
|
-
return import(new URL(`./${name}`, import.meta.url).href);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** Open the graph database when it exists; otherwise undefined (not indexed yet). */
|
|
34
|
-
async function openIndex(root) {
|
|
35
|
-
const path = dbPath(root);
|
|
36
|
-
if (!existsSync(path)) return undefined;
|
|
37
|
-
const { openDb } = await mod("db.mjs");
|
|
38
|
-
return { db: openDb(path), path };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function workspaceRelative(root, path) {
|
|
42
|
-
if (path === undefined) return undefined;
|
|
43
|
-
const candidate = isAbsolute(path) ? relative(root, path) : path;
|
|
44
|
-
return candidate.split("\\").join("/").replace(/^\.\//, "");
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async function runIndex(root, options = {}) {
|
|
48
|
-
const { openDb } = await mod("db.mjs");
|
|
49
|
-
const { indexProject } = await mod("indexer.mjs");
|
|
50
|
-
const db = openDb(dbPath(root));
|
|
51
|
-
try {
|
|
52
|
-
return await indexProject(root, { db, ...options });
|
|
53
|
-
} finally {
|
|
54
|
-
db.close();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const HELP = {
|
|
59
|
-
usage: [
|
|
60
|
-
"/ast init Build the full code graph (.flavor/astgraph/index.db)",
|
|
61
|
-
"/ast sync [path...] Incrementally re-index (defaults to changed files only)",
|
|
62
|
-
"/ast status Show graph statistics",
|
|
63
|
-
"/ast search <query> Find anchor symbols (FTS + identifier segments)",
|
|
64
|
-
"/ast callers <node-id> Who calls this node",
|
|
65
|
-
"/ast callees <node-id> What this node calls",
|
|
66
|
-
"/ast impact <node-id> [--hops N] [--direction up|down|both] Blast radius",
|
|
67
|
-
"/ast context <node-id> [--hops N] Precise file:line read ranges around a node",
|
|
68
|
-
],
|
|
69
|
-
hint: "Node ids look like 'src/order.ts#cancelOrder'. Agent tools: ast_search, ast_callers, ast_callees, ast_impact, ast_context.",
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
async function commandAst(args, context) {
|
|
73
|
-
const root = context.workspace;
|
|
74
|
-
const [sub, ...rest] = args;
|
|
75
|
-
switch (sub) {
|
|
76
|
-
case undefined:
|
|
77
|
-
case "help":
|
|
78
|
-
return HELP;
|
|
79
|
-
case "init": {
|
|
80
|
-
const result = await runIndex(root);
|
|
81
|
-
return { command: "init", workspace: root, db: dbPath(root), ...result };
|
|
82
|
-
}
|
|
83
|
-
case "sync": {
|
|
84
|
-
const onlyPaths = rest.map((path) => workspaceRelative(root, path)).filter((path) => path !== undefined);
|
|
85
|
-
const result = await runIndex(root, onlyPaths.length > 0 ? { onlyPaths } : {});
|
|
86
|
-
return { command: "sync", ...result };
|
|
87
|
-
}
|
|
88
|
-
case "status": {
|
|
89
|
-
const index = await openIndex(root);
|
|
90
|
-
if (index === undefined) return { command: "status", indexed: false, hint: "Run /ast init first." };
|
|
91
|
-
try {
|
|
92
|
-
const { stats, getMetadata } = await mod("db.mjs");
|
|
93
|
-
const lastIndex = getMetadata(index.db, "last_index");
|
|
94
|
-
return { command: "status", indexed: true, db: index.path, ...stats(index.db), lastIndex: lastIndex === undefined ? undefined : JSON.parse(lastIndex) };
|
|
95
|
-
} finally {
|
|
96
|
-
index.db.close();
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
default: {
|
|
100
|
-
const nodeId = rest[0];
|
|
101
|
-
if (["search", "callers", "callees", "impact", "context"].includes(sub) && (sub === "search" ? rest.length === 0 : nodeId === undefined)) {
|
|
102
|
-
return { command: sub, error: `Missing ${sub === "search" ? "query" : "node id"}. See /ast help.` };
|
|
103
|
-
}
|
|
104
|
-
const index = await openIndex(root);
|
|
105
|
-
if (index === undefined) return { command: sub, error: "Graph not built. Run /ast init first." };
|
|
106
|
-
try {
|
|
107
|
-
const { search, callers, callees, impact, subgraphContext, getNode } = await mod("query.mjs");
|
|
108
|
-
if (sub === "search") return { command: sub, query: rest.join(" "), results: search(index.db, rest.join(" ")) };
|
|
109
|
-
const anchor = getNode(index.db, nodeId);
|
|
110
|
-
if (anchor === undefined) return { command: sub, error: `Unknown node id "${nodeId}". Use /ast search to find anchors.` };
|
|
111
|
-
if (sub === "callers") return { command: sub, node: anchor, callers: callers(index.db, nodeId) };
|
|
112
|
-
if (sub === "callees") return { command: sub, node: anchor, callees: callees(index.db, nodeId) };
|
|
113
|
-
const hops = Math.min(5, Math.max(1, parseFlag(rest, "--hops", 2)));
|
|
114
|
-
const direction = parseStringFlag(rest, "--direction", "up");
|
|
115
|
-
if (sub === "impact") return { command: sub, ...impact(index.db, nodeId, { hops, direction: ["up", "down", "both"].includes(direction) ? direction : "up" }) };
|
|
116
|
-
return { command: "context", ...subgraphContext(index.db, nodeId, { hops }) };
|
|
117
|
-
} finally {
|
|
118
|
-
index.db.close();
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function parseFlag(args, name, fallback) {
|
|
125
|
-
const index = args.indexOf(name);
|
|
126
|
-
if (index < 0) return fallback;
|
|
127
|
-
const value = Number(args[index + 1]);
|
|
128
|
-
return Number.isFinite(value) ? value : fallback;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function parseStringFlag(args, name, fallback) {
|
|
132
|
-
const index = args.indexOf(name);
|
|
133
|
-
return index < 0 ? fallback : args[index + 1] ?? fallback;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Numeric fields use z.coerce so providers that serialize numbers as JSON
|
|
137
|
-
// strings ("10" instead of 10) still validate without a repair round-trip.
|
|
138
|
-
const boundedInt = (min, max) => z.coerce.number().int().min(min).max(max).optional();
|
|
139
|
-
|
|
140
|
-
function toolSchemas() {
|
|
141
|
-
const nodeInput = z.object({
|
|
142
|
-
id: z.string().describe("Node id, e.g. 'src/order.ts#cancelOrder' (find with ast_search)"),
|
|
143
|
-
});
|
|
144
|
-
return {
|
|
145
|
-
ast_search: z.object({
|
|
146
|
-
query: z.string().describe("Natural-language or symbol keywords, e.g. '订单 取消' or 'cancelOrder'"),
|
|
147
|
-
limit: boundedInt(1, 50).describe("Max results (default 10)"),
|
|
148
|
-
}),
|
|
149
|
-
ast_callers: nodeInput,
|
|
150
|
-
ast_callees: nodeInput,
|
|
151
|
-
ast_impact: z.object({
|
|
152
|
-
id: z.string().describe("Node id, e.g. 'src/order.ts#cancelOrder'"),
|
|
153
|
-
hops: boundedInt(1, 5).describe("Traversal depth (default 2)"),
|
|
154
|
-
direction: z.enum(["up", "down", "both"]).optional().describe("up=who depends on me, down=what I depend on (default up)"),
|
|
155
|
-
}),
|
|
156
|
-
ast_context: z.object({
|
|
157
|
-
id: z.string().describe("Node id, e.g. 'src/order.ts#cancelOrder'"),
|
|
158
|
-
hops: boundedInt(1, 3).describe("Subgraph depth (default 1)"),
|
|
159
|
-
}),
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function createTools(rootRef) {
|
|
164
|
-
const schemas = toolSchemas();
|
|
165
|
-
const notIndexed = { ok: false, error: "astgraph index not built. Ask the user to run /ast init." };
|
|
166
|
-
|
|
167
|
-
const withDb = async (fn) => {
|
|
168
|
-
const root = rootRef();
|
|
169
|
-
const index = await openIndex(root);
|
|
170
|
-
if (index === undefined) return notIndexed;
|
|
171
|
-
try {
|
|
172
|
-
return await fn(index.db, root);
|
|
173
|
-
} finally {
|
|
174
|
-
index.db.close();
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
return [
|
|
179
|
-
{
|
|
180
|
-
name: "ast_search",
|
|
181
|
-
description: "Search the code graph for anchor symbols by keyword (full-text + identifier segments). Use before grep/read when locating where a change belongs.",
|
|
182
|
-
inputSchema: schemas.ast_search,
|
|
183
|
-
modelInputSchema: undefined,
|
|
184
|
-
readOnly: true,
|
|
185
|
-
paths: () => [],
|
|
186
|
-
summarize: (input) => input.query,
|
|
187
|
-
execute: async (input) => withDb(async (db) => {
|
|
188
|
-
const { search } = await mod("query.mjs");
|
|
189
|
-
return { results: search(db, input.query, { limit: input.limit ?? 10 }) };
|
|
190
|
-
}),
|
|
191
|
-
},
|
|
192
|
-
{
|
|
193
|
-
name: "ast_callers",
|
|
194
|
-
description: "List the functions/classes that call or import a node in the code graph (upward dependencies).",
|
|
195
|
-
inputSchema: schemas.ast_callers,
|
|
196
|
-
readOnly: true,
|
|
197
|
-
paths: () => [],
|
|
198
|
-
summarize: (input) => input.id,
|
|
199
|
-
execute: async (input) => withDb(async (db) => {
|
|
200
|
-
const { callers, getNode } = await mod("query.mjs");
|
|
201
|
-
const node = getNode(db, input.id);
|
|
202
|
-
return node === undefined ? { error: `Unknown node id "${input.id}"` } : { node, callers: callers(db, input.id) };
|
|
203
|
-
}),
|
|
204
|
-
},
|
|
205
|
-
{
|
|
206
|
-
name: "ast_callees",
|
|
207
|
-
description: "List the functions a node calls or imports (downward dependencies).",
|
|
208
|
-
inputSchema: schemas.ast_callees,
|
|
209
|
-
readOnly: true,
|
|
210
|
-
paths: () => [],
|
|
211
|
-
summarize: (input) => input.id,
|
|
212
|
-
execute: async (input) => withDb(async (db) => {
|
|
213
|
-
const { callees, getNode } = await mod("query.mjs");
|
|
214
|
-
const node = getNode(db, input.id);
|
|
215
|
-
return node === undefined ? { error: `Unknown node id "${input.id}"` } : { node, callees: callees(db, input.id) };
|
|
216
|
-
}),
|
|
217
|
-
},
|
|
218
|
-
{
|
|
219
|
-
name: "ast_impact",
|
|
220
|
-
description: "Compute the K-hop blast radius of changing a node: who depends on it (up) or what it depends on (down). Use to scope a modification safely.",
|
|
221
|
-
inputSchema: schemas.ast_impact,
|
|
222
|
-
readOnly: true,
|
|
223
|
-
paths: () => [],
|
|
224
|
-
summarize: (input) => `${input.id} ${input.direction ?? "up"}x${input.hops ?? 2}`,
|
|
225
|
-
execute: async (input) => withDb(async (db) => {
|
|
226
|
-
const { impact, getNode } = await mod("query.mjs");
|
|
227
|
-
if (getNode(db, input.id) === undefined) return { error: `Unknown node id "${input.id}"` };
|
|
228
|
-
return impact(db, input.id, { hops: input.hops ?? 2, direction: input.direction ?? "up" });
|
|
229
|
-
}),
|
|
230
|
-
},
|
|
231
|
-
{
|
|
232
|
-
name: "ast_context",
|
|
233
|
-
description: "Assemble the precise subgraph around a node: file paths and line ranges of the anchor, its callers and callees. Read only these ranges instead of grepping.",
|
|
234
|
-
inputSchema: schemas.ast_context,
|
|
235
|
-
readOnly: true,
|
|
236
|
-
paths: () => [],
|
|
237
|
-
summarize: (input) => input.id,
|
|
238
|
-
execute: async (input) => withDb(async (db) => {
|
|
239
|
-
const { subgraphContext } = await mod("query.mjs");
|
|
240
|
-
return subgraphContext(db, input.id, { hops: input.hops ?? 1 });
|
|
241
|
-
}),
|
|
242
|
-
},
|
|
243
|
-
];
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/** Extract workspace-relative file paths from a PostToolUse event's tool input. */
|
|
247
|
-
function changedPaths(event, root) {
|
|
248
|
-
const payload = event?.payload;
|
|
249
|
-
if (payload === undefined) return [];
|
|
250
|
-
const input = payload.input ?? {};
|
|
251
|
-
const tool = payload.tool;
|
|
252
|
-
const paths = [];
|
|
253
|
-
if (tool === "Edit" || tool === "Write") {
|
|
254
|
-
if (typeof input.path === "string") paths.push(input.path);
|
|
255
|
-
} else if (tool === "ApplyPatch" && typeof input.patch === "string") {
|
|
256
|
-
for (const line of input.patch.split("\n")) {
|
|
257
|
-
if (!line.startsWith("+++ ")) continue;
|
|
258
|
-
const target = line.slice(4).trim().replace(/^b\//, "");
|
|
259
|
-
if (target !== "/dev/null") paths.push(target);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
return paths
|
|
263
|
-
.map((path) => workspaceRelative(root, path))
|
|
264
|
-
.filter((path) => path !== undefined && /\.(ts|tsx|js|jsx|mjs|cjs|mts|cts)$/.test(path));
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export function activate(context) {
|
|
268
|
-
// Per-plugin-instance workspace cache, populated by SessionStart; falls back
|
|
269
|
-
// to the host's command context workspace or the process cwd.
|
|
270
|
-
let workspaceRoot;
|
|
271
|
-
const rootRef = () => workspaceRoot ?? process.cwd();
|
|
272
|
-
|
|
273
|
-
context.registerCommand("ast", commandAst, "Build and query the code AST graph");
|
|
274
|
-
|
|
275
|
-
const disposers = [];
|
|
276
|
-
for (const tool of createTools(rootRef)) disposers.push(context.registerTool(tool.name, tool));
|
|
277
|
-
|
|
278
|
-
disposers.push(context.registerHook("SessionStart", (event) => {
|
|
279
|
-
workspaceRoot = workspaceFrom(event) ?? workspaceRoot;
|
|
280
|
-
return ALLOW;
|
|
281
|
-
}, { failurePolicy: "allow" }));
|
|
282
|
-
|
|
283
|
-
// Incremental sync after file-mutating tools. Fire-and-forget semantics:
|
|
284
|
-
// never block or fail the agent's edit.
|
|
285
|
-
let syncing = Promise.resolve();
|
|
286
|
-
disposers.push(context.registerHook("PostToolUse", (event) => {
|
|
287
|
-
const root = rootRef();
|
|
288
|
-
const paths = changedPaths(event, root);
|
|
289
|
-
if (paths.length === 0) return ALLOW;
|
|
290
|
-
syncing = syncing
|
|
291
|
-
.then(() => runIndex(root, { onlyPaths: paths }))
|
|
292
|
-
.catch((error) => context.logger.debug(`astgraph sync skipped: ${error?.message ?? error}`));
|
|
293
|
-
return ALLOW;
|
|
294
|
-
}, { failurePolicy: "allow", timeoutMs: 10_000 }));
|
|
295
|
-
|
|
296
|
-
return async () => {
|
|
297
|
-
for (const dispose of disposers.reverse()) {
|
|
298
|
-
try { await dispose(); } catch { /* ignore */ }
|
|
299
|
-
}
|
|
300
|
-
};
|
|
301
|
-
}
|