@trazum/core 1.81.0 → 1.82.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/README.md +34 -18
- package/dist/advisories.d.ts +33 -1
- package/dist/advisories.d.ts.map +1 -1
- package/dist/advisories.js +103 -16
- package/dist/advisories.js.map +1 -1
- package/dist/band.d.ts +131 -0
- package/dist/band.d.ts.map +1 -0
- package/dist/band.js +210 -0
- package/dist/band.js.map +1 -0
- package/dist/conform.d.ts +1 -1
- package/dist/conform.d.ts.map +1 -1
- package/dist/conform.js +84 -1
- package/dist/conform.js.map +1 -1
- package/dist/helicone.d.ts +92 -0
- package/dist/helicone.d.ts.map +1 -0
- package/dist/helicone.js +194 -0
- package/dist/helicone.js.map +1 -0
- package/dist/i18n/en.d.ts.map +1 -1
- package/dist/i18n/en.js +9 -3
- package/dist/i18n/en.js.map +1 -1
- package/dist/i18n/es.d.ts.map +1 -1
- package/dist/i18n/es.js +9 -3
- package/dist/i18n/es.js.map +1 -1
- package/dist/i18n/types.d.ts +43 -1
- package/dist/i18n/types.d.ts.map +1 -1
- package/dist/index.d.ts +10 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/litellm.d.ts +98 -0
- package/dist/litellm.d.ts.map +1 -0
- package/dist/litellm.js +207 -0
- package/dist/litellm.js.map +1 -0
- package/dist/measured-profile.d.ts +4 -0
- package/dist/measured-profile.d.ts.map +1 -1
- package/dist/measured-profile.js +22 -1
- package/dist/measured-profile.js.map +1 -1
- package/dist/schemas.d.ts.map +1 -1
- package/dist/schemas.js +36 -0
- package/dist/schemas.js.map +1 -1
- package/dist/tokenizer.d.ts +45 -49
- package/dist/tokenizer.d.ts.map +1 -1
- package/dist/tokenizer.js +100 -51
- package/dist/tokenizer.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/verdict-bridge.d.ts +113 -0
- package/dist/verdict-bridge.d.ts.map +1 -0
- package/dist/verdict-bridge.js +127 -0
- package/dist/verdict-bridge.js.map +1 -0
- package/package.json +1 -1
- package/src/advisories.ts +132 -15
- package/src/band.ts +213 -0
- package/src/conform.ts +100 -1
- package/src/helicone.ts +227 -0
- package/src/i18n/en.ts +10 -3
- package/src/i18n/es.ts +10 -3
- package/src/i18n/types.ts +45 -1
- package/src/index.ts +17 -1
- package/src/litellm.ts +243 -0
- package/src/measured-profile.ts +26 -2
- package/src/schemas.ts +36 -0
- package/src/tokenizer.ts +101 -51
- package/src/types.ts +1 -0
- package/src/verdict-bridge.ts +200 -0
package/src/measured-profile.ts
CHANGED
|
@@ -70,6 +70,10 @@ export interface MeasuredUsage {
|
|
|
70
70
|
* call is the same size. It is handed over as the better of two
|
|
71
71
|
* approximations — a measured token share beats a typed call share — and
|
|
72
72
|
* named honestly here so no rendering can call it something it is not.
|
|
73
|
+
*
|
|
74
|
+
* The denominator is every input token, **writes included**. Leaving writes
|
|
75
|
+
* out made a constantly-rewritten cache report a high hit rate, which is the
|
|
76
|
+
* one workload that most needs to be told otherwise.
|
|
73
77
|
*/
|
|
74
78
|
cacheReadShare: number;
|
|
75
79
|
/**
|
|
@@ -105,6 +109,7 @@ export function measuredUsage(
|
|
|
105
109
|
const outputTokens = slices.reduce((sum, row) => sum + row.breakdown.outputTokens, 0);
|
|
106
110
|
const inputTokens = slices.reduce((sum, row) => sum + row.breakdown.inputTokens, 0);
|
|
107
111
|
const cacheReadTokens = slices.reduce((sum, row) => sum + row.breakdown.cacheReadTokens, 0);
|
|
112
|
+
const cacheWriteTokens = slices.reduce((sum, row) => sum + row.breakdown.cacheWriteTokens, 0);
|
|
108
113
|
|
|
109
114
|
// The model carrying the most spend. Ties break on the larger call count, so
|
|
110
115
|
// the answer is stable rather than dependent on map ordering.
|
|
@@ -127,8 +132,27 @@ export function measuredUsage(
|
|
|
127
132
|
? { fromDays: spanDays, factor: SCALE_TO_DAYS / spanDays }
|
|
128
133
|
: null;
|
|
129
134
|
|
|
130
|
-
|
|
131
|
-
|
|
135
|
+
/**
|
|
136
|
+
* Every input token in the denominator, writes included.
|
|
137
|
+
*
|
|
138
|
+
* This was `reads / (input + reads)`, which leaves cache *writes* out of the
|
|
139
|
+
* total it is a share of. A workload that rewrites its prefix on every call
|
|
140
|
+
* and reads it back rarely then reports a **high** hit rate: with 100 read
|
|
141
|
+
* tokens, no plain input and 10,000 written, the old expression answers 100%
|
|
142
|
+
* where the truth is 1%.
|
|
143
|
+
*
|
|
144
|
+
* That is the worst direction for this number to be wrong in. It is handed
|
|
145
|
+
* to `optimize` as `cacheHitRate`, which decides whether caching is paying
|
|
146
|
+
* off — so the shape that is burning money on writes was the shape most
|
|
147
|
+
* likely to be told its cache was working perfectly.
|
|
148
|
+
*
|
|
149
|
+
* Writes are input tokens: they are billed at the input rate times the write
|
|
150
|
+
* multiplier, they arrive in the same `usage` block, and any accounting of
|
|
151
|
+
* "what share of the input came from cache" that omits them is answering a
|
|
152
|
+
* narrower question than the one it is named for.
|
|
153
|
+
*/
|
|
154
|
+
const allInputTokens = inputTokens + cacheReadTokens + cacheWriteTokens;
|
|
155
|
+
const readShare = allInputTokens > 0 ? cacheReadTokens / allInputTokens : 0;
|
|
132
156
|
|
|
133
157
|
return {
|
|
134
158
|
profile: {
|
package/src/schemas.ts
CHANGED
|
@@ -151,6 +151,42 @@ export const CONTRACT_SCHEMAS: Record<ContractName, Record<string, unknown>> = {
|
|
|
151
151
|
cannotSay: array,
|
|
152
152
|
unpricedRecords: number,
|
|
153
153
|
}),
|
|
154
|
+
'routing-measurement': doc('routing-measurement', {
|
|
155
|
+
slice: object,
|
|
156
|
+
evaluation: object,
|
|
157
|
+
}),
|
|
158
|
+
'example-pruning': doc('example-pruning', {
|
|
159
|
+
provider: string,
|
|
160
|
+
model: string,
|
|
161
|
+
selfAgreement: number,
|
|
162
|
+
recoverableTokens: number,
|
|
163
|
+
callsMade: number,
|
|
164
|
+
contributions: array,
|
|
165
|
+
}),
|
|
166
|
+
'store-inventory': doc('store-inventory', {
|
|
167
|
+
providers: array,
|
|
168
|
+
totalRecords: number,
|
|
169
|
+
span: objectOrNull,
|
|
170
|
+
possiblyDouble: number,
|
|
171
|
+
unknownVersion: number,
|
|
172
|
+
totalUsd: number,
|
|
173
|
+
unreadable: array,
|
|
174
|
+
}),
|
|
175
|
+
conformance: doc('conformance', {
|
|
176
|
+
contract: stringOrNull,
|
|
177
|
+
because: stringOrNull,
|
|
178
|
+
problems: array,
|
|
179
|
+
unavailable: array,
|
|
180
|
+
records: { type: ['number', 'null'] },
|
|
181
|
+
conforms: boolean,
|
|
182
|
+
}),
|
|
183
|
+
'watch-cycle': doc('watch-cycle', {
|
|
184
|
+
firedAtMs: number,
|
|
185
|
+
crossings: array,
|
|
186
|
+
suppressed: array,
|
|
187
|
+
abstentions: array,
|
|
188
|
+
gap: objectOrNull,
|
|
189
|
+
}),
|
|
154
190
|
};
|
|
155
191
|
|
|
156
192
|
/** The schema for one named contract — the object `trazum schema` prints. */
|
package/src/tokenizer.ts
CHANGED
|
@@ -2,20 +2,22 @@
|
|
|
2
2
|
* Dependency-free token estimator.
|
|
3
3
|
*
|
|
4
4
|
* This is NOT a real tokenizer: it is a heuristic calibrated per character
|
|
5
|
-
* class
|
|
6
|
-
*
|
|
7
|
-
* two versions of the same prompt — but do NOT bill anyone from it.
|
|
5
|
+
* class, built for comparing two versions of the same prompt — but do NOT bill
|
|
6
|
+
* anyone from it.
|
|
8
7
|
*
|
|
9
|
-
* **
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
8
|
+
* **Its accuracy is not one number, and the years it was written as one were
|
|
9
|
+
* the mistake.** The branches below treat CJK, digits and punctuation quite
|
|
10
|
+
* differently from words, and there was never a reason those should land on the
|
|
11
|
+
* same accuracy. Measured over 47 samples against the official counting
|
|
12
|
+
* endpoint they do not: the worst error is 3.2% on CJK, 5.6% on Latin prose,
|
|
13
|
+
* 25.1% on code and quoting, and 32.5% on a digit-dominant table. `band.ts`
|
|
14
|
+
* holds the bands those measurements earn and `bandFor(text)` is what a caller
|
|
15
|
+
* should print; the constant below is only the widest of them, for callers that
|
|
16
|
+
* have no text in hand.
|
|
16
17
|
*
|
|
17
|
-
* `test/token-band.test.js`
|
|
18
|
-
*
|
|
18
|
+
* `test/token-band.test.js` holds every sample against the band `bandFor` gives
|
|
19
|
+
* it, and a sample edited since it was measured fails rather than passing
|
|
20
|
+
* quietly.
|
|
19
21
|
*
|
|
20
22
|
* For exact numbers use `countTokensAnthropic` (the official token-counting
|
|
21
23
|
* endpoint, which is free) or pass your own `TokenCounter`.
|
|
@@ -25,53 +27,47 @@ import { detectTextLanguage } from './language.js';
|
|
|
25
27
|
import { SAFE_FETCH_INIT, checkedEndpoint } from './net.js';
|
|
26
28
|
|
|
27
29
|
/**
|
|
28
|
-
* The
|
|
30
|
+
* The widest band this estimator is published under, as a percentage.
|
|
29
31
|
*
|
|
30
|
-
* **
|
|
31
|
-
* releases as a design target nobody had checked; the first
|
|
32
|
-
* of eight samples outside it and it went to `25`; fixing
|
|
33
|
-
* calibrating per language brought it back to `15
|
|
34
|
-
*
|
|
32
|
+
* **A history worth keeping, because each value was believed at the time.** It
|
|
33
|
+
* was `15` for eight releases as a design target nobody had checked; the first
|
|
34
|
+
* measurement found two of eight samples outside it and it went to `25`; fixing
|
|
35
|
+
* the digit divisor and calibrating per language brought it back to `15`, and
|
|
36
|
+
* splitting kana from han — Japanese had been +11.2% and Chinese −3.2% under one
|
|
37
|
+
* rule — brought it to `10`. Four values, each an improvement, and all four
|
|
38
|
+
* wrong in the same way.
|
|
35
39
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
+
* **The corpus they were measured on was the corpus they were fitted to.**
|
|
41
|
+
* Twenty-one samples held thirteen files of Latin prose and exactly one each of
|
|
42
|
+
* code, numeric and punctuation, and those single files were where the constants
|
|
43
|
+
* came from. The worst error it could show was 6.4%, and 10 looked like a
|
|
44
|
+
* comfortable margin over it. Neither figure was about the estimator.
|
|
40
45
|
*
|
|
41
|
-
* **
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* safe direction for a tool that reports money.
|
|
46
|
+
* **It is no longer one number, and this one is the widest.** A corpus of
|
|
47
|
+
* thirteen prose files and one each of code, numeric and punctuation put the
|
|
48
|
+
* band at 10, and those single files were the set the constants had been fitted
|
|
49
|
+
* to. Measured on forty-seven, the same estimator is 6% out on prose and 33%
|
|
50
|
+
* out on a CSV ledger. `bandFor` in `band.ts` is what a caller that has the
|
|
51
|
+
* text should use.
|
|
48
52
|
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* **Which samples the band rests on matters more than the number.** Eight of the
|
|
56
|
-
* twenty-one had a constant fitted to them — seven Latin divisors and the digit
|
|
57
|
-
* divisor — so their residuals are optimistic by construction. The two worst
|
|
58
|
-
* errors in the corpus, `code-heavy` at 6.4% and `punctuation-heavy` at 5.7%, are
|
|
59
|
-
* fitted to nothing at all, and they are what sets this figure.
|
|
60
|
-
*
|
|
61
|
-
* Exported so every report, README and tool description reads the same number. It
|
|
62
|
-
* was a literal in twenty-four files before this, with the only machine-readable
|
|
63
|
-
* copy in a test, and `token-band.test.js` now fails any file that states a
|
|
64
|
-
* different one.
|
|
53
|
+
* This stays for the callers that do not have it — a figure covering the whole
|
|
54
|
+
* catalogue of text, which can only be the worst of them. Widening it from 10
|
|
55
|
+
* to 33 makes every claim that reads it true where it had been false, at the
|
|
56
|
+
* cost of understating the estimator on prose. That trade is deliberate: a
|
|
57
|
+
* caller with no text cannot know which it is holding, and there is only one
|
|
58
|
+
* safe direction to guess in.
|
|
65
59
|
*/
|
|
66
|
-
export const ESTIMATE_ERROR_BAND_PCT =
|
|
60
|
+
export const ESTIMATE_ERROR_BAND_PCT = 33;
|
|
67
61
|
|
|
68
62
|
/**
|
|
69
63
|
* Whose tokenizer that band was measured against.
|
|
70
64
|
*
|
|
71
65
|
* The number above is not a property of the estimator; it is a property of the
|
|
72
|
-
* estimator **against Claude**.
|
|
73
|
-
* family. Trazum prices seven, and
|
|
74
|
-
*
|
|
66
|
+
* estimator **against Claude**. Forty-seven samples, one counting endpoint, one
|
|
67
|
+
* family. Trazum prices seven, and the two that have since been run are far
|
|
68
|
+
* outside anything published here: DeepSeek 94.5% at worst, Mistral 103.1%. On
|
|
69
|
+
* a GPT or a Gemini prompt nobody has measured it at all, and
|
|
70
|
+
* `measuredForeignError` answers null rather than the nearest number.
|
|
75
71
|
*
|
|
76
72
|
* Exported as a name rather than left implicit in a comment because the band
|
|
77
73
|
* had already leaked out of its domain in three places: an advisory that told a
|
|
@@ -173,6 +169,34 @@ const DIVISOR_BY_LANGUAGE: Readonly<Record<string, number>> = {
|
|
|
173
169
|
const KANA_TOKENS_PER_CHAR = 0.75;
|
|
174
170
|
const HAN_TOKENS_PER_CHAR = 1.05;
|
|
175
171
|
|
|
172
|
+
/**
|
|
173
|
+
* Hangul, measured at last.
|
|
174
|
+
*
|
|
175
|
+
* The comment above used to end *"Hangul keeps the old cost of 1, because
|
|
176
|
+
* nothing here measures Korean. Guessing it from Japanese would be inventing a
|
|
177
|
+
* figure."* That was right, and it stayed right for exactly as long as nobody
|
|
178
|
+
* measured Korean. `cjk-korean.txt` now does: at 1 token per character the
|
|
179
|
+
* estimate came out **20.0% under** — 232 against 290 — and 1.20 takes it to
|
|
180
|
+
* -10.0%.
|
|
181
|
+
*
|
|
182
|
+
* **1.20 was the one-sample answer and it was wrong by ten points.** A second
|
|
183
|
+
* Korean sample in a different register — a deployment review against support
|
|
184
|
+
* copy — put both at -10.6% and -10.0% under it, and solving the constant on
|
|
185
|
+
* the two together lands on 1.35, where both come out at 0.0%.
|
|
186
|
+
*
|
|
187
|
+
* Two independent texts agreeing to within four tenths of a point across the
|
|
188
|
+
* whole search is what makes this a property of Hangul rather than a fit to a
|
|
189
|
+
* file. Their residuals move in lockstep at every candidate value: -10.6/-10.0
|
|
190
|
+
* at 1.20, -3.5/-3.1 at 1.30, 3.2/3.4 at 1.40. One sample cannot show that and
|
|
191
|
+
* two can, which is the entire argument for measuring more of the thin classes
|
|
192
|
+
* rather than tuning harder on the thin ones.
|
|
193
|
+
*
|
|
194
|
+
* Still in-sample: the honest test is a third Korean prompt, and the corpus
|
|
195
|
+
* grows one at a time.
|
|
196
|
+
*/
|
|
197
|
+
const HANGUL_TOKENS_PER_CHAR = 1.35;
|
|
198
|
+
const HANGUL = /[가-]/;
|
|
199
|
+
|
|
176
200
|
/**
|
|
177
201
|
* What a prompt gets when the language could not be told.
|
|
178
202
|
*
|
|
@@ -248,7 +272,11 @@ export function estimateTokens(text: string): number {
|
|
|
248
272
|
* where the loop happens to break, not of what the text costs.
|
|
249
273
|
*/
|
|
250
274
|
while (i < chars.length && CJK.test(chars[i]!)) {
|
|
251
|
-
cjkTokens += KANA.test(chars[i]!)
|
|
275
|
+
cjkTokens += KANA.test(chars[i]!)
|
|
276
|
+
? KANA_TOKENS_PER_CHAR
|
|
277
|
+
: HANGUL.test(chars[i]!)
|
|
278
|
+
? HANGUL_TOKENS_PER_CHAR
|
|
279
|
+
: HAN_TOKENS_PER_CHAR;
|
|
252
280
|
i++;
|
|
253
281
|
}
|
|
254
282
|
continue;
|
|
@@ -315,7 +343,29 @@ export function estimateTokens(text: string): number {
|
|
|
315
343
|
total += 1;
|
|
316
344
|
i++;
|
|
317
345
|
} else {
|
|
318
|
-
|
|
346
|
+
/**
|
|
347
|
+
* One token per symbol, not one per two.
|
|
348
|
+
*
|
|
349
|
+
* Two was the shipped value and every symbol-dense sample in the corpus
|
|
350
|
+
* undercounted under it, in the same direction, by amounts that grow with
|
|
351
|
+
* density: punctuation-heavy -5.7%, code-heavy -6.4%,
|
|
352
|
+
* punctuation-markup -11.4%, code-sql -25.6%, code-shell -29.8%. Five
|
|
353
|
+
* samples agreeing on a direction is a shape, not a fit — a merge table
|
|
354
|
+
* covers `, ` and `. ` and covers `${`, `"$`, `--` and `‑` far less
|
|
355
|
+
* well, and shell and SQL are made of the second kind.
|
|
356
|
+
*
|
|
357
|
+
* Searched over the whole corpus rather than the class it helps, so a
|
|
358
|
+
* value that fixed code by wrecking prose could not look like a win: at
|
|
359
|
+
* 1.0 the samples outside the published band drop from five to three and
|
|
360
|
+
* the mean error from 6.6% to 5.1%, with no sample moved more than a
|
|
361
|
+
* point in the wrong direction.
|
|
362
|
+
*
|
|
363
|
+
* The search wanted to go below 1.0 and was not allowed to. Below one is
|
|
364
|
+
* more than a token per symbol, which may well be true of SQL — code-sql
|
|
365
|
+
* is still 24.9% under — but it is a claim about a class this corpus has
|
|
366
|
+
* two samples of, and two samples do not earn a constant.
|
|
367
|
+
*/
|
|
368
|
+
total += Math.ceil(n / 1);
|
|
319
369
|
}
|
|
320
370
|
}
|
|
321
371
|
|
package/src/types.ts
CHANGED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading a dropped `route --json` verdict, so quality can stand beside cost.
|
|
3
|
+
*
|
|
4
|
+
* The bill is arithmetic: every figure in it comes from records already on
|
|
5
|
+
* disk, and it can say exactly what a cheaper model would have saved. It can
|
|
6
|
+
* say nothing at all about whether that model still does the job. That answer
|
|
7
|
+
* costs provider calls and a credential, which is why `trazum route` exists
|
|
8
|
+
* and why no surface that only reads a log will ever produce it.
|
|
9
|
+
*
|
|
10
|
+
* This is the bridge between the two, and it is deliberately one-directional:
|
|
11
|
+
* the measurement is made where the credential is, written out as the
|
|
12
|
+
* `routing-measurement` contract, and read back here by a pure function that
|
|
13
|
+
* touches no network and holds no key. A reader who has run `route` once can
|
|
14
|
+
* carry the verdict to any surface that prices the same workload.
|
|
15
|
+
*
|
|
16
|
+
* It was not possible to write until the document was worth reading. Before
|
|
17
|
+
* the JSON-text sweep, `route --json` printed the whole `EvalReport` — every
|
|
18
|
+
* case input and three model answers per case — so a bridge would have been a
|
|
19
|
+
* feature for carrying prompt text into a browser. The document carries the
|
|
20
|
+
* measurement and nothing else now, and `conform` is what says so rather than
|
|
21
|
+
* this file's own reading: the contract validates, the bridge only maps.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { conform } from './conform.js';
|
|
25
|
+
import type { EvalVerdict } from './evaluate.js';
|
|
26
|
+
import { UNLABELLED } from './usage.js';
|
|
27
|
+
|
|
28
|
+
/** What a dropped routing measurement says, mapped onto what a bill can match. */
|
|
29
|
+
export interface DroppedVerdict {
|
|
30
|
+
/** The workload the measurement was made on, or null when it was unlabelled. */
|
|
31
|
+
label: string | null;
|
|
32
|
+
/**
|
|
33
|
+
* The model those calls go to today, from the bill's own slice.
|
|
34
|
+
*
|
|
35
|
+
* Not the model that answered. `route` builds its baseline provider from
|
|
36
|
+
* the environment, so the model in the measurement is whatever
|
|
37
|
+
* `TRAZUM_LLM_MODEL` names, and it is only the log's model when the reader
|
|
38
|
+
* configured it that way. Pairing on it matched nothing on a real run
|
|
39
|
+
* against an OpenAI-compatible endpoint — `evaluation.model` said
|
|
40
|
+
* `stub-strong` while the log said `claude-opus-5` — and the bill would
|
|
41
|
+
* have shown no verdict at all with no explanation.
|
|
42
|
+
*/
|
|
43
|
+
model: string;
|
|
44
|
+
/**
|
|
45
|
+
* The model that actually answered, when it is not the one the log records.
|
|
46
|
+
*
|
|
47
|
+
* Null when they agree, which is the ordinary case. Not null is worth
|
|
48
|
+
* saying out loud rather than smoothing over: a verdict measured on a
|
|
49
|
+
* stand-in is a weaker claim about this workload than one measured on the
|
|
50
|
+
* model the workload actually uses, and only this field can tell the
|
|
51
|
+
* difference.
|
|
52
|
+
*/
|
|
53
|
+
measuredOn: string | null;
|
|
54
|
+
/** The model measured against it. */
|
|
55
|
+
candidateModel: string;
|
|
56
|
+
verdict: EvalVerdict;
|
|
57
|
+
/** The model's agreement with itself. The yardstick the other rate is read against. */
|
|
58
|
+
selfAgreement: number;
|
|
59
|
+
/** Agreement between the two models, same prompt on both sides. */
|
|
60
|
+
crossAgreement: number;
|
|
61
|
+
/** Cases the measurement covered, so a two-case verdict cannot pose as a hundred. */
|
|
62
|
+
cases: number;
|
|
63
|
+
/** Calls it cost to reach. */
|
|
64
|
+
callsMade: number;
|
|
65
|
+
/**
|
|
66
|
+
* What the measurement's own bill priced the route at, when the document
|
|
67
|
+
* carried it. Null rather than zero when it did not: a saving nobody
|
|
68
|
+
* measured is not a saving of nothing.
|
|
69
|
+
*/
|
|
70
|
+
savingUsd: number | null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* What the bridge did with a file.
|
|
75
|
+
*
|
|
76
|
+
* `null` is not a failure: it means the file is not a routing measurement at
|
|
77
|
+
* all, and the caller should go on treating it as whatever else it was. A
|
|
78
|
+
* refusal is the other case — it looked like one and could not be read — and
|
|
79
|
+
* it names what is missing, because a refusal with nothing after it is
|
|
80
|
+
* indistinguishable from a bug.
|
|
81
|
+
*/
|
|
82
|
+
export type BridgeReading =
|
|
83
|
+
| { kind: 'verdict'; verdict: DroppedVerdict }
|
|
84
|
+
| { kind: 'refusal'; because: string };
|
|
85
|
+
|
|
86
|
+
const isObject = (value: unknown): value is Record<string, unknown> =>
|
|
87
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Whether this file is even claiming to be a routing measurement.
|
|
91
|
+
*
|
|
92
|
+
* Shape only, and deliberately narrow: an `evaluation` object carrying a
|
|
93
|
+
* `verdict`. Anything looser would swallow a usage log or a profile and
|
|
94
|
+
* refuse it in this file's words instead of letting the caller price it.
|
|
95
|
+
*/
|
|
96
|
+
function claimsToBeOne(parsed: unknown): boolean {
|
|
97
|
+
if (!isObject(parsed)) return false;
|
|
98
|
+
const evaluation = parsed.evaluation;
|
|
99
|
+
return isObject(evaluation) && typeof evaluation.verdict === 'string';
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Reads a dropped `trazum route --json` document.
|
|
104
|
+
*
|
|
105
|
+
* Returns null when the text is something else entirely. Validation is
|
|
106
|
+
* `conform`'s, against the published `routing-measurement` contract, so this
|
|
107
|
+
* function cannot drift from the schema a connector author builds against —
|
|
108
|
+
* and a document that fails carries the contract's own sentences back to the
|
|
109
|
+
* reader rather than a second opinion written here.
|
|
110
|
+
*/
|
|
111
|
+
export function readDroppedVerdict(text: string): BridgeReading | null {
|
|
112
|
+
let parsed: unknown;
|
|
113
|
+
try {
|
|
114
|
+
parsed = JSON.parse(text);
|
|
115
|
+
} catch {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
if (!claimsToBeOne(parsed)) return null;
|
|
119
|
+
|
|
120
|
+
const report = conform(text, { contract: 'routing-measurement' });
|
|
121
|
+
if (!report.conforms) {
|
|
122
|
+
const detail =
|
|
123
|
+
report.problems.length > 0
|
|
124
|
+
? report.problems.map((problem) => `${problem.at}: ${problem.detail}`).join('; ')
|
|
125
|
+
: (report.because ?? 'it does not match the routing-measurement contract');
|
|
126
|
+
return { kind: 'refusal', because: detail };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const document = parsed as { slice?: unknown; evaluation?: unknown };
|
|
130
|
+
const slice = isObject(document.slice) ? document.slice : {};
|
|
131
|
+
const evaluation = document.evaluation as Record<string, unknown>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Every field below is read defensively even though `conform` has passed,
|
|
135
|
+
* because the contract requires `slice` and `evaluation` to be objects and
|
|
136
|
+
* says nothing about what is inside them. A document from a future version
|
|
137
|
+
* with a thinner slice is a document this reads what it can from, not one
|
|
138
|
+
* it crashes on.
|
|
139
|
+
*/
|
|
140
|
+
const answered = typeof evaluation.model === 'string' ? evaluation.model : null;
|
|
141
|
+
// The slice's model first: that is the fact the bill can be matched against.
|
|
142
|
+
const model = typeof slice.model === 'string' ? slice.model : answered;
|
|
143
|
+
const candidateModel =
|
|
144
|
+
typeof evaluation.candidateModel === 'string' ? evaluation.candidateModel : null;
|
|
145
|
+
const verdict = typeof evaluation.verdict === 'string' ? (evaluation.verdict as EvalVerdict) : null;
|
|
146
|
+
if (model === null || candidateModel === null || verdict === null) {
|
|
147
|
+
return {
|
|
148
|
+
kind: 'refusal',
|
|
149
|
+
because:
|
|
150
|
+
'the evaluation names no model, candidate model or verdict, so there is nothing to set beside a bill',
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const number = (value: unknown): number => (typeof value === 'number' ? value : 0);
|
|
155
|
+
const route = isObject(slice.route) ? slice.route : null;
|
|
156
|
+
const rawLabel = typeof slice.label === 'string' ? slice.label : null;
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
kind: 'verdict',
|
|
160
|
+
verdict: {
|
|
161
|
+
// `UNLABELLED` is the profile's own sentinel for calls that carry no
|
|
162
|
+
// label. It travels through the document as that sentinel and comes
|
|
163
|
+
// back as null here, so the bridge speaks the bill's language rather
|
|
164
|
+
// than leaking an internal marker into a caption.
|
|
165
|
+
label: rawLabel === null || rawLabel === UNLABELLED ? null : rawLabel,
|
|
166
|
+
model,
|
|
167
|
+
measuredOn: answered !== null && answered !== model ? answered : null,
|
|
168
|
+
candidateModel,
|
|
169
|
+
verdict,
|
|
170
|
+
selfAgreement: number(evaluation.selfAgreement),
|
|
171
|
+
crossAgreement: number(evaluation.crossAgreement),
|
|
172
|
+
cases: Array.isArray(evaluation.cases) ? evaluation.cases.length : 0,
|
|
173
|
+
callsMade: number(evaluation.callsMade),
|
|
174
|
+
savingUsd: route !== null && typeof route.savingUsd === 'number' ? route.savingUsd : null,
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Whether a verdict describes the route a given slice of the bill is offering.
|
|
181
|
+
*
|
|
182
|
+
* Both halves must agree or the pairing is a lie: the same workload, the same
|
|
183
|
+
* model it goes to now, and the same candidate. A verdict measured on `chat`
|
|
184
|
+
* shown against `summarise`'s saving would be the exact fault this repository
|
|
185
|
+
* keeps finding in itself — a number describing something other than what was
|
|
186
|
+
* measured — and matching on the model alone would produce it on any log with
|
|
187
|
+
* two workloads on one model.
|
|
188
|
+
*/
|
|
189
|
+
export function verdictMatchesSlice(
|
|
190
|
+
verdict: DroppedVerdict,
|
|
191
|
+
slice: { label: string; model: string; route: { candidate: { id: string } } | null },
|
|
192
|
+
): boolean {
|
|
193
|
+
if (slice.route === null) return false;
|
|
194
|
+
const sliceLabel = slice.label === UNLABELLED ? null : slice.label;
|
|
195
|
+
return (
|
|
196
|
+
sliceLabel === verdict.label &&
|
|
197
|
+
slice.model === verdict.model &&
|
|
198
|
+
slice.route.candidate.id === verdict.candidateModel
|
|
199
|
+
);
|
|
200
|
+
}
|