@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.
Files changed (165) hide show
  1. package/dist/reglow/core/entities.d.ts +135 -0
  2. package/dist/reglow/core/models.d.ts +22 -0
  3. package/dist/reglow/core/models.js +15 -0
  4. package/dist/reglow/core/models.js.map +1 -1
  5. package/dist/reglow/core/tenant_context.d.ts +45 -0
  6. package/dist/reglow/core/tenant_context.js +77 -0
  7. package/dist/reglow/core/tenant_context.js.map +1 -0
  8. package/dist/reglow/core/tenant_guard.js +17 -5
  9. package/dist/reglow/core/tenant_guard.js.map +1 -1
  10. package/dist/reglow/modules/ai/controller.js +129 -0
  11. package/dist/reglow/modules/ai/controller.js.map +1 -1
  12. package/dist/reglow/modules/ai_usage/controller.d.ts +8 -0
  13. package/dist/reglow/modules/ai_usage/controller.js +278 -0
  14. package/dist/reglow/modules/ai_usage/controller.js.map +1 -0
  15. package/dist/reglow/modules/ai_usage/model.d.ts +216 -0
  16. package/dist/reglow/modules/ai_usage/model.js +25 -0
  17. package/dist/reglow/modules/ai_usage/model.js.map +1 -0
  18. package/dist/reglow/modules/ai_usage/pricing.d.ts +80 -0
  19. package/dist/reglow/modules/ai_usage/pricing.js +276 -0
  20. package/dist/reglow/modules/ai_usage/pricing.js.map +1 -0
  21. package/dist/reglow/modules/ai_usage/reconcile.d.ts +34 -0
  22. package/dist/reglow/modules/ai_usage/reconcile.js +179 -0
  23. package/dist/reglow/modules/ai_usage/reconcile.js.map +1 -0
  24. package/dist/reglow/modules/ai_usage/recorder.d.ts +14 -0
  25. package/dist/reglow/modules/ai_usage/recorder.js +227 -0
  26. package/dist/reglow/modules/ai_usage/recorder.js.map +1 -0
  27. package/dist/reglow/modules/ai_usage/router.d.ts +10 -0
  28. package/dist/reglow/modules/ai_usage/router.js +10 -0
  29. package/dist/reglow/modules/ai_usage/router.js.map +1 -0
  30. package/dist/reglow/modules/ai_usage/schema.d.ts +152 -0
  31. package/dist/reglow/modules/ai_usage/schema.js +8 -0
  32. package/dist/reglow/modules/ai_usage/schema.js.map +1 -0
  33. package/dist/reglow/modules/ai_usage/service.d.ts +40 -0
  34. package/dist/reglow/modules/ai_usage/service.js +654 -0
  35. package/dist/reglow/modules/ai_usage/service.js.map +1 -0
  36. package/dist/reglow/modules/ai_usage/sync.d.ts +98 -0
  37. package/dist/reglow/modules/ai_usage/sync.js +954 -0
  38. package/dist/reglow/modules/ai_usage/sync.js.map +1 -0
  39. package/dist/reglow/modules/ai_usage/xlsx_io.d.ts +35 -0
  40. package/dist/reglow/modules/ai_usage/xlsx_io.js +494 -0
  41. package/dist/reglow/modules/ai_usage/xlsx_io.js.map +1 -0
  42. package/dist/reglow/modules/billing/controller.d.ts +6 -0
  43. package/dist/reglow/modules/billing/controller.js +112 -0
  44. package/dist/reglow/modules/billing/controller.js.map +1 -0
  45. package/dist/reglow/modules/billing/repository.d.ts +20 -0
  46. package/dist/reglow/modules/billing/repository.js +101 -0
  47. package/dist/reglow/modules/billing/repository.js.map +1 -0
  48. package/dist/reglow/modules/billing/router.d.ts +10 -0
  49. package/dist/reglow/modules/billing/router.js +8 -0
  50. package/dist/reglow/modules/billing/router.js.map +1 -0
  51. package/dist/reglow/modules/billing/schema.d.ts +38 -0
  52. package/dist/reglow/modules/billing/schema.js +42 -0
  53. package/dist/reglow/modules/billing/schema.js.map +1 -0
  54. package/dist/reglow/modules/billing/service.d.ts +22 -0
  55. package/dist/reglow/modules/billing/service.js +179 -0
  56. package/dist/reglow/modules/billing/service.js.map +1 -0
  57. package/dist/reglow/modules/customer_service/controller.js +81 -3
  58. package/dist/reglow/modules/customer_service/controller.js.map +1 -1
  59. package/dist/reglow/modules/customer_service/repository.d.ts +9 -1
  60. package/dist/reglow/modules/customer_service/repository.js +56 -2
  61. package/dist/reglow/modules/customer_service/repository.js.map +1 -1
  62. package/dist/reglow/modules/customer_service/schema.d.ts +2 -0
  63. package/dist/reglow/modules/customer_service/schema.js +8 -0
  64. package/dist/reglow/modules/customer_service/schema.js.map +1 -1
  65. package/dist/reglow/modules/customer_service/sdk_template.html +276 -5
  66. package/dist/reglow/modules/customer_service/service.d.ts +45 -4
  67. package/dist/reglow/modules/customer_service/service.js +352 -96
  68. package/dist/reglow/modules/customer_service/service.js.map +1 -1
  69. package/dist/reglow/modules/customer_service/ws.d.ts +8 -1
  70. package/dist/reglow/modules/customer_service/ws.js +38 -1
  71. package/dist/reglow/modules/customer_service/ws.js.map +1 -1
  72. package/dist/reglow/modules/open_api/auth.d.ts +28 -0
  73. package/dist/reglow/modules/open_api/auth.js +65 -0
  74. package/dist/reglow/modules/open_api/auth.js.map +1 -0
  75. package/dist/reglow/modules/open_api/controller.d.ts +8 -0
  76. package/dist/reglow/modules/open_api/controller.js +109 -0
  77. package/dist/reglow/modules/open_api/controller.js.map +1 -0
  78. package/dist/reglow/modules/open_api/repository.d.ts +20 -0
  79. package/dist/reglow/modules/open_api/repository.js +111 -0
  80. package/dist/reglow/modules/open_api/repository.js.map +1 -0
  81. package/dist/reglow/modules/open_api/router.d.ts +10 -0
  82. package/dist/reglow/modules/open_api/router.js +8 -0
  83. package/dist/reglow/modules/open_api/router.js.map +1 -0
  84. package/dist/reglow/modules/open_api/schema.d.ts +24 -0
  85. package/dist/reglow/modules/open_api/schema.js +26 -0
  86. package/dist/reglow/modules/open_api/schema.js.map +1 -0
  87. package/dist/reglow/modules/open_api/security.d.ts +19 -0
  88. package/dist/reglow/modules/open_api/security.js +53 -0
  89. package/dist/reglow/modules/open_api/security.js.map +1 -0
  90. package/dist/reglow/modules/open_api/service.d.ts +28 -0
  91. package/dist/reglow/modules/open_api/service.js +158 -0
  92. package/dist/reglow/modules/open_api/service.js.map +1 -0
  93. package/dist/reglow/modules/principal/controller.d.ts +6 -0
  94. package/dist/reglow/modules/principal/controller.js +158 -0
  95. package/dist/reglow/modules/principal/controller.js.map +1 -0
  96. package/dist/reglow/modules/principal/repository.d.ts +20 -0
  97. package/dist/reglow/modules/principal/repository.js +76 -0
  98. package/dist/reglow/modules/principal/repository.js.map +1 -0
  99. package/dist/reglow/modules/principal/router.d.ts +9 -0
  100. package/dist/reglow/modules/principal/router.js +7 -0
  101. package/dist/reglow/modules/principal/router.js.map +1 -0
  102. package/dist/reglow/modules/principal/schema.d.ts +81 -0
  103. package/dist/reglow/modules/principal/schema.js +85 -0
  104. package/dist/reglow/modules/principal/schema.js.map +1 -0
  105. package/dist/reglow/modules/principal/service.d.ts +21 -0
  106. package/dist/reglow/modules/principal/service.js +193 -0
  107. package/dist/reglow/modules/principal/service.js.map +1 -0
  108. package/dist/reglow/modules/principal_member/controller.d.ts +6 -0
  109. package/dist/reglow/modules/principal_member/controller.js +64 -0
  110. package/dist/reglow/modules/principal_member/controller.js.map +1 -0
  111. package/dist/reglow/modules/principal_member/repository.d.ts +13 -0
  112. package/dist/reglow/modules/principal_member/repository.js +80 -0
  113. package/dist/reglow/modules/principal_member/repository.js.map +1 -0
  114. package/dist/reglow/modules/principal_member/router.d.ts +10 -0
  115. package/dist/reglow/modules/principal_member/router.js +8 -0
  116. package/dist/reglow/modules/principal_member/router.js.map +1 -0
  117. package/dist/reglow/modules/principal_member/schema.d.ts +27 -0
  118. package/dist/reglow/modules/principal_member/schema.js +28 -0
  119. package/dist/reglow/modules/principal_member/schema.js.map +1 -0
  120. package/dist/reglow/modules/principal_member/service.d.ts +16 -0
  121. package/dist/reglow/modules/principal_member/service.js +135 -0
  122. package/dist/reglow/modules/principal_member/service.js.map +1 -0
  123. package/dist/reglow/modules/tenant/controller.d.ts +6 -0
  124. package/dist/reglow/modules/tenant/controller.js +99 -0
  125. package/dist/reglow/modules/tenant/controller.js.map +1 -0
  126. package/dist/reglow/modules/tenant/repository.d.ts +18 -0
  127. package/dist/reglow/modules/tenant/repository.js +71 -0
  128. package/dist/reglow/modules/tenant/repository.js.map +1 -0
  129. package/dist/reglow/modules/tenant/router.d.ts +9 -0
  130. package/dist/reglow/modules/tenant/router.js +7 -0
  131. package/dist/reglow/modules/tenant/router.js.map +1 -0
  132. package/dist/reglow/modules/tenant/schema.d.ts +33 -0
  133. package/dist/reglow/modules/tenant/schema.js +42 -0
  134. package/dist/reglow/modules/tenant/schema.js.map +1 -0
  135. package/dist/reglow/modules/tenant/service.d.ts +16 -0
  136. package/dist/reglow/modules/tenant/service.js +75 -0
  137. package/dist/reglow/modules/tenant/service.js.map +1 -0
  138. package/dist/reglow/modules/workflow/controller.d.ts +7 -0
  139. package/dist/reglow/modules/workflow/controller.js +133 -0
  140. package/dist/reglow/modules/workflow/controller.js.map +1 -0
  141. package/dist/reglow/modules/workflow/dsl.d.ts +41 -0
  142. package/dist/reglow/modules/workflow/dsl.js +173 -0
  143. package/dist/reglow/modules/workflow/dsl.js.map +1 -0
  144. package/dist/reglow/modules/workflow/model.d.ts +15 -0
  145. package/dist/reglow/modules/workflow/model.js +16 -0
  146. package/dist/reglow/modules/workflow/model.js.map +1 -0
  147. package/dist/reglow/modules/workflow/repository.d.ts +27 -0
  148. package/dist/reglow/modules/workflow/repository.js +151 -0
  149. package/dist/reglow/modules/workflow/repository.js.map +1 -0
  150. package/dist/reglow/modules/workflow/router.d.ts +10 -0
  151. package/dist/reglow/modules/workflow/router.js +8 -0
  152. package/dist/reglow/modules/workflow/router.js.map +1 -0
  153. package/dist/reglow/modules/workflow/schema.d.ts +32 -0
  154. package/dist/reglow/modules/workflow/schema.js +39 -0
  155. package/dist/reglow/modules/workflow/schema.js.map +1 -0
  156. package/dist/reglow/modules/workflow/service.d.ts +65 -0
  157. package/dist/reglow/modules/workflow/service.js +209 -0
  158. package/dist/reglow/modules/workflow/service.js.map +1 -0
  159. package/dist/reglow/scripts/init-data.js +157 -1
  160. package/dist/reglow/scripts/init-data.js.map +1 -1
  161. package/dist/reglow/scripts/schema.js +360 -0
  162. package/dist/reglow/scripts/schema.js.map +1 -1
  163. package/dist/server.js +3 -0
  164. package/dist/server.js.map +1 -1
  165. package/package.json +3 -3
@@ -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
+ };
@@ -0,0 +1,276 @@
1
+ import { AI_MODEL_PRICE_TABLE, PRICING_FREE, PRICING_MODE_IMAGE, PRICING_MODE_MEGAPIXEL, PRICING_MODE_REQUEST, PRICING_MODE_SECOND, PRICING_MODE_TOKEN, PRICING_MODE_VIDEO_TOKEN, PRICING_PRICED, } from "./model.js";
2
+ export const MONEY_SCALE = 8;
3
+ const SCALE_QTY = 6;
4
+ /** 峰谷时段价格:DeepSeek 等按北京时间区分高峰 / 空闲 */
5
+ const PEAK_VARIANTS = ["peak", "高峰", "高峰时段"];
6
+ const OFFPEAK_VARIANTS = ["offpeak", "off-peak", "空闲", "空闲时段"];
7
+ function toDate(value) {
8
+ if (value === null || value === undefined)
9
+ return null;
10
+ const d = value instanceof Date ? value : new Date(value);
11
+ return Number.isNaN(d.getTime()) ? null : d;
12
+ }
13
+ export function peakBucket(at) {
14
+ const d = toDate(at) ?? new Date();
15
+ // 北京时间 = UTC+8,与服务器本地时区无关
16
+ const beijing = new Date(d.getTime() + 8 * 60 * 60 * 1000);
17
+ const weekday = beijing.getUTCDay();
18
+ const hour = beijing.getUTCHours();
19
+ const isPeak = weekday < 5 && ((hour >= 9 && hour < 12) || (hour >= 14 && hour < 18));
20
+ return isPeak ? "peak" : "offpeak";
21
+ }
22
+ // ─────────────── 定点十进制 ───────────────
23
+ function expandExponential(s) {
24
+ const m = /^(-?)(\d+)(?:\.(\d+))?[eE]([+-]?\d+)$/.exec(s);
25
+ if (!m)
26
+ return s;
27
+ const sign = m[1];
28
+ const intPart = m[2];
29
+ const frac = m[3] ?? "";
30
+ const exp = parseInt(m[4], 10);
31
+ const digits = intPart + frac;
32
+ const pointPos = intPart.length + exp;
33
+ let out;
34
+ if (pointPos <= 0)
35
+ out = `0.${"0".repeat(-pointPos)}${digits}`;
36
+ else if (pointPos >= digits.length)
37
+ out = digits + "0".repeat(pointPos - digits.length);
38
+ else
39
+ out = `${digits.slice(0, pointPos)}.${digits.slice(pointPos)}`;
40
+ return sign + out;
41
+ }
42
+ /** 解析为放大 10^scale 的整数,超出精度按 ROUND_HALF_UP */
43
+ function toScaled(value, scale) {
44
+ if (value === null || value === undefined)
45
+ return 0n;
46
+ if (typeof value === "bigint")
47
+ return value * 10n ** BigInt(scale);
48
+ let s = String(value).trim();
49
+ if (!s)
50
+ return 0n;
51
+ if (s.includes("e") || s.includes("E"))
52
+ s = expandExponential(s);
53
+ let sign = 1n;
54
+ if (s.startsWith("-")) {
55
+ sign = -1n;
56
+ s = s.slice(1);
57
+ }
58
+ else if (s.startsWith("+")) {
59
+ s = s.slice(1);
60
+ }
61
+ const [intRaw, fracRaw = ""] = s.split(".");
62
+ const intPart = (intRaw || "0").replace(/[^\d]/g, "") || "0";
63
+ const fracDigits = fracRaw.replace(/[^\d]/g, "");
64
+ let scaled = BigInt(intPart) * 10n ** BigInt(scale);
65
+ if (scale > 0) {
66
+ const head = fracDigits.slice(0, scale).padEnd(scale, "0");
67
+ scaled += BigInt(head || "0");
68
+ const rest = fracDigits.slice(scale);
69
+ if (rest && rest[0] >= "5")
70
+ scaled += 1n;
71
+ }
72
+ return sign * scaled;
73
+ }
74
+ function fromScaled(value, scale) {
75
+ return Number(value) / 10 ** scale;
76
+ }
77
+ /** 非负整除并四舍五入(Python ROUND_HALF_UP:远离 0) */
78
+ function divHalfUp(num, den) {
79
+ if (den === 0n)
80
+ return 0n;
81
+ const negative = num < 0n !== den < 0n;
82
+ const a = num < 0n ? -num : num;
83
+ const b = den < 0n ? -den : den;
84
+ let q = a / b;
85
+ const r = a % b;
86
+ if (r * 2n >= b)
87
+ q += 1n;
88
+ return negative ? -q : q;
89
+ }
90
+ /** 金额保留 8 位小数,四舍五入 */
91
+ export function quantizeMoney(value) {
92
+ return fromScaled(toScaled(value, MONEY_SCALE), MONEY_SCALE);
93
+ }
94
+ function rowToSnapshot(row) {
95
+ const source = row.source || "manual";
96
+ const unitSize = toScaled(row.unit_size, SCALE_QTY);
97
+ return {
98
+ id: row.id ?? null,
99
+ provider: row.provider || "",
100
+ model: row.model || "",
101
+ model_type: row.model_type || "t2t",
102
+ billing_unit: row.billing_unit || "token",
103
+ pricing_mode: row.pricing_mode || PRICING_MODE_TOKEN,
104
+ // Python:_dec(unit_size) or 1000000,0 与 null 都回落 1M
105
+ unit_size: unitSize === 0n ? 1_000_000 : (row.unit_size ?? 1_000_000),
106
+ rounding: row.rounding || "exact",
107
+ variant: row.variant || "",
108
+ currency: row.currency || "CNY",
109
+ input_price: row.cost_input_per_1m ?? 0,
110
+ output_price: row.cost_output_per_1m ?? 0,
111
+ cached_input_price: row.cost_cached_input_per_1m === null || row.cost_cached_input_per_1m === undefined
112
+ ? null
113
+ : row.cost_cached_input_per_1m,
114
+ unit_price: row.cost_unit_price ?? 0,
115
+ sale_input_price: row.sale_input_per_1m === null || row.sale_input_per_1m === undefined
116
+ ? null
117
+ : row.sale_input_per_1m,
118
+ sale_output_price: row.sale_output_per_1m === null || row.sale_output_per_1m === undefined
119
+ ? null
120
+ : row.sale_output_per_1m,
121
+ sale_unit_price: row.sale_unit_price === null || row.sale_unit_price === undefined
122
+ ? null
123
+ : row.sale_unit_price,
124
+ source,
125
+ price_source: source,
126
+ };
127
+ }
128
+ /** 匹配顺序:显式档位 → 空串通用 → 按调用时间选峰谷 → 任意 */
129
+ export function pickVariant(rows, variant, at) {
130
+ if (!rows.length)
131
+ return null;
132
+ if (variant) {
133
+ for (const row of rows)
134
+ if ((row.variant || "") === variant)
135
+ return row;
136
+ for (const row of rows)
137
+ if (!row.variant)
138
+ return row;
139
+ return null;
140
+ }
141
+ for (const row of rows)
142
+ if (!row.variant)
143
+ return row;
144
+ const aliases = peakBucket(at) === "peak" ? PEAK_VARIANTS : OFFPEAK_VARIANTS;
145
+ for (const alias of aliases) {
146
+ for (const row of rows)
147
+ if ((row.variant || "") === alias)
148
+ return row;
149
+ }
150
+ return rows[0];
151
+ }
152
+ export async function resolvePrice(db, params) {
153
+ const at = toDate(params.at) ?? new Date();
154
+ const windowAt = toDate(params.effective_at) ?? at;
155
+ const rows = (await db(AI_MODEL_PRICE_TABLE)
156
+ .where({
157
+ provider: params.provider,
158
+ model: params.model,
159
+ model_type: params.model_type,
160
+ billing_unit: params.billing_unit,
161
+ pricing_mode: params.pricing_mode,
162
+ status: 1,
163
+ })
164
+ .where("effective_from", "<=", windowAt)
165
+ .andWhere((qb) => {
166
+ qb.whereNull("effective_to").orWhere("effective_to", ">", windowAt);
167
+ })
168
+ .orderBy([
169
+ { column: "effective_from", order: "desc" },
170
+ { column: "id", order: "desc" },
171
+ ]));
172
+ const row = pickVariant(rows, params.variant, at);
173
+ return row ? rowToSnapshot(row) : null;
174
+ }
175
+ // ─────────────── 成本计算 ───────────────
176
+ function nonNegativeInt(value) {
177
+ const n = Math.trunc(Number(value));
178
+ return Number.isFinite(n) && n > 0 ? n : 0;
179
+ }
180
+ /** 文本 / embedding 成本与售价;缓存价缺失时回退输入价并返回 fallback */
181
+ export function calcTextCost(price, params) {
182
+ const promptTokens = nonNegativeInt(params.prompt_tokens);
183
+ const completionTokens = nonNegativeInt(params.completion_tokens);
184
+ const cachedTokens = Math.min(nonNegativeInt(params.cached_tokens), promptTokens);
185
+ const uncached = promptTokens - cachedTokens;
186
+ const inputScaled = toScaled(price.input_price, MONEY_SCALE);
187
+ const outputScaled = toScaled(price.output_price, MONEY_SCALE);
188
+ const cachedScaled = price.cached_input_price === null || price.cached_input_price === undefined
189
+ ? null
190
+ : toScaled(price.cached_input_price, MONEY_SCALE);
191
+ let status = PRICING_PRICED;
192
+ let effectiveCached = cachedScaled;
193
+ if (cachedTokens > 0 && effectiveCached === null) {
194
+ effectiveCached = inputScaled;
195
+ status = "fallback";
196
+ }
197
+ // 三项分母同为 1e6,先求和再一次性四舍五入,与 Python Decimal 行为一致
198
+ const numerator = BigInt(uncached) * inputScaled +
199
+ BigInt(cachedTokens) * (effectiveCached ?? 0n) +
200
+ BigInt(completionTokens) * outputScaled;
201
+ const costScaled = divHalfUp(numerator, 1000000n);
202
+ let sale = null;
203
+ if ((price.sale_input_price !== null && price.sale_input_price !== undefined) ||
204
+ (price.sale_output_price !== null && price.sale_output_price !== undefined)) {
205
+ const saleInput = toScaled(price.sale_input_price, MONEY_SCALE);
206
+ const saleOutput = toScaled(price.sale_output_price, MONEY_SCALE);
207
+ const saleNumerator = BigInt(uncached + cachedTokens) * saleInput + BigInt(completionTokens) * saleOutput;
208
+ sale = fromScaled(divHalfUp(saleNumerator, 1000000n), MONEY_SCALE);
209
+ }
210
+ if (costScaled === 0n && inputScaled === 0n && outputScaled === 0n) {
211
+ status = PRICING_FREE;
212
+ }
213
+ return [fromScaled(costScaled, MONEY_SCALE), sale, status];
214
+ }
215
+ /** 按计价模式计算参与成本的统一数量 */
216
+ export function computeBillingQuantity(params) {
217
+ const qty = Number(params.quantity ?? 0) || 0;
218
+ const duration = Number(params.duration_seconds ?? 0) || 0;
219
+ switch (params.pricing_mode) {
220
+ case PRICING_MODE_TOKEN:
221
+ return 1;
222
+ case PRICING_MODE_IMAGE:
223
+ return qty;
224
+ case PRICING_MODE_SECOND:
225
+ if (params.rounding === "ceil" && duration > 0)
226
+ return Math.ceil(duration);
227
+ return duration;
228
+ case PRICING_MODE_REQUEST:
229
+ return 1;
230
+ case PRICING_MODE_MEGAPIXEL:
231
+ if (!params.width || !params.height)
232
+ return 0;
233
+ return ((Number(params.width) * Number(params.height)) / 1_000_000) * Math.max(qty, 1);
234
+ case PRICING_MODE_VIDEO_TOKEN:
235
+ return Number(params.video_tokens ?? 0) || 0;
236
+ default:
237
+ return qty;
238
+ }
239
+ }
240
+ /** 图片 / 视频成本与售价 */
241
+ export function calcMediaCost(price, billingQuantity) {
242
+ const unitSizeScaled = toScaled(price.unit_size, SCALE_QTY);
243
+ const qtyScaled = toScaled(billingQuantity, 9);
244
+ const unitPriceScaled = toScaled(price.unit_price, MONEY_SCALE);
245
+ let costScaled = 0n;
246
+ if (unitSizeScaled !== 0n) {
247
+ // units = qty / unit_size;金额放大 1e8
248
+ costScaled = divHalfUp(qtyScaled * unitPriceScaled, unitSizeScaled * 1000n);
249
+ }
250
+ let sale = null;
251
+ if (price.sale_unit_price !== null && price.sale_unit_price !== undefined) {
252
+ const saleUnitScaled = toScaled(price.sale_unit_price, MONEY_SCALE);
253
+ sale =
254
+ unitSizeScaled === 0n
255
+ ? 0
256
+ : fromScaled(divHalfUp(qtyScaled * saleUnitScaled, unitSizeScaled * 1000n), MONEY_SCALE);
257
+ }
258
+ let status = PRICING_PRICED;
259
+ if (costScaled === 0n && unitPriceScaled === 0n)
260
+ status = PRICING_FREE;
261
+ return [fromScaled(costScaled, MONEY_SCALE), sale, status];
262
+ }
263
+ /** 写入 sys_ai_usage 的价格快照字段 */
264
+ export function priceSnapshotFields(price) {
265
+ return {
266
+ price_id: price.id,
267
+ price_source: price.price_source,
268
+ price_currency: price.currency,
269
+ pricing_mode: price.pricing_mode,
270
+ input_price: price.input_price,
271
+ output_price: price.output_price,
272
+ cached_input_price: price.cached_input_price,
273
+ unit_price: price.unit_price,
274
+ };
275
+ }
276
+ //# sourceMappingURL=pricing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pricing.js","sourceRoot":"","sources":["../../../../reglow/modules/ai_usage/pricing.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,oBAAoB,EACpB,YAAY,EACZ,kBAAkB,EAClB,sBAAsB,EACtB,oBAAoB,EACpB,mBAAmB,EACnB,kBAAkB,EAClB,wBAAwB,EACxB,cAAc,GAGf,MAAM,YAAY,CAAC;AAEpB,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAC;AAC7B,MAAM,SAAS,GAAG,CAAC,CAAC;AAEpB,sCAAsC;AACtC,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;AAC7C,MAAM,gBAAgB,GAAG,CAAC,SAAS,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;AAE/D,SAAS,MAAM,CAAC,KAAgD;IAC9D,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IACvD,MAAM,CAAC,GAAG,KAAK,YAAY,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1D,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AAC9C,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,EAA6C;IACtE,MAAM,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;QAClB,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,cAAc,EAAE,KAAK,CAAC,QAAQ;QAC9B,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,WAAW,EAAE,KAAK,CAAC,WAAW;QAC9B,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,kBAAkB,EAAE,KAAK,CAAC,kBAAkB;QAC5C,UAAU,EAAE,KAAK,CAAC,UAAU;KAC7B,CAAC;AACJ,CAAC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * AI 媒体 submitted 任务对账(§15.2 B-3)
3
+ * 对应 backend-python 的 reglow/modules/ai_usage/reconcile.py
4
+ *
5
+ * 定时扫描未完成任务,调用已注册的媒体轮询器转终态并重新计价。
6
+ * 未注册轮询器时任务保持 running 直到超时(与 Python 在无 adapter 时一致)。
7
+ */
8
+ import type { Knex } from "knex";
9
+ import { type AiUsageRow } from "./model.js";
10
+ /** 媒体任务轮询结果(与 gateway.media_poll 返回结构对齐) */
11
+ export interface MediaJobResult {
12
+ status: string;
13
+ quantity?: number | null;
14
+ duration_seconds?: number | null;
15
+ resolution?: string | null;
16
+ variant?: string | null;
17
+ error?: string | null;
18
+ actual?: boolean | null;
19
+ }
20
+ export type MediaKind = "image" | "video";
21
+ export type MediaPoller = (row: AiUsageRow) => Promise<MediaJobResult>;
22
+ /** 注册媒体任务轮询器(kind: image / video) */
23
+ export declare function registerMediaPoller(provider: string, kind: MediaKind, fn: MediaPoller): void;
24
+ /** 对账单条记录;异常只记录,不影响其他任务 */
25
+ export declare function reconcileRow(trx: Knex, row: AiUsageRow): Promise<AiUsageRow>;
26
+ /** 扫描到期任务并收尾;供定时任务与手动接口调用 */
27
+ export declare function reconcileDue(limit?: number): Promise<{
28
+ scanned: number;
29
+ done: number;
30
+ failed: number;
31
+ }>;
32
+ /** 启动 60s 定时对账(测试环境不启动,定时器 unref 不阻塞进程退出) */
33
+ export declare function startReconcileTimer(): void;
34
+ export declare function stopReconcileTimer(): void;