@peakinfer/cli 1.0.133
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/settings.local.json +8 -0
- package/.env.example +6 -0
- package/.github/workflows/peakinfer.yml +64 -0
- package/CHANGELOG.md +31 -0
- package/LICENSE +190 -0
- package/README.md +335 -0
- package/data/inferencemax.json +274 -0
- package/dist/agent-analyzer.d.ts +45 -0
- package/dist/agent-analyzer.d.ts.map +1 -0
- package/dist/agent-analyzer.js +374 -0
- package/dist/agent-analyzer.js.map +1 -0
- package/dist/agent.d.ts +76 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +965 -0
- package/dist/agent.js.map +1 -0
- package/dist/agents/correlation-analyzer.d.ts +34 -0
- package/dist/agents/correlation-analyzer.d.ts.map +1 -0
- package/dist/agents/correlation-analyzer.js +261 -0
- package/dist/agents/correlation-analyzer.js.map +1 -0
- package/dist/agents/index.d.ts +91 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +111 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/runtime-analyzer.d.ts +38 -0
- package/dist/agents/runtime-analyzer.d.ts.map +1 -0
- package/dist/agents/runtime-analyzer.js +244 -0
- package/dist/agents/runtime-analyzer.js.map +1 -0
- package/dist/analysis-types.d.ts +500 -0
- package/dist/analysis-types.d.ts.map +1 -0
- package/dist/analysis-types.js +11 -0
- package/dist/analysis-types.js.map +1 -0
- package/dist/analytics.d.ts +25 -0
- package/dist/analytics.d.ts.map +1 -0
- package/dist/analytics.js +94 -0
- package/dist/analytics.js.map +1 -0
- package/dist/analyzer.d.ts +48 -0
- package/dist/analyzer.d.ts.map +1 -0
- package/dist/analyzer.js +547 -0
- package/dist/analyzer.js.map +1 -0
- package/dist/artifacts.d.ts +44 -0
- package/dist/artifacts.d.ts.map +1 -0
- package/dist/artifacts.js +165 -0
- package/dist/artifacts.js.map +1 -0
- package/dist/benchmarks/index.d.ts +88 -0
- package/dist/benchmarks/index.d.ts.map +1 -0
- package/dist/benchmarks/index.js +205 -0
- package/dist/benchmarks/index.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +427 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/ci.d.ts +19 -0
- package/dist/commands/ci.d.ts.map +1 -0
- package/dist/commands/ci.js +253 -0
- package/dist/commands/ci.js.map +1 -0
- package/dist/commands/config.d.ts +16 -0
- package/dist/commands/config.d.ts.map +1 -0
- package/dist/commands/config.js +249 -0
- package/dist/commands/config.js.map +1 -0
- package/dist/commands/demo.d.ts +15 -0
- package/dist/commands/demo.d.ts.map +1 -0
- package/dist/commands/demo.js +106 -0
- package/dist/commands/demo.js.map +1 -0
- package/dist/commands/export.d.ts +14 -0
- package/dist/commands/export.d.ts.map +1 -0
- package/dist/commands/export.js +209 -0
- package/dist/commands/export.js.map +1 -0
- package/dist/commands/history.d.ts +15 -0
- package/dist/commands/history.d.ts.map +1 -0
- package/dist/commands/history.js +389 -0
- package/dist/commands/history.js.map +1 -0
- package/dist/commands/template.d.ts +14 -0
- package/dist/commands/template.d.ts.map +1 -0
- package/dist/commands/template.js +341 -0
- package/dist/commands/template.js.map +1 -0
- package/dist/commands/validate-map.d.ts +12 -0
- package/dist/commands/validate-map.d.ts.map +1 -0
- package/dist/commands/validate-map.js +274 -0
- package/dist/commands/validate-map.js.map +1 -0
- package/dist/commands/whatif.d.ts +17 -0
- package/dist/commands/whatif.d.ts.map +1 -0
- package/dist/commands/whatif.js +206 -0
- package/dist/commands/whatif.js.map +1 -0
- package/dist/comparison.d.ts +38 -0
- package/dist/comparison.d.ts.map +1 -0
- package/dist/comparison.js +223 -0
- package/dist/comparison.js.map +1 -0
- package/dist/config.d.ts +42 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +158 -0
- package/dist/config.js.map +1 -0
- package/dist/connectors/helicone.d.ts +9 -0
- package/dist/connectors/helicone.d.ts.map +1 -0
- package/dist/connectors/helicone.js +106 -0
- package/dist/connectors/helicone.js.map +1 -0
- package/dist/connectors/index.d.ts +37 -0
- package/dist/connectors/index.d.ts.map +1 -0
- package/dist/connectors/index.js +65 -0
- package/dist/connectors/index.js.map +1 -0
- package/dist/connectors/langsmith.d.ts +9 -0
- package/dist/connectors/langsmith.d.ts.map +1 -0
- package/dist/connectors/langsmith.js +122 -0
- package/dist/connectors/langsmith.js.map +1 -0
- package/dist/connectors/types.d.ts +83 -0
- package/dist/connectors/types.d.ts.map +1 -0
- package/dist/connectors/types.js +98 -0
- package/dist/connectors/types.js.map +1 -0
- package/dist/cost-estimator.d.ts +46 -0
- package/dist/cost-estimator.d.ts.map +1 -0
- package/dist/cost-estimator.js +104 -0
- package/dist/cost-estimator.js.map +1 -0
- package/dist/costs.d.ts +57 -0
- package/dist/costs.d.ts.map +1 -0
- package/dist/costs.js +251 -0
- package/dist/costs.js.map +1 -0
- package/dist/counterfactuals.d.ts +29 -0
- package/dist/counterfactuals.d.ts.map +1 -0
- package/dist/counterfactuals.js +448 -0
- package/dist/counterfactuals.js.map +1 -0
- package/dist/enhancement-prompts.d.ts +41 -0
- package/dist/enhancement-prompts.d.ts.map +1 -0
- package/dist/enhancement-prompts.js +88 -0
- package/dist/enhancement-prompts.js.map +1 -0
- package/dist/envelopes.d.ts +20 -0
- package/dist/envelopes.d.ts.map +1 -0
- package/dist/envelopes.js +790 -0
- package/dist/envelopes.js.map +1 -0
- package/dist/format-normalizer.d.ts +71 -0
- package/dist/format-normalizer.d.ts.map +1 -0
- package/dist/format-normalizer.js +1331 -0
- package/dist/format-normalizer.js.map +1 -0
- package/dist/history.d.ts +79 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +313 -0
- package/dist/history.js.map +1 -0
- package/dist/html.d.ts +11 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/html.js +463 -0
- package/dist/html.js.map +1 -0
- package/dist/impact.d.ts +42 -0
- package/dist/impact.d.ts.map +1 -0
- package/dist/impact.js +443 -0
- package/dist/impact.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/insights.d.ts +5 -0
- package/dist/insights.d.ts.map +1 -0
- package/dist/insights.js +271 -0
- package/dist/insights.js.map +1 -0
- package/dist/joiner.d.ts +9 -0
- package/dist/joiner.d.ts.map +1 -0
- package/dist/joiner.js +247 -0
- package/dist/joiner.js.map +1 -0
- package/dist/orchestrator.d.ts +34 -0
- package/dist/orchestrator.d.ts.map +1 -0
- package/dist/orchestrator.js +827 -0
- package/dist/orchestrator.js.map +1 -0
- package/dist/pdf.d.ts +26 -0
- package/dist/pdf.d.ts.map +1 -0
- package/dist/pdf.js +84 -0
- package/dist/pdf.js.map +1 -0
- package/dist/prediction.d.ts +33 -0
- package/dist/prediction.d.ts.map +1 -0
- package/dist/prediction.js +316 -0
- package/dist/prediction.js.map +1 -0
- package/dist/prompts/loader.d.ts +38 -0
- package/dist/prompts/loader.d.ts.map +1 -0
- package/dist/prompts/loader.js +60 -0
- package/dist/prompts/loader.js.map +1 -0
- package/dist/renderer.d.ts +64 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +923 -0
- package/dist/renderer.js.map +1 -0
- package/dist/runid.d.ts +57 -0
- package/dist/runid.d.ts.map +1 -0
- package/dist/runid.js +199 -0
- package/dist/runid.js.map +1 -0
- package/dist/runtime.d.ts +29 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +366 -0
- package/dist/runtime.js.map +1 -0
- package/dist/scanner.d.ts +11 -0
- package/dist/scanner.d.ts.map +1 -0
- package/dist/scanner.js +426 -0
- package/dist/scanner.js.map +1 -0
- package/dist/templates.d.ts +120 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +429 -0
- package/dist/templates.js.map +1 -0
- package/dist/tools/index.d.ts +153 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +177 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/types.d.ts +3647 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +703 -0
- package/dist/types.js.map +1 -0
- package/dist/version.d.ts +7 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +23 -0
- package/dist/version.js.map +1 -0
- package/docs/demo-guide.md +423 -0
- package/docs/events-format.md +295 -0
- package/docs/inferencemap-spec.md +344 -0
- package/docs/migration-v2.md +293 -0
- package/fixtures/demo/precomputed.json +142 -0
- package/fixtures/demo-project/README.md +52 -0
- package/fixtures/demo-project/ai-service.ts +65 -0
- package/fixtures/demo-project/sample-events.jsonl +15 -0
- package/fixtures/demo-project/src/ai-service.ts +128 -0
- package/fixtures/demo-project/src/llm-client.ts +155 -0
- package/package.json +65 -0
- package/prompts/agent-analyzer.yaml +47 -0
- package/prompts/ci-gate.yaml +98 -0
- package/prompts/correlation-analyzer.yaml +178 -0
- package/prompts/format-normalizer.yaml +46 -0
- package/prompts/peak-performance.yaml +180 -0
- package/prompts/pr-comment.yaml +111 -0
- package/prompts/runtime-analyzer.yaml +189 -0
- package/prompts/unified-analyzer.yaml +241 -0
- package/schemas/inference-map.v0.1.json +215 -0
- package/scripts/benchmark.ts +394 -0
- package/scripts/demo-v1.5.sh +158 -0
- package/scripts/sync-from-site.sh +197 -0
- package/scripts/validate-sync.sh +178 -0
- package/src/agent-analyzer.ts +481 -0
- package/src/agent.ts +1232 -0
- package/src/agents/correlation-analyzer.ts +353 -0
- package/src/agents/index.ts +235 -0
- package/src/agents/runtime-analyzer.ts +343 -0
- package/src/analysis-types.ts +558 -0
- package/src/analytics.ts +100 -0
- package/src/analyzer.ts +692 -0
- package/src/artifacts.ts +218 -0
- package/src/benchmarks/index.ts +309 -0
- package/src/cli.ts +503 -0
- package/src/commands/ci.ts +336 -0
- package/src/commands/config.ts +288 -0
- package/src/commands/demo.ts +175 -0
- package/src/commands/export.ts +297 -0
- package/src/commands/history.ts +425 -0
- package/src/commands/template.ts +385 -0
- package/src/commands/validate-map.ts +324 -0
- package/src/commands/whatif.ts +272 -0
- package/src/comparison.ts +283 -0
- package/src/config.ts +188 -0
- package/src/connectors/helicone.ts +164 -0
- package/src/connectors/index.ts +93 -0
- package/src/connectors/langsmith.ts +179 -0
- package/src/connectors/types.ts +180 -0
- package/src/cost-estimator.ts +146 -0
- package/src/costs.ts +347 -0
- package/src/counterfactuals.ts +516 -0
- package/src/enhancement-prompts.ts +118 -0
- package/src/envelopes.ts +814 -0
- package/src/format-normalizer.ts +1486 -0
- package/src/history.ts +400 -0
- package/src/html.ts +512 -0
- package/src/impact.ts +522 -0
- package/src/index.ts +83 -0
- package/src/insights.ts +341 -0
- package/src/joiner.ts +289 -0
- package/src/orchestrator.ts +1015 -0
- package/src/pdf.ts +110 -0
- package/src/prediction.ts +392 -0
- package/src/prompts/loader.ts +88 -0
- package/src/renderer.ts +1045 -0
- package/src/runid.ts +261 -0
- package/src/runtime.ts +450 -0
- package/src/scanner.ts +508 -0
- package/src/templates.ts +561 -0
- package/src/tools/index.ts +214 -0
- package/src/types.ts +873 -0
- package/src/version.ts +24 -0
- package/templates/context-accumulation.yaml +23 -0
- package/templates/cost-concentration.yaml +20 -0
- package/templates/dead-code.yaml +20 -0
- package/templates/latency-explainer.yaml +23 -0
- package/templates/optimizations/ab-testing-framework.yaml +74 -0
- package/templates/optimizations/api-gateway-optimization.yaml +81 -0
- package/templates/optimizations/api-model-routing-strategy.yaml +126 -0
- package/templates/optimizations/auto-scaling-optimization.yaml +85 -0
- package/templates/optimizations/batch-utilization-diagnostic.yaml +142 -0
- package/templates/optimizations/comprehensive-apm.yaml +76 -0
- package/templates/optimizations/context-window-optimization.yaml +91 -0
- package/templates/optimizations/cost-sensitive-batch-processing.yaml +77 -0
- package/templates/optimizations/distributed-training-optimization.yaml +77 -0
- package/templates/optimizations/document-analysis-edge.yaml +77 -0
- package/templates/optimizations/document-pipeline-optimization.yaml +78 -0
- package/templates/optimizations/domain-specific-distillation.yaml +78 -0
- package/templates/optimizations/error-handling-optimization.yaml +76 -0
- package/templates/optimizations/gptq-4bit-quantization.yaml +96 -0
- package/templates/optimizations/long-context-memory-management.yaml +78 -0
- package/templates/optimizations/max-tokens-optimization.yaml +76 -0
- package/templates/optimizations/memory-bandwidth-optimization.yaml +73 -0
- package/templates/optimizations/multi-framework-resilience.yaml +75 -0
- package/templates/optimizations/multi-tenant-optimization.yaml +75 -0
- package/templates/optimizations/prompt-caching-optimization.yaml +143 -0
- package/templates/optimizations/pytorch-to-onnx-migration.yaml +109 -0
- package/templates/optimizations/quality-monitoring.yaml +74 -0
- package/templates/optimizations/realtime-budget-controls.yaml +74 -0
- package/templates/optimizations/realtime-latency-optimization.yaml +74 -0
- package/templates/optimizations/sglang-concurrency-optimization.yaml +78 -0
- package/templates/optimizations/smart-model-routing.yaml +96 -0
- package/templates/optimizations/streaming-batch-selection.yaml +167 -0
- package/templates/optimizations/system-prompt-optimization.yaml +75 -0
- package/templates/optimizations/tensorrt-llm-performance.yaml +77 -0
- package/templates/optimizations/vllm-high-throughput-optimization.yaml +93 -0
- package/templates/optimizations/vllm-migration-memory-bound.yaml +78 -0
- package/templates/overpowered-extraction.yaml +32 -0
- package/templates/overpowered-model.yaml +31 -0
- package/templates/prompt-bloat.yaml +24 -0
- package/templates/retry-explosion.yaml +28 -0
- package/templates/schema/insight.schema.json +113 -0
- package/templates/schema/optimization.schema.json +180 -0
- package/templates/streaming-drift.yaml +30 -0
- package/templates/throughput-gap.yaml +21 -0
- package/templates/token-underutilization.yaml +28 -0
- package/templates/untested-fallback.yaml +21 -0
- package/tests/accuracy/drift-detection.test.ts +184 -0
- package/tests/accuracy/false-positives.test.ts +166 -0
- package/tests/accuracy/templates.test.ts +205 -0
- package/tests/action/commands.test.ts +125 -0
- package/tests/action/comments.test.ts +347 -0
- package/tests/cli.test.ts +203 -0
- package/tests/comparison.test.ts +309 -0
- package/tests/correlation-analyzer.test.ts +534 -0
- package/tests/counterfactuals.test.ts +347 -0
- package/tests/fixtures/events/missing-id.jsonl +1 -0
- package/tests/fixtures/events/missing-input.jsonl +1 -0
- package/tests/fixtures/events/missing-latency.jsonl +1 -0
- package/tests/fixtures/events/missing-model.jsonl +1 -0
- package/tests/fixtures/events/missing-output.jsonl +1 -0
- package/tests/fixtures/events/missing-provider.jsonl +1 -0
- package/tests/fixtures/events/missing-ts.jsonl +1 -0
- package/tests/fixtures/events/valid.csv +3 -0
- package/tests/fixtures/events/valid.json +1 -0
- package/tests/fixtures/events/valid.jsonl +2 -0
- package/tests/fixtures/events/with-callsite.jsonl +1 -0
- package/tests/fixtures/events/with-intent.jsonl +1 -0
- package/tests/fixtures/events/wrong-type.jsonl +1 -0
- package/tests/fixtures/repos/empty/.gitkeep +0 -0
- package/tests/fixtures/repos/hybrid-router/router.py +35 -0
- package/tests/fixtures/repos/saas-anthropic/agent.ts +27 -0
- package/tests/fixtures/repos/saas-openai/assistant.js +33 -0
- package/tests/fixtures/repos/saas-openai/client.py +26 -0
- package/tests/fixtures/repos/self-hosted-vllm/inference.py +22 -0
- package/tests/github-action.test.ts +292 -0
- package/tests/insights.test.ts +878 -0
- package/tests/joiner.test.ts +168 -0
- package/tests/performance/action-latency.test.ts +132 -0
- package/tests/performance/benchmark.test.ts +189 -0
- package/tests/performance/cli-latency.test.ts +102 -0
- package/tests/pr-comment.test.ts +313 -0
- package/tests/prediction.test.ts +296 -0
- package/tests/runtime-analyzer.test.ts +375 -0
- package/tests/runtime.test.ts +205 -0
- package/tests/scanner.test.ts +122 -0
- package/tests/template-conformance.test.ts +526 -0
- package/tests/unit/cost-calculator.test.ts +303 -0
- package/tests/unit/credits.test.ts +180 -0
- package/tests/unit/inference-map.test.ts +276 -0
- package/tests/unit/schema.test.ts +300 -0
- package/tsconfig.json +20 -0
- package/vitest.config.ts +14 -0
package/src/scanner.ts
ADDED
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import { glob } from 'glob';
|
|
2
|
+
import ignoreDefault from 'ignore';
|
|
3
|
+
import { readFileSync, existsSync, statSync } from 'fs';
|
|
4
|
+
import { join, relative, extname } from 'path';
|
|
5
|
+
import type { ScanResult, ScanCandidate } from './types.js';
|
|
6
|
+
|
|
7
|
+
// Handle ESM default import with type assertion for NodeNext resolution
|
|
8
|
+
const ignore = ignoreDefault as unknown as (options?: { ignorecase?: boolean }) => {
|
|
9
|
+
add(patterns: string | readonly string[]): void;
|
|
10
|
+
ignores(pathname: string): boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
interface IgnoreInstance {
|
|
14
|
+
add(patterns: string | readonly string[]): void;
|
|
15
|
+
ignores(pathname: string): boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// =============================================================================
|
|
19
|
+
// CONSTANTS
|
|
20
|
+
// =============================================================================
|
|
21
|
+
|
|
22
|
+
const DEFAULT_IGNORES = [
|
|
23
|
+
'node_modules/**',
|
|
24
|
+
'dist/**',
|
|
25
|
+
'.git/**',
|
|
26
|
+
'__pycache__/**',
|
|
27
|
+
'*.pyc',
|
|
28
|
+
'.venv/**',
|
|
29
|
+
'venv/**',
|
|
30
|
+
'.env/**',
|
|
31
|
+
'env/**',
|
|
32
|
+
'build/**',
|
|
33
|
+
'target/**',
|
|
34
|
+
'.next/**',
|
|
35
|
+
'.nuxt/**',
|
|
36
|
+
'coverage/**',
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
// v1.9.5: Files to skip for cost optimization
|
|
40
|
+
const SKIP_PATTERNS = [
|
|
41
|
+
// Test files
|
|
42
|
+
'**/*.test.*',
|
|
43
|
+
'**/*.spec.*',
|
|
44
|
+
'**/__tests__/**',
|
|
45
|
+
'**/test/**',
|
|
46
|
+
'**/tests/**',
|
|
47
|
+
'**/*.test.ts',
|
|
48
|
+
'**/*.test.js',
|
|
49
|
+
'**/*.spec.ts',
|
|
50
|
+
'**/*.spec.js',
|
|
51
|
+
'**/test_*.py',
|
|
52
|
+
'**/*_test.py',
|
|
53
|
+
'**/*_test.go',
|
|
54
|
+
// Config files
|
|
55
|
+
'**/jest.config.*',
|
|
56
|
+
'**/vitest.config.*',
|
|
57
|
+
'**/webpack.config.*',
|
|
58
|
+
'**/rollup.config.*',
|
|
59
|
+
'**/vite.config.*',
|
|
60
|
+
'**/tsconfig.json',
|
|
61
|
+
'**/package.json',
|
|
62
|
+
'**/package-lock.json',
|
|
63
|
+
'**/yarn.lock',
|
|
64
|
+
'**/pnpm-lock.yaml',
|
|
65
|
+
// Documentation
|
|
66
|
+
'**/*.md',
|
|
67
|
+
'**/docs/**',
|
|
68
|
+
'**/documentation/**',
|
|
69
|
+
// Generated files
|
|
70
|
+
'**/generated/**',
|
|
71
|
+
'**/*.generated.*',
|
|
72
|
+
'**/*.d.ts',
|
|
73
|
+
// Fixtures and mocks
|
|
74
|
+
'**/fixtures/**',
|
|
75
|
+
'**/mocks/**',
|
|
76
|
+
'**/__mocks__/**',
|
|
77
|
+
'**/snapshots/**',
|
|
78
|
+
'**/__snapshots__/**',
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
// v1.9.5: Maximum file size in bytes (50KB)
|
|
82
|
+
const MAX_FILE_SIZE = 50 * 1024;
|
|
83
|
+
|
|
84
|
+
const LANGUAGE_MAP: Record<string, string> = {
|
|
85
|
+
'.py': 'python',
|
|
86
|
+
'.ts': 'typescript',
|
|
87
|
+
'.tsx': 'typescript',
|
|
88
|
+
'.js': 'javascript',
|
|
89
|
+
'.jsx': 'javascript',
|
|
90
|
+
'.mjs': 'javascript',
|
|
91
|
+
'.cjs': 'javascript',
|
|
92
|
+
'.go': 'go',
|
|
93
|
+
'.java': 'java',
|
|
94
|
+
'.kt': 'kotlin',
|
|
95
|
+
'.rs': 'rust',
|
|
96
|
+
'.rb': 'ruby',
|
|
97
|
+
'.php': 'php',
|
|
98
|
+
'.cs': 'csharp',
|
|
99
|
+
'.swift': 'swift',
|
|
100
|
+
'.scala': 'scala',
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const CODE_EXTENSIONS = Object.keys(LANGUAGE_MAP);
|
|
104
|
+
|
|
105
|
+
// Patterns that indicate LLM inference calls
|
|
106
|
+
const INFERENCE_PATTERNS = [
|
|
107
|
+
// ==========================================================================
|
|
108
|
+
// OpenAI SDK
|
|
109
|
+
// ==========================================================================
|
|
110
|
+
/\.chat\.completions\.create\(/,
|
|
111
|
+
/openai\.completions\.create\(/,
|
|
112
|
+
/\.completions\.create\(/,
|
|
113
|
+
// NOTE: Removed /OpenAI\(\)/ - client initialization, not an inference call
|
|
114
|
+
// NOTE: Removed /AsyncOpenAI\(\)/ - client initialization, not an inference call
|
|
115
|
+
/openai\.ChatCompletion\.create\(/,
|
|
116
|
+
/openai\.Completion\.create\(/,
|
|
117
|
+
/\.embeddings\.create\(/, // OpenAI embeddings API
|
|
118
|
+
|
|
119
|
+
// ==========================================================================
|
|
120
|
+
// Anthropic SDK
|
|
121
|
+
// ==========================================================================
|
|
122
|
+
/\.messages\.create\(/,
|
|
123
|
+
/anthropic\.messages\(/,
|
|
124
|
+
/\.create_message\(/,
|
|
125
|
+
// NOTE: Removed /Anthropic\(\)/ - client initialization, not an inference call
|
|
126
|
+
// NOTE: Removed /AsyncAnthropic\(\)/ - client initialization, not an inference call
|
|
127
|
+
/anthropic\.completions\(/,
|
|
128
|
+
|
|
129
|
+
// ==========================================================================
|
|
130
|
+
// Google AI / Vertex AI
|
|
131
|
+
// ==========================================================================
|
|
132
|
+
/\.generate_content\(/,
|
|
133
|
+
/genai\.GenerativeModel\(/,
|
|
134
|
+
/GenerativeModel\(/,
|
|
135
|
+
/vertexai\.generative_models/,
|
|
136
|
+
/aiplatform\.gapic/,
|
|
137
|
+
|
|
138
|
+
// ==========================================================================
|
|
139
|
+
// Mistral
|
|
140
|
+
// ==========================================================================
|
|
141
|
+
/MistralClient\(/,
|
|
142
|
+
/mistral\.chat\(/,
|
|
143
|
+
/mistral\.complete\(/,
|
|
144
|
+
|
|
145
|
+
// ==========================================================================
|
|
146
|
+
// Cohere
|
|
147
|
+
// ==========================================================================
|
|
148
|
+
/cohere\.chat\(/,
|
|
149
|
+
/cohere\.generate\(/,
|
|
150
|
+
/CohereClient\(/,
|
|
151
|
+
|
|
152
|
+
// ==========================================================================
|
|
153
|
+
// Together AI
|
|
154
|
+
// ==========================================================================
|
|
155
|
+
/together\.chat\./,
|
|
156
|
+
/Together\(\)/,
|
|
157
|
+
/together\.completions/,
|
|
158
|
+
/together_ai/,
|
|
159
|
+
|
|
160
|
+
// ==========================================================================
|
|
161
|
+
// Fireworks AI
|
|
162
|
+
// ==========================================================================
|
|
163
|
+
/fireworks\.chat\./,
|
|
164
|
+
/Fireworks\(/,
|
|
165
|
+
/fireworks\.completions/,
|
|
166
|
+
/fireworks_ai/,
|
|
167
|
+
|
|
168
|
+
// ==========================================================================
|
|
169
|
+
// Groq
|
|
170
|
+
// ==========================================================================
|
|
171
|
+
/groq\.chat\./,
|
|
172
|
+
/Groq\(\)/,
|
|
173
|
+
/groq\.completions/,
|
|
174
|
+
|
|
175
|
+
// ==========================================================================
|
|
176
|
+
// Replicate
|
|
177
|
+
// ==========================================================================
|
|
178
|
+
/replicate\.run\(/,
|
|
179
|
+
/replicate\.predictions\.create\(/,
|
|
180
|
+
/Replicate\(\)/,
|
|
181
|
+
|
|
182
|
+
// ==========================================================================
|
|
183
|
+
// Perplexity
|
|
184
|
+
// ==========================================================================
|
|
185
|
+
/perplexity\.chat\./,
|
|
186
|
+
/PerplexityClient\(/,
|
|
187
|
+
|
|
188
|
+
// ==========================================================================
|
|
189
|
+
// AWS Bedrock
|
|
190
|
+
// ==========================================================================
|
|
191
|
+
/bedrock-runtime/,
|
|
192
|
+
/invoke_model\(/,
|
|
193
|
+
/InvokeModel/,
|
|
194
|
+
/BedrockRuntime\(/,
|
|
195
|
+
/bedrock\.converse\(/,
|
|
196
|
+
|
|
197
|
+
// ==========================================================================
|
|
198
|
+
// Azure OpenAI
|
|
199
|
+
// ==========================================================================
|
|
200
|
+
/AzureOpenAI\(/,
|
|
201
|
+
/azure\.openai/,
|
|
202
|
+
/openai\.azure/,
|
|
203
|
+
|
|
204
|
+
// ==========================================================================
|
|
205
|
+
// LangChain
|
|
206
|
+
// ==========================================================================
|
|
207
|
+
/ChatOpenAI\(/,
|
|
208
|
+
/ChatAnthropic\(/,
|
|
209
|
+
/ChatGoogleGenerativeAI\(/,
|
|
210
|
+
/ChatMistralAI\(/,
|
|
211
|
+
/ChatCohere\(/,
|
|
212
|
+
/ChatGroq\(/,
|
|
213
|
+
/ChatFireworks\(/,
|
|
214
|
+
/ChatTogether\(/,
|
|
215
|
+
/ChatBedrock\(/,
|
|
216
|
+
/ChatVertexAI\(/,
|
|
217
|
+
/ChatOllama\(/,
|
|
218
|
+
/LLMChain\(/,
|
|
219
|
+
/ConversationChain\(/,
|
|
220
|
+
|
|
221
|
+
// ==========================================================================
|
|
222
|
+
// LlamaIndex
|
|
223
|
+
// ==========================================================================
|
|
224
|
+
/llama_index\.llms/,
|
|
225
|
+
/OpenAILike\(/,
|
|
226
|
+
/Ollama\(/,
|
|
227
|
+
/LlamaCPP\(/,
|
|
228
|
+
|
|
229
|
+
// ==========================================================================
|
|
230
|
+
// DSPy Framework
|
|
231
|
+
// ==========================================================================
|
|
232
|
+
/dspy\.Predict\(/,
|
|
233
|
+
/dspy\.ChainOfThought\(/,
|
|
234
|
+
/dspy\.ProgramOfThought\(/,
|
|
235
|
+
/dspy\.ReAct\(/,
|
|
236
|
+
/dspy\.Retrieve\(/,
|
|
237
|
+
/dspy\.generate\(/,
|
|
238
|
+
/dspy\.forward\(/,
|
|
239
|
+
/\.forward\(.*question/, // DSPy module forward calls with question param
|
|
240
|
+
|
|
241
|
+
// ==========================================================================
|
|
242
|
+
// vLLM (Self-hosted)
|
|
243
|
+
// ==========================================================================
|
|
244
|
+
/vllm\.generate/,
|
|
245
|
+
/vllm\.LLM\(/,
|
|
246
|
+
/from vllm import/,
|
|
247
|
+
/vllm\.SamplingParams/,
|
|
248
|
+
/vllm\.AsyncLLMEngine/,
|
|
249
|
+
/vllm\.entrypoints/,
|
|
250
|
+
/\/v1\/completions/, // OpenAI-compatible endpoint
|
|
251
|
+
|
|
252
|
+
// ==========================================================================
|
|
253
|
+
// SGLang (Self-hosted)
|
|
254
|
+
// ==========================================================================
|
|
255
|
+
/sglang\.generate/,
|
|
256
|
+
/sglang\.Engine\(/,
|
|
257
|
+
/from sglang import/,
|
|
258
|
+
/sglang\.RuntimeEndpoint/,
|
|
259
|
+
/sglang\.function/,
|
|
260
|
+
/sglang\.gen\(/,
|
|
261
|
+
|
|
262
|
+
// ==========================================================================
|
|
263
|
+
// TGI - Text Generation Inference (Self-hosted)
|
|
264
|
+
// ==========================================================================
|
|
265
|
+
/text-generation-inference/,
|
|
266
|
+
/InferenceClient\(/,
|
|
267
|
+
/huggingface_hub\.inference/,
|
|
268
|
+
/text_generation\(/,
|
|
269
|
+
/HuggingFaceEndpoint\(/,
|
|
270
|
+
/tgi\.generate/,
|
|
271
|
+
|
|
272
|
+
// ==========================================================================
|
|
273
|
+
// Ollama (Local inference)
|
|
274
|
+
// ==========================================================================
|
|
275
|
+
/ollama\.generate/,
|
|
276
|
+
/ollama\.chat/,
|
|
277
|
+
/ollama\.create\(/,
|
|
278
|
+
/ollama\.pull\(/,
|
|
279
|
+
/from ollama import/,
|
|
280
|
+
/Ollama\(\)/,
|
|
281
|
+
/localhost:11434/, // Default Ollama port
|
|
282
|
+
/127\.0\.0\.1:11434/,
|
|
283
|
+
|
|
284
|
+
// ==========================================================================
|
|
285
|
+
// llama.cpp / llama-cpp-python (Bare metal)
|
|
286
|
+
// ==========================================================================
|
|
287
|
+
/llama_cpp/,
|
|
288
|
+
/Llama\(/,
|
|
289
|
+
/llama\.generate/,
|
|
290
|
+
/llama\.create_completion/,
|
|
291
|
+
/llama\.create_chat_completion/,
|
|
292
|
+
/from llama_cpp import/,
|
|
293
|
+
/LlamaCpp\(/,
|
|
294
|
+
|
|
295
|
+
// ==========================================================================
|
|
296
|
+
// Transformers / HuggingFace (Bare metal)
|
|
297
|
+
// ==========================================================================
|
|
298
|
+
/pipeline\("text-generation"/,
|
|
299
|
+
/pipeline\('text-generation'/,
|
|
300
|
+
/AutoModelForCausalLM/,
|
|
301
|
+
/AutoModelForSeq2SeqLM/,
|
|
302
|
+
/\.generate\(input_ids/,
|
|
303
|
+
/transformers\.pipeline/,
|
|
304
|
+
/model\.generate\(/,
|
|
305
|
+
|
|
306
|
+
// ==========================================================================
|
|
307
|
+
// GGUF / GGML models
|
|
308
|
+
// ==========================================================================
|
|
309
|
+
/\.gguf/,
|
|
310
|
+
/\.ggml/,
|
|
311
|
+
/ctransformers/,
|
|
312
|
+
/CTransformers\(/,
|
|
313
|
+
|
|
314
|
+
// ==========================================================================
|
|
315
|
+
// ExLlama / ExLlamaV2 (Bare metal, GPU optimized)
|
|
316
|
+
// ==========================================================================
|
|
317
|
+
/exllama/,
|
|
318
|
+
/ExLlama/,
|
|
319
|
+
/exllamav2/,
|
|
320
|
+
/ExLlamaV2/,
|
|
321
|
+
|
|
322
|
+
// ==========================================================================
|
|
323
|
+
// Generic patterns (conservative - only match with LLM context)
|
|
324
|
+
// ==========================================================================
|
|
325
|
+
// NOTE: Removed overly generic patterns that cause false positives:
|
|
326
|
+
// - /\.invoke\(/ - too generic, matches any invoke method
|
|
327
|
+
// - /\.generate\(/ - too generic, matches generators, UUIDs, etc.
|
|
328
|
+
// - /\.chat\(/ - too generic, matches any chat method
|
|
329
|
+
// - /llm\./i - case-insensitive, matches "film.", variable names
|
|
330
|
+
// - /\.llm\(/ - too generic without context
|
|
331
|
+
// - /LLM\(/ - only keep if clearly a class instantiation
|
|
332
|
+
/\.ainvoke\(/, // LangChain async invoke - specific enough
|
|
333
|
+
/\.complete\(/, // Usually LLM-specific
|
|
334
|
+
/ChatModel\(/, // Usually LLM-specific class
|
|
335
|
+
/completion_tokens/, // OpenAI response field
|
|
336
|
+
/prompt_tokens/, // OpenAI response field
|
|
337
|
+
];
|
|
338
|
+
|
|
339
|
+
// =============================================================================
|
|
340
|
+
// HELPERS
|
|
341
|
+
// =============================================================================
|
|
342
|
+
|
|
343
|
+
function detectLanguage(filePath: string): string {
|
|
344
|
+
const ext = extname(filePath).toLowerCase();
|
|
345
|
+
return LANGUAGE_MAP[ext] || 'unknown';
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function countLines(content: string): number {
|
|
349
|
+
if (!content) return 0;
|
|
350
|
+
return content.split('\n').filter(line => line.trim().length > 0).length;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function loadGitignore(root: string, skipOptimization = true): IgnoreInstance {
|
|
354
|
+
const ig = ignore();
|
|
355
|
+
|
|
356
|
+
// Add default ignores
|
|
357
|
+
ig.add(DEFAULT_IGNORES);
|
|
358
|
+
|
|
359
|
+
// v1.9.5: Add cost optimization skip patterns
|
|
360
|
+
if (skipOptimization) {
|
|
361
|
+
ig.add(SKIP_PATTERNS);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Load .gitignore if exists
|
|
365
|
+
const gitignorePath = join(root, '.gitignore');
|
|
366
|
+
if (existsSync(gitignorePath)) {
|
|
367
|
+
try {
|
|
368
|
+
const content = readFileSync(gitignorePath, 'utf-8');
|
|
369
|
+
ig.add(content);
|
|
370
|
+
} catch {
|
|
371
|
+
// Ignore errors reading .gitignore
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
return ig;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function findCandidatesInContent(filePath: string, content: string): ScanCandidate[] {
|
|
379
|
+
const candidates: ScanCandidate[] = [];
|
|
380
|
+
const lines = content.split('\n');
|
|
381
|
+
|
|
382
|
+
for (let i = 0; i < lines.length; i++) {
|
|
383
|
+
const line = lines[i];
|
|
384
|
+
|
|
385
|
+
for (const pattern of INFERENCE_PATTERNS) {
|
|
386
|
+
if (pattern.test(line)) {
|
|
387
|
+
candidates.push({
|
|
388
|
+
file: filePath,
|
|
389
|
+
line: i + 1, // 1-indexed
|
|
390
|
+
snippet: line.trim().slice(0, 100), // First 100 chars
|
|
391
|
+
});
|
|
392
|
+
break; // Only one candidate per line
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
return candidates;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// =============================================================================
|
|
401
|
+
// PUBLIC API
|
|
402
|
+
// =============================================================================
|
|
403
|
+
|
|
404
|
+
export interface ScanOptions {
|
|
405
|
+
/** Skip test files, configs, etc. for cost optimization (default: true) */
|
|
406
|
+
skipOptimization?: boolean;
|
|
407
|
+
/** Maximum file size in bytes (default: 50KB) */
|
|
408
|
+
maxFileSize?: number;
|
|
409
|
+
/** Include files even if no inference patterns found */
|
|
410
|
+
includeAll?: boolean;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export async function scan(root: string, options: ScanOptions = {}): Promise<ScanResult> {
|
|
414
|
+
const {
|
|
415
|
+
skipOptimization = true,
|
|
416
|
+
maxFileSize = MAX_FILE_SIZE,
|
|
417
|
+
includeAll = false,
|
|
418
|
+
} = options;
|
|
419
|
+
|
|
420
|
+
// Validate directory exists
|
|
421
|
+
if (!existsSync(root)) {
|
|
422
|
+
throw new Error(`Directory not found: ${root}`);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const stat = statSync(root);
|
|
426
|
+
if (!stat.isDirectory()) {
|
|
427
|
+
throw new Error(`Not a directory: ${root}`);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const ig = loadGitignore(root, skipOptimization);
|
|
431
|
+
|
|
432
|
+
// Build glob pattern for code files
|
|
433
|
+
const patterns = CODE_EXTENSIONS.map(ext => `**/*${ext}`);
|
|
434
|
+
|
|
435
|
+
// Find all matching files
|
|
436
|
+
const absolutePaths = await glob(patterns, {
|
|
437
|
+
cwd: root,
|
|
438
|
+
absolute: true,
|
|
439
|
+
nodir: true,
|
|
440
|
+
dot: false,
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
// Process files
|
|
444
|
+
const files: ScanResult['files'] = [];
|
|
445
|
+
const candidates: ScanCandidate[] = [];
|
|
446
|
+
const languageSet = new Set<string>();
|
|
447
|
+
|
|
448
|
+
let skippedLargeFiles = 0;
|
|
449
|
+
let skippedByPattern = 0;
|
|
450
|
+
|
|
451
|
+
for (const absPath of absolutePaths) {
|
|
452
|
+
const relPath = relative(root, absPath);
|
|
453
|
+
|
|
454
|
+
// Check against ignore patterns
|
|
455
|
+
if (ig.ignores(relPath)) {
|
|
456
|
+
skippedByPattern++;
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
try {
|
|
461
|
+
// v1.9.5: Check file size before reading
|
|
462
|
+
const fileStat = statSync(absPath);
|
|
463
|
+
if (fileStat.size > maxFileSize) {
|
|
464
|
+
skippedLargeFiles++;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const content = readFileSync(absPath, 'utf-8');
|
|
469
|
+
const language = detectLanguage(absPath);
|
|
470
|
+
const loc = countLines(content);
|
|
471
|
+
|
|
472
|
+
// Find inference call candidates
|
|
473
|
+
const fileCandidates = findCandidatesInContent(relPath, content);
|
|
474
|
+
|
|
475
|
+
// v1.9.5: Only include files with candidates (unless includeAll)
|
|
476
|
+
if (includeAll || fileCandidates.length > 0) {
|
|
477
|
+
files.push({
|
|
478
|
+
path: relPath,
|
|
479
|
+
language,
|
|
480
|
+
loc,
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
languageSet.add(language);
|
|
484
|
+
candidates.push(...fileCandidates);
|
|
485
|
+
}
|
|
486
|
+
} catch {
|
|
487
|
+
// Skip files that can't be read
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// Calculate summary
|
|
492
|
+
const totalLoc = files.reduce((sum, f) => sum + f.loc, 0);
|
|
493
|
+
|
|
494
|
+
return {
|
|
495
|
+
root,
|
|
496
|
+
files,
|
|
497
|
+
candidates,
|
|
498
|
+
summary: {
|
|
499
|
+
totalFiles: files.length,
|
|
500
|
+
totalLoc,
|
|
501
|
+
languages: Array.from(languageSet).sort(),
|
|
502
|
+
totalCandidates: candidates.length,
|
|
503
|
+
// v1.9.5: Cost optimization stats
|
|
504
|
+
skippedLargeFiles,
|
|
505
|
+
skippedByPattern,
|
|
506
|
+
},
|
|
507
|
+
};
|
|
508
|
+
}
|