dsh-model-allowance 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,6 +5,9 @@
5
5
  Shows the balance or remaining allowance of the provider behind the current
6
6
  conversation model.
7
7
 
8
+ The card also switches the session: the button next to the console link
9
+ lists the provider's models and moves the conversation to the one you pick.
10
+
8
11
  ## Supported
9
12
 
10
13
  - DeepSeek
@@ -16,6 +19,7 @@ conversation model.
16
19
  ### Not supported
17
20
 
18
21
  - Qwen Token Plan: the vendor publishes no balance API
22
+ - Mimo Token Plan:the vendor publishes no balance API
19
23
 
20
24
  ## Installation
21
25
 
package/README.zh.md CHANGED
@@ -4,6 +4,9 @@
4
4
 
5
5
  显示当前会话所用模型对应厂商的余额或剩余额度。
6
6
 
7
+ 卡片同时负责切换:控制台链接旁边的按钮会列出该厂商的模型,选中哪一个,
8
+ 会话就切到哪一个。
9
+
7
10
  ## 支持
8
11
 
9
12
  - DeepSeek
@@ -15,6 +18,7 @@
15
18
  ### 不支持
16
19
 
17
20
  - 千问 Token Plan:官方未提供余额接口
21
+ - Mimo Token Plan:官方未提供余额接口
18
22
 
19
23
  ## 安装
20
24
 
package/client.js CHANGED
@@ -97,8 +97,9 @@ function formatDelta(consumption) {
97
97
  if (magnitude >= 0.05) return `${magnitude.toFixed(1)}%`;
98
98
  return "";
99
99
  }
100
+ if (magnitude < 5e-5) return "";
100
101
  const value = magnitude >= 0.01 ? magnitude.toFixed(2) : magnitude.toFixed(4);
101
- return Number(value) === 0 ? "" : value;
102
+ return value;
102
103
  }
103
104
  function formatClock(epochMs) {
104
105
  const at = new Date(epochMs);
@@ -150,6 +151,40 @@ function consoleGlyph(size) {
150
151
  })
151
152
  );
152
153
  }
154
+ function modelGlyph(size) {
155
+ return (0, import_react.createElement)(
156
+ "svg",
157
+ { viewBox: "0 0 20 20", width: size, height: size, "aria-hidden": "true", focusable: "false" },
158
+ (0, import_react.createElement)("path", {
159
+ d: "M3.4 6.5h9.8",
160
+ fill: "none",
161
+ stroke: "currentColor",
162
+ strokeWidth: 1.6,
163
+ strokeLinecap: "round"
164
+ }),
165
+ (0, import_react.createElement)("path", {
166
+ d: "M13.2 3.6L16.9 6.5L13.2 9.4Z",
167
+ fill: "currentColor",
168
+ stroke: "currentColor",
169
+ strokeWidth: 0.6,
170
+ strokeLinejoin: "round"
171
+ }),
172
+ (0, import_react.createElement)("path", {
173
+ d: "M16.6 13.5H6.8",
174
+ fill: "none",
175
+ stroke: "currentColor",
176
+ strokeWidth: 1.6,
177
+ strokeLinecap: "round"
178
+ }),
179
+ (0, import_react.createElement)("path", {
180
+ d: "M6.8 10.6L3.1 13.5L6.8 16.4Z",
181
+ fill: "currentColor",
182
+ stroke: "currentColor",
183
+ strokeWidth: 0.6,
184
+ strokeLinejoin: "round"
185
+ })
186
+ );
187
+ }
153
188
  var DEEPSEEK_PATH = "M48.8354 10.0479C48.3232 9.79199 48.1025 10.2798 47.8032 10.5278C47.7007 10.6079 47.6143 10.7119 47.5273 10.8076C46.7793 11.624 45.9048 12.1597 44.7622 12.0957C43.0923 12 41.666 12.5356 40.4058 13.8398C40.1377 12.2319 39.2476 11.272 37.8926 10.6558C37.1836 10.3359 36.4668 10.0156 35.9702 9.31982C35.6235 8.82373 35.5293 8.27197 35.356 7.72754C35.2456 7.3999 35.1353 7.06396 34.7651 7.00781C34.3633 6.94385 34.2056 7.2876 34.0479 7.57568C33.418 8.75195 33.1733 10.0479 33.1973 11.3599C33.2524 14.312 34.4736 16.6641 36.8999 18.3359C37.1758 18.5278 37.2466 18.7197 37.1597 19C36.9946 19.5757 36.7974 20.1357 36.624 20.7119C36.5137 21.0801 36.3486 21.1597 35.9624 21C34.6309 20.4321 33.481 19.5918 32.4644 18.5757C30.7393 16.8721 29.1792 14.9917 27.2334 13.52C26.7764 13.1758 26.3193 12.856 25.8467 12.5518C23.8618 10.584 26.1069 8.96777 26.627 8.77588C27.1704 8.57568 26.8159 7.8877 25.0591 7.896C23.3022 7.90381 21.6953 8.50391 19.647 9.30371C19.3477 9.42383 19.0322 9.51172 18.7095 9.58398C16.8501 9.22363 14.9199 9.14355 12.9033 9.37598C9.10596 9.80762 6.07275 11.6396 3.84326 14.7681C1.16455 18.5278 0.53418 22.7998 1.30664 27.2559C2.11768 31.9521 4.46582 35.8398 8.07373 38.8799C11.8159 42.0322 16.1255 43.5762 21.041 43.2803C24.0269 43.104 27.3516 42.6963 31.1016 39.4561C32.0469 39.936 33.0396 40.1279 34.686 40.272C35.9546 40.3921 37.1758 40.208 38.1211 40.0078C39.6021 39.688 39.4995 38.2881 38.9639 38.0322C34.623 35.9678 35.5762 36.8081 34.71 36.1279C36.9155 33.4639 40.2402 30.6958 41.54 21.728C41.6426 21.0161 41.5557 20.5679 41.54 19.9917C41.5322 19.6396 41.6108 19.5039 42.0049 19.4639C43.0923 19.3359 44.1479 19.0317 45.1167 18.4878C47.9292 16.9199 49.064 14.3438 49.3315 11.2559C49.3711 10.7837 49.3237 10.2959 48.8354 10.0479ZM24.3262 37.8398C20.1196 34.4639 18.0791 33.3521 17.2358 33.3999C16.4482 33.4482 16.5898 34.3682 16.7632 34.9678C16.9443 35.5601 17.1812 35.9683 17.5117 36.4878C17.7402 36.832 17.8979 37.3442 17.2832 37.728C15.9282 38.584 13.5728 37.4399 13.4624 37.3838C10.7207 35.7358 8.42822 33.5601 6.81348 30.584C5.25342 27.7197 4.34766 24.6479 4.19775 21.3677C4.1582 20.5757 4.38672 20.2959 5.15869 20.1519C6.17529 19.96 7.22314 19.9199 8.23926 20.0718C12.5327 20.7119 16.1885 22.6719 19.2529 25.7759C21.002 27.5439 22.3252 29.6558 23.6885 31.7202C25.1377 33.9121 26.6978 36 28.6831 37.7119C29.3843 38.312 29.9434 38.7681 30.479 39.104C28.8643 39.2881 26.1699 39.3281 24.3262 37.8398ZM26.3433 24.6001C26.3433 24.248 26.6191 23.9678 26.9658 23.9678C27.0444 23.9678 27.1152 23.9839 27.1782 24.0078C27.2651 24.04 27.3438 24.0879 27.4067 24.1602C27.5171 24.272 27.5801 24.4321 27.5801 24.6001C27.5801 24.9521 27.3042 25.2319 26.9575 25.2319C26.6108 25.2319 26.3433 24.9521 26.3433 24.6001ZM32.6064 27.8799C32.2046 28.0479 31.8027 28.1919 31.4165 28.208C30.8179 28.2397 30.1641 27.9922 29.8096 27.688C29.2583 27.2158 28.8643 26.9521 28.6987 26.1279C28.6279 25.7759 28.6675 25.2319 28.7305 24.9199C28.8721 24.248 28.7144 23.8159 28.2495 23.4238C27.8716 23.104 27.3911 23.0161 26.8633 23.0161C26.666 23.0161 26.4849 22.9277 26.3511 22.856C26.1304 22.7441 25.9492 22.4639 26.1226 22.1201C26.1777 22.0078 26.4458 21.7358 26.5088 21.688C27.2256 21.272 28.0527 21.4077 28.8169 21.7197C29.5259 22.0161 30.0615 22.5601 30.834 23.3281C31.6216 24.2559 31.7632 24.5117 32.2124 25.208C32.5669 25.752 32.8901 26.312 33.1104 26.9521C33.2446 27.3521 33.0713 27.6802 32.6064 27.8799Z";
154
189
  var MINIMAX_PATH = "M17.5802 3.08155C17.5802 2.35036 16.987 1.75702 16.2589 1.75702C15.5309 1.75702 14.9377 2.35171 14.9377 3.08155V25.0348C14.9377 26.7337 13.5586 28.1164 11.8639 28.1164C10.1693 28.1164 8.79008 26.7337 8.79008 25.0348V10.9503C8.79008 10.2191 8.19688 9.62575 7.46886 9.62575C6.74085 9.62575 6.14765 10.2204 6.14765 10.9503V16.9201C6.14765 18.619 4.76846 20.0016 3.07381 20.0016C1.37915 20.0016 0 18.619 0 16.9201V14.7563C0 14.2711 0.392318 13.8778 0.876312 13.8778C1.36031 13.8778 1.75262 14.2711 1.75262 14.7563V16.9201C1.75262 17.6513 2.34579 18.2446 3.07381 18.2446C3.80182 18.2446 4.39502 17.6499 4.39502 16.9201V10.9503C4.39502 9.25138 5.77421 7.86874 7.46886 7.86874C9.16352 7.86874 10.5427 9.25138 10.5427 10.9503V25.0348C10.5427 25.766 11.1359 26.3593 11.8639 26.3593C12.5919 26.3593 13.1851 25.7646 13.1851 25.0348V16.4227V3.08155C13.1851 1.38265 14.5643 0 16.2589 0C17.9536 0 19.3328 1.38265 19.3328 3.08155V21.2599C19.3328 21.7451 18.9405 22.1384 18.4565 22.1384C17.9725 22.1384 17.5802 21.7451 17.5802 21.2599V3.08155ZM33.8405 7.86874C32.1458 7.86874 30.7666 9.25138 30.7666 10.9503V22.7155C30.7666 23.4467 30.1734 24.0401 29.4454 24.0401C28.7174 24.0401 28.1242 23.4454 28.1242 22.7155V3.08155C28.1242 1.38265 26.745 0 25.0504 0C23.3557 0 21.9766 1.38265 21.9766 3.08155V28.0042C21.9766 28.7354 21.3834 29.3287 20.6553 29.3287C19.9273 29.3287 19.3341 28.734 19.3341 28.0042V24.9186C19.3341 24.4334 18.9418 24.0401 18.4578 24.0401C17.9738 24.0401 17.5815 24.4334 17.5815 24.9186V28.0042C17.5815 29.7031 18.9607 31.0857 20.6553 31.0857C22.35 31.0857 23.7292 29.7031 23.7292 28.0042V3.08155C23.7292 2.35036 24.3224 1.75702 25.0504 1.75702C25.7784 1.75702 26.3716 2.35171 26.3716 3.08155V22.7155C26.3716 24.4144 27.7508 25.7971 29.4454 25.7971C31.1401 25.7971 32.5193 24.4144 32.5193 22.7155V10.9503C32.5193 10.2191 33.1125 9.62575 33.8405 9.62575C34.5685 9.62575 35.1617 10.2204 35.1617 10.9503V21.2599C35.1617 21.7451 35.554 22.1384 36.038 22.1384C36.522 22.1384 36.9143 21.7451 36.9143 21.2599V10.9503C36.9143 9.25138 35.5351 7.86874 33.8405 7.86874Z";
155
190
  var MINIMAX_GRADIENT = {
@@ -289,7 +324,8 @@ var zh = {
289
324
  "badge.quotaPercent": "\u5269\u4F59 {percent}%",
290
325
  "badge.quotaRatio": "{remaining} / {total}",
291
326
  "badge.quotaDuration": "\u5269\u4F59 {duration}",
292
- "badge.unsupported": "\u65E0\u4F59\u989D\u63A5\u53E3",
327
+ "badge.unsupported": "\u76EE\u524D\u65E0\u4F59\u989D\u63A5\u53E3",
328
+ "badge.unsupportedWithName": "{name} \u76EE\u524D\u65E0\u4F59\u989D\u63A5\u53E3",
293
329
  "badge.unconfigured": "\u672A\u914D\u7F6E\u51ED\u8BC1",
294
330
  "badge.failed": "\u65E0\u6CD5\u83B7\u53D6",
295
331
  "detail.title": "\u5382\u5546\u4F59\u989D",
@@ -303,6 +339,16 @@ var zh = {
303
339
  "detail.console": "\u63A7\u5236\u53F0",
304
340
  "detail.error": "\u65E0\u6CD5\u8BFB\u53D6\u5382\u5546\u4F59\u989D\u3002",
305
341
  "detail.moreProviders": "\u66F4\u591A\u5382\u5546",
342
+ "detail.switchModelHere": "\u5207\u6362 {name} \u7684\u6A21\u578B",
343
+ "detail.switchModelThere": "\u5207\u6362\u5230 {name} \u7684\u6A21\u578B",
344
+ "model.title": "\u6A21\u578B",
345
+ "model.loading": "\u52A0\u8F7D\u6A21\u578B\u2026",
346
+ "model.empty": "\u8BE5\u5382\u5546\u6682\u65E0\u53EF\u7528\u6A21\u578B",
347
+ "model.current": "\u5F53\u524D",
348
+ "model.switching": "\u5207\u6362\u4E2D\u2026",
349
+ "model.failed": "\u5207\u6362\u5931\u8D25\uFF1A{error}",
350
+ "model.loadFailed": "\u8BFB\u53D6\u6A21\u578B\u5217\u8868\u5931\u8D25\uFF1A{error}",
351
+ "model.unavailable": "\u672C\u673A\u672A\u63D0\u4F9B\u6A21\u578B\u5207\u6362\u63A5\u53E3",
306
352
  "status.operational": "\u8FD0\u884C\u6B63\u5E38",
307
353
  "status.degraded": "\u670D\u52A1\u964D\u7EA7",
308
354
  "status.partialOutage": "\u90E8\u5206\u670D\u52A1\u4E2D\u65AD",
@@ -340,6 +386,7 @@ var en = {
340
386
  "badge.quotaRatio": "{remaining} / {total}",
341
387
  "badge.quotaDuration": "{duration} left",
342
388
  "badge.unsupported": "No balance API",
389
+ "badge.unsupportedWithName": "{name} has no balance API",
343
390
  "badge.unconfigured": "No credential",
344
391
  "badge.failed": "Unavailable",
345
392
  "detail.title": "Provider balance",
@@ -353,6 +400,16 @@ var en = {
353
400
  "detail.console": "Console",
354
401
  "detail.error": "Could not read the provider balance.",
355
402
  "detail.moreProviders": "More providers",
403
+ "detail.switchModelHere": "Switch model of {name}",
404
+ "detail.switchModelThere": "Switch to a model of {name}",
405
+ "model.title": "Models",
406
+ "model.loading": "Loading models\u2026",
407
+ "model.empty": "No models available for this provider",
408
+ "model.current": "Current",
409
+ "model.switching": "Switching\u2026",
410
+ "model.failed": "Switch failed: {error}",
411
+ "model.loadFailed": "Could not read the model list: {error}",
412
+ "model.unavailable": "This host offers no model switch",
356
413
  "status.operational": "Operational",
357
414
  "status.degraded": "Degraded",
358
415
  "status.partialOutage": "Partial outage",
@@ -404,6 +461,22 @@ function statusLabel(state, t) {
404
461
  return key === void 0 ? t("status.unknown") : t(key);
405
462
  }
406
463
 
464
+ // src/client/models.js
465
+ function providerGroup(catalog, provider) {
466
+ const groups = Array.isArray(catalog?.groups) ? catalog.groups : [];
467
+ return groups.find((group) => group?.id === provider);
468
+ }
469
+ function modelsForProvider(catalog, provider) {
470
+ const group = providerGroup(catalog, provider);
471
+ const models = Array.isArray(group?.models) ? group.models : [];
472
+ return models.filter((model) => typeof model?.id === "string" && model.id !== "");
473
+ }
474
+ function currentModelFor(selection, provider) {
475
+ const next = selection?.next ?? selection?.lastUsed;
476
+ if (next?.provider !== provider) return void 0;
477
+ return typeof next.model === "string" ? next.model : void 0;
478
+ }
479
+
407
480
  // src/client/namespace.js
408
481
  var balanceNamespace = null;
409
482
  var settleBalanceNamespace;
@@ -431,6 +504,14 @@ function resetBalanceNamespace() {
431
504
  balanceNamespace = null;
432
505
  modelSupport = void 0;
433
506
  }
507
+ function lookupSessionFace(ctx) {
508
+ try {
509
+ const face = ctx.get("remote.session");
510
+ if (face !== void 0) return face;
511
+ } catch {
512
+ }
513
+ return ctx.remote?.session;
514
+ }
434
515
  async function supportsModel(api) {
435
516
  if (modelSupport !== void 0) return modelSupport;
436
517
  if (typeof api?.capabilities !== "function") {
@@ -762,6 +843,33 @@ var STYLES = [
762
843
  "line-height:1;background:0 0;",
763
844
  "color:var(--provider-mark,var(--dsw-alias-label-primary))}",
764
845
  ".dsh-model-allowance-menu-mark svg{display:block}",
846
+ // The model list is a third floating surface. It opens upwards from the
847
+ // footer button, so it carries the same menu surface and elevation as the
848
+ // card and the provider overflow menu.
849
+ ".dsh-model-allowance-model-wrap{position:relative;display:inline-flex}",
850
+ ".dsh-model-allowance-model-menu{position:absolute;bottom:calc(100% + 4px);right:0;z-index:30;",
851
+ "min-width:180px;max-width:260px;max-height:240px;overflow-y:auto;padding:4px;border:0;",
852
+ "border-radius:var(--dsw-radius-md);background:var(--dsw-specific-menu);",
853
+ "backdrop-filter:var(--dsw-menu-backdrop-filter);",
854
+ "box-shadow:var(--dsw-elevation-prominent)}",
855
+ ".dsh-model-allowance-model-item{display:flex;align-items:center;justify-content:space-between;",
856
+ "gap:10px;width:100%;padding:4px 6px;border:0;border-radius:var(--dsw-radius-xs);",
857
+ "background:0 0;color:var(--dsw-alias-label-primary);font-size:12px;text-align:left;cursor:pointer}",
858
+ ".dsh-model-allowance-model-item:hover{background:var(--dsw-alias-interactive-bg-hover)}",
859
+ // The model the session uses is dimmed rather than framed: a frame here
860
+ // would give the chip a second meaning it does not have.
861
+ '.dsh-model-allowance-model-item[data-current="true"]{color:var(--dsw-alias-label-secondary)}',
862
+ ".dsh-model-allowance-model-item[disabled]{opacity:.6;cursor:default}",
863
+ ".dsh-model-allowance-model-name{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}",
864
+ // The trailing label names the state of one model: the one in use, or the
865
+ // one being switched to.
866
+ ".dsh-model-allowance-model-note{flex:none;color:var(--dsw-alias-label-tertiary);font-size:11px}",
867
+ // A message covers the whole list: it loads, it is empty, or the switch
868
+ // failed and says why.
869
+ ".dsh-model-allowance-model-message{padding:4px 6px;color:var(--dsw-alias-label-secondary);",
870
+ "font-size:11px;word-break:break-word}",
871
+ '.dsh-model-allowance-model-message[data-tone="error"]{',
872
+ "color:var(--dsw-alias-state-error-primary)}",
765
873
  ".dsh-model-allowance-window{margin-top:10px;padding-top:10px;",
766
874
  "border-top:1px solid var(--dsw-alias-border-l1)}",
767
875
  // The first window has no boundary above it: the switcher already ends
@@ -804,6 +912,16 @@ var STYLES = [
804
912
  "color:var(--dsw-alias-label-tertiary);cursor:pointer;text-decoration:none}",
805
913
  ".dsh-model-allowance-icon-button:hover{color:var(--dsw-alias-label-secondary);",
806
914
  "border-color:var(--dsw-alias-border-l3);background:var(--dsw-alias-interactive-bg-hover)}",
915
+ // The vendor in use paints the switch glyph in that vendor's own colour,
916
+ // so the button says which vendor the card is on. The frame stays
917
+ // neutral: a coloured frame would fight the vendor marks in the strip
918
+ // above it.
919
+ '.dsh-model-allowance-icon-button[data-in-use="true"]{',
920
+ "color:var(--provider-mark,var(--dsw-alias-label-primary));",
921
+ "border-color:var(--dsw-alias-border-l3)}",
922
+ '.dsh-model-allowance-icon-button[data-in-use="true"]:hover{',
923
+ "color:var(--provider-mark,var(--dsw-alias-label-primary));",
924
+ "background:var(--dsw-alias-interactive-bg-hover)}",
807
925
  ".dsh-model-allowance-icon-button:focus-visible{",
808
926
  "outline:2px solid var(--dsw-alias-brand-primary);outline-offset:1px}",
809
927
  ".dsh-model-allowance-icon-button[disabled]{opacity:.5;cursor:default}",
@@ -963,11 +1081,16 @@ function barTone(percent) {
963
1081
  }
964
1082
 
965
1083
  // src/client/badge.js
1084
+ var CATALOG_TTL_MS = 60 * 1e3;
1085
+ function describe(error) {
1086
+ return error instanceof Error ? error.message : String(error);
1087
+ }
966
1088
  function BalanceBadge(props) {
967
1089
  const { useProjection, ctx, t } = props;
968
1090
  const selection = useProjection("modelSelection");
969
1091
  const selected = selection?.next?.provider ?? selection?.lastUsed?.provider ?? null;
970
1092
  const selectedModel = selection?.next?.model ?? selection?.lastUsed?.model ?? void 0;
1093
+ const sessionId = props.sessionId;
971
1094
  const [rawView, setView] = React.useState(null);
972
1095
  const [loading, setLoading] = React.useState(false);
973
1096
  const [open, setOpen] = React.useState(false);
@@ -978,8 +1101,11 @@ function BalanceBadge(props) {
978
1101
  const [directory, setDirectory] = React.useState([]);
979
1102
  const lastModelProvider = React.useRef(null);
980
1103
  const available = directory.filter((entry) => entry.configured);
981
- const entryOf = (route) => directory.find((entry) => entry.route === route);
982
- const nameOf = (route) => pickName(entryOf(route), route);
1104
+ const entryOf = React.useCallback(
1105
+ (route) => directory.find((entry) => entry.route === route),
1106
+ [directory]
1107
+ );
1108
+ const nameOf = React.useCallback((route) => pickName(entryOf(route), route), [entryOf]);
983
1109
  const markFor = (route) => markOf(entryOf(route), route);
984
1110
  const [menuOpen, setMenuOpen] = React.useState(false);
985
1111
  const [stripWidth, setStripWidth] = React.useState(0);
@@ -987,6 +1113,13 @@ function BalanceBadge(props) {
987
1113
  const [defaultProvider, setDefaultProvider] = React.useState(null);
988
1114
  const [providerStatus, setProviderStatus] = React.useState(null);
989
1115
  const [schedule, setSchedule] = React.useState(null);
1116
+ const [modelList, setModelList] = React.useState([]);
1117
+ const [modelListState, setModelListState] = React.useState("idle");
1118
+ const [modelError, setModelError] = React.useState(null);
1119
+ const [modelMenuOpen, setModelMenuOpen] = React.useState(false);
1120
+ const [switchingModel, setSwitchingModel] = React.useState(null);
1121
+ const modelWrapRef = React.useRef(null);
1122
+ const catalogRef = React.useRef({ value: null, at: 0 });
990
1123
  React.useEffect(() => {
991
1124
  let cancelled = false;
992
1125
  (async () => {
@@ -1032,11 +1165,19 @@ function BalanceBadge(props) {
1032
1165
  });
1033
1166
  const activeProvider = picked ?? provider;
1034
1167
  const activeModel = picked === null || picked === provider ? selectedModel : void 0;
1168
+ const inUse = activeProvider === provider;
1169
+ const switchLabel = t(inUse ? "detail.switchModelHere" : "detail.switchModelThere", {
1170
+ name: nameOf(activeProvider)
1171
+ });
1172
+ const currentModel = currentModelFor(selection, activeProvider);
1173
+ const sessionFace = lookupSessionFace(ctx);
1174
+ const canSwitch = typeof sessionId === "string" && sessionId !== "" && sessionFace !== void 0 && typeof sessionFace.selectModel === "function";
1035
1175
  const view = rawView?.provider === activeProvider ? rawView : null;
1036
1176
  React.useEffect(() => {
1037
1177
  if (open) return;
1038
1178
  setPicked(null);
1039
1179
  setMenuOpen(false);
1180
+ setModelMenuOpen(false);
1040
1181
  }, [open]);
1041
1182
  React.useEffect(() => {
1042
1183
  if (!open) return;
@@ -1057,7 +1198,12 @@ function BalanceBadge(props) {
1057
1198
  setOpen(false);
1058
1199
  };
1059
1200
  const onKeyDown = (event) => {
1060
- if (event.key === "Escape") setOpen(false);
1201
+ if (event.key !== "Escape") return;
1202
+ if (modelMenuOpen) {
1203
+ setModelMenuOpen(false);
1204
+ return;
1205
+ }
1206
+ setOpen(false);
1061
1207
  };
1062
1208
  document.addEventListener("pointerdown", onPointerDown, true);
1063
1209
  document.addEventListener("keydown", onKeyDown);
@@ -1065,7 +1211,7 @@ function BalanceBadge(props) {
1065
1211
  document.removeEventListener("pointerdown", onPointerDown, true);
1066
1212
  document.removeEventListener("keydown", onKeyDown);
1067
1213
  };
1068
- }, [open]);
1214
+ }, [open, modelMenuOpen]);
1069
1215
  const showingDifference = consumptionFreshAt(view?.consumption, now);
1070
1216
  React.useEffect(() => {
1071
1217
  if (!open && !showingDifference) return;
@@ -1253,6 +1399,101 @@ function BalanceBadge(props) {
1253
1399
  setRefreshing(false);
1254
1400
  }
1255
1401
  }, [activeProvider, activeModel]);
1402
+ const showModels = React.useCallback(
1403
+ (catalog) => {
1404
+ const models = modelsForProvider(catalog, activeProvider);
1405
+ setModelList(models);
1406
+ setModelListState(models.length > 0 ? "ready" : "empty");
1407
+ },
1408
+ [activeProvider]
1409
+ );
1410
+ React.useEffect(() => {
1411
+ if (!modelMenuOpen) return void 0;
1412
+ let cancelled = false;
1413
+ (async () => {
1414
+ const face = lookupSessionFace(ctx);
1415
+ if (face === void 0 || typeof face.modelCatalog !== "function") {
1416
+ setModelListState("error");
1417
+ setModelError(t("model.unavailable"));
1418
+ return;
1419
+ }
1420
+ const cached = catalogRef.current;
1421
+ if (cached.value !== null && Date.now() - cached.at < CATALOG_TTL_MS) {
1422
+ if (!cancelled) showModels(cached.value);
1423
+ return;
1424
+ }
1425
+ setModelListState("loading");
1426
+ try {
1427
+ const result = await face.modelCatalog();
1428
+ if (cancelled) return;
1429
+ if (!result?.ok) {
1430
+ setModelListState("error");
1431
+ setModelError(
1432
+ t("model.loadFailed", { error: result?.error?.message ?? result?.error?.code ?? "" })
1433
+ );
1434
+ return;
1435
+ }
1436
+ catalogRef.current = { value: result.value, at: Date.now() };
1437
+ showModels(result.value);
1438
+ } catch (error) {
1439
+ if (cancelled) return;
1440
+ setModelListState("error");
1441
+ setModelError(t("model.loadFailed", { error: describe(error) }));
1442
+ }
1443
+ })();
1444
+ return () => {
1445
+ cancelled = true;
1446
+ };
1447
+ }, [modelMenuOpen, ctx, t, showModels]);
1448
+ React.useEffect(() => {
1449
+ if (!modelMenuOpen) return void 0;
1450
+ const onPointerDown = (event) => {
1451
+ const wrap = modelWrapRef.current;
1452
+ if (wrap && event.target instanceof Node && wrap.contains(event.target)) return;
1453
+ setModelMenuOpen(false);
1454
+ };
1455
+ document.addEventListener("pointerdown", onPointerDown, true);
1456
+ return () => {
1457
+ document.removeEventListener("pointerdown", onPointerDown, true);
1458
+ };
1459
+ }, [modelMenuOpen]);
1460
+ const switchToModel = React.useCallback(
1461
+ async (model) => {
1462
+ const face = lookupSessionFace(ctx);
1463
+ if (face === void 0 || typeof face.selectModel !== "function") {
1464
+ setModelError(t("model.unavailable"));
1465
+ return;
1466
+ }
1467
+ if (typeof sessionId !== "string" || sessionId === "") {
1468
+ setModelError(t("model.unavailable"));
1469
+ return;
1470
+ }
1471
+ setSwitchingModel(model.id);
1472
+ try {
1473
+ const effort = model.reasoning?.defaultEffort;
1474
+ const result = await face.selectModel({
1475
+ sessionId,
1476
+ provider: activeProvider,
1477
+ model: model.id,
1478
+ ...typeof effort === "string" && effort !== "" ? { reasoningEffort: effort } : {}
1479
+ });
1480
+ if (!result?.ok) {
1481
+ setModelError(
1482
+ t("model.failed", { error: result?.error?.message ?? result?.error?.code ?? "" })
1483
+ );
1484
+ return;
1485
+ }
1486
+ setModelError(null);
1487
+ setModelMenuOpen(false);
1488
+ } catch (error) {
1489
+ console.error("[model-allowance] switching the model failed", { error });
1490
+ setModelError(t("model.failed", { error: describe(error) }));
1491
+ } finally {
1492
+ setSwitchingModel(null);
1493
+ }
1494
+ },
1495
+ [ctx, sessionId, activeProvider, t]
1496
+ );
1256
1497
  const label = React.useMemo(() => {
1257
1498
  if (!provider) return "";
1258
1499
  if (!view) return t("badge.loading");
@@ -1281,10 +1522,13 @@ function BalanceBadge(props) {
1281
1522
  }
1282
1523
  return t("badge.noData");
1283
1524
  }
1284
- if (view.status === "unsupported") return t("badge.unsupported");
1525
+ if (view.status === "unsupported") {
1526
+ const name = nameOf(activeProvider);
1527
+ return t("badge.unsupportedWithName", { name });
1528
+ }
1285
1529
  if (view.status === "unconfigured") return t("badge.unconfigured");
1286
1530
  return t("badge.failed");
1287
- }, [view, provider, t]);
1531
+ }, [view, provider, activeProvider, nameOf, t]);
1288
1532
  if (!provider) return null;
1289
1533
  const serviceState = providerStatus && providerStatus.provider === activeProvider ? providerStatus.state : "unknown";
1290
1534
  const MARK_SIZE = 26;
@@ -1545,7 +1789,11 @@ function BalanceBadge(props) {
1545
1789
  // turns "it does not work" into something actionable.
1546
1790
  view.error ? (0, import_react3.createElement)("div", { className: "dsh-model-allowance-reason" }, view.error) : null
1547
1791
  ) : null,
1548
- view?.status === "unsupported" ? (0, import_react3.createElement)("div", { className: "dsh-model-allowance-reason" }, t("badge.unsupported")) : null,
1792
+ view?.status === "unsupported" ? (0, import_react3.createElement)(
1793
+ "div",
1794
+ { className: "dsh-model-allowance-reason" },
1795
+ t("badge.unsupportedWithName", { name: nameOf(activeProvider) })
1796
+ ) : null,
1549
1797
  view?.status === "unconfigured" ? (0, import_react3.createElement)("div", { className: "dsh-model-allowance-reason" }, t("badge.unconfigured")) : null,
1550
1798
  // A vendor that prices by the wall clock gets its phase and
1551
1799
  // countdown here, above the footer.
@@ -1643,6 +1891,102 @@ function BalanceBadge(props) {
1643
1891
  "aria-label": t("detail.console")
1644
1892
  },
1645
1893
  consoleGlyph(16)
1894
+ ) : null,
1895
+ // The switch to another model of this provider. It sits to the
1896
+ // right of the console link, which is the last thing the footer
1897
+ // offered before, so the two never trade places.
1898
+ canSwitch ? (0, import_react3.createElement)(
1899
+ "span",
1900
+ { ref: modelWrapRef, className: "dsh-model-allowance-model-wrap" },
1901
+ (0, import_react3.createElement)(
1902
+ "button",
1903
+ {
1904
+ type: "button",
1905
+ className: "dsh-model-allowance-icon-button",
1906
+ "aria-haspopup": "menu",
1907
+ "aria-expanded": modelMenuOpen ? "true" : "false",
1908
+ // The vendor in use paints the glyph in its own
1909
+ // colour, so the button says which vendor the card is
1910
+ // on instead of the strip having to say it twice.
1911
+ "data-in-use": inUse ? "true" : "false",
1912
+ style: { "--provider-mark": markFor(activeProvider).color },
1913
+ title: switchLabel,
1914
+ "aria-label": switchLabel,
1915
+ onClick: () => {
1916
+ setModelError(null);
1917
+ setModelMenuOpen((value) => !value);
1918
+ }
1919
+ },
1920
+ modelGlyph(16)
1921
+ ),
1922
+ modelMenuOpen ? (0, import_react3.createElement)(
1923
+ "div",
1924
+ {
1925
+ className: "dsh-model-allowance-model-menu",
1926
+ role: "menu",
1927
+ "aria-label": t("model.title")
1928
+ },
1929
+ modelListState === "loading" ? (0, import_react3.createElement)(
1930
+ "div",
1931
+ { className: "dsh-model-allowance-model-message" },
1932
+ t("model.loading")
1933
+ ) : null,
1934
+ modelListState === "empty" ? (0, import_react3.createElement)(
1935
+ "div",
1936
+ { className: "dsh-model-allowance-model-message" },
1937
+ t("model.empty")
1938
+ ) : null,
1939
+ modelListState === "error" ? (0, import_react3.createElement)(
1940
+ "div",
1941
+ {
1942
+ className: "dsh-model-allowance-model-message",
1943
+ "data-tone": "error"
1944
+ },
1945
+ modelError ?? t("model.unavailable")
1946
+ ) : null,
1947
+ ...modelList.map((model) => {
1948
+ const isCurrent = model.id === currentModel;
1949
+ return (0, import_react3.createElement)(
1950
+ "button",
1951
+ {
1952
+ type: "button",
1953
+ key: model.id,
1954
+ role: "menuitemradio",
1955
+ "aria-checked": isCurrent ? "true" : "false",
1956
+ "data-current": isCurrent ? "true" : "false",
1957
+ className: "dsh-model-allowance-model-item",
1958
+ disabled: switchingModel !== null,
1959
+ title: model.id,
1960
+ onClick: () => {
1961
+ void switchToModel(model);
1962
+ }
1963
+ },
1964
+ (0, import_react3.createElement)(
1965
+ "span",
1966
+ { className: "dsh-model-allowance-model-name" },
1967
+ model.name ?? model.id
1968
+ ),
1969
+ switchingModel === model.id ? (0, import_react3.createElement)(
1970
+ "span",
1971
+ { className: "dsh-model-allowance-model-note" },
1972
+ t("model.switching")
1973
+ ) : isCurrent ? (0, import_react3.createElement)(
1974
+ "span",
1975
+ { className: "dsh-model-allowance-model-note" },
1976
+ t("model.current")
1977
+ ) : null
1978
+ );
1979
+ }),
1980
+ // A failed switch keeps the list open and says why.
1981
+ modelError && modelListState !== "error" ? (0, import_react3.createElement)(
1982
+ "div",
1983
+ {
1984
+ className: "dsh-model-allowance-model-message",
1985
+ "data-tone": "error"
1986
+ },
1987
+ modelError
1988
+ ) : null
1989
+ ) : null
1646
1990
  ) : null
1647
1991
  )
1648
1992
  ) : null
package/lib/balance.js CHANGED
@@ -13,6 +13,33 @@ import { findAdapter, KIND_AMOUNT, KIND_QUOTA, PROVIDER_ADAPTERS } from './provi
13
13
  /** Default lifetime of one cached reading, in milliseconds. */
14
14
  const DEFAULT_TTL_MS = 5 * 60 * 1000
15
15
 
16
+ /**
17
+ * Round a monetary amount to avoid floating-point precision artifacts.
18
+ *
19
+ * Amounts are rounded to 4 decimal places (0.0001), which is sufficient for
20
+ * currency values while eliminating IEEE 754 noise like -0.00002 that arises
21
+ * from subtraction of nearly equal floats.
22
+ *
23
+ * @param {number} value The raw computed value.
24
+ * @returns {number} The rounded value.
25
+ */
26
+ function roundAmount(value) {
27
+ return Math.round(value * 10000) / 10000
28
+ }
29
+
30
+ /**
31
+ * Round a percentage delta to avoid floating-point precision artifacts.
32
+ *
33
+ * Percentages are rounded to 2 decimal places, which is appropriate for
34
+ * plan window remaining percentages.
35
+ *
36
+ * @param {number} value The raw computed value.
37
+ * @returns {number} The rounded value.
38
+ */
39
+ function roundPercent(value) {
40
+ return Math.round(value * 100) / 100
41
+ }
42
+
16
43
  /** Timeout for one provider request, in milliseconds. */
17
44
  const REQUEST_TIMEOUT_MS = 15 * 1000
18
45
 
@@ -85,7 +112,11 @@ function withConsumption(view, before) {
85
112
  if (typeof view.amount !== 'number' || typeof before.amount !== 'number') return view
86
113
  return {
87
114
  ...view,
88
- consumption: { kind: 'amount', delta: view.amount - before.amount, at: Date.now() },
115
+ consumption: {
116
+ kind: 'amount',
117
+ delta: roundAmount(view.amount - before.amount),
118
+ at: Date.now(),
119
+ },
89
120
  }
90
121
  }
91
122
  // A plan meters several windows at once, and the one the badge speaks for is
@@ -94,12 +125,16 @@ function withConsumption(view, before) {
94
125
  // changed, so the largest movement wins.
95
126
  const moved = largestWindowMove(view, before)
96
127
  if (moved !== undefined) {
97
- return { ...view, consumption: { kind: 'percent', delta: moved, at: Date.now() } }
128
+ return { ...view, consumption: { kind: 'percent', delta: roundPercent(moved), at: Date.now() } }
98
129
  }
99
130
  if (typeof view.percent !== 'number' || typeof before.percent !== 'number') return view
100
131
  return {
101
132
  ...view,
102
- consumption: { kind: 'percent', delta: view.percent - before.percent, at: Date.now() },
133
+ consumption: {
134
+ kind: 'percent',
135
+ delta: roundPercent(view.percent - before.percent),
136
+ at: Date.now(),
137
+ },
103
138
  }
104
139
  }
105
140
 
@@ -119,7 +154,7 @@ function largestWindowMove(view, before) {
119
154
  for (const window of Array.isArray(view.windows) ? view.windows : []) {
120
155
  const was = previous.get(window.id)
121
156
  if (was === undefined || typeof window.remainingPercent !== 'number') continue
122
- const delta = window.remainingPercent - was
157
+ const delta = roundPercent(window.remainingPercent - was)
123
158
  if (largest === undefined || Math.abs(delta) > Math.abs(largest)) largest = delta
124
159
  }
125
160
  return largest