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/index.js
CHANGED
|
@@ -1,301 +1,301 @@
|
|
|
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
|
-
}
|
|
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
|
+
}
|