swarmdo 1.9.0 → 1.11.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/.claude/helpers/statusline.cjs +1 -1
- package/README.md +4 -3
- package/package.json +1 -1
- package/v3/@swarmdo/cli/dist/src/codegraph/codegraph.d.ts +34 -0
- package/v3/@swarmdo/cli/dist/src/codegraph/codegraph.js +98 -2
- package/v3/@swarmdo/cli/dist/src/commands/codegraph.js +85 -11
- package/v3/@swarmdo/cli/dist/src/commands/index.js +6 -3
- package/v3/@swarmdo/cli/dist/src/commands/pack.d.ts +18 -0
- package/v3/@swarmdo/cli/dist/src/commands/pack.js +184 -0
- package/v3/@swarmdo/cli/dist/src/mcp-tools/codegraph-tools.js +56 -1
- package/v3/@swarmdo/cli/dist/src/pack/pack.d.ts +59 -0
- package/v3/@swarmdo/cli/dist/src/pack/pack.js +178 -0
- package/v3/@swarmdo/cli/package.json +1 -1
|
@@ -515,7 +515,7 @@ function getCostFromStdin() {
|
|
|
515
515
|
// misses, the displayed version is meaningful (matches what the user
|
|
516
516
|
// installed), not a stale hard-coded string.
|
|
517
517
|
function getPkgVersion() {
|
|
518
|
-
let ver = '1.
|
|
518
|
+
let ver = '1.11.0';
|
|
519
519
|
try {
|
|
520
520
|
const home = os.homedir();
|
|
521
521
|
const pkgPaths = [
|
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
|
|
3
|
-
[](https://swarmdo.com)
|
|
4
4
|
|
|
5
|
-
[](https://img.shields.io/badge/npx%20swarmdo-v1.11.0-cb3837?style=for-the-badge&logo=npm&logoColor=white)](https://github.com/SwarmDo/swarmdo/releases)
|
|
6
6
|
[](https://github.com/SwarmDo/swarmdo/blob/main/LICENSE)
|
|
7
7
|
[](https://swarmdo.com)
|
|
8
8
|
[](https://github.com/SwarmDo/swarmdo)
|
|
@@ -277,8 +277,9 @@ The recent release train added a full day-to-day operations layer around the swa
|
|
|
277
277
|
| `swarmdo worktree` (alias `wt`) | **Parallel-agent isolation** on git worktrees — add / list / diff / merge / remove |
|
|
278
278
|
| `swarmdo transcript` (alias `tx`) | **Export any Claude Code session** to clean markdown — system noise stripped, ready to share; `transcript search <query>` full-text-searches every session |
|
|
279
279
|
| `swarmdo compact` | **Compress noisy command output** before it reaches an LLM — strip ANSI, collapse repeats, fold `node_modules` stack frames, window long logs. `npm test 2>&1 \| swarmdo compact` or `swarmdo compact -- npm test` (exit code propagates). Deterministic, zero tokens |
|
|
280
|
-
| `swarmdo codegraph` (alias `cg`) | **Queryable symbol index** — `codegraph index` scans TS/JS for exported symbols; `query <name>` (with `--fuzzy`/`--kind`) and `file <path>` answer "where is X defined / what does this file export" from `.swarm/codegraph.json` instead of grep+read round-trips. 1,
|
|
280
|
+
| `swarmdo codegraph` (alias `cg`) | **Queryable symbol index** — `codegraph index` scans TS/JS for exported symbols; `query <name>` (with `--fuzzy`/`--kind`) and `file <path>` answer "where is X defined / what does this file export" from `.swarm/codegraph.json` instead of grep+read round-trips. `codegraph importers <file>` shows reverse deps ("what breaks if I change this"); `codegraph imports <file>` shows a file's dependencies. 1,786 symbols + import graph across 296 files in <1s. Also MCP tools (`codegraph_query`/`file`/`imports`/`importers`/`index`/`stats`) so agents query the graph in-session |
|
|
281
281
|
| `swarmdo redact` | **Mask secrets before they reach an LLM/log/memory** — detect API keys, tokens, and private keys (gitleaks-style rule catalog + Shannon-entropy fallback) and redact them. Stdin filter (`cat deploy.log \| swarmdo redact`), command-wrap (`swarmdo redact -- npm run deploy`), or `--scan` to fail CI on any secret. Also MCP tools (`redact_text`/`redact_scan`). Deterministic, zero tokens |
|
|
282
|
+
| `swarmdo pack` | **Bundle a repo into one AI-context blob** — walk the tree (respects `.gitignore` + glob `--include`/`--exclude`, skips binaries/node_modules), emit markdown/xml/json/plain with a directory tree and per-file + total token estimates. `swarmdo pack --tokens` for a budget breakdown; `--redact` masks secrets first. Deterministic |
|
|
282
283
|
| `swarmdo integrations` (alias `integrate`) | **Use swarmdo from Codex CLI, GitHub Copilot CLI, and pi** — one command wires AGENTS.md + each CLI's MCP config (idempotent, dry-run first, never touches your Claude Code setup) |
|
|
283
284
|
| OpenRouter model pool | **Let swarms pick from any models you configure** — declare tier-mapped OpenRouter models in `swarmdo.config.json`; the router Thompson-samples among them per task and the execution layer dispatches the winner |
|
|
284
285
|
| `swarmdo changelog` (alias `notes`) | **Release notes from conventional commits** — `--out NOTES.md` feeds `gh release create --notes-file` |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swarmdo",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Swarmdo - Enterprise AI agent orchestration for Claude Code. Deploy 60+ specialized agents in coordinated swarms with self-learning, fault-tolerant consensus, vector memory, and MCP integration",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -21,13 +21,39 @@ export interface CodeSymbol {
|
|
|
21
21
|
/** The trimmed declaration line (signature), truncated for storage. */
|
|
22
22
|
signature: string;
|
|
23
23
|
}
|
|
24
|
+
/** A dependency edge: `from` imports module `spec`, resolved to a repo file when relative. */
|
|
25
|
+
export interface ImportEdge {
|
|
26
|
+
/** repo-relative file containing the import */
|
|
27
|
+
from: string;
|
|
28
|
+
/** the raw module specifier as written, e.g. './store.js' or 'node:fs' */
|
|
29
|
+
spec: string;
|
|
30
|
+
/** repo-relative file the spec resolves to, or null for external/unresolved */
|
|
31
|
+
resolved: string | null;
|
|
32
|
+
/** 1-based line of the import */
|
|
33
|
+
line: number;
|
|
34
|
+
}
|
|
24
35
|
export interface CodeIndex {
|
|
25
36
|
/** Repo-relative file → symbols declared in it. */
|
|
26
37
|
symbols: CodeSymbol[];
|
|
38
|
+
/** Dependency edges between files (relative imports resolved; externals kept unresolved). */
|
|
39
|
+
imports: ImportEdge[];
|
|
27
40
|
fileCount: number;
|
|
28
41
|
}
|
|
29
42
|
/** Extract exported symbols from one source file. Pure. */
|
|
30
43
|
export declare function extractSymbols(source: string, file: string): CodeSymbol[];
|
|
44
|
+
/** Extract the raw import specifiers from one source file. Pure. */
|
|
45
|
+
export declare function extractImports(source: string, file: string): Array<{
|
|
46
|
+
from: string;
|
|
47
|
+
spec: string;
|
|
48
|
+
line: number;
|
|
49
|
+
}>;
|
|
50
|
+
/**
|
|
51
|
+
* Resolve a spec to a repo-relative file in `fileSet`, or null. Only relative
|
|
52
|
+
* specs (`.`/`..`) resolve; bare specifiers (packages, aliases, `node:`) are
|
|
53
|
+
* external → null. Tries the path as-is, common extensions, and `/index.*`,
|
|
54
|
+
* and rewrites a `.js` spec to its `.ts` sibling (TS ESM convention).
|
|
55
|
+
*/
|
|
56
|
+
export declare function resolveImport(fromFile: string, spec: string, fileSet: Set<string>): string | null;
|
|
31
57
|
/** Build an index from already-read files. Pure. */
|
|
32
58
|
export declare function buildIndex(files: Array<{
|
|
33
59
|
file: string;
|
|
@@ -43,10 +69,18 @@ export interface QueryOptions {
|
|
|
43
69
|
export declare function queryIndex(index: CodeIndex, name: string, opts?: QueryOptions): CodeSymbol[];
|
|
44
70
|
/** Symbols declared in a specific file (exact repo-relative path). Pure. */
|
|
45
71
|
export declare function symbolsInFile(index: CodeIndex, file: string): CodeSymbol[];
|
|
72
|
+
/** Edges FROM a file — what it imports (resolved repo files + external specs). Pure. */
|
|
73
|
+
export declare function fileImports(index: CodeIndex, file: string): ImportEdge[];
|
|
74
|
+
/** Edges TO a file — who imports it ("what breaks if I change this"). Pure. */
|
|
75
|
+
export declare function fileImporters(index: CodeIndex, file: string): ImportEdge[];
|
|
46
76
|
export interface IndexStats {
|
|
47
77
|
files: number;
|
|
48
78
|
symbols: number;
|
|
49
79
|
byKind: Record<string, number>;
|
|
80
|
+
/** total import edges */
|
|
81
|
+
imports: number;
|
|
82
|
+
/** edges resolved to an in-repo file (vs external packages) */
|
|
83
|
+
internalImports: number;
|
|
50
84
|
}
|
|
51
85
|
/** Summary counts for the index. Pure. */
|
|
52
86
|
export declare function indexStats(index: CodeIndex): IndexStats;
|
|
@@ -57,15 +57,96 @@ export function extractSymbols(source, file) {
|
|
|
57
57
|
}
|
|
58
58
|
return out;
|
|
59
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Module specifiers on a line, from any of: `import … from 'x'`,
|
|
62
|
+
* `export … from 'x'`, side-effect `import 'x'`, `require('x')`, and dynamic
|
|
63
|
+
* `import('x')`. Line-based like the symbol matchers; captures every spec on
|
|
64
|
+
* the line (dynamic imports can be mid-line).
|
|
65
|
+
*/
|
|
66
|
+
const IMPORT_RES = [
|
|
67
|
+
/\bfrom\s*['"]([^'"]+)['"]/g, // import/export … from '…'
|
|
68
|
+
/\bimport\s*['"]([^'"]+)['"]/g, // side-effect import '…'
|
|
69
|
+
/\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g, // dynamic import('…')
|
|
70
|
+
/\brequire\s*\(\s*['"]([^'"]+)['"]\s*\)/g, // require('…')
|
|
71
|
+
];
|
|
72
|
+
/** Extract the raw import specifiers from one source file. Pure. */
|
|
73
|
+
export function extractImports(source, file) {
|
|
74
|
+
const out = [];
|
|
75
|
+
const lines = source.split('\n');
|
|
76
|
+
for (let i = 0; i < lines.length; i++) {
|
|
77
|
+
const line = lines[i];
|
|
78
|
+
if (!/\b(?:from|import|require)\b/.test(line))
|
|
79
|
+
continue;
|
|
80
|
+
const seen = new Set();
|
|
81
|
+
for (const re of IMPORT_RES) {
|
|
82
|
+
re.lastIndex = 0;
|
|
83
|
+
let m;
|
|
84
|
+
while ((m = re.exec(line)) !== null) {
|
|
85
|
+
const spec = m[1];
|
|
86
|
+
if (seen.has(spec))
|
|
87
|
+
continue; // same spec caught by two patterns on one line
|
|
88
|
+
seen.add(spec);
|
|
89
|
+
out.push({ from: file, spec, line: i + 1 });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
/** Normalize a relative spec against the importing file's dir → repo-relative POSIX path (no extension). */
|
|
96
|
+
function joinRelative(fromFile, spec) {
|
|
97
|
+
const parts = fromFile.includes('/') ? fromFile.slice(0, fromFile.lastIndexOf('/')).split('/') : [];
|
|
98
|
+
for (const seg of spec.split('/')) {
|
|
99
|
+
if (seg === '' || seg === '.')
|
|
100
|
+
continue;
|
|
101
|
+
if (seg === '..')
|
|
102
|
+
parts.pop();
|
|
103
|
+
else
|
|
104
|
+
parts.push(seg);
|
|
105
|
+
}
|
|
106
|
+
return parts.join('/');
|
|
107
|
+
}
|
|
108
|
+
const RESOLVE_EXTS = ['', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs', '.mts', '.cts'];
|
|
109
|
+
/**
|
|
110
|
+
* Resolve a spec to a repo-relative file in `fileSet`, or null. Only relative
|
|
111
|
+
* specs (`.`/`..`) resolve; bare specifiers (packages, aliases, `node:`) are
|
|
112
|
+
* external → null. Tries the path as-is, common extensions, and `/index.*`,
|
|
113
|
+
* and rewrites a `.js` spec to its `.ts` sibling (TS ESM convention).
|
|
114
|
+
*/
|
|
115
|
+
export function resolveImport(fromFile, spec, fileSet) {
|
|
116
|
+
if (!spec.startsWith('.'))
|
|
117
|
+
return null;
|
|
118
|
+
const base = joinRelative(fromFile, spec);
|
|
119
|
+
const bases = [base];
|
|
120
|
+
const jsExt = base.match(/\.(js|jsx|mjs|cjs)$/);
|
|
121
|
+
if (jsExt)
|
|
122
|
+
bases.push(base.slice(0, -jsExt[0].length)); // ./x.js → try ./x(.ts…)
|
|
123
|
+
for (const b of bases) {
|
|
124
|
+
for (const ext of RESOLVE_EXTS) {
|
|
125
|
+
if (fileSet.has(b + ext))
|
|
126
|
+
return b + ext;
|
|
127
|
+
}
|
|
128
|
+
for (const ext of RESOLVE_EXTS.slice(1)) {
|
|
129
|
+
if (fileSet.has(b + '/index' + ext))
|
|
130
|
+
return b + '/index' + ext;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
60
135
|
/** Build an index from already-read files. Pure. */
|
|
61
136
|
export function buildIndex(files) {
|
|
62
137
|
const symbols = [];
|
|
138
|
+
const fileSet = new Set(files.map((f) => f.file));
|
|
139
|
+
const imports = [];
|
|
63
140
|
for (const { file, source } of files) {
|
|
64
141
|
symbols.push(...extractSymbols(source, file));
|
|
142
|
+
for (const imp of extractImports(source, file)) {
|
|
143
|
+
imports.push({ ...imp, resolved: resolveImport(imp.from, imp.spec, fileSet) });
|
|
144
|
+
}
|
|
65
145
|
}
|
|
66
146
|
// Stable order: file, then line.
|
|
67
147
|
symbols.sort((a, b) => (a.file < b.file ? -1 : a.file > b.file ? 1 : a.line - b.line));
|
|
68
|
-
|
|
148
|
+
imports.sort((a, b) => (a.from < b.from ? -1 : a.from > b.from ? 1 : a.line - b.line));
|
|
149
|
+
return { symbols, imports, fileCount: files.length };
|
|
69
150
|
}
|
|
70
151
|
/** Look up symbols by name. Pure. */
|
|
71
152
|
export function queryIndex(index, name, opts = {}) {
|
|
@@ -80,11 +161,26 @@ export function queryIndex(index, name, opts = {}) {
|
|
|
80
161
|
export function symbolsInFile(index, file) {
|
|
81
162
|
return index.symbols.filter((s) => s.file === file);
|
|
82
163
|
}
|
|
164
|
+
/** Edges FROM a file — what it imports (resolved repo files + external specs). Pure. */
|
|
165
|
+
export function fileImports(index, file) {
|
|
166
|
+
return (index.imports ?? []).filter((e) => e.from === file);
|
|
167
|
+
}
|
|
168
|
+
/** Edges TO a file — who imports it ("what breaks if I change this"). Pure. */
|
|
169
|
+
export function fileImporters(index, file) {
|
|
170
|
+
return (index.imports ?? []).filter((e) => e.resolved === file);
|
|
171
|
+
}
|
|
83
172
|
/** Summary counts for the index. Pure. */
|
|
84
173
|
export function indexStats(index) {
|
|
85
174
|
const byKind = {};
|
|
86
175
|
for (const s of index.symbols)
|
|
87
176
|
byKind[s.kind] = (byKind[s.kind] ?? 0) + 1;
|
|
88
|
-
|
|
177
|
+
const imports = index.imports ?? [];
|
|
178
|
+
return {
|
|
179
|
+
files: index.fileCount,
|
|
180
|
+
symbols: index.symbols.length,
|
|
181
|
+
byKind,
|
|
182
|
+
imports: imports.length,
|
|
183
|
+
internalImports: imports.filter((e) => e.resolved !== null).length,
|
|
184
|
+
};
|
|
89
185
|
}
|
|
90
186
|
//# sourceMappingURL=codegraph.js.map
|
|
@@ -13,11 +13,15 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import * as path from 'node:path';
|
|
15
15
|
import { output } from '../output.js';
|
|
16
|
-
import { queryIndex, symbolsInFile, indexStats, } from '../codegraph/codegraph.js';
|
|
16
|
+
import { queryIndex, symbolsInFile, indexStats, fileImports, fileImporters, } from '../codegraph/codegraph.js';
|
|
17
17
|
import { INDEX_REL, scanRepo, saveIndex, loadIndex } from '../codegraph/store.js';
|
|
18
18
|
function fmtSymbol(s) {
|
|
19
19
|
return `${s.file}:${s.line} ${output.dim(`[${s.kind}]`)} ${s.signature}`;
|
|
20
20
|
}
|
|
21
|
+
/** Accept an absolute or cwd-relative path; return the repo-relative key. */
|
|
22
|
+
function relKey(root, arg) {
|
|
23
|
+
return path.relative(root, path.resolve(root, arg)).split(path.sep).join('/');
|
|
24
|
+
}
|
|
21
25
|
const indexCommand = {
|
|
22
26
|
name: 'index',
|
|
23
27
|
description: 'Scan the tree for exported symbols and persist the index',
|
|
@@ -124,32 +128,102 @@ const statsCommand = {
|
|
|
124
128
|
output.printJson(stats);
|
|
125
129
|
return { success: true, data: stats };
|
|
126
130
|
}
|
|
127
|
-
output.writeln(output.bold(`codegraph: ${stats.symbols} symbols, ${stats.files} files`));
|
|
131
|
+
output.writeln(output.bold(`codegraph: ${stats.symbols} symbols, ${stats.files} files, ${stats.imports} imports (${stats.internalImports} internal)`));
|
|
128
132
|
output.printList(Object.entries(stats.byKind).sort((a, b) => b[1] - a[1]).map(([k, n]) => `${k}: ${n}`));
|
|
129
133
|
return { success: true, exitCode: 0 };
|
|
130
134
|
},
|
|
131
135
|
};
|
|
136
|
+
const importsCommand = {
|
|
137
|
+
name: 'imports',
|
|
138
|
+
description: 'What a file imports — resolved repo files + external packages',
|
|
139
|
+
options: [
|
|
140
|
+
{ name: 'internal', description: 'only edges resolved to a repo file (hide external packages)', type: 'boolean', default: false },
|
|
141
|
+
{ name: 'json', description: 'machine-readable output', type: 'boolean', default: false },
|
|
142
|
+
],
|
|
143
|
+
action: async (ctx) => {
|
|
144
|
+
const root = ctx.cwd || process.cwd();
|
|
145
|
+
const arg = ctx.args[0];
|
|
146
|
+
if (!arg) {
|
|
147
|
+
output.printError('Usage: swarmdo codegraph imports <path>');
|
|
148
|
+
return { success: false, exitCode: 1 };
|
|
149
|
+
}
|
|
150
|
+
const index = loadIndex(root);
|
|
151
|
+
if (!index) {
|
|
152
|
+
output.printError(`no index — run \`swarmdo codegraph index\` first`);
|
|
153
|
+
return { success: false, exitCode: 1 };
|
|
154
|
+
}
|
|
155
|
+
let edges = fileImports(index, relKey(root, arg));
|
|
156
|
+
if (ctx.flags.internal === true)
|
|
157
|
+
edges = edges.filter((e) => e.resolved !== null);
|
|
158
|
+
if (ctx.flags.json === true) {
|
|
159
|
+
output.printJson(edges);
|
|
160
|
+
return { success: true, data: edges };
|
|
161
|
+
}
|
|
162
|
+
if (edges.length === 0) {
|
|
163
|
+
output.writeln(output.dim(`no imports in ${relKey(root, arg)} (indexed?)`));
|
|
164
|
+
return { success: true, exitCode: 0 };
|
|
165
|
+
}
|
|
166
|
+
for (const e of edges) {
|
|
167
|
+
output.writeln(` ${e.line} ${e.spec}${e.resolved ? output.dim(` → ${e.resolved}`) : output.dim(' (external)')}`);
|
|
168
|
+
}
|
|
169
|
+
return { success: true, exitCode: 0 };
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
const importersCommand = {
|
|
173
|
+
name: 'importers',
|
|
174
|
+
aliases: ['rdeps'],
|
|
175
|
+
description: 'Who imports a file — reverse deps ("what breaks if I change this")',
|
|
176
|
+
options: [{ name: 'json', description: 'machine-readable output', type: 'boolean', default: false }],
|
|
177
|
+
action: async (ctx) => {
|
|
178
|
+
const root = ctx.cwd || process.cwd();
|
|
179
|
+
const arg = ctx.args[0];
|
|
180
|
+
if (!arg) {
|
|
181
|
+
output.printError('Usage: swarmdo codegraph importers <path>');
|
|
182
|
+
return { success: false, exitCode: 1 };
|
|
183
|
+
}
|
|
184
|
+
const index = loadIndex(root);
|
|
185
|
+
if (!index) {
|
|
186
|
+
output.printError(`no index — run \`swarmdo codegraph index\` first`);
|
|
187
|
+
return { success: false, exitCode: 1 };
|
|
188
|
+
}
|
|
189
|
+
const edges = fileImporters(index, relKey(root, arg));
|
|
190
|
+
if (ctx.flags.json === true) {
|
|
191
|
+
output.printJson(edges);
|
|
192
|
+
return { success: true, data: edges };
|
|
193
|
+
}
|
|
194
|
+
if (edges.length === 0) {
|
|
195
|
+
output.writeln(output.dim(`nothing imports ${relKey(root, arg)} (leaf, entrypoint, or not indexed)`));
|
|
196
|
+
return { success: true, exitCode: 0 };
|
|
197
|
+
}
|
|
198
|
+
for (const e of edges)
|
|
199
|
+
output.writeln(` ${e.from}:${e.line} ${output.dim(e.spec)}`);
|
|
200
|
+
return { success: true, exitCode: 0 };
|
|
201
|
+
},
|
|
202
|
+
};
|
|
132
203
|
export const codegraphCommand = {
|
|
133
204
|
name: 'codegraph',
|
|
134
205
|
aliases: ['cg'],
|
|
135
|
-
description: 'Queryable index of exported symbols — where things are defined, without grep+read',
|
|
136
|
-
subcommands: [indexCommand, queryCommand, fileCommand, statsCommand],
|
|
206
|
+
description: 'Queryable index of exported symbols + import graph — where things are defined and what depends on what, without grep+read',
|
|
207
|
+
subcommands: [indexCommand, queryCommand, fileCommand, importsCommand, importersCommand, statsCommand],
|
|
137
208
|
options: [],
|
|
138
209
|
examples: [
|
|
139
|
-
{ command: 'swarmdo codegraph index', description: 'Scan the repo and persist the symbol index' },
|
|
210
|
+
{ command: 'swarmdo codegraph index', description: 'Scan the repo and persist the symbol + import index' },
|
|
140
211
|
{ command: 'swarmdo codegraph query buildIndex', description: 'Find where a symbol is defined' },
|
|
141
212
|
{ command: 'swarmdo codegraph query Handler --fuzzy --kind type', description: 'Fuzzy, kind-filtered lookup' },
|
|
142
213
|
{ command: 'swarmdo codegraph file src/index.ts', description: 'What a file exports' },
|
|
214
|
+
{ command: 'swarmdo codegraph importers src/codegraph/store.ts', description: 'What breaks if I change this file' },
|
|
143
215
|
],
|
|
144
216
|
action: async () => {
|
|
145
|
-
output.writeln(output.bold('swarmdo codegraph — exported-symbol index'));
|
|
217
|
+
output.writeln(output.bold('swarmdo codegraph — exported-symbol index + import graph'));
|
|
146
218
|
output.printList([
|
|
147
|
-
'index [path]
|
|
148
|
-
'query <name>
|
|
149
|
-
'file <path>
|
|
150
|
-
'
|
|
219
|
+
'index [path] scan TS/JS and persist to .swarm/codegraph.json',
|
|
220
|
+
'query <name> find a symbol (--fuzzy, --kind)',
|
|
221
|
+
'file <path> symbols a file exports',
|
|
222
|
+
'imports <path> what a file imports (--internal)',
|
|
223
|
+
'importers <path> who imports a file (reverse deps)',
|
|
224
|
+
'stats index summary',
|
|
151
225
|
]);
|
|
152
|
-
output.writeln(output.dim('MVP: exported top-level symbols
|
|
226
|
+
output.writeln(output.dim('MVP: exported top-level symbols + relative-import edges. Incremental watch is a follow-up.'));
|
|
153
227
|
return { success: true, exitCode: 0 };
|
|
154
228
|
},
|
|
155
229
|
};
|
|
@@ -25,6 +25,9 @@ const commandLoaders = {
|
|
|
25
25
|
// Secret detection + redaction on the agent data path (gitleaks/trufflehog
|
|
26
26
|
// demand) — mask API keys/tokens before they reach an LLM/log/memory.
|
|
27
27
|
redact: () => import('./redact.js'),
|
|
28
|
+
// Repo→AI-context bundle (repomix demand) — one promptable blob with tree +
|
|
29
|
+
// token counts; swarmdo's context-assembly primitive.
|
|
30
|
+
pack: () => import('./pack.js'),
|
|
28
31
|
// Queryable exported-symbol index (codegraph demand) — where things are
|
|
29
32
|
// defined without grep+read round-trips.
|
|
30
33
|
codegraph: () => import('./codegraph.js'),
|
|
@@ -258,7 +261,7 @@ export async function getCommandsByCategory() {
|
|
|
258
261
|
// three slots from 'statusline' onward (completionsCmd received the
|
|
259
262
|
// statusline module, analyzeCmd received completions, …), scrambling the
|
|
260
263
|
// categorized help. Every load now has a named slot.
|
|
261
|
-
const [daemonCmd, doctorCmd, embeddingsCmd, neuralCmd, performanceCmd, securityCmd, swarmvectorCmd, hiveMindCmd, configCmd, statuslineCmd, compressCmd, efficiencyCmd, completionsCmd, migrateCmd, workflowCmd, analyzeCmd, routeCmd, progressCmd, providersCmd, pluginsCmd, deploymentCmd, claimsCmd, issuesCmd, updateCmd, processCmd, guidanceCmd, applianceCmd, cleanupCmd, autopilotCmd, demoCmd, usageCmd, repairCmd, hudCmd, compactCmd, codegraphCmd, redactCmd,] = await Promise.all([
|
|
264
|
+
const [daemonCmd, doctorCmd, embeddingsCmd, neuralCmd, performanceCmd, securityCmd, swarmvectorCmd, hiveMindCmd, configCmd, statuslineCmd, compressCmd, efficiencyCmd, completionsCmd, migrateCmd, workflowCmd, analyzeCmd, routeCmd, progressCmd, providersCmd, pluginsCmd, deploymentCmd, claimsCmd, issuesCmd, updateCmd, processCmd, guidanceCmd, applianceCmd, cleanupCmd, autopilotCmd, demoCmd, usageCmd, repairCmd, hudCmd, compactCmd, codegraphCmd, redactCmd, packCmd,] = await Promise.all([
|
|
262
265
|
loadCommand('daemon'), loadCommand('doctor'), loadCommand('embeddings'), loadCommand('neural'),
|
|
263
266
|
loadCommand('performance'), loadCommand('security'), loadCommand('swarmvector'), loadCommand('hive-mind'),
|
|
264
267
|
loadCommand('config'), loadCommand('statusline'), loadCommand('compress'), loadCommand('efficiency'),
|
|
@@ -266,7 +269,7 @@ export async function getCommandsByCategory() {
|
|
|
266
269
|
loadCommand('analyze'), loadCommand('route'), loadCommand('progress'), loadCommand('providers'),
|
|
267
270
|
loadCommand('plugins'), loadCommand('deployment'), loadCommand('claims'), loadCommand('issues'),
|
|
268
271
|
loadCommand('update'), loadCommand('process'), loadCommand('guidance'), loadCommand('appliance'),
|
|
269
|
-
loadCommand('cleanup'), loadCommand('autopilot'), loadCommand('demo'), loadCommand('usage'), loadCommand('repair'), loadCommand('hud'), loadCommand('compact'), loadCommand('codegraph'), loadCommand('redact'),
|
|
272
|
+
loadCommand('cleanup'), loadCommand('autopilot'), loadCommand('demo'), loadCommand('usage'), loadCommand('repair'), loadCommand('hud'), loadCommand('compact'), loadCommand('codegraph'), loadCommand('redact'), loadCommand('pack'),
|
|
270
273
|
]);
|
|
271
274
|
return {
|
|
272
275
|
primary: [
|
|
@@ -282,7 +285,7 @@ export async function getCommandsByCategory() {
|
|
|
282
285
|
utility: [
|
|
283
286
|
configCmd, doctorCmd, daemonCmd, completionsCmd,
|
|
284
287
|
migrateCmd, workflowCmd, demoCmd,
|
|
285
|
-
statuslineCmd, compressCmd, compactCmd, redactCmd, efficiencyCmd,
|
|
288
|
+
statuslineCmd, compressCmd, compactCmd, redactCmd, packCmd, efficiencyCmd,
|
|
286
289
|
].filter(Boolean),
|
|
287
290
|
analysis: [
|
|
288
291
|
analyzeCmd, routeCmd, progressCmd, usageCmd, hudCmd, codegraphCmd,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `swarmdo pack` — bundle a repo (or subset) into one AI-friendly context blob.
|
|
3
|
+
*
|
|
4
|
+
* swarmdo pack # markdown bundle of the repo → stdout
|
|
5
|
+
* swarmdo pack src --format xml -o ctx.xml
|
|
6
|
+
* swarmdo pack --include '*.ts' --exclude '*.test.ts'
|
|
7
|
+
* swarmdo pack --tokens # just per-file + total token counts
|
|
8
|
+
* swarmdo pack --redact # mask secrets in each file first
|
|
9
|
+
*
|
|
10
|
+
* The fs walk lives here (default skips + .gitignore + glob include/exclude +
|
|
11
|
+
* binary/size guards); the pure engine (../pack/pack.ts) does formatting +
|
|
12
|
+
* token accounting. --redact composes the ../redact engine so secrets never
|
|
13
|
+
* land in the bundle.
|
|
14
|
+
*/
|
|
15
|
+
import type { Command } from '../types.js';
|
|
16
|
+
export declare const packCommand: Command;
|
|
17
|
+
export default packCommand;
|
|
18
|
+
//# sourceMappingURL=pack.d.ts.map
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `swarmdo pack` — bundle a repo (or subset) into one AI-friendly context blob.
|
|
3
|
+
*
|
|
4
|
+
* swarmdo pack # markdown bundle of the repo → stdout
|
|
5
|
+
* swarmdo pack src --format xml -o ctx.xml
|
|
6
|
+
* swarmdo pack --include '*.ts' --exclude '*.test.ts'
|
|
7
|
+
* swarmdo pack --tokens # just per-file + total token counts
|
|
8
|
+
* swarmdo pack --redact # mask secrets in each file first
|
|
9
|
+
*
|
|
10
|
+
* The fs walk lives here (default skips + .gitignore + glob include/exclude +
|
|
11
|
+
* binary/size guards); the pure engine (../pack/pack.ts) does formatting +
|
|
12
|
+
* token accounting. --redact composes the ../redact engine so secrets never
|
|
13
|
+
* land in the bundle.
|
|
14
|
+
*/
|
|
15
|
+
import * as fs from 'node:fs';
|
|
16
|
+
import * as path from 'node:path';
|
|
17
|
+
import { output } from '../output.js';
|
|
18
|
+
import { packFiles, makeIgnoreMatcher } from '../pack/pack.js';
|
|
19
|
+
import { redactText } from '../redact/redact.js';
|
|
20
|
+
const SKIP_DIRS = new Set([
|
|
21
|
+
'node_modules', '.git', '.swarm', 'dist', 'dist-standalone', 'build',
|
|
22
|
+
'coverage', '.next', '.turbo', 'out', 'vendor', '.cache',
|
|
23
|
+
]);
|
|
24
|
+
const BINARY_EXT = new Set([
|
|
25
|
+
'png', 'jpg', 'jpeg', 'gif', 'webp', 'ico', 'pdf', 'zip', 'gz', 'tar', 'tgz',
|
|
26
|
+
'woff', 'woff2', 'ttf', 'eot', 'mp4', 'mov', 'mp3', 'wav', 'wasm', 'so', 'dylib',
|
|
27
|
+
'dll', 'exe', 'bin', 'node', 'lock', 'jpeg',
|
|
28
|
+
]);
|
|
29
|
+
/** Looks binary if it has a NUL in the first 8 KiB. */
|
|
30
|
+
function looksBinary(buf) {
|
|
31
|
+
const n = Math.min(buf.length, 8192);
|
|
32
|
+
for (let i = 0; i < n; i++)
|
|
33
|
+
if (buf[i] === 0)
|
|
34
|
+
return true;
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
function walk(o) {
|
|
38
|
+
const files = [];
|
|
39
|
+
const stack = [o.root];
|
|
40
|
+
while (stack.length) {
|
|
41
|
+
const dir = stack.pop();
|
|
42
|
+
let entries;
|
|
43
|
+
try {
|
|
44
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
for (const e of entries) {
|
|
50
|
+
const full = path.join(dir, e.name);
|
|
51
|
+
const rel = path.relative(o.root, full).split(path.sep).join('/');
|
|
52
|
+
if (e.isDirectory()) {
|
|
53
|
+
if (SKIP_DIRS.has(e.name))
|
|
54
|
+
continue;
|
|
55
|
+
if (o.gitignore?.(rel + '/'))
|
|
56
|
+
continue;
|
|
57
|
+
stack.push(full);
|
|
58
|
+
}
|
|
59
|
+
else if (e.isFile()) {
|
|
60
|
+
if (o.gitignore?.(rel))
|
|
61
|
+
continue;
|
|
62
|
+
if (o.exclude?.(rel))
|
|
63
|
+
continue;
|
|
64
|
+
if (o.include && !o.include(rel))
|
|
65
|
+
continue;
|
|
66
|
+
if (BINARY_EXT.has(e.name.slice(e.name.lastIndexOf('.') + 1).toLowerCase()))
|
|
67
|
+
continue;
|
|
68
|
+
let stat;
|
|
69
|
+
try {
|
|
70
|
+
stat = fs.statSync(full);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (stat.size > o.maxBytes)
|
|
76
|
+
continue;
|
|
77
|
+
let buf;
|
|
78
|
+
try {
|
|
79
|
+
buf = fs.readFileSync(full);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (looksBinary(buf))
|
|
85
|
+
continue;
|
|
86
|
+
files.push({ path: rel, content: buf.toString('utf8') });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return files;
|
|
91
|
+
}
|
|
92
|
+
function csv(v) {
|
|
93
|
+
if (typeof v !== 'string' || !v)
|
|
94
|
+
return undefined;
|
|
95
|
+
return v.split(',').map((s) => s.trim()).filter(Boolean);
|
|
96
|
+
}
|
|
97
|
+
async function run(ctx) {
|
|
98
|
+
const repoRoot = ctx.cwd || process.cwd();
|
|
99
|
+
const scanRoot = ctx.args[0] ? path.resolve(repoRoot, ctx.args[0]) : repoRoot;
|
|
100
|
+
// NB: use --style, not --format — `format` is a global flag (text/json/table,
|
|
101
|
+
// default 'text') that would shadow ours (#1425 removed its short alias).
|
|
102
|
+
const format = (typeof ctx.flags.style === 'string' ? ctx.flags.style : 'md');
|
|
103
|
+
if (!['md', 'xml', 'json', 'plain'].includes(format)) {
|
|
104
|
+
output.printError(`unknown --style '${format}' (use md|xml|json|plain)`);
|
|
105
|
+
return { success: false, exitCode: 1 };
|
|
106
|
+
}
|
|
107
|
+
const maxKb = typeof ctx.flags['max-file-size'] === 'string' ? parseInt(ctx.flags['max-file-size'], 10) : 512;
|
|
108
|
+
const maxBytes = (Number.isFinite(maxKb) ? maxKb : 512) * 1024;
|
|
109
|
+
const includePats = csv(ctx.flags.include);
|
|
110
|
+
const excludePats = csv(ctx.flags.exclude);
|
|
111
|
+
let gitignore;
|
|
112
|
+
if (ctx.flags['no-gitignore'] !== true) {
|
|
113
|
+
try {
|
|
114
|
+
const gi = fs.readFileSync(path.join(scanRoot, '.gitignore'), 'utf8').split('\n');
|
|
115
|
+
gitignore = makeIgnoreMatcher(gi);
|
|
116
|
+
}
|
|
117
|
+
catch { /* no .gitignore */ }
|
|
118
|
+
}
|
|
119
|
+
const files = walk({
|
|
120
|
+
root: scanRoot,
|
|
121
|
+
include: includePats ? makeIgnoreMatcher(includePats) : undefined,
|
|
122
|
+
exclude: excludePats ? makeIgnoreMatcher(excludePats) : undefined,
|
|
123
|
+
gitignore,
|
|
124
|
+
maxBytes,
|
|
125
|
+
});
|
|
126
|
+
if (files.length === 0) {
|
|
127
|
+
output.printError('no files matched — check the path, --include/--exclude, or .gitignore');
|
|
128
|
+
return { success: false, exitCode: 1 };
|
|
129
|
+
}
|
|
130
|
+
const opts = { format, tree: ctx.flags['no-tree'] !== true };
|
|
131
|
+
if (ctx.flags.redact === true) {
|
|
132
|
+
opts.transform = (content) => redactText(content).output;
|
|
133
|
+
}
|
|
134
|
+
const { output: bundle, stats } = packFiles(files, opts);
|
|
135
|
+
// --tokens: report accounting only, no bundle.
|
|
136
|
+
if (ctx.flags.tokens === true) {
|
|
137
|
+
if (ctx.flags.json === true) {
|
|
138
|
+
output.printJson(stats);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
for (const f of [...stats.perFile].sort((a, b) => b.tokens - a.tokens)) {
|
|
142
|
+
output.writeln(`${String(f.tokens).padStart(8)} ${f.path}`);
|
|
143
|
+
}
|
|
144
|
+
output.writeln(output.bold(`${String(stats.tokens).padStart(8)} TOTAL (${stats.files} files, ${(stats.bytes / 1024).toFixed(1)} KiB)`));
|
|
145
|
+
}
|
|
146
|
+
return { success: true, exitCode: 0 };
|
|
147
|
+
}
|
|
148
|
+
const outFile = (typeof ctx.flags.output === 'string' && ctx.flags.output) || (typeof ctx.flags.o === 'string' && ctx.flags.o);
|
|
149
|
+
if (outFile) {
|
|
150
|
+
fs.writeFileSync(path.resolve(repoRoot, outFile), bundle);
|
|
151
|
+
output.printSuccess(`packed ${stats.files} files (~${stats.tokens} tokens) → ${outFile}`);
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
process.stdout.write(bundle);
|
|
155
|
+
if (!process.stdout.isTTY) { /* piped: keep stdout clean */ }
|
|
156
|
+
process.stderr.write(output.dim(`packed ${stats.files} files (~${stats.tokens} tokens)\n`));
|
|
157
|
+
}
|
|
158
|
+
return { success: true, exitCode: 0 };
|
|
159
|
+
}
|
|
160
|
+
export const packCommand = {
|
|
161
|
+
name: 'pack',
|
|
162
|
+
description: 'Bundle a repo (or subset) into one AI-friendly context blob (md/xml/json/plain) with a tree + token counts — deterministic',
|
|
163
|
+
options: [
|
|
164
|
+
{ name: 'style', description: 'output format: md (default), xml, json, plain', type: 'string' },
|
|
165
|
+
{ name: 'include', description: 'comma-separated globs; keep only matching files (e.g. "*.ts,*.md")', type: 'string' },
|
|
166
|
+
{ name: 'exclude', description: 'comma-separated globs to skip (e.g. "*.test.ts")', type: 'string' },
|
|
167
|
+
{ name: 'output', short: 'o', description: 'write the bundle to a file instead of stdout', type: 'string' },
|
|
168
|
+
{ name: 'no-tree', description: 'omit the directory tree header', type: 'boolean' },
|
|
169
|
+
{ name: 'no-gitignore', description: "don't apply the repo's .gitignore", type: 'boolean' },
|
|
170
|
+
{ name: 'max-file-size', description: 'skip files larger than N KiB (default 512)', type: 'string' },
|
|
171
|
+
{ name: 'redact', description: 'mask secrets in each file before bundling (uses `swarmdo redact`)', type: 'boolean' },
|
|
172
|
+
{ name: 'tokens', description: 'print per-file + total token estimates only, no bundle', type: 'boolean' },
|
|
173
|
+
{ name: 'json', description: 'with --tokens, emit the stats as JSON', type: 'boolean' },
|
|
174
|
+
],
|
|
175
|
+
examples: [
|
|
176
|
+
{ command: 'swarmdo pack src -o context.md', description: 'Bundle src/ to a markdown file' },
|
|
177
|
+
{ command: "swarmdo pack --include '*.ts' --exclude '*.test.ts'", description: 'Only non-test TypeScript' },
|
|
178
|
+
{ command: 'swarmdo pack --tokens', description: 'Token budget breakdown, largest first' },
|
|
179
|
+
{ command: 'swarmdo pack --redact --style xml', description: 'Secret-safe XML bundle' },
|
|
180
|
+
],
|
|
181
|
+
action: run,
|
|
182
|
+
};
|
|
183
|
+
export default packCommand;
|
|
184
|
+
//# sourceMappingURL=pack.js.map
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* instead of shelling out to `swarmdo codegraph` or grep+read. Reads the same
|
|
7
7
|
* .swarm/codegraph.json the CLI writes (shared fs layer in codegraph/store.ts).
|
|
8
8
|
*/
|
|
9
|
-
import { queryIndex, symbolsInFile, indexStats } from '../codegraph/codegraph.js';
|
|
9
|
+
import { queryIndex, symbolsInFile, indexStats, fileImports, fileImporters } from '../codegraph/codegraph.js';
|
|
10
10
|
import { INDEX_REL, scanRepo, saveIndex, loadIndex } from '../codegraph/store.js';
|
|
11
11
|
const KINDS = ['function', 'class', 'interface', 'type', 'const', 'enum', 'default'];
|
|
12
12
|
/** Resolve the repo root for scan/load. Params may override; default = cwd. */
|
|
@@ -113,10 +113,65 @@ const codegraphStatsTool = {
|
|
|
113
113
|
return indexStats(index);
|
|
114
114
|
},
|
|
115
115
|
};
|
|
116
|
+
const codegraphImportsTool = {
|
|
117
|
+
name: 'codegraph_imports',
|
|
118
|
+
description: 'List what a file imports — each edge as the raw specifier plus the repo file it resolves to (external packages resolve to null). Answers "what does this module depend on" from the index. Requires a prior codegraph_index.',
|
|
119
|
+
category: 'codegraph',
|
|
120
|
+
tags: ['code', 'imports', 'dependencies', 'navigation'],
|
|
121
|
+
inputSchema: {
|
|
122
|
+
type: 'object',
|
|
123
|
+
properties: {
|
|
124
|
+
file: { type: 'string', description: 'Repo-relative file path (as indexed, e.g. src/index.ts)' },
|
|
125
|
+
internal: { type: 'boolean', description: 'only edges resolved to a repo file (hide external packages)', default: false },
|
|
126
|
+
root: { type: 'string', description: 'Repo root (default: current working directory)' },
|
|
127
|
+
},
|
|
128
|
+
required: ['file'],
|
|
129
|
+
},
|
|
130
|
+
handler: async (params) => {
|
|
131
|
+
const root = rootOf(params);
|
|
132
|
+
const file = params.file;
|
|
133
|
+
if (typeof file !== 'string' || !file)
|
|
134
|
+
return { error: true, message: 'file is required' };
|
|
135
|
+
const index = loadIndex(root);
|
|
136
|
+
if (!index)
|
|
137
|
+
return { error: true, message: 'no index — run codegraph_index first' };
|
|
138
|
+
let edges = fileImports(index, file);
|
|
139
|
+
if (params.internal === true)
|
|
140
|
+
edges = edges.filter((e) => e.resolved !== null);
|
|
141
|
+
return { file, count: edges.length, edges };
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
const codegraphImportersTool = {
|
|
145
|
+
name: 'codegraph_importers',
|
|
146
|
+
description: 'List which files import a given file — its reverse dependencies. Use before changing or moving a file to see what depends on it ("what breaks if I change this"). Requires a prior codegraph_index.',
|
|
147
|
+
category: 'codegraph',
|
|
148
|
+
tags: ['code', 'imports', 'dependencies', 'impact', 'navigation'],
|
|
149
|
+
inputSchema: {
|
|
150
|
+
type: 'object',
|
|
151
|
+
properties: {
|
|
152
|
+
file: { type: 'string', description: 'Repo-relative file path whose importers you want' },
|
|
153
|
+
root: { type: 'string', description: 'Repo root (default: current working directory)' },
|
|
154
|
+
},
|
|
155
|
+
required: ['file'],
|
|
156
|
+
},
|
|
157
|
+
handler: async (params) => {
|
|
158
|
+
const root = rootOf(params);
|
|
159
|
+
const file = params.file;
|
|
160
|
+
if (typeof file !== 'string' || !file)
|
|
161
|
+
return { error: true, message: 'file is required' };
|
|
162
|
+
const index = loadIndex(root);
|
|
163
|
+
if (!index)
|
|
164
|
+
return { error: true, message: 'no index — run codegraph_index first' };
|
|
165
|
+
const edges = fileImporters(index, file);
|
|
166
|
+
return { file, count: edges.length, importers: edges };
|
|
167
|
+
},
|
|
168
|
+
};
|
|
116
169
|
export const codegraphTools = [
|
|
117
170
|
codegraphIndexTool,
|
|
118
171
|
codegraphQueryTool,
|
|
119
172
|
codegraphFileTool,
|
|
173
|
+
codegraphImportsTool,
|
|
174
|
+
codegraphImportersTool,
|
|
120
175
|
codegraphStatsTool,
|
|
121
176
|
];
|
|
122
177
|
//# sourceMappingURL=codegraph-tools.js.map
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pack.ts — bundle a repo (or a subset) into one AI-friendly context blob.
|
|
3
|
+
*
|
|
4
|
+
* swarmdo has no context-assembly primitive: `codegraph` indexes symbols for
|
|
5
|
+
* *querying* and `compact` de-noises a *stream*, but neither emits promptable
|
|
6
|
+
* source for *feeding a model*. `pack` walks the tree (respecting default skips
|
|
7
|
+
* + a minimal .gitignore + glob include/exclude), and writes a bundle in
|
|
8
|
+
* markdown / xml / json / plain with a directory tree and per-file + total
|
|
9
|
+
* token estimates. Deterministic — same inputs, byte-identical output.
|
|
10
|
+
*
|
|
11
|
+
* Pure engine: the fs walk is injected as a file list + reader by the caller
|
|
12
|
+
* (../commands/pack.ts), so this module has no fs dependency and is trivially
|
|
13
|
+
* testable. Optional secret redaction reuses ../redact/redact.ts.
|
|
14
|
+
*/
|
|
15
|
+
export type PackFormat = 'md' | 'xml' | 'json' | 'plain';
|
|
16
|
+
export interface PackFile {
|
|
17
|
+
/** repo-relative path, POSIX separators */
|
|
18
|
+
path: string;
|
|
19
|
+
content: string;
|
|
20
|
+
}
|
|
21
|
+
export interface PackOptions {
|
|
22
|
+
format?: PackFormat;
|
|
23
|
+
/** include a directory tree at the top (default true) */
|
|
24
|
+
tree?: boolean;
|
|
25
|
+
/** optional per-file transform (e.g. secret redaction) applied before bundling */
|
|
26
|
+
transform?: (content: string, path: string) => string;
|
|
27
|
+
}
|
|
28
|
+
export interface PackFileStat {
|
|
29
|
+
path: string;
|
|
30
|
+
bytes: number;
|
|
31
|
+
tokens: number;
|
|
32
|
+
}
|
|
33
|
+
export interface PackResult {
|
|
34
|
+
output: string;
|
|
35
|
+
stats: {
|
|
36
|
+
files: number;
|
|
37
|
+
bytes: number;
|
|
38
|
+
tokens: number;
|
|
39
|
+
perFile: PackFileStat[];
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Cheap, deterministic token estimate (~4 chars/token, the widely-used rule of
|
|
44
|
+
* thumb for English + code). Not a real BPE tokenizer — good enough for budget
|
|
45
|
+
* warnings, and dependency-free.
|
|
46
|
+
*/
|
|
47
|
+
export declare function estimateTokens(text: string): number;
|
|
48
|
+
/** Build an ASCII directory tree from a sorted file list. */
|
|
49
|
+
export declare function buildTree(paths: string[]): string;
|
|
50
|
+
/** Bundle files into a single context blob per `format`. */
|
|
51
|
+
export declare function packFiles(input: PackFile[], opts?: PackOptions): PackResult;
|
|
52
|
+
/**
|
|
53
|
+
* Minimal .gitignore-style matcher. Handles the common cases — plain names,
|
|
54
|
+
* `dir/`, `*.ext` globs, leading `/` anchors, and `!` negation — not the full
|
|
55
|
+
* spec (no `**` depth semantics beyond substring, no nested ignore files).
|
|
56
|
+
* Good enough to keep obvious noise out; full semantics is a follow-up.
|
|
57
|
+
*/
|
|
58
|
+
export declare function makeIgnoreMatcher(patterns: string[]): (relPath: string) => boolean;
|
|
59
|
+
//# sourceMappingURL=pack.d.ts.map
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pack.ts — bundle a repo (or a subset) into one AI-friendly context blob.
|
|
3
|
+
*
|
|
4
|
+
* swarmdo has no context-assembly primitive: `codegraph` indexes symbols for
|
|
5
|
+
* *querying* and `compact` de-noises a *stream*, but neither emits promptable
|
|
6
|
+
* source for *feeding a model*. `pack` walks the tree (respecting default skips
|
|
7
|
+
* + a minimal .gitignore + glob include/exclude), and writes a bundle in
|
|
8
|
+
* markdown / xml / json / plain with a directory tree and per-file + total
|
|
9
|
+
* token estimates. Deterministic — same inputs, byte-identical output.
|
|
10
|
+
*
|
|
11
|
+
* Pure engine: the fs walk is injected as a file list + reader by the caller
|
|
12
|
+
* (../commands/pack.ts), so this module has no fs dependency and is trivially
|
|
13
|
+
* testable. Optional secret redaction reuses ../redact/redact.ts.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Cheap, deterministic token estimate (~4 chars/token, the widely-used rule of
|
|
17
|
+
* thumb for English + code). Not a real BPE tokenizer — good enough for budget
|
|
18
|
+
* warnings, and dependency-free.
|
|
19
|
+
*/
|
|
20
|
+
export function estimateTokens(text) {
|
|
21
|
+
if (!text)
|
|
22
|
+
return 0;
|
|
23
|
+
return Math.ceil(text.length / 4);
|
|
24
|
+
}
|
|
25
|
+
/** Build an ASCII directory tree from a sorted file list. */
|
|
26
|
+
export function buildTree(paths) {
|
|
27
|
+
const root = {};
|
|
28
|
+
for (const p of [...paths].sort()) {
|
|
29
|
+
const parts = p.split('/');
|
|
30
|
+
let node = root;
|
|
31
|
+
for (let i = 0; i < parts.length; i++) {
|
|
32
|
+
const part = parts[i];
|
|
33
|
+
const isFile = i === parts.length - 1;
|
|
34
|
+
if (isFile) {
|
|
35
|
+
if (!node['\0files'])
|
|
36
|
+
node['\0files'] = [];
|
|
37
|
+
node['\0files'].push(part);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
node[part] = node[part] ?? {};
|
|
41
|
+
node = node[part];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const lines = [];
|
|
46
|
+
const walk = (node, prefix) => {
|
|
47
|
+
const dirs = Object.keys(node).filter((k) => k !== '\0files').sort();
|
|
48
|
+
const files = (node['\0files'] ?? []).sort();
|
|
49
|
+
const entries = [...dirs.map((d) => ['dir', d]), ...files.map((f) => ['file', f])];
|
|
50
|
+
entries.forEach(([kind, name], i) => {
|
|
51
|
+
const last = i === entries.length - 1;
|
|
52
|
+
lines.push(`${prefix}${last ? '└── ' : '├── '}${name}${kind === 'dir' ? '/' : ''}`);
|
|
53
|
+
if (kind === 'dir')
|
|
54
|
+
walk(node[name], prefix + (last ? ' ' : '│ '));
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
walk(root, '');
|
|
58
|
+
return lines.join('\n');
|
|
59
|
+
}
|
|
60
|
+
/** Pick a fenced-code language hint from a file extension. */
|
|
61
|
+
function langOf(path) {
|
|
62
|
+
const ext = path.slice(path.lastIndexOf('.') + 1).toLowerCase();
|
|
63
|
+
const map = {
|
|
64
|
+
ts: 'typescript', tsx: 'tsx', js: 'javascript', jsx: 'jsx', mjs: 'javascript', cjs: 'javascript',
|
|
65
|
+
json: 'json', md: 'markdown', py: 'python', rs: 'rust', go: 'go', sh: 'bash', yml: 'yaml', yaml: 'yaml',
|
|
66
|
+
html: 'html', css: 'css', sql: 'sql', toml: 'toml',
|
|
67
|
+
};
|
|
68
|
+
return map[ext] ?? '';
|
|
69
|
+
}
|
|
70
|
+
function renderMarkdown(files, opts) {
|
|
71
|
+
const parts = ['# Repository context', ''];
|
|
72
|
+
if (opts.tree !== false) {
|
|
73
|
+
parts.push('## Files', '', '```', buildTree(files.map((f) => f.path)), '```', '');
|
|
74
|
+
}
|
|
75
|
+
parts.push('## Contents', '');
|
|
76
|
+
for (const f of files) {
|
|
77
|
+
const lang = langOf(f.path);
|
|
78
|
+
parts.push(`### ${f.path}`, '', '```' + lang, f.content.replace(/\n$/, ''), '```', '');
|
|
79
|
+
}
|
|
80
|
+
return parts.join('\n');
|
|
81
|
+
}
|
|
82
|
+
function escapeXml(s) {
|
|
83
|
+
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
84
|
+
}
|
|
85
|
+
function renderXml(files, opts) {
|
|
86
|
+
const parts = ['<repository>'];
|
|
87
|
+
if (opts.tree !== false) {
|
|
88
|
+
parts.push(' <tree>', escapeXml(buildTree(files.map((f) => f.path))), ' </tree>');
|
|
89
|
+
}
|
|
90
|
+
parts.push(' <files>');
|
|
91
|
+
for (const f of files) {
|
|
92
|
+
parts.push(` <file path="${escapeXml(f.path)}">`, escapeXml(f.content.replace(/\n$/, '')), ' </file>');
|
|
93
|
+
}
|
|
94
|
+
parts.push(' </files>', '</repository>');
|
|
95
|
+
return parts.join('\n');
|
|
96
|
+
}
|
|
97
|
+
function renderJson(files) {
|
|
98
|
+
return JSON.stringify({ files: files.map((f) => ({ path: f.path, content: f.content })) }, null, 2);
|
|
99
|
+
}
|
|
100
|
+
function renderPlain(files) {
|
|
101
|
+
const parts = [];
|
|
102
|
+
for (const f of files) {
|
|
103
|
+
parts.push('='.repeat(8) + ' ' + f.path + ' ' + '='.repeat(8), f.content.replace(/\n$/, ''), '');
|
|
104
|
+
}
|
|
105
|
+
return parts.join('\n');
|
|
106
|
+
}
|
|
107
|
+
/** Bundle files into a single context blob per `format`. */
|
|
108
|
+
export function packFiles(input, opts = {}) {
|
|
109
|
+
const files = input
|
|
110
|
+
.map((f) => ({ path: f.path, content: opts.transform ? opts.transform(f.content, f.path) : f.content }))
|
|
111
|
+
.sort((a, b) => a.path.localeCompare(b.path));
|
|
112
|
+
const format = opts.format ?? 'md';
|
|
113
|
+
let output;
|
|
114
|
+
switch (format) {
|
|
115
|
+
case 'xml':
|
|
116
|
+
output = renderXml(files, opts);
|
|
117
|
+
break;
|
|
118
|
+
case 'json':
|
|
119
|
+
output = renderJson(files);
|
|
120
|
+
break;
|
|
121
|
+
case 'plain':
|
|
122
|
+
output = renderPlain(files);
|
|
123
|
+
break;
|
|
124
|
+
default: output = renderMarkdown(files, opts);
|
|
125
|
+
}
|
|
126
|
+
const perFile = files.map((f) => ({
|
|
127
|
+
path: f.path,
|
|
128
|
+
bytes: Buffer.byteLength(f.content, 'utf8'),
|
|
129
|
+
tokens: estimateTokens(f.content),
|
|
130
|
+
}));
|
|
131
|
+
return {
|
|
132
|
+
output,
|
|
133
|
+
stats: {
|
|
134
|
+
files: files.length,
|
|
135
|
+
bytes: perFile.reduce((n, f) => n + f.bytes, 0),
|
|
136
|
+
tokens: estimateTokens(output),
|
|
137
|
+
perFile,
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Minimal .gitignore-style matcher. Handles the common cases — plain names,
|
|
143
|
+
* `dir/`, `*.ext` globs, leading `/` anchors, and `!` negation — not the full
|
|
144
|
+
* spec (no `**` depth semantics beyond substring, no nested ignore files).
|
|
145
|
+
* Good enough to keep obvious noise out; full semantics is a follow-up.
|
|
146
|
+
*/
|
|
147
|
+
export function makeIgnoreMatcher(patterns) {
|
|
148
|
+
const rules = patterns
|
|
149
|
+
.map((p) => p.trim())
|
|
150
|
+
.filter((p) => p && !p.startsWith('#'))
|
|
151
|
+
.map((p) => {
|
|
152
|
+
const negate = p.startsWith('!');
|
|
153
|
+
let body = negate ? p.slice(1) : p;
|
|
154
|
+
const dirOnly = body.endsWith('/');
|
|
155
|
+
if (dirOnly)
|
|
156
|
+
body = body.slice(0, -1);
|
|
157
|
+
const anchored = body.startsWith('/');
|
|
158
|
+
if (anchored)
|
|
159
|
+
body = body.slice(1);
|
|
160
|
+
const re = new RegExp('^' +
|
|
161
|
+
body
|
|
162
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
|
163
|
+
.replace(/\*/g, '[^/]*')
|
|
164
|
+
.replace(/\?/g, '[^/]') +
|
|
165
|
+
(dirOnly ? '(/|$)' : '($|/)'));
|
|
166
|
+
return { negate, anchored, re };
|
|
167
|
+
});
|
|
168
|
+
return (relPath) => {
|
|
169
|
+
let ignored = false;
|
|
170
|
+
for (const r of rules) {
|
|
171
|
+
const candidates = r.anchored ? [relPath] : [relPath, ...relPath.split('/').map((_, i, a) => a.slice(i).join('/'))];
|
|
172
|
+
if (candidates.some((c) => r.re.test(c)))
|
|
173
|
+
ignored = !r.negate;
|
|
174
|
+
}
|
|
175
|
+
return ignored;
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=pack.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swarmdo/cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Swarmdo CLI - Enterprise AI agent orchestration with 60+ specialized agents, swarm coordination, MCP server, self-learning hooks, and vector memory for Claude Code",
|
|
6
6
|
"main": "dist/src/index.js",
|