token-goat 2.9.14 → 2.9.16

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 (46) hide show
  1. package/README.md +15 -1
  2. package/dist/{token-goat-chunk-2ESBO4IN.mjs → token-goat-chunk-2IEIBVVD.mjs} +16 -9
  3. package/dist/{token-goat-chunk-RUDOKYPJ.mjs → token-goat-chunk-2PCFAQAS.mjs} +6582 -6194
  4. package/dist/{token-goat-chunk-AMYCQJX4.mjs → token-goat-chunk-3H6FQT62.mjs} +49 -256
  5. package/dist/{token-goat-chunk-PRJVGIC5.mjs → token-goat-chunk-4DC6DDIB.mjs} +7 -5
  6. package/dist/token-goat-chunk-4EXFN2AW.mjs +29 -0
  7. package/dist/{token-goat-chunk-VZYD4OZB.mjs → token-goat-chunk-5USHO7B6.mjs} +416 -4168
  8. package/dist/token-goat-chunk-63QWJ6UM.mjs +277 -0
  9. package/dist/token-goat-chunk-BFNTLGVA.mjs +4547 -0
  10. package/dist/token-goat-chunk-BGHSSJUA.mjs +102 -0
  11. package/dist/{token-goat-chunk-T2IWWTHB.mjs → token-goat-chunk-DMQ32ZSR.mjs} +35 -1142
  12. package/dist/{token-goat-chunk-DBNY4RLN.mjs → token-goat-chunk-EJCY6KHO.mjs} +5 -3
  13. package/dist/token-goat-chunk-ERTXEKB6.mjs +228 -0
  14. package/dist/{token-goat-chunk-Z6UXPYJA.mjs → token-goat-chunk-F2ARFHLJ.mjs} +1 -1
  15. package/dist/{token-goat-chunk-EG3663UT.mjs → token-goat-chunk-GIMGT34T.mjs} +4 -2
  16. package/dist/{token-goat-chunk-XVZ4MNQC.mjs → token-goat-chunk-GMXBQS6O.mjs} +360 -116
  17. package/dist/{token-goat-chunk-SAQ5PG4L.mjs → token-goat-chunk-H3DXI6VD.mjs} +8 -4
  18. package/dist/token-goat-chunk-LJL6FGDS.mjs +35 -0
  19. package/dist/{token-goat-chunk-NEI4NC54.mjs → token-goat-chunk-LKSXAMJB.mjs} +7 -2
  20. package/dist/token-goat-chunk-LMFO66YD.mjs +331 -0
  21. package/dist/{token-goat-chunk-7OGKZ7AP.mjs → token-goat-chunk-M2R4UQ3B.mjs} +5 -3
  22. package/dist/{token-goat-chunk-YHGTGG6K.mjs → token-goat-chunk-MA5BZ2KI.mjs} +68 -7
  23. package/dist/{token-goat-chunk-ASVVF4JV.mjs → token-goat-chunk-MIL62GMN.mjs} +171 -23
  24. package/dist/{token-goat-chunk-ZFM4PWXL.mjs → token-goat-chunk-NAMH252H.mjs} +10 -7
  25. package/dist/{token-goat-chunk-YTQHZJXW.mjs → token-goat-chunk-NOBZV7PM.mjs} +96 -22
  26. package/dist/{token-goat-chunk-4SDX3QP3.mjs → token-goat-chunk-NQZCSVN2.mjs} +13 -5
  27. package/dist/{token-goat-chunk-XEH6KBWW.mjs → token-goat-chunk-OCI25AXY.mjs} +4 -3
  28. package/dist/token-goat-chunk-OSUFN2FV.mjs +326 -0
  29. package/dist/{token-goat-chunk-O5WAMISC.mjs → token-goat-chunk-QMZMHQRR.mjs} +29 -18
  30. package/dist/{token-goat-chunk-POBYR64E.mjs → token-goat-chunk-R3X5TCPB.mjs} +801 -3
  31. package/dist/token-goat-chunk-RRNZMM3A.mjs +3364 -0
  32. package/dist/{token-goat-chunk-PGGDW7DZ.mjs → token-goat-chunk-ULA4WCED.mjs} +9 -6
  33. package/dist/token-goat-chunk-UMXD2M7O.mjs +975 -0
  34. package/dist/{token-goat-chunk-3XQPEJMV.mjs → token-goat-chunk-UXHDBVUR.mjs} +1 -1
  35. package/dist/{token-goat-chunk-EZNVAIR3.mjs → token-goat-chunk-VVIYEFBI.mjs} +4 -3
  36. package/dist/token-goat-chunk-XOHTFRDH.mjs +10760 -0
  37. package/dist/{token-goat-chunk-HF6H7RNK.mjs → token-goat-chunk-YPS64ACE.mjs} +13 -3
  38. package/dist/{token-goat-chunk-DAMXYVIW.mjs → token-goat-chunk-Z5VPAXTC.mjs} +2560 -820
  39. package/dist/{token-goat-chunk-XQF5J25J.mjs → token-goat-chunk-ZEHFYJ7Z.mjs} +5 -4
  40. package/dist/token-goat-hook.mjs +15 -10
  41. package/dist/token-goat.core.mjs +24 -18
  42. package/docs/cli.md +8 -6
  43. package/package.json +1 -1
  44. package/dist/token-goat-chunk-IT6O3PNN.mjs +0 -15463
  45. package/dist/token-goat-chunk-ZD4EM4LR.mjs +0 -30
  46. package/dist/token-goat-chunk-ZYNNQ36L.mjs +0 -479
@@ -0,0 +1,277 @@
1
+ import { createRequire as __cjsRequire } from 'node:module';
2
+ const require = __cjsRequire(import.meta.url);
3
+ import {
4
+ getDb
5
+ } from "./token-goat-chunk-R3X5TCPB.mjs";
6
+ import {
7
+ foldPath,
8
+ globalDbPath,
9
+ isCaseInsensitiveFs
10
+ } from "./token-goat-chunk-3H6FQT62.mjs";
11
+ import {
12
+ normalizePath
13
+ } from "./token-goat-chunk-ERTXEKB6.mjs";
14
+ import {
15
+ init_define_import_meta_env
16
+ } from "./token-goat-chunk-A37V4PBF.mjs";
17
+
18
+ // src/index_reader.ts
19
+ init_define_import_meta_env();
20
+
21
+ // src/sql_path.ts
22
+ init_define_import_meta_env();
23
+ function pathEqClause(column) {
24
+ return isCaseInsensitiveFs() ? `TG_LOWER(${column}) = ?` : `${column} = ?`;
25
+ }
26
+ function pathSuffixClause(column) {
27
+ const col = isCaseInsensitiveFs() ? `TG_LOWER(${column})` : column;
28
+ return {
29
+ // Both separators are accepted because not every writer into `symbols` stores a normalizePath'd (forward-slash) file_path, and the JS-side path-boundary test this narrowing feeds treats `/` and `\` alike.
30
+ clause: `(${col} = ? OR substr(${col}, -length(?)) = ? OR substr(${col}, -length(?)) = ?)`,
31
+ params: (baseName) => {
32
+ const folded = foldPath(baseName);
33
+ return [folded, `/${folded}`, `/${folded}`, `\\${folded}`, `\\${folded}`];
34
+ }
35
+ };
36
+ }
37
+ function projectScopeClause(column) {
38
+ const caseInsensitive = isCaseInsensitiveFs();
39
+ const col = caseInsensitive ? `TG_LOWER(${column})` : column;
40
+ return {
41
+ // Parenthesised so an OR anywhere in the surrounding WHERE cannot bind tighter than the pair.
42
+ clause: `(${col} >= ? AND ${col} < ?)`,
43
+ params: (root) => {
44
+ const folded = caseInsensitive ? foldPath(normalizePath(root)) : normalizePath(root);
45
+ const lower = folded.endsWith("/") ? folded : `${folded}/`;
46
+ const upper = `${lower.slice(0, -1)}0`;
47
+ return [lower, upper];
48
+ }
49
+ };
50
+ }
51
+
52
+ // src/index_reader.ts
53
+ function toSymbolEntry(row) {
54
+ return {
55
+ filePath: row.file_path,
56
+ name: row.name,
57
+ kind: row.kind,
58
+ lineStart: row.line_start,
59
+ lineEnd: row.line_end,
60
+ body: row.body ?? "",
61
+ docstring: row.docstring ?? "",
62
+ parent: row.parent ?? ""
63
+ };
64
+ }
65
+ function toRefEntry(row) {
66
+ return {
67
+ filePath: row.file_path,
68
+ name: row.name,
69
+ line: row.line,
70
+ col: row.col,
71
+ context: row.context ?? ""
72
+ };
73
+ }
74
+ function applyRootDirScope(rootDir, column, where, params) {
75
+ if (rootDir === void 0) return;
76
+ const { clause, params: bounds } = projectScopeClause(column);
77
+ where.push(clause);
78
+ params.push(...bounds(rootDir));
79
+ }
80
+ function buildSymbolWhere(opts) {
81
+ const where = [];
82
+ const params = [];
83
+ if (opts.name !== void 0) {
84
+ where.push("name = ?");
85
+ params.push(opts.name);
86
+ }
87
+ if (opts.filePath !== void 0) {
88
+ if (opts.filePath.includes("/") || opts.filePath.includes("\\")) {
89
+ const alt = opts.filePath.includes("/") ? opts.filePath.replace(/\//g, "\\") : opts.filePath.replace(/\\/g, "/");
90
+ where.push(`(${pathEqClause("file_path")} OR ${pathEqClause("file_path")})`);
91
+ params.push(foldPath(opts.filePath), foldPath(alt));
92
+ } else {
93
+ where.push(pathEqClause("file_path"));
94
+ params.push(foldPath(opts.filePath));
95
+ }
96
+ }
97
+ if (opts.kind !== void 0) {
98
+ where.push("kind = ?");
99
+ params.push(opts.kind);
100
+ }
101
+ if (opts.fileBaseName !== void 0) {
102
+ const { clause: suffixClause, params: suffixParams } = pathSuffixClause("file_path");
103
+ where.push(suffixClause);
104
+ params.push(...suffixParams(opts.fileBaseName));
105
+ }
106
+ if (opts.enclosingLine !== void 0) {
107
+ where.push("line_start <= ? AND ? <= line_end");
108
+ params.push(opts.enclosingLine, opts.enclosingLine);
109
+ }
110
+ applyRootDirScope(opts.rootDir, "file_path", where, params);
111
+ return { clause: where.length > 0 ? `WHERE ${where.join(" AND ")}` : "", params };
112
+ }
113
+ var DEFAULT_QUERY_LIMIT = 100;
114
+ function querySymbols(opts = {}, dbPath = globalDbPath()) {
115
+ const { clause, params } = buildSymbolWhere(opts);
116
+ const limit = opts.limit ?? DEFAULT_QUERY_LIMIT;
117
+ const offset = opts.offset ?? 0;
118
+ const sql = `SELECT file_path, name, kind, line_start, line_end, body, docstring, parent FROM symbols ${clause} ORDER BY file_path, line_start, rowid LIMIT ? OFFSET ?`;
119
+ const db = getDb(dbPath);
120
+ const rows = db.prepare(sql).all(...params, limit, offset);
121
+ return rows.map(toSymbolEntry);
122
+ }
123
+ function distinctSymbolKinds(rootDir, dbPath = globalDbPath()) {
124
+ const where = [];
125
+ const params = [];
126
+ applyRootDirScope(rootDir, "file_path", where, params);
127
+ const clause = where.length > 0 ? `WHERE ${where.join(" AND ")}` : "";
128
+ const db = getDb(dbPath);
129
+ const rows = db.prepare(`SELECT DISTINCT kind FROM symbols ${clause} ORDER BY kind`).all(...params);
130
+ return rows.map((r) => r.kind);
131
+ }
132
+ function countSymbols(opts = {}, dbPath = globalDbPath()) {
133
+ const { clause, params } = buildSymbolWhere(opts);
134
+ const sql = `SELECT COUNT(*) as cnt FROM symbols ${clause}`;
135
+ const db = getDb(dbPath);
136
+ const row = db.prepare(sql).get(...params);
137
+ return row.cnt;
138
+ }
139
+ function buildRefsWhere(opts) {
140
+ const where = ["name = ?"];
141
+ const params = [opts.name];
142
+ if (opts.filePath !== void 0) {
143
+ where.push(pathEqClause("file_path"));
144
+ params.push(foldPath(opts.filePath));
145
+ }
146
+ applyRootDirScope(opts.rootDir, "file_path", where, params);
147
+ return { clause: `WHERE ${where.join(" AND ")}`, params };
148
+ }
149
+ function queryRefs(opts, dbPath = globalDbPath()) {
150
+ const { clause, params } = buildRefsWhere(opts);
151
+ const limit = opts.limit ?? DEFAULT_QUERY_LIMIT;
152
+ const sql = `SELECT file_path, name, line, col, context FROM refs ${clause} ORDER BY file_path, line LIMIT ?`;
153
+ const db = getDb(dbPath);
154
+ const rows = db.prepare(sql).all(...params, limit);
155
+ return rows.map(toRefEntry);
156
+ }
157
+ function countRefs(opts, dbPath = globalDbPath()) {
158
+ const { clause, params } = buildRefsWhere(opts);
159
+ const sql = `SELECT COUNT(*) as cnt FROM refs ${clause}`;
160
+ const db = getDb(dbPath);
161
+ const row = db.prepare(sql).get(...params);
162
+ return row.cnt;
163
+ }
164
+ function queryRefsByContext(context, filePath, dbPath = globalDbPath()) {
165
+ const sql = `SELECT file_path, name, line, col, context FROM refs WHERE context = ? AND ${pathEqClause("file_path")} ORDER BY line LIMIT 20`;
166
+ const db = getDb(dbPath);
167
+ const rows = db.prepare(sql).all(context, foldPath(filePath));
168
+ return rows.map(toRefEntry);
169
+ }
170
+ var REF_COUNT_BATCH = 900;
171
+ function queryRefCounts(names, dbPath = globalDbPath(), rootDir) {
172
+ const counts = /* @__PURE__ */ new Map();
173
+ if (names.length === 0) return counts;
174
+ const db = getDb(dbPath);
175
+ const scopeWhere = [];
176
+ const scopeParams = [];
177
+ applyRootDirScope(rootDir, "file_path", scopeWhere, scopeParams);
178
+ const scopeSql = scopeWhere.length > 0 ? ` AND ${scopeWhere.join(" AND ")}` : "";
179
+ const sqlFor = (width) => `SELECT name, COUNT(*) as c FROM refs WHERE name IN (${Array.from({ length: width }, () => "?").join(", ")})${scopeSql} GROUP BY name`;
180
+ const full = names.length >= REF_COUNT_BATCH ? db.prepare(sqlFor(REF_COUNT_BATCH)) : void 0;
181
+ db.transaction(() => {
182
+ for (let start = 0; start < names.length; start += REF_COUNT_BATCH) {
183
+ const batch = names.slice(start, start + REF_COUNT_BATCH);
184
+ const stmt = batch.length === REF_COUNT_BATCH && full !== void 0 ? full : db.prepare(sqlFor(batch.length));
185
+ for (const row of stmt.all(...batch, ...scopeParams)) {
186
+ counts.set(row.name, row.c);
187
+ }
188
+ }
189
+ })();
190
+ return counts;
191
+ }
192
+ function getProjectFileEntries(rootDir, dbPath = globalDbPath()) {
193
+ const db = getDb(dbPath);
194
+ const { clause, params } = projectScopeClause("path");
195
+ const rows = db.prepare(`SELECT path, sha, mtime, language, indexed_at, embed_sha, parser_sha FROM files WHERE ${clause}`).all(...params(rootDir));
196
+ const out = /* @__PURE__ */ new Map();
197
+ for (const row of rows) {
198
+ out.set(foldPath(row.path), {
199
+ filePath: row.path,
200
+ sha: row.sha ?? "",
201
+ mtime: row.mtime ?? 0,
202
+ language: row.language ?? "unknown",
203
+ indexedAt: row.indexed_at ?? 0,
204
+ embedSha: row.embed_sha ?? "",
205
+ parserSha: row.parser_sha ?? ""
206
+ });
207
+ }
208
+ return out;
209
+ }
210
+ function getFileEntry(filePath, dbPath = globalDbPath()) {
211
+ const db = getDb(dbPath);
212
+ let row = db.prepare(
213
+ `SELECT path, sha, mtime, language, indexed_at, embed_sha, parser_sha FROM files WHERE ${pathEqClause("path")}`
214
+ ).get(foldPath(filePath));
215
+ if (row === void 0 && (filePath.includes("/") || filePath.includes("\\"))) {
216
+ const altPath = filePath.includes("/") ? filePath.replace(/\//g, "\\") : filePath.replace(/\\/g, "/");
217
+ row = db.prepare(
218
+ `SELECT path, sha, mtime, language, indexed_at, embed_sha, parser_sha FROM files WHERE ${pathEqClause("path")}`
219
+ ).get(foldPath(altPath));
220
+ }
221
+ if (row === void 0) return null;
222
+ return {
223
+ filePath: row.path,
224
+ sha: row.sha ?? "",
225
+ mtime: row.mtime ?? 0,
226
+ language: row.language ?? "unknown",
227
+ indexedAt: row.indexed_at ?? 0,
228
+ embedSha: row.embed_sha ?? "",
229
+ parserSha: row.parser_sha ?? ""
230
+ };
231
+ }
232
+ function sanitizeFtsQuery(query, join = "AND") {
233
+ const terms = query.split(/\s+/).filter((t) => t.length > 0).map((t) => '"' + t.replace(/"/g, '""') + '"');
234
+ return terms.join(join === "OR" ? " OR " : " ");
235
+ }
236
+ function runFtsQuery(db, match, limit, scope, rootDir) {
237
+ const sql = `SELECT s.file_path, s.name, s.kind, s.line_start, s.line_end, s.body, s.docstring, s.parent FROM symbols_fts JOIN symbols s ON s.id = symbols_fts.rowid WHERE symbols_fts MATCH ?${scope !== void 0 ? ` AND ${scope.clause}` : ""} ORDER BY bm25(symbols_fts) LIMIT ?`;
238
+ const params = [match];
239
+ if (scope !== void 0 && rootDir !== void 0) {
240
+ params.push(...scope.params(rootDir));
241
+ }
242
+ params.push(limit);
243
+ const rows = db.prepare(sql).all(...params);
244
+ return rows.map(toSymbolEntry);
245
+ }
246
+ function searchSymbolsFts(query, limit = 50, dbPath = globalDbPath(), rootDir) {
247
+ const andMatch = sanitizeFtsQuery(query, "AND");
248
+ if (andMatch === "") return [];
249
+ const db = getDb(dbPath);
250
+ const scope = rootDir !== void 0 ? projectScopeClause("s.file_path") : void 0;
251
+ try {
252
+ const andResults = runFtsQuery(db, andMatch, limit, scope, rootDir);
253
+ if (andResults.length > 0) return andResults;
254
+ const orMatch = sanitizeFtsQuery(query, "OR");
255
+ if (orMatch === andMatch) return andResults;
256
+ return runFtsQuery(db, orMatch, limit, scope, rootDir);
257
+ } catch {
258
+ return [];
259
+ }
260
+ }
261
+
262
+ export {
263
+ pathEqClause,
264
+ projectScopeClause,
265
+ DEFAULT_QUERY_LIMIT,
266
+ querySymbols,
267
+ distinctSymbolKinds,
268
+ countSymbols,
269
+ queryRefs,
270
+ countRefs,
271
+ queryRefsByContext,
272
+ queryRefCounts,
273
+ getProjectFileEntries,
274
+ getFileEntry,
275
+ sanitizeFtsQuery,
276
+ searchSymbolsFts
277
+ };