@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.
Files changed (367) hide show
  1. package/.claude/settings.local.json +8 -0
  2. package/.env.example +6 -0
  3. package/.github/workflows/peakinfer.yml +64 -0
  4. package/CHANGELOG.md +31 -0
  5. package/LICENSE +190 -0
  6. package/README.md +335 -0
  7. package/data/inferencemax.json +274 -0
  8. package/dist/agent-analyzer.d.ts +45 -0
  9. package/dist/agent-analyzer.d.ts.map +1 -0
  10. package/dist/agent-analyzer.js +374 -0
  11. package/dist/agent-analyzer.js.map +1 -0
  12. package/dist/agent.d.ts +76 -0
  13. package/dist/agent.d.ts.map +1 -0
  14. package/dist/agent.js +965 -0
  15. package/dist/agent.js.map +1 -0
  16. package/dist/agents/correlation-analyzer.d.ts +34 -0
  17. package/dist/agents/correlation-analyzer.d.ts.map +1 -0
  18. package/dist/agents/correlation-analyzer.js +261 -0
  19. package/dist/agents/correlation-analyzer.js.map +1 -0
  20. package/dist/agents/index.d.ts +91 -0
  21. package/dist/agents/index.d.ts.map +1 -0
  22. package/dist/agents/index.js +111 -0
  23. package/dist/agents/index.js.map +1 -0
  24. package/dist/agents/runtime-analyzer.d.ts +38 -0
  25. package/dist/agents/runtime-analyzer.d.ts.map +1 -0
  26. package/dist/agents/runtime-analyzer.js +244 -0
  27. package/dist/agents/runtime-analyzer.js.map +1 -0
  28. package/dist/analysis-types.d.ts +500 -0
  29. package/dist/analysis-types.d.ts.map +1 -0
  30. package/dist/analysis-types.js +11 -0
  31. package/dist/analysis-types.js.map +1 -0
  32. package/dist/analytics.d.ts +25 -0
  33. package/dist/analytics.d.ts.map +1 -0
  34. package/dist/analytics.js +94 -0
  35. package/dist/analytics.js.map +1 -0
  36. package/dist/analyzer.d.ts +48 -0
  37. package/dist/analyzer.d.ts.map +1 -0
  38. package/dist/analyzer.js +547 -0
  39. package/dist/analyzer.js.map +1 -0
  40. package/dist/artifacts.d.ts +44 -0
  41. package/dist/artifacts.d.ts.map +1 -0
  42. package/dist/artifacts.js +165 -0
  43. package/dist/artifacts.js.map +1 -0
  44. package/dist/benchmarks/index.d.ts +88 -0
  45. package/dist/benchmarks/index.d.ts.map +1 -0
  46. package/dist/benchmarks/index.js +205 -0
  47. package/dist/benchmarks/index.js.map +1 -0
  48. package/dist/cli.d.ts +3 -0
  49. package/dist/cli.d.ts.map +1 -0
  50. package/dist/cli.js +427 -0
  51. package/dist/cli.js.map +1 -0
  52. package/dist/commands/ci.d.ts +19 -0
  53. package/dist/commands/ci.d.ts.map +1 -0
  54. package/dist/commands/ci.js +253 -0
  55. package/dist/commands/ci.js.map +1 -0
  56. package/dist/commands/config.d.ts +16 -0
  57. package/dist/commands/config.d.ts.map +1 -0
  58. package/dist/commands/config.js +249 -0
  59. package/dist/commands/config.js.map +1 -0
  60. package/dist/commands/demo.d.ts +15 -0
  61. package/dist/commands/demo.d.ts.map +1 -0
  62. package/dist/commands/demo.js +106 -0
  63. package/dist/commands/demo.js.map +1 -0
  64. package/dist/commands/export.d.ts +14 -0
  65. package/dist/commands/export.d.ts.map +1 -0
  66. package/dist/commands/export.js +209 -0
  67. package/dist/commands/export.js.map +1 -0
  68. package/dist/commands/history.d.ts +15 -0
  69. package/dist/commands/history.d.ts.map +1 -0
  70. package/dist/commands/history.js +389 -0
  71. package/dist/commands/history.js.map +1 -0
  72. package/dist/commands/template.d.ts +14 -0
  73. package/dist/commands/template.d.ts.map +1 -0
  74. package/dist/commands/template.js +341 -0
  75. package/dist/commands/template.js.map +1 -0
  76. package/dist/commands/validate-map.d.ts +12 -0
  77. package/dist/commands/validate-map.d.ts.map +1 -0
  78. package/dist/commands/validate-map.js +274 -0
  79. package/dist/commands/validate-map.js.map +1 -0
  80. package/dist/commands/whatif.d.ts +17 -0
  81. package/dist/commands/whatif.d.ts.map +1 -0
  82. package/dist/commands/whatif.js +206 -0
  83. package/dist/commands/whatif.js.map +1 -0
  84. package/dist/comparison.d.ts +38 -0
  85. package/dist/comparison.d.ts.map +1 -0
  86. package/dist/comparison.js +223 -0
  87. package/dist/comparison.js.map +1 -0
  88. package/dist/config.d.ts +42 -0
  89. package/dist/config.d.ts.map +1 -0
  90. package/dist/config.js +158 -0
  91. package/dist/config.js.map +1 -0
  92. package/dist/connectors/helicone.d.ts +9 -0
  93. package/dist/connectors/helicone.d.ts.map +1 -0
  94. package/dist/connectors/helicone.js +106 -0
  95. package/dist/connectors/helicone.js.map +1 -0
  96. package/dist/connectors/index.d.ts +37 -0
  97. package/dist/connectors/index.d.ts.map +1 -0
  98. package/dist/connectors/index.js +65 -0
  99. package/dist/connectors/index.js.map +1 -0
  100. package/dist/connectors/langsmith.d.ts +9 -0
  101. package/dist/connectors/langsmith.d.ts.map +1 -0
  102. package/dist/connectors/langsmith.js +122 -0
  103. package/dist/connectors/langsmith.js.map +1 -0
  104. package/dist/connectors/types.d.ts +83 -0
  105. package/dist/connectors/types.d.ts.map +1 -0
  106. package/dist/connectors/types.js +98 -0
  107. package/dist/connectors/types.js.map +1 -0
  108. package/dist/cost-estimator.d.ts +46 -0
  109. package/dist/cost-estimator.d.ts.map +1 -0
  110. package/dist/cost-estimator.js +104 -0
  111. package/dist/cost-estimator.js.map +1 -0
  112. package/dist/costs.d.ts +57 -0
  113. package/dist/costs.d.ts.map +1 -0
  114. package/dist/costs.js +251 -0
  115. package/dist/costs.js.map +1 -0
  116. package/dist/counterfactuals.d.ts +29 -0
  117. package/dist/counterfactuals.d.ts.map +1 -0
  118. package/dist/counterfactuals.js +448 -0
  119. package/dist/counterfactuals.js.map +1 -0
  120. package/dist/enhancement-prompts.d.ts +41 -0
  121. package/dist/enhancement-prompts.d.ts.map +1 -0
  122. package/dist/enhancement-prompts.js +88 -0
  123. package/dist/enhancement-prompts.js.map +1 -0
  124. package/dist/envelopes.d.ts +20 -0
  125. package/dist/envelopes.d.ts.map +1 -0
  126. package/dist/envelopes.js +790 -0
  127. package/dist/envelopes.js.map +1 -0
  128. package/dist/format-normalizer.d.ts +71 -0
  129. package/dist/format-normalizer.d.ts.map +1 -0
  130. package/dist/format-normalizer.js +1331 -0
  131. package/dist/format-normalizer.js.map +1 -0
  132. package/dist/history.d.ts +79 -0
  133. package/dist/history.d.ts.map +1 -0
  134. package/dist/history.js +313 -0
  135. package/dist/history.js.map +1 -0
  136. package/dist/html.d.ts +11 -0
  137. package/dist/html.d.ts.map +1 -0
  138. package/dist/html.js +463 -0
  139. package/dist/html.js.map +1 -0
  140. package/dist/impact.d.ts +42 -0
  141. package/dist/impact.d.ts.map +1 -0
  142. package/dist/impact.js +443 -0
  143. package/dist/impact.js.map +1 -0
  144. package/dist/index.d.ts +26 -0
  145. package/dist/index.d.ts.map +1 -0
  146. package/dist/index.js +34 -0
  147. package/dist/index.js.map +1 -0
  148. package/dist/insights.d.ts +5 -0
  149. package/dist/insights.d.ts.map +1 -0
  150. package/dist/insights.js +271 -0
  151. package/dist/insights.js.map +1 -0
  152. package/dist/joiner.d.ts +9 -0
  153. package/dist/joiner.d.ts.map +1 -0
  154. package/dist/joiner.js +247 -0
  155. package/dist/joiner.js.map +1 -0
  156. package/dist/orchestrator.d.ts +34 -0
  157. package/dist/orchestrator.d.ts.map +1 -0
  158. package/dist/orchestrator.js +827 -0
  159. package/dist/orchestrator.js.map +1 -0
  160. package/dist/pdf.d.ts +26 -0
  161. package/dist/pdf.d.ts.map +1 -0
  162. package/dist/pdf.js +84 -0
  163. package/dist/pdf.js.map +1 -0
  164. package/dist/prediction.d.ts +33 -0
  165. package/dist/prediction.d.ts.map +1 -0
  166. package/dist/prediction.js +316 -0
  167. package/dist/prediction.js.map +1 -0
  168. package/dist/prompts/loader.d.ts +38 -0
  169. package/dist/prompts/loader.d.ts.map +1 -0
  170. package/dist/prompts/loader.js +60 -0
  171. package/dist/prompts/loader.js.map +1 -0
  172. package/dist/renderer.d.ts +64 -0
  173. package/dist/renderer.d.ts.map +1 -0
  174. package/dist/renderer.js +923 -0
  175. package/dist/renderer.js.map +1 -0
  176. package/dist/runid.d.ts +57 -0
  177. package/dist/runid.d.ts.map +1 -0
  178. package/dist/runid.js +199 -0
  179. package/dist/runid.js.map +1 -0
  180. package/dist/runtime.d.ts +29 -0
  181. package/dist/runtime.d.ts.map +1 -0
  182. package/dist/runtime.js +366 -0
  183. package/dist/runtime.js.map +1 -0
  184. package/dist/scanner.d.ts +11 -0
  185. package/dist/scanner.d.ts.map +1 -0
  186. package/dist/scanner.js +426 -0
  187. package/dist/scanner.js.map +1 -0
  188. package/dist/templates.d.ts +120 -0
  189. package/dist/templates.d.ts.map +1 -0
  190. package/dist/templates.js +429 -0
  191. package/dist/templates.js.map +1 -0
  192. package/dist/tools/index.d.ts +153 -0
  193. package/dist/tools/index.d.ts.map +1 -0
  194. package/dist/tools/index.js +177 -0
  195. package/dist/tools/index.js.map +1 -0
  196. package/dist/types.d.ts +3647 -0
  197. package/dist/types.d.ts.map +1 -0
  198. package/dist/types.js +703 -0
  199. package/dist/types.js.map +1 -0
  200. package/dist/version.d.ts +7 -0
  201. package/dist/version.d.ts.map +1 -0
  202. package/dist/version.js +23 -0
  203. package/dist/version.js.map +1 -0
  204. package/docs/demo-guide.md +423 -0
  205. package/docs/events-format.md +295 -0
  206. package/docs/inferencemap-spec.md +344 -0
  207. package/docs/migration-v2.md +293 -0
  208. package/fixtures/demo/precomputed.json +142 -0
  209. package/fixtures/demo-project/README.md +52 -0
  210. package/fixtures/demo-project/ai-service.ts +65 -0
  211. package/fixtures/demo-project/sample-events.jsonl +15 -0
  212. package/fixtures/demo-project/src/ai-service.ts +128 -0
  213. package/fixtures/demo-project/src/llm-client.ts +155 -0
  214. package/package.json +65 -0
  215. package/prompts/agent-analyzer.yaml +47 -0
  216. package/prompts/ci-gate.yaml +98 -0
  217. package/prompts/correlation-analyzer.yaml +178 -0
  218. package/prompts/format-normalizer.yaml +46 -0
  219. package/prompts/peak-performance.yaml +180 -0
  220. package/prompts/pr-comment.yaml +111 -0
  221. package/prompts/runtime-analyzer.yaml +189 -0
  222. package/prompts/unified-analyzer.yaml +241 -0
  223. package/schemas/inference-map.v0.1.json +215 -0
  224. package/scripts/benchmark.ts +394 -0
  225. package/scripts/demo-v1.5.sh +158 -0
  226. package/scripts/sync-from-site.sh +197 -0
  227. package/scripts/validate-sync.sh +178 -0
  228. package/src/agent-analyzer.ts +481 -0
  229. package/src/agent.ts +1232 -0
  230. package/src/agents/correlation-analyzer.ts +353 -0
  231. package/src/agents/index.ts +235 -0
  232. package/src/agents/runtime-analyzer.ts +343 -0
  233. package/src/analysis-types.ts +558 -0
  234. package/src/analytics.ts +100 -0
  235. package/src/analyzer.ts +692 -0
  236. package/src/artifacts.ts +218 -0
  237. package/src/benchmarks/index.ts +309 -0
  238. package/src/cli.ts +503 -0
  239. package/src/commands/ci.ts +336 -0
  240. package/src/commands/config.ts +288 -0
  241. package/src/commands/demo.ts +175 -0
  242. package/src/commands/export.ts +297 -0
  243. package/src/commands/history.ts +425 -0
  244. package/src/commands/template.ts +385 -0
  245. package/src/commands/validate-map.ts +324 -0
  246. package/src/commands/whatif.ts +272 -0
  247. package/src/comparison.ts +283 -0
  248. package/src/config.ts +188 -0
  249. package/src/connectors/helicone.ts +164 -0
  250. package/src/connectors/index.ts +93 -0
  251. package/src/connectors/langsmith.ts +179 -0
  252. package/src/connectors/types.ts +180 -0
  253. package/src/cost-estimator.ts +146 -0
  254. package/src/costs.ts +347 -0
  255. package/src/counterfactuals.ts +516 -0
  256. package/src/enhancement-prompts.ts +118 -0
  257. package/src/envelopes.ts +814 -0
  258. package/src/format-normalizer.ts +1486 -0
  259. package/src/history.ts +400 -0
  260. package/src/html.ts +512 -0
  261. package/src/impact.ts +522 -0
  262. package/src/index.ts +83 -0
  263. package/src/insights.ts +341 -0
  264. package/src/joiner.ts +289 -0
  265. package/src/orchestrator.ts +1015 -0
  266. package/src/pdf.ts +110 -0
  267. package/src/prediction.ts +392 -0
  268. package/src/prompts/loader.ts +88 -0
  269. package/src/renderer.ts +1045 -0
  270. package/src/runid.ts +261 -0
  271. package/src/runtime.ts +450 -0
  272. package/src/scanner.ts +508 -0
  273. package/src/templates.ts +561 -0
  274. package/src/tools/index.ts +214 -0
  275. package/src/types.ts +873 -0
  276. package/src/version.ts +24 -0
  277. package/templates/context-accumulation.yaml +23 -0
  278. package/templates/cost-concentration.yaml +20 -0
  279. package/templates/dead-code.yaml +20 -0
  280. package/templates/latency-explainer.yaml +23 -0
  281. package/templates/optimizations/ab-testing-framework.yaml +74 -0
  282. package/templates/optimizations/api-gateway-optimization.yaml +81 -0
  283. package/templates/optimizations/api-model-routing-strategy.yaml +126 -0
  284. package/templates/optimizations/auto-scaling-optimization.yaml +85 -0
  285. package/templates/optimizations/batch-utilization-diagnostic.yaml +142 -0
  286. package/templates/optimizations/comprehensive-apm.yaml +76 -0
  287. package/templates/optimizations/context-window-optimization.yaml +91 -0
  288. package/templates/optimizations/cost-sensitive-batch-processing.yaml +77 -0
  289. package/templates/optimizations/distributed-training-optimization.yaml +77 -0
  290. package/templates/optimizations/document-analysis-edge.yaml +77 -0
  291. package/templates/optimizations/document-pipeline-optimization.yaml +78 -0
  292. package/templates/optimizations/domain-specific-distillation.yaml +78 -0
  293. package/templates/optimizations/error-handling-optimization.yaml +76 -0
  294. package/templates/optimizations/gptq-4bit-quantization.yaml +96 -0
  295. package/templates/optimizations/long-context-memory-management.yaml +78 -0
  296. package/templates/optimizations/max-tokens-optimization.yaml +76 -0
  297. package/templates/optimizations/memory-bandwidth-optimization.yaml +73 -0
  298. package/templates/optimizations/multi-framework-resilience.yaml +75 -0
  299. package/templates/optimizations/multi-tenant-optimization.yaml +75 -0
  300. package/templates/optimizations/prompt-caching-optimization.yaml +143 -0
  301. package/templates/optimizations/pytorch-to-onnx-migration.yaml +109 -0
  302. package/templates/optimizations/quality-monitoring.yaml +74 -0
  303. package/templates/optimizations/realtime-budget-controls.yaml +74 -0
  304. package/templates/optimizations/realtime-latency-optimization.yaml +74 -0
  305. package/templates/optimizations/sglang-concurrency-optimization.yaml +78 -0
  306. package/templates/optimizations/smart-model-routing.yaml +96 -0
  307. package/templates/optimizations/streaming-batch-selection.yaml +167 -0
  308. package/templates/optimizations/system-prompt-optimization.yaml +75 -0
  309. package/templates/optimizations/tensorrt-llm-performance.yaml +77 -0
  310. package/templates/optimizations/vllm-high-throughput-optimization.yaml +93 -0
  311. package/templates/optimizations/vllm-migration-memory-bound.yaml +78 -0
  312. package/templates/overpowered-extraction.yaml +32 -0
  313. package/templates/overpowered-model.yaml +31 -0
  314. package/templates/prompt-bloat.yaml +24 -0
  315. package/templates/retry-explosion.yaml +28 -0
  316. package/templates/schema/insight.schema.json +113 -0
  317. package/templates/schema/optimization.schema.json +180 -0
  318. package/templates/streaming-drift.yaml +30 -0
  319. package/templates/throughput-gap.yaml +21 -0
  320. package/templates/token-underutilization.yaml +28 -0
  321. package/templates/untested-fallback.yaml +21 -0
  322. package/tests/accuracy/drift-detection.test.ts +184 -0
  323. package/tests/accuracy/false-positives.test.ts +166 -0
  324. package/tests/accuracy/templates.test.ts +205 -0
  325. package/tests/action/commands.test.ts +125 -0
  326. package/tests/action/comments.test.ts +347 -0
  327. package/tests/cli.test.ts +203 -0
  328. package/tests/comparison.test.ts +309 -0
  329. package/tests/correlation-analyzer.test.ts +534 -0
  330. package/tests/counterfactuals.test.ts +347 -0
  331. package/tests/fixtures/events/missing-id.jsonl +1 -0
  332. package/tests/fixtures/events/missing-input.jsonl +1 -0
  333. package/tests/fixtures/events/missing-latency.jsonl +1 -0
  334. package/tests/fixtures/events/missing-model.jsonl +1 -0
  335. package/tests/fixtures/events/missing-output.jsonl +1 -0
  336. package/tests/fixtures/events/missing-provider.jsonl +1 -0
  337. package/tests/fixtures/events/missing-ts.jsonl +1 -0
  338. package/tests/fixtures/events/valid.csv +3 -0
  339. package/tests/fixtures/events/valid.json +1 -0
  340. package/tests/fixtures/events/valid.jsonl +2 -0
  341. package/tests/fixtures/events/with-callsite.jsonl +1 -0
  342. package/tests/fixtures/events/with-intent.jsonl +1 -0
  343. package/tests/fixtures/events/wrong-type.jsonl +1 -0
  344. package/tests/fixtures/repos/empty/.gitkeep +0 -0
  345. package/tests/fixtures/repos/hybrid-router/router.py +35 -0
  346. package/tests/fixtures/repos/saas-anthropic/agent.ts +27 -0
  347. package/tests/fixtures/repos/saas-openai/assistant.js +33 -0
  348. package/tests/fixtures/repos/saas-openai/client.py +26 -0
  349. package/tests/fixtures/repos/self-hosted-vllm/inference.py +22 -0
  350. package/tests/github-action.test.ts +292 -0
  351. package/tests/insights.test.ts +878 -0
  352. package/tests/joiner.test.ts +168 -0
  353. package/tests/performance/action-latency.test.ts +132 -0
  354. package/tests/performance/benchmark.test.ts +189 -0
  355. package/tests/performance/cli-latency.test.ts +102 -0
  356. package/tests/pr-comment.test.ts +313 -0
  357. package/tests/prediction.test.ts +296 -0
  358. package/tests/runtime-analyzer.test.ts +375 -0
  359. package/tests/runtime.test.ts +205 -0
  360. package/tests/scanner.test.ts +122 -0
  361. package/tests/template-conformance.test.ts +526 -0
  362. package/tests/unit/cost-calculator.test.ts +303 -0
  363. package/tests/unit/credits.test.ts +180 -0
  364. package/tests/unit/inference-map.test.ts +276 -0
  365. package/tests/unit/schema.test.ts +300 -0
  366. package/tsconfig.json +20 -0
  367. package/vitest.config.ts +14 -0
@@ -0,0 +1,558 @@
1
+ /**
2
+ * =============================================================================
3
+ * SYNC NOTE: This file is copied from peakinfer-site (SOURCE OF TRUTH)
4
+ * Source: peakinfer-site/lib/agents/types.ts
5
+ *
6
+ * DO NOT MODIFY THIS FILE DIRECTLY IN THE CLI REPO.
7
+ * All changes must be made in peakinfer-site first, then synced here.
8
+ * =============================================================================
9
+ */
10
+
11
+ /**
12
+ * Shared Types for PeakInfer Agents
13
+ * Mirrors CLI types for consistency
14
+ */
15
+
16
+ // =============================================================================
17
+ // INFERENCE POINT TYPES
18
+ // =============================================================================
19
+
20
+ export interface InferencePoint {
21
+ id: string;
22
+ line: number;
23
+ column: number;
24
+ function_context: string;
25
+ class_context: string | null;
26
+ call_expression: string;
27
+ call_type: 'direct' | 'wrapper' | 'framework' | 'http';
28
+ provider: {
29
+ value: string;
30
+ source: 'hardcoded' | 'env' | 'config' | 'parameter' | 'dynamic' | 'unknown';
31
+ confidence: number;
32
+ };
33
+ model: {
34
+ value: string | null;
35
+ source: 'hardcoded' | 'env' | 'config' | 'parameter' | 'dynamic' | 'unknown';
36
+ confidence: number;
37
+ };
38
+ is_async: boolean;
39
+ in_loop: boolean;
40
+ loop_type: 'for' | 'while' | 'map' | 'recursive' | 'none';
41
+ estimated_calls: 'single' | 'multiple' | 'unbounded';
42
+ needs_tracing: boolean;
43
+ confidence: number;
44
+ }
45
+
46
+ // =============================================================================
47
+ // IMPORT ANALYZER TYPES
48
+ // =============================================================================
49
+
50
+ export interface SDK {
51
+ name: string;
52
+ provider: string;
53
+ import_line: number;
54
+ alias: string | null;
55
+ confidence: number;
56
+ }
57
+
58
+ export interface Framework {
59
+ name: string;
60
+ import_line: number;
61
+ components: string[];
62
+ confidence: number;
63
+ }
64
+
65
+ export interface CustomWrapper {
66
+ name: string;
67
+ import_path: string;
68
+ likely_purpose: string;
69
+ needs_tracing: boolean;
70
+ confidence: number;
71
+ }
72
+
73
+ export interface Infrastructure {
74
+ name: string;
75
+ type: string;
76
+ import_line: number;
77
+ }
78
+
79
+ export interface ImportAnalyzerOutput {
80
+ sdks: SDK[];
81
+ frameworks: Framework[];
82
+ custom_wrappers: CustomWrapper[];
83
+ infrastructure: Infrastructure[];
84
+ summary: {
85
+ has_llm_usage: boolean;
86
+ primary_provider: string | null;
87
+ framework: string | null;
88
+ complexity: 'simple' | 'moderate' | 'complex';
89
+ };
90
+ }
91
+
92
+ // =============================================================================
93
+ // CALLSITE FINDER TYPES
94
+ // =============================================================================
95
+
96
+ export interface WrapperDefinition {
97
+ name: string;
98
+ line: number;
99
+ wraps_provider: string | null;
100
+ wraps_model: string | null;
101
+ is_llm_wrapper: boolean;
102
+ confidence: number;
103
+ }
104
+
105
+ export interface CallSiteFinderOutput {
106
+ inference_points: InferencePoint[];
107
+ wrapper_definitions: WrapperDefinition[];
108
+ summary: {
109
+ total_inference_points: number;
110
+ direct_calls: number;
111
+ wrapped_calls: number;
112
+ framework_calls: number;
113
+ providers_detected: string[];
114
+ models_detected: string[];
115
+ has_dynamic_routing: boolean;
116
+ };
117
+ }
118
+
119
+ // =============================================================================
120
+ // COST ANALYZER TYPES
121
+ // =============================================================================
122
+
123
+ export interface CostOptimization {
124
+ type: 'model_downgrade' | 'reduce_tokens' | 'add_caching' | 'batch_requests' | 'limit_context';
125
+ description: string;
126
+ current_cost: string;
127
+ optimized_cost: string;
128
+ savings_percent: number;
129
+ effort: 'low' | 'medium' | 'high';
130
+ sample_change: string | null;
131
+ }
132
+
133
+ export interface CostProfile {
134
+ inference_point_id: string;
135
+ line: number;
136
+ model_analysis: {
137
+ model: string;
138
+ tier: 'premium' | 'standard' | 'budget' | 'unknown';
139
+ pricing: {
140
+ input_per_1m: number;
141
+ output_per_1m: number;
142
+ };
143
+ is_overqualified: boolean;
144
+ reason: string | null;
145
+ };
146
+ token_estimates: {
147
+ input: { min: number; typical: number; max: number; basis: string };
148
+ output: { min: number; typical: number; max: number; basis: string };
149
+ has_few_shot: boolean;
150
+ few_shot_tokens: number;
151
+ has_rag_context: boolean;
152
+ rag_context_estimate: number;
153
+ };
154
+ call_frequency: {
155
+ pattern: 'single' | 'per_request' | 'loop' | 'recursive' | 'batch';
156
+ multiplier: number | null;
157
+ loop_bound: 'bounded' | 'unbounded' | 'unknown';
158
+ estimated_calls_per_invocation: number;
159
+ };
160
+ cost_estimate: {
161
+ per_call_min: number;
162
+ per_call_typical: number;
163
+ per_call_max: number;
164
+ currency: string;
165
+ };
166
+ cost_risk: {
167
+ level: 'low' | 'medium' | 'high' | 'critical';
168
+ factors: string[];
169
+ unbounded_growth: boolean;
170
+ context_accumulation: boolean;
171
+ };
172
+ optimizations: CostOptimization[];
173
+ confidence: number;
174
+ }
175
+
176
+ // =============================================================================
177
+ // LATENCY ANALYZER TYPES
178
+ // =============================================================================
179
+
180
+ export interface LatencyOptimization {
181
+ type: 'add_streaming' | 'parallelize' | 'add_async' | 'add_timeout' | 'reduce_chain';
182
+ description: string;
183
+ current_latency: string;
184
+ optimized_latency: string;
185
+ improvement_percent: number;
186
+ effort: 'low' | 'medium' | 'high';
187
+ sample_change: string | null;
188
+ }
189
+
190
+ export interface LatencyProfile {
191
+ inference_point_id: string;
192
+ line: number;
193
+ blocking_analysis: {
194
+ is_blocking: boolean;
195
+ is_in_request_handler: boolean;
196
+ blocks_event_loop: boolean;
197
+ handler_type: 'http' | 'websocket' | 'grpc' | 'background' | 'cli' | 'unknown';
198
+ user_facing: boolean;
199
+ };
200
+ streaming_analysis: {
201
+ streaming_enabled: boolean;
202
+ should_enable_streaming: boolean;
203
+ reason: string;
204
+ time_to_first_token_benefit: string | null;
205
+ };
206
+ async_analysis: {
207
+ is_async: boolean;
208
+ uses_await: boolean;
209
+ could_be_async: boolean;
210
+ async_benefit: string | null;
211
+ };
212
+ parallel_analysis: {
213
+ has_parallel_potential: boolean;
214
+ independent_calls: number;
215
+ current_pattern: 'sequential' | 'parallel' | 'mixed';
216
+ parallelizable_calls: { line: number; reason: string }[];
217
+ parallel_speedup_estimate: string | null;
218
+ };
219
+ chain_analysis: {
220
+ chain_depth: number;
221
+ sequential_calls: number;
222
+ total_latency_estimate: {
223
+ min_ms: number;
224
+ typical_ms: number;
225
+ max_ms: number;
226
+ };
227
+ chain_pattern: 'single' | 'pipeline' | 'loop' | 'recursive' | 'agent';
228
+ };
229
+ timeout_analysis: {
230
+ timeout_configured: boolean;
231
+ timeout_value_ms: number | null;
232
+ has_fallback_on_timeout: boolean;
233
+ timeout_risk: 'none' | 'low' | 'medium' | 'high';
234
+ };
235
+ latency_risk: {
236
+ level: 'low' | 'medium' | 'high' | 'critical';
237
+ factors: string[];
238
+ tail_latency_risk: boolean;
239
+ unpredictable: boolean;
240
+ };
241
+ latency_estimate: {
242
+ min_ms: number;
243
+ typical_ms: number;
244
+ p95_ms: number;
245
+ max_ms: number;
246
+ basis: string;
247
+ };
248
+ optimizations: LatencyOptimization[];
249
+ confidence: number;
250
+ }
251
+
252
+ // =============================================================================
253
+ // THROUGHPUT ANALYZER TYPES
254
+ // =============================================================================
255
+
256
+ export interface ThroughputOptimization {
257
+ type: 'add_rate_limiter' | 'add_batching' | 'add_queue' | 'fix_bottleneck' | 'increase_concurrency';
258
+ description: string;
259
+ current_throughput: string;
260
+ optimized_throughput: string;
261
+ improvement: string;
262
+ effort: 'low' | 'medium' | 'high';
263
+ sample_change: string | null;
264
+ }
265
+
266
+ export interface ScalingBottleneck {
267
+ type: 'shared_state' | 'global_client' | 'file_lock' | 'db_connection' | 'memory';
268
+ location: string;
269
+ description: string;
270
+ severity: 'low' | 'medium' | 'high';
271
+ }
272
+
273
+ export interface ThroughputProfile {
274
+ inference_point_id: string;
275
+ line: number;
276
+ concurrency_analysis: {
277
+ concurrency_limit: number | null;
278
+ limit_source: 'semaphore' | 'pool' | 'rate_limiter' | 'none' | 'unknown';
279
+ limit_location: string | null;
280
+ is_global_limit: boolean;
281
+ recommended_limit: number | null;
282
+ };
283
+ rate_limiting: {
284
+ has_rate_limiter: boolean;
285
+ rate_limit_type: 'token_bucket' | 'sliding_window' | 'fixed_window' | 'none';
286
+ requests_per_minute: number | null;
287
+ handles_429: boolean;
288
+ backoff_strategy: 'none' | 'fixed' | 'exponential' | 'custom';
289
+ };
290
+ batching_analysis: {
291
+ batching_enabled: boolean;
292
+ batch_size: number | null;
293
+ could_batch: boolean;
294
+ batching_benefit: string | null;
295
+ batch_api_available: boolean;
296
+ };
297
+ queue_analysis: {
298
+ uses_queue: boolean;
299
+ queue_type: 'celery' | 'rq' | 'bull' | 'sqs' | 'redis' | 'none';
300
+ async_processing: boolean;
301
+ worker_pattern: boolean;
302
+ };
303
+ scaling_analysis: {
304
+ horizontally_scalable: boolean;
305
+ bottlenecks: ScalingBottleneck[];
306
+ stateless: boolean;
307
+ client_reuse: boolean;
308
+ };
309
+ capacity_estimate: {
310
+ max_concurrent_calls: number | null;
311
+ estimated_rps: number | null;
312
+ limiting_factor: string;
313
+ };
314
+ throughput_risk: {
315
+ level: 'low' | 'medium' | 'high' | 'critical';
316
+ factors: string[];
317
+ will_hit_rate_limits: boolean;
318
+ scaling_blocked: boolean;
319
+ };
320
+ optimizations: ThroughputOptimization[];
321
+ confidence: number;
322
+ }
323
+
324
+ // =============================================================================
325
+ // RELIABILITY ANALYZER TYPES
326
+ // =============================================================================
327
+
328
+ export interface ReliabilityOptimization {
329
+ type: 'add_retry' | 'add_fallback' | 'add_timeout' | 'add_circuit_breaker' | 'add_validation' | 'fix_antipattern';
330
+ description: string;
331
+ reliability_before: string;
332
+ reliability_after: string;
333
+ effort: 'low' | 'medium' | 'high';
334
+ priority: 'low' | 'medium' | 'high' | 'critical';
335
+ sample_change: string | null;
336
+ }
337
+
338
+ export interface AntiPattern {
339
+ pattern: string;
340
+ location: string;
341
+ severity: 'low' | 'medium' | 'high' | 'critical';
342
+ description: string;
343
+ }
344
+
345
+ export interface ReliabilityProfile {
346
+ inference_point_id: string;
347
+ line: number;
348
+ error_handling: {
349
+ has_try_catch: boolean;
350
+ caught_exceptions: string[];
351
+ specific_llm_errors: boolean;
352
+ error_logged: boolean;
353
+ error_propagated: boolean;
354
+ silent_failure: boolean;
355
+ user_friendly_error: boolean;
356
+ };
357
+ retry_strategy: {
358
+ has_retry: boolean;
359
+ retry_library: 'tenacity' | 'backoff' | 'custom' | 'none';
360
+ max_retries: number | null;
361
+ backoff_type: 'none' | 'fixed' | 'exponential' | 'custom';
362
+ initial_delay_ms: number | null;
363
+ max_delay_ms: number | null;
364
+ retry_on: string[];
365
+ jitter: boolean;
366
+ retry_budget_risk: 'none' | 'low' | 'medium' | 'high';
367
+ };
368
+ fallback_strategy: {
369
+ has_fallback: boolean;
370
+ fallback_type: 'model' | 'provider' | 'cached' | 'default' | 'none';
371
+ fallback_model: string | null;
372
+ fallback_provider: string | null;
373
+ graceful_degradation: boolean;
374
+ fallback_tested: 'unknown' | 'likely' | 'unlikely';
375
+ };
376
+ timeout_handling: {
377
+ timeout_configured: boolean;
378
+ timeout_ms: number | null;
379
+ timeout_source: 'client' | 'wrapper' | 'none';
380
+ on_timeout: 'retry' | 'fallback' | 'error' | 'none';
381
+ };
382
+ circuit_breaker: {
383
+ has_circuit_breaker: boolean;
384
+ library: string | null;
385
+ failure_threshold: number | null;
386
+ recovery_time_ms: number | null;
387
+ };
388
+ validation: {
389
+ validates_response: boolean;
390
+ validates_json: boolean;
391
+ validates_schema: boolean;
392
+ handles_empty_response: boolean;
393
+ handles_truncated: boolean;
394
+ };
395
+ reliability_risk: {
396
+ level: 'fragile' | 'moderate' | 'robust' | 'resilient';
397
+ factors: string[];
398
+ single_point_of_failure: boolean;
399
+ cascade_risk: boolean;
400
+ data_loss_risk: boolean;
401
+ };
402
+ anti_patterns: AntiPattern[];
403
+ optimizations: ReliabilityOptimization[];
404
+ confidence: number;
405
+ }
406
+
407
+ // =============================================================================
408
+ // ISSUE TYPE (for fixes)
409
+ // =============================================================================
410
+
411
+ export interface Issue {
412
+ type: string;
413
+ severity: 'critical' | 'warning' | 'info';
414
+ headline: string;
415
+ evidence: string;
416
+ originalCode: string;
417
+ suggestedFix: string | null;
418
+ aiAgentPrompt: string;
419
+ }
420
+
421
+ // =============================================================================
422
+ // PERFORMANCE PROFILE (Combined)
423
+ // =============================================================================
424
+
425
+ export interface PerformanceProfile {
426
+ inference_point_id: string;
427
+ line: number;
428
+ file: string;
429
+ provider: string;
430
+ model: string | null;
431
+ originalCode?: string;
432
+ issues?: Issue[];
433
+ cost: CostProfile | null;
434
+ latency: LatencyProfile | null;
435
+ throughput: ThroughputProfile | null;
436
+ reliability: ReliabilityProfile | null;
437
+ }
438
+
439
+ // =============================================================================
440
+ // INSIGHT TYPES
441
+ // =============================================================================
442
+
443
+ export interface Insight {
444
+ id?: string;
445
+ severity: 'critical' | 'warning' | 'info';
446
+ category: 'cost' | 'latency' | 'drift' | 'reliability' | 'waste' | 'throughput' | 'security' | 'best-practice';
447
+ templateId?: string;
448
+ headline: string;
449
+ evidence: string;
450
+ location?: string;
451
+ recommendation?: string;
452
+ source?: 'template' | 'llm';
453
+ impact?: {
454
+ layer: 'application' | 'model' | 'runtime' | 'infrastructure';
455
+ impactType: 'cost' | 'latency' | 'throughput';
456
+ estimatedImpactPercent: number;
457
+ effort: 'low' | 'medium' | 'high';
458
+ annualSavingsUSD?: number;
459
+ latencyReductionMs?: number;
460
+ throughputGainPercent?: number;
461
+ confidence?: number;
462
+ assumptions?: string;
463
+ };
464
+ }
465
+
466
+ // =============================================================================
467
+ // STATIC ANALYSIS OUTPUT
468
+ // =============================================================================
469
+
470
+ export interface StaticAnalysisOutput {
471
+ imports: ImportAnalyzerOutput[];
472
+ callsites: CallSiteFinderOutput[];
473
+ performance_profiles: PerformanceProfile[];
474
+ cost_analysis: Array<{
475
+ cost_profiles: CostProfile[];
476
+ summary: {
477
+ total_inference_points: number;
478
+ estimated_cost_per_1k_calls: number;
479
+ highest_cost_point: string | null;
480
+ optimization_potential_percent: number;
481
+ };
482
+ }>;
483
+ latency_analysis: Array<{
484
+ latency_profiles: LatencyProfile[];
485
+ summary: {
486
+ total_inference_points: number;
487
+ blocking_calls: number;
488
+ streaming_enabled: number;
489
+ parallelizable: number;
490
+ estimated_p95_ms: number;
491
+ };
492
+ }>;
493
+ throughput_analysis: Array<{
494
+ throughput_profiles: ThroughputProfile[];
495
+ summary: {
496
+ total_inference_points: number;
497
+ has_rate_limiting: number;
498
+ has_batching: number;
499
+ scaling_bottlenecks: number;
500
+ estimated_max_rps: number | null;
501
+ };
502
+ }>;
503
+ reliability_analysis: Array<{
504
+ reliability_profiles: ReliabilityProfile[];
505
+ summary: {
506
+ total_inference_points: number;
507
+ has_error_handling: number;
508
+ has_retry: number;
509
+ has_fallback: number;
510
+ anti_patterns_found: number;
511
+ overall_reliability: 'fragile' | 'moderate' | 'robust' | 'resilient';
512
+ };
513
+ }>;
514
+ summary: {
515
+ total_files: number;
516
+ total_inference_points: number;
517
+ providers: string[];
518
+ models: string[];
519
+ estimated_cost_per_1k_calls: number;
520
+ cost_risk_high: number;
521
+ blocking_calls: number;
522
+ streaming_enabled: number;
523
+ estimated_p95_ms: number;
524
+ has_rate_limiting: number;
525
+ scaling_bottlenecks: number;
526
+ has_error_handling: number;
527
+ has_retry: number;
528
+ has_fallback: number;
529
+ anti_patterns_found: number;
530
+ overall_reliability: string;
531
+ total_optimizations: number;
532
+ critical_optimizations: number;
533
+ };
534
+ all_optimizations: Array<{
535
+ dimension: 'cost' | 'latency' | 'throughput' | 'reliability';
536
+ inference_point_id: string;
537
+ file: string;
538
+ line: number;
539
+ type: string;
540
+ description: string;
541
+ impact: string;
542
+ effort: string;
543
+ priority: string;
544
+ }>;
545
+ insights: Insight[];
546
+ }
547
+
548
+ // =============================================================================
549
+ // INPUT TYPES
550
+ // =============================================================================
551
+
552
+ export interface StaticAnalysisInput {
553
+ files: Array<{
554
+ path: string;
555
+ content: string;
556
+ language: string;
557
+ }>;
558
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * PostHog Analytics (v1.8)
3
+ *
4
+ * Opt-in analytics for understanding PeakInfer usage patterns.
5
+ * Respects DO_NOT_TRACK and PEAKINFER_NO_ANALYTICS.
6
+ * Never collects PII or code content.
7
+ */
8
+
9
+ import { PostHog } from 'posthog-node';
10
+ import { createHash } from 'crypto';
11
+ import { hostname } from 'os';
12
+
13
+ // =============================================================================
14
+ // STATE
15
+ // =============================================================================
16
+
17
+ let client: PostHog | null = null;
18
+ let distinctId: string = 'anon';
19
+
20
+ // =============================================================================
21
+ // PUBLIC API
22
+ // =============================================================================
23
+
24
+ /**
25
+ * Initialize analytics. Respects DO_NOT_TRACK and PEAKINFER_NO_ANALYTICS.
26
+ * Safe to call multiple times - will only initialize once.
27
+ */
28
+ export function initAnalytics(): void {
29
+ // Already initialized
30
+ if (client) return;
31
+
32
+ // Respect opt-out signals
33
+ if (process.env.DO_NOT_TRACK === '1' || process.env.PEAKINFER_NO_ANALYTICS === '1') {
34
+ return;
35
+ }
36
+
37
+ // Require API key (from env or default public key)
38
+ const apiKey = process.env.PEAKINFER_POSTHOG_KEY || 'phc_LfZNH5EM2cJ5n3P9rRwYPyLx3TqZRNPGjBzqNGR9lkV';
39
+
40
+ try {
41
+ client = new PostHog(apiKey, {
42
+ host: 'https://app.posthog.com',
43
+ flushAt: 5,
44
+ flushInterval: 10000, // 10 seconds
45
+ });
46
+
47
+ // Generate stable anonymous ID from machine hostname
48
+ distinctId = createHash('sha256')
49
+ .update(`peakinfer-${hostname()}`)
50
+ .digest('hex')
51
+ .substring(0, 16);
52
+ } catch {
53
+ // Silently fail - analytics should never break the tool
54
+ client = null;
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Track an event. No-op if analytics disabled.
60
+ */
61
+ export function track(event: string, properties?: Record<string, unknown>): void {
62
+ if (!client) return;
63
+
64
+ try {
65
+ client.capture({
66
+ distinctId,
67
+ event,
68
+ properties: {
69
+ ...properties,
70
+ version: process.env.npm_package_version || 'unknown',
71
+ node_version: process.version,
72
+ platform: process.platform,
73
+ },
74
+ });
75
+ } catch {
76
+ // Silently fail
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Flush pending events and shutdown. Call before process exit.
82
+ */
83
+ export async function flush(): Promise<void> {
84
+ if (!client) return;
85
+
86
+ try {
87
+ await client.shutdown();
88
+ } catch {
89
+ // Silently fail
90
+ } finally {
91
+ client = null;
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Check if analytics is enabled.
97
+ */
98
+ export function isEnabled(): boolean {
99
+ return client !== null;
100
+ }