@reglow/reglow 0.13.0 → 0.14.1
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 +101 -41
- package/dist/reglow/core/config.js +1 -1
- package/dist/reglow/core/entities.d.ts +135 -0
- package/dist/reglow/core/models.d.ts +22 -0
- package/dist/reglow/core/models.js +15 -0
- package/dist/reglow/core/models.js.map +1 -1
- package/dist/reglow/core/storage.js +6 -2
- package/dist/reglow/core/storage.js.map +1 -1
- package/dist/reglow/core/tenant_context.d.ts +45 -0
- package/dist/reglow/core/tenant_context.js +77 -0
- package/dist/reglow/core/tenant_context.js.map +1 -0
- package/dist/reglow/core/tenant_guard.js +17 -5
- package/dist/reglow/core/tenant_guard.js.map +1 -1
- package/dist/reglow/modules/ai/controller.js +129 -0
- package/dist/reglow/modules/ai/controller.js.map +1 -1
- package/dist/reglow/modules/ai_usage/controller.d.ts +8 -0
- package/dist/reglow/modules/ai_usage/controller.js +278 -0
- package/dist/reglow/modules/ai_usage/controller.js.map +1 -0
- package/dist/reglow/modules/ai_usage/model.d.ts +216 -0
- package/dist/reglow/modules/ai_usage/model.js +25 -0
- package/dist/reglow/modules/ai_usage/model.js.map +1 -0
- package/dist/reglow/modules/ai_usage/pricing.d.ts +80 -0
- package/dist/reglow/modules/ai_usage/pricing.js +276 -0
- package/dist/reglow/modules/ai_usage/pricing.js.map +1 -0
- package/dist/reglow/modules/ai_usage/reconcile.d.ts +34 -0
- package/dist/reglow/modules/ai_usage/reconcile.js +179 -0
- package/dist/reglow/modules/ai_usage/reconcile.js.map +1 -0
- package/dist/reglow/modules/ai_usage/recorder.d.ts +14 -0
- package/dist/reglow/modules/ai_usage/recorder.js +227 -0
- package/dist/reglow/modules/ai_usage/recorder.js.map +1 -0
- package/dist/reglow/modules/ai_usage/router.d.ts +10 -0
- package/dist/reglow/modules/ai_usage/router.js +10 -0
- package/dist/reglow/modules/ai_usage/router.js.map +1 -0
- package/dist/reglow/modules/ai_usage/schema.d.ts +152 -0
- package/dist/reglow/modules/ai_usage/schema.js +8 -0
- package/dist/reglow/modules/ai_usage/schema.js.map +1 -0
- package/dist/reglow/modules/ai_usage/service.d.ts +40 -0
- package/dist/reglow/modules/ai_usage/service.js +654 -0
- package/dist/reglow/modules/ai_usage/service.js.map +1 -0
- package/dist/reglow/modules/ai_usage/sync.d.ts +98 -0
- package/dist/reglow/modules/ai_usage/sync.js +954 -0
- package/dist/reglow/modules/ai_usage/sync.js.map +1 -0
- package/dist/reglow/modules/ai_usage/xlsx_io.d.ts +35 -0
- package/dist/reglow/modules/ai_usage/xlsx_io.js +494 -0
- package/dist/reglow/modules/ai_usage/xlsx_io.js.map +1 -0
- package/dist/reglow/modules/billing/controller.d.ts +6 -0
- package/dist/reglow/modules/billing/controller.js +112 -0
- package/dist/reglow/modules/billing/controller.js.map +1 -0
- package/dist/reglow/modules/billing/repository.d.ts +20 -0
- package/dist/reglow/modules/billing/repository.js +101 -0
- package/dist/reglow/modules/billing/repository.js.map +1 -0
- package/dist/reglow/modules/billing/router.d.ts +10 -0
- package/dist/reglow/modules/billing/router.js +8 -0
- package/dist/reglow/modules/billing/router.js.map +1 -0
- package/dist/reglow/modules/billing/schema.d.ts +38 -0
- package/dist/reglow/modules/billing/schema.js +42 -0
- package/dist/reglow/modules/billing/schema.js.map +1 -0
- package/dist/reglow/modules/billing/service.d.ts +22 -0
- package/dist/reglow/modules/billing/service.js +179 -0
- package/dist/reglow/modules/billing/service.js.map +1 -0
- package/dist/reglow/modules/customer_service/controller.js +81 -3
- package/dist/reglow/modules/customer_service/controller.js.map +1 -1
- package/dist/reglow/modules/customer_service/repository.d.ts +9 -1
- package/dist/reglow/modules/customer_service/repository.js +56 -2
- package/dist/reglow/modules/customer_service/repository.js.map +1 -1
- package/dist/reglow/modules/customer_service/schema.d.ts +2 -0
- package/dist/reglow/modules/customer_service/schema.js +8 -0
- package/dist/reglow/modules/customer_service/schema.js.map +1 -1
- package/dist/reglow/modules/customer_service/sdk_template.html +276 -5
- package/dist/reglow/modules/customer_service/service.d.ts +45 -4
- package/dist/reglow/modules/customer_service/service.js +352 -96
- package/dist/reglow/modules/customer_service/service.js.map +1 -1
- package/dist/reglow/modules/customer_service/ws.d.ts +8 -1
- package/dist/reglow/modules/customer_service/ws.js +38 -1
- package/dist/reglow/modules/customer_service/ws.js.map +1 -1
- package/dist/reglow/modules/open_api/auth.d.ts +28 -0
- package/dist/reglow/modules/open_api/auth.js +65 -0
- package/dist/reglow/modules/open_api/auth.js.map +1 -0
- package/dist/reglow/modules/open_api/controller.d.ts +8 -0
- package/dist/reglow/modules/open_api/controller.js +109 -0
- package/dist/reglow/modules/open_api/controller.js.map +1 -0
- package/dist/reglow/modules/open_api/repository.d.ts +20 -0
- package/dist/reglow/modules/open_api/repository.js +111 -0
- package/dist/reglow/modules/open_api/repository.js.map +1 -0
- package/dist/reglow/modules/open_api/router.d.ts +10 -0
- package/dist/reglow/modules/open_api/router.js +8 -0
- package/dist/reglow/modules/open_api/router.js.map +1 -0
- package/dist/reglow/modules/open_api/schema.d.ts +24 -0
- package/dist/reglow/modules/open_api/schema.js +26 -0
- package/dist/reglow/modules/open_api/schema.js.map +1 -0
- package/dist/reglow/modules/open_api/security.d.ts +19 -0
- package/dist/reglow/modules/open_api/security.js +53 -0
- package/dist/reglow/modules/open_api/security.js.map +1 -0
- package/dist/reglow/modules/open_api/service.d.ts +28 -0
- package/dist/reglow/modules/open_api/service.js +158 -0
- package/dist/reglow/modules/open_api/service.js.map +1 -0
- package/dist/reglow/modules/principal/controller.d.ts +6 -0
- package/dist/reglow/modules/principal/controller.js +158 -0
- package/dist/reglow/modules/principal/controller.js.map +1 -0
- package/dist/reglow/modules/principal/repository.d.ts +20 -0
- package/dist/reglow/modules/principal/repository.js +76 -0
- package/dist/reglow/modules/principal/repository.js.map +1 -0
- package/dist/reglow/modules/principal/router.d.ts +9 -0
- package/dist/reglow/modules/principal/router.js +7 -0
- package/dist/reglow/modules/principal/router.js.map +1 -0
- package/dist/reglow/modules/principal/schema.d.ts +81 -0
- package/dist/reglow/modules/principal/schema.js +85 -0
- package/dist/reglow/modules/principal/schema.js.map +1 -0
- package/dist/reglow/modules/principal/service.d.ts +21 -0
- package/dist/reglow/modules/principal/service.js +193 -0
- package/dist/reglow/modules/principal/service.js.map +1 -0
- package/dist/reglow/modules/principal_member/controller.d.ts +6 -0
- package/dist/reglow/modules/principal_member/controller.js +64 -0
- package/dist/reglow/modules/principal_member/controller.js.map +1 -0
- package/dist/reglow/modules/principal_member/repository.d.ts +13 -0
- package/dist/reglow/modules/principal_member/repository.js +80 -0
- package/dist/reglow/modules/principal_member/repository.js.map +1 -0
- package/dist/reglow/modules/principal_member/router.d.ts +10 -0
- package/dist/reglow/modules/principal_member/router.js +8 -0
- package/dist/reglow/modules/principal_member/router.js.map +1 -0
- package/dist/reglow/modules/principal_member/schema.d.ts +27 -0
- package/dist/reglow/modules/principal_member/schema.js +28 -0
- package/dist/reglow/modules/principal_member/schema.js.map +1 -0
- package/dist/reglow/modules/principal_member/service.d.ts +16 -0
- package/dist/reglow/modules/principal_member/service.js +135 -0
- package/dist/reglow/modules/principal_member/service.js.map +1 -0
- package/dist/reglow/modules/studio/mcp/client.js +2 -2
- package/dist/reglow/modules/tenant/controller.d.ts +6 -0
- package/dist/reglow/modules/tenant/controller.js +99 -0
- package/dist/reglow/modules/tenant/controller.js.map +1 -0
- package/dist/reglow/modules/tenant/repository.d.ts +18 -0
- package/dist/reglow/modules/tenant/repository.js +71 -0
- package/dist/reglow/modules/tenant/repository.js.map +1 -0
- package/dist/reglow/modules/tenant/router.d.ts +9 -0
- package/dist/reglow/modules/tenant/router.js +7 -0
- package/dist/reglow/modules/tenant/router.js.map +1 -0
- package/dist/reglow/modules/tenant/schema.d.ts +33 -0
- package/dist/reglow/modules/tenant/schema.js +42 -0
- package/dist/reglow/modules/tenant/schema.js.map +1 -0
- package/dist/reglow/modules/tenant/service.d.ts +16 -0
- package/dist/reglow/modules/tenant/service.js +75 -0
- package/dist/reglow/modules/tenant/service.js.map +1 -0
- package/dist/reglow/modules/workflow/controller.d.ts +7 -0
- package/dist/reglow/modules/workflow/controller.js +133 -0
- package/dist/reglow/modules/workflow/controller.js.map +1 -0
- package/dist/reglow/modules/workflow/dsl.d.ts +41 -0
- package/dist/reglow/modules/workflow/dsl.js +173 -0
- package/dist/reglow/modules/workflow/dsl.js.map +1 -0
- package/dist/reglow/modules/workflow/model.d.ts +15 -0
- package/dist/reglow/modules/workflow/model.js +16 -0
- package/dist/reglow/modules/workflow/model.js.map +1 -0
- package/dist/reglow/modules/workflow/repository.d.ts +27 -0
- package/dist/reglow/modules/workflow/repository.js +151 -0
- package/dist/reglow/modules/workflow/repository.js.map +1 -0
- package/dist/reglow/modules/workflow/router.d.ts +10 -0
- package/dist/reglow/modules/workflow/router.js +8 -0
- package/dist/reglow/modules/workflow/router.js.map +1 -0
- package/dist/reglow/modules/workflow/schema.d.ts +32 -0
- package/dist/reglow/modules/workflow/schema.js +39 -0
- package/dist/reglow/modules/workflow/schema.js.map +1 -0
- package/dist/reglow/modules/workflow/service.d.ts +65 -0
- package/dist/reglow/modules/workflow/service.js +209 -0
- package/dist/reglow/modules/workflow/service.js.map +1 -0
- package/dist/reglow/scripts/init-data.js +157 -1
- package/dist/reglow/scripts/init-data.js.map +1 -1
- package/dist/reglow/scripts/schema.js +360 -0
- package/dist/reglow/scripts/schema.js.map +1 -1
- package/dist/server.js +3 -0
- package/dist/server.js.map +1 -1
- package/package.json +21 -21
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { ApiResponse, PageData } from "../../common/response.js";
|
|
2
|
+
import { AppException, ErrorCode, UnauthorizedException, } from "../../common/errors.js";
|
|
3
|
+
import { AiUsageService } from "./service.js";
|
|
4
|
+
import { buildUsageXlsx, XLSX_CONTENT_TYPE } from "./xlsx_io.js";
|
|
5
|
+
import { reconcileDue } from "./reconcile.js";
|
|
6
|
+
function int(v, def) {
|
|
7
|
+
const n = parseInt(String(v), 10);
|
|
8
|
+
return Number.isNaN(n) ? def : n;
|
|
9
|
+
}
|
|
10
|
+
function intOrNull(v) {
|
|
11
|
+
if (v === undefined || v === null || v === "")
|
|
12
|
+
return null;
|
|
13
|
+
const n = parseInt(String(v), 10);
|
|
14
|
+
return Number.isNaN(n) ? null : n;
|
|
15
|
+
}
|
|
16
|
+
function str(v) {
|
|
17
|
+
return v === undefined || v === null ? "" : String(v);
|
|
18
|
+
}
|
|
19
|
+
function strOrNull(v) {
|
|
20
|
+
if (v === undefined || v === null || v === "")
|
|
21
|
+
return null;
|
|
22
|
+
return String(v);
|
|
23
|
+
}
|
|
24
|
+
function parseQuery(q) {
|
|
25
|
+
return {
|
|
26
|
+
start: strOrNull(q.start),
|
|
27
|
+
end: strOrNull(q.end),
|
|
28
|
+
provider: strOrNull(q.provider),
|
|
29
|
+
model: strOrNull(q.model),
|
|
30
|
+
model_type: strOrNull(q.model_type),
|
|
31
|
+
scene: strOrNull(q.scene),
|
|
32
|
+
employee_id: intOrNull(q.employee_id),
|
|
33
|
+
status: strOrNull(q.status),
|
|
34
|
+
usage_source: strOrNull(q.usage_source),
|
|
35
|
+
granularity: str(q.granularity) || "day",
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function currentEmployeeId(request) {
|
|
39
|
+
const id = request.currentEmployee?.id;
|
|
40
|
+
if (!id)
|
|
41
|
+
throw new UnauthorizedException("未登录");
|
|
42
|
+
return id;
|
|
43
|
+
}
|
|
44
|
+
function pad2(n) {
|
|
45
|
+
return String(n).padStart(2, "0");
|
|
46
|
+
}
|
|
47
|
+
/** 与 Python strftime('%Y%m%d%H%M%S') 对齐 */
|
|
48
|
+
function timestampSuffix() {
|
|
49
|
+
const d = new Date();
|
|
50
|
+
return `${d.getFullYear()}${pad2(d.getMonth() + 1)}${pad2(d.getDate())}${pad2(d.getHours())}${pad2(d.getMinutes())}${pad2(d.getSeconds())}`;
|
|
51
|
+
}
|
|
52
|
+
function optNumber(v) {
|
|
53
|
+
if (v === undefined || v === null || v === "")
|
|
54
|
+
return undefined;
|
|
55
|
+
const n = Number(v);
|
|
56
|
+
return Number.isFinite(n) ? n : undefined;
|
|
57
|
+
}
|
|
58
|
+
function optNullableNumber(v) {
|
|
59
|
+
if (v === undefined || v === null || v === "")
|
|
60
|
+
return null;
|
|
61
|
+
const n = Number(v);
|
|
62
|
+
return Number.isFinite(n) ? n : null;
|
|
63
|
+
}
|
|
64
|
+
/** 解析价格写入请求体(对应 PriceUpsertRequest) */
|
|
65
|
+
function parsePriceBody(body) {
|
|
66
|
+
const raw = (body ?? {});
|
|
67
|
+
const provider = str(raw.provider).trim();
|
|
68
|
+
const model = str(raw.model).trim();
|
|
69
|
+
if (!provider || !model) {
|
|
70
|
+
throw new AppException(ErrorCode.PARAM_ERROR, "provider 和 model 不能为空");
|
|
71
|
+
}
|
|
72
|
+
const effectiveFrom = raw.effective_from;
|
|
73
|
+
return {
|
|
74
|
+
provider,
|
|
75
|
+
model,
|
|
76
|
+
model_type: strOrNull(raw.model_type) ?? undefined,
|
|
77
|
+
billing_unit: strOrNull(raw.billing_unit) ?? undefined,
|
|
78
|
+
pricing_mode: strOrNull(raw.pricing_mode) ?? undefined,
|
|
79
|
+
unit_size: optNumber(raw.unit_size),
|
|
80
|
+
variant: strOrNull(raw.variant) ?? undefined,
|
|
81
|
+
currency: strOrNull(raw.currency) ?? undefined,
|
|
82
|
+
cost_input_per_1m: optNumber(raw.cost_input_per_1m),
|
|
83
|
+
cost_output_per_1m: optNumber(raw.cost_output_per_1m),
|
|
84
|
+
cost_cached_input_per_1m: optNullableNumber(raw.cost_cached_input_per_1m),
|
|
85
|
+
cost_unit_price: optNumber(raw.cost_unit_price),
|
|
86
|
+
sale_input_per_1m: optNullableNumber(raw.sale_input_per_1m),
|
|
87
|
+
sale_output_per_1m: optNullableNumber(raw.sale_output_per_1m),
|
|
88
|
+
sale_unit_price: optNullableNumber(raw.sale_unit_price),
|
|
89
|
+
effective_from: effectiveFrom === undefined ||
|
|
90
|
+
effectiveFrom === null ||
|
|
91
|
+
effectiveFrom === ""
|
|
92
|
+
? null
|
|
93
|
+
: String(effectiveFrom),
|
|
94
|
+
source: strOrNull(raw.source) ?? undefined,
|
|
95
|
+
remark: strOrNull(raw.remark),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
export async function aiUsageAdminRoutes(fastify) {
|
|
99
|
+
const service = new AiUsageService();
|
|
100
|
+
// ─── 管理端 ───
|
|
101
|
+
fastify.get("/summary", {
|
|
102
|
+
schema: { summary: "AI 用量汇总" },
|
|
103
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:view")],
|
|
104
|
+
}, async (request) => {
|
|
105
|
+
const query = parseQuery(request.query);
|
|
106
|
+
return ApiResponse.success(await service.summary(query));
|
|
107
|
+
});
|
|
108
|
+
fastify.get("/trend", {
|
|
109
|
+
schema: { summary: "AI 用量趋势" },
|
|
110
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:view")],
|
|
111
|
+
}, async (request) => {
|
|
112
|
+
const query = parseQuery(request.query);
|
|
113
|
+
return ApiResponse.success(await service.trend(query));
|
|
114
|
+
});
|
|
115
|
+
fastify.get("/breakdown", {
|
|
116
|
+
schema: { summary: "AI 用量分组统计" },
|
|
117
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:view")],
|
|
118
|
+
}, async (request) => {
|
|
119
|
+
const q = request.query;
|
|
120
|
+
const query = parseQuery(q);
|
|
121
|
+
const groupBy = str(q.group_by) || "provider";
|
|
122
|
+
const limit = Math.min(500, Math.max(1, int(q.limit, 100)));
|
|
123
|
+
return ApiResponse.success(await service.breakdown(query, groupBy, null, limit));
|
|
124
|
+
});
|
|
125
|
+
fastify.get("/records", {
|
|
126
|
+
schema: { summary: "AI 用量明细" },
|
|
127
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:view")],
|
|
128
|
+
}, async (request) => {
|
|
129
|
+
const q = request.query;
|
|
130
|
+
const query = parseQuery(q);
|
|
131
|
+
const page = Math.max(1, int(q.page, 1));
|
|
132
|
+
const size = Math.min(200, Math.max(1, int(q.size, 20)));
|
|
133
|
+
const [items, total] = await service.records(query, page, size);
|
|
134
|
+
return ApiResponse.success(new PageData(items, total, page, size));
|
|
135
|
+
});
|
|
136
|
+
fastify.get("/options", {
|
|
137
|
+
schema: { summary: "AI 用量筛选项" },
|
|
138
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:view")],
|
|
139
|
+
}, async () => ApiResponse.success(await service.options()));
|
|
140
|
+
fastify.get("/model-options", {
|
|
141
|
+
schema: { summary: "系统设置中的 AI 模型选项" },
|
|
142
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:view")],
|
|
143
|
+
}, async () => ApiResponse.success(await service.modelOptions()));
|
|
144
|
+
// ─── 导出 ───
|
|
145
|
+
fastify.get("/export", {
|
|
146
|
+
schema: { summary: "导出 AI 用量明细 XLSX" },
|
|
147
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:export")],
|
|
148
|
+
}, async (request, reply) => {
|
|
149
|
+
const query = parseQuery(request.query);
|
|
150
|
+
const rows = await service.exportRows(query);
|
|
151
|
+
const buffer = buildUsageXlsx(rows);
|
|
152
|
+
reply
|
|
153
|
+
.header("Content-Type", XLSX_CONTENT_TYPE)
|
|
154
|
+
.header("Content-Disposition", `attachment; filename="ai-usage-${timestampSuffix()}.xlsx"`);
|
|
155
|
+
return reply.send(buffer);
|
|
156
|
+
});
|
|
157
|
+
// ─── 价格配置 ───
|
|
158
|
+
fastify.get("/pricing", {
|
|
159
|
+
schema: { summary: "价格配置列表" },
|
|
160
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:price")],
|
|
161
|
+
}, async (request) => {
|
|
162
|
+
const q = request.query;
|
|
163
|
+
const page = Math.max(1, int(q.page, 1));
|
|
164
|
+
const size = Math.min(200, Math.max(1, int(q.size, 20)));
|
|
165
|
+
const [items, total] = await service.listPrices(page, size, strOrNull(q.provider), strOrNull(q.model), strOrNull(q.keyword));
|
|
166
|
+
return ApiResponse.success(new PageData(items, total, page, size));
|
|
167
|
+
});
|
|
168
|
+
fastify.put("/pricing", {
|
|
169
|
+
schema: { summary: "保存价格(append-only)" },
|
|
170
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:price")],
|
|
171
|
+
}, async (request) => {
|
|
172
|
+
const data = parsePriceBody(request.body);
|
|
173
|
+
return ApiResponse.success(await service.upsertPrice(data), "已保存");
|
|
174
|
+
});
|
|
175
|
+
fastify.put("/pricing/:price_id", {
|
|
176
|
+
schema: { summary: "更新价格(append-only)" },
|
|
177
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:price")],
|
|
178
|
+
}, async (request) => {
|
|
179
|
+
const priceId = int(request.params.price_id, 0);
|
|
180
|
+
const data = parsePriceBody(request.body);
|
|
181
|
+
return ApiResponse.success(await service.updatePrice(priceId, data), "已更新");
|
|
182
|
+
});
|
|
183
|
+
fastify.delete("/pricing/:price_id", {
|
|
184
|
+
schema: { summary: "删除价格(只关闭)" },
|
|
185
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:price")],
|
|
186
|
+
}, async (request) => {
|
|
187
|
+
const priceId = int(request.params.price_id, 0);
|
|
188
|
+
await service.deletePrice(priceId);
|
|
189
|
+
return ApiResponse.success(null, "已删除");
|
|
190
|
+
});
|
|
191
|
+
fastify.get("/pricing/sync-logs", {
|
|
192
|
+
schema: { summary: "价格同步记录" },
|
|
193
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:price")],
|
|
194
|
+
}, async (request) => {
|
|
195
|
+
const q = request.query;
|
|
196
|
+
const page = Math.max(1, int(q.page, 1));
|
|
197
|
+
const size = Math.min(200, Math.max(1, int(q.size, 20)));
|
|
198
|
+
const [items, total] = await service.listSyncLogs(page, size, strOrNull(q.provider));
|
|
199
|
+
return ApiResponse.success(new PageData(items, total, page, size));
|
|
200
|
+
});
|
|
201
|
+
fastify.post("/pricing/sync", {
|
|
202
|
+
schema: { summary: "同步官方价格(未注册适配器记为 skipped)" },
|
|
203
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:price")],
|
|
204
|
+
}, async (request) => {
|
|
205
|
+
const provider = strOrNull(request.query.provider);
|
|
206
|
+
if (!provider) {
|
|
207
|
+
throw new AppException(ErrorCode.PARAM_ERROR, "provider 不能为空");
|
|
208
|
+
}
|
|
209
|
+
return ApiResponse.success(await service.syncPrices(provider));
|
|
210
|
+
});
|
|
211
|
+
fastify.post("/pricing/import", {
|
|
212
|
+
schema: { summary: "导入价格(XLSX / CSV)" },
|
|
213
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:price")],
|
|
214
|
+
}, async (request) => {
|
|
215
|
+
let provider = "";
|
|
216
|
+
let filename = "";
|
|
217
|
+
let content = null;
|
|
218
|
+
for await (const raw of request.parts()) {
|
|
219
|
+
const part = raw;
|
|
220
|
+
if (part.type === "file") {
|
|
221
|
+
filename = part.filename || "";
|
|
222
|
+
content = await (part.toBuffer
|
|
223
|
+
? part.toBuffer()
|
|
224
|
+
: Promise.resolve(Buffer.alloc(0)));
|
|
225
|
+
}
|
|
226
|
+
else if (part.fieldname === "provider") {
|
|
227
|
+
provider = String(part.value ?? "");
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (!content) {
|
|
231
|
+
throw new AppException(ErrorCode.PARAM_ERROR, "请上传价格文件");
|
|
232
|
+
}
|
|
233
|
+
const result = await service.importPrices(provider, content, filename);
|
|
234
|
+
return ApiResponse.success(result, `已导入 ${result.imported} 条`);
|
|
235
|
+
});
|
|
236
|
+
fastify.post("/reconcile", {
|
|
237
|
+
schema: { summary: "手动触发媒体任务对账" },
|
|
238
|
+
preHandler: [fastify.requirePermission("analytics:ai_usage:price")],
|
|
239
|
+
}, async () => ApiResponse.success(await reconcileDue()));
|
|
240
|
+
// ─── 个人中心(只统计当前员工,仅需登录) ───
|
|
241
|
+
fastify.get("/me/summary", {
|
|
242
|
+
schema: { summary: "我的 AI 用量汇总" },
|
|
243
|
+
preHandler: [fastify.requireAuth],
|
|
244
|
+
}, async (request) => {
|
|
245
|
+
const query = parseQuery(request.query);
|
|
246
|
+
return ApiResponse.success(await service.summary(query, currentEmployeeId(request)));
|
|
247
|
+
});
|
|
248
|
+
fastify.get("/me/trend", {
|
|
249
|
+
schema: { summary: "我的 AI 用量趋势" },
|
|
250
|
+
preHandler: [fastify.requireAuth],
|
|
251
|
+
}, async (request) => {
|
|
252
|
+
const query = parseQuery(request.query);
|
|
253
|
+
return ApiResponse.success(await service.trend(query, currentEmployeeId(request)));
|
|
254
|
+
});
|
|
255
|
+
fastify.get("/me/breakdown", {
|
|
256
|
+
schema: { summary: "我的 AI 用量分组统计" },
|
|
257
|
+
preHandler: [fastify.requireAuth],
|
|
258
|
+
}, async (request) => {
|
|
259
|
+
const q = request.query;
|
|
260
|
+
const query = parseQuery(q);
|
|
261
|
+
const groupBy = str(q.group_by) || "provider";
|
|
262
|
+
const limit = Math.min(500, Math.max(1, int(q.limit, 100)));
|
|
263
|
+
return ApiResponse.success(await service.breakdown(query, groupBy, currentEmployeeId(request), limit));
|
|
264
|
+
});
|
|
265
|
+
fastify.get("/me/export", {
|
|
266
|
+
schema: { summary: "导出我的 AI 用量明细 XLSX" },
|
|
267
|
+
preHandler: [fastify.requireAuth],
|
|
268
|
+
}, async (request, reply) => {
|
|
269
|
+
const query = parseQuery(request.query);
|
|
270
|
+
const rows = await service.exportRows(query, currentEmployeeId(request));
|
|
271
|
+
const buffer = buildUsageXlsx(rows);
|
|
272
|
+
reply
|
|
273
|
+
.header("Content-Type", XLSX_CONTENT_TYPE)
|
|
274
|
+
.header("Content-Disposition", `attachment; filename="my-ai-usage-${timestampSuffix()}.xlsx"`);
|
|
275
|
+
return reply.send(buffer);
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
//# sourceMappingURL=controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../../../../reglow/modules/ai_usage/controller.ts"],"names":[],"mappings":"AAO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WAAW,CAAC,OAAO,CACxB,MAAM,OAAO,CAAC,SAAS,CACrB,KAAK,EACL,OAAO,EACP,iBAAiB,CAAC,OAAO,CAAC,EAC1B,KAAK,CACN,CACF,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,OAAO,CAAC,GAAG,CACT,YAAY,EACZ;QACE,MAAM,EAAE,EAAE,OAAO,EAAE,mBAAmB,EAAE;QACxC,UAAU,EAAE,CAAC,OAAO,CAAC,WAAW,CAAC;KAClC,EACD,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE;QACvB,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,KAAgC,CAAC,CAAC;QACnE,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,UAAU,CAAC,KAAK,EAAE,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAC;QACzE,MAAM,MAAM,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;QACpC,KAAK;aACF,MAAM,CAAC,cAAc,EAAE,iBAAiB,CAAC;aACzC,MAAM,CACL,qBAAqB,EACrB,qCAAqC,eAAe,EAAE,QAAQ,CAC/D,CAAC;QACJ,OAAO,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5B,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI 用量统计 - 表名、常量与实体类型
|
|
3
|
+
* 对应 backend-python 的 reglow/modules/ai_usage/model.py
|
|
4
|
+
*
|
|
5
|
+
* - sys_ai_usage:调用明细(文本 / embedding / 图片 / 视频,成功与失败都记)
|
|
6
|
+
* - sys_ai_model_price:价格快照来源(官方同步 / 手工 / CSV)
|
|
7
|
+
* - sys_ai_price_sync_log:价格同步记录
|
|
8
|
+
*/
|
|
9
|
+
import type { AnyDate, TimestampFields } from "../../core/entities.js";
|
|
10
|
+
export declare const AI_USAGE_TABLE: "sys_ai_usage";
|
|
11
|
+
export declare const AI_MODEL_PRICE_TABLE: "sys_ai_model_price";
|
|
12
|
+
export declare const AI_PRICE_SYNC_LOG_TABLE: "sys_ai_price_sync_log";
|
|
13
|
+
export declare const PRICING_MODE_TOKEN = "per_token";
|
|
14
|
+
export declare const PRICING_MODE_IMAGE = "per_image";
|
|
15
|
+
export declare const PRICING_MODE_SECOND = "per_second";
|
|
16
|
+
export declare const PRICING_MODE_REQUEST = "per_request";
|
|
17
|
+
export declare const PRICING_MODE_MEGAPIXEL = "per_megapixel";
|
|
18
|
+
export declare const PRICING_MODE_VIDEO_TOKEN = "per_video_token";
|
|
19
|
+
export declare const STATUS_SUBMITTED = "submitted";
|
|
20
|
+
export declare const STATUS_RUNNING = "running";
|
|
21
|
+
export declare const STATUS_SUCCESS = "success";
|
|
22
|
+
export declare const STATUS_FAILED = "failed";
|
|
23
|
+
export declare const STATUS_PARTIAL = "partial";
|
|
24
|
+
export declare const STATUS_TIMEOUT = "timeout";
|
|
25
|
+
export declare const STATUS_CANCELLED = "cancelled";
|
|
26
|
+
export declare const PRICING_PRICED = "priced";
|
|
27
|
+
export declare const PRICING_MISSING = "missing";
|
|
28
|
+
export declare const PRICING_FREE = "free";
|
|
29
|
+
export declare const PRICING_FALLBACK = "fallback";
|
|
30
|
+
export declare const PRICING_PENDING = "pending";
|
|
31
|
+
/** 数值列在 SQLite 下是 number、MySQL DECIMAL 下是 string */
|
|
32
|
+
export type NumericValue = number | string | bigint | null | undefined;
|
|
33
|
+
export interface AiUsageRow extends TimestampFields {
|
|
34
|
+
id: number;
|
|
35
|
+
tenant_id: number | null;
|
|
36
|
+
request_id: string;
|
|
37
|
+
parent_request_id: string | null;
|
|
38
|
+
attempt_no: number | null;
|
|
39
|
+
upstream_calls: number | null;
|
|
40
|
+
provider: string;
|
|
41
|
+
model: string;
|
|
42
|
+
model_type: string | null;
|
|
43
|
+
billing_unit: string | null;
|
|
44
|
+
pricing_mode: string | null;
|
|
45
|
+
variant: string | null;
|
|
46
|
+
variant_raw: string | null;
|
|
47
|
+
resolution: string | null;
|
|
48
|
+
scene: string;
|
|
49
|
+
source: string | null;
|
|
50
|
+
employee_id: number | null;
|
|
51
|
+
dept_id: number | null;
|
|
52
|
+
user_id: number | null;
|
|
53
|
+
app_id: string | null;
|
|
54
|
+
session_id: string | null;
|
|
55
|
+
run_id: number | null;
|
|
56
|
+
workflow_id: number | null;
|
|
57
|
+
step_key: string | null;
|
|
58
|
+
external_task_id: string | null;
|
|
59
|
+
provider_request_id: string | null;
|
|
60
|
+
is_stream: boolean | number | null;
|
|
61
|
+
status: string | null;
|
|
62
|
+
http_status: number | null;
|
|
63
|
+
error_code: string | null;
|
|
64
|
+
error_category: string | null;
|
|
65
|
+
error_message: string | null;
|
|
66
|
+
latency_ms: number | null;
|
|
67
|
+
first_token_ms: number | null;
|
|
68
|
+
request_count: number | null;
|
|
69
|
+
prompt_tokens: number | null;
|
|
70
|
+
completion_tokens: number | null;
|
|
71
|
+
cached_tokens: number | null;
|
|
72
|
+
cache_write_tokens: number | null;
|
|
73
|
+
reasoning_tokens: number | null;
|
|
74
|
+
total_tokens: number | null;
|
|
75
|
+
quantity: number | null;
|
|
76
|
+
requested_quantity: number | null;
|
|
77
|
+
actual_quantity: number | null;
|
|
78
|
+
duration_seconds: NumericValue;
|
|
79
|
+
requested_duration: NumericValue;
|
|
80
|
+
actual_duration: NumericValue;
|
|
81
|
+
duration_source: string | null;
|
|
82
|
+
video_tokens: number | null;
|
|
83
|
+
billing_quantity: NumericValue;
|
|
84
|
+
usage_source: string | null;
|
|
85
|
+
input_price: NumericValue;
|
|
86
|
+
output_price: NumericValue;
|
|
87
|
+
cached_input_price: NumericValue;
|
|
88
|
+
unit_price: NumericValue;
|
|
89
|
+
cost_amount: NumericValue;
|
|
90
|
+
sale_amount: NumericValue;
|
|
91
|
+
currency: string | null;
|
|
92
|
+
price_currency: string | null;
|
|
93
|
+
fx_rate: NumericValue;
|
|
94
|
+
fx_date: string | null;
|
|
95
|
+
pricing_status: string | null;
|
|
96
|
+
price_source: string | null;
|
|
97
|
+
price_id: number | null;
|
|
98
|
+
billable: string | null;
|
|
99
|
+
submitted_at: AnyDate;
|
|
100
|
+
completed_at: AnyDate;
|
|
101
|
+
next_poll_at: AnyDate;
|
|
102
|
+
poll_count: number | null;
|
|
103
|
+
raw_usage: string | null;
|
|
104
|
+
meta: string | null;
|
|
105
|
+
day: string;
|
|
106
|
+
}
|
|
107
|
+
export interface AiModelPriceRow extends TimestampFields {
|
|
108
|
+
id: number;
|
|
109
|
+
provider: string;
|
|
110
|
+
model: string;
|
|
111
|
+
model_type: string | null;
|
|
112
|
+
billing_unit: string | null;
|
|
113
|
+
pricing_mode: string | null;
|
|
114
|
+
unit_size: NumericValue;
|
|
115
|
+
tier_rule: string | null;
|
|
116
|
+
rounding: string | null;
|
|
117
|
+
variant: string;
|
|
118
|
+
currency: string | null;
|
|
119
|
+
cost_input_per_1m: NumericValue;
|
|
120
|
+
cost_output_per_1m: NumericValue;
|
|
121
|
+
cost_cached_input_per_1m: NumericValue;
|
|
122
|
+
cost_unit_price: NumericValue;
|
|
123
|
+
sale_input_per_1m: NumericValue;
|
|
124
|
+
sale_output_per_1m: NumericValue;
|
|
125
|
+
sale_unit_price: NumericValue;
|
|
126
|
+
source: string | null;
|
|
127
|
+
source_url: string | null;
|
|
128
|
+
synced_at: AnyDate;
|
|
129
|
+
effective_from: AnyDate;
|
|
130
|
+
effective_to: AnyDate;
|
|
131
|
+
checksum: string | null;
|
|
132
|
+
status: number | null;
|
|
133
|
+
remark: string | null;
|
|
134
|
+
}
|
|
135
|
+
export interface AiPriceSyncLogRow extends TimestampFields {
|
|
136
|
+
id: number;
|
|
137
|
+
provider: string;
|
|
138
|
+
job_id: string | null;
|
|
139
|
+
started_at: AnyDate;
|
|
140
|
+
finished_at: AnyDate;
|
|
141
|
+
status: string | null;
|
|
142
|
+
changed_count: number | null;
|
|
143
|
+
error: string | null;
|
|
144
|
+
}
|
|
145
|
+
/** 一次 AI 调用的归因上下文 */
|
|
146
|
+
export interface AICallContext {
|
|
147
|
+
scene: string;
|
|
148
|
+
source?: string | null;
|
|
149
|
+
tenant_id?: number | null;
|
|
150
|
+
employee_id?: number | null;
|
|
151
|
+
dept_id?: number | null;
|
|
152
|
+
user_id?: number | null;
|
|
153
|
+
app_id?: string | null;
|
|
154
|
+
session_id?: string | null;
|
|
155
|
+
run_id?: number | null;
|
|
156
|
+
workflow_id?: number | null;
|
|
157
|
+
step_key?: string | null;
|
|
158
|
+
request_id?: string | null;
|
|
159
|
+
parent_request_id?: string | null;
|
|
160
|
+
meta?: Record<string, unknown> | null;
|
|
161
|
+
}
|
|
162
|
+
/** token 用量;source:provider / estimated / partial / none */
|
|
163
|
+
export interface TokenUsage {
|
|
164
|
+
prompt_tokens?: number | null;
|
|
165
|
+
completion_tokens?: number | null;
|
|
166
|
+
cached_tokens?: number | null;
|
|
167
|
+
cache_write_tokens?: number | null;
|
|
168
|
+
reasoning_tokens?: number | null;
|
|
169
|
+
total_tokens?: number | null;
|
|
170
|
+
source?: string | null;
|
|
171
|
+
}
|
|
172
|
+
/** 一次 AI 调用的记账事件(成功 / 失败 / 提交都发) */
|
|
173
|
+
export interface UsageEvent {
|
|
174
|
+
ctx?: Partial<AICallContext> | null;
|
|
175
|
+
provider: string;
|
|
176
|
+
model: string;
|
|
177
|
+
model_type?: string | null;
|
|
178
|
+
billing_unit?: string | null;
|
|
179
|
+
pricing_mode?: string | null;
|
|
180
|
+
variant?: string | null;
|
|
181
|
+
variant_raw?: string | null;
|
|
182
|
+
resolution?: string | null;
|
|
183
|
+
status?: string | null;
|
|
184
|
+
http_status?: number | null;
|
|
185
|
+
error_code?: string | null;
|
|
186
|
+
error_category?: string | null;
|
|
187
|
+
error_message?: string | null;
|
|
188
|
+
latency_ms?: number | null;
|
|
189
|
+
first_token_ms?: number | null;
|
|
190
|
+
is_stream?: boolean | null;
|
|
191
|
+
request_id?: string | null;
|
|
192
|
+
parent_request_id?: string | null;
|
|
193
|
+
attempt_no?: number | null;
|
|
194
|
+
upstream_calls?: number | null;
|
|
195
|
+
usage?: TokenUsage | null;
|
|
196
|
+
quantity?: number | null;
|
|
197
|
+
requested_quantity?: number | null;
|
|
198
|
+
actual_quantity?: number | null;
|
|
199
|
+
duration_seconds?: NumericValue;
|
|
200
|
+
requested_duration?: NumericValue;
|
|
201
|
+
actual_duration?: NumericValue;
|
|
202
|
+
duration_source?: string | null;
|
|
203
|
+
video_tokens?: number | null;
|
|
204
|
+
width?: number | null;
|
|
205
|
+
height?: number | null;
|
|
206
|
+
created_at?: Date | string | null;
|
|
207
|
+
external_task_id?: string | null;
|
|
208
|
+
provider_request_id?: string | null;
|
|
209
|
+
submitted_at?: Date | string | null;
|
|
210
|
+
completed_at?: Date | string | null;
|
|
211
|
+
next_poll_at?: Date | string | null;
|
|
212
|
+
billable?: string | null;
|
|
213
|
+
request_count?: number | null;
|
|
214
|
+
raw_usage?: Record<string, unknown> | null;
|
|
215
|
+
meta?: Record<string, unknown> | null;
|
|
216
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const AI_USAGE_TABLE = "sys_ai_usage";
|
|
2
|
+
export const AI_MODEL_PRICE_TABLE = "sys_ai_model_price";
|
|
3
|
+
export const AI_PRICE_SYNC_LOG_TABLE = "sys_ai_price_sync_log";
|
|
4
|
+
// 计价模式(§15.3 C-2)
|
|
5
|
+
export const PRICING_MODE_TOKEN = "per_token";
|
|
6
|
+
export const PRICING_MODE_IMAGE = "per_image";
|
|
7
|
+
export const PRICING_MODE_SECOND = "per_second";
|
|
8
|
+
export const PRICING_MODE_REQUEST = "per_request";
|
|
9
|
+
export const PRICING_MODE_MEGAPIXEL = "per_megapixel";
|
|
10
|
+
export const PRICING_MODE_VIDEO_TOKEN = "per_video_token";
|
|
11
|
+
// 用量状态(§15.3 C-5)
|
|
12
|
+
export const STATUS_SUBMITTED = "submitted";
|
|
13
|
+
export const STATUS_RUNNING = "running";
|
|
14
|
+
export const STATUS_SUCCESS = "success";
|
|
15
|
+
export const STATUS_FAILED = "failed";
|
|
16
|
+
export const STATUS_PARTIAL = "partial";
|
|
17
|
+
export const STATUS_TIMEOUT = "timeout";
|
|
18
|
+
export const STATUS_CANCELLED = "cancelled";
|
|
19
|
+
// 定价状态
|
|
20
|
+
export const PRICING_PRICED = "priced";
|
|
21
|
+
export const PRICING_MISSING = "missing";
|
|
22
|
+
export const PRICING_FREE = "free";
|
|
23
|
+
export const PRICING_FALLBACK = "fallback";
|
|
24
|
+
export const PRICING_PENDING = "pending";
|
|
25
|
+
//# sourceMappingURL=model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../../../reglow/modules/ai_usage/model.ts"],"names":[],"mappings":"AAUA,MAAM,CAAC,MAAM,cAAc,GAAG,cAAuB,CAAC;AACtD,MAAM,CAAC,MAAM,oBAAoB,GAAG,oBAA6B,CAAC;AAClE,MAAM,CAAC,MAAM,uBAAuB,GAAG,uBAAgC,CAAC;AAExE,kBAAkB;AAClB,MAAM,CAAC,MAAM,kBAAkB,GAAG,WAAW,CAAC;AAC9C,MAAM,CAAC,MAAM,kBAAkB,GAAG,WAAW,CAAC;AAC9C,MAAM,CAAC,MAAM,mBAAmB,GAAG,YAAY,CAAC;AAChD,MAAM,CAAC,MAAM,oBAAoB,GAAG,aAAa,CAAC;AAClD,MAAM,CAAC,MAAM,sBAAsB,GAAG,eAAe,CAAC;AACtD,MAAM,CAAC,MAAM,wBAAwB,GAAG,iBAAiB,CAAC;AAE1D,kBAAkB;AAClB,MAAM,CAAC,MAAM,gBAAgB,GAAG,WAAW,CAAC;AAC5C,MAAM,CAAC,MAAM,cAAc,GAAG,SAAS,CAAC;AACxC,MAAM,CAAC,MAAM,cAAc,GAAG,SAAS,CAAC;AACxC,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC;AACtC,MAAM,CAAC,MAAM,cAAc,GAAG,SAAS,CAAC;AACxC,MAAM,CAAC,MAAM,cAAc,GAAG,SAAS,CAAC;AACxC,MAAM,CAAC,MAAM,gBAAgB,GAAG,WAAW,CAAC;AAE5C,OAAO;AACP,MAAM,CAAC,MAAM,cAAc,GAAG,QAAQ,CAAC;AACvC,MAAM,CAAC,MAAM,eAAe,GAAG,SAAS,CAAC;AACzC,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC;AACnC,MAAM,CAAC,MAAM,gBAAgB,GAAG,UAAU,CAAC;AAC3C,MAAM,CAAC,MAAM,eAAe,GAAG,SAAS,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI 计价:价格匹配、缓存 token、媒体计价模式(§15.3)
|
|
3
|
+
* 对应 backend-python 的 reglow/modules/ai_usage/pricing.py
|
|
4
|
+
*
|
|
5
|
+
* 金额一律 8 位小数四舍五入(ROUND_HALF_UP)。内部用 BigInt 定点运算,
|
|
6
|
+
* 避免浮点误差;输出为 number(MySQL DECIMAL 读入可能是 string,解析时兼容)。
|
|
7
|
+
*/
|
|
8
|
+
import type { Knex } from "knex";
|
|
9
|
+
import { type AiModelPriceRow, type NumericValue } from "./model.js";
|
|
10
|
+
export declare const MONEY_SCALE = 8;
|
|
11
|
+
export declare function peakBucket(at: Date | string | number | null | undefined): "peak" | "offpeak";
|
|
12
|
+
/** 金额保留 8 位小数,四舍五入 */
|
|
13
|
+
export declare function quantizeMoney(value: NumericValue): number;
|
|
14
|
+
export interface PriceSnapshot {
|
|
15
|
+
id: number | null;
|
|
16
|
+
provider: string;
|
|
17
|
+
model: string;
|
|
18
|
+
model_type: string;
|
|
19
|
+
billing_unit: string;
|
|
20
|
+
pricing_mode: string;
|
|
21
|
+
unit_size: NumericValue;
|
|
22
|
+
rounding: string;
|
|
23
|
+
variant: string;
|
|
24
|
+
currency: string;
|
|
25
|
+
input_price: NumericValue;
|
|
26
|
+
output_price: NumericValue;
|
|
27
|
+
cached_input_price: NumericValue;
|
|
28
|
+
unit_price: NumericValue;
|
|
29
|
+
sale_input_price: NumericValue;
|
|
30
|
+
sale_output_price: NumericValue;
|
|
31
|
+
sale_unit_price: NumericValue;
|
|
32
|
+
source: string;
|
|
33
|
+
price_source: string;
|
|
34
|
+
}
|
|
35
|
+
/** 匹配顺序:显式档位 → 空串通用 → 按调用时间选峰谷 → 任意 */
|
|
36
|
+
export declare function pickVariant(rows: AiModelPriceRow[], variant: string | null | undefined, at: Date): AiModelPriceRow | null;
|
|
37
|
+
export interface ResolvePriceParams {
|
|
38
|
+
provider: string;
|
|
39
|
+
model: string;
|
|
40
|
+
model_type: string;
|
|
41
|
+
billing_unit: string;
|
|
42
|
+
pricing_mode: string;
|
|
43
|
+
variant?: string | null;
|
|
44
|
+
/** 峰谷档位按它判断(调用时间) */
|
|
45
|
+
at?: Date | string | number | null;
|
|
46
|
+
/** 价格生效区间按它判断(历史回算);缺省等于 at */
|
|
47
|
+
effective_at?: Date | string | number | null;
|
|
48
|
+
}
|
|
49
|
+
export declare function resolvePrice(db: Knex, params: ResolvePriceParams): Promise<PriceSnapshot | null>;
|
|
50
|
+
export interface TextCostParams {
|
|
51
|
+
prompt_tokens?: number | null;
|
|
52
|
+
completion_tokens?: number | null;
|
|
53
|
+
cached_tokens?: number | null;
|
|
54
|
+
}
|
|
55
|
+
/** 文本 / embedding 成本与售价;缓存价缺失时回退输入价并返回 fallback */
|
|
56
|
+
export declare function calcTextCost(price: PriceSnapshot, params: TextCostParams): [number, number | null, string];
|
|
57
|
+
export interface BillingQuantityParams {
|
|
58
|
+
pricing_mode: string;
|
|
59
|
+
quantity?: number | null;
|
|
60
|
+
duration_seconds?: NumericValue;
|
|
61
|
+
width?: number | null;
|
|
62
|
+
height?: number | null;
|
|
63
|
+
video_tokens?: number | null;
|
|
64
|
+
rounding?: string | null;
|
|
65
|
+
}
|
|
66
|
+
/** 按计价模式计算参与成本的统一数量 */
|
|
67
|
+
export declare function computeBillingQuantity(params: BillingQuantityParams): number;
|
|
68
|
+
/** 图片 / 视频成本与售价 */
|
|
69
|
+
export declare function calcMediaCost(price: PriceSnapshot, billingQuantity: NumericValue): [number, number | null, string];
|
|
70
|
+
/** 写入 sys_ai_usage 的价格快照字段 */
|
|
71
|
+
export declare function priceSnapshotFields(price: PriceSnapshot): {
|
|
72
|
+
price_id: number | null;
|
|
73
|
+
price_source: string;
|
|
74
|
+
price_currency: string;
|
|
75
|
+
pricing_mode: string;
|
|
76
|
+
input_price: NumericValue;
|
|
77
|
+
output_price: NumericValue;
|
|
78
|
+
cached_input_price: NumericValue;
|
|
79
|
+
unit_price: NumericValue;
|
|
80
|
+
};
|