@lanlinling/lab-billing 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # @lanlinling/lab-billing
2
+
3
+ The TypeScript client for the shared Lab billing contract. It contains no provider price table and no secrets. Cost and sales-policy facts live in the shared Supabase platform schema.
4
+
5
+ ```ts
6
+ const quote = await quotePlatformCharge(service, {
7
+ experimentId: "english_testing",
8
+ action: "tts",
9
+ provider: "volcengine",
10
+ model: "doubao-tts-2",
11
+ usage: { unit: "char", chars: text.length },
12
+ });
13
+ ```
14
+
15
+ The caller passes `quote.costCny` and `quote.sellCny` to the server-only `debit_wallet` RPC after choosing the product-appropriate charge point.
@@ -0,0 +1,44 @@
1
+ type BillableUsage = {
2
+ unit: "token";
3
+ inputTokens: number;
4
+ outputTokens: number;
5
+ } | {
6
+ unit: "char";
7
+ chars: number;
8
+ } | {
9
+ unit: "count";
10
+ count: number;
11
+ };
12
+ type RpcResult<T> = {
13
+ data: T | null;
14
+ error: {
15
+ message: string;
16
+ } | null;
17
+ };
18
+ interface BillingRpcClient {
19
+ rpc<T>(name: string, args: Record<string, unknown>): PromiseLike<RpcResult<T>>;
20
+ }
21
+ declare function usageForRpc(usage: BillableUsage): Record<string, number | string>;
22
+ declare function quotePlatformCost(service: BillingRpcClient, params: {
23
+ provider: string;
24
+ model: string;
25
+ usage: BillableUsage;
26
+ }): Promise<number>;
27
+ declare function quotePlatformSell(service: BillingRpcClient, params: {
28
+ experimentId: string;
29
+ action: string;
30
+ costCny: number;
31
+ }): Promise<number>;
32
+ declare function quotePlatformCharge(service: BillingRpcClient, params: {
33
+ experimentId: string;
34
+ action: string;
35
+ provider: string;
36
+ model: string;
37
+ usage: BillableUsage;
38
+ }): Promise<{
39
+ costCny: number;
40
+ sellCny: number;
41
+ usage: Record<string, string | number>;
42
+ }>;
43
+
44
+ export { type BillableUsage, type BillingRpcClient, type RpcResult, quotePlatformCharge, quotePlatformCost, quotePlatformSell, usageForRpc };
package/dist/index.js ADDED
@@ -0,0 +1,44 @@
1
+ // src/index.ts
2
+ function usageForRpc(usage) {
3
+ switch (usage.unit) {
4
+ case "token":
5
+ return { unit: usage.unit, input_tokens: usage.inputTokens, output_tokens: usage.outputTokens };
6
+ case "char":
7
+ return { unit: usage.unit, chars: usage.chars };
8
+ case "count":
9
+ return { unit: usage.unit, count: usage.count };
10
+ }
11
+ }
12
+ async function quotePlatformCost(service, params) {
13
+ const { data, error } = await service.rpc("quote_platform_cost_cny", {
14
+ p_provider: params.provider,
15
+ p_model: params.model,
16
+ p_usage: usageForRpc(params.usage)
17
+ });
18
+ if (error || data === null) throw new Error(`platform_cost_quote_failed:${(error == null ? void 0 : error.message) ?? "unknown"}`);
19
+ return Number(data);
20
+ }
21
+ async function quotePlatformSell(service, params) {
22
+ const { data, error } = await service.rpc("quote_platform_sell_cny", {
23
+ p_experiment: params.experimentId,
24
+ p_action: params.action,
25
+ p_cost_cny: params.costCny
26
+ });
27
+ if (error || data === null) throw new Error(`platform_sell_quote_failed:${(error == null ? void 0 : error.message) ?? "unknown"}`);
28
+ return Number(data);
29
+ }
30
+ async function quotePlatformCharge(service, params) {
31
+ const costCny = await quotePlatformCost(service, params);
32
+ const sellCny = await quotePlatformSell(service, {
33
+ experimentId: params.experimentId,
34
+ action: params.action,
35
+ costCny
36
+ });
37
+ return { costCny, sellCny, usage: usageForRpc(params.usage) };
38
+ }
39
+ export {
40
+ quotePlatformCharge,
41
+ quotePlatformCost,
42
+ quotePlatformSell,
43
+ usageForRpc
44
+ };
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@lanlinling/lab-billing",
3
+ "version": "0.1.0",
4
+ "description": "Shared Lab billing quote contract for TypeScript products",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "files": ["dist", "README.md"],
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "scripts": {
16
+ "build": "tsup src/index.ts --format esm --dts --clean",
17
+ "test": "npm run build && node --test test/index.test.mjs",
18
+ "prepublishOnly": "npm run test"
19
+ },
20
+ "devDependencies": {
21
+ "tsup": "8.5.1",
22
+ "typescript": "5.9.3"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ }
27
+ }