vibezcheck 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/LICENSE +21 -0
- package/README.md +219 -0
- package/dist/ai-sdk/index.d.mts +34 -0
- package/dist/ai-sdk/index.d.ts +34 -0
- package/dist/ai-sdk/index.js +982 -0
- package/dist/ai-sdk/index.js.map +1 -0
- package/dist/ai-sdk/index.mjs +944 -0
- package/dist/ai-sdk/index.mjs.map +1 -0
- package/dist/auth/index.d.mts +59 -0
- package/dist/auth/index.d.ts +59 -0
- package/dist/auth/index.js +129 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/index.mjs +90 -0
- package/dist/auth/index.mjs.map +1 -0
- package/dist/billing/index.d.mts +48 -0
- package/dist/billing/index.d.ts +48 -0
- package/dist/billing/index.js +128 -0
- package/dist/billing/index.js.map +1 -0
- package/dist/billing/index.mjs +90 -0
- package/dist/billing/index.mjs.map +1 -0
- package/dist/client-MJ3tl7bz.d.mts +44 -0
- package/dist/client-txrE0D_D.d.ts +44 -0
- package/dist/customers/index.d.mts +49 -0
- package/dist/customers/index.d.ts +49 -0
- package/dist/customers/index.js +158 -0
- package/dist/customers/index.js.map +1 -0
- package/dist/customers/index.mjs +119 -0
- package/dist/customers/index.mjs.map +1 -0
- package/dist/index.d.mts +92 -0
- package/dist/index.d.ts +92 -0
- package/dist/index.js +1458 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1386 -0
- package/dist/index.mjs.map +1 -0
- package/dist/meter/index.d.mts +131 -0
- package/dist/meter/index.d.ts +131 -0
- package/dist/meter/index.js +895 -0
- package/dist/meter/index.js.map +1 -0
- package/dist/meter/index.mjs +843 -0
- package/dist/meter/index.mjs.map +1 -0
- package/dist/pricing/index.d.mts +40 -0
- package/dist/pricing/index.d.ts +40 -0
- package/dist/pricing/index.js +178 -0
- package/dist/pricing/index.js.map +1 -0
- package/dist/pricing/index.mjs +146 -0
- package/dist/pricing/index.mjs.map +1 -0
- package/dist/types-CSrSmsd1.d.mts +159 -0
- package/dist/types-CSrSmsd1.d.ts +159 -0
- package/package.json +131 -0
|
@@ -0,0 +1,982 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/ai-sdk/index.ts
|
|
31
|
+
var ai_sdk_exports = {};
|
|
32
|
+
__export(ai_sdk_exports, {
|
|
33
|
+
meteredModel: () => meteredModel,
|
|
34
|
+
withBilling: () => withBilling
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(ai_sdk_exports);
|
|
37
|
+
|
|
38
|
+
// src/meter/client.ts
|
|
39
|
+
var import_stripe = __toESM(require("stripe"));
|
|
40
|
+
|
|
41
|
+
// src/meter/batcher.ts
|
|
42
|
+
var MeterBatcher = class {
|
|
43
|
+
queue = [];
|
|
44
|
+
timer = null;
|
|
45
|
+
isFlushing = false;
|
|
46
|
+
maxBatchSize;
|
|
47
|
+
flushIntervalMs;
|
|
48
|
+
stripeClient;
|
|
49
|
+
eventName;
|
|
50
|
+
onUsageCallback;
|
|
51
|
+
onErrorCallback;
|
|
52
|
+
debug;
|
|
53
|
+
// In-memory ledger for local stats
|
|
54
|
+
totalRequests = 0;
|
|
55
|
+
totalTokens = 0;
|
|
56
|
+
totalInputTokens = 0;
|
|
57
|
+
totalOutputTokens = 0;
|
|
58
|
+
totalReasoningTokens = 0;
|
|
59
|
+
totalCostUSD = 0;
|
|
60
|
+
byModel = {};
|
|
61
|
+
constructor(options = {}) {
|
|
62
|
+
this.stripeClient = options.stripe;
|
|
63
|
+
this.eventName = options.eventName || "token-billing-tokens";
|
|
64
|
+
this.maxBatchSize = options.batching?.maxBatchSize ?? 50;
|
|
65
|
+
this.flushIntervalMs = options.batching?.flushIntervalMs ?? 50;
|
|
66
|
+
this.onUsageCallback = options.onUsage;
|
|
67
|
+
this.onErrorCallback = options.onError;
|
|
68
|
+
this.debug = options.debug ?? false;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Enqueue a usage event for batch dispatching
|
|
72
|
+
*/
|
|
73
|
+
enqueue(event) {
|
|
74
|
+
this.recordInLedger(event);
|
|
75
|
+
if (this.onUsageCallback) {
|
|
76
|
+
try {
|
|
77
|
+
const res = this.onUsageCallback(event);
|
|
78
|
+
if (res instanceof Promise) {
|
|
79
|
+
res.catch((err) => {
|
|
80
|
+
if (this.debug) console.error("[vibezcheck] Error in onUsage callback:", err);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
} catch (err) {
|
|
84
|
+
if (this.debug) console.error("[vibezcheck] Error in onUsage callback:", err);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (!this.stripeClient) {
|
|
88
|
+
if (this.debug) {
|
|
89
|
+
console.log(
|
|
90
|
+
`[vibezcheck:local] \u{1F4CA} ${event.model} | Tokens: ${event.usage.totalTokens} | Cost: $${event.cost.totalUSD.toFixed(6)}`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
this.queue.push(event);
|
|
96
|
+
if (this.queue.length >= this.maxBatchSize) {
|
|
97
|
+
this.flush().catch((err) => {
|
|
98
|
+
if (this.debug) console.error("[vibezcheck] Batch flush error:", err);
|
|
99
|
+
});
|
|
100
|
+
} else if (!this.timer) {
|
|
101
|
+
this.timer = setTimeout(() => {
|
|
102
|
+
this.timer = null;
|
|
103
|
+
this.flush().catch((err) => {
|
|
104
|
+
if (this.debug) console.error("[vibezcheck] Debounce flush error:", err);
|
|
105
|
+
});
|
|
106
|
+
}, this.flushIntervalMs);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Immediately flush all queued events to Stripe
|
|
111
|
+
*/
|
|
112
|
+
async flush() {
|
|
113
|
+
if (this.timer) {
|
|
114
|
+
clearTimeout(this.timer);
|
|
115
|
+
this.timer = null;
|
|
116
|
+
}
|
|
117
|
+
if (this.queue.length === 0 || !this.stripeClient || this.isFlushing) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
this.isFlushing = true;
|
|
121
|
+
const eventsToSend = [...this.queue];
|
|
122
|
+
this.queue = [];
|
|
123
|
+
try {
|
|
124
|
+
await this.sendEventsToStripe(eventsToSend);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
127
|
+
if (this.debug) {
|
|
128
|
+
console.error("[vibezcheck] Failed to send meter events to Stripe:", err);
|
|
129
|
+
}
|
|
130
|
+
if (this.onErrorCallback) {
|
|
131
|
+
this.onErrorCallback(err, eventsToSend);
|
|
132
|
+
}
|
|
133
|
+
} finally {
|
|
134
|
+
this.isFlushing = false;
|
|
135
|
+
if (this.queue.length > 0) {
|
|
136
|
+
this.flush().catch(() => {
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Sends events to Stripe Billing Meter Events API
|
|
143
|
+
*/
|
|
144
|
+
async sendEventsToStripe(events) {
|
|
145
|
+
if (!this.stripeClient) return;
|
|
146
|
+
for (const event of events) {
|
|
147
|
+
const customerId = event.customerId;
|
|
148
|
+
if (!customerId) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const timestamp = event.timestamp || (/* @__PURE__ */ new Date()).toISOString();
|
|
152
|
+
const model = `${event.provider}/${event.model}`;
|
|
153
|
+
if (event.usage.inputTokens > 0) {
|
|
154
|
+
try {
|
|
155
|
+
await this.stripeClient.v2.billing.meterEvents.create({
|
|
156
|
+
event_name: this.eventName,
|
|
157
|
+
timestamp,
|
|
158
|
+
payload: {
|
|
159
|
+
stripe_customer_id: customerId,
|
|
160
|
+
value: event.usage.inputTokens.toString(),
|
|
161
|
+
model,
|
|
162
|
+
token_type: "input",
|
|
163
|
+
cached_tokens: (event.usage.cachedTokens ?? 0).toString(),
|
|
164
|
+
...event.metadata ? event.metadata : {}
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
} catch (e) {
|
|
168
|
+
if (this.debug) console.warn("[vibezcheck] Input meter event error:", e);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (event.usage.outputTokens > 0) {
|
|
172
|
+
try {
|
|
173
|
+
await this.stripeClient.v2.billing.meterEvents.create({
|
|
174
|
+
event_name: this.eventName,
|
|
175
|
+
timestamp,
|
|
176
|
+
payload: {
|
|
177
|
+
stripe_customer_id: customerId,
|
|
178
|
+
value: event.usage.outputTokens.toString(),
|
|
179
|
+
model,
|
|
180
|
+
token_type: "output",
|
|
181
|
+
reasoning_tokens: (event.usage.reasoningTokens ?? 0).toString(),
|
|
182
|
+
visible_tokens: (event.usage.visibleOutputTokens ?? event.usage.outputTokens).toString(),
|
|
183
|
+
...event.metadata ? event.metadata : {}
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
} catch (e) {
|
|
187
|
+
if (this.debug) console.warn("[vibezcheck] Output meter event error:", e);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Updates internal in-memory ledger
|
|
194
|
+
*/
|
|
195
|
+
recordInLedger(event) {
|
|
196
|
+
this.totalRequests += 1;
|
|
197
|
+
this.totalTokens += event.usage.totalTokens;
|
|
198
|
+
this.totalInputTokens += event.usage.inputTokens;
|
|
199
|
+
this.totalOutputTokens += event.usage.outputTokens;
|
|
200
|
+
this.totalReasoningTokens += event.usage.reasoningTokens ?? 0;
|
|
201
|
+
this.totalCostUSD += event.cost.totalUSD;
|
|
202
|
+
const modelKey = event.model;
|
|
203
|
+
if (!this.byModel[modelKey]) {
|
|
204
|
+
this.byModel[modelKey] = { requests: 0, tokens: 0, costUSD: 0 };
|
|
205
|
+
}
|
|
206
|
+
this.byModel[modelKey].requests += 1;
|
|
207
|
+
this.byModel[modelKey].tokens += event.usage.totalTokens;
|
|
208
|
+
this.byModel[modelKey].costUSD += event.cost.totalUSD;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Get in-memory usage summary
|
|
212
|
+
*/
|
|
213
|
+
getSummary() {
|
|
214
|
+
return {
|
|
215
|
+
totalRequests: this.totalRequests,
|
|
216
|
+
totalTokens: this.totalTokens,
|
|
217
|
+
totalInputTokens: this.totalInputTokens,
|
|
218
|
+
totalOutputTokens: this.totalOutputTokens,
|
|
219
|
+
totalReasoningTokens: this.totalReasoningTokens,
|
|
220
|
+
totalCostUSD: Number(this.totalCostUSD.toFixed(6)),
|
|
221
|
+
byModel: { ...this.byModel }
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Reset in-memory ledger
|
|
226
|
+
*/
|
|
227
|
+
resetLedger() {
|
|
228
|
+
this.totalRequests = 0;
|
|
229
|
+
this.totalTokens = 0;
|
|
230
|
+
this.totalInputTokens = 0;
|
|
231
|
+
this.totalOutputTokens = 0;
|
|
232
|
+
this.totalReasoningTokens = 0;
|
|
233
|
+
this.totalCostUSD = 0;
|
|
234
|
+
this.byModel = {};
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
// src/meter/extractors/openai.ts
|
|
239
|
+
function extractOpenAIResponseUsage(response) {
|
|
240
|
+
if (!response || typeof response !== "object") return null;
|
|
241
|
+
if ("choices" in response && "usage" in response && response.usage) {
|
|
242
|
+
const rawUsage = response.usage;
|
|
243
|
+
const model = response.model || "gpt-4o";
|
|
244
|
+
const inputTokens = rawUsage.prompt_tokens ?? 0;
|
|
245
|
+
const outputTokens = rawUsage.completion_tokens ?? 0;
|
|
246
|
+
const reasoningTokens = rawUsage.completion_tokens_details?.reasoning_tokens ?? 0;
|
|
247
|
+
const cachedTokens = rawUsage.prompt_tokens_details?.cached_tokens ?? 0;
|
|
248
|
+
return {
|
|
249
|
+
model,
|
|
250
|
+
provider: "openai",
|
|
251
|
+
usage: {
|
|
252
|
+
inputTokens,
|
|
253
|
+
outputTokens,
|
|
254
|
+
totalTokens: inputTokens + outputTokens,
|
|
255
|
+
reasoningTokens: reasoningTokens > 0 ? reasoningTokens : void 0,
|
|
256
|
+
visibleOutputTokens: Math.max(0, outputTokens - reasoningTokens),
|
|
257
|
+
cachedTokens: cachedTokens > 0 ? cachedTokens : void 0
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
if ("data" in response && "usage" in response && response.usage && "model" in response) {
|
|
262
|
+
const rawUsage = response.usage;
|
|
263
|
+
const inputTokens = rawUsage.prompt_tokens ?? 0;
|
|
264
|
+
return {
|
|
265
|
+
model: response.model || "text-embedding-3-small",
|
|
266
|
+
provider: "openai",
|
|
267
|
+
usage: {
|
|
268
|
+
inputTokens,
|
|
269
|
+
outputTokens: 0,
|
|
270
|
+
totalTokens: inputTokens
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
if ("status" in response && "usage" in response && response.usage) {
|
|
275
|
+
const rawUsage = response.usage;
|
|
276
|
+
const model = response.model || "gpt-5.6-sol";
|
|
277
|
+
const inputTokens = rawUsage.input_tokens ?? rawUsage.prompt_tokens ?? 0;
|
|
278
|
+
const outputTokens = rawUsage.output_tokens ?? rawUsage.completion_tokens ?? 0;
|
|
279
|
+
const reasoningTokens = rawUsage.output_token_details?.reasoning_tokens ?? rawUsage.completion_tokens_details?.reasoning_tokens ?? 0;
|
|
280
|
+
const cachedTokens = rawUsage.input_token_details?.cached_tokens ?? rawUsage.prompt_tokens_details?.cached_tokens ?? 0;
|
|
281
|
+
return {
|
|
282
|
+
model,
|
|
283
|
+
provider: "openai",
|
|
284
|
+
usage: {
|
|
285
|
+
inputTokens,
|
|
286
|
+
outputTokens,
|
|
287
|
+
totalTokens: inputTokens + outputTokens,
|
|
288
|
+
reasoningTokens: reasoningTokens > 0 ? reasoningTokens : void 0,
|
|
289
|
+
visibleOutputTokens: Math.max(0, outputTokens - reasoningTokens),
|
|
290
|
+
cachedTokens: cachedTokens > 0 ? cachedTokens : void 0
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
function inspectOpenAIStreamChunk(chunk) {
|
|
297
|
+
if (!chunk || typeof chunk !== "object") return {};
|
|
298
|
+
const model = chunk.model;
|
|
299
|
+
if (chunk.usage) {
|
|
300
|
+
const rawUsage = chunk.usage;
|
|
301
|
+
const inputTokens = rawUsage.prompt_tokens ?? rawUsage.input_tokens ?? 0;
|
|
302
|
+
const outputTokens = rawUsage.completion_tokens ?? rawUsage.output_tokens ?? 0;
|
|
303
|
+
const reasoningTokens = rawUsage.completion_tokens_details?.reasoning_tokens ?? rawUsage.output_token_details?.reasoning_tokens ?? 0;
|
|
304
|
+
const cachedTokens = rawUsage.prompt_tokens_details?.cached_tokens ?? rawUsage.input_token_details?.cached_tokens ?? 0;
|
|
305
|
+
return {
|
|
306
|
+
model,
|
|
307
|
+
usage: {
|
|
308
|
+
inputTokens,
|
|
309
|
+
outputTokens,
|
|
310
|
+
totalTokens: inputTokens + outputTokens,
|
|
311
|
+
reasoningTokens: reasoningTokens > 0 ? reasoningTokens : void 0,
|
|
312
|
+
visibleOutputTokens: Math.max(0, outputTokens - reasoningTokens),
|
|
313
|
+
cachedTokens: cachedTokens > 0 ? cachedTokens : void 0
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
if (chunk.type === "response.completed" || chunk.type === "response.done") {
|
|
318
|
+
if (chunk.response?.usage) {
|
|
319
|
+
const rawUsage = chunk.response.usage;
|
|
320
|
+
const inputTokens = rawUsage.input_tokens ?? 0;
|
|
321
|
+
const outputTokens = rawUsage.output_tokens ?? 0;
|
|
322
|
+
const reasoningTokens = rawUsage.output_token_details?.reasoning_tokens ?? 0;
|
|
323
|
+
const cachedTokens = rawUsage.input_token_details?.cached_tokens ?? 0;
|
|
324
|
+
return {
|
|
325
|
+
model: chunk.response.model || model,
|
|
326
|
+
usage: {
|
|
327
|
+
inputTokens,
|
|
328
|
+
outputTokens,
|
|
329
|
+
totalTokens: inputTokens + outputTokens,
|
|
330
|
+
reasoningTokens: reasoningTokens > 0 ? reasoningTokens : void 0,
|
|
331
|
+
visibleOutputTokens: Math.max(0, outputTokens - reasoningTokens),
|
|
332
|
+
cachedTokens: cachedTokens > 0 ? cachedTokens : void 0
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return { model };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// src/meter/extractors/anthropic.ts
|
|
341
|
+
function extractAnthropicResponseUsage(response) {
|
|
342
|
+
if (!response || typeof response !== "object") return null;
|
|
343
|
+
if (response.type === "message" || "content" in response && "usage" in response) {
|
|
344
|
+
const rawUsage = response.usage || {};
|
|
345
|
+
const model = response.model || "claude-3-7-sonnet";
|
|
346
|
+
const inputTokens = rawUsage.input_tokens ?? 0;
|
|
347
|
+
const outputTokens = rawUsage.output_tokens ?? 0;
|
|
348
|
+
const cachedTokens = rawUsage.cache_read_input_tokens ?? 0;
|
|
349
|
+
const cacheWriteTokens = rawUsage.cache_creation_input_tokens ?? 0;
|
|
350
|
+
let reasoningTokens = void 0;
|
|
351
|
+
if (Array.isArray(response.content)) {
|
|
352
|
+
const thinkingBlocks = response.content.filter((b) => b.type === "thinking");
|
|
353
|
+
if (thinkingBlocks.length > 0) {
|
|
354
|
+
reasoningTokens = rawUsage.thinking_tokens ?? void 0;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return {
|
|
358
|
+
model,
|
|
359
|
+
provider: "anthropic",
|
|
360
|
+
usage: {
|
|
361
|
+
inputTokens,
|
|
362
|
+
outputTokens,
|
|
363
|
+
totalTokens: inputTokens + outputTokens,
|
|
364
|
+
reasoningTokens,
|
|
365
|
+
visibleOutputTokens: reasoningTokens !== void 0 ? Math.max(0, outputTokens - reasoningTokens) : outputTokens,
|
|
366
|
+
cachedTokens: cachedTokens > 0 ? cachedTokens : void 0,
|
|
367
|
+
cacheWriteTokens: cacheWriteTokens > 0 ? cacheWriteTokens : void 0
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
var AnthropicStreamAccumulator = class {
|
|
374
|
+
model = "claude-3-7-sonnet";
|
|
375
|
+
inputTokens = 0;
|
|
376
|
+
outputTokens = 0;
|
|
377
|
+
cachedTokens = 0;
|
|
378
|
+
cacheWriteTokens = 0;
|
|
379
|
+
reasoningTokens = 0;
|
|
380
|
+
processEvent(event) {
|
|
381
|
+
if (!event || typeof event !== "object") return;
|
|
382
|
+
if (event.type === "message_start" && event.message) {
|
|
383
|
+
if (event.message.model) {
|
|
384
|
+
this.model = event.message.model;
|
|
385
|
+
}
|
|
386
|
+
if (event.message.usage) {
|
|
387
|
+
this.inputTokens = event.message.usage.input_tokens ?? 0;
|
|
388
|
+
this.cachedTokens = event.message.usage.cache_read_input_tokens ?? 0;
|
|
389
|
+
this.cacheWriteTokens = event.message.usage.cache_creation_input_tokens ?? 0;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
if (event.type === "message_delta" && event.usage) {
|
|
393
|
+
this.outputTokens = event.usage.output_tokens ?? 0;
|
|
394
|
+
if (event.usage.thinking_tokens) {
|
|
395
|
+
this.reasoningTokens = event.usage.thinking_tokens;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (event.type === "content_block_start" && event.content_block?.type === "thinking") {
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
getUsage() {
|
|
402
|
+
return {
|
|
403
|
+
model: this.model,
|
|
404
|
+
provider: "anthropic",
|
|
405
|
+
usage: {
|
|
406
|
+
inputTokens: this.inputTokens,
|
|
407
|
+
outputTokens: this.outputTokens,
|
|
408
|
+
totalTokens: this.inputTokens + this.outputTokens,
|
|
409
|
+
reasoningTokens: this.reasoningTokens > 0 ? this.reasoningTokens : void 0,
|
|
410
|
+
visibleOutputTokens: this.reasoningTokens > 0 ? Math.max(0, this.outputTokens - this.reasoningTokens) : this.outputTokens,
|
|
411
|
+
cachedTokens: this.cachedTokens > 0 ? this.cachedTokens : void 0,
|
|
412
|
+
cacheWriteTokens: this.cacheWriteTokens > 0 ? this.cacheWriteTokens : void 0
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
// src/meter/extractors/gemini.ts
|
|
419
|
+
function extractGeminiResponseUsage(response, fallbackModel = "gemini-3.7-flash") {
|
|
420
|
+
if (!response || typeof response !== "object") return null;
|
|
421
|
+
const usageMetadata = response.usageMetadata || response.response?.usageMetadata;
|
|
422
|
+
if (usageMetadata) {
|
|
423
|
+
const inputTokens = usageMetadata.promptTokenCount ?? 0;
|
|
424
|
+
const baseOutputTokens = usageMetadata.candidatesTokenCount ?? 0;
|
|
425
|
+
const thoughtsTokenCount = usageMetadata.thoughtsTokenCount ?? usageMetadata.reasoningTokenCount ?? 0;
|
|
426
|
+
const cachedTokens = usageMetadata.cachedContentTokenCount ?? 0;
|
|
427
|
+
const totalOutput = baseOutputTokens + thoughtsTokenCount;
|
|
428
|
+
const model = response.model || response.response?.model || fallbackModel;
|
|
429
|
+
return {
|
|
430
|
+
model,
|
|
431
|
+
provider: "google",
|
|
432
|
+
usage: {
|
|
433
|
+
inputTokens,
|
|
434
|
+
outputTokens: totalOutput,
|
|
435
|
+
totalTokens: inputTokens + totalOutput,
|
|
436
|
+
reasoningTokens: thoughtsTokenCount > 0 ? thoughtsTokenCount : void 0,
|
|
437
|
+
visibleOutputTokens: baseOutputTokens,
|
|
438
|
+
cachedTokens: cachedTokens > 0 ? cachedTokens : void 0
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
return null;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// src/meter/extractors/generic.ts
|
|
446
|
+
function extractGenericResponseUsage(response, fallbackModel = "generic-llm", fallbackProvider = "generic") {
|
|
447
|
+
if (!response || typeof response !== "object") return null;
|
|
448
|
+
const usage = response.usage || response.token_usage || response.usageMetadata;
|
|
449
|
+
if (usage) {
|
|
450
|
+
const inputTokens = usage.prompt_tokens ?? usage.input_tokens ?? usage.promptTokenCount ?? usage.prompt_eval_count ?? 0;
|
|
451
|
+
const outputTokens = usage.completion_tokens ?? usage.output_tokens ?? usage.candidatesTokenCount ?? usage.eval_count ?? 0;
|
|
452
|
+
const reasoningTokens = usage.reasoning_tokens ?? usage.thoughtsTokenCount ?? usage.completion_tokens_details?.reasoning_tokens ?? 0;
|
|
453
|
+
const cachedTokens = usage.prompt_tokens_details?.cached_tokens ?? usage.cached_tokens ?? usage.cachedContentTokenCount ?? 0;
|
|
454
|
+
const model = response.model || fallbackModel;
|
|
455
|
+
const provider = response.provider || fallbackProvider;
|
|
456
|
+
return {
|
|
457
|
+
model,
|
|
458
|
+
provider,
|
|
459
|
+
usage: {
|
|
460
|
+
inputTokens,
|
|
461
|
+
outputTokens,
|
|
462
|
+
totalTokens: inputTokens + outputTokens,
|
|
463
|
+
reasoningTokens: reasoningTokens > 0 ? reasoningTokens : void 0,
|
|
464
|
+
visibleOutputTokens: Math.max(0, outputTokens - reasoningTokens),
|
|
465
|
+
cachedTokens: cachedTokens > 0 ? cachedTokens : void 0
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
return null;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// src/meter/extractors/index.ts
|
|
473
|
+
function detectAndExtractUsage(response, fallbackModel, fallbackProvider) {
|
|
474
|
+
if (!response || typeof response !== "object") return null;
|
|
475
|
+
const openaiResult = extractOpenAIResponseUsage(response);
|
|
476
|
+
if (openaiResult) return openaiResult;
|
|
477
|
+
const anthropicResult = extractAnthropicResponseUsage(response);
|
|
478
|
+
if (anthropicResult) return anthropicResult;
|
|
479
|
+
const geminiResult = extractGeminiResponseUsage(response, fallbackModel);
|
|
480
|
+
if (geminiResult) return geminiResult;
|
|
481
|
+
const genericResult = extractGenericResponseUsage(response, fallbackModel, fallbackProvider);
|
|
482
|
+
if (genericResult) return genericResult;
|
|
483
|
+
return null;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// src/pricing/table.ts
|
|
487
|
+
var MODEL_PRICING_TABLE = {
|
|
488
|
+
// --- OpenAI ---
|
|
489
|
+
"gpt-5.6-sol": { inputPer1M: 4, outputPer1M: 20, cachedInputPer1M: 0.4 },
|
|
490
|
+
"gpt-5.6-terra": { inputPer1M: 2, outputPer1M: 12, cachedInputPer1M: 0.2 },
|
|
491
|
+
"gpt-5.6-luna": { inputPer1M: 0.2, outputPer1M: 1.2, cachedInputPer1M: 0.02 },
|
|
492
|
+
"gpt-5": { inputPer1M: 4, outputPer1M: 20, cachedInputPer1M: 0.4 },
|
|
493
|
+
"gpt-5-mini": { inputPer1M: 0.2, outputPer1M: 1.2, cachedInputPer1M: 0.02 },
|
|
494
|
+
"o1": { inputPer1M: 15, outputPer1M: 60, cachedInputPer1M: 7.5 },
|
|
495
|
+
"o1-mini": { inputPer1M: 1.1, outputPer1M: 4.4, cachedInputPer1M: 0.55 },
|
|
496
|
+
"o3": { inputPer1M: 15, outputPer1M: 60, cachedInputPer1M: 7.5 },
|
|
497
|
+
"o3-mini": { inputPer1M: 1.1, outputPer1M: 4.4, cachedInputPer1M: 0.55 },
|
|
498
|
+
"gpt-4o": { inputPer1M: 2.5, outputPer1M: 10, cachedInputPer1M: 1.25 },
|
|
499
|
+
"gpt-4o-mini": { inputPer1M: 0.15, outputPer1M: 0.6, cachedInputPer1M: 0.075 },
|
|
500
|
+
"gpt-4.1": { inputPer1M: 2, outputPer1M: 8, cachedInputPer1M: 1 },
|
|
501
|
+
"gpt-4.1-nano": { inputPer1M: 0.1, outputPer1M: 0.4, cachedInputPer1M: 0.05 },
|
|
502
|
+
"text-embedding-3-small": { inputPer1M: 0.02, outputPer1M: 0 },
|
|
503
|
+
"text-embedding-3-large": { inputPer1M: 0.13, outputPer1M: 0 },
|
|
504
|
+
// --- Anthropic ---
|
|
505
|
+
"claude-3-7-sonnet": { inputPer1M: 0.59, outputPer1M: 2.93, cachedInputPer1M: 0.3 },
|
|
506
|
+
"claude-sonnet-5": { inputPer1M: 2, outputPer1M: 10, cachedInputPer1M: 0.3 },
|
|
507
|
+
"claude-3-5-sonnet": { inputPer1M: 3, outputPer1M: 15, cachedInputPer1M: 0.3 },
|
|
508
|
+
"claude-3-5-haiku": { inputPer1M: 0.8, outputPer1M: 4, cachedInputPer1M: 0.08 },
|
|
509
|
+
"haiku-4.5": { inputPer1M: 1, outputPer1M: 5, cachedInputPer1M: 0.1 },
|
|
510
|
+
"claude-opus-5": { inputPer1M: 5, outputPer1M: 25, cachedInputPer1M: 1.5 },
|
|
511
|
+
"claude-3-opus": { inputPer1M: 15, outputPer1M: 75, cachedInputPer1M: 1.5 },
|
|
512
|
+
// --- Google Gemini ---
|
|
513
|
+
"gemini-3.7-flash": { inputPer1M: 0.75, outputPer1M: 3.75, cachedInputPer1M: 0.18 },
|
|
514
|
+
"gemini-3.1-pro": { inputPer1M: 2, outputPer1M: 12, cachedInputPer1M: 0.5 },
|
|
515
|
+
"gemini-3.5-flash": { inputPer1M: 1.5, outputPer1M: 9, cachedInputPer1M: 0.38 },
|
|
516
|
+
"gemini-3.1-flash-lite": { inputPer1M: 0.25, outputPer1M: 1.5, cachedInputPer1M: 0.06 },
|
|
517
|
+
"gemini-2.0-flash": { inputPer1M: 0.1, outputPer1M: 0.4, cachedInputPer1M: 0.025 },
|
|
518
|
+
"gemini-1.5-pro": { inputPer1M: 1.25, outputPer1M: 5, cachedInputPer1M: 0.3125 },
|
|
519
|
+
"gemini-1.5-flash": { inputPer1M: 0.075, outputPer1M: 0.3, cachedInputPer1M: 0.01875 },
|
|
520
|
+
// --- xAI Grok ---
|
|
521
|
+
"grok-4.6": { inputPer1M: 3, outputPer1M: 15 },
|
|
522
|
+
"grok-2": { inputPer1M: 2, outputPer1M: 10 },
|
|
523
|
+
"grok-2-vision": { inputPer1M: 2, outputPer1M: 10 },
|
|
524
|
+
"grok-beta": { inputPer1M: 5, outputPer1M: 15 },
|
|
525
|
+
// --- Mistral ---
|
|
526
|
+
"mistral-large-3": { inputPer1M: 2, outputPer1M: 6 },
|
|
527
|
+
"mistral-large-latest": { inputPer1M: 2, outputPer1M: 6 },
|
|
528
|
+
"codestral-latest": { inputPer1M: 0.3, outputPer1M: 0.9 },
|
|
529
|
+
"mistral-small-latest": { inputPer1M: 0.2, outputPer1M: 0.6 },
|
|
530
|
+
"ministral-8b-latest": { inputPer1M: 0.1, outputPer1M: 0.1 },
|
|
531
|
+
// --- Groq LPUs ---
|
|
532
|
+
"llama-3.3-70b-versatile": { inputPer1M: 0.59, outputPer1M: 0.79 },
|
|
533
|
+
"llama-3.1-8b-instant": { inputPer1M: 0.05, outputPer1M: 0.08 },
|
|
534
|
+
"deepseek-r1-distill-llama-70b": { inputPer1M: 0.75, outputPer1M: 0.99 },
|
|
535
|
+
"qwen-2.5-32b": { inputPer1M: 0.29, outputPer1M: 0.39 },
|
|
536
|
+
// --- DeepSeek ---
|
|
537
|
+
"deepseek-v4-pro": { inputPer1M: 0.66, outputPer1M: 1.98, cachedInputPer1M: 0.15 },
|
|
538
|
+
"deepseek-v4-flash": { inputPer1M: 0.22, outputPer1M: 0.66, cachedInputPer1M: 0.05 },
|
|
539
|
+
"deepseek-chat": { inputPer1M: 0.22, outputPer1M: 0.66, cachedInputPer1M: 0.05 },
|
|
540
|
+
"deepseek-reasoner": { inputPer1M: 0.66, outputPer1M: 1.98, cachedInputPer1M: 0.15 },
|
|
541
|
+
// --- Cohere ---
|
|
542
|
+
"command-r-plus": { inputPer1M: 2.5, outputPer1M: 10 },
|
|
543
|
+
"command-r": { inputPer1M: 0.15, outputPer1M: 0.6 }
|
|
544
|
+
};
|
|
545
|
+
var customPricingRegistry = {};
|
|
546
|
+
function normalizeModelKey(rawModel) {
|
|
547
|
+
if (!rawModel) return "unknown";
|
|
548
|
+
let model = rawModel.toLowerCase().trim();
|
|
549
|
+
if (model.includes("/")) {
|
|
550
|
+
model = model.split("/")[1] || model;
|
|
551
|
+
}
|
|
552
|
+
model = model.replace(/-\d{8}$/, "");
|
|
553
|
+
model = model.replace(/-\d{4}-\d{2}-\d{2}$/, "");
|
|
554
|
+
return model;
|
|
555
|
+
}
|
|
556
|
+
function getModelPricing(modelName) {
|
|
557
|
+
const normalized = normalizeModelKey(modelName);
|
|
558
|
+
if (customPricingRegistry[normalized]) {
|
|
559
|
+
return customPricingRegistry[normalized];
|
|
560
|
+
}
|
|
561
|
+
if (customPricingRegistry[modelName]) {
|
|
562
|
+
return customPricingRegistry[modelName];
|
|
563
|
+
}
|
|
564
|
+
if (MODEL_PRICING_TABLE[normalized]) {
|
|
565
|
+
return MODEL_PRICING_TABLE[normalized];
|
|
566
|
+
}
|
|
567
|
+
if (MODEL_PRICING_TABLE[modelName]) {
|
|
568
|
+
return MODEL_PRICING_TABLE[modelName];
|
|
569
|
+
}
|
|
570
|
+
return {
|
|
571
|
+
inputPer1M: 1,
|
|
572
|
+
outputPer1M: 3,
|
|
573
|
+
cachedInputPer1M: 0.5
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// src/pricing/calculator.ts
|
|
578
|
+
function calculateCost(params) {
|
|
579
|
+
const rates = getModelPricing(params.model);
|
|
580
|
+
const inputTokens = params.inputTokens ?? 0;
|
|
581
|
+
const outputTokens = params.outputTokens ?? 0;
|
|
582
|
+
const reasoningTokens = params.reasoningTokens ?? 0;
|
|
583
|
+
const cachedTokens = params.cachedTokens ?? 0;
|
|
584
|
+
const regularInputTokens = Math.max(0, inputTokens - cachedTokens);
|
|
585
|
+
const regularInputCost = regularInputTokens / 1e6 * rates.inputPer1M;
|
|
586
|
+
const cachedRate = rates.cachedInputPer1M ?? rates.inputPer1M * 0.5;
|
|
587
|
+
const cachedInputCost = cachedTokens / 1e6 * cachedRate;
|
|
588
|
+
const inputCostUSD = regularInputCost + cachedInputCost;
|
|
589
|
+
const outputCostUSD = outputTokens / 1e6 * rates.outputPer1M;
|
|
590
|
+
const reasoningRate = rates.reasoningPer1M ?? rates.outputPer1M;
|
|
591
|
+
const reasoningCostUSD = reasoningTokens / 1e6 * reasoningRate;
|
|
592
|
+
const standardCacheCost = cachedTokens / 1e6 * rates.inputPer1M;
|
|
593
|
+
const cachedDiscountUSD = Math.max(0, standardCacheCost - cachedInputCost);
|
|
594
|
+
const totalUSD = inputCostUSD + outputCostUSD;
|
|
595
|
+
const markup = params.markupMultiplier ?? 1;
|
|
596
|
+
const retailUSD = markup !== 1 ? totalUSD * markup : void 0;
|
|
597
|
+
return {
|
|
598
|
+
inputCostUSD: Number(inputCostUSD.toFixed(8)),
|
|
599
|
+
outputCostUSD: Number(outputCostUSD.toFixed(8)),
|
|
600
|
+
reasoningCostUSD: reasoningTokens > 0 ? Number(reasoningCostUSD.toFixed(8)) : void 0,
|
|
601
|
+
cachedDiscountUSD: cachedTokens > 0 ? Number(cachedDiscountUSD.toFixed(8)) : void 0,
|
|
602
|
+
totalUSD: Number(totalUSD.toFixed(8)),
|
|
603
|
+
retailUSD: retailUSD ? Number(retailUSD.toFixed(8)) : void 0,
|
|
604
|
+
currency: rates.currency || "USD"
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
function calculateUsageCost(model, usage, markupMultiplier) {
|
|
608
|
+
return calculateCost({
|
|
609
|
+
model,
|
|
610
|
+
inputTokens: usage.inputTokens,
|
|
611
|
+
outputTokens: usage.outputTokens,
|
|
612
|
+
reasoningTokens: usage.reasoningTokens,
|
|
613
|
+
cachedTokens: usage.cachedTokens,
|
|
614
|
+
cacheWriteTokens: usage.cacheWriteTokens,
|
|
615
|
+
markupMultiplier
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// src/meter/stream.ts
|
|
620
|
+
function wrapOpenAIStream(stream, options, onComplete) {
|
|
621
|
+
let detectedModel = options.model || "gpt-4o";
|
|
622
|
+
let finalUsage = null;
|
|
623
|
+
const wrappedAsyncIterable = {
|
|
624
|
+
async *[Symbol.asyncIterator]() {
|
|
625
|
+
try {
|
|
626
|
+
for await (const chunk of stream) {
|
|
627
|
+
const inspected = inspectOpenAIStreamChunk(chunk);
|
|
628
|
+
if (inspected.model) {
|
|
629
|
+
detectedModel = inspected.model;
|
|
630
|
+
}
|
|
631
|
+
if (inspected.usage) {
|
|
632
|
+
finalUsage = inspected.usage;
|
|
633
|
+
}
|
|
634
|
+
yield chunk;
|
|
635
|
+
}
|
|
636
|
+
} finally {
|
|
637
|
+
if (finalUsage) {
|
|
638
|
+
const cost = calculateUsageCost(detectedModel, finalUsage);
|
|
639
|
+
const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
|
|
640
|
+
const event = {
|
|
641
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
642
|
+
model: detectedModel,
|
|
643
|
+
provider: "openai",
|
|
644
|
+
usage: finalUsage,
|
|
645
|
+
cost,
|
|
646
|
+
customerId,
|
|
647
|
+
metadata: options.metadata
|
|
648
|
+
};
|
|
649
|
+
onComplete(event);
|
|
650
|
+
if (options.onUsage) {
|
|
651
|
+
options.onUsage(event);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
return wrappedAsyncIterable;
|
|
658
|
+
}
|
|
659
|
+
function wrapAnthropicStream(stream, options, onComplete) {
|
|
660
|
+
const accumulator = new AnthropicStreamAccumulator();
|
|
661
|
+
const wrappedAsyncIterable = {
|
|
662
|
+
async *[Symbol.asyncIterator]() {
|
|
663
|
+
try {
|
|
664
|
+
for await (const event of stream) {
|
|
665
|
+
accumulator.processEvent(event);
|
|
666
|
+
yield event;
|
|
667
|
+
}
|
|
668
|
+
} finally {
|
|
669
|
+
const extracted = accumulator.getUsage();
|
|
670
|
+
const model = options.model || extracted.model;
|
|
671
|
+
const cost = calculateUsageCost(model, extracted.usage);
|
|
672
|
+
const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
|
|
673
|
+
const usageEvent = {
|
|
674
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
675
|
+
model,
|
|
676
|
+
provider: "anthropic",
|
|
677
|
+
usage: extracted.usage,
|
|
678
|
+
cost,
|
|
679
|
+
customerId,
|
|
680
|
+
metadata: options.metadata
|
|
681
|
+
};
|
|
682
|
+
onComplete(usageEvent);
|
|
683
|
+
if (options.onUsage) {
|
|
684
|
+
options.onUsage(usageEvent);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
return wrappedAsyncIterable;
|
|
690
|
+
}
|
|
691
|
+
function wrapGeminiStream(result, options, onComplete) {
|
|
692
|
+
if (!result || !result.stream) return result;
|
|
693
|
+
const originalStream = result.stream;
|
|
694
|
+
const model = options.model || "gemini-3.7-flash";
|
|
695
|
+
let lastChunkWithUsage = null;
|
|
696
|
+
const wrappedStream = (async function* () {
|
|
697
|
+
try {
|
|
698
|
+
for await (const chunk of originalStream) {
|
|
699
|
+
if (chunk.usageMetadata) {
|
|
700
|
+
lastChunkWithUsage = chunk;
|
|
701
|
+
}
|
|
702
|
+
yield chunk;
|
|
703
|
+
}
|
|
704
|
+
} finally {
|
|
705
|
+
if (lastChunkWithUsage) {
|
|
706
|
+
const extracted = extractGeminiResponseUsage(lastChunkWithUsage, model);
|
|
707
|
+
if (extracted) {
|
|
708
|
+
const cost = calculateUsageCost(extracted.model, extracted.usage);
|
|
709
|
+
const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
|
|
710
|
+
const event = {
|
|
711
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
712
|
+
model: extracted.model,
|
|
713
|
+
provider: "google",
|
|
714
|
+
usage: extracted.usage,
|
|
715
|
+
cost,
|
|
716
|
+
customerId,
|
|
717
|
+
metadata: options.metadata
|
|
718
|
+
};
|
|
719
|
+
onComplete(event);
|
|
720
|
+
if (options.onUsage) {
|
|
721
|
+
options.onUsage(event);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
})();
|
|
727
|
+
return {
|
|
728
|
+
...result,
|
|
729
|
+
stream: wrappedStream
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
function wrapUniversalStream(stream, options = {}, onComplete) {
|
|
733
|
+
if (!stream || typeof stream !== "object") return stream;
|
|
734
|
+
if ("stream" in stream && "response" in stream) {
|
|
735
|
+
return wrapGeminiStream(stream, options, onComplete);
|
|
736
|
+
}
|
|
737
|
+
if (Symbol.asyncIterator in stream) {
|
|
738
|
+
if (options.provider === "anthropic") {
|
|
739
|
+
return wrapAnthropicStream(stream, options, onComplete);
|
|
740
|
+
}
|
|
741
|
+
return wrapOpenAIStream(stream, options, onComplete);
|
|
742
|
+
}
|
|
743
|
+
return stream;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
// src/meter/client.ts
|
|
747
|
+
var VibezMeter = class {
|
|
748
|
+
batcher;
|
|
749
|
+
stripeClient;
|
|
750
|
+
markupMultiplier;
|
|
751
|
+
constructor(options = {}) {
|
|
752
|
+
this.markupMultiplier = options.markupMultiplier;
|
|
753
|
+
if (options.stripe) {
|
|
754
|
+
this.stripeClient = options.stripe;
|
|
755
|
+
} else if (options.apiKey || process.env.STRIPE_SECRET_KEY) {
|
|
756
|
+
const key = options.apiKey || process.env.STRIPE_SECRET_KEY;
|
|
757
|
+
this.stripeClient = new import_stripe.default(key, {
|
|
758
|
+
appInfo: {
|
|
759
|
+
name: "vibezcheck",
|
|
760
|
+
version: "0.1.0",
|
|
761
|
+
url: "https://vibezcheck.xyz"
|
|
762
|
+
}
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
this.batcher = new MeterBatcher({
|
|
766
|
+
...options,
|
|
767
|
+
stripe: this.stripeClient
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Track token usage from a non-streaming response object (OpenAI, Anthropic, Gemini, etc.)
|
|
772
|
+
*/
|
|
773
|
+
trackUsage(response, options = {}) {
|
|
774
|
+
const extracted = detectAndExtractUsage(response, options.model, options.provider);
|
|
775
|
+
if (!extracted) {
|
|
776
|
+
return null;
|
|
777
|
+
}
|
|
778
|
+
const model = options.model || extracted.model;
|
|
779
|
+
const cost = calculateUsageCost(model, extracted.usage, this.markupMultiplier);
|
|
780
|
+
const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
|
|
781
|
+
const event = {
|
|
782
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
783
|
+
model,
|
|
784
|
+
provider: extracted.provider,
|
|
785
|
+
usage: extracted.usage,
|
|
786
|
+
cost,
|
|
787
|
+
customerId,
|
|
788
|
+
metadata: options.metadata
|
|
789
|
+
};
|
|
790
|
+
this.batcher.enqueue(event);
|
|
791
|
+
return event;
|
|
792
|
+
}
|
|
793
|
+
/**
|
|
794
|
+
* Wrap any LLM stream (OpenAI, Anthropic, Gemini) with zero added latency
|
|
795
|
+
*/
|
|
796
|
+
wrapStream(stream, options = {}) {
|
|
797
|
+
return wrapUniversalStream(stream, options, (event) => {
|
|
798
|
+
this.batcher.enqueue(event);
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Directly record token usage manually
|
|
803
|
+
*/
|
|
804
|
+
recordUsage(options) {
|
|
805
|
+
const inputTokens = options.inputTokens ?? 0;
|
|
806
|
+
const outputTokens = options.outputTokens ?? 0;
|
|
807
|
+
const reasoningTokens = options.reasoningTokens;
|
|
808
|
+
const cachedTokens = options.cachedTokens;
|
|
809
|
+
const usage = {
|
|
810
|
+
inputTokens,
|
|
811
|
+
outputTokens,
|
|
812
|
+
totalTokens: inputTokens + outputTokens,
|
|
813
|
+
reasoningTokens,
|
|
814
|
+
visibleOutputTokens: reasoningTokens !== void 0 ? Math.max(0, outputTokens - reasoningTokens) : outputTokens,
|
|
815
|
+
cachedTokens
|
|
816
|
+
};
|
|
817
|
+
const cost = calculateUsageCost(options.model, usage, this.markupMultiplier);
|
|
818
|
+
const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
|
|
819
|
+
const event = {
|
|
820
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
821
|
+
model: options.model,
|
|
822
|
+
provider: options.provider || "custom",
|
|
823
|
+
usage,
|
|
824
|
+
cost,
|
|
825
|
+
customerId,
|
|
826
|
+
metadata: options.metadata
|
|
827
|
+
};
|
|
828
|
+
this.batcher.enqueue(event);
|
|
829
|
+
return event;
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Flush pending events to Stripe (vital for Serverless & Edge environments)
|
|
833
|
+
*/
|
|
834
|
+
async flush() {
|
|
835
|
+
await this.batcher.flush();
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Get in-memory aggregated usage statistics
|
|
839
|
+
*/
|
|
840
|
+
getUsageSummary() {
|
|
841
|
+
return this.batcher.getSummary();
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Reset in-memory ledger
|
|
845
|
+
*/
|
|
846
|
+
resetSummary() {
|
|
847
|
+
this.batcher.resetLedger();
|
|
848
|
+
}
|
|
849
|
+
};
|
|
850
|
+
function createMeter(options = {}) {
|
|
851
|
+
return new VibezMeter(options);
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
// src/ai-sdk/with-billing.ts
|
|
855
|
+
function withBilling(model, options = {}) {
|
|
856
|
+
if (!model || typeof model !== "object") {
|
|
857
|
+
return model;
|
|
858
|
+
}
|
|
859
|
+
const meter = options.meter || createMeter({
|
|
860
|
+
apiKey: options.stripeApiKey,
|
|
861
|
+
eventName: options.eventName
|
|
862
|
+
});
|
|
863
|
+
const customerId = typeof options.customer === "string" ? options.customer : options.customer?.id || options.customerId;
|
|
864
|
+
const modelId = model.modelId || "unknown-model";
|
|
865
|
+
const provider = model.provider?.replace(/^@ai-sdk\//, "") || "ai-sdk";
|
|
866
|
+
const handleUsage = (rawUsage) => {
|
|
867
|
+
if (!rawUsage) return;
|
|
868
|
+
const inputTokens = rawUsage.promptTokens ?? rawUsage.inputTokens ?? 0;
|
|
869
|
+
const outputTokens = rawUsage.completionTokens ?? rawUsage.outputTokens ?? 0;
|
|
870
|
+
const reasoningTokens = rawUsage.reasoningTokens ?? rawUsage.completionTokensDetails?.reasoningTokens ?? rawUsage.outputTokenDetails?.reasoningTokens ?? 0;
|
|
871
|
+
const cachedTokens = rawUsage.promptTokensDetails?.cachedTokens ?? rawUsage.inputTokenDetails?.cachedTokens ?? 0;
|
|
872
|
+
const usage = {
|
|
873
|
+
inputTokens,
|
|
874
|
+
outputTokens,
|
|
875
|
+
totalTokens: inputTokens + outputTokens,
|
|
876
|
+
reasoningTokens: reasoningTokens > 0 ? reasoningTokens : void 0,
|
|
877
|
+
visibleOutputTokens: Math.max(0, outputTokens - reasoningTokens),
|
|
878
|
+
cachedTokens: cachedTokens > 0 ? cachedTokens : void 0
|
|
879
|
+
};
|
|
880
|
+
const cost = calculateUsageCost(modelId, usage);
|
|
881
|
+
const event = {
|
|
882
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
883
|
+
model: modelId,
|
|
884
|
+
provider,
|
|
885
|
+
usage,
|
|
886
|
+
cost,
|
|
887
|
+
customerId,
|
|
888
|
+
metadata: options.metadata
|
|
889
|
+
};
|
|
890
|
+
meter.recordUsage({
|
|
891
|
+
model: modelId,
|
|
892
|
+
provider,
|
|
893
|
+
inputTokens,
|
|
894
|
+
outputTokens,
|
|
895
|
+
reasoningTokens: usage.reasoningTokens,
|
|
896
|
+
cachedTokens: usage.cachedTokens,
|
|
897
|
+
customerId,
|
|
898
|
+
metadata: options.metadata
|
|
899
|
+
});
|
|
900
|
+
if (options.onUsage) {
|
|
901
|
+
options.onUsage(event);
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
return new Proxy(model, {
|
|
905
|
+
get(target, prop, receiver) {
|
|
906
|
+
const originalValue = Reflect.get(target, prop, receiver);
|
|
907
|
+
if (prop === "doGenerate" && typeof originalValue === "function") {
|
|
908
|
+
return async function(...args) {
|
|
909
|
+
const result = await originalValue.apply(target, args);
|
|
910
|
+
if (result && result.usage) {
|
|
911
|
+
handleUsage(result.usage);
|
|
912
|
+
}
|
|
913
|
+
return result;
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
if (prop === "doStream" && typeof originalValue === "function") {
|
|
917
|
+
return async function(...args) {
|
|
918
|
+
const result = await originalValue.apply(target, args);
|
|
919
|
+
if (!result || !result.stream) {
|
|
920
|
+
return result;
|
|
921
|
+
}
|
|
922
|
+
const originalStream = result.stream;
|
|
923
|
+
if (typeof originalStream.getReader === "function") {
|
|
924
|
+
const reader = originalStream.getReader();
|
|
925
|
+
const transformedStream = new ReadableStream({
|
|
926
|
+
async start(controller) {
|
|
927
|
+
try {
|
|
928
|
+
while (true) {
|
|
929
|
+
const { done, value } = await reader.read();
|
|
930
|
+
if (done) {
|
|
931
|
+
controller.close();
|
|
932
|
+
break;
|
|
933
|
+
}
|
|
934
|
+
if (value && typeof value === "object") {
|
|
935
|
+
if (value.type === "finish" && value.usage) {
|
|
936
|
+
handleUsage(value.usage);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
controller.enqueue(value);
|
|
940
|
+
}
|
|
941
|
+
} catch (err) {
|
|
942
|
+
controller.error(err);
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
});
|
|
946
|
+
return {
|
|
947
|
+
...result,
|
|
948
|
+
stream: transformedStream
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
if (Symbol.asyncIterator in originalStream) {
|
|
952
|
+
const wrappedAsyncIterable = {
|
|
953
|
+
async *[Symbol.asyncIterator]() {
|
|
954
|
+
for await (const chunk of originalStream) {
|
|
955
|
+
if (chunk && typeof chunk === "object") {
|
|
956
|
+
if (chunk.type === "finish" && chunk.usage) {
|
|
957
|
+
handleUsage(chunk.usage);
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
yield chunk;
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
};
|
|
964
|
+
return {
|
|
965
|
+
...result,
|
|
966
|
+
stream: wrappedAsyncIterable
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
return result;
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
return originalValue;
|
|
973
|
+
}
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
var meteredModel = withBilling;
|
|
977
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
978
|
+
0 && (module.exports = {
|
|
979
|
+
meteredModel,
|
|
980
|
+
withBilling
|
|
981
|
+
});
|
|
982
|
+
//# sourceMappingURL=index.js.map
|