@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,40 @@
|
|
|
1
|
+
import type { Knex } from "knex";
|
|
2
|
+
import type { AiModelOptions, BreakdownItem, OptionsResponse, PriceItem, PriceUpsertRequest, SyncLogItem, TrendPoint, UsageQuery, UsageRecord, UsageSummary } from "./schema.js";
|
|
3
|
+
export declare class AiUsageService {
|
|
4
|
+
private readonly db;
|
|
5
|
+
constructor(db?: Knex);
|
|
6
|
+
private conditions;
|
|
7
|
+
private base;
|
|
8
|
+
private aggColumns;
|
|
9
|
+
summary(query: UsageQuery, employeeId?: number | null): Promise<UsageSummary>;
|
|
10
|
+
trend(query: UsageQuery, employeeId?: number | null): Promise<TrendPoint[]>;
|
|
11
|
+
breakdown(query: UsageQuery, groupBy?: string, employeeId?: number | null, limit?: number): Promise<BreakdownItem[]>;
|
|
12
|
+
records(query: UsageQuery, page?: number, size?: number, employeeId?: number | null): Promise<[UsageRecord[], number]>;
|
|
13
|
+
private distinctValues;
|
|
14
|
+
options(): Promise<OptionsResponse>;
|
|
15
|
+
/** 读取系统设置里的 AI 服务商与可用模型(不返回 API Key) */
|
|
16
|
+
modelOptions(): Promise<AiModelOptions>;
|
|
17
|
+
exportRows(query: UsageQuery, employeeId?: number | null, limit?: number): Promise<UsageRecord[]>;
|
|
18
|
+
listPrices(page?: number, size?: number, provider?: string | null, model?: string | null, keyword?: string | null): Promise<[PriceItem[], number]>;
|
|
19
|
+
upsertPrice(data: PriceUpsertRequest): Promise<PriceItem>;
|
|
20
|
+
/** 编辑价格:关闭旧价后按新值写入(保持 append-only) */
|
|
21
|
+
updatePrice(priceId: number, data: PriceUpsertRequest): Promise<PriceItem>;
|
|
22
|
+
/** 删除价格:只关闭当前生效价,历史用量金额不受影响 */
|
|
23
|
+
deletePrice(priceId: number): Promise<void>;
|
|
24
|
+
/** 执行官方价格同步;未注册适配器时记录 skipped */
|
|
25
|
+
syncPrices(provider: string): Promise<{
|
|
26
|
+
job_id: string;
|
|
27
|
+
status: string;
|
|
28
|
+
changed_count: number;
|
|
29
|
+
error: string | null;
|
|
30
|
+
}>;
|
|
31
|
+
listSyncLogs(page?: number, size?: number, provider?: string | null): Promise<[SyncLogItem[], number]>;
|
|
32
|
+
/** 导入价格(XLSX / CSV);支持中英双语表头,忽略 CSV 中 # 开头的说明行 */
|
|
33
|
+
importPrices(provider: string, content: Buffer, filename?: string | null): Promise<{
|
|
34
|
+
imported: number;
|
|
35
|
+
errors: Array<{
|
|
36
|
+
row: number;
|
|
37
|
+
error: string;
|
|
38
|
+
}>;
|
|
39
|
+
}>;
|
|
40
|
+
}
|
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI 用量报表服务(汇总 / 趋势 / 分组 / 明细 / 选项 / 模型选项)
|
|
3
|
+
* 对应 backend-python 的 reglow/modules/ai_usage/service.py
|
|
4
|
+
*/
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
import { knex } from "../../core/database.js";
|
|
7
|
+
import { toIsoDate } from "../../common/serialize.js";
|
|
8
|
+
import { NotFoundException } from "../../common/errors.js";
|
|
9
|
+
import { AI_MODEL_PRICE_TABLE, AI_PRICE_SYNC_LOG_TABLE, AI_USAGE_TABLE, } from "./model.js";
|
|
10
|
+
import { normalizePriceHeader, readPriceXlsx } from "./xlsx_io.js";
|
|
11
|
+
import { getAdapter } from "./sync.js";
|
|
12
|
+
const ALLOWED_GROUP_BY = new Set([
|
|
13
|
+
"provider",
|
|
14
|
+
"model",
|
|
15
|
+
"model_type",
|
|
16
|
+
"scene",
|
|
17
|
+
"employee_id",
|
|
18
|
+
"dept_id",
|
|
19
|
+
]);
|
|
20
|
+
const PENDING_STATUSES = ["submitted", "running"];
|
|
21
|
+
const ESTIMATED_SOURCES = ["estimated", "partial"];
|
|
22
|
+
function toNum(value, def = 0) {
|
|
23
|
+
if (value === null || value === undefined)
|
|
24
|
+
return def;
|
|
25
|
+
const n = Number(value);
|
|
26
|
+
return Number.isFinite(n) ? n : def;
|
|
27
|
+
}
|
|
28
|
+
function toInt(value, def = 0) {
|
|
29
|
+
if (value === null || value === undefined)
|
|
30
|
+
return def;
|
|
31
|
+
const n = Number(value);
|
|
32
|
+
return Number.isFinite(n) ? Math.trunc(n) : def;
|
|
33
|
+
}
|
|
34
|
+
function pad2(n) {
|
|
35
|
+
return String(n).padStart(2, "0");
|
|
36
|
+
}
|
|
37
|
+
function parseDayStart(value) {
|
|
38
|
+
const [y, m, d] = value
|
|
39
|
+
.slice(0, 10)
|
|
40
|
+
.split("-")
|
|
41
|
+
.map((v) => Number(v));
|
|
42
|
+
return new Date(y || 1970, (m || 1) - 1, d || 1, 0, 0, 0, 0);
|
|
43
|
+
}
|
|
44
|
+
function addDays(date, days) {
|
|
45
|
+
const d = new Date(date.getTime());
|
|
46
|
+
d.setDate(d.getDate() + days);
|
|
47
|
+
return d;
|
|
48
|
+
}
|
|
49
|
+
/** 与 Python _parse_range 一致:end 含当天(+1 天),start 缺省 end-30 天 */
|
|
50
|
+
function parseRange(query) {
|
|
51
|
+
const end = query.end ? addDays(parseDayStart(query.end), 1) : new Date();
|
|
52
|
+
const start = query.start
|
|
53
|
+
? parseDayStart(query.start)
|
|
54
|
+
: new Date(end.getTime() - 30 * 24 * 60 * 60 * 1000);
|
|
55
|
+
return [start, end];
|
|
56
|
+
}
|
|
57
|
+
function mapUsageRecord(row) {
|
|
58
|
+
return {
|
|
59
|
+
id: row.id,
|
|
60
|
+
created_at: toIsoDate(row.created_at),
|
|
61
|
+
request_id: row.request_id,
|
|
62
|
+
provider: row.provider,
|
|
63
|
+
model: row.model,
|
|
64
|
+
model_type: row.model_type || "",
|
|
65
|
+
billing_unit: row.billing_unit || "",
|
|
66
|
+
scene: row.scene,
|
|
67
|
+
employee_id: row.employee_id ?? null,
|
|
68
|
+
employee_name: row.emp_nickname || row.emp_real_name || row.emp_username || null,
|
|
69
|
+
request_count: row.request_count ?? 1,
|
|
70
|
+
prompt_tokens: row.prompt_tokens ?? 0,
|
|
71
|
+
completion_tokens: row.completion_tokens ?? 0,
|
|
72
|
+
cached_tokens: row.cached_tokens ?? 0,
|
|
73
|
+
total_tokens: row.total_tokens ?? 0,
|
|
74
|
+
quantity: row.quantity ?? 0,
|
|
75
|
+
duration_seconds: toNum(row.duration_seconds),
|
|
76
|
+
cost_amount: toNum(row.cost_amount),
|
|
77
|
+
sale_amount: toNum(row.sale_amount),
|
|
78
|
+
currency: row.currency || "CNY",
|
|
79
|
+
status: String(row.status ?? ""),
|
|
80
|
+
usage_source: row.usage_source || "",
|
|
81
|
+
pricing_status: row.pricing_status || "",
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
export class AiUsageService {
|
|
85
|
+
db;
|
|
86
|
+
constructor(db = knex) {
|
|
87
|
+
this.db = db;
|
|
88
|
+
}
|
|
89
|
+
conditions(qb, query, employeeId) {
|
|
90
|
+
const [start, end] = parseRange(query);
|
|
91
|
+
qb.where("u.created_at", ">=", start).where("u.created_at", "<", end);
|
|
92
|
+
for (const field of [
|
|
93
|
+
"provider",
|
|
94
|
+
"model",
|
|
95
|
+
"model_type",
|
|
96
|
+
"scene",
|
|
97
|
+
"status",
|
|
98
|
+
"usage_source",
|
|
99
|
+
]) {
|
|
100
|
+
const value = query[field];
|
|
101
|
+
if (value)
|
|
102
|
+
qb.where(`u.${field}`, value);
|
|
103
|
+
}
|
|
104
|
+
if (query.employee_id)
|
|
105
|
+
qb.where("u.employee_id", query.employee_id);
|
|
106
|
+
if (employeeId !== null && employeeId !== undefined) {
|
|
107
|
+
qb.where("u.employee_id", employeeId);
|
|
108
|
+
}
|
|
109
|
+
return qb;
|
|
110
|
+
}
|
|
111
|
+
base(query, employeeId) {
|
|
112
|
+
return this.conditions(this.db(`${AI_USAGE_TABLE} as u`), query, employeeId);
|
|
113
|
+
}
|
|
114
|
+
aggColumns() {
|
|
115
|
+
return [
|
|
116
|
+
this.db.raw("count(u.id) as request_count"),
|
|
117
|
+
this.db.raw("coalesce(sum(u.prompt_tokens), 0) as prompt_tokens"),
|
|
118
|
+
this.db.raw("coalesce(sum(u.completion_tokens), 0) as completion_tokens"),
|
|
119
|
+
this.db.raw("coalesce(sum(u.cached_tokens), 0) as cached_tokens"),
|
|
120
|
+
this.db.raw("coalesce(sum(u.total_tokens), 0) as total_tokens"),
|
|
121
|
+
this.db.raw("coalesce(sum(u.quantity), 0) as image_count"),
|
|
122
|
+
this.db.raw("coalesce(sum(u.duration_seconds), 0) as video_seconds"),
|
|
123
|
+
this.db.raw("coalesce(sum(u.cost_amount), 0) as cost_amount"),
|
|
124
|
+
this.db.raw("coalesce(sum(u.sale_amount), 0) as sale_amount"),
|
|
125
|
+
this.db.raw("coalesce(sum(case when u.pricing_status = 'missing' then 1 else 0 end), 0) as unpriced_count"),
|
|
126
|
+
this.db.raw(`coalesce(sum(case when u.usage_source in ('${ESTIMATED_SOURCES.join("','")}') then 1 else 0 end), 0) as estimated_count`),
|
|
127
|
+
this.db.raw(`coalesce(sum(case when u.status in ('${PENDING_STATUSES.join("','")}') then 1 else 0 end), 0) as pending_count`),
|
|
128
|
+
];
|
|
129
|
+
}
|
|
130
|
+
async summary(query, employeeId) {
|
|
131
|
+
const row = (await this.base(query, employeeId)
|
|
132
|
+
.select(this.aggColumns())
|
|
133
|
+
.first());
|
|
134
|
+
return {
|
|
135
|
+
request_count: toInt(row?.request_count),
|
|
136
|
+
prompt_tokens: toInt(row?.prompt_tokens),
|
|
137
|
+
completion_tokens: toInt(row?.completion_tokens),
|
|
138
|
+
cached_tokens: toInt(row?.cached_tokens),
|
|
139
|
+
total_tokens: toInt(row?.total_tokens),
|
|
140
|
+
image_count: toInt(row?.image_count),
|
|
141
|
+
video_seconds: toNum(row?.video_seconds),
|
|
142
|
+
cost_amount: toNum(row?.cost_amount),
|
|
143
|
+
sale_amount: toNum(row?.sale_amount),
|
|
144
|
+
currency: "CNY",
|
|
145
|
+
unpriced_count: toInt(row?.unpriced_count),
|
|
146
|
+
estimated_count: toInt(row?.estimated_count),
|
|
147
|
+
pending_count: toInt(row?.pending_count),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
async trend(query, employeeId) {
|
|
151
|
+
const rows = (await this.base(query, employeeId)
|
|
152
|
+
.select([this.db.raw("u.day as day"), ...this.aggColumns()])
|
|
153
|
+
.groupBy("u.day")
|
|
154
|
+
.orderBy("u.day", "asc"));
|
|
155
|
+
return rows.map((row) => ({
|
|
156
|
+
day: String(row.day ?? ""),
|
|
157
|
+
request_count: toInt(row.request_count),
|
|
158
|
+
total_tokens: toInt(row.total_tokens),
|
|
159
|
+
image_count: toInt(row.image_count),
|
|
160
|
+
video_seconds: toNum(row.video_seconds),
|
|
161
|
+
cost_amount: toNum(row.cost_amount),
|
|
162
|
+
}));
|
|
163
|
+
}
|
|
164
|
+
async breakdown(query, groupBy = "provider", employeeId, limit = 100) {
|
|
165
|
+
const group = ALLOWED_GROUP_BY.has(groupBy) ? groupBy : "provider";
|
|
166
|
+
const take = Math.min(500, Math.max(1, limit || 100));
|
|
167
|
+
let rows;
|
|
168
|
+
if (group === "employee_id") {
|
|
169
|
+
const qb = this.conditions(this.db(`${AI_USAGE_TABLE} as u`).leftJoin("sys_employee as e", "e.id", "u.employee_id"), query, employeeId);
|
|
170
|
+
rows = (await qb
|
|
171
|
+
.select([
|
|
172
|
+
this.db.raw("coalesce(e.nickname, e.real_name, e.username, cast(u.employee_id as char)) as name"),
|
|
173
|
+
...this.aggColumns(),
|
|
174
|
+
])
|
|
175
|
+
.groupBy("u.employee_id", "e.nickname", "e.real_name", "e.username")
|
|
176
|
+
.orderByRaw("sum(u.cost_amount) desc")
|
|
177
|
+
.limit(take));
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
rows = (await this.base(query, employeeId)
|
|
181
|
+
.select([
|
|
182
|
+
this.db.raw("cast(u.?? as char) as name", [group]),
|
|
183
|
+
...this.aggColumns(),
|
|
184
|
+
])
|
|
185
|
+
.groupBy(`u.${group}`)
|
|
186
|
+
.orderByRaw("sum(u.cost_amount) desc")
|
|
187
|
+
.limit(take));
|
|
188
|
+
}
|
|
189
|
+
return rows.map((row) => ({
|
|
190
|
+
name: row.name === null || row.name === undefined || row.name === ""
|
|
191
|
+
? "未归属"
|
|
192
|
+
: String(row.name),
|
|
193
|
+
request_count: toInt(row.request_count),
|
|
194
|
+
total_tokens: toInt(row.total_tokens),
|
|
195
|
+
image_count: toInt(row.image_count),
|
|
196
|
+
video_seconds: toNum(row.video_seconds),
|
|
197
|
+
cost_amount: toNum(row.cost_amount),
|
|
198
|
+
sale_amount: toNum(row.sale_amount),
|
|
199
|
+
}));
|
|
200
|
+
}
|
|
201
|
+
async records(query, page = 1, size = 20, employeeId) {
|
|
202
|
+
const safePage = Math.max(1, page || 1);
|
|
203
|
+
const safeSize = Math.min(200, Math.max(1, size || 20));
|
|
204
|
+
const countRow = (await this.base(query, employeeId)
|
|
205
|
+
.count({ total: "u.id" })
|
|
206
|
+
.first());
|
|
207
|
+
const total = toInt(countRow?.total);
|
|
208
|
+
const rows = (await this.base(query, employeeId)
|
|
209
|
+
.leftJoin("sys_employee as e", "e.id", "u.employee_id")
|
|
210
|
+
.select("u.*", "e.nickname as emp_nickname", "e.real_name as emp_real_name", "e.username as emp_username")
|
|
211
|
+
.orderBy([
|
|
212
|
+
{ column: "u.created_at", order: "desc" },
|
|
213
|
+
{ column: "u.id", order: "desc" },
|
|
214
|
+
])
|
|
215
|
+
.offset((safePage - 1) * safeSize)
|
|
216
|
+
.limit(safeSize));
|
|
217
|
+
const items = rows.map((row) => mapUsageRecord(row));
|
|
218
|
+
return [items, total];
|
|
219
|
+
}
|
|
220
|
+
async distinctValues(column) {
|
|
221
|
+
const rows = (await this.db(AI_USAGE_TABLE)
|
|
222
|
+
.distinct(column)
|
|
223
|
+
.whereNotNull(column)
|
|
224
|
+
.where(column, "!=", ""));
|
|
225
|
+
const values = new Set();
|
|
226
|
+
for (const row of rows) {
|
|
227
|
+
const value = row[column];
|
|
228
|
+
if (value !== null && value !== undefined && value !== "") {
|
|
229
|
+
values.add(String(value));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return [...values].sort();
|
|
233
|
+
}
|
|
234
|
+
async options() {
|
|
235
|
+
return {
|
|
236
|
+
providers: await this.distinctValues("provider"),
|
|
237
|
+
models: await this.distinctValues("model"),
|
|
238
|
+
model_types: await this.distinctValues("model_type"),
|
|
239
|
+
scenes: await this.distinctValues("scene"),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
/** 读取系统设置里的 AI 服务商与可用模型(不返回 API Key) */
|
|
243
|
+
async modelOptions() {
|
|
244
|
+
const { getAllProviderConfigs } = await import("../ai/controller.js");
|
|
245
|
+
const configs = await getAllProviderConfigs();
|
|
246
|
+
const providers = [];
|
|
247
|
+
const allModels = new Set();
|
|
248
|
+
for (const cfg of configs) {
|
|
249
|
+
const items = [];
|
|
250
|
+
for (const item of cfg.models) {
|
|
251
|
+
const modelId = item.model_id;
|
|
252
|
+
if (modelId) {
|
|
253
|
+
items.push({
|
|
254
|
+
model_id: String(modelId),
|
|
255
|
+
name: String(item.name ?? ""),
|
|
256
|
+
type: String(item.type ?? "t2t"),
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
const defaultModel = String(cfg.model ?? "");
|
|
261
|
+
if (defaultModel &&
|
|
262
|
+
!items.some((item) => item.model_id === defaultModel)) {
|
|
263
|
+
items.unshift({
|
|
264
|
+
model_id: defaultModel,
|
|
265
|
+
name: defaultModel,
|
|
266
|
+
type: "t2t",
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
const models = [...new Set(items.map((item) => item.model_id))].sort();
|
|
270
|
+
for (const model of models)
|
|
271
|
+
allModels.add(model);
|
|
272
|
+
providers.push({
|
|
273
|
+
provider: cfg.provider ?? "",
|
|
274
|
+
default_model: defaultModel,
|
|
275
|
+
models,
|
|
276
|
+
items,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
providers: providers.filter((p) => p.provider),
|
|
281
|
+
models: [...allModels].sort(),
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
// ─── 导出 ───
|
|
285
|
+
async exportRows(query, employeeId = null, limit = 100_000) {
|
|
286
|
+
const take = Math.min(100_000, Math.max(1, limit || 100_000));
|
|
287
|
+
const rows = (await this.base(query, employeeId)
|
|
288
|
+
.leftJoin("sys_employee as e", "e.id", "u.employee_id")
|
|
289
|
+
.select("u.*", "e.nickname as emp_nickname", "e.real_name as emp_real_name", "e.username as emp_username")
|
|
290
|
+
.orderBy([
|
|
291
|
+
{ column: "u.created_at", order: "desc" },
|
|
292
|
+
{ column: "u.id", order: "desc" },
|
|
293
|
+
])
|
|
294
|
+
.limit(take));
|
|
295
|
+
return rows.map((row) => mapUsageRecord(row));
|
|
296
|
+
}
|
|
297
|
+
// ─── 价格配置 ───
|
|
298
|
+
async listPrices(page = 1, size = 20, provider = null, model = null, keyword = null) {
|
|
299
|
+
const safePage = Math.max(1, page || 1);
|
|
300
|
+
const safeSize = Math.min(200, Math.max(1, size || 20));
|
|
301
|
+
const now = new Date();
|
|
302
|
+
const build = () => {
|
|
303
|
+
const qb = this.db(AI_MODEL_PRICE_TABLE);
|
|
304
|
+
if (provider)
|
|
305
|
+
qb.where("provider", provider);
|
|
306
|
+
if (model)
|
|
307
|
+
qb.where("model", model);
|
|
308
|
+
const kw = (keyword ?? "").trim();
|
|
309
|
+
if (kw) {
|
|
310
|
+
const like = `%${kw}%`;
|
|
311
|
+
qb.where((sub) => {
|
|
312
|
+
sub
|
|
313
|
+
.where("provider", "like", like)
|
|
314
|
+
.orWhere("model", "like", like)
|
|
315
|
+
.orWhere("variant", "like", like);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
// 价格配置只展示当前生效的价格,历史/已关闭的 append-only 记录不再列出
|
|
319
|
+
qb.where("status", 1).andWhere((sub) => sub.whereNull("effective_to").orWhere("effective_to", ">", now));
|
|
320
|
+
return qb;
|
|
321
|
+
};
|
|
322
|
+
const countRow = (await build().count({ total: "id" }).first());
|
|
323
|
+
const total = toInt(countRow?.total);
|
|
324
|
+
const rows = (await build()
|
|
325
|
+
.orderBy([
|
|
326
|
+
{ column: "effective_from", order: "desc" },
|
|
327
|
+
{ column: "id", order: "desc" },
|
|
328
|
+
])
|
|
329
|
+
.offset((safePage - 1) * safeSize)
|
|
330
|
+
.limit(safeSize));
|
|
331
|
+
return [rows.map((row) => mapPriceItem(row)), total];
|
|
332
|
+
}
|
|
333
|
+
async upsertPrice(data) {
|
|
334
|
+
const now = new Date();
|
|
335
|
+
const effectiveFrom = data.effective_from
|
|
336
|
+
? new Date(data.effective_from)
|
|
337
|
+
: now;
|
|
338
|
+
const dims = {
|
|
339
|
+
provider: data.provider,
|
|
340
|
+
model: data.model,
|
|
341
|
+
model_type: data.model_type || "t2t",
|
|
342
|
+
billing_unit: data.billing_unit || "token",
|
|
343
|
+
pricing_mode: data.pricing_mode || "per_token",
|
|
344
|
+
variant: data.variant || "",
|
|
345
|
+
};
|
|
346
|
+
return this.db.transaction(async (trx) => {
|
|
347
|
+
// append-only:关闭同维度当前有效价格
|
|
348
|
+
await trx(AI_MODEL_PRICE_TABLE)
|
|
349
|
+
.where(dims)
|
|
350
|
+
.where("status", 1)
|
|
351
|
+
.andWhere((qb) => qb.whereNull("effective_to").orWhere("effective_to", ">", now))
|
|
352
|
+
.update({ status: 0, effective_to: effectiveFrom, updated_at: now });
|
|
353
|
+
const [id] = await trx(AI_MODEL_PRICE_TABLE).insert({
|
|
354
|
+
...dims,
|
|
355
|
+
unit_size: data.unit_size || 1,
|
|
356
|
+
currency: data.currency || "CNY",
|
|
357
|
+
cost_input_per_1m: data.cost_input_per_1m || 0,
|
|
358
|
+
cost_output_per_1m: data.cost_output_per_1m || 0,
|
|
359
|
+
cost_cached_input_per_1m: data.cost_cached_input_per_1m === null ||
|
|
360
|
+
data.cost_cached_input_per_1m === undefined
|
|
361
|
+
? null
|
|
362
|
+
: data.cost_cached_input_per_1m,
|
|
363
|
+
cost_unit_price: data.cost_unit_price || 0,
|
|
364
|
+
sale_input_per_1m: data.sale_input_per_1m ?? null,
|
|
365
|
+
sale_output_per_1m: data.sale_output_per_1m ?? null,
|
|
366
|
+
sale_unit_price: data.sale_unit_price ?? null,
|
|
367
|
+
source: data.source || "manual",
|
|
368
|
+
effective_from: effectiveFrom,
|
|
369
|
+
status: 1,
|
|
370
|
+
remark: data.remark ?? null,
|
|
371
|
+
});
|
|
372
|
+
const row = (await trx(AI_MODEL_PRICE_TABLE).where("id", id).first());
|
|
373
|
+
if (!row)
|
|
374
|
+
throw new NotFoundException("价格不存在");
|
|
375
|
+
return mapPriceItem(row);
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
/** 编辑价格:关闭旧价后按新值写入(保持 append-only) */
|
|
379
|
+
async updatePrice(priceId, data) {
|
|
380
|
+
const row = (await this.db(AI_MODEL_PRICE_TABLE)
|
|
381
|
+
.where("id", priceId)
|
|
382
|
+
.first());
|
|
383
|
+
if (!row)
|
|
384
|
+
throw new NotFoundException("价格不存在");
|
|
385
|
+
if (Number(row.status ?? 0) === 1) {
|
|
386
|
+
const now = new Date();
|
|
387
|
+
await this.db(AI_MODEL_PRICE_TABLE).where("id", priceId).update({
|
|
388
|
+
status: 0,
|
|
389
|
+
effective_to: now,
|
|
390
|
+
updated_at: now,
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
return this.upsertPrice(data);
|
|
394
|
+
}
|
|
395
|
+
/** 删除价格:只关闭当前生效价,历史用量金额不受影响 */
|
|
396
|
+
async deletePrice(priceId) {
|
|
397
|
+
const row = (await this.db(AI_MODEL_PRICE_TABLE)
|
|
398
|
+
.where("id", priceId)
|
|
399
|
+
.first());
|
|
400
|
+
if (!row)
|
|
401
|
+
throw new NotFoundException("价格不存在");
|
|
402
|
+
if (Number(row.status ?? 0) === 1) {
|
|
403
|
+
const now = new Date();
|
|
404
|
+
await this.db(AI_MODEL_PRICE_TABLE).where("id", priceId).update({
|
|
405
|
+
status: 0,
|
|
406
|
+
effective_to: now,
|
|
407
|
+
updated_at: now,
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
/** 执行官方价格同步;未注册适配器时记录 skipped */
|
|
412
|
+
async syncPrices(provider) {
|
|
413
|
+
const jobId = randomUUID().replace(/-/g, "");
|
|
414
|
+
const [logId] = await this.db(AI_PRICE_SYNC_LOG_TABLE).insert({
|
|
415
|
+
provider,
|
|
416
|
+
job_id: jobId,
|
|
417
|
+
started_at: new Date(),
|
|
418
|
+
status: "running",
|
|
419
|
+
});
|
|
420
|
+
let status = "running";
|
|
421
|
+
let changedCount = 0;
|
|
422
|
+
let error = null;
|
|
423
|
+
try {
|
|
424
|
+
const adapter = getAdapter(provider);
|
|
425
|
+
if (!adapter) {
|
|
426
|
+
status = "skipped";
|
|
427
|
+
changedCount = 0;
|
|
428
|
+
error = "官方价格同步适配器尚未配置,请使用手工价格或 XLSX 导入";
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
const result = await adapter.sync(this.db);
|
|
432
|
+
changedCount = toInt(result.changed_count);
|
|
433
|
+
const total = toInt(result.total);
|
|
434
|
+
if ("total" in result && total === 0) {
|
|
435
|
+
status = "empty";
|
|
436
|
+
error =
|
|
437
|
+
"未从官方页面解析到价格数据,请改用 XLSX 导入," +
|
|
438
|
+
`或通过 AI_USAGE_PRICE_SYNC_${provider.toUpperCase()}_* 环境变量配置同步地址与列名`;
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
status = "success";
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
catch (e) {
|
|
446
|
+
status = "failed";
|
|
447
|
+
error = String(e?.message ?? e).slice(0, 500);
|
|
448
|
+
}
|
|
449
|
+
await this.db(AI_PRICE_SYNC_LOG_TABLE).where("id", Number(logId)).update({
|
|
450
|
+
status,
|
|
451
|
+
changed_count: changedCount,
|
|
452
|
+
error,
|
|
453
|
+
finished_at: new Date(),
|
|
454
|
+
updated_at: new Date(),
|
|
455
|
+
});
|
|
456
|
+
return {
|
|
457
|
+
job_id: jobId,
|
|
458
|
+
status,
|
|
459
|
+
changed_count: changedCount,
|
|
460
|
+
error,
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
async listSyncLogs(page = 1, size = 20, provider = null) {
|
|
464
|
+
const safePage = Math.max(1, page || 1);
|
|
465
|
+
const safeSize = Math.min(200, Math.max(1, size || 20));
|
|
466
|
+
const build = () => {
|
|
467
|
+
const qb = this.db(AI_PRICE_SYNC_LOG_TABLE);
|
|
468
|
+
if (provider)
|
|
469
|
+
qb.where("provider", provider);
|
|
470
|
+
return qb;
|
|
471
|
+
};
|
|
472
|
+
const countRow = (await build().count({ total: "id" }).first());
|
|
473
|
+
const total = toInt(countRow?.total);
|
|
474
|
+
const rows = (await build()
|
|
475
|
+
.orderBy("id", "desc")
|
|
476
|
+
.offset((safePage - 1) * safeSize)
|
|
477
|
+
.limit(safeSize));
|
|
478
|
+
return [
|
|
479
|
+
rows.map((row) => ({
|
|
480
|
+
id: row.id,
|
|
481
|
+
provider: row.provider,
|
|
482
|
+
job_id: row.job_id ?? null,
|
|
483
|
+
started_at: toIsoDate(row.started_at),
|
|
484
|
+
finished_at: toIsoDate(row.finished_at),
|
|
485
|
+
status: String(row.status ?? ""),
|
|
486
|
+
changed_count: toInt(row.changed_count),
|
|
487
|
+
error: row.error ?? null,
|
|
488
|
+
})),
|
|
489
|
+
total,
|
|
490
|
+
];
|
|
491
|
+
}
|
|
492
|
+
/** 导入价格(XLSX / CSV);支持中英双语表头,忽略 CSV 中 # 开头的说明行 */
|
|
493
|
+
async importPrices(provider, content, filename = null) {
|
|
494
|
+
const isXlsx = (filename ?? "").toLowerCase().endsWith(".xlsx");
|
|
495
|
+
const rows = isXlsx ? readPriceXlsx(content) : readCsvRows(content);
|
|
496
|
+
let imported = 0;
|
|
497
|
+
const errors = [];
|
|
498
|
+
for (let index = 0; index < rows.length; index++) {
|
|
499
|
+
const rowNo = index + 2;
|
|
500
|
+
try {
|
|
501
|
+
const row = rows[index];
|
|
502
|
+
const model = (row.model ?? "").trim();
|
|
503
|
+
if (!model)
|
|
504
|
+
throw new Error("model 不能为空");
|
|
505
|
+
await this.upsertPrice({
|
|
506
|
+
provider: (row.provider || provider).trim(),
|
|
507
|
+
model,
|
|
508
|
+
model_type: (row.model_type || "t2t").trim(),
|
|
509
|
+
billing_unit: (row.billing_unit || "token").trim(),
|
|
510
|
+
pricing_mode: (row.pricing_mode || "per_token").trim(),
|
|
511
|
+
unit_size: parsePriceNumber(row.unit_size, 1),
|
|
512
|
+
variant: (row.variant || "").trim(),
|
|
513
|
+
currency: (row.currency || "CNY").trim(),
|
|
514
|
+
cost_input_per_1m: parsePriceNumber(row.cost_input_per_1m),
|
|
515
|
+
cost_output_per_1m: parsePriceNumber(row.cost_output_per_1m),
|
|
516
|
+
cost_cached_input_per_1m: parseOptionalPriceNumber(row.cost_cached_input_per_1m),
|
|
517
|
+
cost_unit_price: parsePriceNumber(row.cost_unit_price),
|
|
518
|
+
sale_input_per_1m: parseOptionalPriceNumber(row.sale_input_per_1m),
|
|
519
|
+
sale_output_per_1m: parseOptionalPriceNumber(row.sale_output_per_1m),
|
|
520
|
+
sale_unit_price: parseOptionalPriceNumber(row.sale_unit_price),
|
|
521
|
+
source: "csv",
|
|
522
|
+
remark: (row.remark || "").trim() || null,
|
|
523
|
+
});
|
|
524
|
+
imported += 1;
|
|
525
|
+
}
|
|
526
|
+
catch (e) {
|
|
527
|
+
errors.push({
|
|
528
|
+
row: rowNo,
|
|
529
|
+
error: String(e?.message ?? e).slice(0, 200),
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return { imported, errors };
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
// ─────────────── 价格 DTO / 导入辅助 ───────────────
|
|
537
|
+
function numOrNull(value) {
|
|
538
|
+
if (value === null || value === undefined)
|
|
539
|
+
return null;
|
|
540
|
+
return toNum(value);
|
|
541
|
+
}
|
|
542
|
+
function mapPriceItem(row) {
|
|
543
|
+
return {
|
|
544
|
+
id: row.id,
|
|
545
|
+
provider: row.provider,
|
|
546
|
+
model: row.model,
|
|
547
|
+
model_type: row.model_type || "t2t",
|
|
548
|
+
billing_unit: row.billing_unit || "token",
|
|
549
|
+
pricing_mode: row.pricing_mode || "per_token",
|
|
550
|
+
unit_size: toNum(row.unit_size),
|
|
551
|
+
variant: row.variant || "",
|
|
552
|
+
currency: row.currency || "CNY",
|
|
553
|
+
cost_input_per_1m: toNum(row.cost_input_per_1m),
|
|
554
|
+
cost_output_per_1m: toNum(row.cost_output_per_1m),
|
|
555
|
+
cost_cached_input_per_1m: numOrNull(row.cost_cached_input_per_1m),
|
|
556
|
+
cost_unit_price: toNum(row.cost_unit_price),
|
|
557
|
+
sale_input_per_1m: numOrNull(row.sale_input_per_1m),
|
|
558
|
+
sale_output_per_1m: numOrNull(row.sale_output_per_1m),
|
|
559
|
+
sale_unit_price: numOrNull(row.sale_unit_price),
|
|
560
|
+
source: row.source || "manual",
|
|
561
|
+
effective_from: toIsoDate(row.effective_from),
|
|
562
|
+
effective_to: toIsoDate(row.effective_to),
|
|
563
|
+
status: toInt(row.status),
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
function parsePriceNumber(value, def = 0) {
|
|
567
|
+
const text = value === null || value === undefined ? "" : String(value).trim();
|
|
568
|
+
if (!text)
|
|
569
|
+
return def;
|
|
570
|
+
const n = Number(text);
|
|
571
|
+
if (!Number.isFinite(n)) {
|
|
572
|
+
throw new Error(`could not convert string to float: '${text}'`);
|
|
573
|
+
}
|
|
574
|
+
return n;
|
|
575
|
+
}
|
|
576
|
+
function parseOptionalPriceNumber(value) {
|
|
577
|
+
const text = value === null || value === undefined ? "" : String(value).trim();
|
|
578
|
+
if (!text)
|
|
579
|
+
return null;
|
|
580
|
+
const n = Number(text);
|
|
581
|
+
if (!Number.isFinite(n)) {
|
|
582
|
+
throw new Error(`could not convert string to float: '${text}'`);
|
|
583
|
+
}
|
|
584
|
+
return n;
|
|
585
|
+
}
|
|
586
|
+
/** 简单 CSV 解析(RFC4180,支持引号包裹与转义) */
|
|
587
|
+
function parseCsv(text) {
|
|
588
|
+
const rows = [];
|
|
589
|
+
let row = [];
|
|
590
|
+
let field = "";
|
|
591
|
+
let inQuotes = false;
|
|
592
|
+
for (let i = 0; i < text.length; i++) {
|
|
593
|
+
const ch = text[i];
|
|
594
|
+
if (inQuotes) {
|
|
595
|
+
if (ch === '"') {
|
|
596
|
+
if (text[i + 1] === '"') {
|
|
597
|
+
field += '"';
|
|
598
|
+
i++;
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
inQuotes = false;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
else {
|
|
605
|
+
field += ch;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
else if (ch === '"') {
|
|
609
|
+
inQuotes = true;
|
|
610
|
+
}
|
|
611
|
+
else if (ch === ",") {
|
|
612
|
+
row.push(field);
|
|
613
|
+
field = "";
|
|
614
|
+
}
|
|
615
|
+
else if (ch === "\n") {
|
|
616
|
+
row.push(field);
|
|
617
|
+
rows.push(row);
|
|
618
|
+
row = [];
|
|
619
|
+
field = "";
|
|
620
|
+
}
|
|
621
|
+
else if (ch !== "\r") {
|
|
622
|
+
field += ch;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
if (field !== "" || row.length) {
|
|
626
|
+
row.push(field);
|
|
627
|
+
rows.push(row);
|
|
628
|
+
}
|
|
629
|
+
return rows;
|
|
630
|
+
}
|
|
631
|
+
function readCsvRows(content) {
|
|
632
|
+
const text = content.toString("utf8").replace(/^\ufeff/, "");
|
|
633
|
+
const lines = text
|
|
634
|
+
.split(/\r?\n/)
|
|
635
|
+
.filter((line) => !line.trimStart().startsWith("#"));
|
|
636
|
+
const parsed = parseCsv(lines.join("\n"));
|
|
637
|
+
if (!parsed.length)
|
|
638
|
+
return [];
|
|
639
|
+
const keys = parsed[0].map((name) => normalizePriceHeader(name));
|
|
640
|
+
const result = [];
|
|
641
|
+
for (const raw of parsed.slice(1)) {
|
|
642
|
+
if (!raw.length || raw.every((value) => String(value).trim() === ""))
|
|
643
|
+
continue;
|
|
644
|
+
const row = {};
|
|
645
|
+
keys.forEach((key, index) => {
|
|
646
|
+
if (!key)
|
|
647
|
+
return;
|
|
648
|
+
row[key] = raw[index] ?? "";
|
|
649
|
+
});
|
|
650
|
+
result.push(row);
|
|
651
|
+
}
|
|
652
|
+
return result;
|
|
653
|
+
}
|
|
654
|
+
//# sourceMappingURL=service.js.map
|