@aiready/context-analyzer 0.1.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.
@@ -0,0 +1,24 @@
1
+
2
+ 
3
+ > @aiready/context-analyzer@0.1.0 build /Users/pengcao/projects/aiready/packages/context-analyzer
4
+ > tsup src/index.ts src/cli.ts --format cjs,esm --dts
5
+
6
+ CLI Building entry: src/cli.ts, src/index.ts
7
+ CLI Using tsconfig: tsconfig.json
8
+ CLI tsup v8.5.1
9
+ CLI Target: es2020
10
+ CJS Build start
11
+ ESM Build start
12
+ ESM dist/index.mjs 124.00 B
13
+ ESM dist/cli.mjs 12.61 KB
14
+ ESM dist/chunk-T6ZCOPPI.mjs 17.25 KB
15
+ ESM ⚡️ Build success in 36ms
16
+ CJS dist/cli.js 31.86 KB
17
+ CJS dist/index.js 18.33 KB
18
+ CJS ⚡️ Build success in 36ms
19
+ DTS Build start
20
+ DTS ⚡️ Build success in 647ms
21
+ DTS dist/cli.d.ts 20.00 B
22
+ DTS dist/index.d.ts 2.14 KB
23
+ DTS dist/cli.d.mts 20.00 B
24
+ DTS dist/index.d.mts 2.14 KB
@@ -0,0 +1,134 @@
1
+ # Contributing to @aiready/pattern-detect
2
+
3
+ Thank you for your interest in contributing to AIReady Pattern Detection! We welcome bug reports, feature requests, and code contributions.
4
+
5
+ ## 🐛 Reporting Issues
6
+
7
+ Found a bug or have a feature request? [Open an issue](https://github.com/caopengau/aiready-pattern-detect/issues) with:
8
+ - Clear description of the problem or feature
9
+ - Sample code that demonstrates the issue
10
+ - Expected vs actual behavior
11
+ - Your environment (Node version, OS)
12
+
13
+ ## 🔧 Development Setup
14
+
15
+ ```bash
16
+ # Clone your fork
17
+ git clone https://github.com/YOUR_USERNAME/aiready-pattern-detect
18
+ cd aiready-pattern-detect
19
+
20
+ # Install dependencies
21
+ pnpm install
22
+
23
+ # Build
24
+ pnpm build
25
+
26
+ # Run tests
27
+ pnpm test
28
+
29
+ # Test CLI locally
30
+ ./dist/cli.js ../test-project
31
+ ```
32
+
33
+ ## 📝 Making Changes
34
+
35
+ 1. **Fork the repository** and create a new branch:
36
+ ```bash
37
+ git checkout -b fix/similarity-calculation
38
+ # or
39
+ git checkout -b feat/new-pattern-type
40
+ ```
41
+
42
+ 2. **Make your changes** following our code style:
43
+ - Use TypeScript strict mode
44
+ - Add tests for new pattern types
45
+ - Update README with new features
46
+ - Keep detection logic modular
47
+
48
+ 3. **Test your changes**:
49
+ ```bash
50
+ pnpm build
51
+ pnpm test
52
+
53
+ # Test on real codebases
54
+ ./dist/cli.js /path/to/test-repo
55
+ ```
56
+
57
+ 4. **Commit using conventional commits**:
58
+ ```bash
59
+ git commit -m "fix: improve similarity threshold accuracy"
60
+ git commit -m "feat: add React component pattern detection"
61
+ ```
62
+
63
+ 5. **Push and open a PR**:
64
+ ```bash
65
+ git push origin feat/new-pattern-type
66
+ ```
67
+
68
+ ## 📋 Commit Convention
69
+
70
+ We use [Conventional Commits](https://www.conventionalcommits.org/):
71
+
72
+ - `feat:` - New feature (new pattern type, output format)
73
+ - `fix:` - Bug fix (detection accuracy, false positives)
74
+ - `docs:` - Documentation updates
75
+ - `perf:` - Performance improvements
76
+ - `refactor:` - Code restructuring
77
+ - `test:` - Test additions/updates
78
+
79
+ ## 🧪 Testing Guidelines
80
+
81
+ - Add test cases in `src/__tests__/detector.test.ts`
82
+ - Include real-world pattern examples
83
+ - Test edge cases (empty files, single-line functions)
84
+ - Verify output formats (console, JSON, HTML)
85
+
86
+ Example test:
87
+ ```typescript
88
+ test('detects API handler patterns', () => {
89
+ const results = detectDuplicatePatterns([...]);
90
+ expect(results).toHaveLength(2);
91
+ expect(results[0].patternType).toBe('api-handler');
92
+ });
93
+ ```
94
+
95
+ ## 🎯 Areas for Contribution
96
+
97
+ Great places to start:
98
+ - **New pattern types**: Add detection for new code patterns
99
+ - **Better categorization**: Improve pattern type classification
100
+ - **Detection accuracy**: Reduce false positives/negatives
101
+ - **Performance**: Optimize for large codebases
102
+ - **Output formats**: Add new export options
103
+ - **Documentation**: Usage examples, best practices
104
+
105
+ ## 🔍 Code Review
106
+
107
+ - All checks must pass (build, tests, lint)
108
+ - Maintainers review within 2 business days
109
+ - Address feedback and update PR
110
+ - Once approved, we'll merge and publish
111
+
112
+ ## 📚 Documentation
113
+
114
+ - Update README.md for new features
115
+ - Add examples for new pattern types
116
+ - Document CLI options
117
+ - Include real-world use cases
118
+
119
+ ## 💡 Feature Ideas
120
+
121
+ Looking for inspiration? Consider:
122
+ - Language-specific pattern types (Go, Rust, etc.)
123
+ - Integration with popular linters
124
+ - VS Code extension
125
+ - CI/CD report generation
126
+ - Pattern suggestion improvements
127
+
128
+ ## ❓ Questions?
129
+
130
+ Open an issue or reach out to the maintainers. We're here to help!
131
+
132
+ ---
133
+
134
+ **Thank you for helping make AI-generated code better!** 💙
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AIReady Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,412 @@
1
+ # @aiready/context-analyzer
2
+
3
+ > **AI context window cost analysis - Detect fragmented code, deep import chains, and expensive context budgets**
4
+
5
+ When AI tools try to help with your code, they need to load files into their context window. Fragmented code structures make this expensive and sometimes impossible. This tool analyzes your codebase to identify:
6
+
7
+ - Deep import chains that require loading dozens of files
8
+ - Fragmented modules scattered across many directories
9
+ - Low-cohesion files mixing unrelated concerns
10
+ - Files with excessive context budgets
11
+
12
+ ## 🎯 Why This Tool?
13
+
14
+ ### The AI Context Cost Problem
15
+
16
+ AI coding assistants are limited by context windows, but teams unknowingly structure code in ways that maximize context consumption:
17
+
18
+ ```typescript
19
+ // Scattered user management across 8 files = 12,450 tokens
20
+ src/user/get.ts // 850 tokens
21
+ src/api/user.ts // 1,200 tokens
22
+ src/services/user.ts // 2,100 tokens
23
+ src/helpers/user.ts // 900 tokens
24
+ src/utils/user.ts // 750 tokens
25
+ src/lib/user-validation.ts // 1,800 tokens
26
+ src/models/user.ts // 2,100 tokens
27
+ src/types/user.ts // 2,750 tokens
28
+
29
+ Result: AI hits context limit, gives incomplete answers ❌
30
+
31
+ // Consolidated into 2 cohesive files = 2,100 tokens
32
+ src/user/user.ts // 1,400 tokens (core logic)
33
+ src/user/types.ts // 700 tokens (types)
34
+
35
+ Result: AI sees everything, gives complete answers ✅
36
+ ```
37
+
38
+ ### What Makes Us Different?
39
+
40
+ | Feature | madge | dependency-cruiser | @aiready/context-analyzer |
41
+ |---------|-------|-------------------|--------------------------|
42
+ | Focus | Circular dependencies | Dependency rules | AI context cost |
43
+ | Metrics | Graph visualization | Rule violations | Token cost, fragmentation |
44
+ | AI-Specific | ❌ No | ❌ No | ✅ Yes - quantifies AI impact |
45
+ | Cohesion Analysis | ❌ No | ❌ No | ✅ Yes - detects mixed concerns |
46
+ | Recommendations | Generic | Rule-based | AI context optimization |
47
+
48
+ **Recommended Workflow:**
49
+ - Use **dependency-cruiser** to enforce architecture rules (blocking)
50
+ - Use **@aiready/context-analyzer** to optimize for AI tools (advisory)
51
+ - Track improvements over time with SaaS tier
52
+
53
+ ## 🚀 Installation
54
+
55
+ ```bash
56
+ npm install -g @aiready/context-analyzer
57
+
58
+ # Or use directly with npx
59
+ npx @aiready/context-analyzer ./src
60
+ ```
61
+
62
+ ## 📊 Usage
63
+
64
+ ### CLI
65
+
66
+ ```bash
67
+ # Basic usage
68
+ aiready-context ./src
69
+
70
+ # Focus on specific concerns
71
+ aiready-context ./src --focus fragmentation
72
+ aiready-context ./src --focus cohesion
73
+ aiready-context ./src --focus depth
74
+
75
+ # Set thresholds
76
+ aiready-context ./src --max-depth 5 --max-context 10000 --min-cohesion 0.6
77
+
78
+ # Export to JSON
79
+ aiready-context ./src --output json --output-file report.json
80
+
81
+ # Generate HTML report
82
+ aiready-context ./src --output html --output-file report.html
83
+
84
+ # Include/exclude patterns
85
+ aiready-context ./src --exclude "**/test/**,**/*.test.ts"
86
+ ```
87
+
88
+ ### Sample Output
89
+
90
+ ```bash
91
+ 🔍 Analyzing context window costs...
92
+
93
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
94
+ CONTEXT ANALYSIS SUMMARY
95
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
96
+
97
+ 📁 Files analyzed: 127
98
+ 📊 Total tokens: 145,680
99
+ 💰 Avg context budget: 1,147 tokens/file
100
+ ⏱ Analysis time: 0.52s
101
+
102
+ ⚠️ Issues Found:
103
+
104
+ 🔴 Critical: 3
105
+ 🟡 Major: 12
106
+ 🔵 Minor: 8
107
+
108
+ 💡 Potential savings: 28,450 tokens
109
+
110
+ 📏 Deep Import Chains:
111
+
112
+ Average depth: 3.2
113
+ Maximum depth: 8
114
+
115
+ → src/services/order.ts (depth: 8)
116
+ → src/api/payment.ts (depth: 7)
117
+ → src/lib/validation.ts (depth: 6)
118
+
119
+ 🧩 Fragmented Modules:
120
+
121
+ Average fragmentation: 42%
122
+
123
+ ● user-management - 8 files, 67% scattered
124
+ Token cost: 12,450, Cohesion: 45%
125
+ ● order-processing - 12 files, 58% scattered
126
+ Token cost: 18,200, Cohesion: 52%
127
+
128
+ 🔀 Low Cohesion Files:
129
+
130
+ Average cohesion: 68%
131
+
132
+ ○ src/utils/helpers.ts (35% cohesion)
133
+ ○ src/lib/shared.ts (42% cohesion)
134
+
135
+ 💸 Most Expensive Files (Context Budget):
136
+
137
+ ● src/services/order.ts - 8,450 tokens
138
+ ● src/api/payment.ts - 6,200 tokens
139
+ ● src/utils/helpers.ts - 5,100 tokens
140
+
141
+ 💡 Top Recommendations:
142
+
143
+ 1. src/services/order.ts
144
+ • Flatten dependency tree or use facade pattern
145
+ • Split file by domain - separate unrelated functionality
146
+
147
+ 2. src/utils/helpers.ts
148
+ • Very low cohesion (35%) - mixed concerns
149
+ • Split file by domain - separate unrelated functionality
150
+
151
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
152
+
153
+ 💎 Want historical trends and refactoring plans? → aiready.dev/pro
154
+ 💼 Enterprise: CI/CD integration → aiready.dev/demo
155
+ ```
156
+
157
+ ### Programmatic API
158
+
159
+ ```typescript
160
+ import { analyzeContext, generateSummary } from '@aiready/context-analyzer';
161
+
162
+ // Analyze entire project
163
+ const results = await analyzeContext({
164
+ rootDir: './src',
165
+ maxDepth: 5,
166
+ maxContextBudget: 10000,
167
+ minCohesion: 0.6,
168
+ maxFragmentation: 0.5,
169
+ });
170
+
171
+ // Generate summary
172
+ const summary = generateSummary(results);
173
+
174
+ console.log(`Total files: ${summary.totalFiles}`);
175
+ console.log(`Total tokens: ${summary.totalTokens}`);
176
+ console.log(`Avg context budget: ${summary.avgContextBudget}`);
177
+ console.log(`Critical issues: ${summary.criticalIssues}`);
178
+
179
+ // Find high-cost files
180
+ const expensiveFiles = results.filter(r => r.contextBudget > 5000);
181
+ console.log(`Files with >5000 token budgets: ${expensiveFiles.length}`);
182
+
183
+ // Find fragmented modules
184
+ const fragmented = summary.fragmentedModules.filter(m => m.fragmentationScore > 0.5);
185
+ console.log(`Highly fragmented modules: ${fragmented.length}`);
186
+
187
+ // Get refactoring recommendations
188
+ for (const result of results) {
189
+ if (result.severity === 'critical') {
190
+ console.log(`${result.file}:`);
191
+ result.recommendations.forEach(rec => console.log(` - ${rec}`));
192
+ }
193
+ }
194
+ ```
195
+
196
+ ## 📊 Metrics Explained
197
+
198
+ ### Import Depth
199
+ **What:** Maximum chain length of transitive dependencies
200
+ **Impact:** Deeper chains = more files to load = higher context cost
201
+ **Threshold:** >5 is concerning, >8 is critical
202
+ **Fix:** Flatten dependency tree, use facade pattern, break circular deps
203
+
204
+ ### Context Budget
205
+ **What:** Total tokens AI needs to load to understand this file
206
+ **Impact:** Higher budget = more expensive AI assistance
207
+ **Threshold:** >10,000 tokens often hits context limits
208
+ **Fix:** Split files, reduce dependencies, extract interfaces
209
+
210
+ ### Fragmentation Score
211
+ **What:** How scattered related code is across directories (0-100%)
212
+ **Impact:** Higher = more files to load for domain understanding
213
+ **Threshold:** >50% indicates poor organization
214
+ **Fix:** Consolidate related code into cohesive modules
215
+
216
+ ### Cohesion Score
217
+ **What:** How related exports are within a file (0-100%)
218
+ **Impact:** Lower = mixed concerns = wasted context
219
+ **Threshold:** <60% indicates low cohesion
220
+ **Fix:** Split by domain, separate unrelated functionality
221
+
222
+ ## 🎯 Configuration
223
+
224
+ ### CLI Options
225
+
226
+ ```bash
227
+ --max-depth <number> # Maximum acceptable import depth (default: 5)
228
+ --max-context <number> # Maximum acceptable context budget in tokens (default: 10000)
229
+ --min-cohesion <number> # Minimum acceptable cohesion score 0-1 (default: 0.6)
230
+ --max-fragmentation <number> # Maximum acceptable fragmentation 0-1 (default: 0.5)
231
+ --focus <type> # Analysis focus: fragmentation|cohesion|depth|all (default: all)
232
+ --include-node-modules # Include node_modules in analysis (default: false)
233
+ --include <patterns> # File patterns to include (comma-separated)
234
+ --exclude <patterns> # File patterns to exclude (comma-separated)
235
+ -o, --output <format> # Output format: console|json|html (default: console)
236
+ --output-file <path> # Output file path (for json/html)
237
+ ```
238
+
239
+ ### API Options
240
+
241
+ ```typescript
242
+ interface ContextAnalyzerOptions {
243
+ rootDir: string; // Root directory to analyze
244
+ maxDepth?: number; // Maximum acceptable import depth (default: 5)
245
+ maxContextBudget?: number; // Maximum acceptable token budget (default: 10000)
246
+ minCohesion?: number; // Minimum acceptable cohesion score (default: 0.6)
247
+ maxFragmentation?: number; // Maximum acceptable fragmentation (default: 0.5)
248
+ focus?: 'fragmentation' | 'cohesion' | 'depth' | 'all'; // Analysis focus (default: 'all')
249
+ includeNodeModules?: boolean; // Include node_modules (default: false)
250
+ include?: string[]; // File patterns to include
251
+ exclude?: string[]; // File patterns to exclude
252
+ }
253
+ ```
254
+
255
+ ## 🔬 How It Works
256
+
257
+ ### 1. Dependency Graph Builder
258
+ Parses imports and exports to build a complete dependency graph of your codebase.
259
+
260
+ ### 2. Depth Calculator
261
+ Calculates maximum import chain depth using graph traversal, identifying circular dependencies.
262
+
263
+ ### 3. Domain Classifier
264
+ Infers domains from export names (e.g., "user", "order", "payment") to detect module boundaries.
265
+
266
+ ### 4. Fragmentation Detector
267
+ Groups files by domain and calculates how scattered they are across directories.
268
+
269
+ ### 5. Cohesion Analyzer
270
+ Uses entropy to measure how related exports are within each file (low entropy = high cohesion).
271
+
272
+ ### 6. Context Budget Calculator
273
+ Sums tokens across entire dependency tree to estimate AI context cost for each file.
274
+
275
+ ## 🎨 Output Formats
276
+
277
+ ### Console (Default)
278
+ Rich formatted output with colors, emojis, and actionable recommendations.
279
+
280
+ ### JSON
281
+ Machine-readable output for CI/CD integration:
282
+
283
+ ```json
284
+ {
285
+ "summary": {
286
+ "totalFiles": 127,
287
+ "totalTokens": 145680,
288
+ "avgContextBudget": 1147,
289
+ "criticalIssues": 3,
290
+ "majorIssues": 12,
291
+ "totalPotentialSavings": 28450
292
+ },
293
+ "results": [
294
+ {
295
+ "file": "src/services/order.ts",
296
+ "tokenCost": 2100,
297
+ "importDepth": 8,
298
+ "contextBudget": 8450,
299
+ "severity": "critical",
300
+ "recommendations": ["Flatten dependency tree"]
301
+ }
302
+ ]
303
+ }
304
+ ```
305
+
306
+ ### HTML
307
+ Shareable report with tables and visualizations. Perfect for stakeholders:
308
+
309
+ ```bash
310
+ aiready-context ./src --output html --output-file context-report.html
311
+ ```
312
+
313
+ ## 🔗 Integration
314
+
315
+ ### CI/CD
316
+
317
+ ```yaml
318
+ # .github/workflows/code-quality.yml
319
+ - name: Analyze context costs
320
+ run: npx @aiready/context-analyzer ./src --output json --output-file context-report.json
321
+
322
+ - name: Check critical issues
323
+ run: |
324
+ CRITICAL=$(jq '.summary.criticalIssues' context-report.json)
325
+ if [ $CRITICAL -gt 0 ]; then
326
+ echo "❌ $CRITICAL critical context issues found"
327
+ exit 1
328
+ fi
329
+ ```
330
+
331
+ ### Pre-commit Hook
332
+
333
+ ```bash
334
+ #!/bin/sh
335
+ # .git/hooks/pre-commit
336
+ npx @aiready/context-analyzer ./src --max-depth 6 --max-context 8000 --output json > /tmp/context.json
337
+ CRITICAL=$(jq '.summary.criticalIssues' /tmp/context.json)
338
+ if [ $CRITICAL -gt 0 ]; then
339
+ echo "❌ Critical context issues detected. Fix before committing."
340
+ exit 1
341
+ fi
342
+ ```
343
+
344
+ ### With Other Tools
345
+
346
+ ```bash
347
+ # Run all quality checks
348
+ npm run lint # ESLint for code quality
349
+ npm run type-check # TypeScript for type safety
350
+ dependency-cruiser src # Architecture rules
351
+ aiready-context src # AI context optimization
352
+ aiready-patterns src # Duplicate pattern detection
353
+ ```
354
+
355
+ ## 💰 SaaS Features (Coming Soon)
356
+
357
+ ### Free Tier (CLI)
358
+ ✅ One-time snapshot analysis
359
+ ✅ All metrics and recommendations
360
+ ✅ JSON/HTML export
361
+
362
+ ### Pro ($49/month)
363
+ 📈 Historical trend tracking
364
+ 📊 Team benchmarks
365
+ 🎯 Automated refactoring plans (5/month)
366
+ 🔌 Integration API
367
+
368
+ ### Enterprise (Custom Pricing)
369
+ 🔄 CI/CD integration
370
+ 💰 AI usage correlation (show $ waste)
371
+ ⚙️ Custom rules engine
372
+ 👥 Team analytics
373
+ 🎯 Unlimited refactoring plans
374
+
375
+ **Want to track improvements over time?** Visit [aiready.dev](https://aiready.dev)
376
+
377
+ ## 🤝 Contributing
378
+
379
+ Contributions welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
380
+
381
+ ### Development Setup
382
+
383
+ ```bash
384
+ git clone https://github.com/caopengau/aiready.git
385
+ cd aiready
386
+ pnpm install
387
+
388
+ # Build all packages
389
+ pnpm build
390
+
391
+ # Run tests
392
+ pnpm --filter @aiready/context-analyzer test
393
+
394
+ # Dev mode (watch)
395
+ pnpm --filter @aiready/context-analyzer dev
396
+ ```
397
+
398
+ ## 📝 License
399
+
400
+ MIT © AIReady Team
401
+
402
+ ## 🔗 Related Tools
403
+
404
+ - **[@aiready/pattern-detect](https://www.npmjs.com/package/@aiready/pattern-detect)** - Find semantic duplicate patterns
405
+ - **[@aiready/doc-drift](https://github.com/caopengau/aiready)** - Detect stale documentation
406
+ - **[@aiready/consistency](https://github.com/caopengau/aiready)** - Check naming consistency
407
+
408
+ ---
409
+
410
+ **Made with ❤️ for AI-assisted development**
411
+
412
+ *Stop wasting context tokens on fragmented code.*