@scrawn/core 0.0.3 → 0.0.7
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/config.d.ts +41 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +32 -0
- package/dist/config.js.map +1 -0
- package/dist/core/auth/apiKeyAuth.d.ts +58 -0
- package/dist/core/auth/apiKeyAuth.d.ts.map +1 -0
- package/dist/core/auth/apiKeyAuth.js +66 -0
- package/dist/core/auth/apiKeyAuth.js.map +1 -0
- package/dist/core/auth/baseAuth.d.ts +70 -0
- package/dist/core/auth/baseAuth.d.ts.map +1 -0
- package/dist/core/auth/baseAuth.js +22 -0
- package/dist/core/auth/baseAuth.js.map +1 -0
- package/dist/core/errors/index.d.ts +192 -0
- package/dist/core/errors/index.d.ts.map +1 -0
- package/dist/core/errors/index.js +280 -0
- package/dist/core/errors/index.js.map +1 -0
- package/dist/core/grpc/callContext.d.ts +18 -0
- package/dist/core/grpc/callContext.d.ts.map +1 -0
- package/dist/core/grpc/callContext.js +35 -0
- package/dist/core/grpc/callContext.js.map +1 -0
- package/dist/core/grpc/client.d.ts +16 -0
- package/dist/core/grpc/client.d.ts.map +1 -0
- package/dist/core/grpc/client.js +30 -0
- package/dist/core/grpc/client.js.map +1 -0
- package/dist/core/grpc/index.d.ts +14 -0
- package/dist/core/grpc/index.d.ts.map +1 -0
- package/dist/core/grpc/index.js +13 -0
- package/dist/core/grpc/index.js.map +1 -0
- package/dist/core/grpc/requestBuilder.d.ts +15 -0
- package/dist/core/grpc/requestBuilder.d.ts.map +1 -0
- package/dist/core/grpc/requestBuilder.js +56 -0
- package/dist/core/grpc/requestBuilder.js.map +1 -0
- package/dist/core/grpc/streamRequestBuilder.d.ts +13 -0
- package/dist/core/grpc/streamRequestBuilder.d.ts.map +1 -0
- package/dist/core/grpc/streamRequestBuilder.js +60 -0
- package/dist/core/grpc/streamRequestBuilder.js.map +1 -0
- package/dist/core/grpc/types.d.ts +7 -0
- package/dist/core/grpc/types.d.ts.map +1 -0
- package/dist/core/grpc/types.js +2 -0
- package/dist/core/grpc/types.js.map +1 -0
- package/dist/core/pricing/builders.d.ts +157 -0
- package/dist/core/pricing/builders.d.ts.map +1 -0
- package/dist/core/pricing/builders.js +218 -0
- package/dist/core/pricing/builders.js.map +1 -0
- package/dist/core/pricing/index.d.ts +30 -0
- package/dist/core/pricing/index.d.ts.map +1 -0
- package/dist/core/pricing/index.js +32 -0
- package/dist/core/pricing/index.js.map +1 -0
- package/dist/core/pricing/resolve.d.ts +39 -0
- package/dist/core/pricing/resolve.d.ts.map +1 -0
- package/dist/core/pricing/resolve.js +50 -0
- package/dist/core/pricing/resolve.js.map +1 -0
- package/dist/core/pricing/serialize.d.ts +55 -0
- package/dist/core/pricing/serialize.d.ts.map +1 -0
- package/dist/core/pricing/serialize.js +127 -0
- package/dist/core/pricing/serialize.js.map +1 -0
- package/dist/core/pricing/types.d.ts +122 -0
- package/dist/core/pricing/types.d.ts.map +1 -0
- package/dist/core/pricing/types.js +17 -0
- package/dist/core/pricing/types.js.map +1 -0
- package/dist/core/pricing/validate.d.ts +56 -0
- package/dist/core/pricing/validate.d.ts.map +1 -0
- package/dist/core/pricing/validate.js +162 -0
- package/dist/core/pricing/validate.js.map +1 -0
- package/dist/core/scrawn.d.ts +414 -0
- package/dist/core/scrawn.d.ts.map +1 -0
- package/dist/core/scrawn.js +822 -0
- package/dist/core/scrawn.js.map +1 -0
- package/dist/core/types/auth.d.ts +31 -0
- package/dist/core/types/auth.d.ts.map +1 -0
- package/dist/core/types/auth.js +2 -0
- package/dist/core/types/auth.js.map +1 -0
- package/dist/core/types/event.d.ts +320 -0
- package/dist/core/types/event.d.ts.map +1 -0
- package/dist/core/types/event.js +155 -0
- package/dist/core/types/event.js.map +1 -0
- package/dist/gen/auth/v1/auth_grpc_pb.d.ts +3 -0
- package/dist/gen/auth/v1/auth_pb.d.ts +65 -0
- package/dist/gen/data/v1/data_grpc_pb.d.ts +5 -0
- package/dist/gen/data/v1/data_pb.d.ts +254 -0
- package/dist/gen/event/v1/event_grpc_pb.d.ts +3 -0
- package/dist/gen/event/v1/event_pb.d.ts +342 -0
- package/dist/gen/event/v1/event_pb.js +573 -117
- package/dist/gen/payment/v1/payment_grpc_pb.d.ts +3 -0
- package/dist/gen/payment/v1/payment_pb.d.ts +45 -0
- package/dist/gen/query/v1/query_grpc_pb.d.ts +5 -0
- package/dist/gen/query/v1/query_pb.d.ts +381 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/forkAsyncIterable.d.ts +13 -0
- package/dist/utils/forkAsyncIterable.d.ts.map +1 -0
- package/dist/utils/forkAsyncIterable.js +78 -0
- package/dist/utils/forkAsyncIterable.js.map +1 -0
- package/dist/utils/logger.d.ts +10 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +62 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/pathMatcher.d.ts +25 -0
- package/dist/utils/pathMatcher.d.ts.map +1 -0
- package/dist/utils/pathMatcher.js +46 -0
- package/dist/utils/pathMatcher.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pricing DSL Builder Functions
|
|
3
|
+
*
|
|
4
|
+
* This module provides the fluent builder functions for constructing
|
|
5
|
+
* type-safe pricing expressions. These functions perform light validation
|
|
6
|
+
* and build the AST that gets serialized to a string for the backend.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { add, mul, tag } from '@scrawn/core';
|
|
11
|
+
*
|
|
12
|
+
* // (PREMIUM_CALL * 3) + EXTRA_FEE + 250 cents
|
|
13
|
+
* const expr = add(mul(tag('PREMIUM_CALL'), 3), tag('EXTRA_FEE'), 250);
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
import type { AmountExpr, TagExpr, OpExpr, InputTokensExpr, OutputTokensExpr, ExprInput, ExprRef } from "./types.js";
|
|
17
|
+
/**
|
|
18
|
+
* Create a tag reference expression.
|
|
19
|
+
* Tags are resolved to their cent values by the backend.
|
|
20
|
+
*
|
|
21
|
+
* @param name - The name of the price tag (must be ALL CAPS with underscores only, e.g., PREMIUM_CALL, FEE)
|
|
22
|
+
* @returns A TagExpr referencing the named tag
|
|
23
|
+
* @throws Error if name is empty, whitespace-only, or not ALL_CAPS format
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const premiumTag = tag('PREMIUM_CALL');
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function tag<T extends string>(name: T): TagExpr<T>;
|
|
31
|
+
/**
|
|
32
|
+
* Create an addition expression.
|
|
33
|
+
* Adds all arguments together: arg1 + arg2 + arg3 + ...
|
|
34
|
+
*
|
|
35
|
+
* @param args - Two or more expressions or numbers (cents) to add
|
|
36
|
+
* @returns An OpExpr representing the sum
|
|
37
|
+
* @throws Error if fewer than 2 arguments provided
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* // 100 + 200 + tag('BONUS')
|
|
42
|
+
* const sum = add(100, 200, tag('BONUS'));
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function add<T extends string = string>(...args: ExprInput<T>[]): OpExpr<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Create a subtraction expression.
|
|
48
|
+
* Subtracts subsequent arguments from the first: arg1 - arg2 - arg3 - ...
|
|
49
|
+
*
|
|
50
|
+
* @param args - Two or more expressions or numbers (cents) to subtract
|
|
51
|
+
* @returns An OpExpr representing the difference
|
|
52
|
+
* @throws Error if fewer than 2 arguments provided
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // tag('TOTAL') - 50
|
|
57
|
+
* const diff = sub(tag('TOTAL'), 50);
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function sub<T extends string = string>(...args: ExprInput<T>[]): OpExpr<T>;
|
|
61
|
+
/**
|
|
62
|
+
* Create a multiplication expression.
|
|
63
|
+
* Multiplies all arguments together: arg1 * arg2 * arg3 * ...
|
|
64
|
+
*
|
|
65
|
+
* @param args - Two or more expressions or numbers (cents) to multiply
|
|
66
|
+
* @returns An OpExpr representing the product
|
|
67
|
+
* @throws Error if fewer than 2 arguments provided
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* // tag('PER_TOKEN') * 100
|
|
72
|
+
* const product = mul(tag('PER_TOKEN'), 100);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function mul<T extends string = string>(...args: ExprInput<T>[]): OpExpr<T>;
|
|
76
|
+
/**
|
|
77
|
+
* Create a division expression.
|
|
78
|
+
* Divides the first argument by subsequent arguments: arg1 / arg2 / arg3 / ...
|
|
79
|
+
*
|
|
80
|
+
* Note: The backend performs integer division. Results are truncated, not rounded.
|
|
81
|
+
*
|
|
82
|
+
* @param args - Two or more expressions or numbers (cents) to divide
|
|
83
|
+
* @returns An OpExpr representing the quotient
|
|
84
|
+
* @throws Error if fewer than 2 arguments provided
|
|
85
|
+
* @throws Error if any literal divisor is 0 (detected at build time)
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* // tag('TOTAL') / 2
|
|
90
|
+
* const half = div(tag('TOTAL'), 2);
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare function div<T extends string = string>(...args: ExprInput<T>[]): OpExpr<T>;
|
|
94
|
+
/**
|
|
95
|
+
* Create an amount expression from a number of cents.
|
|
96
|
+
* This is useful when you need to explicitly create an AmountExpr
|
|
97
|
+
* rather than relying on auto-conversion.
|
|
98
|
+
*
|
|
99
|
+
* @param cents - The amount in cents (must be an integer)
|
|
100
|
+
* @returns An AmountExpr representing the amount
|
|
101
|
+
* @throws Error if cents is not a finite integer
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* const fee = amount(250); // 250 cents = $2.50
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export declare function amount(cents: number): AmountExpr;
|
|
109
|
+
/**
|
|
110
|
+
* Create an inputTokens placeholder expression.
|
|
111
|
+
* Represents the actual inputTokens value from an AI token usage payload.
|
|
112
|
+
*
|
|
113
|
+
* Only valid in expressions used with aiTokenStreamConsumer.
|
|
114
|
+
* The placeholder is resolved SDK-side to an AmountExpr before serialization.
|
|
115
|
+
*
|
|
116
|
+
* @returns An InputTokensExpr placeholder
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* // Per-token pricing: INPUT_RATE * actual input tokens
|
|
121
|
+
* const inputExpr = mul(tag('INPUT_RATE'), inputTokens());
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export declare function inputTokens(): InputTokensExpr;
|
|
125
|
+
/**
|
|
126
|
+
* Create an outputTokens placeholder expression.
|
|
127
|
+
* Represents the actual outputTokens value from an AI token usage payload.
|
|
128
|
+
*
|
|
129
|
+
* Only valid in expressions used with aiTokenStreamConsumer.
|
|
130
|
+
* The placeholder is resolved SDK-side to an AmountExpr before serialization.
|
|
131
|
+
*
|
|
132
|
+
* @returns An OutputTokensExpr placeholder
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* // Per-token pricing: OUTPUT_RATE * actual output tokens
|
|
137
|
+
* const outputExpr = mul(tag('OUTPUT_RATE'), outputTokens());
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export declare function outputTokens(): OutputTokensExpr;
|
|
141
|
+
/**
|
|
142
|
+
* Create a reference to a persisted expression stored in the backend.
|
|
143
|
+
* Expression names must be ALL CAPS with underscores (e.g., MY_EXPR).
|
|
144
|
+
*
|
|
145
|
+
* @param name - The name of the persisted expression
|
|
146
|
+
* @returns An ExprRef referencing the named expression
|
|
147
|
+
* @throws Error if name is empty or invalid format
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* const expr = biller.expr("MY_EXPR");
|
|
152
|
+
* // or standalone:
|
|
153
|
+
* const expr = expr("MY_EXPR");
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
export declare function expr(name: string): ExprRef;
|
|
157
|
+
//# sourceMappingURL=builders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builders.d.ts","sourceRoot":"","sources":["../../../src/core/pricing/builders.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EACV,UAAU,EACV,OAAO,EACP,MAAM,EACN,eAAe,EACf,gBAAgB,EAEhB,SAAS,EACT,OAAO,EAER,MAAM,YAAY,CAAC;AAiBpB;;;;;;;;;;;;GAYG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAIzD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAQjF;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAQjF;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAQjF;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAQjF;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAIhD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,WAAW,IAAI,eAAe,CAE7C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,IAAI,gBAAgB,CAE/C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAI1C"}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pricing DSL Builder Functions
|
|
3
|
+
*
|
|
4
|
+
* This module provides the fluent builder functions for constructing
|
|
5
|
+
* type-safe pricing expressions. These functions perform light validation
|
|
6
|
+
* and build the AST that gets serialized to a string for the backend.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { add, mul, tag } from '@scrawn/core';
|
|
11
|
+
*
|
|
12
|
+
* // (PREMIUM_CALL * 3) + EXTRA_FEE + 250 cents
|
|
13
|
+
* const expr = add(mul(tag('PREMIUM_CALL'), 3), tag('EXTRA_FEE'), 250);
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
import { validateExpr } from "./validate.js";
|
|
17
|
+
/**
|
|
18
|
+
* Convert an ExprInput (PriceExpr or number) to a PriceExpr.
|
|
19
|
+
* Numbers are wrapped as AmountExpr (cents).
|
|
20
|
+
*/
|
|
21
|
+
function toExpr(input) {
|
|
22
|
+
if (typeof input === "number") {
|
|
23
|
+
return { kind: "amount", value: input };
|
|
24
|
+
}
|
|
25
|
+
if ("_expr" in input) {
|
|
26
|
+
return input._expr;
|
|
27
|
+
}
|
|
28
|
+
return input;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Create a tag reference expression.
|
|
32
|
+
* Tags are resolved to their cent values by the backend.
|
|
33
|
+
*
|
|
34
|
+
* @param name - The name of the price tag (must be ALL CAPS with underscores only, e.g., PREMIUM_CALL, FEE)
|
|
35
|
+
* @returns A TagExpr referencing the named tag
|
|
36
|
+
* @throws Error if name is empty, whitespace-only, or not ALL_CAPS format
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const premiumTag = tag('PREMIUM_CALL');
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export function tag(name) {
|
|
44
|
+
const expr = { kind: "tag", name };
|
|
45
|
+
validateExpr(expr); // Will throw if invalid
|
|
46
|
+
return expr;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Create an addition expression.
|
|
50
|
+
* Adds all arguments together: arg1 + arg2 + arg3 + ...
|
|
51
|
+
*
|
|
52
|
+
* @param args - Two or more expressions or numbers (cents) to add
|
|
53
|
+
* @returns An OpExpr representing the sum
|
|
54
|
+
* @throws Error if fewer than 2 arguments provided
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* // 100 + 200 + tag('BONUS')
|
|
59
|
+
* const sum = add(100, 200, tag('BONUS'));
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function add(...args) {
|
|
63
|
+
const expr = {
|
|
64
|
+
kind: "op",
|
|
65
|
+
op: "ADD",
|
|
66
|
+
args: args.map(toExpr),
|
|
67
|
+
};
|
|
68
|
+
validateExpr(expr);
|
|
69
|
+
return expr;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Create a subtraction expression.
|
|
73
|
+
* Subtracts subsequent arguments from the first: arg1 - arg2 - arg3 - ...
|
|
74
|
+
*
|
|
75
|
+
* @param args - Two or more expressions or numbers (cents) to subtract
|
|
76
|
+
* @returns An OpExpr representing the difference
|
|
77
|
+
* @throws Error if fewer than 2 arguments provided
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* // tag('TOTAL') - 50
|
|
82
|
+
* const diff = sub(tag('TOTAL'), 50);
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export function sub(...args) {
|
|
86
|
+
const expr = {
|
|
87
|
+
kind: "op",
|
|
88
|
+
op: "SUB",
|
|
89
|
+
args: args.map(toExpr),
|
|
90
|
+
};
|
|
91
|
+
validateExpr(expr);
|
|
92
|
+
return expr;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Create a multiplication expression.
|
|
96
|
+
* Multiplies all arguments together: arg1 * arg2 * arg3 * ...
|
|
97
|
+
*
|
|
98
|
+
* @param args - Two or more expressions or numbers (cents) to multiply
|
|
99
|
+
* @returns An OpExpr representing the product
|
|
100
|
+
* @throws Error if fewer than 2 arguments provided
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* // tag('PER_TOKEN') * 100
|
|
105
|
+
* const product = mul(tag('PER_TOKEN'), 100);
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export function mul(...args) {
|
|
109
|
+
const expr = {
|
|
110
|
+
kind: "op",
|
|
111
|
+
op: "MUL",
|
|
112
|
+
args: args.map(toExpr),
|
|
113
|
+
};
|
|
114
|
+
validateExpr(expr);
|
|
115
|
+
return expr;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Create a division expression.
|
|
119
|
+
* Divides the first argument by subsequent arguments: arg1 / arg2 / arg3 / ...
|
|
120
|
+
*
|
|
121
|
+
* Note: The backend performs integer division. Results are truncated, not rounded.
|
|
122
|
+
*
|
|
123
|
+
* @param args - Two or more expressions or numbers (cents) to divide
|
|
124
|
+
* @returns An OpExpr representing the quotient
|
|
125
|
+
* @throws Error if fewer than 2 arguments provided
|
|
126
|
+
* @throws Error if any literal divisor is 0 (detected at build time)
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* // tag('TOTAL') / 2
|
|
131
|
+
* const half = div(tag('TOTAL'), 2);
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export function div(...args) {
|
|
135
|
+
const expr = {
|
|
136
|
+
kind: "op",
|
|
137
|
+
op: "DIV",
|
|
138
|
+
args: args.map(toExpr),
|
|
139
|
+
};
|
|
140
|
+
validateExpr(expr);
|
|
141
|
+
return expr;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Create an amount expression from a number of cents.
|
|
145
|
+
* This is useful when you need to explicitly create an AmountExpr
|
|
146
|
+
* rather than relying on auto-conversion.
|
|
147
|
+
*
|
|
148
|
+
* @param cents - The amount in cents (must be an integer)
|
|
149
|
+
* @returns An AmountExpr representing the amount
|
|
150
|
+
* @throws Error if cents is not a finite integer
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* const fee = amount(250); // 250 cents = $2.50
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
export function amount(cents) {
|
|
158
|
+
const expr = { kind: "amount", value: cents };
|
|
159
|
+
validateExpr(expr);
|
|
160
|
+
return expr;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Create an inputTokens placeholder expression.
|
|
164
|
+
* Represents the actual inputTokens value from an AI token usage payload.
|
|
165
|
+
*
|
|
166
|
+
* Only valid in expressions used with aiTokenStreamConsumer.
|
|
167
|
+
* The placeholder is resolved SDK-side to an AmountExpr before serialization.
|
|
168
|
+
*
|
|
169
|
+
* @returns An InputTokensExpr placeholder
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* // Per-token pricing: INPUT_RATE * actual input tokens
|
|
174
|
+
* const inputExpr = mul(tag('INPUT_RATE'), inputTokens());
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
export function inputTokens() {
|
|
178
|
+
return { kind: "inputTokens" };
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Create an outputTokens placeholder expression.
|
|
182
|
+
* Represents the actual outputTokens value from an AI token usage payload.
|
|
183
|
+
*
|
|
184
|
+
* Only valid in expressions used with aiTokenStreamConsumer.
|
|
185
|
+
* The placeholder is resolved SDK-side to an AmountExpr before serialization.
|
|
186
|
+
*
|
|
187
|
+
* @returns An OutputTokensExpr placeholder
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* // Per-token pricing: OUTPUT_RATE * actual output tokens
|
|
192
|
+
* const outputExpr = mul(tag('OUTPUT_RATE'), outputTokens());
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
export function outputTokens() {
|
|
196
|
+
return { kind: "outputTokens" };
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Create a reference to a persisted expression stored in the backend.
|
|
200
|
+
* Expression names must be ALL CAPS with underscores (e.g., MY_EXPR).
|
|
201
|
+
*
|
|
202
|
+
* @param name - The name of the persisted expression
|
|
203
|
+
* @returns An ExprRef referencing the named expression
|
|
204
|
+
* @throws Error if name is empty or invalid format
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```typescript
|
|
208
|
+
* const expr = biller.expr("MY_EXPR");
|
|
209
|
+
* // or standalone:
|
|
210
|
+
* const expr = expr("MY_EXPR");
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
export function expr(name) {
|
|
214
|
+
const exprRef = { kind: "exprRef", name };
|
|
215
|
+
validateExpr(exprRef);
|
|
216
|
+
return exprRef;
|
|
217
|
+
}
|
|
218
|
+
//# sourceMappingURL=builders.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builders.js","sourceRoot":"","sources":["../../../src/core/pricing/builders.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pricing DSL Module
|
|
3
|
+
*
|
|
4
|
+
* Provides a type-safe DSL for building pricing expressions that combine
|
|
5
|
+
* literal amounts, named price tags, and arithmetic operations.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { add, mul, tag, serializeExpr } from '@scrawn/core';
|
|
10
|
+
*
|
|
11
|
+
* // Build expression: (PREMIUM_CALL * 3) + EXTRA_FEE + 250 cents
|
|
12
|
+
* const expr = add(mul(tag('PREMIUM_CALL'), 3), tag('EXTRA_FEE'), 250);
|
|
13
|
+
*
|
|
14
|
+
* // Serialize for backend: "add(mul(tag('PREMIUM_CALL'),3),tag('EXTRA_FEE'),250)"
|
|
15
|
+
* const exprString = serializeExpr(expr);
|
|
16
|
+
*
|
|
17
|
+
* // Use in event payload
|
|
18
|
+
* await scrawn.basicUsageEventConsumer({
|
|
19
|
+
* userId: 'u123',
|
|
20
|
+
* debitExpr: expr
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export type { OpType, AmountExpr, TagExpr, OpExpr, InputTokensExpr, OutputTokensExpr, ExprRef, ScrawnExpr, PriceExpr, ExprInput, } from "./types.js";
|
|
25
|
+
export { tag, add, sub, mul, div, amount, inputTokens, outputTokens, expr } from "./builders.js";
|
|
26
|
+
export { serializeExpr, prettyPrintExpr } from "./serialize.js";
|
|
27
|
+
export { validateExpr, isValidExpr, containsTokenExpr, PricingExpressionError, } from "./validate.js";
|
|
28
|
+
export { resolveTokens } from "./resolve.js";
|
|
29
|
+
export type { TokenContext } from "./resolve.js";
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/pricing/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,YAAY,EACV,MAAM,EACN,UAAU,EACV,OAAO,EACP,MAAM,EACN,eAAe,EACf,gBAAgB,EAChB,OAAO,EACP,UAAU,EACV,SAAS,EACT,SAAS,GACV,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,WAAW,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAGjG,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAGhE,OAAO,EACL,YAAY,EACZ,WAAW,EACX,iBAAiB,EACjB,sBAAsB,GACvB,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pricing DSL Module
|
|
3
|
+
*
|
|
4
|
+
* Provides a type-safe DSL for building pricing expressions that combine
|
|
5
|
+
* literal amounts, named price tags, and arithmetic operations.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { add, mul, tag, serializeExpr } from '@scrawn/core';
|
|
10
|
+
*
|
|
11
|
+
* // Build expression: (PREMIUM_CALL * 3) + EXTRA_FEE + 250 cents
|
|
12
|
+
* const expr = add(mul(tag('PREMIUM_CALL'), 3), tag('EXTRA_FEE'), 250);
|
|
13
|
+
*
|
|
14
|
+
* // Serialize for backend: "add(mul(tag('PREMIUM_CALL'),3),tag('EXTRA_FEE'),250)"
|
|
15
|
+
* const exprString = serializeExpr(expr);
|
|
16
|
+
*
|
|
17
|
+
* // Use in event payload
|
|
18
|
+
* await scrawn.basicUsageEventConsumer({
|
|
19
|
+
* userId: 'u123',
|
|
20
|
+
* debitExpr: expr
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
// Export builder functions
|
|
25
|
+
export { tag, add, sub, mul, div, amount, inputTokens, outputTokens, expr } from "./builders.js";
|
|
26
|
+
// Export serialization
|
|
27
|
+
export { serializeExpr, prettyPrintExpr } from "./serialize.js";
|
|
28
|
+
// Export validation
|
|
29
|
+
export { validateExpr, isValidExpr, containsTokenExpr, PricingExpressionError, } from "./validate.js";
|
|
30
|
+
// Export token resolution
|
|
31
|
+
export { resolveTokens } from "./resolve.js";
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/pricing/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAgBH,2BAA2B;AAC3B,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,WAAW,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAEjG,uBAAuB;AACvB,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEhE,oBAAoB;AACpB,OAAO,EACL,YAAY,EACZ,WAAW,EACX,iBAAiB,EACjB,sBAAsB,GACvB,MAAM,eAAe,CAAC;AAEvB,0BAA0B;AAC1B,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token Placeholder Resolution
|
|
3
|
+
*
|
|
4
|
+
* This module provides the resolveTokens() function that replaces
|
|
5
|
+
* inputTokens() / outputTokens() placeholder nodes in a pricing expression
|
|
6
|
+
* with concrete AmountExpr values from an AI token usage payload.
|
|
7
|
+
*
|
|
8
|
+
* Resolution happens SDK-side before serialization, so the backend
|
|
9
|
+
* only sees normal expressions with concrete numbers.
|
|
10
|
+
*/
|
|
11
|
+
import type { PriceExpr } from "./types.js";
|
|
12
|
+
/**
|
|
13
|
+
* Context for resolving token placeholders.
|
|
14
|
+
* Contains the actual token counts from an AI token usage payload.
|
|
15
|
+
*/
|
|
16
|
+
export interface TokenContext {
|
|
17
|
+
readonly inputTokens: number;
|
|
18
|
+
readonly outputTokens: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Deeply traverse a PriceExpr tree and replace InputTokensExpr / OutputTokensExpr
|
|
22
|
+
* placeholder nodes with AmountExpr nodes using actual values from the context.
|
|
23
|
+
*
|
|
24
|
+
* Returns a new expression tree with all token placeholders resolved.
|
|
25
|
+
* The original tree is not mutated.
|
|
26
|
+
*
|
|
27
|
+
* @param expr - The expression tree (may contain token placeholders)
|
|
28
|
+
* @param context - The actual inputTokens/outputTokens values
|
|
29
|
+
* @returns A new PriceExpr tree with all token placeholders replaced by AmountExpr nodes
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const expr = mul(tag('INPUT_RATE'), inputTokens());
|
|
34
|
+
* const resolved = resolveTokens(expr, { inputTokens: 150, outputTokens: 50 });
|
|
35
|
+
* // resolved is: mul(tag('INPUT_RATE'), 150)
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function resolveTokens(expr: PriceExpr<string>, context: TokenContext): PriceExpr<string>;
|
|
39
|
+
//# sourceMappingURL=resolve.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../../../src/core/pricing/resolve.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAc,MAAM,YAAY,CAAC;AAExD;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CAC/B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,YAAY,GAAG,SAAS,CAAC,MAAM,CAAC,CAoB/F"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token Placeholder Resolution
|
|
3
|
+
*
|
|
4
|
+
* This module provides the resolveTokens() function that replaces
|
|
5
|
+
* inputTokens() / outputTokens() placeholder nodes in a pricing expression
|
|
6
|
+
* with concrete AmountExpr values from an AI token usage payload.
|
|
7
|
+
*
|
|
8
|
+
* Resolution happens SDK-side before serialization, so the backend
|
|
9
|
+
* only sees normal expressions with concrete numbers.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Deeply traverse a PriceExpr tree and replace InputTokensExpr / OutputTokensExpr
|
|
13
|
+
* placeholder nodes with AmountExpr nodes using actual values from the context.
|
|
14
|
+
*
|
|
15
|
+
* Returns a new expression tree with all token placeholders resolved.
|
|
16
|
+
* The original tree is not mutated.
|
|
17
|
+
*
|
|
18
|
+
* @param expr - The expression tree (may contain token placeholders)
|
|
19
|
+
* @param context - The actual inputTokens/outputTokens values
|
|
20
|
+
* @returns A new PriceExpr tree with all token placeholders replaced by AmountExpr nodes
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const expr = mul(tag('INPUT_RATE'), inputTokens());
|
|
25
|
+
* const resolved = resolveTokens(expr, { inputTokens: 150, outputTokens: 50 });
|
|
26
|
+
* // resolved is: mul(tag('INPUT_RATE'), 150)
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function resolveTokens(expr, context) {
|
|
30
|
+
switch (expr.kind) {
|
|
31
|
+
case "inputTokens": {
|
|
32
|
+
const resolved = { kind: "amount", value: context.inputTokens };
|
|
33
|
+
return resolved;
|
|
34
|
+
}
|
|
35
|
+
case "outputTokens": {
|
|
36
|
+
const resolved = { kind: "amount", value: context.outputTokens };
|
|
37
|
+
return resolved;
|
|
38
|
+
}
|
|
39
|
+
case "op": {
|
|
40
|
+
const resolvedArgs = expr.args.map((arg) => resolveTokens(arg, context));
|
|
41
|
+
return { kind: "op", op: expr.op, args: resolvedArgs };
|
|
42
|
+
}
|
|
43
|
+
case "amount":
|
|
44
|
+
case "tag":
|
|
45
|
+
case "exprRef":
|
|
46
|
+
// These nodes don't contain token placeholders — return as-is
|
|
47
|
+
return expr;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=resolve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve.js","sourceRoot":"","sources":["../../../src/core/pricing/resolve.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAaH;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,aAAa,CAAC,IAAuB,EAAE,OAAqB;IAC1E,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;QAClB,KAAK,aAAa,CAAC,CAAC,CAAC;YACnB,MAAM,QAAQ,GAAe,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,CAAC,WAAW,EAAE,CAAC;YAC5E,OAAO,QAAQ,CAAC;QAClB,CAAC;QACD,KAAK,cAAc,CAAC,CAAC,CAAC;YACpB,MAAM,QAAQ,GAAe,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,CAAC,YAAY,EAAE,CAAC;YAC7E,OAAO,QAAQ,CAAC;QAClB,CAAC;QACD,KAAK,IAAI,CAAC,CAAC,CAAC;YACV,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,aAAa,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;YACzE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,YAAY,EAAE,CAAC;QACzD,CAAC;QACD,KAAK,QAAQ,CAAC;QACd,KAAK,KAAK,CAAC;QACX,KAAK,SAAS;YACZ,8DAA8D;YAC9D,OAAO,IAAI,CAAC;IAChB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pricing DSL Serialization
|
|
3
|
+
*
|
|
4
|
+
* This module converts typed pricing expression ASTs into string format
|
|
5
|
+
* that the backend can parse and evaluate.
|
|
6
|
+
*
|
|
7
|
+
* Output format examples:
|
|
8
|
+
* - Amount: 250
|
|
9
|
+
* - Tag: tag(PREMIUM_CALL)
|
|
10
|
+
* - Addition: add(100,tag(FEE),250)
|
|
11
|
+
* - Complex: add(mul(tag(PREMIUM_CALL),3),tag(EXTRA_FEE),250)
|
|
12
|
+
*
|
|
13
|
+
* The format is designed to be:
|
|
14
|
+
* - Unambiguous (parseable by the backend)
|
|
15
|
+
* - Human-readable (for debugging)
|
|
16
|
+
* - Compact (minimal whitespace, no quotes around tag names)
|
|
17
|
+
*/
|
|
18
|
+
import type { PriceExpr } from "./types.js";
|
|
19
|
+
/**
|
|
20
|
+
* Serialize a pricing expression to a string.
|
|
21
|
+
*
|
|
22
|
+
* @param expr - The expression to serialize
|
|
23
|
+
* @returns A string representation that the backend can parse
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const expr = add(mul(tag('PREMIUM'), 3), 100);
|
|
28
|
+
* const str = serializeExpr(expr);
|
|
29
|
+
* // "add(mul(tag(PREMIUM),3),100)"
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare function serializeExpr(expr: PriceExpr<string>): string;
|
|
33
|
+
/**
|
|
34
|
+
* Pretty-print a pricing expression with indentation.
|
|
35
|
+
* Useful for debugging and logging.
|
|
36
|
+
*
|
|
37
|
+
* @param expr - The expression to format
|
|
38
|
+
* @param indent - Number of spaces for indentation (default 2)
|
|
39
|
+
* @returns A formatted, multi-line string representation
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const expr = add(mul(tag('PREMIUM'), 3), 100);
|
|
44
|
+
* console.log(prettyPrintExpr(expr));
|
|
45
|
+
* // add(
|
|
46
|
+
* // mul(
|
|
47
|
+
* // tag(PREMIUM),
|
|
48
|
+
* // 3
|
|
49
|
+
* // ),
|
|
50
|
+
* // 100
|
|
51
|
+
* // )
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function prettyPrintExpr(expr: PriceExpr<string>, indent?: number): string;
|
|
55
|
+
//# sourceMappingURL=serialize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../../../src/core/pricing/serialize.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,SAAS,EAA+B,MAAM,YAAY,CAAC;AAEzE;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,MAAM,CAuB7D;AA6BD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,GAAE,MAAU,GAAG,MAAM,CAEnF"}
|