dsh-model-billing 0.3.2 → 0.3.3

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 (3) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/client.js +2345 -2308
  3. package/package.json +68 -68
package/client.js CHANGED
@@ -1,2308 +1,2345 @@
1
- window.__ModuleLoader__.load({
2
- id: 'dsh-model-billing',
3
- factory(require) {
4
- const React = require('react')
5
- const ReactDOM = require('react-dom')
6
- const h = React.createElement
7
- const {
8
- Fragment,
9
- useEffect,
10
- useId,
11
- useLayoutEffect,
12
- useMemo,
13
- useRef,
14
- useState,
15
- useSyncExternalStore,
16
- } = React
17
-
18
- // ---------------------------------------------------------------------
19
- // Locale
20
- // ---------------------------------------------------------------------
21
-
22
- const STRINGS = {
23
- zh: {
24
- nav: '模型与费用',
25
- title: '模型与费用',
26
- intro: '调整已配置模型的顺序,并填写每百万 tokens 的价格。金额根据会话日志估算所有模型;DeepSeek 使用内置峰谷价。',
27
- loading: '正在加载模型目录…',
28
- loadFailed: '模型目录加载失败',
29
- retry: '重试',
30
- modelsCount: '{count} 个模型',
31
- save: '保存',
32
- saving: '保存中…',
33
- saved: '已保存',
34
- unsaved: '有未保存的修改',
35
- undo: '撤销修改',
36
- orderHint: '用 ↑ / ↓ 调整顺序;顺序直接决定对话框右下角模型选择器中的排列,也用于费用估算。',
37
- noModels: '当前没有可用的已配置模型。',
38
- up: '上移',
39
- down: '下移',
40
- cacheHitPrice: '输入 · 缓存命中',
41
- cacheMissPrice: '输入 · 缓存未命中',
42
- outputPrice: '输出',
43
- perMillion: '元 / 百万 tokens',
44
- deepseekTag: 'DeepSeek',
45
- deepseekNote: '默认使用官方峰谷价;填写完整的自定义价格后,将优先按自定义价格估算。',
46
- officialPrice: '官方定价',
47
- priceSourceOfficial: '官方价',
48
- priceSourceCustom: '自定义',
49
- customizePrice: '自定义价格',
50
- useOfficialPrice: '恢复官方价',
51
- customPriceNote: '自定义价格优先于官方峰谷价;三项留空并保存即可恢复官方价。',
52
- peak: '高峰',
53
- offPeak: '空闲',
54
- deepseekSchedule: '高峰为北京时间周一至周五 9:00–12:00、14:00–18:00(法定节假日除外);其余时段为空闲。',
55
- pricePlaceholder: '未设置',
56
- priceUnset: '未定价',
57
- priceHitShort: '命中',
58
- priceMissShort: '未命中',
59
- priceOutShort: '输出',
60
- priceUnit: '元/M',
61
- invalidPrice: '价格必须是非负数字,或留空表示未设置。',
62
- saveFailed: '保存失败:{message}',
63
- formula: '估算公式:缓存命中输入 × 命中价 +(未缓存输入 + 缓存写入)× 未命中价 + 输出 × 输出价。',
64
- formulaNote: '价格按每百万 tokens 计。缓存写入按缓存未命中价计算;未配置价格的模型不产生金额。',
65
- singlePrice: '价格',
66
- settingsOpen: '在设置中编辑',
67
- costEstimate: '费用估算',
68
- costDetails: '输入框下方费用估算',
69
- close: '关闭',
70
- thisTurn: '本轮',
71
- sessionTotal: '本会话',
72
- cacheHit: '缓存命中率',
73
- tokens: '输入 / 输出',
74
- turn: '轮次',
75
- content: '内容',
76
- time: '时间',
77
- model: '使用模型',
78
- cost: '费用',
79
- byModel: '按模型',
80
- excludedShort: '不计入',
81
- partial: '部分未定价',
82
- partialShort: '部分未定价',
83
- partialNote: '有非 DeepSeek 调用尚未配置完整价格,合计只包含已定价部分。',
84
- showUnpricedNotice: '显示未定价提示',
85
- unpricedRoutes: '未定价路由',
86
- addPrice: '补充价格',
87
- priceSaved: '价格已保存',
88
- archiveSection: '历史价格',
89
- archiveHint: '已移除模型的调用仍按这里保存的价格计价,不需要把它们加回模型列表。',
90
- archiveEmpty: '暂无历史价格',
91
- addArchive: '添加历史价格',
92
- archiveProvider: '提供商',
93
- archiveModel: '模型',
94
- archiveConflict: '该路由仍在当前模型列表中,请直接编辑上方的模型价格。',
95
- invalidRoute: '提供商和模型都不能为空。',
96
- remove: '删除',
97
- estimateNote: '金额由会话日志中的 token 用量和缓存命中估算;DeepSeek 按事件时间使用内置峰谷价,实际账单以提供方为准。',
98
- webToolNote: '含 DeepSeek Search/Fetch 按次费 ≈ {amount}(搜索 {searchBilled}/{searchCalls} 次,抓取 {fetchBilled}/{fetchCalls} 次)。',
99
- listSep: '、',
100
- turnCost: '费用',
101
- turnTokens: '本轮 Token',
102
- displaySection: '轮尾显示',
103
- displayTurnTail: '启用轮尾显示',
104
- displayDock: '显示输入框下方费用估算',
105
- dockSection: '输入框下方费用估算',
106
- dockHint: '控制输入框下方的费用条与费用估算弹层。',
107
- displayHint: '控制每轮结束后在对话尾部显示的内容;修改后点“保存”生效。',
108
- displayModels: '使用模型',
109
- displayProvider: '模型提供商',
110
- displayCost: '本轮费用',
111
- displayTokens: 'Token 用量',
112
- displayCacheHit: '缓存命中率',
113
- displayLabels: '显示文字标签',
114
- displayFontSize: '字号',
115
- displayDensity: '间距',
116
- displayWeight: '字重',
117
- fontSizeSmall: '小',
118
- fontSizeNormal: '标准',
119
- fontSizeLarge: '大',
120
- densityCompact: '紧凑',
121
- densityComfortable: '舒适',
122
- weightNormal: '常规',
123
- weightBold: '加粗',
124
- pickerSection: '模型选择器',
125
- takeoverPicker: '接管模型选择器',
126
- takeoverHint: '开启后使用本插件按自定义顺序排列的模型选择器;关闭后恢复 DSH 内置选择器。',
127
- chooseModel: '选择模型',
128
- loadingModels: '加载模型…',
129
- searchModel: '搜索模型',
130
- noMatch: '没有匹配的模型',
131
- pickerEmpty: '暂无可用模型',
132
- reasoningEffort: '推理强度',
133
- providerDefault: '提供方默认',
134
- switching: '切换中…',
135
- pickerRetry: '重新加载',
136
- pickerAria: '选择模型与推理强度',
137
- pickerUnavailable: '模型目录尚未就绪,请刷新页面',
138
- },
139
- en: {
140
- nav: 'Models & Cost',
141
- title: 'Models & Cost',
142
- intro: 'Reorder configured models and enter prices per million tokens. Amounts estimate every model from the session log; DeepSeek uses its built-in peak/off-peak prices.',
143
- loading: 'Loading the model catalog…',
144
- loadFailed: 'Could not load the model catalog',
145
- retry: 'Retry',
146
- modelsCount: '{count} models',
147
- save: 'Save',
148
- saving: 'Saving…',
149
- saved: 'Saved',
150
- unsaved: 'Unsaved changes',
151
- undo: 'Discard changes',
152
- orderHint: 'Use ↑ / ↓ to reorder. This order is used directly by the model picker at the bottom-right of the composer, and by the cost details.',
153
- noModels: 'No configured models are available.',
154
- up: 'Move up',
155
- down: 'Move down',
156
- cacheHitPrice: 'Input · cache hit',
157
- cacheMissPrice: 'Input · cache miss',
158
- outputPrice: 'Output',
159
- perMillion: 'CNY / million tokens',
160
- deepseekTag: 'DeepSeek',
161
- deepseekNote: 'Official peak/off-peak prices are used by default; a complete custom price set takes precedence.',
162
- officialPrice: 'Official prices',
163
- priceSourceOfficial: 'Official',
164
- priceSourceCustom: 'Custom',
165
- customizePrice: 'Set custom prices',
166
- useOfficialPrice: 'Restore official prices',
167
- customPriceNote: 'Custom prices override the official peak/off-peak table. Leave all three blank and save to restore official prices.',
168
- peak: 'Peak',
169
- offPeak: 'Off-peak',
170
- deepseekSchedule: 'Peak: Beijing time Mon–Fri 09:00–12:00 and 14:00–18:00, excluding Chinese statutory holidays; all other times are off-peak.',
171
- pricePlaceholder: 'Not set',
172
- priceUnset: 'Not priced',
173
- priceHitShort: 'hit',
174
- priceMissShort: 'miss',
175
- priceOutShort: 'out',
176
- priceUnit: 'CNY/M',
177
- invalidPrice: 'Prices must be non-negative numbers, or blank when unset.',
178
- saveFailed: 'Save failed: {message}',
179
- formula: 'Estimate: cached input × hit price + (uncached input + cache write) × miss price + output × output price.',
180
- formulaNote: 'Prices are per million tokens. Cache writes use the cache-miss price; models without a full price set produce no amount.',
181
- singlePrice: 'Prices',
182
- settingsOpen: 'Edit in Settings',
183
- costEstimate: 'Cost estimate',
184
- costDetails: 'Composer cost estimate',
185
- close: 'Close',
186
- thisTurn: 'Turn',
187
- sessionTotal: 'Session',
188
- cacheHit: 'Cache hit rate',
189
- tokens: 'Input / output',
190
- turn: 'Turn',
191
- content: 'Content',
192
- time: 'Time',
193
- model: 'Model',
194
- cost: 'Cost',
195
- byModel: 'By model',
196
- excludedShort: 'Excluded',
197
- partial: 'partly unpriced',
198
- partialShort: 'partly unpriced',
199
- partialNote: 'Some non-DeepSeek calls do not have a complete price set; the total includes only priced calls.',
200
- showUnpricedNotice: 'Show unpriced notices',
201
- unpricedRoutes: 'Unpriced routes',
202
- addPrice: 'Add price',
203
- priceSaved: 'Price saved',
204
- archiveSection: 'Historical prices',
205
- archiveHint: 'Calls to removed models stay priced from this book; you do not need to add them back to the model list.',
206
- archiveEmpty: 'No historical prices yet',
207
- addArchive: 'Add historical price',
208
- archiveProvider: 'Provider',
209
- archiveModel: 'Model',
210
- archiveConflict: 'This route is still in the configured list; edit its price above instead.',
211
- invalidRoute: 'Provider and model are both required.',
212
- remove: 'Remove',
213
- estimateNote: 'Amounts are estimated from token usage and cache hits in the session log; DeepSeek uses built-in peak/off-peak prices at the event time, and the provider bill is authoritative.',
214
- webToolNote: 'Includes DeepSeek Search/Fetch per-request fees ≈ {amount} (search {searchBilled}/{searchCalls}, fetch {fetchBilled}/{fetchCalls}).',
215
- listSep: ', ',
216
- turnCost: 'Cost',
217
- turnTokens: 'Turn tokens',
218
- displaySection: 'Turn-tail display',
219
- displayTurnTail: 'Show the turn tail',
220
- displayDock: 'Show the composer cost estimate',
221
- dockSection: 'Composer cost estimate',
222
- dockHint: 'Controls the cost bar below the composer and its detail panel.',
223
- displayHint: 'Choose what appears below each completed turn; click Save to apply.',
224
- displayModels: 'Models used',
225
- displayProvider: 'Model provider',
226
- displayCost: 'Turn cost',
227
- displayTokens: 'Token usage',
228
- displayCacheHit: 'Cache hit rate',
229
- displayLabels: 'Show text labels',
230
- displayFontSize: 'Font size',
231
- displayDensity: 'Spacing',
232
- displayWeight: 'Font weight',
233
- fontSizeSmall: 'Small',
234
- fontSizeNormal: 'Normal',
235
- fontSizeLarge: 'Large',
236
- densityCompact: 'Compact',
237
- densityComfortable: 'Comfortable',
238
- weightNormal: 'Regular',
239
- weightBold: 'Bold',
240
- pickerSection: 'Model picker',
241
- takeoverPicker: 'Take over the model picker',
242
- takeoverHint: 'On: use this plugin’s picker with your custom model order. Off: restore the built-in DSH picker.',
243
- chooseModel: 'Select model',
244
- loadingModels: 'Loading models…',
245
- searchModel: 'Search models',
246
- noMatch: 'No matching model',
247
- pickerEmpty: 'No model available',
248
- reasoningEffort: 'Reasoning effort',
249
- providerDefault: 'Provider default',
250
- switching: 'Switching…',
251
- pickerRetry: 'Reload',
252
- pickerAria: 'Select model and reasoning effort',
253
- pickerUnavailable: 'The model directory is not ready; reload the page',
254
- },
255
- }
256
-
257
- const t = (key, values) => {
258
- const active = localeService?.getSnapshot?.().active ?? 'zh'
259
- const dict = active.toLowerCase().startsWith('en') ? STRINGS.en : STRINGS.zh
260
- const template = dict[key] ?? STRINGS.zh[key] ?? key
261
- return values === undefined
262
- ? template
263
- : template.replace(/\{(\w+)\}/g, (match, name) => values[name] === undefined ? match : String(values[name]))
264
- }
265
-
266
- let localeService
267
- let modelDirectories
268
- let modelDirectoryRevision = 0
269
- const modelDirectoryListeners = new Set()
270
-
271
- function publishModelDirectories(next) {
272
- if (next === undefined || next === modelDirectories) return
273
- modelDirectories = next
274
- modelDirectoryRevision += 1
275
- for (const listener of [...modelDirectoryListeners]) listener()
276
- }
277
-
278
- function subscribeModelDirectories(listener) {
279
- modelDirectoryListeners.add(listener)
280
- return () => modelDirectoryListeners.delete(listener)
281
- }
282
-
283
- function useModelDirectoriesRevision() {
284
- return useSyncExternalStore(
285
- subscribeModelDirectories,
286
- () => modelDirectoryRevision,
287
- () => 0,
288
- )
289
- }
290
-
291
- function subscribeLocale(listener) {
292
- return localeService?.subscribe?.(listener) ?? (() => {})
293
- }
294
-
295
- function localeRevision() {
296
- return localeService?.getSnapshot?.().revision ?? 0
297
- }
298
-
299
- function useT() {
300
- useSyncExternalStore(subscribeLocale, localeRevision, () => 0)
301
- return t
302
- }
303
-
304
- const DISPLAY_DEFAULTS = Object.freeze({
305
- showTurnTail: true,
306
- showDock: true,
307
- showModels: true,
308
- showProvider: false,
309
- showCost: true,
310
- showTokens: false,
311
- showCacheHit: false,
312
- showLabels: true,
313
- showUnpricedNotice: true,
314
- takeoverPicker: true,
315
- fontSize: 'normal',
316
- density: 'comfortable',
317
- fontWeight: 'normal',
318
- })
319
-
320
- const DISPLAY_STORAGE_KEY = 'dsh-model-billing.display'
321
-
322
- function clearStoredDisplay() {
323
- try {
324
- localStorage.removeItem(DISPLAY_STORAGE_KEY)
325
- } catch {
326
- // Ignore unavailable storage.
327
- }
328
- }
329
-
330
- // Older builds mirrored display settings into localStorage, which made the
331
- // browser a second source of truth that could never sync across devices.
332
- // Clear the legacy key once; the Host state is now the only copy.
333
- clearStoredDisplay()
334
-
335
- function readDisplay(data) {
336
- const source = data?.display
337
- return source !== null && typeof source === 'object' && !Array.isArray(source)
338
- ? { ...DISPLAY_DEFAULTS, ...source }
339
- : { ...DISPLAY_DEFAULTS }
340
- }
341
-
342
- // ---------------------------------------------------------------------
343
- // Shared catalog state
344
- // ---------------------------------------------------------------------
345
-
346
- const catalog = {
347
- snap: { status: 'idle', data: null, error: null },
348
- listeners: new Set(),
349
- inflight: null,
350
- generation: 0,
351
- }
352
-
353
- const REQUEST_TIMEOUT_MS = 8000
354
-
355
- async function requestJson(url, options) {
356
- const controller = typeof AbortController === 'function' ? new AbortController() : null
357
- const timeout = controller === null
358
- ? null
359
- : setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS)
360
- let response
361
- try {
362
- response = await fetch(url, {
363
- credentials: 'same-origin',
364
- cache: 'no-store',
365
- ...options,
366
- signal: options?.signal ?? controller?.signal ?? null,
367
- headers: {
368
- accept: 'application/json',
369
- ...(options?.headers ?? {}),
370
- },
371
- })
372
- } catch (error) {
373
- if (timeout !== null) clearTimeout(timeout)
374
- throw controller !== null && error?.name === 'AbortError'
375
- ? new Error(`timeout after ${REQUEST_TIMEOUT_MS}ms`)
376
- : error
377
- }
378
- if (timeout !== null) clearTimeout(timeout)
379
- let body
380
- try {
381
- body = await response.json()
382
- } catch {
383
- throw new Error(`HTTP ${response.status}`)
384
- }
385
- if (!response.ok || body?.ok === false) {
386
- throw new Error(body?.message ?? `HTTP ${response.status}`)
387
- }
388
- return body
389
- }
390
-
391
- function emitCatalog() {
392
- for (const listener of [...catalog.listeners]) listener()
393
- }
394
-
395
- function setCatalog(draft) {
396
- catalog.snap = { ...catalog.snap, ...draft }
397
- emitCatalog()
398
- }
399
-
400
- async function ensureCatalog(force = false) {
401
- if (!force && catalog.snap.status === 'ready') return catalog.snap.data
402
- if (catalog.inflight !== null && !force) return catalog.inflight
403
- catalog.generation += 1
404
- const generation = catalog.generation
405
- setCatalog({ status: 'loading', error: null })
406
- const operation = requestJson('/model-billing/catalog')
407
- .then((data) => {
408
- if (generation !== catalog.generation) return data
409
- setCatalog({ status: 'ready', data, error: null })
410
- return data
411
- })
412
- .catch((error) => {
413
- if (generation === catalog.generation) {
414
- setCatalog({ status: 'error', error: error instanceof Error ? error.message : String(error) })
415
- }
416
- return null
417
- })
418
- .finally(() => {
419
- if (catalog.inflight === operation) catalog.inflight = null
420
- })
421
- catalog.inflight = operation
422
- return operation
423
- }
424
-
425
- function subscribeCatalog(listener) {
426
- catalog.listeners.add(listener)
427
- if (catalog.snap.status === 'idle') void ensureCatalog()
428
- return () => catalog.listeners.delete(listener)
429
- }
430
-
431
- function catalogSnapshot() {
432
- return catalog.snap
433
- }
434
-
435
- function useCatalog() {
436
- return useSyncExternalStore(subscribeCatalog, catalogSnapshot, catalogSnapshot)
437
- }
438
-
439
- // ---------------------------------------------------------------------
440
- // Shared session usage state
441
- // ---------------------------------------------------------------------
442
-
443
- const usageSlots = new Map()
444
- const USAGE_DEBOUNCE_MS = 300
445
-
446
- function usageSlot(sessionId) {
447
- let slot = usageSlots.get(sessionId)
448
- if (slot === undefined) {
449
- slot = {
450
- snap: { status: 'idle', data: null, error: null },
451
- listeners: new Set(),
452
- inflight: null,
453
- generation: 0,
454
- revision: null,
455
- pending: null,
456
- refs: 0,
457
- rowsByTurn: new Map(),
458
- }
459
- usageSlots.set(sessionId, slot)
460
- }
461
- slot.refs += 1
462
- return slot
463
- }
464
-
465
- function releaseSlot(sessionId, slot) {
466
- slot.refs = Math.max(0, slot.refs - 1)
467
- if (slot.listeners.size > 0 || slot.refs > 0) return
468
- if (slot.pending !== null) {
469
- clearTimeout(slot.pending)
470
- slot.pending = null
471
- }
472
- usageSlots.delete(sessionId)
473
- }
474
-
475
- function emitUsage(slot) {
476
- for (const listener of [...slot.listeners]) listener()
477
- }
478
-
479
- function ensureUsage(sessionId, revision, force = false) {
480
- const slot = usageSlot(sessionId)
481
- if (!force && slot.revision === revision && slot.snap.status !== 'error') return
482
- if (slot.inflight !== null && !force) return
483
- slot.revision = revision
484
- if (slot.pending !== null) clearTimeout(slot.pending)
485
- // Collapse the token-counter updates a streaming reply produces into one
486
- // trailing request instead of one request per token.
487
- slot.pending = setTimeout(() => {
488
- slot.pending = null
489
- void fetchUsage(sessionId, revision)
490
- }, USAGE_DEBOUNCE_MS)
491
- }
492
-
493
- function fetchUsage(sessionId, revision) {
494
- const slot = usageSlots.get(sessionId)
495
- if (slot === undefined) return
496
- slot.generation += 1
497
- const generation = slot.generation
498
- slot.snap = { ...slot.snap, status: 'loading', error: null }
499
- emitUsage(slot)
500
- const operation = requestJson(`/model-billing/usage?sessionId=${encodeURIComponent(sessionId)}`)
501
- .then((data) => {
502
- if (generation !== slot.generation) return
503
- // Index once per fetch rather than scanning the row list on every
504
- // turn render.
505
- slot.snap = { status: 'ready', data, rowsByTurn: indexRowsByTurn(data), error: null }
506
- slot.rowsByTurn = slot.snap.rowsByTurn
507
- emitUsage(slot)
508
- })
509
- .catch((error) => {
510
- if (generation !== slot.generation) return
511
- slot.snap = {
512
- status: 'error',
513
- data: slot.snap.data,
514
- rowsByTurn: slot.snap.rowsByTurn,
515
- error: error instanceof Error ? error.message : String(error),
516
- }
517
- emitUsage(slot)
518
- })
519
- .finally(() => {
520
- if (slot.inflight === operation) slot.inflight = null
521
- })
522
- slot.inflight = operation
523
- return operation
524
- }
525
-
526
- function indexRowsByTurn(data) {
527
- const index = new Map()
528
- if (Array.isArray(data?.usage?.turns)) {
529
- for (const row of data.usage.turns) index.set(row.turn, row)
530
- }
531
- return index
532
- }
533
-
534
- function useSessionUsage(sessionId, revision) {
535
- const slot = usageSlot(sessionId)
536
- useEffect(() => () => releaseSlot(sessionId, slot), [sessionId, slot])
537
- const snap = useSyncExternalStore(
538
- (listener) => {
539
- slot.listeners.add(listener)
540
- return () => slot.listeners.delete(listener)
541
- },
542
- () => slot.snap,
543
- () => slot.snap,
544
- )
545
- useEffect(() => {
546
- if (sessionId !== undefined && revision !== null) ensureUsage(sessionId, revision)
547
- }, [sessionId, revision])
548
- return snap
549
- }
550
-
551
- function tokenUsageRevision(tokenUsage) {
552
- if (tokenUsage === undefined || tokenUsage === null) return 'none'
553
- return [
554
- tokenUsage.uncachedInputTokens ?? '',
555
- tokenUsage.outputTokens ?? '',
556
- tokenUsage.cacheReadTokens ?? '',
557
- tokenUsage.cacheWriteTokens ?? '',
558
- ].join(':')
559
- }
560
-
561
- // ---------------------------------------------------------------------
562
- // Formatting
563
- // ---------------------------------------------------------------------
564
-
565
- function formatMoney(value) {
566
- if (value === null || value === undefined || !Number.isFinite(value)) return '—'
567
- if (value === 0) return '¥0'
568
- const fixed = value.toFixed(2)
569
- return fixed === '0.00' ? '<¥0.01' : `¥${fixed}`
570
- }
571
-
572
- function formatTokens(value) {
573
- if (!Number.isFinite(value)) return '—'
574
- if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(2)}M`
575
- if (value >= 1_000) return `${(value / 1_000).toFixed(1)}k`
576
- return String(Math.round(value))
577
- }
578
-
579
- function formatPercent(value) {
580
- return value === null || value === undefined ? '—' : `${(value * 100).toFixed(1)}%`
581
- }
582
-
583
- function formatPrice(value) {
584
- if (value === null || value === undefined) return '—'
585
- return String(Number(value.toFixed(6)))
586
- }
587
-
588
- function PriceLines({ pricing, translate, showSource = true }) {
589
- const source = showSource === false ? null : pricing?.source === 'official'
590
- ? translate('priceSourceOfficial')
591
- : pricing?.source === 'user' ? translate('priceSourceCustom') : null
592
- const sourceLine = source === null ? null : h('div', { className: 'mb-picker-price-source' }, source)
593
- if (pricing?.builtin === true) {
594
- const line = (label, prices) =>
595
- `${label} ${translate('priceHitShort')} ${formatPrice(prices?.cacheHit)} · ${translate('priceMissShort')} ${formatPrice(prices?.cacheMiss)} · ${translate('priceOutShort')} ${formatPrice(prices?.output)} ${translate('priceUnit')}`
596
- return h('div', { className: 'mb-picker-prices' },
597
- sourceLine,
598
- h('div', { className: 'mb-picker-price-line' }, line(translate('peak'), pricing.peak)),
599
- h('div', { className: 'mb-picker-price-line' }, line(translate('offPeak'), pricing.offPeak)))
600
- }
601
- const hasAny = pricing !== null && pricing !== undefined
602
- && [pricing.cacheHit, pricing.cacheMiss, pricing.output].some((value) => typeof value === 'number')
603
- if (!hasAny) {
604
- return h('div', { className: 'mb-picker-prices' }, translate('priceUnset'))
605
- }
606
- return h('div', { className: 'mb-picker-prices' },
607
- sourceLine,
608
- h('div', { className: 'mb-picker-price-line' },
609
- `${translate('priceHitShort')} ${formatPrice(pricing.cacheHit)} · ${translate('priceMissShort')} ${formatPrice(pricing.cacheMiss)} · ${translate('priceOutShort')} ${formatPrice(pricing.output)} ${translate('priceUnit')}`))
610
- }
611
-
612
- function timeText(timestamp) {
613
- if (!Number.isFinite(timestamp) || timestamp <= 0) return ''
614
- try {
615
- return new Date(timestamp).toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
616
- } catch {
617
- return ''
618
- }
619
- }
620
-
621
- // ---------------------------------------------------------------------
622
- // Styles
623
- // ---------------------------------------------------------------------
624
-
625
- const CSS = `
626
- .mb-page { color: var(--dsw-alias-label-primary); font-size: 13px; line-height: 1.5; padding: 4px 0 28px; }
627
- .mb-page * { box-sizing: border-box; }
628
- .mb-title { margin: 0 0 4px; font-size: 18px; font-weight: 600; }
629
- .mb-intro { margin: 0 0 14px; color: var(--dsw-alias-label-secondary); max-width: 760px; }
630
- .mb-toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 14px 0; }
631
- .mb-muted { color: var(--dsw-alias-label-secondary); }
632
- .mb-spacer { flex: 1; }
633
- .mb-btn { appearance: none; border: 1px solid var(--dsw-alias-border-l2); background: var(--dsw-alias-bg-layer-1); color: var(--dsw-alias-label-primary); border-radius: 7px; height: 30px; padding: 0 12px; font: inherit; cursor: pointer; }
634
- .mb-btn:hover:not(:disabled) { background: var(--dsw-alias-bg-layer-2); }
635
- .mb-btn:disabled { opacity: .45; cursor: default; }
636
- .mb-btn-primary { background: var(--dsw-alias-brand-primary); border-color: var(--dsw-alias-brand-primary); color: var(--dsw-alias-bg-base); font-weight: 600; }
637
- .mb-btn-primary:hover:not(:disabled) { background: var(--dsw-alias-brand-primary); opacity: .9; }
638
- .mb-btn-icon { width: 28px; height: 28px; padding: 0; font-size: 13px; }
639
- .mb-status { color: var(--dsw-alias-label-secondary); min-height: 18px; }
640
- .mb-status[data-tone="error"] { color: var(--dsw-alias-state-error-primary); }
641
- .mb-status[data-tone="success"] { color: var(--dsw-alias-state-success-primary); }
642
- .mb-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; max-width: 980px; }
643
- .mb-card { border: 1px solid var(--dsw-alias-border-l1); background: var(--dsw-alias-bg-layer-1); border-radius: 10px; padding: 12px 14px; }
644
- .mb-card-head { display: flex; align-items: flex-start; gap: 12px; }
645
- .mb-index { width: 26px; height: 26px; border-radius: 7px; background: var(--dsw-alias-bg-layer-2); color: var(--dsw-alias-label-secondary); display: grid; place-items: center; font-variant-numeric: tabular-nums; flex: none; }
646
- .mb-name { font-weight: 600; font-size: 14px; }
647
- .mb-sub { color: var(--dsw-alias-label-secondary); font-size: 12px; overflow-wrap: anywhere; }
648
- .mb-head-actions { margin-left: auto; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
649
- .mb-tag { display: inline-block; margin-left: 6px; padding: 1px 6px; border-radius: 999px; background: var(--dsw-alias-bg-layer-2); color: var(--dsw-alias-label-secondary); font-size: 11px; font-weight: 500; vertical-align: 1px; }
650
- .mb-tag-excluded { color: var(--dsw-alias-state-warn-primary); }
651
- .mb-prices { display: grid; gap: 10px; margin-top: 12px; }
652
- .mb-price-group { border: 1px solid var(--dsw-alias-border-l1); border-radius: 8px; padding: 10px 12px 12px; }
653
- .mb-price-group-title { display: flex; gap: 8px; align-items: baseline; margin-bottom: 8px; font-weight: 600; }
654
- .mb-price-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
655
- .mb-card-actions { margin-top: 10px; display: flex; justify-content: flex-end; }
656
- .mb-field { display: grid; gap: 4px; min-width: 0; }
657
- .mb-field-label { color: var(--dsw-alias-label-secondary); font-size: 11px; }
658
- .mb-input { width: 100%; height: 30px; border: 1px solid var(--dsw-alias-border-l2); border-radius: 6px; background: var(--dsw-alias-bg-base); color: var(--dsw-alias-label-primary); padding: 0 8px; font: inherit; font-variant-numeric: tabular-nums; }
659
- .mb-input:focus { outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: -1px; }
660
- .mb-note { margin: 8px 0 0; color: var(--dsw-alias-label-secondary); font-size: 12px; }
661
- .mb-warn { margin: 6px 0 0; color: var(--dsw-alias-state-warn-primary); font-size: 12px; }
662
- .mb-empty { border: 1px dashed var(--dsw-alias-border-l2); border-radius: 10px; padding: 28px; text-align: center; color: var(--dsw-alias-label-secondary); }
663
- .mb-footer-note { max-width: 980px; margin-top: 14px; color: var(--dsw-alias-label-secondary); font-size: 12px; }
664
- .mb-dock { align-self: center; display: flex; flex-direction: column; align-items: flex-start; color: var(--dsw-alias-label-tertiary); font-size: calc(var(--dsh-content-font-size-secondary, 13px) - 1px); line-height: calc(20px + var(--dsh-content-font-delta-secondary, 0px)); }
665
- .mb-dock-trigger { appearance: none; border: 0; background: transparent; color: inherit; font: inherit; line-height: inherit; padding: 1px 8px; border-radius: var(--dsw-radius-sm); display: inline-flex; align-items: center; gap: 6px; cursor: pointer; text-align: left; max-width: 100%; flex-wrap: nowrap; }
666
- .mb-dock-trigger:hover, .mb-dock-trigger[aria-expanded="true"] { background: var(--dsw-alias-interactive-bg-hover); color: var(--dsw-alias-label-secondary); }
667
- .mb-dock-amount { color: var(--dsw-alias-label-secondary); font-weight: 500; font-variant-numeric: tabular-nums; }
668
- .mb-dock-sep { opacity: .55; }
669
- .mb-dock-icon { flex: none; width: 14px; height: 14px; }
670
- .mb-dock-caret { font-size: 10px; opacity: .7; }
671
- .mb-panel { box-sizing: border-box; border: 1px solid var(--dsw-alias-border-l1); background: var(--dsw-menu-surface-fill, var(--dsw-alias-bg-layer-1)); border-radius: var(--dsw-radius-lg, 10px); padding: 10px 12px; color: var(--dsw-alias-label-primary); }
672
- .mb-panel-floating { position: fixed; z-index: 1200; max-width: calc(100vw - 24px); max-height: min(430px, calc(100vh - 96px)); overflow-x: hidden; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; scrollbar-color: var(--dsw-alias-scrollbar-bg-l2, var(--dsw-alias-border-l2)) transparent; box-shadow: var(--dsw-elevation-prominent); backdrop-filter: var(--dsw-menu-backdrop-filter, none); }
673
- .mb-panel-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
674
- .mb-panel-title { font-weight: 600; }
675
- .mb-panel-close { margin-left: auto; width: 24px; height: 24px; border: 0; border-radius: var(--dsw-radius-sm); background: 0 0; color: var(--dsw-alias-label-secondary); font: inherit; font-size: 18px; line-height: 20px; cursor: pointer; }
676
- .mb-panel-close:hover { background: var(--dsw-alias-interactive-bg-hover); color: var(--dsw-alias-label-primary); }
677
- .mb-summary { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; margin-bottom: 10px; }
678
- .mb-summary-item { background: var(--dsw-alias-bg-layer-2); border-radius: 7px; padding: 7px 9px; min-width: 0; }
679
- .mb-summary-key { color: var(--dsw-alias-label-secondary); font-size: 11px; }
680
- .mb-summary-value { font-variant-numeric: tabular-nums; font-weight: 600; overflow-wrap: anywhere; }
681
- .mb-table { width: 100%; border-collapse: collapse; font-size: 12px; }
682
- .mb-table th, .mb-table td { text-align: left; padding: 4px 5px; border-bottom: 1px solid var(--dsw-alias-border-l1); vertical-align: top; }
683
- .mb-table th { color: var(--dsw-alias-label-secondary); font-weight: 500; font-size: 11px; }
684
- .mb-table td.num, .mb-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
685
- .mb-table tr:last-child td { border-bottom: 0; }
686
- .mb-turn-table { table-layout: fixed; }
687
- .mb-turn-table th, .mb-turn-table td, .mb-turn-table th.num, .mb-turn-table td.num { text-align: left; }
688
- .mb-turn-table th:nth-child(1), .mb-turn-table td:nth-child(1) { width: 5%; }
689
- .mb-turn-table th:nth-child(2), .mb-turn-table td:nth-child(2) { width: 25.5%; }
690
- .mb-turn-table th:nth-child(3), .mb-turn-table td:nth-child(3) { width: 12.5%; }
691
- .mb-turn-table th:nth-child(4), .mb-turn-table td:nth-child(4) { width: 21%; }
692
- .mb-turn-table th:nth-child(5), .mb-turn-table td:nth-child(5) { width: 10%; }
693
- .mb-turn-table th:nth-child(6), .mb-turn-table td:nth-child(6) { width: 16%; }
694
- .mb-turn-table th:nth-child(7), .mb-turn-table td:nth-child(7) { width: 10%; }
695
- .mb-turn-preview, .mb-turn-model { color: var(--dsw-alias-label-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
696
- .mb-turn-model { color: var(--dsw-alias-label-primary); }
697
- .mb-panel-note { margin: 9px 0 0; color: var(--dsw-alias-label-secondary); font-size: 11px; }
698
- .mb-turn { color: var(--dsw-alias-label-secondary); font-size: 12px; font-variant-numeric: tabular-nums; display: inline-flex; flex-wrap: wrap; align-items: baseline; column-gap: 10px; row-gap: 2px; max-width: 100%; }
699
- .mb-turn[data-size="small"] { font-size: 11px; }
700
- .mb-turn[data-size="large"] { font-size: 13px; }
701
- .mb-turn[data-density="compact"] { column-gap: 6px; }
702
- .mb-turn-item { white-space: nowrap; }
703
- .mb-turn strong { color: inherit; font-weight: 600; }
704
- .mb-turn[data-weight="normal"] { font-weight: 400; }
705
- .mb-turn[data-weight="normal"] strong { font-weight: 400; }
706
- .mb-turn[data-weight="bold"] { font-weight: 600; }
707
- .mb-turn[data-weight="bold"] strong { font-weight: 600; }
708
- .mb-turn-partial { color: var(--dsw-alias-state-warn-primary); }
709
- .mb-display { border: 1px solid var(--dsw-alias-border-l1); border-radius: 10px; padding: 12px 14px; margin: 10px 0 14px; background: var(--dsw-alias-bg-layer-2); }
710
- .mb-display-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
711
- .mb-display-title { font-weight: 600; }
712
- .mb-display-hint { color: var(--dsw-alias-label-secondary); font-size: 12px; }
713
- .mb-check-grid { display: flex; flex-wrap: wrap; gap: 8px 18px; }
714
- .mb-check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; cursor: pointer; }
715
- .mb-check.is-disabled { opacity: .55; cursor: default; }
716
- .mb-check input { width: 15px; height: 15px; accent-color: var(--dsw-alias-brand-primary); }
717
- .mb-display-controls { display: flex; flex-wrap: wrap; gap: 10px 18px; margin-top: 12px; }
718
- .mb-archive-list { display: flex; flex-direction: column; gap: 8px; }
719
- .mb-archive-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; gap: 8px; align-items: center; }
720
- .mb-archive-row .mb-archive-provider { grid-column: 1; grid-row: 1; min-width: 0; }
721
- .mb-archive-row .mb-archive-model { grid-column: 2; grid-row: 1; min-width: 0; }
722
- .mb-archive-row > .mb-btn { grid-column: 3; grid-row: 1; }
723
- .mb-archive-row .mb-archive-price { grid-row: 2; min-width: 0; }
724
- .mb-route-editor { display: flex; flex-wrap: wrap; }
725
- .mb-route-editor .mb-input { flex: 1 1 120px; min-width: 110px; }
726
- .mb-route-editor .mb-btn { flex: 0 0 auto; }
727
- .mb-archive-actions { display: flex; gap: 8px; margin-top: 10px; }
728
- .mb-master { display: flex; align-items: center; gap: 10px; padding: 10px 0 4px; font-size: 15px; font-weight: 600; cursor: pointer; }
729
- .mb-master input { width: 16px; height: 16px; flex: 0 0 auto; }
730
- .mb-master-label { line-height: 1.4; }
731
- .mb-master-hint { color: var(--dsw-alias-label-secondary); font-size: 12px; font-weight: 400; }
732
- .mb-subchecks { margin: 2px 0 0 26px; }
733
- .mb-tag.is-muted { color: var(--dsw-alias-label-secondary); }
734
- @media (max-width: 720px) {
735
- .mb-subchecks { margin-left: 18px; }
736
- }
737
- .mb-select { height: 30px; border: 1px solid var(--dsw-alias-border-l2); border-radius: 6px; background: var(--dsw-alias-bg-base); color: var(--dsw-alias-label-primary); padding: 0 8px; font: inherit; }
738
- .mb-select:focus { outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: -1px; }
739
- .mb-picker { position: relative; display: flex; min-width: 0; }
740
- .mb-picker-trigger { border-radius: var(--dsw-radius-sm); min-width: 0; max-width: min(360px, 45cqw); height: 28px; color: var(--dsw-alias-label-secondary); cursor: pointer; background: 0 0; border: none; outline: none; align-items: center; gap: 4px; padding: 0 4px 0 8px; font-size: 13px; font-weight: 400; line-height: 20px; display: flex; }
741
- .mb-picker-trigger:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover); }
742
- .mb-picker-trigger:focus-visible { box-shadow: 0 0 0 2px var(--dsw-focus-ring-color, var(--dsw-alias-state-business-primary)); }
743
- .mb-picker-trigger:disabled { color: var(--dsw-alias-label-dimmed, var(--dsw-alias-label-secondary)); cursor: default; }
744
- .mb-picker-icon { display: var(--dsh-composer-model-icon-display, none); flex: none; }
745
- .mb-picker-name { color: inherit; text-overflow: ellipsis; white-space: nowrap; min-width: 0; overflow: hidden; display: var(--dsh-composer-model-text-display, block); }
746
- .mb-picker-trigger-effort { text-overflow: ellipsis; white-space: nowrap; min-width: 0; color: var(--dsw-alias-label-caption, var(--dsw-alias-label-secondary)); flex-shrink: 1000; overflow: hidden; display: var(--dsh-composer-model-text-display, block); }
747
- .mb-picker-chevron { color: var(--dsw-alias-label-caption, var(--dsw-alias-label-secondary)); flex: none; transition: transform .12s; }
748
- .mb-picker-chevron-open { transform: rotate(180deg); }
749
- .mb-picker-menu { box-sizing: border-box; z-index: 1100; width: min(360px, calc(100vw - 32px)); max-width: min(360px, calc(100vw - 32px)); max-height: min(480px, calc(100vh - 96px)); box-shadow: var(--dsw-elevation-prominent); background: var(--dsw-menu-surface-fill, var(--dsw-alias-bg-layer-1)); backdrop-filter: var(--dsw-menu-backdrop-filter, none); color: var(--dsw-alias-label-primary); border: 0; border-radius: var(--dsw-radius-lg, 10px); flex-direction: column; padding: 10px; display: flex; position: fixed; overflow: hidden; }
750
- .mb-picker-search { box-sizing: border-box; border-radius: var(--dsw-radius-md); background: var(--dsw-alias-bg-layer-2); border: 1px solid var(--dsw-alias-border-l1); height: 32px; margin: 0 0 6px; padding: 0 9px; font: inherit; font-size: 13px; color: var(--dsw-alias-label-primary); width: 100%; flex: none; }
751
- .mb-picker-search::placeholder { color: var(--dsw-alias-label-caption, var(--dsw-alias-label-secondary)); }
752
- .mb-picker-search:focus { border-color: var(--dsw-alias-state-business-primary); outline: none; }
753
- .mb-picker-list { min-height: 0; overflow-y: auto; padding: 0 2px; display: flex; flex-direction: column; }
754
- .mb-picker-group { color: var(--dsw-alias-label-tertiary, var(--dsw-alias-label-secondary)); background: var(--dsw-alias-menu-group-header-fill, var(--dsw-menu-surface-fill, var(--dsw-alias-bg-layer-1))); position: sticky; top: 0; z-index: 1; margin-top: 2px; padding: 5px 10px; font-size: 11px; font-weight: 500; line-height: 16px; }
755
- /* The first group header opens the list, so the menu's own padding already
756
- separates it from the top edge; it only drops the inter-group lead-in.
757
- A negative margin cannot do this: the list is a scroll container, and the
758
- overflow clip eats any lead-in that leaves its scrollport. */
759
- .mb-picker-group:first-child { margin-top: 0; }
760
- .mb-picker-row { box-sizing: border-box; border-radius: var(--dsw-radius-md); width: auto; min-width: 100%; color: inherit; text-align: left; cursor: pointer; background: 0 0; border: none; outline: none; align-items: center; gap: 8px; padding: 10px; font: inherit; display: flex; }
761
- .mb-picker-row + .mb-picker-row { margin-top: 2px; }
762
- .mb-picker-row:hover:not(:disabled), .mb-picker-row:focus-visible { background: var(--dsw-alias-interactive-bg-hover); }
763
- .mb-picker-row:disabled { color: var(--dsw-alias-label-dimmed, var(--dsw-alias-label-secondary)); cursor: default; }
764
- .mb-picker-option-copy { flex-direction: column; flex: 1; min-width: 0; gap: 4px; display: flex; }
765
- .mb-picker-model { color: inherit; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; font-weight: 400; line-height: 20px; overflow: hidden; }
766
- .mb-picker-prices { color: var(--dsw-alias-label-caption, var(--dsw-alias-label-secondary)); font-size: 11px; line-height: 17px; font-variant-numeric: tabular-nums; }
767
- .mb-picker-price-line { white-space: normal; overflow-wrap: anywhere; }
768
- .mb-picker-price-source { margin-bottom: 2px; color: var(--dsw-alias-label-secondary); font-weight: 600; }
769
- .mb-picker-check { color: var(--dsw-alias-label-primary); flex: 0 0 14px; place-items: center; display: grid; font-size: 12px; }
770
- .mb-picker-check svg { width: 14px; height: 14px; }
771
- .mb-picker-empty { color: var(--dsw-alias-label-tertiary, var(--dsw-alias-label-secondary)); padding: 8px; font-size: 12px; line-height: 18px; }
772
- .mb-picker-divider { border-top: 1px solid var(--dsw-alias-border-l1); margin: 3px 0; }
773
- .mb-picker-efforts { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; padding: 2px 4px 4px; }
774
- .mb-picker-efforts-label { color: var(--dsw-alias-label-tertiary, var(--dsw-alias-label-secondary)); font-size: 11px; margin-right: 2px; }
775
- .mb-picker-chip { appearance: none; border: 0; background: 0 0; color: var(--dsw-alias-label-secondary); font: inherit; font-size: 11px; height: 24px; padding: 0 7px; border-radius: var(--dsw-radius-sm); cursor: pointer; }
776
- .mb-picker-chip:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover); color: var(--dsw-alias-label-primary); }
777
- .mb-picker-chip[aria-pressed="true"] { background: var(--dsw-alias-interactive-bg-hover); color: var(--dsw-alias-label-primary); }
778
- .mb-picker-error { border-radius: var(--dsw-radius-md); background: var(--dsw-alias-interactive-bg-hover-danger); color: var(--dsw-alias-state-error-primary); margin-bottom: 3px; padding: 6px 7px; font-size: 11px; line-height: 16px; }
779
- .mb-picker-retry { align-self: flex-start; margin: 0 4px 2px; }
780
- .mb-picker-status { color: var(--dsw-alias-label-tertiary, var(--dsw-alias-label-secondary)); padding: 4px 7px; font-size: 11px; line-height: 16px; }
781
- .mb-picker-dot { width: 12px; height: 12px; border: 2px solid var(--dsw-alias-state-business-primary); border-right-color: transparent; border-radius: 50%; flex: none; animation: mb-picker-spin .7s linear infinite; }
782
- @keyframes mb-picker-spin { to { transform: rotate(360deg); } }
783
- .mb-picker-toast { position: absolute; left: 8px; right: 8px; bottom: 8px; z-index: 3; border-radius: var(--dsw-radius-md); background: var(--dsw-alias-state-error-primary); color: #fff; padding: 7px 9px; font-size: 12px; line-height: 16px; box-shadow: var(--dsw-elevation-prominent); }
784
- .mb-fatal { border: 1px solid var(--dsw-alias-state-error-primary); color: var(--dsw-alias-state-error-primary); border-radius: 8px; padding: 12px; }
785
- @media (max-width: 720px) {
786
- .mb-price-grid { grid-template-columns: 1fr; }
787
- .mb-summary { grid-template-columns: repeat(2, minmax(0, 1fr)); }
788
- .mb-head-actions { width: 100%; margin-left: 38px; justify-content: flex-start; }
789
- }
790
- `
791
-
792
- const STYLE_MARKER = 'data-dsh-model-billing'
793
-
794
- // The stylesheet belongs to the plugin, not to any one surface, so inject it
795
- // once into <head> instead of rendering a copy per settings page, dock,
796
- // panel and picker.
797
- function ensureStylesheet() {
798
- if (typeof document === 'undefined') return
799
- if (document.head.querySelector(`style[${STYLE_MARKER}]`) !== null) return
800
- const style = document.createElement('style')
801
- style.setAttribute(STYLE_MARKER, '')
802
- style.textContent = CSS
803
- document.head.appendChild(style)
804
- }
805
-
806
- function Style() {
807
- ensureStylesheet()
808
- return null
809
- }
810
-
811
- // ---------------------------------------------------------------------
812
- // Settings section
813
- // ---------------------------------------------------------------------
814
-
815
- const PRICE_FIELDS = ['cacheHit', 'cacheMiss', 'output']
816
- const PRICE_LABEL_KEYS = {
817
- cacheHit: 'cacheHitPrice',
818
- cacheMiss: 'cacheMissPrice',
819
- output: 'outputPrice',
820
- }
821
-
822
- function toDraftPricing(pricing) {
823
- if (pricing?.builtin === true) return { ...pricing, peak: { ...pricing.peak }, offPeak: { ...pricing.offPeak } }
824
- const source = pricing ?? {}
825
- return Object.fromEntries(PRICE_FIELDS.map((field) => [
826
- field,
827
- source[field] === null || source[field] === undefined ? '' : String(source[field]),
828
- ]))
829
- }
830
-
831
- function toDraft(models) {
832
- return models.map((model) => ({
833
- provider: model.provider,
834
- providerName: model.providerName,
835
- model: model.model,
836
- name: model.name,
837
- isDeepSeek: model.isDeepSeek,
838
- pricing: toDraftPricing(model.pricing),
839
- }))
840
- }
841
-
842
- function toDraftArchive(entries) {
843
- return (entries ?? []).map((entry) => ({
844
- provider: entry.provider,
845
- model: entry.model,
846
- pricing: toDraftPricing(entry.pricing),
847
- }))
848
- }
849
-
850
- function parsePriceInput(value) {
851
- if (typeof value === 'string' && value.trim() === '') return null
852
- const number = Number(value)
853
- if (!Number.isFinite(number) || number < 0 || number > 1e12) return undefined
854
- return Math.round(number * 1e8) / 1e8
855
- }
856
-
857
- function SettingsSection() {
858
- const translate = useT()
859
- const snap = useCatalog()
860
- const [draft, setDraft] = useState(null)
861
- const [archive, setArchive] = useState(null)
862
- const [display, setDisplay] = useState(null)
863
- const [dirty, setDirty] = useState(false)
864
- const [saving, setSaving] = useState(false)
865
- const [status, setStatus] = useState(null)
866
-
867
- useEffect(() => {
868
- if (snap.data === null) return
869
- setDraft(toDraft(snap.data.models))
870
- setArchive(toDraftArchive(snap.data.pricingArchive))
871
- setDisplay(readDisplay(snap.data))
872
- setDirty(false)
873
- }, [snap.data])
874
-
875
- const models = draft ?? []
876
- const archiveRows = archive ?? []
877
- const update = (index, change) => {
878
- setDraft((current) => current === null ? current : current.map((entry, at) => at === index ? change(entry) : entry))
879
- setDirty(true)
880
- setStatus(null)
881
- }
882
-
883
- const updatePrice = (index, field, value) => {
884
- update(index, (entry) => ({
885
- ...entry,
886
- pricing: { ...entry.pricing, [field]: value },
887
- }))
888
- }
889
-
890
- // "Custom prices" starts from blank fields; the user opts in explicitly.
891
- const resetPricing = (index) => {
892
- update(index, (entry) => ({
893
- ...entry,
894
- pricing: { cacheHit: '', cacheMiss: '', output: '' },
895
- }))
896
- }
897
-
898
- // "Restore official prices" puts the built-in peak/off-peak card back
899
- // immediately, so the user sees the official table before saving.
900
- const restoreOfficialPricing = (index) => {
901
- const entry = models[index]
902
- if (entry === undefined) return
903
- const catalogRow = (snap.data?.models ?? []).find((row) =>
904
- row.provider === entry.provider && row.model === entry.model)
905
- const official = catalogRow?.pricing?.official
906
- ?? (catalogRow?.pricing?.builtin === true ? catalogRow.pricing : null)
907
- const restored = official?.builtin === true
908
- ? toDraftPricing(official)
909
- : { cacheHit: '', cacheMiss: '', output: '' }
910
- update(index, (current) => ({ ...current, pricing: restored }))
911
- }
912
-
913
- const move = (index, delta) => {
914
- const target = index + delta
915
- if (target < 0 || target >= models.length) return
916
- setDraft((current) => {
917
- if (current === null) return current
918
- const next = [...current]
919
- const [item] = next.splice(index, 1)
920
- next.splice(target, 0, item)
921
- return next
922
- })
923
- setDirty(true)
924
- setStatus(null)
925
- }
926
-
927
- const updateArchive = (index, change) => {
928
- setArchive((current) => current === null ? current : current.map((entry, at) => at === index ? change(entry) : entry))
929
- setDirty(true)
930
- setStatus(null)
931
- }
932
-
933
- const updateArchivePrice = (index, field, value) => {
934
- updateArchive(index, (entry) => ({ ...entry, pricing: { ...entry.pricing, [field]: value } }))
935
- }
936
-
937
- const addArchive = () => {
938
- setArchive((current) => [...(current ?? []), { provider: '', model: '', pricing: { cacheHit: '', cacheMiss: '', output: '' } }])
939
- setDirty(true)
940
- setStatus(null)
941
- }
942
-
943
- const removeArchive = (index) => {
944
- setArchive((current) => current === null ? current : current.filter((entry, at) => at !== index))
945
- setDirty(true)
946
- setStatus(null)
947
- }
948
-
949
- const reset = () => {
950
- emitCatalog()
951
- if (snap.data !== null) {
952
- setDraft(toDraft(snap.data.models))
953
- setArchive(toDraftArchive(snap.data.pricingArchive))
954
- setDisplay(readDisplay(snap.data))
955
- }
956
- setDirty(false)
957
- setStatus(null)
958
- }
959
-
960
- const displayValue = display ?? readDisplay(snap.data)
961
-
962
- const updateDisplay = (change) => {
963
- setDisplay((current) => ({ ...(current ?? readDisplay(snap.data)), ...change }))
964
- setDirty(true)
965
- setStatus(null)
966
- }
967
-
968
- const masterToggle = (field, label, hint) => h('label', { className: 'mb-master' },
969
- h('input', {
970
- type: 'checkbox',
971
- checked: displayValue[field] === true,
972
- onChange: (event) => updateDisplay({ [field]: event.target.checked }),
973
- }),
974
- h('span', { className: 'mb-master-label' }, translate(label)),
975
- hint === undefined ? null : h('span', { className: 'mb-master-hint' }, hint))
976
-
977
- const renderDisplaySettings = () => h(Fragment, null,
978
- h('div', { className: 'mb-display' },
979
- h('div', { className: 'mb-display-head' },
980
- h('div', { className: 'mb-display-title' }, translate('displaySection')),
981
- h('div', { className: 'mb-display-hint' }, translate('displayHint'))),
982
- masterToggle('showTurnTail', 'displayTurnTail'),
983
- h('div', { className: 'mb-check-grid mb-subchecks' },
984
- [
985
- ['showProvider', 'displayProvider'],
986
- ['showModels', 'displayModels'],
987
- ['showCost', 'displayCost'],
988
- ['showTokens', 'displayTokens'],
989
- ['showCacheHit', 'displayCacheHit'],
990
- ['showLabels', 'displayLabels'],
991
- ].map(([field, label]) => {
992
- const disabled = displayValue.showTurnTail !== true ||
993
- (field === 'showProvider' && displayValue.showModels !== true)
994
- return h('label', {
995
- className: `mb-check${disabled ? ' is-disabled' : ''}`,
996
- key: field,
997
- },
998
- h('input', {
999
- type: 'checkbox',
1000
- checked: displayValue[field] === true,
1001
- disabled,
1002
- onChange: (event) => updateDisplay({ [field]: event.target.checked }),
1003
- }),
1004
- h('span', null, translate(label)))
1005
- })),
1006
- h('div', { className: 'mb-display-controls' },
1007
- h('label', { className: 'mb-field' },
1008
- h('span', { className: 'mb-field-label' }, translate('displayFontSize')),
1009
- h('select', {
1010
- className: 'mb-select',
1011
- value: displayValue.fontSize,
1012
- onChange: (event) => updateDisplay({ fontSize: event.target.value }),
1013
- },
1014
- h('option', { value: 'small' }, translate('fontSizeSmall')),
1015
- h('option', { value: 'normal' }, translate('fontSizeNormal')),
1016
- h('option', { value: 'large' }, translate('fontSizeLarge')))),
1017
- h('label', { className: 'mb-field' },
1018
- h('span', { className: 'mb-field-label' }, translate('displayDensity')),
1019
- h('select', {
1020
- className: 'mb-select',
1021
- value: displayValue.density,
1022
- onChange: (event) => updateDisplay({ density: event.target.value }),
1023
- },
1024
- h('option', { value: 'compact' }, translate('densityCompact')),
1025
- h('option', { value: 'comfortable' }, translate('densityComfortable')))),
1026
- h('label', { className: 'mb-field' },
1027
- h('span', { className: 'mb-field-label' }, translate('displayWeight')),
1028
- h('select', {
1029
- className: 'mb-select',
1030
- value: displayValue.fontWeight,
1031
- onChange: (event) => updateDisplay({ fontWeight: event.target.value }),
1032
- },
1033
- h('option', { value: 'normal' }, translate('weightNormal')),
1034
- h('option', { value: 'bold' }, translate('weightBold')))))),
1035
- h('div', { className: 'mb-display' },
1036
- h('div', { className: 'mb-display-head' },
1037
- h('div', { className: 'mb-display-title' }, translate('dockSection')),
1038
- h('div', { className: 'mb-display-hint' }, translate('dockHint'))),
1039
- masterToggle('showDock', 'displayDock'),
1040
- h('div', { className: 'mb-check-grid mb-subchecks' },
1041
- h('label', { className: `mb-check${displayValue.showDock === true ? '' : ' is-disabled'}` },
1042
- h('input', {
1043
- type: 'checkbox',
1044
- checked: displayValue.showUnpricedNotice === true,
1045
- disabled: displayValue.showDock !== true,
1046
- onChange: (event) => updateDisplay({ showUnpricedNotice: event.target.checked }),
1047
- }),
1048
- h('span', null, translate('showUnpricedNotice'))))),
1049
- h('div', { className: 'mb-display' },
1050
- h('div', { className: 'mb-display-head' },
1051
- h('div', { className: 'mb-display-title' }, translate('pickerSection')),
1052
- h('div', { className: 'mb-display-hint' }, translate('takeoverHint'))),
1053
- h('label', { className: 'mb-check' },
1054
- h('input', {
1055
- type: 'checkbox',
1056
- checked: displayValue.takeoverPicker === true,
1057
- onChange: (event) => updateDisplay({ takeoverPicker: event.target.checked }),
1058
- }),
1059
- h('span', null, translate('takeoverPicker')))))
1060
-
1061
- const renderArchive = () => h('div', { className: 'mb-display' },
1062
- h('div', { className: 'mb-display-head' },
1063
- h('div', { className: 'mb-display-title' }, translate('archiveSection')),
1064
- h('div', { className: 'mb-display-hint' }, translate('archiveHint'))),
1065
- archiveRows.length === 0
1066
- ? h('div', { className: 'mb-muted' }, translate('archiveEmpty'))
1067
- : h('div', { className: 'mb-archive-list' },
1068
- archiveRows.map((entry, index) => h('div', { className: 'mb-archive-row', key: index },
1069
- h('input', {
1070
- className: 'mb-input mb-archive-provider',
1071
- placeholder: translate('archiveProvider'),
1072
- value: entry.provider,
1073
- onChange: (event) => updateArchive(index, (current) => ({ ...current, provider: event.target.value })),
1074
- }),
1075
- h('input', {
1076
- className: 'mb-input mb-archive-model',
1077
- placeholder: translate('archiveModel'),
1078
- value: entry.model,
1079
- onChange: (event) => updateArchive(index, (current) => ({ ...current, model: event.target.value })),
1080
- }),
1081
- ...PRICE_FIELDS.map((field) => h('input', {
1082
- key: field,
1083
- className: 'mb-input mb-archive-price',
1084
- inputMode: 'decimal',
1085
- placeholder: translate(PRICE_LABEL_KEYS[field]),
1086
- 'aria-label': translate(PRICE_LABEL_KEYS[field]),
1087
- value: entry.pricing[field] ?? '',
1088
- onChange: (event) => updateArchivePrice(index, field, event.target.value),
1089
- })),
1090
- h('button', {
1091
- type: 'button',
1092
- className: 'mb-btn',
1093
- title: translate('remove'),
1094
- 'aria-label': translate('remove'),
1095
- onClick: () => removeArchive(index),
1096
- }, translate('remove'))))),
1097
- h('div', { className: 'mb-archive-actions' },
1098
- h('button', { type: 'button', className: 'mb-btn', onClick: addArchive }, translate('addArchive'))))
1099
-
1100
- const save = async () => {
1101
- if (draft === null) return
1102
- const payload = []
1103
- for (const entry of draft) {
1104
- if (entry.pricing.builtin === true) {
1105
- payload.push({ provider: entry.provider, model: entry.model, pricing: {} })
1106
- continue
1107
- }
1108
- const pricing = {}
1109
- const blank = PRICE_FIELDS.every((field) => String(entry.pricing[field] ?? '').trim() === '')
1110
- if (blank) {
1111
- // Three blank fields mean "drop the override", so the official table applies again.
1112
- payload.push({ provider: entry.provider, model: entry.model, pricing: {} })
1113
- continue
1114
- }
1115
- for (const field of PRICE_FIELDS) {
1116
- const parsed = parsePriceInput(entry.pricing[field])
1117
- if (parsed === undefined) {
1118
- setStatus({ tone: 'error', text: translate('invalidPrice') })
1119
- return
1120
- }
1121
- pricing[field] = parsed
1122
- }
1123
- payload.push({ provider: entry.provider, model: entry.model, pricing })
1124
- }
1125
- const archivePayload = []
1126
- const currentKeys = new Set(draft.map((entry) => `${entry.provider}\u0000${entry.model}`))
1127
- for (const entry of archiveRows) {
1128
- const provider = entry.provider.trim()
1129
- const model = entry.model.trim()
1130
- if (provider === '' && model === '') continue
1131
- if (provider === '' || model === '') {
1132
- setStatus({ tone: 'error', text: translate('invalidRoute') })
1133
- return
1134
- }
1135
- if (currentKeys.has(`${provider}\u0000${model}`)) {
1136
- setStatus({ tone: 'error', text: translate('archiveConflict') })
1137
- return
1138
- }
1139
- const pricing = {}
1140
- const blank = PRICE_FIELDS.every((field) => String(entry.pricing[field] ?? '').trim() === '')
1141
- if (blank) {
1142
- archivePayload.push({ provider, model, pricing: {} })
1143
- continue
1144
- }
1145
- for (const field of PRICE_FIELDS) {
1146
- const parsed = parsePriceInput(entry.pricing[field])
1147
- if (parsed === undefined) {
1148
- setStatus({ tone: 'error', text: translate('invalidPrice') })
1149
- return
1150
- }
1151
- pricing[field] = parsed
1152
- }
1153
- archivePayload.push({ provider, model, pricing })
1154
- }
1155
- setSaving(true)
1156
- setStatus(null)
1157
- try {
1158
- await requestJson('/model-billing/save', {
1159
- method: 'POST',
1160
- headers: { 'content-type': 'application/json' },
1161
- body: JSON.stringify({ models: payload, pricingArchive: archivePayload, display: displayValue }),
1162
- })
1163
- setDirty(false)
1164
- setStatus({ tone: 'success', text: translate('saved') })
1165
- await ensureCatalog(true)
1166
- } catch (error) {
1167
- setStatus({ tone: 'error', text: translate('saveFailed', { message: error instanceof Error ? error.message : String(error) }) })
1168
- } finally {
1169
- setSaving(false)
1170
- }
1171
- }
1172
-
1173
- if (snap.data === null) {
1174
- return h(Fragment, null,
1175
- h(Style),
1176
- h('section', { className: 'mb-page' },
1177
- h('h2', { className: 'mb-title' }, translate('title')),
1178
- snap.status === 'error'
1179
- ? h('div', { className: 'mb-fatal' },
1180
- h('div', null, translate('loadFailed')),
1181
- h('p', { className: 'mb-note' }, snap.error),
1182
- h('button', { type: 'button', className: 'mb-btn', onClick: () => void ensureCatalog(true) }, translate('retry')))
1183
- : h('p', { className: 'mb-muted' }, translate('loading'))))
1184
- }
1185
-
1186
- if (draft === null) {
1187
- return h(Fragment, null,
1188
- h(Style),
1189
- h('section', { className: 'mb-page' },
1190
- h('h2', { className: 'mb-title' }, translate('title')),
1191
- h('p', { className: 'mb-muted' }, translate('loading'))))
1192
- }
1193
-
1194
- const renderToolbar = () => h('div', { className: 'mb-toolbar' },
1195
- h('span', { className: 'mb-muted' }, translate('modelsCount', { count: models.length })),
1196
- h('span', { className: 'mb-spacer' }),
1197
- dirty ? h('span', { className: 'mb-muted' }, translate('unsaved')) : null,
1198
- h('button', { type: 'button', className: 'mb-btn', disabled: !dirty || saving, onClick: reset }, translate('undo')),
1199
- h('button', { type: 'button', className: 'mb-btn mb-btn-primary', disabled: !dirty || saving, onClick: () => void save() }, saving ? translate('saving') : translate('save')))
1200
-
1201
- if (models.length === 0) {
1202
- // The display controls stay editable with no models configured, so the
1203
- // toolbar (and its save button) must still render here.
1204
- return h(Fragment, null,
1205
- h(Style),
1206
- h('section', { className: 'mb-page' },
1207
- h('h2', { className: 'mb-title' }, translate('title')),
1208
- h('p', { className: 'mb-intro' }, translate('intro')),
1209
- renderToolbar(),
1210
- status === null ? null : h('div', { className: 'mb-status', 'data-tone': status.tone, role: 'status' }, status.text),
1211
- renderDisplaySettings(),
1212
- renderArchive(),
1213
- h('div', { className: 'mb-empty' }, translate('noModels'))))
1214
- }
1215
-
1216
- return h(Fragment, null,
1217
- h(Style),
1218
- h('section', { className: 'mb-page' },
1219
- h('h2', { className: 'mb-title' }, translate('title')),
1220
- h('p', { className: 'mb-intro' }, translate('intro')),
1221
- renderToolbar(),
1222
- status === null ? null : h('div', { className: 'mb-status', 'data-tone': status.tone, role: 'status' }, status.text),
1223
- renderDisplaySettings(),
1224
- h('p', { className: 'mb-note' }, translate('orderHint')),
1225
- h('ul', { className: 'mb-list' },
1226
- models.map((entry, index) => h(SettingsModelCard, {
1227
- key: `${entry.provider}\u0000${entry.model}`,
1228
- entry,
1229
- index,
1230
- count: models.length,
1231
- translate,
1232
- onMove: move,
1233
- onUpdatePrice: updatePrice,
1234
- onResetPricing: resetPricing,
1235
- onRestoreOfficial: restoreOfficialPricing,
1236
- }))),
1237
- renderArchive(),
1238
- h('p', { className: 'mb-footer-note' }, translate('formula')),
1239
- h('p', { className: 'mb-footer-note' }, translate('formulaNote'))))
1240
- }
1241
-
1242
- function SettingsModelCard({ entry, index, count, translate, onMove, onUpdatePrice, onResetPricing, onRestoreOfficial }) {
1243
- const id = useId()
1244
- const pricing = entry.pricing
1245
- const builtin = pricing.builtin === true
1246
- const custom = entry.isDeepSeek && !builtin
1247
- const fields = builtin ? null : PRICE_FIELDS.map((field) => {
1248
- const inputId = `${id}-${field}`
1249
- return h('label', { className: 'mb-field', htmlFor: inputId, key: field },
1250
- h('span', { className: 'mb-field-label' }, translate(PRICE_LABEL_KEYS[field])),
1251
- h('input', {
1252
- id: inputId,
1253
- className: 'mb-input',
1254
- type: 'text',
1255
- inputMode: 'decimal',
1256
- autoComplete: 'off',
1257
- placeholder: translate('pricePlaceholder'),
1258
- value: pricing[field],
1259
- onChange: (event) => onUpdatePrice(index, field, event.target.value),
1260
- }))
1261
- })
1262
-
1263
- return h('li', { className: 'mb-card' },
1264
- h('div', { className: 'mb-card-head' },
1265
- h('div', { className: 'mb-index' }, index + 1),
1266
- h('div', { style: { minWidth: 0 } },
1267
- h('div', { className: 'mb-name' },
1268
- entry.name,
1269
- entry.isDeepSeek ? h('span', { className: 'mb-tag' }, translate('deepseekTag')) : null),
1270
- h('div', { className: 'mb-sub' }, `${entry.providerName} · ${entry.provider} / ${entry.model}`)),
1271
- h('div', { className: 'mb-head-actions' },
1272
- h('button', {
1273
- type: 'button',
1274
- className: 'mb-btn mb-btn-icon',
1275
- disabled: index === 0,
1276
- title: translate('up'),
1277
- 'aria-label': translate('up'),
1278
- onClick: () => onMove(index, -1),
1279
- }, '↑'),
1280
- h('button', {
1281
- type: 'button',
1282
- className: 'mb-btn mb-btn-icon',
1283
- disabled: index === count - 1,
1284
- title: translate('down'),
1285
- 'aria-label': translate('down'),
1286
- onClick: () => onMove(index, 1),
1287
- }, '↓'))),
1288
- entry.isDeepSeek ? h('p', { className: 'mb-note' }, translate('deepseekNote')) : null,
1289
- h('div', { className: 'mb-prices' },
1290
- h('div', { className: 'mb-price-group' },
1291
- h('div', { className: 'mb-price-group-title' },
1292
- h('span', null, builtin ? translate('officialPrice') : translate('singlePrice')),
1293
- custom ? h('span', { className: 'mb-tag' }, translate('priceSourceCustom')) : null,
1294
- h('span', { className: 'mb-muted', style: { fontWeight: 400, fontSize: '11px' } }, translate('perMillion'))),
1295
- builtin
1296
- ? h(Fragment, null,
1297
- h(PriceLines, { pricing, translate, showSource: false }),
1298
- h('p', { className: 'mb-note' }, translate('deepseekSchedule')))
1299
- : h(Fragment, null,
1300
- h('div', { className: 'mb-price-grid' }, fields),
1301
- custom ? h('p', { className: 'mb-note' }, translate('customPriceNote')) : null),
1302
- entry.isDeepSeek
1303
- ? h('div', { className: 'mb-card-actions' },
1304
- h('button', {
1305
- type: 'button',
1306
- className: 'mb-btn',
1307
- onClick: () => builtin ? onResetPricing(index) : onRestoreOfficial(index),
1308
- }, builtin ? translate('customizePrice') : translate('useOfficialPrice')))
1309
- : null)))
1310
- }
1311
-
1312
- // ---------------------------------------------------------------------
1313
- // Composer dock cost summary
1314
- // ---------------------------------------------------------------------
1315
-
1316
- function CostDock({ sessionId, useProjection }) {
1317
- const translate = useT()
1318
- const tokenUsage = useProjection('tokenUsage')
1319
- const revision = tokenUsageRevision(tokenUsage)
1320
- const usage = useSessionUsage(sessionId, revision)
1321
- const catalogSnap = useCatalog()
1322
- const [open, setOpen] = useState(false)
1323
- const [panelPos, setPanelPos] = useState(null)
1324
- const [editingRoute, setEditingRoute] = useState(null)
1325
- const [editPrices, setEditPrices] = useState({ cacheHit: '', cacheMiss: '', output: '' })
1326
- const [savingPrice, setSavingPrice] = useState(false)
1327
- const [priceStatus, setPriceStatus] = useState(null)
1328
- const triggerRef = useRef(null)
1329
- const panelRef = useRef(null)
1330
-
1331
- useLayoutEffect(() => {
1332
- if (!open) {
1333
- setPanelPos(null)
1334
- return undefined
1335
- }
1336
- const place = () => {
1337
- const rect = triggerRef.current?.getBoundingClientRect()
1338
- if (rect === undefined) return
1339
- const width = Math.min(660, window.innerWidth - 24)
1340
- const naturalLeft = Math.max(12, Math.min(rect.left, window.innerWidth - width - 12))
1341
- // The dock sits near the right edge; shift the whole detail panel left so
1342
- // the bottom-right whale widget cannot cover it.
1343
- const left = Math.max(12, naturalLeft - 120)
1344
- const spaceAbove = rect.top - 12
1345
- const spaceBelow = window.innerHeight - rect.bottom - 12
1346
- const above = spaceAbove > spaceBelow
1347
- const maxHeight = Math.min(430, Math.max(180, above ? spaceAbove : spaceBelow))
1348
- setPanelPos(above
1349
- ? { left, width, bottom: window.innerHeight - rect.top + 8, maxHeight }
1350
- : { left, width, top: rect.bottom + 8, maxHeight })
1351
- }
1352
- place()
1353
- window.addEventListener('resize', place)
1354
- window.addEventListener('scroll', place, true)
1355
- return () => {
1356
- window.removeEventListener('resize', place)
1357
- window.removeEventListener('scroll', place, true)
1358
- }
1359
- }, [open])
1360
-
1361
- useEffect(() => {
1362
- if (!open) return undefined
1363
- const closeOutside = (event) => {
1364
- if (triggerRef.current?.contains(event.target) === true) return
1365
- if (panelRef.current?.contains(event.target) === true) return
1366
- setOpen(false)
1367
- }
1368
- const closeOnEscape = (event) => {
1369
- if (event.key !== 'Escape') return
1370
- event.preventDefault()
1371
- setOpen(false)
1372
- triggerRef.current?.focus()
1373
- }
1374
- document.addEventListener('mousedown', closeOutside)
1375
- document.addEventListener('keydown', closeOnEscape)
1376
- // A dialog must take focus when it opens so keyboard users land inside it.
1377
- panelRef.current?.focus()
1378
- return () => {
1379
- document.removeEventListener('mousedown', closeOutside)
1380
- document.removeEventListener('keydown', closeOnEscape)
1381
- }
1382
- }, [open])
1383
-
1384
- // Hooks must run on every render, so derive the catalog indexes before any
1385
- // early return; React rejects a different hook count between renders.
1386
- const orderIndex = useMemo(() =>
1387
- new Map((catalogSnap.data?.models ?? []).map((model) => [`${model.provider}\u0000${model.model}`, model.order])),
1388
- [catalogSnap.data])
1389
- const catalogByKey = useMemo(() =>
1390
- new Map((catalogSnap.data?.models ?? []).map((model) => [`${model.provider}\u0000${model.model}`, model])),
1391
- [catalogSnap.data])
1392
-
1393
- const display = readDisplay(catalogSnap.data)
1394
- if (display.showDock === false) return null
1395
-
1396
- const data = usage.data
1397
- if (data === null || data === undefined || data.totals?.hasBillable !== true) {
1398
- return null
1399
- }
1400
- const totals = data.totals
1401
- const unpricedRoutes = data.unpricedRoutes ?? []
1402
- const beginRouteEdit = (route) => {
1403
- setEditingRoute(`${route.provider}\u0000${route.model}`)
1404
- setEditPrices({ cacheHit: '', cacheMiss: '', output: '' })
1405
- setPriceStatus(null)
1406
- }
1407
- const saveRoutePrice = async (route) => {
1408
- const pricing = {}
1409
- for (const field of PRICE_FIELDS) {
1410
- const parsed = parsePriceInput(editPrices[field])
1411
- if (parsed === undefined || parsed === null) {
1412
- setPriceStatus({ tone: 'error', text: translate('invalidPrice') })
1413
- return
1414
- }
1415
- pricing[field] = parsed
1416
- }
1417
- setSavingPrice(true)
1418
- setPriceStatus(null)
1419
- try {
1420
- await requestJson('/model-billing/save', {
1421
- method: 'POST',
1422
- headers: { 'content-type': 'application/json' },
1423
- body: JSON.stringify({ pricingArchive: [{ provider: route.provider, model: route.model, pricing }] }),
1424
- })
1425
- await ensureCatalog(true)
1426
- setEditingRoute(null)
1427
- setPriceStatus({ tone: 'success', text: translate('priceSaved') })
1428
- await ensureUsage(sessionId, revision, true)
1429
- } catch (error) {
1430
- setPriceStatus({ tone: 'error', text: translate('saveFailed', { message: error instanceof Error ? error.message : String(error) }) })
1431
- } finally {
1432
- setSavingPrice(false)
1433
- }
1434
- }
1435
- const last = [...(data.turns ?? [])].reverse().find((entry) => entry.hasBillable) ?? null
1436
- const totalText = totals.costKnown
1437
- ? `≈ ${formatMoney(totals.cost)}`
1438
- : totals.pricedAttempts > 0 ? `≈ ${formatMoney(totals.cost)}` : translate('pricePlaceholder')
1439
- const lastText = last === null || last === undefined || !last.hasBillable
1440
- ? null
1441
- : last.costKnown ? `≈ ${formatMoney(last.cost)}` : last.pricedAttempts > 0 ? `≈ ${formatMoney(last.cost)}` : translate('pricePlaceholder')
1442
- // Rebuilding the index and scanning the catalog on every render gets
1443
- // expensive on a long session, so both indexes are memoized above.
1444
- const byModel = [...(data.byModel ?? [])].sort((left, right) => {
1445
- const leftKey = left.provider === null ? '' : `${left.provider}\u0000${left.model}`
1446
- const rightKey = right.provider === null ? '' : `${right.provider}\u0000${right.model}`
1447
- return (orderIndex.get(leftKey) ?? Number.MAX_SAFE_INTEGER) - (orderIndex.get(rightKey) ?? Number.MAX_SAFE_INTEGER)
1448
- })
1449
-
1450
- const modelNameOf = (model) => {
1451
- const catalogRow = model?.provider === null || model?.provider === undefined
1452
- ? undefined
1453
- : catalogByKey.get(`${model.provider}\u0000${model.model}`)
1454
- return catalogRow?.name ?? model?.name ?? model?.model ?? '—'
1455
- }
1456
- const turnModelNames = (turn) => {
1457
- const names = []
1458
- for (const model of turn.models ?? []) {
1459
- const name = modelNameOf(model)
1460
- if (!names.includes(name)) names.push(name)
1461
- }
1462
- return names
1463
- }
1464
-
1465
- const header = h('thead', null,
1466
- h('tr', null,
1467
- h('th', null, translate('turn')),
1468
- h('th', null, translate('content')),
1469
- h('th', null, translate('time')),
1470
- h('th', null, translate('model')),
1471
- h('th', { className: 'num' }, translate('cost')),
1472
- h('th', { className: 'num' }, translate('tokens')),
1473
- h('th', { className: 'num' }, translate('cacheHit'))))
1474
-
1475
- const turnRows = [...(data.turns ?? [])].reverse().slice(0, 20).map((turn) => {
1476
- const costText = turn.costKnown
1477
- ? `≈ ${formatMoney(turn.cost)}`
1478
- : turn.pricedAttempts > 0 ? `≈ ${formatMoney(turn.cost)}` : translate('pricePlaceholder')
1479
- const modelNames = turnModelNames(turn)
1480
- const modelText = modelNames.length === 0
1481
- ? '—'
1482
- : modelNames.length === 1 ? modelNames[0] : `${modelNames[0]} +${modelNames.length - 1}`
1483
- return h('tr', { key: turn.turn },
1484
- h('td', null, `#${turn.turn}`),
1485
- h('td', { className: 'mb-turn-preview', title: turn.preview }, turn.preview || '—'),
1486
- h('td', null, `${timeText(turn.startTime)}–${timeText(turn.endTime)}`),
1487
- h('td', { className: 'mb-turn-model', title: modelNames.join(' / ') }, modelText),
1488
- h('td', { className: 'num' }, costText),
1489
- h('td', { className: 'num' }, `${formatTokens(turn.promptTokens)} / ${formatTokens(turn.outputTokens)}`),
1490
- h('td', { className: 'num' }, formatPercent(turn.cacheHit)))
1491
- })
1492
-
1493
- const turnTable = h('div', { style: { overflowX: 'auto' } },
1494
- h('table', { className: 'mb-table mb-turn-table' }, header, h('tbody', null, turnRows)))
1495
-
1496
- const modelRows = byModel.map((model) => {
1497
- const catalogRow = model.provider === null
1498
- ? undefined
1499
- : catalogByKey.get(`${model.provider}\u0000${model.model}`)
1500
- const modelName = catalogRow === undefined ? model.name : `${catalogRow.providerName} / ${catalogRow.name}`
1501
- return h('tr', { key: `${model.provider}\u0000${model.model}\u0000${model.name}` },
1502
- h('td', null,
1503
- modelName,
1504
- model.isDeepSeek ? h('span', { className: 'mb-tag' }, translate('deepseekTag')) : null),
1505
- h('td', { className: 'num' },
1506
- model.pricedAttempts > 0 ? `≈ ${formatMoney(model.cost)}` : translate('pricePlaceholder')),
1507
- h('td', { className: 'num', style: { color: 'var(--dsw-alias-label-secondary)' } },
1508
- `${formatTokens(model.promptTokens)} / ${formatTokens(model.outputTokens)}`))
1509
- })
1510
-
1511
- const modelTable = byModel.length === 0
1512
- ? null
1513
- : h('div', { style: { marginTop: '10px' } },
1514
- h('div', { className: 'mb-panel-title', style: { fontSize: '12px', marginBottom: '4px' } }, translate('byModel')),
1515
- h('div', { style: { overflowX: 'auto' } },
1516
- h('table', { className: 'mb-table' }, h('tbody', null, modelRows))))
1517
-
1518
- const editingRouteRow = editingRoute === null
1519
- ? null
1520
- : unpricedRoutes.find((route) => `${route.provider}\u0000${route.model}` === editingRoute) ?? null
1521
- const routeEditor = editingRouteRow === null
1522
- ? null
1523
- : h('div', { className: 'mb-archive-row mb-route-editor', style: { marginTop: '8px' } },
1524
- h('span', { className: 'mb-muted' }, `${editingRouteRow.provider} / ${editingRouteRow.model}`),
1525
- ...PRICE_FIELDS.map((field) => h('input', {
1526
- key: field,
1527
- className: 'mb-input',
1528
- inputMode: 'decimal',
1529
- placeholder: translate(PRICE_LABEL_KEYS[field]),
1530
- 'aria-label': translate(PRICE_LABEL_KEYS[field]),
1531
- value: editPrices[field],
1532
- onChange: (event) => setEditPrices((current) => ({ ...current, [field]: event.target.value })),
1533
- })),
1534
- h('button', {
1535
- type: 'button',
1536
- className: 'mb-btn mb-btn-primary',
1537
- disabled: savingPrice,
1538
- onClick: () => void saveRoutePrice(editingRouteRow),
1539
- }, savingPrice ? translate('saving') : translate('save')))
1540
-
1541
- const unpricedTable = unpricedRoutes.length === 0 || display.showUnpricedNotice === false
1542
- ? null
1543
- : h('div', { style: { marginTop: '10px' } },
1544
- h('div', { className: 'mb-panel-title', style: { fontSize: '12px', marginBottom: '4px' } }, translate('unpricedRoutes')),
1545
- h('div', { style: { overflowX: 'auto' } },
1546
- h('table', { className: 'mb-table' },
1547
- h('tbody', null, unpricedRoutes.map((route) => h('tr', { key: `${route.provider}\u0000${route.model}` },
1548
- h('td', null, `${route.provider} / ${route.model}`),
1549
- h('td', { className: 'num' }, String(route.attempts)),
1550
- h('td', { className: 'num', style: { color: 'var(--dsw-alias-label-secondary)' } },
1551
- `${formatTokens(route.promptTokens)} / ${formatTokens(route.outputTokens)}`),
1552
- h('td', { className: 'num' },
1553
- h('button', {
1554
- type: 'button',
1555
- className: 'mb-btn',
1556
- onClick: () => beginRouteEdit(route),
1557
- }, translate('addPrice')))))))),
1558
- routeEditor,
1559
- priceStatus === null ? null : h('div', { className: 'mb-status', 'data-tone': priceStatus.tone, role: 'status' }, priceStatus.text))
1560
-
1561
- const panelStyle = panelPos === null
1562
- ? { left: 0, top: 0, width: 660, visibility: 'hidden' }
1563
- : {
1564
- left: panelPos.left,
1565
- width: panelPos.width,
1566
- maxHeight: panelPos.maxHeight,
1567
- ...panelPos.top === undefined ? { bottom: panelPos.bottom } : { top: panelPos.top },
1568
- }
1569
-
1570
- const panel = h('div', {
1571
- ref: panelRef,
1572
- className: 'mb-panel mb-panel-floating',
1573
- style: panelStyle,
1574
- role: 'dialog',
1575
- 'aria-modal': true,
1576
- 'aria-label': translate('costDetails'),
1577
- tabIndex: -1,
1578
- },
1579
- h('div', { className: 'mb-panel-head' },
1580
- h('span', { className: 'mb-panel-title' }, translate('costDetails')),
1581
- h('span', { className: 'mb-muted' }, translate('sessionTotal')),
1582
- usage.status === 'error'
1583
- ? h('button', {
1584
- type: 'button',
1585
- className: 'mb-btn',
1586
- onClick: () => void ensureUsage(sessionId, revision, true),
1587
- }, translate('retry'))
1588
- : null,
1589
- h('button', {
1590
- type: 'button',
1591
- className: 'mb-panel-close',
1592
- 'aria-label': translate('close'),
1593
- title: translate('close'),
1594
- onClick: () => {
1595
- setOpen(false)
1596
- triggerRef.current?.focus()
1597
- },
1598
- }, '×')),
1599
- h('div', { className: 'mb-summary' },
1600
- summaryItem(translate('sessionTotal'), totalText),
1601
- summaryItem(translate('thisTurn'), lastText ?? '—'),
1602
- summaryItem(translate('cacheHit'), formatPercent(totals.cacheHit)),
1603
- summaryItem(translate('tokens'), `${formatTokens(totals.promptTokens)} / ${formatTokens(totals.outputTokens)}`)),
1604
- turnTable,
1605
- modelTable,
1606
- unpricedTable,
1607
- h('p', { className: 'mb-panel-note' }, translate('estimateNote')),
1608
- totals.toolCost > 0
1609
- ? h('p', { className: 'mb-panel-note' }, translate('webToolNote', {
1610
- amount: formatMoney(totals.toolCost),
1611
- searchBilled: totals.webSearchBilled ?? 0,
1612
- searchCalls: totals.webSearchCalls ?? 0,
1613
- fetchBilled: totals.webFetchBilled ?? 0,
1614
- fetchCalls: totals.webFetchCalls ?? 0,
1615
- }))
1616
- : null,
1617
- totals.partial && display.showUnpricedNotice
1618
- ? h('p', { className: 'mb-warn' }, translate('partialNote'))
1619
- : null)
1620
-
1621
- return h('div', { className: 'mb-dock' },
1622
- h(Style),
1623
- h('button', {
1624
- ref: triggerRef,
1625
- type: 'button',
1626
- className: 'mb-dock-trigger',
1627
- 'aria-expanded': open ? 'true' : 'false',
1628
- 'aria-haspopup': 'dialog',
1629
- onClick: () => setOpen((value) => !value),
1630
- title: translate('costEstimate'),
1631
- },
1632
- h('svg', {
1633
- className: 'mb-dock-icon',
1634
- viewBox: '0 0 16 16',
1635
- fill: 'none',
1636
- stroke: 'currentColor',
1637
- strokeWidth: 1.2,
1638
- 'aria-hidden': true,
1639
- },
1640
- h('circle', { cx: 8, cy: 8, r: 5.5 }),
1641
- h('path', { d: 'M8 5v6M6.2 6.8h3.6M6.2 9.2h3.6' })),
1642
- h('span', null, translate('costEstimate')),
1643
- h('span', { className: 'mb-dock-amount' }, totalText),
1644
- lastText === null ? null : h(Fragment, null,
1645
- h('span', { className: 'mb-dock-sep' }, '·'),
1646
- h('span', null, `${translate('thisTurn')} ${lastText}`)),
1647
- totals.partial && display.showUnpricedNotice ? h('span', { className: 'mb-warn', style: { margin: 0 } }, translate('partial')) : null),
1648
- open ? ReactDOM.createPortal(panel, document.body) : null)
1649
- }
1650
-
1651
- function summaryItem(label, value) {
1652
- return h('div', { className: 'mb-summary-item', key: label },
1653
- h('div', { className: 'mb-summary-key' }, label),
1654
- h('div', { className: 'mb-summary-value' }, value))
1655
- }
1656
-
1657
- // ---------------------------------------------------------------------
1658
- // Per-turn cost chip
1659
- // ---------------------------------------------------------------------
1660
-
1661
- function turnModelText(models, translate, showProvider, catalogModels) {
1662
- const findModel = (entry) => {
1663
- if (entry.provider === undefined || entry.model === undefined) return undefined
1664
- return (catalogModels ?? []).find((candidate) => candidate.provider === entry.provider && candidate.model === entry.model)
1665
- }
1666
- const names = (models ?? []).map((entry) => {
1667
- if (entry === undefined || entry === null) return null
1668
- const matched = findModel(entry)
1669
- const modelName = typeof matched?.name === 'string' && matched.name !== ''
1670
- ? matched.name
1671
- : typeof entry.name === 'string' && entry.name !== ''
1672
- ? entry.name
1673
- : typeof entry.model === 'string' && entry.model !== '' ? entry.model : null
1674
- if (modelName === null) return null
1675
- if (!showProvider) return modelName
1676
- const providerName = typeof matched?.providerName === 'string' && matched.providerName !== ''
1677
- ? matched.providerName
1678
- : typeof entry.providerName === 'string' && entry.providerName !== ''
1679
- ? entry.providerName
1680
- : typeof entry.provider === 'string' && entry.provider !== '' ? entry.provider : null
1681
- return providerName === null ? modelName : `${providerName} / ${modelName}`
1682
- }).filter((name) => name !== null)
1683
- if (names.length === 0) return null
1684
- return names.join(translate('listSep'))
1685
- }
1686
-
1687
- function TurnCost(props) {
1688
- const { turn } = props
1689
- // The turnTail owner hands over the whole folded turn life cycle, not just its number.
1690
- const turnNumber = turn === null || turn === undefined
1691
- ? undefined
1692
- : typeof turn === 'number' ? turn : turn.turn
1693
- const sessionId = props.sessionId
1694
- const useProjection = props.useProjection
1695
- const translate = useT()
1696
- const catalogSnap = useCatalog()
1697
- const display = readDisplay(catalogSnap.data)
1698
- const tokenUsage = typeof useProjection === 'function' ? useProjection('tokenUsage') : undefined
1699
- const revision = tokenUsageRevision(tokenUsage)
1700
- const usage = useSessionUsage(sessionId, revision)
1701
- // The master switch hides the whole chip while the per-field switches keep
1702
- // their saved values, so turning it back on restores the previous layout.
1703
- if (display.showTurnTail === false) return null
1704
- const rows = usage.data?.turns
1705
- const row = (usage.rowsByTurn instanceof Map ? usage.rowsByTurn.get(turnNumber) : undefined) ??
1706
- rows?.find((entry) => entry.turn === turnNumber)
1707
- if (row === undefined) return null
1708
- const costText = row.costKnown
1709
- ? `≈ ${formatMoney(row.cost)}`
1710
- : row.pricedAttempts > 0 ? `≈ ${formatMoney(row.cost)}` : translate('pricePlaceholder')
1711
- const modelText = turnModelText(row.models, translate, display.showProvider === true, catalogSnap.data?.models)
1712
- const label = (key) => display.showLabels ? `${translate(key)} ` : ''
1713
- const parts = []
1714
- if (display.showModels && modelText !== null) {
1715
- parts.push(h('span', { className: 'mb-turn-item', key: 'model' },
1716
- label('model'), h('strong', null, modelText)))
1717
- }
1718
- if (display.showCost) {
1719
- parts.push(h('span', { className: 'mb-turn-item', key: 'cost' },
1720
- label('turnCost'), h('strong', null, costText)))
1721
- }
1722
- const promptTokens = Number.isFinite(row.promptTokens) ? row.promptTokens : 0
1723
- const outputTokens = Number.isFinite(row.outputTokens) ? row.outputTokens : 0
1724
- if (display.showTokens && (promptTokens > 0 || outputTokens > 0)) {
1725
- parts.push(h('span', { className: 'mb-turn-item', key: 'tokens' },
1726
- label('turnTokens'), h('strong', null, `${formatTokens(promptTokens)} / ${formatTokens(outputTokens)}`)))
1727
- }
1728
- if (display.showCacheHit && row.cacheHit !== null && row.cacheHit !== undefined) {
1729
- parts.push(h('span', { className: 'mb-turn-item', key: 'cache' },
1730
- label('cacheHit'), h('strong', null, formatPercent(row.cacheHit))))
1731
- }
1732
- if (row.partial) {
1733
- parts.push(h('span', { className: 'mb-turn-item mb-turn-partial', key: 'partial' }, translate('partialShort')))
1734
- }
1735
- if (parts.length === 0) return null
1736
- return h('span', {
1737
- className: 'mb-turn',
1738
- 'data-size': display.fontSize,
1739
- 'data-density': display.density,
1740
- 'data-weight': display.fontWeight,
1741
- }, parts)
1742
- }
1743
-
1744
- // ---------------------------------------------------------------------
1745
- // Custom-ordered model picker in the composer seat
1746
- // ---------------------------------------------------------------------
1747
-
1748
- const MODEL_KEY_SEPARATOR = '\u0000'
1749
-
1750
- function OrderedModelPicker({ locked, sessionId, useProjection }) {
1751
- const translate = useT()
1752
- const catalogSnap = useCatalog()
1753
- const selectionProjection = useProjection('modelSelection')
1754
- const directoryRevision = useModelDirectoriesRevision()
1755
- const [directoryTick, setDirectoryTick] = useState(0)
1756
- const directory = useMemo(() => {
1757
- if (modelDirectories === undefined || sessionId === undefined) return null
1758
- try {
1759
- return modelDirectories.directoryFor(sessionId)
1760
- } catch {
1761
- return null
1762
- }
1763
- }, [sessionId, directoryRevision, directoryTick])
1764
-
1765
- const [state, setState] = useState(null)
1766
- const [open, setOpen] = useState(false)
1767
- const [query, setQuery] = useState('')
1768
- const [selecting, setSelecting] = useState(false)
1769
- // The route being submitted, so its row can show a pending spinner, and a
1770
- // transient rejection message that must not push the list around.
1771
- const [pending, setPending] = useState(null)
1772
- const [toast, setToast] = useState(null)
1773
- const [menuPos, setMenuPos] = useState(null)
1774
- // Roving cursor for arrow-key navigation inside the model list.
1775
- const [activeIndex, setActiveIndex] = useState(0)
1776
- const rootRef = useRef(null)
1777
- const triggerRef = useRef(null)
1778
- const menuRef = useRef(null)
1779
- const searchRef = useRef(null)
1780
- const id = useId()
1781
-
1782
- useEffect(() => {
1783
- if (directory === null) {
1784
- setState(null)
1785
- return undefined
1786
- }
1787
- const sync = () => setState(directory.store.getSnapshot())
1788
- sync()
1789
- const unsubscribe = directory.store.subscribe(sync)
1790
- directory.load().catch(() => {})
1791
- return unsubscribe
1792
- }, [directory])
1793
-
1794
- useEffect(() => {
1795
- if (directory !== null || directoryTick >= 20) return undefined
1796
- const timer = setTimeout(() => setDirectoryTick((value) => value + 1), 300)
1797
- return () => clearTimeout(timer)
1798
- }, [directory, directoryTick])
1799
-
1800
- useEffect(() => {
1801
- if (!open) return undefined
1802
- const closeOutside = (event) => {
1803
- if (rootRef.current?.contains(event.target) === true) return
1804
- if (menuRef.current?.contains(event.target) === true) return
1805
- setOpen(false)
1806
- }
1807
- document.addEventListener('mousedown', closeOutside)
1808
- return () => document.removeEventListener('mousedown', closeOutside)
1809
- }, [open])
1810
-
1811
- useEffect(() => {
1812
- if (!open) {
1813
- setQuery('')
1814
- return undefined
1815
- }
1816
- const closeOnEscape = (event) => {
1817
- if (event.key !== 'Escape') return
1818
- event.preventDefault()
1819
- setOpen(false)
1820
- queueMicrotask(() => triggerRef.current?.focus())
1821
- }
1822
- document.addEventListener('keydown', closeOnEscape)
1823
- return () => document.removeEventListener('keydown', closeOnEscape)
1824
- }, [open])
1825
-
1826
- useEffect(() => {
1827
- if (locked && open) setOpen(false)
1828
- }, [locked, open])
1829
-
1830
- // A rejection toast clears itself; the menu is not blocked while it shows.
1831
- useEffect(() => {
1832
- if (toast === null) return undefined
1833
- const timer = setTimeout(() => setToast(null), 4200)
1834
- return () => clearTimeout(timer)
1835
- }, [toast])
1836
-
1837
- const rank = useMemo(() => {
1838
- const map = new Map()
1839
- for (const model of catalogSnap.data?.models ?? []) {
1840
- map.set(`${model.provider}${MODEL_KEY_SEPARATOR}${model.model}`, model.order)
1841
- }
1842
- return map
1843
- }, [catalogSnap.data])
1844
-
1845
- const priceByKey = useMemo(() => {
1846
- const map = new Map()
1847
- for (const model of catalogSnap.data?.models ?? []) {
1848
- map.set(`${model.provider}${MODEL_KEY_SEPARATOR}${model.model}`, model.pricing)
1849
- }
1850
- return map
1851
- }, [catalogSnap.data])
1852
-
1853
- const groups = state?.groups
1854
- const catalogModels = catalogSnap.data?.models
1855
- const entries = useMemo(() => {
1856
- if (Array.isArray(groups) && groups.length > 0) {
1857
- const list = []
1858
- for (const [groupIndex, group] of groups.entries()) {
1859
- for (const [modelIndex, model] of group.models.entries()) {
1860
- list.push({ group, model, groupIndex, modelIndex })
1861
- }
1862
- }
1863
- list.sort((left, right) => {
1864
- const leftRank = rank.get(`${left.group.id}${MODEL_KEY_SEPARATOR}${left.model.id}`) ?? Number.MAX_SAFE_INTEGER
1865
- const rightRank = rank.get(`${right.group.id}${MODEL_KEY_SEPARATOR}${right.model.id}`) ?? Number.MAX_SAFE_INTEGER
1866
- return leftRank - rightRank || left.groupIndex - right.groupIndex || left.modelIndex - right.modelIndex
1867
- })
1868
- return list
1869
- }
1870
- return (catalogModels ?? []).map((model, index) => ({
1871
- group: { id: model.provider, name: model.providerName },
1872
- model: {
1873
- id: model.model,
1874
- name: model.name,
1875
- ...(model.reasoning === undefined ? {} : { reasoning: model.reasoning }),
1876
- },
1877
- groupIndex: model.order ?? index,
1878
- modelIndex: index,
1879
- }))
1880
- }, [groups, catalogModels, rank])
1881
-
1882
- const projected = selectionProjection?.next ?? selectionProjection?.lastUsed ?? null
1883
- const current = state?.current ?? projected
1884
- const currentEntry = current === null
1885
- ? undefined
1886
- : entries.find((entry) => entry.group.id === current.provider && entry.model.id === current.model)
1887
- const currentCatalog = current === null
1888
- ? undefined
1889
- : (catalogModels ?? []).find((model) => model.provider === current.provider && model.model === current.model)
1890
- const currentReasoning = currentEntry?.model.reasoning
1891
- const effectiveEffort = current?.reasoningEffort ?? currentReasoning?.defaultEffort
1892
- const effortLabel = current === null
1893
- ? undefined
1894
- : currentReasoning === undefined
1895
- ? state?.retainedEffort
1896
- : effectiveEffort === undefined
1897
- ? translate('providerDefault')
1898
- : currentReasoning.efforts.find((effort) => effort.id === effectiveEffort)?.name ?? effectiveEffort
1899
- const effortChoices = currentReasoning === undefined
1900
- ? []
1901
- : [
1902
- ...(currentReasoning.defaultEffort === undefined
1903
- ? [{ key: 'provider-default', effort: undefined, label: translate('providerDefault') }]
1904
- : []),
1905
- ...currentReasoning.efforts.map((effort) => ({
1906
- key: `effort:${effort.id}`,
1907
- effort: effort.id,
1908
- label: effort.name,
1909
- })),
1910
- ]
1911
-
1912
- const showSearch = entries.length > 4
1913
- const term = query.trim().toLowerCase()
1914
- const filtered = useMemo(() => {
1915
- if (term === '') return entries
1916
- return entries.filter((entry) => `${entry.model.name} ${entry.group.name}`.toLowerCase().includes(term))
1917
- }, [entries, term])
1918
- // Keep the roving cursor inside the visible list; a shrunk search result
1919
- // must not leave the index pointing past the end.
1920
- const active = filtered.length === 0 ? -1 : Math.min(Math.max(activeIndex, 0), filtered.length - 1)
1921
-
1922
- useLayoutEffect(() => {
1923
- if (!open) {
1924
- setMenuPos(null)
1925
- return undefined
1926
- }
1927
- const place = () => {
1928
- const rect = triggerRef.current?.getBoundingClientRect()
1929
- if (rect === undefined) return
1930
- const width = Math.min(360, window.innerWidth - 24)
1931
- const left = Math.max(12, Math.min(rect.left, window.innerWidth - width - 12))
1932
- const spaceAbove = rect.top - 12
1933
- const spaceBelow = window.innerHeight - rect.bottom - 12
1934
- const openBelow = spaceBelow > spaceAbove
1935
- const maxHeight = Math.min(480, Math.max(160, openBelow ? spaceBelow : spaceAbove))
1936
- setMenuPos(openBelow
1937
- ? { left, top: rect.bottom + 8, width, maxHeight }
1938
- : { left, bottom: window.innerHeight - rect.top + 8, width, maxHeight })
1939
- }
1940
- place()
1941
- window.addEventListener('scroll', place, true)
1942
- window.addEventListener('resize', place)
1943
- return () => {
1944
- window.removeEventListener('scroll', place, true)
1945
- window.removeEventListener('resize', place)
1946
- }
1947
- }, [open, filtered.length, state?.status])
1948
-
1949
- useEffect(() => {
1950
- if (!open) return undefined
1951
- const timer = setTimeout(() => {
1952
- if (showSearch) {
1953
- searchRef.current?.focus()
1954
- return
1955
- }
1956
- const rows = menuRef.current?.querySelectorAll('.mb-picker-row')
1957
- const target = rows?.[active]
1958
- if (target instanceof HTMLElement) target.focus()
1959
- }, 0)
1960
- return () => clearTimeout(timer)
1961
- // Focus follows the roving cursor so arrow keys keep focus on the active row.
1962
- }, [open, showSearch, active])
1963
-
1964
- const loading = catalogSnap.data === null && (state === null || state.status === 'loading')
1965
- const busy = selecting || state?.status === 'selecting'
1966
- const currentName = currentEntry?.model.name ?? currentCatalog?.name
1967
- const triggerModel = currentName ?? (loading ? translate('loadingModels') : translate('chooseModel'))
1968
- const triggerAria = effortLabel === undefined ? triggerModel : `${triggerModel} · ${effortLabel}`
1969
-
1970
- const close = (restoreFocus) => {
1971
- setOpen(false)
1972
- setQuery('')
1973
- setToast(null)
1974
- if (restoreFocus) queueMicrotask(() => triggerRef.current?.focus())
1975
- }
1976
-
1977
- const submit = async (selection) => {
1978
- if (busy) return
1979
- setSelecting(true)
1980
- setToast(null)
1981
- setPending(selection)
1982
- try {
1983
- const result = directory !== null
1984
- ? await directory.select(selection)
1985
- : await requestJson('/model-billing/select', {
1986
- method: 'POST',
1987
- headers: { 'content-type': 'application/json' },
1988
- body: JSON.stringify({ sessionId, ...selection }),
1989
- })
1990
- if (result?.ok === false) {
1991
- // A refusal is a toast: the list stays where the user left it.
1992
- setToast(result.error?.message ?? translate('pickerEmpty'))
1993
- return
1994
- }
1995
- close(true)
1996
- } catch (error) {
1997
- setToast(error instanceof Error ? error.message : String(error))
1998
- } finally {
1999
- setSelecting(false)
2000
- setPending(null)
2001
- }
2002
- }
2003
-
2004
- const chooseModel = (entry) => {
2005
- if (current?.provider === entry.group.id && current?.model === entry.model.id) {
2006
- close(true)
2007
- return
2008
- }
2009
- void submit({ provider: entry.group.id, model: entry.model.id })
2010
- }
2011
-
2012
- const chooseEffort = (effort) => {
2013
- if (current === null) return
2014
- if (effectiveEffort === effort) {
2015
- close(true)
2016
- return
2017
- }
2018
- void submit({
2019
- provider: current.provider,
2020
- model: current.model,
2021
- ...effort === undefined ? {} : { reasoningEffort: effort },
2022
- })
2023
- }
2024
-
2025
- const toggle = () => {
2026
- if (open) {
2027
- close(true)
2028
- return
2029
- }
2030
- setToast(null)
2031
- // Open the cursor on the row the user is already using, when there is one.
2032
- const selectedAt = filtered.findIndex((entry) => entry.group.id === current?.provider && entry.model.id === current?.model)
2033
- setActiveIndex(selectedAt < 0 ? 0 : selectedAt)
2034
- setOpen(true)
2035
- if (directory !== null) directory.load().catch(() => {})
2036
- }
2037
-
2038
- const menuStyle = menuPos === null
2039
- ? { left: 0, top: 0, width: 360, visibility: 'hidden' }
2040
- : {
2041
- left: menuPos.left,
2042
- width: menuPos.width,
2043
- maxHeight: menuPos.maxHeight,
2044
- ...menuPos.top === undefined ? { bottom: menuPos.bottom } : { top: menuPos.top },
2045
- }
2046
-
2047
- // Arrow keys move a roving cursor through the list. Enter and Space are
2048
- // left to the focused row itself, so a <button> keeps its native
2049
- // activation and the menu never fires the choice twice.
2050
- const focusRow = (index) => {
2051
- const rows = menuRef.current?.querySelectorAll('.mb-picker-row')
2052
- const target = rows?.[index]
2053
- if (target instanceof HTMLElement) target.focus()
2054
- }
2055
- const moveActive = (delta) => {
2056
- if (filtered.length === 0) return
2057
- const next = Math.min(Math.max(active + delta, 0), filtered.length - 1)
2058
- setActiveIndex(next)
2059
- focusRow(next)
2060
- }
2061
- const onMenuKeyDown = (event) => {
2062
- if (event.key === 'ArrowDown') {
2063
- event.preventDefault()
2064
- moveActive(1)
2065
- return
2066
- }
2067
- if (event.key === 'ArrowUp') {
2068
- event.preventDefault()
2069
- moveActive(-1)
2070
- return
2071
- }
2072
- if (event.key === 'Home') {
2073
- event.preventDefault()
2074
- if (filtered.length === 0) return
2075
- setActiveIndex(0)
2076
- focusRow(0)
2077
- return
2078
- }
2079
- if (event.key === 'End') {
2080
- event.preventDefault()
2081
- const last = filtered.length - 1
2082
- if (last < 0) return
2083
- setActiveIndex(last)
2084
- focusRow(last)
2085
- }
2086
- }
2087
-
2088
- const menu = h('div', {
2089
- ref: menuRef,
2090
- id: `${id}-menu`,
2091
- className: 'mb-picker-menu',
2092
- style: menuStyle,
2093
- role: 'dialog',
2094
- 'aria-label': translate('pickerAria'),
2095
- onKeyDown: onMenuKeyDown,
2096
- },
2097
- showSearch ? h('input', {
2098
- ref: searchRef,
2099
- className: 'mb-picker-search',
2100
- type: 'search',
2101
- value: query,
2102
- placeholder: translate('searchModel'),
2103
- 'aria-label': translate('searchModel'),
2104
- onChange: (event) => {
2105
- setQuery(event.target.value)
2106
- // A shorter result list must not leave the cursor on a removed row.
2107
- setActiveIndex(0)
2108
- },
2109
- onKeyDown: (event) => {
2110
- // Enter picks the highlighted row; a downward arrow steps into the list.
2111
- if (event.key === 'ArrowDown') {
2112
- event.preventDefault()
2113
- moveActive(1)
2114
- return
2115
- }
2116
- if (event.key !== 'Enter' || filtered.length === 0 || busy) return
2117
- event.preventDefault()
2118
- chooseModel(filtered[active])
2119
- },
2120
- }) : null,
2121
- h('div', { className: 'mb-picker-list', role: 'menu' },
2122
- filtered.length === 0
2123
- ? h('div', { className: 'mb-picker-empty' }, term === '' ? translate('pickerEmpty') : translate('noMatch'))
2124
- : filtered.map((entry, index) => {
2125
- const selected = current?.provider === entry.group.id && current?.model === entry.model.id
2126
- const previous = filtered[index - 1]
2127
- const showGroup = previous === undefined || previous.group.id !== entry.group.id
2128
- const price = priceByKey.get(`${entry.group.id}${MODEL_KEY_SEPARATOR}${entry.model.id}`)
2129
- return h(Fragment, { key: `${entry.group.id}${MODEL_KEY_SEPARATOR}${entry.model.id}` },
2130
- showGroup ? h('div', { className: 'mb-picker-group' }, entry.group.name) : null,
2131
- h('button', {
2132
- type: 'button',
2133
- role: 'menuitemradio',
2134
- className: 'mb-picker-row',
2135
- 'aria-checked': selected ? 'true' : 'false',
2136
- title: entry.model.name,
2137
- // Roving tabindex: only the cursor row is reachable by Tab.
2138
- tabIndex: index === active ? 0 : -1,
2139
- disabled: busy,
2140
- onClick: () => chooseModel(entry),
2141
- },
2142
- h('span', { className: 'mb-picker-option-copy' },
2143
- h('span', { className: 'mb-picker-model' }, entry.model.name),
2144
- h(PriceLines, { pricing: price, translate })),
2145
- h('span', { className: 'mb-picker-check' },
2146
- pending !== null && pending.provider === entry.group.id && pending.model === entry.model.id
2147
- ? h('span', { className: 'mb-picker-dot', 'aria-hidden': true })
2148
- : selected
2149
- ? h('svg', {
2150
- viewBox: '0 0 16 16',
2151
- width: 14,
2152
- height: 14,
2153
- fill: 'none',
2154
- stroke: 'currentColor',
2155
- strokeWidth: 1.7,
2156
- 'aria-hidden': true,
2157
- },
2158
- h('path', { d: 'M3.5 8.5l3 3 6-7' }))
2159
- : null)))
2160
- })),
2161
- effortChoices.length === 0 ? null : h(Fragment, null,
2162
- h('div', { className: 'mb-picker-divider' }),
2163
- h('div', { className: 'mb-picker-efforts' },
2164
- h('span', { className: 'mb-picker-efforts-label' }, translate('reasoningEffort')),
2165
- effortChoices.map((choice) => h('button', {
2166
- type: 'button',
2167
- key: choice.key,
2168
- className: 'mb-picker-chip',
2169
- 'aria-pressed': effectiveEffort === choice.effort ? 'true' : 'false',
2170
- disabled: busy,
2171
- onClick: () => chooseEffort(choice.effort),
2172
- }, choice.label)))),
2173
- busy ? h('div', { className: 'mb-picker-status' }, translate('switching')) : null,
2174
- toast === null ? null : h('div', { className: 'mb-picker-toast', role: 'status', 'aria-live': 'polite' }, toast),
2175
- state?.status === 'error'
2176
- ? h('div', { className: 'mb-picker-error' }, state.error)
2177
- : null,
2178
- state?.status === 'error'
2179
- ? h('button', {
2180
- type: 'button',
2181
- className: 'mb-btn mb-picker-retry',
2182
- onClick: () => {
2183
- directory.load().catch(() => {})
2184
- },
2185
- }, translate('pickerRetry'))
2186
- : null)
2187
-
2188
- return h('div', {
2189
- className: 'mb-picker',
2190
- ref: rootRef,
2191
- },
2192
- h(Style),
2193
- h('button', {
2194
- ref: triggerRef,
2195
- type: 'button',
2196
- className: 'mb-picker-trigger',
2197
- disabled: locked,
2198
- 'aria-label': triggerAria,
2199
- 'aria-haspopup': 'menu',
2200
- 'aria-controls': open ? `${id}-menu` : undefined,
2201
- 'aria-expanded': open ? 'true' : 'false',
2202
- 'aria-busy': busy ? 'true' : undefined,
2203
- title: triggerAria,
2204
- onClick: toggle,
2205
- },
2206
- h('svg', {
2207
- viewBox: '0 0 16 16',
2208
- width: 14,
2209
- height: 14,
2210
- fill: 'none',
2211
- stroke: 'currentColor',
2212
- strokeWidth: 1.3,
2213
- 'aria-hidden': true,
2214
- className: 'mb-picker-icon',
2215
- },
2216
- h('rect', { x: 2, y: 3, width: 12, height: 10, rx: 2 }),
2217
- h('path', { d: 'M2 6.5h12M6 6.5V13' })),
2218
- h('span', { className: 'mb-picker-name' }, triggerModel),
2219
- effortLabel === undefined ? null : h('span', { className: 'mb-picker-trigger-effort' }, effortLabel),
2220
- h('svg', {
2221
- className: open ? 'mb-picker-chevron mb-picker-chevron-open' : 'mb-picker-chevron',
2222
- viewBox: '0 0 16 16',
2223
- width: 14,
2224
- height: 14,
2225
- fill: 'none',
2226
- stroke: 'currentColor',
2227
- strokeWidth: 1.5,
2228
- 'aria-hidden': true,
2229
- },
2230
- h('path', { d: 'M4 6l4 4 4-4' }))),
2231
- open ? ReactDOM.createPortal(menu, document.body) : null)
2232
- }
2233
-
2234
- function ModelPickerSeat(props) {
2235
- const catalogSnap = useCatalog()
2236
- // Take over only while we hold a usable catalog. On a fetch error or an
2237
- // in-flight load, return null so the built-in picker keeps working
2238
- // instead of leaving the user with no way to change models.
2239
- if (catalogSnap.status !== 'ready') return null
2240
- const display = readDisplay(catalogSnap.data)
2241
- if (!display.takeoverPicker) return null
2242
- return h(OrderedModelPicker, props)
2243
- }
2244
-
2245
- return {
2246
- inject: ['slots', 'locale'],
2247
- apply(ctx) {
2248
- localeService = ctx.get('locale')
2249
- publishModelDirectories(ctx.get('modelDirectories'))
2250
- ctx.inject(['modelDirectories'], (scope) => {
2251
- publishModelDirectories(scope.modelDirectories)
2252
- let seat = null
2253
- const takeoverEnabled = () => catalog.snap.status === 'ready' && readDisplay(catalog.snap.data).takeoverPicker !== false
2254
- const syncSeat = () => {
2255
- if (takeoverEnabled()) {
2256
- if (seat === null) {
2257
- seat = scope.slots.register({
2258
- name: 'conversation.input.model',
2259
- priority: -1,
2260
- }, ModelPickerSeat)
2261
- }
2262
- return
2263
- }
2264
- if (seat !== null) {
2265
- seat()
2266
- seat = null
2267
- }
2268
- }
2269
- scope.slots.inject('conversation.input.model', () => {
2270
- const unsubscribe = subscribeCatalog(syncSeat)
2271
- syncSeat()
2272
- return () => {
2273
- unsubscribe()
2274
- if (seat !== null) {
2275
- seat()
2276
- seat = null
2277
- }
2278
- }
2279
- })
2280
- })
2281
-
2282
- void ensureCatalog()
2283
-
2284
- ctx.slots.inject('settings.section', () => ctx.slots.register({
2285
- name: 'settings.section',
2286
- id: 'model-billing',
2287
- // Native sections run -10..20 (account, general, models, plugins,
2288
- // agent presets); third-party pages start at 25 so they sit below
2289
- // the native group instead of splitting it.
2290
- order: 25,
2291
- label: () => t('nav'),
2292
- }, SettingsSection))
2293
-
2294
- ctx.slots.inject('conversation.composer.dock', () => ctx.slots.register({
2295
- name: 'conversation.composer.dock',
2296
- id: 'model-billing-cost',
2297
- order: 2,
2298
- }, CostDock))
2299
-
2300
- ctx.slots.inject('conversation.chat.turnTail', () => ctx.slots.register({
2301
- name: 'conversation.chat.turnTail',
2302
- id: 'model-billing-turn',
2303
- order: 30,
2304
- }, TurnCost))
2305
- },
2306
- }
2307
- },
2308
- })
1
+ window.__ModuleLoader__.load({
2
+ id: 'dsh-model-billing',
3
+ factory(require) {
4
+ const React = require('react')
5
+ const ReactDOM = require('react-dom')
6
+ const h = React.createElement
7
+ const {
8
+ Fragment,
9
+ useEffect,
10
+ useId,
11
+ useLayoutEffect,
12
+ useMemo,
13
+ useRef,
14
+ useState,
15
+ useSyncExternalStore,
16
+ } = React
17
+
18
+ // ---------------------------------------------------------------------
19
+ // Locale
20
+ // ---------------------------------------------------------------------
21
+
22
+ const STRINGS = {
23
+ zh: {
24
+ nav: '模型与费用',
25
+ title: '模型与费用',
26
+ intro: '调整已配置模型的顺序,并填写每百万 tokens 的价格。金额根据会话日志估算所有模型;DeepSeek 使用内置峰谷价。',
27
+ loading: '正在加载模型目录…',
28
+ loadFailed: '模型目录加载失败',
29
+ retry: '重试',
30
+ modelsCount: '{count} 个模型',
31
+ save: '保存',
32
+ saving: '保存中…',
33
+ saved: '已保存',
34
+ unsaved: '有未保存的修改',
35
+ undo: '撤销修改',
36
+ orderHint: '用 ↑ / ↓ 调整顺序;顺序直接决定对话框右下角模型选择器中的排列,也用于费用估算。',
37
+ noModels: '当前没有可用的已配置模型。',
38
+ up: '上移',
39
+ down: '下移',
40
+ cacheHitPrice: '输入 · 缓存命中',
41
+ cacheMissPrice: '输入 · 缓存未命中',
42
+ outputPrice: '输出',
43
+ perMillion: '元 / 百万 tokens',
44
+ deepseekTag: 'DeepSeek',
45
+ deepseekNote: '默认使用官方峰谷价;填写完整的自定义价格后,将优先按自定义价格估算。',
46
+ officialPrice: '官方定价',
47
+ priceSourceOfficial: '官方价',
48
+ priceSourceCustom: '自定义',
49
+ customizePrice: '自定义价格',
50
+ useOfficialPrice: '恢复官方价',
51
+ customPriceNote: '自定义价格优先于官方峰谷价;三项留空并保存即可恢复官方价。',
52
+ peak: '高峰',
53
+ offPeak: '空闲',
54
+ deepseekSchedule: '高峰为北京时间周一至周五 9:00–12:00、14:00–18:00(法定节假日除外);其余时段为空闲。',
55
+ pricePlaceholder: '未设置',
56
+ priceUnset: '未定价',
57
+ priceHitShort: '命中',
58
+ priceMissShort: '未命中',
59
+ priceOutShort: '输出',
60
+ priceUnit: '元/M',
61
+ invalidPrice: '价格必须是非负数字,或留空表示未设置。',
62
+ saveFailed: '保存失败:{message}',
63
+ formula: '估算公式:缓存命中输入 × 命中价 +(未缓存输入 + 缓存写入)× 未命中价 + 输出 × 输出价。',
64
+ formulaNote: '价格按每百万 tokens 计。缓存写入按缓存未命中价计算;未配置价格的模型不产生金额。',
65
+ singlePrice: '价格',
66
+ settingsOpen: '在设置中编辑',
67
+ costEstimate: '费用估算',
68
+ costDetails: '输入框下方费用估算',
69
+ close: '关闭',
70
+ thisTurn: '本轮',
71
+ sessionTotal: '本会话',
72
+ cacheHit: '缓存命中率',
73
+ tokens: '输入 / 输出',
74
+ turn: '轮次',
75
+ content: '内容',
76
+ time: '时间',
77
+ model: '使用模型',
78
+ cost: '费用',
79
+ byModel: '按模型',
80
+ excludedShort: '不计入',
81
+ partial: '部分未定价',
82
+ partialShort: '部分未定价',
83
+ partialNote: '有非 DeepSeek 调用尚未配置完整价格,合计只包含已定价部分。',
84
+ showUnpricedNotice: '显示未定价提示',
85
+ unpricedRoutes: '未定价路由',
86
+ addPrice: '补充价格',
87
+ priceSaved: '价格已保存',
88
+ archiveSection: '历史价格',
89
+ archiveHint: '已移除模型的调用仍按这里保存的价格计价,不需要把它们加回模型列表。',
90
+ archiveEmpty: '暂无历史价格',
91
+ addArchive: '添加历史价格',
92
+ archiveProvider: '提供商',
93
+ archiveModel: '模型',
94
+ archiveConflict: '该路由仍在当前模型列表中,请直接编辑上方的模型价格。',
95
+ invalidRoute: '提供商和模型都不能为空。',
96
+ remove: '删除',
97
+ estimateNote: '金额由会话日志中的 token 用量和缓存命中估算;DeepSeek 按事件时间使用内置峰谷价,实际账单以提供方为准。',
98
+ webToolNote: '含 DeepSeek Search/Fetch 按次费 ≈ {amount}(搜索 {searchBilled}/{searchCalls} 次,抓取 {fetchBilled}/{fetchCalls} 次)。',
99
+ listSep: '、',
100
+ turnCost: '费用',
101
+ turnTokens: '本轮 Token',
102
+ displaySection: '轮尾显示',
103
+ displayFields: '显示项目',
104
+ displayLook: '外观',
105
+ modelsSection: '模型顺序与单价',
106
+ modelsHint: '↑ / ↓ 决定选择器里的排列;单价按每百万 tokens 填写,三项留空即恢复官方价。',
107
+ displayTurnTail: '启用轮尾显示',
108
+ displayDock: '显示输入框下方费用估算',
109
+ dockSection: '输入框下方费用估算',
110
+ dockHint: '控制输入框下方的费用条与费用估算弹层。',
111
+ displayHint: '控制每轮结束后在对话尾部显示的内容;修改后点“保存”生效。',
112
+ displayModels: '使用模型',
113
+ displayProvider: '模型提供商',
114
+ displayCost: '本轮费用',
115
+ displayTokens: 'Token 用量',
116
+ displayCacheHit: '缓存命中率',
117
+ displayLabels: '显示文字标签',
118
+ displayFontSize: '字号',
119
+ displayDensity: '间距',
120
+ displayWeight: '字重',
121
+ fontSizeSmall: '小',
122
+ fontSizeNormal: '标准',
123
+ fontSizeLarge: '大',
124
+ densityCompact: '紧凑',
125
+ densityComfortable: '舒适',
126
+ weightNormal: '常规',
127
+ weightBold: '加粗',
128
+ pickerSection: '模型选择器',
129
+ takeoverPicker: '接管模型选择器',
130
+ takeoverHint: '开启后使用本插件按自定义顺序排列的模型选择器;关闭后恢复 DSH 内置选择器。',
131
+ chooseModel: '选择模型',
132
+ loadingModels: '加载模型…',
133
+ searchModel: '搜索模型',
134
+ noMatch: '没有匹配的模型',
135
+ pickerEmpty: '暂无可用模型',
136
+ reasoningEffort: '推理强度',
137
+ providerDefault: '提供方默认',
138
+ switching: '切换中…',
139
+ pickerRetry: '重新加载',
140
+ pickerAria: '选择模型与推理强度',
141
+ pickerUnavailable: '模型目录尚未就绪,请刷新页面',
142
+ },
143
+ en: {
144
+ nav: 'Models & Cost',
145
+ title: 'Models & Cost',
146
+ intro: 'Reorder configured models and enter prices per million tokens. Amounts estimate every model from the session log; DeepSeek uses its built-in peak/off-peak prices.',
147
+ loading: 'Loading the model catalog…',
148
+ loadFailed: 'Could not load the model catalog',
149
+ retry: 'Retry',
150
+ modelsCount: '{count} models',
151
+ save: 'Save',
152
+ saving: 'Saving…',
153
+ saved: 'Saved',
154
+ unsaved: 'Unsaved changes',
155
+ undo: 'Discard changes',
156
+ orderHint: 'Use ↑ / ↓ to reorder. This order is used directly by the model picker at the bottom-right of the composer, and by the cost details.',
157
+ noModels: 'No configured models are available.',
158
+ up: 'Move up',
159
+ down: 'Move down',
160
+ cacheHitPrice: 'Input · cache hit',
161
+ cacheMissPrice: 'Input · cache miss',
162
+ outputPrice: 'Output',
163
+ perMillion: 'CNY / million tokens',
164
+ deepseekTag: 'DeepSeek',
165
+ deepseekNote: 'Official peak/off-peak prices are used by default; a complete custom price set takes precedence.',
166
+ officialPrice: 'Official prices',
167
+ priceSourceOfficial: 'Official',
168
+ priceSourceCustom: 'Custom',
169
+ customizePrice: 'Set custom prices',
170
+ useOfficialPrice: 'Restore official prices',
171
+ customPriceNote: 'Custom prices override the official peak/off-peak table. Leave all three blank and save to restore official prices.',
172
+ peak: 'Peak',
173
+ offPeak: 'Off-peak',
174
+ deepseekSchedule: 'Peak: Beijing time Mon–Fri 09:00–12:00 and 14:00–18:00, excluding Chinese statutory holidays; all other times are off-peak.',
175
+ pricePlaceholder: 'Not set',
176
+ priceUnset: 'Not priced',
177
+ priceHitShort: 'hit',
178
+ priceMissShort: 'miss',
179
+ priceOutShort: 'out',
180
+ priceUnit: 'CNY/M',
181
+ invalidPrice: 'Prices must be non-negative numbers, or blank when unset.',
182
+ saveFailed: 'Save failed: {message}',
183
+ formula: 'Estimate: cached input × hit price + (uncached input + cache write) × miss price + output × output price.',
184
+ formulaNote: 'Prices are per million tokens. Cache writes use the cache-miss price; models without a full price set produce no amount.',
185
+ singlePrice: 'Prices',
186
+ settingsOpen: 'Edit in Settings',
187
+ costEstimate: 'Cost estimate',
188
+ costDetails: 'Composer cost estimate',
189
+ close: 'Close',
190
+ thisTurn: 'Turn',
191
+ sessionTotal: 'Session',
192
+ cacheHit: 'Cache hit rate',
193
+ tokens: 'Input / output',
194
+ turn: 'Turn',
195
+ content: 'Content',
196
+ time: 'Time',
197
+ model: 'Model',
198
+ cost: 'Cost',
199
+ byModel: 'By model',
200
+ excludedShort: 'Excluded',
201
+ partial: 'partly unpriced',
202
+ partialShort: 'partly unpriced',
203
+ partialNote: 'Some non-DeepSeek calls do not have a complete price set; the total includes only priced calls.',
204
+ showUnpricedNotice: 'Show unpriced notices',
205
+ unpricedRoutes: 'Unpriced routes',
206
+ addPrice: 'Add price',
207
+ priceSaved: 'Price saved',
208
+ archiveSection: 'Historical prices',
209
+ archiveHint: 'Calls to removed models stay priced from this book; you do not need to add them back to the model list.',
210
+ archiveEmpty: 'No historical prices yet',
211
+ addArchive: 'Add historical price',
212
+ archiveProvider: 'Provider',
213
+ archiveModel: 'Model',
214
+ archiveConflict: 'This route is still in the configured list; edit its price above instead.',
215
+ invalidRoute: 'Provider and model are both required.',
216
+ remove: 'Remove',
217
+ estimateNote: 'Amounts are estimated from token usage and cache hits in the session log; DeepSeek uses built-in peak/off-peak prices at the event time, and the provider bill is authoritative.',
218
+ webToolNote: 'Includes DeepSeek Search/Fetch per-request fees ≈ {amount} (search {searchBilled}/{searchCalls}, fetch {fetchBilled}/{fetchCalls}).',
219
+ listSep: ', ',
220
+ turnCost: 'Cost',
221
+ turnTokens: 'Turn tokens',
222
+ displaySection: 'Turn-tail display',
223
+ displayFields: 'Shown items',
224
+ displayLook: 'Appearance',
225
+ modelsSection: 'Model order and prices',
226
+ modelsHint: 'The arrows set the picker order; prices are per million tokens, and leaving all three blank restores the official table.',
227
+ displayTurnTail: 'Show the turn tail',
228
+ displayDock: 'Show the composer cost estimate',
229
+ dockSection: 'Composer cost estimate',
230
+ dockHint: 'Controls the cost bar below the composer and its detail panel.',
231
+ displayHint: 'Choose what appears below each completed turn; click Save to apply.',
232
+ displayModels: 'Models used',
233
+ displayProvider: 'Model provider',
234
+ displayCost: 'Turn cost',
235
+ displayTokens: 'Token usage',
236
+ displayCacheHit: 'Cache hit rate',
237
+ displayLabels: 'Show text labels',
238
+ displayFontSize: 'Font size',
239
+ displayDensity: 'Spacing',
240
+ displayWeight: 'Font weight',
241
+ fontSizeSmall: 'Small',
242
+ fontSizeNormal: 'Normal',
243
+ fontSizeLarge: 'Large',
244
+ densityCompact: 'Compact',
245
+ densityComfortable: 'Comfortable',
246
+ weightNormal: 'Regular',
247
+ weightBold: 'Bold',
248
+ pickerSection: 'Model picker',
249
+ takeoverPicker: 'Take over the model picker',
250
+ takeoverHint: 'On: use this plugin’s picker with your custom model order. Off: restore the built-in DSH picker.',
251
+ chooseModel: 'Select model',
252
+ loadingModels: 'Loading models…',
253
+ searchModel: 'Search models',
254
+ noMatch: 'No matching model',
255
+ pickerEmpty: 'No model available',
256
+ reasoningEffort: 'Reasoning effort',
257
+ providerDefault: 'Provider default',
258
+ switching: 'Switching…',
259
+ pickerRetry: 'Reload',
260
+ pickerAria: 'Select model and reasoning effort',
261
+ pickerUnavailable: 'The model directory is not ready; reload the page',
262
+ },
263
+ }
264
+
265
+ const t = (key, values) => {
266
+ const active = localeService?.getSnapshot?.().active ?? 'zh'
267
+ const dict = active.toLowerCase().startsWith('en') ? STRINGS.en : STRINGS.zh
268
+ const template = dict[key] ?? STRINGS.zh[key] ?? key
269
+ return values === undefined
270
+ ? template
271
+ : template.replace(/\{(\w+)\}/g, (match, name) => values[name] === undefined ? match : String(values[name]))
272
+ }
273
+
274
+ let localeService
275
+ let modelDirectories
276
+ let modelDirectoryRevision = 0
277
+ const modelDirectoryListeners = new Set()
278
+
279
+ function publishModelDirectories(next) {
280
+ if (next === undefined || next === modelDirectories) return
281
+ modelDirectories = next
282
+ modelDirectoryRevision += 1
283
+ for (const listener of [...modelDirectoryListeners]) listener()
284
+ }
285
+
286
+ function subscribeModelDirectories(listener) {
287
+ modelDirectoryListeners.add(listener)
288
+ return () => modelDirectoryListeners.delete(listener)
289
+ }
290
+
291
+ function useModelDirectoriesRevision() {
292
+ return useSyncExternalStore(
293
+ subscribeModelDirectories,
294
+ () => modelDirectoryRevision,
295
+ () => 0,
296
+ )
297
+ }
298
+
299
+ function subscribeLocale(listener) {
300
+ return localeService?.subscribe?.(listener) ?? (() => {})
301
+ }
302
+
303
+ function localeRevision() {
304
+ return localeService?.getSnapshot?.().revision ?? 0
305
+ }
306
+
307
+ function useT() {
308
+ useSyncExternalStore(subscribeLocale, localeRevision, () => 0)
309
+ return t
310
+ }
311
+
312
+ const DISPLAY_DEFAULTS = Object.freeze({
313
+ showTurnTail: true,
314
+ showDock: true,
315
+ showModels: true,
316
+ showProvider: false,
317
+ showCost: true,
318
+ showTokens: false,
319
+ showCacheHit: false,
320
+ showLabels: true,
321
+ showUnpricedNotice: true,
322
+ takeoverPicker: true,
323
+ fontSize: 'normal',
324
+ density: 'comfortable',
325
+ fontWeight: 'normal',
326
+ })
327
+
328
+ const DISPLAY_STORAGE_KEY = 'dsh-model-billing.display'
329
+
330
+ function clearStoredDisplay() {
331
+ try {
332
+ localStorage.removeItem(DISPLAY_STORAGE_KEY)
333
+ } catch {
334
+ // Ignore unavailable storage.
335
+ }
336
+ }
337
+
338
+ // Older builds mirrored display settings into localStorage, which made the
339
+ // browser a second source of truth that could never sync across devices.
340
+ // Clear the legacy key once; the Host state is now the only copy.
341
+ clearStoredDisplay()
342
+
343
+ function readDisplay(data) {
344
+ const source = data?.display
345
+ return source !== null && typeof source === 'object' && !Array.isArray(source)
346
+ ? { ...DISPLAY_DEFAULTS, ...source }
347
+ : { ...DISPLAY_DEFAULTS }
348
+ }
349
+
350
+ // ---------------------------------------------------------------------
351
+ // Shared catalog state
352
+ // ---------------------------------------------------------------------
353
+
354
+ const catalog = {
355
+ snap: { status: 'idle', data: null, error: null },
356
+ listeners: new Set(),
357
+ inflight: null,
358
+ generation: 0,
359
+ }
360
+
361
+ const REQUEST_TIMEOUT_MS = 8000
362
+
363
+ async function requestJson(url, options) {
364
+ const controller = typeof AbortController === 'function' ? new AbortController() : null
365
+ const timeout = controller === null
366
+ ? null
367
+ : setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS)
368
+ let response
369
+ try {
370
+ response = await fetch(url, {
371
+ credentials: 'same-origin',
372
+ cache: 'no-store',
373
+ ...options,
374
+ signal: options?.signal ?? controller?.signal ?? null,
375
+ headers: {
376
+ accept: 'application/json',
377
+ ...(options?.headers ?? {}),
378
+ },
379
+ })
380
+ } catch (error) {
381
+ if (timeout !== null) clearTimeout(timeout)
382
+ throw controller !== null && error?.name === 'AbortError'
383
+ ? new Error(`timeout after ${REQUEST_TIMEOUT_MS}ms`)
384
+ : error
385
+ }
386
+ if (timeout !== null) clearTimeout(timeout)
387
+ let body
388
+ try {
389
+ body = await response.json()
390
+ } catch {
391
+ throw new Error(`HTTP ${response.status}`)
392
+ }
393
+ if (!response.ok || body?.ok === false) {
394
+ throw new Error(body?.message ?? `HTTP ${response.status}`)
395
+ }
396
+ return body
397
+ }
398
+
399
+ function emitCatalog() {
400
+ for (const listener of [...catalog.listeners]) listener()
401
+ }
402
+
403
+ function setCatalog(draft) {
404
+ catalog.snap = { ...catalog.snap, ...draft }
405
+ emitCatalog()
406
+ }
407
+
408
+ async function ensureCatalog(force = false) {
409
+ if (!force && catalog.snap.status === 'ready') return catalog.snap.data
410
+ if (catalog.inflight !== null && !force) return catalog.inflight
411
+ catalog.generation += 1
412
+ const generation = catalog.generation
413
+ setCatalog({ status: 'loading', error: null })
414
+ const operation = requestJson('/model-billing/catalog')
415
+ .then((data) => {
416
+ if (generation !== catalog.generation) return data
417
+ setCatalog({ status: 'ready', data, error: null })
418
+ return data
419
+ })
420
+ .catch((error) => {
421
+ if (generation === catalog.generation) {
422
+ setCatalog({ status: 'error', error: error instanceof Error ? error.message : String(error) })
423
+ }
424
+ return null
425
+ })
426
+ .finally(() => {
427
+ if (catalog.inflight === operation) catalog.inflight = null
428
+ })
429
+ catalog.inflight = operation
430
+ return operation
431
+ }
432
+
433
+ function subscribeCatalog(listener) {
434
+ catalog.listeners.add(listener)
435
+ if (catalog.snap.status === 'idle') void ensureCatalog()
436
+ return () => catalog.listeners.delete(listener)
437
+ }
438
+
439
+ function catalogSnapshot() {
440
+ return catalog.snap
441
+ }
442
+
443
+ function useCatalog() {
444
+ return useSyncExternalStore(subscribeCatalog, catalogSnapshot, catalogSnapshot)
445
+ }
446
+
447
+ // ---------------------------------------------------------------------
448
+ // Shared session usage state
449
+ // ---------------------------------------------------------------------
450
+
451
+ const usageSlots = new Map()
452
+ const USAGE_DEBOUNCE_MS = 300
453
+
454
+ function usageSlot(sessionId) {
455
+ let slot = usageSlots.get(sessionId)
456
+ if (slot === undefined) {
457
+ slot = {
458
+ snap: { status: 'idle', data: null, error: null },
459
+ listeners: new Set(),
460
+ inflight: null,
461
+ generation: 0,
462
+ revision: null,
463
+ pending: null,
464
+ refs: 0,
465
+ rowsByTurn: new Map(),
466
+ }
467
+ usageSlots.set(sessionId, slot)
468
+ }
469
+ slot.refs += 1
470
+ return slot
471
+ }
472
+
473
+ function releaseSlot(sessionId, slot) {
474
+ slot.refs = Math.max(0, slot.refs - 1)
475
+ if (slot.listeners.size > 0 || slot.refs > 0) return
476
+ if (slot.pending !== null) {
477
+ clearTimeout(slot.pending)
478
+ slot.pending = null
479
+ }
480
+ usageSlots.delete(sessionId)
481
+ }
482
+
483
+ function emitUsage(slot) {
484
+ for (const listener of [...slot.listeners]) listener()
485
+ }
486
+
487
+ function ensureUsage(sessionId, revision, force = false) {
488
+ const slot = usageSlot(sessionId)
489
+ if (!force && slot.revision === revision && slot.snap.status !== 'error') return
490
+ if (slot.inflight !== null && !force) return
491
+ slot.revision = revision
492
+ if (slot.pending !== null) clearTimeout(slot.pending)
493
+ // Collapse the token-counter updates a streaming reply produces into one
494
+ // trailing request instead of one request per token.
495
+ slot.pending = setTimeout(() => {
496
+ slot.pending = null
497
+ void fetchUsage(sessionId, revision)
498
+ }, USAGE_DEBOUNCE_MS)
499
+ }
500
+
501
+ function fetchUsage(sessionId, revision) {
502
+ const slot = usageSlots.get(sessionId)
503
+ if (slot === undefined) return
504
+ slot.generation += 1
505
+ const generation = slot.generation
506
+ slot.snap = { ...slot.snap, status: 'loading', error: null }
507
+ emitUsage(slot)
508
+ const operation = requestJson(`/model-billing/usage?sessionId=${encodeURIComponent(sessionId)}`)
509
+ .then((data) => {
510
+ if (generation !== slot.generation) return
511
+ // Index once per fetch rather than scanning the row list on every
512
+ // turn render.
513
+ slot.snap = { status: 'ready', data, rowsByTurn: indexRowsByTurn(data), error: null }
514
+ slot.rowsByTurn = slot.snap.rowsByTurn
515
+ emitUsage(slot)
516
+ })
517
+ .catch((error) => {
518
+ if (generation !== slot.generation) return
519
+ slot.snap = {
520
+ status: 'error',
521
+ data: slot.snap.data,
522
+ rowsByTurn: slot.snap.rowsByTurn,
523
+ error: error instanceof Error ? error.message : String(error),
524
+ }
525
+ emitUsage(slot)
526
+ })
527
+ .finally(() => {
528
+ if (slot.inflight === operation) slot.inflight = null
529
+ })
530
+ slot.inflight = operation
531
+ return operation
532
+ }
533
+
534
+ function indexRowsByTurn(data) {
535
+ const index = new Map()
536
+ if (Array.isArray(data?.usage?.turns)) {
537
+ for (const row of data.usage.turns) index.set(row.turn, row)
538
+ }
539
+ return index
540
+ }
541
+
542
+ function useSessionUsage(sessionId, revision) {
543
+ const slot = usageSlot(sessionId)
544
+ useEffect(() => () => releaseSlot(sessionId, slot), [sessionId, slot])
545
+ const snap = useSyncExternalStore(
546
+ (listener) => {
547
+ slot.listeners.add(listener)
548
+ return () => slot.listeners.delete(listener)
549
+ },
550
+ () => slot.snap,
551
+ () => slot.snap,
552
+ )
553
+ useEffect(() => {
554
+ if (sessionId !== undefined && revision !== null) ensureUsage(sessionId, revision)
555
+ }, [sessionId, revision])
556
+ return snap
557
+ }
558
+
559
+ function tokenUsageRevision(tokenUsage) {
560
+ if (tokenUsage === undefined || tokenUsage === null) return 'none'
561
+ return [
562
+ tokenUsage.uncachedInputTokens ?? '',
563
+ tokenUsage.outputTokens ?? '',
564
+ tokenUsage.cacheReadTokens ?? '',
565
+ tokenUsage.cacheWriteTokens ?? '',
566
+ ].join(':')
567
+ }
568
+
569
+ // ---------------------------------------------------------------------
570
+ // Formatting
571
+ // ---------------------------------------------------------------------
572
+
573
+ function formatMoney(value) {
574
+ if (value === null || value === undefined || !Number.isFinite(value)) return '—'
575
+ if (value === 0) return '¥0'
576
+ const fixed = value.toFixed(2)
577
+ return fixed === '0.00' ? '<¥0.01' : `¥${fixed}`
578
+ }
579
+
580
+ function formatTokens(value) {
581
+ if (!Number.isFinite(value)) return '—'
582
+ if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(2)}M`
583
+ if (value >= 1_000) return `${(value / 1_000).toFixed(1)}k`
584
+ return String(Math.round(value))
585
+ }
586
+
587
+ function formatPercent(value) {
588
+ return value === null || value === undefined ? '—' : `${(value * 100).toFixed(1)}%`
589
+ }
590
+
591
+ function formatPrice(value) {
592
+ if (value === null || value === undefined) return '—'
593
+ return String(Number(value.toFixed(6)))
594
+ }
595
+
596
+ function PriceLines({ pricing, translate, showSource = true }) {
597
+ const source = showSource === false ? null : pricing?.source === 'official'
598
+ ? translate('priceSourceOfficial')
599
+ : pricing?.source === 'user' ? translate('priceSourceCustom') : null
600
+ const sourceLine = source === null ? null : h('div', { className: 'mb-picker-price-source' }, source)
601
+ if (pricing?.builtin === true) {
602
+ const line = (label, prices) =>
603
+ `${label} ${translate('priceHitShort')} ${formatPrice(prices?.cacheHit)} · ${translate('priceMissShort')} ${formatPrice(prices?.cacheMiss)} · ${translate('priceOutShort')} ${formatPrice(prices?.output)} ${translate('priceUnit')}`
604
+ return h('div', { className: 'mb-picker-prices' },
605
+ sourceLine,
606
+ h('div', { className: 'mb-picker-price-line' }, line(translate('peak'), pricing.peak)),
607
+ h('div', { className: 'mb-picker-price-line' }, line(translate('offPeak'), pricing.offPeak)))
608
+ }
609
+ const hasAny = pricing !== null && pricing !== undefined
610
+ && [pricing.cacheHit, pricing.cacheMiss, pricing.output].some((value) => typeof value === 'number')
611
+ if (!hasAny) {
612
+ return h('div', { className: 'mb-picker-prices' }, translate('priceUnset'))
613
+ }
614
+ return h('div', { className: 'mb-picker-prices' },
615
+ sourceLine,
616
+ h('div', { className: 'mb-picker-price-line' },
617
+ `${translate('priceHitShort')} ${formatPrice(pricing.cacheHit)} · ${translate('priceMissShort')} ${formatPrice(pricing.cacheMiss)} · ${translate('priceOutShort')} ${formatPrice(pricing.output)} ${translate('priceUnit')}`))
618
+ }
619
+
620
+ function timeText(timestamp) {
621
+ if (!Number.isFinite(timestamp) || timestamp <= 0) return ''
622
+ try {
623
+ return new Date(timestamp).toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
624
+ } catch {
625
+ return ''
626
+ }
627
+ }
628
+
629
+ // ---------------------------------------------------------------------
630
+ // Styles
631
+ // ---------------------------------------------------------------------
632
+
633
+ const CSS = `
634
+ .mb-page { max-width: 780px; padding: 6px 2px 48px; color: var(--dsw-alias-label-primary, #e8e8ea); font: 13px/1.5 var(--ds-font-family, system-ui, -apple-system, "Segoe UI", sans-serif); }
635
+ .mb-page * { box-sizing: border-box; }
636
+ .mb-hero { display: flex; align-items: flex-start; gap: 14px; margin: 0 0 16px; }
637
+ .mb-mark { flex: none; display: grid; place-items: center; width: 42px; height: 42px; border-radius: 11px; overflow: hidden; line-height: 0; }
638
+ .mb-mark svg { width: 42px; height: 42px; display: block; }
639
+ .mb-hero h2 { margin: 0 0 4px; font-size: 20px; line-height: 1.25; font-weight: 650; }
640
+ .mb-hero p { margin: 0; color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 12.5px; }
641
+ .mb-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin: 0 0 2px; }
642
+ .mb-bar-count { color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 12px; }
643
+ .mb-muted { color: var(--dsw-alias-label-secondary, #a5a5ad); }
644
+ .mb-spacer { flex: 1; }
645
+ .mb-btn { appearance: none; height: 30px; padding: 0 11px; border: 1px solid var(--dsw-alias-border-l2, #3a3a3d); border-radius: 7px; background: var(--dsw-alias-bg-layer-1, #202024); color: var(--dsw-alias-label-primary, #e8e8ea); font: inherit; font-size: 12px; cursor: pointer; }
646
+ .mb-btn:hover:not(:disabled) { background: var(--dsw-alias-bg-layer-2, #26262a); }
647
+ .mb-btn:disabled { opacity: 1; color: var(--dsw-alias-label-secondary, #a5a5ad); border-color: var(--dsw-alias-border-l1, rgba(255, 255, 255, .1)); background: var(--dsw-alias-bg-layer-1, #202024); cursor: default; }
648
+ .mb-btn-primary { background: var(--dsw-alias-brand-primary, #3b82f6); border-color: var(--dsw-alias-brand-primary, #3b82f6); color: var(--dsw-alias-bg-base, #14141a); font-weight: 600; }
649
+ .mb-btn-primary:hover:not(:disabled) { opacity: .9; }
650
+ .mb-btn-primary:disabled { background: var(--dsw-alias-bg-layer-2, #26262a); border-color: var(--dsw-alias-border-l2, #3a3a3d); color: var(--dsw-alias-label-secondary, #a5a5ad); }
651
+ .mb-btn-icon { width: 28px; height: 28px; padding: 0; font-size: 13px; }
652
+ .mb-status { min-height: 18px; margin: 2px 2px 0; color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 12px; }
653
+ .mb-status[data-tone="error"] { color: var(--dsw-alias-state-error-primary, #f85149); }
654
+ .mb-status[data-tone="success"] { color: var(--dsw-alias-state-success-primary, #3fb950); }
655
+ .mb-group { margin-top: 16px; border: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, .1)); border-radius: 12px; background: var(--dsw-alias-bg-layer-1, #202024); overflow: hidden; }
656
+ .mb-group-head { padding: 13px 16px 11px; border-bottom: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, .08)); }
657
+ .mb-group-head h3 { margin: 0; font-size: 13.5px; font-weight: 600; }
658
+ .mb-group-head p { margin: 3px 0 0; color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 12px; }
659
+ .mb-group-body { padding: 0 16px; }
660
+ .mb-row { display: flex; align-items: center; gap: 18px; min-height: 52px; padding: 10px 0; border-top: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, .07)); }
661
+ .mb-row:first-child { border-top: 0; }
662
+ .mb-row.is-disabled { opacity: .48; }
663
+ .mb-row-copy { flex: 1; min-width: 0; }
664
+ .mb-row-label { font-size: 12.8px; font-weight: 500; }
665
+ .mb-row-desc { margin-top: 2px; color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 11.5px; }
666
+ .mb-row-control { flex: none; display: flex; align-items: center; gap: 6px; }
667
+ .mb-subhead { padding: 12px 0 2px; color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 11px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; border-top: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, .07)); }
668
+ .mb-subhead:first-child { border-top: 0; }
669
+ .mb-switch { position: relative; width: 38px; height: 22px; padding: 0; border: 1px solid var(--dsw-alias-border-l2, #3a3a3d); border-radius: 999px; background: var(--dsw-alias-bg-layer-2, #26262a); cursor: pointer; transition: background .14s ease, border-color .14s ease; }
670
+ .mb-switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .3); transition: transform .14s ease; }
671
+ .mb-switch[aria-checked="true"] { border-color: transparent; background: var(--dsw-alias-brand-primary, #3b82f6); }
672
+ .mb-switch[aria-checked="true"]::after { transform: translateX(16px); background: var(--dsw-alias-bg-base, #14141a); }
673
+ .mb-switch:disabled { opacity: .38; cursor: not-allowed; }
674
+ .mb-switch:focus-visible, .mb-btn:focus-visible, .mb-select:focus-visible, .mb-input:focus-visible { outline: 2px solid var(--dsw-alias-brand-primary, #3b82f6); outline-offset: 2px; }
675
+ .mb-select { max-width: 230px; height: 28px; padding: 0 28px 0 9px; border: 1px solid var(--dsw-alias-border-l2, #3a3a3d); border-radius: 7px; background: var(--dsw-alias-bg-layer-2, #26262a); color: var(--dsw-alias-label-primary, #e8e8ea); font: inherit; font-size: 12px; cursor: pointer; }
676
+ .mb-select:disabled { cursor: not-allowed; opacity: .5; }
677
+ .mb-model { padding: 12px 0; border-top: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, .07)); }
678
+ .mb-model:first-child { border-top: 0; }
679
+ .mb-model-head { display: flex; align-items: flex-start; gap: 10px; }
680
+ .mb-index { flex: none; display: grid; place-items: center; width: 22px; height: 22px; margin-top: 1px; border-radius: 6px; background: var(--dsw-alias-bg-layer-2, #26262a); color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 11px; font-variant-numeric: tabular-nums; }
681
+ .mb-name { display: flex; align-items: center; gap: 6px; min-width: 0; font-size: 12.8px; font-weight: 500; }
682
+ .mb-name-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
683
+ .mb-sub { margin-top: 2px; color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 11.5px; overflow-wrap: anywhere; }
684
+ .mb-head-actions { margin-left: auto; display: flex; align-items: center; gap: 6px; flex: none; }
685
+ .mb-tag { display: inline-block; padding: 1px 6px; border-radius: 999px; background: var(--dsw-alias-bg-layer-2, #26262a); color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 10.5px; font-weight: 500; white-space: nowrap; }
686
+ .mb-tag-excluded { color: var(--dsw-alias-state-warn-primary); }
687
+ .mb-model-body { margin: 10px 0 0 32px; display: grid; gap: 10px; }
688
+ .mb-price-group { border: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, .08)); border-radius: 9px; padding: 10px 12px 12px; }
689
+ .mb-price-group-title { display: flex; gap: 8px; align-items: baseline; margin-bottom: 8px; font-size: 12.5px; font-weight: 600; }
690
+ .mb-price-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
691
+ .mb-card-actions { margin-top: 10px; display: flex; justify-content: flex-end; }
692
+ .mb-field { display: grid; gap: 4px; min-width: 0; }
693
+ .mb-field-label { color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 11px; }
694
+ .mb-input { width: 100%; height: 30px; border: 1px solid var(--dsw-alias-border-l2, #3a3a3d); border-radius: 7px; background: var(--dsw-alias-bg-layer-2, #26262a); color: var(--dsw-alias-label-primary, #e8e8ea); padding: 0 9px; font: inherit; font-size: 12px; font-variant-numeric: tabular-nums; }
695
+ .mb-input:focus { border-color: var(--dsw-alias-brand-primary, #3b82f6); }
696
+ .mb-note { margin: 8px 0 0; color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 12px; }
697
+ .mb-warn { margin: 6px 0 0; color: var(--dsw-alias-state-warn-primary, #d29922); font-size: 12px; }
698
+ .mb-empty { border: 1px dashed var(--dsw-alias-border-l2, #3a3a3d); border-radius: 10px; padding: 22px; text-align: center; color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 12px; }
699
+ .mb-footer-note { margin: 12px 2px 0; color: var(--dsw-alias-label-secondary, #a5a5ad); font-size: 11.5px; }
700
+ .mb-archive-row { padding: 11px 0; border-top: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, .07)); display: grid; gap: 8px; }
701
+ .mb-archive-row:first-child { border-top: 0; }
702
+ .mb-archive-head { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; gap: 8px; align-items: center; }
703
+ .mb-archive-actions { display: flex; gap: 8px; padding: 11px 0; border-top: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, .07)); }
704
+ .mb-route-editor { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 8px; padding: 10px 0 0; border-top: 1px solid var(--dsw-alias-border-l1, rgba(255, 255, 255, .07)); }
705
+ .mb-route-editor .mb-input { flex: 1 1 104px; width: auto; min-width: 84px; }
706
+ .mb-route-editor .mb-btn { flex: none; }
707
+ .mb-dock { align-self: center; display: flex; flex-direction: column; align-items: flex-start; color: var(--dsw-alias-label-tertiary); font-size: calc(var(--dsh-content-font-size-secondary, 13px) - 1px); line-height: calc(20px + var(--dsh-content-font-delta-secondary, 0px)); }
708
+ .mb-dock-trigger { appearance: none; border: 0; background: transparent; color: inherit; font: inherit; line-height: inherit; padding: 1px 8px; border-radius: var(--dsw-radius-sm); display: inline-flex; align-items: center; gap: 6px; cursor: pointer; text-align: left; max-width: 100%; flex-wrap: nowrap; }
709
+ .mb-dock-trigger:hover, .mb-dock-trigger[aria-expanded="true"] { background: var(--dsw-alias-interactive-bg-hover); color: var(--dsw-alias-label-secondary); }
710
+ .mb-dock-amount { color: var(--dsw-alias-label-secondary); font-weight: 500; font-variant-numeric: tabular-nums; }
711
+ .mb-dock-sep { opacity: .55; }
712
+ .mb-dock-icon { flex: none; width: 14px; height: 14px; }
713
+ .mb-dock-caret { font-size: 10px; opacity: .7; }
714
+ .mb-panel { box-sizing: border-box; border: 1px solid var(--dsw-alias-border-l1); background: var(--dsw-menu-surface-fill, var(--dsw-alias-bg-layer-1)); border-radius: var(--dsw-radius-lg, 10px); padding: 10px 12px; color: var(--dsw-alias-label-primary); }
715
+ .mb-panel-floating { position: fixed; z-index: 1200; max-width: calc(100vw - 24px); max-height: min(430px, calc(100vh - 96px)); overflow-x: hidden; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; scrollbar-color: var(--dsw-alias-scrollbar-bg-l2, var(--dsw-alias-border-l2)) transparent; box-shadow: var(--dsw-elevation-prominent); backdrop-filter: var(--dsw-menu-backdrop-filter, none); }
716
+ .mb-panel-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
717
+ .mb-panel-title { font-weight: 600; }
718
+ .mb-panel-close { margin-left: auto; width: 24px; height: 24px; border: 0; border-radius: var(--dsw-radius-sm); background: 0 0; color: var(--dsw-alias-label-secondary); font: inherit; font-size: 18px; line-height: 20px; cursor: pointer; }
719
+ .mb-panel-close:hover { background: var(--dsw-alias-interactive-bg-hover); color: var(--dsw-alias-label-primary); }
720
+ .mb-summary { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; margin-bottom: 10px; }
721
+ .mb-summary-item { background: var(--dsw-alias-bg-layer-2); border-radius: 7px; padding: 7px 9px; min-width: 0; }
722
+ .mb-summary-key { color: var(--dsw-alias-label-secondary); font-size: 11px; }
723
+ .mb-summary-value { font-variant-numeric: tabular-nums; font-weight: 600; overflow-wrap: anywhere; }
724
+ .mb-table { width: 100%; border-collapse: collapse; font-size: 12px; }
725
+ .mb-table th, .mb-table td { text-align: left; padding: 4px 5px; border-bottom: 1px solid var(--dsw-alias-border-l1); vertical-align: top; }
726
+ .mb-table th { color: var(--dsw-alias-label-secondary); font-weight: 500; font-size: 11px; }
727
+ .mb-table td.num, .mb-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
728
+ .mb-table tr:last-child td { border-bottom: 0; }
729
+ .mb-turn-table { table-layout: fixed; }
730
+ .mb-turn-table th, .mb-turn-table td, .mb-turn-table th.num, .mb-turn-table td.num { text-align: left; }
731
+ .mb-turn-table th:nth-child(1), .mb-turn-table td:nth-child(1) { width: 5%; }
732
+ .mb-turn-table th:nth-child(2), .mb-turn-table td:nth-child(2) { width: 25.5%; }
733
+ .mb-turn-table th:nth-child(3), .mb-turn-table td:nth-child(3) { width: 12.5%; }
734
+ .mb-turn-table th:nth-child(4), .mb-turn-table td:nth-child(4) { width: 21%; }
735
+ .mb-turn-table th:nth-child(5), .mb-turn-table td:nth-child(5) { width: 10%; }
736
+ .mb-turn-table th:nth-child(6), .mb-turn-table td:nth-child(6) { width: 16%; }
737
+ .mb-turn-table th:nth-child(7), .mb-turn-table td:nth-child(7) { width: 10%; }
738
+ .mb-turn-preview, .mb-turn-model { color: var(--dsw-alias-label-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
739
+ .mb-turn-model { color: var(--dsw-alias-label-primary); }
740
+ .mb-panel-note { margin: 9px 0 0; color: var(--dsw-alias-label-secondary); font-size: 11px; }
741
+ .mb-turn { color: var(--dsw-alias-label-secondary); font-size: 12px; font-variant-numeric: tabular-nums; display: inline-flex; flex-wrap: wrap; align-items: baseline; column-gap: 10px; row-gap: 2px; max-width: 100%; }
742
+ .mb-turn[data-size="small"] { font-size: 11px; }
743
+ .mb-turn[data-size="large"] { font-size: 13px; }
744
+ .mb-turn[data-density="compact"] { column-gap: 6px; }
745
+ .mb-turn-item { white-space: nowrap; }
746
+ .mb-turn strong { color: inherit; font-weight: 600; }
747
+ .mb-turn[data-weight="normal"] { font-weight: 400; }
748
+ .mb-turn[data-weight="normal"] strong { font-weight: 400; }
749
+ .mb-turn[data-weight="bold"] { font-weight: 600; }
750
+ .mb-turn[data-weight="bold"] strong { font-weight: 600; }
751
+ .mb-turn-partial { color: var(--dsw-alias-state-warn-primary); }
752
+ .mb-tag.is-muted { color: var(--dsw-alias-label-secondary); }
753
+ @media (max-width: 720px) {
754
+ .mb-group-head { padding: 11px 12px 9px; }
755
+ .mb-group-body { padding: 0 12px; }
756
+ .mb-model-body { margin-left: 0; }
757
+ }
758
+ .mb-picker { position: relative; display: flex; min-width: 0; }
759
+ .mb-picker-trigger { border-radius: var(--dsw-radius-sm); min-width: 0; max-width: min(360px, 45cqw); height: 28px; color: var(--dsw-alias-label-secondary); cursor: pointer; background: 0 0; border: none; outline: none; align-items: center; gap: 4px; padding: 0 4px 0 8px; font-size: 13px; font-weight: 400; line-height: 20px; display: flex; }
760
+ .mb-picker-trigger:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover); }
761
+ .mb-picker-trigger:focus-visible { box-shadow: 0 0 0 2px var(--dsw-focus-ring-color, var(--dsw-alias-state-business-primary)); }
762
+ .mb-picker-trigger:disabled { color: var(--dsw-alias-label-dimmed, var(--dsw-alias-label-secondary)); cursor: default; }
763
+ .mb-picker-icon { display: var(--dsh-composer-model-icon-display, none); flex: none; }
764
+ .mb-picker-name { color: inherit; text-overflow: ellipsis; white-space: nowrap; min-width: 0; overflow: hidden; display: var(--dsh-composer-model-text-display, block); }
765
+ .mb-picker-trigger-effort { text-overflow: ellipsis; white-space: nowrap; min-width: 0; color: var(--dsw-alias-label-caption, var(--dsw-alias-label-secondary)); flex-shrink: 1000; overflow: hidden; display: var(--dsh-composer-model-text-display, block); }
766
+ .mb-picker-chevron { color: var(--dsw-alias-label-caption, var(--dsw-alias-label-secondary)); flex: none; transition: transform .12s; }
767
+ .mb-picker-chevron-open { transform: rotate(180deg); }
768
+ .mb-picker-menu { box-sizing: border-box; z-index: 1100; width: min(360px, calc(100vw - 32px)); max-width: min(360px, calc(100vw - 32px)); max-height: min(480px, calc(100vh - 96px)); box-shadow: var(--dsw-elevation-prominent); background: var(--dsw-menu-surface-fill, var(--dsw-alias-bg-layer-1)); backdrop-filter: var(--dsw-menu-backdrop-filter, none); color: var(--dsw-alias-label-primary); border: 0; border-radius: var(--dsw-radius-lg, 10px); flex-direction: column; padding: 10px; display: flex; position: fixed; overflow: hidden; }
769
+ .mb-picker-search { box-sizing: border-box; border-radius: var(--dsw-radius-md); background: var(--dsw-alias-bg-layer-2); border: 1px solid var(--dsw-alias-border-l1); height: 32px; margin: 0 0 6px; padding: 0 9px; font: inherit; font-size: 13px; color: var(--dsw-alias-label-primary); width: 100%; flex: none; }
770
+ .mb-picker-search::placeholder { color: var(--dsw-alias-label-caption, var(--dsw-alias-label-secondary)); }
771
+ .mb-picker-search:focus { border-color: var(--dsw-alias-state-business-primary); outline: none; }
772
+ .mb-picker-list { min-height: 0; overflow-y: auto; padding: 0 2px; display: flex; flex-direction: column; }
773
+ .mb-picker-group { color: var(--dsw-alias-label-tertiary, var(--dsw-alias-label-secondary)); background: var(--dsw-alias-menu-group-header-fill, var(--dsw-menu-surface-fill, var(--dsw-alias-bg-layer-1))); position: sticky; top: 0; z-index: 1; margin-top: 2px; padding: 5px 10px; font-size: 11px; font-weight: 500; line-height: 16px; }
774
+ /* The first group header opens the list, so the menu's own padding already
775
+ separates it from the top edge; it only drops the inter-group lead-in.
776
+ A negative margin cannot do this: the list is a scroll container, and the
777
+ overflow clip eats any lead-in that leaves its scrollport. */
778
+ .mb-picker-group:first-child { margin-top: 0; }
779
+ .mb-picker-row { box-sizing: border-box; border-radius: var(--dsw-radius-md); width: auto; min-width: 100%; color: inherit; text-align: left; cursor: pointer; background: 0 0; border: none; outline: none; align-items: center; gap: 8px; padding: 10px; font: inherit; display: flex; }
780
+ .mb-picker-row + .mb-picker-row { margin-top: 2px; }
781
+ .mb-picker-row:hover:not(:disabled), .mb-picker-row:focus-visible { background: var(--dsw-alias-interactive-bg-hover); }
782
+ .mb-picker-row:disabled { color: var(--dsw-alias-label-dimmed, var(--dsw-alias-label-secondary)); cursor: default; }
783
+ .mb-picker-option-copy { flex-direction: column; flex: 1; min-width: 0; gap: 4px; display: flex; }
784
+ .mb-picker-model { color: inherit; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; font-weight: 400; line-height: 20px; overflow: hidden; }
785
+ .mb-picker-prices { color: var(--dsw-alias-label-caption, var(--dsw-alias-label-secondary)); font-size: 11px; line-height: 17px; font-variant-numeric: tabular-nums; }
786
+ .mb-picker-price-line { white-space: normal; overflow-wrap: anywhere; }
787
+ .mb-picker-price-source { margin-bottom: 2px; color: var(--dsw-alias-label-secondary); font-weight: 600; }
788
+ .mb-picker-check { color: var(--dsw-alias-label-primary); flex: 0 0 14px; place-items: center; display: grid; font-size: 12px; }
789
+ .mb-picker-check svg { width: 14px; height: 14px; }
790
+ .mb-picker-empty { color: var(--dsw-alias-label-tertiary, var(--dsw-alias-label-secondary)); padding: 8px; font-size: 12px; line-height: 18px; }
791
+ .mb-picker-divider { border-top: 1px solid var(--dsw-alias-border-l1); margin: 3px 0; }
792
+ .mb-picker-efforts { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; padding: 2px 4px 4px; }
793
+ .mb-picker-efforts-label { color: var(--dsw-alias-label-tertiary, var(--dsw-alias-label-secondary)); font-size: 11px; margin-right: 2px; }
794
+ .mb-picker-chip { appearance: none; border: 0; background: 0 0; color: var(--dsw-alias-label-secondary); font: inherit; font-size: 11px; height: 24px; padding: 0 7px; border-radius: var(--dsw-radius-sm); cursor: pointer; }
795
+ .mb-picker-chip:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover); color: var(--dsw-alias-label-primary); }
796
+ .mb-picker-chip[aria-pressed="true"] { background: var(--dsw-alias-interactive-bg-hover); color: var(--dsw-alias-label-primary); }
797
+ .mb-picker-error { border-radius: var(--dsw-radius-md); background: var(--dsw-alias-interactive-bg-hover-danger); color: var(--dsw-alias-state-error-primary); margin-bottom: 3px; padding: 6px 7px; font-size: 11px; line-height: 16px; }
798
+ .mb-picker-retry { align-self: flex-start; margin: 0 4px 2px; }
799
+ .mb-picker-status { color: var(--dsw-alias-label-tertiary, var(--dsw-alias-label-secondary)); padding: 4px 7px; font-size: 11px; line-height: 16px; }
800
+ .mb-picker-dot { width: 12px; height: 12px; border: 2px solid var(--dsw-alias-state-business-primary); border-right-color: transparent; border-radius: 50%; flex: none; animation: mb-picker-spin .7s linear infinite; }
801
+ @keyframes mb-picker-spin { to { transform: rotate(360deg); } }
802
+ .mb-picker-toast { position: absolute; left: 8px; right: 8px; bottom: 8px; z-index: 3; border-radius: var(--dsw-radius-md); background: var(--dsw-alias-state-error-primary); color: #fff; padding: 7px 9px; font-size: 12px; line-height: 16px; box-shadow: var(--dsw-elevation-prominent); }
803
+ .mb-fatal { border: 1px solid var(--dsw-alias-state-error-primary); color: var(--dsw-alias-state-error-primary); border-radius: 8px; padding: 12px; }
804
+ @media (max-width: 720px) {
805
+ .mb-price-grid { grid-template-columns: 1fr; }
806
+ .mb-summary { grid-template-columns: repeat(2, minmax(0, 1fr)); }
807
+ .mb-head-actions { width: 100%; margin-left: 38px; justify-content: flex-start; }
808
+ }
809
+ `
810
+
811
+ const STYLE_MARKER = 'data-dsh-model-billing'
812
+
813
+ // The stylesheet belongs to the plugin, not to any one surface, so inject it
814
+ // once into <head> instead of rendering a copy per settings page, dock,
815
+ // panel and picker.
816
+ function ensureStylesheet() {
817
+ if (typeof document === 'undefined') return
818
+ if (document.head.querySelector(`style[${STYLE_MARKER}]`) !== null) return
819
+ const style = document.createElement('style')
820
+ style.setAttribute(STYLE_MARKER, '')
821
+ style.textContent = CSS
822
+ document.head.appendChild(style)
823
+ }
824
+
825
+ function Style() {
826
+ ensureStylesheet()
827
+ return null
828
+ }
829
+
830
+ // ---------------------------------------------------------------------
831
+ // Settings section
832
+ // ---------------------------------------------------------------------
833
+
834
+ const PRICE_FIELDS = ['cacheHit', 'cacheMiss', 'output']
835
+ const PRICE_LABEL_KEYS = {
836
+ cacheHit: 'cacheHitPrice',
837
+ cacheMiss: 'cacheMissPrice',
838
+ output: 'outputPrice',
839
+ }
840
+
841
+ function toDraftPricing(pricing) {
842
+ if (pricing?.builtin === true) return { ...pricing, peak: { ...pricing.peak }, offPeak: { ...pricing.offPeak } }
843
+ const source = pricing ?? {}
844
+ return Object.fromEntries(PRICE_FIELDS.map((field) => [
845
+ field,
846
+ source[field] === null || source[field] === undefined ? '' : String(source[field]),
847
+ ]))
848
+ }
849
+
850
+ function toDraft(models) {
851
+ return models.map((model) => ({
852
+ provider: model.provider,
853
+ providerName: model.providerName,
854
+ model: model.model,
855
+ name: model.name,
856
+ isDeepSeek: model.isDeepSeek,
857
+ pricing: toDraftPricing(model.pricing),
858
+ }))
859
+ }
860
+
861
+ function toDraftArchive(entries) {
862
+ return (entries ?? []).map((entry) => ({
863
+ provider: entry.provider,
864
+ model: entry.model,
865
+ pricing: toDraftPricing(entry.pricing),
866
+ }))
867
+ }
868
+
869
+ function parsePriceInput(value) {
870
+ if (typeof value === 'string' && value.trim() === '') return null
871
+ const number = Number(value)
872
+ if (!Number.isFinite(number) || number < 0 || number > 1e12) return undefined
873
+ return Math.round(number * 1e8) / 1e8
874
+ }
875
+
876
+ function SettingsSection() {
877
+ const translate = useT()
878
+ const snap = useCatalog()
879
+ const [draft, setDraft] = useState(null)
880
+ const [archive, setArchive] = useState(null)
881
+ const [display, setDisplay] = useState(null)
882
+ const [dirty, setDirty] = useState(false)
883
+ const [saving, setSaving] = useState(false)
884
+ const [status, setStatus] = useState(null)
885
+
886
+ useEffect(() => {
887
+ if (snap.data === null) return
888
+ setDraft(toDraft(snap.data.models))
889
+ setArchive(toDraftArchive(snap.data.pricingArchive))
890
+ setDisplay(readDisplay(snap.data))
891
+ setDirty(false)
892
+ }, [snap.data])
893
+
894
+ const models = draft ?? []
895
+ const archiveRows = archive ?? []
896
+ const update = (index, change) => {
897
+ setDraft((current) => current === null ? current : current.map((entry, at) => at === index ? change(entry) : entry))
898
+ setDirty(true)
899
+ setStatus(null)
900
+ }
901
+
902
+ const updatePrice = (index, field, value) => {
903
+ update(index, (entry) => ({
904
+ ...entry,
905
+ pricing: { ...entry.pricing, [field]: value },
906
+ }))
907
+ }
908
+
909
+ // "Custom prices" starts from blank fields; the user opts in explicitly.
910
+ const resetPricing = (index) => {
911
+ update(index, (entry) => ({
912
+ ...entry,
913
+ pricing: { cacheHit: '', cacheMiss: '', output: '' },
914
+ }))
915
+ }
916
+
917
+ // "Restore official prices" puts the built-in peak/off-peak card back
918
+ // immediately, so the user sees the official table before saving.
919
+ const restoreOfficialPricing = (index) => {
920
+ const entry = models[index]
921
+ if (entry === undefined) return
922
+ const catalogRow = (snap.data?.models ?? []).find((row) =>
923
+ row.provider === entry.provider && row.model === entry.model)
924
+ const official = catalogRow?.pricing?.official
925
+ ?? (catalogRow?.pricing?.builtin === true ? catalogRow.pricing : null)
926
+ const restored = official?.builtin === true
927
+ ? toDraftPricing(official)
928
+ : { cacheHit: '', cacheMiss: '', output: '' }
929
+ update(index, (current) => ({ ...current, pricing: restored }))
930
+ }
931
+
932
+ const move = (index, delta) => {
933
+ const target = index + delta
934
+ if (target < 0 || target >= models.length) return
935
+ setDraft((current) => {
936
+ if (current === null) return current
937
+ const next = [...current]
938
+ const [item] = next.splice(index, 1)
939
+ next.splice(target, 0, item)
940
+ return next
941
+ })
942
+ setDirty(true)
943
+ setStatus(null)
944
+ }
945
+
946
+ const updateArchive = (index, change) => {
947
+ setArchive((current) => current === null ? current : current.map((entry, at) => at === index ? change(entry) : entry))
948
+ setDirty(true)
949
+ setStatus(null)
950
+ }
951
+
952
+ const updateArchivePrice = (index, field, value) => {
953
+ updateArchive(index, (entry) => ({ ...entry, pricing: { ...entry.pricing, [field]: value } }))
954
+ }
955
+
956
+ const addArchive = () => {
957
+ setArchive((current) => [...(current ?? []), { provider: '', model: '', pricing: { cacheHit: '', cacheMiss: '', output: '' } }])
958
+ setDirty(true)
959
+ setStatus(null)
960
+ }
961
+
962
+ const removeArchive = (index) => {
963
+ setArchive((current) => current === null ? current : current.filter((entry, at) => at !== index))
964
+ setDirty(true)
965
+ setStatus(null)
966
+ }
967
+
968
+ const reset = () => {
969
+ emitCatalog()
970
+ if (snap.data !== null) {
971
+ setDraft(toDraft(snap.data.models))
972
+ setArchive(toDraftArchive(snap.data.pricingArchive))
973
+ setDisplay(readDisplay(snap.data))
974
+ }
975
+ setDirty(false)
976
+ setStatus(null)
977
+ }
978
+
979
+ const displayValue = display ?? readDisplay(snap.data)
980
+
981
+ const updateDisplay = (change) => {
982
+ setDisplay((current) => ({ ...(current ?? readDisplay(snap.data)), ...change }))
983
+ setDirty(true)
984
+ setStatus(null)
985
+ }
986
+
987
+ const HERO_ICON = h('svg', {
988
+ viewBox: '0 0 64 64',
989
+ width: 42,
990
+ height: 42,
991
+ 'aria-hidden': true,
992
+ focusable: 'false',
993
+ },
994
+ h('defs', null,
995
+ h('linearGradient', { id: 'mb-hero-coin', x1: 8, y1: 6, x2: 56, y2: 58, gradientUnits: 'userSpaceOnUse' },
996
+ h('stop', { offset: 0, stopColor: '#1f8a8a' }),
997
+ h('stop', { offset: 1, stopColor: '#185a9d' }))),
998
+ h('rect', { x: 4, y: 4, width: 56, height: 56, rx: 14, fill: 'url(#mb-hero-coin)' }),
999
+ h('circle', { cx: 32, cy: 32, r: 19, fill: 'none', stroke: '#ffffff', strokeOpacity: 0.9, strokeWidth: 2.4 }),
1000
+ h('path', {
1001
+ d: 'M25 23l7 9 7-9',
1002
+ fill: 'none',
1003
+ stroke: '#ffffff',
1004
+ strokeWidth: 2.8,
1005
+ strokeLinecap: 'round',
1006
+ strokeLinejoin: 'round',
1007
+ }),
1008
+ h('path', { d: 'M32 32v11M25.5 31.5h13M25.5 36.5h13', fill: 'none', stroke: '#ffffff', strokeWidth: 2.4, strokeLinecap: 'round' }),
1009
+ h('path', { d: 'M18 47.5h8M28 47.5h8M38 47.5h8', stroke: '#ffffff', strokeOpacity: 0.55, strokeWidth: 2, strokeLinecap: 'round' }))
1010
+
1011
+ const renderHero = () => h('header', { className: 'mb-hero' },
1012
+ h('span', { className: 'mb-mark' }, HERO_ICON),
1013
+ h('div', null,
1014
+ h('h2', null, translate('title')),
1015
+ h('p', null, translate('intro'))))
1016
+
1017
+ const switchControl = (checked, onChange, label, disabled = false) => h('button', {
1018
+ type: 'button',
1019
+ role: 'switch',
1020
+ className: 'mb-switch',
1021
+ 'aria-checked': checked ? 'true' : 'false',
1022
+ 'aria-label': label,
1023
+ title: label,
1024
+ disabled,
1025
+ onClick: () => onChange(!checked),
1026
+ })
1027
+
1028
+ const row = (label, description, control, disabled = false, key) => h('div', {
1029
+ className: 'mb-row' + (disabled ? ' is-disabled' : ''),
1030
+ key,
1031
+ },
1032
+ h('div', { className: 'mb-row-copy' },
1033
+ h('div', { className: 'mb-row-label' }, label),
1034
+ description === undefined ? null : h('div', { className: 'mb-row-desc' }, description)),
1035
+ h('div', { className: 'mb-row-control' }, control))
1036
+
1037
+ const group = (title, description, ...children) => h('section', { className: 'mb-group' },
1038
+ h('header', { className: 'mb-group-head' },
1039
+ h('h3', null, title),
1040
+ description === undefined ? null : h('p', null, description)),
1041
+ h('div', { className: 'mb-group-body' }, children))
1042
+
1043
+ const toggleRow = (field, label, disabled = false) => row(
1044
+ translate(label),
1045
+ undefined,
1046
+ switchControl(displayValue[field] === true, (next) => updateDisplay({ [field]: next }), translate(label), disabled),
1047
+ disabled,
1048
+ field)
1049
+
1050
+ const renderDisplaySettings = () => h(Fragment, null,
1051
+ group(translate('pickerSection'), translate('takeoverHint'),
1052
+ toggleRow('takeoverPicker', 'takeoverPicker')),
1053
+ group(translate('dockSection'), translate('dockHint'),
1054
+ toggleRow('showDock', 'displayDock'),
1055
+ toggleRow('showUnpricedNotice', 'showUnpricedNotice', displayValue.showDock !== true)),
1056
+ group(translate('displaySection'), translate('displayHint'),
1057
+ toggleRow('showTurnTail', 'displayTurnTail'),
1058
+ h('div', { className: 'mb-subhead' }, translate('displayFields')),
1059
+ [
1060
+ ['showProvider', 'displayProvider'],
1061
+ ['showModels', 'displayModels'],
1062
+ ['showCost', 'displayCost'],
1063
+ ['showTokens', 'displayTokens'],
1064
+ ['showCacheHit', 'displayCacheHit'],
1065
+ ['showLabels', 'displayLabels'],
1066
+ ].map(([field, label]) => {
1067
+ const off = displayValue.showTurnTail !== true ||
1068
+ (field === 'showProvider' && displayValue.showModels !== true)
1069
+ return toggleRow(field, label, off)
1070
+ }),
1071
+ h('div', { className: 'mb-subhead' }, translate('displayLook')),
1072
+ row(translate('displayFontSize'), undefined, h('select', {
1073
+ className: 'mb-select',
1074
+ value: displayValue.fontSize,
1075
+ onChange: (event) => updateDisplay({ fontSize: event.target.value }),
1076
+ },
1077
+ h('option', { value: 'small' }, translate('fontSizeSmall')),
1078
+ h('option', { value: 'normal' }, translate('fontSizeNormal')),
1079
+ h('option', { value: 'large' }, translate('fontSizeLarge')))),
1080
+ row(translate('displayDensity'), undefined, h('select', {
1081
+ className: 'mb-select',
1082
+ value: displayValue.density,
1083
+ onChange: (event) => updateDisplay({ density: event.target.value }),
1084
+ },
1085
+ h('option', { value: 'compact' }, translate('densityCompact')),
1086
+ h('option', { value: 'comfortable' }, translate('densityComfortable')))),
1087
+ row(translate('displayWeight'), undefined, h('select', {
1088
+ className: 'mb-select',
1089
+ value: displayValue.fontWeight,
1090
+ onChange: (event) => updateDisplay({ fontWeight: event.target.value }),
1091
+ },
1092
+ h('option', { value: 'normal' }, translate('weightNormal')),
1093
+ h('option', { value: 'bold' }, translate('weightBold'))))))
1094
+
1095
+ const renderArchive = () => group(translate('archiveSection'), translate('archiveHint'),
1096
+ archiveRows.length === 0
1097
+ ? h('div', { className: 'mb-empty' }, translate('archiveEmpty'))
1098
+ : archiveRows.map((entry, index) => h('div', { className: 'mb-archive-row', key: index },
1099
+ h('div', { className: 'mb-archive-head' },
1100
+ h('input', {
1101
+ className: 'mb-input',
1102
+ placeholder: translate('archiveProvider'),
1103
+ 'aria-label': translate('archiveProvider'),
1104
+ value: entry.provider,
1105
+ onChange: (event) => updateArchive(index, (current) => ({ ...current, provider: event.target.value })),
1106
+ }),
1107
+ h('input', {
1108
+ className: 'mb-input',
1109
+ placeholder: translate('archiveModel'),
1110
+ 'aria-label': translate('archiveModel'),
1111
+ value: entry.model,
1112
+ onChange: (event) => updateArchive(index, (current) => ({ ...current, model: event.target.value })),
1113
+ }),
1114
+ h('button', {
1115
+ type: 'button',
1116
+ className: 'mb-btn',
1117
+ title: translate('remove'),
1118
+ 'aria-label': translate('remove'),
1119
+ onClick: () => removeArchive(index),
1120
+ }, translate('remove'))),
1121
+ h('div', { className: 'mb-price-grid' },
1122
+ PRICE_FIELDS.map((field) => h('input', {
1123
+ key: field,
1124
+ className: 'mb-input',
1125
+ inputMode: 'decimal',
1126
+ placeholder: translate(PRICE_LABEL_KEYS[field]),
1127
+ 'aria-label': translate(PRICE_LABEL_KEYS[field]),
1128
+ value: entry.pricing[field] ?? '',
1129
+ onChange: (event) => updateArchivePrice(index, field, event.target.value),
1130
+ }))))),
1131
+ h('div', { className: 'mb-archive-actions' },
1132
+ h('button', { type: 'button', className: 'mb-btn', onClick: addArchive }, translate('addArchive'))))
1133
+
1134
+ const save = async () => {
1135
+ if (draft === null) return
1136
+ const payload = []
1137
+ for (const entry of draft) {
1138
+ if (entry.pricing.builtin === true) {
1139
+ payload.push({ provider: entry.provider, model: entry.model, pricing: {} })
1140
+ continue
1141
+ }
1142
+ const pricing = {}
1143
+ const blank = PRICE_FIELDS.every((field) => String(entry.pricing[field] ?? '').trim() === '')
1144
+ if (blank) {
1145
+ // Three blank fields mean "drop the override", so the official table applies again.
1146
+ payload.push({ provider: entry.provider, model: entry.model, pricing: {} })
1147
+ continue
1148
+ }
1149
+ for (const field of PRICE_FIELDS) {
1150
+ const parsed = parsePriceInput(entry.pricing[field])
1151
+ if (parsed === undefined) {
1152
+ setStatus({ tone: 'error', text: translate('invalidPrice') })
1153
+ return
1154
+ }
1155
+ pricing[field] = parsed
1156
+ }
1157
+ payload.push({ provider: entry.provider, model: entry.model, pricing })
1158
+ }
1159
+ const archivePayload = []
1160
+ const currentKeys = new Set(draft.map((entry) => `${entry.provider}\u0000${entry.model}`))
1161
+ for (const entry of archiveRows) {
1162
+ const provider = entry.provider.trim()
1163
+ const model = entry.model.trim()
1164
+ if (provider === '' && model === '') continue
1165
+ if (provider === '' || model === '') {
1166
+ setStatus({ tone: 'error', text: translate('invalidRoute') })
1167
+ return
1168
+ }
1169
+ if (currentKeys.has(`${provider}\u0000${model}`)) {
1170
+ setStatus({ tone: 'error', text: translate('archiveConflict') })
1171
+ return
1172
+ }
1173
+ const pricing = {}
1174
+ const blank = PRICE_FIELDS.every((field) => String(entry.pricing[field] ?? '').trim() === '')
1175
+ if (blank) {
1176
+ archivePayload.push({ provider, model, pricing: {} })
1177
+ continue
1178
+ }
1179
+ for (const field of PRICE_FIELDS) {
1180
+ const parsed = parsePriceInput(entry.pricing[field])
1181
+ if (parsed === undefined) {
1182
+ setStatus({ tone: 'error', text: translate('invalidPrice') })
1183
+ return
1184
+ }
1185
+ pricing[field] = parsed
1186
+ }
1187
+ archivePayload.push({ provider, model, pricing })
1188
+ }
1189
+ setSaving(true)
1190
+ setStatus(null)
1191
+ try {
1192
+ await requestJson('/model-billing/save', {
1193
+ method: 'POST',
1194
+ headers: { 'content-type': 'application/json' },
1195
+ body: JSON.stringify({ models: payload, pricingArchive: archivePayload, display: displayValue }),
1196
+ })
1197
+ setDirty(false)
1198
+ setStatus({ tone: 'success', text: translate('saved') })
1199
+ await ensureCatalog(true)
1200
+ } catch (error) {
1201
+ setStatus({ tone: 'error', text: translate('saveFailed', { message: error instanceof Error ? error.message : String(error) }) })
1202
+ } finally {
1203
+ setSaving(false)
1204
+ }
1205
+ }
1206
+
1207
+ if (snap.data === null) {
1208
+ return h(Fragment, null,
1209
+ h(Style),
1210
+ h('section', { className: 'mb-page' },
1211
+ renderHero(),
1212
+ snap.status === 'error'
1213
+ ? h('div', { className: 'mb-fatal' },
1214
+ h('div', null, translate('loadFailed')),
1215
+ h('p', { className: 'mb-note' }, snap.error),
1216
+ h('button', { type: 'button', className: 'mb-btn', onClick: () => void ensureCatalog(true) }, translate('retry')))
1217
+ : h('p', { className: 'mb-muted' }, translate('loading'))))
1218
+ }
1219
+
1220
+ if (draft === null) {
1221
+ return h(Fragment, null,
1222
+ h(Style),
1223
+ h('section', { className: 'mb-page' },
1224
+ renderHero(),
1225
+ h('p', { className: 'mb-muted' }, translate('loading'))))
1226
+ }
1227
+
1228
+ const renderToolbar = () => h('div', { className: 'mb-bar' },
1229
+ h('span', { className: 'mb-bar-count' }, translate('modelsCount', { count: models.length })),
1230
+ h('span', { className: 'mb-spacer' }),
1231
+ dirty ? h('span', { className: 'mb-bar-count' }, translate('unsaved')) : null,
1232
+ h('button', { type: 'button', className: 'mb-btn', disabled: !dirty || saving, onClick: reset }, translate('undo')),
1233
+ h('button', { type: 'button', className: 'mb-btn mb-btn-primary', disabled: !dirty || saving, onClick: () => void save() }, saving ? translate('saving') : translate('save')))
1234
+
1235
+ const renderModels = () => group(translate('modelsSection'), translate('modelsHint'),
1236
+ models.length === 0
1237
+ ? h('div', { className: 'mb-empty' }, translate('noModels'))
1238
+ : models.map((entry, index) => h(SettingsModelCard, {
1239
+ key: `${entry.provider}\u0000${entry.model}`,
1240
+ entry,
1241
+ index,
1242
+ count: models.length,
1243
+ translate,
1244
+ onMove: move,
1245
+ onUpdatePrice: updatePrice,
1246
+ onResetPricing: resetPricing,
1247
+ onRestoreOfficial: restoreOfficialPricing,
1248
+ })))
1249
+
1250
+ if (models.length === 0) {
1251
+ // The display controls stay editable with no models configured, so the
1252
+ // toolbar (and its save button) must still render here.
1253
+ return h(Fragment, null,
1254
+ h(Style),
1255
+ h('section', { className: 'mb-page' },
1256
+ renderHero(),
1257
+ renderToolbar(),
1258
+ status === null ? null : h('div', { className: 'mb-status', 'data-tone': status.tone, role: 'status' }, status.text),
1259
+ renderDisplaySettings(),
1260
+ renderModels(),
1261
+ renderArchive(),
1262
+ h('p', { className: 'mb-footer-note' }, translate('formula')),
1263
+ h('p', { className: 'mb-footer-note' }, translate('formulaNote'))))
1264
+ }
1265
+
1266
+ return h(Fragment, null,
1267
+ h(Style),
1268
+ h('section', { className: 'mb-page' },
1269
+ renderHero(),
1270
+ renderToolbar(),
1271
+ status === null ? null : h('div', { className: 'mb-status', 'data-tone': status.tone, role: 'status' }, status.text),
1272
+ renderDisplaySettings(),
1273
+ renderModels(),
1274
+ renderArchive(),
1275
+ h('p', { className: 'mb-footer-note' }, translate('formula')),
1276
+ h('p', { className: 'mb-footer-note' }, translate('formulaNote'))))
1277
+ }
1278
+
1279
+ function SettingsModelCard({ entry, index, count, translate, onMove, onUpdatePrice, onResetPricing, onRestoreOfficial }) {
1280
+ const id = useId()
1281
+ const pricing = entry.pricing
1282
+ const builtin = pricing.builtin === true
1283
+ const custom = entry.isDeepSeek && !builtin
1284
+ const fields = builtin ? null : PRICE_FIELDS.map((field) => {
1285
+ const inputId = `${id}-${field}`
1286
+ return h('label', { className: 'mb-field', htmlFor: inputId, key: field },
1287
+ h('span', { className: 'mb-field-label' }, translate(PRICE_LABEL_KEYS[field])),
1288
+ h('input', {
1289
+ id: inputId,
1290
+ className: 'mb-input',
1291
+ type: 'text',
1292
+ inputMode: 'decimal',
1293
+ autoComplete: 'off',
1294
+ placeholder: translate('pricePlaceholder'),
1295
+ value: pricing[field],
1296
+ onChange: (event) => onUpdatePrice(index, field, event.target.value),
1297
+ }))
1298
+ })
1299
+
1300
+ return h('div', { className: 'mb-model' },
1301
+ h('div', { className: 'mb-model-head' },
1302
+ h('span', { className: 'mb-index' }, index + 1),
1303
+ h('div', { style: { minWidth: 0, flex: 1 } },
1304
+ h('div', { className: 'mb-name' },
1305
+ h('span', { className: 'mb-name-text' }, entry.name),
1306
+ entry.isDeepSeek ? h('span', { className: 'mb-tag' }, translate('deepseekTag')) : null),
1307
+ h('div', { className: 'mb-sub' }, `${entry.providerName} · ${entry.provider} / ${entry.model}`)),
1308
+ h('div', { className: 'mb-head-actions' },
1309
+ h('button', {
1310
+ type: 'button',
1311
+ className: 'mb-btn mb-btn-icon',
1312
+ disabled: index === 0,
1313
+ title: translate('up'),
1314
+ 'aria-label': translate('up'),
1315
+ onClick: () => onMove(index, -1),
1316
+ }, '↑'),
1317
+ h('button', {
1318
+ type: 'button',
1319
+ className: 'mb-btn mb-btn-icon',
1320
+ disabled: index === count - 1,
1321
+ title: translate('down'),
1322
+ 'aria-label': translate('down'),
1323
+ onClick: () => onMove(index, 1),
1324
+ }, '↓'))),
1325
+ h('div', { className: 'mb-model-body' },
1326
+ entry.isDeepSeek ? h('p', { className: 'mb-note', style: { margin: 0 } }, translate('deepseekNote')) : null,
1327
+ h('div', { className: 'mb-price-group' },
1328
+ h('div', { className: 'mb-price-group-title' },
1329
+ h('span', null, builtin ? translate('officialPrice') : translate('singlePrice')),
1330
+ custom ? h('span', { className: 'mb-tag' }, translate('priceSourceCustom')) : null,
1331
+ h('span', { className: 'mb-muted', style: { fontWeight: 400, fontSize: '11px' } }, translate('perMillion'))),
1332
+ builtin
1333
+ ? h(Fragment, null,
1334
+ h(PriceLines, { pricing, translate, showSource: false }),
1335
+ h('p', { className: 'mb-note' }, translate('deepseekSchedule')))
1336
+ : h(Fragment, null,
1337
+ h('div', { className: 'mb-price-grid' }, fields),
1338
+ custom ? h('p', { className: 'mb-note' }, translate('customPriceNote')) : null),
1339
+ entry.isDeepSeek
1340
+ ? h('div', { className: 'mb-card-actions' },
1341
+ h('button', {
1342
+ type: 'button',
1343
+ className: 'mb-btn',
1344
+ onClick: () => builtin ? onResetPricing(index) : onRestoreOfficial(index),
1345
+ }, builtin ? translate('customizePrice') : translate('useOfficialPrice')))
1346
+ : null)))
1347
+ }
1348
+
1349
+ // ---------------------------------------------------------------------
1350
+ // Composer dock cost summary
1351
+ // ---------------------------------------------------------------------
1352
+
1353
+ function CostDock({ sessionId, useProjection }) {
1354
+ const translate = useT()
1355
+ const tokenUsage = useProjection('tokenUsage')
1356
+ const revision = tokenUsageRevision(tokenUsage)
1357
+ const usage = useSessionUsage(sessionId, revision)
1358
+ const catalogSnap = useCatalog()
1359
+ const [open, setOpen] = useState(false)
1360
+ const [panelPos, setPanelPos] = useState(null)
1361
+ const [editingRoute, setEditingRoute] = useState(null)
1362
+ const [editPrices, setEditPrices] = useState({ cacheHit: '', cacheMiss: '', output: '' })
1363
+ const [savingPrice, setSavingPrice] = useState(false)
1364
+ const [priceStatus, setPriceStatus] = useState(null)
1365
+ const triggerRef = useRef(null)
1366
+ const panelRef = useRef(null)
1367
+
1368
+ useLayoutEffect(() => {
1369
+ if (!open) {
1370
+ setPanelPos(null)
1371
+ return undefined
1372
+ }
1373
+ const place = () => {
1374
+ const rect = triggerRef.current?.getBoundingClientRect()
1375
+ if (rect === undefined) return
1376
+ const width = Math.min(660, window.innerWidth - 24)
1377
+ const naturalLeft = Math.max(12, Math.min(rect.left, window.innerWidth - width - 12))
1378
+ // The dock sits near the right edge; shift the whole detail panel left so
1379
+ // the bottom-right whale widget cannot cover it.
1380
+ const left = Math.max(12, naturalLeft - 120)
1381
+ const spaceAbove = rect.top - 12
1382
+ const spaceBelow = window.innerHeight - rect.bottom - 12
1383
+ const above = spaceAbove > spaceBelow
1384
+ const maxHeight = Math.min(430, Math.max(180, above ? spaceAbove : spaceBelow))
1385
+ setPanelPos(above
1386
+ ? { left, width, bottom: window.innerHeight - rect.top + 8, maxHeight }
1387
+ : { left, width, top: rect.bottom + 8, maxHeight })
1388
+ }
1389
+ place()
1390
+ window.addEventListener('resize', place)
1391
+ window.addEventListener('scroll', place, true)
1392
+ return () => {
1393
+ window.removeEventListener('resize', place)
1394
+ window.removeEventListener('scroll', place, true)
1395
+ }
1396
+ }, [open])
1397
+
1398
+ useEffect(() => {
1399
+ if (!open) return undefined
1400
+ const closeOutside = (event) => {
1401
+ if (triggerRef.current?.contains(event.target) === true) return
1402
+ if (panelRef.current?.contains(event.target) === true) return
1403
+ setOpen(false)
1404
+ }
1405
+ const closeOnEscape = (event) => {
1406
+ if (event.key !== 'Escape') return
1407
+ event.preventDefault()
1408
+ setOpen(false)
1409
+ triggerRef.current?.focus()
1410
+ }
1411
+ document.addEventListener('mousedown', closeOutside)
1412
+ document.addEventListener('keydown', closeOnEscape)
1413
+ // A dialog must take focus when it opens so keyboard users land inside it.
1414
+ panelRef.current?.focus()
1415
+ return () => {
1416
+ document.removeEventListener('mousedown', closeOutside)
1417
+ document.removeEventListener('keydown', closeOnEscape)
1418
+ }
1419
+ }, [open])
1420
+
1421
+ // Hooks must run on every render, so derive the catalog indexes before any
1422
+ // early return; React rejects a different hook count between renders.
1423
+ const orderIndex = useMemo(() =>
1424
+ new Map((catalogSnap.data?.models ?? []).map((model) => [`${model.provider}\u0000${model.model}`, model.order])),
1425
+ [catalogSnap.data])
1426
+ const catalogByKey = useMemo(() =>
1427
+ new Map((catalogSnap.data?.models ?? []).map((model) => [`${model.provider}\u0000${model.model}`, model])),
1428
+ [catalogSnap.data])
1429
+
1430
+ const display = readDisplay(catalogSnap.data)
1431
+ if (display.showDock === false) return null
1432
+
1433
+ const data = usage.data
1434
+ if (data === null || data === undefined || data.totals?.hasBillable !== true) {
1435
+ return null
1436
+ }
1437
+ const totals = data.totals
1438
+ const unpricedRoutes = data.unpricedRoutes ?? []
1439
+ const beginRouteEdit = (route) => {
1440
+ setEditingRoute(`${route.provider}\u0000${route.model}`)
1441
+ setEditPrices({ cacheHit: '', cacheMiss: '', output: '' })
1442
+ setPriceStatus(null)
1443
+ }
1444
+ const saveRoutePrice = async (route) => {
1445
+ const pricing = {}
1446
+ for (const field of PRICE_FIELDS) {
1447
+ const parsed = parsePriceInput(editPrices[field])
1448
+ if (parsed === undefined || parsed === null) {
1449
+ setPriceStatus({ tone: 'error', text: translate('invalidPrice') })
1450
+ return
1451
+ }
1452
+ pricing[field] = parsed
1453
+ }
1454
+ setSavingPrice(true)
1455
+ setPriceStatus(null)
1456
+ try {
1457
+ await requestJson('/model-billing/save', {
1458
+ method: 'POST',
1459
+ headers: { 'content-type': 'application/json' },
1460
+ body: JSON.stringify({ pricingArchive: [{ provider: route.provider, model: route.model, pricing }] }),
1461
+ })
1462
+ await ensureCatalog(true)
1463
+ setEditingRoute(null)
1464
+ setPriceStatus({ tone: 'success', text: translate('priceSaved') })
1465
+ await ensureUsage(sessionId, revision, true)
1466
+ } catch (error) {
1467
+ setPriceStatus({ tone: 'error', text: translate('saveFailed', { message: error instanceof Error ? error.message : String(error) }) })
1468
+ } finally {
1469
+ setSavingPrice(false)
1470
+ }
1471
+ }
1472
+ const last = [...(data.turns ?? [])].reverse().find((entry) => entry.hasBillable) ?? null
1473
+ const totalText = totals.costKnown
1474
+ ? `≈ ${formatMoney(totals.cost)}`
1475
+ : totals.pricedAttempts > 0 ? `≈ ${formatMoney(totals.cost)}` : translate('pricePlaceholder')
1476
+ const lastText = last === null || last === undefined || !last.hasBillable
1477
+ ? null
1478
+ : last.costKnown ? `≈ ${formatMoney(last.cost)}` : last.pricedAttempts > 0 ? `≈ ${formatMoney(last.cost)}` : translate('pricePlaceholder')
1479
+ // Rebuilding the index and scanning the catalog on every render gets
1480
+ // expensive on a long session, so both indexes are memoized above.
1481
+ const byModel = [...(data.byModel ?? [])].sort((left, right) => {
1482
+ const leftKey = left.provider === null ? '' : `${left.provider}\u0000${left.model}`
1483
+ const rightKey = right.provider === null ? '' : `${right.provider}\u0000${right.model}`
1484
+ return (orderIndex.get(leftKey) ?? Number.MAX_SAFE_INTEGER) - (orderIndex.get(rightKey) ?? Number.MAX_SAFE_INTEGER)
1485
+ })
1486
+
1487
+ const modelNameOf = (model) => {
1488
+ const catalogRow = model?.provider === null || model?.provider === undefined
1489
+ ? undefined
1490
+ : catalogByKey.get(`${model.provider}\u0000${model.model}`)
1491
+ return catalogRow?.name ?? model?.name ?? model?.model ?? '—'
1492
+ }
1493
+ const turnModelNames = (turn) => {
1494
+ const names = []
1495
+ for (const model of turn.models ?? []) {
1496
+ const name = modelNameOf(model)
1497
+ if (!names.includes(name)) names.push(name)
1498
+ }
1499
+ return names
1500
+ }
1501
+
1502
+ const header = h('thead', null,
1503
+ h('tr', null,
1504
+ h('th', null, translate('turn')),
1505
+ h('th', null, translate('content')),
1506
+ h('th', null, translate('time')),
1507
+ h('th', null, translate('model')),
1508
+ h('th', { className: 'num' }, translate('cost')),
1509
+ h('th', { className: 'num' }, translate('tokens')),
1510
+ h('th', { className: 'num' }, translate('cacheHit'))))
1511
+
1512
+ const turnRows = [...(data.turns ?? [])].reverse().slice(0, 20).map((turn) => {
1513
+ const costText = turn.costKnown
1514
+ ? `≈ ${formatMoney(turn.cost)}`
1515
+ : turn.pricedAttempts > 0 ? `≈ ${formatMoney(turn.cost)}` : translate('pricePlaceholder')
1516
+ const modelNames = turnModelNames(turn)
1517
+ const modelText = modelNames.length === 0
1518
+ ? '—'
1519
+ : modelNames.length === 1 ? modelNames[0] : `${modelNames[0]} +${modelNames.length - 1}`
1520
+ return h('tr', { key: turn.turn },
1521
+ h('td', null, `#${turn.turn}`),
1522
+ h('td', { className: 'mb-turn-preview', title: turn.preview }, turn.preview || '—'),
1523
+ h('td', null, `${timeText(turn.startTime)}–${timeText(turn.endTime)}`),
1524
+ h('td', { className: 'mb-turn-model', title: modelNames.join(' / ') }, modelText),
1525
+ h('td', { className: 'num' }, costText),
1526
+ h('td', { className: 'num' }, `${formatTokens(turn.promptTokens)} / ${formatTokens(turn.outputTokens)}`),
1527
+ h('td', { className: 'num' }, formatPercent(turn.cacheHit)))
1528
+ })
1529
+
1530
+ const turnTable = h('div', { style: { overflowX: 'auto' } },
1531
+ h('table', { className: 'mb-table mb-turn-table' }, header, h('tbody', null, turnRows)))
1532
+
1533
+ const modelRows = byModel.map((model) => {
1534
+ const catalogRow = model.provider === null
1535
+ ? undefined
1536
+ : catalogByKey.get(`${model.provider}\u0000${model.model}`)
1537
+ const modelName = catalogRow === undefined ? model.name : `${catalogRow.providerName} / ${catalogRow.name}`
1538
+ return h('tr', { key: `${model.provider}\u0000${model.model}\u0000${model.name}` },
1539
+ h('td', null,
1540
+ modelName,
1541
+ model.isDeepSeek ? h('span', { className: 'mb-tag' }, translate('deepseekTag')) : null),
1542
+ h('td', { className: 'num' },
1543
+ model.pricedAttempts > 0 ? `≈ ${formatMoney(model.cost)}` : translate('pricePlaceholder')),
1544
+ h('td', { className: 'num', style: { color: 'var(--dsw-alias-label-secondary)' } },
1545
+ `${formatTokens(model.promptTokens)} / ${formatTokens(model.outputTokens)}`))
1546
+ })
1547
+
1548
+ const modelTable = byModel.length === 0
1549
+ ? null
1550
+ : h('div', { style: { marginTop: '10px' } },
1551
+ h('div', { className: 'mb-panel-title', style: { fontSize: '12px', marginBottom: '4px' } }, translate('byModel')),
1552
+ h('div', { style: { overflowX: 'auto' } },
1553
+ h('table', { className: 'mb-table' }, h('tbody', null, modelRows))))
1554
+
1555
+ const editingRouteRow = editingRoute === null
1556
+ ? null
1557
+ : unpricedRoutes.find((route) => `${route.provider}\u0000${route.model}` === editingRoute) ?? null
1558
+ const routeEditor = editingRouteRow === null
1559
+ ? null
1560
+ : h('div', { className: 'mb-route-editor' },
1561
+ h('span', { className: 'mb-muted' }, `${editingRouteRow.provider} / ${editingRouteRow.model}`),
1562
+ ...PRICE_FIELDS.map((field) => h('input', {
1563
+ key: field,
1564
+ className: 'mb-input',
1565
+ inputMode: 'decimal',
1566
+ placeholder: translate(PRICE_LABEL_KEYS[field]),
1567
+ 'aria-label': translate(PRICE_LABEL_KEYS[field]),
1568
+ value: editPrices[field],
1569
+ onChange: (event) => setEditPrices((current) => ({ ...current, [field]: event.target.value })),
1570
+ })),
1571
+ h('button', {
1572
+ type: 'button',
1573
+ className: 'mb-btn mb-btn-primary',
1574
+ disabled: savingPrice,
1575
+ onClick: () => void saveRoutePrice(editingRouteRow),
1576
+ }, savingPrice ? translate('saving') : translate('save')))
1577
+
1578
+ const unpricedTable = unpricedRoutes.length === 0 || display.showUnpricedNotice === false
1579
+ ? null
1580
+ : h('div', { style: { marginTop: '10px' } },
1581
+ h('div', { className: 'mb-panel-title', style: { fontSize: '12px', marginBottom: '4px' } }, translate('unpricedRoutes')),
1582
+ h('div', { style: { overflowX: 'auto' } },
1583
+ h('table', { className: 'mb-table' },
1584
+ h('tbody', null, unpricedRoutes.map((route) => h('tr', { key: `${route.provider}\u0000${route.model}` },
1585
+ h('td', null, `${route.provider} / ${route.model}`),
1586
+ h('td', { className: 'num' }, String(route.attempts)),
1587
+ h('td', { className: 'num', style: { color: 'var(--dsw-alias-label-secondary)' } },
1588
+ `${formatTokens(route.promptTokens)} / ${formatTokens(route.outputTokens)}`),
1589
+ h('td', { className: 'num' },
1590
+ h('button', {
1591
+ type: 'button',
1592
+ className: 'mb-btn',
1593
+ onClick: () => beginRouteEdit(route),
1594
+ }, translate('addPrice')))))))),
1595
+ routeEditor,
1596
+ priceStatus === null ? null : h('div', { className: 'mb-status', 'data-tone': priceStatus.tone, role: 'status' }, priceStatus.text))
1597
+
1598
+ const panelStyle = panelPos === null
1599
+ ? { left: 0, top: 0, width: 660, visibility: 'hidden' }
1600
+ : {
1601
+ left: panelPos.left,
1602
+ width: panelPos.width,
1603
+ maxHeight: panelPos.maxHeight,
1604
+ ...panelPos.top === undefined ? { bottom: panelPos.bottom } : { top: panelPos.top },
1605
+ }
1606
+
1607
+ const panel = h('div', {
1608
+ ref: panelRef,
1609
+ className: 'mb-panel mb-panel-floating',
1610
+ style: panelStyle,
1611
+ role: 'dialog',
1612
+ 'aria-modal': true,
1613
+ 'aria-label': translate('costDetails'),
1614
+ tabIndex: -1,
1615
+ },
1616
+ h('div', { className: 'mb-panel-head' },
1617
+ h('span', { className: 'mb-panel-title' }, translate('costDetails')),
1618
+ h('span', { className: 'mb-muted' }, translate('sessionTotal')),
1619
+ usage.status === 'error'
1620
+ ? h('button', {
1621
+ type: 'button',
1622
+ className: 'mb-btn',
1623
+ onClick: () => void ensureUsage(sessionId, revision, true),
1624
+ }, translate('retry'))
1625
+ : null,
1626
+ h('button', {
1627
+ type: 'button',
1628
+ className: 'mb-panel-close',
1629
+ 'aria-label': translate('close'),
1630
+ title: translate('close'),
1631
+ onClick: () => {
1632
+ setOpen(false)
1633
+ triggerRef.current?.focus()
1634
+ },
1635
+ }, '×')),
1636
+ h('div', { className: 'mb-summary' },
1637
+ summaryItem(translate('sessionTotal'), totalText),
1638
+ summaryItem(translate('thisTurn'), lastText ?? '—'),
1639
+ summaryItem(translate('cacheHit'), formatPercent(totals.cacheHit)),
1640
+ summaryItem(translate('tokens'), `${formatTokens(totals.promptTokens)} / ${formatTokens(totals.outputTokens)}`)),
1641
+ turnTable,
1642
+ modelTable,
1643
+ unpricedTable,
1644
+ h('p', { className: 'mb-panel-note' }, translate('estimateNote')),
1645
+ totals.toolCost > 0
1646
+ ? h('p', { className: 'mb-panel-note' }, translate('webToolNote', {
1647
+ amount: formatMoney(totals.toolCost),
1648
+ searchBilled: totals.webSearchBilled ?? 0,
1649
+ searchCalls: totals.webSearchCalls ?? 0,
1650
+ fetchBilled: totals.webFetchBilled ?? 0,
1651
+ fetchCalls: totals.webFetchCalls ?? 0,
1652
+ }))
1653
+ : null,
1654
+ totals.partial && display.showUnpricedNotice
1655
+ ? h('p', { className: 'mb-warn' }, translate('partialNote'))
1656
+ : null)
1657
+
1658
+ return h('div', { className: 'mb-dock' },
1659
+ h(Style),
1660
+ h('button', {
1661
+ ref: triggerRef,
1662
+ type: 'button',
1663
+ className: 'mb-dock-trigger',
1664
+ 'aria-expanded': open ? 'true' : 'false',
1665
+ 'aria-haspopup': 'dialog',
1666
+ onClick: () => setOpen((value) => !value),
1667
+ title: translate('costEstimate'),
1668
+ },
1669
+ h('svg', {
1670
+ className: 'mb-dock-icon',
1671
+ viewBox: '0 0 16 16',
1672
+ fill: 'none',
1673
+ stroke: 'currentColor',
1674
+ strokeWidth: 1.2,
1675
+ 'aria-hidden': true,
1676
+ },
1677
+ h('circle', { cx: 8, cy: 8, r: 5.5 }),
1678
+ h('path', { d: 'M8 5v6M6.2 6.8h3.6M6.2 9.2h3.6' })),
1679
+ h('span', null, translate('costEstimate')),
1680
+ h('span', { className: 'mb-dock-amount' }, totalText),
1681
+ lastText === null ? null : h(Fragment, null,
1682
+ h('span', { className: 'mb-dock-sep' }, '·'),
1683
+ h('span', null, `${translate('thisTurn')} ${lastText}`)),
1684
+ totals.partial && display.showUnpricedNotice ? h('span', { className: 'mb-warn', style: { margin: 0 } }, translate('partial')) : null),
1685
+ open ? ReactDOM.createPortal(panel, document.body) : null)
1686
+ }
1687
+
1688
+ function summaryItem(label, value) {
1689
+ return h('div', { className: 'mb-summary-item', key: label },
1690
+ h('div', { className: 'mb-summary-key' }, label),
1691
+ h('div', { className: 'mb-summary-value' }, value))
1692
+ }
1693
+
1694
+ // ---------------------------------------------------------------------
1695
+ // Per-turn cost chip
1696
+ // ---------------------------------------------------------------------
1697
+
1698
+ function turnModelText(models, translate, showProvider, catalogModels) {
1699
+ const findModel = (entry) => {
1700
+ if (entry.provider === undefined || entry.model === undefined) return undefined
1701
+ return (catalogModels ?? []).find((candidate) => candidate.provider === entry.provider && candidate.model === entry.model)
1702
+ }
1703
+ const names = (models ?? []).map((entry) => {
1704
+ if (entry === undefined || entry === null) return null
1705
+ const matched = findModel(entry)
1706
+ const modelName = typeof matched?.name === 'string' && matched.name !== ''
1707
+ ? matched.name
1708
+ : typeof entry.name === 'string' && entry.name !== ''
1709
+ ? entry.name
1710
+ : typeof entry.model === 'string' && entry.model !== '' ? entry.model : null
1711
+ if (modelName === null) return null
1712
+ if (!showProvider) return modelName
1713
+ const providerName = typeof matched?.providerName === 'string' && matched.providerName !== ''
1714
+ ? matched.providerName
1715
+ : typeof entry.providerName === 'string' && entry.providerName !== ''
1716
+ ? entry.providerName
1717
+ : typeof entry.provider === 'string' && entry.provider !== '' ? entry.provider : null
1718
+ return providerName === null ? modelName : `${providerName} / ${modelName}`
1719
+ }).filter((name) => name !== null)
1720
+ if (names.length === 0) return null
1721
+ return names.join(translate('listSep'))
1722
+ }
1723
+
1724
+ function TurnCost(props) {
1725
+ const { turn } = props
1726
+ // The turnTail owner hands over the whole folded turn life cycle, not just its number.
1727
+ const turnNumber = turn === null || turn === undefined
1728
+ ? undefined
1729
+ : typeof turn === 'number' ? turn : turn.turn
1730
+ const sessionId = props.sessionId
1731
+ const useProjection = props.useProjection
1732
+ const translate = useT()
1733
+ const catalogSnap = useCatalog()
1734
+ const display = readDisplay(catalogSnap.data)
1735
+ const tokenUsage = typeof useProjection === 'function' ? useProjection('tokenUsage') : undefined
1736
+ const revision = tokenUsageRevision(tokenUsage)
1737
+ const usage = useSessionUsage(sessionId, revision)
1738
+ // The master switch hides the whole chip while the per-field switches keep
1739
+ // their saved values, so turning it back on restores the previous layout.
1740
+ if (display.showTurnTail === false) return null
1741
+ const rows = usage.data?.turns
1742
+ const row = (usage.rowsByTurn instanceof Map ? usage.rowsByTurn.get(turnNumber) : undefined) ??
1743
+ rows?.find((entry) => entry.turn === turnNumber)
1744
+ if (row === undefined) return null
1745
+ const costText = row.costKnown
1746
+ ? `≈ ${formatMoney(row.cost)}`
1747
+ : row.pricedAttempts > 0 ? `≈ ${formatMoney(row.cost)}` : translate('pricePlaceholder')
1748
+ const modelText = turnModelText(row.models, translate, display.showProvider === true, catalogSnap.data?.models)
1749
+ const label = (key) => display.showLabels ? `${translate(key)} ` : ''
1750
+ const parts = []
1751
+ if (display.showModels && modelText !== null) {
1752
+ parts.push(h('span', { className: 'mb-turn-item', key: 'model' },
1753
+ label('model'), h('strong', null, modelText)))
1754
+ }
1755
+ if (display.showCost) {
1756
+ parts.push(h('span', { className: 'mb-turn-item', key: 'cost' },
1757
+ label('turnCost'), h('strong', null, costText)))
1758
+ }
1759
+ const promptTokens = Number.isFinite(row.promptTokens) ? row.promptTokens : 0
1760
+ const outputTokens = Number.isFinite(row.outputTokens) ? row.outputTokens : 0
1761
+ if (display.showTokens && (promptTokens > 0 || outputTokens > 0)) {
1762
+ parts.push(h('span', { className: 'mb-turn-item', key: 'tokens' },
1763
+ label('turnTokens'), h('strong', null, `${formatTokens(promptTokens)} / ${formatTokens(outputTokens)}`)))
1764
+ }
1765
+ if (display.showCacheHit && row.cacheHit !== null && row.cacheHit !== undefined) {
1766
+ parts.push(h('span', { className: 'mb-turn-item', key: 'cache' },
1767
+ label('cacheHit'), h('strong', null, formatPercent(row.cacheHit))))
1768
+ }
1769
+ if (row.partial) {
1770
+ parts.push(h('span', { className: 'mb-turn-item mb-turn-partial', key: 'partial' }, translate('partialShort')))
1771
+ }
1772
+ if (parts.length === 0) return null
1773
+ return h('span', {
1774
+ className: 'mb-turn',
1775
+ 'data-size': display.fontSize,
1776
+ 'data-density': display.density,
1777
+ 'data-weight': display.fontWeight,
1778
+ }, parts)
1779
+ }
1780
+
1781
+ // ---------------------------------------------------------------------
1782
+ // Custom-ordered model picker in the composer seat
1783
+ // ---------------------------------------------------------------------
1784
+
1785
+ const MODEL_KEY_SEPARATOR = '\u0000'
1786
+
1787
+ function OrderedModelPicker({ locked, sessionId, useProjection }) {
1788
+ const translate = useT()
1789
+ const catalogSnap = useCatalog()
1790
+ const selectionProjection = useProjection('modelSelection')
1791
+ const directoryRevision = useModelDirectoriesRevision()
1792
+ const [directoryTick, setDirectoryTick] = useState(0)
1793
+ const directory = useMemo(() => {
1794
+ if (modelDirectories === undefined || sessionId === undefined) return null
1795
+ try {
1796
+ return modelDirectories.directoryFor(sessionId)
1797
+ } catch {
1798
+ return null
1799
+ }
1800
+ }, [sessionId, directoryRevision, directoryTick])
1801
+
1802
+ const [state, setState] = useState(null)
1803
+ const [open, setOpen] = useState(false)
1804
+ const [query, setQuery] = useState('')
1805
+ const [selecting, setSelecting] = useState(false)
1806
+ // The route being submitted, so its row can show a pending spinner, and a
1807
+ // transient rejection message that must not push the list around.
1808
+ const [pending, setPending] = useState(null)
1809
+ const [toast, setToast] = useState(null)
1810
+ const [menuPos, setMenuPos] = useState(null)
1811
+ // Roving cursor for arrow-key navigation inside the model list.
1812
+ const [activeIndex, setActiveIndex] = useState(0)
1813
+ const rootRef = useRef(null)
1814
+ const triggerRef = useRef(null)
1815
+ const menuRef = useRef(null)
1816
+ const searchRef = useRef(null)
1817
+ const id = useId()
1818
+
1819
+ useEffect(() => {
1820
+ if (directory === null) {
1821
+ setState(null)
1822
+ return undefined
1823
+ }
1824
+ const sync = () => setState(directory.store.getSnapshot())
1825
+ sync()
1826
+ const unsubscribe = directory.store.subscribe(sync)
1827
+ directory.load().catch(() => {})
1828
+ return unsubscribe
1829
+ }, [directory])
1830
+
1831
+ useEffect(() => {
1832
+ if (directory !== null || directoryTick >= 20) return undefined
1833
+ const timer = setTimeout(() => setDirectoryTick((value) => value + 1), 300)
1834
+ return () => clearTimeout(timer)
1835
+ }, [directory, directoryTick])
1836
+
1837
+ useEffect(() => {
1838
+ if (!open) return undefined
1839
+ const closeOutside = (event) => {
1840
+ if (rootRef.current?.contains(event.target) === true) return
1841
+ if (menuRef.current?.contains(event.target) === true) return
1842
+ setOpen(false)
1843
+ }
1844
+ document.addEventListener('mousedown', closeOutside)
1845
+ return () => document.removeEventListener('mousedown', closeOutside)
1846
+ }, [open])
1847
+
1848
+ useEffect(() => {
1849
+ if (!open) {
1850
+ setQuery('')
1851
+ return undefined
1852
+ }
1853
+ const closeOnEscape = (event) => {
1854
+ if (event.key !== 'Escape') return
1855
+ event.preventDefault()
1856
+ setOpen(false)
1857
+ queueMicrotask(() => triggerRef.current?.focus())
1858
+ }
1859
+ document.addEventListener('keydown', closeOnEscape)
1860
+ return () => document.removeEventListener('keydown', closeOnEscape)
1861
+ }, [open])
1862
+
1863
+ useEffect(() => {
1864
+ if (locked && open) setOpen(false)
1865
+ }, [locked, open])
1866
+
1867
+ // A rejection toast clears itself; the menu is not blocked while it shows.
1868
+ useEffect(() => {
1869
+ if (toast === null) return undefined
1870
+ const timer = setTimeout(() => setToast(null), 4200)
1871
+ return () => clearTimeout(timer)
1872
+ }, [toast])
1873
+
1874
+ const rank = useMemo(() => {
1875
+ const map = new Map()
1876
+ for (const model of catalogSnap.data?.models ?? []) {
1877
+ map.set(`${model.provider}${MODEL_KEY_SEPARATOR}${model.model}`, model.order)
1878
+ }
1879
+ return map
1880
+ }, [catalogSnap.data])
1881
+
1882
+ const priceByKey = useMemo(() => {
1883
+ const map = new Map()
1884
+ for (const model of catalogSnap.data?.models ?? []) {
1885
+ map.set(`${model.provider}${MODEL_KEY_SEPARATOR}${model.model}`, model.pricing)
1886
+ }
1887
+ return map
1888
+ }, [catalogSnap.data])
1889
+
1890
+ const groups = state?.groups
1891
+ const catalogModels = catalogSnap.data?.models
1892
+ const entries = useMemo(() => {
1893
+ if (Array.isArray(groups) && groups.length > 0) {
1894
+ const list = []
1895
+ for (const [groupIndex, group] of groups.entries()) {
1896
+ for (const [modelIndex, model] of group.models.entries()) {
1897
+ list.push({ group, model, groupIndex, modelIndex })
1898
+ }
1899
+ }
1900
+ list.sort((left, right) => {
1901
+ const leftRank = rank.get(`${left.group.id}${MODEL_KEY_SEPARATOR}${left.model.id}`) ?? Number.MAX_SAFE_INTEGER
1902
+ const rightRank = rank.get(`${right.group.id}${MODEL_KEY_SEPARATOR}${right.model.id}`) ?? Number.MAX_SAFE_INTEGER
1903
+ return leftRank - rightRank || left.groupIndex - right.groupIndex || left.modelIndex - right.modelIndex
1904
+ })
1905
+ return list
1906
+ }
1907
+ return (catalogModels ?? []).map((model, index) => ({
1908
+ group: { id: model.provider, name: model.providerName },
1909
+ model: {
1910
+ id: model.model,
1911
+ name: model.name,
1912
+ ...(model.reasoning === undefined ? {} : { reasoning: model.reasoning }),
1913
+ },
1914
+ groupIndex: model.order ?? index,
1915
+ modelIndex: index,
1916
+ }))
1917
+ }, [groups, catalogModels, rank])
1918
+
1919
+ const projected = selectionProjection?.next ?? selectionProjection?.lastUsed ?? null
1920
+ const current = state?.current ?? projected
1921
+ const currentEntry = current === null
1922
+ ? undefined
1923
+ : entries.find((entry) => entry.group.id === current.provider && entry.model.id === current.model)
1924
+ const currentCatalog = current === null
1925
+ ? undefined
1926
+ : (catalogModels ?? []).find((model) => model.provider === current.provider && model.model === current.model)
1927
+ const currentReasoning = currentEntry?.model.reasoning
1928
+ const effectiveEffort = current?.reasoningEffort ?? currentReasoning?.defaultEffort
1929
+ const effortLabel = current === null
1930
+ ? undefined
1931
+ : currentReasoning === undefined
1932
+ ? state?.retainedEffort
1933
+ : effectiveEffort === undefined
1934
+ ? translate('providerDefault')
1935
+ : currentReasoning.efforts.find((effort) => effort.id === effectiveEffort)?.name ?? effectiveEffort
1936
+ const effortChoices = currentReasoning === undefined
1937
+ ? []
1938
+ : [
1939
+ ...(currentReasoning.defaultEffort === undefined
1940
+ ? [{ key: 'provider-default', effort: undefined, label: translate('providerDefault') }]
1941
+ : []),
1942
+ ...currentReasoning.efforts.map((effort) => ({
1943
+ key: `effort:${effort.id}`,
1944
+ effort: effort.id,
1945
+ label: effort.name,
1946
+ })),
1947
+ ]
1948
+
1949
+ const showSearch = entries.length > 4
1950
+ const term = query.trim().toLowerCase()
1951
+ const filtered = useMemo(() => {
1952
+ if (term === '') return entries
1953
+ return entries.filter((entry) => `${entry.model.name} ${entry.group.name}`.toLowerCase().includes(term))
1954
+ }, [entries, term])
1955
+ // Keep the roving cursor inside the visible list; a shrunk search result
1956
+ // must not leave the index pointing past the end.
1957
+ const active = filtered.length === 0 ? -1 : Math.min(Math.max(activeIndex, 0), filtered.length - 1)
1958
+
1959
+ useLayoutEffect(() => {
1960
+ if (!open) {
1961
+ setMenuPos(null)
1962
+ return undefined
1963
+ }
1964
+ const place = () => {
1965
+ const rect = triggerRef.current?.getBoundingClientRect()
1966
+ if (rect === undefined) return
1967
+ const width = Math.min(360, window.innerWidth - 24)
1968
+ const left = Math.max(12, Math.min(rect.left, window.innerWidth - width - 12))
1969
+ const spaceAbove = rect.top - 12
1970
+ const spaceBelow = window.innerHeight - rect.bottom - 12
1971
+ const openBelow = spaceBelow > spaceAbove
1972
+ const maxHeight = Math.min(480, Math.max(160, openBelow ? spaceBelow : spaceAbove))
1973
+ setMenuPos(openBelow
1974
+ ? { left, top: rect.bottom + 8, width, maxHeight }
1975
+ : { left, bottom: window.innerHeight - rect.top + 8, width, maxHeight })
1976
+ }
1977
+ place()
1978
+ window.addEventListener('scroll', place, true)
1979
+ window.addEventListener('resize', place)
1980
+ return () => {
1981
+ window.removeEventListener('scroll', place, true)
1982
+ window.removeEventListener('resize', place)
1983
+ }
1984
+ }, [open, filtered.length, state?.status])
1985
+
1986
+ useEffect(() => {
1987
+ if (!open) return undefined
1988
+ const timer = setTimeout(() => {
1989
+ if (showSearch) {
1990
+ searchRef.current?.focus()
1991
+ return
1992
+ }
1993
+ const rows = menuRef.current?.querySelectorAll('.mb-picker-row')
1994
+ const target = rows?.[active]
1995
+ if (target instanceof HTMLElement) target.focus()
1996
+ }, 0)
1997
+ return () => clearTimeout(timer)
1998
+ // Focus follows the roving cursor so arrow keys keep focus on the active row.
1999
+ }, [open, showSearch, active])
2000
+
2001
+ const loading = catalogSnap.data === null && (state === null || state.status === 'loading')
2002
+ const busy = selecting || state?.status === 'selecting'
2003
+ const currentName = currentEntry?.model.name ?? currentCatalog?.name
2004
+ const triggerModel = currentName ?? (loading ? translate('loadingModels') : translate('chooseModel'))
2005
+ const triggerAria = effortLabel === undefined ? triggerModel : `${triggerModel} · ${effortLabel}`
2006
+
2007
+ const close = (restoreFocus) => {
2008
+ setOpen(false)
2009
+ setQuery('')
2010
+ setToast(null)
2011
+ if (restoreFocus) queueMicrotask(() => triggerRef.current?.focus())
2012
+ }
2013
+
2014
+ const submit = async (selection) => {
2015
+ if (busy) return
2016
+ setSelecting(true)
2017
+ setToast(null)
2018
+ setPending(selection)
2019
+ try {
2020
+ const result = directory !== null
2021
+ ? await directory.select(selection)
2022
+ : await requestJson('/model-billing/select', {
2023
+ method: 'POST',
2024
+ headers: { 'content-type': 'application/json' },
2025
+ body: JSON.stringify({ sessionId, ...selection }),
2026
+ })
2027
+ if (result?.ok === false) {
2028
+ // A refusal is a toast: the list stays where the user left it.
2029
+ setToast(result.error?.message ?? translate('pickerEmpty'))
2030
+ return
2031
+ }
2032
+ close(true)
2033
+ } catch (error) {
2034
+ setToast(error instanceof Error ? error.message : String(error))
2035
+ } finally {
2036
+ setSelecting(false)
2037
+ setPending(null)
2038
+ }
2039
+ }
2040
+
2041
+ const chooseModel = (entry) => {
2042
+ if (current?.provider === entry.group.id && current?.model === entry.model.id) {
2043
+ close(true)
2044
+ return
2045
+ }
2046
+ void submit({ provider: entry.group.id, model: entry.model.id })
2047
+ }
2048
+
2049
+ const chooseEffort = (effort) => {
2050
+ if (current === null) return
2051
+ if (effectiveEffort === effort) {
2052
+ close(true)
2053
+ return
2054
+ }
2055
+ void submit({
2056
+ provider: current.provider,
2057
+ model: current.model,
2058
+ ...effort === undefined ? {} : { reasoningEffort: effort },
2059
+ })
2060
+ }
2061
+
2062
+ const toggle = () => {
2063
+ if (open) {
2064
+ close(true)
2065
+ return
2066
+ }
2067
+ setToast(null)
2068
+ // Open the cursor on the row the user is already using, when there is one.
2069
+ const selectedAt = filtered.findIndex((entry) => entry.group.id === current?.provider && entry.model.id === current?.model)
2070
+ setActiveIndex(selectedAt < 0 ? 0 : selectedAt)
2071
+ setOpen(true)
2072
+ if (directory !== null) directory.load().catch(() => {})
2073
+ }
2074
+
2075
+ const menuStyle = menuPos === null
2076
+ ? { left: 0, top: 0, width: 360, visibility: 'hidden' }
2077
+ : {
2078
+ left: menuPos.left,
2079
+ width: menuPos.width,
2080
+ maxHeight: menuPos.maxHeight,
2081
+ ...menuPos.top === undefined ? { bottom: menuPos.bottom } : { top: menuPos.top },
2082
+ }
2083
+
2084
+ // Arrow keys move a roving cursor through the list. Enter and Space are
2085
+ // left to the focused row itself, so a <button> keeps its native
2086
+ // activation and the menu never fires the choice twice.
2087
+ const focusRow = (index) => {
2088
+ const rows = menuRef.current?.querySelectorAll('.mb-picker-row')
2089
+ const target = rows?.[index]
2090
+ if (target instanceof HTMLElement) target.focus()
2091
+ }
2092
+ const moveActive = (delta) => {
2093
+ if (filtered.length === 0) return
2094
+ const next = Math.min(Math.max(active + delta, 0), filtered.length - 1)
2095
+ setActiveIndex(next)
2096
+ focusRow(next)
2097
+ }
2098
+ const onMenuKeyDown = (event) => {
2099
+ if (event.key === 'ArrowDown') {
2100
+ event.preventDefault()
2101
+ moveActive(1)
2102
+ return
2103
+ }
2104
+ if (event.key === 'ArrowUp') {
2105
+ event.preventDefault()
2106
+ moveActive(-1)
2107
+ return
2108
+ }
2109
+ if (event.key === 'Home') {
2110
+ event.preventDefault()
2111
+ if (filtered.length === 0) return
2112
+ setActiveIndex(0)
2113
+ focusRow(0)
2114
+ return
2115
+ }
2116
+ if (event.key === 'End') {
2117
+ event.preventDefault()
2118
+ const last = filtered.length - 1
2119
+ if (last < 0) return
2120
+ setActiveIndex(last)
2121
+ focusRow(last)
2122
+ }
2123
+ }
2124
+
2125
+ const menu = h('div', {
2126
+ ref: menuRef,
2127
+ id: `${id}-menu`,
2128
+ className: 'mb-picker-menu',
2129
+ style: menuStyle,
2130
+ role: 'dialog',
2131
+ 'aria-label': translate('pickerAria'),
2132
+ onKeyDown: onMenuKeyDown,
2133
+ },
2134
+ showSearch ? h('input', {
2135
+ ref: searchRef,
2136
+ className: 'mb-picker-search',
2137
+ type: 'search',
2138
+ value: query,
2139
+ placeholder: translate('searchModel'),
2140
+ 'aria-label': translate('searchModel'),
2141
+ onChange: (event) => {
2142
+ setQuery(event.target.value)
2143
+ // A shorter result list must not leave the cursor on a removed row.
2144
+ setActiveIndex(0)
2145
+ },
2146
+ onKeyDown: (event) => {
2147
+ // Enter picks the highlighted row; a downward arrow steps into the list.
2148
+ if (event.key === 'ArrowDown') {
2149
+ event.preventDefault()
2150
+ moveActive(1)
2151
+ return
2152
+ }
2153
+ if (event.key !== 'Enter' || filtered.length === 0 || busy) return
2154
+ event.preventDefault()
2155
+ chooseModel(filtered[active])
2156
+ },
2157
+ }) : null,
2158
+ h('div', { className: 'mb-picker-list', role: 'menu' },
2159
+ filtered.length === 0
2160
+ ? h('div', { className: 'mb-picker-empty' }, term === '' ? translate('pickerEmpty') : translate('noMatch'))
2161
+ : filtered.map((entry, index) => {
2162
+ const selected = current?.provider === entry.group.id && current?.model === entry.model.id
2163
+ const previous = filtered[index - 1]
2164
+ const showGroup = previous === undefined || previous.group.id !== entry.group.id
2165
+ const price = priceByKey.get(`${entry.group.id}${MODEL_KEY_SEPARATOR}${entry.model.id}`)
2166
+ return h(Fragment, { key: `${entry.group.id}${MODEL_KEY_SEPARATOR}${entry.model.id}` },
2167
+ showGroup ? h('div', { className: 'mb-picker-group' }, entry.group.name) : null,
2168
+ h('button', {
2169
+ type: 'button',
2170
+ role: 'menuitemradio',
2171
+ className: 'mb-picker-row',
2172
+ 'aria-checked': selected ? 'true' : 'false',
2173
+ title: entry.model.name,
2174
+ // Roving tabindex: only the cursor row is reachable by Tab.
2175
+ tabIndex: index === active ? 0 : -1,
2176
+ disabled: busy,
2177
+ onClick: () => chooseModel(entry),
2178
+ },
2179
+ h('span', { className: 'mb-picker-option-copy' },
2180
+ h('span', { className: 'mb-picker-model' }, entry.model.name),
2181
+ h(PriceLines, { pricing: price, translate })),
2182
+ h('span', { className: 'mb-picker-check' },
2183
+ pending !== null && pending.provider === entry.group.id && pending.model === entry.model.id
2184
+ ? h('span', { className: 'mb-picker-dot', 'aria-hidden': true })
2185
+ : selected
2186
+ ? h('svg', {
2187
+ viewBox: '0 0 16 16',
2188
+ width: 14,
2189
+ height: 14,
2190
+ fill: 'none',
2191
+ stroke: 'currentColor',
2192
+ strokeWidth: 1.7,
2193
+ 'aria-hidden': true,
2194
+ },
2195
+ h('path', { d: 'M3.5 8.5l3 3 6-7' }))
2196
+ : null)))
2197
+ })),
2198
+ effortChoices.length === 0 ? null : h(Fragment, null,
2199
+ h('div', { className: 'mb-picker-divider' }),
2200
+ h('div', { className: 'mb-picker-efforts' },
2201
+ h('span', { className: 'mb-picker-efforts-label' }, translate('reasoningEffort')),
2202
+ effortChoices.map((choice) => h('button', {
2203
+ type: 'button',
2204
+ key: choice.key,
2205
+ className: 'mb-picker-chip',
2206
+ 'aria-pressed': effectiveEffort === choice.effort ? 'true' : 'false',
2207
+ disabled: busy,
2208
+ onClick: () => chooseEffort(choice.effort),
2209
+ }, choice.label)))),
2210
+ busy ? h('div', { className: 'mb-picker-status' }, translate('switching')) : null,
2211
+ toast === null ? null : h('div', { className: 'mb-picker-toast', role: 'status', 'aria-live': 'polite' }, toast),
2212
+ state?.status === 'error'
2213
+ ? h('div', { className: 'mb-picker-error' }, state.error)
2214
+ : null,
2215
+ state?.status === 'error'
2216
+ ? h('button', {
2217
+ type: 'button',
2218
+ className: 'mb-btn mb-picker-retry',
2219
+ onClick: () => {
2220
+ directory.load().catch(() => {})
2221
+ },
2222
+ }, translate('pickerRetry'))
2223
+ : null)
2224
+
2225
+ return h('div', {
2226
+ className: 'mb-picker',
2227
+ ref: rootRef,
2228
+ },
2229
+ h(Style),
2230
+ h('button', {
2231
+ ref: triggerRef,
2232
+ type: 'button',
2233
+ className: 'mb-picker-trigger',
2234
+ disabled: locked,
2235
+ 'aria-label': triggerAria,
2236
+ 'aria-haspopup': 'menu',
2237
+ 'aria-controls': open ? `${id}-menu` : undefined,
2238
+ 'aria-expanded': open ? 'true' : 'false',
2239
+ 'aria-busy': busy ? 'true' : undefined,
2240
+ title: triggerAria,
2241
+ onClick: toggle,
2242
+ },
2243
+ h('svg', {
2244
+ viewBox: '0 0 16 16',
2245
+ width: 14,
2246
+ height: 14,
2247
+ fill: 'none',
2248
+ stroke: 'currentColor',
2249
+ strokeWidth: 1.3,
2250
+ 'aria-hidden': true,
2251
+ className: 'mb-picker-icon',
2252
+ },
2253
+ h('rect', { x: 2, y: 3, width: 12, height: 10, rx: 2 }),
2254
+ h('path', { d: 'M2 6.5h12M6 6.5V13' })),
2255
+ h('span', { className: 'mb-picker-name' }, triggerModel),
2256
+ effortLabel === undefined ? null : h('span', { className: 'mb-picker-trigger-effort' }, effortLabel),
2257
+ h('svg', {
2258
+ className: open ? 'mb-picker-chevron mb-picker-chevron-open' : 'mb-picker-chevron',
2259
+ viewBox: '0 0 16 16',
2260
+ width: 14,
2261
+ height: 14,
2262
+ fill: 'none',
2263
+ stroke: 'currentColor',
2264
+ strokeWidth: 1.5,
2265
+ 'aria-hidden': true,
2266
+ },
2267
+ h('path', { d: 'M4 6l4 4 4-4' }))),
2268
+ open ? ReactDOM.createPortal(menu, document.body) : null)
2269
+ }
2270
+
2271
+ function ModelPickerSeat(props) {
2272
+ const catalogSnap = useCatalog()
2273
+ // Take over only while we hold a usable catalog. On a fetch error or an
2274
+ // in-flight load, return null so the built-in picker keeps working
2275
+ // instead of leaving the user with no way to change models.
2276
+ if (catalogSnap.status !== 'ready') return null
2277
+ const display = readDisplay(catalogSnap.data)
2278
+ if (!display.takeoverPicker) return null
2279
+ return h(OrderedModelPicker, props)
2280
+ }
2281
+
2282
+ return {
2283
+ inject: ['slots', 'locale'],
2284
+ apply(ctx) {
2285
+ localeService = ctx.get('locale')
2286
+ publishModelDirectories(ctx.get('modelDirectories'))
2287
+ ctx.inject(['modelDirectories'], (scope) => {
2288
+ publishModelDirectories(scope.modelDirectories)
2289
+ let seat = null
2290
+ const takeoverEnabled = () => catalog.snap.status === 'ready' && readDisplay(catalog.snap.data).takeoverPicker !== false
2291
+ const syncSeat = () => {
2292
+ if (takeoverEnabled()) {
2293
+ if (seat === null) {
2294
+ seat = scope.slots.register({
2295
+ name: 'conversation.input.model',
2296
+ priority: -1,
2297
+ }, ModelPickerSeat)
2298
+ }
2299
+ return
2300
+ }
2301
+ if (seat !== null) {
2302
+ seat()
2303
+ seat = null
2304
+ }
2305
+ }
2306
+ scope.slots.inject('conversation.input.model', () => {
2307
+ const unsubscribe = subscribeCatalog(syncSeat)
2308
+ syncSeat()
2309
+ return () => {
2310
+ unsubscribe()
2311
+ if (seat !== null) {
2312
+ seat()
2313
+ seat = null
2314
+ }
2315
+ }
2316
+ })
2317
+ })
2318
+
2319
+ void ensureCatalog()
2320
+
2321
+ ctx.slots.inject('settings.section', () => ctx.slots.register({
2322
+ name: 'settings.section',
2323
+ id: 'model-billing',
2324
+ // Native sections run -10..20 (account, general, models, plugins,
2325
+ // agent presets); third-party pages start at 25 so they sit below
2326
+ // the native group instead of splitting it.
2327
+ order: 25,
2328
+ label: () => t('nav'),
2329
+ }, SettingsSection))
2330
+
2331
+ ctx.slots.inject('conversation.composer.dock', () => ctx.slots.register({
2332
+ name: 'conversation.composer.dock',
2333
+ id: 'model-billing-cost',
2334
+ order: 2,
2335
+ }, CostDock))
2336
+
2337
+ ctx.slots.inject('conversation.chat.turnTail', () => ctx.slots.register({
2338
+ name: 'conversation.chat.turnTail',
2339
+ id: 'model-billing-turn',
2340
+ order: 30,
2341
+ }, TurnCost))
2342
+ },
2343
+ }
2344
+ },
2345
+ })