@ajdev0/token-shrink 2.0.0
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 +345 -0
- package/dist/chunk-7H6PGILN.js +86 -0
- package/dist/chunk-7H6PGILN.js.map +1 -0
- package/dist/chunk-7SQ6HMWM.js +145 -0
- package/dist/chunk-7SQ6HMWM.js.map +1 -0
- package/dist/chunk-HRF3BIOV.js +518 -0
- package/dist/chunk-HRF3BIOV.js.map +1 -0
- package/dist/chunk-LPJMNP4N.js +179 -0
- package/dist/chunk-LPJMNP4N.js.map +1 -0
- package/dist/cli-EpVqinpB.d.cts +96 -0
- package/dist/cli-EpVqinpB.d.ts +96 -0
- package/dist/cli.cjs +771 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +4 -0
- package/dist/cli.d.ts +4 -0
- package/dist/cli.js +10 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.cjs +988 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +169 -0
- package/dist/index.d.ts +169 -0
- package/dist/index.js +68 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp.cjs +856 -0
- package/dist/mcp.cjs.map +1 -0
- package/dist/mcp.d.cts +58 -0
- package/dist/mcp.d.ts +58 -0
- package/dist/mcp.js +26 -0
- package/dist/mcp.js.map +1 -0
- package/dist/registry-JLP6X4QB.js +14 -0
- package/dist/registry-JLP6X4QB.js.map +1 -0
- package/dist/tree-sitter-typescript.wasm +0 -0
- package/package.json +59 -0
- package/wasm/tree-sitter-typescript.wasm +0 -0
package/dist/mcp.cjs
ADDED
|
@@ -0,0 +1,856 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __esm = (fn, res) => function __init() {
|
|
10
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
11
|
+
};
|
|
12
|
+
var __export = (target, all) => {
|
|
13
|
+
for (var name in all)
|
|
14
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from))
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
25
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
26
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
27
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
28
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
29
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
30
|
+
mod
|
|
31
|
+
));
|
|
32
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
33
|
+
|
|
34
|
+
// src/parser/registry.ts
|
|
35
|
+
function languageForFile(filePath) {
|
|
36
|
+
const langId = extensionIndex.get(import_node_path2.default.extname(filePath).toLowerCase());
|
|
37
|
+
if (!langId) return null;
|
|
38
|
+
return registry[langId];
|
|
39
|
+
}
|
|
40
|
+
var import_node_path2, BLOCK, SWIFT_BLOCK, GO_BLOCK, RUST_BLOCK, PY_BLOCK, C_BLOCK, JAVA_BLOCK, PHP_BLOCK, KOTLIN_BLOCK, DART_BLOCK, DIRECTIVE_KEEP, registry, extensionIndex, supportedLanguageIds, allExtensions;
|
|
41
|
+
var init_registry = __esm({
|
|
42
|
+
"src/parser/registry.ts"() {
|
|
43
|
+
"use strict";
|
|
44
|
+
import_node_path2 = __toESM(require("path"), 1);
|
|
45
|
+
BLOCK = { query: "(statement_block) @block", replacement: { token: "/* ... */" } };
|
|
46
|
+
SWIFT_BLOCK = { query: "(statements) @block", replacement: { token: "/* ... */" } };
|
|
47
|
+
GO_BLOCK = { query: "(block) @block", replacement: { token: "/* ... */" } };
|
|
48
|
+
RUST_BLOCK = { query: "(block) @block", replacement: { token: "/* ... */" } };
|
|
49
|
+
PY_BLOCK = { query: "(block) @block", replacement: { token: "pass" } };
|
|
50
|
+
C_BLOCK = { query: "(compound_statement) @block", replacement: { token: "/* ... */" } };
|
|
51
|
+
JAVA_BLOCK = {
|
|
52
|
+
query: "(block) @block",
|
|
53
|
+
replacement: { token: "/* ... */" }
|
|
54
|
+
};
|
|
55
|
+
PHP_BLOCK = {
|
|
56
|
+
query: "(compound_statement) @block",
|
|
57
|
+
replacement: { token: "/* ... */" }
|
|
58
|
+
};
|
|
59
|
+
KOTLIN_BLOCK = { query: "(block) @block", replacement: { token: "/* ... */" } };
|
|
60
|
+
DART_BLOCK = { query: "(block) @block", replacement: { token: "/* ... */" } };
|
|
61
|
+
DIRECTIVE_KEEP = {
|
|
62
|
+
token: "/* ... */",
|
|
63
|
+
keepIf: /^\s*'use (client|server)'/
|
|
64
|
+
};
|
|
65
|
+
registry = {
|
|
66
|
+
typescript: {
|
|
67
|
+
name: "TypeScript",
|
|
68
|
+
wasm: "tree-sitter-typescript.wasm",
|
|
69
|
+
url: "https://github.com/tree-sitter/tree-sitter-typescript/releases/latest/download/tree-sitter-typescript.wasm",
|
|
70
|
+
extensions: [".ts", ".cts", ".mts"],
|
|
71
|
+
rules: [BLOCK]
|
|
72
|
+
},
|
|
73
|
+
javascript: {
|
|
74
|
+
name: "JavaScript",
|
|
75
|
+
wasm: "tree-sitter-javascript.wasm",
|
|
76
|
+
url: "https://github.com/tree-sitter/tree-sitter-javascript/releases/latest/download/tree-sitter-javascript.wasm",
|
|
77
|
+
extensions: [".js", ".cjs", ".mjs"],
|
|
78
|
+
rules: [BLOCK]
|
|
79
|
+
},
|
|
80
|
+
tsx: {
|
|
81
|
+
name: "React / Next.js (TSX)",
|
|
82
|
+
wasm: "tree-sitter-tsx.wasm",
|
|
83
|
+
url: "https://github.com/tree-sitter/tree-sitter-typescript/releases/latest/download/tree-sitter-tsx.wasm",
|
|
84
|
+
extensions: [".tsx"],
|
|
85
|
+
rules: [{ query: "(statement_block) @block", replacement: DIRECTIVE_KEEP }]
|
|
86
|
+
},
|
|
87
|
+
jsx: {
|
|
88
|
+
name: "React (JSX)",
|
|
89
|
+
wasm: "tree-sitter-javascript.wasm",
|
|
90
|
+
url: "https://github.com/tree-sitter/tree-sitter-javascript/releases/latest/download/tree-sitter-javascript.wasm",
|
|
91
|
+
extensions: [".jsx"],
|
|
92
|
+
rules: [{ query: "(statement_block) @block", replacement: DIRECTIVE_KEEP }]
|
|
93
|
+
},
|
|
94
|
+
python: {
|
|
95
|
+
name: "Python",
|
|
96
|
+
wasm: "tree-sitter-python.wasm",
|
|
97
|
+
url: "https://github.com/tree-sitter/tree-sitter-python/releases/latest/download/tree-sitter-python.wasm",
|
|
98
|
+
extensions: [".py", ".pyi"],
|
|
99
|
+
rules: [PY_BLOCK]
|
|
100
|
+
},
|
|
101
|
+
dart: {
|
|
102
|
+
name: "Dart / Flutter",
|
|
103
|
+
wasm: "tree-sitter-dart.wasm",
|
|
104
|
+
url: "https://github.com/UserNobody14/tree-sitter-dart.wasm/releases/latest/download/tree-sitter-dart.wasm",
|
|
105
|
+
extensions: [".dart"],
|
|
106
|
+
rules: [DART_BLOCK]
|
|
107
|
+
},
|
|
108
|
+
swift: {
|
|
109
|
+
name: "Swift / SwiftUI",
|
|
110
|
+
wasm: "tree-sitter-swift.wasm",
|
|
111
|
+
url: "https://github.com/alex-pinkus/tree-sitter-swift/releases/latest/download/tree-sitter-swift.wasm",
|
|
112
|
+
extensions: [".swift"],
|
|
113
|
+
rules: [SWIFT_BLOCK]
|
|
114
|
+
},
|
|
115
|
+
go: {
|
|
116
|
+
name: "Go",
|
|
117
|
+
wasm: "tree-sitter-go.wasm",
|
|
118
|
+
url: "https://github.com/tree-sitter/tree-sitter-go/releases/latest/download/tree-sitter-go.wasm",
|
|
119
|
+
extensions: [".go"],
|
|
120
|
+
rules: [GO_BLOCK]
|
|
121
|
+
},
|
|
122
|
+
rust: {
|
|
123
|
+
name: "Rust",
|
|
124
|
+
wasm: "tree-sitter-rust.wasm",
|
|
125
|
+
url: "https://github.com/tree-sitter/tree-sitter-rust/releases/latest/download/tree-sitter-rust.wasm",
|
|
126
|
+
extensions: [".rs"],
|
|
127
|
+
rules: [RUST_BLOCK]
|
|
128
|
+
},
|
|
129
|
+
java: {
|
|
130
|
+
name: "Java",
|
|
131
|
+
wasm: "tree-sitter-java.wasm",
|
|
132
|
+
url: "https://github.com/tree-sitter/tree-sitter-java/releases/latest/download/tree-sitter-java.wasm",
|
|
133
|
+
extensions: [".java"],
|
|
134
|
+
rules: [JAVA_BLOCK]
|
|
135
|
+
},
|
|
136
|
+
kotlin: {
|
|
137
|
+
name: "Kotlin",
|
|
138
|
+
wasm: "tree-sitter-kotlin.wasm",
|
|
139
|
+
url: "https://github.com/fwcd/tree-sitter-kotlin/releases/latest/download/tree-sitter-kotlin.wasm",
|
|
140
|
+
extensions: [".kt", ".kts"],
|
|
141
|
+
rules: [KOTLIN_BLOCK]
|
|
142
|
+
},
|
|
143
|
+
c: {
|
|
144
|
+
name: "C",
|
|
145
|
+
wasm: "tree-sitter-c.wasm",
|
|
146
|
+
url: "https://github.com/tree-sitter/tree-sitter-c/releases/latest/download/tree-sitter-c.wasm",
|
|
147
|
+
extensions: [".c", ".h"],
|
|
148
|
+
rules: [C_BLOCK]
|
|
149
|
+
},
|
|
150
|
+
cpp: {
|
|
151
|
+
name: "C++",
|
|
152
|
+
wasm: "tree-sitter-cpp.wasm",
|
|
153
|
+
url: "https://github.com/tree-sitter/tree-sitter-cpp/releases/latest/download/tree-sitter-cpp.wasm",
|
|
154
|
+
extensions: [".cc", ".cpp", ".cxx", ".hpp", ".hh", ".hxx"],
|
|
155
|
+
rules: [C_BLOCK]
|
|
156
|
+
},
|
|
157
|
+
php: {
|
|
158
|
+
name: "PHP",
|
|
159
|
+
wasm: "tree-sitter-php.wasm",
|
|
160
|
+
url: "https://github.com/tree-sitter/tree-sitter-php/releases/latest/download/tree-sitter-php.wasm",
|
|
161
|
+
extensions: [".php"],
|
|
162
|
+
rules: [PHP_BLOCK]
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
extensionIndex = /* @__PURE__ */ new Map();
|
|
166
|
+
for (const [id, spec] of Object.entries(registry)) {
|
|
167
|
+
for (const ext2 of spec.extensions) {
|
|
168
|
+
extensionIndex.set(ext2, id);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
supportedLanguageIds = Object.keys(registry);
|
|
172
|
+
allExtensions = [...extensionIndex.keys()];
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// src/mcp.ts
|
|
177
|
+
var mcp_exports = {};
|
|
178
|
+
__export(mcp_exports, {
|
|
179
|
+
AUTO_RULE_PATH: () => AUTO_RULE_PATH,
|
|
180
|
+
AUTO_RULE_SENTINEL: () => AUTO_RULE_SENTINEL,
|
|
181
|
+
CLAUDE_RULE_PATH: () => CLAUDE_RULE_PATH,
|
|
182
|
+
CLAUDE_RULE_SENTINEL: () => CLAUDE_RULE_SENTINEL,
|
|
183
|
+
CURSOR_RULE_PATH: () => CURSOR_RULE_PATH,
|
|
184
|
+
RULE_TARGET_PATH: () => RULE_TARGET_PATH,
|
|
185
|
+
createAutoRule: () => createAutoRule,
|
|
186
|
+
createCursorRule: () => createCursorRule,
|
|
187
|
+
startMcpServer: () => startMcpServer
|
|
188
|
+
});
|
|
189
|
+
module.exports = __toCommonJS(mcp_exports);
|
|
190
|
+
var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
191
|
+
var import_stdio = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
192
|
+
var import_zod = require("zod");
|
|
193
|
+
var import_node_fs4 = __toESM(require("fs"), 1);
|
|
194
|
+
var import_node_path6 = __toESM(require("path"), 1);
|
|
195
|
+
|
|
196
|
+
// src/server/assembler.ts
|
|
197
|
+
var import_node_fs = __toESM(require("fs"), 1);
|
|
198
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
199
|
+
function assemble(activeFilePath, cache, opts = {}) {
|
|
200
|
+
const { includeStats = false, maxSkeletons = 50, pruneActiveFile = false } = opts;
|
|
201
|
+
const abs = import_node_path.default.resolve(activeFilePath);
|
|
202
|
+
const activeSource = import_node_fs.default.existsSync(abs) ? import_node_fs.default.readFileSync(abs, "utf8") : "";
|
|
203
|
+
const activeEntry = cache.get(abs);
|
|
204
|
+
const included = [];
|
|
205
|
+
const unresolved = [];
|
|
206
|
+
const seen = /* @__PURE__ */ new Set();
|
|
207
|
+
if (activeEntry) {
|
|
208
|
+
for (const imp of activeEntry.imports) {
|
|
209
|
+
const entry = cache.get(imp);
|
|
210
|
+
if (!entry) {
|
|
211
|
+
unresolved.push(imp);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (seen.has(imp)) continue;
|
|
215
|
+
seen.add(imp);
|
|
216
|
+
included.push({ filePath: imp, language: entry.language });
|
|
217
|
+
if (included.length >= maxSkeletons) break;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (!activeEntry && activeSource) {
|
|
221
|
+
const specifiers = extractSpecifiers(activeSource);
|
|
222
|
+
for (const spec of specifiers) {
|
|
223
|
+
const resolved = resolveLocal(abs, spec);
|
|
224
|
+
if (!resolved) {
|
|
225
|
+
unresolved.push(spec);
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (seen.has(resolved)) continue;
|
|
229
|
+
seen.add(resolved);
|
|
230
|
+
const cached = cache.get(resolved);
|
|
231
|
+
included.push({ filePath: resolved, language: cached?.language ?? null });
|
|
232
|
+
if (included.length >= maxSkeletons) break;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const lines = [];
|
|
236
|
+
lines.push("# Compressed Code Context", "");
|
|
237
|
+
lines.push(`Active file: \`${rel(abs)}\``, "");
|
|
238
|
+
lines.push("## Ring 0 \u2014 Active file (full text)", "");
|
|
239
|
+
const ring0 = pruneActiveFile && activeEntry ? activeEntry.skeleton : activeSource;
|
|
240
|
+
lines.push(`\`\`\`${ext(activeFilePath)}`);
|
|
241
|
+
lines.push(ring0.trim() || "(empty or unreadable file)");
|
|
242
|
+
lines.push("```", "");
|
|
243
|
+
lines.push(
|
|
244
|
+
`## Ring 1 \u2014 Pruned dependencies (${included.length})`,
|
|
245
|
+
"",
|
|
246
|
+
"Implementation bodies removed; type signatures, interfaces and exports retained.",
|
|
247
|
+
""
|
|
248
|
+
);
|
|
249
|
+
if (included.length === 0) {
|
|
250
|
+
lines.push("_No local dependency skeletons available._", "");
|
|
251
|
+
}
|
|
252
|
+
for (const inc of included) {
|
|
253
|
+
const entry = cache.get(inc.filePath);
|
|
254
|
+
const label = rel(inc.filePath);
|
|
255
|
+
lines.push(`### \`${label}\``, "");
|
|
256
|
+
if (entry) {
|
|
257
|
+
lines.push(`\`\`\`${ext(inc.filePath)}`);
|
|
258
|
+
lines.push(entry.skeleton.trim());
|
|
259
|
+
lines.push("```", "");
|
|
260
|
+
} else {
|
|
261
|
+
lines.push("_Unindexed file._", "");
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (unresolved.length > 0) {
|
|
265
|
+
lines.push("## Unresolved imports", "");
|
|
266
|
+
for (const u of unresolved) lines.push(`- \`${u}\``);
|
|
267
|
+
lines.push("");
|
|
268
|
+
}
|
|
269
|
+
if (includeStats) {
|
|
270
|
+
const depTokens = included.reduce(
|
|
271
|
+
(acc, inc) => {
|
|
272
|
+
const e = cache.get(inc.filePath);
|
|
273
|
+
return e ? acc + approximateTokens(e.skeleton) : acc;
|
|
274
|
+
},
|
|
275
|
+
0
|
|
276
|
+
);
|
|
277
|
+
lines.push("---", "");
|
|
278
|
+
lines.push(
|
|
279
|
+
`_Token estimate \u2014 active: ${approximateTokens(activeSource)} \xB7 pruned deps: ${depTokens}._`,
|
|
280
|
+
""
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
markdown: lines.join("\n"),
|
|
285
|
+
activeFilePath: abs,
|
|
286
|
+
activeSource,
|
|
287
|
+
included,
|
|
288
|
+
unresolved
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
function extractSpecifiers(source) {
|
|
292
|
+
const found = [];
|
|
293
|
+
const re = /(?:from\s+['"`]|import\s+['"`]|require\s*\(\s*['"`])([^'"`]+)['"`]/g;
|
|
294
|
+
let m;
|
|
295
|
+
while ((m = re.exec(source)) !== null) {
|
|
296
|
+
if (m[1]) found.push(m[1]);
|
|
297
|
+
}
|
|
298
|
+
return [...new Set(found)];
|
|
299
|
+
}
|
|
300
|
+
function resolveLocal(importer, specifier) {
|
|
301
|
+
if (!/^[.~@]/.test(specifier)) return null;
|
|
302
|
+
if (specifier.startsWith("@/")) specifier = specifier.slice(2);
|
|
303
|
+
else if (specifier.startsWith("~")) specifier = specifier.slice(1);
|
|
304
|
+
const base = import_node_path.default.dirname(import_node_path.default.resolve(importer));
|
|
305
|
+
const p = import_node_path.default.resolve(base, specifier);
|
|
306
|
+
const candidates = [
|
|
307
|
+
p,
|
|
308
|
+
`${p}.ts`,
|
|
309
|
+
`${p}.tsx`,
|
|
310
|
+
`${p}.js`,
|
|
311
|
+
`${p}.jsx`,
|
|
312
|
+
`${p}.py`,
|
|
313
|
+
`${p}.go`,
|
|
314
|
+
`${p}.rs`,
|
|
315
|
+
`${p}.dart`,
|
|
316
|
+
`${p}.swift`,
|
|
317
|
+
`${p}.java`,
|
|
318
|
+
`${p}.kt`,
|
|
319
|
+
`${p}.c`,
|
|
320
|
+
`${p}.cpp`,
|
|
321
|
+
import_node_path.default.join(p, "index.ts"),
|
|
322
|
+
import_node_path.default.join(p, "index.js")
|
|
323
|
+
];
|
|
324
|
+
for (const c of candidates) {
|
|
325
|
+
if (import_node_fs.default.existsSync(c) && import_node_fs.default.statSync(c).isFile()) return import_node_path.default.resolve(c);
|
|
326
|
+
}
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
function rel(p) {
|
|
330
|
+
try {
|
|
331
|
+
const cwd = process.cwd();
|
|
332
|
+
if (p.startsWith(cwd)) return "." + p.slice(cwd.length);
|
|
333
|
+
} catch {
|
|
334
|
+
}
|
|
335
|
+
return p;
|
|
336
|
+
}
|
|
337
|
+
function ext(p) {
|
|
338
|
+
return import_node_path.default.extname(p).replace(/^\./, "") || "text";
|
|
339
|
+
}
|
|
340
|
+
function approximateTokens(text) {
|
|
341
|
+
if (!text) return 0;
|
|
342
|
+
const tokens = text.match(/[A-Za-z0-9_$]+|[^A-Za-z0-9_\s$]/g);
|
|
343
|
+
return tokens ? tokens.length : 0;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// src/watcher/sync.ts
|
|
347
|
+
var import_node_fs3 = __toESM(require("fs"), 1);
|
|
348
|
+
var import_node_path5 = __toESM(require("path"), 1);
|
|
349
|
+
var import_node_crypto = require("crypto");
|
|
350
|
+
var import_chokidar = __toESM(require("chokidar"), 1);
|
|
351
|
+
|
|
352
|
+
// src/parser/pruner.ts
|
|
353
|
+
var import_web_tree_sitter = __toESM(require("web-tree-sitter"), 1);
|
|
354
|
+
var import_node_module = require("module");
|
|
355
|
+
var import_node_path4 = __toESM(require("path"), 1);
|
|
356
|
+
init_registry();
|
|
357
|
+
|
|
358
|
+
// src/parser/wasm.ts
|
|
359
|
+
var import_node_fs2 = __toESM(require("fs"), 1);
|
|
360
|
+
var import_node_path3 = __toESM(require("path"), 1);
|
|
361
|
+
function resolveWasmDir() {
|
|
362
|
+
const candidates = [
|
|
363
|
+
import_node_path3.default.resolve(__dirname, "../../wasm"),
|
|
364
|
+
import_node_path3.default.resolve(__dirname, "../wasm"),
|
|
365
|
+
import_node_path3.default.resolve(process.cwd(), "wasm")
|
|
366
|
+
];
|
|
367
|
+
for (const dir2 of candidates) {
|
|
368
|
+
try {
|
|
369
|
+
import_node_fs2.default.mkdirSync(dir2, { recursive: true });
|
|
370
|
+
return dir2;
|
|
371
|
+
} catch {
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
const dir = import_node_path3.default.resolve(process.cwd(), "wasm");
|
|
375
|
+
import_node_fs2.default.mkdirSync(dir, { recursive: true });
|
|
376
|
+
return dir;
|
|
377
|
+
}
|
|
378
|
+
var wasmDir = null;
|
|
379
|
+
function cacheDir() {
|
|
380
|
+
if (!wasmDir) wasmDir = resolveWasmDir();
|
|
381
|
+
return wasmDir;
|
|
382
|
+
}
|
|
383
|
+
function cachePath(spec) {
|
|
384
|
+
return import_node_path3.default.join(cacheDir(), spec.wasm);
|
|
385
|
+
}
|
|
386
|
+
function looksLikeWasm(bytes) {
|
|
387
|
+
return bytes.length >= 4 && bytes[0] === 0 && bytes[1] === 97 && bytes[2] === 115 && bytes[3] === 109;
|
|
388
|
+
}
|
|
389
|
+
var inflight = /* @__PURE__ */ new Map();
|
|
390
|
+
function download(spec) {
|
|
391
|
+
const pending = inflight.get(spec.wasm);
|
|
392
|
+
if (pending) return pending;
|
|
393
|
+
const job = (async () => {
|
|
394
|
+
const res = await fetch(spec.url, { redirect: "follow" });
|
|
395
|
+
if (!res.ok) {
|
|
396
|
+
throw new Error(
|
|
397
|
+
`Failed to download grammar "${spec.wasm}" from ${spec.url} (HTTP ${res.status})`
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
const bytes = new Uint8Array(await res.arrayBuffer());
|
|
401
|
+
if (!looksLikeWasm(bytes)) {
|
|
402
|
+
throw new Error(
|
|
403
|
+
`Downloaded grammar "${spec.wasm}" is not valid WebAssembly (got HTML/redirect).`
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
const target = cachePath(spec);
|
|
407
|
+
const tmp = `${target}.${process.pid}.tmp`;
|
|
408
|
+
import_node_fs2.default.writeFileSync(tmp, bytes);
|
|
409
|
+
import_node_fs2.default.renameSync(tmp, target);
|
|
410
|
+
inflight.delete(spec.wasm);
|
|
411
|
+
return bytes;
|
|
412
|
+
})().catch((err) => {
|
|
413
|
+
inflight.delete(spec.wasm);
|
|
414
|
+
throw err;
|
|
415
|
+
});
|
|
416
|
+
inflight.set(spec.wasm, job);
|
|
417
|
+
return job;
|
|
418
|
+
}
|
|
419
|
+
async function getGrammar(spec, force = false) {
|
|
420
|
+
if (!force && import_node_fs2.default.existsSync(cachePath(spec))) {
|
|
421
|
+
const cached = import_node_fs2.default.readFileSync(cachePath(spec));
|
|
422
|
+
if (looksLikeWasm(cached)) return cached;
|
|
423
|
+
}
|
|
424
|
+
return download(spec);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// src/parser/pruner.ts
|
|
428
|
+
var languageCache = /* @__PURE__ */ new Map();
|
|
429
|
+
var initPromise = null;
|
|
430
|
+
function ensureParserInit() {
|
|
431
|
+
if (!initPromise) {
|
|
432
|
+
initPromise = import_web_tree_sitter.default.init({
|
|
433
|
+
locateFile: (file) => {
|
|
434
|
+
const require2 = (0, import_node_module.createRequire)(__filename);
|
|
435
|
+
try {
|
|
436
|
+
const pkgRoot = import_node_path4.default.dirname(require2.resolve("web-tree-sitter/package.json"));
|
|
437
|
+
return import_node_path4.default.join(pkgRoot, file);
|
|
438
|
+
} catch {
|
|
439
|
+
return file;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
return initPromise;
|
|
445
|
+
}
|
|
446
|
+
function loadLanguage(spec, force = false) {
|
|
447
|
+
if (!force) {
|
|
448
|
+
const cached = languageCache.get(spec.wasm);
|
|
449
|
+
if (cached) return Promise.resolve(cached);
|
|
450
|
+
}
|
|
451
|
+
if (force) languageCache.delete(spec.wasm);
|
|
452
|
+
return (async () => {
|
|
453
|
+
await ensureParserInit();
|
|
454
|
+
const grammar = await getGrammar(spec, force);
|
|
455
|
+
const language = await import_web_tree_sitter.default.Language.load(grammar);
|
|
456
|
+
languageCache.set(spec.wasm, language);
|
|
457
|
+
return language;
|
|
458
|
+
})();
|
|
459
|
+
}
|
|
460
|
+
async function prune(filePath, source, opts = {}) {
|
|
461
|
+
const spec = languageForFile(filePath);
|
|
462
|
+
if (!spec || spec.rules.length === 0) {
|
|
463
|
+
return { code: source, language: null, removed: 0 };
|
|
464
|
+
}
|
|
465
|
+
const language = await loadLanguage(spec, opts.forceDownload);
|
|
466
|
+
const parser = new import_web_tree_sitter.default();
|
|
467
|
+
parser.setLanguage(language);
|
|
468
|
+
const tree = parser.parse(source);
|
|
469
|
+
const ranges = [];
|
|
470
|
+
for (const rule of spec.rules) {
|
|
471
|
+
const query = language.query(rule.query);
|
|
472
|
+
const captures = query.captures(tree.rootNode);
|
|
473
|
+
for (const cap of captures) {
|
|
474
|
+
if (cap.node.startIndex === cap.node.endIndex) continue;
|
|
475
|
+
if (rule.replacement.keepIf?.test(cap.node.text)) continue;
|
|
476
|
+
ranges.push({
|
|
477
|
+
start: cap.node.startIndex,
|
|
478
|
+
end: cap.node.endIndex,
|
|
479
|
+
token: rule.replacement.token
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
query.delete();
|
|
483
|
+
}
|
|
484
|
+
tree.delete();
|
|
485
|
+
parser.delete();
|
|
486
|
+
const { code, removed } = spliceRanges(source, ranges);
|
|
487
|
+
return { code, language: spec.name, removed };
|
|
488
|
+
}
|
|
489
|
+
function spliceRanges(source, ranges) {
|
|
490
|
+
let removed = 0;
|
|
491
|
+
const sorted = [...ranges].sort((a, b) => b.start - a.start);
|
|
492
|
+
let out = source;
|
|
493
|
+
for (const r of sorted) {
|
|
494
|
+
if (r.start < 0 || r.end > out.length || r.end < r.start) continue;
|
|
495
|
+
removed += r.end - r.start;
|
|
496
|
+
out = out.slice(0, r.start) + r.token + out.slice(r.end);
|
|
497
|
+
}
|
|
498
|
+
return { code: out, removed };
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// src/watcher/sync.ts
|
|
502
|
+
var DEFAULT_IGNORED = [
|
|
503
|
+
/(^|[/\\])\.[^/\\]+/,
|
|
504
|
+
// dotfiles / dot-dirs (.git, .cursor, .env, ...)
|
|
505
|
+
/node_modules/,
|
|
506
|
+
/[/\\](build|dist|out|coverage|\.next|\.turbo|\.cache)[/\\]/
|
|
507
|
+
];
|
|
508
|
+
var ContextCache = class {
|
|
509
|
+
entries = /* @__PURE__ */ new Map();
|
|
510
|
+
initPromise = null;
|
|
511
|
+
ensureInit() {
|
|
512
|
+
if (!this.initPromise) {
|
|
513
|
+
this.initPromise = ensureParserInit();
|
|
514
|
+
}
|
|
515
|
+
return this.initPromise;
|
|
516
|
+
}
|
|
517
|
+
/** Returns the cached skeleton for a file, if present. */
|
|
518
|
+
getSkeleton(filePath) {
|
|
519
|
+
return this.entries.get(import_node_path5.default.resolve(filePath)) ?? null;
|
|
520
|
+
}
|
|
521
|
+
get imports() {
|
|
522
|
+
return this.entries;
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
function hashOf(text) {
|
|
526
|
+
return (0, import_node_crypto.createHash)("sha1").update(text).digest("hex");
|
|
527
|
+
}
|
|
528
|
+
function extractImports(_filePath, source) {
|
|
529
|
+
const out = [];
|
|
530
|
+
const re = /(?:from\s+['"`]([^'"`]+)['"`]|import\s+['"`]([^'"`]+)['"`]|require\s*\(\s*['"]([^'"]+)['"]\s*\))/g;
|
|
531
|
+
let m;
|
|
532
|
+
while ((m = re.exec(source)) !== null) {
|
|
533
|
+
const spec = m[1] ?? m[2] ?? m[3];
|
|
534
|
+
if (spec) out.push(spec);
|
|
535
|
+
}
|
|
536
|
+
return [...new Set(out)].filter(Boolean);
|
|
537
|
+
}
|
|
538
|
+
function resolveImport(importer, specifier, root) {
|
|
539
|
+
if (!/^[.~@]/.test(specifier)) {
|
|
540
|
+
if (specifier.startsWith("@/")) {
|
|
541
|
+
return resolveCandidate(import_node_path5.default.join(root, specifier.slice(2)));
|
|
542
|
+
}
|
|
543
|
+
if (specifier.startsWith("@")) {
|
|
544
|
+
return null;
|
|
545
|
+
}
|
|
546
|
+
if (specifier.startsWith("~")) {
|
|
547
|
+
return resolveCandidate(import_node_path5.default.join(root, specifier.slice(1)));
|
|
548
|
+
}
|
|
549
|
+
return null;
|
|
550
|
+
}
|
|
551
|
+
const base = import_node_path5.default.dirname(import_node_path5.default.resolve(importer));
|
|
552
|
+
return resolveCandidate(import_node_path5.default.resolve(base, specifier));
|
|
553
|
+
}
|
|
554
|
+
function resolveCandidate(p) {
|
|
555
|
+
const candidates = [
|
|
556
|
+
p,
|
|
557
|
+
`${p}.ts`,
|
|
558
|
+
`${p}.tsx`,
|
|
559
|
+
`${p}.js`,
|
|
560
|
+
`${p}.jsx`,
|
|
561
|
+
`${p}.mjs`,
|
|
562
|
+
`${p}.cjs`,
|
|
563
|
+
`${p}.py`,
|
|
564
|
+
`${p}.go`,
|
|
565
|
+
`${p}.rs`,
|
|
566
|
+
`${p}.dart`,
|
|
567
|
+
`${p}.swift`,
|
|
568
|
+
`${p}.java`,
|
|
569
|
+
`${p}.kt`,
|
|
570
|
+
`${p}.c`,
|
|
571
|
+
`${p}.cpp`,
|
|
572
|
+
`${p}.h`,
|
|
573
|
+
`${p}.hpp`,
|
|
574
|
+
`${p}.php`,
|
|
575
|
+
import_node_path5.default.join(p, "index.ts"),
|
|
576
|
+
import_node_path5.default.join(p, "index.js"),
|
|
577
|
+
import_node_path5.default.join(p, "index.tsx"),
|
|
578
|
+
import_node_path5.default.join(p, "index.jsx"),
|
|
579
|
+
import_node_path5.default.join(p, "index.py")
|
|
580
|
+
];
|
|
581
|
+
for (const c of candidates) {
|
|
582
|
+
if (import_node_fs3.default.existsSync(c) && import_node_fs3.default.statSync(c).isFile()) return import_node_path5.default.resolve(c);
|
|
583
|
+
}
|
|
584
|
+
return null;
|
|
585
|
+
}
|
|
586
|
+
function createWatcher(opts) {
|
|
587
|
+
const cache = new ContextCache();
|
|
588
|
+
const { root, ignored = DEFAULT_IGNORED, onIndexed } = opts;
|
|
589
|
+
const pending = /* @__PURE__ */ new Map();
|
|
590
|
+
const DEBOUNCE_MS = 100;
|
|
591
|
+
async function handle(filePath) {
|
|
592
|
+
const abs = import_node_path5.default.resolve(filePath);
|
|
593
|
+
let source;
|
|
594
|
+
try {
|
|
595
|
+
const st = import_node_fs3.default.statSync(abs);
|
|
596
|
+
if (!st.isFile()) return;
|
|
597
|
+
source = import_node_fs3.default.readFileSync(abs, "utf8");
|
|
598
|
+
} catch {
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
const hash = hashOf(source);
|
|
602
|
+
const cached = cache.entries.get(abs);
|
|
603
|
+
if (cached && cached.hash === hash) return;
|
|
604
|
+
await cache.ensureInit();
|
|
605
|
+
const { code, language } = await prune(abs, source);
|
|
606
|
+
const imports = await extractImports(abs, source);
|
|
607
|
+
const entry = {
|
|
608
|
+
hash,
|
|
609
|
+
skeleton: code,
|
|
610
|
+
language,
|
|
611
|
+
imports: imports.map((spec) => resolveImport(abs, spec, root)).filter((p) => p !== null)
|
|
612
|
+
};
|
|
613
|
+
cache.entries.set(abs, entry);
|
|
614
|
+
onIndexed?.(abs, entry);
|
|
615
|
+
}
|
|
616
|
+
const watcher = import_chokidar.default.watch(root, {
|
|
617
|
+
// Combine path matchers with a stat check so non-regular files (e.g. unix
|
|
618
|
+
// sockets) are never opened with fs.watch (which raises UVException).
|
|
619
|
+
ignored(_p, stats) {
|
|
620
|
+
if (stats && !stats.isFile() && !stats.isDirectory()) return true;
|
|
621
|
+
return isIgnored(import_node_path5.default.resolve(String(_p)), ignored);
|
|
622
|
+
},
|
|
623
|
+
alwaysStat: true,
|
|
624
|
+
ignoreInitial: true,
|
|
625
|
+
persistent: true,
|
|
626
|
+
awaitWriteFinish: { stabilityThreshold: 50, pollInterval: 10 }
|
|
627
|
+
});
|
|
628
|
+
function debounce(filePath) {
|
|
629
|
+
const abs = import_node_path5.default.resolve(filePath);
|
|
630
|
+
const existing = pending.get(abs);
|
|
631
|
+
if (existing) clearTimeout(existing);
|
|
632
|
+
if (!existing) void handle(abs);
|
|
633
|
+
pending.set(
|
|
634
|
+
abs,
|
|
635
|
+
setTimeout(() => pending.delete(abs), DEBOUNCE_MS)
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
watcher.on("add", debounce);
|
|
639
|
+
watcher.on("change", debounce);
|
|
640
|
+
watcher.on("unlink", (filePath) => {
|
|
641
|
+
const abs = import_node_path5.default.resolve(filePath);
|
|
642
|
+
cache.entries.delete(abs);
|
|
643
|
+
});
|
|
644
|
+
return {
|
|
645
|
+
cache,
|
|
646
|
+
watcher,
|
|
647
|
+
/** Index an existing file right now (bypasses the watcher). */
|
|
648
|
+
index: handle,
|
|
649
|
+
/**
|
|
650
|
+
* Index the whole tree once (cold start). Returns the number of files
|
|
651
|
+
* successfully indexed.
|
|
652
|
+
*/
|
|
653
|
+
async indexAll() {
|
|
654
|
+
const files = [];
|
|
655
|
+
await walk(root, (f) => files.push(f), ignored);
|
|
656
|
+
let ok = 0;
|
|
657
|
+
for (const f of files) {
|
|
658
|
+
try {
|
|
659
|
+
await handle(f);
|
|
660
|
+
ok++;
|
|
661
|
+
} catch {
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return ok;
|
|
665
|
+
},
|
|
666
|
+
close: () => watcher.close()
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
async function walk(root, push, ignored) {
|
|
670
|
+
const entries = await import_node_fs3.default.promises.readdir(root, { withFileTypes: true });
|
|
671
|
+
for (const e of entries) {
|
|
672
|
+
const abs = import_node_path5.default.join(root, e.name);
|
|
673
|
+
if (isIgnored(abs, ignored)) continue;
|
|
674
|
+
if (e.isDirectory()) {
|
|
675
|
+
await walk(abs, push, ignored);
|
|
676
|
+
} else if (e.isFile()) {
|
|
677
|
+
push(abs);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
function isIgnored(abs, ignored) {
|
|
682
|
+
for (const m of ignored) {
|
|
683
|
+
if (typeof m === "string" && abs.includes(m)) return true;
|
|
684
|
+
if (m instanceof RegExp && m.test(abs)) return true;
|
|
685
|
+
}
|
|
686
|
+
return false;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// src/mcp.ts
|
|
690
|
+
var RULE_TARGET_PATH = {
|
|
691
|
+
cursor: ".cursor/rules/token-shrink.mdc",
|
|
692
|
+
claude: ".claude/rules/token-shrink.md"
|
|
693
|
+
};
|
|
694
|
+
var AUTO_RULE_SENTINEL = "# auto-generated by token-shrink";
|
|
695
|
+
var CURSOR_RULE_PATH = RULE_TARGET_PATH.cursor;
|
|
696
|
+
var AUTO_RULE_PATH = CURSOR_RULE_PATH;
|
|
697
|
+
var CLAUDE_RULE_PATH = RULE_TARGET_PATH.claude;
|
|
698
|
+
var CLAUDE_RULE_SENTINEL = "# auto-generated by token-shrink (claude)";
|
|
699
|
+
var ruleTargets = {
|
|
700
|
+
cursor: {
|
|
701
|
+
relPath: CURSOR_RULE_PATH,
|
|
702
|
+
sentinel: AUTO_RULE_SENTINEL,
|
|
703
|
+
body: `---
|
|
704
|
+
description: Compress dependency context with token-shrink on every task
|
|
705
|
+
globs: **/*.{ts,tsx,js,jsx,py,go,rs,dart,swift,java,kt,c,cpp,h,hpp,php}
|
|
706
|
+
alwaysApply: true
|
|
707
|
+
---
|
|
708
|
+
Before working on a file in this repo, call the \`get_compressed_code_context\` MCP tool with
|
|
709
|
+
that file's path, and use the returned Ring 0 + Ring 1 payload as the context for that file
|
|
710
|
+
and its direct imports.
|
|
711
|
+
|
|
712
|
+
${AUTO_RULE_SENTINEL}
|
|
713
|
+
`
|
|
714
|
+
},
|
|
715
|
+
claude: {
|
|
716
|
+
relPath: CLAUDE_RULE_PATH,
|
|
717
|
+
sentinel: CLAUDE_RULE_SENTINEL,
|
|
718
|
+
body: `---
|
|
719
|
+
description: Compress dependency context with token-shrink on every task
|
|
720
|
+
paths: ["**/*.{ts,tsx,js,jsx,py,go,rs,dart,swift,java,kt,c,cpp,h,hpp,php}"]
|
|
721
|
+
---
|
|
722
|
+
Before working on a file in this repo, call the \`get_compressed_code_context\` MCP tool with
|
|
723
|
+
that file's path, and use the returned Ring 0 + Ring 1 payload as the context for that file
|
|
724
|
+
and its direct imports.
|
|
725
|
+
|
|
726
|
+
${CLAUDE_RULE_SENTINEL}
|
|
727
|
+
`
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
function createAutoRule(root, target) {
|
|
731
|
+
const spec = ruleTargets[target];
|
|
732
|
+
const rulePath = import_node_path6.default.join(root, spec.relPath);
|
|
733
|
+
try {
|
|
734
|
+
if (import_node_fs4.default.existsSync(rulePath)) {
|
|
735
|
+
const existing = import_node_fs4.default.readFileSync(rulePath, "utf8");
|
|
736
|
+
if (existing.includes(spec.sentinel)) {
|
|
737
|
+
return { created: false, skipped: "exists", filePath: rulePath };
|
|
738
|
+
}
|
|
739
|
+
return { created: false, skipped: "user", filePath: rulePath };
|
|
740
|
+
}
|
|
741
|
+
import_node_fs4.default.mkdirSync(import_node_path6.default.dirname(rulePath), { recursive: true });
|
|
742
|
+
import_node_fs4.default.writeFileSync(rulePath, spec.body, "utf8");
|
|
743
|
+
return { created: true, skipped: "none", filePath: rulePath };
|
|
744
|
+
} catch (err) {
|
|
745
|
+
process.stderr.write(
|
|
746
|
+
`[token-shrink] Failed to write rule for "${target}": ${err.message}
|
|
747
|
+
`
|
|
748
|
+
);
|
|
749
|
+
return { created: false, skipped: "none", filePath: rulePath };
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
function createCursorRule(root) {
|
|
753
|
+
return createAutoRule(root, "cursor");
|
|
754
|
+
}
|
|
755
|
+
function resolveTargets(ruleTarget) {
|
|
756
|
+
if (!ruleTarget) return ["cursor", "claude"];
|
|
757
|
+
const list = Array.isArray(ruleTarget) ? ruleTarget : [ruleTarget];
|
|
758
|
+
if (list.includes("all")) return ["cursor", "claude"];
|
|
759
|
+
return list;
|
|
760
|
+
}
|
|
761
|
+
async function startMcpServer(opts = {}) {
|
|
762
|
+
const root = import_node_path6.default.resolve(opts.root ?? process.env.ROOT ?? process.cwd());
|
|
763
|
+
const log = (msg) => {
|
|
764
|
+
if (!opts.silent) process.stderr.write(`[token-shrink] ${msg}
|
|
765
|
+
`);
|
|
766
|
+
};
|
|
767
|
+
const watcher = createWatcher({ root, ignored: opts.ignored });
|
|
768
|
+
if (!opts.silent) {
|
|
769
|
+
log(`Indexing ${root} in the background\u2026`);
|
|
770
|
+
}
|
|
771
|
+
if (opts.createRule !== false) {
|
|
772
|
+
for (const target of resolveTargets(opts.ruleTarget)) {
|
|
773
|
+
const res = createAutoRule(root, target);
|
|
774
|
+
if (res.created) {
|
|
775
|
+
log(`Wrote ${target} rule to ${res.filePath}`);
|
|
776
|
+
} else if (res.skipped === "user") {
|
|
777
|
+
log(`${target} rule exists (user-authored); leaving it untouched.`);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
void watcher.indexAll().then((n) => log(`Indexed ${n} files.`));
|
|
782
|
+
const server = new import_mcp.McpServer(
|
|
783
|
+
{ name: "token-shrink", version: "2.0.0" },
|
|
784
|
+
{ capabilities: { tools: {} } }
|
|
785
|
+
);
|
|
786
|
+
server.registerTool(
|
|
787
|
+
"get_compressed_code_context",
|
|
788
|
+
{
|
|
789
|
+
title: "Get Compressed Code Context",
|
|
790
|
+
description: "Returns a compressed, framework-aware AST context payload for a file: the active file\u2019s full source (Ring 0) plus pruned skeletons of its direct imports (Ring 1). Implementation bodies are removed but type signatures, interfaces, and module exports are preserved for ~80-90% token reduction.",
|
|
791
|
+
inputSchema: {
|
|
792
|
+
activeFilePath: import_zod.z.string().describe("Path to the file the agent is working on"),
|
|
793
|
+
maxSkeletons: import_zod.z.number().int().min(1).max(200).optional().describe("Cap on number of dependency skeletons to include"),
|
|
794
|
+
includeStats: import_zod.z.boolean().optional().describe("Append approximate token-count stats")
|
|
795
|
+
}
|
|
796
|
+
},
|
|
797
|
+
async ({ activeFilePath, maxSkeletons, includeStats }) => {
|
|
798
|
+
const result = assemble(activeFilePath, watcher.cache.entries, {
|
|
799
|
+
maxSkeletons,
|
|
800
|
+
includeStats
|
|
801
|
+
});
|
|
802
|
+
return {
|
|
803
|
+
content: [
|
|
804
|
+
{
|
|
805
|
+
type: "text",
|
|
806
|
+
text: result.markdown
|
|
807
|
+
},
|
|
808
|
+
{
|
|
809
|
+
type: "text",
|
|
810
|
+
text: `[stats] active=${result.activeFilePath} dependencies=${result.included.length} unresolved=${result.unresolved.length}`
|
|
811
|
+
}
|
|
812
|
+
]
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
);
|
|
816
|
+
const transport = new import_stdio.StdioServerTransport();
|
|
817
|
+
await server.connect(transport);
|
|
818
|
+
log("MCP server connected.");
|
|
819
|
+
return server;
|
|
820
|
+
}
|
|
821
|
+
var argv1 = process.argv[1] ? import_node_path6.default.basename(process.argv[1]) : "";
|
|
822
|
+
if (argv1 === "mcp.js" || argv1 === "mcp.mjs" || argv1 === "mcp.cjs" || argv1 === "mcp.ts") {
|
|
823
|
+
const rootArg = (() => {
|
|
824
|
+
const i = process.argv.indexOf("--root");
|
|
825
|
+
if (i !== -1 && process.argv[i + 1]) return process.argv[i + 1];
|
|
826
|
+
const eq = process.argv.find((a) => a.startsWith("--root="));
|
|
827
|
+
if (eq) return eq.slice("--root=".length);
|
|
828
|
+
return void 0;
|
|
829
|
+
})();
|
|
830
|
+
const createRule = !(process.env.TOKEN_SHRINK_CREATE_RULE === "0" || process.env.TOKEN_SHRINK_CREATE_RULE === "false" || process.env.CONTEXT_SHRINK_CREATE_RULE === "0" || process.env.CONTEXT_SHRINK_CREATE_RULE === "false" || process.argv.includes("--no-create-rule") || (() => {
|
|
831
|
+
const f = process.argv.find((a) => a.startsWith("--create-rule="));
|
|
832
|
+
return f ? f.slice("--create-rule=".length) === "false" : false;
|
|
833
|
+
})());
|
|
834
|
+
const rawTargets = process.argv.filter((a) => a.startsWith("--rule-target=")).flatMap((a) => a.slice("--rule-target=".length).split(","));
|
|
835
|
+
const ruleTarget = rawTargets.length > 0 && !rawTargets.includes("all") ? [...new Set(rawTargets)].filter(
|
|
836
|
+
(v) => v === "cursor" || v === "claude"
|
|
837
|
+
) : void 0;
|
|
838
|
+
void startMcpServer({ root: rootArg, createRule, ruleTarget }).catch((err) => {
|
|
839
|
+
process.stderr.write(`[token-shrink] MCP server error: ${err.message}
|
|
840
|
+
`);
|
|
841
|
+
process.exitCode = 1;
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
845
|
+
0 && (module.exports = {
|
|
846
|
+
AUTO_RULE_PATH,
|
|
847
|
+
AUTO_RULE_SENTINEL,
|
|
848
|
+
CLAUDE_RULE_PATH,
|
|
849
|
+
CLAUDE_RULE_SENTINEL,
|
|
850
|
+
CURSOR_RULE_PATH,
|
|
851
|
+
RULE_TARGET_PATH,
|
|
852
|
+
createAutoRule,
|
|
853
|
+
createCursorRule,
|
|
854
|
+
startMcpServer
|
|
855
|
+
});
|
|
856
|
+
//# sourceMappingURL=mcp.cjs.map
|