@ai-billing/core 0.0.1-alpha.6 → 0.0.1-alpha.8
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/dist/index.cjs +96 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +38 -24
- package/dist/index.d.ts +38 -24
- package/dist/index.js +91 -13
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -4,8 +4,8 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
6
|
var __export = (target, all) => {
|
|
7
|
-
for (var
|
|
8
|
-
__defProp(target,
|
|
7
|
+
for (var name4 in all)
|
|
8
|
+
__defProp(target, name4, { get: all[name4], enumerable: true });
|
|
9
9
|
};
|
|
10
10
|
var __copyProps = (to, from, except, desc) => {
|
|
11
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
@@ -21,14 +21,22 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
AIBillingError: () => AIBillingError,
|
|
24
|
+
AiBillingCostError: () => AiBillingCostError,
|
|
24
25
|
AiBillingDestinationError: () => AiBillingDestinationError,
|
|
25
26
|
AiBillingExtractorError: () => AiBillingExtractorError,
|
|
26
27
|
consoleDestination: () => consoleDestination,
|
|
28
|
+
convertCostUnit: () => convertCostUnit,
|
|
29
|
+
costToNumber: () => costToNumber,
|
|
27
30
|
createDestination: () => createDestination,
|
|
28
31
|
createV3BillingMiddleware: () => createV3BillingMiddleware
|
|
29
32
|
});
|
|
30
33
|
module.exports = __toCommonJS(index_exports);
|
|
31
34
|
|
|
35
|
+
// src/event/to-json-object.ts
|
|
36
|
+
function toJSONObject(event) {
|
|
37
|
+
return event;
|
|
38
|
+
}
|
|
39
|
+
|
|
32
40
|
// src/ai-sdk/language-model-middleware/v3/language-model-v3-base-billing-middleware.ts
|
|
33
41
|
function createV3BillingMiddleware(options) {
|
|
34
42
|
const { buildEvent, destinations, defaultTags, waitUntil, onError } = options;
|
|
@@ -40,11 +48,10 @@ function createV3BillingMiddleware(options) {
|
|
|
40
48
|
responseId
|
|
41
49
|
}) => {
|
|
42
50
|
try {
|
|
43
|
-
const
|
|
44
|
-
const headerTags = rawHeader ? JSON.parse(rawHeader) : {};
|
|
51
|
+
const requestTags = params.providerOptions?.["ai-billing-tags"];
|
|
45
52
|
const tags = {
|
|
46
53
|
...defaultTags ?? {},
|
|
47
|
-
...
|
|
54
|
+
...requestTags ?? {}
|
|
48
55
|
};
|
|
49
56
|
const event = await buildEvent({
|
|
50
57
|
responseId,
|
|
@@ -53,35 +60,47 @@ function createV3BillingMiddleware(options) {
|
|
|
53
60
|
providerMetadata,
|
|
54
61
|
tags
|
|
55
62
|
});
|
|
56
|
-
if (event) {
|
|
57
|
-
|
|
63
|
+
if (event && destinations.length > 0) {
|
|
64
|
+
const dispatchDestinationsPromise = Promise.allSettled(
|
|
58
65
|
destinations.map((d) => Promise.resolve(d(event)))
|
|
59
66
|
);
|
|
67
|
+
if (waitUntil) waitUntil(dispatchDestinationsPromise);
|
|
60
68
|
}
|
|
69
|
+
return event;
|
|
61
70
|
} catch (err) {
|
|
62
71
|
if (onError) onError(err);
|
|
63
72
|
else console.error("[ai-billing] Core Error:", err);
|
|
73
|
+
return null;
|
|
64
74
|
}
|
|
65
75
|
};
|
|
66
76
|
return {
|
|
67
77
|
specificationVersion: "v3",
|
|
68
78
|
wrapGenerate: async ({ doGenerate, model, params }) => {
|
|
69
79
|
const result = await doGenerate();
|
|
70
|
-
const
|
|
80
|
+
const event = await processEvent({
|
|
71
81
|
model,
|
|
72
82
|
params,
|
|
73
83
|
usage: result.usage,
|
|
74
84
|
providerMetadata: result.providerMetadata,
|
|
75
85
|
responseId: result.response?.id
|
|
76
86
|
});
|
|
77
|
-
|
|
78
|
-
|
|
87
|
+
const providerMetadataWithBilling = {
|
|
88
|
+
...result.providerMetadata
|
|
89
|
+
};
|
|
90
|
+
if (event) {
|
|
91
|
+
providerMetadataWithBilling["ai-billing"] = toJSONObject(event);
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
...result,
|
|
95
|
+
providerMetadata: providerMetadataWithBilling
|
|
96
|
+
};
|
|
79
97
|
},
|
|
80
98
|
wrapStream: async ({ doStream, model, params }) => {
|
|
81
99
|
const { stream, ...rest } = await doStream();
|
|
82
100
|
let responseId;
|
|
83
101
|
let usage;
|
|
84
102
|
let providerMetadata;
|
|
103
|
+
let finishChunk;
|
|
85
104
|
const billedStream = stream.pipeThrough(
|
|
86
105
|
new TransformStream({
|
|
87
106
|
transform(chunk, controller) {
|
|
@@ -92,18 +111,31 @@ function createV3BillingMiddleware(options) {
|
|
|
92
111
|
if (chunk.type === "finish") {
|
|
93
112
|
usage = chunk.usage;
|
|
94
113
|
providerMetadata = chunk.providerMetadata;
|
|
114
|
+
finishChunk = chunk;
|
|
115
|
+
return;
|
|
95
116
|
}
|
|
96
117
|
controller.enqueue(chunk);
|
|
97
118
|
},
|
|
98
|
-
flush() {
|
|
99
|
-
const
|
|
119
|
+
async flush(controller) {
|
|
120
|
+
const event = await processEvent({
|
|
100
121
|
model,
|
|
101
122
|
params,
|
|
102
123
|
usage,
|
|
103
124
|
providerMetadata,
|
|
104
125
|
responseId
|
|
105
126
|
});
|
|
106
|
-
|
|
127
|
+
const providerMetadataWithBilling = {
|
|
128
|
+
...providerMetadata
|
|
129
|
+
};
|
|
130
|
+
if (event) {
|
|
131
|
+
providerMetadataWithBilling["ai-billing"] = toJSONObject(event);
|
|
132
|
+
}
|
|
133
|
+
if (finishChunk) {
|
|
134
|
+
controller.enqueue({
|
|
135
|
+
...finishChunk,
|
|
136
|
+
providerMetadata: providerMetadataWithBilling
|
|
137
|
+
});
|
|
138
|
+
}
|
|
107
139
|
}
|
|
108
140
|
})
|
|
109
141
|
);
|
|
@@ -119,12 +151,12 @@ var AIBillingError = class _AIBillingError extends Error {
|
|
|
119
151
|
[symbol] = true;
|
|
120
152
|
cause;
|
|
121
153
|
constructor({
|
|
122
|
-
name:
|
|
154
|
+
name: name4,
|
|
123
155
|
message,
|
|
124
156
|
cause
|
|
125
157
|
}) {
|
|
126
158
|
super(message);
|
|
127
|
-
this.name =
|
|
159
|
+
this.name = name4;
|
|
128
160
|
this.cause = cause;
|
|
129
161
|
}
|
|
130
162
|
static isInstance(error) {
|
|
@@ -173,6 +205,20 @@ var AiBillingDestinationError = class extends AIBillingError {
|
|
|
173
205
|
}
|
|
174
206
|
};
|
|
175
207
|
|
|
208
|
+
// src/error/cost-error.ts
|
|
209
|
+
var name3 = "AiBillingCostError";
|
|
210
|
+
var marker4 = `ai-billing.error.${name3}`;
|
|
211
|
+
var symbol4 = Symbol.for(marker4);
|
|
212
|
+
var AiBillingCostError = class extends AIBillingError {
|
|
213
|
+
[symbol4] = true;
|
|
214
|
+
constructor({ message, cause }) {
|
|
215
|
+
super({ name: name3, message, cause });
|
|
216
|
+
}
|
|
217
|
+
static isInstance(error) {
|
|
218
|
+
return AIBillingError.hasMarker(error, marker4);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
176
222
|
// src/destination/base-destination.ts
|
|
177
223
|
function createDestination(destinationId, handler) {
|
|
178
224
|
return async (event) => {
|
|
@@ -197,12 +243,47 @@ function consoleDestination() {
|
|
|
197
243
|
});
|
|
198
244
|
});
|
|
199
245
|
}
|
|
246
|
+
|
|
247
|
+
// src/cost/convert-cost.ts
|
|
248
|
+
var getNanos = (cost) => {
|
|
249
|
+
switch (cost.unit) {
|
|
250
|
+
case "base":
|
|
251
|
+
return Math.round(cost.amount * 1e9);
|
|
252
|
+
case "cents":
|
|
253
|
+
return Math.round(cost.amount * 1e7);
|
|
254
|
+
case "micros":
|
|
255
|
+
return Math.round(cost.amount * 1e3);
|
|
256
|
+
case "nanos":
|
|
257
|
+
return Math.round(cost.amount);
|
|
258
|
+
default:
|
|
259
|
+
throw new AiBillingCostError({
|
|
260
|
+
message: `Failed to process cost. Unknown CostUnit: '${String(cost.unit)}'.`
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
var costToNumber = (cost, targetUnit) => {
|
|
265
|
+
const nanos = getNanos(cost);
|
|
266
|
+
if (targetUnit === "nanos") return nanos;
|
|
267
|
+
if (targetUnit === "micros") return nanos / 1e3;
|
|
268
|
+
if (targetUnit === "cents") return nanos / 1e7;
|
|
269
|
+
return nanos / 1e9;
|
|
270
|
+
};
|
|
271
|
+
var convertCostUnit = (cost, targetUnit) => {
|
|
272
|
+
return {
|
|
273
|
+
amount: costToNumber(cost, targetUnit),
|
|
274
|
+
currency: cost.currency,
|
|
275
|
+
unit: targetUnit
|
|
276
|
+
};
|
|
277
|
+
};
|
|
200
278
|
// Annotate the CommonJS export names for ESM import in node:
|
|
201
279
|
0 && (module.exports = {
|
|
202
280
|
AIBillingError,
|
|
281
|
+
AiBillingCostError,
|
|
203
282
|
AiBillingDestinationError,
|
|
204
283
|
AiBillingExtractorError,
|
|
205
284
|
consoleDestination,
|
|
285
|
+
convertCostUnit,
|
|
286
|
+
costToNumber,
|
|
206
287
|
createDestination,
|
|
207
288
|
createV3BillingMiddleware
|
|
208
289
|
});
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/ai-sdk/language-model-middleware/v3/language-model-v3-base-billing-middleware.ts","../src/error/ai-billing-error.ts","../src/error/extractor-error.ts","../src/error/destination-error.ts","../src/destination/base-destination.ts","../src/destination/console-destination.ts"],"sourcesContent":["export * from './types/index.js';\nexport * from './ai-sdk/index.js';\nexport * from './destination/index.js';\nexport * from './error/index.js';\n","import type {\n LanguageModelV3,\n LanguageModelV3CallOptions,\n LanguageModelV3Usage,\n LanguageModelV3GenerateResult,\n LanguageModelV3StreamPart,\n LanguageModelV3Middleware,\n SharedV3ProviderMetadata,\n} from '@ai-sdk/provider';\nimport type {\n BaseBillingMiddlewareOptions,\n EventBuilder,\n} from '../../../types/index.js';\n\nexport interface BuildV3EventPayload<TTags> {\n responseId: string | undefined;\n model: LanguageModelV3;\n usage: LanguageModelV3Usage | undefined;\n providerMetadata: SharedV3ProviderMetadata | undefined;\n tags: TTags;\n}\n\nexport interface BillingMiddlewareV3Options<\n TTags,\n> extends BaseBillingMiddlewareOptions<TTags> {\n buildEvent: EventBuilder<BuildV3EventPayload<TTags>, TTags>;\n}\n\nexport function createV3BillingMiddleware<TTags>(\n options: BillingMiddlewareV3Options<TTags>,\n): LanguageModelV3Middleware {\n const { buildEvent, destinations, defaultTags, waitUntil, onError } = options;\n\n const processEvent = async ({\n model,\n params,\n usage,\n providerMetadata,\n responseId,\n }: {\n model: LanguageModelV3;\n params: LanguageModelV3CallOptions;\n usage: LanguageModelV3Usage | undefined;\n providerMetadata: SharedV3ProviderMetadata | undefined;\n responseId: string | undefined;\n }): Promise<void> => {\n try {\n const rawHeader = params.headers?.['x-ai-billing-tags'];\n const headerTags = rawHeader ? JSON.parse(rawHeader) : {};\n\n const tags = {\n ...(defaultTags ?? {}),\n ...headerTags,\n } as TTags;\n\n const event = await buildEvent({\n responseId,\n model,\n usage,\n providerMetadata,\n tags,\n });\n\n if (event) {\n await Promise.allSettled(\n destinations.map(d => Promise.resolve(d(event))),\n );\n }\n } catch (err) {\n if (onError) onError(err);\n else console.error('[ai-billing] Core Error:', err);\n }\n };\n\n return {\n specificationVersion: 'v3',\n\n wrapGenerate: async ({ doGenerate, model, params }) => {\n const result: LanguageModelV3GenerateResult = await doGenerate();\n\n const promise = processEvent({\n model,\n params,\n usage: result.usage,\n providerMetadata: result.providerMetadata,\n responseId: result.response?.id,\n });\n\n if (waitUntil) waitUntil(promise);\n return result;\n },\n\n wrapStream: async ({ doStream, model, params }) => {\n const { stream, ...rest } = await doStream();\n\n let responseId: string | undefined;\n let usage: LanguageModelV3Usage | undefined;\n let providerMetadata: SharedV3ProviderMetadata | undefined;\n\n const billedStream = stream.pipeThrough(\n new TransformStream<\n LanguageModelV3StreamPart,\n LanguageModelV3StreamPart\n >({\n transform(chunk, controller) {\n if (chunk.type === 'text-start') responseId = chunk.id;\n if (chunk.type === 'response-metadata' && !responseId) {\n responseId = chunk.id;\n }\n if (chunk.type === 'finish') {\n usage = chunk.usage;\n providerMetadata = chunk.providerMetadata;\n }\n controller.enqueue(chunk);\n },\n flush() {\n const promise = processEvent({\n model,\n params,\n usage,\n providerMetadata,\n responseId,\n });\n if (waitUntil) waitUntil(promise);\n },\n }),\n );\n\n return { ...rest, stream: billedStream };\n },\n };\n}\n","const marker = 'ai-billing.error';\nconst symbol = Symbol.for(marker);\n\nexport class AIBillingError extends Error {\n private readonly [symbol] = true;\n\n readonly cause?: unknown;\n\n constructor({\n name,\n message,\n cause,\n }: {\n name: string;\n message: string;\n cause?: unknown;\n }) {\n super(message);\n this.name = name;\n this.cause = cause;\n }\n\n static isInstance(error: unknown): error is AIBillingError {\n return AIBillingError.hasMarker(error, marker);\n }\n\n protected static hasMarker(error: unknown, markerString: string): boolean {\n const markerSymbol = Symbol.for(markerString);\n return (\n error != null &&\n typeof error === 'object' &&\n markerSymbol in error &&\n typeof error[markerSymbol] === 'boolean' &&\n error[markerSymbol] === true\n );\n }\n}\n","import { AIBillingError } from './ai-billing-error.js';\n\nconst name = 'AiBillingExtractorError';\nconst marker = `ai-billing.error.${name}`;\nconst symbol = Symbol.for(marker);\n\nexport class AiBillingExtractorError extends AIBillingError {\n private readonly [symbol] = true;\n\n constructor({\n message = `Failed to extract billing data.`,\n cause,\n }: {\n message?: string;\n cause?: unknown;\n }) {\n super({ name, message, cause });\n }\n\n static isInstance(error: unknown): error is AiBillingExtractorError {\n return AIBillingError.hasMarker(error, marker);\n }\n}\n","import { AIBillingError } from './ai-billing-error.js';\n\nconst name = 'AiBillingDestinationError';\nconst marker = `ai-billing.error.${name}`;\nconst symbol = Symbol.for(marker);\n\nexport class AiBillingDestinationError extends AIBillingError {\n private readonly [symbol] = true;\n\n readonly destinationId?: string;\n\n constructor({\n destinationId,\n message = `Failed to process billing data for destination: '${destinationId}'.`,\n cause,\n }: {\n destinationId?: string;\n message?: string;\n cause?: unknown;\n }) {\n super({ name, message, cause });\n this.destinationId = destinationId;\n }\n\n static isInstance(error: unknown): error is AiBillingDestinationError {\n return AIBillingError.hasMarker(error, marker);\n }\n}\n","import type { Destination, BillingEvent } from '../types/index.js';\nimport { AiBillingDestinationError } from '../error/index.js';\n\nexport function createDestination<TTags>(\n destinationId: string,\n handler: (event: BillingEvent<TTags>) => Promise<void> | void,\n): Destination<TTags> {\n return async (event: BillingEvent<TTags>) => {\n try {\n await handler(event);\n } catch (error) {\n throw new AiBillingDestinationError({\n destinationId,\n cause: error,\n });\n }\n };\n}\n","import { createDestination } from './base-destination.js';\nimport type { Destination } from '../types/index.js';\n\nexport function consoleDestination<TTags>(): Destination<TTags> {\n return createDestination<TTags>('console-logger', event => {\n console.dir(event, {\n depth: null,\n colors: true,\n compact: false,\n });\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC4BO,SAAS,0BACd,SAC2B;AAC3B,QAAM,EAAE,YAAY,cAAc,aAAa,WAAW,QAAQ,IAAI;AAEtE,QAAM,eAAe,OAAO;AAAA,IAC1B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,MAMqB;AACnB,QAAI;AACF,YAAM,YAAY,OAAO,UAAU,mBAAmB;AACtD,YAAM,aAAa,YAAY,KAAK,MAAM,SAAS,IAAI,CAAC;AAExD,YAAM,OAAO;AAAA,QACX,GAAI,eAAe,CAAC;AAAA,QACpB,GAAG;AAAA,MACL;AAEA,YAAM,QAAQ,MAAM,WAAW;AAAA,QAC7B;AAAA,QACA;AAAA,QACA;A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DAAoD,aAAa;AAAA,IAC3E;AAAA,EACF,GAIG;AACD,UAAM,EAAE,MAAAF,OAAM,SAAS,MAAM,CAAC;AAC9B,SAAK,gBAAgB;AAAA,EACvB;AAAA,EAEA,OAAO,WAAW,OAAoD;AACpE,WAAO,eAAe,UAAU,OAAOC,OAAM;AAAA,EAC/C;AACF;;;ACxBO,SAAS,kBACd,eACA,SACoB;AACpB,SAAO,OAAO,UAA+B;AAC3C,QAAI;AACF,YAAM,QAAQ,KAAK;AAAA,IACrB,SAAS,OAAO;AACd,YAAM,IAAI,0BAA0B;AAAA,QAClC;AAAA,QACA,OAAO;AAAA,MACT,CAAC;AAAA,IACH;AAAA,EACF;AACF;;;ACdO,SAAS,qBAAgD;AAC9D,SAAO,kBAAyB,kBAAkB,WAAS;AACzD,YAAQ,IAAI,OAAO;AAAA,MACjB,OAAO;AAAA,MACP,QAAQ;AAAA,MACR,SAAS;AAAA,IACX,CAAC;AAAA,EACH,CAAC;AACH;","names":["name","marker","symbol","name","marker","symbol"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/event/to-json-object.ts","../src/ai-sdk/language-model-middleware/v3/language-model-v3-base-billing-middleware.ts","../src/error/ai-billing-error.ts","../src/error/extractor-error.ts","../src/error/destination-error.ts","../src/error/cost-error.ts","../src/destination/base-destination.ts","../src/destination/console-destination.ts","../src/cost/convert-cost.ts"],"sourcesContent":["export * from './types/index.js';\nexport * from './ai-sdk/index.js';\nexport * from './destination/index.js';\nexport * from './error/index.js';\nexport * from './cost/index.js';\n","import { DefaultTags, BillingEvent } from '../types/index.js';\nimport type { JSONObject } from '@ai-sdk/provider';\n\nexport function toJSONObject(event: BillingEvent<DefaultTags>): JSONObject {\n return event as unknown as JSONObject;\n}\n","import type {\n LanguageModelV3,\n LanguageModelV3CallOptions,\n LanguageModelV3Usage,\n LanguageModelV3GenerateResult,\n LanguageModelV3StreamPart,\n LanguageModelV3Middleware,\n SharedV3ProviderMetadata,\n} from '@ai-sdk/provider';\nimport type {\n BaseBillingMiddlewareOptions,\n EventBuilder,\n BillingEvent,\n DefaultTags,\n} from '../../../types/index.js';\nimport { toJSONObject } from '../../../event/index.js';\n\nexport interface BuildV3EventPayload<TTags extends DefaultTags = DefaultTags> {\n responseId: string | undefined;\n model: LanguageModelV3;\n usage: LanguageModelV3Usage | undefined;\n providerMetadata: SharedV3ProviderMetadata | undefined;\n tags: TTags;\n}\n\nexport interface BillingMiddlewareV3Options<\n TTags extends DefaultTags = DefaultTags,\n> extends BaseBillingMiddlewareOptions<TTags> {\n buildEvent: EventBuilder<BuildV3EventPayload<TTags>, TTags>;\n}\n\nexport function createV3BillingMiddleware<\n TTags extends DefaultTags = DefaultTags,\n>(options: BillingMiddlewareV3Options<TTags>): LanguageModelV3Middleware {\n const { buildEvent, destinations, defaultTags, waitUntil, onError } = options;\n\n const processEvent = async ({\n model,\n params,\n usage,\n providerMetadata,\n responseId,\n }: {\n model: LanguageModelV3;\n params: LanguageModelV3CallOptions;\n usage: LanguageModelV3Usage | undefined;\n providerMetadata: SharedV3ProviderMetadata | undefined;\n responseId: string | undefined;\n }): Promise<BillingEvent<TTags> | null> => {\n try {\n const requestTags = params.providerOptions?.['ai-billing-tags'];\n\n const tags = {\n ...(defaultTags ?? {}),\n ...(requestTags ?? {}),\n } as TTags;\n\n const event = await buildEvent({\n responseId,\n model,\n usage,\n providerMetadata,\n tags,\n });\n\n if (event && destinations.length > 0) {\n const dispatchDestinationsPromise = Promise.allSettled(\n destinations.map(d => Promise.resolve(d(event))),\n );\n if (waitUntil) waitUntil(dispatchDestinationsPromise);\n }\n return event;\n } catch (err) {\n if (onError) onError(err);\n else console.error('[ai-billing] Core Error:', err);\n return null;\n }\n };\n\n return {\n specificationVersion: 'v3',\n\n wrapGenerate: async ({ doGenerate, model, params }) => {\n const result: LanguageModelV3GenerateResult = await doGenerate();\n\n const event = await processEvent({\n model,\n params,\n usage: result.usage,\n providerMetadata: result.providerMetadata,\n responseId: result.response?.id,\n });\n\n const providerMetadataWithBilling = {\n ...result.providerMetadata,\n } as SharedV3ProviderMetadata;\n\n if (event) {\n providerMetadataWithBilling['ai-billing'] = toJSONObject(event);\n }\n\n return {\n ...result,\n providerMetadata: providerMetadataWithBilling,\n };\n },\n\n wrapStream: async ({ doStream, model, params }) => {\n const { stream, ...rest } = await doStream();\n\n let responseId: string | undefined;\n let usage: LanguageModelV3Usage | undefined;\n let providerMetadata: SharedV3ProviderMetadata | undefined;\n let finishChunk:\n | Extract<LanguageModelV3StreamPart, { type: 'finish' }>\n | undefined;\n\n const billedStream = stream.pipeThrough(\n new TransformStream<\n LanguageModelV3StreamPart,\n LanguageModelV3StreamPart\n >({\n transform(chunk, controller) {\n if (chunk.type === 'text-start') responseId = chunk.id;\n if (chunk.type === 'response-metadata' && !responseId) {\n responseId = chunk.id;\n }\n if (chunk.type === 'finish') {\n usage = chunk.usage;\n providerMetadata = chunk.providerMetadata;\n finishChunk = chunk;\n return; // held until flush\n }\n controller.enqueue(chunk);\n },\n async flush(controller) {\n const event = await processEvent({\n model,\n params,\n usage,\n providerMetadata,\n responseId,\n });\n\n const providerMetadataWithBilling = {\n ...providerMetadata,\n } as SharedV3ProviderMetadata;\n\n if (event) {\n providerMetadataWithBilling['ai-billing'] = toJSONObject(event);\n }\n\n if (finishChunk) {\n controller.enqueue({\n ...finishChunk,\n providerMetadata: providerMetadataWithBilling,\n });\n }\n },\n }),\n );\n\n return { ...rest, stream: billedStream };\n },\n };\n}\n","const marker = 'ai-billing.error';\nconst symbol = Symbol.for(marker);\n\nexport class AIBillingError extends Error {\n private readonly [symbol] = true;\n\n readonly cause?: unknown;\n\n constructor({\n name,\n message,\n cause,\n }: {\n name: string;\n message: string;\n cause?: unknown;\n }) {\n super(message);\n this.name = name;\n this.cause = cause;\n }\n\n static isInstance(error: unknown): error is AIBillingError {\n return AIBillingError.hasMarker(error, marker);\n }\n\n protected static hasMarker(error: unknown, markerString: string): boolean {\n const markerSymbol = Symbol.for(markerString);\n return (\n error != null &&\n typeof error === 'object' &&\n markerSymbol in error &&\n typeof error[markerSymbol] === 'boolean' &&\n error[markerSymbol] === true\n );\n }\n}\n","import { AIBillingError } from './ai-billing-error.js';\n\nconst name = 'AiBillingExtractorError';\nconst marker = `ai-billing.error.${name}`;\nconst symbol = Symbol.for(marker);\n\nexport class AiBillingExtractorError extends AIBillingError {\n private readonly [symbol] = true;\n\n constructor({\n message = `Failed to extract billing data.`,\n cause,\n }: {\n message?: string;\n cause?: unknown;\n }) {\n super({ name, message, cause });\n }\n\n static isInstance(error: unknown): error is AiBillingExtractorError {\n return AIBillingError.hasMarker(error, marker);\n }\n}\n","import { AIBillingError } from './ai-billing-error.js';\n\nconst name = 'AiBillingDestinationError';\nconst marker = `ai-billing.error.${name}`;\nconst symbol = Symbol.for(marker);\n\nexport class AiBillingDestinationError extends AIBillingError {\n private readonly [symbol] = true;\n\n readonly destinationId?: string;\n\n constructor({\n destinationId,\n message = `Failed to process billing data for destination: '${destinationId}'.`,\n cause,\n }: {\n destinationId?: string;\n message?: string;\n cause?: unknown;\n }) {\n super({ name, message, cause });\n this.destinationId = destinationId;\n }\n\n static isInstance(error: unknown): error is AiBillingDestinationError {\n return AIBillingError.hasMarker(error, marker);\n }\n}\n","import { AIBillingError } from './ai-billing-error.js';\n\nconst name = 'AiBillingCostError';\nconst marker = `ai-billing.error.${name}`;\nconst symbol = Symbol.for(marker);\n\nexport class AiBillingCostError extends AIBillingError {\n private readonly [symbol] = true;\n\n constructor({ message, cause }: { message: string; cause?: unknown }) {\n super({ name, message, cause });\n }\n\n static isInstance(error: unknown): error is AiBillingCostError {\n return AIBillingError.hasMarker(error, marker);\n }\n}\n","import type { Destination, BillingEvent, DefaultTags } from '../types/index.js';\nimport { AiBillingDestinationError } from '../error/index.js';\n\nexport function createDestination<TTags extends DefaultTags = DefaultTags>(\n destinationId: string,\n handler: (event: BillingEvent<TTags>) => Promise<void> | void,\n): Destination<TTags> {\n return async (event: BillingEvent<TTags>) => {\n try {\n await handler(event);\n } catch (error) {\n throw new AiBillingDestinationError({\n destinationId,\n cause: error,\n });\n }\n };\n}\n","import { createDestination } from './base-destination.js';\nimport type { DefaultTags, Destination } from '../types/index.js';\nimport { JSONObject } from '@ai-sdk/provider';\n\nexport function consoleDestination<\n TTags extends DefaultTags = DefaultTags,\n>(): Destination<TTags> {\n return createDestination<TTags>('console-logger', event => {\n console.dir(event, {\n depth: null,\n colors: true,\n compact: false,\n });\n });\n}\n","import { AiBillingCostError } from '../index.js';\nimport type { Cost, CostUnit } from '../index.js';\n\nconst getNanos = (cost: Cost): number => {\n switch (cost.unit) {\n case 'base':\n return Math.round(cost.amount * 1_000_000_000);\n case 'cents':\n return Math.round(cost.amount * 10_000_000);\n case 'micros':\n return Math.round(cost.amount * 1_000);\n case 'nanos':\n return Math.round(cost.amount);\n default:\n throw new AiBillingCostError({\n message: `Failed to process cost. Unknown CostUnit: '${String(cost.unit)}'.`,\n });\n }\n};\n\nexport const costToNumber = (cost: Cost, targetUnit: CostUnit): number => {\n const nanos = getNanos(cost);\n\n if (targetUnit === 'nanos') return nanos;\n if (targetUnit === 'micros') return nanos / 1_000;\n if (targetUnit === 'cents') return nanos / 10_000_000;\n return nanos / 1_000_000_000; // base\n};\n\nexport const convertCostUnit = (cost: Cost, targetUnit: CostUnit): Cost => {\n return {\n amount: costToNumber(cost, targetUnit),\n currency: cost.currency,\n unit: targetUnit,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,SAAS,aAAa,OAA8C;AACzE,SAAO;AACT;;;AC0BO,SAAS,0BAEd,SAAuE;AACvE,QAAM,EAAE,YAAY,cAAc,aAAa,WAAW,QAAQ,IAAI;AAEtE,QAAM,eAAe,OAAO;AAAA,IAC1B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,MAM2C;AACzC,QAAI;AACF,YAAM,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|
|
1
|
-
import { LanguageModelV3, LanguageModelV3Usage, SharedV3ProviderMetadata, LanguageModelV3Middleware } from '@ai-sdk/provider';
|
|
1
|
+
import { JSONObject, LanguageModelV3, LanguageModelV3Usage, SharedV3ProviderMetadata, LanguageModelV3Middleware } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
|
-
type Destination<TTags = DefaultTags> = (event: BillingEvent<TTags>) => Promise<void> | void;
|
|
3
|
+
type Destination<TTags extends DefaultTags = DefaultTags> = (event: BillingEvent<TTags>) => Promise<void> | void;
|
|
4
4
|
|
|
5
|
-
type CostUnit = 'base' | 'cents' | 'millicents' | 'microcents';
|
|
6
|
-
interface Cost {
|
|
7
|
-
readonly amount: number;
|
|
8
|
-
readonly currency: string;
|
|
9
|
-
readonly unit: CostUnit;
|
|
10
|
-
}
|
|
11
5
|
interface Usage {
|
|
12
6
|
readonly subProviderId?: string;
|
|
13
7
|
readonly inputTokens: number;
|
|
@@ -19,7 +13,7 @@ interface Usage {
|
|
|
19
13
|
readonly requestCount?: number;
|
|
20
14
|
readonly rawProviderCost?: number;
|
|
21
15
|
}
|
|
22
|
-
interface BillingEvent<TTags = DefaultTags> {
|
|
16
|
+
interface BillingEvent<TTags extends DefaultTags = DefaultTags> {
|
|
23
17
|
readonly generationId: string;
|
|
24
18
|
readonly modelId: string;
|
|
25
19
|
readonly provider: string;
|
|
@@ -27,35 +21,42 @@ interface BillingEvent<TTags = DefaultTags> {
|
|
|
27
21
|
readonly cost: Cost;
|
|
28
22
|
readonly tags: TTags;
|
|
29
23
|
}
|
|
30
|
-
type EventBuilder<TPayload, TTags = DefaultTags> = (payload: TPayload) => Promise<BillingEvent<TTags> | null> | BillingEvent<TTags> | null;
|
|
24
|
+
type EventBuilder<TPayload, TTags extends DefaultTags = DefaultTags> = (payload: TPayload) => Promise<BillingEvent<TTags> | null> | BillingEvent<TTags> | null;
|
|
31
25
|
|
|
32
|
-
type DefaultTags =
|
|
33
|
-
interface BaseBillingMiddlewareOptions<TTags = DefaultTags> {
|
|
26
|
+
type DefaultTags = JSONObject;
|
|
27
|
+
interface BaseBillingMiddlewareOptions<TTags extends JSONObject = DefaultTags> {
|
|
34
28
|
destinations: Destination<TTags>[];
|
|
35
29
|
defaultTags?: TTags;
|
|
36
30
|
waitUntil?: (promise: Promise<unknown>) => void;
|
|
37
31
|
onError?: (error: unknown) => void;
|
|
38
32
|
}
|
|
39
33
|
|
|
40
|
-
|
|
34
|
+
type CostUnit = 'base' | 'cents' | 'micros' | 'nanos';
|
|
35
|
+
interface Cost {
|
|
36
|
+
readonly amount: number;
|
|
37
|
+
readonly currency: string;
|
|
38
|
+
readonly unit: CostUnit;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface BuildV3EventPayload<TTags extends DefaultTags = DefaultTags> {
|
|
41
42
|
responseId: string | undefined;
|
|
42
43
|
model: LanguageModelV3;
|
|
43
44
|
usage: LanguageModelV3Usage | undefined;
|
|
44
45
|
providerMetadata: SharedV3ProviderMetadata | undefined;
|
|
45
46
|
tags: TTags;
|
|
46
47
|
}
|
|
47
|
-
interface BillingMiddlewareV3Options<TTags> extends BaseBillingMiddlewareOptions<TTags> {
|
|
48
|
+
interface BillingMiddlewareV3Options<TTags extends DefaultTags = DefaultTags> extends BaseBillingMiddlewareOptions<TTags> {
|
|
48
49
|
buildEvent: EventBuilder<BuildV3EventPayload<TTags>, TTags>;
|
|
49
50
|
}
|
|
50
|
-
declare function createV3BillingMiddleware<TTags>(options: BillingMiddlewareV3Options<TTags>): LanguageModelV3Middleware;
|
|
51
|
+
declare function createV3BillingMiddleware<TTags extends DefaultTags = DefaultTags>(options: BillingMiddlewareV3Options<TTags>): LanguageModelV3Middleware;
|
|
51
52
|
|
|
52
|
-
declare function createDestination<TTags>(destinationId: string, handler: (event: BillingEvent<TTags>) => Promise<void> | void): Destination<TTags>;
|
|
53
|
+
declare function createDestination<TTags extends DefaultTags = DefaultTags>(destinationId: string, handler: (event: BillingEvent<TTags>) => Promise<void> | void): Destination<TTags>;
|
|
53
54
|
|
|
54
|
-
declare function consoleDestination<TTags>(): Destination<TTags>;
|
|
55
|
+
declare function consoleDestination<TTags extends DefaultTags = DefaultTags>(): Destination<TTags>;
|
|
55
56
|
|
|
56
|
-
declare const symbol$
|
|
57
|
+
declare const symbol$3: unique symbol;
|
|
57
58
|
declare class AIBillingError extends Error {
|
|
58
|
-
private readonly [symbol$
|
|
59
|
+
private readonly [symbol$3];
|
|
59
60
|
readonly cause?: unknown;
|
|
60
61
|
constructor({ name, message, cause, }: {
|
|
61
62
|
name: string;
|
|
@@ -66,9 +67,9 @@ declare class AIBillingError extends Error {
|
|
|
66
67
|
protected static hasMarker(error: unknown, markerString: string): boolean;
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
declare const symbol$
|
|
70
|
+
declare const symbol$2: unique symbol;
|
|
70
71
|
declare class AiBillingExtractorError extends AIBillingError {
|
|
71
|
-
private readonly [symbol$
|
|
72
|
+
private readonly [symbol$2];
|
|
72
73
|
constructor({ message, cause, }: {
|
|
73
74
|
message?: string;
|
|
74
75
|
cause?: unknown;
|
|
@@ -76,9 +77,9 @@ declare class AiBillingExtractorError extends AIBillingError {
|
|
|
76
77
|
static isInstance(error: unknown): error is AiBillingExtractorError;
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
declare const symbol: unique symbol;
|
|
80
|
+
declare const symbol$1: unique symbol;
|
|
80
81
|
declare class AiBillingDestinationError extends AIBillingError {
|
|
81
|
-
private readonly [symbol];
|
|
82
|
+
private readonly [symbol$1];
|
|
82
83
|
readonly destinationId?: string;
|
|
83
84
|
constructor({ destinationId, message, cause, }: {
|
|
84
85
|
destinationId?: string;
|
|
@@ -88,4 +89,17 @@ declare class AiBillingDestinationError extends AIBillingError {
|
|
|
88
89
|
static isInstance(error: unknown): error is AiBillingDestinationError;
|
|
89
90
|
}
|
|
90
91
|
|
|
91
|
-
|
|
92
|
+
declare const symbol: unique symbol;
|
|
93
|
+
declare class AiBillingCostError extends AIBillingError {
|
|
94
|
+
private readonly [symbol];
|
|
95
|
+
constructor({ message, cause }: {
|
|
96
|
+
message: string;
|
|
97
|
+
cause?: unknown;
|
|
98
|
+
});
|
|
99
|
+
static isInstance(error: unknown): error is AiBillingCostError;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
declare const costToNumber: (cost: Cost, targetUnit: CostUnit) => number;
|
|
103
|
+
declare const convertCostUnit: (cost: Cost, targetUnit: CostUnit) => Cost;
|
|
104
|
+
|
|
105
|
+
export { AIBillingError, AiBillingCostError, AiBillingDestinationError, AiBillingExtractorError, type BaseBillingMiddlewareOptions, type BillingEvent, type BillingMiddlewareV3Options, type BuildV3EventPayload, type Cost, type CostUnit, type DefaultTags, type Destination, type EventBuilder, type Usage, consoleDestination, convertCostUnit, costToNumber, createDestination, createV3BillingMiddleware };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
import { LanguageModelV3, LanguageModelV3Usage, SharedV3ProviderMetadata, LanguageModelV3Middleware } from '@ai-sdk/provider';
|
|
1
|
+
import { JSONObject, LanguageModelV3, LanguageModelV3Usage, SharedV3ProviderMetadata, LanguageModelV3Middleware } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
|
-
type Destination<TTags = DefaultTags> = (event: BillingEvent<TTags>) => Promise<void> | void;
|
|
3
|
+
type Destination<TTags extends DefaultTags = DefaultTags> = (event: BillingEvent<TTags>) => Promise<void> | void;
|
|
4
4
|
|
|
5
|
-
type CostUnit = 'base' | 'cents' | 'millicents' | 'microcents';
|
|
6
|
-
interface Cost {
|
|
7
|
-
readonly amount: number;
|
|
8
|
-
readonly currency: string;
|
|
9
|
-
readonly unit: CostUnit;
|
|
10
|
-
}
|
|
11
5
|
interface Usage {
|
|
12
6
|
readonly subProviderId?: string;
|
|
13
7
|
readonly inputTokens: number;
|
|
@@ -19,7 +13,7 @@ interface Usage {
|
|
|
19
13
|
readonly requestCount?: number;
|
|
20
14
|
readonly rawProviderCost?: number;
|
|
21
15
|
}
|
|
22
|
-
interface BillingEvent<TTags = DefaultTags> {
|
|
16
|
+
interface BillingEvent<TTags extends DefaultTags = DefaultTags> {
|
|
23
17
|
readonly generationId: string;
|
|
24
18
|
readonly modelId: string;
|
|
25
19
|
readonly provider: string;
|
|
@@ -27,35 +21,42 @@ interface BillingEvent<TTags = DefaultTags> {
|
|
|
27
21
|
readonly cost: Cost;
|
|
28
22
|
readonly tags: TTags;
|
|
29
23
|
}
|
|
30
|
-
type EventBuilder<TPayload, TTags = DefaultTags> = (payload: TPayload) => Promise<BillingEvent<TTags> | null> | BillingEvent<TTags> | null;
|
|
24
|
+
type EventBuilder<TPayload, TTags extends DefaultTags = DefaultTags> = (payload: TPayload) => Promise<BillingEvent<TTags> | null> | BillingEvent<TTags> | null;
|
|
31
25
|
|
|
32
|
-
type DefaultTags =
|
|
33
|
-
interface BaseBillingMiddlewareOptions<TTags = DefaultTags> {
|
|
26
|
+
type DefaultTags = JSONObject;
|
|
27
|
+
interface BaseBillingMiddlewareOptions<TTags extends JSONObject = DefaultTags> {
|
|
34
28
|
destinations: Destination<TTags>[];
|
|
35
29
|
defaultTags?: TTags;
|
|
36
30
|
waitUntil?: (promise: Promise<unknown>) => void;
|
|
37
31
|
onError?: (error: unknown) => void;
|
|
38
32
|
}
|
|
39
33
|
|
|
40
|
-
|
|
34
|
+
type CostUnit = 'base' | 'cents' | 'micros' | 'nanos';
|
|
35
|
+
interface Cost {
|
|
36
|
+
readonly amount: number;
|
|
37
|
+
readonly currency: string;
|
|
38
|
+
readonly unit: CostUnit;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface BuildV3EventPayload<TTags extends DefaultTags = DefaultTags> {
|
|
41
42
|
responseId: string | undefined;
|
|
42
43
|
model: LanguageModelV3;
|
|
43
44
|
usage: LanguageModelV3Usage | undefined;
|
|
44
45
|
providerMetadata: SharedV3ProviderMetadata | undefined;
|
|
45
46
|
tags: TTags;
|
|
46
47
|
}
|
|
47
|
-
interface BillingMiddlewareV3Options<TTags> extends BaseBillingMiddlewareOptions<TTags> {
|
|
48
|
+
interface BillingMiddlewareV3Options<TTags extends DefaultTags = DefaultTags> extends BaseBillingMiddlewareOptions<TTags> {
|
|
48
49
|
buildEvent: EventBuilder<BuildV3EventPayload<TTags>, TTags>;
|
|
49
50
|
}
|
|
50
|
-
declare function createV3BillingMiddleware<TTags>(options: BillingMiddlewareV3Options<TTags>): LanguageModelV3Middleware;
|
|
51
|
+
declare function createV3BillingMiddleware<TTags extends DefaultTags = DefaultTags>(options: BillingMiddlewareV3Options<TTags>): LanguageModelV3Middleware;
|
|
51
52
|
|
|
52
|
-
declare function createDestination<TTags>(destinationId: string, handler: (event: BillingEvent<TTags>) => Promise<void> | void): Destination<TTags>;
|
|
53
|
+
declare function createDestination<TTags extends DefaultTags = DefaultTags>(destinationId: string, handler: (event: BillingEvent<TTags>) => Promise<void> | void): Destination<TTags>;
|
|
53
54
|
|
|
54
|
-
declare function consoleDestination<TTags>(): Destination<TTags>;
|
|
55
|
+
declare function consoleDestination<TTags extends DefaultTags = DefaultTags>(): Destination<TTags>;
|
|
55
56
|
|
|
56
|
-
declare const symbol$
|
|
57
|
+
declare const symbol$3: unique symbol;
|
|
57
58
|
declare class AIBillingError extends Error {
|
|
58
|
-
private readonly [symbol$
|
|
59
|
+
private readonly [symbol$3];
|
|
59
60
|
readonly cause?: unknown;
|
|
60
61
|
constructor({ name, message, cause, }: {
|
|
61
62
|
name: string;
|
|
@@ -66,9 +67,9 @@ declare class AIBillingError extends Error {
|
|
|
66
67
|
protected static hasMarker(error: unknown, markerString: string): boolean;
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
declare const symbol$
|
|
70
|
+
declare const symbol$2: unique symbol;
|
|
70
71
|
declare class AiBillingExtractorError extends AIBillingError {
|
|
71
|
-
private readonly [symbol$
|
|
72
|
+
private readonly [symbol$2];
|
|
72
73
|
constructor({ message, cause, }: {
|
|
73
74
|
message?: string;
|
|
74
75
|
cause?: unknown;
|
|
@@ -76,9 +77,9 @@ declare class AiBillingExtractorError extends AIBillingError {
|
|
|
76
77
|
static isInstance(error: unknown): error is AiBillingExtractorError;
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
declare const symbol: unique symbol;
|
|
80
|
+
declare const symbol$1: unique symbol;
|
|
80
81
|
declare class AiBillingDestinationError extends AIBillingError {
|
|
81
|
-
private readonly [symbol];
|
|
82
|
+
private readonly [symbol$1];
|
|
82
83
|
readonly destinationId?: string;
|
|
83
84
|
constructor({ destinationId, message, cause, }: {
|
|
84
85
|
destinationId?: string;
|
|
@@ -88,4 +89,17 @@ declare class AiBillingDestinationError extends AIBillingError {
|
|
|
88
89
|
static isInstance(error: unknown): error is AiBillingDestinationError;
|
|
89
90
|
}
|
|
90
91
|
|
|
91
|
-
|
|
92
|
+
declare const symbol: unique symbol;
|
|
93
|
+
declare class AiBillingCostError extends AIBillingError {
|
|
94
|
+
private readonly [symbol];
|
|
95
|
+
constructor({ message, cause }: {
|
|
96
|
+
message: string;
|
|
97
|
+
cause?: unknown;
|
|
98
|
+
});
|
|
99
|
+
static isInstance(error: unknown): error is AiBillingCostError;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
declare const costToNumber: (cost: Cost, targetUnit: CostUnit) => number;
|
|
103
|
+
declare const convertCostUnit: (cost: Cost, targetUnit: CostUnit) => Cost;
|
|
104
|
+
|
|
105
|
+
export { AIBillingError, AiBillingCostError, AiBillingDestinationError, AiBillingExtractorError, type BaseBillingMiddlewareOptions, type BillingEvent, type BillingMiddlewareV3Options, type BuildV3EventPayload, type Cost, type CostUnit, type DefaultTags, type Destination, type EventBuilder, type Usage, consoleDestination, convertCostUnit, costToNumber, createDestination, createV3BillingMiddleware };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
// src/event/to-json-object.ts
|
|
2
|
+
function toJSONObject(event) {
|
|
3
|
+
return event;
|
|
4
|
+
}
|
|
5
|
+
|
|
1
6
|
// src/ai-sdk/language-model-middleware/v3/language-model-v3-base-billing-middleware.ts
|
|
2
7
|
function createV3BillingMiddleware(options) {
|
|
3
8
|
const { buildEvent, destinations, defaultTags, waitUntil, onError } = options;
|
|
@@ -9,11 +14,10 @@ function createV3BillingMiddleware(options) {
|
|
|
9
14
|
responseId
|
|
10
15
|
}) => {
|
|
11
16
|
try {
|
|
12
|
-
const
|
|
13
|
-
const headerTags = rawHeader ? JSON.parse(rawHeader) : {};
|
|
17
|
+
const requestTags = params.providerOptions?.["ai-billing-tags"];
|
|
14
18
|
const tags = {
|
|
15
19
|
...defaultTags ?? {},
|
|
16
|
-
...
|
|
20
|
+
...requestTags ?? {}
|
|
17
21
|
};
|
|
18
22
|
const event = await buildEvent({
|
|
19
23
|
responseId,
|
|
@@ -22,35 +26,47 @@ function createV3BillingMiddleware(options) {
|
|
|
22
26
|
providerMetadata,
|
|
23
27
|
tags
|
|
24
28
|
});
|
|
25
|
-
if (event) {
|
|
26
|
-
|
|
29
|
+
if (event && destinations.length > 0) {
|
|
30
|
+
const dispatchDestinationsPromise = Promise.allSettled(
|
|
27
31
|
destinations.map((d) => Promise.resolve(d(event)))
|
|
28
32
|
);
|
|
33
|
+
if (waitUntil) waitUntil(dispatchDestinationsPromise);
|
|
29
34
|
}
|
|
35
|
+
return event;
|
|
30
36
|
} catch (err) {
|
|
31
37
|
if (onError) onError(err);
|
|
32
38
|
else console.error("[ai-billing] Core Error:", err);
|
|
39
|
+
return null;
|
|
33
40
|
}
|
|
34
41
|
};
|
|
35
42
|
return {
|
|
36
43
|
specificationVersion: "v3",
|
|
37
44
|
wrapGenerate: async ({ doGenerate, model, params }) => {
|
|
38
45
|
const result = await doGenerate();
|
|
39
|
-
const
|
|
46
|
+
const event = await processEvent({
|
|
40
47
|
model,
|
|
41
48
|
params,
|
|
42
49
|
usage: result.usage,
|
|
43
50
|
providerMetadata: result.providerMetadata,
|
|
44
51
|
responseId: result.response?.id
|
|
45
52
|
});
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
const providerMetadataWithBilling = {
|
|
54
|
+
...result.providerMetadata
|
|
55
|
+
};
|
|
56
|
+
if (event) {
|
|
57
|
+
providerMetadataWithBilling["ai-billing"] = toJSONObject(event);
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
...result,
|
|
61
|
+
providerMetadata: providerMetadataWithBilling
|
|
62
|
+
};
|
|
48
63
|
},
|
|
49
64
|
wrapStream: async ({ doStream, model, params }) => {
|
|
50
65
|
const { stream, ...rest } = await doStream();
|
|
51
66
|
let responseId;
|
|
52
67
|
let usage;
|
|
53
68
|
let providerMetadata;
|
|
69
|
+
let finishChunk;
|
|
54
70
|
const billedStream = stream.pipeThrough(
|
|
55
71
|
new TransformStream({
|
|
56
72
|
transform(chunk, controller) {
|
|
@@ -61,18 +77,31 @@ function createV3BillingMiddleware(options) {
|
|
|
61
77
|
if (chunk.type === "finish") {
|
|
62
78
|
usage = chunk.usage;
|
|
63
79
|
providerMetadata = chunk.providerMetadata;
|
|
80
|
+
finishChunk = chunk;
|
|
81
|
+
return;
|
|
64
82
|
}
|
|
65
83
|
controller.enqueue(chunk);
|
|
66
84
|
},
|
|
67
|
-
flush() {
|
|
68
|
-
const
|
|
85
|
+
async flush(controller) {
|
|
86
|
+
const event = await processEvent({
|
|
69
87
|
model,
|
|
70
88
|
params,
|
|
71
89
|
usage,
|
|
72
90
|
providerMetadata,
|
|
73
91
|
responseId
|
|
74
92
|
});
|
|
75
|
-
|
|
93
|
+
const providerMetadataWithBilling = {
|
|
94
|
+
...providerMetadata
|
|
95
|
+
};
|
|
96
|
+
if (event) {
|
|
97
|
+
providerMetadataWithBilling["ai-billing"] = toJSONObject(event);
|
|
98
|
+
}
|
|
99
|
+
if (finishChunk) {
|
|
100
|
+
controller.enqueue({
|
|
101
|
+
...finishChunk,
|
|
102
|
+
providerMetadata: providerMetadataWithBilling
|
|
103
|
+
});
|
|
104
|
+
}
|
|
76
105
|
}
|
|
77
106
|
})
|
|
78
107
|
);
|
|
@@ -88,12 +117,12 @@ var AIBillingError = class _AIBillingError extends Error {
|
|
|
88
117
|
[symbol] = true;
|
|
89
118
|
cause;
|
|
90
119
|
constructor({
|
|
91
|
-
name:
|
|
120
|
+
name: name4,
|
|
92
121
|
message,
|
|
93
122
|
cause
|
|
94
123
|
}) {
|
|
95
124
|
super(message);
|
|
96
|
-
this.name =
|
|
125
|
+
this.name = name4;
|
|
97
126
|
this.cause = cause;
|
|
98
127
|
}
|
|
99
128
|
static isInstance(error) {
|
|
@@ -142,6 +171,20 @@ var AiBillingDestinationError = class extends AIBillingError {
|
|
|
142
171
|
}
|
|
143
172
|
};
|
|
144
173
|
|
|
174
|
+
// src/error/cost-error.ts
|
|
175
|
+
var name3 = "AiBillingCostError";
|
|
176
|
+
var marker4 = `ai-billing.error.${name3}`;
|
|
177
|
+
var symbol4 = Symbol.for(marker4);
|
|
178
|
+
var AiBillingCostError = class extends AIBillingError {
|
|
179
|
+
[symbol4] = true;
|
|
180
|
+
constructor({ message, cause }) {
|
|
181
|
+
super({ name: name3, message, cause });
|
|
182
|
+
}
|
|
183
|
+
static isInstance(error) {
|
|
184
|
+
return AIBillingError.hasMarker(error, marker4);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
145
188
|
// src/destination/base-destination.ts
|
|
146
189
|
function createDestination(destinationId, handler) {
|
|
147
190
|
return async (event) => {
|
|
@@ -166,11 +209,46 @@ function consoleDestination() {
|
|
|
166
209
|
});
|
|
167
210
|
});
|
|
168
211
|
}
|
|
212
|
+
|
|
213
|
+
// src/cost/convert-cost.ts
|
|
214
|
+
var getNanos = (cost) => {
|
|
215
|
+
switch (cost.unit) {
|
|
216
|
+
case "base":
|
|
217
|
+
return Math.round(cost.amount * 1e9);
|
|
218
|
+
case "cents":
|
|
219
|
+
return Math.round(cost.amount * 1e7);
|
|
220
|
+
case "micros":
|
|
221
|
+
return Math.round(cost.amount * 1e3);
|
|
222
|
+
case "nanos":
|
|
223
|
+
return Math.round(cost.amount);
|
|
224
|
+
default:
|
|
225
|
+
throw new AiBillingCostError({
|
|
226
|
+
message: `Failed to process cost. Unknown CostUnit: '${String(cost.unit)}'.`
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
var costToNumber = (cost, targetUnit) => {
|
|
231
|
+
const nanos = getNanos(cost);
|
|
232
|
+
if (targetUnit === "nanos") return nanos;
|
|
233
|
+
if (targetUnit === "micros") return nanos / 1e3;
|
|
234
|
+
if (targetUnit === "cents") return nanos / 1e7;
|
|
235
|
+
return nanos / 1e9;
|
|
236
|
+
};
|
|
237
|
+
var convertCostUnit = (cost, targetUnit) => {
|
|
238
|
+
return {
|
|
239
|
+
amount: costToNumber(cost, targetUnit),
|
|
240
|
+
currency: cost.currency,
|
|
241
|
+
unit: targetUnit
|
|
242
|
+
};
|
|
243
|
+
};
|
|
169
244
|
export {
|
|
170
245
|
AIBillingError,
|
|
246
|
+
AiBillingCostError,
|
|
171
247
|
AiBillingDestinationError,
|
|
172
248
|
AiBillingExtractorError,
|
|
173
249
|
consoleDestination,
|
|
250
|
+
convertCostUnit,
|
|
251
|
+
costToNumber,
|
|
174
252
|
createDestination,
|
|
175
253
|
createV3BillingMiddleware
|
|
176
254
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/ai-sdk/language-model-middleware/v3/language-model-v3-base-billing-middleware.ts","../src/error/ai-billing-error.ts","../src/error/extractor-error.ts","../src/error/destination-error.ts","../src/destination/base-destination.ts","../src/destination/console-destination.ts"],"sourcesContent":["import type {\n LanguageModelV3,\n LanguageModelV3CallOptions,\n LanguageModelV3Usage,\n LanguageModelV3GenerateResult,\n LanguageModelV3StreamPart,\n LanguageModelV3Middleware,\n SharedV3ProviderMetadata,\n} from '@ai-sdk/provider';\nimport type {\n BaseBillingMiddlewareOptions,\n EventBuilder,\n} from '../../../types/index.js';\n\nexport interface BuildV3EventPayload<TTags> {\n responseId: string | undefined;\n model: LanguageModelV3;\n usage: LanguageModelV3Usage | undefined;\n providerMetadata: SharedV3ProviderMetadata | undefined;\n tags: TTags;\n}\n\nexport interface BillingMiddlewareV3Options<\n TTags,\n> extends BaseBillingMiddlewareOptions<TTags> {\n buildEvent: EventBuilder<BuildV3EventPayload<TTags>, TTags>;\n}\n\nexport function createV3BillingMiddleware<TTags>(\n options: BillingMiddlewareV3Options<TTags>,\n): LanguageModelV3Middleware {\n const { buildEvent, destinations, defaultTags, waitUntil, onError } = options;\n\n const processEvent = async ({\n model,\n params,\n usage,\n providerMetadata,\n responseId,\n }: {\n model: LanguageModelV3;\n params: LanguageModelV3CallOptions;\n usage: LanguageModelV3Usage | undefined;\n providerMetadata: SharedV3ProviderMetadata | undefined;\n responseId: string | undefined;\n }): Promise<void> => {\n try {\n const rawHeader = params.headers?.['x-ai-billing-tags'];\n const headerTags = rawHeader ? JSON.parse(rawHeader) : {};\n\n const tags = {\n ...(defaultTags ?? {}),\n ...headerTags,\n } as TTags;\n\n const event = await buildEvent({\n responseId,\n model,\n usage,\n providerMetadata,\n tags,\n });\n\n if (event) {\n await Promise.allSettled(\n destinations.map(d => Promise.resolve(d(event))),\n );\n }\n } catch (err) {\n if (onError) onError(err);\n else console.error('[ai-billing] Core Error:', err);\n }\n };\n\n return {\n specificationVersion: 'v3',\n\n wrapGenerate: async ({ doGenerate, model, params }) => {\n const result: LanguageModelV3GenerateResult = await doGenerate();\n\n const promise = processEvent({\n model,\n params,\n usage: result.usage,\n providerMetadata: result.providerMetadata,\n responseId: result.response?.id,\n });\n\n if (waitUntil) waitUntil(promise);\n return result;\n },\n\n wrapStream: async ({ doStream, model, params }) => {\n const { stream, ...rest } = await doStream();\n\n let responseId: string | undefined;\n let usage: LanguageModelV3Usage | undefined;\n let providerMetadata: SharedV3ProviderMetadata | undefined;\n\n const billedStream = stream.pipeThrough(\n new TransformStream<\n LanguageModelV3StreamPart,\n LanguageModelV3StreamPart\n >({\n transform(chunk, controller) {\n if (chunk.type === 'text-start') responseId = chunk.id;\n if (chunk.type === 'response-metadata' && !responseId) {\n responseId = chunk.id;\n }\n if (chunk.type === 'finish') {\n usage = chunk.usage;\n providerMetadata = chunk.providerMetadata;\n }\n controller.enqueue(chunk);\n },\n flush() {\n const promise = processEvent({\n model,\n params,\n usage,\n providerMetadata,\n responseId,\n });\n if (waitUntil) waitUntil(promise);\n },\n }),\n );\n\n return { ...rest, stream: billedStream };\n },\n };\n}\n","const marker = 'ai-billing.error';\nconst symbol = Symbol.for(marker);\n\nexport class AIBillingError extends Error {\n private readonly [symbol] = true;\n\n readonly cause?: unknown;\n\n constructor({\n name,\n message,\n cause,\n }: {\n name: string;\n message: string;\n cause?: unknown;\n }) {\n super(message);\n this.name = name;\n this.cause = cause;\n }\n\n static isInstance(error: unknown): error is AIBillingError {\n return AIBillingError.hasMarker(error, marker);\n }\n\n protected static hasMarker(error: unknown, markerString: string): boolean {\n const markerSymbol = Symbol.for(markerString);\n return (\n error != null &&\n typeof error === 'object' &&\n markerSymbol in error &&\n typeof error[markerSymbol] === 'boolean' &&\n error[markerSymbol] === true\n );\n }\n}\n","import { AIBillingError } from './ai-billing-error.js';\n\nconst name = 'AiBillingExtractorError';\nconst marker = `ai-billing.error.${name}`;\nconst symbol = Symbol.for(marker);\n\nexport class AiBillingExtractorError extends AIBillingError {\n private readonly [symbol] = true;\n\n constructor({\n message = `Failed to extract billing data.`,\n cause,\n }: {\n message?: string;\n cause?: unknown;\n }) {\n super({ name, message, cause });\n }\n\n static isInstance(error: unknown): error is AiBillingExtractorError {\n return AIBillingError.hasMarker(error, marker);\n }\n}\n","import { AIBillingError } from './ai-billing-error.js';\n\nconst name = 'AiBillingDestinationError';\nconst marker = `ai-billing.error.${name}`;\nconst symbol = Symbol.for(marker);\n\nexport class AiBillingDestinationError extends AIBillingError {\n private readonly [symbol] = true;\n\n readonly destinationId?: string;\n\n constructor({\n destinationId,\n message = `Failed to process billing data for destination: '${destinationId}'.`,\n cause,\n }: {\n destinationId?: string;\n message?: string;\n cause?: unknown;\n }) {\n super({ name, message, cause });\n this.destinationId = destinationId;\n }\n\n static isInstance(error: unknown): error is AiBillingDestinationError {\n return AIBillingError.hasMarker(error, marker);\n }\n}\n","import type { Destination, BillingEvent } from '../types/index.js';\nimport { AiBillingDestinationError } from '../error/index.js';\n\nexport function createDestination<TTags>(\n destinationId: string,\n handler: (event: BillingEvent<TTags>) => Promise<void> | void,\n): Destination<TTags> {\n return async (event: BillingEvent<TTags>) => {\n try {\n await handler(event);\n } catch (error) {\n throw new AiBillingDestinationError({\n destinationId,\n cause: error,\n });\n }\n };\n}\n","import { createDestination } from './base-destination.js';\nimport type { Destination } from '../types/index.js';\n\nexport function consoleDestination<TTags>(): Destination<TTags> {\n return createDestination<TTags>('console-logger', event => {\n console.dir(event, {\n depth: null,\n colors: true,\n compact: false,\n });\n });\n}\n"],"mappings":";AA4BO,SAAS,0BACd,SAC2B;AAC3B,QAAM,EAAE,YAAY,cAAc,aAAa,WAAW,QAAQ,IAAI;AAEtE,QAAM,eAAe,OAAO;AAAA,IAC1B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,MAMqB;AACnB,QAAI;AACF,YAAM,YAAY,OAAO,UAAU,mBAAmB;AACtD,YAAM,aAAa,YAAY,KAAK,MAAM,SAAS,IAAI,CAAC;AAExD,YAAM,OAAO;AAAA,QACX,GAAI,eAAe,CAAC;AAAA,QACpB,GAAG;AAAA,MACL;AAEA,YAAM,QAAQ,MAAM,WAAW;AAAA,QAC7B;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC;AAED,UAAI,OAAO;AACT,cAAM,QAAQ;AAAA,UACZ,aAAa,IAAI,OAAK,QAAQ,QAAQ,EAAE,KAAK,CAAC,CAAC;AAAA,QACjD;AAAA,MACF;AAAA,IACF,SAAS,KAAK;AACZ,UAAI,QAAS,SAAQ,GAAG;AAAA,UACnB,SAAQ,MAAM,4BAA4B,G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AO,OAAO,UAA+B;AAC3C,QAAI;AACF,YAAM,QAAQ,KAAK;AAAA,IACrB,SAAS,OAAO;AACd,YAAM,IAAI,0BAA0B;AAAA,QAClC;AAAA,QACA,OAAO;AAAA,MACT,CAAC;AAAA,IACH;AAAA,EACF;AACF;;;ACdO,SAAS,qBAAgD;AAC9D,SAAO,kBAAyB,kBAAkB,WAAS;AACzD,YAAQ,IAAI,OAAO;AAAA,MACjB,OAAO;AAAA,MACP,QAAQ;AAAA,MACR,SAAS;AAAA,IACX,CAAC;AAAA,EACH,CAAC;AACH;","names":["name","marker","symbol","name","marker","symbol"]}
|
|
1
|
+
{"version":3,"sources":["../src/event/to-json-object.ts","../src/ai-sdk/language-model-middleware/v3/language-model-v3-base-billing-middleware.ts","../src/error/ai-billing-error.ts","../src/error/extractor-error.ts","../src/error/destination-error.ts","../src/error/cost-error.ts","../src/destination/base-destination.ts","../src/destination/console-destination.ts","../src/cost/convert-cost.ts"],"sourcesContent":["import { DefaultTags, BillingEvent } from '../types/index.js';\nimport type { JSONObject } from '@ai-sdk/provider';\n\nexport function toJSONObject(event: BillingEvent<DefaultTags>): JSONObject {\n return event as unknown as JSONObject;\n}\n","import type {\n LanguageModelV3,\n LanguageModelV3CallOptions,\n LanguageModelV3Usage,\n LanguageModelV3GenerateResult,\n LanguageModelV3StreamPart,\n LanguageModelV3Middleware,\n SharedV3ProviderMetadata,\n} from '@ai-sdk/provider';\nimport type {\n BaseBillingMiddlewareOptions,\n EventBuilder,\n BillingEvent,\n DefaultTags,\n} from '../../../types/index.js';\nimport { toJSONObject } from '../../../event/index.js';\n\nexport interface BuildV3EventPayload<TTags extends DefaultTags = DefaultTags> {\n responseId: string | undefined;\n model: LanguageModelV3;\n usage: LanguageModelV3Usage | undefined;\n providerMetadata: SharedV3ProviderMetadata | undefined;\n tags: TTags;\n}\n\nexport interface BillingMiddlewareV3Options<\n TTags extends DefaultTags = DefaultTags,\n> extends BaseBillingMiddlewareOptions<TTags> {\n buildEvent: EventBuilder<BuildV3EventPayload<TTags>, TTags>;\n}\n\nexport function createV3BillingMiddleware<\n TTags extends DefaultTags = DefaultTags,\n>(options: BillingMiddlewareV3Options<TTags>): LanguageModelV3Middleware {\n const { buildEvent, destinations, defaultTags, waitUntil, onError } = options;\n\n const processEvent = async ({\n model,\n params,\n usage,\n providerMetadata,\n responseId,\n }: {\n model: LanguageModelV3;\n params: LanguageModelV3CallOptions;\n usage: LanguageModelV3Usage | undefined;\n providerMetadata: SharedV3ProviderMetadata | undefined;\n responseId: string | undefined;\n }): Promise<BillingEvent<TTags> | null> => {\n try {\n const requestTags = params.providerOptions?.['ai-billing-tags'];\n\n const tags = {\n ...(defaultTags ?? {}),\n ...(requestTags ?? {}),\n } as TTags;\n\n const event = await buildEvent({\n responseId,\n model,\n usage,\n providerMetadata,\n tags,\n });\n\n if (event && destinations.length > 0) {\n const dispatchDestinationsPromise = Promise.allSettled(\n destinations.map(d => Promise.resolve(d(event))),\n );\n if (waitUntil) waitUntil(dispatchDestinationsPromise);\n }\n return event;\n } catch (err) {\n if (onError) onError(err);\n else console.error('[ai-billing] Core Error:', err);\n return null;\n }\n };\n\n return {\n specificationVersion: 'v3',\n\n wrapGenerate: async ({ doGenerate, model, params }) => {\n const result: LanguageModelV3GenerateResult = await doGenerate();\n\n const event = await processEvent({\n model,\n params,\n usage: result.usage,\n providerMetadata: result.providerMetadata,\n responseId: result.response?.id,\n });\n\n const providerMetadataWithBilling = {\n ...result.providerMetadata,\n } as SharedV3ProviderMetadata;\n\n if (event) {\n providerMetadataWithBilling['ai-billing'] = toJSONObject(event);\n }\n\n return {\n ...result,\n providerMetadata: providerMetadataWithBilling,\n };\n },\n\n wrapStream: async ({ doStream, model, params }) => {\n const { stream, ...rest } = await doStream();\n\n let responseId: string | undefined;\n let usage: LanguageModelV3Usage | undefined;\n let providerMetadata: SharedV3ProviderMetadata | undefined;\n let finishChunk:\n | Extract<LanguageModelV3StreamPart, { type: 'finish' }>\n | undefined;\n\n const billedStream = stream.pipeThrough(\n new TransformStream<\n LanguageModelV3StreamPart,\n LanguageModelV3StreamPart\n >({\n transform(chunk, controller) {\n if (chunk.type === 'text-start') responseId = chunk.id;\n if (chunk.type === 'response-metadata' && !responseId) {\n responseId = chunk.id;\n }\n if (chunk.type === 'finish') {\n usage = chunk.usage;\n providerMetadata = chunk.providerMetadata;\n finishChunk = chunk;\n return; // held until flush\n }\n controller.enqueue(chunk);\n },\n async flush(controller) {\n const event = await processEvent({\n model,\n params,\n usage,\n providerMetadata,\n responseId,\n });\n\n const providerMetadataWithBilling = {\n ...providerMetadata,\n } as SharedV3ProviderMetadata;\n\n if (event) {\n providerMetadataWithBilling['ai-billing'] = toJSONObject(event);\n }\n\n if (finishChunk) {\n controller.enqueue({\n ...finishChunk,\n providerMetadata: providerMetadataWithBilling,\n });\n }\n },\n }),\n );\n\n return { ...rest, stream: billedStream };\n },\n };\n}\n","const marker = 'ai-billing.error';\nconst symbol = Symbol.for(marker);\n\nexport class AIBillingError extends Error {\n private readonly [symbol] = true;\n\n readonly cause?: unknown;\n\n constructor({\n name,\n message,\n cause,\n }: {\n name: string;\n message: string;\n cause?: unknown;\n }) {\n super(message);\n this.name = name;\n this.cause = cause;\n }\n\n static isInstance(error: unknown): error is AIBillingError {\n return AIBillingError.hasMarker(error, marker);\n }\n\n protected static hasMarker(error: unknown, markerString: string): boolean {\n const markerSymbol = Symbol.for(markerString);\n return (\n error != null &&\n typeof error === 'object' &&\n markerSymbol in error &&\n typeof error[markerSymbol] === 'boolean' &&\n error[markerSymbol] === true\n );\n }\n}\n","import { AIBillingError } from './ai-billing-error.js';\n\nconst name = 'AiBillingExtractorError';\nconst marker = `ai-billing.error.${name}`;\nconst symbol = Symbol.for(marker);\n\nexport class AiBillingExtractorError extends AIBillingError {\n private readonly [symbol] = true;\n\n constructor({\n message = `Failed to extract billing data.`,\n cause,\n }: {\n message?: string;\n cause?: unknown;\n }) {\n super({ name, message, cause });\n }\n\n static isInstance(error: unknown): error is AiBillingExtractorError {\n return AIBillingError.hasMarker(error, marker);\n }\n}\n","import { AIBillingError } from './ai-billing-error.js';\n\nconst name = 'AiBillingDestinationError';\nconst marker = `ai-billing.error.${name}`;\nconst symbol = Symbol.for(marker);\n\nexport class AiBillingDestinationError extends AIBillingError {\n private readonly [symbol] = true;\n\n readonly destinationId?: string;\n\n constructor({\n destinationId,\n message = `Failed to process billing data for destination: '${destinationId}'.`,\n cause,\n }: {\n destinationId?: string;\n message?: string;\n cause?: unknown;\n }) {\n super({ name, message, cause });\n this.destinationId = destinationId;\n }\n\n static isInstance(error: unknown): error is AiBillingDestinationError {\n return AIBillingError.hasMarker(error, marker);\n }\n}\n","import { AIBillingError } from './ai-billing-error.js';\n\nconst name = 'AiBillingCostError';\nconst marker = `ai-billing.error.${name}`;\nconst symbol = Symbol.for(marker);\n\nexport class AiBillingCostError extends AIBillingError {\n private readonly [symbol] = true;\n\n constructor({ message, cause }: { message: string; cause?: unknown }) {\n super({ name, message, cause });\n }\n\n static isInstance(error: unknown): error is AiBillingCostError {\n return AIBillingError.hasMarker(error, marker);\n }\n}\n","import type { Destination, BillingEvent, DefaultTags } from '../types/index.js';\nimport { AiBillingDestinationError } from '../error/index.js';\n\nexport function createDestination<TTags extends DefaultTags = DefaultTags>(\n destinationId: string,\n handler: (event: BillingEvent<TTags>) => Promise<void> | void,\n): Destination<TTags> {\n return async (event: BillingEvent<TTags>) => {\n try {\n await handler(event);\n } catch (error) {\n throw new AiBillingDestinationError({\n destinationId,\n cause: error,\n });\n }\n };\n}\n","import { createDestination } from './base-destination.js';\nimport type { DefaultTags, Destination } from '../types/index.js';\nimport { JSONObject } from '@ai-sdk/provider';\n\nexport function consoleDestination<\n TTags extends DefaultTags = DefaultTags,\n>(): Destination<TTags> {\n return createDestination<TTags>('console-logger', event => {\n console.dir(event, {\n depth: null,\n colors: true,\n compact: false,\n });\n });\n}\n","import { AiBillingCostError } from '../index.js';\nimport type { Cost, CostUnit } from '../index.js';\n\nconst getNanos = (cost: Cost): number => {\n switch (cost.unit) {\n case 'base':\n return Math.round(cost.amount * 1_000_000_000);\n case 'cents':\n return Math.round(cost.amount * 10_000_000);\n case 'micros':\n return Math.round(cost.amount * 1_000);\n case 'nanos':\n return Math.round(cost.amount);\n default:\n throw new AiBillingCostError({\n message: `Failed to process cost. Unknown CostUnit: '${String(cost.unit)}'.`,\n });\n }\n};\n\nexport const costToNumber = (cost: Cost, targetUnit: CostUnit): number => {\n const nanos = getNanos(cost);\n\n if (targetUnit === 'nanos') return nanos;\n if (targetUnit === 'micros') return nanos / 1_000;\n if (targetUnit === 'cents') return nanos / 10_000_000;\n return nanos / 1_000_000_000; // base\n};\n\nexport const convertCostUnit = (cost: Cost, targetUnit: CostUnit): Cost => {\n return {\n amount: costToNumber(cost, targetUnit),\n currency: cost.currency,\n unit: targetUnit,\n };\n};\n"],"mappings":";AAGO,SAAS,aAAa,OAA8C;AACzE,SAAO;AACT;;;AC0BO,SAAS,0BAEd,SAAuE;AACvE,QAAM,EAAE,YAAY,cAAc,aAAa,WAAW,QAAQ,IAAI;AAEtE,QAAM,eAAe,OAAO;AAAA,IAC1B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,MAM2C;AACzC,QAAI;AACF,YAAM,cAAc,OAAO,kBAAkB,iBAAiB;AAE9D,YAAM,OAAO;AAAA,QACX,GAAI,eAAe,CAAC;AAAA,QACpB,GAAI,eAAe,CAAC;AAAA,MACtB;AAEA,YAAM,QAAQ,MAAM,WAAW;AAAA,QAC7B;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC;AAED,UAAI,SAAS,aAAa,SAAS,GAAG;AACpC,cAAM,8BAA8B,QAAQ;AAAA,UAC1C,aAAa,IAAI,OAAK,QAAQ,QAAQ,EAAE,KAAK,CAAC,CAAC;AAAA,QACjD;AACA,YAAI,UAAW,WAAU,2BAA2B;AAAA,MACtD;AACA,aAAO;AAAA,IACT,SAAS,KAAK;AACZ,U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AAK;AACH,aAAO,KAAK,MAAM,KAAK,SAAS,GAAa;AAAA,IAC/C,KAAK;AACH,aAAO,KAAK,MAAM,KAAK,SAAS,GAAU;AAAA,IAC5C,KAAK;AACH,aAAO,KAAK,MAAM,KAAK,SAAS,GAAK;AAAA,IACvC,KAAK;AACH,aAAO,KAAK,MAAM,KAAK,MAAM;AAAA,IAC/B;AACE,YAAM,IAAI,mBAAmB;AAAA,QAC3B,SAAS,8CAA8C,OAAO,KAAK,IAAI,CAAC;AAAA,MAC1E,CAAC;AAAA,EACL;AACF;AAEO,IAAM,eAAe,CAAC,MAAY,eAAiC;AACxE,QAAM,QAAQ,SAAS,IAAI;AAE3B,MAAI,eAAe,QAAS,QAAO;AACnC,MAAI,eAAe,SAAU,QAAO,QAAQ;AAC5C,MAAI,eAAe,QAAS,QAAO,QAAQ;AAC3C,SAAO,QAAQ;AACjB;AAEO,IAAM,kBAAkB,CAAC,MAAY,eAA+B;AACzE,SAAO;AAAA,IACL,QAAQ,aAAa,MAAM,UAAU;AAAA,IACrC,UAAU,KAAK;AAAA,IACf,MAAM;AAAA,EACR;AACF;","names":["name","marker","symbol","name","marker","symbol","name","marker","symbol"]}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-billing/core",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.8",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/narevai/ai-billing.git",
|
|
8
|
+
"url": "git+https://github.com/narevai/ai-billing.git",
|
|
9
9
|
"directory": "packages/core"
|
|
10
10
|
},
|
|
11
11
|
"main": "./dist/index.js",
|