@trazum/core 1.44.0 → 1.46.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 +10 -0
- package/dist/guard.d.ts +79 -0
- package/dist/guard.d.ts.map +1 -0
- package/dist/guard.js +136 -0
- package/dist/guard.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +231 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +325 -0
- package/dist/init.js.map +1 -0
- package/package.json +1 -1
- package/src/guard.ts +204 -0
- package/src/index.ts +15 -0
- package/src/init.ts +500 -0
package/dist/init.js
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The first five minutes, decided without touching a disk.
|
|
3
|
+
*
|
|
4
|
+
* Everything since 1.41 raised the ceiling. This lowers the floor: from
|
|
5
|
+
* `npx @trazum/cli` to a finding worth money, without reading a page of
|
|
6
|
+
* documentation. Twenty-one commands is a wall to somebody who has none of
|
|
7
|
+
* them yet, and that wall is why a good tool gets closed inside a minute.
|
|
8
|
+
*
|
|
9
|
+
* **The proposal is a document, not a side effect.** What `init` would write
|
|
10
|
+
* is decided here, from observations the CLI collected, and returned as a
|
|
11
|
+
* value. So every rule below is testable without a filesystem, and `--dry-run`
|
|
12
|
+
* is the same code path minus the write rather than a second implementation
|
|
13
|
+
* that drifts from the first.
|
|
14
|
+
*
|
|
15
|
+
* **A key with no evidence is not written.** A generated config full of
|
|
16
|
+
* guessed thresholds is a config nobody trusts and everybody deletes — and it
|
|
17
|
+
* is worse than an empty one, because it looks like a decision somebody made.
|
|
18
|
+
* Every key that lands carries the observation that justified it; every key
|
|
19
|
+
* that does not carries what would settle it. Both are typed values, because a
|
|
20
|
+
* first run that explains itself only in prose cannot be checked by a test.
|
|
21
|
+
*
|
|
22
|
+
* **Measurement is not policy.** A log says what your traffic *was* — how many
|
|
23
|
+
* calls, which model, how long the outputs ran. Those are measurements and
|
|
24
|
+
* this file writes them. A budget says what your traffic *may cost*, which no
|
|
25
|
+
* log can answer: picking "the measured month plus twenty per cent" would be
|
|
26
|
+
* this tool inventing a threshold and then grading somebody against it. So
|
|
27
|
+
* `spend` is always declined, and the measured figure is handed over so the
|
|
28
|
+
* person who *can* set a budget has the number in front of them.
|
|
29
|
+
*/
|
|
30
|
+
import { DEFAULT_EXTENSIONS } from './config-schema.js';
|
|
31
|
+
import { billLevers } from './levers.js';
|
|
32
|
+
/** A month, for turning a measured span into a rate. */
|
|
33
|
+
const DAYS_PER_MONTH = 30;
|
|
34
|
+
/**
|
|
35
|
+
* The shortest measured span that may be stated as a monthly rate.
|
|
36
|
+
*
|
|
37
|
+
* Four weeks, so every weekday appears the same number of times. Three days of
|
|
38
|
+
* traffic multiplied by ten is a forecast wearing a measurement's clothes, and
|
|
39
|
+
* this repository has refused that since the series shipped in 1.40.
|
|
40
|
+
*/
|
|
41
|
+
export const MIN_RATE_DAYS = 28;
|
|
42
|
+
/** The share of the bill a headline must clear to be worth being the headline. */
|
|
43
|
+
export const HEADLINE_FLOOR_SHARE = 0.01;
|
|
44
|
+
/** Days between two `YYYY-MM-DD` days, inclusive of both. */
|
|
45
|
+
function spanDays(first, last) {
|
|
46
|
+
const a = Date.parse(`${first}T00:00:00Z`);
|
|
47
|
+
const b = Date.parse(`${last}T00:00:00Z`);
|
|
48
|
+
if (Number.isNaN(a) || Number.isNaN(b))
|
|
49
|
+
return 0;
|
|
50
|
+
return Math.round((b - a) / 86_400_000) + 1;
|
|
51
|
+
}
|
|
52
|
+
/** The extensions actually present among the prompt files found. */
|
|
53
|
+
function extensionsOf(files) {
|
|
54
|
+
const seen = new Set();
|
|
55
|
+
for (const file of files) {
|
|
56
|
+
const dot = file.lastIndexOf('.');
|
|
57
|
+
const slash = Math.max(file.lastIndexOf('/'), file.lastIndexOf('\\'));
|
|
58
|
+
if (dot > slash + 1)
|
|
59
|
+
seen.add(file.slice(dot).toLowerCase());
|
|
60
|
+
}
|
|
61
|
+
return [...seen].sort();
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The one model a config may name, from what was measured.
|
|
65
|
+
*
|
|
66
|
+
* A log spread across four models has no single answer, and writing the
|
|
67
|
+
* largest of the four would silently price every prompt in the repository
|
|
68
|
+
* against a model most of them never touch. The threshold is a majority: past
|
|
69
|
+
* half the bill, "this is what you use" is a fair sentence.
|
|
70
|
+
*/
|
|
71
|
+
function dominantModel(report) {
|
|
72
|
+
if (report.total.totalUsd <= 0 || report.byModel.length === 0)
|
|
73
|
+
return null;
|
|
74
|
+
const top = report.byModel[0];
|
|
75
|
+
if (top === undefined)
|
|
76
|
+
return null;
|
|
77
|
+
const share = top.breakdown.totalUsd / report.total.totalUsd;
|
|
78
|
+
return share > 0.5 ? { model: top.model, share } : null;
|
|
79
|
+
}
|
|
80
|
+
export function proposeInit(observations, options) {
|
|
81
|
+
const { catalogue, on = new Date() } = options;
|
|
82
|
+
const { measured } = observations;
|
|
83
|
+
const justified = [];
|
|
84
|
+
const declined = [];
|
|
85
|
+
const config = {};
|
|
86
|
+
// --- locale -------------------------------------------------------------
|
|
87
|
+
if (observations.locale !== null) {
|
|
88
|
+
config.locale = observations.locale;
|
|
89
|
+
justified.push({ key: 'locale', value: observations.locale, from: 'environment' });
|
|
90
|
+
}
|
|
91
|
+
// --- extensions ---------------------------------------------------------
|
|
92
|
+
//
|
|
93
|
+
// Only when the walk found something the defaults do not already cover.
|
|
94
|
+
// Writing `[".txt", ".md", ".prompt", ".tmpl"]` back into a config is a key
|
|
95
|
+
// that changes nothing and has to be maintained forever.
|
|
96
|
+
const extensions = extensionsOf(observations.promptFiles);
|
|
97
|
+
if (extensions.length > 0 && extensions.some((e) => !DEFAULT_EXTENSIONS.includes(e))) {
|
|
98
|
+
config.extensions = extensions;
|
|
99
|
+
justified.push({
|
|
100
|
+
key: 'extensions',
|
|
101
|
+
value: extensions,
|
|
102
|
+
from: 'walk',
|
|
103
|
+
files: observations.promptFiles.length,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
// --- usage.model --------------------------------------------------------
|
|
107
|
+
//
|
|
108
|
+
// Measured beats detected, and it is not close. An import says which SDK
|
|
109
|
+
// somebody installed; a log says which model was billed. When there is no
|
|
110
|
+
// log, a literal in the source will do — but a *provider* with no model is
|
|
111
|
+
// declined rather than filled in from that provider's default. `where` may
|
|
112
|
+
// print a provider default because a reader can see it is a guess; a config
|
|
113
|
+
// file cannot, because six weeks later it reads as a decision.
|
|
114
|
+
const dominant = measured === null ? null : dominantModel(measured);
|
|
115
|
+
const modelSighting = observations.sightings.find((s) => s.detection.model !== null && s.detection.conflicts.length === 0);
|
|
116
|
+
const conflicting = observations.sightings.filter((s) => s.detection.conflicts.length > 0);
|
|
117
|
+
const providerOnly = observations.sightings.find((s) => s.detection.provider !== null && s.detection.model === null && s.detection.conflicts.length === 0);
|
|
118
|
+
if (dominant !== null) {
|
|
119
|
+
config.usage = { ...config.usage, model: dominant.model };
|
|
120
|
+
justified.push({
|
|
121
|
+
key: 'usage.model',
|
|
122
|
+
value: dominant.model,
|
|
123
|
+
from: 'measured',
|
|
124
|
+
share: dominant.share,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
else if (modelSighting !== undefined) {
|
|
128
|
+
const evidence = modelSighting.detection.evidence.find((e) => e.model !== undefined) ??
|
|
129
|
+
modelSighting.detection.evidence[0];
|
|
130
|
+
config.usage = { ...config.usage, model: modelSighting.detection.model };
|
|
131
|
+
justified.push({
|
|
132
|
+
key: 'usage.model',
|
|
133
|
+
value: modelSighting.detection.model,
|
|
134
|
+
from: 'source',
|
|
135
|
+
file: modelSighting.file,
|
|
136
|
+
line: evidence?.line ?? 0,
|
|
137
|
+
evidence: evidence?.kind ?? 'model-literal',
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
else if (conflicting.length > 0) {
|
|
141
|
+
declined.push({
|
|
142
|
+
key: 'usage.model',
|
|
143
|
+
why: 'conflicting-evidence',
|
|
144
|
+
files: conflicting.map((s) => s.file),
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
else if (providerOnly !== undefined) {
|
|
148
|
+
declined.push({
|
|
149
|
+
key: 'usage.model',
|
|
150
|
+
why: 'provider-only',
|
|
151
|
+
provider: providerOnly.detection.provider,
|
|
152
|
+
file: providerOnly.file,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
declined.push({ key: 'usage.model', why: 'no-evidence' });
|
|
157
|
+
}
|
|
158
|
+
// --- the measured span, which three keys below depend on ----------------
|
|
159
|
+
const dated = measured?.spendByDay ?? [];
|
|
160
|
+
const first = dated[0];
|
|
161
|
+
const last = dated[dated.length - 1];
|
|
162
|
+
const days = first === undefined || last === undefined ? 0 : spanDays(first.day, last.day);
|
|
163
|
+
const datedCalls = dated.reduce((sum, d) => sum + d.calls, 0);
|
|
164
|
+
const undated = measured === null ? 0 : Math.max(0, measured.total.calls - datedCalls);
|
|
165
|
+
// --- usage.callsPerMonth ------------------------------------------------
|
|
166
|
+
//
|
|
167
|
+
// Three separate refusals, and they are not the same refusal. No log is one
|
|
168
|
+
// situation; a log covering four days is another; a log whose records carry
|
|
169
|
+
// no clock is a third — and that last one is the one that would go wrong
|
|
170
|
+
// quietly, because the calls are all *there*, they simply cannot be placed
|
|
171
|
+
// in time. Dividing them by a span they were never proven to fall inside is
|
|
172
|
+
// how a rate comes out too high and nobody can see why.
|
|
173
|
+
if (measured === null) {
|
|
174
|
+
declined.push({ key: 'usage.callsPerMonth', why: 'nothing-measured' });
|
|
175
|
+
}
|
|
176
|
+
else if (undated > 0) {
|
|
177
|
+
declined.push({
|
|
178
|
+
key: 'usage.callsPerMonth',
|
|
179
|
+
why: 'undated-calls',
|
|
180
|
+
undated,
|
|
181
|
+
calls: measured.total.calls,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
else if (days < MIN_RATE_DAYS) {
|
|
185
|
+
declined.push({
|
|
186
|
+
key: 'usage.callsPerMonth',
|
|
187
|
+
why: 'window-too-short',
|
|
188
|
+
days,
|
|
189
|
+
calls: measured.total.calls,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
const perMonth = Math.round((measured.total.calls / days) * DAYS_PER_MONTH);
|
|
194
|
+
config.usage = { ...config.usage, callsPerMonth: perMonth };
|
|
195
|
+
justified.push({
|
|
196
|
+
key: 'usage.callsPerMonth',
|
|
197
|
+
value: perMonth,
|
|
198
|
+
from: 'measured',
|
|
199
|
+
calls: measured.total.calls,
|
|
200
|
+
days,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
// --- usage.avgOutputTokens ----------------------------------------------
|
|
204
|
+
//
|
|
205
|
+
// An average of what happened, not a forecast of what will — so unlike the
|
|
206
|
+
// rate above it needs no minimum span. One day of real calls gives a real
|
|
207
|
+
// average of those calls.
|
|
208
|
+
if (measured === null || measured.total.calls === 0) {
|
|
209
|
+
declined.push({ key: 'usage.avgOutputTokens', why: 'nothing-measured' });
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
const avg = Math.round(measured.total.outputTokens / measured.total.calls);
|
|
213
|
+
config.usage = { ...config.usage, avgOutputTokens: avg };
|
|
214
|
+
justified.push({
|
|
215
|
+
key: 'usage.avgOutputTokens',
|
|
216
|
+
value: avg,
|
|
217
|
+
from: 'measured',
|
|
218
|
+
outputTokens: measured.total.outputTokens,
|
|
219
|
+
calls: measured.total.calls,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
// --- usage.cacheHitRate -------------------------------------------------
|
|
223
|
+
//
|
|
224
|
+
// **Not recorded is not not-happened.** A log with no cache fields at all
|
|
225
|
+
// does not prove a hit rate of zero — it proves the exporter did not write
|
|
226
|
+
// the column. Writing 0 there would tell every later caching advisory that
|
|
227
|
+
// caching is doing nothing, which is a finding invented out of a missing
|
|
228
|
+
// field.
|
|
229
|
+
if (measured === null) {
|
|
230
|
+
declined.push({ key: 'usage.cacheHitRate', why: 'nothing-measured' });
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
const read = measured.total.cacheReadTokens;
|
|
234
|
+
const written = measured.total.cacheWriteTokens;
|
|
235
|
+
const input = measured.total.inputTokens;
|
|
236
|
+
if (read === 0 && written === 0) {
|
|
237
|
+
declined.push({ key: 'usage.cacheHitRate', why: 'not-recorded' });
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
const denominator = read + input;
|
|
241
|
+
const rate = denominator === 0 ? 0 : Math.round((read / denominator) * 100) / 100;
|
|
242
|
+
config.usage = { ...config.usage, cacheHitRate: rate };
|
|
243
|
+
justified.push({
|
|
244
|
+
key: 'usage.cacheHitRate',
|
|
245
|
+
value: rate,
|
|
246
|
+
from: 'measured',
|
|
247
|
+
cacheReadTokens: read,
|
|
248
|
+
inputTokens: input,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
// --- usage.batchEligible ------------------------------------------------
|
|
253
|
+
//
|
|
254
|
+
// Never written, in either direction. Whether the work tolerates a batch
|
|
255
|
+
// window is a fact about a product decision, and no log records it. A
|
|
256
|
+
// default of `false` would quietly delete the batch lever from every report
|
|
257
|
+
// this config touches; `true` would offer a saving on latency somebody never
|
|
258
|
+
// agreed to give up.
|
|
259
|
+
declined.push({ key: 'usage.batchEligible', why: 'only-you-know' });
|
|
260
|
+
// --- labels -------------------------------------------------------------
|
|
261
|
+
//
|
|
262
|
+
// Mapping a log label to a prompt file is a claim about which file produced
|
|
263
|
+
// which calls, and a directory walk cannot prove one. A wrong entry here is
|
|
264
|
+
// worse than a missing one: `profile` would go and read the wrong file and
|
|
265
|
+
// explain a cache verdict with the wrong prompt's structure, confidently.
|
|
266
|
+
const labels = measured?.byLabel.length ?? 0;
|
|
267
|
+
if (labels > 0)
|
|
268
|
+
declined.push({ key: 'labels', why: 'unprovable', labels });
|
|
269
|
+
// --- spend --------------------------------------------------------------
|
|
270
|
+
//
|
|
271
|
+
// The measured figure is handed over, and the threshold is not invented.
|
|
272
|
+
declined.push({
|
|
273
|
+
key: 'spend.maxUsd',
|
|
274
|
+
why: 'a-budget-is-a-policy',
|
|
275
|
+
measuredUsd: measured === null ? null : measured.total.totalUsd,
|
|
276
|
+
days: measured === null ? null : days,
|
|
277
|
+
});
|
|
278
|
+
// --- the headline -------------------------------------------------------
|
|
279
|
+
let headline = null;
|
|
280
|
+
let noHeadline = null;
|
|
281
|
+
if (measured === null) {
|
|
282
|
+
noHeadline = 'nothing-measured';
|
|
283
|
+
}
|
|
284
|
+
else if (measured.total.totalUsd <= 0) {
|
|
285
|
+
noHeadline = 'nothing-could-be-priced';
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
const levers = billLevers(measured, { catalogue, on, minShare: HEADLINE_FLOOR_SHARE });
|
|
289
|
+
const top = levers.slices[0];
|
|
290
|
+
if (top === undefined || top.combinedUsd <= 0) {
|
|
291
|
+
noHeadline = 'no-lever-clears-the-floor';
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
headline = {
|
|
295
|
+
slice: top,
|
|
296
|
+
lever: top.route !== null && top.batch !== null
|
|
297
|
+
? 'route+batch'
|
|
298
|
+
: top.route !== null
|
|
299
|
+
? 'route'
|
|
300
|
+
: 'batch',
|
|
301
|
+
savingUsd: top.combinedUsd,
|
|
302
|
+
provenance: 'measured',
|
|
303
|
+
days,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
// --- what this would overwrite ------------------------------------------
|
|
308
|
+
let overwrites = null;
|
|
309
|
+
if (observations.existing !== null) {
|
|
310
|
+
const already = observations.existing.config;
|
|
311
|
+
const keys = [];
|
|
312
|
+
if (config.locale !== undefined && already.locale !== undefined)
|
|
313
|
+
keys.push('locale');
|
|
314
|
+
if (config.extensions !== undefined && already.extensions !== undefined)
|
|
315
|
+
keys.push('extensions');
|
|
316
|
+
for (const k of ['model', 'callsPerMonth', 'avgOutputTokens', 'cacheHitRate']) {
|
|
317
|
+
if (config.usage?.[k] !== undefined && already.usage?.[k] !== undefined) {
|
|
318
|
+
keys.push(`usage.${k}`);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
overwrites = { path: observations.existing.path, keys };
|
|
322
|
+
}
|
|
323
|
+
return { schemaVersion: 1, config, justified, declined, headline, noHeadline, overwrites };
|
|
324
|
+
}
|
|
325
|
+
//# sourceMappingURL=init.js.map
|
package/dist/init.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.js","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAIxD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AA8JzC,wDAAwD;AACxD,MAAM,cAAc,GAAG,EAAE,CAAC;AAE1B;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,CAAC;AAEhC,kFAAkF;AAClF,MAAM,CAAC,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAEzC,6DAA6D;AAC7D,SAAS,QAAQ,CAAC,KAAa,EAAE,IAAY;IAC3C,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,KAAK,YAAY,CAAC,CAAC;IAC3C,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,YAAY,CAAC,CAAC;IAC1C,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QAAE,OAAO,CAAC,CAAC;IACjD,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;AAC9C,CAAC;AAED,oEAAoE;AACpE,SAAS,YAAY,CAAC,K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I,EAAE,CAAC;IAC1D,CAAC;IAED,OAAO,EAAE,aAAa,EAAE,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC;AAC7F,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trazum/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.46.0",
|
|
4
4
|
"description": "Trazum core: priced advisories for LLM prompts (caching, model tier, batching, schemas), plus deterministic trimming, token counting and pricing.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "David Mu\u00f1oz Rey",
|
package/src/guard.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The thing spending the money can finally ask, and be told no.
|
|
3
|
+
*
|
|
4
|
+
* 1.44 gave an endpoint that *answers*. An agent may consult it and ignore it,
|
|
5
|
+
* which is fine — advice an implementation can skip is still advice worth
|
|
6
|
+
* having. What was missing is the shape of a refusal an agent can act on.
|
|
7
|
+
*
|
|
8
|
+
* **A guard that only says no teaches a caller to stop asking.** A model told
|
|
9
|
+
* "denied" with no alternative has exactly two moves: send it anyway, or fail
|
|
10
|
+
* the user's request. Both are worse than the call it wanted to make. So every
|
|
11
|
+
* refusal here arrives with the levers that exist — this work routes to a
|
|
12
|
+
* cheaper model that still fits, a batch window would halve it — each with
|
|
13
|
+
* what it is worth *for this call*, and the assumption it rests on.
|
|
14
|
+
*
|
|
15
|
+
* **The guard never spends to answer.** No provider call, no LLM pass, no
|
|
16
|
+
* pull. The answer comes from the store and the catalogue, or it says it
|
|
17
|
+
* cannot tell. A cost guard that costs money to consult is a joke with a bill
|
|
18
|
+
* attached.
|
|
19
|
+
*
|
|
20
|
+
* **An alternative the prompt does not fit in is not an alternative.** A
|
|
21
|
+
* cheaper model with a smaller context window does not make this call cheaper;
|
|
22
|
+
* it makes it impossible. Those are filtered out here rather than offered and
|
|
23
|
+
* blamed later.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { answerCost } from './answer.js';
|
|
27
|
+
import type { AnswerRequest, CostAnswer } from './answer.js';
|
|
28
|
+
import { effectivePricing, multipliersFor } from './pricing.js';
|
|
29
|
+
import type { PricingCatalogue } from './pricing.js';
|
|
30
|
+
import type { ModelPricing } from './types.js';
|
|
31
|
+
import type { PlanAssumption } from './plan.js';
|
|
32
|
+
|
|
33
|
+
export type GuardVerdict = 'yes' | 'no' | 'cannot-tell';
|
|
34
|
+
|
|
35
|
+
export interface GuardAlternative {
|
|
36
|
+
kind: 'route' | 'batch' | 'route+batch';
|
|
37
|
+
/** The model this moves to, when it moves. */
|
|
38
|
+
model: { id: string; displayName: string } | null;
|
|
39
|
+
/**
|
|
40
|
+
* What this alternative saves **on this call** — not per month.
|
|
41
|
+
*
|
|
42
|
+
* The caller is deciding one call, right now. A monthly figure would be the
|
|
43
|
+
* right number at the wrong moment, and an agent has no way to act on it.
|
|
44
|
+
*/
|
|
45
|
+
savingUsd: number;
|
|
46
|
+
/** What the log cannot confirm, typed as everywhere since 1.38. */
|
|
47
|
+
assumes: PlanAssumption[];
|
|
48
|
+
/**
|
|
49
|
+
* Whether the described call fits this alternative's context window.
|
|
50
|
+
*
|
|
51
|
+
* Only `true` ever reaches a caller — the false ones are dropped before
|
|
52
|
+
* they are offered. The field exists so the rule is visible in the type
|
|
53
|
+
* rather than buried in a filter nobody reads.
|
|
54
|
+
*/
|
|
55
|
+
fits: true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface GuardAnswer {
|
|
59
|
+
schemaVersion: 1;
|
|
60
|
+
verdict: GuardVerdict;
|
|
61
|
+
/** The full cost answer, halves and provenance intact. */
|
|
62
|
+
cost: CostAnswer;
|
|
63
|
+
/**
|
|
64
|
+
* What to do instead, dearest saving first. Present on a refusal, and on a
|
|
65
|
+
* yes as well: an agent that can spend less while still being allowed to
|
|
66
|
+
* spend should be told so.
|
|
67
|
+
*/
|
|
68
|
+
alternatives: GuardAlternative[];
|
|
69
|
+
/**
|
|
70
|
+
* A one-line reason a human will read in a log. The fields above are what a
|
|
71
|
+
* machine acts on; this is never the only place a fact appears.
|
|
72
|
+
*/
|
|
73
|
+
because: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Models cheaper than this one, in the same family, that the prompt fits in. */
|
|
77
|
+
function cheaperThan(
|
|
78
|
+
model: ModelPricing,
|
|
79
|
+
catalogue: PricingCatalogue,
|
|
80
|
+
inputTokens: number,
|
|
81
|
+
on: Date,
|
|
82
|
+
): ModelPricing[] {
|
|
83
|
+
const here = effectivePricing(model, on);
|
|
84
|
+
return [...catalogue.byId.values()]
|
|
85
|
+
.filter((candidate) => {
|
|
86
|
+
if (candidate.id === model.id) return false;
|
|
87
|
+
if (candidate.provider !== model.provider) return false;
|
|
88
|
+
const there = effectivePricing(candidate, on);
|
|
89
|
+
if (there.inputPerMTok >= here.inputPerMTok) return false;
|
|
90
|
+
// A model the prompt does not fit in is not a cheaper way to make this
|
|
91
|
+
// call; it is a way not to make it.
|
|
92
|
+
return candidate.contextWindow >= inputTokens;
|
|
93
|
+
})
|
|
94
|
+
.sort((a, b) => effectivePricing(b, on).inputPerMTok - effectivePricing(a, on).inputPerMTok);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface GuardRequest extends AnswerRequest {
|
|
98
|
+
/** Whether the caller says this work can wait for a batch window. */
|
|
99
|
+
batchEligible?: boolean;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function guardSpend(
|
|
103
|
+
request: GuardRequest,
|
|
104
|
+
options: { catalogue: PricingCatalogue; on?: Date },
|
|
105
|
+
): GuardAnswer {
|
|
106
|
+
const { catalogue, on = new Date() } = options;
|
|
107
|
+
const cost = answerCost(request, { catalogue, on });
|
|
108
|
+
|
|
109
|
+
const alternatives: GuardAlternative[] = [];
|
|
110
|
+
const model = request.model === undefined ? undefined : catalogue.byId.get(request.model);
|
|
111
|
+
|
|
112
|
+
if (model !== undefined && cost.call !== null) {
|
|
113
|
+
const inputTokens = cost.call.inputTokens;
|
|
114
|
+
const outputTokens = cost.call.outputTokens;
|
|
115
|
+
const here = effectivePricing(model, on);
|
|
116
|
+
const priceOf = (candidate: ModelPricing): number => {
|
|
117
|
+
const rates = effectivePricing(candidate, on);
|
|
118
|
+
return (inputTokens / 1_000_000) * rates.inputPerMTok + (outputTokens / 1_000_000) * rates.outputPerMTok;
|
|
119
|
+
};
|
|
120
|
+
const mine = (inputTokens / 1_000_000) * here.inputPerMTok + (outputTokens / 1_000_000) * here.outputPerMTok;
|
|
121
|
+
const batchRate = multipliersFor(model).batch;
|
|
122
|
+
|
|
123
|
+
for (const candidate of cheaperThan(model, catalogue, inputTokens, on)) {
|
|
124
|
+
const routed = priceOf(candidate);
|
|
125
|
+
const candidateBatch = multipliersFor(candidate).batch;
|
|
126
|
+
const both = candidateBatch === null ? null : routed * candidateBatch;
|
|
127
|
+
/**
|
|
128
|
+
* Route and batch on the same call combine the way `billLevers` has
|
|
129
|
+
* combined them since 1.23: the batch discount applies to the *cheaper*
|
|
130
|
+
* model's price, never as a second subtraction from this one. Adding the
|
|
131
|
+
* two savings is the arithmetic `plan` exists to kill.
|
|
132
|
+
*/
|
|
133
|
+
if (request.batchEligible === true && both !== null) {
|
|
134
|
+
alternatives.push({
|
|
135
|
+
kind: 'route+batch',
|
|
136
|
+
model: { id: candidate.id, displayName: candidate.displayName },
|
|
137
|
+
savingUsd: mine - both,
|
|
138
|
+
assumes: [
|
|
139
|
+
{ kind: 'model-capability', model: candidate.displayName },
|
|
140
|
+
{ kind: 'batch-window' },
|
|
141
|
+
],
|
|
142
|
+
fits: true,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
alternatives.push({
|
|
146
|
+
kind: 'route',
|
|
147
|
+
model: { id: candidate.id, displayName: candidate.displayName },
|
|
148
|
+
savingUsd: mine - routed,
|
|
149
|
+
assumes: [{ kind: 'model-capability', model: candidate.displayName }],
|
|
150
|
+
fits: true,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (request.batchEligible === true && batchRate !== null) {
|
|
155
|
+
alternatives.push({
|
|
156
|
+
kind: 'batch',
|
|
157
|
+
model: null,
|
|
158
|
+
savingUsd: mine - mine * batchRate,
|
|
159
|
+
assumes: [{ kind: 'batch-window' }],
|
|
160
|
+
fits: true,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
alternatives.sort((a, b) => b.savingUsd - a.savingUsd);
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The verdict maps the cost answer's three outcomes onto the three an agent
|
|
169
|
+
* can act on. `cannot-tell` stays `cannot-tell` rather than defaulting to
|
|
170
|
+
* yes: a guard that permits whatever it cannot judge is a guard that permits
|
|
171
|
+
* everything the moment its inputs go missing.
|
|
172
|
+
*/
|
|
173
|
+
const verdict: GuardVerdict =
|
|
174
|
+
cost.verdict === 'cannot-tell' ? 'cannot-tell' : cost.verdict === 'over' ? 'no' : 'yes';
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
schemaVersion: 1,
|
|
178
|
+
verdict,
|
|
179
|
+
cost,
|
|
180
|
+
alternatives,
|
|
181
|
+
because: reasonFor(verdict, cost, alternatives),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function reasonFor(verdict: GuardVerdict, cost: CostAnswer, alternatives: GuardAlternative[]): string {
|
|
186
|
+
if (verdict === 'cannot-tell') {
|
|
187
|
+
return cost.reason === 'no-budget-configured'
|
|
188
|
+
? 'No budget is configured, so there is nothing to judge this against.'
|
|
189
|
+
: cost.reason === 'nothing-measured'
|
|
190
|
+
? 'Nothing has been measured yet, so how much of the budget is gone is unknown.'
|
|
191
|
+
: 'This model is not in the price catalogue, so the call cannot be priced.';
|
|
192
|
+
}
|
|
193
|
+
if (verdict === 'no') {
|
|
194
|
+
const lead = cost.restsOn === 'measured'
|
|
195
|
+
? 'The budget is already spent, measured.'
|
|
196
|
+
: 'This call would take the budget past its limit, on an estimate of the call.';
|
|
197
|
+
return alternatives.length === 0
|
|
198
|
+
? `${lead} No cheaper way to make this call exists in the catalogue.`
|
|
199
|
+
: `${lead} The cheapest alternative below saves the most.`;
|
|
200
|
+
}
|
|
201
|
+
return alternatives.length === 0
|
|
202
|
+
? 'Within budget, and no cheaper way to make this call exists in the catalogue.'
|
|
203
|
+
: 'Within budget — and there is still a cheaper way to make this call.';
|
|
204
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -49,6 +49,21 @@ export type { PlanAction, PlanActionKind, PlanAssumption, PlanDocument } from '.
|
|
|
49
49
|
export { verifyPlan } from './verify.js';
|
|
50
50
|
export { buildHistory, storedReportFrom, MIN_RUN } from './history.js';
|
|
51
51
|
export { answerCost } from './answer.js';
|
|
52
|
+
export { guardSpend } from './guard.js';
|
|
53
|
+
export type { GuardAlternative, GuardAnswer, GuardRequest, GuardVerdict } from './guard.js';
|
|
54
|
+
export { proposeInit, HEADLINE_FLOOR_SHARE, MIN_RATE_DAYS } from './init.js';
|
|
55
|
+
export type {
|
|
56
|
+
InitDecline,
|
|
57
|
+
InitHeadline,
|
|
58
|
+
InitJustification,
|
|
59
|
+
InitKey,
|
|
60
|
+
InitObservations,
|
|
61
|
+
InitOptions,
|
|
62
|
+
InitProposal,
|
|
63
|
+
NoHeadline,
|
|
64
|
+
ProviderSighting,
|
|
65
|
+
UsageSighting,
|
|
66
|
+
} from './init.js';
|
|
52
67
|
export type {
|
|
53
68
|
AnswerRequest,
|
|
54
69
|
AnswerVerdict,
|