@downcity/services 0.1.59 → 0.1.62
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +183 -0
- package/bin/balance/amount.d.ts +51 -0
- package/bin/balance/amount.d.ts.map +1 -0
- package/bin/balance/amount.js +101 -0
- package/bin/balance/amount.js.map +1 -0
- package/bin/balance/index.d.ts +2 -0
- package/bin/balance/index.d.ts.map +1 -1
- package/bin/balance/index.js +1 -0
- package/bin/balance/index.js.map +1 -1
- package/bin/balance/routes.d.ts.map +1 -1
- package/bin/balance/routes.js +1 -0
- package/bin/balance/routes.js.map +1 -1
- package/bin/balance/service.d.ts +13 -1
- package/bin/balance/service.d.ts.map +1 -1
- package/bin/balance/service.js +54 -25
- package/bin/balance/service.js.map +1 -1
- package/bin/balance/types.d.ts +44 -9
- package/bin/balance/types.d.ts.map +1 -1
- package/bin/balance/types.js +2 -1
- package/bin/balance/types.js.map +1 -1
- package/bin/balance/utils.d.ts.map +1 -1
- package/bin/balance/utils.js +17 -5
- package/bin/balance/utils.js.map +1 -1
- package/bin/billing/index.d.ts +7 -0
- package/bin/billing/index.d.ts.map +1 -0
- package/bin/billing/index.js +6 -0
- package/bin/billing/index.js.map +1 -0
- package/bin/billing/routes.d.ts +14 -0
- package/bin/billing/routes.d.ts.map +1 -0
- package/bin/billing/routes.js +61 -0
- package/bin/billing/routes.js.map +1 -0
- package/bin/billing/schema.d.ts +582 -0
- package/bin/billing/schema.d.ts.map +1 -0
- package/bin/billing/schema.js +81 -0
- package/bin/billing/schema.js.map +1 -0
- package/bin/billing/service.d.ts +619 -0
- package/bin/billing/service.d.ts.map +1 -0
- package/bin/billing/service.js +457 -0
- package/bin/billing/service.js.map +1 -0
- package/bin/billing/types.d.ts +205 -0
- package/bin/billing/types.d.ts.map +1 -0
- package/bin/billing/types.js +10 -0
- package/bin/billing/types.js.map +1 -0
- package/bin/index.d.ts +5 -1
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +3 -1
- package/bin/index.js.map +1 -1
- package/bin/payment/types.d.ts +5 -1
- package/bin/payment/types.d.ts.map +1 -1
- package/bin/payment-creem/creem.d.ts.map +1 -1
- package/bin/payment-creem/creem.js +15 -0
- package/bin/payment-creem/creem.js.map +1 -1
- package/bin/payment-dodo/dodo.d.ts.map +1 -1
- package/bin/payment-dodo/dodo.js +16 -1
- package/bin/payment-dodo/dodo.js.map +1 -1
- package/bin/payment-stripe/stripe.d.ts.map +1 -1
- package/bin/payment-stripe/stripe.js +14 -1
- package/bin/payment-stripe/stripe.js.map +1 -1
- package/bin/payment-waffo/waffo.d.ts.map +1 -1
- package/bin/payment-waffo/waffo.js +15 -0
- package/bin/payment-waffo/waffo.js.map +1 -1
- package/bin/types/Amount.d.ts +25 -0
- package/bin/types/Amount.d.ts.map +1 -0
- package/bin/types/Amount.js +17 -0
- package/bin/types/Amount.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Downcity 官方 Billing 服务实现。
|
|
3
|
+
*
|
|
4
|
+
* 设计边界:
|
|
5
|
+
* - usage 负责记录事实,billing 负责把事实换算成 charge
|
|
6
|
+
* - balance 负责真正的钱包扣减与流水
|
|
7
|
+
* - pricing rule 使用 microcredits,1 credit = 1 USD = 1_000_000 microcredits
|
|
8
|
+
*/
|
|
9
|
+
import { InstallableService, } from "@downcity/city";
|
|
10
|
+
import { rawAll, rawFirst, rawRun } from "../balance/raw.js";
|
|
11
|
+
import { microcreditsToCredits } from "../balance/amount.js";
|
|
12
|
+
import { billingCharges, billingPricingRules, CHARGE_TABLE, PRICING_RULE_TABLE } from "./schema.js";
|
|
13
|
+
import { registerBillingRoutes } from "./routes.js";
|
|
14
|
+
const TOKENS_PER_MTOKEN = 1_000_000;
|
|
15
|
+
const PRICING_RULE_COLUMNS = [
|
|
16
|
+
"rule_id",
|
|
17
|
+
"service_id",
|
|
18
|
+
"action_id",
|
|
19
|
+
"model_id",
|
|
20
|
+
"provider_id",
|
|
21
|
+
"request_microcredits",
|
|
22
|
+
"input_token_microcredits",
|
|
23
|
+
"input_mtoken_microcredits",
|
|
24
|
+
"output_token_microcredits",
|
|
25
|
+
"output_mtoken_microcredits",
|
|
26
|
+
"cached_token_microcredits",
|
|
27
|
+
"cached_mtoken_microcredits",
|
|
28
|
+
"image_microcredits",
|
|
29
|
+
"status",
|
|
30
|
+
"note",
|
|
31
|
+
"created_at",
|
|
32
|
+
"updated_at",
|
|
33
|
+
].join(", ");
|
|
34
|
+
const PRICING_RULE_MTOKEN_COLUMNS = [
|
|
35
|
+
"input_mtoken_microcredits",
|
|
36
|
+
"output_mtoken_microcredits",
|
|
37
|
+
"cached_mtoken_microcredits",
|
|
38
|
+
];
|
|
39
|
+
/**
|
|
40
|
+
* Billing 服务实例。
|
|
41
|
+
*/
|
|
42
|
+
export class BillingService extends InstallableService {
|
|
43
|
+
id = "billing";
|
|
44
|
+
name = "Billing";
|
|
45
|
+
version = "0.1.0";
|
|
46
|
+
schema = {
|
|
47
|
+
pricing_rules: billingPricingRules,
|
|
48
|
+
charges: billingCharges,
|
|
49
|
+
};
|
|
50
|
+
balance;
|
|
51
|
+
initialRules;
|
|
52
|
+
requireBeforeCall;
|
|
53
|
+
constructor(options) {
|
|
54
|
+
super();
|
|
55
|
+
this.balance = options.balance;
|
|
56
|
+
this.initialRules = options.pricing_rules ?? [];
|
|
57
|
+
this.requireBeforeCall = options.require_before_call !== false;
|
|
58
|
+
this.instruction = [
|
|
59
|
+
"根据 service 调用的标准 metering 计算扣费,并通过 balance 执行 microcredits 扣款。",
|
|
60
|
+
"pricing rule 使用 microcredits:1 credit = 1 USD = 1_000_000 microcredits。",
|
|
61
|
+
"Billing 不负责 provider 调用,也不替代 usage 事实层。",
|
|
62
|
+
].join("\n");
|
|
63
|
+
}
|
|
64
|
+
async _onInit() {
|
|
65
|
+
await super._onInit();
|
|
66
|
+
await this.ensurePricingRuleColumns();
|
|
67
|
+
for (const rule of this.initialRules) {
|
|
68
|
+
await this.upsertPricingRule(rule);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
install(ctx) {
|
|
72
|
+
registerBillingRoutes(this, ctx);
|
|
73
|
+
if (this.requireBeforeCall) {
|
|
74
|
+
ctx.hook.before(async (serviceCtx) => {
|
|
75
|
+
if (!shouldBill(serviceCtx))
|
|
76
|
+
return;
|
|
77
|
+
const rule = await this.resolvePricingRule(serviceCtx);
|
|
78
|
+
if (!rule)
|
|
79
|
+
return;
|
|
80
|
+
const amount_microcredits = calculateChargeAmount(serviceCtx, rule, { estimate: true });
|
|
81
|
+
if (amount_microcredits <= 0)
|
|
82
|
+
return;
|
|
83
|
+
await this.balance.requireMicrocredits(serviceCtx.user.user_id, amount_microcredits);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
ctx.hook.after(async (serviceCtx) => {
|
|
87
|
+
if (!shouldBill(serviceCtx))
|
|
88
|
+
return;
|
|
89
|
+
if (!isSuccessfulOutput(serviceCtx.output))
|
|
90
|
+
return;
|
|
91
|
+
await this.settle(serviceCtx);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 新增或更新 pricing rule。
|
|
96
|
+
*/
|
|
97
|
+
async upsertPricingRule(input) {
|
|
98
|
+
const now = new Date().toISOString();
|
|
99
|
+
const rule = normalizePricingRule(input, now);
|
|
100
|
+
const existing = await this.readPricingRule(rule.rule_id);
|
|
101
|
+
if (existing) {
|
|
102
|
+
await rawRun(this.resolveRaw(), [
|
|
103
|
+
`UPDATE ${PRICING_RULE_TABLE}`,
|
|
104
|
+
"SET service_id = ?, action_id = ?, model_id = ?, provider_id = ?, request_microcredits = ?, input_token_microcredits = ?, input_mtoken_microcredits = ?, output_token_microcredits = ?, output_mtoken_microcredits = ?, cached_token_microcredits = ?, cached_mtoken_microcredits = ?, image_microcredits = ?, status = ?, note = ?, updated_at = ?",
|
|
105
|
+
"WHERE rule_id = ?",
|
|
106
|
+
].join(" "), [
|
|
107
|
+
rule.service_id,
|
|
108
|
+
rule.action_id,
|
|
109
|
+
rule.model_id,
|
|
110
|
+
rule.provider_id,
|
|
111
|
+
rule.request_microcredits,
|
|
112
|
+
rule.input_token_microcredits,
|
|
113
|
+
rule.input_mtoken_microcredits,
|
|
114
|
+
rule.output_token_microcredits,
|
|
115
|
+
rule.output_mtoken_microcredits,
|
|
116
|
+
rule.cached_token_microcredits,
|
|
117
|
+
rule.cached_mtoken_microcredits,
|
|
118
|
+
rule.image_microcredits,
|
|
119
|
+
rule.status,
|
|
120
|
+
rule.note,
|
|
121
|
+
now,
|
|
122
|
+
rule.rule_id,
|
|
123
|
+
]);
|
|
124
|
+
return await this.readPricingRuleRequired(rule.rule_id);
|
|
125
|
+
}
|
|
126
|
+
await rawRun(this.resolveRaw(), [
|
|
127
|
+
`INSERT INTO ${PRICING_RULE_TABLE} (${PRICING_RULE_COLUMNS})`,
|
|
128
|
+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
129
|
+
].join(" "), [
|
|
130
|
+
rule.rule_id,
|
|
131
|
+
rule.service_id,
|
|
132
|
+
rule.action_id,
|
|
133
|
+
rule.model_id,
|
|
134
|
+
rule.provider_id,
|
|
135
|
+
rule.request_microcredits,
|
|
136
|
+
rule.input_token_microcredits,
|
|
137
|
+
rule.input_mtoken_microcredits,
|
|
138
|
+
rule.output_token_microcredits,
|
|
139
|
+
rule.output_mtoken_microcredits,
|
|
140
|
+
rule.cached_token_microcredits,
|
|
141
|
+
rule.cached_mtoken_microcredits,
|
|
142
|
+
rule.image_microcredits,
|
|
143
|
+
rule.status,
|
|
144
|
+
rule.note,
|
|
145
|
+
rule.created_at,
|
|
146
|
+
rule.updated_at,
|
|
147
|
+
]);
|
|
148
|
+
return await this.readPricingRuleRequired(rule.rule_id);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 列出 pricing rules。
|
|
152
|
+
*/
|
|
153
|
+
async listPricingRules(query = {}) {
|
|
154
|
+
const rows = await rawAll(this.resolveRaw(), [
|
|
155
|
+
`SELECT ${PRICING_RULE_COLUMNS} FROM ${PRICING_RULE_TABLE}`,
|
|
156
|
+
"ORDER BY updated_at DESC, rowid DESC",
|
|
157
|
+
"LIMIT ?",
|
|
158
|
+
].join(" "), [normalizeLimit(query.limit)]);
|
|
159
|
+
return rows.map(parsePricingRuleRow);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 列出 charge。
|
|
163
|
+
*/
|
|
164
|
+
async listCharges(query = {}) {
|
|
165
|
+
const params = [];
|
|
166
|
+
const clauses = [];
|
|
167
|
+
if (query.user_id) {
|
|
168
|
+
clauses.push("user_id = ?");
|
|
169
|
+
params.push(String(query.user_id));
|
|
170
|
+
}
|
|
171
|
+
if (query.town_id) {
|
|
172
|
+
clauses.push("town_id = ?");
|
|
173
|
+
params.push(String(query.town_id));
|
|
174
|
+
}
|
|
175
|
+
const where = clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : "";
|
|
176
|
+
const rows = await rawAll(this.resolveRaw(), [
|
|
177
|
+
`SELECT charge_id, user_id, town_id, service_id, action_id, model_id, provider_id, rule_id, amount_microcredits, status, note, metadata_json, created_at FROM ${CHARGE_TABLE}`,
|
|
178
|
+
where,
|
|
179
|
+
"ORDER BY created_at DESC, rowid DESC",
|
|
180
|
+
"LIMIT ?",
|
|
181
|
+
].join(" "), [...params, normalizeLimit(query.limit)]);
|
|
182
|
+
return rows.map(parseChargeRow);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 对当前 service 调用执行结算。
|
|
186
|
+
*/
|
|
187
|
+
async settle(ctx) {
|
|
188
|
+
const rule = await this.resolvePricingRule(ctx);
|
|
189
|
+
if (!rule)
|
|
190
|
+
return undefined;
|
|
191
|
+
const amount_microcredits = calculateChargeAmount(ctx, rule, { estimate: false });
|
|
192
|
+
if (amount_microcredits <= 0)
|
|
193
|
+
return undefined;
|
|
194
|
+
const charge = await this.createCharge(ctx, rule, amount_microcredits);
|
|
195
|
+
await this.balance.subMicrocredits(ctx.user.user_id, amount_microcredits, {
|
|
196
|
+
note: charge.note,
|
|
197
|
+
ref: charge.charge_id,
|
|
198
|
+
meta: {
|
|
199
|
+
charge_id: charge.charge_id,
|
|
200
|
+
service_id: charge.service_id,
|
|
201
|
+
action_id: charge.action_id,
|
|
202
|
+
model_id: charge.model_id,
|
|
203
|
+
provider_id: charge.provider_id,
|
|
204
|
+
rule_id: charge.rule_id,
|
|
205
|
+
metering: ctx.metering ?? {},
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
return charge;
|
|
209
|
+
}
|
|
210
|
+
async createCharge(ctx, rule, amount_microcredits) {
|
|
211
|
+
const now = new Date().toISOString();
|
|
212
|
+
const charge = {
|
|
213
|
+
charge_id: `chg_${randomId()}`,
|
|
214
|
+
user_id: ctx.user?.user_id ?? "",
|
|
215
|
+
town_id: ctx.town?.town_id ?? "",
|
|
216
|
+
service_id: ctx.service?.id ?? "",
|
|
217
|
+
action_id: ctx.action?.id ?? "",
|
|
218
|
+
model_id: ctx.variant?.id ?? ctx.metering?.model_id ?? "",
|
|
219
|
+
provider_id: ctx.metering?.provider_id ?? "",
|
|
220
|
+
rule_id: rule.rule_id,
|
|
221
|
+
amount: microcreditsToCredits(amount_microcredits),
|
|
222
|
+
amount_microcredits,
|
|
223
|
+
status: "settled",
|
|
224
|
+
note: `${ctx.service?.id ?? "service"} ${ctx.action?.id ?? "action"}`,
|
|
225
|
+
metadata_json: JSON.stringify({
|
|
226
|
+
metering: ctx.metering ?? {},
|
|
227
|
+
pricing_rule: rule.rule_id,
|
|
228
|
+
}),
|
|
229
|
+
created_at: now,
|
|
230
|
+
};
|
|
231
|
+
await rawRun(this.resolveRaw(), [
|
|
232
|
+
`INSERT INTO ${CHARGE_TABLE} (charge_id, user_id, town_id, service_id, action_id, model_id, provider_id, rule_id, amount_microcredits, status, note, metadata_json, created_at)`,
|
|
233
|
+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
234
|
+
].join(" "), [
|
|
235
|
+
charge.charge_id,
|
|
236
|
+
charge.user_id,
|
|
237
|
+
charge.town_id,
|
|
238
|
+
charge.service_id,
|
|
239
|
+
charge.action_id,
|
|
240
|
+
charge.model_id,
|
|
241
|
+
charge.provider_id,
|
|
242
|
+
charge.rule_id,
|
|
243
|
+
charge.amount_microcredits,
|
|
244
|
+
charge.status,
|
|
245
|
+
charge.note,
|
|
246
|
+
charge.metadata_json,
|
|
247
|
+
charge.created_at,
|
|
248
|
+
]);
|
|
249
|
+
return charge;
|
|
250
|
+
}
|
|
251
|
+
async resolvePricingRule(ctx) {
|
|
252
|
+
const rules = (await this.listPricingRules({ limit: 200 }))
|
|
253
|
+
.filter((rule) => rule.status === "active")
|
|
254
|
+
.filter((rule) => matchesRule(rule.service_id, ctx.service?.id ?? ""))
|
|
255
|
+
.filter((rule) => matchesRule(rule.action_id, ctx.action?.id ?? ""))
|
|
256
|
+
.filter((rule) => matchesRule(rule.model_id, ctx.variant?.id ?? ctx.metering?.model_id ?? ""))
|
|
257
|
+
.filter((rule) => matchesRule(rule.provider_id, ctx.metering?.provider_id ?? ""));
|
|
258
|
+
return rules.sort((a, b) => scoreRule(b) - scoreRule(a))[0];
|
|
259
|
+
}
|
|
260
|
+
async readPricingRule(rule_id) {
|
|
261
|
+
const row = await rawFirst(this.resolveRaw(), [
|
|
262
|
+
`SELECT ${PRICING_RULE_COLUMNS} FROM ${PRICING_RULE_TABLE}`,
|
|
263
|
+
"WHERE rule_id = ?",
|
|
264
|
+
].join(" "), [rule_id]);
|
|
265
|
+
return row ? parsePricingRuleRow(row) : undefined;
|
|
266
|
+
}
|
|
267
|
+
async readPricingRuleRequired(rule_id) {
|
|
268
|
+
const row = await this.readPricingRule(rule_id);
|
|
269
|
+
if (!row)
|
|
270
|
+
throw new Error(`pricing rule not found: ${rule_id}`);
|
|
271
|
+
return row;
|
|
272
|
+
}
|
|
273
|
+
async ensurePricingRuleColumns() {
|
|
274
|
+
for (const column of PRICING_RULE_MTOKEN_COLUMNS) {
|
|
275
|
+
try {
|
|
276
|
+
await rawRun(this.resolveRaw(), `ALTER TABLE ${PRICING_RULE_TABLE} ADD COLUMN ${column} INTEGER NOT NULL DEFAULT 0`, []);
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
280
|
+
if (!/duplicate column|already exists/i.test(message))
|
|
281
|
+
throw error;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
resolveRaw() {
|
|
286
|
+
if (!this._raw) {
|
|
287
|
+
throw new Error("billing service raw database is not ready");
|
|
288
|
+
}
|
|
289
|
+
return this._raw;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* 创建 Billing 服务实例。
|
|
294
|
+
*/
|
|
295
|
+
export function billingService(options) {
|
|
296
|
+
return new BillingService(options);
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* 是否应该对当前调用计费。
|
|
300
|
+
*/
|
|
301
|
+
function shouldBill(ctx) {
|
|
302
|
+
return Boolean(ctx.identity?.kind === "user" && ctx.user?.user_id && ctx.town?.town_id && ctx.service?.id !== "billing");
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* 判断输出是否成功。
|
|
306
|
+
*/
|
|
307
|
+
function isSuccessfulOutput(output) {
|
|
308
|
+
return !(output instanceof Response) || output.status < 400;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* 计算扣费金额。
|
|
312
|
+
*/
|
|
313
|
+
function calculateChargeAmount(ctx, rule, options) {
|
|
314
|
+
const metering = ctx.metering ?? {};
|
|
315
|
+
const request_count = Number(metering.request_count ?? 1);
|
|
316
|
+
const input_tokens = options.estimate ? 0 : Number(metering.input_tokens ?? 0);
|
|
317
|
+
const output_tokens = options.estimate ? 0 : Number(metering.output_tokens ?? 0);
|
|
318
|
+
const cached_tokens = options.estimate ? 0 : Number(metering.cached_tokens ?? 0);
|
|
319
|
+
const image_count = options.estimate ? 0 : Number(metering.image_count ?? 0);
|
|
320
|
+
const total = request_count * rule.request_microcredits
|
|
321
|
+
+ input_tokens * rule.input_token_microcredits
|
|
322
|
+
+ (input_tokens * rule.input_mtoken_microcredits) / TOKENS_PER_MTOKEN
|
|
323
|
+
+ output_tokens * rule.output_token_microcredits
|
|
324
|
+
+ (output_tokens * rule.output_mtoken_microcredits) / TOKENS_PER_MTOKEN
|
|
325
|
+
+ cached_tokens * rule.cached_token_microcredits
|
|
326
|
+
+ (cached_tokens * rule.cached_mtoken_microcredits) / TOKENS_PER_MTOKEN
|
|
327
|
+
+ image_count * rule.image_microcredits;
|
|
328
|
+
return Number.isFinite(total) && total > 0 ? Math.ceil(total) : 0;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* 规则字段为空时表示 fallback。
|
|
332
|
+
*/
|
|
333
|
+
function matchesRule(rule_value, actual_value) {
|
|
334
|
+
return !rule_value || rule_value === actual_value;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* 规则越具体优先级越高。
|
|
338
|
+
*/
|
|
339
|
+
function scoreRule(rule) {
|
|
340
|
+
return [
|
|
341
|
+
rule.service_id,
|
|
342
|
+
rule.action_id,
|
|
343
|
+
rule.model_id,
|
|
344
|
+
rule.provider_id,
|
|
345
|
+
].filter(Boolean).length;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* 标准化 pricing rule。
|
|
349
|
+
*/
|
|
350
|
+
function normalizePricingRule(input, now) {
|
|
351
|
+
const service_id = normalizeText(input.service_id ?? "ai");
|
|
352
|
+
const action_id = normalizeText(input.action_id ?? "");
|
|
353
|
+
const model_id = normalizeText(input.model_id ?? "");
|
|
354
|
+
const provider_id = normalizeText(input.provider_id ?? "");
|
|
355
|
+
return {
|
|
356
|
+
rule_id: normalizeText(input.rule_id) || `price_${randomId()}`,
|
|
357
|
+
service_id,
|
|
358
|
+
action_id,
|
|
359
|
+
model_id,
|
|
360
|
+
provider_id,
|
|
361
|
+
request_microcredits: normalizeNonNegativeInteger(input.request_microcredits ?? 0, "request_microcredits"),
|
|
362
|
+
input_token_microcredits: normalizeNonNegativeInteger(input.input_token_microcredits ?? 0, "input_token_microcredits"),
|
|
363
|
+
input_mtoken_microcredits: normalizeNonNegativeInteger(input.input_mtoken_microcredits ?? 0, "input_mtoken_microcredits"),
|
|
364
|
+
output_token_microcredits: normalizeNonNegativeInteger(input.output_token_microcredits ?? 0, "output_token_microcredits"),
|
|
365
|
+
output_mtoken_microcredits: normalizeNonNegativeInteger(input.output_mtoken_microcredits ?? 0, "output_mtoken_microcredits"),
|
|
366
|
+
cached_token_microcredits: normalizeNonNegativeInteger(input.cached_token_microcredits ?? 0, "cached_token_microcredits"),
|
|
367
|
+
cached_mtoken_microcredits: normalizeNonNegativeInteger(input.cached_mtoken_microcredits ?? 0, "cached_mtoken_microcredits"),
|
|
368
|
+
image_microcredits: normalizeNonNegativeInteger(input.image_microcredits ?? 0, "image_microcredits"),
|
|
369
|
+
status: input.status === "disabled" ? "disabled" : "active",
|
|
370
|
+
note: normalizeText(input.note),
|
|
371
|
+
created_at: now,
|
|
372
|
+
updated_at: now,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* 解析 pricing rule 行。
|
|
377
|
+
*/
|
|
378
|
+
function parsePricingRuleRow(row) {
|
|
379
|
+
return {
|
|
380
|
+
rule_id: String(row.rule_id),
|
|
381
|
+
service_id: String(row.service_id ?? ""),
|
|
382
|
+
action_id: String(row.action_id ?? ""),
|
|
383
|
+
model_id: String(row.model_id ?? ""),
|
|
384
|
+
provider_id: String(row.provider_id ?? ""),
|
|
385
|
+
request_microcredits: Number(row.request_microcredits ?? 0),
|
|
386
|
+
input_token_microcredits: Number(row.input_token_microcredits ?? 0),
|
|
387
|
+
input_mtoken_microcredits: Number(row.input_mtoken_microcredits ?? 0),
|
|
388
|
+
output_token_microcredits: Number(row.output_token_microcredits ?? 0),
|
|
389
|
+
output_mtoken_microcredits: Number(row.output_mtoken_microcredits ?? 0),
|
|
390
|
+
cached_token_microcredits: Number(row.cached_token_microcredits ?? 0),
|
|
391
|
+
cached_mtoken_microcredits: Number(row.cached_mtoken_microcredits ?? 0),
|
|
392
|
+
image_microcredits: Number(row.image_microcredits ?? 0),
|
|
393
|
+
status: row.status === "disabled" ? "disabled" : "active",
|
|
394
|
+
note: String(row.note ?? ""),
|
|
395
|
+
created_at: String(row.created_at ?? ""),
|
|
396
|
+
updated_at: String(row.updated_at ?? ""),
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* 解析 charge 行。
|
|
401
|
+
*/
|
|
402
|
+
function parseChargeRow(row) {
|
|
403
|
+
const amount_microcredits = Number(row.amount_microcredits ?? 0);
|
|
404
|
+
return {
|
|
405
|
+
charge_id: String(row.charge_id),
|
|
406
|
+
user_id: String(row.user_id ?? ""),
|
|
407
|
+
town_id: String(row.town_id ?? ""),
|
|
408
|
+
service_id: String(row.service_id ?? ""),
|
|
409
|
+
action_id: String(row.action_id ?? ""),
|
|
410
|
+
model_id: String(row.model_id ?? ""),
|
|
411
|
+
provider_id: String(row.provider_id ?? ""),
|
|
412
|
+
rule_id: String(row.rule_id ?? ""),
|
|
413
|
+
amount: microcreditsToCredits(amount_microcredits),
|
|
414
|
+
amount_microcredits,
|
|
415
|
+
status: row.status === "skipped" ? "skipped" : "settled",
|
|
416
|
+
note: String(row.note ?? ""),
|
|
417
|
+
metadata_json: String(row.metadata_json ?? "{}"),
|
|
418
|
+
created_at: String(row.created_at ?? ""),
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* 标准化非负整数。
|
|
423
|
+
*/
|
|
424
|
+
function normalizeNonNegativeInteger(value, label) {
|
|
425
|
+
const normalized = Number(value ?? 0);
|
|
426
|
+
if (!Number.isSafeInteger(normalized) || normalized < 0) {
|
|
427
|
+
throw new TypeError(`${label} must be a non-negative integer`);
|
|
428
|
+
}
|
|
429
|
+
return normalized;
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* 标准化 limit。
|
|
433
|
+
*/
|
|
434
|
+
function normalizeLimit(value) {
|
|
435
|
+
const normalized = Number(value ?? 20);
|
|
436
|
+
if (!Number.isInteger(normalized) || normalized <= 0)
|
|
437
|
+
return 20;
|
|
438
|
+
return Math.min(normalized, 200);
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* 标准化文本。
|
|
442
|
+
*/
|
|
443
|
+
function normalizeText(value) {
|
|
444
|
+
return String(value ?? "").trim();
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* 生成随机 ID。
|
|
448
|
+
*/
|
|
449
|
+
function randomId() {
|
|
450
|
+
const bytes = new Uint8Array(12);
|
|
451
|
+
crypto.getRandomValues(bytes);
|
|
452
|
+
return btoa(String.fromCharCode(...bytes))
|
|
453
|
+
.replace(/\+/g, "-")
|
|
454
|
+
.replace(/\//g, "_")
|
|
455
|
+
.replace(/=+$/, "");
|
|
456
|
+
}
|
|
457
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../src/billing/service.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,kBAAkB,GAGnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACpG,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAWpD,MAAM,iBAAiB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,KAAa;IAChE,MAAM,UAAU,GAAG,MAAM,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC;IACtC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC,IAAI,UAAU,GAAG,CAAC,EAAE,CAAC;QACxD,MAAM,IAAI,SAAS,CAAC,GAAG,KAAK,iCAAiC,CAAC,CAAC;IACjE,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;GAEG;AACH,SAAS,cAAc,CAAC,KAAc;IACpC,MAAM,UAAU,GAAG,MAAM,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;IACvC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,UAAU,IAAI,CAAC;QAAE,OAAO,EAAE,CAAC;IAChE,OAAO,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;AACnC,CAAC;AAED;;GAEG;AACH,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,MAAM,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;AACpC,CAAC;AAED;;GAEG;AACH,SAAS,QAAQ;IACf,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IACjC,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;IAC9B,OAAO,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,KAAK,CAAC,CAAC;SACvC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;SACnB,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;SACnB,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Billing 服务类型定义。
|
|
3
|
+
*
|
|
4
|
+
* 关键说明(中文)
|
|
5
|
+
* - pricing rule 使用 microcredits 描述单价
|
|
6
|
+
* - charge 是一次已经结算的账单事实
|
|
7
|
+
* - balance 桥接只暴露 billing 需要的最小扣费能力
|
|
8
|
+
*/
|
|
9
|
+
import type { Context } from "@downcity/city";
|
|
10
|
+
/**
|
|
11
|
+
* Pricing rule 状态。
|
|
12
|
+
*/
|
|
13
|
+
export type BillingPricingRuleStatus = "active" | "disabled";
|
|
14
|
+
/**
|
|
15
|
+
* Charge 状态。
|
|
16
|
+
*/
|
|
17
|
+
export type BillingChargeStatus = "settled" | "skipped";
|
|
18
|
+
/**
|
|
19
|
+
* Billing 所需的 balance 最小桥接接口。
|
|
20
|
+
*/
|
|
21
|
+
export interface BillingBalanceBridge {
|
|
22
|
+
/**
|
|
23
|
+
* 检查余额是否足够,单位为 microcredits。
|
|
24
|
+
*/
|
|
25
|
+
requireMicrocredits(user_id: string, amount_microcredits: number): Promise<unknown>;
|
|
26
|
+
/**
|
|
27
|
+
* 扣减余额,单位为 microcredits。
|
|
28
|
+
*/
|
|
29
|
+
subMicrocredits(user_id: string, amount_microcredits: number, extra?: {
|
|
30
|
+
/** 说明文本。 */
|
|
31
|
+
note?: string;
|
|
32
|
+
/** 外部引用 ID。 */
|
|
33
|
+
ref?: string;
|
|
34
|
+
/** 结构化扩展字段。 */
|
|
35
|
+
meta?: Record<string, unknown>;
|
|
36
|
+
}): Promise<unknown>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Billing 服务配置。
|
|
40
|
+
*/
|
|
41
|
+
export interface BillingServiceOptions {
|
|
42
|
+
/**
|
|
43
|
+
* 已挂载到 City 的 balance 服务实例。
|
|
44
|
+
*/
|
|
45
|
+
balance: BillingBalanceBridge;
|
|
46
|
+
/**
|
|
47
|
+
* 默认 pricing rules。
|
|
48
|
+
*/
|
|
49
|
+
pricing_rules?: BillingPricingRuleInput[];
|
|
50
|
+
/**
|
|
51
|
+
* 是否启用请求前余额检查。
|
|
52
|
+
*/
|
|
53
|
+
require_before_call?: boolean;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Pricing rule 输入。
|
|
57
|
+
*/
|
|
58
|
+
export interface BillingPricingRuleInput extends Record<string, unknown> {
|
|
59
|
+
/**
|
|
60
|
+
* 规则 ID。
|
|
61
|
+
*/
|
|
62
|
+
rule_id?: string;
|
|
63
|
+
/**
|
|
64
|
+
* 服务 ID,例如 ai。
|
|
65
|
+
*/
|
|
66
|
+
service_id?: string;
|
|
67
|
+
/**
|
|
68
|
+
* action ID,例如 chat/completions。
|
|
69
|
+
*/
|
|
70
|
+
action_id?: string;
|
|
71
|
+
/**
|
|
72
|
+
* 模型 ID,空表示 fallback。
|
|
73
|
+
*/
|
|
74
|
+
model_id?: string;
|
|
75
|
+
/**
|
|
76
|
+
* provider ID,空表示 fallback。
|
|
77
|
+
*/
|
|
78
|
+
provider_id?: string;
|
|
79
|
+
/**
|
|
80
|
+
* 每次请求固定扣费,单位为 microcredits。
|
|
81
|
+
*/
|
|
82
|
+
request_microcredits?: number;
|
|
83
|
+
/**
|
|
84
|
+
* 每个 input token 扣费,单位为 microcredits。
|
|
85
|
+
*/
|
|
86
|
+
input_token_microcredits?: number;
|
|
87
|
+
/**
|
|
88
|
+
* 每 1,000,000 个 input token 扣费,单位为 microcredits。
|
|
89
|
+
*/
|
|
90
|
+
input_mtoken_microcredits?: number;
|
|
91
|
+
/**
|
|
92
|
+
* 每个 output token 扣费,单位为 microcredits。
|
|
93
|
+
*/
|
|
94
|
+
output_token_microcredits?: number;
|
|
95
|
+
/**
|
|
96
|
+
* 每 1,000,000 个 output token 扣费,单位为 microcredits。
|
|
97
|
+
*/
|
|
98
|
+
output_mtoken_microcredits?: number;
|
|
99
|
+
/**
|
|
100
|
+
* 每个 cached input token 扣费,单位为 microcredits。
|
|
101
|
+
*/
|
|
102
|
+
cached_token_microcredits?: number;
|
|
103
|
+
/**
|
|
104
|
+
* 每 1,000,000 个 cached input token 扣费,单位为 microcredits。
|
|
105
|
+
*/
|
|
106
|
+
cached_mtoken_microcredits?: number;
|
|
107
|
+
/**
|
|
108
|
+
* 每张图片扣费,单位为 microcredits。
|
|
109
|
+
*/
|
|
110
|
+
image_microcredits?: number;
|
|
111
|
+
/**
|
|
112
|
+
* 规则状态。
|
|
113
|
+
*/
|
|
114
|
+
status?: BillingPricingRuleStatus;
|
|
115
|
+
/**
|
|
116
|
+
* 说明文本。
|
|
117
|
+
*/
|
|
118
|
+
note?: string;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Pricing rule 记录。
|
|
122
|
+
*/
|
|
123
|
+
export interface BillingPricingRule extends Required<BillingPricingRuleInput>, Record<string, unknown> {
|
|
124
|
+
/**
|
|
125
|
+
* 规则 ID。
|
|
126
|
+
*/
|
|
127
|
+
rule_id: string;
|
|
128
|
+
/**
|
|
129
|
+
* 创建时间。
|
|
130
|
+
*/
|
|
131
|
+
created_at: string;
|
|
132
|
+
/**
|
|
133
|
+
* 更新时间。
|
|
134
|
+
*/
|
|
135
|
+
updated_at: string;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Charge 记录。
|
|
139
|
+
*/
|
|
140
|
+
export interface BillingCharge extends Record<string, unknown> {
|
|
141
|
+
/** 扣费记录 ID。 */
|
|
142
|
+
charge_id: string;
|
|
143
|
+
/** 用户 ID。 */
|
|
144
|
+
user_id: string;
|
|
145
|
+
/** Town ID。 */
|
|
146
|
+
town_id: string;
|
|
147
|
+
/** 服务 ID。 */
|
|
148
|
+
service_id: string;
|
|
149
|
+
/** action ID。 */
|
|
150
|
+
action_id: string;
|
|
151
|
+
/** 模型 ID。 */
|
|
152
|
+
model_id: string;
|
|
153
|
+
/** provider ID。 */
|
|
154
|
+
provider_id: string;
|
|
155
|
+
/** 命中的 pricing rule ID。 */
|
|
156
|
+
rule_id: string;
|
|
157
|
+
/** 扣费金额,单位为 credits。 */
|
|
158
|
+
amount: number;
|
|
159
|
+
/** 扣费金额,单位为 microcredits。 */
|
|
160
|
+
amount_microcredits: number;
|
|
161
|
+
/** 状态。 */
|
|
162
|
+
status: BillingChargeStatus;
|
|
163
|
+
/** 说明文本。 */
|
|
164
|
+
note: string;
|
|
165
|
+
/** 扩展字段 JSON。 */
|
|
166
|
+
metadata_json: string;
|
|
167
|
+
/** 创建时间。 */
|
|
168
|
+
created_at: string;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Charge 查询条件。
|
|
172
|
+
*/
|
|
173
|
+
export interface BillingChargeQuery extends Record<string, unknown> {
|
|
174
|
+
/** 可选用户 ID。 */
|
|
175
|
+
user_id?: string;
|
|
176
|
+
/** 可选 Town ID。 */
|
|
177
|
+
town_id?: string;
|
|
178
|
+
/** 返回条数上限。 */
|
|
179
|
+
limit?: string | number;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Pricing rule 查询条件。
|
|
183
|
+
*/
|
|
184
|
+
export interface BillingPricingRuleQuery extends Record<string, unknown> {
|
|
185
|
+
/** 返回条数上限。 */
|
|
186
|
+
limit?: string | number;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Billing hook 结算上下文。
|
|
190
|
+
*/
|
|
191
|
+
export interface BillingSettlementContext {
|
|
192
|
+
/**
|
|
193
|
+
* 当前 service action context。
|
|
194
|
+
*/
|
|
195
|
+
ctx: Context;
|
|
196
|
+
/**
|
|
197
|
+
* 命中的 pricing rule。
|
|
198
|
+
*/
|
|
199
|
+
rule: BillingPricingRule;
|
|
200
|
+
/**
|
|
201
|
+
* 结算金额,单位为 microcredits。
|
|
202
|
+
*/
|
|
203
|
+
amount_microcredits: number;
|
|
204
|
+
}
|
|
205
|
+
//# sourceMappingURL=types.d.ts.map
|