dsh-thinking-levels 4.2.1 → 4.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,10 +7,16 @@
7
7
  * replacement settles old-minus-new, turn aggregation resets on turn switch,
8
8
  * session totals price every sample at its own event-time rate.
9
9
  *
10
- * Pricing: single model DeepSeek-V4.1-Flash, Beijing-time peak/off-peak
11
- * (workdays 09-12 & 14-18 peak, weekends and CN statutory holidays off-peak
12
- * all day; pure UTC+8 math, never the local clock). Unmatched model names are
13
- * still priced at Flash rates and flagged via modelMatched=false.
10
+ * 目录化改造 T3:价格与日历判定全部下沉 `price-catalog.ts`(唯一事实源),本单
11
+ * 元只做记账。计价在**折入时刻**结算并作为凭证(`Sample.billed`)持久进 state:
12
+ * - 同 step 替换「减记录值、加新算值」——杜绝按新费率重算旧账的混口径负数路径;
13
+ * - 金额按币种分桶(CNY/USD),跨币种会话不再相加失真;
14
+ * - `view` 只读凭证不重算,设置改价不会漂移已展示的读数。
15
+ * 语义边界:改价只影响在线增量新账;冷重放(checkpoint 废弃后整段重折)必然
16
+ * 按当前目录重算——stateVersion 2 即为此声明(宿主对旧 ver 行整行废弃重折)。
17
+ *
18
+ * 计价快照经 `buildBillingDefinition(getPricing)` 闭包注入,按事件时刻现读;
19
+ * **热路径绝不走 describeSettings**(宿主 describe 非轻量且会 emit 事件)。
14
20
  *
15
21
  * Schema library: the projection framework duck-types `stateSchema.parse()` /
16
22
  * `wire.viewSchema.parse()` (dsh-session-projection lib:255/259/305), so these
@@ -19,74 +25,21 @@
19
25
  * @module dsh-thinking-levels/billing-projection
20
26
  */
21
27
  import { z } from 'zod';
22
- /** 账目只认 DeepSeek-V4.1-Flash:现名、旧名与历史变体全部由 Flash 服务。 */
23
- export const BILLING_MODEL = {
24
- key: 'deepseek-v4.1-flash',
25
- label: 'DeepSeek-V4.1-Flash',
26
- aliases: [
27
- 'deepseek-v4.1-flash',
28
- 'deepseek-flash',
29
- 'deepseek-v4-flash',
30
- 'deepseek-v4-flash-vision-exp',
31
- ],
32
- peak: { cacheHit: 0.04, cacheMiss: 2, output: 8 },
33
- offPeak: { cacheHit: 0.02, cacheMiss: 1, output: 4 },
34
- };
35
- /**
36
- * 中国法定节假日放假日(北京时间 YYYY-MM-DD),2026 年国务院安排 33 天(holiday-cn)。
37
- * 只收放假日不收调休补班周末;表外年份退化为只认周末。
38
- */
39
- export const HOLIDAYS_2026 = new Set([
40
- '2026-01-01', '2026-01-02', '2026-01-03',
41
- '2026-02-15', '2026-02-16', '2026-02-17', '2026-02-18', '2026-02-19',
42
- '2026-02-20', '2026-02-21', '2026-02-22', '2026-02-23',
43
- '2026-04-04', '2026-04-05', '2026-04-06',
44
- '2026-05-01', '2026-05-02', '2026-05-03', '2026-05-04', '2026-05-05',
45
- '2026-06-19', '2026-06-20', '2026-06-21',
46
- '2026-09-25', '2026-09-26', '2026-09-27',
47
- '2026-10-01', '2026-10-02', '2026-10-03', '2026-10-04', '2026-10-05',
48
- '2026-10-06', '2026-10-07',
49
- ]);
50
- /** 时刻是否为北京高峰。UTC+8 纯数学换算;无效时间戳用 getUTC* 拼串不抛错。 */
51
- export function isPeakBeijing(timeMs) {
52
- const shifted = new Date(timeMs + 8 * 3600 * 1000);
53
- const day = shifted.getUTCDay();
54
- if (day === 0 || day === 6)
55
- return false;
56
- const month = String(shifted.getUTCMonth() + 1).padStart(2, '0');
57
- const dayOfMonth = String(shifted.getUTCDate()).padStart(2, '0');
58
- if (HOLIDAYS_2026.has(`${shifted.getUTCFullYear()}-${month}-${dayOfMonth}`))
59
- return false;
60
- const hour = shifted.getUTCHours();
61
- return (hour >= 9 && hour < 12) || (hour >= 14 && hour < 18);
62
- }
63
- /** 模型在某时刻的费率行 + 峰谷 + 白名单命中标记。 */
64
- export function rateOf(model, timeMs) {
65
- const key = (model ?? '').toLowerCase();
66
- const tier = isPeakBeijing(timeMs) ? 'peak' : 'offPeak';
67
- const row = tier === 'peak' ? BILLING_MODEL.peak : BILLING_MODEL.offPeak;
68
- const matched = BILLING_MODEL.aliases.some((alias) => key === alias || key.endsWith(alias) || key.includes(alias));
69
- return { row, tier, matchedModel: BILLING_MODEL.label, matched };
70
- }
28
+ import { resolveRate, } from "./price-catalog.js";
29
+ // 日历与峰谷判定迁至目录模块;旧导入面在此保持兼容(tests/index 均走本模块)。
30
+ export { isPeakBeijing, HOLIDAY_TABLE_2026, HOLIDAY_TABLES } from "./price-catalog.js";
31
+ export { PRICE_CATALOG, CATALOG_VERSION } from "./price-catalog.js";
32
+ /** 目录外模型的兜底档位(设置面 defaultPricing 的值域)。 */
33
+ const POLICY_VALUES = ['cn', 'global', 'custom', 'off'];
71
34
  const round9 = (n) => Math.round(n * 1e9) / 1e9;
72
- /** 按样本模型与事件时刻计一轮三笔费用(元)。 */
73
- export function costOf(sample) {
74
- const { row } = rateOf(sample.model, sample.time);
75
- return {
76
- hit: round9((sample.cacheReadTokens * row.cacheHit) / 1e6),
77
- miss: round9(((sample.inputTokens + sample.cacheWriteTokens) * row.cacheMiss) / 1e6),
78
- output: round9((sample.outputTokens * row.output) / 1e6),
79
- };
80
- }
81
35
  /** 写失效:发生过缓存写入(官方不报,恒 false,个别中转有值)。 */
82
36
  export const isWriteMiss = (s) => s.cacheWriteTokens > 0;
83
37
  /** 完全失效:有输入但缓存命中为 0(首轮无缓存可命中也算,近似,任何路由可靠)。 */
84
38
  export const isFullMiss = (s) => s.inputTokens + s.cacheReadTokens + s.cacheWriteTokens > 0 && s.cacheReadTokens === 0;
85
39
  const sampleInput = (s) => s.inputTokens + s.cacheReadTokens + s.cacheWriteTokens;
86
- const EMPTY_TOTALS = {
87
- cacheHitCost: 0,
88
- missCost: 0,
89
- outputCost: 0,
40
+ const ZERO_MONEY = () => ({ hitCost: 0, missCost: 0, outputCost: 0 });
41
+ const ZERO_BUCKETS = () => ({ CNY: ZERO_MONEY(), USD: ZERO_MONEY() });
42
+ const EMPTY_COUNTS = {
90
43
  inputTokens: 0,
91
44
  cacheReadTokens: 0,
92
45
  outputTokens: 0,
@@ -95,6 +48,44 @@ const EMPTY_TOTALS = {
95
48
  writeTokens: 0,
96
49
  fullMissSteps: 0,
97
50
  };
51
+ const emptyTotals = () => ({ counts: { ...EMPTY_COUNTS }, money: ZERO_BUCKETS() });
52
+ /** 折入时刻结算:按解析出的费率行算三笔(off → 恒 0 且 priced=false)。 */
53
+ export function billFor(sample, snapshot) {
54
+ const resolved = resolveRate(sample.model, sample.provider, sample.time, snapshot);
55
+ const { row } = resolved;
56
+ if (row === null || !resolved.priced) {
57
+ return {
58
+ hit: 0, miss: 0, output: 0,
59
+ currency: resolved.currency ?? 'CNY',
60
+ tier: resolved.tier, source: resolved.source, priced: false,
61
+ catalogKey: resolved.entry?.key ?? null,
62
+ catalogLabel: resolved.entry?.label ?? null,
63
+ priceRow: null,
64
+ };
65
+ }
66
+ return {
67
+ hit: round9((sample.cacheReadTokens * row.inputCacheHit) / 1e6),
68
+ miss: round9(((sample.inputTokens + sample.cacheWriteTokens) * row.inputMiss) / 1e6),
69
+ output: round9((sample.outputTokens * row.output) / 1e6),
70
+ currency: resolved.currency ?? 'CNY',
71
+ tier: resolved.tier, source: resolved.source, priced: true,
72
+ catalogKey: resolved.entry?.key ?? null,
73
+ catalogLabel: resolved.entry?.label ?? null,
74
+ priceRow: { ...row },
75
+ };
76
+ }
77
+ const billedShape = z.object({
78
+ hit: z.number(),
79
+ miss: z.number(),
80
+ output: z.number(),
81
+ currency: z.enum(['CNY', 'USD']),
82
+ tier: z.enum(['peak', 'offPeak', 'flat']).nullable(),
83
+ source: z.enum(['cn', 'global', 'custom', 'off', 'catalog-miss']),
84
+ priced: z.boolean(),
85
+ catalogKey: z.string().nullable(),
86
+ catalogLabel: z.string().nullable(),
87
+ priceRow: z.object({ inputMiss: z.number(), inputCacheHit: z.number(), output: z.number() }).nullable(),
88
+ });
98
89
  const sampleSchemaFields = {
99
90
  inputTokens: z.number().int().nonnegative(),
100
91
  cacheReadTokens: z.number().int().nonnegative(),
@@ -104,19 +95,9 @@ const sampleSchemaFields = {
104
95
  provider: z.string().nullable(),
105
96
  time: z.number(),
106
97
  };
107
- const turnTotalsShape = {
108
- id: z.number().int(),
109
- hitCost: z.number().nonnegative(),
110
- missCost: z.number().nonnegative(),
111
- outputCost: z.number().nonnegative(),
112
- inputTokens: z.number().int().nonnegative(),
113
- cacheReadTokens: z.number().int().nonnegative(),
114
- outputTokens: z.number().int().nonnegative(),
115
- };
116
- const totalsShape = {
117
- cacheHitCost: z.number().nonnegative(),
118
- missCost: z.number().nonnegative(),
119
- outputCost: z.number().nonnegative(),
98
+ const moneyShape = z.object({ hitCost: z.number(), missCost: z.number(), outputCost: z.number() });
99
+ const bucketsShape = z.object({ CNY: moneyShape, USD: moneyShape });
100
+ const countersShape = {
120
101
  inputTokens: z.number().int().nonnegative(),
121
102
  cacheReadTokens: z.number().int().nonnegative(),
122
103
  outputTokens: z.number().int().nonnegative(),
@@ -125,26 +106,34 @@ const totalsShape = {
125
106
  writeTokens: z.number().int().nonnegative(),
126
107
  fullMissSteps: z.number().int().nonnegative(),
127
108
  };
109
+ const turnTotalsShape = {
110
+ id: z.number().int(),
111
+ inputTokens: z.number().int().nonnegative(),
112
+ cacheReadTokens: z.number().int().nonnegative(),
113
+ outputTokens: z.number().int().nonnegative(),
114
+ money: bucketsShape,
115
+ };
128
116
  /**
129
117
  * Build the `tlCacheBilling` projection definition. Registered soft-coupled
130
118
  * (runtime inject — NEVER a top-level export inject, which would make the
131
119
  * whole plugin 0.2.0-only): hosts without the sessionProjections service
132
120
  * simply never register it, and the ring's billing section stays hidden.
121
+ * @param getPricing - 折入时刻的计价快照读取(O(1),由插件 config 派生)。
133
122
  */
134
- export function buildBillingDefinition() {
123
+ export function buildBillingDefinition(getPricing) {
135
124
  const stateSchema = z.object({
136
125
  provider: z.string().nullable(),
137
126
  model: z.string().nullable(),
138
- last: z.object({ turn: z.number().int(), step: z.number().int(), ...sampleSchemaFields }).nullable(),
127
+ last: z.object({ turn: z.number().int(), step: z.number().int(), ...sampleSchemaFields, billed: billedShape }).nullable(),
139
128
  turn: z.object(turnTotalsShape).nullable(),
140
- totals: z.object(totalsShape),
129
+ totals: z.object({ counts: z.object(countersShape), money: bucketsShape }),
141
130
  });
142
131
  const init = () => ({
143
132
  provider: null,
144
133
  model: null,
145
134
  last: null,
146
135
  turn: null,
147
- totals: { ...EMPTY_TOTALS },
136
+ totals: emptyTotals(),
148
137
  });
149
138
  // Wire-format reducer: events arrive untyped off the transport mirror and
150
139
  // every field access below already narrows defensively.
@@ -199,52 +188,65 @@ export function buildBillingDefinition() {
199
188
  model: sourceModel ?? state.model,
200
189
  provider: sourceProvider ?? state.provider,
201
190
  time: typeof event.time === 'number' ? event.time : Date.now(),
191
+ // 凭证在折入时刻结算(事件时刻的费率 + 当时的计价快照)
192
+ billed: { hit: 0, miss: 0, output: 0, currency: 'CNY', tier: null, source: 'off', priced: false, catalogKey: null, catalogLabel: null, priceRow: null },
202
193
  };
194
+ sample.billed = billFor(sample, getPricing());
203
195
  const prev = state.last;
204
- // 同 step 替换样本:数据全同则引用不变
196
+ // 同 step 替换样本:数据全同且凭证同值则引用不变
205
197
  if (prev !== null && prev.turn === turn && prev.step === step &&
206
198
  prev.inputTokens === sample.inputTokens &&
207
199
  prev.cacheReadTokens === sample.cacheReadTokens &&
208
200
  prev.cacheWriteTokens === sample.cacheWriteTokens &&
209
201
  prev.outputTokens === sample.outputTokens &&
210
- prev.model === sample.model && prev.provider === sample.provider) {
202
+ prev.model === sample.model && prev.provider === sample.provider &&
203
+ prev.billed.hit === sample.billed.hit &&
204
+ prev.billed.miss === sample.billed.miss &&
205
+ prev.billed.output === sample.billed.output &&
206
+ prev.billed.currency === sample.billed.currency) {
211
207
  return state;
212
208
  }
213
- const current = costOf(sample);
214
209
  const writeMiss = isWriteMiss(sample);
215
210
  const fullMiss = isFullMiss(sample);
216
211
  const input = sampleInput(sample);
212
+ /** 桶加减:add 为正、sub 为负贡献。 */
213
+ const settle = (buckets, billed, sign) => {
214
+ const bucket = buckets[billed.currency];
215
+ return {
216
+ ...buckets,
217
+ [billed.currency]: {
218
+ hitCost: bucket.hitCost + sign * billed.hit,
219
+ missCost: bucket.missCost + sign * billed.miss,
220
+ outputCost: bucket.outputCost + sign * billed.output,
221
+ },
222
+ };
223
+ };
217
224
  if (prev !== null && prev.turn === turn && prev.step === step) {
218
- // 同 step 替换:扣旧加新,轮数不变
219
- const old = costOf(prev);
220
- const prevInput = sampleInput(prev);
225
+ // 同 step 替换:**减 prev 凭证记录值**(不重算),加新凭证。轮数不变。
221
226
  const turnBase = state.turn !== null && state.turn.id === prev.turn
222
227
  ? state.turn
223
- : { id: turn, hitCost: 0, missCost: 0, outputCost: 0, inputTokens: 0, cacheReadTokens: 0, outputTokens: 0 };
228
+ : { id: turn, inputTokens: 0, cacheReadTokens: 0, outputTokens: 0, money: ZERO_BUCKETS() };
224
229
  return {
225
230
  ...state,
226
231
  last: sample,
227
232
  turn: {
228
- ...turnBase,
229
233
  id: turn,
230
- hitCost: turnBase.hitCost - old.hit + current.hit,
231
- missCost: turnBase.missCost - old.miss + current.miss,
232
- outputCost: turnBase.outputCost - old.output + current.output,
233
- inputTokens: turnBase.inputTokens - prevInput + input,
234
+ inputTokens: turnBase.inputTokens - sampleInput(prev) + input,
234
235
  cacheReadTokens: turnBase.cacheReadTokens - prev.cacheReadTokens + sample.cacheReadTokens,
235
236
  outputTokens: turnBase.outputTokens - prev.outputTokens + sample.outputTokens,
237
+ money: settle(settle(turnBase.money, prev.billed, -1), sample.billed, 1),
236
238
  },
237
239
  totals: {
238
- cacheHitCost: state.totals.cacheHitCost - old.hit + current.hit,
239
- missCost: state.totals.missCost - old.miss + current.miss,
240
- outputCost: state.totals.outputCost - old.output + current.output,
241
- inputTokens: state.totals.inputTokens - prevInput + input,
242
- cacheReadTokens: state.totals.cacheReadTokens - prev.cacheReadTokens + sample.cacheReadTokens,
243
- outputTokens: state.totals.outputTokens - prev.outputTokens + sample.outputTokens,
244
- rounds: state.totals.rounds,
245
- missSteps: state.totals.missSteps - (isWriteMiss(prev) ? 1 : 0) + (writeMiss ? 1 : 0),
246
- writeTokens: state.totals.writeTokens - prev.cacheWriteTokens + sample.cacheWriteTokens,
247
- fullMissSteps: state.totals.fullMissSteps - (isFullMiss(prev) ? 1 : 0) + (fullMiss ? 1 : 0),
240
+ counts: {
241
+ ...state.totals.counts,
242
+ inputTokens: state.totals.counts.inputTokens - sampleInput(prev) + input,
243
+ cacheReadTokens: state.totals.counts.cacheReadTokens - prev.cacheReadTokens + sample.cacheReadTokens,
244
+ outputTokens: state.totals.counts.outputTokens - prev.outputTokens + sample.outputTokens,
245
+ missSteps: state.totals.counts.missSteps - (isWriteMiss(prev) ? 1 : 0) + (writeMiss ? 1 : 0),
246
+ writeTokens: state.totals.counts.writeTokens - prev.cacheWriteTokens + sample.cacheWriteTokens,
247
+ fullMissSteps: state.totals.counts.fullMissSteps - (isFullMiss(prev) ? 1 : 0) + (fullMiss ? 1 : 0),
248
+ },
249
+ money: settle(settle(state.totals.money, prev.billed, -1), sample.billed, 1),
248
250
  },
249
251
  };
250
252
  }
@@ -256,101 +258,111 @@ export function buildBillingDefinition() {
256
258
  turn: sameTurn
257
259
  ? {
258
260
  ...state.turn,
259
- hitCost: state.turn.hitCost + current.hit,
260
- missCost: state.turn.missCost + current.miss,
261
- outputCost: state.turn.outputCost + current.output,
262
261
  inputTokens: state.turn.inputTokens + input,
263
262
  cacheReadTokens: state.turn.cacheReadTokens + sample.cacheReadTokens,
264
263
  outputTokens: state.turn.outputTokens + sample.outputTokens,
264
+ money: settle(state.turn.money, sample.billed, 1),
265
265
  }
266
266
  : {
267
267
  id: turn,
268
- hitCost: current.hit,
269
- missCost: current.miss,
270
- outputCost: current.output,
271
268
  inputTokens: input,
272
269
  cacheReadTokens: sample.cacheReadTokens,
273
270
  outputTokens: sample.outputTokens,
271
+ money: settle(ZERO_BUCKETS(), sample.billed, 1),
274
272
  },
275
273
  totals: {
276
- cacheHitCost: state.totals.cacheHitCost + current.hit,
277
- missCost: state.totals.missCost + current.miss,
278
- outputCost: state.totals.outputCost + current.output,
279
- inputTokens: state.totals.inputTokens + input,
280
- cacheReadTokens: state.totals.cacheReadTokens + sample.cacheReadTokens,
281
- outputTokens: state.totals.outputTokens + sample.outputTokens,
282
- rounds: state.totals.rounds + 1,
283
- missSteps: state.totals.missSteps + (writeMiss ? 1 : 0),
284
- writeTokens: state.totals.writeTokens + sample.cacheWriteTokens,
285
- fullMissSteps: state.totals.fullMissSteps + (fullMiss ? 1 : 0),
274
+ counts: {
275
+ ...state.totals.counts,
276
+ inputTokens: state.totals.counts.inputTokens + input,
277
+ cacheReadTokens: state.totals.counts.cacheReadTokens + sample.cacheReadTokens,
278
+ outputTokens: state.totals.counts.outputTokens + sample.outputTokens,
279
+ rounds: state.totals.counts.rounds + 1,
280
+ missSteps: state.totals.counts.missSteps + (writeMiss ? 1 : 0),
281
+ writeTokens: state.totals.counts.writeTokens + sample.cacheWriteTokens,
282
+ fullMissSteps: state.totals.counts.fullMissSteps + (fullMiss ? 1 : 0),
283
+ },
284
+ money: settle(state.totals.money, sample.billed, 1),
286
285
  },
287
286
  };
288
287
  };
289
288
  const view = (state) => {
290
289
  const s = state.last;
291
290
  const t = state.totals;
291
+ const sessionByCurrency = (buckets) => ['CNY', 'USD']
292
+ .filter((c) => buckets[c].hitCost > 0 || buckets[c].missCost > 0 || buckets[c].outputCost > 0)
293
+ .map((c) => ({ currency: c, ...buckets[c] }));
292
294
  if (s === null) {
293
295
  return {
294
296
  available: false, cost: 0, missCost: 0, outputCost: 0, currency: 'CNY',
295
297
  cacheReadTokens: 0, totalInputTokens: 0, outputTokens: 0, hitRate: null,
296
298
  model: state.model, provider: state.provider, matchedModel: null, modelMatched: false,
297
299
  tier: null, unitPricePerM: null, turn: null, step: null,
300
+ priced: false, pricingSource: null, catalogKey: null, catalogLabel: null, priceRow: null,
298
301
  turnCost: 0, turnHitCost: 0, turnMissCost: 0, turnOutputCost: 0,
299
302
  turnTokens: 0, turnCacheReadTokens: 0, turnInputTokens: 0, turnOutputTokens: 0,
300
- sessionCacheHitCost: t.cacheHitCost, sessionMissCost: t.missCost, sessionOutputCost: t.outputCost,
301
- sessionInputTokens: t.inputTokens, sessionCacheReadTokens: t.cacheReadTokens,
302
- sessionOutputTokens: t.outputTokens, sessionRounds: t.rounds, sessionMissSteps: t.missSteps,
303
- sessionWriteTokens: t.writeTokens, sessionFullMissSteps: t.fullMissSteps,
303
+ sessionCacheHitCost: 0, sessionMissCost: 0, sessionOutputCost: 0,
304
+ sessionInputTokens: 0, sessionCacheReadTokens: 0,
305
+ sessionOutputTokens: 0, sessionRounds: 0, sessionMissSteps: 0,
306
+ sessionWriteTokens: 0, sessionFullMissSteps: 0,
307
+ sessionByCurrency: [], mixedCurrency: false,
304
308
  };
305
309
  }
306
310
  const totalInput = sampleInput(s);
307
- const { row, tier, matchedModel, matched } = rateOf(s.model, s.time);
308
- const cost = round9((s.cacheReadTokens * row.cacheHit) / 1e6);
309
- const missCost = round9(((s.inputTokens + s.cacheWriteTokens) * row.cacheMiss) / 1e6);
310
- const outputCost = round9((s.outputTokens * row.output) / 1e6);
311
+ const b = s.billed;
312
+ const currency = b.currency;
311
313
  const turn = state.turn;
314
+ const turnMoney = turn === null ? ZERO_MONEY() : turn.money[currency];
315
+ const primary = t.money[currency];
312
316
  return {
313
317
  available: totalInput > 0 || s.outputTokens > 0,
314
- cost, missCost, outputCost,
315
- currency: 'CNY',
318
+ cost: b.hit, missCost: b.miss, outputCost: b.output,
319
+ currency,
316
320
  cacheReadTokens: s.cacheReadTokens,
317
321
  totalInputTokens: totalInput,
318
322
  outputTokens: s.outputTokens,
319
323
  hitRate: totalInput > 0 ? Math.round((s.cacheReadTokens / totalInput) * 1000) / 10 : null,
320
324
  model: s.model,
321
325
  provider: s.provider,
322
- matchedModel,
323
- modelMatched: matched,
324
- tier,
325
- unitPricePerM: row.cacheHit,
326
+ matchedModel: b.catalogLabel,
327
+ modelMatched: b.source !== 'catalog-miss',
328
+ tier: b.tier,
329
+ /** @deprecated 目录化后请以 priceRow 三价目位为准;保留一代 = 当前行缓存命中价。 */
330
+ unitPricePerM: b.priceRow?.inputCacheHit ?? null,
326
331
  turn: s.turn,
327
332
  step: s.step,
328
- turnCost: turn === null ? 0 : turn.hitCost + turn.missCost + turn.outputCost,
329
- turnHitCost: turn === null ? 0 : turn.hitCost,
330
- turnMissCost: turn === null ? 0 : turn.missCost,
331
- turnOutputCost: turn === null ? 0 : turn.outputCost,
333
+ priced: b.priced,
334
+ pricingSource: b.source,
335
+ catalogKey: b.catalogKey,
336
+ catalogLabel: b.catalogLabel,
337
+ priceRow: b.priceRow,
338
+ turnCost: turnMoney.hitCost + turnMoney.missCost + turnMoney.outputCost,
339
+ turnHitCost: turnMoney.hitCost,
340
+ turnMissCost: turnMoney.missCost,
341
+ turnOutputCost: turnMoney.outputCost,
332
342
  turnTokens: turn === null ? 0 : turn.inputTokens + turn.outputTokens,
333
343
  turnCacheReadTokens: turn === null ? 0 : turn.cacheReadTokens,
334
344
  turnInputTokens: turn === null ? 0 : turn.inputTokens,
335
345
  turnOutputTokens: turn === null ? 0 : turn.outputTokens,
336
- sessionCacheHitCost: t.cacheHitCost,
337
- sessionMissCost: t.missCost,
338
- sessionOutputCost: t.outputCost,
339
- sessionInputTokens: t.inputTokens,
340
- sessionCacheReadTokens: t.cacheReadTokens,
341
- sessionOutputTokens: t.outputTokens,
342
- sessionRounds: t.rounds,
343
- sessionMissSteps: t.missSteps,
344
- sessionWriteTokens: t.writeTokens,
345
- sessionFullMissSteps: t.fullMissSteps,
346
+ sessionCacheHitCost: primary.hitCost,
347
+ sessionMissCost: primary.missCost,
348
+ sessionOutputCost: primary.outputCost,
349
+ sessionInputTokens: t.counts.inputTokens,
350
+ sessionCacheReadTokens: t.counts.cacheReadTokens,
351
+ sessionOutputTokens: t.counts.outputTokens,
352
+ sessionRounds: t.counts.rounds,
353
+ sessionMissSteps: t.counts.missSteps,
354
+ sessionWriteTokens: t.counts.writeTokens,
355
+ sessionFullMissSteps: t.counts.fullMissSteps,
356
+ sessionByCurrency: sessionByCurrency(t.money),
357
+ mixedCurrency: sessionByCurrency(t.money).length > 1,
346
358
  };
347
359
  };
348
360
  const viewSchema = z.object({
349
361
  available: z.boolean(),
350
- cost: z.number().nonnegative(),
351
- missCost: z.number().nonnegative(),
352
- outputCost: z.number().nonnegative(),
353
- currency: z.literal('CNY'),
362
+ cost: z.number(),
363
+ missCost: z.number(),
364
+ outputCost: z.number(),
365
+ currency: z.enum(['CNY', 'USD']),
354
366
  cacheReadTokens: z.number().int().nonnegative(),
355
367
  totalInputTokens: z.number().int().nonnegative(),
356
368
  outputTokens: z.number().int().nonnegative(),
@@ -359,10 +371,15 @@ export function buildBillingDefinition() {
359
371
  provider: z.string().nullable(),
360
372
  matchedModel: z.string().nullable(),
361
373
  modelMatched: z.boolean(),
362
- tier: z.enum(['peak', 'offPeak']).nullable(),
374
+ tier: z.enum(['peak', 'offPeak', 'flat']).nullable(),
363
375
  unitPricePerM: z.number().nullable(),
364
376
  turn: z.number().int().nullable(),
365
377
  step: z.number().int().nullable(),
378
+ priced: z.boolean(),
379
+ pricingSource: z.enum(['cn', 'global', 'custom', 'off', 'catalog-miss']).nullable(),
380
+ catalogKey: z.string().nullable(),
381
+ catalogLabel: z.string().nullable(),
382
+ priceRow: z.object({ inputMiss: z.number(), inputCacheHit: z.number(), output: z.number() }).nullable(),
366
383
  turnCost: z.number().nonnegative(),
367
384
  turnHitCost: z.number().nonnegative(),
368
385
  turnMissCost: z.number().nonnegative(),
@@ -381,13 +398,17 @@ export function buildBillingDefinition() {
381
398
  sessionMissSteps: z.number().int().nonnegative(),
382
399
  sessionWriteTokens: z.number().int().nonnegative(),
383
400
  sessionFullMissSteps: z.number().int().nonnegative(),
401
+ sessionByCurrency: z.array(z.object({ currency: z.enum(['CNY', 'USD']), hitCost: z.number().nonnegative(), missCost: z.number().nonnegative(), outputCost: z.number().nonnegative() })),
402
+ mixedCurrency: z.boolean(),
384
403
  });
385
404
  return {
386
405
  key: 'tlCacheBilling',
387
- stateVersion: 1,
406
+ stateVersion: 2,
388
407
  stateSchema,
389
408
  init,
390
409
  apply,
391
410
  wire: { viewSchema, view },
392
411
  };
393
412
  }
413
+ /** 设置面 defaultPricing 值域(index.ts schema 与本模块共用,防漂移)。 */
414
+ export { POLICY_VALUES };