@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
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll } from 'vitest';
|
|
2
|
+
import { RuntimeAnalyzerAgent, type RuntimeAnalyzerInput } from '../src/agents/runtime-analyzer.js';
|
|
3
|
+
import { setTestPricing } from '../src/costs.js';
|
|
4
|
+
import type { InferenceEvent, RuntimeSummary } from '../src/types.js';
|
|
5
|
+
|
|
6
|
+
// =============================================================================
|
|
7
|
+
// TEST FIXTURES
|
|
8
|
+
// =============================================================================
|
|
9
|
+
|
|
10
|
+
beforeAll(() => {
|
|
11
|
+
setTestPricing({
|
|
12
|
+
'gpt-4o': { input: 5.0, output: 15.0 },
|
|
13
|
+
'gpt-4o-mini': { input: 0.15, output: 0.6 },
|
|
14
|
+
'claude-3-opus': { input: 15.0, output: 75.0 },
|
|
15
|
+
'claude-3-5-sonnet': { input: 3.0, output: 15.0 },
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const makeEvent = (overrides: Partial<InferenceEvent> = {}): InferenceEvent => ({
|
|
20
|
+
id: `evt_${Math.random().toString(36).slice(2, 9)}`,
|
|
21
|
+
ts: '2024-01-01T00:00:00Z',
|
|
22
|
+
provider: 'openai',
|
|
23
|
+
model: 'gpt-4o',
|
|
24
|
+
input_tokens: 100,
|
|
25
|
+
output_tokens: 50,
|
|
26
|
+
latency_ms: 420,
|
|
27
|
+
...overrides,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const makeRuntimeSummary = (overrides: Partial<RuntimeSummary> = {}): RuntimeSummary => ({
|
|
31
|
+
totalEvents: 100,
|
|
32
|
+
byProvider: {
|
|
33
|
+
openai: {
|
|
34
|
+
calls: 80,
|
|
35
|
+
tokens_in: 8000,
|
|
36
|
+
tokens_out: 4000,
|
|
37
|
+
latency_p50: 400,
|
|
38
|
+
latency_p95: 800,
|
|
39
|
+
latency_p99: 1200,
|
|
40
|
+
},
|
|
41
|
+
anthropic: {
|
|
42
|
+
calls: 20,
|
|
43
|
+
tokens_in: 2000,
|
|
44
|
+
tokens_out: 1000,
|
|
45
|
+
latency_p50: 500,
|
|
46
|
+
latency_p95: 1000,
|
|
47
|
+
latency_p99: 1500,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
byModel: {
|
|
51
|
+
'gpt-4o': {
|
|
52
|
+
calls: 60,
|
|
53
|
+
tokens_in: 6000,
|
|
54
|
+
tokens_out: 3000,
|
|
55
|
+
latency_p50: 350,
|
|
56
|
+
latency_p95: 700,
|
|
57
|
+
latency_p99: 1100,
|
|
58
|
+
},
|
|
59
|
+
'gpt-4o-mini': {
|
|
60
|
+
calls: 20,
|
|
61
|
+
tokens_in: 2000,
|
|
62
|
+
tokens_out: 1000,
|
|
63
|
+
latency_p50: 200,
|
|
64
|
+
latency_p95: 400,
|
|
65
|
+
latency_p99: 600,
|
|
66
|
+
},
|
|
67
|
+
'claude-3-5-sonnet': {
|
|
68
|
+
calls: 20,
|
|
69
|
+
tokens_in: 2000,
|
|
70
|
+
tokens_out: 1000,
|
|
71
|
+
latency_p50: 500,
|
|
72
|
+
latency_p95: 1000,
|
|
73
|
+
latency_p99: 1500,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
global: {
|
|
77
|
+
p50: 400,
|
|
78
|
+
p95: 800,
|
|
79
|
+
p99: 1200,
|
|
80
|
+
},
|
|
81
|
+
...overrides,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const makeInput = (overrides: Partial<RuntimeAnalyzerInput> = {}): RuntimeAnalyzerInput => ({
|
|
85
|
+
events: Array.from({ length: 10 }, (_, i) => makeEvent({ id: `evt_${i}` })),
|
|
86
|
+
runtimeSummary: makeRuntimeSummary(),
|
|
87
|
+
...overrides,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// =============================================================================
|
|
91
|
+
// AGENT PROPERTIES TESTS
|
|
92
|
+
// =============================================================================
|
|
93
|
+
|
|
94
|
+
describe('RuntimeAnalyzerAgent', () => {
|
|
95
|
+
describe('agent properties', () => {
|
|
96
|
+
it('has correct name', () => {
|
|
97
|
+
expect(RuntimeAnalyzerAgent.name).toBe('runtime-analyzer');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('has description', () => {
|
|
101
|
+
expect(RuntimeAnalyzerAgent.description).toBeDefined();
|
|
102
|
+
expect(RuntimeAnalyzerAgent.description.length).toBeGreaterThan(0);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('has tools registry', () => {
|
|
106
|
+
expect(RuntimeAnalyzerAgent.tools).toBeDefined();
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// =============================================================================
|
|
111
|
+
// EXECUTION TESTS
|
|
112
|
+
// =============================================================================
|
|
113
|
+
|
|
114
|
+
describe('execute', () => {
|
|
115
|
+
it('returns valid output structure', async () => {
|
|
116
|
+
const input = makeInput();
|
|
117
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
118
|
+
|
|
119
|
+
// Check insights array
|
|
120
|
+
expect(Array.isArray(result.insights)).toBe(true);
|
|
121
|
+
|
|
122
|
+
// Check detectedPatterns structure
|
|
123
|
+
expect(result.detectedPatterns).toHaveProperty('applicationType');
|
|
124
|
+
expect(result.detectedPatterns).toHaveProperty('multiModelPipeline');
|
|
125
|
+
expect(result.detectedPatterns).toHaveProperty('streamingDetected');
|
|
126
|
+
expect(result.detectedPatterns).toHaveProperty('batchingDetected');
|
|
127
|
+
expect(result.detectedPatterns).toHaveProperty('cachingDetected');
|
|
128
|
+
|
|
129
|
+
// Check summary structure
|
|
130
|
+
expect(result.summary).toHaveProperty('totalCalls');
|
|
131
|
+
expect(result.summary).toHaveProperty('totalTokens');
|
|
132
|
+
expect(result.summary).toHaveProperty('dominantProvider');
|
|
133
|
+
expect(result.summary).toHaveProperty('dominantModel');
|
|
134
|
+
expect(result.summary).toHaveProperty('estimatedDailyCostUSD');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('application type is valid enum value', async () => {
|
|
138
|
+
const input = makeInput();
|
|
139
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
140
|
+
|
|
141
|
+
const validTypes = ['rag', 'agent', 'batch', 'chat', 'pipeline', 'unknown'];
|
|
142
|
+
expect(validTypes).toContain(result.detectedPatterns.applicationType);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('correctly detects multi-model pipeline', async () => {
|
|
146
|
+
const input = makeInput({
|
|
147
|
+
runtimeSummary: makeRuntimeSummary({
|
|
148
|
+
byModel: {
|
|
149
|
+
'gpt-4o': { calls: 50, tokens_in: 5000, tokens_out: 2500, latency_p50: 400, latency_p95: 800, latency_p99: 1200 },
|
|
150
|
+
'gpt-4o-mini': { calls: 50, tokens_in: 5000, tokens_out: 2500, latency_p50: 200, latency_p95: 400, latency_p99: 600 },
|
|
151
|
+
},
|
|
152
|
+
}),
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
156
|
+
|
|
157
|
+
expect(result.detectedPatterns.multiModelPipeline).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('detects streaming pattern from events', async () => {
|
|
161
|
+
const input = makeInput({
|
|
162
|
+
events: [
|
|
163
|
+
makeEvent({ streaming: true }),
|
|
164
|
+
makeEvent({ streaming: true }),
|
|
165
|
+
makeEvent({ streaming: false }),
|
|
166
|
+
],
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
170
|
+
|
|
171
|
+
expect(result.detectedPatterns.streamingDetected).toBe(true);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('detects batching pattern from events', async () => {
|
|
175
|
+
const input = makeInput({
|
|
176
|
+
events: [
|
|
177
|
+
makeEvent({ batch_id: 'batch_001' }),
|
|
178
|
+
makeEvent({ batch_id: 'batch_001' }),
|
|
179
|
+
makeEvent(),
|
|
180
|
+
],
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
184
|
+
|
|
185
|
+
expect(result.detectedPatterns.batchingDetected).toBe(true);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('detects caching pattern from events', async () => {
|
|
189
|
+
const input = makeInput({
|
|
190
|
+
events: [
|
|
191
|
+
makeEvent({ cached: true }),
|
|
192
|
+
makeEvent({ cached: false }),
|
|
193
|
+
],
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
197
|
+
|
|
198
|
+
expect(result.detectedPatterns.cachingDetected).toBe(true);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('calculates total tokens', async () => {
|
|
202
|
+
const runtimeSummary = makeRuntimeSummary({
|
|
203
|
+
byModel: {
|
|
204
|
+
'gpt-4o': { calls: 10, tokens_in: 1000, tokens_out: 500, latency_p50: 400, latency_p95: 800, latency_p99: 1200 },
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
const input = makeInput({ runtimeSummary });
|
|
209
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
210
|
+
|
|
211
|
+
// Total tokens should be a reasonable positive number
|
|
212
|
+
// LLM may calculate from events or summary differently
|
|
213
|
+
expect(result.summary.totalTokens).toBeGreaterThan(0);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('identifies dominant provider', async () => {
|
|
217
|
+
const runtimeSummary = makeRuntimeSummary({
|
|
218
|
+
byProvider: {
|
|
219
|
+
openai: { calls: 100, tokens_in: 10000, tokens_out: 5000, latency_p50: 400, latency_p95: 800, latency_p99: 1200 },
|
|
220
|
+
anthropic: { calls: 20, tokens_in: 2000, tokens_out: 1000, latency_p50: 500, latency_p95: 1000, latency_p99: 1500 },
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const input = makeInput({ runtimeSummary });
|
|
225
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
226
|
+
|
|
227
|
+
expect(result.summary.dominantProvider).toBe('openai');
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('identifies dominant model', async () => {
|
|
231
|
+
const runtimeSummary = makeRuntimeSummary({
|
|
232
|
+
byModel: {
|
|
233
|
+
'gpt-4o': { calls: 80, tokens_in: 8000, tokens_out: 4000, latency_p50: 400, latency_p95: 800, latency_p99: 1200 },
|
|
234
|
+
'gpt-4o-mini': { calls: 20, tokens_in: 2000, tokens_out: 1000, latency_p50: 200, latency_p95: 400, latency_p99: 600 },
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
const input = makeInput({ runtimeSummary });
|
|
239
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
240
|
+
|
|
241
|
+
expect(result.summary.dominantModel).toBe('gpt-4o');
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// =============================================================================
|
|
246
|
+
// EDGE CASES
|
|
247
|
+
// =============================================================================
|
|
248
|
+
|
|
249
|
+
describe('edge cases', () => {
|
|
250
|
+
it('handles empty events array', async () => {
|
|
251
|
+
const input = makeInput({
|
|
252
|
+
events: [],
|
|
253
|
+
runtimeSummary: makeRuntimeSummary({ totalEvents: 0 }),
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
257
|
+
|
|
258
|
+
expect(result.detectedPatterns.streamingDetected).toBe(false);
|
|
259
|
+
expect(result.detectedPatterns.batchingDetected).toBe(false);
|
|
260
|
+
expect(result.detectedPatterns.cachingDetected).toBe(false);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('handles single model', async () => {
|
|
264
|
+
const input = makeInput({
|
|
265
|
+
runtimeSummary: makeRuntimeSummary({
|
|
266
|
+
byModel: {
|
|
267
|
+
'gpt-4o': { calls: 100, tokens_in: 10000, tokens_out: 5000, latency_p50: 400, latency_p95: 800, latency_p99: 1200 },
|
|
268
|
+
},
|
|
269
|
+
}),
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
273
|
+
|
|
274
|
+
// LLM may still detect patterns; key assertion: returns valid result
|
|
275
|
+
expect(result.detectedPatterns).toBeDefined();
|
|
276
|
+
expect(result.summary.dominantModel).toBeDefined();
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it('handles missing optional event fields', async () => {
|
|
280
|
+
const input = makeInput({
|
|
281
|
+
events: [makeEvent()], // No streaming, batch_id, cached, retry_count, fallback_used
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
285
|
+
|
|
286
|
+
expect(result.detectedPatterns.streamingDetected).toBe(false);
|
|
287
|
+
expect(result.detectedPatterns.batchingDetected).toBe(false);
|
|
288
|
+
expect(result.detectedPatterns.cachingDetected).toBe(false);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it('handles empty byProvider and byModel', async () => {
|
|
292
|
+
const input = makeInput({
|
|
293
|
+
runtimeSummary: {
|
|
294
|
+
totalEvents: 0,
|
|
295
|
+
byProvider: {},
|
|
296
|
+
byModel: {},
|
|
297
|
+
global: { p50: 0, p95: 0, p99: 0 },
|
|
298
|
+
},
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
302
|
+
|
|
303
|
+
// LLM may return 'unknown', 'none', 'n/a' etc for empty data
|
|
304
|
+
expect(['unknown', 'none', 'n/a', '']).toContain(result.summary.dominantProvider.toLowerCase());
|
|
305
|
+
expect(['unknown', 'none', 'n/a', '']).toContain(result.summary.dominantModel.toLowerCase());
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
// =============================================================================
|
|
310
|
+
// PATTERN DETECTION COMBINATIONS
|
|
311
|
+
// =============================================================================
|
|
312
|
+
|
|
313
|
+
describe('pattern detection combinations', () => {
|
|
314
|
+
it('detects all patterns when present', async () => {
|
|
315
|
+
const input = makeInput({
|
|
316
|
+
events: [
|
|
317
|
+
makeEvent({ streaming: true, batch_id: 'b1', cached: true }),
|
|
318
|
+
],
|
|
319
|
+
runtimeSummary: makeRuntimeSummary({
|
|
320
|
+
byModel: {
|
|
321
|
+
'gpt-4o': { calls: 50, tokens_in: 5000, tokens_out: 2500, latency_p50: 400, latency_p95: 800, latency_p99: 1200 },
|
|
322
|
+
'claude-3-5-sonnet': { calls: 50, tokens_in: 5000, tokens_out: 2500, latency_p50: 500, latency_p95: 1000, latency_p99: 1500 },
|
|
323
|
+
},
|
|
324
|
+
}),
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
328
|
+
|
|
329
|
+
expect(result.detectedPatterns.streamingDetected).toBe(true);
|
|
330
|
+
expect(result.detectedPatterns.batchingDetected).toBe(true);
|
|
331
|
+
expect(result.detectedPatterns.cachingDetected).toBe(true);
|
|
332
|
+
expect(result.detectedPatterns.multiModelPipeline).toBe(true);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it('returns valid patterns structure for minimal input', async () => {
|
|
336
|
+
const input = makeInput({
|
|
337
|
+
events: [makeEvent()], // No optional fields
|
|
338
|
+
runtimeSummary: makeRuntimeSummary({
|
|
339
|
+
byModel: {
|
|
340
|
+
'gpt-4o': { calls: 100, tokens_in: 10000, tokens_out: 5000, latency_p50: 400, latency_p95: 800, latency_p99: 1200 },
|
|
341
|
+
},
|
|
342
|
+
}),
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
346
|
+
|
|
347
|
+
// LLM may infer patterns from context; key assertion: returns valid structure
|
|
348
|
+
expect(typeof result.detectedPatterns.streamingDetected).toBe('boolean');
|
|
349
|
+
expect(typeof result.detectedPatterns.batchingDetected).toBe('boolean');
|
|
350
|
+
expect(typeof result.detectedPatterns.cachingDetected).toBe('boolean');
|
|
351
|
+
expect(typeof result.detectedPatterns.multiModelPipeline).toBe('boolean');
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
// =============================================================================
|
|
356
|
+
// INSIGHTS VALIDATION (when LLM is available)
|
|
357
|
+
// =============================================================================
|
|
358
|
+
|
|
359
|
+
describe('insights structure', () => {
|
|
360
|
+
it('insights have required fields when generated', async () => {
|
|
361
|
+
const input = makeInput();
|
|
362
|
+
const { result } = await RuntimeAnalyzerAgent.execute(input);
|
|
363
|
+
|
|
364
|
+
// Insights may be empty in fallback mode, but if present, must have structure
|
|
365
|
+
for (const insight of result.insights) {
|
|
366
|
+
expect(insight).toHaveProperty('id');
|
|
367
|
+
expect(insight).toHaveProperty('severity');
|
|
368
|
+
expect(insight).toHaveProperty('category');
|
|
369
|
+
expect(insight).toHaveProperty('headline');
|
|
370
|
+
expect(insight).toHaveProperty('evidence');
|
|
371
|
+
expect(['critical', 'warning', 'info']).toContain(insight.severity);
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
});
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import { parseEvents, aggregate, percentile } from '../src/runtime.js';
|
|
3
|
+
import { writeFileSync, mkdirSync, rmSync } from 'fs';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
|
|
6
|
+
const FIXTURES_DIR = join(__dirname, 'fixtures', 'events');
|
|
7
|
+
|
|
8
|
+
// Ensure fixtures directory exists
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
mkdirSync(FIXTURES_DIR, { recursive: true });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe('runtime parser', () => {
|
|
14
|
+
describe('JSONL parsing', () => {
|
|
15
|
+
it('parses valid JSONL', async () => {
|
|
16
|
+
const content = [
|
|
17
|
+
'{"id":"1","ts":"2024-01-01T00:00:00Z","provider":"openai","model":"gpt-4o","input_tokens":100,"output_tokens":50,"latency_ms":420}',
|
|
18
|
+
'{"id":"2","ts":"2024-01-01T00:01:00Z","provider":"openai","model":"gpt-4o","input_tokens":200,"output_tokens":80,"latency_ms":580}',
|
|
19
|
+
].join('\n');
|
|
20
|
+
|
|
21
|
+
const path = join(FIXTURES_DIR, 'valid.jsonl');
|
|
22
|
+
writeFileSync(path, content);
|
|
23
|
+
|
|
24
|
+
const events = await parseEvents(path);
|
|
25
|
+
expect(events.length).toBe(2);
|
|
26
|
+
expect(events[0].provider).toBe('openai');
|
|
27
|
+
expect(events[1].latency_ms).toBe(580);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('JSON array parsing', () => {
|
|
32
|
+
it('parses valid JSON array', async () => {
|
|
33
|
+
const data = [
|
|
34
|
+
{ id: '1', ts: '2024-01-01T00:00:00Z', provider: 'anthropic', model: 'claude-3-sonnet-20240229', input_tokens: 150, output_tokens: 60, latency_ms: 350 },
|
|
35
|
+
{ id: '2', ts: '2024-01-01T00:01:00Z', provider: 'anthropic', model: 'claude-3-sonnet-20240229', input_tokens: 180, output_tokens: 90, latency_ms: 400 },
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
const path = join(FIXTURES_DIR, 'valid.json');
|
|
39
|
+
writeFileSync(path, JSON.stringify(data));
|
|
40
|
+
|
|
41
|
+
const events = await parseEvents(path);
|
|
42
|
+
expect(events.length).toBe(2);
|
|
43
|
+
expect(events[0].provider).toBe('anthropic');
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('CSV parsing', () => {
|
|
48
|
+
it('parses valid CSV', async () => {
|
|
49
|
+
const content = [
|
|
50
|
+
'id,ts,provider,model,input_tokens,output_tokens,latency_ms',
|
|
51
|
+
'1,2024-01-01T00:00:00Z,openai,gpt-4o,100,50,420',
|
|
52
|
+
'2,2024-01-01T00:01:00Z,openai,gpt-4o,200,80,580',
|
|
53
|
+
].join('\n');
|
|
54
|
+
|
|
55
|
+
const path = join(FIXTURES_DIR, 'valid.csv');
|
|
56
|
+
writeFileSync(path, content);
|
|
57
|
+
|
|
58
|
+
const events = await parseEvents(path);
|
|
59
|
+
expect(events.length).toBe(2);
|
|
60
|
+
expect(events[0].input_tokens).toBe(100);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('validation', () => {
|
|
65
|
+
it('rejects missing id field', async () => {
|
|
66
|
+
const content = '{"ts":"2024-01-01","provider":"openai","model":"gpt-4o","input_tokens":100,"output_tokens":50,"latency_ms":420}';
|
|
67
|
+
const path = join(FIXTURES_DIR, 'missing-id.jsonl');
|
|
68
|
+
writeFileSync(path, content);
|
|
69
|
+
|
|
70
|
+
await expect(parseEvents(path)).rejects.toThrow();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('rejects missing ts field', async () => {
|
|
74
|
+
const content = '{"id":"1","provider":"openai","model":"gpt-4o","input_tokens":100,"output_tokens":50,"latency_ms":420}';
|
|
75
|
+
const path = join(FIXTURES_DIR, 'missing-ts.jsonl');
|
|
76
|
+
writeFileSync(path, content);
|
|
77
|
+
|
|
78
|
+
await expect(parseEvents(path)).rejects.toThrow();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('rejects missing provider field', async () => {
|
|
82
|
+
const content = '{"id":"1","ts":"2024-01-01","model":"gpt-4o","input_tokens":100,"output_tokens":50,"latency_ms":420}';
|
|
83
|
+
const path = join(FIXTURES_DIR, 'missing-provider.jsonl');
|
|
84
|
+
writeFileSync(path, content);
|
|
85
|
+
|
|
86
|
+
await expect(parseEvents(path)).rejects.toThrow();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('rejects missing model field', async () => {
|
|
90
|
+
const content = '{"id":"1","ts":"2024-01-01","provider":"openai","input_tokens":100,"output_tokens":50,"latency_ms":420}';
|
|
91
|
+
const path = join(FIXTURES_DIR, 'missing-model.jsonl');
|
|
92
|
+
writeFileSync(path, content);
|
|
93
|
+
|
|
94
|
+
await expect(parseEvents(path)).rejects.toThrow();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('rejects missing input_tokens field', async () => {
|
|
98
|
+
const content = '{"id":"1","ts":"2024-01-01","provider":"openai","model":"gpt-4o","output_tokens":50,"latency_ms":420}';
|
|
99
|
+
const path = join(FIXTURES_DIR, 'missing-input.jsonl');
|
|
100
|
+
writeFileSync(path, content);
|
|
101
|
+
|
|
102
|
+
await expect(parseEvents(path)).rejects.toThrow();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('rejects missing output_tokens field', async () => {
|
|
106
|
+
const content = '{"id":"1","ts":"2024-01-01","provider":"openai","model":"gpt-4o","input_tokens":100,"latency_ms":420}';
|
|
107
|
+
const path = join(FIXTURES_DIR, 'missing-output.jsonl');
|
|
108
|
+
writeFileSync(path, content);
|
|
109
|
+
|
|
110
|
+
await expect(parseEvents(path)).rejects.toThrow();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('rejects missing latency_ms field', async () => {
|
|
114
|
+
const content = '{"id":"1","ts":"2024-01-01","provider":"openai","model":"gpt-4o","input_tokens":100,"output_tokens":50}';
|
|
115
|
+
const path = join(FIXTURES_DIR, 'missing-latency.jsonl');
|
|
116
|
+
writeFileSync(path, content);
|
|
117
|
+
|
|
118
|
+
await expect(parseEvents(path)).rejects.toThrow();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('rejects wrong type for latency_ms', async () => {
|
|
122
|
+
const content = '{"id":"1","ts":"2024-01-01","provider":"openai","model":"gpt-4o","input_tokens":100,"output_tokens":50,"latency_ms":"fast"}';
|
|
123
|
+
const path = join(FIXTURES_DIR, 'wrong-type.jsonl');
|
|
124
|
+
writeFileSync(path, content);
|
|
125
|
+
|
|
126
|
+
await expect(parseEvents(path)).rejects.toThrow();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('accepts optional intent field', async () => {
|
|
130
|
+
const content = '{"id":"1","ts":"2024-01-01T00:00:00Z","provider":"openai","model":"gpt-4o","input_tokens":100,"output_tokens":50,"latency_ms":420,"intent":"chat"}';
|
|
131
|
+
const path = join(FIXTURES_DIR, 'with-intent.jsonl');
|
|
132
|
+
writeFileSync(path, content);
|
|
133
|
+
|
|
134
|
+
const events = await parseEvents(path);
|
|
135
|
+
expect(events[0].intent).toBe('chat');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('accepts optional callsite_id field', async () => {
|
|
139
|
+
const content = '{"id":"1","ts":"2024-01-01T00:00:00Z","provider":"openai","model":"gpt-4o","input_tokens":100,"output_tokens":50,"latency_ms":420,"callsite_id":"cs_001"}';
|
|
140
|
+
const path = join(FIXTURES_DIR, 'with-callsite.jsonl');
|
|
141
|
+
writeFileSync(path, content);
|
|
142
|
+
|
|
143
|
+
const events = await parseEvents(path);
|
|
144
|
+
expect(events[0].callsite_id).toBe('cs_001');
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe('percentile calculation', () => {
|
|
149
|
+
it('calculates p50 correctly', () => {
|
|
150
|
+
const values = [100, 200, 300, 400, 500];
|
|
151
|
+
expect(percentile(values, 50)).toBe(300);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('calculates p95 correctly', () => {
|
|
155
|
+
const values = Array.from({ length: 100 }, (_, i) => (i + 1) * 10);
|
|
156
|
+
expect(percentile(values, 95)).toBe(950);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('calculates p99 correctly', () => {
|
|
160
|
+
const values = Array.from({ length: 100 }, (_, i) => (i + 1) * 10);
|
|
161
|
+
expect(percentile(values, 99)).toBe(990);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
describe('aggregation', () => {
|
|
166
|
+
it('aggregates by provider', () => {
|
|
167
|
+
const events = [
|
|
168
|
+
{ id: '1', ts: '2024-01-01', provider: 'openai' as const, model: 'gpt-4o', input_tokens: 100, output_tokens: 50, latency_ms: 420 },
|
|
169
|
+
{ id: '2', ts: '2024-01-01', provider: 'openai' as const, model: 'gpt-4o', input_tokens: 200, output_tokens: 80, latency_ms: 580 },
|
|
170
|
+
{ id: '3', ts: '2024-01-01', provider: 'anthropic' as const, model: 'claude-3', input_tokens: 150, output_tokens: 60, latency_ms: 350 },
|
|
171
|
+
];
|
|
172
|
+
|
|
173
|
+
const summary = aggregate(events);
|
|
174
|
+
expect(summary.byProvider['openai'].calls).toBe(2);
|
|
175
|
+
expect(summary.byProvider['anthropic'].calls).toBe(1);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('aggregates by model', () => {
|
|
179
|
+
const events = [
|
|
180
|
+
{ id: '1', ts: '2024-01-01', provider: 'openai' as const, model: 'gpt-4o', input_tokens: 100, output_tokens: 50, latency_ms: 420 },
|
|
181
|
+
{ id: '2', ts: '2024-01-01', provider: 'openai' as const, model: 'gpt-4o-mini', input_tokens: 200, output_tokens: 80, latency_ms: 180 },
|
|
182
|
+
];
|
|
183
|
+
|
|
184
|
+
const summary = aggregate(events);
|
|
185
|
+
expect(summary.byModel['gpt-4o'].calls).toBe(1);
|
|
186
|
+
expect(summary.byModel['gpt-4o-mini'].calls).toBe(1);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('handles single event', () => {
|
|
190
|
+
const events = [
|
|
191
|
+
{ id: '1', ts: '2024-01-01', provider: 'openai' as const, model: 'gpt-4o', input_tokens: 100, output_tokens: 50, latency_ms: 420 },
|
|
192
|
+
];
|
|
193
|
+
|
|
194
|
+
const summary = aggregate(events);
|
|
195
|
+
expect(summary.totalEvents).toBe(1);
|
|
196
|
+
expect(summary.global.p50).toBe(420);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('handles empty array', () => {
|
|
200
|
+
const summary = aggregate([]);
|
|
201
|
+
expect(summary.totalEvents).toBe(0);
|
|
202
|
+
expect(summary.global.p50).toBe(0);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
});
|