@harrym1707/mesh 0.0.0-stage → 0.1.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 +28 -2
- package/mesh-runtime +4537 -0
- package/package.json +30 -4
package/mesh-runtime
ADDED
|
@@ -0,0 +1,4537 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire as __cr } from "node:module";
|
|
3
|
+
const require = __cr(import.meta.url);
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __esm = (fn, res, err) => function __init() {
|
|
7
|
+
if (err) throw err[0];
|
|
8
|
+
try {
|
|
9
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
+
} catch (e) {
|
|
11
|
+
throw err = [e], e;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// packages/core/src/cost.ts
|
|
20
|
+
var init_cost = __esm({
|
|
21
|
+
"packages/core/src/cost.ts"() {
|
|
22
|
+
"use strict";
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// packages/core/src/price-catalogue.ts
|
|
27
|
+
function assertIsoDate(value, label) {
|
|
28
|
+
if (!ISO_DATE.test(value) || Number.isNaN(Date.parse(value))) {
|
|
29
|
+
throw new Error(`${label} must be an ISO calendar date (YYYY-MM-DD); received ${JSON.stringify(value)}.`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function assertRate(value, label) {
|
|
33
|
+
if (value === void 0) return;
|
|
34
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
35
|
+
throw new Error(`${label} must be a non-negative finite number of USD per million tokens.`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function assertTiers(tiers, label) {
|
|
39
|
+
if (tiers.length === 0) throw new Error(`${label} must declare at least one price tier.`);
|
|
40
|
+
const unbounded = tiers.filter((tier) => tier.maxInputTokens === void 0);
|
|
41
|
+
if (unbounded.length !== 1) {
|
|
42
|
+
throw new Error(`${label} must declare exactly one tier without maxInputTokens; found ${unbounded.length}.`);
|
|
43
|
+
}
|
|
44
|
+
const bounds = tiers.map((tier) => tier.maxInputTokens).filter((bound) => bound !== void 0);
|
|
45
|
+
if (bounds.some((bound) => !Number.isInteger(bound) || bound <= 0)) {
|
|
46
|
+
throw new Error(`${label} tier bounds must be positive integers.`);
|
|
47
|
+
}
|
|
48
|
+
if (new Set(bounds).size !== bounds.length) {
|
|
49
|
+
throw new Error(`${label} declares duplicate tier bounds.`);
|
|
50
|
+
}
|
|
51
|
+
for (const tier of tiers) {
|
|
52
|
+
assertRate(tier.inputPerMillion, `${label} inputPerMillion`);
|
|
53
|
+
assertRate(tier.outputPerMillion, `${label} outputPerMillion`);
|
|
54
|
+
assertRate(tier.cachedInputPerMillion, `${label} cachedInputPerMillion`);
|
|
55
|
+
assertRate(tier.cacheWriteInputPerMillion, `${label} cacheWriteInputPerMillion`);
|
|
56
|
+
assertRate(tier.cacheWrite1hInputPerMillion, `${label} cacheWrite1hInputPerMillion`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function assertProvenance(provenance, label) {
|
|
60
|
+
if (!provenance || typeof provenance.source !== "string" || provenance.source.trim() === "") {
|
|
61
|
+
throw new Error(`${label} needs a provenance.source so the rate can be traced to a published price.`);
|
|
62
|
+
}
|
|
63
|
+
assertIsoDate(provenance.capturedOn, `${label} provenance.capturedOn`);
|
|
64
|
+
}
|
|
65
|
+
function rangeKey(item) {
|
|
66
|
+
return `${item.provider} ${item.model} ${item.mode ?? "standard"}`;
|
|
67
|
+
}
|
|
68
|
+
function overlaps(left, right) {
|
|
69
|
+
return left.effectiveFrom < (right.effectiveTo ?? OPEN_ENDED) && right.effectiveFrom < (left.effectiveTo ?? OPEN_ENDED);
|
|
70
|
+
}
|
|
71
|
+
function validatePriceCatalogue(catalogue) {
|
|
72
|
+
const seen = /* @__PURE__ */ new Map();
|
|
73
|
+
for (const entry3 of catalogue.entries) {
|
|
74
|
+
const label = `Price entry ${entry3.provider}/${entry3.model} (${entry3.mode}) from ${entry3.effectiveFrom}`;
|
|
75
|
+
if (!entry3.provider.trim() || !entry3.model.trim()) {
|
|
76
|
+
throw new Error("Price entries need a non-empty provider and model.");
|
|
77
|
+
}
|
|
78
|
+
assertIsoDate(entry3.effectiveFrom, `${label} effectiveFrom`);
|
|
79
|
+
if (entry3.effectiveTo !== void 0) {
|
|
80
|
+
assertIsoDate(entry3.effectiveTo, `${label} effectiveTo`);
|
|
81
|
+
if (entry3.effectiveTo <= entry3.effectiveFrom) {
|
|
82
|
+
throw new Error(`${label} ends on or before it starts.`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
assertTiers(entry3.tiers, label);
|
|
86
|
+
assertProvenance(entry3.provenance, label);
|
|
87
|
+
const key = rangeKey(entry3);
|
|
88
|
+
const ranges = seen.get(key) ?? [];
|
|
89
|
+
const clash = ranges.find((range) => overlaps(range, entry3));
|
|
90
|
+
if (clash) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
`${label} overlaps another rate for the same model and mode starting ${clash.effectiveFrom}; close the earlier rate with effectiveTo so each billing date resolves to exactly one price.`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
ranges.push(entry3);
|
|
96
|
+
seen.set(key, ranges);
|
|
97
|
+
}
|
|
98
|
+
const negotiatedSeen = /* @__PURE__ */ new Map();
|
|
99
|
+
for (const rate of catalogue.negotiated) {
|
|
100
|
+
const label = `Negotiated rate ${rate.provider}/${rate.model} from ${rate.effectiveFrom}`;
|
|
101
|
+
const hasDiscount = rate.discountPercent !== void 0;
|
|
102
|
+
const hasTiers = rate.tiers !== void 0;
|
|
103
|
+
if (hasDiscount === hasTiers) {
|
|
104
|
+
throw new Error(`${label} must set exactly one of discountPercent or tiers.`);
|
|
105
|
+
}
|
|
106
|
+
assertIsoDate(rate.effectiveFrom, `${label} effectiveFrom`);
|
|
107
|
+
if (rate.effectiveTo !== void 0) {
|
|
108
|
+
assertIsoDate(rate.effectiveTo, `${label} effectiveTo`);
|
|
109
|
+
if (rate.effectiveTo <= rate.effectiveFrom) throw new Error(`${label} ends on or before it starts.`);
|
|
110
|
+
}
|
|
111
|
+
if (hasDiscount && (!Number.isFinite(rate.discountPercent) || rate.discountPercent < 0 || rate.discountPercent >= 100)) {
|
|
112
|
+
throw new Error(`${label} discountPercent must be at least 0 and below 100.`);
|
|
113
|
+
}
|
|
114
|
+
if (hasTiers) assertTiers(rate.tiers, label);
|
|
115
|
+
assertProvenance(rate.provenance, label);
|
|
116
|
+
const key = rangeKey(rate);
|
|
117
|
+
const ranges = negotiatedSeen.get(key) ?? [];
|
|
118
|
+
if (ranges.some((range) => overlaps(range, rate))) {
|
|
119
|
+
throw new Error(`${label} overlaps another negotiated rate for the same model and mode.`);
|
|
120
|
+
}
|
|
121
|
+
ranges.push(rate);
|
|
122
|
+
negotiatedSeen.set(key, ranges);
|
|
123
|
+
}
|
|
124
|
+
return catalogue;
|
|
125
|
+
}
|
|
126
|
+
function applicable(items, at) {
|
|
127
|
+
return items.find((item) => item.effectiveFrom <= at && (item.effectiveTo === void 0 || at < item.effectiveTo));
|
|
128
|
+
}
|
|
129
|
+
function tierFor(tiers, inputTokens) {
|
|
130
|
+
const bounded = tiers.filter((tier) => tier.maxInputTokens !== void 0).sort((left, right) => left.maxInputTokens - right.maxInputTokens);
|
|
131
|
+
const match = bounded.find((tier) => inputTokens <= tier.maxInputTokens);
|
|
132
|
+
return match ?? tiers.find((tier) => tier.maxInputTokens === void 0);
|
|
133
|
+
}
|
|
134
|
+
function withoutBound(tier) {
|
|
135
|
+
const { maxInputTokens: _bound, ...price } = tier;
|
|
136
|
+
return price;
|
|
137
|
+
}
|
|
138
|
+
function discounted(tier, discountPercent) {
|
|
139
|
+
const factor = 1 - discountPercent / 100;
|
|
140
|
+
return {
|
|
141
|
+
inputPerMillion: tier.inputPerMillion * factor,
|
|
142
|
+
outputPerMillion: tier.outputPerMillion * factor,
|
|
143
|
+
...tier.cachedInputPerMillion === void 0 ? {} : { cachedInputPerMillion: tier.cachedInputPerMillion * factor },
|
|
144
|
+
...tier.cacheWriteInputPerMillion === void 0 ? {} : { cacheWriteInputPerMillion: tier.cacheWriteInputPerMillion * factor },
|
|
145
|
+
...tier.cacheWrite1hInputPerMillion === void 0 ? {} : { cacheWrite1hInputPerMillion: tier.cacheWrite1hInputPerMillion * factor }
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function lookupPrice(catalogue, query, inputTokens = 0) {
|
|
149
|
+
assertIsoDate(query.at, "Price query `at`");
|
|
150
|
+
const mode = query.mode ?? "standard";
|
|
151
|
+
const candidates = catalogue.entries.filter((entry4) => entry4.provider === query.provider && entry4.model === query.model && entry4.mode === mode);
|
|
152
|
+
if (candidates.length === 0) {
|
|
153
|
+
return { status: "unpriced", reason: `No ${mode} rate is catalogued for ${query.provider}/${query.model}.` };
|
|
154
|
+
}
|
|
155
|
+
const entry3 = applicable(candidates, query.at);
|
|
156
|
+
if (!entry3) {
|
|
157
|
+
const ranges = candidates.map((candidate) => `${candidate.effectiveFrom} to ${candidate.effectiveTo ?? "current"}`).join("; ");
|
|
158
|
+
return {
|
|
159
|
+
status: "unpriced",
|
|
160
|
+
reason: `No ${mode} rate for ${query.provider}/${query.model} covers ${query.at}; catalogued ranges are ${ranges}.`
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
const tier = tierFor(entry3.tiers, inputTokens);
|
|
164
|
+
const override = applicable(
|
|
165
|
+
catalogue.negotiated.filter((rate) => rate.provider === query.provider && rate.model === query.model && (rate.mode ?? "standard") === mode),
|
|
166
|
+
query.at
|
|
167
|
+
);
|
|
168
|
+
if (!override) return { status: "priced", price: withoutBound(tier), entry: entry3 };
|
|
169
|
+
const price = override.tiers ? withoutBound(tierFor(override.tiers, inputTokens)) : discounted(tier, override.discountPercent);
|
|
170
|
+
return { status: "priced", price, entry: entry3, negotiated: override };
|
|
171
|
+
}
|
|
172
|
+
function canonicalJson(value) {
|
|
173
|
+
if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "null";
|
|
174
|
+
if (Array.isArray(value)) {
|
|
175
|
+
return `[${value.map((item) => canonicalJson(item)).join(",")}]`;
|
|
176
|
+
}
|
|
177
|
+
const entries = Object.entries(value).filter(([, member]) => member !== void 0).sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0).map(([key, member]) => `${JSON.stringify(key)}:${canonicalJson(member)}`);
|
|
178
|
+
return `{${entries.join(",")}}`;
|
|
179
|
+
}
|
|
180
|
+
function canonicalSegments(value) {
|
|
181
|
+
return value.toLowerCase().replace(/[-_]latest$/, "").replace(/[-_]20\d{2}[-_]\d{2}[-_]\d{2}$/, "").replace(/[-_]20\d{6}$/, "").split(/[^a-z0-9]+/).filter((segment) => segment !== "");
|
|
182
|
+
}
|
|
183
|
+
function containsSegments(id, key) {
|
|
184
|
+
if (key.length === 0 || key.length > id.length) return false;
|
|
185
|
+
for (let offset = 0; offset + key.length <= id.length; offset += 1) {
|
|
186
|
+
if (key.every((segment, index) => id[offset + index] === segment)) return true;
|
|
187
|
+
}
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
function resolveModelId(catalogue, provider, rawModelId) {
|
|
191
|
+
const known = [...new Set(catalogue.entries.filter((entry3) => entry3.provider === provider).map((entry3) => entry3.model))];
|
|
192
|
+
if (known.length === 0) {
|
|
193
|
+
return { status: "unresolved", reason: `No models are catalogued for provider ${provider}.`, candidates: [] };
|
|
194
|
+
}
|
|
195
|
+
if (known.includes(rawModelId)) return { status: "resolved", model: rawModelId, via: "exact" };
|
|
196
|
+
for (const alias of catalogue.aliases ?? []) {
|
|
197
|
+
if (alias.provider !== provider) continue;
|
|
198
|
+
const matches = alias.id.endsWith("*") ? rawModelId.startsWith(alias.id.slice(0, -1)) : alias.id === rawModelId;
|
|
199
|
+
if (!matches) continue;
|
|
200
|
+
if (!known.includes(alias.model)) {
|
|
201
|
+
return {
|
|
202
|
+
status: "unresolved",
|
|
203
|
+
reason: `Alias ${alias.id} points at ${alias.model}, which is not catalogued for ${provider}.`,
|
|
204
|
+
candidates: known
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
return { status: "resolved", model: alias.model, via: "alias" };
|
|
208
|
+
}
|
|
209
|
+
const target = canonicalSegments(rawModelId);
|
|
210
|
+
const hits = known.map((model) => ({ model, key: canonicalSegments(model) })).filter(({ key }) => containsSegments(target, key)).sort((left, right) => right.key.length - left.key.length || right.key.join("").length - left.key.join("").length);
|
|
211
|
+
if (hits.length === 0) {
|
|
212
|
+
return {
|
|
213
|
+
status: "unresolved",
|
|
214
|
+
reason: `No catalogue key for ${provider} matches ${rawModelId}; add an alias.`,
|
|
215
|
+
candidates: known
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
const longest = hits[0].key.length;
|
|
219
|
+
const tied = hits.filter(({ key }) => key.length === longest);
|
|
220
|
+
if (tied.length > 1) {
|
|
221
|
+
return {
|
|
222
|
+
status: "unresolved",
|
|
223
|
+
reason: `${rawModelId} matches ${tied.length} catalogue keys equally well; add an explicit alias.`,
|
|
224
|
+
candidates: tied.map(({ model }) => model)
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
return { status: "resolved", model: hits[0].model, via: "normalised" };
|
|
228
|
+
}
|
|
229
|
+
var ISO_DATE, OPEN_ENDED;
|
|
230
|
+
var init_price_catalogue = __esm({
|
|
231
|
+
"packages/core/src/price-catalogue.ts"() {
|
|
232
|
+
"use strict";
|
|
233
|
+
init_cost();
|
|
234
|
+
ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
|
|
235
|
+
OPEN_ENDED = "9999-12-31";
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
// packages/core/src/savings-ledger.ts
|
|
240
|
+
import { createHash } from "node:crypto";
|
|
241
|
+
function ledgerEventHash(seq, prevHash, event) {
|
|
242
|
+
return createHash("sha256").update(`${seq}
|
|
243
|
+
${prevHash}
|
|
244
|
+
${canonicalJson(event)}`).digest("hex");
|
|
245
|
+
}
|
|
246
|
+
function verifyLedgerChain(records) {
|
|
247
|
+
let prevHash = LEDGER_GENESIS_HASH;
|
|
248
|
+
for (let index = 0; index < records.length; index += 1) {
|
|
249
|
+
const record = records[index];
|
|
250
|
+
if (record.seq !== index) {
|
|
251
|
+
return { ok: false, seq: record.seq, reason: `Expected seq ${index}, found ${record.seq}.` };
|
|
252
|
+
}
|
|
253
|
+
if (record.prevHash !== prevHash) {
|
|
254
|
+
return { ok: false, seq: record.seq, reason: "prevHash does not match the preceding record." };
|
|
255
|
+
}
|
|
256
|
+
const expected = ledgerEventHash(record.seq, record.prevHash, record.event);
|
|
257
|
+
if (record.eventHash !== expected) {
|
|
258
|
+
return { ok: false, seq: record.seq, reason: "eventHash does not match the event content." };
|
|
259
|
+
}
|
|
260
|
+
prevHash = record.eventHash;
|
|
261
|
+
}
|
|
262
|
+
return { ok: true, length: records.length };
|
|
263
|
+
}
|
|
264
|
+
function compareEvents(left, right) {
|
|
265
|
+
if (left.timestamp !== right.timestamp) return left.timestamp < right.timestamp ? -1 : 1;
|
|
266
|
+
if (left.requestId !== right.requestId) return left.requestId < right.requestId ? -1 : 1;
|
|
267
|
+
return 0;
|
|
268
|
+
}
|
|
269
|
+
function leverUnbilledReason(lever, gateVerdictOverrides) {
|
|
270
|
+
const verdict = gateVerdictOverrides?.[lever.leverId] ?? lever.gateVerdict?.verdict;
|
|
271
|
+
if (lever.savedUsd === void 0 || !Number.isFinite(lever.savedUsd)) {
|
|
272
|
+
return "no per-request verified measurement";
|
|
273
|
+
}
|
|
274
|
+
if (lever.kind === "lossy" && verdict !== "approved") {
|
|
275
|
+
return verdict === "rejected" ? "replay gate verdict rejected" : "no approved replay gate verdict";
|
|
276
|
+
}
|
|
277
|
+
return void 0;
|
|
278
|
+
}
|
|
279
|
+
function selectBillableEvents(events, options = {}) {
|
|
280
|
+
for (const event of events) {
|
|
281
|
+
if (event.schemaVersion !== 1) {
|
|
282
|
+
throw new Error(`Unsupported billable event schemaVersion ${JSON.stringify(event.schemaVersion)}.`);
|
|
283
|
+
}
|
|
284
|
+
if (typeof event.requestId !== "string" || event.requestId === "") {
|
|
285
|
+
throw new Error("Billable events need a non-empty requestId idempotency key.");
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
const sorted = [...events].sort(compareEvents);
|
|
289
|
+
const seen = /* @__PURE__ */ new Set();
|
|
290
|
+
let duplicatesIgnored = 0;
|
|
291
|
+
const deduped = [];
|
|
292
|
+
for (const event of sorted) {
|
|
293
|
+
if (seen.has(event.requestId)) {
|
|
294
|
+
duplicatesIgnored += 1;
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
seen.add(event.requestId);
|
|
298
|
+
deduped.push(event);
|
|
299
|
+
}
|
|
300
|
+
const consideredEvents = options.period === void 0 ? deduped : deduped.filter((event) => event.invoicePeriod === options.period);
|
|
301
|
+
const failedTaskIds = new Set(options.failedTaskIds ?? []);
|
|
302
|
+
for (const event of consideredEvents) {
|
|
303
|
+
if (event.status === "failed" && event.taskId !== void 0) failedTaskIds.add(event.taskId);
|
|
304
|
+
}
|
|
305
|
+
let eventsExcludedForFailedTasks = 0;
|
|
306
|
+
const billableEvents = [];
|
|
307
|
+
for (const event of consideredEvents) {
|
|
308
|
+
const taskFailed = event.taskId !== void 0 && failedTaskIds.has(event.taskId);
|
|
309
|
+
if (taskFailed || event.status === "failed") {
|
|
310
|
+
eventsExcludedForFailedTasks += 1;
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
billableEvents.push(event);
|
|
314
|
+
}
|
|
315
|
+
return { consideredEvents, billableEvents, failedTaskIds, eventsExcludedForFailedTasks, duplicatesIgnored };
|
|
316
|
+
}
|
|
317
|
+
function recomputeInvoice(events, options = {}) {
|
|
318
|
+
return recomputeInvoiceWithAudit(events, options).invoice;
|
|
319
|
+
}
|
|
320
|
+
function recomputeInvoiceWithAudit(events, options = {}) {
|
|
321
|
+
if (options.feePercent !== void 0 && (!Number.isFinite(options.feePercent) || options.feePercent < 0 || options.feePercent > 100)) {
|
|
322
|
+
throw new Error("feePercent must be between 0 and 100.");
|
|
323
|
+
}
|
|
324
|
+
const {
|
|
325
|
+
consideredEvents: inPeriod,
|
|
326
|
+
billableEvents,
|
|
327
|
+
failedTaskIds,
|
|
328
|
+
eventsExcludedForFailedTasks,
|
|
329
|
+
duplicatesIgnored
|
|
330
|
+
} = selectBillableEvents(events, options);
|
|
331
|
+
const lineMap = /* @__PURE__ */ new Map();
|
|
332
|
+
const unbilledMap = /* @__PURE__ */ new Map();
|
|
333
|
+
const catalogueVersions = /* @__PURE__ */ new Set();
|
|
334
|
+
let unpricedEvents = 0;
|
|
335
|
+
let providerCacheOnAppendedHistoryUsd = 0;
|
|
336
|
+
let gatewayCacheUsd = 0;
|
|
337
|
+
let eventsBilled = 0;
|
|
338
|
+
for (const event of billableEvents) {
|
|
339
|
+
if (event.catalogueVersion !== void 0) catalogueVersions.add(event.catalogueVersion);
|
|
340
|
+
if (event.pricingStatus === "unpriced") unpricedEvents += 1;
|
|
341
|
+
providerCacheOnAppendedHistoryUsd += event.excluded?.providerCacheAlreadyOwnedUsd ?? 0;
|
|
342
|
+
gatewayCacheUsd += event.excluded?.gatewayCacheUsd ?? 0;
|
|
343
|
+
let billedAny = false;
|
|
344
|
+
for (const lever of event.levers) {
|
|
345
|
+
const unbilledReason = leverUnbilledReason(lever, options.gateVerdictOverrides);
|
|
346
|
+
if (unbilledReason !== void 0) {
|
|
347
|
+
const key2 = `${lever.kind}
|
|
348
|
+
${lever.leverId}
|
|
349
|
+
${unbilledReason}`;
|
|
350
|
+
const existing = unbilledMap.get(key2);
|
|
351
|
+
if (existing) existing.events += 1;
|
|
352
|
+
else unbilledMap.set(key2, { leverId: lever.leverId, kind: lever.kind, events: 1, reason: unbilledReason });
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
const key = `${lever.kind}
|
|
356
|
+
${lever.leverId}`;
|
|
357
|
+
const line = lineMap.get(key) ?? { leverId: lever.leverId, kind: lever.kind, events: 0, savedUsd: 0, estimated: false, refs: /* @__PURE__ */ new Set() };
|
|
358
|
+
line.events += 1;
|
|
359
|
+
line.savedUsd += lever.savedUsd;
|
|
360
|
+
if (lever.basis === "estimated-baseline") line.estimated = true;
|
|
361
|
+
if (lever.gateVerdict?.ref) line.refs.add(lever.gateVerdict.ref);
|
|
362
|
+
lineMap.set(key, line);
|
|
363
|
+
billedAny = true;
|
|
364
|
+
}
|
|
365
|
+
if (billedAny) eventsBilled += 1;
|
|
366
|
+
}
|
|
367
|
+
const lineItems = [...lineMap.values()].sort((left, right) => left.leverId < right.leverId ? -1 : left.leverId > right.leverId ? 1 : 0).map((line) => ({
|
|
368
|
+
leverId: line.leverId,
|
|
369
|
+
kind: line.kind,
|
|
370
|
+
events: line.events,
|
|
371
|
+
savedUsd: line.savedUsd,
|
|
372
|
+
savedCents: Math.round(line.savedUsd * 100),
|
|
373
|
+
...line.estimated ? { basis: "estimated-baseline" } : {},
|
|
374
|
+
gateVerdictRefs: [...line.refs].sort()
|
|
375
|
+
}));
|
|
376
|
+
const recordedUnbilled = [...unbilledMap.values()].sort((left, right) => left.leverId < right.leverId ? -1 : left.leverId > right.leverId ? 1 : 0);
|
|
377
|
+
const netVerifiedSavingsUsd = lineItems.reduce((sum, line) => sum + line.savedUsd, 0);
|
|
378
|
+
const netVerifiedSavingsCents = Math.round(netVerifiedSavingsUsd * 100);
|
|
379
|
+
const carryIn = options.carryForwardDeficitUsd ?? 0;
|
|
380
|
+
if (carryIn < 0 || !Number.isFinite(carryIn)) {
|
|
381
|
+
throw new Error("carryForwardDeficitUsd must be a non-negative, finite USD amount.");
|
|
382
|
+
}
|
|
383
|
+
const netAfterCarryUsd = netVerifiedSavingsUsd - carryIn;
|
|
384
|
+
const carryOutUsd = Math.max(0, -netAfterCarryUsd);
|
|
385
|
+
const billableCents = Math.round(netAfterCarryUsd * 100);
|
|
386
|
+
const verdict = billableCents > 0 ? "charge" : "do_not_charge";
|
|
387
|
+
const amountDueUsd = options.feePercent === void 0 ? void 0 : verdict === "charge" ? netAfterCarryUsd * options.feePercent / 100 : 0;
|
|
388
|
+
const invoice = {
|
|
389
|
+
schemaVersion: 1,
|
|
390
|
+
period: options.period ?? null,
|
|
391
|
+
catalogueVersions: [...catalogueVersions].sort(),
|
|
392
|
+
eventsConsidered: inPeriod.length,
|
|
393
|
+
eventsBilled,
|
|
394
|
+
failedTaskIds: [...failedTaskIds].sort(),
|
|
395
|
+
eventsExcludedForFailedTasks,
|
|
396
|
+
unpricedEvents,
|
|
397
|
+
lineItems,
|
|
398
|
+
recordedUnbilled,
|
|
399
|
+
excludedAlreadyOwned: {
|
|
400
|
+
providerCacheOnAppendedHistoryUsd,
|
|
401
|
+
gatewayCacheUsd
|
|
402
|
+
},
|
|
403
|
+
netVerifiedSavingsUsd,
|
|
404
|
+
netVerifiedSavingsCents,
|
|
405
|
+
...options.carryForwardDeficitUsd === void 0 ? {} : { carryForwardDeficitInUsd: carryIn },
|
|
406
|
+
...carryOutUsd > 0 ? { carryForwardDeficitOutUsd: carryOutUsd } : {},
|
|
407
|
+
verdict,
|
|
408
|
+
...options.feePercent === void 0 ? {} : {
|
|
409
|
+
feePercent: options.feePercent,
|
|
410
|
+
amountDueUsd,
|
|
411
|
+
amountDueCents: Math.round((amountDueUsd ?? 0) * 100)
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
return { invoice, audit: { duplicatesIgnored } };
|
|
415
|
+
}
|
|
416
|
+
var LEDGER_GENESIS_HASH;
|
|
417
|
+
var init_savings_ledger = __esm({
|
|
418
|
+
"packages/core/src/savings-ledger.ts"() {
|
|
419
|
+
"use strict";
|
|
420
|
+
init_price_catalogue();
|
|
421
|
+
LEDGER_GENESIS_HASH = "0".repeat(64);
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
// packages/core/dist/src/cost.js
|
|
426
|
+
function estimateTokenCost2(inputTokens, outputTokens, price, cacheUsage = {}) {
|
|
427
|
+
const cached = cacheUsage.cachedInputTokens ?? 0;
|
|
428
|
+
const cacheWrite = cacheUsage.cacheWriteInputTokens ?? 0;
|
|
429
|
+
const cacheWrite1h = cacheUsage.cacheWrite1hInputTokens ?? 0;
|
|
430
|
+
if (!Number.isFinite(cached) || cached < 0 || !Number.isFinite(cacheWrite) || cacheWrite < 0 || !Number.isFinite(cacheWrite1h) || cacheWrite1h < 0 || cached + cacheWrite > inputTokens || cacheWrite1h > cacheWrite) {
|
|
431
|
+
throw new Error("Cached input token counts must be non-negative and no greater than total input tokens.");
|
|
432
|
+
}
|
|
433
|
+
const cacheWrite5m = cacheWrite - cacheWrite1h;
|
|
434
|
+
if (cached > 0 && price.cachedInputPerMillion === void 0 || cacheWrite5m > 0 && price.cacheWriteInputPerMillion === void 0 || cacheWrite1h > 0 && price.cacheWrite1hInputPerMillion === void 0) {
|
|
435
|
+
throw new Error("Cached input pricing is required to calculate actual cost for cached tokens.");
|
|
436
|
+
}
|
|
437
|
+
const uncached = inputTokens - cached - cacheWrite;
|
|
438
|
+
return uncached / 1e6 * price.inputPerMillion + cached / 1e6 * (price.cachedInputPerMillion ?? 0) + cacheWrite5m / 1e6 * (price.cacheWriteInputPerMillion ?? 0) + cacheWrite1h / 1e6 * (price.cacheWrite1hInputPerMillion ?? 0) + outputTokens / 1e6 * price.outputPerMillion;
|
|
439
|
+
}
|
|
440
|
+
var init_cost2 = __esm({
|
|
441
|
+
"packages/core/dist/src/cost.js"() {
|
|
442
|
+
"use strict";
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
// packages/core/dist/src/token-reduction.js
|
|
447
|
+
function dedupeRepeatedBlocks(minimumBlockTokens = 24, countTokens = approximateTokenCount) {
|
|
448
|
+
return {
|
|
449
|
+
id: "dedupe-repeated-blocks",
|
|
450
|
+
version: "2",
|
|
451
|
+
risk: "lossless",
|
|
452
|
+
reduce: (text) => {
|
|
453
|
+
const seen = /* @__PURE__ */ new Set();
|
|
454
|
+
const notes = [];
|
|
455
|
+
let dropped = false;
|
|
456
|
+
const describe = (unit, tokens) => `Dropped a repeated ${tokens}-token block beginning "${unit.trim().replace(/\s+/g, " ").slice(0, 60)}".`;
|
|
457
|
+
const dedupeLines = (block) => {
|
|
458
|
+
if (block.includes("```") || !block.includes("\n"))
|
|
459
|
+
return block;
|
|
460
|
+
const lines = block.split(/(\n)/);
|
|
461
|
+
let changed = false;
|
|
462
|
+
const keptLines = [];
|
|
463
|
+
for (let index = 0; index < lines.length; index += 2) {
|
|
464
|
+
const line = lines[index];
|
|
465
|
+
const separator = lines[index + 1] ?? "";
|
|
466
|
+
const tokens = countTokens(line);
|
|
467
|
+
if (line.trim() !== "" && tokens >= minimumBlockTokens && seen.has(line)) {
|
|
468
|
+
dropped = true;
|
|
469
|
+
changed = true;
|
|
470
|
+
notes.push(describe(line, tokens));
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
seen.add(line);
|
|
474
|
+
keptLines.push(line, separator);
|
|
475
|
+
}
|
|
476
|
+
return changed ? keptLines.join("") : block;
|
|
477
|
+
};
|
|
478
|
+
const parts = text.split(/(\n{2,})/);
|
|
479
|
+
const kept = [];
|
|
480
|
+
for (let index = 0; index < parts.length; index += 2) {
|
|
481
|
+
const block = parts[index];
|
|
482
|
+
const separator = parts[index + 1] ?? "";
|
|
483
|
+
const tokens = countTokens(block);
|
|
484
|
+
if (block.trim() !== "" && !block.includes("```") && tokens >= minimumBlockTokens && seen.has(block)) {
|
|
485
|
+
dropped = true;
|
|
486
|
+
notes.push(describe(block, tokens));
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
seen.add(block);
|
|
490
|
+
kept.push(dedupeLines(block), separator);
|
|
491
|
+
}
|
|
492
|
+
if (!dropped)
|
|
493
|
+
return { text, notes: [] };
|
|
494
|
+
return { text: kept.join(""), notes };
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
function commonPrefixLength(left, right) {
|
|
499
|
+
const limit = Math.min(left.length, right.length);
|
|
500
|
+
let index = 0;
|
|
501
|
+
while (index < limit && left[index] === right[index])
|
|
502
|
+
index += 1;
|
|
503
|
+
return index;
|
|
504
|
+
}
|
|
505
|
+
function dedupeRepeatedLeadingBlock(texts, minimumBlockTokens = 24, countTokens = approximateTokenCount) {
|
|
506
|
+
if (texts.length === 0)
|
|
507
|
+
return { texts: [], notes: [] };
|
|
508
|
+
const canonical = texts[0];
|
|
509
|
+
const notes = [];
|
|
510
|
+
const nextTexts = texts.map((text, index) => {
|
|
511
|
+
if (index === 0)
|
|
512
|
+
return text;
|
|
513
|
+
const raw = commonPrefixLength(canonical, text);
|
|
514
|
+
if (raw === 0)
|
|
515
|
+
return text;
|
|
516
|
+
const fullContainment = raw === canonical.length || raw === text.length;
|
|
517
|
+
let safe = raw;
|
|
518
|
+
if (!fullContainment) {
|
|
519
|
+
const lastNewline = text.lastIndexOf("\n", raw - 1);
|
|
520
|
+
safe = lastNewline === -1 ? 0 : lastNewline + 1;
|
|
521
|
+
}
|
|
522
|
+
if (safe === 0)
|
|
523
|
+
return text;
|
|
524
|
+
const removed = text.slice(0, safe);
|
|
525
|
+
if (removed.trim() === "" || removed.includes("```"))
|
|
526
|
+
return text;
|
|
527
|
+
const tokens = countTokens(removed);
|
|
528
|
+
if (tokens < minimumBlockTokens)
|
|
529
|
+
return text;
|
|
530
|
+
notes.push(`Turn ${index + 1}: replaced a ${tokens}-token leading block identical to turn 1's opening with a reference marker.`);
|
|
531
|
+
return `[mesh: ${tokens} tokens of this turn's leading context, identical to turn 1, omitted]${text.slice(safe)}`;
|
|
532
|
+
});
|
|
533
|
+
return { texts: nextTexts, notes };
|
|
534
|
+
}
|
|
535
|
+
var approximateTokenCount;
|
|
536
|
+
var init_token_reduction = __esm({
|
|
537
|
+
"packages/core/dist/src/token-reduction.js"() {
|
|
538
|
+
"use strict";
|
|
539
|
+
init_cost2();
|
|
540
|
+
approximateTokenCount = (text) => {
|
|
541
|
+
if (text === "")
|
|
542
|
+
return 0;
|
|
543
|
+
let total = 0;
|
|
544
|
+
for (const match of text.matchAll(/[\p{L}\p{N}']+|[^\p{L}\p{N}'\s]+|\s+/gu)) {
|
|
545
|
+
const chunk = match[0];
|
|
546
|
+
if (/^\s+$/.test(chunk)) {
|
|
547
|
+
total += chunk.match(/\n/g)?.length ?? 0;
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
550
|
+
if (/^[\p{L}\p{N}']+$/u.test(chunk)) {
|
|
551
|
+
total += Math.max(1, Math.ceil(chunk.length / 4.5));
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
total += Math.max(1, Math.ceil(chunk.length / 2));
|
|
555
|
+
}
|
|
556
|
+
return total;
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
// packages/core/dist/src/output-diagnostics.js
|
|
562
|
+
function normalizeLine(line) {
|
|
563
|
+
return line.replace(LINE_NUMBER_PREFIX, "").trim().replace(/\s+/g, " ");
|
|
564
|
+
}
|
|
565
|
+
function looksLikeCode(line) {
|
|
566
|
+
if (/[{};=()[\]<>]/.test(line) && (line.match(/[{};=()[\]<>.:]/g) ?? []).length >= 3)
|
|
567
|
+
return true;
|
|
568
|
+
return /^(import |from |def |class |function |const |let |var |return |if |for |while |#include|public |private )/.test(line.trimStart());
|
|
569
|
+
}
|
|
570
|
+
function collectRequestText(value, depth = 0) {
|
|
571
|
+
if (depth > 12)
|
|
572
|
+
return "";
|
|
573
|
+
if (typeof value === "string")
|
|
574
|
+
return value;
|
|
575
|
+
if (Array.isArray(value))
|
|
576
|
+
return value.map((entry3) => collectRequestText(entry3, depth + 1)).filter(Boolean).join("\n");
|
|
577
|
+
if (typeof value === "object" && value !== null) {
|
|
578
|
+
return Object.values(value).map((entry3) => collectRequestText(entry3, depth + 1)).filter(Boolean).join("\n");
|
|
579
|
+
}
|
|
580
|
+
return "";
|
|
581
|
+
}
|
|
582
|
+
function measureOutputReEmission(requestText, outputText, countTokens = approximateTokenCount) {
|
|
583
|
+
const contextLines = /* @__PURE__ */ new Set();
|
|
584
|
+
for (const line of requestText.split("\n")) {
|
|
585
|
+
const normalized = normalizeLine(line);
|
|
586
|
+
if (normalized.length >= 8)
|
|
587
|
+
contextLines.add(normalized);
|
|
588
|
+
}
|
|
589
|
+
let inspected = 0;
|
|
590
|
+
let reEmitted = 0;
|
|
591
|
+
let reEmittedCode = 0;
|
|
592
|
+
for (const line of outputText.split("\n")) {
|
|
593
|
+
const tokens = countTokens(line);
|
|
594
|
+
inspected += tokens;
|
|
595
|
+
const normalized = normalizeLine(line);
|
|
596
|
+
if (normalized.length >= 8 && contextLines.has(normalized)) {
|
|
597
|
+
reEmitted += tokens;
|
|
598
|
+
if (looksLikeCode(normalized))
|
|
599
|
+
reEmittedCode += tokens;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return {
|
|
603
|
+
inspectedOutputTokens: inspected,
|
|
604
|
+
reEmittedOutputTokens: reEmitted,
|
|
605
|
+
reEmittedCodeTokens: reEmittedCode
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
var LINE_NUMBER_PREFIX;
|
|
609
|
+
var init_output_diagnostics = __esm({
|
|
610
|
+
"packages/core/dist/src/output-diagnostics.js"() {
|
|
611
|
+
"use strict";
|
|
612
|
+
init_token_reduction();
|
|
613
|
+
LINE_NUMBER_PREFIX = /^\s*\d+[:.|]\s?/;
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
// packages/core/dist/src/provider-usage.js
|
|
618
|
+
function isPlainObject(value) {
|
|
619
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
620
|
+
}
|
|
621
|
+
function asCount(value) {
|
|
622
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
623
|
+
}
|
|
624
|
+
function normalizeProviderUsage(response) {
|
|
625
|
+
if (!isPlainObject(response))
|
|
626
|
+
return { status: "missing" };
|
|
627
|
+
const usage = isPlainObject(response.usage) ? response.usage : void 0;
|
|
628
|
+
if (!usage)
|
|
629
|
+
return { status: "missing" };
|
|
630
|
+
const output = asCount(usage.output_tokens) ?? asCount(usage.completion_tokens);
|
|
631
|
+
if (output === void 0) {
|
|
632
|
+
return { status: "invalid", reason: "Usage carries no readable output_tokens/completion_tokens." };
|
|
633
|
+
}
|
|
634
|
+
const anthropicRead = asCount(usage.cache_read_input_tokens);
|
|
635
|
+
const anthropicWriteFlat = asCount(usage.cache_creation_input_tokens);
|
|
636
|
+
const creation = isPlainObject(usage.cache_creation) ? usage.cache_creation : void 0;
|
|
637
|
+
if (anthropicRead !== void 0 || anthropicWriteFlat !== void 0 || creation !== void 0) {
|
|
638
|
+
const fresh = asCount(usage.input_tokens) ?? 0;
|
|
639
|
+
const cached2 = anthropicRead ?? 0;
|
|
640
|
+
let written = anthropicWriteFlat ?? 0;
|
|
641
|
+
let write5m;
|
|
642
|
+
let write1h;
|
|
643
|
+
if (creation !== void 0) {
|
|
644
|
+
write5m = asCount(creation.ephemeral_5m_input_tokens) ?? 0;
|
|
645
|
+
write1h = asCount(creation.ephemeral_1h_input_tokens) ?? 0;
|
|
646
|
+
const splitTotal = write5m + write1h;
|
|
647
|
+
if (anthropicWriteFlat !== void 0 && splitTotal !== anthropicWriteFlat) {
|
|
648
|
+
return {
|
|
649
|
+
status: "invalid",
|
|
650
|
+
reason: `cache_creation split (5m ${write5m} + 1h ${write1h} = ${splitTotal}) does not sum to cache_creation_input_tokens ${anthropicWriteFlat}.`
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
written = anthropicWriteFlat ?? splitTotal;
|
|
654
|
+
}
|
|
655
|
+
return {
|
|
656
|
+
status: "ok",
|
|
657
|
+
usage: {
|
|
658
|
+
inputTokens: fresh + cached2 + written,
|
|
659
|
+
uncachedInputTokens: fresh,
|
|
660
|
+
cachedInputTokens: cached2,
|
|
661
|
+
cacheWriteInputTokens: written,
|
|
662
|
+
...write5m === void 0 ? {} : { cacheWrite5mInputTokens: write5m },
|
|
663
|
+
...write1h === void 0 ? {} : { cacheWrite1hInputTokens: write1h },
|
|
664
|
+
outputTokens: output
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
const input = asCount(usage.input_tokens) ?? asCount(usage.prompt_tokens);
|
|
669
|
+
if (input === void 0) {
|
|
670
|
+
return { status: "invalid", reason: "Usage carries no readable input_tokens/prompt_tokens." };
|
|
671
|
+
}
|
|
672
|
+
const details = isPlainObject(usage.input_tokens_details) ? usage.input_tokens_details : void 0;
|
|
673
|
+
const promptDetails = isPlainObject(usage.prompt_tokens_details) ? usage.prompt_tokens_details : void 0;
|
|
674
|
+
const cached = asCount(details?.cached_tokens) ?? asCount(promptDetails?.cached_tokens) ?? 0;
|
|
675
|
+
const boundedCached = Math.min(cached, input);
|
|
676
|
+
return {
|
|
677
|
+
status: "ok",
|
|
678
|
+
usage: {
|
|
679
|
+
inputTokens: input,
|
|
680
|
+
uncachedInputTokens: input - boundedCached,
|
|
681
|
+
cachedInputTokens: boundedCached,
|
|
682
|
+
cacheWriteInputTokens: 0,
|
|
683
|
+
outputTokens: output
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
function responseServiceTier(response) {
|
|
688
|
+
if (!isPlainObject(response))
|
|
689
|
+
return void 0;
|
|
690
|
+
if (typeof response.service_tier === "string")
|
|
691
|
+
return response.service_tier;
|
|
692
|
+
const usage = isPlainObject(response.usage) ? response.usage : void 0;
|
|
693
|
+
return typeof usage?.service_tier === "string" ? usage.service_tier : void 0;
|
|
694
|
+
}
|
|
695
|
+
var init_provider_usage = __esm({
|
|
696
|
+
"packages/core/dist/src/provider-usage.js"() {
|
|
697
|
+
"use strict";
|
|
698
|
+
}
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
// packages/core/dist/src/confidence.js
|
|
702
|
+
var init_confidence = __esm({
|
|
703
|
+
"packages/core/dist/src/confidence.js"() {
|
|
704
|
+
"use strict";
|
|
705
|
+
}
|
|
706
|
+
});
|
|
707
|
+
|
|
708
|
+
// packages/core/dist/src/price-catalogue.js
|
|
709
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
710
|
+
function assertIsoDate2(value, label) {
|
|
711
|
+
if (!ISO_DATE2.test(value) || Number.isNaN(Date.parse(value))) {
|
|
712
|
+
throw new Error(`${label} must be an ISO calendar date (YYYY-MM-DD); received ${JSON.stringify(value)}.`);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
function assertRate2(value, label) {
|
|
716
|
+
if (value === void 0)
|
|
717
|
+
return;
|
|
718
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
719
|
+
throw new Error(`${label} must be a non-negative finite number of USD per million tokens.`);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
function assertTiers2(tiers, label) {
|
|
723
|
+
if (tiers.length === 0)
|
|
724
|
+
throw new Error(`${label} must declare at least one price tier.`);
|
|
725
|
+
const unbounded = tiers.filter((tier) => tier.maxInputTokens === void 0);
|
|
726
|
+
if (unbounded.length !== 1) {
|
|
727
|
+
throw new Error(`${label} must declare exactly one tier without maxInputTokens; found ${unbounded.length}.`);
|
|
728
|
+
}
|
|
729
|
+
const bounds = tiers.map((tier) => tier.maxInputTokens).filter((bound) => bound !== void 0);
|
|
730
|
+
if (bounds.some((bound) => !Number.isInteger(bound) || bound <= 0)) {
|
|
731
|
+
throw new Error(`${label} tier bounds must be positive integers.`);
|
|
732
|
+
}
|
|
733
|
+
if (new Set(bounds).size !== bounds.length) {
|
|
734
|
+
throw new Error(`${label} declares duplicate tier bounds.`);
|
|
735
|
+
}
|
|
736
|
+
for (const tier of tiers) {
|
|
737
|
+
assertRate2(tier.inputPerMillion, `${label} inputPerMillion`);
|
|
738
|
+
assertRate2(tier.outputPerMillion, `${label} outputPerMillion`);
|
|
739
|
+
assertRate2(tier.cachedInputPerMillion, `${label} cachedInputPerMillion`);
|
|
740
|
+
assertRate2(tier.cacheWriteInputPerMillion, `${label} cacheWriteInputPerMillion`);
|
|
741
|
+
assertRate2(tier.cacheWrite1hInputPerMillion, `${label} cacheWrite1hInputPerMillion`);
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
function assertProvenance2(provenance, label) {
|
|
745
|
+
if (!provenance || typeof provenance.source !== "string" || provenance.source.trim() === "") {
|
|
746
|
+
throw new Error(`${label} needs a provenance.source so the rate can be traced to a published price.`);
|
|
747
|
+
}
|
|
748
|
+
assertIsoDate2(provenance.capturedOn, `${label} provenance.capturedOn`);
|
|
749
|
+
}
|
|
750
|
+
function rangeKey2(item) {
|
|
751
|
+
return `${item.provider} ${item.model} ${item.mode ?? "standard"}`;
|
|
752
|
+
}
|
|
753
|
+
function overlaps2(left, right) {
|
|
754
|
+
return left.effectiveFrom < (right.effectiveTo ?? OPEN_ENDED2) && right.effectiveFrom < (left.effectiveTo ?? OPEN_ENDED2);
|
|
755
|
+
}
|
|
756
|
+
function validatePriceCatalogue2(catalogue) {
|
|
757
|
+
const seen = /* @__PURE__ */ new Map();
|
|
758
|
+
for (const entry3 of catalogue.entries) {
|
|
759
|
+
const label = `Price entry ${entry3.provider}/${entry3.model} (${entry3.mode}) from ${entry3.effectiveFrom}`;
|
|
760
|
+
if (!entry3.provider.trim() || !entry3.model.trim()) {
|
|
761
|
+
throw new Error("Price entries need a non-empty provider and model.");
|
|
762
|
+
}
|
|
763
|
+
assertIsoDate2(entry3.effectiveFrom, `${label} effectiveFrom`);
|
|
764
|
+
if (entry3.effectiveTo !== void 0) {
|
|
765
|
+
assertIsoDate2(entry3.effectiveTo, `${label} effectiveTo`);
|
|
766
|
+
if (entry3.effectiveTo <= entry3.effectiveFrom) {
|
|
767
|
+
throw new Error(`${label} ends on or before it starts.`);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
assertTiers2(entry3.tiers, label);
|
|
771
|
+
assertProvenance2(entry3.provenance, label);
|
|
772
|
+
const key = rangeKey2(entry3);
|
|
773
|
+
const ranges = seen.get(key) ?? [];
|
|
774
|
+
const clash = ranges.find((range) => overlaps2(range, entry3));
|
|
775
|
+
if (clash) {
|
|
776
|
+
throw new Error(`${label} overlaps another rate for the same model and mode starting ${clash.effectiveFrom}; close the earlier rate with effectiveTo so each billing date resolves to exactly one price.`);
|
|
777
|
+
}
|
|
778
|
+
ranges.push(entry3);
|
|
779
|
+
seen.set(key, ranges);
|
|
780
|
+
}
|
|
781
|
+
const negotiatedSeen = /* @__PURE__ */ new Map();
|
|
782
|
+
for (const rate of catalogue.negotiated) {
|
|
783
|
+
const label = `Negotiated rate ${rate.provider}/${rate.model} from ${rate.effectiveFrom}`;
|
|
784
|
+
const hasDiscount = rate.discountPercent !== void 0;
|
|
785
|
+
const hasTiers = rate.tiers !== void 0;
|
|
786
|
+
if (hasDiscount === hasTiers) {
|
|
787
|
+
throw new Error(`${label} must set exactly one of discountPercent or tiers.`);
|
|
788
|
+
}
|
|
789
|
+
assertIsoDate2(rate.effectiveFrom, `${label} effectiveFrom`);
|
|
790
|
+
if (rate.effectiveTo !== void 0) {
|
|
791
|
+
assertIsoDate2(rate.effectiveTo, `${label} effectiveTo`);
|
|
792
|
+
if (rate.effectiveTo <= rate.effectiveFrom)
|
|
793
|
+
throw new Error(`${label} ends on or before it starts.`);
|
|
794
|
+
}
|
|
795
|
+
if (hasDiscount && (!Number.isFinite(rate.discountPercent) || rate.discountPercent < 0 || rate.discountPercent >= 100)) {
|
|
796
|
+
throw new Error(`${label} discountPercent must be at least 0 and below 100.`);
|
|
797
|
+
}
|
|
798
|
+
if (hasTiers)
|
|
799
|
+
assertTiers2(rate.tiers, label);
|
|
800
|
+
assertProvenance2(rate.provenance, label);
|
|
801
|
+
const key = rangeKey2(rate);
|
|
802
|
+
const ranges = negotiatedSeen.get(key) ?? [];
|
|
803
|
+
if (ranges.some((range) => overlaps2(range, rate))) {
|
|
804
|
+
throw new Error(`${label} overlaps another negotiated rate for the same model and mode.`);
|
|
805
|
+
}
|
|
806
|
+
ranges.push(rate);
|
|
807
|
+
negotiatedSeen.set(key, ranges);
|
|
808
|
+
}
|
|
809
|
+
return catalogue;
|
|
810
|
+
}
|
|
811
|
+
function applicable2(items, at) {
|
|
812
|
+
return items.find((item) => item.effectiveFrom <= at && (item.effectiveTo === void 0 || at < item.effectiveTo));
|
|
813
|
+
}
|
|
814
|
+
function tierFor2(tiers, inputTokens) {
|
|
815
|
+
const bounded = tiers.filter((tier) => tier.maxInputTokens !== void 0).sort((left, right) => left.maxInputTokens - right.maxInputTokens);
|
|
816
|
+
const match = bounded.find((tier) => inputTokens <= tier.maxInputTokens);
|
|
817
|
+
return match ?? tiers.find((tier) => tier.maxInputTokens === void 0);
|
|
818
|
+
}
|
|
819
|
+
function withoutBound2(tier) {
|
|
820
|
+
const { maxInputTokens: _bound, ...price } = tier;
|
|
821
|
+
return price;
|
|
822
|
+
}
|
|
823
|
+
function discounted2(tier, discountPercent) {
|
|
824
|
+
const factor = 1 - discountPercent / 100;
|
|
825
|
+
return {
|
|
826
|
+
inputPerMillion: tier.inputPerMillion * factor,
|
|
827
|
+
outputPerMillion: tier.outputPerMillion * factor,
|
|
828
|
+
...tier.cachedInputPerMillion === void 0 ? {} : { cachedInputPerMillion: tier.cachedInputPerMillion * factor },
|
|
829
|
+
...tier.cacheWriteInputPerMillion === void 0 ? {} : { cacheWriteInputPerMillion: tier.cacheWriteInputPerMillion * factor },
|
|
830
|
+
...tier.cacheWrite1hInputPerMillion === void 0 ? {} : { cacheWrite1hInputPerMillion: tier.cacheWrite1hInputPerMillion * factor }
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
function lookupPrice2(catalogue, query, inputTokens = 0) {
|
|
834
|
+
assertIsoDate2(query.at, "Price query `at`");
|
|
835
|
+
const mode = query.mode ?? "standard";
|
|
836
|
+
const candidates = catalogue.entries.filter((entry4) => entry4.provider === query.provider && entry4.model === query.model && entry4.mode === mode);
|
|
837
|
+
if (candidates.length === 0) {
|
|
838
|
+
return { status: "unpriced", reason: `No ${mode} rate is catalogued for ${query.provider}/${query.model}.` };
|
|
839
|
+
}
|
|
840
|
+
const entry3 = applicable2(candidates, query.at);
|
|
841
|
+
if (!entry3) {
|
|
842
|
+
const ranges = candidates.map((candidate) => `${candidate.effectiveFrom} to ${candidate.effectiveTo ?? "current"}`).join("; ");
|
|
843
|
+
return {
|
|
844
|
+
status: "unpriced",
|
|
845
|
+
reason: `No ${mode} rate for ${query.provider}/${query.model} covers ${query.at}; catalogued ranges are ${ranges}.`
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
const tier = tierFor2(entry3.tiers, inputTokens);
|
|
849
|
+
const override = applicable2(catalogue.negotiated.filter((rate) => rate.provider === query.provider && rate.model === query.model && (rate.mode ?? "standard") === mode), query.at);
|
|
850
|
+
if (!override)
|
|
851
|
+
return { status: "priced", price: withoutBound2(tier), entry: entry3 };
|
|
852
|
+
const price = override.tiers ? withoutBound2(tierFor2(override.tiers, inputTokens)) : discounted2(tier, override.discountPercent);
|
|
853
|
+
return { status: "priced", price, entry: entry3, negotiated: override };
|
|
854
|
+
}
|
|
855
|
+
function priceUsage2(catalogue, query, usage) {
|
|
856
|
+
for (const [name, value] of Object.entries(usage)) {
|
|
857
|
+
if (value === void 0)
|
|
858
|
+
continue;
|
|
859
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
860
|
+
throw new Error(`Token usage ${name} must be a non-negative finite number.`);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
const lookup = lookupPrice2(catalogue, query, usage.inputTokens);
|
|
864
|
+
if (lookup.status === "unpriced")
|
|
865
|
+
return lookup;
|
|
866
|
+
const cached = usage.cachedInputTokens ?? 0;
|
|
867
|
+
const cacheWrite = usage.cacheWriteInputTokens ?? 0;
|
|
868
|
+
const cacheWrite1h = usage.cacheWrite1hInputTokens ?? 0;
|
|
869
|
+
if (cacheWrite1h > cacheWrite) {
|
|
870
|
+
return {
|
|
871
|
+
status: "unpriced",
|
|
872
|
+
reason: `${query.provider}/${query.model} reports more 1h cache writes (${cacheWrite1h}) than total cache writes (${cacheWrite}).`
|
|
873
|
+
};
|
|
874
|
+
}
|
|
875
|
+
if (cached > 0 && lookup.price.cachedInputPerMillion === void 0 || cacheWrite - cacheWrite1h > 0 && lookup.price.cacheWriteInputPerMillion === void 0 || cacheWrite1h > 0 && lookup.price.cacheWrite1hInputPerMillion === void 0) {
|
|
876
|
+
return {
|
|
877
|
+
status: "unpriced",
|
|
878
|
+
reason: `${query.provider}/${query.model} billed cached input tokens on ${query.at}, but the catalogue has no cached-input rate for it.`
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
const costUsd = estimateTokenCost2(usage.inputTokens, usage.outputTokens, lookup.price, {
|
|
882
|
+
cachedInputTokens: usage.cachedInputTokens,
|
|
883
|
+
cacheWriteInputTokens: usage.cacheWriteInputTokens,
|
|
884
|
+
cacheWrite1hInputTokens: usage.cacheWrite1hInputTokens
|
|
885
|
+
});
|
|
886
|
+
return { status: "priced", costUsd, price: lookup.price, entry: lookup.entry, negotiated: lookup.negotiated };
|
|
887
|
+
}
|
|
888
|
+
function canonicalJson2(value) {
|
|
889
|
+
if (value === null || typeof value !== "object")
|
|
890
|
+
return JSON.stringify(value) ?? "null";
|
|
891
|
+
if (Array.isArray(value)) {
|
|
892
|
+
return `[${value.map((item) => canonicalJson2(item)).join(",")}]`;
|
|
893
|
+
}
|
|
894
|
+
const entries = Object.entries(value).filter(([, member]) => member !== void 0).sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0).map(([key, member]) => `${JSON.stringify(key)}:${canonicalJson2(member)}`);
|
|
895
|
+
return `{${entries.join(",")}}`;
|
|
896
|
+
}
|
|
897
|
+
function catalogueVersion2(catalogue) {
|
|
898
|
+
return createHash2("sha256").update(canonicalJson2(catalogue)).digest("hex").slice(0, 16);
|
|
899
|
+
}
|
|
900
|
+
function canonicalSegments2(value) {
|
|
901
|
+
return value.toLowerCase().replace(/[-_]latest$/, "").replace(/[-_]20\d{2}[-_]\d{2}[-_]\d{2}$/, "").replace(/[-_]20\d{6}$/, "").split(/[^a-z0-9]+/).filter((segment) => segment !== "");
|
|
902
|
+
}
|
|
903
|
+
function containsSegments2(id, key) {
|
|
904
|
+
if (key.length === 0 || key.length > id.length)
|
|
905
|
+
return false;
|
|
906
|
+
for (let offset = 0; offset + key.length <= id.length; offset += 1) {
|
|
907
|
+
if (key.every((segment, index) => id[offset + index] === segment))
|
|
908
|
+
return true;
|
|
909
|
+
}
|
|
910
|
+
return false;
|
|
911
|
+
}
|
|
912
|
+
function resolveModelId2(catalogue, provider, rawModelId) {
|
|
913
|
+
const known = [...new Set(catalogue.entries.filter((entry3) => entry3.provider === provider).map((entry3) => entry3.model))];
|
|
914
|
+
if (known.length === 0) {
|
|
915
|
+
return { status: "unresolved", reason: `No models are catalogued for provider ${provider}.`, candidates: [] };
|
|
916
|
+
}
|
|
917
|
+
if (known.includes(rawModelId))
|
|
918
|
+
return { status: "resolved", model: rawModelId, via: "exact" };
|
|
919
|
+
for (const alias of catalogue.aliases ?? []) {
|
|
920
|
+
if (alias.provider !== provider)
|
|
921
|
+
continue;
|
|
922
|
+
const matches = alias.id.endsWith("*") ? rawModelId.startsWith(alias.id.slice(0, -1)) : alias.id === rawModelId;
|
|
923
|
+
if (!matches)
|
|
924
|
+
continue;
|
|
925
|
+
if (!known.includes(alias.model)) {
|
|
926
|
+
return {
|
|
927
|
+
status: "unresolved",
|
|
928
|
+
reason: `Alias ${alias.id} points at ${alias.model}, which is not catalogued for ${provider}.`,
|
|
929
|
+
candidates: known
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
return { status: "resolved", model: alias.model, via: "alias" };
|
|
933
|
+
}
|
|
934
|
+
const target = canonicalSegments2(rawModelId);
|
|
935
|
+
const hits = known.map((model) => ({ model, key: canonicalSegments2(model) })).filter(({ key }) => containsSegments2(target, key)).sort((left, right) => right.key.length - left.key.length || right.key.join("").length - left.key.join("").length);
|
|
936
|
+
if (hits.length === 0) {
|
|
937
|
+
return {
|
|
938
|
+
status: "unresolved",
|
|
939
|
+
reason: `No catalogue key for ${provider} matches ${rawModelId}; add an alias.`,
|
|
940
|
+
candidates: known
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
const longest = hits[0].key.length;
|
|
944
|
+
const tied = hits.filter(({ key }) => key.length === longest);
|
|
945
|
+
if (tied.length > 1) {
|
|
946
|
+
return {
|
|
947
|
+
status: "unresolved",
|
|
948
|
+
reason: `${rawModelId} matches ${tied.length} catalogue keys equally well; add an explicit alias.`,
|
|
949
|
+
candidates: tied.map(({ model }) => model)
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
return { status: "resolved", model: hits[0].model, via: "normalised" };
|
|
953
|
+
}
|
|
954
|
+
var ISO_DATE2, OPEN_ENDED2;
|
|
955
|
+
var init_price_catalogue2 = __esm({
|
|
956
|
+
"packages/core/dist/src/price-catalogue.js"() {
|
|
957
|
+
"use strict";
|
|
958
|
+
init_cost2();
|
|
959
|
+
ISO_DATE2 = /^\d{4}-\d{2}-\d{2}$/;
|
|
960
|
+
OPEN_ENDED2 = "9999-12-31";
|
|
961
|
+
}
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
// packages/core/dist/src/model-prices.js
|
|
965
|
+
function entry(provider, model, provenance, bands, mode = "standard", effectiveFrom = OBSERVED_FROM) {
|
|
966
|
+
return {
|
|
967
|
+
provider,
|
|
968
|
+
model,
|
|
969
|
+
mode,
|
|
970
|
+
effectiveFrom,
|
|
971
|
+
provenance,
|
|
972
|
+
tiers: bands.map((band) => ({
|
|
973
|
+
...band.maxInputTokens === void 0 ? {} : { maxInputTokens: band.maxInputTokens },
|
|
974
|
+
inputPerMillion: band.input,
|
|
975
|
+
outputPerMillion: band.output,
|
|
976
|
+
...band.cachedInput === void 0 ? {} : { cachedInputPerMillion: band.cachedInput },
|
|
977
|
+
...band.cacheWriteInput === void 0 ? {} : { cacheWriteInputPerMillion: band.cacheWriteInput },
|
|
978
|
+
...band.cacheWrite1hInput === void 0 ? {} : { cacheWrite1hInputPerMillion: band.cacheWrite1hInput }
|
|
979
|
+
}))
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
function anthropicCacheableMinimumTokens(modelId) {
|
|
983
|
+
return /haiku/i.test(modelId) ? 2048 : 1024;
|
|
984
|
+
}
|
|
985
|
+
var ANTHROPIC, OPENAI, ANTHROPIC_BATCH, DEEPSEEK, OBSERVED_FROM, anthropic, openai, deepseek, aliases, publishedModelPrices;
|
|
986
|
+
var init_model_prices = __esm({
|
|
987
|
+
"packages/core/dist/src/model-prices.js"() {
|
|
988
|
+
"use strict";
|
|
989
|
+
init_price_catalogue2();
|
|
990
|
+
ANTHROPIC = {
|
|
991
|
+
source: "https://claude.com/pricing",
|
|
992
|
+
capturedOn: "2026-10-08",
|
|
993
|
+
note: "Published display names; cache read/write rates as listed. 5m cache writes bill 1.25x base input, 1h cache writes 2x base input, per the published prompt-caching pricing."
|
|
994
|
+
};
|
|
995
|
+
OPENAI = {
|
|
996
|
+
source: "https://developers.openai.com/api/docs/pricing",
|
|
997
|
+
capturedOn: "2026-10-08",
|
|
998
|
+
note: "Standard tier unless the entry says otherwise."
|
|
999
|
+
};
|
|
1000
|
+
ANTHROPIC_BATCH = {
|
|
1001
|
+
source: "https://claude.com/pricing",
|
|
1002
|
+
capturedOn: "2026-10-27",
|
|
1003
|
+
note: `Batch processing = 50% of standard, per the page's definitive "Save 50% with batch processing". Derived by halving the dated standard rates; cache-write premiums under batch are not published and are omitted (priced unpriced, never guessed).`
|
|
1004
|
+
};
|
|
1005
|
+
DEEPSEEK = {
|
|
1006
|
+
source: "https://api-docs.deepseek.com/quick_start/pricing",
|
|
1007
|
+
capturedOn: "2026-11-03",
|
|
1008
|
+
note: "Off-peak list rates (peak is 2x in the 01-10 UTC weekday windows); catalogued at off-peak so a saving is never over-valued. Per 1M tokens."
|
|
1009
|
+
};
|
|
1010
|
+
OBSERVED_FROM = "2026-10-08";
|
|
1011
|
+
anthropic = [
|
|
1012
|
+
entry("anthropic", "Fable 5.1", ANTHROPIC, [
|
|
1013
|
+
{ input: 10, output: 50, cachedInput: 0.25, cacheWriteInput: 12.5, cacheWrite1hInput: 20 }
|
|
1014
|
+
]),
|
|
1015
|
+
entry("anthropic", "Opus 5.5", ANTHROPIC, [
|
|
1016
|
+
{ input: 4, output: 20, cachedInput: 0.2, cacheWriteInput: 5, cacheWrite1hInput: 8 }
|
|
1017
|
+
]),
|
|
1018
|
+
entry("anthropic", "Sonnet 5.5", ANTHROPIC, [
|
|
1019
|
+
{ input: 2, output: 10, cachedInput: 0.1, cacheWriteInput: 2.5, cacheWrite1hInput: 4 }
|
|
1020
|
+
]),
|
|
1021
|
+
// Haiku 5.5 is tiered by prompt size: the higher band applies above 100K input tokens.
|
|
1022
|
+
entry("anthropic", "Haiku 5.5", ANTHROPIC, [
|
|
1023
|
+
{ maxInputTokens: 1e5, input: 0.1, output: 0.5, cachedInput: 0.01, cacheWriteInput: 0.125, cacheWrite1hInput: 0.2 },
|
|
1024
|
+
{ input: 0.5, output: 2.5, cachedInput: 0.05, cacheWriteInput: 0.625, cacheWrite1hInput: 1 }
|
|
1025
|
+
]),
|
|
1026
|
+
// Batch mode: 50% of standard, per the definitive "Save 50% with batch processing"
|
|
1027
|
+
// (ANTHROPIC_BATCH provenance). Each rate is exactly half its standard entry above.
|
|
1028
|
+
entry("anthropic", "Fable 5.1", ANTHROPIC_BATCH, [
|
|
1029
|
+
{ input: 5, output: 25, cachedInput: 0.125 }
|
|
1030
|
+
], "batch", "2026-10-27"),
|
|
1031
|
+
entry("anthropic", "Opus 5.5", ANTHROPIC_BATCH, [
|
|
1032
|
+
{ input: 2, output: 10, cachedInput: 0.1 }
|
|
1033
|
+
], "batch", "2026-10-27"),
|
|
1034
|
+
entry("anthropic", "Sonnet 5.5", ANTHROPIC_BATCH, [
|
|
1035
|
+
{ input: 1, output: 5, cachedInput: 0.05 }
|
|
1036
|
+
], "batch", "2026-10-27"),
|
|
1037
|
+
entry("anthropic", "Haiku 5.5", ANTHROPIC_BATCH, [
|
|
1038
|
+
{ maxInputTokens: 1e5, input: 0.05, output: 0.25, cachedInput: 5e-3 },
|
|
1039
|
+
{ input: 0.25, output: 1.25, cachedInput: 0.025 }
|
|
1040
|
+
], "batch", "2026-10-27")
|
|
1041
|
+
];
|
|
1042
|
+
openai = [
|
|
1043
|
+
entry("openai", "gpt-6-astra", OPENAI, [{ input: 10, output: 50, cachedInput: 1 }]),
|
|
1044
|
+
entry("openai", "gpt-6.1-sol", OPENAI, [{ input: 2, output: 10, cachedInput: 0.1 }]),
|
|
1045
|
+
entry("openai", "gpt-6-luna", OPENAI, [{ input: 0.1, output: 0.5, cachedInput: 0.01 }]),
|
|
1046
|
+
entry("openai", "gpt-5.5", OPENAI, [{ input: 5, output: 30, cachedInput: 0.5 }]),
|
|
1047
|
+
entry("openai", "gpt-5.4", OPENAI, [{ input: 2.5, output: 15, cachedInput: 0.25 }]),
|
|
1048
|
+
entry("openai", "gpt-5.2", OPENAI, [{ input: 1.75, output: 14, cachedInput: 0.175 }]),
|
|
1049
|
+
entry("openai", "gpt-5", OPENAI, [{ input: 1.25, output: 10, cachedInput: 0.125 }]),
|
|
1050
|
+
entry("openai", "gpt-4o", OPENAI, [{ input: 2.5, output: 10, cachedInput: 1.25 }]),
|
|
1051
|
+
entry("openai", "gpt-4o-mini", OPENAI, [{ input: 0.15, output: 0.6, cachedInput: 0.075 }]),
|
|
1052
|
+
entry("openai", "o1", OPENAI, [{ input: 15, output: 60, cachedInput: 7.5 }]),
|
|
1053
|
+
entry("openai", "o3-mini", OPENAI, [{ input: 1.1, output: 4.4, cachedInput: 0.55 }]),
|
|
1054
|
+
// Only the batch rate published explicitly is catalogued. The page describes batch
|
|
1055
|
+
// and flex as "typically" half of standard; that is not a rate, so it is not seeded.
|
|
1056
|
+
entry("openai", "gpt-6-astra", OPENAI, [{ input: 5, output: 25, cachedInput: 0.5 }], "batch"),
|
|
1057
|
+
// Flex processing is billed at Batch API rates = 50% of standard, on a
|
|
1058
|
+
// synchronous API, per OpenAI's flex-processing guide. Cached-input discount
|
|
1059
|
+
// stacks on top. Catalogued so the ledger can price flex-tier events rather
|
|
1060
|
+
// than leaving async savings unpriced.
|
|
1061
|
+
entry("openai", "gpt-5", OPENAI, [{ input: 0.625, output: 5, cachedInput: 0.0625 }], "flex")
|
|
1062
|
+
];
|
|
1063
|
+
deepseek = [
|
|
1064
|
+
entry("deepseek", "deepseek-flash", DEEPSEEK, [{ input: 0.15, output: 0.6, cachedInput: 3e-3 }], "standard", "2026-11-03"),
|
|
1065
|
+
entry("deepseek", "deepseek-v4-pro", DEEPSEEK, [{ input: 0.66, output: 1.98, cachedInput: 0.022 }], "standard", "2026-11-03")
|
|
1066
|
+
];
|
|
1067
|
+
aliases = [
|
|
1068
|
+
// Anthropic ids embed the published name, so normalisation covers dated
|
|
1069
|
+
// builds such as claude-sonnet-5-5-20260514. These cover the pointers.
|
|
1070
|
+
{ provider: "anthropic", id: "claude-sonnet-5-5-latest", model: "Sonnet 5.5" },
|
|
1071
|
+
{ provider: "anthropic", id: "claude-opus-5-5-latest", model: "Opus 5.5" },
|
|
1072
|
+
{ provider: "anthropic", id: "claude-haiku-5-5-latest", model: "Haiku 5.5" },
|
|
1073
|
+
{ provider: "anthropic", id: "claude-fable-5-1-latest", model: "Fable 5.1" },
|
|
1074
|
+
// OpenAI ids match their published names directly; these are the pointers
|
|
1075
|
+
// and the dated snapshots that do not normalise onto a catalogued key.
|
|
1076
|
+
{ provider: "openai", id: "chatgpt-4o-latest", model: "gpt-4o" },
|
|
1077
|
+
{ provider: "openai", id: "gpt-4o-2024-*", model: "gpt-4o" },
|
|
1078
|
+
{ provider: "openai", id: "gpt-4o-mini-2024-*", model: "gpt-4o-mini" },
|
|
1079
|
+
{ provider: "openai", id: "o3-mini-2025-*", model: "o3-mini" },
|
|
1080
|
+
// DeepSeek: the current ids match their keys (normalisation covers them); only
|
|
1081
|
+
// the legacy pointer needs an explicit map. deepseek-v4-flash bills at Flash,
|
|
1082
|
+
// per DeepSeek's pricing page.
|
|
1083
|
+
{ provider: "deepseek", id: "deepseek-v4-flash", model: "deepseek-flash" }
|
|
1084
|
+
];
|
|
1085
|
+
publishedModelPrices = validatePriceCatalogue2({
|
|
1086
|
+
entries: [...anthropic, ...openai, ...deepseek],
|
|
1087
|
+
negotiated: [],
|
|
1088
|
+
aliases
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
});
|
|
1092
|
+
|
|
1093
|
+
// packages/core/dist/src/tool-schema.js
|
|
1094
|
+
function toolNameOf(tool, index) {
|
|
1095
|
+
if (typeof tool === "object" && tool !== null) {
|
|
1096
|
+
const row = tool;
|
|
1097
|
+
const direct = typeof row.name === "string" ? row.name : void 0;
|
|
1098
|
+
const nested = typeof row.function === "object" && row.function !== null ? row.function.name : void 0;
|
|
1099
|
+
return direct ?? (typeof nested === "string" ? nested : `tool-${index}`);
|
|
1100
|
+
}
|
|
1101
|
+
return `tool-${index}`;
|
|
1102
|
+
}
|
|
1103
|
+
function truncateToTokens(text, limit, countTokens) {
|
|
1104
|
+
if (countTokens(text) <= limit)
|
|
1105
|
+
return text;
|
|
1106
|
+
const sentences = text.split(/(?<=[.!?])\s+/);
|
|
1107
|
+
let kept = "";
|
|
1108
|
+
for (const sentence of sentences) {
|
|
1109
|
+
const candidate = kept === "" ? sentence : `${kept} ${sentence}`;
|
|
1110
|
+
if (countTokens(candidate) > limit)
|
|
1111
|
+
break;
|
|
1112
|
+
kept = candidate;
|
|
1113
|
+
}
|
|
1114
|
+
if (kept !== "")
|
|
1115
|
+
return kept;
|
|
1116
|
+
const words = text.split(/\s+/);
|
|
1117
|
+
let fallback = "";
|
|
1118
|
+
for (const word of words) {
|
|
1119
|
+
const candidate = fallback === "" ? word : `${fallback} ${word}`;
|
|
1120
|
+
if (countTokens(candidate) > limit)
|
|
1121
|
+
break;
|
|
1122
|
+
fallback = candidate;
|
|
1123
|
+
}
|
|
1124
|
+
return fallback;
|
|
1125
|
+
}
|
|
1126
|
+
function minifyToolSchemas(tools, options = {}) {
|
|
1127
|
+
if (!Array.isArray(tools))
|
|
1128
|
+
throw new Error("A tool catalogue must be an array.");
|
|
1129
|
+
const countTokens = options.countTokens ?? approximateTokenCount;
|
|
1130
|
+
const descriptions = options.descriptions ?? "keep";
|
|
1131
|
+
const maximumDescriptionTokens = options.maximumDescriptionTokens ?? 24;
|
|
1132
|
+
if (!Number.isInteger(maximumDescriptionTokens) || maximumDescriptionTokens < 1) {
|
|
1133
|
+
throw new Error("maximumDescriptionTokens must be a positive integer.");
|
|
1134
|
+
}
|
|
1135
|
+
const changes = [];
|
|
1136
|
+
const tokensBefore = countTokens(JSON.stringify(tools));
|
|
1137
|
+
const keep = options.keepTools === void 0 ? void 0 : new Set(options.keepTools);
|
|
1138
|
+
const retained = [];
|
|
1139
|
+
tools.forEach((tool, index) => {
|
|
1140
|
+
const name = toolNameOf(tool, index);
|
|
1141
|
+
if (keep !== void 0 && !keep.has(name)) {
|
|
1142
|
+
changes.push({
|
|
1143
|
+
kind: "dropped-tool",
|
|
1144
|
+
lossless: false,
|
|
1145
|
+
at: name,
|
|
1146
|
+
tokensSaved: countTokens(JSON.stringify(tool)),
|
|
1147
|
+
detail: "Tool removed from the catalogue. The model cannot call what it cannot see, so this is safe only where the tool is genuinely unreachable for the workload."
|
|
1148
|
+
});
|
|
1149
|
+
return;
|
|
1150
|
+
}
|
|
1151
|
+
retained.push(prune(tool, name));
|
|
1152
|
+
});
|
|
1153
|
+
function prune(node, path) {
|
|
1154
|
+
if (Array.isArray(node))
|
|
1155
|
+
return node.map((item, index) => prune(item, `${path}[${index}]`));
|
|
1156
|
+
if (typeof node !== "object" || node === null)
|
|
1157
|
+
return node;
|
|
1158
|
+
const row = node;
|
|
1159
|
+
const output = {};
|
|
1160
|
+
for (const [key, value] of Object.entries(row)) {
|
|
1161
|
+
if (PLUMBING_KEYS.has(key)) {
|
|
1162
|
+
changes.push({
|
|
1163
|
+
kind: "plumbing",
|
|
1164
|
+
lossless: true,
|
|
1165
|
+
at: `${path}.${key}`,
|
|
1166
|
+
tokensSaved: countTokens(JSON.stringify({ [key]: value })),
|
|
1167
|
+
detail: `Removed ${key}, which the model never reads.`
|
|
1168
|
+
});
|
|
1169
|
+
continue;
|
|
1170
|
+
}
|
|
1171
|
+
if (key === "title" && typeof value === "string") {
|
|
1172
|
+
const leaf = path.split(".").pop() ?? "";
|
|
1173
|
+
if (value.toLowerCase().replace(/[^a-z0-9]/g, "") === leaf.toLowerCase().replace(/[^a-z0-9]/g, "")) {
|
|
1174
|
+
changes.push({
|
|
1175
|
+
kind: "redundant-title",
|
|
1176
|
+
lossless: true,
|
|
1177
|
+
at: `${path}.title`,
|
|
1178
|
+
tokensSaved: countTokens(JSON.stringify({ title: value })),
|
|
1179
|
+
detail: `Removed title "${value}", which restates the property name.`
|
|
1180
|
+
});
|
|
1181
|
+
continue;
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
if (key === "examples" && options.dropExamples) {
|
|
1185
|
+
changes.push({
|
|
1186
|
+
kind: "examples",
|
|
1187
|
+
lossless: false,
|
|
1188
|
+
at: `${path}.examples`,
|
|
1189
|
+
tokensSaved: countTokens(JSON.stringify({ examples: value })),
|
|
1190
|
+
detail: "Removed examples. The model does read these when choosing arguments."
|
|
1191
|
+
});
|
|
1192
|
+
continue;
|
|
1193
|
+
}
|
|
1194
|
+
if (key === "description" && typeof value === "string" && descriptions !== "keep") {
|
|
1195
|
+
if (descriptions === "drop") {
|
|
1196
|
+
changes.push({
|
|
1197
|
+
kind: "description",
|
|
1198
|
+
lossless: false,
|
|
1199
|
+
at: `${path}.description`,
|
|
1200
|
+
tokensSaved: countTokens(JSON.stringify({ description: value })),
|
|
1201
|
+
detail: "Removed description. Descriptions are how the model chooses between tools."
|
|
1202
|
+
});
|
|
1203
|
+
continue;
|
|
1204
|
+
}
|
|
1205
|
+
const trimmed = truncateToTokens(value, maximumDescriptionTokens, countTokens);
|
|
1206
|
+
if (trimmed !== value) {
|
|
1207
|
+
changes.push({
|
|
1208
|
+
kind: "description",
|
|
1209
|
+
lossless: false,
|
|
1210
|
+
at: `${path}.description`,
|
|
1211
|
+
tokensSaved: countTokens(value) - countTokens(trimmed),
|
|
1212
|
+
detail: `Truncated description from ${countTokens(value)} to ${countTokens(trimmed)} tokens.`
|
|
1213
|
+
});
|
|
1214
|
+
}
|
|
1215
|
+
output[key] = trimmed;
|
|
1216
|
+
continue;
|
|
1217
|
+
}
|
|
1218
|
+
output[key] = prune(value, `${path}.${key}`);
|
|
1219
|
+
}
|
|
1220
|
+
return output;
|
|
1221
|
+
}
|
|
1222
|
+
const compact = JSON.stringify(retained);
|
|
1223
|
+
const tokensAfter = countTokens(compact);
|
|
1224
|
+
return {
|
|
1225
|
+
tools: retained,
|
|
1226
|
+
tokensBefore,
|
|
1227
|
+
tokensAfter,
|
|
1228
|
+
losslessTokensSaved: changes.filter((change) => change.lossless).reduce((sum, change) => sum + change.tokensSaved, 0),
|
|
1229
|
+
lossyTokensSaved: changes.filter((change) => !change.lossless).reduce((sum, change) => sum + change.tokensSaved, 0),
|
|
1230
|
+
changes
|
|
1231
|
+
};
|
|
1232
|
+
}
|
|
1233
|
+
var PLUMBING_KEYS;
|
|
1234
|
+
var init_tool_schema = __esm({
|
|
1235
|
+
"packages/core/dist/src/tool-schema.js"() {
|
|
1236
|
+
"use strict";
|
|
1237
|
+
init_token_reduction();
|
|
1238
|
+
PLUMBING_KEYS = /* @__PURE__ */ new Set(["$schema", "$id", "$comment", "$defs"]);
|
|
1239
|
+
}
|
|
1240
|
+
});
|
|
1241
|
+
|
|
1242
|
+
// packages/sdk-js/src/reduce-request.ts
|
|
1243
|
+
function isPlainObject2(value) {
|
|
1244
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1245
|
+
}
|
|
1246
|
+
function anthropicEffortSupport(model) {
|
|
1247
|
+
const m = model.toLowerCase();
|
|
1248
|
+
if (!m.includes("claude")) return /* @__PURE__ */ new Set();
|
|
1249
|
+
if (m.includes("opus-5") || m.includes("opus-4-8") || m.includes("opus-4-7") || m.includes("sonnet-5") || m.includes("fable-5") || m.includes("mythos-5")) {
|
|
1250
|
+
return /* @__PURE__ */ new Set(["low", "medium", "high", "xhigh", "max"]);
|
|
1251
|
+
}
|
|
1252
|
+
if (m.includes("opus-4-6") || m.includes("sonnet-4-6")) {
|
|
1253
|
+
return /* @__PURE__ */ new Set(["low", "medium", "high", "max"]);
|
|
1254
|
+
}
|
|
1255
|
+
if (m.includes("opus-4-5")) return /* @__PURE__ */ new Set(["low", "medium", "high"]);
|
|
1256
|
+
return /* @__PURE__ */ new Set();
|
|
1257
|
+
}
|
|
1258
|
+
function repairPrefix(instructions) {
|
|
1259
|
+
const lines = instructions.split("\n");
|
|
1260
|
+
const moved = [];
|
|
1261
|
+
const kept = [];
|
|
1262
|
+
const window = Math.min(lines.length, 8);
|
|
1263
|
+
for (const [index, line] of lines.entries()) {
|
|
1264
|
+
const breaks = index < window && PREFIX_BREAKERS.some((pattern) => {
|
|
1265
|
+
pattern.lastIndex = 0;
|
|
1266
|
+
return pattern.test(line);
|
|
1267
|
+
});
|
|
1268
|
+
if (breaks) moved.push(line);
|
|
1269
|
+
else kept.push(line);
|
|
1270
|
+
}
|
|
1271
|
+
if (moved.length === 0) return { text: instructions, moved };
|
|
1272
|
+
return { text: [...kept, ...moved].join("\n"), moved };
|
|
1273
|
+
}
|
|
1274
|
+
function digestStaleBody(text) {
|
|
1275
|
+
const segments = text.split(/(?<=[.!?])\s+|\n+/).filter((segment) => segment.length > 0);
|
|
1276
|
+
if (segments.length <= 1) return text;
|
|
1277
|
+
const kept = [];
|
|
1278
|
+
let droppedChars = 0;
|
|
1279
|
+
for (const [index, segment] of segments.entries()) {
|
|
1280
|
+
const keep = index === 0 || STALE_BODY_MUST_KEEP.test(segment) || STALE_BODY_FILE_PATH.test(segment) || MESH_MARKER.test(segment);
|
|
1281
|
+
if (keep) kept.push(segment);
|
|
1282
|
+
else droppedChars += segment.length;
|
|
1283
|
+
}
|
|
1284
|
+
if (droppedChars === 0) return text;
|
|
1285
|
+
const digest2 = `${kept.join(" ")} [mesh: ${droppedChars} chars of stale detail compacted]`;
|
|
1286
|
+
return digest2.length < text.length ? digest2 : text;
|
|
1287
|
+
}
|
|
1288
|
+
function capToolResultBody(text, maximumTokens, countTokens) {
|
|
1289
|
+
if (!Number.isInteger(maximumTokens) || maximumTokens < 64) {
|
|
1290
|
+
throw new Error("freshToolResultCap maximumTokens must be an integer of at least 64.");
|
|
1291
|
+
}
|
|
1292
|
+
if (countTokens(text) <= maximumTokens) return text;
|
|
1293
|
+
const segments = text.split(/(?<=[.!?])\s+|\n+/).filter((segment) => segment.length > 0);
|
|
1294
|
+
if (segments.length <= 1) return text;
|
|
1295
|
+
const kept = [];
|
|
1296
|
+
let droppedChars = 0;
|
|
1297
|
+
for (const [index, segment] of segments.entries()) {
|
|
1298
|
+
const keep = index === 0 || STALE_BODY_MUST_KEEP.test(segment) || STALE_BODY_FILE_PATH.test(segment) || MESH_MARKER.test(segment);
|
|
1299
|
+
if (keep) kept.push(segment);
|
|
1300
|
+
else droppedChars += segment.length;
|
|
1301
|
+
}
|
|
1302
|
+
if (droppedChars === 0) return text;
|
|
1303
|
+
const capped = `${kept.join(" ")} [mesh: ${droppedChars} chars of fresh tool-result detail capped]`;
|
|
1304
|
+
return capped.length < text.length ? capped : text;
|
|
1305
|
+
}
|
|
1306
|
+
function transformBodyValue(value, transform, state) {
|
|
1307
|
+
if (typeof value === "string") {
|
|
1308
|
+
const transformed = transform(value);
|
|
1309
|
+
if (transformed !== value) state.bodies += 1;
|
|
1310
|
+
return transformed;
|
|
1311
|
+
}
|
|
1312
|
+
if (Array.isArray(value)) {
|
|
1313
|
+
return value.map((part) => {
|
|
1314
|
+
if (!isPlainObject2(part)) return part;
|
|
1315
|
+
if (typeof part.text === "string") {
|
|
1316
|
+
const transformed = transform(part.text);
|
|
1317
|
+
if (transformed !== part.text) state.bodies += 1;
|
|
1318
|
+
return transformed === part.text ? part : { ...part, text: transformed };
|
|
1319
|
+
}
|
|
1320
|
+
if ("content" in part) {
|
|
1321
|
+
const transformed = transformBodyValue(part.content, transform, state);
|
|
1322
|
+
return transformed === part.content ? part : { ...part, content: transformed };
|
|
1323
|
+
}
|
|
1324
|
+
return part;
|
|
1325
|
+
});
|
|
1326
|
+
}
|
|
1327
|
+
return value;
|
|
1328
|
+
}
|
|
1329
|
+
function digestBodyValue(value, state) {
|
|
1330
|
+
return transformBodyValue(value, digestStaleBody, state);
|
|
1331
|
+
}
|
|
1332
|
+
function isToolResultCarrier(message) {
|
|
1333
|
+
return isPlainObject2(message) && message.role === "user" && Array.isArray(message.content) && message.content.length > 0 && message.content.every((block) => isPlainObject2(block) && block.type === "tool_result");
|
|
1334
|
+
}
|
|
1335
|
+
function transformUserToolResultBlocks(message, transform, countTokens, state, addSavedTokens) {
|
|
1336
|
+
if (!Array.isArray(message.content)) return message;
|
|
1337
|
+
const sizeOf = (value) => countTokens(typeof value === "string" ? value : JSON.stringify(value) ?? "");
|
|
1338
|
+
let changed = false;
|
|
1339
|
+
const blocks = message.content.map((block) => {
|
|
1340
|
+
if (!isPlainObject2(block) || block.type !== "tool_result" || !("content" in block)) return block;
|
|
1341
|
+
const before = sizeOf(block.content);
|
|
1342
|
+
const bodiesBefore = state.bodies;
|
|
1343
|
+
const transformed = transformBodyValue(block.content, transform, state);
|
|
1344
|
+
if (state.bodies === bodiesBefore) return block;
|
|
1345
|
+
changed = true;
|
|
1346
|
+
addSavedTokens(Math.max(0, before - sizeOf(transformed)));
|
|
1347
|
+
return { ...block, content: transformed };
|
|
1348
|
+
});
|
|
1349
|
+
return changed ? { ...message, content: blocks } : message;
|
|
1350
|
+
}
|
|
1351
|
+
function capFreshToolResults(input, maximumTokens, countTokens, changes) {
|
|
1352
|
+
const cap = (text) => capToolResultBody(text, maximumTokens, countTokens);
|
|
1353
|
+
const state = { bodies: 0 };
|
|
1354
|
+
let savedTokens = 0;
|
|
1355
|
+
const next = input.map((message) => {
|
|
1356
|
+
if (!isPlainObject2(message)) return message;
|
|
1357
|
+
const role = typeof message.role === "string" ? message.role : void 0;
|
|
1358
|
+
if (role === "system" || role === "developer") return message;
|
|
1359
|
+
if (role === "user") {
|
|
1360
|
+
return transformUserToolResultBlocks(message, cap, countTokens, state, (saved) => {
|
|
1361
|
+
savedTokens += saved;
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
if (message.type === "function_call_output" && typeof message.output === "string") {
|
|
1365
|
+
const before = countTokens(message.output);
|
|
1366
|
+
const capped = transformBodyValue(message.output, cap, state);
|
|
1367
|
+
if (capped === message.output) return message;
|
|
1368
|
+
savedTokens += Math.max(0, before - countTokens(capped));
|
|
1369
|
+
return { ...message, output: capped };
|
|
1370
|
+
}
|
|
1371
|
+
if ((role === "tool" || role === "assistant") && "content" in message) {
|
|
1372
|
+
const before = countTokens(
|
|
1373
|
+
typeof message.content === "string" ? message.content : JSON.stringify(message.content) ?? ""
|
|
1374
|
+
);
|
|
1375
|
+
const capped = transformBodyValue(message.content, cap, state);
|
|
1376
|
+
if (capped === message.content) return message;
|
|
1377
|
+
savedTokens += Math.max(0, before - countTokens(
|
|
1378
|
+
typeof capped === "string" ? capped : JSON.stringify(capped) ?? ""
|
|
1379
|
+
));
|
|
1380
|
+
return { ...message, content: capped };
|
|
1381
|
+
}
|
|
1382
|
+
return message;
|
|
1383
|
+
});
|
|
1384
|
+
if (state.bodies === 0) return input;
|
|
1385
|
+
changes.push({
|
|
1386
|
+
lever: "fresh-tool-result-cap",
|
|
1387
|
+
lossless: false,
|
|
1388
|
+
estimatedTokensSaved: savedTokens,
|
|
1389
|
+
detail: `Capped ${state.bodies} oversized tool-result/assistant body(ies) above ${maximumTokens} tokens at injection, before first send (headlines, errors, file paths and MESH markers kept verbatim). These tokens would have been billed once at the FULL input rate; history compaction only reaches their cached rebills.`
|
|
1390
|
+
});
|
|
1391
|
+
return next;
|
|
1392
|
+
}
|
|
1393
|
+
function compactStaleHistory(input, options, countTokens, changes) {
|
|
1394
|
+
const minimumTurns = options.minimumTurns ?? 3;
|
|
1395
|
+
const keepRecentTurns = options.keepRecentTurns ?? 2;
|
|
1396
|
+
const epochTurns = options.epochTurns ?? 2;
|
|
1397
|
+
if (minimumTurns < 1 || keepRecentTurns < 1 || epochTurns < 1) {
|
|
1398
|
+
throw new Error("History compaction turn counts must be positive integers.");
|
|
1399
|
+
}
|
|
1400
|
+
const isRealUserTurn = (message) => isPlainObject2(message) && message.role === "user" && !isToolResultCarrier(message);
|
|
1401
|
+
const totalTurns = input.filter(isRealUserTurn).length;
|
|
1402
|
+
if (totalTurns < minimumTurns) return input;
|
|
1403
|
+
const compactedTurns = Math.floor((totalTurns - keepRecentTurns) / epochTurns) * epochTurns;
|
|
1404
|
+
if (compactedTurns <= 0) return input;
|
|
1405
|
+
const state = { bodies: 0 };
|
|
1406
|
+
let savedTokens = 0;
|
|
1407
|
+
let turn = -1;
|
|
1408
|
+
const next = input.map((message) => {
|
|
1409
|
+
if (isRealUserTurn(message)) turn += 1;
|
|
1410
|
+
if (turn < 0 || turn >= compactedTurns) return message;
|
|
1411
|
+
if (!isPlainObject2(message)) return message;
|
|
1412
|
+
const role = typeof message.role === "string" ? message.role : void 0;
|
|
1413
|
+
if (role === "system" || role === "developer") return message;
|
|
1414
|
+
if (role === "user") {
|
|
1415
|
+
return transformUserToolResultBlocks(message, digestStaleBody, countTokens, state, (saved) => {
|
|
1416
|
+
savedTokens += saved;
|
|
1417
|
+
});
|
|
1418
|
+
}
|
|
1419
|
+
if (message.type === "function_call_output" && typeof message.output === "string") {
|
|
1420
|
+
const before = countTokens(message.output);
|
|
1421
|
+
const digested = digestBodyValue(message.output, state);
|
|
1422
|
+
if (digested === message.output) return message;
|
|
1423
|
+
savedTokens += Math.max(0, before - countTokens(digested));
|
|
1424
|
+
return { ...message, output: digested };
|
|
1425
|
+
}
|
|
1426
|
+
if ((role === "tool" || role === "assistant") && "content" in message) {
|
|
1427
|
+
const before = countTokens(
|
|
1428
|
+
typeof message.content === "string" ? message.content : JSON.stringify(message.content) ?? ""
|
|
1429
|
+
);
|
|
1430
|
+
const digested = digestBodyValue(message.content, state);
|
|
1431
|
+
if (digested === message.content) return message;
|
|
1432
|
+
savedTokens += Math.max(0, before - countTokens(
|
|
1433
|
+
typeof digested === "string" ? digested : JSON.stringify(digested) ?? ""
|
|
1434
|
+
));
|
|
1435
|
+
return { ...message, content: digested };
|
|
1436
|
+
}
|
|
1437
|
+
return message;
|
|
1438
|
+
});
|
|
1439
|
+
if (state.bodies === 0) return input;
|
|
1440
|
+
changes.push({
|
|
1441
|
+
lever: "stale-history-compaction",
|
|
1442
|
+
lossless: false,
|
|
1443
|
+
estimatedTokensSaved: savedTokens,
|
|
1444
|
+
detail: `Compacted ${state.bodies} stale tool/assistant body(ies) from the first ${compactedTurns} of ${totalTurns} turns into deterministic digests (errors, file paths and headlines kept verbatim). The boundary is frozen to ${epochTurns}-turn epochs so the compacted prefix stays byte-identical across consecutive requests.`
|
|
1445
|
+
});
|
|
1446
|
+
return next;
|
|
1447
|
+
}
|
|
1448
|
+
function leadingText(message) {
|
|
1449
|
+
if (typeof message.content === "string") return message.content;
|
|
1450
|
+
if (Array.isArray(message.content) && message.content.length > 0) {
|
|
1451
|
+
const first = message.content[0];
|
|
1452
|
+
if (isPlainObject2(first) && first.type === "text" && typeof first.text === "string") return first.text;
|
|
1453
|
+
}
|
|
1454
|
+
return void 0;
|
|
1455
|
+
}
|
|
1456
|
+
function withLeadingText(message, text) {
|
|
1457
|
+
if (typeof message.content === "string") return { ...message, content: text };
|
|
1458
|
+
const content = message.content;
|
|
1459
|
+
return { ...message, content: [{ ...content[0], text }, ...content.slice(1)] };
|
|
1460
|
+
}
|
|
1461
|
+
function dedupeMemoryAcrossTurns(input, options, countTokens, changes) {
|
|
1462
|
+
const minimumBlockTokens = options.minimumBlockTokens ?? 64;
|
|
1463
|
+
if (!Number.isInteger(minimumBlockTokens) || minimumBlockTokens < 1) {
|
|
1464
|
+
throw new Error("crossTurnMemoryDedup minimumBlockTokens must be a positive integer.");
|
|
1465
|
+
}
|
|
1466
|
+
const indices = [];
|
|
1467
|
+
for (const [index, message] of input.entries()) {
|
|
1468
|
+
if (!isPlainObject2(message) || message.role !== "user") continue;
|
|
1469
|
+
if (leadingText(message) === void 0) continue;
|
|
1470
|
+
indices.push(index);
|
|
1471
|
+
}
|
|
1472
|
+
if (indices.length < 2) return input;
|
|
1473
|
+
const texts = indices.map((index) => leadingText(input[index]));
|
|
1474
|
+
const before = texts.reduce((sum, text) => sum + countTokens(text), 0);
|
|
1475
|
+
const result = dedupeRepeatedLeadingBlock(texts, minimumBlockTokens, countTokens);
|
|
1476
|
+
if (result.notes.length === 0) return input;
|
|
1477
|
+
const after = result.texts.reduce((sum, text) => sum + countTokens(text), 0);
|
|
1478
|
+
const next = [...input];
|
|
1479
|
+
indices.forEach((index, position) => {
|
|
1480
|
+
if (result.texts[position] === texts[position]) return;
|
|
1481
|
+
next[index] = withLeadingText(next[index], result.texts[position]);
|
|
1482
|
+
});
|
|
1483
|
+
changes.push({
|
|
1484
|
+
lever: "cross-turn-memory-dedup",
|
|
1485
|
+
lossless: false,
|
|
1486
|
+
estimatedTokensSaved: Math.max(0, before - after),
|
|
1487
|
+
detail: `Replaced ${result.notes.length} repeated leading block(s) across user turns with reference markers (the first turn's block, and every turn's own differing remainder, kept verbatim). This is a behaviour change, not a cache-rate change, so it needs a replay-gate pass on the customer's own traffic before the saving may be billed.`
|
|
1488
|
+
});
|
|
1489
|
+
return next;
|
|
1490
|
+
}
|
|
1491
|
+
function reduceText(text, config, countTokens, changes, label) {
|
|
1492
|
+
let current = text;
|
|
1493
|
+
if (config.duplicateContext !== false) {
|
|
1494
|
+
const before = countTokens(current);
|
|
1495
|
+
const reduced = dedupeRepeatedBlocks(24, countTokens).reduce(current);
|
|
1496
|
+
const saved = before - countTokens(reduced.text);
|
|
1497
|
+
if (saved > 0) {
|
|
1498
|
+
current = reduced.text;
|
|
1499
|
+
changes.push({
|
|
1500
|
+
lever: "duplicate-context-removal",
|
|
1501
|
+
lossless: true,
|
|
1502
|
+
estimatedTokensSaved: saved,
|
|
1503
|
+
detail: `Removed ${reduced.notes.length} duplicate block(s) from ${label}.`
|
|
1504
|
+
});
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
return current;
|
|
1508
|
+
}
|
|
1509
|
+
function reduceMessageContent(content, config, countTokens, changes) {
|
|
1510
|
+
if (typeof content === "string") return reduceText(content, config, countTokens, changes, "a message");
|
|
1511
|
+
if (Array.isArray(content)) {
|
|
1512
|
+
return content.map((part) => {
|
|
1513
|
+
if (!isPlainObject2(part) || typeof part.text !== "string") return part;
|
|
1514
|
+
return { ...part, text: reduceText(part.text, config, countTokens, changes, "a message part") };
|
|
1515
|
+
});
|
|
1516
|
+
}
|
|
1517
|
+
return content;
|
|
1518
|
+
}
|
|
1519
|
+
function stampAnthropicCacheTtl(next, ttl, systemVolatileTail, countTokens) {
|
|
1520
|
+
const model = typeof next.model === "string" ? next.model.toLowerCase() : "";
|
|
1521
|
+
if (!model.includes("claude")) return { stamped: false };
|
|
1522
|
+
if (!Array.isArray(next.messages) || next.messages.length === 0) return { stamped: false };
|
|
1523
|
+
if (next.cache_control !== void 0) return { stamped: false };
|
|
1524
|
+
const carriesCacheControl = (value) => (JSON.stringify(value ?? null) ?? "").includes('"cache_control"');
|
|
1525
|
+
if (carriesCacheControl(next.messages) || carriesCacheControl(next.system) || carriesCacheControl(next.tools)) {
|
|
1526
|
+
return { stamped: false };
|
|
1527
|
+
}
|
|
1528
|
+
const APPROX_OVERCOUNT_MARGIN = 1.3;
|
|
1529
|
+
const minimumTokens = Math.ceil(anthropicCacheableMinimumTokens(model) * APPROX_OVERCOUNT_MARGIN);
|
|
1530
|
+
const measureTokens = (value) => {
|
|
1531
|
+
try {
|
|
1532
|
+
return typeof value === "string" ? countTokens(value) : countTokens(JSON.stringify(value) ?? "");
|
|
1533
|
+
} catch {
|
|
1534
|
+
return 0;
|
|
1535
|
+
}
|
|
1536
|
+
};
|
|
1537
|
+
const toolsTokens = Array.isArray(next.tools) && next.tools.length > 0 ? measureTokens(next.tools) : 0;
|
|
1538
|
+
const fullSystemTokens = measureTokens(next.system);
|
|
1539
|
+
const fullMessagesTokens = measureTokens(next.messages);
|
|
1540
|
+
let belowMinimum;
|
|
1541
|
+
const recordBelowMinimum = (measuredTokens) => {
|
|
1542
|
+
if (belowMinimum === void 0) belowMinimum = { measuredTokens, minimumTokens };
|
|
1543
|
+
};
|
|
1544
|
+
const cacheControl = { type: "ephemeral", ttl };
|
|
1545
|
+
let stamped = false;
|
|
1546
|
+
let volatileTailPresent = false;
|
|
1547
|
+
if (typeof next.system === "string" && next.system.trim() !== "") {
|
|
1548
|
+
const tailText = systemVolatileTail.join("\n");
|
|
1549
|
+
if (systemVolatileTail.length > 0 && tailText.trim() !== "" && (next.system === tailText || next.system.endsWith(`
|
|
1550
|
+
${tailText}`))) {
|
|
1551
|
+
volatileTailPresent = true;
|
|
1552
|
+
const stable = next.system === tailText ? "" : next.system.slice(0, -tailText.length);
|
|
1553
|
+
if (stable.trim() !== "") {
|
|
1554
|
+
const stableTokens = toolsTokens + measureTokens(stable);
|
|
1555
|
+
if (stableTokens < minimumTokens) {
|
|
1556
|
+
recordBelowMinimum(stableTokens);
|
|
1557
|
+
} else {
|
|
1558
|
+
next.system = [
|
|
1559
|
+
{ type: "text", text: stable, cache_control: cacheControl },
|
|
1560
|
+
{ type: "text", text: tailText }
|
|
1561
|
+
];
|
|
1562
|
+
stamped = true;
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
} else {
|
|
1566
|
+
const stableTokens = toolsTokens + fullSystemTokens;
|
|
1567
|
+
if (stableTokens < minimumTokens) {
|
|
1568
|
+
recordBelowMinimum(stableTokens);
|
|
1569
|
+
} else {
|
|
1570
|
+
next.system = [{ type: "text", text: next.system, cache_control: cacheControl }];
|
|
1571
|
+
stamped = true;
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
} else if (Array.isArray(next.system) && next.system.length > 0) {
|
|
1575
|
+
const blocks = [...next.system];
|
|
1576
|
+
const lastBlock = blocks[blocks.length - 1];
|
|
1577
|
+
if (isPlainObject2(lastBlock) && !(typeof lastBlock.text === "string" && lastBlock.text.trim() === "")) {
|
|
1578
|
+
const stableTokens = toolsTokens + fullSystemTokens;
|
|
1579
|
+
if (stableTokens < minimumTokens) {
|
|
1580
|
+
recordBelowMinimum(stableTokens);
|
|
1581
|
+
} else {
|
|
1582
|
+
blocks[blocks.length - 1] = { ...lastBlock, cache_control: cacheControl };
|
|
1583
|
+
next.system = blocks;
|
|
1584
|
+
stamped = true;
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
if (!volatileTailPresent && next.messages.length >= 2) {
|
|
1589
|
+
const messages = [...next.messages];
|
|
1590
|
+
const lastIndex = messages.length - 1;
|
|
1591
|
+
const last = messages[lastIndex];
|
|
1592
|
+
if (isPlainObject2(last)) {
|
|
1593
|
+
const content = last.content;
|
|
1594
|
+
const prefixTokens = toolsTokens + fullSystemTokens + fullMessagesTokens;
|
|
1595
|
+
if (typeof content === "string") {
|
|
1596
|
+
if (content.trim() !== "") {
|
|
1597
|
+
if (prefixTokens < minimumTokens) {
|
|
1598
|
+
recordBelowMinimum(prefixTokens);
|
|
1599
|
+
} else {
|
|
1600
|
+
messages[lastIndex] = {
|
|
1601
|
+
...last,
|
|
1602
|
+
content: [{ type: "text", text: content, cache_control: cacheControl }]
|
|
1603
|
+
};
|
|
1604
|
+
next.messages = messages;
|
|
1605
|
+
stamped = true;
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
} else if (Array.isArray(content) && content.length > 0) {
|
|
1609
|
+
const blocks = [...content];
|
|
1610
|
+
const lastBlock = blocks[blocks.length - 1];
|
|
1611
|
+
if (isPlainObject2(lastBlock) && !(typeof lastBlock.text === "string" && lastBlock.text.trim() === "")) {
|
|
1612
|
+
if (prefixTokens < minimumTokens) {
|
|
1613
|
+
recordBelowMinimum(prefixTokens);
|
|
1614
|
+
} else {
|
|
1615
|
+
blocks[blocks.length - 1] = { ...lastBlock, cache_control: cacheControl };
|
|
1616
|
+
messages[lastIndex] = { ...last, content: blocks };
|
|
1617
|
+
next.messages = messages;
|
|
1618
|
+
stamped = true;
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
return stamped ? { stamped: true } : { stamped: false, belowMinimum };
|
|
1625
|
+
}
|
|
1626
|
+
function reduceRequest(request, config = {}) {
|
|
1627
|
+
const countTokens = config.countTokens ?? approximateTokenCount;
|
|
1628
|
+
const changes = [];
|
|
1629
|
+
const unchanged = (reason) => ({
|
|
1630
|
+
request,
|
|
1631
|
+
summary: {
|
|
1632
|
+
estimatedTokensBefore: 0,
|
|
1633
|
+
estimatedTokensAfter: 0,
|
|
1634
|
+
estimatedTokensSaved: 0,
|
|
1635
|
+
estimatedLosslessTokensSaved: 0,
|
|
1636
|
+
changes: [],
|
|
1637
|
+
skipped: true,
|
|
1638
|
+
skippedReason: reason
|
|
1639
|
+
}
|
|
1640
|
+
});
|
|
1641
|
+
if (!isPlainObject2(request)) return unchanged("Request is not an object.");
|
|
1642
|
+
try {
|
|
1643
|
+
const next = { ...request };
|
|
1644
|
+
const measure = (value) => {
|
|
1645
|
+
try {
|
|
1646
|
+
return typeof value === "string" ? countTokens(value) : countTokens(JSON.stringify(value) ?? "");
|
|
1647
|
+
} catch {
|
|
1648
|
+
return 0;
|
|
1649
|
+
}
|
|
1650
|
+
};
|
|
1651
|
+
const before = measure(next.instructions) + measure(next.system) + measure(next.input) + measure(next.messages) + measure(next.tools);
|
|
1652
|
+
if (Array.isArray(next.tools) && next.tools.length > 0 && config.toolSchemas !== false) {
|
|
1653
|
+
const toolOptions = isPlainObject2(config.toolSchemas) ? config.toolSchemas : {};
|
|
1654
|
+
const minified = minifyToolSchemas(next.tools, {
|
|
1655
|
+
countTokens,
|
|
1656
|
+
descriptions: toolOptions.descriptions ?? "keep",
|
|
1657
|
+
...typeof toolOptions.maximumDescriptionTokens === "number" ? { maximumDescriptionTokens: toolOptions.maximumDescriptionTokens } : {}
|
|
1658
|
+
});
|
|
1659
|
+
const saved = minified.tokensBefore - minified.tokensAfter;
|
|
1660
|
+
if (saved > 0) {
|
|
1661
|
+
next.tools = minified.tools;
|
|
1662
|
+
if (minified.losslessTokensSaved > 0) {
|
|
1663
|
+
changes.push({
|
|
1664
|
+
lever: "tool-schema-compaction",
|
|
1665
|
+
lossless: true,
|
|
1666
|
+
estimatedTokensSaved: minified.losslessTokensSaved,
|
|
1667
|
+
detail: `Compacted ${minified.tools.length} tool definition(s); the decoded schema is unchanged.`
|
|
1668
|
+
});
|
|
1669
|
+
}
|
|
1670
|
+
if (minified.lossyTokensSaved > 0) {
|
|
1671
|
+
changes.push({
|
|
1672
|
+
lever: "tool-schema-description-trim",
|
|
1673
|
+
lossless: false,
|
|
1674
|
+
estimatedTokensSaved: minified.lossyTokensSaved,
|
|
1675
|
+
detail: "Trimmed tool descriptions, which is how the model chooses between tools."
|
|
1676
|
+
});
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
let systemVolatileTail = [];
|
|
1681
|
+
const reduceSystemText = (text, label) => {
|
|
1682
|
+
let current = reduceText(text, config, countTokens, changes, label);
|
|
1683
|
+
if (config.repairCachePrefix) {
|
|
1684
|
+
const repaired = repairPrefix(current);
|
|
1685
|
+
if (repaired.moved.length > 0) {
|
|
1686
|
+
if (label === "system") systemVolatileTail = repaired.moved;
|
|
1687
|
+
current = repaired.text;
|
|
1688
|
+
changes.push({
|
|
1689
|
+
lever: "cache-prefix-repair",
|
|
1690
|
+
lossless: true,
|
|
1691
|
+
// The saving is a cache-rate change, not tokens removed.
|
|
1692
|
+
estimatedTokensSaved: 0,
|
|
1693
|
+
detail: `Moved ${repaired.moved.length} per-request value(s) out of the ${label} prefix, making the stable remainder cacheable. The text is reordered, not removed.`
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
return current;
|
|
1698
|
+
};
|
|
1699
|
+
if (typeof next.instructions === "string") {
|
|
1700
|
+
next.instructions = reduceSystemText(next.instructions, "instructions");
|
|
1701
|
+
}
|
|
1702
|
+
if (typeof next.system === "string") {
|
|
1703
|
+
next.system = reduceSystemText(next.system, "system");
|
|
1704
|
+
}
|
|
1705
|
+
if (config.freshToolResultCap) {
|
|
1706
|
+
const capOptions = isPlainObject2(config.freshToolResultCap) ? config.freshToolResultCap : {};
|
|
1707
|
+
const threshold = capOptions.maximumTokens === void 0 ? 600 : capOptions.maximumTokens;
|
|
1708
|
+
if (typeof threshold !== "number" || !Number.isInteger(threshold) || threshold < 64) {
|
|
1709
|
+
throw new Error("freshToolResultCap maximumTokens must be an integer of at least 64.");
|
|
1710
|
+
}
|
|
1711
|
+
if (Array.isArray(next.input)) {
|
|
1712
|
+
next.input = capFreshToolResults(next.input, threshold, countTokens, changes);
|
|
1713
|
+
}
|
|
1714
|
+
if (Array.isArray(next.messages)) {
|
|
1715
|
+
next.messages = capFreshToolResults(next.messages, threshold, countTokens, changes);
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
if (config.historyCompaction) {
|
|
1719
|
+
const options = isPlainObject2(config.historyCompaction) ? config.historyCompaction : {};
|
|
1720
|
+
if (Array.isArray(next.input)) {
|
|
1721
|
+
next.input = compactStaleHistory(next.input, options, countTokens, changes);
|
|
1722
|
+
}
|
|
1723
|
+
if (Array.isArray(next.messages)) {
|
|
1724
|
+
next.messages = compactStaleHistory(next.messages, options, countTokens, changes);
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
if (config.crossTurnMemoryDedup) {
|
|
1728
|
+
const memoryOptions = isPlainObject2(config.crossTurnMemoryDedup) ? config.crossTurnMemoryDedup : {};
|
|
1729
|
+
if (Array.isArray(next.input)) {
|
|
1730
|
+
next.input = dedupeMemoryAcrossTurns(next.input, memoryOptions, countTokens, changes);
|
|
1731
|
+
}
|
|
1732
|
+
if (Array.isArray(next.messages)) {
|
|
1733
|
+
next.messages = dedupeMemoryAcrossTurns(next.messages, memoryOptions, countTokens, changes);
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
if (Array.isArray(next.input)) {
|
|
1737
|
+
next.input = next.input.map((message) => {
|
|
1738
|
+
if (!isPlainObject2(message)) return message;
|
|
1739
|
+
if (!("content" in message)) return message;
|
|
1740
|
+
return { ...message, content: reduceMessageContent(message.content, config, countTokens, changes) };
|
|
1741
|
+
});
|
|
1742
|
+
} else if (typeof next.input === "string") {
|
|
1743
|
+
next.input = reduceText(next.input, config, countTokens, changes, "input");
|
|
1744
|
+
}
|
|
1745
|
+
if (Array.isArray(next.messages)) {
|
|
1746
|
+
next.messages = next.messages.map((message) => {
|
|
1747
|
+
if (!isPlainObject2(message)) return message;
|
|
1748
|
+
if (!("content" in message)) return message;
|
|
1749
|
+
return { ...message, content: reduceMessageContent(message.content, config, countTokens, changes) };
|
|
1750
|
+
});
|
|
1751
|
+
}
|
|
1752
|
+
if (config.executionTier === "flex" && next.service_tier === void 0) {
|
|
1753
|
+
const model = typeof next.model === "string" ? next.model : "";
|
|
1754
|
+
const anthropicShaped = model.toLowerCase().includes("claude");
|
|
1755
|
+
const catalogued = !anthropicShaped && resolveModelId2(publishedModelPrices, "openai", model).status === "resolved";
|
|
1756
|
+
if (catalogued) {
|
|
1757
|
+
next.service_tier = "flex";
|
|
1758
|
+
changes.push({
|
|
1759
|
+
lever: "execution-tier-flex",
|
|
1760
|
+
lossless: true,
|
|
1761
|
+
// The saving is a rate change on the whole request, not tokens removed.
|
|
1762
|
+
estimatedTokensSaved: 0,
|
|
1763
|
+
detail: "Set service_tier to flex: batch-level pricing (50% off) on a synchronous API. Same model, same answer; latency may rise and 429s fall back to standard, so this applies only to workloads the caller flagged latency-tolerant."
|
|
1764
|
+
});
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
if (config.cacheTtl === "5m" || config.cacheTtl === "1h") {
|
|
1768
|
+
const ttl = config.cacheTtl;
|
|
1769
|
+
const decision = config.cacheVolatilityGuard?.observe(request) ?? { stamp: true };
|
|
1770
|
+
const result = decision.stamp ? stampAnthropicCacheTtl(next, ttl, systemVolatileTail, countTokens) : { stamped: false };
|
|
1771
|
+
if (!decision.stamp) {
|
|
1772
|
+
changes.push({
|
|
1773
|
+
lever: "cache-ttl-suppressed",
|
|
1774
|
+
lossless: true,
|
|
1775
|
+
// Nothing was changed; this records WHY no stamp was placed.
|
|
1776
|
+
estimatedTokensSaved: 0,
|
|
1777
|
+
detail: `Cache stamp withheld: ${decision.reason ?? "the conversation's prefix is not stable"}. A breakpoint over a churning prefix buys write premiums and never reads \u2014 a measured loss, not a saving.`
|
|
1778
|
+
});
|
|
1779
|
+
} else if (!result.stamped && result.belowMinimum) {
|
|
1780
|
+
changes.push({
|
|
1781
|
+
lever: "cache-ttl-suppressed",
|
|
1782
|
+
lossless: true,
|
|
1783
|
+
// Nothing was changed; this records WHY no stamp was placed.
|
|
1784
|
+
estimatedTokensSaved: 0,
|
|
1785
|
+
detail: `Cache stamp withheld: the stable prefix measures ${result.belowMinimum.measuredTokens} token(s) by MESH's approximate counter, under the ${result.belowMinimum.minimumTokens}-token threshold MESH requires \u2014 the model's documented ${anthropicCacheableMinimumTokens(typeof next.model === "string" ? next.model : "")}-token cacheable minimum with a 1.30x margin for the counter's calibrated overcount on prose. Anthropic processes a prefix under its minimum without caching at all \u2014 a provider-side no-op \u2014 so a breakpoint here would buy a write premium and never read, and would record a saving that can never happen.`
|
|
1786
|
+
});
|
|
1787
|
+
}
|
|
1788
|
+
if (result.stamped) {
|
|
1789
|
+
changes.push({
|
|
1790
|
+
lever: `cache-ttl-${ttl}`,
|
|
1791
|
+
lossless: true,
|
|
1792
|
+
// The saving is a rate change (cache reads at 0.1x), not tokens removed.
|
|
1793
|
+
estimatedTokensSaved: 0,
|
|
1794
|
+
detail: `Stamped cache_control {type: 'ephemeral', ttl: '${ttl}'} on the stable prefix (system prompt; plus the last message on multi-turn requests, whose history repeats next turn). ` + (ttl === "5m" ? "Writes bill at 1.25x base input, reads at 0.1x: the multi-turn agent setting, since Anthropic's cache is opt-in and an unstamped agent re-pays full rate for its history every turn. The caller must flag the workload - it is never set by default." : "Writes bill at 2x base input, reads at 0.1x: this pays only when calls arrive more than ~5 minutes apart, and the caller must flag that workload - it is never set by default.")
|
|
1795
|
+
});
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
if (config.maxOutputTokens !== void 0) {
|
|
1799
|
+
const assertValidCap = () => {
|
|
1800
|
+
if (!Number.isInteger(config.maxOutputTokens) || config.maxOutputTokens < 1) {
|
|
1801
|
+
throw new Error("maxOutputTokens must be a positive integer.");
|
|
1802
|
+
}
|
|
1803
|
+
};
|
|
1804
|
+
const claudeModel = typeof next.model === "string" && next.model.toLowerCase().includes("claude");
|
|
1805
|
+
const messagesShaped = Array.isArray(next.messages);
|
|
1806
|
+
if (claudeModel && messagesShaped && typeof next.max_tokens === "number" && Number.isFinite(next.max_tokens)) {
|
|
1807
|
+
assertValidCap();
|
|
1808
|
+
if (next.max_tokens > config.maxOutputTokens) {
|
|
1809
|
+
next.max_tokens = config.maxOutputTokens;
|
|
1810
|
+
changes.push({
|
|
1811
|
+
lever: "output-cap",
|
|
1812
|
+
lossless: false,
|
|
1813
|
+
estimatedTokensSaved: 0,
|
|
1814
|
+
detail: `Clamped max_tokens down to ${config.maxOutputTokens} (downward only; a caller cap at or below the budget is never touched). Responses that would have run longer are truncated, so this needs checking against real output lengths.`
|
|
1815
|
+
});
|
|
1816
|
+
}
|
|
1817
|
+
} else if (!claudeModel && messagesShaped) {
|
|
1818
|
+
if (next.max_completion_tokens === void 0 && next.max_tokens === void 0 && next.max_output_tokens === void 0) {
|
|
1819
|
+
assertValidCap();
|
|
1820
|
+
next.max_completion_tokens = config.maxOutputTokens;
|
|
1821
|
+
changes.push({
|
|
1822
|
+
lever: "output-cap",
|
|
1823
|
+
lossless: false,
|
|
1824
|
+
estimatedTokensSaved: 0,
|
|
1825
|
+
detail: `Set max_completion_tokens to ${config.maxOutputTokens}. Responses that would have run longer are truncated, so this needs checking against real output lengths.`
|
|
1826
|
+
});
|
|
1827
|
+
}
|
|
1828
|
+
} else if (next.max_output_tokens === void 0) {
|
|
1829
|
+
assertValidCap();
|
|
1830
|
+
next.max_output_tokens = config.maxOutputTokens;
|
|
1831
|
+
changes.push({
|
|
1832
|
+
lever: "output-cap",
|
|
1833
|
+
lossless: false,
|
|
1834
|
+
estimatedTokensSaved: 0,
|
|
1835
|
+
detail: `Set max_output_tokens to ${config.maxOutputTokens}. Responses that would have run longer are truncated, so this needs checking against real output lengths.`
|
|
1836
|
+
});
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1839
|
+
if (config.outputEffort !== void 0) {
|
|
1840
|
+
const level = config.outputEffort;
|
|
1841
|
+
const model = typeof next.model === "string" ? next.model : "";
|
|
1842
|
+
const claudeMessagesShaped = model.toLowerCase().includes("claude") && Array.isArray(next.messages);
|
|
1843
|
+
const existingConfig = isPlainObject2(next.output_config) ? next.output_config : void 0;
|
|
1844
|
+
const callerSetEffort = existingConfig !== void 0 && "effort" in existingConfig;
|
|
1845
|
+
if (claudeMessagesShaped && !callerSetEffort) {
|
|
1846
|
+
if (anthropicEffortSupport(model).has(level)) {
|
|
1847
|
+
next.output_config = { ...existingConfig ?? {}, effort: level };
|
|
1848
|
+
changes.push({
|
|
1849
|
+
lever: `output-effort-${level}`,
|
|
1850
|
+
lossless: false,
|
|
1851
|
+
// The saving is fewer output tokens the model chooses to emit, read
|
|
1852
|
+
// from provider usage by the gate, not an input-token count here.
|
|
1853
|
+
estimatedTokensSaved: 0,
|
|
1854
|
+
detail: `Set output_config.effort to '${level}'. The model thinks less and answers terser, cutting output tokens (the dearer half of the bill). A top-level field outside the cached prefix, so a stable value never invalidates caching. Lossy \u2014 it changes the answer's depth, so it needs a task-completion replay-gate pass on the caller's own traffic before the saving may be billed.`
|
|
1855
|
+
});
|
|
1856
|
+
} else {
|
|
1857
|
+
changes.push({
|
|
1858
|
+
lever: "output-effort-suppressed",
|
|
1859
|
+
lossless: true,
|
|
1860
|
+
// Nothing was changed; this records WHY no effort was set.
|
|
1861
|
+
estimatedTokensSaved: 0,
|
|
1862
|
+
detail: `Effort '${level}' withheld: model '${model || "(none)"}' is not in MESH's dated allow-list of Claude models known to accept that level. An unsupported effort value is a 400, and the reducer never risks breaking a request, so it is left as the caller sent it.`
|
|
1863
|
+
});
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
}
|
|
1867
|
+
const after = measure(next.instructions) + measure(next.system) + measure(next.input) + measure(next.messages) + measure(next.tools);
|
|
1868
|
+
const summary = {
|
|
1869
|
+
estimatedTokensBefore: before,
|
|
1870
|
+
estimatedTokensAfter: after,
|
|
1871
|
+
estimatedTokensSaved: Math.max(0, before - after),
|
|
1872
|
+
estimatedLosslessTokensSaved: changes.filter((change) => change.lossless).reduce((sum, change) => sum + change.estimatedTokensSaved, 0),
|
|
1873
|
+
changes,
|
|
1874
|
+
skipped: false
|
|
1875
|
+
};
|
|
1876
|
+
config.onReduction?.(summary);
|
|
1877
|
+
return { request: changes.length === 0 ? request : next, summary };
|
|
1878
|
+
} catch (cause) {
|
|
1879
|
+
return unchanged(cause instanceof Error ? cause.message : String(cause));
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
var PREFIX_BREAKERS, STALE_BODY_MUST_KEEP, STALE_BODY_FILE_PATH, MESH_MARKER;
|
|
1883
|
+
var init_reduce_request = __esm({
|
|
1884
|
+
"packages/sdk-js/src/reduce-request.ts"() {
|
|
1885
|
+
"use strict";
|
|
1886
|
+
init_model_prices();
|
|
1887
|
+
init_token_reduction();
|
|
1888
|
+
init_tool_schema();
|
|
1889
|
+
init_price_catalogue2();
|
|
1890
|
+
PREFIX_BREAKERS = [
|
|
1891
|
+
/\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(?::\d{2})?(?:\.\d+)?Z?/g,
|
|
1892
|
+
/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi
|
|
1893
|
+
];
|
|
1894
|
+
STALE_BODY_MUST_KEEP = /\b(error|errors|errored|fail|fails|failed|failure|exception|traceback|stack trace|denied|timeout|timed out|warning|fatal|panic|refused|rejected|not found|invalid)\b/i;
|
|
1895
|
+
STALE_BODY_FILE_PATH = /(?:^|[\s"'`(=[])((?:\/|\.{1,2}\/|~\/|[A-Za-z]:\\)[\w.\\/-]+|[\w.-]+\/[\w./-]+|[\w-]+\.(?:ts|tsx|js|jsx|mjs|cjs|py|json|jsonl|md|yml|yaml|toml|txt|csv|tsv|html|css|go|rs|java|rb|sh|sql|log|xml|lock))\b/;
|
|
1896
|
+
MESH_MARKER = /\[mesh: \d+ chars of [\w -]+\]/;
|
|
1897
|
+
}
|
|
1898
|
+
});
|
|
1899
|
+
|
|
1900
|
+
// packages/sdk-js/src/replay-gate.ts
|
|
1901
|
+
function isPlainObject3(value) {
|
|
1902
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1903
|
+
}
|
|
1904
|
+
function responseText(response) {
|
|
1905
|
+
if (!isPlainObject3(response)) return "";
|
|
1906
|
+
if (typeof response.output_text === "string") return response.output_text;
|
|
1907
|
+
const parts = [];
|
|
1908
|
+
const walk = (value) => {
|
|
1909
|
+
if (Array.isArray(value)) {
|
|
1910
|
+
for (const item of value) walk(item);
|
|
1911
|
+
return;
|
|
1912
|
+
}
|
|
1913
|
+
if (!isPlainObject3(value)) return;
|
|
1914
|
+
if (typeof value.text === "string") parts.push(value.text);
|
|
1915
|
+
if (Array.isArray(value.content)) walk(value.content);
|
|
1916
|
+
if (isPlainObject3(value.message)) walk(value.message);
|
|
1917
|
+
};
|
|
1918
|
+
walk(response.output ?? response.choices ?? response.content ?? []);
|
|
1919
|
+
return parts.join("\n");
|
|
1920
|
+
}
|
|
1921
|
+
var init_replay_gate = __esm({
|
|
1922
|
+
"packages/sdk-js/src/replay-gate.ts"() {
|
|
1923
|
+
"use strict";
|
|
1924
|
+
init_cost2();
|
|
1925
|
+
init_provider_usage();
|
|
1926
|
+
init_confidence();
|
|
1927
|
+
init_reduce_request();
|
|
1928
|
+
}
|
|
1929
|
+
});
|
|
1930
|
+
|
|
1931
|
+
// packages/sdk-js/src/cache-volatility-guard.ts
|
|
1932
|
+
import { createHash as createHash3 } from "node:crypto";
|
|
1933
|
+
var isPlainObject4, digest, CachePrefixVolatilityGuard;
|
|
1934
|
+
var init_cache_volatility_guard = __esm({
|
|
1935
|
+
"packages/sdk-js/src/cache-volatility-guard.ts"() {
|
|
1936
|
+
"use strict";
|
|
1937
|
+
isPlainObject4 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1938
|
+
digest = (value) => createHash3("sha256").update(JSON.stringify(value ?? null) ?? "null").digest("hex").slice(0, 32);
|
|
1939
|
+
CachePrefixVolatilityGuard = class {
|
|
1940
|
+
constructor(maxConversations = 1e3) {
|
|
1941
|
+
this.maxConversations = maxConversations;
|
|
1942
|
+
if (!Number.isInteger(maxConversations) || maxConversations < 1) {
|
|
1943
|
+
throw new Error("maxConversations must be a positive integer.");
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
maxConversations;
|
|
1947
|
+
/** Insertion-ordered; re-inserted on touch, so the first key is the LRU. */
|
|
1948
|
+
conversations = /* @__PURE__ */ new Map();
|
|
1949
|
+
/**
|
|
1950
|
+
* Record this request and decide whether its conversation has earned a
|
|
1951
|
+
* cache stamp. Call once per request, with the raw (unreduced) request.
|
|
1952
|
+
*/
|
|
1953
|
+
observe(request) {
|
|
1954
|
+
if (!isPlainObject4(request) || !Array.isArray(request.messages) || request.messages.length === 0) {
|
|
1955
|
+
return { stamp: true };
|
|
1956
|
+
}
|
|
1957
|
+
const key = digest([request.model ?? null, request.messages[0]]);
|
|
1958
|
+
const prefixHash = digest([request.tools ?? null, request.system ?? null]);
|
|
1959
|
+
const messageHashes = request.messages.map(digest);
|
|
1960
|
+
const prior = this.conversations.get(key);
|
|
1961
|
+
if (prior !== void 0) this.conversations.delete(key);
|
|
1962
|
+
if (prior === void 0) {
|
|
1963
|
+
this.conversations.set(key, { prefixHash, messageHashes, volatile: false, stableStreak: 0 });
|
|
1964
|
+
this.evict();
|
|
1965
|
+
return { stamp: true };
|
|
1966
|
+
}
|
|
1967
|
+
const prefixChanged = prior.prefixHash !== prefixHash;
|
|
1968
|
+
const historyExtends = messageHashes.length >= prior.messageHashes.length && prior.messageHashes.every((hash, index) => messageHashes[index] === hash);
|
|
1969
|
+
const entry3 = { prefixHash, messageHashes, volatile: prior.volatile, stableStreak: prior.stableStreak };
|
|
1970
|
+
let decision = { stamp: true };
|
|
1971
|
+
if (prefixChanged || !historyExtends) {
|
|
1972
|
+
entry3.volatile = true;
|
|
1973
|
+
entry3.stableStreak = 0;
|
|
1974
|
+
decision = {
|
|
1975
|
+
stamp: false,
|
|
1976
|
+
reason: prefixChanged ? "tools/system changed between turns of this conversation, so a cached prefix could never be read back" : "the conversation history was rewritten between turns, so a cached prefix could never be read back"
|
|
1977
|
+
};
|
|
1978
|
+
} else if (entry3.volatile) {
|
|
1979
|
+
entry3.stableStreak += 1;
|
|
1980
|
+
if (entry3.stableStreak >= 2) {
|
|
1981
|
+
entry3.volatile = false;
|
|
1982
|
+
entry3.stableStreak = 0;
|
|
1983
|
+
} else {
|
|
1984
|
+
decision = {
|
|
1985
|
+
stamp: false,
|
|
1986
|
+
reason: "this conversation's prefix changed on an earlier turn; waiting for it to prove stable before stamping again"
|
|
1987
|
+
};
|
|
1988
|
+
}
|
|
1989
|
+
}
|
|
1990
|
+
this.conversations.set(key, entry3);
|
|
1991
|
+
return decision;
|
|
1992
|
+
}
|
|
1993
|
+
evict() {
|
|
1994
|
+
while (this.conversations.size > this.maxConversations) {
|
|
1995
|
+
const oldest = this.conversations.keys().next().value;
|
|
1996
|
+
if (oldest === void 0) return;
|
|
1997
|
+
this.conversations.delete(oldest);
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
};
|
|
2001
|
+
}
|
|
2002
|
+
});
|
|
2003
|
+
|
|
2004
|
+
// packages/sdk-js/src/blind-features.ts
|
|
2005
|
+
import { createHmac } from "node:crypto";
|
|
2006
|
+
function isPlainObject5(value) {
|
|
2007
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2008
|
+
}
|
|
2009
|
+
function textOf(value) {
|
|
2010
|
+
if (typeof value === "string") return value;
|
|
2011
|
+
if (Array.isArray(value)) {
|
|
2012
|
+
return value.map((part) => isPlainObject5(part) && typeof part.text === "string" ? part.text : "").join("\n");
|
|
2013
|
+
}
|
|
2014
|
+
if (isPlainObject5(value) && typeof value.text === "string") return value.text;
|
|
2015
|
+
return "";
|
|
2016
|
+
}
|
|
2017
|
+
function extractBlindFeatures(request, options) {
|
|
2018
|
+
if (!options.hmacKey) throw new Error("extractBlindFeatures requires a per-deployment hmacKey.");
|
|
2019
|
+
const hash = (text) => createHmac("sha256", options.hmacKey).update(text).digest("hex").slice(0, 32);
|
|
2020
|
+
const req = isPlainObject5(request) ? request : {};
|
|
2021
|
+
const model = typeof req.model === "string" ? req.model : "unknown";
|
|
2022
|
+
const messages = Array.isArray(req.messages) ? req.messages : Array.isArray(req.input) ? req.input : [];
|
|
2023
|
+
const tools = Array.isArray(req.tools) ? req.tools : [];
|
|
2024
|
+
const anthropicShaped = model.toLowerCase().includes("claude") || typeof req.system !== "undefined";
|
|
2025
|
+
const provider = model.toLowerCase().includes("claude") ? "anthropic" : model === "unknown" ? "unknown" : "openai";
|
|
2026
|
+
const roleTokensApprox = {};
|
|
2027
|
+
const blockHashes = [];
|
|
2028
|
+
let inputTokensApprox = 0;
|
|
2029
|
+
if (req.system !== void 0) {
|
|
2030
|
+
const text = textOf(req.system);
|
|
2031
|
+
const tokens = approximateTokenCount(text);
|
|
2032
|
+
roleTokensApprox.system = (roleTokensApprox.system ?? 0) + tokens;
|
|
2033
|
+
inputTokensApprox += tokens;
|
|
2034
|
+
if (text.trim() !== "") blockHashes.push(hash(text));
|
|
2035
|
+
}
|
|
2036
|
+
for (const message of messages) {
|
|
2037
|
+
if (!isPlainObject5(message)) continue;
|
|
2038
|
+
const role = typeof message.role === "string" ? message.role : message.type === "function_call_output" ? "tool" : "unknown";
|
|
2039
|
+
const text = textOf("content" in message ? message.content : message.output);
|
|
2040
|
+
const tokens = approximateTokenCount(text);
|
|
2041
|
+
roleTokensApprox[role] = (roleTokensApprox[role] ?? 0) + tokens;
|
|
2042
|
+
inputTokensApprox += tokens;
|
|
2043
|
+
if (text.trim() !== "") blockHashes.push(hash(text));
|
|
2044
|
+
}
|
|
2045
|
+
const toolSchemaText = tools.length > 0 ? JSON.stringify(tools) : "";
|
|
2046
|
+
const toolSchemaTokensApprox = approximateTokenCount(toolSchemaText);
|
|
2047
|
+
inputTokensApprox += toolSchemaTokensApprox;
|
|
2048
|
+
const uniqueBlocks = new Set(blockHashes);
|
|
2049
|
+
const duplicateBlockFraction = blockHashes.length === 0 ? 0 : (blockHashes.length - uniqueBlocks.size) / blockHashes.length;
|
|
2050
|
+
const prefixText = `${toolSchemaText}\0${anthropicShaped ? textOf(req.system) : ""}`;
|
|
2051
|
+
const stablePrefixHash = hash(prefixText);
|
|
2052
|
+
return {
|
|
2053
|
+
schemaVersion: 1,
|
|
2054
|
+
provider,
|
|
2055
|
+
model,
|
|
2056
|
+
messageCount: messages.length,
|
|
2057
|
+
toolCount: tools.length,
|
|
2058
|
+
roleTokensApprox,
|
|
2059
|
+
inputTokensApprox,
|
|
2060
|
+
toolSchemaTokensApprox,
|
|
2061
|
+
blockHashes,
|
|
2062
|
+
duplicateBlockFraction,
|
|
2063
|
+
stablePrefixHash
|
|
2064
|
+
};
|
|
2065
|
+
}
|
|
2066
|
+
var init_blind_features = __esm({
|
|
2067
|
+
"packages/sdk-js/src/blind-features.ts"() {
|
|
2068
|
+
"use strict";
|
|
2069
|
+
init_token_reduction();
|
|
2070
|
+
}
|
|
2071
|
+
});
|
|
2072
|
+
|
|
2073
|
+
// packages/core/dist/src/savings-ledger.js
|
|
2074
|
+
import { createHash as createHash4 } from "node:crypto";
|
|
2075
|
+
function ledgerEventHash2(seq, prevHash, event) {
|
|
2076
|
+
return createHash4("sha256").update(`${seq}
|
|
2077
|
+
${prevHash}
|
|
2078
|
+
${canonicalJson2(event)}`).digest("hex");
|
|
2079
|
+
}
|
|
2080
|
+
function chainLedgerRecord(event, previous) {
|
|
2081
|
+
const seq = previous ? previous.seq + 1 : 0;
|
|
2082
|
+
const prevHash = previous ? previous.eventHash : LEDGER_GENESIS_HASH2;
|
|
2083
|
+
return { seq, prevHash, eventHash: ledgerEventHash2(seq, prevHash, event), event };
|
|
2084
|
+
}
|
|
2085
|
+
function priceLedgerUsage(catalogue, input) {
|
|
2086
|
+
const resolution = resolveModelId2(catalogue, input.provider, input.model);
|
|
2087
|
+
if (resolution.status === "unresolved") {
|
|
2088
|
+
return { pricingStatus: "unpriced", reason: resolution.reason };
|
|
2089
|
+
}
|
|
2090
|
+
const split5m = input.usage.cacheWrite5mInputTokens;
|
|
2091
|
+
const split1h = input.usage.cacheWrite1hInputTokens;
|
|
2092
|
+
const splitReported = split5m !== void 0 || split1h !== void 0;
|
|
2093
|
+
if (splitReported && (split5m ?? 0) + (split1h ?? 0) !== input.usage.cacheWriteInputTokens) {
|
|
2094
|
+
return {
|
|
2095
|
+
pricingStatus: "unpriced",
|
|
2096
|
+
reason: `Cache-write TTL split (5m ${split5m ?? 0} + 1h ${split1h ?? 0}) does not sum to cacheWriteInputTokens ${input.usage.cacheWriteInputTokens}.`
|
|
2097
|
+
};
|
|
2098
|
+
}
|
|
2099
|
+
const write1hForPricing = splitReported ? split1h ?? 0 : input.meshPlacedCacheControl && input.meshPlacedTtl !== "5m" ? input.usage.cacheWriteInputTokens : 0;
|
|
2100
|
+
const pricingUsage = {
|
|
2101
|
+
inputTokens: input.usage.inputTokens,
|
|
2102
|
+
outputTokens: input.usage.outputTokens,
|
|
2103
|
+
cachedInputTokens: input.usage.cachedInputTokens,
|
|
2104
|
+
cacheWriteInputTokens: input.usage.cacheWriteInputTokens,
|
|
2105
|
+
cacheWrite1hInputTokens: write1hForPricing
|
|
2106
|
+
};
|
|
2107
|
+
const query = { provider: input.provider, model: resolution.model, at: input.at };
|
|
2108
|
+
const actual = priceUsage2(catalogue, { ...query, mode: input.executionMode }, pricingUsage);
|
|
2109
|
+
if (actual.status === "unpriced")
|
|
2110
|
+
return { pricingStatus: "unpriced", reason: actual.reason };
|
|
2111
|
+
const actualInputOnly = priceUsage2(catalogue, { ...query, mode: input.executionMode }, {
|
|
2112
|
+
...pricingUsage,
|
|
2113
|
+
outputTokens: 0
|
|
2114
|
+
});
|
|
2115
|
+
if (actualInputOnly.status === "unpriced") {
|
|
2116
|
+
return { pricingStatus: "unpriced", reason: actualInputOnly.reason };
|
|
2117
|
+
}
|
|
2118
|
+
const standardWithCache = input.executionMode === "standard" ? actual : priceUsage2(catalogue, { ...query, mode: "standard" }, pricingUsage);
|
|
2119
|
+
if (standardWithCache.status === "unpriced") {
|
|
2120
|
+
return { pricingStatus: "unpriced", reason: standardWithCache.reason };
|
|
2121
|
+
}
|
|
2122
|
+
const fullRateStandard = priceUsage2(catalogue, { ...query, mode: "standard" }, {
|
|
2123
|
+
inputTokens: input.usage.inputTokens,
|
|
2124
|
+
outputTokens: input.usage.outputTokens
|
|
2125
|
+
});
|
|
2126
|
+
if (fullRateStandard.status === "unpriced") {
|
|
2127
|
+
return { pricingStatus: "unpriced", reason: fullRateStandard.reason };
|
|
2128
|
+
}
|
|
2129
|
+
const cacheDeltaUsd = fullRateStandard.costUsd - standardWithCache.costUsd;
|
|
2130
|
+
const hasCacheTraffic = input.usage.cachedInputTokens > 0 || input.usage.cacheWriteInputTokens > 0;
|
|
2131
|
+
return {
|
|
2132
|
+
pricingStatus: "priced",
|
|
2133
|
+
actualUsd: actual.costUsd,
|
|
2134
|
+
inputUsd: actualInputOnly.costUsd,
|
|
2135
|
+
...input.executionMode === "standard" ? {} : { tierSavedUsd: standardWithCache.costUsd - actual.costUsd },
|
|
2136
|
+
...hasCacheTraffic ? input.meshPlacedCacheControl ? { cacheAttributedUsd: cacheDeltaUsd } : { providerCacheAlreadyOwnedUsd: cacheDeltaUsd } : {}
|
|
2137
|
+
};
|
|
2138
|
+
}
|
|
2139
|
+
var LEDGER_GENESIS_HASH2;
|
|
2140
|
+
var init_savings_ledger2 = __esm({
|
|
2141
|
+
"packages/core/dist/src/savings-ledger.js"() {
|
|
2142
|
+
"use strict";
|
|
2143
|
+
init_price_catalogue2();
|
|
2144
|
+
LEDGER_GENESIS_HASH2 = "0".repeat(64);
|
|
2145
|
+
}
|
|
2146
|
+
});
|
|
2147
|
+
|
|
2148
|
+
// packages/sdk-js/src/savings-ledger.ts
|
|
2149
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
2150
|
+
import { dirname } from "node:path";
|
|
2151
|
+
function executionModeOf(responseServiceTier2) {
|
|
2152
|
+
if (responseServiceTier2 === "flex") return "flex";
|
|
2153
|
+
if (responseServiceTier2 === "batch") return "batch";
|
|
2154
|
+
return "standard";
|
|
2155
|
+
}
|
|
2156
|
+
function usageOf(event) {
|
|
2157
|
+
const { inputTokens, outputTokens, cachedInputTokens, cacheWriteInputTokens } = event;
|
|
2158
|
+
if ([inputTokens, outputTokens, cachedInputTokens, cacheWriteInputTokens].some((count) => typeof count !== "number" || !Number.isFinite(count) || count < 0)) {
|
|
2159
|
+
return void 0;
|
|
2160
|
+
}
|
|
2161
|
+
const validCount = (count) => typeof count === "number" && Number.isFinite(count) && count >= 0;
|
|
2162
|
+
const splitReported = event.cacheWrite5mInputTokens !== void 0 || event.cacheWrite1hInputTokens !== void 0;
|
|
2163
|
+
const splitValid = splitReported && validCount(event.cacheWrite5mInputTokens ?? 0) && validCount(event.cacheWrite1hInputTokens ?? 0);
|
|
2164
|
+
if (splitReported && !splitValid) return void 0;
|
|
2165
|
+
return {
|
|
2166
|
+
inputTokens,
|
|
2167
|
+
outputTokens,
|
|
2168
|
+
cachedInputTokens,
|
|
2169
|
+
cacheWriteInputTokens,
|
|
2170
|
+
...splitValid ? {
|
|
2171
|
+
cacheWrite5mInputTokens: event.cacheWrite5mInputTokens ?? 0,
|
|
2172
|
+
cacheWrite1hInputTokens: event.cacheWrite1hInputTokens ?? 0
|
|
2173
|
+
} : {}
|
|
2174
|
+
};
|
|
2175
|
+
}
|
|
2176
|
+
function billableEventFromRequest(event, config, catalogue, pinnedCatalogueVersion) {
|
|
2177
|
+
const provider = config.provider ?? "openai";
|
|
2178
|
+
const changes = event.reduction && !event.reduction.skipped ? event.reduction.changes : [];
|
|
2179
|
+
const ttlChange = changes.find((change) => CACHE_TTL_LEVERS.has(change.lever));
|
|
2180
|
+
const meshPlacedCacheControl = ttlChange !== void 0;
|
|
2181
|
+
const meshPlacedTtl = ttlChange === void 0 ? void 0 : CACHE_TTL_LEVERS.get(ttlChange.lever);
|
|
2182
|
+
const executionMode = executionModeOf(event.responseServiceTier);
|
|
2183
|
+
const usage = usageOf(event);
|
|
2184
|
+
const pricing = usage === void 0 ? { pricingStatus: "unpriced", reason: "The event carries no complete, valid provider-verbatim usage." } : priceLedgerUsage(catalogue, {
|
|
2185
|
+
provider,
|
|
2186
|
+
model: event.model,
|
|
2187
|
+
at: event.timestamp.slice(0, 10),
|
|
2188
|
+
executionMode,
|
|
2189
|
+
usage,
|
|
2190
|
+
meshPlacedCacheControl,
|
|
2191
|
+
...meshPlacedTtl === void 0 ? {} : { meshPlacedTtl }
|
|
2192
|
+
});
|
|
2193
|
+
const priced = pricing.pricingStatus === "priced" ? pricing : void 0;
|
|
2194
|
+
const levers = [];
|
|
2195
|
+
const excluded = {};
|
|
2196
|
+
if (event.cacheHit) {
|
|
2197
|
+
levers.push({
|
|
2198
|
+
leverId: EXACT_CACHE_LEVER,
|
|
2199
|
+
kind: "exact_cache",
|
|
2200
|
+
...event.costAvoidedUsd === void 0 ? {} : { savedUsd: event.costAvoidedUsd }
|
|
2201
|
+
});
|
|
2202
|
+
}
|
|
2203
|
+
if (priced?.tierSavedUsd !== void 0) {
|
|
2204
|
+
levers.push({
|
|
2205
|
+
leverId: `execution-tier-${executionMode}`,
|
|
2206
|
+
kind: "tier",
|
|
2207
|
+
savedUsd: priced.tierSavedUsd
|
|
2208
|
+
});
|
|
2209
|
+
}
|
|
2210
|
+
if (priced?.cacheAttributedUsd !== void 0 && ttlChange !== void 0) {
|
|
2211
|
+
levers.push({
|
|
2212
|
+
leverId: ttlChange.lever,
|
|
2213
|
+
kind: "provider_cache",
|
|
2214
|
+
savedUsd: priced.cacheAttributedUsd
|
|
2215
|
+
});
|
|
2216
|
+
}
|
|
2217
|
+
if (priced?.providerCacheAlreadyOwnedUsd !== void 0) {
|
|
2218
|
+
excluded.providerCacheAlreadyOwnedUsd = priced.providerCacheAlreadyOwnedUsd;
|
|
2219
|
+
}
|
|
2220
|
+
let baselineUsd;
|
|
2221
|
+
try {
|
|
2222
|
+
const supplied = config.getBaselineUsd?.(event);
|
|
2223
|
+
if (typeof supplied === "number" && Number.isFinite(supplied) && supplied >= 0) baselineUsd = supplied;
|
|
2224
|
+
} catch {
|
|
2225
|
+
baselineUsd = void 0;
|
|
2226
|
+
}
|
|
2227
|
+
const tokenChanges = changes.filter((change) => !EXECUTION_TIER_LEVERS.has(change.lever) && !(CACHE_TTL_LEVERS.has(change.lever) && priced?.cacheAttributedUsd !== void 0));
|
|
2228
|
+
const summary = event.reduction && !event.reduction.skipped ? event.reduction : void 0;
|
|
2229
|
+
const estimatedBefore = summary?.estimatedTokensBefore ?? 0;
|
|
2230
|
+
const estimatedRemoved = tokenChanges.reduce((sum, change) => sum + (Number.isFinite(change.estimatedTokensSaved) && change.estimatedTokensSaved > 0 ? change.estimatedTokensSaved : 0), 0);
|
|
2231
|
+
const removedFraction = estimatedBefore > 0 ? estimatedRemoved / estimatedBefore : 0;
|
|
2232
|
+
const estimateUsable = baselineUsd === void 0 && priced !== void 0 && usage !== void 0 && usage.inputTokens > 0 && estimatedRemoved > 0 && removedFraction > 0 && removedFraction < 0.95;
|
|
2233
|
+
const estimatedSavedUsdTotal = estimateUsable ? (usage.inputTokens / (1 - removedFraction) - usage.inputTokens) * (priced.inputUsd / usage.inputTokens) : 0;
|
|
2234
|
+
for (const change of tokenChanges) {
|
|
2235
|
+
const share = estimateUsable && change.estimatedTokensSaved > 0 ? estimatedSavedUsdTotal * (change.estimatedTokensSaved / estimatedRemoved) : void 0;
|
|
2236
|
+
levers.push({
|
|
2237
|
+
leverId: change.lever,
|
|
2238
|
+
kind: change.lossless ? "lossless" : "lossy",
|
|
2239
|
+
...share === void 0 ? {} : { savedUsd: share, basis: "estimated-baseline" },
|
|
2240
|
+
...config.gateVerdicts?.[change.lever] === void 0 ? {} : { gateVerdict: config.gateVerdicts[change.lever] }
|
|
2241
|
+
});
|
|
2242
|
+
}
|
|
2243
|
+
for (const change of changes) {
|
|
2244
|
+
if (EXECUTION_TIER_LEVERS.has(change.lever) && priced?.tierSavedUsd === void 0) {
|
|
2245
|
+
levers.push({ leverId: change.lever, kind: "lossless" });
|
|
2246
|
+
}
|
|
2247
|
+
}
|
|
2248
|
+
const measuredTokenChanges = changes.filter((change) => !EXECUTION_TIER_LEVERS.has(change.lever) && !CACHE_TTL_LEVERS.has(change.lever));
|
|
2249
|
+
if (baselineUsd !== void 0 && priced !== void 0 && measuredTokenChanges.length > 0) {
|
|
2250
|
+
const lossy = measuredTokenChanges.filter((change) => !change.lossless);
|
|
2251
|
+
const verdicts = lossy.map((change) => config.gateVerdicts?.[change.lever]);
|
|
2252
|
+
const allApproved = verdicts.every((verdict) => verdict?.verdict === "approved");
|
|
2253
|
+
const savedUsd = baselineUsd - priced.actualUsd - (priced.tierSavedUsd ?? 0) - (priced.cacheAttributedUsd ?? 0);
|
|
2254
|
+
levers.push({
|
|
2255
|
+
leverId: MEASURED_COMPOSITE_LEVER,
|
|
2256
|
+
kind: lossy.length > 0 ? "lossy" : "lossless",
|
|
2257
|
+
savedUsd,
|
|
2258
|
+
...lossy.length > 0 && allApproved ? {
|
|
2259
|
+
gateVerdict: {
|
|
2260
|
+
ref: verdicts.map((verdict) => verdict.ref).sort().join("+"),
|
|
2261
|
+
verdict: "approved"
|
|
2262
|
+
}
|
|
2263
|
+
} : {},
|
|
2264
|
+
detail: "baselineUsd minus actual catalogue cost, net of tier and cache components"
|
|
2265
|
+
});
|
|
2266
|
+
}
|
|
2267
|
+
let correlation;
|
|
2268
|
+
try {
|
|
2269
|
+
correlation = config.getCorrelation?.(event);
|
|
2270
|
+
} catch {
|
|
2271
|
+
correlation = void 0;
|
|
2272
|
+
}
|
|
2273
|
+
return {
|
|
2274
|
+
schemaVersion: 1,
|
|
2275
|
+
requestId: event.requestId,
|
|
2276
|
+
applicationId: event.applicationId,
|
|
2277
|
+
...correlation?.taskId === void 0 ? {} : { taskId: correlation.taskId },
|
|
2278
|
+
...correlation?.turnIndex === void 0 ? {} : { turnIndex: correlation.turnIndex },
|
|
2279
|
+
invoicePeriod: event.timestamp.slice(0, 7),
|
|
2280
|
+
timestamp: event.timestamp,
|
|
2281
|
+
provider,
|
|
2282
|
+
model: event.model,
|
|
2283
|
+
executionMode,
|
|
2284
|
+
pricingStatus: pricing.pricingStatus,
|
|
2285
|
+
...pricing.pricingStatus === "unpriced" ? { pricingReason: pricing.reason } : {},
|
|
2286
|
+
catalogueVersion: pinnedCatalogueVersion,
|
|
2287
|
+
...usage === void 0 ? {} : { usage },
|
|
2288
|
+
...priced === void 0 ? {} : { actualUsd: priced.actualUsd },
|
|
2289
|
+
...baselineUsd === void 0 ? {} : { baselineUsd },
|
|
2290
|
+
...event.costUsd === void 0 ? {} : { reportedCostUsd: event.costUsd },
|
|
2291
|
+
levers,
|
|
2292
|
+
...estimateUsable ? {
|
|
2293
|
+
estimatedReduction: {
|
|
2294
|
+
estimatedInputTokensBefore: estimatedBefore,
|
|
2295
|
+
estimatedInputTokensRemoved: estimatedRemoved
|
|
2296
|
+
}
|
|
2297
|
+
} : {},
|
|
2298
|
+
...Object.keys(excluded).length === 0 ? {} : { excluded },
|
|
2299
|
+
...event.outputDiagnostics === void 0 ? {} : { outputDiagnostics: event.outputDiagnostics },
|
|
2300
|
+
...event.duplicateOfRequestId === void 0 ? {} : { duplicateOfRequestId: event.duplicateOfRequestId },
|
|
2301
|
+
status: event.status
|
|
2302
|
+
};
|
|
2303
|
+
}
|
|
2304
|
+
function resumeChainTail(path) {
|
|
2305
|
+
try {
|
|
2306
|
+
if (!existsSync(path)) return { disabled: false };
|
|
2307
|
+
const lines = readFileSync(path, "utf8").split("\n").filter((line) => line.trim() !== "");
|
|
2308
|
+
if (lines.length === 0) return { disabled: false };
|
|
2309
|
+
const tail = JSON.parse(lines[lines.length - 1]);
|
|
2310
|
+
if (typeof tail.seq !== "number" || typeof tail.eventHash !== "string") return { disabled: true };
|
|
2311
|
+
return { previous: tail, disabled: false };
|
|
2312
|
+
} catch {
|
|
2313
|
+
return { disabled: true };
|
|
2314
|
+
}
|
|
2315
|
+
}
|
|
2316
|
+
function createSavingsLedger(config) {
|
|
2317
|
+
if (typeof config.path !== "string" || config.path.trim() === "") {
|
|
2318
|
+
throw new Error("The savings ledger needs a file path for its append-only JSONL spool.");
|
|
2319
|
+
}
|
|
2320
|
+
const catalogue = config.catalogue ?? publishedModelPrices;
|
|
2321
|
+
const pinnedCatalogueVersion = catalogueVersion2(catalogue);
|
|
2322
|
+
const tail = resumeChainTail(config.path);
|
|
2323
|
+
let previous = tail.previous;
|
|
2324
|
+
let disabled = tail.disabled;
|
|
2325
|
+
return {
|
|
2326
|
+
path: config.path,
|
|
2327
|
+
emit(event) {
|
|
2328
|
+
if (disabled) return;
|
|
2329
|
+
try {
|
|
2330
|
+
const billable = billableEventFromRequest(event, config, catalogue, pinnedCatalogueVersion);
|
|
2331
|
+
const record = chainLedgerRecord(billable, previous);
|
|
2332
|
+
mkdirSync(dirname(config.path), { recursive: true, mode: 448 });
|
|
2333
|
+
appendFileSync(config.path, `${JSON.stringify(record)}
|
|
2334
|
+
`, { encoding: "utf8", mode: 384 });
|
|
2335
|
+
previous = record;
|
|
2336
|
+
} catch {
|
|
2337
|
+
disabled = true;
|
|
2338
|
+
}
|
|
2339
|
+
}
|
|
2340
|
+
};
|
|
2341
|
+
}
|
|
2342
|
+
var EXECUTION_TIER_LEVERS, CACHE_TTL_LEVERS, MEASURED_COMPOSITE_LEVER, EXACT_CACHE_LEVER;
|
|
2343
|
+
var init_savings_ledger3 = __esm({
|
|
2344
|
+
"packages/sdk-js/src/savings-ledger.ts"() {
|
|
2345
|
+
"use strict";
|
|
2346
|
+
init_price_catalogue2();
|
|
2347
|
+
init_model_prices();
|
|
2348
|
+
init_savings_ledger2();
|
|
2349
|
+
EXECUTION_TIER_LEVERS = /* @__PURE__ */ new Set(["execution-tier-flex"]);
|
|
2350
|
+
CACHE_TTL_LEVERS = /* @__PURE__ */ new Map([["cache-ttl-5m", "5m"], ["cache-ttl-1h", "1h"]]);
|
|
2351
|
+
MEASURED_COMPOSITE_LEVER = "token-reduction-measured";
|
|
2352
|
+
EXACT_CACHE_LEVER = "exact-response-cache";
|
|
2353
|
+
}
|
|
2354
|
+
});
|
|
2355
|
+
|
|
2356
|
+
// packages/sdk-js/src/proxy.ts
|
|
2357
|
+
import { createServer } from "node:http";
|
|
2358
|
+
import { createHash as createHash5, randomUUID } from "node:crypto";
|
|
2359
|
+
import { appendFileSync as appendFileSync2, mkdirSync as mkdirSync2 } from "node:fs";
|
|
2360
|
+
import { join } from "node:path";
|
|
2361
|
+
function forwardHeaders(req, provider) {
|
|
2362
|
+
const out = {};
|
|
2363
|
+
for (const [name, value] of Object.entries(req.headers)) {
|
|
2364
|
+
if (HOP_HEADERS.has(name.toLowerCase()) || value === void 0) continue;
|
|
2365
|
+
out[name.toLowerCase()] = Array.isArray(value) ? value.join(", ") : value;
|
|
2366
|
+
}
|
|
2367
|
+
delete out["x-mesh-application"];
|
|
2368
|
+
if (provider === "openai") {
|
|
2369
|
+
delete out["x-api-key"];
|
|
2370
|
+
} else if (out["x-api-key"] !== void 0) {
|
|
2371
|
+
delete out.authorization;
|
|
2372
|
+
}
|
|
2373
|
+
out["accept-encoding"] = "identity";
|
|
2374
|
+
return out;
|
|
2375
|
+
}
|
|
2376
|
+
function readBody(req) {
|
|
2377
|
+
return new Promise((resolve3, reject) => {
|
|
2378
|
+
const chunks = [];
|
|
2379
|
+
let received = 0;
|
|
2380
|
+
req.on("data", (chunk) => {
|
|
2381
|
+
received += chunk.byteLength;
|
|
2382
|
+
if (received > BODY_CAP) {
|
|
2383
|
+
reject(new BodyTooLargeError());
|
|
2384
|
+
req.destroy();
|
|
2385
|
+
return;
|
|
2386
|
+
}
|
|
2387
|
+
chunks.push(chunk);
|
|
2388
|
+
});
|
|
2389
|
+
req.on("end", () => resolve3(Buffer.concat(chunks)));
|
|
2390
|
+
req.on("error", reject);
|
|
2391
|
+
});
|
|
2392
|
+
}
|
|
2393
|
+
function hostAllowed(req) {
|
|
2394
|
+
const host = (req.headers.host ?? "").toLowerCase().replace(/:\d+$/, "");
|
|
2395
|
+
return host === "127.0.0.1" || host === "localhost" || host === "[::1]" || host === "";
|
|
2396
|
+
}
|
|
2397
|
+
function surfaceOf(path) {
|
|
2398
|
+
if (path === "/v1/messages") return { provider: "anthropic", upstreamPath: "/v1/messages" };
|
|
2399
|
+
if (path === "/v1/responses") return { provider: "openai", upstreamPath: "/v1/responses" };
|
|
2400
|
+
if (path === "/v1/chat/completions") return { provider: "openai", upstreamPath: "/v1/chat/completions" };
|
|
2401
|
+
return void 0;
|
|
2402
|
+
}
|
|
2403
|
+
function usageFromSse(provider, sseText) {
|
|
2404
|
+
const events = [];
|
|
2405
|
+
for (const line of sseText.split(/\r?\n/)) {
|
|
2406
|
+
if (!line.startsWith("data:")) continue;
|
|
2407
|
+
const payload = line.slice(5).trim();
|
|
2408
|
+
if (payload === "" || payload === "[DONE]") continue;
|
|
2409
|
+
try {
|
|
2410
|
+
const parsed = JSON.parse(payload);
|
|
2411
|
+
if (typeof parsed === "object" && parsed !== null) events.push(parsed);
|
|
2412
|
+
} catch {
|
|
2413
|
+
}
|
|
2414
|
+
}
|
|
2415
|
+
if (events.length === 0) return void 0;
|
|
2416
|
+
if (provider === "anthropic") {
|
|
2417
|
+
const start = events.find((e) => e.type === "message_start");
|
|
2418
|
+
const startUsage = start?.message?.usage;
|
|
2419
|
+
if (startUsage === void 0) return void 0;
|
|
2420
|
+
let outputTokens = startUsage.output_tokens;
|
|
2421
|
+
let tier = start?.message?.service_tier ?? startUsage.service_tier;
|
|
2422
|
+
for (const e of events) {
|
|
2423
|
+
if (e.type !== "message_delta") continue;
|
|
2424
|
+
const deltaUsage = e.usage;
|
|
2425
|
+
if (deltaUsage?.output_tokens !== void 0) outputTokens = deltaUsage.output_tokens;
|
|
2426
|
+
}
|
|
2427
|
+
return {
|
|
2428
|
+
usage: { ...startUsage, output_tokens: outputTokens ?? 0 },
|
|
2429
|
+
...typeof tier === "string" ? { service_tier: tier } : {}
|
|
2430
|
+
};
|
|
2431
|
+
}
|
|
2432
|
+
const completed = [...events].reverse().find((e) => e.type === "response.completed");
|
|
2433
|
+
const completedResponse = completed?.response;
|
|
2434
|
+
if (completedResponse?.usage !== void 0) return completedResponse;
|
|
2435
|
+
const withUsage = [...events].reverse().find((e) => e.usage !== void 0 && e.usage !== null);
|
|
2436
|
+
return withUsage;
|
|
2437
|
+
}
|
|
2438
|
+
function outputTextFromSse(provider, sseText) {
|
|
2439
|
+
const parts = [];
|
|
2440
|
+
for (const line of sseText.split(/\r?\n/)) {
|
|
2441
|
+
if (!line.startsWith("data:")) continue;
|
|
2442
|
+
const payload = line.slice(5).trim();
|
|
2443
|
+
if (payload === "" || payload === "[DONE]") continue;
|
|
2444
|
+
try {
|
|
2445
|
+
const parsed = JSON.parse(payload);
|
|
2446
|
+
if (provider === "anthropic") {
|
|
2447
|
+
const delta = parsed.delta;
|
|
2448
|
+
if (parsed.type === "content_block_delta" && typeof delta?.text === "string") parts.push(delta.text);
|
|
2449
|
+
} else {
|
|
2450
|
+
if (parsed.type === "response.output_text.delta" && typeof parsed.delta === "string") parts.push(parsed.delta);
|
|
2451
|
+
const choices = parsed.choices;
|
|
2452
|
+
const chatDelta = choices?.[0]?.delta;
|
|
2453
|
+
if (typeof chatDelta?.content === "string") parts.push(chatDelta.content);
|
|
2454
|
+
}
|
|
2455
|
+
} catch {
|
|
2456
|
+
}
|
|
2457
|
+
}
|
|
2458
|
+
return parts.join("");
|
|
2459
|
+
}
|
|
2460
|
+
function startMeshProxy(options) {
|
|
2461
|
+
const baseReduction = options.tokenReduction ?? LOSSLESS_DEFAULT;
|
|
2462
|
+
const reduction = baseReduction.cacheTtl !== void 0 && baseReduction.cacheVolatilityGuard === void 0 ? { ...baseReduction, cacheVolatilityGuard: new CachePrefixVolatilityGuard() } : baseReduction;
|
|
2463
|
+
const upstreams = {
|
|
2464
|
+
anthropic: (options.anthropicUpstream ?? "https://api.anthropic.com").replace(/\/$/, ""),
|
|
2465
|
+
openai: (options.openaiUpstream ?? "https://api.openai.com").replace(/\/$/, "")
|
|
2466
|
+
};
|
|
2467
|
+
const ledgers = /* @__PURE__ */ new Map();
|
|
2468
|
+
const LEDGER_CAP = 200;
|
|
2469
|
+
const recentRequestHashes = /* @__PURE__ */ new Map();
|
|
2470
|
+
const RECENT_HASH_CAP = 500;
|
|
2471
|
+
const ledgerFor = (applicationId, provider) => {
|
|
2472
|
+
const safeApp = applicationId.replace(/[^A-Za-z0-9._-]/g, "_").slice(0, 64) || "proxy";
|
|
2473
|
+
const key = `${safeApp}
|
|
2474
|
+
${provider}`;
|
|
2475
|
+
const existing = ledgers.get(key);
|
|
2476
|
+
if (existing !== void 0) {
|
|
2477
|
+
ledgers.delete(key);
|
|
2478
|
+
ledgers.set(key, existing);
|
|
2479
|
+
return existing;
|
|
2480
|
+
}
|
|
2481
|
+
while (ledgers.size >= LEDGER_CAP) {
|
|
2482
|
+
const oldest = ledgers.keys().next().value;
|
|
2483
|
+
if (oldest === void 0) break;
|
|
2484
|
+
ledgers.delete(oldest);
|
|
2485
|
+
}
|
|
2486
|
+
const ledger = createSavingsLedger({
|
|
2487
|
+
path: join(options.ledgerDirectory, `${safeApp}-${provider}.jsonl`),
|
|
2488
|
+
provider
|
|
2489
|
+
});
|
|
2490
|
+
ledgers.set(key, ledger);
|
|
2491
|
+
return ledger;
|
|
2492
|
+
};
|
|
2493
|
+
const blindFeaturesKey = options.blindFeaturesKey;
|
|
2494
|
+
const writeBlindFeatures = (applicationId, provider, record) => {
|
|
2495
|
+
const safeApp = applicationId.replace(/[^A-Za-z0-9._-]/g, "_").slice(0, 64) || "proxy";
|
|
2496
|
+
mkdirSync2(options.ledgerDirectory, { recursive: true, mode: 448 });
|
|
2497
|
+
appendFileSync2(join(options.ledgerDirectory, `${safeApp}-${provider}.features.jsonl`), `${JSON.stringify(record)}
|
|
2498
|
+
`, { mode: 384 });
|
|
2499
|
+
};
|
|
2500
|
+
const handle = async (req, res) => {
|
|
2501
|
+
const path = (req.url ?? "").split("?")[0];
|
|
2502
|
+
if (!hostAllowed(req)) {
|
|
2503
|
+
res.writeHead(403, { "content-type": "application/json" });
|
|
2504
|
+
res.end(JSON.stringify({ error: "MESH proxy serves loopback clients only." }));
|
|
2505
|
+
return;
|
|
2506
|
+
}
|
|
2507
|
+
const fetchSite = req.headers["sec-fetch-site"];
|
|
2508
|
+
if (fetchSite === "cross-site" || fetchSite === "same-site") {
|
|
2509
|
+
res.writeHead(403, { "content-type": "application/json" });
|
|
2510
|
+
res.end(JSON.stringify({ error: "MESH proxy does not serve cross-origin browser requests." }));
|
|
2511
|
+
return;
|
|
2512
|
+
}
|
|
2513
|
+
if (req.method === "GET" && path.startsWith("/v1/")) {
|
|
2514
|
+
const provider = req.headers["x-api-key"] !== void 0 ? "anthropic" : "openai";
|
|
2515
|
+
let upstreamResponse2;
|
|
2516
|
+
try {
|
|
2517
|
+
upstreamResponse2 = await fetch(`${upstreams[provider]}${req.url}`, {
|
|
2518
|
+
headers: forwardHeaders(req, provider),
|
|
2519
|
+
signal: AbortSignal.timeout(6e4)
|
|
2520
|
+
});
|
|
2521
|
+
} catch (cause) {
|
|
2522
|
+
res.writeHead(502, { "content-type": "application/json" });
|
|
2523
|
+
res.end(JSON.stringify({ error: `Upstream unreachable: ${cause.message}` }));
|
|
2524
|
+
return;
|
|
2525
|
+
}
|
|
2526
|
+
const passHeaders = {};
|
|
2527
|
+
upstreamResponse2.headers.forEach((value, name) => {
|
|
2528
|
+
if (!HOP_HEADERS.has(name.toLowerCase())) passHeaders[name] = value;
|
|
2529
|
+
});
|
|
2530
|
+
res.writeHead(upstreamResponse2.status, passHeaders);
|
|
2531
|
+
res.end(Buffer.from(await upstreamResponse2.arrayBuffer()));
|
|
2532
|
+
return;
|
|
2533
|
+
}
|
|
2534
|
+
const surface = req.method === "POST" ? surfaceOf(path) : void 0;
|
|
2535
|
+
if (surface === void 0) {
|
|
2536
|
+
res.writeHead(404, { "content-type": "application/json" });
|
|
2537
|
+
res.end(JSON.stringify({
|
|
2538
|
+
error: "MESH proxy forwards POST /v1/messages (Anthropic), /v1/responses and /v1/chat/completions (OpenAI-compatible). Point your client's base_url at this server."
|
|
2539
|
+
}));
|
|
2540
|
+
return;
|
|
2541
|
+
}
|
|
2542
|
+
let rawBody;
|
|
2543
|
+
try {
|
|
2544
|
+
rawBody = await readBody(req);
|
|
2545
|
+
} catch (cause) {
|
|
2546
|
+
if (cause instanceof BodyTooLargeError && !res.headersSent) {
|
|
2547
|
+
res.writeHead(413, { "content-type": "application/json" });
|
|
2548
|
+
res.end(JSON.stringify({ error: cause.message }));
|
|
2549
|
+
}
|
|
2550
|
+
return;
|
|
2551
|
+
}
|
|
2552
|
+
let body;
|
|
2553
|
+
try {
|
|
2554
|
+
body = JSON.parse(rawBody.toString("utf8"));
|
|
2555
|
+
} catch {
|
|
2556
|
+
body = void 0;
|
|
2557
|
+
}
|
|
2558
|
+
if (body === void 0 || typeof body !== "object") {
|
|
2559
|
+
res.writeHead(400, { "content-type": "application/json" });
|
|
2560
|
+
res.end(JSON.stringify({ error: "The request body is not a JSON object." }));
|
|
2561
|
+
return;
|
|
2562
|
+
}
|
|
2563
|
+
const { request: reducedRequest, summary } = reduceRequest(body, reduction);
|
|
2564
|
+
const sendBody = JSON.stringify(reducedRequest);
|
|
2565
|
+
const applicationIdHeader = req.headers["x-mesh-application"];
|
|
2566
|
+
const applicationId = ((Array.isArray(applicationIdHeader) ? applicationIdHeader[0] : applicationIdHeader) ?? "proxy").slice(0, 128);
|
|
2567
|
+
const headers = forwardHeaders(req, surface.provider);
|
|
2568
|
+
headers["content-type"] = "application/json";
|
|
2569
|
+
const upstreamControl = new AbortController();
|
|
2570
|
+
const abortUpstream = (reason) => upstreamControl.abort(new Error(reason));
|
|
2571
|
+
const onClientClose = () => {
|
|
2572
|
+
if (!res.writableEnded) abortUpstream("client disconnected");
|
|
2573
|
+
};
|
|
2574
|
+
res.on("close", onClientClose);
|
|
2575
|
+
const HEADER_TIMEOUT_MS = 12e4;
|
|
2576
|
+
const IDLE_TIMEOUT_MS = 12e4;
|
|
2577
|
+
let phaseTimer = setTimeout(() => abortUpstream("upstream produced no response headers within 120s"), HEADER_TIMEOUT_MS);
|
|
2578
|
+
const requestId = randomUUID();
|
|
2579
|
+
const bodyHash = createHash5("sha256").update(applicationId).update("\n").update(rawBody).digest("hex");
|
|
2580
|
+
const duplicateOfRequestId = recentRequestHashes.get(bodyHash);
|
|
2581
|
+
if (duplicateOfRequestId === void 0) {
|
|
2582
|
+
while (recentRequestHashes.size >= RECENT_HASH_CAP) {
|
|
2583
|
+
const oldest = recentRequestHashes.keys().next().value;
|
|
2584
|
+
if (oldest === void 0) break;
|
|
2585
|
+
recentRequestHashes.delete(oldest);
|
|
2586
|
+
}
|
|
2587
|
+
recentRequestHashes.set(bodyHash, requestId);
|
|
2588
|
+
}
|
|
2589
|
+
const startedAt = Date.now();
|
|
2590
|
+
let upstreamResponse;
|
|
2591
|
+
try {
|
|
2592
|
+
upstreamResponse = await fetch(`${upstreams[surface.provider]}${surface.upstreamPath}`, {
|
|
2593
|
+
method: "POST",
|
|
2594
|
+
headers,
|
|
2595
|
+
body: sendBody,
|
|
2596
|
+
signal: upstreamControl.signal
|
|
2597
|
+
});
|
|
2598
|
+
} catch (cause) {
|
|
2599
|
+
clearTimeout(phaseTimer);
|
|
2600
|
+
if (!res.headersSent && !res.destroyed) {
|
|
2601
|
+
res.writeHead(502, { "content-type": "application/json" });
|
|
2602
|
+
res.end(JSON.stringify({ error: `Upstream unreachable: ${cause.message}` }));
|
|
2603
|
+
}
|
|
2604
|
+
return;
|
|
2605
|
+
}
|
|
2606
|
+
clearTimeout(phaseTimer);
|
|
2607
|
+
const responseHeaders = {};
|
|
2608
|
+
upstreamResponse.headers.forEach((value, name) => {
|
|
2609
|
+
if (!HOP_HEADERS.has(name.toLowerCase())) responseHeaders[name] = value;
|
|
2610
|
+
});
|
|
2611
|
+
if (res.destroyed) {
|
|
2612
|
+
abortUpstream("client disconnected");
|
|
2613
|
+
return;
|
|
2614
|
+
}
|
|
2615
|
+
res.writeHead(upstreamResponse.status, responseHeaders);
|
|
2616
|
+
const chunks = [];
|
|
2617
|
+
let accumulated = 0;
|
|
2618
|
+
const ACCUMULATION_CAP = 32 * 1024 * 1024;
|
|
2619
|
+
let clientLost = false;
|
|
2620
|
+
if (upstreamResponse.body !== null) {
|
|
2621
|
+
const reader = upstreamResponse.body.getReader();
|
|
2622
|
+
try {
|
|
2623
|
+
for (; ; ) {
|
|
2624
|
+
phaseTimer = setTimeout(() => abortUpstream("upstream stream idle for 120s"), IDLE_TIMEOUT_MS);
|
|
2625
|
+
let done;
|
|
2626
|
+
let value;
|
|
2627
|
+
try {
|
|
2628
|
+
({ done, value } = await reader.read());
|
|
2629
|
+
} finally {
|
|
2630
|
+
clearTimeout(phaseTimer);
|
|
2631
|
+
}
|
|
2632
|
+
if (done) break;
|
|
2633
|
+
if (res.destroyed) {
|
|
2634
|
+
clientLost = true;
|
|
2635
|
+
abortUpstream("client disconnected");
|
|
2636
|
+
break;
|
|
2637
|
+
}
|
|
2638
|
+
const chunk = Buffer.from(value);
|
|
2639
|
+
const flushed = res.write(chunk);
|
|
2640
|
+
if (accumulated < ACCUMULATION_CAP) {
|
|
2641
|
+
chunks.push(chunk);
|
|
2642
|
+
accumulated += chunk.byteLength;
|
|
2643
|
+
}
|
|
2644
|
+
if (!flushed) {
|
|
2645
|
+
await new Promise((proceed) => {
|
|
2646
|
+
const onDrain = () => {
|
|
2647
|
+
res.off("close", onGone);
|
|
2648
|
+
proceed();
|
|
2649
|
+
};
|
|
2650
|
+
const onGone = () => {
|
|
2651
|
+
res.off("drain", onDrain);
|
|
2652
|
+
proceed();
|
|
2653
|
+
};
|
|
2654
|
+
res.once("drain", onDrain);
|
|
2655
|
+
res.once("close", onGone);
|
|
2656
|
+
});
|
|
2657
|
+
}
|
|
2658
|
+
}
|
|
2659
|
+
} catch {
|
|
2660
|
+
clientLost = res.destroyed;
|
|
2661
|
+
}
|
|
2662
|
+
}
|
|
2663
|
+
res.off("close", onClientClose);
|
|
2664
|
+
if (!clientLost && !res.destroyed) res.end();
|
|
2665
|
+
try {
|
|
2666
|
+
const text = Buffer.concat(chunks).toString("utf8");
|
|
2667
|
+
const contentType = upstreamResponse.headers.get("content-type") ?? "";
|
|
2668
|
+
if (process.env.MESH_PROXY_DEBUG === "1" && !upstreamResponse.ok) {
|
|
2669
|
+
console.warn(
|
|
2670
|
+
`[mesh-debug] upstream ${upstreamResponse.status} model=${JSON.stringify(body.model)} path=${path} anthropic-beta=${JSON.stringify(req.headers["anthropic-beta"] ?? null)} errbody=${text.slice(0, 500)}`
|
|
2671
|
+
);
|
|
2672
|
+
}
|
|
2673
|
+
let responseShaped;
|
|
2674
|
+
if (contentType.includes("text/event-stream")) {
|
|
2675
|
+
responseShaped = usageFromSse(surface.provider, text);
|
|
2676
|
+
} else {
|
|
2677
|
+
try {
|
|
2678
|
+
responseShaped = JSON.parse(text);
|
|
2679
|
+
} catch {
|
|
2680
|
+
responseShaped = void 0;
|
|
2681
|
+
}
|
|
2682
|
+
}
|
|
2683
|
+
const usageResult = responseShaped === void 0 ? void 0 : normalizeProviderUsage(responseShaped);
|
|
2684
|
+
const usage = usageResult?.status === "ok" ? usageResult.usage : void 0;
|
|
2685
|
+
const tier = responseShaped === void 0 ? void 0 : responseServiceTier(responseShaped);
|
|
2686
|
+
let outputDiagnostics;
|
|
2687
|
+
if (usage !== void 0 && usage.outputTokens >= 200 && responseShaped !== void 0) {
|
|
2688
|
+
const outputText = contentType.includes("text/event-stream") ? outputTextFromSse(surface.provider, text) : responseText(responseShaped);
|
|
2689
|
+
if (outputText.length > 0) {
|
|
2690
|
+
outputDiagnostics = measureOutputReEmission(collectRequestText(body), outputText);
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2693
|
+
const event = {
|
|
2694
|
+
requestId,
|
|
2695
|
+
applicationId,
|
|
2696
|
+
provider: `${surface.provider}-compatible`,
|
|
2697
|
+
model: typeof body.model === "string" ? body.model : "unknown",
|
|
2698
|
+
latencyMs: Date.now() - startedAt,
|
|
2699
|
+
status: upstreamResponse.ok ? "succeeded" : "failed",
|
|
2700
|
+
timestamp: new Date(startedAt).toISOString(),
|
|
2701
|
+
...summary.skipped ? {} : { reduction: summary },
|
|
2702
|
+
...usage === void 0 ? {} : {
|
|
2703
|
+
inputTokens: usage.inputTokens,
|
|
2704
|
+
outputTokens: usage.outputTokens,
|
|
2705
|
+
uncachedInputTokens: usage.uncachedInputTokens,
|
|
2706
|
+
cachedInputTokens: usage.cachedInputTokens,
|
|
2707
|
+
cacheWriteInputTokens: usage.cacheWriteInputTokens,
|
|
2708
|
+
...usage.cacheWrite5mInputTokens === void 0 ? {} : {
|
|
2709
|
+
cacheWrite5mInputTokens: usage.cacheWrite5mInputTokens,
|
|
2710
|
+
cacheWrite1hInputTokens: usage.cacheWrite1hInputTokens ?? 0
|
|
2711
|
+
}
|
|
2712
|
+
},
|
|
2713
|
+
...tier === void 0 ? {} : { responseServiceTier: tier },
|
|
2714
|
+
...outputDiagnostics === void 0 ? {} : { outputDiagnostics },
|
|
2715
|
+
...duplicateOfRequestId === void 0 ? {} : { duplicateOfRequestId }
|
|
2716
|
+
};
|
|
2717
|
+
ledgerFor(applicationId, surface.provider).emit(event);
|
|
2718
|
+
if (blindFeaturesKey !== void 0) {
|
|
2719
|
+
const features = extractBlindFeatures(body, { hmacKey: blindFeaturesKey });
|
|
2720
|
+
writeBlindFeatures(applicationId, surface.provider, {
|
|
2721
|
+
requestId,
|
|
2722
|
+
applicationId,
|
|
2723
|
+
timestamp: event.timestamp,
|
|
2724
|
+
features
|
|
2725
|
+
});
|
|
2726
|
+
}
|
|
2727
|
+
} catch {
|
|
2728
|
+
}
|
|
2729
|
+
};
|
|
2730
|
+
const server = createServer((req, res) => {
|
|
2731
|
+
handle(req, res).catch(() => {
|
|
2732
|
+
if (!res.headersSent) {
|
|
2733
|
+
res.writeHead(500, { "content-type": "application/json" });
|
|
2734
|
+
res.end(JSON.stringify({ error: "MESH proxy internal error." }));
|
|
2735
|
+
} else {
|
|
2736
|
+
res.end();
|
|
2737
|
+
}
|
|
2738
|
+
});
|
|
2739
|
+
});
|
|
2740
|
+
server.maxConnections = 256;
|
|
2741
|
+
return new Promise((resolve3, reject) => {
|
|
2742
|
+
server.once("error", reject);
|
|
2743
|
+
server.listen(options.port ?? 0, "127.0.0.1", () => {
|
|
2744
|
+
const address = server.address();
|
|
2745
|
+
const port = typeof address === "object" && address !== null ? address.port : options.port ?? 0;
|
|
2746
|
+
resolve3({
|
|
2747
|
+
port,
|
|
2748
|
+
url: `http://127.0.0.1:${port}`,
|
|
2749
|
+
close: () => new Promise((done, fail) => server.close((err) => err ? fail(err) : done()))
|
|
2750
|
+
});
|
|
2751
|
+
});
|
|
2752
|
+
});
|
|
2753
|
+
}
|
|
2754
|
+
var LOSSLESS_DEFAULT, HOP_HEADERS, BODY_CAP, BodyTooLargeError;
|
|
2755
|
+
var init_proxy = __esm({
|
|
2756
|
+
"packages/sdk-js/src/proxy.ts"() {
|
|
2757
|
+
"use strict";
|
|
2758
|
+
init_output_diagnostics();
|
|
2759
|
+
init_provider_usage();
|
|
2760
|
+
init_replay_gate();
|
|
2761
|
+
init_cache_volatility_guard();
|
|
2762
|
+
init_reduce_request();
|
|
2763
|
+
init_blind_features();
|
|
2764
|
+
init_savings_ledger3();
|
|
2765
|
+
LOSSLESS_DEFAULT = {
|
|
2766
|
+
toolSchemas: true,
|
|
2767
|
+
duplicateContext: true,
|
|
2768
|
+
repairCachePrefix: true
|
|
2769
|
+
};
|
|
2770
|
+
HOP_HEADERS = /* @__PURE__ */ new Set([
|
|
2771
|
+
"host",
|
|
2772
|
+
"content-length",
|
|
2773
|
+
"connection",
|
|
2774
|
+
"accept-encoding",
|
|
2775
|
+
"transfer-encoding",
|
|
2776
|
+
"upgrade",
|
|
2777
|
+
"te",
|
|
2778
|
+
"trailer",
|
|
2779
|
+
"keep-alive",
|
|
2780
|
+
"proxy-authorization"
|
|
2781
|
+
]);
|
|
2782
|
+
BODY_CAP = 64 * 1024 * 1024;
|
|
2783
|
+
BodyTooLargeError = class extends Error {
|
|
2784
|
+
constructor() {
|
|
2785
|
+
super(`Request body exceeds the ${BODY_CAP / (1024 * 1024)} MB cap.`);
|
|
2786
|
+
}
|
|
2787
|
+
};
|
|
2788
|
+
}
|
|
2789
|
+
});
|
|
2790
|
+
|
|
2791
|
+
// packages/sdk-js/src/theo-policy.ts
|
|
2792
|
+
function proofClearsCeiling(lever, ceilingPercent) {
|
|
2793
|
+
if (lever.gate === void 0) return false;
|
|
2794
|
+
const y = lever.gate.qualityDeltaPercent;
|
|
2795
|
+
return Number.isFinite(y) && Math.abs(y) <= ceilingPercent;
|
|
2796
|
+
}
|
|
2797
|
+
function applyLever(config, lever) {
|
|
2798
|
+
const p = lever.params ?? {};
|
|
2799
|
+
switch (lever.lever) {
|
|
2800
|
+
case "tool-schema-compaction":
|
|
2801
|
+
if (config.toolSchemas === void 0) config.toolSchemas = true;
|
|
2802
|
+
return;
|
|
2803
|
+
case "tool-schema-description-trim":
|
|
2804
|
+
config.toolSchemas = {
|
|
2805
|
+
descriptions: "truncate",
|
|
2806
|
+
...typeof p.maximumDescriptionTokens === "number" ? { maximumDescriptionTokens: p.maximumDescriptionTokens } : {}
|
|
2807
|
+
};
|
|
2808
|
+
return;
|
|
2809
|
+
case "duplicate-context-removal":
|
|
2810
|
+
config.duplicateContext = true;
|
|
2811
|
+
return;
|
|
2812
|
+
case "cache-prefix-repair":
|
|
2813
|
+
config.repairCachePrefix = true;
|
|
2814
|
+
return;
|
|
2815
|
+
case "stale-history-compaction":
|
|
2816
|
+
config.historyCompaction = true;
|
|
2817
|
+
return;
|
|
2818
|
+
case "cross-turn-memory-dedup":
|
|
2819
|
+
config.crossTurnMemoryDedup = true;
|
|
2820
|
+
return;
|
|
2821
|
+
case "fresh-tool-result-cap":
|
|
2822
|
+
config.freshToolResultCap = typeof p.maximumTokens === "number" ? { maximumTokens: p.maximumTokens } : true;
|
|
2823
|
+
return;
|
|
2824
|
+
case "output-cap":
|
|
2825
|
+
if (typeof p.maxOutputTokens === "number") config.maxOutputTokens = p.maxOutputTokens;
|
|
2826
|
+
return;
|
|
2827
|
+
case "execution-tier-flex":
|
|
2828
|
+
config.executionTier = "flex";
|
|
2829
|
+
return;
|
|
2830
|
+
case "cache-ttl-5m":
|
|
2831
|
+
config.cacheTtl = "5m";
|
|
2832
|
+
return;
|
|
2833
|
+
case "cache-ttl-1h":
|
|
2834
|
+
config.cacheTtl = "1h";
|
|
2835
|
+
return;
|
|
2836
|
+
case "output-effort-low":
|
|
2837
|
+
case "output-effort-medium":
|
|
2838
|
+
case "output-effort-high":
|
|
2839
|
+
case "output-effort-xhigh":
|
|
2840
|
+
case "output-effort-max":
|
|
2841
|
+
config.outputEffort = lever.lever.replace("output-effort-", "");
|
|
2842
|
+
return;
|
|
2843
|
+
default:
|
|
2844
|
+
return;
|
|
2845
|
+
}
|
|
2846
|
+
}
|
|
2847
|
+
function policyToReductionConfig(policy) {
|
|
2848
|
+
const config = {};
|
|
2849
|
+
for (const lever of policy.levers) {
|
|
2850
|
+
if (lever.class === "lossy" && !proofClearsCeiling(lever, policy.qualityCeilingPercent)) continue;
|
|
2851
|
+
applyLever(config, lever);
|
|
2852
|
+
}
|
|
2853
|
+
return config;
|
|
2854
|
+
}
|
|
2855
|
+
function withheldLevers(policy) {
|
|
2856
|
+
const out = [];
|
|
2857
|
+
for (const lever of policy.levers) {
|
|
2858
|
+
if (lever.class !== "lossy") continue;
|
|
2859
|
+
if (lever.gate === void 0) {
|
|
2860
|
+
out.push({ lever: lever.lever, reason: "no gate proof yet \u2014 queued for the live gate" });
|
|
2861
|
+
} else if (!proofClearsCeiling(lever, policy.qualityCeilingPercent)) {
|
|
2862
|
+
out.push({
|
|
2863
|
+
lever: lever.lever,
|
|
2864
|
+
reason: `measured quality drop ${Math.abs(lever.gate.qualityDeltaPercent).toFixed(2)}% exceeds the ${policy.qualityCeilingPercent}% ceiling for this route`
|
|
2865
|
+
});
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
return out;
|
|
2869
|
+
}
|
|
2870
|
+
function defaultPolicy(user, at) {
|
|
2871
|
+
return {
|
|
2872
|
+
schemaVersion: 1,
|
|
2873
|
+
user,
|
|
2874
|
+
version: 1,
|
|
2875
|
+
qualityCeilingPercent: 2,
|
|
2876
|
+
// strict by default; loosened only by the user's revealed preference
|
|
2877
|
+
levers: [
|
|
2878
|
+
{ lever: "tool-schema-compaction", class: "lossless" },
|
|
2879
|
+
{ lever: "duplicate-context-removal", class: "lossless" },
|
|
2880
|
+
{ lever: "cache-prefix-repair", class: "lossless" }
|
|
2881
|
+
],
|
|
2882
|
+
updatedAt: at
|
|
2883
|
+
};
|
|
2884
|
+
}
|
|
2885
|
+
function policyCoverage(policy) {
|
|
2886
|
+
const byId = new Map(policy.levers.map((lever) => [lever.lever, lever]));
|
|
2887
|
+
const heldById = new Map(withheldLevers(policy).map((withheld) => [withheld.lever, withheld.reason]));
|
|
2888
|
+
const stands = ENFORCEABLE_LEVERS.map((group) => {
|
|
2889
|
+
let state = "available";
|
|
2890
|
+
let reason;
|
|
2891
|
+
let leverClass;
|
|
2892
|
+
for (const member of group.members) {
|
|
2893
|
+
const lever = byId.get(member);
|
|
2894
|
+
if (lever === void 0) continue;
|
|
2895
|
+
leverClass = lever.class;
|
|
2896
|
+
const enforced = lever.class === "lossless" || proofClearsCeiling(lever, policy.qualityCeilingPercent);
|
|
2897
|
+
if (enforced) {
|
|
2898
|
+
state = "active";
|
|
2899
|
+
reason = void 0;
|
|
2900
|
+
break;
|
|
2901
|
+
}
|
|
2902
|
+
state = "held";
|
|
2903
|
+
reason = heldById.get(member);
|
|
2904
|
+
}
|
|
2905
|
+
return {
|
|
2906
|
+
lever: group.id,
|
|
2907
|
+
label: group.label,
|
|
2908
|
+
state,
|
|
2909
|
+
...leverClass ? { class: leverClass } : {},
|
|
2910
|
+
...reason ? { reason } : {}
|
|
2911
|
+
};
|
|
2912
|
+
});
|
|
2913
|
+
const activeCount = stands.filter((stand) => stand.state === "active").length;
|
|
2914
|
+
const heldCount = stands.filter((stand) => stand.state === "held").length;
|
|
2915
|
+
const availableCount = stands.filter((stand) => stand.state === "available").length;
|
|
2916
|
+
const enforceableCount = ENFORCEABLE_LEVERS.length;
|
|
2917
|
+
return {
|
|
2918
|
+
enforceableCount,
|
|
2919
|
+
activeCount,
|
|
2920
|
+
heldCount,
|
|
2921
|
+
availableCount,
|
|
2922
|
+
activeCoveragePercent: enforceableCount === 0 ? 0 : activeCount / enforceableCount * 100,
|
|
2923
|
+
stands
|
|
2924
|
+
};
|
|
2925
|
+
}
|
|
2926
|
+
var ENFORCEABLE_LEVERS;
|
|
2927
|
+
var init_theo_policy = __esm({
|
|
2928
|
+
"packages/sdk-js/src/theo-policy.ts"() {
|
|
2929
|
+
"use strict";
|
|
2930
|
+
ENFORCEABLE_LEVERS = [
|
|
2931
|
+
{ id: "tool-schema-compaction", label: "Tool-schema compaction", members: ["tool-schema-compaction"] },
|
|
2932
|
+
{ id: "tool-schema-description-trim", label: "Tool-schema description trim", members: ["tool-schema-description-trim"] },
|
|
2933
|
+
{ id: "duplicate-context-removal", label: "Duplicate-context removal", members: ["duplicate-context-removal"] },
|
|
2934
|
+
{ id: "cache-prefix-repair", label: "Cache-prefix repair", members: ["cache-prefix-repair"] },
|
|
2935
|
+
{ id: "stale-history-compaction", label: "Stale-history compaction", members: ["stale-history-compaction"] },
|
|
2936
|
+
{ id: "cross-turn-memory-dedup", label: "Cross-turn memory dedup", members: ["cross-turn-memory-dedup"] },
|
|
2937
|
+
{ id: "fresh-tool-result-cap", label: "Fresh tool-result cap", members: ["fresh-tool-result-cap"] },
|
|
2938
|
+
{ id: "output-cap", label: "Output cap", members: ["output-cap"] },
|
|
2939
|
+
{ id: "execution-tier-flex", label: "Execution-tier flex", members: ["execution-tier-flex"] },
|
|
2940
|
+
{ id: "cache-ttl", label: "Cache TTL", members: ["cache-ttl-5m", "cache-ttl-1h"] },
|
|
2941
|
+
{
|
|
2942
|
+
id: "output-effort",
|
|
2943
|
+
label: "Output effort",
|
|
2944
|
+
members: ["output-effort-low", "output-effort-medium", "output-effort-high", "output-effort-xhigh", "output-effort-max"]
|
|
2945
|
+
}
|
|
2946
|
+
];
|
|
2947
|
+
}
|
|
2948
|
+
});
|
|
2949
|
+
|
|
2950
|
+
// packages/cli/src/update.ts
|
|
2951
|
+
var update_exports = {};
|
|
2952
|
+
__export(update_exports, {
|
|
2953
|
+
pullNewerRuntime: () => pullNewerRuntime,
|
|
2954
|
+
runUpdate: () => runUpdate
|
|
2955
|
+
});
|
|
2956
|
+
import { createHash as createHash6 } from "node:crypto";
|
|
2957
|
+
import { copyFileSync, existsSync as existsSync2, mkdirSync as mkdirSync3, readFileSync as readFileSync2, writeFileSync } from "node:fs";
|
|
2958
|
+
import { homedir } from "node:os";
|
|
2959
|
+
import { join as join2 } from "node:path";
|
|
2960
|
+
function read(path) {
|
|
2961
|
+
try {
|
|
2962
|
+
return readFileSync2(path, "utf8").trim();
|
|
2963
|
+
} catch {
|
|
2964
|
+
return null;
|
|
2965
|
+
}
|
|
2966
|
+
}
|
|
2967
|
+
async function pullNewerRuntime() {
|
|
2968
|
+
mkdirSync3(DIR, { recursive: true });
|
|
2969
|
+
const have = existsSync2(VERSION_FILE) ? read(VERSION_FILE) : null;
|
|
2970
|
+
let manifest;
|
|
2971
|
+
try {
|
|
2972
|
+
const res = await fetch(`${BASE}/mesh-version.json`);
|
|
2973
|
+
if (!res.ok) return null;
|
|
2974
|
+
manifest = await res.json();
|
|
2975
|
+
} catch {
|
|
2976
|
+
return null;
|
|
2977
|
+
}
|
|
2978
|
+
if (have === manifest.version) return null;
|
|
2979
|
+
let bytes;
|
|
2980
|
+
try {
|
|
2981
|
+
const res = await fetch(`${BASE}/${manifest.runtime ?? "mesh-runtime"}`);
|
|
2982
|
+
if (!res.ok) return null;
|
|
2983
|
+
bytes = Buffer.from(await res.arrayBuffer());
|
|
2984
|
+
} catch {
|
|
2985
|
+
return null;
|
|
2986
|
+
}
|
|
2987
|
+
if (createHash6("sha256").update(bytes).digest("hex") !== manifest.sha256) return null;
|
|
2988
|
+
if (existsSync2(RUNTIME)) {
|
|
2989
|
+
copyFileSync(RUNTIME, BACKUP);
|
|
2990
|
+
if (have !== null) writeFileSync(BACKUP_VERSION, have);
|
|
2991
|
+
if (existsSync2(SHA_FILE)) copyFileSync(SHA_FILE, BACKUP_SHA);
|
|
2992
|
+
}
|
|
2993
|
+
writeFileSync(RUNTIME, bytes);
|
|
2994
|
+
writeFileSync(VERSION_FILE, manifest.version);
|
|
2995
|
+
writeFileSync(SHA_FILE, manifest.sha256);
|
|
2996
|
+
return manifest.version;
|
|
2997
|
+
}
|
|
2998
|
+
function rollback() {
|
|
2999
|
+
if (!existsSync2(BACKUP)) {
|
|
3000
|
+
console.error("No previous runtime to roll back to.");
|
|
3001
|
+
process.exit(1);
|
|
3002
|
+
}
|
|
3003
|
+
copyFileSync(BACKUP, RUNTIME);
|
|
3004
|
+
const prev = read(BACKUP_VERSION) ?? "previous";
|
|
3005
|
+
writeFileSync(VERSION_FILE, prev);
|
|
3006
|
+
if (existsSync2(BACKUP_SHA)) copyFileSync(BACKUP_SHA, SHA_FILE);
|
|
3007
|
+
console.log(`Rolled back to ${prev}.`);
|
|
3008
|
+
}
|
|
3009
|
+
async function runUpdate(argv) {
|
|
3010
|
+
if (argv.includes("--rollback")) {
|
|
3011
|
+
rollback();
|
|
3012
|
+
return;
|
|
3013
|
+
}
|
|
3014
|
+
mkdirSync3(DIR, { recursive: true });
|
|
3015
|
+
const have = existsSync2(VERSION_FILE) ? read(VERSION_FILE) : null;
|
|
3016
|
+
let manifest;
|
|
3017
|
+
try {
|
|
3018
|
+
const res = await fetch(`${BASE}/mesh-version.json`);
|
|
3019
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
3020
|
+
manifest = await res.json();
|
|
3021
|
+
} catch (error) {
|
|
3022
|
+
console.error(`Couldn't reach ${BASE} to check for updates (${String(error)}).`);
|
|
3023
|
+
process.exit(1);
|
|
3024
|
+
}
|
|
3025
|
+
if (have === manifest.version) {
|
|
3026
|
+
console.log(`Already up to date (${have}).`);
|
|
3027
|
+
return;
|
|
3028
|
+
}
|
|
3029
|
+
process.stdout.write(`Updating ${have ?? "(none)"} \u2192 ${manifest.version}\u2026 `);
|
|
3030
|
+
let bytes;
|
|
3031
|
+
try {
|
|
3032
|
+
const res = await fetch(`${BASE}/${manifest.runtime ?? "mesh-runtime"}`);
|
|
3033
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
3034
|
+
bytes = Buffer.from(await res.arrayBuffer());
|
|
3035
|
+
} catch (error) {
|
|
3036
|
+
console.log("failed");
|
|
3037
|
+
console.error(String(error));
|
|
3038
|
+
process.exit(1);
|
|
3039
|
+
}
|
|
3040
|
+
if (createHash6("sha256").update(bytes).digest("hex") !== manifest.sha256) {
|
|
3041
|
+
console.log("failed");
|
|
3042
|
+
console.error("The downloaded runtime failed its checksum \u2014 refusing to install it.");
|
|
3043
|
+
process.exit(1);
|
|
3044
|
+
}
|
|
3045
|
+
if (existsSync2(RUNTIME)) {
|
|
3046
|
+
copyFileSync(RUNTIME, BACKUP);
|
|
3047
|
+
if (have !== null) writeFileSync(BACKUP_VERSION, have);
|
|
3048
|
+
if (existsSync2(SHA_FILE)) copyFileSync(SHA_FILE, BACKUP_SHA);
|
|
3049
|
+
}
|
|
3050
|
+
writeFileSync(RUNTIME, bytes);
|
|
3051
|
+
writeFileSync(VERSION_FILE, manifest.version);
|
|
3052
|
+
writeFileSync(SHA_FILE, manifest.sha256);
|
|
3053
|
+
console.log("done.");
|
|
3054
|
+
console.log(`Now on ${manifest.version}. Roll back with: mesh update --rollback`);
|
|
3055
|
+
}
|
|
3056
|
+
var BASE, DIR, RUNTIME, VERSION_FILE, SHA_FILE, BACKUP, BACKUP_VERSION, BACKUP_SHA;
|
|
3057
|
+
var init_update = __esm({
|
|
3058
|
+
"packages/cli/src/update.ts"() {
|
|
3059
|
+
BASE = (process.env.MESH_BASE_URL ?? "https://www.mesh-technology.com").replace(/\/$/, "");
|
|
3060
|
+
DIR = join2(homedir(), ".mesh", "runtime");
|
|
3061
|
+
RUNTIME = join2(DIR, "mesh-runtime.mjs");
|
|
3062
|
+
VERSION_FILE = join2(DIR, "version");
|
|
3063
|
+
SHA_FILE = join2(DIR, "sha256");
|
|
3064
|
+
BACKUP = join2(DIR, "mesh-runtime.previous.mjs");
|
|
3065
|
+
BACKUP_VERSION = join2(DIR, "version.previous");
|
|
3066
|
+
BACKUP_SHA = join2(DIR, "sha256.previous");
|
|
3067
|
+
}
|
|
3068
|
+
});
|
|
3069
|
+
|
|
3070
|
+
// packages/cli/src/start.ts
|
|
3071
|
+
var start_exports = {};
|
|
3072
|
+
__export(start_exports, {
|
|
3073
|
+
BUILD_COMMIT: () => BUILD_COMMIT,
|
|
3074
|
+
CLAUDE_SETTINGS: () => CLAUDE_SETTINGS,
|
|
3075
|
+
DEFAULT_PORT: () => DEFAULT_PORT,
|
|
3076
|
+
MESH_HOME: () => MESH_HOME,
|
|
3077
|
+
readLedger: () => readLedger,
|
|
3078
|
+
readPolicy: () => readPolicy,
|
|
3079
|
+
runServe: () => runServe,
|
|
3080
|
+
runStart: () => runStart,
|
|
3081
|
+
startProxyWithPolicy: () => startProxyWithPolicy,
|
|
3082
|
+
summariseLedger: () => summariseLedger
|
|
3083
|
+
});
|
|
3084
|
+
import { randomBytes } from "node:crypto";
|
|
3085
|
+
import {
|
|
3086
|
+
existsSync as existsSync3,
|
|
3087
|
+
mkdirSync as mkdirSync4,
|
|
3088
|
+
readFileSync as readFileSync3,
|
|
3089
|
+
readdirSync,
|
|
3090
|
+
writeFileSync as writeFileSync2
|
|
3091
|
+
} from "node:fs";
|
|
3092
|
+
import { homedir as homedir2 } from "node:os";
|
|
3093
|
+
import { join as join3 } from "node:path";
|
|
3094
|
+
function readPolicy() {
|
|
3095
|
+
const path = join3(MESH_HOME, "theo-policy.json");
|
|
3096
|
+
if (!existsSync3(path)) return defaultPolicy("local", (/* @__PURE__ */ new Date()).toISOString());
|
|
3097
|
+
try {
|
|
3098
|
+
return JSON.parse(readFileSync3(path, "utf8"));
|
|
3099
|
+
} catch {
|
|
3100
|
+
return defaultPolicy("local", (/* @__PURE__ */ new Date()).toISOString());
|
|
3101
|
+
}
|
|
3102
|
+
}
|
|
3103
|
+
function parseStart(argv) {
|
|
3104
|
+
const args = {
|
|
3105
|
+
port: 8787,
|
|
3106
|
+
ledgerDir: join3(MESH_HOME, "ledger"),
|
|
3107
|
+
install: true,
|
|
3108
|
+
// Env defaults so the background service (mesh serve) can be pointed at an
|
|
3109
|
+
// OpenAI-compatible provider too, not just the foreground command.
|
|
3110
|
+
anthropicUpstream: process.env.MESH_ANTHROPIC_UPSTREAM,
|
|
3111
|
+
openaiUpstream: process.env.MESH_OPENAI_UPSTREAM
|
|
3112
|
+
};
|
|
3113
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
3114
|
+
const key = argv[i];
|
|
3115
|
+
if (key === "--help" || key === "-h") {
|
|
3116
|
+
console.log(
|
|
3117
|
+
`Usage: mesh start [options]
|
|
3118
|
+
|
|
3119
|
+
--port <n> Port for the local proxy (default 8787).
|
|
3120
|
+
--ledger <dir> Where to write the hash-chained ledger (default ~/.mesh/ledger).
|
|
3121
|
+
--openai-base <url> Send OpenAI-style traffic to this host instead of api.openai.com \u2014
|
|
3122
|
+
any OpenAI-compatible provider (Grok, DeepSeek, Mistral, Llama, \u2026).
|
|
3123
|
+
--anthropic-base <url> Send Anthropic traffic to this host instead of api.anthropic.com.
|
|
3124
|
+
--no-install Don't touch ~/.claude/settings.json; just run the proxy.
|
|
3125
|
+
--help Show this message.`
|
|
3126
|
+
);
|
|
3127
|
+
process.exit(0);
|
|
3128
|
+
}
|
|
3129
|
+
if (key === "--no-install") {
|
|
3130
|
+
args.install = false;
|
|
3131
|
+
continue;
|
|
3132
|
+
}
|
|
3133
|
+
const value = argv[i + 1];
|
|
3134
|
+
if (value === void 0 || value.startsWith("--")) throw new Error(`${key} needs a value.`);
|
|
3135
|
+
i += 1;
|
|
3136
|
+
if (key === "--port") args.port = Number.parseInt(value, 10);
|
|
3137
|
+
else if (key === "--ledger") args.ledgerDir = value;
|
|
3138
|
+
else if (key === "--openai-base") args.openaiUpstream = value;
|
|
3139
|
+
else if (key === "--anthropic-base") args.anthropicUpstream = value;
|
|
3140
|
+
else throw new Error(`Unknown option: ${key}. See mesh start --help.`);
|
|
3141
|
+
}
|
|
3142
|
+
if (!Number.isInteger(args.port) || args.port < 0 || args.port > 65535) throw new Error("--port must be 0-65535.");
|
|
3143
|
+
for (const url of [args.anthropicUpstream, args.openaiUpstream]) {
|
|
3144
|
+
if (url !== void 0 && !/^https?:\/\//.test(url)) throw new Error(`Upstream must be an http(s) URL: ${url}`);
|
|
3145
|
+
}
|
|
3146
|
+
return args;
|
|
3147
|
+
}
|
|
3148
|
+
function blindKey() {
|
|
3149
|
+
const path = join3(MESH_HOME, "blind.key");
|
|
3150
|
+
if (existsSync3(path)) return readFileSync3(path, "utf8").trim();
|
|
3151
|
+
const key = randomBytes(32).toString("hex");
|
|
3152
|
+
writeFileSync2(path, key, { mode: 384 });
|
|
3153
|
+
return key;
|
|
3154
|
+
}
|
|
3155
|
+
function configureClaudeCode(baseUrl) {
|
|
3156
|
+
mkdirSync4(join3(homedir2(), ".claude"), { recursive: true });
|
|
3157
|
+
let settings = {};
|
|
3158
|
+
let hadFile = false;
|
|
3159
|
+
if (existsSync3(CLAUDE_SETTINGS)) {
|
|
3160
|
+
hadFile = true;
|
|
3161
|
+
try {
|
|
3162
|
+
settings = JSON.parse(readFileSync3(CLAUDE_SETTINGS, "utf8"));
|
|
3163
|
+
} catch {
|
|
3164
|
+
throw new Error(`~/.claude/settings.json isn't valid JSON \u2014 fix or move it, then re-run. MESH won't overwrite it.`);
|
|
3165
|
+
}
|
|
3166
|
+
}
|
|
3167
|
+
const env = { ...settings.env ?? {} };
|
|
3168
|
+
const priorBaseUrl = env.ANTHROPIC_BASE_URL;
|
|
3169
|
+
env.ANTHROPIC_BASE_URL = baseUrl;
|
|
3170
|
+
writeFileSync2(CLAUDE_SETTINGS, `${JSON.stringify({ ...settings, env }, null, 2)}
|
|
3171
|
+
`);
|
|
3172
|
+
return function restore() {
|
|
3173
|
+
try {
|
|
3174
|
+
const now = JSON.parse(readFileSync3(CLAUDE_SETTINGS, "utf8"));
|
|
3175
|
+
const nowEnv = { ...now.env ?? {} };
|
|
3176
|
+
if (priorBaseUrl === void 0) delete nowEnv.ANTHROPIC_BASE_URL;
|
|
3177
|
+
else nowEnv.ANTHROPIC_BASE_URL = priorBaseUrl;
|
|
3178
|
+
const next = { ...now, env: nowEnv };
|
|
3179
|
+
if (Object.keys(nowEnv).length === 0) delete next.env;
|
|
3180
|
+
if (!hadFile && Object.keys(next).length === 0) return;
|
|
3181
|
+
writeFileSync2(CLAUDE_SETTINGS, `${JSON.stringify(next, null, 2)}
|
|
3182
|
+
`);
|
|
3183
|
+
} catch {
|
|
3184
|
+
}
|
|
3185
|
+
};
|
|
3186
|
+
}
|
|
3187
|
+
function readLedger(ledgerDir) {
|
|
3188
|
+
if (!existsSync3(ledgerDir)) return { records: [], chainOk: true };
|
|
3189
|
+
const spools = readdirSync(ledgerDir).filter((f) => f.endsWith(".jsonl") && !f.endsWith(".features.jsonl"));
|
|
3190
|
+
const allRecords = [];
|
|
3191
|
+
let chainOk = true;
|
|
3192
|
+
let brokenAt;
|
|
3193
|
+
for (const spool of spools) {
|
|
3194
|
+
const records = [];
|
|
3195
|
+
const raw = readFileSync3(join3(ledgerDir, spool), "utf8").trim();
|
|
3196
|
+
if (raw === "") continue;
|
|
3197
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
3198
|
+
if (line.trim() === "") continue;
|
|
3199
|
+
try {
|
|
3200
|
+
records.push(JSON.parse(line));
|
|
3201
|
+
} catch {
|
|
3202
|
+
}
|
|
3203
|
+
}
|
|
3204
|
+
const chain = verifyLedgerChain(records);
|
|
3205
|
+
if (!chain.ok) {
|
|
3206
|
+
chainOk = false;
|
|
3207
|
+
brokenAt = `${spool} seq ${chain.seq}`;
|
|
3208
|
+
}
|
|
3209
|
+
allRecords.push(...records);
|
|
3210
|
+
}
|
|
3211
|
+
return { records: allRecords, chainOk, ...brokenAt === void 0 ? {} : { brokenAt } };
|
|
3212
|
+
}
|
|
3213
|
+
function summariseLedger(ledgerDir) {
|
|
3214
|
+
const { records, chainOk, brokenAt } = readLedger(ledgerDir);
|
|
3215
|
+
if (records.length === 0) return { events: 0, savedUsd: 0, verdict: "do_not_charge", chainOk, ...brokenAt === void 0 ? {} : { brokenAt } };
|
|
3216
|
+
const invoice = recomputeInvoice(records.map((r) => r.event));
|
|
3217
|
+
return {
|
|
3218
|
+
events: records.length,
|
|
3219
|
+
savedUsd: invoice.netVerifiedSavingsUsd,
|
|
3220
|
+
verdict: invoice.verdict,
|
|
3221
|
+
chainOk,
|
|
3222
|
+
...brokenAt === void 0 ? {} : { brokenAt }
|
|
3223
|
+
};
|
|
3224
|
+
}
|
|
3225
|
+
async function startProxyWithPolicy(port, ledgerDir, upstreams = {}) {
|
|
3226
|
+
mkdirSync4(MESH_HOME, { recursive: true });
|
|
3227
|
+
mkdirSync4(ledgerDir, { recursive: true });
|
|
3228
|
+
const policyPath = join3(MESH_HOME, "theo-policy.json");
|
|
3229
|
+
const policy = existsSync3(policyPath) ? JSON.parse(readFileSync3(policyPath, "utf8")) : defaultPolicy("local", (/* @__PURE__ */ new Date()).toISOString());
|
|
3230
|
+
if (!existsSync3(policyPath)) writeFileSync2(policyPath, `${JSON.stringify(policy, null, 2)}
|
|
3231
|
+
`);
|
|
3232
|
+
const proxy = await startMeshProxy({
|
|
3233
|
+
port,
|
|
3234
|
+
ledgerDirectory: ledgerDir,
|
|
3235
|
+
tokenReduction: policyToReductionConfig(policy),
|
|
3236
|
+
blindFeaturesKey: blindKey(),
|
|
3237
|
+
...upstreams.anthropicUpstream ? { anthropicUpstream: upstreams.anthropicUpstream } : {},
|
|
3238
|
+
...upstreams.openaiUpstream ? { openaiUpstream: upstreams.openaiUpstream } : {}
|
|
3239
|
+
});
|
|
3240
|
+
return { proxy, policy };
|
|
3241
|
+
}
|
|
3242
|
+
async function runServe(argv) {
|
|
3243
|
+
const args = parseStart(argv);
|
|
3244
|
+
const { proxy } = await startProxyWithPolicy(args.port, args.ledgerDir, args);
|
|
3245
|
+
const stamp = () => (/* @__PURE__ */ new Date()).toISOString();
|
|
3246
|
+
console.log(`[${stamp()}] mesh serve \xB7 build ${BUILD_COMMIT} \xB7 proxy ${proxy.url} \xB7 ledger ${args.ledgerDir}`);
|
|
3247
|
+
if (args.openaiUpstream) console.log(`[${stamp()}] openai-style upstream \u2192 ${args.openaiUpstream}`);
|
|
3248
|
+
if (args.anthropicUpstream) console.log(`[${stamp()}] anthropic upstream \u2192 ${args.anthropicUpstream}`);
|
|
3249
|
+
let lastEvents = -1;
|
|
3250
|
+
const monitor = setInterval(() => {
|
|
3251
|
+
const s = summariseLedger(args.ledgerDir);
|
|
3252
|
+
if (!s.chainOk) {
|
|
3253
|
+
console.log(`[${stamp()}] ${T} \u26A0 ledger chain failed at ${s.brokenAt}`);
|
|
3254
|
+
return;
|
|
3255
|
+
}
|
|
3256
|
+
if (s.events === lastEvents || s.events === 0) return;
|
|
3257
|
+
lastEvents = s.events;
|
|
3258
|
+
console.log(`[${stamp()}] ${T} ${s.events} requests \xB7 $${s.savedUsd.toFixed(4)} verified saved (${s.verdict})`);
|
|
3259
|
+
}, 1e4);
|
|
3260
|
+
const UPDATE_CHECK_MS = 6 * 60 * 60 * 1e3;
|
|
3261
|
+
const autoUpdate = process.env.MESH_NO_AUTO_UPDATE === void 0 && BUILD_COMMIT !== "dev";
|
|
3262
|
+
const updateTimer = autoUpdate ? setInterval(() => {
|
|
3263
|
+
void (async () => {
|
|
3264
|
+
try {
|
|
3265
|
+
const next = await pullNewerRuntime();
|
|
3266
|
+
if (next && next !== BUILD_COMMIT) {
|
|
3267
|
+
console.log(`[${stamp()}] ${T} runtime updated ${BUILD_COMMIT} \u2192 ${next}; restarting to apply.`);
|
|
3268
|
+
clearInterval(monitor);
|
|
3269
|
+
clearInterval(updateTimer);
|
|
3270
|
+
await proxy.close();
|
|
3271
|
+
process.exit(0);
|
|
3272
|
+
}
|
|
3273
|
+
} catch {
|
|
3274
|
+
}
|
|
3275
|
+
})();
|
|
3276
|
+
}, UPDATE_CHECK_MS) : void 0;
|
|
3277
|
+
const shutdown = async () => {
|
|
3278
|
+
clearInterval(monitor);
|
|
3279
|
+
if (updateTimer) clearInterval(updateTimer);
|
|
3280
|
+
await proxy.close();
|
|
3281
|
+
process.exit(0);
|
|
3282
|
+
};
|
|
3283
|
+
process.on("SIGTERM", () => {
|
|
3284
|
+
void shutdown();
|
|
3285
|
+
});
|
|
3286
|
+
process.on("SIGINT", () => {
|
|
3287
|
+
void shutdown();
|
|
3288
|
+
});
|
|
3289
|
+
await new Promise(() => {
|
|
3290
|
+
});
|
|
3291
|
+
}
|
|
3292
|
+
async function runStart(argv) {
|
|
3293
|
+
const args = parseStart(argv);
|
|
3294
|
+
const { proxy, policy } = await startProxyWithPolicy(args.port, args.ledgerDir, args);
|
|
3295
|
+
const restore = args.install ? configureClaudeCode(proxy.url) : () => {
|
|
3296
|
+
};
|
|
3297
|
+
const withheld = withheldLevers(policy);
|
|
3298
|
+
console.log("");
|
|
3299
|
+
console.log(` MESH is running \xB7 build ${BUILD_COMMIT}`);
|
|
3300
|
+
console.log(` proxy ${proxy.url}`);
|
|
3301
|
+
console.log(` ledger ${args.ledgerDir} (hash-chained; recompute any time with: mesh verify)`);
|
|
3302
|
+
if (args.install) console.log(` claude pointed at MESH via ~/.claude/settings.json (restored on exit)`);
|
|
3303
|
+
else console.log(` claude not configured (--no-install); set ANTHROPIC_BASE_URL=${proxy.url} yourself`);
|
|
3304
|
+
console.log(` openai point any OpenAI-style client here: OPENAI_BASE_URL=${proxy.url}/v1 (keep your own key)`);
|
|
3305
|
+
if (args.openaiUpstream) console.log(` upstream OpenAI-style traffic \u2192 ${args.openaiUpstream}`);
|
|
3306
|
+
if (args.anthropicUpstream) console.log(` upstream Anthropic traffic \u2192 ${args.anthropicUpstream}`);
|
|
3307
|
+
console.log("");
|
|
3308
|
+
console.log(` ${T} \u2014 I'm watching your traffic. I only ever tune MESH, never your prompts`);
|
|
3309
|
+
console.log(` ${T} or answers. Everything I report is verified from your provider's own`);
|
|
3310
|
+
console.log(` ${T} usage \u2014 never an estimate.`);
|
|
3311
|
+
if (withheld.length > 0) {
|
|
3312
|
+
console.log(` ${T} Holding ${withheld.length} lossy lever${withheld.length === 1 ? "" : "s"} back until a replay`);
|
|
3313
|
+
console.log(` ${T} proves ${withheld.length === 1 ? "it is" : "they are"} safe on your own traffic.`);
|
|
3314
|
+
}
|
|
3315
|
+
console.log("");
|
|
3316
|
+
console.log(` Just use Claude Code as normal. Press Ctrl+C to stop (Claude Code reverts cleanly).`);
|
|
3317
|
+
console.log("");
|
|
3318
|
+
let lastEvents = -1;
|
|
3319
|
+
const tick = () => {
|
|
3320
|
+
const s = summariseLedger(args.ledgerDir);
|
|
3321
|
+
if (!s.chainOk) {
|
|
3322
|
+
console.log(` ${T} \u2014 \u26A0 ledger chain check failed at ${s.brokenAt}. Stop and run: mesh verify`);
|
|
3323
|
+
return;
|
|
3324
|
+
}
|
|
3325
|
+
if (s.events === lastEvents) return;
|
|
3326
|
+
lastEvents = s.events;
|
|
3327
|
+
if (s.events === 0) return;
|
|
3328
|
+
const saved = `$${s.savedUsd.toFixed(4)}`;
|
|
3329
|
+
const verdict = s.verdict === "charge" ? saved : `${saved} (nothing to bill yet \u2014 do not pay us)`;
|
|
3330
|
+
console.log(` ${T} \u2014 ${s.events} request${s.events === 1 ? "" : "s"} seen \xB7 ${verdict} verified saved`);
|
|
3331
|
+
};
|
|
3332
|
+
const monitor = setInterval(tick, 6e3);
|
|
3333
|
+
let closing = false;
|
|
3334
|
+
const shutdown = async () => {
|
|
3335
|
+
if (closing) return;
|
|
3336
|
+
closing = true;
|
|
3337
|
+
clearInterval(monitor);
|
|
3338
|
+
const s = summariseLedger(args.ledgerDir);
|
|
3339
|
+
restore();
|
|
3340
|
+
await proxy.close();
|
|
3341
|
+
console.log("");
|
|
3342
|
+
if (s.events > 0) {
|
|
3343
|
+
console.log(` ${T} \u2014 session done: ${s.events} request${s.events === 1 ? "" : "s"}, $${s.savedUsd.toFixed(4)} verified saved.`);
|
|
3344
|
+
console.log(` ${T} Recompute it yourself any time: mesh verify`);
|
|
3345
|
+
} else {
|
|
3346
|
+
console.log(` ${T} \u2014 no requests ran through MESH this session.`);
|
|
3347
|
+
}
|
|
3348
|
+
console.log(` Claude Code restored. Bye.`);
|
|
3349
|
+
process.exit(0);
|
|
3350
|
+
};
|
|
3351
|
+
process.on("SIGINT", () => {
|
|
3352
|
+
void shutdown();
|
|
3353
|
+
});
|
|
3354
|
+
process.on("SIGTERM", () => {
|
|
3355
|
+
void shutdown();
|
|
3356
|
+
});
|
|
3357
|
+
await new Promise(() => {
|
|
3358
|
+
});
|
|
3359
|
+
}
|
|
3360
|
+
var BUILD_COMMIT, MESH_HOME, CLAUDE_SETTINGS, DEFAULT_PORT, T;
|
|
3361
|
+
var init_start = __esm({
|
|
3362
|
+
"packages/cli/src/start.ts"() {
|
|
3363
|
+
init_savings_ledger();
|
|
3364
|
+
init_proxy();
|
|
3365
|
+
init_theo_policy();
|
|
3366
|
+
init_update();
|
|
3367
|
+
BUILD_COMMIT = true ? "c8378ea" : process.env.MESH_BUILD_COMMIT ?? "dev";
|
|
3368
|
+
MESH_HOME = join3(homedir2(), ".mesh");
|
|
3369
|
+
CLAUDE_SETTINGS = join3(homedir2(), ".claude", "settings.json");
|
|
3370
|
+
DEFAULT_PORT = 8787;
|
|
3371
|
+
T = "\u2B22 theo";
|
|
3372
|
+
}
|
|
3373
|
+
});
|
|
3374
|
+
|
|
3375
|
+
// packages/cli/src/daemon.ts
|
|
3376
|
+
var daemon_exports = {};
|
|
3377
|
+
__export(daemon_exports, {
|
|
3378
|
+
runInstall: () => runInstall,
|
|
3379
|
+
runStatus: () => runStatus,
|
|
3380
|
+
runStop: () => runStop,
|
|
3381
|
+
runUninstall: () => runUninstall
|
|
3382
|
+
});
|
|
3383
|
+
import { connect } from "node:net";
|
|
3384
|
+
import { execFileSync } from "node:child_process";
|
|
3385
|
+
import { chmodSync, copyFileSync as copyFileSync2, existsSync as existsSync4, mkdirSync as mkdirSync5, readFileSync as readFileSync4, rmSync, writeFileSync as writeFileSync3 } from "node:fs";
|
|
3386
|
+
import { homedir as homedir3, platform } from "node:os";
|
|
3387
|
+
import { dirname as dirname2, join as join4, resolve } from "node:path";
|
|
3388
|
+
function requireMac(action) {
|
|
3389
|
+
if (platform() !== "darwin") {
|
|
3390
|
+
console.error(`\`mesh ${action}\` uses macOS launchd and isn't wired for this platform yet.`);
|
|
3391
|
+
console.error(`Use \`mesh start\` instead \u2014 it does the same thing while it's running.`);
|
|
3392
|
+
process.exit(1);
|
|
3393
|
+
}
|
|
3394
|
+
}
|
|
3395
|
+
function parsePort(argv) {
|
|
3396
|
+
const i = argv.indexOf("--port");
|
|
3397
|
+
if (i >= 0 && argv[i + 1]) {
|
|
3398
|
+
const n = Number.parseInt(argv[i + 1], 10);
|
|
3399
|
+
if (Number.isInteger(n) && n > 0 && n < 65536) return n;
|
|
3400
|
+
throw new Error("--port must be 1-65535.");
|
|
3401
|
+
}
|
|
3402
|
+
return DEFAULT_PORT;
|
|
3403
|
+
}
|
|
3404
|
+
function launchctl(...args) {
|
|
3405
|
+
try {
|
|
3406
|
+
execFileSync("launchctl", args, { stdio: "pipe" });
|
|
3407
|
+
} catch {
|
|
3408
|
+
}
|
|
3409
|
+
}
|
|
3410
|
+
function isLoaded() {
|
|
3411
|
+
try {
|
|
3412
|
+
execFileSync("launchctl", ["list", LABEL], { stdio: "pipe" });
|
|
3413
|
+
return true;
|
|
3414
|
+
} catch {
|
|
3415
|
+
return false;
|
|
3416
|
+
}
|
|
3417
|
+
}
|
|
3418
|
+
function portUp(port, timeoutMs = 800) {
|
|
3419
|
+
return new Promise((res) => {
|
|
3420
|
+
const sock = connect({ host: "127.0.0.1", port });
|
|
3421
|
+
const done = (ok) => {
|
|
3422
|
+
sock.destroy();
|
|
3423
|
+
res(ok);
|
|
3424
|
+
};
|
|
3425
|
+
sock.setTimeout(timeoutMs);
|
|
3426
|
+
sock.once("connect", () => done(true));
|
|
3427
|
+
sock.once("timeout", () => done(false));
|
|
3428
|
+
sock.once("error", () => done(false));
|
|
3429
|
+
});
|
|
3430
|
+
}
|
|
3431
|
+
function setClaudeBaseUrl(url) {
|
|
3432
|
+
mkdirSync5(dirname2(CLAUDE_SETTINGS), { recursive: true });
|
|
3433
|
+
let settings = {};
|
|
3434
|
+
if (existsSync4(CLAUDE_SETTINGS)) {
|
|
3435
|
+
try {
|
|
3436
|
+
settings = JSON.parse(readFileSync4(CLAUDE_SETTINGS, "utf8"));
|
|
3437
|
+
} catch {
|
|
3438
|
+
throw new Error("~/.claude/settings.json isn't valid JSON \u2014 fix it first; MESH won't overwrite it.");
|
|
3439
|
+
}
|
|
3440
|
+
}
|
|
3441
|
+
const env = { ...settings.env ?? {}, ANTHROPIC_BASE_URL: url };
|
|
3442
|
+
writeFileSync3(CLAUDE_SETTINGS, `${JSON.stringify({ ...settings, env }, null, 2)}
|
|
3443
|
+
`);
|
|
3444
|
+
}
|
|
3445
|
+
function clearClaudeBaseUrl() {
|
|
3446
|
+
if (!existsSync4(CLAUDE_SETTINGS)) return;
|
|
3447
|
+
try {
|
|
3448
|
+
const settings = JSON.parse(readFileSync4(CLAUDE_SETTINGS, "utf8"));
|
|
3449
|
+
const env = { ...settings.env ?? {} };
|
|
3450
|
+
delete env.ANTHROPIC_BASE_URL;
|
|
3451
|
+
const next = { ...settings, env };
|
|
3452
|
+
if (Object.keys(env).length === 0) delete next.env;
|
|
3453
|
+
writeFileSync3(CLAUDE_SETTINGS, `${JSON.stringify(next, null, 2)}
|
|
3454
|
+
`);
|
|
3455
|
+
} catch {
|
|
3456
|
+
}
|
|
3457
|
+
}
|
|
3458
|
+
function writePlist(port) {
|
|
3459
|
+
const argv = [process.execPath, STABLE_BIN, "serve", "--port", String(port), "--ledger", DEFAULT_LEDGER];
|
|
3460
|
+
const esc = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
3461
|
+
const argXml = argv.map((a) => ` <string>${esc(a)}</string>`).join("\n");
|
|
3462
|
+
const plist = `<?xml version="1.0" encoding="UTF-8"?>
|
|
3463
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
3464
|
+
<plist version="1.0">
|
|
3465
|
+
<dict>
|
|
3466
|
+
<key>Label</key><string>${LABEL}</string>
|
|
3467
|
+
<key>ProgramArguments</key>
|
|
3468
|
+
<array>
|
|
3469
|
+
${argXml}
|
|
3470
|
+
</array>
|
|
3471
|
+
<key>RunAtLoad</key><true/>
|
|
3472
|
+
<key>KeepAlive</key><true/>
|
|
3473
|
+
<key>StandardOutPath</key><string>${esc(DAEMON_LOG)}</string>
|
|
3474
|
+
<key>StandardErrorPath</key><string>${esc(DAEMON_LOG)}</string>
|
|
3475
|
+
</dict>
|
|
3476
|
+
</plist>
|
|
3477
|
+
`;
|
|
3478
|
+
mkdirSync5(dirname2(PLIST), { recursive: true });
|
|
3479
|
+
writeFileSync3(PLIST, plist);
|
|
3480
|
+
}
|
|
3481
|
+
async function runInstall(argv) {
|
|
3482
|
+
requireMac("install");
|
|
3483
|
+
const port = parsePort(argv);
|
|
3484
|
+
mkdirSync5(join4(MESH_HOME, "bin"), { recursive: true });
|
|
3485
|
+
mkdirSync5(DEFAULT_LEDGER, { recursive: true });
|
|
3486
|
+
const self = resolve(process.env.MESH_BOOTSTRAP_PATH ?? process.argv[1] ?? "");
|
|
3487
|
+
if (self !== STABLE_BIN) copyFileSync2(self, STABLE_BIN);
|
|
3488
|
+
chmodSync(STABLE_BIN, 493);
|
|
3489
|
+
if (isLoaded()) launchctl("unload", PLIST);
|
|
3490
|
+
writePlist(port);
|
|
3491
|
+
launchctl("load", "-w", PLIST);
|
|
3492
|
+
setClaudeBaseUrl(`http://127.0.0.1:${port}`);
|
|
3493
|
+
let up = false;
|
|
3494
|
+
for (let i = 0; i < 10 && !up; i += 1) {
|
|
3495
|
+
up = await portUp(port);
|
|
3496
|
+
if (!up) await sleep(400);
|
|
3497
|
+
}
|
|
3498
|
+
console.log("");
|
|
3499
|
+
if (up) {
|
|
3500
|
+
console.log(` \u2B22 theo \u2014 MESH is now always on. build ${BUILD_COMMIT}`);
|
|
3501
|
+
console.log(` service launchd ${LABEL} (starts at login, restarts if it stops)`);
|
|
3502
|
+
console.log(` proxy http://127.0.0.1:${port} \xB7 ledger ${DEFAULT_LEDGER}`);
|
|
3503
|
+
console.log(` claude pointed at MESH permanently via ~/.claude/settings.json`);
|
|
3504
|
+
console.log(` log ${DAEMON_LOG}`);
|
|
3505
|
+
console.log("");
|
|
3506
|
+
console.log(` Just use Claude Code \u2014 every session is optimised. Check savings with: mesh status`);
|
|
3507
|
+
console.log(` Turn it off any time with: mesh uninstall`);
|
|
3508
|
+
} else {
|
|
3509
|
+
console.log(` \u26A0 The service was installed but the proxy didn't answer on port ${port}.`);
|
|
3510
|
+
console.log(` Check ${DAEMON_LOG}, then re-run \`mesh install\`, or use \`mesh start\` instead.`);
|
|
3511
|
+
process.exitCode = 1;
|
|
3512
|
+
}
|
|
3513
|
+
console.log("");
|
|
3514
|
+
}
|
|
3515
|
+
function runUninstall(_argv) {
|
|
3516
|
+
requireMac("uninstall");
|
|
3517
|
+
launchctl("unload", "-w", PLIST);
|
|
3518
|
+
rmSync(PLIST, { force: true });
|
|
3519
|
+
clearClaudeBaseUrl();
|
|
3520
|
+
console.log("");
|
|
3521
|
+
console.log(` \u2B22 theo \u2014 always-on removed. The service is stopped and Claude Code is back to normal.`);
|
|
3522
|
+
console.log(` Your ledger is kept at ${DEFAULT_LEDGER} \u2014 recompute any time with: mesh verify`);
|
|
3523
|
+
console.log("");
|
|
3524
|
+
}
|
|
3525
|
+
function runStop(_argv) {
|
|
3526
|
+
requireMac("stop");
|
|
3527
|
+
if (!isLoaded()) {
|
|
3528
|
+
console.log("");
|
|
3529
|
+
console.log(` No MESH background service is running.`);
|
|
3530
|
+
console.log(` (A foreground \`mesh start\` is stopped with Ctrl+C.)`);
|
|
3531
|
+
console.log("");
|
|
3532
|
+
return;
|
|
3533
|
+
}
|
|
3534
|
+
launchctl("unload", "-w", PLIST);
|
|
3535
|
+
clearClaudeBaseUrl();
|
|
3536
|
+
console.log("");
|
|
3537
|
+
console.log(` \u2B22 theo \u2014 MESH service stopped. Claude Code is back to normal.`);
|
|
3538
|
+
console.log(` The service is kept \u2014 resume always-on any time with: mesh install`);
|
|
3539
|
+
console.log(` Your ledger is untouched.`);
|
|
3540
|
+
console.log("");
|
|
3541
|
+
}
|
|
3542
|
+
async function runStatus(_argv) {
|
|
3543
|
+
const loaded = platform() === "darwin" ? isLoaded() : false;
|
|
3544
|
+
const port = DEFAULT_PORT;
|
|
3545
|
+
const up = await portUp(port);
|
|
3546
|
+
const s = summariseLedger(DEFAULT_LEDGER);
|
|
3547
|
+
console.log("");
|
|
3548
|
+
console.log(` mesh status \u2014 build ${BUILD_COMMIT}`);
|
|
3549
|
+
console.log(` service ${loaded ? "installed (launchd)" : "not installed"}`);
|
|
3550
|
+
console.log(` proxy ${up ? `up on http://127.0.0.1:${port}` : "not responding"}`);
|
|
3551
|
+
if (s.events > 0) {
|
|
3552
|
+
console.log(` \u2B22 theo ${s.events} requests seen \xB7 $${s.savedUsd.toFixed(4)} verified saved (${s.verdict})`);
|
|
3553
|
+
} else {
|
|
3554
|
+
console.log(` \u2B22 theo no traffic yet \u2014 use Claude Code and check back`);
|
|
3555
|
+
}
|
|
3556
|
+
const cov = policyCoverage(readPolicy());
|
|
3557
|
+
console.log(
|
|
3558
|
+
` coverage live on ${cov.activeCount}/${cov.enforceableCount} enforceable levers (${cov.activeCoveragePercent.toFixed(0)}%)${cov.heldCount ? ` \xB7 ${cov.heldCount} held for proof` : ""}${cov.availableCount ? ` \xB7 ${cov.availableCount} not yet adopted` : ""}`
|
|
3559
|
+
);
|
|
3560
|
+
if (!up && loaded) console.log(` note service is registered but the proxy is down; see ${DAEMON_LOG}`);
|
|
3561
|
+
console.log("");
|
|
3562
|
+
}
|
|
3563
|
+
function sleep(ms) {
|
|
3564
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
3565
|
+
}
|
|
3566
|
+
var LABEL, PLIST, DAEMON_LOG, STABLE_BIN, DEFAULT_LEDGER;
|
|
3567
|
+
var init_daemon = __esm({
|
|
3568
|
+
"packages/cli/src/daemon.ts"() {
|
|
3569
|
+
init_start();
|
|
3570
|
+
init_theo_policy();
|
|
3571
|
+
LABEL = "dev.meshtoken.mesh";
|
|
3572
|
+
PLIST = join4(homedir3(), "Library", "LaunchAgents", `${LABEL}.plist`);
|
|
3573
|
+
DAEMON_LOG = join4(MESH_HOME, "daemon.log");
|
|
3574
|
+
STABLE_BIN = join4(MESH_HOME, "bin", "mesh");
|
|
3575
|
+
DEFAULT_LEDGER = join4(MESH_HOME, "ledger");
|
|
3576
|
+
}
|
|
3577
|
+
});
|
|
3578
|
+
|
|
3579
|
+
// packages/cli/src/verify.ts
|
|
3580
|
+
var verify_exports = {};
|
|
3581
|
+
__export(verify_exports, {
|
|
3582
|
+
runVerify: () => runVerify
|
|
3583
|
+
});
|
|
3584
|
+
import { createHash as createHash7 } from "node:crypto";
|
|
3585
|
+
import { existsSync as existsSync5, readFileSync as readFileSync5, readdirSync as readdirSync2 } from "node:fs";
|
|
3586
|
+
import { homedir as homedir4 } from "node:os";
|
|
3587
|
+
import { join as join5 } from "node:path";
|
|
3588
|
+
function sampleRequest() {
|
|
3589
|
+
const dup = "Records 0-19: settlement entry nominal, reconciliation standard, no notification sent, annotation routine.";
|
|
3590
|
+
const schema = {
|
|
3591
|
+
name: "lookup",
|
|
3592
|
+
description: "Look up records in a subsystem.",
|
|
3593
|
+
input_schema: { $schema: "http://json-schema.org/draft-07/schema#", type: "object", additionalProperties: false, properties: { id: { type: "string" } } }
|
|
3594
|
+
};
|
|
3595
|
+
return {
|
|
3596
|
+
model: "claude-sonnet-5-5",
|
|
3597
|
+
system: "You are a careful assistant. Cite sources. Escalate anything risky.",
|
|
3598
|
+
tools: [schema, { ...schema, name: "lookup_2" }, { ...schema, name: "lookup_3" }],
|
|
3599
|
+
messages: [
|
|
3600
|
+
{ role: "user", content: `Excerpts for case 7: ${dup} ${dup} ${dup}` },
|
|
3601
|
+
{ role: "assistant", content: "Understood." },
|
|
3602
|
+
{ role: "user", content: `${dup} ${dup} Question: what does clause 7 require?` }
|
|
3603
|
+
]
|
|
3604
|
+
};
|
|
3605
|
+
}
|
|
3606
|
+
async function runVerify(_argv) {
|
|
3607
|
+
const checks = [];
|
|
3608
|
+
const runtimeDir = join5(homedir4(), ".mesh", "runtime");
|
|
3609
|
+
const runtimeFile = join5(runtimeDir, "mesh-runtime.mjs");
|
|
3610
|
+
const recordedSha = existsSync5(join5(runtimeDir, "sha256")) ? readFileSync5(join5(runtimeDir, "sha256"), "utf8").trim() : null;
|
|
3611
|
+
if (recordedSha !== null && existsSync5(runtimeFile)) {
|
|
3612
|
+
const actual = createHash7("sha256").update(readFileSync5(runtimeFile)).digest("hex");
|
|
3613
|
+
checks.push({
|
|
3614
|
+
name: "provenance",
|
|
3615
|
+
ok: actual === recordedSha,
|
|
3616
|
+
detail: actual === recordedSha ? `build ${BUILD_COMMIT}; cached runtime matches its download checksum` : `build ${BUILD_COMMIT}; CACHED RUNTIME ALTERED since download (checksum mismatch)`
|
|
3617
|
+
});
|
|
3618
|
+
} else {
|
|
3619
|
+
checks.push({ name: "provenance", ok: true, detail: `build ${BUILD_COMMIT} (npm or dev build \u2014 no download checksum to re-verify)` });
|
|
3620
|
+
}
|
|
3621
|
+
const { summary } = reduceRequest(sampleRequest(), { toolSchemas: true, duplicateContext: true, repairCachePrefix: true });
|
|
3622
|
+
const reducerOk = !summary.skipped && summary.estimatedLosslessTokensSaved > 0;
|
|
3623
|
+
checks.push({
|
|
3624
|
+
name: "reducer",
|
|
3625
|
+
ok: reducerOk,
|
|
3626
|
+
detail: reducerOk ? `lossless: ${summary.estimatedTokensBefore} \u2192 ${summary.estimatedTokensAfter} tokens (\u2212${summary.estimatedLosslessTokensSaved}), ${summary.changes.length} lever(s)` : `reducer did not fire (${summary.skippedReason ?? "no lossless saving"})`
|
|
3627
|
+
});
|
|
3628
|
+
if (!existsSync5(LEDGER_DIR) || readdirSync2(LEDGER_DIR).filter((f) => f.endsWith(".jsonl") && !f.endsWith(".features.jsonl")).length === 0) {
|
|
3629
|
+
checks.push({ name: "ledger", ok: true, detail: "no ledger yet \u2014 run `mesh start` and use Claude Code, then verify again" });
|
|
3630
|
+
checks.push({ name: "determinism", ok: true, detail: "nothing to recompute yet" });
|
|
3631
|
+
} else {
|
|
3632
|
+
const spools = readdirSync2(LEDGER_DIR).filter((f) => f.endsWith(".jsonl") && !f.endsWith(".features.jsonl"));
|
|
3633
|
+
const all = [];
|
|
3634
|
+
let chainOk = true;
|
|
3635
|
+
let chainDetail = "";
|
|
3636
|
+
let total = 0;
|
|
3637
|
+
for (const spool of spools) {
|
|
3638
|
+
const records = [];
|
|
3639
|
+
for (const line of readFileSync5(join5(LEDGER_DIR, spool), "utf8").trim().split(/\r?\n/)) {
|
|
3640
|
+
if (line.trim() === "") continue;
|
|
3641
|
+
try {
|
|
3642
|
+
records.push(JSON.parse(line));
|
|
3643
|
+
} catch {
|
|
3644
|
+
}
|
|
3645
|
+
}
|
|
3646
|
+
total += records.length;
|
|
3647
|
+
const chain = verifyLedgerChain(records);
|
|
3648
|
+
if (!chain.ok) {
|
|
3649
|
+
chainOk = false;
|
|
3650
|
+
chainDetail = `${spool}: broken at seq ${chain.seq} (${chain.reason})`;
|
|
3651
|
+
}
|
|
3652
|
+
all.push(...records);
|
|
3653
|
+
}
|
|
3654
|
+
checks.push({
|
|
3655
|
+
name: "ledger",
|
|
3656
|
+
ok: chainOk,
|
|
3657
|
+
detail: chainOk ? `hash-chain verified across ${spools.length} spool(s), ${total} record(s)` : chainDetail
|
|
3658
|
+
});
|
|
3659
|
+
const events = all.map((r) => r.event);
|
|
3660
|
+
const once = JSON.stringify(recomputeInvoice(events));
|
|
3661
|
+
const twice = JSON.stringify(recomputeInvoice(events));
|
|
3662
|
+
const invoice = recomputeInvoice(events);
|
|
3663
|
+
checks.push({
|
|
3664
|
+
name: "determinism",
|
|
3665
|
+
ok: once === twice,
|
|
3666
|
+
detail: once === twice ? `invoice recomputes identically: $${invoice.netVerifiedSavingsUsd.toFixed(4)} verified (${invoice.verdict})` : "invoice is NON-deterministic \u2014 this should never happen; please report it"
|
|
3667
|
+
});
|
|
3668
|
+
}
|
|
3669
|
+
console.log("");
|
|
3670
|
+
console.log(` mesh verify \u2014 build ${BUILD_COMMIT}`);
|
|
3671
|
+
console.log("");
|
|
3672
|
+
let allOk = true;
|
|
3673
|
+
for (const c of checks) {
|
|
3674
|
+
allOk = allOk && c.ok;
|
|
3675
|
+
console.log(` ${c.ok ? "\u2713" : "\u2717"} ${c.name.padEnd(12)} ${c.detail}`);
|
|
3676
|
+
}
|
|
3677
|
+
console.log("");
|
|
3678
|
+
console.log(` Savings are measured from your provider's own reported usage, never MESH's estimate.`);
|
|
3679
|
+
console.log(` The ledger is yours; this command recomputes the invoice from it with no network calls.`);
|
|
3680
|
+
console.log("");
|
|
3681
|
+
console.log(allOk ? ` All checks passed.` : ` SOME CHECKS FAILED \u2014 see above.`);
|
|
3682
|
+
if (!allOk) process.exit(1);
|
|
3683
|
+
}
|
|
3684
|
+
var LEDGER_DIR;
|
|
3685
|
+
var init_verify = __esm({
|
|
3686
|
+
"packages/cli/src/verify.ts"() {
|
|
3687
|
+
init_savings_ledger();
|
|
3688
|
+
init_reduce_request();
|
|
3689
|
+
init_start();
|
|
3690
|
+
LEDGER_DIR = join5(homedir4(), ".mesh", "ledger");
|
|
3691
|
+
}
|
|
3692
|
+
});
|
|
3693
|
+
|
|
3694
|
+
// packages/cli/src/diag.ts
|
|
3695
|
+
var diag_exports = {};
|
|
3696
|
+
__export(diag_exports, {
|
|
3697
|
+
runDoctor: () => runDoctor,
|
|
3698
|
+
runVersion: () => runVersion
|
|
3699
|
+
});
|
|
3700
|
+
import { execFileSync as execFileSync2 } from "node:child_process";
|
|
3701
|
+
import { existsSync as existsSync6, readFileSync as readFileSync6 } from "node:fs";
|
|
3702
|
+
import { connect as connect2 } from "node:net";
|
|
3703
|
+
import { platform as platform2 } from "node:os";
|
|
3704
|
+
import { join as join6 } from "node:path";
|
|
3705
|
+
function portUp2(port, timeoutMs = 700) {
|
|
3706
|
+
return new Promise((res) => {
|
|
3707
|
+
const sock = connect2({ host: "127.0.0.1", port });
|
|
3708
|
+
const done = (ok) => {
|
|
3709
|
+
sock.destroy();
|
|
3710
|
+
res(ok);
|
|
3711
|
+
};
|
|
3712
|
+
sock.setTimeout(timeoutMs);
|
|
3713
|
+
sock.once("connect", () => done(true));
|
|
3714
|
+
sock.once("timeout", () => done(false));
|
|
3715
|
+
sock.once("error", () => done(false));
|
|
3716
|
+
});
|
|
3717
|
+
}
|
|
3718
|
+
function serviceLoaded() {
|
|
3719
|
+
if (platform2() !== "darwin") return false;
|
|
3720
|
+
try {
|
|
3721
|
+
execFileSync2("launchctl", ["list", LAUNCHD_LABEL], { stdio: "pipe" });
|
|
3722
|
+
return true;
|
|
3723
|
+
} catch {
|
|
3724
|
+
return false;
|
|
3725
|
+
}
|
|
3726
|
+
}
|
|
3727
|
+
function runVersion(argv) {
|
|
3728
|
+
const info = { version: VERSION, build: BUILD_COMMIT, node: process.versions.node, platform: `${platform2()}-${process.arch}` };
|
|
3729
|
+
if (argv.includes("--json")) {
|
|
3730
|
+
console.log(JSON.stringify(info));
|
|
3731
|
+
return;
|
|
3732
|
+
}
|
|
3733
|
+
console.log(`mesh ${info.version} (build ${info.build}) \xB7 node ${info.node} \xB7 ${info.platform}`);
|
|
3734
|
+
}
|
|
3735
|
+
async function runDoctor(argv) {
|
|
3736
|
+
const checks = [];
|
|
3737
|
+
const port = DEFAULT_PORT;
|
|
3738
|
+
const nodeOk = nodeMajor() >= 22;
|
|
3739
|
+
checks.push({
|
|
3740
|
+
ok: nodeOk,
|
|
3741
|
+
name: "node",
|
|
3742
|
+
detail: `v${process.versions.node}${nodeOk ? "" : " (Node 22 LTS required)"}`,
|
|
3743
|
+
fix: nodeOk ? void 0 : "Upgrade to Node 22 LTS: https://nodejs.org"
|
|
3744
|
+
});
|
|
3745
|
+
const runtimeVersion = existsSync6(join6(MESH_HOME, "runtime", "version")) ? readFileSync6(join6(MESH_HOME, "runtime", "version"), "utf8").trim() : null;
|
|
3746
|
+
checks.push({
|
|
3747
|
+
ok: true,
|
|
3748
|
+
name: "runtime",
|
|
3749
|
+
detail: runtimeVersion ? `cached ${runtimeVersion}` : "running in place (no fetched runtime cached yet)"
|
|
3750
|
+
});
|
|
3751
|
+
const up = await portUp2(port);
|
|
3752
|
+
checks.push({ ok: true, warn: !up, name: "proxy", detail: up ? `up on 127.0.0.1:${port}` : `not running on :${port}`, fix: up ? void 0 : "Start it with: mesh start" });
|
|
3753
|
+
let settingsDetail = "no ~/.claude/settings.json (Claude Code not configured for MESH)";
|
|
3754
|
+
let settingsOk = true;
|
|
3755
|
+
if (existsSync6(CLAUDE_SETTINGS)) {
|
|
3756
|
+
try {
|
|
3757
|
+
const s2 = JSON.parse(readFileSync6(CLAUDE_SETTINGS, "utf8"));
|
|
3758
|
+
const base = s2.env?.ANTHROPIC_BASE_URL;
|
|
3759
|
+
settingsDetail = base ? `ANTHROPIC_BASE_URL \u2192 ${base}` : "valid, but ANTHROPIC_BASE_URL not set";
|
|
3760
|
+
} catch {
|
|
3761
|
+
settingsOk = false;
|
|
3762
|
+
settingsDetail = "~/.claude/settings.json is not valid JSON";
|
|
3763
|
+
}
|
|
3764
|
+
}
|
|
3765
|
+
checks.push({ ok: settingsOk, name: "claude", detail: settingsDetail, fix: settingsOk ? void 0 : "Fix or remove ~/.claude/settings.json; MESH won't overwrite invalid JSON." });
|
|
3766
|
+
if (platform2() === "darwin") {
|
|
3767
|
+
const loaded = serviceLoaded();
|
|
3768
|
+
checks.push({ ok: true, name: "service", detail: loaded ? "launchd service installed (always-on)" : "not installed (foreground only)" });
|
|
3769
|
+
}
|
|
3770
|
+
const s = summariseLedger(join6(MESH_HOME, "ledger"));
|
|
3771
|
+
checks.push({
|
|
3772
|
+
ok: s.chainOk,
|
|
3773
|
+
name: "ledger",
|
|
3774
|
+
detail: s.events === 0 ? "no ledger yet" : `${s.events} records${s.chainOk ? ", chain verified" : `, CHAIN BROKEN at ${s.brokenAt}`}`,
|
|
3775
|
+
fix: s.chainOk ? void 0 : "Run `mesh verify` and report this \u2014 the ledger should never break."
|
|
3776
|
+
});
|
|
3777
|
+
if (argv.includes("--json")) {
|
|
3778
|
+
console.log(JSON.stringify({ checks }));
|
|
3779
|
+
return;
|
|
3780
|
+
}
|
|
3781
|
+
console.log("");
|
|
3782
|
+
console.log(` mesh doctor \u2014 build ${BUILD_COMMIT}`);
|
|
3783
|
+
console.log("");
|
|
3784
|
+
let hardFail = false;
|
|
3785
|
+
for (const c of checks) {
|
|
3786
|
+
hardFail = hardFail || !c.ok;
|
|
3787
|
+
const mark = !c.ok ? "\u2717" : c.warn ? "\u26A0" : "\u2713";
|
|
3788
|
+
console.log(` ${mark} ${c.name.padEnd(9)} ${c.detail}`);
|
|
3789
|
+
if (c.fix) console.log(` \u21B3 ${c.fix}`);
|
|
3790
|
+
}
|
|
3791
|
+
console.log("");
|
|
3792
|
+
console.log(hardFail ? " Some checks failed \u2014 see above." : " Looks healthy.");
|
|
3793
|
+
if (hardFail) process.exitCode = 1;
|
|
3794
|
+
}
|
|
3795
|
+
var VERSION, LAUNCHD_LABEL, nodeMajor;
|
|
3796
|
+
var init_diag = __esm({
|
|
3797
|
+
"packages/cli/src/diag.ts"() {
|
|
3798
|
+
init_start();
|
|
3799
|
+
VERSION = true ? "0.1.0" : process.env.MESH_VERSION ?? "0.0.0";
|
|
3800
|
+
LAUNCHD_LABEL = "dev.meshtoken.mesh";
|
|
3801
|
+
nodeMajor = () => Number.parseInt(process.versions.node.split(".")[0] ?? "0", 10);
|
|
3802
|
+
}
|
|
3803
|
+
});
|
|
3804
|
+
|
|
3805
|
+
// packages/cli/src/dashboard.ts
|
|
3806
|
+
var dashboard_exports = {};
|
|
3807
|
+
__export(dashboard_exports, {
|
|
3808
|
+
runDashboard: () => runDashboard
|
|
3809
|
+
});
|
|
3810
|
+
import { spawn } from "node:child_process";
|
|
3811
|
+
import { createServer as createServer2 } from "node:http";
|
|
3812
|
+
import { connect as connect3 } from "node:net";
|
|
3813
|
+
import { platform as platform3 } from "node:os";
|
|
3814
|
+
import { join as join7 } from "node:path";
|
|
3815
|
+
function parseArgs(argv) {
|
|
3816
|
+
const args = { port: 8799, proxyPort: DEFAULT_PORT, ledgerDir: join7(MESH_HOME, "ledger"), open: true };
|
|
3817
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
3818
|
+
const key = argv[i];
|
|
3819
|
+
if (key === "--help" || key === "-h") {
|
|
3820
|
+
console.log(
|
|
3821
|
+
`Usage: mesh dashboard [options]
|
|
3822
|
+
|
|
3823
|
+
--port <n> Port for the dashboard (default 8799).
|
|
3824
|
+
--proxy-port <n> Proxy port to show as up/down (default ${DEFAULT_PORT}).
|
|
3825
|
+
--ledger <dir> Ledger to read (default ~/.mesh/ledger).
|
|
3826
|
+
--no-open Don't open a browser; just print the URL.
|
|
3827
|
+
--help Show this message.`
|
|
3828
|
+
);
|
|
3829
|
+
process.exit(0);
|
|
3830
|
+
}
|
|
3831
|
+
if (key === "--no-open") {
|
|
3832
|
+
args.open = false;
|
|
3833
|
+
continue;
|
|
3834
|
+
}
|
|
3835
|
+
const value = argv[i + 1];
|
|
3836
|
+
if (value === void 0 || value.startsWith("--")) throw new Error(`${key} needs a value.`);
|
|
3837
|
+
i += 1;
|
|
3838
|
+
if (key === "--port") args.port = Number.parseInt(value, 10);
|
|
3839
|
+
else if (key === "--proxy-port") args.proxyPort = Number.parseInt(value, 10);
|
|
3840
|
+
else if (key === "--ledger") args.ledgerDir = value;
|
|
3841
|
+
else throw new Error(`Unknown option: ${key}. See mesh dashboard --help.`);
|
|
3842
|
+
}
|
|
3843
|
+
return args;
|
|
3844
|
+
}
|
|
3845
|
+
function proxyUp(port) {
|
|
3846
|
+
return new Promise((resolve3) => {
|
|
3847
|
+
const sock = connect3({ host: "127.0.0.1", port }, () => {
|
|
3848
|
+
sock.destroy();
|
|
3849
|
+
resolve3(true);
|
|
3850
|
+
});
|
|
3851
|
+
sock.setTimeout(400, () => {
|
|
3852
|
+
sock.destroy();
|
|
3853
|
+
resolve3(false);
|
|
3854
|
+
});
|
|
3855
|
+
sock.on("error", () => resolve3(false));
|
|
3856
|
+
});
|
|
3857
|
+
}
|
|
3858
|
+
async function buildData(args) {
|
|
3859
|
+
const { records, chainOk, brokenAt } = readLedger(args.ledgerDir);
|
|
3860
|
+
const invoice = records.length ? recomputeInvoice(records.map((r) => r.event)) : null;
|
|
3861
|
+
const policy = readPolicy();
|
|
3862
|
+
const active = (policy.levers ?? []).map((l) => ({ lever: l.lever, class: l.class }));
|
|
3863
|
+
return {
|
|
3864
|
+
build: BUILD_COMMIT,
|
|
3865
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3866
|
+
chainOk,
|
|
3867
|
+
brokenAt: brokenAt ?? null,
|
|
3868
|
+
proxyUp: await proxyUp(args.proxyPort),
|
|
3869
|
+
savings: invoice ? {
|
|
3870
|
+
netUsd: invoice.netVerifiedSavingsUsd,
|
|
3871
|
+
verdict: invoice.verdict,
|
|
3872
|
+
eventsConsidered: invoice.eventsConsidered,
|
|
3873
|
+
eventsBilled: invoice.eventsBilled,
|
|
3874
|
+
unpricedEvents: invoice.unpricedEvents
|
|
3875
|
+
} : { netUsd: 0, verdict: "do_not_charge", eventsConsidered: 0, eventsBilled: 0, unpricedEvents: 0 },
|
|
3876
|
+
lineItems: invoice?.lineItems ?? [],
|
|
3877
|
+
recordedUnbilled: invoice?.recordedUnbilled ?? [],
|
|
3878
|
+
theo: {
|
|
3879
|
+
qualityCeilingPercent: policy.qualityCeilingPercent,
|
|
3880
|
+
active,
|
|
3881
|
+
withheld: withheldLevers(policy),
|
|
3882
|
+
coverage: policyCoverage(policy)
|
|
3883
|
+
}
|
|
3884
|
+
};
|
|
3885
|
+
}
|
|
3886
|
+
async function runDashboard(argv) {
|
|
3887
|
+
const args = parseArgs(argv);
|
|
3888
|
+
const server = createServer2(async (req, res) => {
|
|
3889
|
+
try {
|
|
3890
|
+
if (req.url === "/data") {
|
|
3891
|
+
res.writeHead(200, { "content-type": "application/json", "cache-control": "no-store" });
|
|
3892
|
+
res.end(JSON.stringify(await buildData(args)));
|
|
3893
|
+
return;
|
|
3894
|
+
}
|
|
3895
|
+
if (req.url === "/" || req.url === "") {
|
|
3896
|
+
res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" });
|
|
3897
|
+
res.end(PAGE);
|
|
3898
|
+
return;
|
|
3899
|
+
}
|
|
3900
|
+
res.writeHead(404);
|
|
3901
|
+
res.end("not found");
|
|
3902
|
+
} catch (error) {
|
|
3903
|
+
res.writeHead(500);
|
|
3904
|
+
res.end(String(error));
|
|
3905
|
+
}
|
|
3906
|
+
});
|
|
3907
|
+
await new Promise((resolve3) => server.listen(args.port, "127.0.0.1", resolve3));
|
|
3908
|
+
const url = `http://127.0.0.1:${server.address().port}`;
|
|
3909
|
+
console.log(`
|
|
3910
|
+
MESH dashboard \xB7 ${url}`);
|
|
3911
|
+
console.log(` Live view of your verified savings and what Theo is doing. Ctrl+C to stop.
|
|
3912
|
+
`);
|
|
3913
|
+
if (args.open) openBrowser(url);
|
|
3914
|
+
const shutdown = () => {
|
|
3915
|
+
server.close();
|
|
3916
|
+
process.exit(0);
|
|
3917
|
+
};
|
|
3918
|
+
process.on("SIGINT", shutdown);
|
|
3919
|
+
process.on("SIGTERM", shutdown);
|
|
3920
|
+
await new Promise(() => {
|
|
3921
|
+
});
|
|
3922
|
+
}
|
|
3923
|
+
function openBrowser(url) {
|
|
3924
|
+
const cmd = platform3() === "darwin" ? "open" : platform3() === "win32" ? "start" : "xdg-open";
|
|
3925
|
+
try {
|
|
3926
|
+
spawn(cmd, [url], { stdio: "ignore", detached: true, shell: platform3() === "win32" }).unref();
|
|
3927
|
+
} catch {
|
|
3928
|
+
}
|
|
3929
|
+
}
|
|
3930
|
+
var SQUIGGLE, DOTS, DOTS_GLOW, PAGE;
|
|
3931
|
+
var init_dashboard = __esm({
|
|
3932
|
+
"packages/cli/src/dashboard.ts"() {
|
|
3933
|
+
init_savings_ledger();
|
|
3934
|
+
init_theo_policy();
|
|
3935
|
+
init_start();
|
|
3936
|
+
SQUIGGLE = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 80 10'%3E%3Cpath d='M0 5 q6 -4 12 0 t12 0 t12 0 t12 0 t12 0 t12 0' fill='none' stroke='%232563EB' stroke-width='1.6' opacity='.7'/%3E%3C/svg%3E";
|
|
3937
|
+
DOTS = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='34' height='59'%3E%3Cg fill='%230B0F14' fill-opacity='0.05'%3E%3Ccircle cx='0' cy='0' r='1.1'/%3E%3Ccircle cx='34' cy='0' r='1.1'/%3E%3Ccircle cx='0' cy='59' r='1.1'/%3E%3Ccircle cx='34' cy='59' r='1.1'/%3E%3Ccircle cx='17' cy='29.5' r='1.1'/%3E%3C/g%3E%3C/svg%3E";
|
|
3938
|
+
DOTS_GLOW = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='34' height='59'%3E%3Cg fill='%232563EB' fill-opacity='0.55'%3E%3Ccircle cx='0' cy='0' r='1.2'/%3E%3Ccircle cx='34' cy='0' r='1.2'/%3E%3Ccircle cx='0' cy='59' r='1.2'/%3E%3Ccircle cx='34' cy='59' r='1.2'/%3E%3Ccircle cx='17' cy='29.5' r='1.2'/%3E%3C/g%3E%3C/svg%3E";
|
|
3939
|
+
PAGE = `<!doctype html><html lang="en"><head><meta charset="utf-8">
|
|
3940
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
3941
|
+
<title>mesh \u2014 live</title>
|
|
3942
|
+
<style>
|
|
3943
|
+
:root{color-scheme:light;
|
|
3944
|
+
--bg:#FAFAF9;--ink:#0B0F14;--muted:#5B6472;--line:#E5E7EB;--accent:#2563EB;--ocean:#1D4ED8;
|
|
3945
|
+
--paper:#ffffff;--stone:#E7E5E4;--stone-line:#CFCAC3;--stone-muted:#57534E;--bad:#DC2626}
|
|
3946
|
+
*{box-sizing:border-box}
|
|
3947
|
+
body{margin:0;background:var(--bg);color:var(--ink);font:16px/1.5 Manrope,-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;padding:40px 24px}
|
|
3948
|
+
.dotpaper{position:fixed;inset:0;z-index:0;pointer-events:none}
|
|
3949
|
+
.dotpaper .base,.dotpaper .glow{position:absolute;inset:0}
|
|
3950
|
+
.dotpaper .base{background:url("${DOTS}")}
|
|
3951
|
+
.dotpaper .glow{opacity:0;transition:opacity .4s ease;background:url("${DOTS_GLOW}");
|
|
3952
|
+
-webkit-mask-image:radial-gradient(circle 150px at var(--mx) var(--my),#000 0%,rgba(0,0,0,.35) 48%,transparent 72%);
|
|
3953
|
+
mask-image:radial-gradient(circle 150px at var(--mx) var(--my),#000 0%,rgba(0,0,0,.35) 48%,transparent 72%)}
|
|
3954
|
+
.dotpaper.lit .glow{opacity:1}
|
|
3955
|
+
@media (prefers-reduced-motion:reduce){.dotpaper .glow{display:none}}
|
|
3956
|
+
.wrap{max-width:900px;margin:0 auto;position:relative;z-index:1}
|
|
3957
|
+
header{display:flex;align-items:baseline;justify-content:space-between;gap:12px;margin-bottom:22px}
|
|
3958
|
+
.mark{font-size:22px;font-weight:800;letter-spacing:-.02em}
|
|
3959
|
+
.mark .dot{color:var(--accent)}
|
|
3960
|
+
.mark .live{font-size:14px;font-weight:600;color:var(--muted);margin-left:8px;letter-spacing:0}
|
|
3961
|
+
.build{font-size:13px;color:var(--muted)}
|
|
3962
|
+
.hero{background:var(--paper);border:1px solid var(--line);border-radius:14px;padding:26px 28px 24px;margin-bottom:16px}
|
|
3963
|
+
.cap{font-size:14px;color:var(--muted)}
|
|
3964
|
+
.saved{font-size:clamp(46px,9vw,92px);font-weight:800;letter-spacing:-.04em;line-height:.95;color:var(--ocean);margin:4px 0 10px}
|
|
3965
|
+
.saved .cur{color:var(--accent)}
|
|
3966
|
+
.verdict{display:inline-block;font-size:13px;font-weight:700;padding:5px 13px;border-radius:999px}
|
|
3967
|
+
.verdict.charge{background:rgba(37,99,235,.12);color:var(--accent)}
|
|
3968
|
+
.verdict.do_not_charge{background:var(--stone);color:var(--stone-muted)}
|
|
3969
|
+
.events{font-size:14px;color:var(--muted);margin-left:12px}
|
|
3970
|
+
.grid{display:grid;grid-template-columns:1fr;gap:16px}@media(min-width:720px){.grid{grid-template-columns:1fr 1fr}}
|
|
3971
|
+
.panel{background:var(--paper);border:1px solid var(--line);border-radius:14px;padding:22px}
|
|
3972
|
+
.panel h2{font-size:12px;letter-spacing:.06em;text-transform:uppercase;color:var(--muted);font-weight:700;margin:0 0 16px;position:relative;padding-bottom:11px}
|
|
3973
|
+
.panel h2::after{content:"";position:absolute;left:0;bottom:0;width:72px;height:9px;background:url("${SQUIGGLE}") left/contain no-repeat}
|
|
3974
|
+
.theo .hex{color:var(--accent);font-weight:800}
|
|
3975
|
+
.sub{font-size:13px;color:var(--muted);margin:0 0 7px}
|
|
3976
|
+
.chip{display:inline-block;font-size:13px;font-weight:600;padding:5px 12px;border-radius:999px;margin:0 6px 7px 0;background:#fff;border:1px solid var(--line)}
|
|
3977
|
+
.chip.on{border-color:var(--accent);color:var(--accent);background:rgba(37,99,235,.06)}
|
|
3978
|
+
.chip.on .k{color:var(--muted);font-weight:500}
|
|
3979
|
+
.chip.held{border-color:var(--stone-line);color:var(--stone-muted);background:var(--stone)}
|
|
3980
|
+
.chip.avail{border-style:dashed;border-color:var(--stone-line);color:var(--muted);background:#fff}
|
|
3981
|
+
.coverage{font-size:13.5px;color:var(--muted);line-height:1.4}
|
|
3982
|
+
.coverage .n{color:var(--ink);font-weight:800}
|
|
3983
|
+
.coverage .pct{color:var(--accent);font-weight:700}
|
|
3984
|
+
.meter{margin-top:9px;height:8px;border-radius:999px;background:var(--stone);overflow:hidden}
|
|
3985
|
+
.meter .fill{height:100%;width:0;background:var(--accent);border-radius:999px;transition:width .5s ease}
|
|
3986
|
+
.ceiling{font-size:13px;color:var(--muted);margin-top:14px;line-height:1.5}
|
|
3987
|
+
table{width:100%;border-collapse:collapse;font-size:14px}
|
|
3988
|
+
th,td{text-align:left;padding:8px 8px;border-bottom:1px solid var(--line)}
|
|
3989
|
+
th{color:var(--muted);font-weight:600;font-size:13px}
|
|
3990
|
+
td.num{text-align:right;font-variant-numeric:tabular-nums}
|
|
3991
|
+
td .k{color:var(--muted)}
|
|
3992
|
+
.empty{color:var(--muted);font-size:14px}
|
|
3993
|
+
.unbilled{font-size:13px;color:var(--muted);margin-top:12px;line-height:1.5}
|
|
3994
|
+
.foot{margin-top:18px;font-size:13px;color:var(--muted);display:flex;gap:18px;flex-wrap:wrap}
|
|
3995
|
+
.ind{display:inline-block;width:8px;height:8px;border-radius:50%;margin-right:7px;vertical-align:1px;background:var(--stone-line)}
|
|
3996
|
+
.ind.ok{background:var(--accent)} .ind.bad{background:var(--bad)}
|
|
3997
|
+
</style></head><body>
|
|
3998
|
+
<div class="dotpaper" id="dp"><div class="base"></div><div class="glow"></div></div>
|
|
3999
|
+
<div class="wrap">
|
|
4000
|
+
<header>
|
|
4001
|
+
<div class="mark">mesh<span class="dot">.</span><span class="live">live</span></div>
|
|
4002
|
+
<span class="build" id="build"></span>
|
|
4003
|
+
</header>
|
|
4004
|
+
<div class="hero">
|
|
4005
|
+
<div class="cap">Verified savings on your own traffic</div>
|
|
4006
|
+
<div class="saved" id="saved"><span class="cur">$</span>0.0000</div>
|
|
4007
|
+
<span class="verdict do_not_charge" id="verdict">\u2014</span>
|
|
4008
|
+
<span class="events" id="events"></span>
|
|
4009
|
+
</div>
|
|
4010
|
+
<div class="grid">
|
|
4011
|
+
<div class="panel theo">
|
|
4012
|
+
<h2><span class="hex">\u2B22 Theo</span> \u2014 what he's doing</h2>
|
|
4013
|
+
<div class="coverage" id="coverage"></div>
|
|
4014
|
+
<div class="meter"><div class="fill" id="coverfill"></div></div>
|
|
4015
|
+
<p class="sub" style="margin-top:16px">Applying now</p>
|
|
4016
|
+
<div id="active"><span class="empty">\u2014</span></div>
|
|
4017
|
+
<p class="sub" style="margin-top:14px">Held behind the replay-gate</p>
|
|
4018
|
+
<div id="withheld"><span class="empty">nothing held</span></div>
|
|
4019
|
+
<p class="sub" style="margin-top:14px">Not yet adopted</p>
|
|
4020
|
+
<div id="available"><span class="empty">\u2014</span></div>
|
|
4021
|
+
<div class="ceiling" id="ceiling"></div>
|
|
4022
|
+
</div>
|
|
4023
|
+
<div class="panel">
|
|
4024
|
+
<h2>Savings by lever</h2>
|
|
4025
|
+
<table><thead><tr><th>lever</th><th>kind</th><th class="num">events</th><th class="num">saved</th></tr></thead>
|
|
4026
|
+
<tbody id="levers"><tr><td colspan="4" class="empty">No billable savings yet.</td></tr></tbody></table>
|
|
4027
|
+
<div class="unbilled" id="unbilled"></div>
|
|
4028
|
+
</div>
|
|
4029
|
+
</div>
|
|
4030
|
+
<div class="foot">
|
|
4031
|
+
<span id="proxy"><span class="ind bad"></span>proxy</span>
|
|
4032
|
+
<span id="chain"><span class="ind ok"></span>ledger</span>
|
|
4033
|
+
<span id="updated"></span>
|
|
4034
|
+
</div>
|
|
4035
|
+
</div>
|
|
4036
|
+
<script>
|
|
4037
|
+
const $=(id)=>document.getElementById(id);
|
|
4038
|
+
const usd=(n)=>Number(n||0).toFixed(4);
|
|
4039
|
+
const dp=$('dp');
|
|
4040
|
+
addEventListener('pointermove',(e)=>{dp.classList.add('lit');dp.style.setProperty('--mx',e.clientX+'px');dp.style.setProperty('--my',e.clientY+'px');});
|
|
4041
|
+
async function tick(){
|
|
4042
|
+
try{
|
|
4043
|
+
const d=await (await fetch('/data',{cache:'no-store'})).json();
|
|
4044
|
+
$('build').textContent='build '+d.build;
|
|
4045
|
+
$('saved').innerHTML='<span class="cur">$</span>'+usd(d.savings.netUsd);
|
|
4046
|
+
const v=$('verdict'); v.textContent=d.savings.verdict==='charge'?'billing':'do not charge'; v.className='verdict '+d.savings.verdict;
|
|
4047
|
+
$('events').textContent=d.savings.eventsConsidered+' requests'+(d.savings.unpricedEvents?(' \xB7 '+d.savings.unpricedEvents+' unpriced (recorded, not billed)'):'');
|
|
4048
|
+
const cov=d.theo.coverage;
|
|
4049
|
+
$('coverage').innerHTML='Theo is live on <span class="n">'+cov.activeCount+'</span> of <span class="n">'+cov.enforceableCount+'</span> levers he can enforce <span class="pct">('+cov.activeCoveragePercent.toFixed(0)+'%)</span> \xB7 '+cov.heldCount+' held \xB7 '+cov.availableCount+' not yet adopted';
|
|
4050
|
+
$('coverfill').style.width=cov.activeCoveragePercent.toFixed(1)+'%';
|
|
4051
|
+
const avail=cov.stands.filter(s=>s.state==='available');
|
|
4052
|
+
$('available').innerHTML=avail.length?avail.map(s=>'<span class="chip avail" title="Theo can enforce this; he has not adopted it for you yet.">'+s.label+'</span>').join(''):'<span class="empty">none \u2014 Theo has adopted every lever he can</span>';
|
|
4053
|
+
$('active').innerHTML=d.theo.active.length?d.theo.active.map(l=>'<span class="chip on">'+l.lever+'<span class="k"> \xB7 '+l.class+'</span></span>').join(''):'<span class="empty">none</span>';
|
|
4054
|
+
$('withheld').innerHTML=d.theo.withheld.length?d.theo.withheld.map(w=>'<span class="chip held" title="'+(w.reason||'')+'">'+w.lever+'</span>').join(''):'<span class="empty">nothing held</span>';
|
|
4055
|
+
$('ceiling').textContent='Quality ceiling: '+d.theo.qualityCeilingPercent+'% \u2014 anything that could cost more than this stays off until a replay of your traffic proves it safe.';
|
|
4056
|
+
const rows=d.lineItems;
|
|
4057
|
+
$('levers').innerHTML=rows.length?rows.map(li=>'<tr><td>'+li.leverId+'</td><td class="k">'+li.kind+(li.basis?' \xB7 '+li.basis:'')+'</td><td class="num">'+li.events+'</td><td class="num">$'+usd(li.savedUsd)+'</td></tr>').join(''):'<tr><td colspan="4" class="empty">No billable savings yet \u2014 run Claude Code through MESH and this fills in.</td></tr>';
|
|
4058
|
+
$('unbilled').textContent=d.recordedUnbilled.length?('Also recorded (applied, never billed): '+d.recordedUnbilled.map(u=>u.leverId+' \xD7'+u.events).join(', ')):'';
|
|
4059
|
+
$('proxy').innerHTML='<span class="ind '+(d.proxyUp?'ok':'bad')+'"></span>proxy '+(d.proxyUp?'up':'down');
|
|
4060
|
+
$('chain').innerHTML='<span class="ind '+(d.chainOk?'ok':'bad')+'"></span>ledger '+(d.chainOk?'verified':('BROKEN at '+d.brokenAt));
|
|
4061
|
+
$('updated').textContent='updated '+new Date(d.updatedAt).toLocaleTimeString();
|
|
4062
|
+
}catch(e){ $('updated').textContent='reconnecting\u2026'; }
|
|
4063
|
+
}
|
|
4064
|
+
tick(); setInterval(tick,3000);
|
|
4065
|
+
</script></body></html>`;
|
|
4066
|
+
}
|
|
4067
|
+
});
|
|
4068
|
+
|
|
4069
|
+
// packages/core/src/token-reduction.ts
|
|
4070
|
+
var init_token_reduction2 = __esm({
|
|
4071
|
+
"packages/core/src/token-reduction.ts"() {
|
|
4072
|
+
"use strict";
|
|
4073
|
+
init_cost();
|
|
4074
|
+
}
|
|
4075
|
+
});
|
|
4076
|
+
|
|
4077
|
+
// packages/core/src/cache-simulator.ts
|
|
4078
|
+
function ledgerTurnsByTask(events) {
|
|
4079
|
+
const byTask = /* @__PURE__ */ new Map();
|
|
4080
|
+
for (const event of events) {
|
|
4081
|
+
if (event.status !== "succeeded" || !event.usage) continue;
|
|
4082
|
+
const key = event.taskId ?? `__single:${event.requestId}`;
|
|
4083
|
+
const turn = {
|
|
4084
|
+
...event.taskId === void 0 ? {} : { taskId: event.taskId },
|
|
4085
|
+
turnIndex: event.turnIndex ?? 0,
|
|
4086
|
+
timestampMs: Date.parse(event.timestamp),
|
|
4087
|
+
inputTokens: event.usage.inputTokens,
|
|
4088
|
+
outputTokens: event.usage.outputTokens,
|
|
4089
|
+
cachedInputTokens: event.usage.cachedInputTokens
|
|
4090
|
+
};
|
|
4091
|
+
const bucket = byTask.get(key);
|
|
4092
|
+
if (bucket) bucket.push(turn);
|
|
4093
|
+
else byTask.set(key, [turn]);
|
|
4094
|
+
}
|
|
4095
|
+
for (const bucket of byTask.values()) {
|
|
4096
|
+
bucket.sort((left, right) => left.turnIndex - right.turnIndex || (left.timestampMs ?? 0) - (right.timestampMs ?? 0));
|
|
4097
|
+
}
|
|
4098
|
+
return byTask;
|
|
4099
|
+
}
|
|
4100
|
+
function inputRatePerToken(price) {
|
|
4101
|
+
return price.inputPerMillion / 1e6;
|
|
4102
|
+
}
|
|
4103
|
+
function cachedRatePerToken(price) {
|
|
4104
|
+
return (price.cachedInputPerMillion ?? price.inputPerMillion * 0.1) / 1e6;
|
|
4105
|
+
}
|
|
4106
|
+
function recordedTurnCostUsd(turn, price) {
|
|
4107
|
+
const cached = Math.min(turn.cachedInputTokens, turn.inputTokens);
|
|
4108
|
+
const fresh = turn.inputTokens - cached;
|
|
4109
|
+
return fresh * inputRatePerToken(price) + cached * cachedRatePerToken(price) + turn.outputTokens / 1e6 * price.outputPerMillion;
|
|
4110
|
+
}
|
|
4111
|
+
function estimateCompactionFromLedger(turns, compaction, price) {
|
|
4112
|
+
if (turns.length < 2) throw new Error("Compaction needs at least two recorded turns.");
|
|
4113
|
+
if (compaction.atTurn < 2 || compaction.atTurn > turns.length) {
|
|
4114
|
+
throw new Error("Compaction must happen after the first turn and within the task.");
|
|
4115
|
+
}
|
|
4116
|
+
const ordered = [...turns].sort((left, right) => left.turnIndex - right.turnIndex || (left.timestampMs ?? 0) - (right.timestampMs ?? 0));
|
|
4117
|
+
const preservedPrefixTokens = compaction.preservedPrefixTokens ?? ordered.reduce((min, turn) => Math.min(min, turn.inputTokens), Infinity);
|
|
4118
|
+
const contextBefore = ordered[compaction.atTurn - 2].inputTokens;
|
|
4119
|
+
const removedTokensPerTurn = Math.max(
|
|
4120
|
+
0,
|
|
4121
|
+
contextBefore - preservedPrefixTokens - compaction.summaryTokens
|
|
4122
|
+
);
|
|
4123
|
+
const post = ordered.slice(compaction.atTurn - 1);
|
|
4124
|
+
const perTurnSavingUsd = post.map((turn) => {
|
|
4125
|
+
const rate = turn.cachedInputTokens > 0 ? cachedRatePerToken(price) : inputRatePerToken(price);
|
|
4126
|
+
return removedTokensPerTurn * rate;
|
|
4127
|
+
});
|
|
4128
|
+
const grossSavingUsd = perTurnSavingUsd.reduce((sum, value) => sum + value, 0);
|
|
4129
|
+
const summarizationCostUsd = contextBefore * inputRatePerToken(price) + compaction.summaryTokens * inputRatePerToken(price) + compaction.summaryTokens / 1e6 * price.outputPerMillion;
|
|
4130
|
+
const netSavingUsd = grossSavingUsd - summarizationCostUsd;
|
|
4131
|
+
const recordedCostUsd = ordered.reduce((sum, turn) => sum + recordedTurnCostUsd(turn, price), 0);
|
|
4132
|
+
const savingPercent = recordedCostUsd === 0 ? 0 : netSavingUsd / recordedCostUsd * 100;
|
|
4133
|
+
let breakEvenTurns = null;
|
|
4134
|
+
let running = 0;
|
|
4135
|
+
for (let index = 0; index < perTurnSavingUsd.length; index += 1) {
|
|
4136
|
+
running += perTurnSavingUsd[index];
|
|
4137
|
+
if (running >= summarizationCostUsd) {
|
|
4138
|
+
breakEvenTurns = index;
|
|
4139
|
+
break;
|
|
4140
|
+
}
|
|
4141
|
+
}
|
|
4142
|
+
return {
|
|
4143
|
+
...ordered[0].taskId === void 0 ? {} : { taskId: ordered[0].taskId },
|
|
4144
|
+
turns: ordered.length,
|
|
4145
|
+
compactionTurn: compaction.atTurn,
|
|
4146
|
+
summaryTokens: compaction.summaryTokens,
|
|
4147
|
+
preservedPrefixTokens,
|
|
4148
|
+
removedTokensPerTurn,
|
|
4149
|
+
postCompactionTurns: post.length,
|
|
4150
|
+
recordedCostUsd,
|
|
4151
|
+
summarizationCostUsd,
|
|
4152
|
+
grossSavingUsd,
|
|
4153
|
+
netSavingUsd,
|
|
4154
|
+
savingPercent,
|
|
4155
|
+
worthwhile: netSavingUsd > 0,
|
|
4156
|
+
breakEvenTurns
|
|
4157
|
+
};
|
|
4158
|
+
}
|
|
4159
|
+
var ANTHROPIC_NO_CACHE;
|
|
4160
|
+
var init_cache_simulator = __esm({
|
|
4161
|
+
"packages/core/src/cache-simulator.ts"() {
|
|
4162
|
+
"use strict";
|
|
4163
|
+
init_cost();
|
|
4164
|
+
init_token_reduction2();
|
|
4165
|
+
ANTHROPIC_NO_CACHE = {
|
|
4166
|
+
name: "anthropic-uncached",
|
|
4167
|
+
minimumCacheableTokens: Number.MAX_SAFE_INTEGER,
|
|
4168
|
+
incrementTokens: 1,
|
|
4169
|
+
ttlSeconds: 0,
|
|
4170
|
+
writeMultiplier: 1,
|
|
4171
|
+
readMultiplier: 1
|
|
4172
|
+
};
|
|
4173
|
+
}
|
|
4174
|
+
});
|
|
4175
|
+
|
|
4176
|
+
// packages/core/src/model-prices.ts
|
|
4177
|
+
function entry2(provider, model, provenance, bands, mode = "standard", effectiveFrom = OBSERVED_FROM2) {
|
|
4178
|
+
return {
|
|
4179
|
+
provider,
|
|
4180
|
+
model,
|
|
4181
|
+
mode,
|
|
4182
|
+
effectiveFrom,
|
|
4183
|
+
provenance,
|
|
4184
|
+
tiers: bands.map((band) => ({
|
|
4185
|
+
...band.maxInputTokens === void 0 ? {} : { maxInputTokens: band.maxInputTokens },
|
|
4186
|
+
inputPerMillion: band.input,
|
|
4187
|
+
outputPerMillion: band.output,
|
|
4188
|
+
...band.cachedInput === void 0 ? {} : { cachedInputPerMillion: band.cachedInput },
|
|
4189
|
+
...band.cacheWriteInput === void 0 ? {} : { cacheWriteInputPerMillion: band.cacheWriteInput },
|
|
4190
|
+
...band.cacheWrite1hInput === void 0 ? {} : { cacheWrite1hInputPerMillion: band.cacheWrite1hInput }
|
|
4191
|
+
}))
|
|
4192
|
+
};
|
|
4193
|
+
}
|
|
4194
|
+
var ANTHROPIC2, OPENAI2, ANTHROPIC_BATCH2, DEEPSEEK2, OBSERVED_FROM2, anthropic2, openai2, deepseek2, aliases2, publishedModelPrices2;
|
|
4195
|
+
var init_model_prices2 = __esm({
|
|
4196
|
+
"packages/core/src/model-prices.ts"() {
|
|
4197
|
+
"use strict";
|
|
4198
|
+
init_price_catalogue();
|
|
4199
|
+
ANTHROPIC2 = {
|
|
4200
|
+
source: "https://claude.com/pricing",
|
|
4201
|
+
capturedOn: "2026-10-08",
|
|
4202
|
+
note: "Published display names; cache read/write rates as listed. 5m cache writes bill 1.25x base input, 1h cache writes 2x base input, per the published prompt-caching pricing."
|
|
4203
|
+
};
|
|
4204
|
+
OPENAI2 = {
|
|
4205
|
+
source: "https://developers.openai.com/api/docs/pricing",
|
|
4206
|
+
capturedOn: "2026-10-08",
|
|
4207
|
+
note: "Standard tier unless the entry says otherwise."
|
|
4208
|
+
};
|
|
4209
|
+
ANTHROPIC_BATCH2 = {
|
|
4210
|
+
source: "https://claude.com/pricing",
|
|
4211
|
+
capturedOn: "2026-10-27",
|
|
4212
|
+
note: `Batch processing = 50% of standard, per the page's definitive "Save 50% with batch processing". Derived by halving the dated standard rates; cache-write premiums under batch are not published and are omitted (priced unpriced, never guessed).`
|
|
4213
|
+
};
|
|
4214
|
+
DEEPSEEK2 = {
|
|
4215
|
+
source: "https://api-docs.deepseek.com/quick_start/pricing",
|
|
4216
|
+
capturedOn: "2026-11-03",
|
|
4217
|
+
note: "Off-peak list rates (peak is 2x in the 01-10 UTC weekday windows); catalogued at off-peak so a saving is never over-valued. Per 1M tokens."
|
|
4218
|
+
};
|
|
4219
|
+
OBSERVED_FROM2 = "2026-10-08";
|
|
4220
|
+
anthropic2 = [
|
|
4221
|
+
entry2("anthropic", "Fable 5.1", ANTHROPIC2, [
|
|
4222
|
+
{ input: 10, output: 50, cachedInput: 0.25, cacheWriteInput: 12.5, cacheWrite1hInput: 20 }
|
|
4223
|
+
]),
|
|
4224
|
+
entry2("anthropic", "Opus 5.5", ANTHROPIC2, [
|
|
4225
|
+
{ input: 4, output: 20, cachedInput: 0.2, cacheWriteInput: 5, cacheWrite1hInput: 8 }
|
|
4226
|
+
]),
|
|
4227
|
+
entry2("anthropic", "Sonnet 5.5", ANTHROPIC2, [
|
|
4228
|
+
{ input: 2, output: 10, cachedInput: 0.1, cacheWriteInput: 2.5, cacheWrite1hInput: 4 }
|
|
4229
|
+
]),
|
|
4230
|
+
// Haiku 5.5 is tiered by prompt size: the higher band applies above 100K input tokens.
|
|
4231
|
+
entry2("anthropic", "Haiku 5.5", ANTHROPIC2, [
|
|
4232
|
+
{ maxInputTokens: 1e5, input: 0.1, output: 0.5, cachedInput: 0.01, cacheWriteInput: 0.125, cacheWrite1hInput: 0.2 },
|
|
4233
|
+
{ input: 0.5, output: 2.5, cachedInput: 0.05, cacheWriteInput: 0.625, cacheWrite1hInput: 1 }
|
|
4234
|
+
]),
|
|
4235
|
+
// Batch mode: 50% of standard, per the definitive "Save 50% with batch processing"
|
|
4236
|
+
// (ANTHROPIC_BATCH provenance). Each rate is exactly half its standard entry above.
|
|
4237
|
+
entry2("anthropic", "Fable 5.1", ANTHROPIC_BATCH2, [
|
|
4238
|
+
{ input: 5, output: 25, cachedInput: 0.125 }
|
|
4239
|
+
], "batch", "2026-10-27"),
|
|
4240
|
+
entry2("anthropic", "Opus 5.5", ANTHROPIC_BATCH2, [
|
|
4241
|
+
{ input: 2, output: 10, cachedInput: 0.1 }
|
|
4242
|
+
], "batch", "2026-10-27"),
|
|
4243
|
+
entry2("anthropic", "Sonnet 5.5", ANTHROPIC_BATCH2, [
|
|
4244
|
+
{ input: 1, output: 5, cachedInput: 0.05 }
|
|
4245
|
+
], "batch", "2026-10-27"),
|
|
4246
|
+
entry2("anthropic", "Haiku 5.5", ANTHROPIC_BATCH2, [
|
|
4247
|
+
{ maxInputTokens: 1e5, input: 0.05, output: 0.25, cachedInput: 5e-3 },
|
|
4248
|
+
{ input: 0.25, output: 1.25, cachedInput: 0.025 }
|
|
4249
|
+
], "batch", "2026-10-27")
|
|
4250
|
+
];
|
|
4251
|
+
openai2 = [
|
|
4252
|
+
entry2("openai", "gpt-6-astra", OPENAI2, [{ input: 10, output: 50, cachedInput: 1 }]),
|
|
4253
|
+
entry2("openai", "gpt-6.1-sol", OPENAI2, [{ input: 2, output: 10, cachedInput: 0.1 }]),
|
|
4254
|
+
entry2("openai", "gpt-6-luna", OPENAI2, [{ input: 0.1, output: 0.5, cachedInput: 0.01 }]),
|
|
4255
|
+
entry2("openai", "gpt-5.5", OPENAI2, [{ input: 5, output: 30, cachedInput: 0.5 }]),
|
|
4256
|
+
entry2("openai", "gpt-5.4", OPENAI2, [{ input: 2.5, output: 15, cachedInput: 0.25 }]),
|
|
4257
|
+
entry2("openai", "gpt-5.2", OPENAI2, [{ input: 1.75, output: 14, cachedInput: 0.175 }]),
|
|
4258
|
+
entry2("openai", "gpt-5", OPENAI2, [{ input: 1.25, output: 10, cachedInput: 0.125 }]),
|
|
4259
|
+
entry2("openai", "gpt-4o", OPENAI2, [{ input: 2.5, output: 10, cachedInput: 1.25 }]),
|
|
4260
|
+
entry2("openai", "gpt-4o-mini", OPENAI2, [{ input: 0.15, output: 0.6, cachedInput: 0.075 }]),
|
|
4261
|
+
entry2("openai", "o1", OPENAI2, [{ input: 15, output: 60, cachedInput: 7.5 }]),
|
|
4262
|
+
entry2("openai", "o3-mini", OPENAI2, [{ input: 1.1, output: 4.4, cachedInput: 0.55 }]),
|
|
4263
|
+
// Only the batch rate published explicitly is catalogued. The page describes batch
|
|
4264
|
+
// and flex as "typically" half of standard; that is not a rate, so it is not seeded.
|
|
4265
|
+
entry2("openai", "gpt-6-astra", OPENAI2, [{ input: 5, output: 25, cachedInput: 0.5 }], "batch"),
|
|
4266
|
+
// Flex processing is billed at Batch API rates = 50% of standard, on a
|
|
4267
|
+
// synchronous API, per OpenAI's flex-processing guide. Cached-input discount
|
|
4268
|
+
// stacks on top. Catalogued so the ledger can price flex-tier events rather
|
|
4269
|
+
// than leaving async savings unpriced.
|
|
4270
|
+
entry2("openai", "gpt-5", OPENAI2, [{ input: 0.625, output: 5, cachedInput: 0.0625 }], "flex")
|
|
4271
|
+
];
|
|
4272
|
+
deepseek2 = [
|
|
4273
|
+
entry2("deepseek", "deepseek-flash", DEEPSEEK2, [{ input: 0.15, output: 0.6, cachedInput: 3e-3 }], "standard", "2026-11-03"),
|
|
4274
|
+
entry2("deepseek", "deepseek-v4-pro", DEEPSEEK2, [{ input: 0.66, output: 1.98, cachedInput: 0.022 }], "standard", "2026-11-03")
|
|
4275
|
+
];
|
|
4276
|
+
aliases2 = [
|
|
4277
|
+
// Anthropic ids embed the published name, so normalisation covers dated
|
|
4278
|
+
// builds such as claude-sonnet-5-5-20260514. These cover the pointers.
|
|
4279
|
+
{ provider: "anthropic", id: "claude-sonnet-5-5-latest", model: "Sonnet 5.5" },
|
|
4280
|
+
{ provider: "anthropic", id: "claude-opus-5-5-latest", model: "Opus 5.5" },
|
|
4281
|
+
{ provider: "anthropic", id: "claude-haiku-5-5-latest", model: "Haiku 5.5" },
|
|
4282
|
+
{ provider: "anthropic", id: "claude-fable-5-1-latest", model: "Fable 5.1" },
|
|
4283
|
+
// OpenAI ids match their published names directly; these are the pointers
|
|
4284
|
+
// and the dated snapshots that do not normalise onto a catalogued key.
|
|
4285
|
+
{ provider: "openai", id: "chatgpt-4o-latest", model: "gpt-4o" },
|
|
4286
|
+
{ provider: "openai", id: "gpt-4o-2024-*", model: "gpt-4o" },
|
|
4287
|
+
{ provider: "openai", id: "gpt-4o-mini-2024-*", model: "gpt-4o-mini" },
|
|
4288
|
+
{ provider: "openai", id: "o3-mini-2025-*", model: "o3-mini" },
|
|
4289
|
+
// DeepSeek: the current ids match their keys (normalisation covers them); only
|
|
4290
|
+
// the legacy pointer needs an explicit map. deepseek-v4-flash bills at Flash,
|
|
4291
|
+
// per DeepSeek's pricing page.
|
|
4292
|
+
{ provider: "deepseek", id: "deepseek-v4-flash", model: "deepseek-flash" }
|
|
4293
|
+
];
|
|
4294
|
+
publishedModelPrices2 = validatePriceCatalogue({
|
|
4295
|
+
entries: [...anthropic2, ...openai2, ...deepseek2],
|
|
4296
|
+
negotiated: [],
|
|
4297
|
+
aliases: aliases2
|
|
4298
|
+
});
|
|
4299
|
+
}
|
|
4300
|
+
});
|
|
4301
|
+
|
|
4302
|
+
// packages/cli/src/compaction.ts
|
|
4303
|
+
var compaction_exports = {};
|
|
4304
|
+
__export(compaction_exports, {
|
|
4305
|
+
estimateLedger: () => estimateLedger,
|
|
4306
|
+
runCompaction: () => runCompaction
|
|
4307
|
+
});
|
|
4308
|
+
import { writeFileSync as writeFileSync4 } from "node:fs";
|
|
4309
|
+
import { join as join8, resolve as resolve2 } from "node:path";
|
|
4310
|
+
function parse(argv) {
|
|
4311
|
+
const args = {
|
|
4312
|
+
ledgerDir: join8(MESH_HOME, "ledger"),
|
|
4313
|
+
atTokens: 6e4,
|
|
4314
|
+
summaryTokens: 2e3,
|
|
4315
|
+
minTurns: 3,
|
|
4316
|
+
date: (/* @__PURE__ */ new Date()).toISOString().slice(0, 10)
|
|
4317
|
+
};
|
|
4318
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
4319
|
+
const key = argv[index];
|
|
4320
|
+
if (key === "--help" || key === "-h") {
|
|
4321
|
+
console.log(USAGE);
|
|
4322
|
+
process.exit(0);
|
|
4323
|
+
}
|
|
4324
|
+
const value = argv[index + 1];
|
|
4325
|
+
if (value === void 0 || value.startsWith("--")) throw new Error(`${key} needs a value.`);
|
|
4326
|
+
index += 1;
|
|
4327
|
+
switch (key) {
|
|
4328
|
+
case "--ledger":
|
|
4329
|
+
args.ledgerDir = resolve2(value);
|
|
4330
|
+
break;
|
|
4331
|
+
case "--at-tokens":
|
|
4332
|
+
args.atTokens = Number(value);
|
|
4333
|
+
break;
|
|
4334
|
+
case "--summary-tokens":
|
|
4335
|
+
args.summaryTokens = Number(value);
|
|
4336
|
+
break;
|
|
4337
|
+
case "--prefix-tokens":
|
|
4338
|
+
args.prefixTokens = Number(value);
|
|
4339
|
+
break;
|
|
4340
|
+
case "--min-turns":
|
|
4341
|
+
args.minTurns = Number(value);
|
|
4342
|
+
break;
|
|
4343
|
+
case "--date":
|
|
4344
|
+
args.date = value;
|
|
4345
|
+
break;
|
|
4346
|
+
case "--json":
|
|
4347
|
+
args.json = resolve2(value);
|
|
4348
|
+
break;
|
|
4349
|
+
default:
|
|
4350
|
+
throw new Error(`Unknown option: ${key}`);
|
|
4351
|
+
}
|
|
4352
|
+
}
|
|
4353
|
+
for (const [name, value] of [
|
|
4354
|
+
["--at-tokens", args.atTokens],
|
|
4355
|
+
["--summary-tokens", args.summaryTokens],
|
|
4356
|
+
["--min-turns", args.minTurns],
|
|
4357
|
+
...args.prefixTokens === void 0 ? [] : [["--prefix-tokens", args.prefixTokens]]
|
|
4358
|
+
]) {
|
|
4359
|
+
if (!Number.isFinite(value) || value < 0) throw new Error(`${name} must be a non-negative number.`);
|
|
4360
|
+
}
|
|
4361
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(args.date)) throw new Error("--date must be YYYY-MM-DD.");
|
|
4362
|
+
return args;
|
|
4363
|
+
}
|
|
4364
|
+
function modelByTask(events) {
|
|
4365
|
+
const map = /* @__PURE__ */ new Map();
|
|
4366
|
+
for (const event of events) {
|
|
4367
|
+
if (event.taskId === void 0 || map.has(event.taskId)) continue;
|
|
4368
|
+
map.set(event.taskId, { provider: event.provider, model: event.model });
|
|
4369
|
+
}
|
|
4370
|
+
return map;
|
|
4371
|
+
}
|
|
4372
|
+
function estimateLedger(events, args) {
|
|
4373
|
+
const byTask = ledgerTurnsByTask(events);
|
|
4374
|
+
const models = modelByTask(events);
|
|
4375
|
+
const estimates = [];
|
|
4376
|
+
const skipped = { unpriced: 0, noTrigger: 0, tooShort: 0 };
|
|
4377
|
+
for (const [key, turns] of byTask) {
|
|
4378
|
+
if (turns.length < args.minTurns) {
|
|
4379
|
+
skipped.tooShort += 1;
|
|
4380
|
+
continue;
|
|
4381
|
+
}
|
|
4382
|
+
const triggerIndex = turns.findIndex((turn) => turn.inputTokens >= args.atTokens);
|
|
4383
|
+
const atTurn = triggerIndex < 1 ? triggerIndex === 0 ? 2 : -1 : triggerIndex + 1;
|
|
4384
|
+
if (atTurn < 2) {
|
|
4385
|
+
skipped.noTrigger += 1;
|
|
4386
|
+
continue;
|
|
4387
|
+
}
|
|
4388
|
+
const meta = models.get(key);
|
|
4389
|
+
const resolved = meta ? resolveModelId(publishedModelPrices2, meta.provider, meta.model) : { status: "unresolved" };
|
|
4390
|
+
const lookup = resolved.status === "resolved" ? lookupPrice(publishedModelPrices2, { provider: meta.provider, model: resolved.model, at: args.date }) : { status: "unpriced" };
|
|
4391
|
+
if (lookup.status === "unpriced") {
|
|
4392
|
+
skipped.unpriced += 1;
|
|
4393
|
+
continue;
|
|
4394
|
+
}
|
|
4395
|
+
const estimate = estimateCompactionFromLedger(
|
|
4396
|
+
turns,
|
|
4397
|
+
{
|
|
4398
|
+
atTurn,
|
|
4399
|
+
summaryTokens: args.summaryTokens,
|
|
4400
|
+
...args.prefixTokens === void 0 ? {} : { preservedPrefixTokens: args.prefixTokens }
|
|
4401
|
+
},
|
|
4402
|
+
lookup.price
|
|
4403
|
+
);
|
|
4404
|
+
estimates.push({ ...estimate, model: resolved.status === "resolved" ? resolved.model : meta.model });
|
|
4405
|
+
}
|
|
4406
|
+
estimates.sort((left, right) => right.netSavingUsd - left.netSavingUsd);
|
|
4407
|
+
return { estimates, skipped };
|
|
4408
|
+
}
|
|
4409
|
+
function money(value) {
|
|
4410
|
+
const sign = value < 0 ? "-" : "";
|
|
4411
|
+
return `${sign}$${Math.abs(value).toFixed(4)}`;
|
|
4412
|
+
}
|
|
4413
|
+
async function runCompaction(argv) {
|
|
4414
|
+
const args = parse(argv);
|
|
4415
|
+
const { records, chainOk, brokenAt } = readLedger(args.ledgerDir);
|
|
4416
|
+
if (records.length === 0) {
|
|
4417
|
+
console.log(`No ledger events found in ${args.ledgerDir}.`);
|
|
4418
|
+
console.log(`Run some traffic through "mesh start" first, then try again.`);
|
|
4419
|
+
return;
|
|
4420
|
+
}
|
|
4421
|
+
if (!chainOk) {
|
|
4422
|
+
console.log(`\u26A0 Ledger hash-chain check failed at ${brokenAt}. Run "mesh verify" before trusting these numbers.`);
|
|
4423
|
+
}
|
|
4424
|
+
const { estimates, skipped } = estimateLedger(records.map((record) => record.event), args);
|
|
4425
|
+
if (args.json !== void 0) {
|
|
4426
|
+
writeFileSync4(args.json, `${JSON.stringify(estimates, null, 2)}
|
|
4427
|
+
`);
|
|
4428
|
+
}
|
|
4429
|
+
if (estimates.length === 0) {
|
|
4430
|
+
console.log(`No multi-turn task reached ${args.atTokens.toLocaleString()} input tokens, so nothing would have been compacted.`);
|
|
4431
|
+
console.log(`Lower --at-tokens to explore smaller tasks. (${records.length} events read.)`);
|
|
4432
|
+
return;
|
|
4433
|
+
}
|
|
4434
|
+
const totalRecorded = estimates.reduce((sum, e) => sum + e.recordedCostUsd, 0);
|
|
4435
|
+
const totalNet = estimates.reduce((sum, e) => sum + e.netSavingUsd, 0);
|
|
4436
|
+
const wins = estimates.filter((e) => e.worthwhile).length;
|
|
4437
|
+
console.log(`mesh compaction \u2014 advisory estimate on ${estimates.length} task(s), ${records.length} recorded events.`);
|
|
4438
|
+
console.log(`Trigger: compact at the first turn \u2265 ${args.atTokens.toLocaleString()} tokens; summary ${args.summaryTokens.toLocaleString()} tokens.
|
|
4439
|
+
`);
|
|
4440
|
+
console.log(` ${"task".padEnd(20)} ${"turns".padStart(5)} ${"at".padStart(3)} ${"recorded".padStart(10)} ${"net".padStart(10)} verdict`);
|
|
4441
|
+
for (const e of estimates) {
|
|
4442
|
+
const label = (e.taskId ?? "\u2014").slice(0, 20).padEnd(20);
|
|
4443
|
+
const verdict = e.worthwhile ? `save ${e.savingPercent.toFixed(0)}% (break-even +${e.breakEvenTurns ?? "?"})` : `net loss ${e.savingPercent.toFixed(0)}% \u2014 leave uncompacted`;
|
|
4444
|
+
console.log(` ${label} ${String(e.turns).padStart(5)} ${String(e.compactionTurn).padStart(3)} ${money(e.recordedCostUsd).padStart(10)} ${money(e.netSavingUsd).padStart(10)} ${verdict}`);
|
|
4445
|
+
}
|
|
4446
|
+
console.log(`
|
|
4447
|
+
${wins}/${estimates.length} task(s) worth compacting.`);
|
|
4448
|
+
console.log(` Recorded cost on these tasks: ${money(totalRecorded)}. Net if all compacted: ${money(totalNet)}.`);
|
|
4449
|
+
if (skipped.noTrigger + skipped.tooShort + skipped.unpriced > 0) {
|
|
4450
|
+
console.log(` Skipped: ${skipped.noTrigger} below the trigger, ${skipped.tooShort} too short, ${skipped.unpriced} unpriced.`);
|
|
4451
|
+
}
|
|
4452
|
+
console.log(`
|
|
4453
|
+
Advisory only: counterfactual savings, valued at the rate the ledger shows you paid.`);
|
|
4454
|
+
console.log(` Not the identical-byte savings the invoice bills on \u2014 nothing here changes your ledger.`);
|
|
4455
|
+
}
|
|
4456
|
+
var USAGE;
|
|
4457
|
+
var init_compaction = __esm({
|
|
4458
|
+
"packages/cli/src/compaction.ts"() {
|
|
4459
|
+
init_cache_simulator();
|
|
4460
|
+
init_price_catalogue();
|
|
4461
|
+
init_model_prices2();
|
|
4462
|
+
init_start();
|
|
4463
|
+
USAGE = `Usage: mesh compaction [options]
|
|
4464
|
+
|
|
4465
|
+
Read your recorded ledger and estimate what context compaction would save,
|
|
4466
|
+
per multi-turn task. Advisory only \u2014 counterfactual, never billed.
|
|
4467
|
+
|
|
4468
|
+
--ledger <dir> Ledger directory to read. Default ~/.mesh/ledger.
|
|
4469
|
+
--at-tokens <n> Compact at the first turn whose input reaches n tokens.
|
|
4470
|
+
Default 60000 (a typical harness trigger).
|
|
4471
|
+
--summary-tokens <n> Size of the summary that replaces the history. Default 2000.
|
|
4472
|
+
--prefix-tokens <n> Preserved system/tool prefix. Default: inferred per task
|
|
4473
|
+
as its smallest recorded input (a conservative floor).
|
|
4474
|
+
--min-turns <n> Ignore tasks shorter than this. Default 3.
|
|
4475
|
+
--date <YYYY-MM-DD> Pricing date for the catalogue lookup. Default: today.
|
|
4476
|
+
--json <path> Write the full per-task estimate as JSON.
|
|
4477
|
+
--help Show this message.`;
|
|
4478
|
+
}
|
|
4479
|
+
});
|
|
4480
|
+
|
|
4481
|
+
// packages/cli/src/mesh-runtime.ts
|
|
4482
|
+
var TOP_USAGE = `mesh \u2014 cut your LLM bill, verified on your own usage.
|
|
4483
|
+
|
|
4484
|
+
Usage: mesh <command> [options]
|
|
4485
|
+
|
|
4486
|
+
start Start the MESH proxy, point Claude Code at it, and run Theo
|
|
4487
|
+
alongside you. One command; reverts cleanly on exit.
|
|
4488
|
+
install Always-on: run MESH as a background service so every Claude Code
|
|
4489
|
+
session is optimised, with nothing to start by hand.
|
|
4490
|
+
stop Stop the background service (kept, resume with install); restore Claude Code.
|
|
4491
|
+
uninstall Stop and remove the background service; restore Claude Code.
|
|
4492
|
+
status Is the service up, and what has Theo saved so far?
|
|
4493
|
+
dashboard Open a live local web view of your savings and what Theo is doing.
|
|
4494
|
+
update Update the runtime now (--rollback to revert to the previous one).
|
|
4495
|
+
verify Re-prove MESH's claims on this machine: the build's provenance,
|
|
4496
|
+
your ledger's hash-chain, and a deterministic invoice recompute.
|
|
4497
|
+
doctor Diagnose this machine: Node, the proxy, Claude Code wiring, the
|
|
4498
|
+
service, and the ledger \u2014 each with a one-line fix.
|
|
4499
|
+
version Print the installed version and build.
|
|
4500
|
+
compaction Estimate, per task in your ledger, what context compaction would
|
|
4501
|
+
save \u2014 and where it would cost more than it saves (advisory).
|
|
4502
|
+
|
|
4503
|
+
Run "mesh <command> --help" for command options.`;
|
|
4504
|
+
async function main() {
|
|
4505
|
+
const sub = process.argv[2];
|
|
4506
|
+
if (sub === "start") {
|
|
4507
|
+
await (await Promise.resolve().then(() => (init_start(), start_exports))).runStart(process.argv.slice(3));
|
|
4508
|
+
} else if (sub === "serve") {
|
|
4509
|
+
await (await Promise.resolve().then(() => (init_start(), start_exports))).runServe(process.argv.slice(3));
|
|
4510
|
+
} else if (sub === "install") {
|
|
4511
|
+
await (await Promise.resolve().then(() => (init_daemon(), daemon_exports))).runInstall(process.argv.slice(3));
|
|
4512
|
+
} else if (sub === "uninstall") {
|
|
4513
|
+
(await Promise.resolve().then(() => (init_daemon(), daemon_exports))).runUninstall(process.argv.slice(3));
|
|
4514
|
+
} else if (sub === "stop") {
|
|
4515
|
+
(await Promise.resolve().then(() => (init_daemon(), daemon_exports))).runStop(process.argv.slice(3));
|
|
4516
|
+
} else if (sub === "status") {
|
|
4517
|
+
await (await Promise.resolve().then(() => (init_daemon(), daemon_exports))).runStatus(process.argv.slice(3));
|
|
4518
|
+
} else if (sub === "update") {
|
|
4519
|
+
await (await Promise.resolve().then(() => (init_update(), update_exports))).runUpdate(process.argv.slice(3));
|
|
4520
|
+
} else if (sub === "verify") {
|
|
4521
|
+
await (await Promise.resolve().then(() => (init_verify(), verify_exports))).runVerify(process.argv.slice(3));
|
|
4522
|
+
} else if (sub === "doctor") {
|
|
4523
|
+
await (await Promise.resolve().then(() => (init_diag(), diag_exports))).runDoctor(process.argv.slice(3));
|
|
4524
|
+
} else if (sub === "dashboard") {
|
|
4525
|
+
await (await Promise.resolve().then(() => (init_dashboard(), dashboard_exports))).runDashboard(process.argv.slice(3));
|
|
4526
|
+
} else if (sub === "version" || sub === "--version" || sub === "-v") {
|
|
4527
|
+
(await Promise.resolve().then(() => (init_diag(), diag_exports))).runVersion(process.argv.slice(3));
|
|
4528
|
+
} else if (sub === "compaction") {
|
|
4529
|
+
await (await Promise.resolve().then(() => (init_compaction(), compaction_exports))).runCompaction(process.argv.slice(3));
|
|
4530
|
+
} else {
|
|
4531
|
+
console.log(TOP_USAGE);
|
|
4532
|
+
}
|
|
4533
|
+
}
|
|
4534
|
+
main().catch((error) => {
|
|
4535
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
4536
|
+
process.exit(1);
|
|
4537
|
+
});
|