@reglow/reglow 0.13.0 → 0.14.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/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/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/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 +3 -3
|
@@ -0,0 +1,954 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 官方价格同步:HTTP 抓取 + 多格式解析 + append-only 写入。
|
|
3
|
+
* 对应 backend-python 的 reglow/modules/ai_usage/sync.py
|
|
4
|
+
*
|
|
5
|
+
* 已内置各大厂商官方定价页的默认入口与解析方式:
|
|
6
|
+
*
|
|
7
|
+
* - format=html_table:HTML 表格(DeepSeek / 阿里百炼 / 腾讯 TokenHub);
|
|
8
|
+
* - format=markdown:Markdown 管道表格(智谱 .md);
|
|
9
|
+
* - format=mintlify:Mintlify DocTable(Kimi .md);
|
|
10
|
+
* - format=volcano_doc:火山引擎文档接口 MDContent;
|
|
11
|
+
* - format=json / format=csv:公开价格 API / 价格文件。
|
|
12
|
+
*
|
|
13
|
+
* 页面改版或需要登录时可通过环境变量覆盖:
|
|
14
|
+
* AI_USAGE_PRICE_SYNC_{PROVIDER}_URL / _FORMAT / _MODEL_TYPE /
|
|
15
|
+
* _BILLING_UNIT / _PRICING_MODE / _UNIT_SIZE / _CURRENCY / _COLUMNS。
|
|
16
|
+
*/
|
|
17
|
+
import { createHash } from "node:crypto";
|
|
18
|
+
import { AI_MODEL_PRICE_TABLE } from "./model.js";
|
|
19
|
+
// ─────────────── 默认列名 / 官方入口 ───────────────
|
|
20
|
+
export const DEFAULT_COLUMNS = {
|
|
21
|
+
model: ["模型", "模型名称", "模型id", "model", "模型名", "产品名"],
|
|
22
|
+
cost_input_per_1m: ["输入", "输入价格", "输入单价", "input"],
|
|
23
|
+
cost_output_per_1m: ["输出", "输出价格", "输出单价", "output"],
|
|
24
|
+
cost_cached_input_per_1m: ["缓存输入", "缓存命中", "cachedinput", "cached"],
|
|
25
|
+
cost_unit_price: ["单价", "价格", "每张", "每秒", "unitprice", "price"],
|
|
26
|
+
variant: ["分辨率", "尺寸", "规格", "variant", "时段"],
|
|
27
|
+
};
|
|
28
|
+
/** 官方价格页默认入口;页面改版或需要登录时可通过环境变量覆盖。 */
|
|
29
|
+
export const DEFAULT_SOURCES = {
|
|
30
|
+
deepseek: {
|
|
31
|
+
url: "https://api-docs.deepseek.com/zh-cn/quick_start/pricing",
|
|
32
|
+
format: "html_table",
|
|
33
|
+
model_type: "t2t",
|
|
34
|
+
},
|
|
35
|
+
aliyun: {
|
|
36
|
+
url: "https://docs.bailian.console.aliyun.com/zh/model-studio/model-pricing",
|
|
37
|
+
format: "html_table",
|
|
38
|
+
model_type: "t2t",
|
|
39
|
+
},
|
|
40
|
+
volcano: {
|
|
41
|
+
url: "https://docs.volcengine.com/api/doc/getDocDetail" +
|
|
42
|
+
"?LibraryCode=ark&DocumentCode=model-pricing",
|
|
43
|
+
format: "volcano_doc",
|
|
44
|
+
model_type: "t2t",
|
|
45
|
+
},
|
|
46
|
+
zhipu: {
|
|
47
|
+
url: "https://docs.bigmodel.cn/cn/guide/start/pricing.md",
|
|
48
|
+
format: "markdown",
|
|
49
|
+
model_type: "t2t",
|
|
50
|
+
},
|
|
51
|
+
tencent: {
|
|
52
|
+
url: "https://cloud.tencent.com/document/product/1823/130055",
|
|
53
|
+
format: "html_table",
|
|
54
|
+
model_type: "t2t",
|
|
55
|
+
},
|
|
56
|
+
moonshot: {
|
|
57
|
+
url: "https://platform.kimi.com/docs/pricing/chat.md",
|
|
58
|
+
format: "mintlify",
|
|
59
|
+
model_type: "t2t",
|
|
60
|
+
},
|
|
61
|
+
kimi: {
|
|
62
|
+
url: "https://platform.kimi.com/docs/pricing/chat.md",
|
|
63
|
+
format: "mintlify",
|
|
64
|
+
model_type: "t2t",
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
function cloneColumns() {
|
|
68
|
+
const out = {};
|
|
69
|
+
for (const [key, aliases] of Object.entries(DEFAULT_COLUMNS)) {
|
|
70
|
+
out[key] = [...aliases];
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
export function createPriceRow(init) {
|
|
75
|
+
return {
|
|
76
|
+
provider: init.provider,
|
|
77
|
+
model: init.model,
|
|
78
|
+
model_type: init.model_type ?? "t2t",
|
|
79
|
+
billing_unit: init.billing_unit ?? "token",
|
|
80
|
+
pricing_mode: init.pricing_mode ?? "per_token",
|
|
81
|
+
unit_size: init.unit_size ?? 1_000_000,
|
|
82
|
+
variant: init.variant ?? "",
|
|
83
|
+
currency: init.currency ?? "CNY",
|
|
84
|
+
cost_input_per_1m: init.cost_input_per_1m ?? 0,
|
|
85
|
+
cost_output_per_1m: init.cost_output_per_1m ?? 0,
|
|
86
|
+
cost_cached_input_per_1m: init.cost_cached_input_per_1m ?? null,
|
|
87
|
+
cost_unit_price: init.cost_unit_price ?? 0,
|
|
88
|
+
sale_input_per_1m: init.sale_input_per_1m ?? null,
|
|
89
|
+
sale_output_per_1m: init.sale_output_per_1m ?? null,
|
|
90
|
+
sale_unit_price: init.sale_unit_price ?? null,
|
|
91
|
+
source_url: init.source_url ?? "",
|
|
92
|
+
checksum: init.checksum ?? "",
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
export class HttpPriceConfig {
|
|
96
|
+
provider;
|
|
97
|
+
url;
|
|
98
|
+
format;
|
|
99
|
+
model_type;
|
|
100
|
+
billing_unit;
|
|
101
|
+
pricing_mode;
|
|
102
|
+
unit_size;
|
|
103
|
+
currency;
|
|
104
|
+
variant;
|
|
105
|
+
column_map;
|
|
106
|
+
constructor(init) {
|
|
107
|
+
this.provider = init.provider;
|
|
108
|
+
this.url = init.url;
|
|
109
|
+
this.format = init.format ?? "html_table";
|
|
110
|
+
this.model_type = init.model_type ?? "t2t";
|
|
111
|
+
this.billing_unit = init.billing_unit ?? "token";
|
|
112
|
+
this.pricing_mode = init.pricing_mode ?? "per_token";
|
|
113
|
+
this.unit_size = init.unit_size ?? 1_000_000;
|
|
114
|
+
this.currency = init.currency ?? "CNY";
|
|
115
|
+
this.variant = init.variant ?? "";
|
|
116
|
+
this.column_map = init.column_map ? { ...init.column_map } : cloneColumns();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// ─────────────── 文本 / 列名工具 ───────────────
|
|
120
|
+
function asText(value) {
|
|
121
|
+
if (value === null || value === undefined || value === false || value === 0) {
|
|
122
|
+
return "";
|
|
123
|
+
}
|
|
124
|
+
return String(value);
|
|
125
|
+
}
|
|
126
|
+
/** 去空白、括号、标点、币种符号再小写,用于列名模糊匹配 */
|
|
127
|
+
export function norm(text) {
|
|
128
|
+
return (text ?? "").toLowerCase().replace(/[\s()()【】[\]::、/¥¥$元,,.·-]/g, "");
|
|
129
|
+
}
|
|
130
|
+
function find(item, aliases) {
|
|
131
|
+
for (const [key, value] of Object.entries(item)) {
|
|
132
|
+
const keyN = norm(key);
|
|
133
|
+
if (aliases.some((alias) => norm(alias) && keyN.includes(norm(alias)))) {
|
|
134
|
+
return asText(value).trim();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return "";
|
|
138
|
+
}
|
|
139
|
+
function findBy(item, predicate) {
|
|
140
|
+
for (const [key, value] of Object.entries(item)) {
|
|
141
|
+
if (predicate(norm(key))) {
|
|
142
|
+
const text = asText(value).trim();
|
|
143
|
+
if (text)
|
|
144
|
+
return text;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return "";
|
|
148
|
+
}
|
|
149
|
+
function looksLikeInput(key) {
|
|
150
|
+
if (!key.includes("输入") && !key.includes("input"))
|
|
151
|
+
return false;
|
|
152
|
+
if (["条件", "长度", "token数", "请求", "模式", "模态", "写入", "存储"].some((word) => key.includes(word))) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
if (key.includes("未命中") || key.includes("miss"))
|
|
156
|
+
return true;
|
|
157
|
+
return !["命中", "hit", "cached", "write"].some((word) => key.includes(word));
|
|
158
|
+
}
|
|
159
|
+
function looksLikeOutput(key) {
|
|
160
|
+
if (!key.includes("输出") && !key.includes("output"))
|
|
161
|
+
return false;
|
|
162
|
+
return !["条件", "长度", "模态", "token数"].some((word) => key.includes(word));
|
|
163
|
+
}
|
|
164
|
+
function looksLikeCached(key) {
|
|
165
|
+
if (key === "缓存输入")
|
|
166
|
+
return true;
|
|
167
|
+
if (["缓存命中", "cachehit", "cachedinput"].some((word) => key.includes(word))) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
return key.includes("cached") && key.includes("input");
|
|
171
|
+
}
|
|
172
|
+
/** 优先取带明确单位且含币种的价格,如「0.15 元/张」「0.375 元/秒」 */
|
|
173
|
+
function findUnitPrice(item) {
|
|
174
|
+
const pattern = /(\/\s*张|每张|\/\s*秒|每秒|\/\s*次|每次|\/\s*mp|百万像素|\/image|\/second|\/request)/i;
|
|
175
|
+
const currency = /(元|¥|¥|\$|usd|cny)/i;
|
|
176
|
+
const candidates = [];
|
|
177
|
+
for (const value of Object.values(item)) {
|
|
178
|
+
const text = asText(value).trim();
|
|
179
|
+
if (text && pattern.test(text)) {
|
|
180
|
+
if (currency.test(text))
|
|
181
|
+
return text;
|
|
182
|
+
candidates.push(text);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return candidates[0] ?? "";
|
|
186
|
+
}
|
|
187
|
+
function hasMillionTokenPrice(item) {
|
|
188
|
+
for (const key of Object.keys(item)) {
|
|
189
|
+
const keyN = norm(key);
|
|
190
|
+
if ((keyN.includes("百万") || keyN.includes("1m")) && keyN.includes("token")) {
|
|
191
|
+
if (["单价", "价格", "price"].some((word) => keyN.includes(word))) {
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
/** 解析价格文本,返回 [数值, 币种, 计价单位];token 价格统一为每 1M */
|
|
199
|
+
export function parsePrice(text) {
|
|
200
|
+
const raw = (text ?? "").trim();
|
|
201
|
+
if (!raw)
|
|
202
|
+
return [0, null, null];
|
|
203
|
+
const lower = raw.toLowerCase();
|
|
204
|
+
let currency = null;
|
|
205
|
+
if (raw.includes("$") || lower.includes("usd")) {
|
|
206
|
+
currency = "USD";
|
|
207
|
+
}
|
|
208
|
+
else if (raw.includes("¥") ||
|
|
209
|
+
raw.includes("¥") ||
|
|
210
|
+
lower.includes("cny") ||
|
|
211
|
+
raw.includes("元")) {
|
|
212
|
+
currency = "CNY";
|
|
213
|
+
}
|
|
214
|
+
let unit = null;
|
|
215
|
+
if (["/张", "每张", "/image", "per image"].some((key) => lower.includes(key))) {
|
|
216
|
+
unit = "image";
|
|
217
|
+
}
|
|
218
|
+
else if (["/秒", "每秒", "/second", "per second"].some((key) => lower.includes(key))) {
|
|
219
|
+
unit = "second";
|
|
220
|
+
}
|
|
221
|
+
else if (["/mp", "每mp", "百万像素", "megapixel"].some((key) => lower.includes(key))) {
|
|
222
|
+
unit = "megapixel";
|
|
223
|
+
}
|
|
224
|
+
else if (["/次", "每次", "/request", "per request"].some((key) => lower.includes(key))) {
|
|
225
|
+
unit = "request";
|
|
226
|
+
}
|
|
227
|
+
const cleaned = raw.replace(/,/g, "");
|
|
228
|
+
// 优先取紧邻币种符号的数字,避免把「480P」「15000 tokens/张」里的数字当价格
|
|
229
|
+
const match = /([0-9]+(?:\.[0-9]+)?)\s*(?:元|¥|¥|\$|usd|cny)/i.exec(cleaned) ??
|
|
230
|
+
/(?:元|¥|¥|\$|usd|cny)\s*([0-9]+(?:\.[0-9]+)?)/i.exec(cleaned) ??
|
|
231
|
+
/([0-9]+(?:\.[0-9]+)?)/.exec(cleaned);
|
|
232
|
+
if (!match)
|
|
233
|
+
return [0, currency, unit];
|
|
234
|
+
let value = Number(match[1]);
|
|
235
|
+
// token 价格:/千、/1K 转成每 1M;/百万、/1M 保持
|
|
236
|
+
if (unit === null) {
|
|
237
|
+
if (["/千", "/1k", "千token", "per 1k"].some((key) => lower.includes(key))) {
|
|
238
|
+
value *= 1000;
|
|
239
|
+
}
|
|
240
|
+
else if (["/百万", "/1m", "百万token", "per 1m"].some((key) => lower.includes(key))) {
|
|
241
|
+
value *= 1;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return [value, currency, unit];
|
|
245
|
+
}
|
|
246
|
+
/** 无有效价格(-、不支持等)返回 null */
|
|
247
|
+
export function parsePriceOrNone(text) {
|
|
248
|
+
const raw = (text ?? "").trim();
|
|
249
|
+
if (["", "-", "—", "–", "N/A", "n/a", "不支持", "暂无"].includes(raw)) {
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
return parsePrice(raw)[0];
|
|
253
|
+
}
|
|
254
|
+
/** 清理文档表格里的模型名:脚注、转义符和中文补充说明 */
|
|
255
|
+
export function cleanModelName(text) {
|
|
256
|
+
let cleaned = (text ?? "")
|
|
257
|
+
.trim()
|
|
258
|
+
.replace(/\\-/g, "-")
|
|
259
|
+
.replace(/\\_/g, "_");
|
|
260
|
+
cleaned = cleaned.replace(/[((]\d+[))]/g, "").trim();
|
|
261
|
+
// 阿里云等页面会在模型 ID 后追加中文说明,如「qwen3-max Batch调用半价」
|
|
262
|
+
const parts = cleaned.split(/\s+/);
|
|
263
|
+
if (parts.length > 1 &&
|
|
264
|
+
parts.slice(1).some((part) => /[\u4e00-\u9fff]/.test(part))) {
|
|
265
|
+
return parts[0];
|
|
266
|
+
}
|
|
267
|
+
return cleaned;
|
|
268
|
+
}
|
|
269
|
+
// ─────────────── HTML 表格 ───────────────
|
|
270
|
+
function decodeEntities(text) {
|
|
271
|
+
return text
|
|
272
|
+
.replace(/ /gi, " ")
|
|
273
|
+
.replace(/</gi, "<")
|
|
274
|
+
.replace(/>/gi, ">")
|
|
275
|
+
.replace(/"/gi, '"')
|
|
276
|
+
.replace(/'/g, "'")
|
|
277
|
+
.replace(/&/gi, "&")
|
|
278
|
+
.replace(/&#(\d+);/g, (_, code) => String.fromCodePoint(Number(code)));
|
|
279
|
+
}
|
|
280
|
+
/** 提取 HTML 中所有表格,返回 [行[单元格]]。对应 Python _TableParser */
|
|
281
|
+
function extractTables(html) {
|
|
282
|
+
const tables = [];
|
|
283
|
+
let depth = 0;
|
|
284
|
+
let rows = null;
|
|
285
|
+
let row = null;
|
|
286
|
+
let cell = null;
|
|
287
|
+
const pushText = (text) => {
|
|
288
|
+
if (cell)
|
|
289
|
+
cell.push(decodeEntities(text));
|
|
290
|
+
};
|
|
291
|
+
const tagRe = /<(\/?)([a-zA-Z][\w:-]*)([^>]*?)(\/?)>/g;
|
|
292
|
+
let lastIndex = 0;
|
|
293
|
+
let match;
|
|
294
|
+
while ((match = tagRe.exec(html))) {
|
|
295
|
+
pushText(html.slice(lastIndex, match.index));
|
|
296
|
+
lastIndex = tagRe.lastIndex;
|
|
297
|
+
const closing = match[1] === "/";
|
|
298
|
+
const tag = match[2].toLowerCase();
|
|
299
|
+
if (tag === "table") {
|
|
300
|
+
if (!closing) {
|
|
301
|
+
depth += 1;
|
|
302
|
+
if (depth === 1)
|
|
303
|
+
rows = [];
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
if (depth === 1 && rows) {
|
|
307
|
+
tables.push(rows);
|
|
308
|
+
rows = null;
|
|
309
|
+
}
|
|
310
|
+
depth = Math.max(0, depth - 1);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
else if (depth === 1 && tag === "tr") {
|
|
314
|
+
if (closing) {
|
|
315
|
+
if (row && rows && row.length)
|
|
316
|
+
rows.push(row);
|
|
317
|
+
row = null;
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
row = [];
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
else if (depth === 1 && (tag === "td" || tag === "th")) {
|
|
324
|
+
if (closing) {
|
|
325
|
+
if (row) {
|
|
326
|
+
const text = (cell ?? [])
|
|
327
|
+
.join("")
|
|
328
|
+
.split(/\s+/)
|
|
329
|
+
.filter(Boolean)
|
|
330
|
+
.join(" ");
|
|
331
|
+
row.push(text);
|
|
332
|
+
}
|
|
333
|
+
cell = null;
|
|
334
|
+
}
|
|
335
|
+
else if (row) {
|
|
336
|
+
cell = [];
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
else if (tag === "br" && cell) {
|
|
340
|
+
cell.push(" ");
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return tables;
|
|
344
|
+
}
|
|
345
|
+
/** 解析「模型横向排列、价格纵向排列」的官方价格表(DeepSeek 等) */
|
|
346
|
+
function parseTransposedTable(table) {
|
|
347
|
+
if (!table.length || table[0].length < 3)
|
|
348
|
+
return [];
|
|
349
|
+
const firstHeader = norm(table[0][0]);
|
|
350
|
+
if (firstHeader !== "model" && firstHeader !== "模型")
|
|
351
|
+
return [];
|
|
352
|
+
const models = table[0].slice(1).map((cell) => cleanModelName(cell));
|
|
353
|
+
const peak = new Map();
|
|
354
|
+
const offPeak = new Map();
|
|
355
|
+
let current = "";
|
|
356
|
+
for (const row of table.slice(1)) {
|
|
357
|
+
if (!row.length)
|
|
358
|
+
continue;
|
|
359
|
+
const joined = row.map((cell) => norm(cell)).join(" ");
|
|
360
|
+
if (joined.includes("token") || joined.includes("tokens")) {
|
|
361
|
+
if (joined.includes("cachehit") || joined.includes("缓存命中")) {
|
|
362
|
+
current = "cached";
|
|
363
|
+
}
|
|
364
|
+
else if (joined.includes("cachemiss") || joined.includes("缓存未命中")) {
|
|
365
|
+
current = "input";
|
|
366
|
+
}
|
|
367
|
+
else if (joined.includes("outputtoken") || joined.includes("输出")) {
|
|
368
|
+
current = "output";
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
if (!current)
|
|
372
|
+
continue;
|
|
373
|
+
let tierIndex = -1;
|
|
374
|
+
for (let index = 0; index < row.length; index++) {
|
|
375
|
+
const cell = norm(row[index]);
|
|
376
|
+
if (["offpeak", "peak", "空闲时段", "高峰时段"].includes(cell)) {
|
|
377
|
+
tierIndex = index;
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (tierIndex < 0) {
|
|
382
|
+
// 区块行直接跟价格(没有时段子标签)
|
|
383
|
+
if (norm(row[0]).includes("token") || norm(row[0]).includes("tokens")) {
|
|
384
|
+
if (!peak.has(current))
|
|
385
|
+
peak.set(current, row.slice(1));
|
|
386
|
+
}
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
const values = row.slice(tierIndex + 1);
|
|
390
|
+
if (["offpeak", "空闲时段"].includes(norm(row[tierIndex]))) {
|
|
391
|
+
offPeak.set(current, values);
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
peak.set(current, values);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
for (const [key, values] of offPeak) {
|
|
398
|
+
if (!peak.has(key))
|
|
399
|
+
peak.set(key, values);
|
|
400
|
+
}
|
|
401
|
+
const items = [];
|
|
402
|
+
models.forEach((model, index) => {
|
|
403
|
+
if (!model)
|
|
404
|
+
return;
|
|
405
|
+
const value = (key, values) => index < values.length ? values[index].trim() : "";
|
|
406
|
+
const variants = [
|
|
407
|
+
["高峰", peak],
|
|
408
|
+
["空闲", offPeak],
|
|
409
|
+
];
|
|
410
|
+
for (const [variant, prices] of variants) {
|
|
411
|
+
const item = { 模型: model, 时段: variant };
|
|
412
|
+
for (const [key, label] of [
|
|
413
|
+
["input", "输入"],
|
|
414
|
+
["output", "输出"],
|
|
415
|
+
["cached", "缓存输入"],
|
|
416
|
+
]) {
|
|
417
|
+
const cell = value(key, prices.get(key) ?? []);
|
|
418
|
+
if (cell)
|
|
419
|
+
item[label] = cell;
|
|
420
|
+
}
|
|
421
|
+
if (Object.keys(item).length > 2)
|
|
422
|
+
items.push(item);
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
return items;
|
|
426
|
+
}
|
|
427
|
+
export function parseHtmlTable(html) {
|
|
428
|
+
const tables = extractTables(html);
|
|
429
|
+
// 优先识别「横向模型、纵向价格」的表(如 DeepSeek 官方定价页)
|
|
430
|
+
for (const table of tables) {
|
|
431
|
+
const rows = parseTransposedTable(table);
|
|
432
|
+
if (rows.length)
|
|
433
|
+
return rows;
|
|
434
|
+
}
|
|
435
|
+
const rows = [];
|
|
436
|
+
for (const table of tables) {
|
|
437
|
+
if (table.length < 2)
|
|
438
|
+
continue;
|
|
439
|
+
const header = table[0];
|
|
440
|
+
// 表头必须包含价格类字段,避免把「免费额度」等说明表当成价格表
|
|
441
|
+
const priceWords = [
|
|
442
|
+
"输入",
|
|
443
|
+
"输出",
|
|
444
|
+
"价格",
|
|
445
|
+
"单价",
|
|
446
|
+
"刊例价",
|
|
447
|
+
"计费",
|
|
448
|
+
"费用",
|
|
449
|
+
"price",
|
|
450
|
+
"cost",
|
|
451
|
+
];
|
|
452
|
+
const hasPriceHeader = header.some((cell) => {
|
|
453
|
+
const cellN = norm(cell);
|
|
454
|
+
return priceWords.some((word) => cellN.includes(norm(word)));
|
|
455
|
+
});
|
|
456
|
+
if (!hasPriceHeader)
|
|
457
|
+
continue;
|
|
458
|
+
for (const cells of table.slice(1)) {
|
|
459
|
+
if (cells.length < 2)
|
|
460
|
+
continue;
|
|
461
|
+
const item = {};
|
|
462
|
+
for (let i = 0; i < Math.min(header.length, cells.length); i++) {
|
|
463
|
+
item[header[i]] = cells[i];
|
|
464
|
+
}
|
|
465
|
+
rows.push(item);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return rows;
|
|
469
|
+
}
|
|
470
|
+
// ─────────────── Markdown / Mintlify ───────────────
|
|
471
|
+
const ESCAPED_PIPE = "\u0000PIPE\u0000";
|
|
472
|
+
function mdCells(line) {
|
|
473
|
+
let text = line.trim();
|
|
474
|
+
if (text.startsWith("|"))
|
|
475
|
+
text = text.slice(1);
|
|
476
|
+
if (text.endsWith("|"))
|
|
477
|
+
text = text.slice(0, -1);
|
|
478
|
+
text = text.replace(/\\\|/g, ESCAPED_PIPE);
|
|
479
|
+
return text
|
|
480
|
+
.split("|")
|
|
481
|
+
.map((cell) => cell.trim().replaceAll(ESCAPED_PIPE, "|"))
|
|
482
|
+
.map((cell) => cell.replace(/<br\s*\/?>/gi, " ").trim());
|
|
483
|
+
}
|
|
484
|
+
/** 解析 Markdown 管道表格(智谱 .md、火山引擎 MDContent 等) */
|
|
485
|
+
export function parseMarkdownTables(text) {
|
|
486
|
+
const rows = [];
|
|
487
|
+
let block = [];
|
|
488
|
+
const isSeparator = (line) => {
|
|
489
|
+
const cells = mdCells(line);
|
|
490
|
+
return (cells.length > 0 &&
|
|
491
|
+
cells.every((cell) => !!cell && /^:?-+:?$/.test(cell.replace(/ /g, ""))));
|
|
492
|
+
};
|
|
493
|
+
const flush = () => {
|
|
494
|
+
if (block.length < 3 || !isSeparator(block[1]))
|
|
495
|
+
return;
|
|
496
|
+
const header = mdCells(block[0]);
|
|
497
|
+
let lastFirst = "";
|
|
498
|
+
for (const line of block.slice(2)) {
|
|
499
|
+
const cells = mdCells(line);
|
|
500
|
+
if (!cells.length || cells.every((cell) => !cell))
|
|
501
|
+
continue;
|
|
502
|
+
if (!cells[0] && lastFirst)
|
|
503
|
+
cells[0] = lastFirst;
|
|
504
|
+
lastFirst = cells[0] || lastFirst;
|
|
505
|
+
const item = {};
|
|
506
|
+
for (let i = 0; i < Math.min(header.length, cells.length); i++) {
|
|
507
|
+
item[header[i]] = cells[i];
|
|
508
|
+
}
|
|
509
|
+
rows.push(item);
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
for (const line of [...text.split(/\r?\n/), ""]) {
|
|
513
|
+
if (line.trim().startsWith("|")) {
|
|
514
|
+
block.push(line);
|
|
515
|
+
}
|
|
516
|
+
else {
|
|
517
|
+
flush();
|
|
518
|
+
block = [];
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return rows;
|
|
522
|
+
}
|
|
523
|
+
/** 解析 Mintlify 文档里的 <DocTable columns={[...]} rows={[[...]]} /> 结构 */
|
|
524
|
+
export function parseMintlifyTables(text) {
|
|
525
|
+
const rows = [];
|
|
526
|
+
for (const match of text.matchAll(/<DocTable([\s\S]*?)\/>/g)) {
|
|
527
|
+
const block = match[1];
|
|
528
|
+
const titles = [...block.matchAll(/title:\s*"([^"]+)"/g)].map((m) => m[1]);
|
|
529
|
+
const rowsMatch = /rows=\{\[([\s\S]*?)\]\s*\}/.exec(block);
|
|
530
|
+
if (!titles.length || !rowsMatch)
|
|
531
|
+
continue;
|
|
532
|
+
for (const rowMatch of rowsMatch[1].matchAll(/\[([\s\S]*?)\]/g)) {
|
|
533
|
+
const values = [...rowMatch[1].matchAll(/"([^"]*)"/g)].map((m) => m[1]);
|
|
534
|
+
if (values.length < 2)
|
|
535
|
+
continue;
|
|
536
|
+
const item = {};
|
|
537
|
+
for (let i = 0; i < Math.min(titles.length, values.length); i++) {
|
|
538
|
+
item[titles[i]] = values[i];
|
|
539
|
+
}
|
|
540
|
+
rows.push(item);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return rows;
|
|
544
|
+
}
|
|
545
|
+
// ─────────────── JSON / CSV ───────────────
|
|
546
|
+
export function parseJsonRows(data) {
|
|
547
|
+
if (Array.isArray(data)) {
|
|
548
|
+
return data.filter((item) => !!item && typeof item === "object" && !Array.isArray(item));
|
|
549
|
+
}
|
|
550
|
+
if (data && typeof data === "object") {
|
|
551
|
+
const obj = data;
|
|
552
|
+
for (const key of ["data", "prices", "items", "models", "result"]) {
|
|
553
|
+
const value = obj[key];
|
|
554
|
+
if (Array.isArray(value)) {
|
|
555
|
+
return value.filter((item) => !!item && typeof item === "object" && !Array.isArray(item));
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return [];
|
|
560
|
+
}
|
|
561
|
+
function parseCsv(text) {
|
|
562
|
+
const rows = [];
|
|
563
|
+
let row = [];
|
|
564
|
+
let field = "";
|
|
565
|
+
let inQuotes = false;
|
|
566
|
+
for (let i = 0; i < text.length; i++) {
|
|
567
|
+
const ch = text[i];
|
|
568
|
+
if (inQuotes) {
|
|
569
|
+
if (ch === '"') {
|
|
570
|
+
if (text[i + 1] === '"') {
|
|
571
|
+
field += '"';
|
|
572
|
+
i++;
|
|
573
|
+
}
|
|
574
|
+
else {
|
|
575
|
+
inQuotes = false;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
field += ch;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
else if (ch === '"') {
|
|
583
|
+
inQuotes = true;
|
|
584
|
+
}
|
|
585
|
+
else if (ch === ",") {
|
|
586
|
+
row.push(field);
|
|
587
|
+
field = "";
|
|
588
|
+
}
|
|
589
|
+
else if (ch === "\n") {
|
|
590
|
+
row.push(field);
|
|
591
|
+
rows.push(row);
|
|
592
|
+
row = [];
|
|
593
|
+
field = "";
|
|
594
|
+
}
|
|
595
|
+
else if (ch !== "\r") {
|
|
596
|
+
field += ch;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
if (field !== "" || row.length) {
|
|
600
|
+
row.push(field);
|
|
601
|
+
rows.push(row);
|
|
602
|
+
}
|
|
603
|
+
return rows;
|
|
604
|
+
}
|
|
605
|
+
function parseCsvItems(text) {
|
|
606
|
+
const rows = parseCsv(text);
|
|
607
|
+
if (!rows.length)
|
|
608
|
+
return [];
|
|
609
|
+
const header = rows[0];
|
|
610
|
+
return rows
|
|
611
|
+
.slice(1)
|
|
612
|
+
.filter((cells) => cells.length && cells.some((cell) => cell.trim() !== ""))
|
|
613
|
+
.map((cells) => {
|
|
614
|
+
const item = {};
|
|
615
|
+
header.forEach((name, index) => {
|
|
616
|
+
item[name] = cells[index] ?? "";
|
|
617
|
+
});
|
|
618
|
+
return item;
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
const defaultFetch = (url) => fetch(url, {
|
|
622
|
+
redirect: "follow",
|
|
623
|
+
signal: AbortSignal.timeout(30_000),
|
|
624
|
+
});
|
|
625
|
+
export class HttpPriceAdapter {
|
|
626
|
+
fetcher;
|
|
627
|
+
config;
|
|
628
|
+
constructor(config, fetcher = defaultFetch) {
|
|
629
|
+
this.fetcher = fetcher;
|
|
630
|
+
this.config = config;
|
|
631
|
+
}
|
|
632
|
+
async fetch() {
|
|
633
|
+
const resp = await this.fetcher(this.config.url);
|
|
634
|
+
if (!resp.ok)
|
|
635
|
+
throw new Error(`HTTP ${resp.status}`);
|
|
636
|
+
let items;
|
|
637
|
+
if (this.config.format === "json") {
|
|
638
|
+
items = parseJsonRows(await resp.json());
|
|
639
|
+
}
|
|
640
|
+
else if (this.config.format === "csv") {
|
|
641
|
+
items = parseCsvItems(await resp.text());
|
|
642
|
+
}
|
|
643
|
+
else if (this.config.format === "markdown") {
|
|
644
|
+
items = parseMarkdownTables(await resp.text());
|
|
645
|
+
}
|
|
646
|
+
else if (this.config.format === "mintlify") {
|
|
647
|
+
items = parseMintlifyTables(await resp.text());
|
|
648
|
+
}
|
|
649
|
+
else if (this.config.format === "volcano_doc") {
|
|
650
|
+
const payload = (await resp.json());
|
|
651
|
+
const content = asText(payload?.Result?.MDContent);
|
|
652
|
+
items = parseMarkdownTables(content);
|
|
653
|
+
}
|
|
654
|
+
else {
|
|
655
|
+
items = parseHtmlTable(await resp.text());
|
|
656
|
+
}
|
|
657
|
+
const rows = [];
|
|
658
|
+
const seen = new Set();
|
|
659
|
+
for (const item of items) {
|
|
660
|
+
const row = this.toRow(item);
|
|
661
|
+
if (!row)
|
|
662
|
+
continue;
|
|
663
|
+
// 同一模型的同一档位只保留第一条
|
|
664
|
+
const key = `${row.model}\u0000${row.variant}`;
|
|
665
|
+
if (seen.has(key))
|
|
666
|
+
continue;
|
|
667
|
+
seen.add(key);
|
|
668
|
+
rows.push(row);
|
|
669
|
+
}
|
|
670
|
+
return rows;
|
|
671
|
+
}
|
|
672
|
+
toRow(item) {
|
|
673
|
+
const column = (key) => this.config.column_map[key] ?? DEFAULT_COLUMNS[key];
|
|
674
|
+
const model = cleanModelName(find(item, column("model")));
|
|
675
|
+
if (!model)
|
|
676
|
+
return null;
|
|
677
|
+
let inputText = findBy(item, looksLikeInput) || find(item, column("cost_input_per_1m"));
|
|
678
|
+
let outputText = findBy(item, looksLikeOutput) || find(item, column("cost_output_per_1m"));
|
|
679
|
+
const cachedText = findBy(item, looksLikeCached) || find(item, column("cost_cached_input_per_1m"));
|
|
680
|
+
const unitText = findUnitPrice(item) || find(item, column("cost_unit_price"));
|
|
681
|
+
const variant = find(item, column("variant"));
|
|
682
|
+
// 部分厂商把输入/输出价写在同一列,如「输入:0.5元输出:2元」
|
|
683
|
+
if (!inputText && !outputText) {
|
|
684
|
+
const combined = find(item, ["刊例价", "计费价", "价格", "price"]);
|
|
685
|
+
if (combined) {
|
|
686
|
+
const mark = combined.includes("元")
|
|
687
|
+
? "元"
|
|
688
|
+
: combined.includes("$")
|
|
689
|
+
? "$"
|
|
690
|
+
: "";
|
|
691
|
+
const matchIn = /输入[::]\s*([0-9]+(?:\.[0-9]+)?)/.exec(combined);
|
|
692
|
+
const matchOut = /输出[::]\s*([0-9]+(?:\.[0-9]+)?)/.exec(combined);
|
|
693
|
+
if (matchIn)
|
|
694
|
+
inputText = matchIn[1] + mark;
|
|
695
|
+
if (matchOut)
|
|
696
|
+
outputText = matchOut[1] + mark;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
let billingUnit = this.config.billing_unit;
|
|
700
|
+
let pricingMode = this.config.pricing_mode;
|
|
701
|
+
let unitSize = this.config.unit_size;
|
|
702
|
+
let currency = this.config.currency;
|
|
703
|
+
// JSON/CSV 可直接携带规范化字段
|
|
704
|
+
const rawBilling = find(item, ["billing_unit", "计费单位"]);
|
|
705
|
+
const rawMode = find(item, ["pricing_mode", "计价模式"]);
|
|
706
|
+
const rawUnitSize = find(item, ["unit_size", "计价单位大小"]);
|
|
707
|
+
const rawCurrency = find(item, ["currency", "币种"]);
|
|
708
|
+
if (rawBilling)
|
|
709
|
+
billingUnit = rawBilling;
|
|
710
|
+
if (rawMode)
|
|
711
|
+
pricingMode = rawMode;
|
|
712
|
+
if (rawUnitSize) {
|
|
713
|
+
const n = Number(rawUnitSize);
|
|
714
|
+
if (Number.isFinite(n))
|
|
715
|
+
unitSize = n;
|
|
716
|
+
}
|
|
717
|
+
if (rawCurrency)
|
|
718
|
+
currency = rawCurrency;
|
|
719
|
+
let costInput = 0;
|
|
720
|
+
let costOutput = 0;
|
|
721
|
+
let costCached = null;
|
|
722
|
+
let costUnit = 0;
|
|
723
|
+
if (inputText || outputText) {
|
|
724
|
+
const [input, curIn] = parsePrice(inputText);
|
|
725
|
+
const [output, curOut] = parsePrice(outputText);
|
|
726
|
+
costInput = input;
|
|
727
|
+
costOutput = output;
|
|
728
|
+
currency = curIn || curOut || currency;
|
|
729
|
+
if (cachedText)
|
|
730
|
+
costCached = parsePriceOrNone(cachedText);
|
|
731
|
+
billingUnit = "token";
|
|
732
|
+
pricingMode = "per_token";
|
|
733
|
+
unitSize = 1_000_000;
|
|
734
|
+
}
|
|
735
|
+
else if (unitText) {
|
|
736
|
+
const [unitValue, cur, unit] = parsePrice(unitText);
|
|
737
|
+
costUnit = unitValue;
|
|
738
|
+
currency = cur || currency;
|
|
739
|
+
if (unit === "image") {
|
|
740
|
+
billingUnit = "image";
|
|
741
|
+
pricingMode = "per_image";
|
|
742
|
+
unitSize = 1;
|
|
743
|
+
}
|
|
744
|
+
else if (unit === "second") {
|
|
745
|
+
billingUnit = "second";
|
|
746
|
+
pricingMode = "per_second";
|
|
747
|
+
unitSize = 1;
|
|
748
|
+
}
|
|
749
|
+
else if (unit === "megapixel") {
|
|
750
|
+
billingUnit = "image";
|
|
751
|
+
pricingMode = "per_megapixel";
|
|
752
|
+
unitSize = 1;
|
|
753
|
+
}
|
|
754
|
+
else if (unit === "request") {
|
|
755
|
+
billingUnit = "request";
|
|
756
|
+
pricingMode = "per_request";
|
|
757
|
+
unitSize = 1;
|
|
758
|
+
}
|
|
759
|
+
else if (unit === null && hasMillionTokenPrice(item)) {
|
|
760
|
+
// 向量化 / 重排序等只有一列「单价(元/百万 Tokens)」
|
|
761
|
+
costInput = costUnit;
|
|
762
|
+
billingUnit = "token";
|
|
763
|
+
pricingMode = "per_token";
|
|
764
|
+
unitSize = 1_000_000;
|
|
765
|
+
}
|
|
766
|
+
else if (unit === null && !rawMode) {
|
|
767
|
+
// 无法识别计价单位(如存储 / 容量 / kTPM),跳过
|
|
768
|
+
return null;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
else {
|
|
772
|
+
return null;
|
|
773
|
+
}
|
|
774
|
+
let modelType = this.config.model_type;
|
|
775
|
+
const modelLower = model.toLowerCase();
|
|
776
|
+
if (pricingMode === "per_image" || pricingMode === "per_megapixel") {
|
|
777
|
+
modelType = "t2i";
|
|
778
|
+
}
|
|
779
|
+
else if (pricingMode === "per_second") {
|
|
780
|
+
modelType = "t2v";
|
|
781
|
+
}
|
|
782
|
+
else if (modelLower.includes("embedding") || modelLower.includes("rerank")) {
|
|
783
|
+
modelType = "embedding";
|
|
784
|
+
}
|
|
785
|
+
return createPriceRow({
|
|
786
|
+
provider: this.config.provider,
|
|
787
|
+
model,
|
|
788
|
+
model_type: modelType,
|
|
789
|
+
billing_unit: billingUnit,
|
|
790
|
+
pricing_mode: pricingMode,
|
|
791
|
+
unit_size: unitSize,
|
|
792
|
+
variant: variant || this.config.variant,
|
|
793
|
+
currency,
|
|
794
|
+
cost_input_per_1m: costInput,
|
|
795
|
+
cost_output_per_1m: costOutput,
|
|
796
|
+
cost_cached_input_per_1m: costCached,
|
|
797
|
+
cost_unit_price: costUnit,
|
|
798
|
+
source_url: this.config.url,
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
async sync(db) {
|
|
802
|
+
const rows = await this.fetch();
|
|
803
|
+
let changed = 0;
|
|
804
|
+
const now = new Date();
|
|
805
|
+
const seen = new Set();
|
|
806
|
+
for (const row of rows) {
|
|
807
|
+
row.checksum = createHash("sha256")
|
|
808
|
+
.update(stableStringify(row))
|
|
809
|
+
.digest("hex");
|
|
810
|
+
seen.add([
|
|
811
|
+
row.model,
|
|
812
|
+
row.model_type,
|
|
813
|
+
row.billing_unit,
|
|
814
|
+
row.pricing_mode,
|
|
815
|
+
row.variant || "",
|
|
816
|
+
].join("\u0000"));
|
|
817
|
+
if (await upsertRow(db, row, now))
|
|
818
|
+
changed += 1;
|
|
819
|
+
}
|
|
820
|
+
// 关闭本次未出现的旧官方价格(解析失败时不清理,避免误删)
|
|
821
|
+
if (rows.length) {
|
|
822
|
+
const stale = (await db(AI_MODEL_PRICE_TABLE)
|
|
823
|
+
.where({
|
|
824
|
+
provider: this.config.provider,
|
|
825
|
+
source: "official",
|
|
826
|
+
status: 1,
|
|
827
|
+
})
|
|
828
|
+
.andWhere((qb) => qb.whereNull("effective_to").orWhere("effective_to", ">", now)));
|
|
829
|
+
for (const item of stale) {
|
|
830
|
+
const key = [
|
|
831
|
+
asText(item.model),
|
|
832
|
+
asText(item.model_type) || "t2t",
|
|
833
|
+
asText(item.billing_unit) || "token",
|
|
834
|
+
asText(item.pricing_mode) || "per_token",
|
|
835
|
+
asText(item.variant),
|
|
836
|
+
].join("\u0000");
|
|
837
|
+
if (!seen.has(key)) {
|
|
838
|
+
await db(AI_MODEL_PRICE_TABLE)
|
|
839
|
+
.where("id", item.id)
|
|
840
|
+
.update({ status: 0, effective_to: now });
|
|
841
|
+
changed += 1;
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
return {
|
|
846
|
+
changed_count: changed,
|
|
847
|
+
total: rows.length,
|
|
848
|
+
synced_at: now.toISOString(),
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
function stableStringify(row) {
|
|
853
|
+
const keys = Object.keys(row).sort();
|
|
854
|
+
const ordered = {};
|
|
855
|
+
for (const key of keys) {
|
|
856
|
+
ordered[key] = row[key];
|
|
857
|
+
}
|
|
858
|
+
return JSON.stringify(ordered);
|
|
859
|
+
}
|
|
860
|
+
async function upsertRow(db, row, now) {
|
|
861
|
+
const variant = row.variant || "";
|
|
862
|
+
const active = (await db(AI_MODEL_PRICE_TABLE)
|
|
863
|
+
.where({
|
|
864
|
+
provider: row.provider,
|
|
865
|
+
model: row.model,
|
|
866
|
+
model_type: row.model_type,
|
|
867
|
+
billing_unit: row.billing_unit,
|
|
868
|
+
pricing_mode: row.pricing_mode,
|
|
869
|
+
variant,
|
|
870
|
+
status: 1,
|
|
871
|
+
})
|
|
872
|
+
.andWhere((qb) => qb.whereNull("effective_to").orWhere("effective_to", ">", now)));
|
|
873
|
+
const same = (current) => Number(current.cost_input_per_1m ?? 0) === Number(row.cost_input_per_1m ?? 0) &&
|
|
874
|
+
Number(current.cost_output_per_1m ?? 0) === Number(row.cost_output_per_1m ?? 0) &&
|
|
875
|
+
Number(current.cost_unit_price ?? 0) === Number(row.cost_unit_price ?? 0) &&
|
|
876
|
+
Number(current.cost_cached_input_per_1m ?? 0) ===
|
|
877
|
+
Number(row.cost_cached_input_per_1m ?? 0) &&
|
|
878
|
+
Number(current.unit_size ?? 0) === Number(row.unit_size ?? 0) &&
|
|
879
|
+
(asText(current.currency) || "CNY") === (row.currency || "CNY") &&
|
|
880
|
+
(asText(current.source) || "") === "official";
|
|
881
|
+
if (active.length && active.every(same))
|
|
882
|
+
return false;
|
|
883
|
+
for (const item of active) {
|
|
884
|
+
await db(AI_MODEL_PRICE_TABLE)
|
|
885
|
+
.where("id", item.id)
|
|
886
|
+
.update({ status: 0, effective_to: now });
|
|
887
|
+
}
|
|
888
|
+
await db(AI_MODEL_PRICE_TABLE).insert({
|
|
889
|
+
provider: row.provider,
|
|
890
|
+
model: row.model,
|
|
891
|
+
model_type: row.model_type,
|
|
892
|
+
billing_unit: row.billing_unit,
|
|
893
|
+
pricing_mode: row.pricing_mode,
|
|
894
|
+
unit_size: row.unit_size || 1,
|
|
895
|
+
variant,
|
|
896
|
+
currency: row.currency || "CNY",
|
|
897
|
+
cost_input_per_1m: row.cost_input_per_1m || 0,
|
|
898
|
+
cost_output_per_1m: row.cost_output_per_1m || 0,
|
|
899
|
+
cost_cached_input_per_1m: row.cost_cached_input_per_1m === null ||
|
|
900
|
+
row.cost_cached_input_per_1m === undefined
|
|
901
|
+
? null
|
|
902
|
+
: row.cost_cached_input_per_1m,
|
|
903
|
+
cost_unit_price: row.cost_unit_price || 0,
|
|
904
|
+
sale_input_per_1m: null,
|
|
905
|
+
sale_output_per_1m: null,
|
|
906
|
+
sale_unit_price: null,
|
|
907
|
+
source: "official",
|
|
908
|
+
source_url: row.source_url,
|
|
909
|
+
synced_at: now,
|
|
910
|
+
effective_from: now,
|
|
911
|
+
checksum: row.checksum,
|
|
912
|
+
status: 1,
|
|
913
|
+
});
|
|
914
|
+
return true;
|
|
915
|
+
}
|
|
916
|
+
// ─────────────── 适配器注册表 / 默认配置 ───────────────
|
|
917
|
+
const adapters = new Map();
|
|
918
|
+
export function registerAdapter(provider, adapter) {
|
|
919
|
+
adapters.set(provider, adapter);
|
|
920
|
+
}
|
|
921
|
+
export function getAdapter(provider) {
|
|
922
|
+
return adapters.get(provider) ?? null;
|
|
923
|
+
}
|
|
924
|
+
function env(provider, key, fallback) {
|
|
925
|
+
return (process.env[`AI_USAGE_PRICE_SYNC_${provider.toUpperCase()}_${key}`] ??
|
|
926
|
+
fallback);
|
|
927
|
+
}
|
|
928
|
+
export function registerDefaultAdapters() {
|
|
929
|
+
for (const [provider, cfg] of Object.entries(DEFAULT_SOURCES)) {
|
|
930
|
+
const columnMap = cloneColumns();
|
|
931
|
+
const rawColumns = env(provider, "COLUMNS", "");
|
|
932
|
+
if (rawColumns) {
|
|
933
|
+
try {
|
|
934
|
+
Object.assign(columnMap, JSON.parse(rawColumns));
|
|
935
|
+
}
|
|
936
|
+
catch {
|
|
937
|
+
console.warn(`价格同步列名配置不是合法 JSON: ${provider}`);
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
registerAdapter(provider, new HttpPriceAdapter(new HttpPriceConfig({
|
|
941
|
+
provider,
|
|
942
|
+
url: env(provider, "URL", cfg.url),
|
|
943
|
+
format: env(provider, "FORMAT", cfg.format ?? "html_table"),
|
|
944
|
+
model_type: env(provider, "MODEL_TYPE", cfg.model_type ?? "t2t"),
|
|
945
|
+
billing_unit: env(provider, "BILLING_UNIT", "token"),
|
|
946
|
+
pricing_mode: env(provider, "PRICING_MODE", "per_token"),
|
|
947
|
+
unit_size: Number(env(provider, "UNIT_SIZE", "1000000")),
|
|
948
|
+
currency: env(provider, "CURRENCY", "CNY"),
|
|
949
|
+
column_map: columnMap,
|
|
950
|
+
})));
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
registerDefaultAdapters();
|
|
954
|
+
//# sourceMappingURL=sync.js.map
|