flavor-code 1.2.11 → 1.2.13

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