modelrig 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +94 -0
- package/README.md +41 -11
- package/data/PROVENANCE.md +19 -0
- package/data/pricing-manual.json +22 -0
- package/data/pricing-map.json +46362 -0
- package/data/pricing-overlay.json +419 -0
- package/dist/adapters/anthropic.d.ts +50 -6
- package/dist/adapters/anthropic.d.ts.map +1 -1
- package/dist/adapters/anthropic.js +123 -29
- package/dist/adapters/build-adapters.d.ts +11 -0
- package/dist/adapters/build-adapters.d.ts.map +1 -0
- package/dist/adapters/build-adapters.js +45 -0
- package/dist/adapters/deepseek.d.ts +6 -0
- package/dist/adapters/deepseek.d.ts.map +1 -1
- package/dist/adapters/deepseek.js +54 -1
- package/dist/adapters/gemini.d.ts.map +1 -1
- package/dist/adapters/gemini.js +46 -6
- package/dist/adapters/grok.d.ts +9 -0
- package/dist/adapters/grok.d.ts.map +1 -1
- package/dist/adapters/grok.js +67 -3
- package/dist/adapters/openai-compat.d.ts +6 -0
- package/dist/adapters/openai-compat.d.ts.map +1 -1
- package/dist/adapters/openai-compat.js +58 -1
- package/dist/adapters/openai-stream.d.ts +58 -0
- package/dist/adapters/openai-stream.d.ts.map +1 -0
- package/dist/adapters/openai-stream.js +120 -0
- package/dist/adapters/openai.d.ts +6 -0
- package/dist/adapters/openai.d.ts.map +1 -1
- package/dist/adapters/openai.js +60 -1
- package/dist/adapters/sampling.d.ts +28 -0
- package/dist/adapters/sampling.d.ts.map +1 -0
- package/dist/adapters/sampling.js +34 -0
- package/dist/adapters/tool-mapping.d.ts +92 -0
- package/dist/adapters/tool-mapping.d.ts.map +1 -0
- package/dist/adapters/tool-mapping.js +143 -0
- package/dist/artifacts/artifact.d.ts +137 -0
- package/dist/artifacts/artifact.d.ts.map +1 -0
- package/dist/artifacts/artifact.js +377 -0
- package/dist/artifacts/content-uploader.d.ts +87 -0
- package/dist/artifacts/content-uploader.d.ts.map +1 -0
- package/dist/artifacts/content-uploader.js +124 -0
- package/dist/artifacts/index.d.ts +63 -0
- package/dist/artifacts/index.d.ts.map +1 -0
- package/dist/artifacts/index.js +80 -0
- package/dist/artifacts/run-context.d.ts +129 -0
- package/dist/artifacts/run-context.d.ts.map +1 -0
- package/dist/artifacts/run-context.js +208 -0
- package/dist/artifacts/scrub.d.ts +60 -0
- package/dist/artifacts/scrub.d.ts.map +1 -0
- package/dist/artifacts/scrub.js +139 -0
- package/dist/artifacts/step.d.ts +26 -0
- package/dist/artifacts/step.d.ts.map +1 -0
- package/dist/artifacts/step.js +37 -0
- package/dist/artifacts/taint.d.ts +63 -0
- package/dist/artifacts/taint.d.ts.map +1 -0
- package/dist/artifacts/taint.js +76 -0
- package/dist/bakeoff/cache-sim.d.ts +99 -0
- package/dist/bakeoff/cache-sim.d.ts.map +1 -0
- package/dist/bakeoff/cache-sim.js +126 -0
- package/dist/bakeoff/eval-cases.d.ts +96 -0
- package/dist/bakeoff/eval-cases.d.ts.map +1 -0
- package/dist/bakeoff/eval-cases.js +105 -0
- package/dist/bakeoff/report.d.ts +41 -0
- package/dist/bakeoff/report.d.ts.map +1 -0
- package/dist/bakeoff/report.js +142 -0
- package/dist/bakeoff/samples.d.ts +15 -0
- package/dist/bakeoff/samples.d.ts.map +1 -1
- package/dist/bakeoff/samples.js +20 -0
- package/dist/cli.d.ts +1 -3
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +134 -116
- package/dist/config.d.ts +100 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +104 -0
- package/dist/deprecations.d.ts +32 -0
- package/dist/deprecations.d.ts.map +1 -0
- package/dist/deprecations.js +59 -0
- package/dist/gateway/http-protocol.d.ts +175 -0
- package/dist/gateway/http-protocol.d.ts.map +1 -0
- package/dist/gateway/http-protocol.js +105 -0
- package/dist/index.d.ts +105 -18
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +266 -138
- package/dist/init/finder.d.ts +15 -0
- package/dist/init/finder.d.ts.map +1 -1
- package/dist/init/finder.js +46 -0
- package/dist/init/init-cli.d.ts +10 -0
- package/dist/init/init-cli.d.ts.map +1 -0
- package/dist/init/init-cli.js +86 -0
- package/dist/init/observe-cli.d.ts +19 -0
- package/dist/init/observe-cli.d.ts.map +1 -0
- package/dist/init/observe-cli.js +73 -0
- package/dist/init/observe.d.ts +81 -0
- package/dist/init/observe.d.ts.map +1 -0
- package/dist/init/observe.js +245 -0
- package/dist/registry/build.d.ts +94 -1
- package/dist/registry/build.d.ts.map +1 -1
- package/dist/registry/build.js +125 -4
- package/dist/registry/cache-policy.d.ts +94 -0
- package/dist/registry/cache-policy.d.ts.map +1 -0
- package/dist/registry/cache-policy.js +238 -0
- package/dist/registry/call-notes.d.ts.map +1 -1
- package/dist/registry/call-notes.js +40 -2
- package/dist/registry/effective-rate.d.ts +151 -0
- package/dist/registry/effective-rate.d.ts.map +1 -0
- package/dist/registry/effective-rate.js +156 -0
- package/dist/registry/facts.d.ts.map +1 -1
- package/dist/registry/facts.js +16 -0
- package/dist/registry/parity.d.ts +26 -0
- package/dist/registry/parity.d.ts.map +1 -1
- package/dist/registry/parity.js +43 -2
- package/dist/registry/pricing-shape.d.ts +113 -0
- package/dist/registry/pricing-shape.d.ts.map +1 -0
- package/dist/registry/pricing-shape.js +123 -0
- package/dist/registry/pricing.d.ts +25 -1
- package/dist/registry/pricing.d.ts.map +1 -1
- package/dist/registry/pricing.js +64 -6
- package/dist/rigindex/bootstrap.d.ts +36 -0
- package/dist/rigindex/bootstrap.d.ts.map +1 -0
- package/dist/rigindex/bootstrap.js +97 -0
- package/dist/rigindex/fit.d.ts +24 -0
- package/dist/rigindex/fit.d.ts.map +1 -0
- package/dist/rigindex/fit.js +76 -0
- package/dist/rigindex/gates.d.ts +52 -0
- package/dist/rigindex/gates.d.ts.map +1 -0
- package/dist/rigindex/gates.js +109 -0
- package/dist/rigindex/graph.d.ts +31 -0
- package/dist/rigindex/graph.d.ts.map +1 -0
- package/dist/rigindex/graph.js +120 -0
- package/dist/rigindex/index.d.ts +23 -0
- package/dist/rigindex/index.d.ts.map +1 -0
- package/dist/rigindex/index.js +112 -0
- package/dist/rigindex/types.d.ts +76 -0
- package/dist/rigindex/types.d.ts.map +1 -0
- package/dist/rigindex/types.js +8 -0
- package/dist/routes/cache-lint.d.ts +41 -0
- package/dist/routes/cache-lint.d.ts.map +1 -0
- package/dist/routes/cache-lint.js +81 -0
- package/dist/routes/loader.d.ts +37 -0
- package/dist/routes/loader.d.ts.map +1 -1
- package/dist/routes/loader.js +138 -0
- package/dist/routes/resolved-bundle.d.ts +79 -0
- package/dist/routes/resolved-bundle.d.ts.map +1 -0
- package/dist/routes/resolved-bundle.js +148 -0
- package/dist/routes/sampling-config.d.ts +32 -0
- package/dist/routes/sampling-config.d.ts.map +1 -0
- package/dist/routes/sampling-config.js +91 -0
- package/dist/run/ajv.d.ts +22 -0
- package/dist/run/ajv.d.ts.map +1 -0
- package/dist/run/ajv.js +32 -0
- package/dist/run/cache-handle.d.ts +58 -0
- package/dist/run/cache-handle.d.ts.map +1 -0
- package/dist/run/cache-handle.js +54 -0
- package/dist/run/cache-stamp.d.ts +65 -0
- package/dist/run/cache-stamp.d.ts.map +1 -0
- package/dist/run/cache-stamp.js +0 -0
- package/dist/run/capture-write.d.ts +33 -0
- package/dist/run/capture-write.d.ts.map +1 -0
- package/dist/run/capture-write.js +55 -0
- package/dist/run/cost.d.ts.map +1 -1
- package/dist/run/cost.js +1 -1
- package/dist/run/dispatch.d.ts +99 -0
- package/dist/run/dispatch.d.ts.map +1 -0
- package/dist/run/dispatch.js +191 -0
- package/dist/run/raw.d.ts +88 -0
- package/dist/run/raw.d.ts.map +1 -0
- package/dist/run/raw.js +286 -0
- package/dist/run/record-attempt.d.ts +40 -0
- package/dist/run/record-attempt.d.ts.map +1 -0
- package/dist/run/record-attempt.js +99 -0
- package/dist/run/resolve.d.ts +75 -2
- package/dist/run/resolve.d.ts.map +1 -1
- package/dist/run/resolve.js +126 -4
- package/dist/run/run.d.ts +14 -0
- package/dist/run/run.d.ts.map +1 -1
- package/dist/run/run.js +0 -0
- package/dist/run/stream.d.ts +49 -0
- package/dist/run/stream.d.ts.map +1 -0
- package/dist/run/stream.js +87 -0
- package/dist/run/validate.d.ts.map +1 -1
- package/dist/run/validate.js +6 -7
- package/dist/savings/cache-ledger.d.ts +108 -0
- package/dist/savings/cache-ledger.d.ts.map +1 -0
- package/dist/savings/cache-ledger.js +174 -0
- package/dist/savings/cache-reconcile.d.ts +64 -0
- package/dist/savings/cache-reconcile.d.ts.map +1 -0
- package/dist/savings/cache-reconcile.js +59 -0
- package/dist/savings/cache-stats.d.ts +63 -0
- package/dist/savings/cache-stats.d.ts.map +1 -0
- package/dist/savings/cache-stats.js +108 -0
- package/dist/savings/emit.d.ts +13 -0
- package/dist/savings/emit.d.ts.map +1 -1
- package/dist/savings/emit.js +25 -0
- package/dist/savings/swap-execute.d.ts +14 -0
- package/dist/savings/swap-execute.d.ts.map +1 -1
- package/dist/savings/swap-execute.js +44 -1
- package/dist/savings/swaps-client.d.ts +14 -0
- package/dist/savings/swaps-client.d.ts.map +1 -1
- package/dist/savings/swaps-client.js +16 -2
- package/dist/settings/api-fetcher.d.ts +39 -0
- package/dist/settings/api-fetcher.d.ts.map +1 -0
- package/dist/settings/api-fetcher.js +92 -0
- package/dist/settings/index.d.ts +10 -0
- package/dist/settings/index.d.ts.map +1 -0
- package/dist/settings/index.js +15 -0
- package/dist/settings/task-settings.d.ts +91 -0
- package/dist/settings/task-settings.d.ts.map +1 -0
- package/dist/settings/task-settings.js +116 -0
- package/dist/status.d.ts +77 -0
- package/dist/status.d.ts.map +1 -0
- package/dist/status.js +113 -0
- package/dist/telemetry/artifact-store.d.ts +48 -0
- package/dist/telemetry/artifact-store.d.ts.map +1 -0
- package/dist/telemetry/artifact-store.js +189 -0
- package/dist/telemetry/cache-miss-streak.d.ts +79 -0
- package/dist/telemetry/cache-miss-streak.d.ts.map +1 -0
- package/dist/telemetry/cache-miss-streak.js +99 -0
- package/dist/telemetry/capture.d.ts +3 -0
- package/dist/telemetry/capture.d.ts.map +1 -1
- package/dist/telemetry/capture.js +15 -8
- package/dist/telemetry/exporter-setup.d.ts +28 -0
- package/dist/telemetry/exporter-setup.d.ts.map +1 -0
- package/dist/telemetry/exporter-setup.js +34 -0
- package/dist/telemetry/exporter.d.ts +75 -0
- package/dist/telemetry/exporter.d.ts.map +1 -1
- package/dist/telemetry/exporter.js +324 -47
- package/dist/telemetry/health.d.ts +76 -0
- package/dist/telemetry/health.d.ts.map +1 -0
- package/dist/telemetry/health.js +92 -0
- package/dist/telemetry/ingest-protocol.d.ts +152 -3
- package/dist/telemetry/ingest-protocol.d.ts.map +1 -1
- package/dist/telemetry/ingest-protocol.js +204 -4
- package/dist/telemetry/load-error-report.d.ts +24 -0
- package/dist/telemetry/load-error-report.d.ts.map +1 -0
- package/dist/telemetry/load-error-report.js +50 -0
- package/dist/telemetry/otel-map.d.ts +121 -0
- package/dist/telemetry/otel-map.d.ts.map +1 -0
- package/dist/telemetry/otel-map.js +224 -0
- package/dist/telemetry/otel-normalize.d.ts +104 -0
- package/dist/telemetry/otel-normalize.d.ts.map +1 -0
- package/dist/telemetry/otel-normalize.js +530 -0
- package/dist/telemetry/otlp-emitter.d.ts +52 -0
- package/dist/telemetry/otlp-emitter.d.ts.map +1 -0
- package/dist/telemetry/otlp-emitter.js +144 -0
- package/dist/telemetry/otlp-proto.d.ts +29 -0
- package/dist/telemetry/otlp-proto.d.ts.map +1 -0
- package/dist/telemetry/otlp-proto.js +266 -0
- package/dist/telemetry/sinks.d.ts +37 -1
- package/dist/telemetry/sinks.d.ts.map +1 -1
- package/dist/telemetry/sinks.js +121 -30
- package/dist/telemetry/store.d.ts +28 -1
- package/dist/telemetry/store.d.ts.map +1 -1
- package/dist/telemetry/store.js +218 -3
- package/dist/tenancy/api-key-format.d.ts +15 -5
- package/dist/tenancy/api-key-format.d.ts.map +1 -1
- package/dist/tenancy/api-key-format.js +24 -5
- package/dist/types/config-teaching.d.ts +62 -0
- package/dist/types/config-teaching.d.ts.map +1 -0
- package/dist/types/config-teaching.js +183 -0
- package/dist/types/provider.d.ts +122 -1
- package/dist/types/provider.d.ts.map +1 -1
- package/dist/types/provider.js +21 -1
- package/dist/types/route.d.ts +79 -1
- package/dist/types/route.d.ts.map +1 -1
- package/dist/types/telemetry.d.ts +117 -0
- package/dist/types/telemetry.d.ts.map +1 -1
- package/dist/validate-cli.d.ts +12 -0
- package/dist/validate-cli.d.ts.map +1 -0
- package/dist/validate-cli.js +57 -0
- package/dist/verify/capture.d.ts +66 -0
- package/dist/verify/capture.d.ts.map +1 -0
- package/dist/verify/capture.js +180 -0
- package/dist/verify/cli-run.d.ts +23 -0
- package/dist/verify/cli-run.d.ts.map +1 -0
- package/dist/verify/cli-run.js +57 -0
- package/dist/verify/diff.d.ts +48 -0
- package/dist/verify/diff.d.ts.map +1 -0
- package/dist/verify/diff.js +157 -0
- package/dist/verify/index.d.ts +17 -0
- package/dist/verify/index.d.ts.map +1 -0
- package/dist/verify/index.js +24 -0
- package/dist/verify/report.d.ts +14 -0
- package/dist/verify/report.d.ts.map +1 -0
- package/dist/verify/report.js +88 -0
- package/dist/verify/types.d.ts +101 -0
- package/dist/verify/types.d.ts.map +1 -0
- package/dist/verify/types.js +17 -0
- package/dist/verify/verify.d.ts +29 -0
- package/dist/verify/verify.d.ts.map +1 -0
- package/dist/verify/verify.js +148 -0
- package/dist/watch/evaluate.d.ts +162 -3
- package/dist/watch/evaluate.d.ts.map +1 -1
- package/dist/watch/evaluate.js +61 -0
- package/dist/watch/monitor.d.ts +116 -0
- package/dist/watch/monitor.d.ts.map +1 -0
- package/dist/watch/monitor.js +199 -0
- package/dist/watch/proposals.d.ts +20 -4
- package/dist/watch/proposals.d.ts.map +1 -1
- package/dist/watch/proposals.js +31 -3
- package/dist/watch/run.d.ts +33 -0
- package/dist/watch/run.d.ts.map +1 -1
- package/dist/watch/run.js +39 -0
- package/package.json +7 -1
- package/registry/LICENSE +20 -0
- package/registry/leaderboard.json +3639 -0
- package/registry/registry.json +7263 -0
- package/dist/telemetry/supabase-exporter.d.ts +0 -64
- package/dist/telemetry/supabase-exporter.d.ts.map +0 -1
- package/dist/telemetry/supabase-exporter.js +0 -211
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Dispatch helpers extracted from run.ts (G-1/G-5 Phase A). The run loop is at
|
|
4
|
+
* its LOC cap, so the tool-passthrough and streaming seams live here — the same
|
|
5
|
+
* move G-2 made when it pulled applyRunRequirements/candidateFilterFromOptions
|
|
6
|
+
* into resolve.ts. Pure/near-pure: request shaping, the stream-vs-buffered
|
|
7
|
+
* dispatch fork, and the tool-call RunResult builder.
|
|
8
|
+
*
|
|
9
|
+
* Tool *execution* is never ours (spec §4.4); these helpers normalize the
|
|
10
|
+
* protocol and surface it — nothing here runs a tool.
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.recordSearchCostRow = recordSearchCostRow;
|
|
14
|
+
exports.buildAdapterRequest = buildAdapterRequest;
|
|
15
|
+
exports.buildRepairAdapterRequest = buildRepairAdapterRequest;
|
|
16
|
+
exports.dispatchCandidate = dispatchCandidate;
|
|
17
|
+
exports.emitToolCallResult = emitToolCallResult;
|
|
18
|
+
const node_crypto_1 = require("node:crypto");
|
|
19
|
+
const cache_stamp_1 = require("./cache-stamp");
|
|
20
|
+
const cache_handle_1 = require("./cache-handle");
|
|
21
|
+
/** Record the grounding-inject search's own telemetry row (WS3): a $-only line
|
|
22
|
+
* under the synthetic "search" provider. Extracted from run.ts to keep it under
|
|
23
|
+
* the LOC cap; behaviour is byte-identical to the prior inline recorder. */
|
|
24
|
+
function recordSearchCostRow(telemetry, route, effectiveTags, servedVariant, searchProviderId, costUsd) {
|
|
25
|
+
telemetry.record({
|
|
26
|
+
id: (0, node_crypto_1.randomUUID)(),
|
|
27
|
+
ts: new Date().toISOString(),
|
|
28
|
+
route: route.route,
|
|
29
|
+
routeVersion: route.version,
|
|
30
|
+
provider: "search",
|
|
31
|
+
model: searchProviderId,
|
|
32
|
+
requestedTier: "standard",
|
|
33
|
+
servedTier: "standard",
|
|
34
|
+
tokensIn: 0,
|
|
35
|
+
tokensOut: 0,
|
|
36
|
+
tokensCached: 0,
|
|
37
|
+
tokensCacheWrite: 0,
|
|
38
|
+
tokensCacheWrite1h: 0,
|
|
39
|
+
costEstimateUsd: costUsd,
|
|
40
|
+
latencyMs: 0,
|
|
41
|
+
ttfbMs: null,
|
|
42
|
+
failureClass: null,
|
|
43
|
+
attemptsJson: "{}",
|
|
44
|
+
tagsJson: JSON.stringify(effectiveTags),
|
|
45
|
+
fingerprintCell: route.fingerprintCell,
|
|
46
|
+
servedVariant,
|
|
47
|
+
repairedBy: null,
|
|
48
|
+
pricingMissing: false,
|
|
49
|
+
cacheKey: null,
|
|
50
|
+
prefixFingerprint: null,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/** Build the primary-attempt AdapterRequest — adds the grounding hint and the
|
|
54
|
+
* unified tools/toolChoice passthrough (spec §4.4). */
|
|
55
|
+
function buildAdapterRequest(parts) {
|
|
56
|
+
const { route, candidate, systemPrompt, userDirective, nativeGrounded, opts } = parts;
|
|
57
|
+
// Request-path caching, two layers with a strict precedence (CE-1 spec §2.2):
|
|
58
|
+
// (1) a CUSTOMER-supplied handle (RunOptions.cache) for the matching
|
|
59
|
+
// provider WINS — it is a real provider-native resource the customer
|
|
60
|
+
// owns. On gemini this is a `cachedContents/<id>`; the gemini adapter
|
|
61
|
+
// then drops systemInstruction+tools by design (they are BAKED INTO the
|
|
62
|
+
// cache at creation, and the API rejects setting both — gemini.ts:145).
|
|
63
|
+
// That is correct API behavior, not a bug; the teaching doc explains it.
|
|
64
|
+
// The WS-C gemini exclusion is untouched — it covers GENERATED keys only.
|
|
65
|
+
// (2) else the WS-C `cache: auto` generated stamp — same pure gate the
|
|
66
|
+
// recorder uses, so wire and telemetry never disagree (gemini excluded
|
|
67
|
+
// here by that gate, not by prose).
|
|
68
|
+
// Absent RunOptions.cache, (1) is null and this is byte-identical to main.
|
|
69
|
+
const customerCache = (0, cache_handle_1.customerCacheHandle)(opts.cache, candidate, parts.suppressCustomerCache === true);
|
|
70
|
+
const cacheStamp = (0, cache_stamp_1.cacheStampForCandidate)(route, candidate);
|
|
71
|
+
const cacheDirective = customerCache ?? cacheStamp.request;
|
|
72
|
+
return {
|
|
73
|
+
candidate,
|
|
74
|
+
systemPrompt,
|
|
75
|
+
userPrompt: userDirective,
|
|
76
|
+
outputSchema: route.outputSchema,
|
|
77
|
+
requestedTier: route.policy.tier,
|
|
78
|
+
timeoutMs: route.policy.timeoutMs,
|
|
79
|
+
...(cacheDirective !== null ? { cache: cacheDirective } : {}),
|
|
80
|
+
...(route.require.includes("grounded") && nativeGrounded ? { grounding: true } : {}),
|
|
81
|
+
// CE-6: the route's declared sampling rides every primary attempt. Absent ⇒
|
|
82
|
+
// no `sampling` key ⇒ adapters keep their constants/omissions (byte-identical).
|
|
83
|
+
...(route.policy.sampling !== undefined ? { sampling: route.policy.sampling } : {}),
|
|
84
|
+
...(opts.tools !== undefined ? { tools: opts.tools } : {}),
|
|
85
|
+
...(opts.toolChoice !== undefined ? { toolChoice: opts.toolChoice } : {}),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** Build the repair-attempt AdapterRequest (no tools — repair re-asks for the
|
|
89
|
+
* structured object, never a tool call). */
|
|
90
|
+
function buildRepairAdapterRequest(route, candidate, systemPrompt, repairDirective) {
|
|
91
|
+
// No cache stamp (WS-C): a repair re-asks with a DIFFERENT prompt (error
|
|
92
|
+
// feedback / repair-model framing), so it does not share the route's cacheable
|
|
93
|
+
// prefix identity. Sending the primary fingerprint as a routing key here would
|
|
94
|
+
// point dissimilar prompts at the same cache slot (openai explicitly warns
|
|
95
|
+
// against that) and would misfile the row in WS-B's warm-set. The recorder
|
|
96
|
+
// (record-attempt.ts) skips repair rows symmetrically.
|
|
97
|
+
return {
|
|
98
|
+
candidate,
|
|
99
|
+
systemPrompt,
|
|
100
|
+
userPrompt: repairDirective,
|
|
101
|
+
outputSchema: route.outputSchema,
|
|
102
|
+
requestedTier: route.policy.tier,
|
|
103
|
+
timeoutMs: route.policy.timeoutMs,
|
|
104
|
+
// CE-6: repair / JSON-extractor followups inherit the route's sampling — one
|
|
105
|
+
// knob per route, not per rung (spec §3). A repair re-ask at a different
|
|
106
|
+
// temperature than the primary would be a silent behaviour split.
|
|
107
|
+
...(route.policy.sampling !== undefined ? { sampling: route.policy.sampling } : {}),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Dispatch a candidate. When a delta sink is supplied AND the adapter can
|
|
112
|
+
* stream, drain its stream — forwarding raw text deltas to the sink — and
|
|
113
|
+
* return the terminal `AdapterResult` (identical to what `call()` returns, so
|
|
114
|
+
* the run loop's cost/validation/telemetry logic is unchanged). Otherwise
|
|
115
|
+
* buffered `call()`.
|
|
116
|
+
*/
|
|
117
|
+
async function dispatchCandidate(adapter, req, onDelta) {
|
|
118
|
+
if (onDelta === undefined || adapter.stream === undefined) {
|
|
119
|
+
return adapter.call(req);
|
|
120
|
+
}
|
|
121
|
+
let final = null;
|
|
122
|
+
for await (const ev of adapter.stream(req)) {
|
|
123
|
+
if (ev.kind === "delta") {
|
|
124
|
+
if (ev.text !== "")
|
|
125
|
+
onDelta({ text: ev.text });
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
final = ev.result;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return (final ?? {
|
|
132
|
+
ok: false,
|
|
133
|
+
failure: {
|
|
134
|
+
class: "network",
|
|
135
|
+
message: "adapter stream ended without a terminal result",
|
|
136
|
+
provider: req.candidate.provider,
|
|
137
|
+
model: req.candidate.model,
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Record a tool-call turn and build its RunResult (spec §4.4). A tool call is
|
|
143
|
+
* NOT a schema-validated JSON turn: it records a normal (non-failure) telemetry
|
|
144
|
+
* row, then returns a result with `validated: false`, `output: null` (the caller
|
|
145
|
+
* reads `meta.toolCalls`, runs the tool, and continues the exchange), and the
|
|
146
|
+
* normalized calls surfaced on the meta. Recording goes through run.ts's own
|
|
147
|
+
* `recordAttempt` so the ledger/tenancy stamping is identical to every other row.
|
|
148
|
+
*/
|
|
149
|
+
function emitToolCallResult(record, parts) {
|
|
150
|
+
const { route, candidate, result, costUsd, latencyMs, attemptsByClass, servedVariant } = parts;
|
|
151
|
+
const inferenceId = record({
|
|
152
|
+
candidate,
|
|
153
|
+
servedTier: result.usage.servedTier,
|
|
154
|
+
tokensIn: result.usage.tokensIn,
|
|
155
|
+
tokensOut: result.usage.tokensOut,
|
|
156
|
+
tokensCached: result.usage.tokensCached,
|
|
157
|
+
...(result.usage.tokensCacheWrite !== undefined
|
|
158
|
+
? { tokensCacheWrite: result.usage.tokensCacheWrite }
|
|
159
|
+
: {}),
|
|
160
|
+
...(result.usage.tokensCacheWrite1h !== undefined
|
|
161
|
+
? { tokensCacheWrite1h: result.usage.tokensCacheWrite1h }
|
|
162
|
+
: {}),
|
|
163
|
+
costUsd,
|
|
164
|
+
latencyMs,
|
|
165
|
+
...(result.usage.ttfbMs !== undefined ? { ttfbMs: result.usage.ttfbMs } : {}),
|
|
166
|
+
failure: null,
|
|
167
|
+
pricingMissing: parts.pricingMissing,
|
|
168
|
+
});
|
|
169
|
+
return {
|
|
170
|
+
output: null,
|
|
171
|
+
meta: {
|
|
172
|
+
inferenceId,
|
|
173
|
+
route: route.route,
|
|
174
|
+
routeVersion: route.version,
|
|
175
|
+
provider: candidate.provider,
|
|
176
|
+
model: candidate.model,
|
|
177
|
+
requestedTier: route.policy.tier,
|
|
178
|
+
servedTier: result.usage.servedTier,
|
|
179
|
+
tokensIn: result.usage.tokensIn,
|
|
180
|
+
tokensOut: result.usage.tokensOut,
|
|
181
|
+
tokensCached: result.usage.tokensCached,
|
|
182
|
+
costEstimateUsd: costUsd,
|
|
183
|
+
latencyMs,
|
|
184
|
+
validated: false,
|
|
185
|
+
attemptsByClass,
|
|
186
|
+
servedVariant,
|
|
187
|
+
repairedBy: null,
|
|
188
|
+
toolCalls: result.toolCalls,
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The RAW LANE — HTTP gateway Phase C (spec §4.2). A zero-margin BYOK
|
|
3
|
+
* passthrough: the caller names a `provider/model` and supplies (server-side,
|
|
4
|
+
* fail-closed) their own provider key; we dispatch DIRECTLY through the matching
|
|
5
|
+
* adapter and meter the call at the provider's list price. There is NO route, NO
|
|
6
|
+
* capability gate, NO validation, NO repair, NO envelope — and, by contract, NO
|
|
7
|
+
* ModelRig margin on the hop (the never-price-the-hop rule; the cost recorded is
|
|
8
|
+
* exactly `attemptCost`'s list price, which is what the verified lane records
|
|
9
|
+
* too). This module is the SDK half; the server half (BYOK resolution + the
|
|
10
|
+
* `key_ownership` tag) lives in `modelrig-server`.
|
|
11
|
+
*
|
|
12
|
+
* WHY THIS IS AN SDK PRIMITIVE, not server code: `AdapterRequest.candidate` is a
|
|
13
|
+
* branded `CandidateRef` mintable only in `run/resolve.ts`, and adapters are
|
|
14
|
+
* constructed with their key baked in — both live here. The server resolves the
|
|
15
|
+
* BYOK key fail-closed and calls `rig.runRaw`/`rig.runRawStream`; it never
|
|
16
|
+
* touches an adapter or the brand.
|
|
17
|
+
*
|
|
18
|
+
* KNOWN LIMITATIONS (flagged for the architect, not hidden):
|
|
19
|
+
* - The adapter contract is single `systemPrompt` + `userPrompt`, not a
|
|
20
|
+
* `messages[]` array, so a multi-turn transcript is flattened by the caller
|
|
21
|
+
* (see `modelrig-server`'s `deriveRawPrompts`).
|
|
22
|
+
* - CE-6: the raw lane now forwards `temperature`/`topP` (via
|
|
23
|
+
* `AdapterRequest.sampling`) when the caller supplies them; absent, they are
|
|
24
|
+
* still dropped and the dispatch is byte-identical to pre-CE-6. `max_tokens`
|
|
25
|
+
* maps to `maxOutputTokens` (unchanged).
|
|
26
|
+
*/
|
|
27
|
+
import type { AdapterRequest, CandidateRef, ProviderId, ToolChoice, ToolDefinition } from "../types/provider";
|
|
28
|
+
import type { RunResult, RunTags } from "../types/route";
|
|
29
|
+
import type { RunStreamHandle } from "./stream";
|
|
30
|
+
import type { TelemetryStore } from "../types/telemetry";
|
|
31
|
+
/** Raw-lane request. `systemPrompt`/`userPrompt` are already flattened from the
|
|
32
|
+
* OpenAI `messages[]` by the caller; `tags` carries the caller's dimensions
|
|
33
|
+
* (the server stamps `key_ownership` here). `lane: "raw"` is stamped by this
|
|
34
|
+
* module and cannot be overridden. */
|
|
35
|
+
export interface RawRunInput {
|
|
36
|
+
readonly provider: ProviderId;
|
|
37
|
+
readonly model: string;
|
|
38
|
+
/** The caller's BYOK provider secret, resolved fail-closed server-side. Held
|
|
39
|
+
* in memory for this one call, then dropped (never logged, never stored). */
|
|
40
|
+
readonly apiKey: string;
|
|
41
|
+
readonly systemPrompt: string;
|
|
42
|
+
readonly userPrompt: string;
|
|
43
|
+
readonly tags?: RunTags;
|
|
44
|
+
readonly maxOutputTokens?: number;
|
|
45
|
+
/** CE-6 sampling on the raw lane (spec §2). `temperature`/`topP` are the raw
|
|
46
|
+
* lane's exposure of the same knobs `RoutePolicy.sampling` gives the routed
|
|
47
|
+
* lane; `maxOutputTokens` above already existed. Absent ⇒ byte-identical raw
|
|
48
|
+
* dispatch (the params were previously dropped, per the header note). */
|
|
49
|
+
readonly temperature?: number;
|
|
50
|
+
readonly topP?: number;
|
|
51
|
+
readonly timeoutMs?: number;
|
|
52
|
+
readonly tools?: readonly ToolDefinition[];
|
|
53
|
+
readonly toolChoice?: ToolChoice;
|
|
54
|
+
/** CE-1 customer cache-handle pass-through on the Lane-B seam (spec §1.3
|
|
55
|
+
* success metric: "…passes its existing cachedContent handle through
|
|
56
|
+
* rig.run/runRaw"). The raw lane has exactly ONE provider (`provider` above),
|
|
57
|
+
* so the handle is unconditionally for it — no provider matching. Forwarded
|
|
58
|
+
* verbatim to `AdapterRequest.cache`; ModelRig never manages the resource. The
|
|
59
|
+
* stamped row carries the customer key with a null `prefix_fingerprint` (the
|
|
60
|
+
* raw lane runs no WS-C stamping), so provenance reads `customer` by the
|
|
61
|
+
* derivation rule. */
|
|
62
|
+
readonly cache?: {
|
|
63
|
+
readonly key: string;
|
|
64
|
+
readonly ttlSeconds?: number;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export interface RawRunDeps {
|
|
68
|
+
readonly telemetry: TelemetryStore;
|
|
69
|
+
/** Mirrors RunDeps: the conservative per-MTok rate for an unpriced model.
|
|
70
|
+
* Used only to keep `attemptCost` honest — the raw lane has no envelope, so
|
|
71
|
+
* it never charges the unpriced fallback; a missing price records `$0`. */
|
|
72
|
+
readonly unpricedRateUsdPerMTok?: number;
|
|
73
|
+
}
|
|
74
|
+
/** Pure raw-lane request shaping — exported for unit tests (no network). */
|
|
75
|
+
export declare function buildRawRequest(candidate: CandidateRef, input: RawRunInput): AdapterRequest;
|
|
76
|
+
/** Buffered raw dispatch (spec §4.2). Records telemetry, returns the RunResult,
|
|
77
|
+
* or throws `RigFailureError` on an adapter failure. */
|
|
78
|
+
export declare function runRaw(deps: RawRunDeps, input: RawRunInput): Promise<RunResult>;
|
|
79
|
+
/**
|
|
80
|
+
* Streaming raw dispatch (spec §4.3, "raw lane: true streaming, deltas passed
|
|
81
|
+
* through"). Drains the adapter's own stream when it has one, forwarding raw
|
|
82
|
+
* text deltas, and emits a single terminal `final` RunResult; adapters without
|
|
83
|
+
* `stream` (anthropic/gemini) fall back to a buffered `call()` with no deltas.
|
|
84
|
+
* There is NO terminal-validation event — that is a structured-route concept;
|
|
85
|
+
* the raw lane has no schema and the streamed bytes ARE the answer.
|
|
86
|
+
*/
|
|
87
|
+
export declare function runRawStream(deps: RawRunDeps, input: RawRunInput): RunStreamHandle;
|
|
88
|
+
//# sourceMappingURL=raw.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"raw.d.ts","sourceRoot":"","sources":["../../src/run/raw.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAYH,OAAO,KAAK,EACV,cAAc,EAGd,YAAY,EAGZ,UAAU,EACV,UAAU,EACV,cAAc,EACf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAW,SAAS,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,KAAK,EAAkB,eAAe,EAAE,MAAM,UAAU,CAAC;AAChE,OAAO,KAAK,EAAgB,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEvE;;;sCAGsC;AACtC,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC;IAC9B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB;iFAC6E;IAC7E,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;IAClC;;;6EAGyE;IACzE,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,cAAc,EAAE,CAAC;IAC3C,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC;IACjC;;;;;;;0BAOsB;IACtB,QAAQ,CAAC,KAAK,CAAC,EAAE;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACzE;AAED,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,SAAS,EAAE,cAAc,CAAC;IACnC;;+EAE2E;IAC3E,QAAQ,CAAC,sBAAsB,CAAC,EAAE,MAAM,CAAC;CAC1C;AA0CD,4EAA4E;AAC5E,wBAAgB,eAAe,CAAC,SAAS,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,GAAG,cAAc,CAyB3F;AA+ID;wDACwD;AACxD,wBAAsB,MAAM,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CAMrF;AAcD;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,WAAW,GAAG,eAAe,CAyClF"}
|
package/dist/run/raw.js
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The RAW LANE — HTTP gateway Phase C (spec §4.2). A zero-margin BYOK
|
|
4
|
+
* passthrough: the caller names a `provider/model` and supplies (server-side,
|
|
5
|
+
* fail-closed) their own provider key; we dispatch DIRECTLY through the matching
|
|
6
|
+
* adapter and meter the call at the provider's list price. There is NO route, NO
|
|
7
|
+
* capability gate, NO validation, NO repair, NO envelope — and, by contract, NO
|
|
8
|
+
* ModelRig margin on the hop (the never-price-the-hop rule; the cost recorded is
|
|
9
|
+
* exactly `attemptCost`'s list price, which is what the verified lane records
|
|
10
|
+
* too). This module is the SDK half; the server half (BYOK resolution + the
|
|
11
|
+
* `key_ownership` tag) lives in `modelrig-server`.
|
|
12
|
+
*
|
|
13
|
+
* WHY THIS IS AN SDK PRIMITIVE, not server code: `AdapterRequest.candidate` is a
|
|
14
|
+
* branded `CandidateRef` mintable only in `run/resolve.ts`, and adapters are
|
|
15
|
+
* constructed with their key baked in — both live here. The server resolves the
|
|
16
|
+
* BYOK key fail-closed and calls `rig.runRaw`/`rig.runRawStream`; it never
|
|
17
|
+
* touches an adapter or the brand.
|
|
18
|
+
*
|
|
19
|
+
* KNOWN LIMITATIONS (flagged for the architect, not hidden):
|
|
20
|
+
* - The adapter contract is single `systemPrompt` + `userPrompt`, not a
|
|
21
|
+
* `messages[]` array, so a multi-turn transcript is flattened by the caller
|
|
22
|
+
* (see `modelrig-server`'s `deriveRawPrompts`).
|
|
23
|
+
* - CE-6: the raw lane now forwards `temperature`/`topP` (via
|
|
24
|
+
* `AdapterRequest.sampling`) when the caller supplies them; absent, they are
|
|
25
|
+
* still dropped and the dispatch is byte-identical to pre-CE-6. `max_tokens`
|
|
26
|
+
* maps to `maxOutputTokens` (unchanged).
|
|
27
|
+
*/
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.buildRawRequest = buildRawRequest;
|
|
30
|
+
exports.runRaw = runRaw;
|
|
31
|
+
exports.runRawStream = runRawStream;
|
|
32
|
+
const node_crypto_1 = require("node:crypto");
|
|
33
|
+
const anthropic_1 = require("../adapters/anthropic");
|
|
34
|
+
const deepseek_1 = require("../adapters/deepseek");
|
|
35
|
+
const gemini_1 = require("../adapters/gemini");
|
|
36
|
+
const grok_1 = require("../adapters/grok");
|
|
37
|
+
const openai_1 = require("../adapters/openai");
|
|
38
|
+
const openai_compat_1 = require("../adapters/openai-compat");
|
|
39
|
+
const cost_1 = require("./cost");
|
|
40
|
+
const resolve_1 = require("./resolve");
|
|
41
|
+
const failures_1 = require("../types/failures");
|
|
42
|
+
/** Absent a route timeout, the raw lane uses a generous default — a passthrough
|
|
43
|
+
* should not be shorter-fused than the underlying provider SDK. */
|
|
44
|
+
const DEFAULT_RAW_TIMEOUT_MS = 120000;
|
|
45
|
+
/** Conservative compat-host capability baseline — parity with createRig's
|
|
46
|
+
* COMPAT_STATIC. Inconsequential for raw calls (outputSchema is always null, so
|
|
47
|
+
* no JSON shaping fires), kept aligned so a raw and a routed compat call shape
|
|
48
|
+
* identically if a schema is ever added. */
|
|
49
|
+
const RAW_COMPAT_PROFILE = new Set(["json_mode"]);
|
|
50
|
+
/** Construct a THROWAWAY adapter bound to the caller's BYOK key for one call.
|
|
51
|
+
* Each adapter builds its provider client fresh inside `call()`/`stream()`, so
|
|
52
|
+
* a per-call instance carries no shared state and leaks no key. Exhaustive over
|
|
53
|
+
* ProviderId — a new provider forces a compile error here. */
|
|
54
|
+
function buildRawAdapter(provider, apiKey) {
|
|
55
|
+
switch (provider) {
|
|
56
|
+
case "openai":
|
|
57
|
+
return (0, openai_1.createOpenAIAdapter)({ apiKey });
|
|
58
|
+
case "anthropic":
|
|
59
|
+
return (0, anthropic_1.createAnthropicAdapter)({ apiKey });
|
|
60
|
+
case "gemini":
|
|
61
|
+
return (0, gemini_1.createGeminiAdapter)({ apiKey });
|
|
62
|
+
case "grok":
|
|
63
|
+
return (0, grok_1.createGrokAdapter)({ apiKey });
|
|
64
|
+
case "deepseek":
|
|
65
|
+
return (0, deepseek_1.createDeepSeekAdapter)({ apiKey });
|
|
66
|
+
case "deepinfra":
|
|
67
|
+
case "fireworks": {
|
|
68
|
+
const host = openai_compat_1.OPENAI_COMPAT_HOSTS[provider];
|
|
69
|
+
return (0, openai_compat_1.createOpenAICompatAdapter)({
|
|
70
|
+
providerId: provider,
|
|
71
|
+
baseUrl: host.baseUrl,
|
|
72
|
+
apiKey,
|
|
73
|
+
...(host.shapeModelId !== undefined ? { shapeModelId: host.shapeModelId } : {}),
|
|
74
|
+
capabilityProfile: () => RAW_COMPAT_PROFILE,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** Pure raw-lane request shaping — exported for unit tests (no network). */
|
|
80
|
+
function buildRawRequest(candidate, input) {
|
|
81
|
+
// CE-6: forward temperature/topP through AdapterRequest.sampling ONLY when the
|
|
82
|
+
// caller declared at least one — absent, no `sampling` key is added and the
|
|
83
|
+
// request is byte-identical to pre-CE-6. maxOutputTokens keeps its top-level
|
|
84
|
+
// channel (the adapter resolves sampling.maxOutputTokens ?? maxOutputTokens).
|
|
85
|
+
const sampling = input.temperature !== undefined || input.topP !== undefined
|
|
86
|
+
? {
|
|
87
|
+
...(input.temperature !== undefined ? { temperature: input.temperature } : {}),
|
|
88
|
+
...(input.topP !== undefined ? { topP: input.topP } : {}),
|
|
89
|
+
}
|
|
90
|
+
: undefined;
|
|
91
|
+
return {
|
|
92
|
+
candidate,
|
|
93
|
+
systemPrompt: input.systemPrompt,
|
|
94
|
+
userPrompt: input.userPrompt,
|
|
95
|
+
outputSchema: null, // raw lane never validates — the product distinction
|
|
96
|
+
requestedTier: "standard",
|
|
97
|
+
timeoutMs: input.timeoutMs ?? DEFAULT_RAW_TIMEOUT_MS,
|
|
98
|
+
...(input.maxOutputTokens !== undefined ? { maxOutputTokens: input.maxOutputTokens } : {}),
|
|
99
|
+
...(sampling !== undefined ? { sampling } : {}),
|
|
100
|
+
...(input.tools !== undefined ? { tools: input.tools } : {}),
|
|
101
|
+
...(input.toolChoice !== undefined ? { toolChoice: input.toolChoice } : {}),
|
|
102
|
+
...(input.cache !== undefined ? { cache: input.cache } : {}),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/** Write the metered raw-lane telemetry row. Mirrors the run loop's minimal
|
|
106
|
+
* `recordSearchCostRow` shape: only load-bearing fields set, route = the
|
|
107
|
+
* `provider/model` handle the caller used, lane + key_ownership riding in
|
|
108
|
+
* `tags_json`. The exporter mirrors it on its normal cadence — no lane column,
|
|
109
|
+
* no schema change (telemetry map, 2026-08-10). */
|
|
110
|
+
function recordRawRow(telemetry, p) {
|
|
111
|
+
const row = {
|
|
112
|
+
id: p.id,
|
|
113
|
+
ts: new Date().toISOString(),
|
|
114
|
+
route: p.rawModelId,
|
|
115
|
+
routeVersion: 0,
|
|
116
|
+
provider: p.provider,
|
|
117
|
+
model: p.model,
|
|
118
|
+
requestedTier: "standard",
|
|
119
|
+
servedTier: p.usage?.servedTier ?? "standard",
|
|
120
|
+
tokensIn: p.usage?.tokensIn ?? 0,
|
|
121
|
+
tokensOut: p.usage?.tokensOut ?? 0,
|
|
122
|
+
tokensCached: p.usage?.tokensCached ?? 0,
|
|
123
|
+
tokensCacheWrite: p.usage?.tokensCacheWrite ?? 0,
|
|
124
|
+
tokensCacheWrite1h: p.usage?.tokensCacheWrite1h ?? 0,
|
|
125
|
+
costEstimateUsd: p.costUsd,
|
|
126
|
+
latencyMs: p.latencyMs,
|
|
127
|
+
ttfbMs: p.usage?.ttfbMs ?? null,
|
|
128
|
+
failureClass: p.failureClass,
|
|
129
|
+
attemptsJson: "{}",
|
|
130
|
+
tagsJson: JSON.stringify(p.tags),
|
|
131
|
+
fingerprintCell: null,
|
|
132
|
+
servedVariant: null,
|
|
133
|
+
repairedBy: null,
|
|
134
|
+
pricingMissing: p.pricingMissing,
|
|
135
|
+
// CE-1: a customer cache handle (if supplied) stamps `cache_key`; the raw
|
|
136
|
+
// lane runs no WS-C stamping, so `prefix_fingerprint` is always null and the
|
|
137
|
+
// provenance rule reads any non-null key here as `customer`.
|
|
138
|
+
cacheKey: p.cacheKey ?? null,
|
|
139
|
+
prefixFingerprint: null,
|
|
140
|
+
};
|
|
141
|
+
telemetry.record(row);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Turn an `AdapterResult` into a RunResult, recording exactly one telemetry row.
|
|
145
|
+
* Shared by the buffered and streaming paths so cost/telemetry are byte-identical.
|
|
146
|
+
* On a failure result: record the (possibly billed) row, then throw
|
|
147
|
+
* `RigFailureError` — the server maps it to a status exactly like the slug lane.
|
|
148
|
+
*/
|
|
149
|
+
function finalizeRaw(deps, input, result, latencyMs) {
|
|
150
|
+
const unpricedRatePerTok = (deps.unpricedRateUsdPerMTok ?? cost_1.DEFAULT_UNPRICED_RATE_USD_PER_MTOK) / 1000000;
|
|
151
|
+
const rawModelId = `${input.provider}/${input.model}`;
|
|
152
|
+
// lane is SDK-owned and cannot be overridden by a caller-supplied tag.
|
|
153
|
+
const tags = { ...(input.tags ?? {}), lane: "raw" };
|
|
154
|
+
if (!result.ok) {
|
|
155
|
+
let costUsd = 0;
|
|
156
|
+
let pricingMissing = false;
|
|
157
|
+
if (result.usage !== undefined) {
|
|
158
|
+
const settled = (0, cost_1.attemptCost)(input.provider, input.model, result.usage, unpricedRatePerTok);
|
|
159
|
+
costUsd = settled.costUsd;
|
|
160
|
+
pricingMissing = settled.pricingMissing;
|
|
161
|
+
}
|
|
162
|
+
recordRawRow(deps.telemetry, {
|
|
163
|
+
id: (0, node_crypto_1.randomUUID)(),
|
|
164
|
+
rawModelId,
|
|
165
|
+
provider: input.provider,
|
|
166
|
+
model: input.model,
|
|
167
|
+
...(result.usage !== undefined ? { usage: result.usage } : {}),
|
|
168
|
+
costUsd,
|
|
169
|
+
latencyMs,
|
|
170
|
+
failureClass: result.failure.class,
|
|
171
|
+
tags,
|
|
172
|
+
pricingMissing,
|
|
173
|
+
...(input.cache !== undefined ? { cacheKey: input.cache.key } : {}),
|
|
174
|
+
});
|
|
175
|
+
throw new failures_1.RigFailureError(result.failure);
|
|
176
|
+
}
|
|
177
|
+
const { costUsd, pricingMissing } = (0, cost_1.attemptCost)(input.provider, input.model, result.usage, unpricedRatePerTok);
|
|
178
|
+
const id = (0, node_crypto_1.randomUUID)();
|
|
179
|
+
recordRawRow(deps.telemetry, {
|
|
180
|
+
id,
|
|
181
|
+
rawModelId,
|
|
182
|
+
provider: input.provider,
|
|
183
|
+
model: input.model,
|
|
184
|
+
usage: result.usage,
|
|
185
|
+
costUsd,
|
|
186
|
+
latencyMs,
|
|
187
|
+
failureClass: null,
|
|
188
|
+
tags,
|
|
189
|
+
pricingMissing,
|
|
190
|
+
...(input.cache !== undefined ? { cacheKey: input.cache.key } : {}),
|
|
191
|
+
});
|
|
192
|
+
const toolCalls = result.toolCalls;
|
|
193
|
+
const isToolTurn = toolCalls !== undefined && toolCalls.length > 0;
|
|
194
|
+
const meta = {
|
|
195
|
+
inferenceId: id,
|
|
196
|
+
route: rawModelId,
|
|
197
|
+
routeVersion: 0,
|
|
198
|
+
provider: input.provider,
|
|
199
|
+
model: input.model,
|
|
200
|
+
requestedTier: "standard",
|
|
201
|
+
servedTier: result.usage.servedTier,
|
|
202
|
+
tokensIn: result.usage.tokensIn,
|
|
203
|
+
tokensOut: result.usage.tokensOut,
|
|
204
|
+
tokensCached: result.usage.tokensCached,
|
|
205
|
+
costEstimateUsd: costUsd,
|
|
206
|
+
latencyMs,
|
|
207
|
+
validated: false, // raw is NEVER validated — the central product claim
|
|
208
|
+
attemptsByClass: {},
|
|
209
|
+
servedVariant: null,
|
|
210
|
+
repairedBy: null,
|
|
211
|
+
...(isToolTurn ? { toolCalls } : {}),
|
|
212
|
+
};
|
|
213
|
+
return { output: isToolTurn ? null : result.text, meta };
|
|
214
|
+
}
|
|
215
|
+
/** Buffered raw dispatch (spec §4.2). Records telemetry, returns the RunResult,
|
|
216
|
+
* or throws `RigFailureError` on an adapter failure. */
|
|
217
|
+
async function runRaw(deps, input) {
|
|
218
|
+
const adapter = buildRawAdapter(input.provider, input.apiKey);
|
|
219
|
+
const candidate = (0, resolve_1.mintRawCandidate)(input.provider, input.model);
|
|
220
|
+
const started = Date.now();
|
|
221
|
+
const result = await adapter.call(buildRawRequest(candidate, input));
|
|
222
|
+
return finalizeRaw(deps, input, result, Date.now() - started);
|
|
223
|
+
}
|
|
224
|
+
function streamEndedFailure(input) {
|
|
225
|
+
return {
|
|
226
|
+
ok: false,
|
|
227
|
+
failure: {
|
|
228
|
+
class: "network",
|
|
229
|
+
message: "adapter stream ended without a terminal result",
|
|
230
|
+
provider: input.provider,
|
|
231
|
+
model: input.model,
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Streaming raw dispatch (spec §4.3, "raw lane: true streaming, deltas passed
|
|
237
|
+
* through"). Drains the adapter's own stream when it has one, forwarding raw
|
|
238
|
+
* text deltas, and emits a single terminal `final` RunResult; adapters without
|
|
239
|
+
* `stream` (anthropic/gemini) fall back to a buffered `call()` with no deltas.
|
|
240
|
+
* There is NO terminal-validation event — that is a structured-route concept;
|
|
241
|
+
* the raw lane has no schema and the streamed bytes ARE the answer.
|
|
242
|
+
*/
|
|
243
|
+
function runRawStream(deps, input) {
|
|
244
|
+
let settle;
|
|
245
|
+
let fail;
|
|
246
|
+
const result = new Promise((res, rej) => {
|
|
247
|
+
settle = res;
|
|
248
|
+
fail = rej;
|
|
249
|
+
});
|
|
250
|
+
// Mark handled so an unconsumed rejection never becomes an unhandledRejection;
|
|
251
|
+
// the events iterator surfaces the same error to whoever drains it.
|
|
252
|
+
result.catch(() => { });
|
|
253
|
+
async function* events() {
|
|
254
|
+
const adapter = buildRawAdapter(input.provider, input.apiKey);
|
|
255
|
+
const candidate = (0, resolve_1.mintRawCandidate)(input.provider, input.model);
|
|
256
|
+
const req = buildRawRequest(candidate, input);
|
|
257
|
+
const started = Date.now();
|
|
258
|
+
try {
|
|
259
|
+
let adapterResult;
|
|
260
|
+
if (adapter.stream !== undefined) {
|
|
261
|
+
let final = null;
|
|
262
|
+
for await (const ev of adapter.stream(req)) {
|
|
263
|
+
if (ev.kind === "delta") {
|
|
264
|
+
if (ev.text !== "")
|
|
265
|
+
yield { type: "delta", text: ev.text };
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
final = ev.result;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
adapterResult = final ?? streamEndedFailure(input);
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
adapterResult = await adapter.call(req);
|
|
275
|
+
}
|
|
276
|
+
const runResult = finalizeRaw(deps, input, adapterResult, Date.now() - started);
|
|
277
|
+
settle(runResult);
|
|
278
|
+
yield { type: "final", result: runResult };
|
|
279
|
+
}
|
|
280
|
+
catch (err) {
|
|
281
|
+
fail(err);
|
|
282
|
+
throw err;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return { events: events(), result };
|
|
286
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The run loop's per-attempt telemetry recorder, extracted from run.ts (WS-A).
|
|
3
|
+
*
|
|
4
|
+
* run.ts sits at the 700-LOC cap; threading cache-write accounting through the
|
|
5
|
+
* row builder pushed it over, so the recorder — a self-contained mapping from
|
|
6
|
+
* an `AttemptTelemetry` to a persisted `TelemetryRow` plus the ground-truth
|
|
7
|
+
* step mirror — moves here as a factory over the loop's per-run state. Behavior
|
|
8
|
+
* is byte-identical to the former inline closure, save the additive cache
|
|
9
|
+
* fields. Keeping it separate also shrinks the run-loop conflict surface for the
|
|
10
|
+
* in-flight E4 / capture-write work (plan coordination note).
|
|
11
|
+
*/
|
|
12
|
+
import type { AttemptTelemetry } from "./dispatch";
|
|
13
|
+
import type { ActiveRunContext } from "../artifacts/run-context";
|
|
14
|
+
import type { RetryLedger } from "./retry";
|
|
15
|
+
import type { RouteBundle, RunOptions, RunTags } from "../types/route";
|
|
16
|
+
import type { TelemetryStore } from "../types/telemetry";
|
|
17
|
+
export interface RecordAttemptDeps {
|
|
18
|
+
readonly telemetry: TelemetryStore;
|
|
19
|
+
readonly route: RouteBundle;
|
|
20
|
+
readonly ledger: RetryLedger;
|
|
21
|
+
readonly effectiveTags: RunTags;
|
|
22
|
+
readonly servedVariant: string | null;
|
|
23
|
+
/** Active run context (E2 chunk A) or null when the artifact namespace is off. */
|
|
24
|
+
readonly runCtx: ActiveRunContext | null;
|
|
25
|
+
readonly autoStepKey: string | null;
|
|
26
|
+
/** CE-1: the run's customer cache handle (RunOptions.cache), or undefined.
|
|
27
|
+
* The recorder derives the customer key from the SAME pure gate the request
|
|
28
|
+
* builder uses ({@link customerCacheHandle}), so wire and row never disagree. */
|
|
29
|
+
readonly cache?: RunOptions["cache"];
|
|
30
|
+
/** CE-1 retry-drop: candidate keys whose handle was dropped after a
|
|
31
|
+
* `cache_invalid` (live reference — the run loop mutates it between calls). A
|
|
32
|
+
* dropped candidate's later rows carry no customer key (they dispatched
|
|
33
|
+
* uncached), exactly matching the request builder's suppression. */
|
|
34
|
+
readonly droppedCacheHandle: ReadonlySet<string>;
|
|
35
|
+
}
|
|
36
|
+
/** Build the run loop's `recordAttempt(attempt) => inferenceId` over this run's
|
|
37
|
+
* state. Records one inference row and, with an active run context, mirrors it
|
|
38
|
+
* as a ground-truth step (fail-open). */
|
|
39
|
+
export declare function createRecordAttempt(deps: RecordAttemptDeps): (attempt: AttemptTelemetry) => string;
|
|
40
|
+
//# sourceMappingURL=record-attempt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record-attempt.d.ts","sourceRoot":"","sources":["../../src/run/record-attempt.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACvE,OAAO,KAAK,EAAgB,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAIvE,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,SAAS,EAAE,cAAc,CAAC;IACnC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAChC,QAAQ,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IACtC,kFAAkF;IAClF,QAAQ,CAAC,MAAM,EAAE,gBAAgB,GAAG,IAAI,CAAC;IACzC,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC;;qFAEiF;IACjF,QAAQ,CAAC,KAAK,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;IACrC;;;wEAGoE;IACpE,QAAQ,CAAC,kBAAkB,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;CAClD;AAED;;yCAEyC;AACzC,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,iBAAiB,GAAG,CAAC,OAAO,EAAE,gBAAgB,KAAK,MAAM,CA6ElG"}
|