dsh-model-allowance 1.0.3 → 1.0.5

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
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
package/client.js CHANGED
@@ -151,6 +151,40 @@ function consoleGlyph(size) {
151
151
  })
152
152
  );
153
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
+ }
154
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";
155
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";
156
190
  var MINIMAX_GRADIENT = {
@@ -305,11 +339,23 @@ var zh = {
305
339
  "detail.console": "\u63A7\u5236\u53F0",
306
340
  "detail.error": "\u65E0\u6CD5\u8BFB\u53D6\u5382\u5546\u4F59\u989D\u3002",
307
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",
308
352
  "status.operational": "\u8FD0\u884C\u6B63\u5E38",
309
353
  "status.degraded": "\u670D\u52A1\u964D\u7EA7",
310
354
  "status.partialOutage": "\u90E8\u5206\u670D\u52A1\u4E2D\u65AD",
311
355
  "status.majorOutage": "\u5927\u9762\u79EF\u4E2D\u65AD",
312
356
  "status.maintenance": "\u7EF4\u62A4\u4E2D",
357
+ "status.loading": "\u72B6\u6001\u52A0\u8F7D\u4E2D\u2026",
358
+ "status.unsupported": "\u672A\u63D0\u4F9B\u72B6\u6001\u670D\u52A1",
313
359
  "status.unknown": "\u72B6\u6001\u672A\u77E5",
314
360
  "schedule.peakTitle": "\u9AD8\u5CF0\u65F6\u6BB5",
315
361
  "schedule.offPeakTitle": "\u4F4E\u8C37\u65F6\u6BB5",
@@ -356,11 +402,23 @@ var en = {
356
402
  "detail.console": "Console",
357
403
  "detail.error": "Could not read the provider balance.",
358
404
  "detail.moreProviders": "More providers",
405
+ "detail.switchModelHere": "Switch model of {name}",
406
+ "detail.switchModelThere": "Switch to a model of {name}",
407
+ "model.title": "Models",
408
+ "model.loading": "Loading models\u2026",
409
+ "model.empty": "No models available for this provider",
410
+ "model.current": "Current",
411
+ "model.switching": "Switching\u2026",
412
+ "model.failed": "Switch failed: {error}",
413
+ "model.loadFailed": "Could not read the model list: {error}",
414
+ "model.unavailable": "This host offers no model switch",
359
415
  "status.operational": "Operational",
360
416
  "status.degraded": "Degraded",
361
417
  "status.partialOutage": "Partial outage",
362
418
  "status.majorOutage": "Major outage",
363
419
  "status.maintenance": "Maintenance",
420
+ "status.loading": "Loading status\u2026",
421
+ "status.unsupported": "No status service provided",
364
422
  "status.unknown": "Status unknown",
365
423
  "schedule.peakTitle": "Peak hours",
366
424
  "schedule.offPeakTitle": "Off-peak",
@@ -400,6 +458,8 @@ var STATUS_LABEL_KEYS = {
400
458
  "partial-outage": "status.partialOutage",
401
459
  "major-outage": "status.majorOutage",
402
460
  maintenance: "status.maintenance",
461
+ loading: "status.loading",
462
+ unsupported: "status.unsupported",
403
463
  unknown: "status.unknown"
404
464
  };
405
465
  function statusLabel(state, t) {
@@ -407,6 +467,22 @@ function statusLabel(state, t) {
407
467
  return key === void 0 ? t("status.unknown") : t(key);
408
468
  }
409
469
 
470
+ // src/client/models.js
471
+ function providerGroup(catalog, provider) {
472
+ const groups = Array.isArray(catalog?.groups) ? catalog.groups : [];
473
+ return groups.find((group) => group?.id === provider);
474
+ }
475
+ function modelsForProvider(catalog, provider) {
476
+ const group = providerGroup(catalog, provider);
477
+ const models = Array.isArray(group?.models) ? group.models : [];
478
+ return models.filter((model) => typeof model?.id === "string" && model.id !== "");
479
+ }
480
+ function currentModelFor(selection, provider) {
481
+ const next = selection?.next ?? selection?.lastUsed;
482
+ if (next?.provider !== provider) return void 0;
483
+ return typeof next.model === "string" ? next.model : void 0;
484
+ }
485
+
410
486
  // src/client/namespace.js
411
487
  var balanceNamespace = null;
412
488
  var settleBalanceNamespace;
@@ -434,6 +510,14 @@ function resetBalanceNamespace() {
434
510
  balanceNamespace = null;
435
511
  modelSupport = void 0;
436
512
  }
513
+ function lookupSessionFace(ctx) {
514
+ try {
515
+ const face = ctx.get("remote.session");
516
+ if (face !== void 0) return face;
517
+ } catch {
518
+ }
519
+ return ctx.remote?.session;
520
+ }
437
521
  async function supportsModel(api) {
438
522
  if (modelSupport !== void 0) return modelSupport;
439
523
  if (typeof api?.capabilities !== "function") {
@@ -765,6 +849,33 @@ var STYLES = [
765
849
  "line-height:1;background:0 0;",
766
850
  "color:var(--provider-mark,var(--dsw-alias-label-primary))}",
767
851
  ".dsh-model-allowance-menu-mark svg{display:block}",
852
+ // The model list is a third floating surface. It opens upwards from the
853
+ // footer button, so it carries the same menu surface and elevation as the
854
+ // card and the provider overflow menu.
855
+ ".dsh-model-allowance-model-wrap{position:relative;display:inline-flex}",
856
+ ".dsh-model-allowance-model-menu{position:absolute;bottom:calc(100% + 4px);right:0;z-index:30;",
857
+ "min-width:180px;max-width:260px;max-height:240px;overflow-y:auto;padding:4px;border:0;",
858
+ "border-radius:var(--dsw-radius-md);background:var(--dsw-specific-menu);",
859
+ "backdrop-filter:var(--dsw-menu-backdrop-filter);",
860
+ "box-shadow:var(--dsw-elevation-prominent)}",
861
+ ".dsh-model-allowance-model-item{display:flex;align-items:center;justify-content:space-between;",
862
+ "gap:10px;width:100%;padding:4px 6px;border:0;border-radius:var(--dsw-radius-xs);",
863
+ "background:0 0;color:var(--dsw-alias-label-primary);font-size:12px;text-align:left;cursor:pointer}",
864
+ ".dsh-model-allowance-model-item:hover{background:var(--dsw-alias-interactive-bg-hover)}",
865
+ // The model the session uses is dimmed rather than framed: a frame here
866
+ // would give the chip a second meaning it does not have.
867
+ '.dsh-model-allowance-model-item[data-current="true"]{color:var(--dsw-alias-label-secondary)}',
868
+ ".dsh-model-allowance-model-item[disabled]{opacity:.6;cursor:default}",
869
+ ".dsh-model-allowance-model-name{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}",
870
+ // The trailing label names the state of one model: the one in use, or the
871
+ // one being switched to.
872
+ ".dsh-model-allowance-model-note{flex:none;color:var(--dsw-alias-label-tertiary);font-size:11px}",
873
+ // A message covers the whole list: it loads, it is empty, or the switch
874
+ // failed and says why.
875
+ ".dsh-model-allowance-model-message{padding:4px 6px;color:var(--dsw-alias-label-secondary);",
876
+ "font-size:11px;word-break:break-word}",
877
+ '.dsh-model-allowance-model-message[data-tone="error"]{',
878
+ "color:var(--dsw-alias-state-error-primary)}",
768
879
  ".dsh-model-allowance-window{margin-top:10px;padding-top:10px;",
769
880
  "border-top:1px solid var(--dsw-alias-border-l1)}",
770
881
  // The first window has no boundary above it: the switcher already ends
@@ -807,6 +918,16 @@ var STYLES = [
807
918
  "color:var(--dsw-alias-label-tertiary);cursor:pointer;text-decoration:none}",
808
919
  ".dsh-model-allowance-icon-button:hover{color:var(--dsw-alias-label-secondary);",
809
920
  "border-color:var(--dsw-alias-border-l3);background:var(--dsw-alias-interactive-bg-hover)}",
921
+ // The vendor in use paints the switch glyph in that vendor's own colour,
922
+ // so the button says which vendor the card is on. The frame stays
923
+ // neutral: a coloured frame would fight the vendor marks in the strip
924
+ // above it.
925
+ '.dsh-model-allowance-icon-button[data-in-use="true"]{',
926
+ "color:var(--provider-mark,var(--dsw-alias-label-primary));",
927
+ "border-color:var(--dsw-alias-border-l3)}",
928
+ '.dsh-model-allowance-icon-button[data-in-use="true"]:hover{',
929
+ "color:var(--provider-mark,var(--dsw-alias-label-primary));",
930
+ "background:var(--dsw-alias-interactive-bg-hover)}",
810
931
  ".dsh-model-allowance-icon-button:focus-visible{",
811
932
  "outline:2px solid var(--dsw-alias-brand-primary);outline-offset:1px}",
812
933
  ".dsh-model-allowance-icon-button[disabled]{opacity:.5;cursor:default}",
@@ -845,6 +966,11 @@ var STYLES = [
845
966
  ".dsh-model-allowance-status-help:focus-visible .dsh-model-allowance-status-tip{display:block}",
846
967
  ".dsh-model-allowance-status-dot{width:6px;height:6px;border-radius:50%;flex:none;",
847
968
  "background:var(--dsw-alias-state-success-primary)}",
969
+ '.dsh-model-allowance-status[data-state="loading"] .dsh-model-allowance-status-dot,',
970
+ '.dsh-model-allowance-status[data-state="unsupported"] .dsh-model-allowance-status-dot,',
971
+ '.dsh-model-allowance-status[data-state="unknown"] .dsh-model-allowance-status-dot{',
972
+ "background:var(--dsw-alias-state-idle-primary)}",
973
+ '.dsh-model-allowance-status[data-state="loading"] .dsh-model-allowance-status-dot{opacity:.5}',
848
974
  '.dsh-model-allowance-status[data-state="degraded"] .dsh-model-allowance-status-dot,',
849
975
  '.dsh-model-allowance-status[data-state="maintenance"] .dsh-model-allowance-status-dot{',
850
976
  "background:var(--dsw-alias-state-warn-primary)}",
@@ -966,11 +1092,38 @@ function barTone(percent) {
966
1092
  }
967
1093
 
968
1094
  // src/client/badge.js
1095
+ var CATALOG_TTL_MS = 60 * 1e3;
1096
+ function describe(error) {
1097
+ return error instanceof Error ? error.message : String(error);
1098
+ }
1099
+ async function readProviderStatus(provider) {
1100
+ try {
1101
+ const api = await getBalanceApi();
1102
+ if (!api || typeof api.status !== "function") {
1103
+ return { provider, state: "unknown", error: "status API unavailable" };
1104
+ }
1105
+ const result = await api.status(provider);
1106
+ if (!result.ok) {
1107
+ const error = typeof result.error?.message === "string" ? result.error.message : typeof result.error === "string" ? result.error : "status reading failed";
1108
+ return { provider, state: "unknown", error };
1109
+ }
1110
+ return result.value;
1111
+ } catch (error) {
1112
+ console.error("[model-allowance] reading provider status failed", { provider, error });
1113
+ return { provider, state: "unknown", error: describe(error) };
1114
+ }
1115
+ }
1116
+ function statusDisplayState(status) {
1117
+ if (status === null) return "loading";
1118
+ if (status.state === "unknown" && !status.url && !status.error) return "unsupported";
1119
+ return status.state;
1120
+ }
969
1121
  function BalanceBadge(props) {
970
1122
  const { useProjection, ctx, t } = props;
971
1123
  const selection = useProjection("modelSelection");
972
1124
  const selected = selection?.next?.provider ?? selection?.lastUsed?.provider ?? null;
973
1125
  const selectedModel = selection?.next?.model ?? selection?.lastUsed?.model ?? void 0;
1126
+ const sessionId = props.sessionId;
974
1127
  const [rawView, setView] = React.useState(null);
975
1128
  const [loading, setLoading] = React.useState(false);
976
1129
  const [open, setOpen] = React.useState(false);
@@ -993,6 +1146,13 @@ function BalanceBadge(props) {
993
1146
  const [defaultProvider, setDefaultProvider] = React.useState(null);
994
1147
  const [providerStatus, setProviderStatus] = React.useState(null);
995
1148
  const [schedule, setSchedule] = React.useState(null);
1149
+ const [modelList, setModelList] = React.useState([]);
1150
+ const [modelListState, setModelListState] = React.useState("idle");
1151
+ const [modelError, setModelError] = React.useState(null);
1152
+ const [modelMenuOpen, setModelMenuOpen] = React.useState(false);
1153
+ const [switchingModel, setSwitchingModel] = React.useState(null);
1154
+ const modelWrapRef = React.useRef(null);
1155
+ const catalogRef = React.useRef({ value: null, at: 0 });
996
1156
  React.useEffect(() => {
997
1157
  let cancelled = false;
998
1158
  (async () => {
@@ -1038,11 +1198,19 @@ function BalanceBadge(props) {
1038
1198
  });
1039
1199
  const activeProvider = picked ?? provider;
1040
1200
  const activeModel = picked === null || picked === provider ? selectedModel : void 0;
1201
+ const inUse = activeProvider === provider;
1202
+ const switchLabel = t(inUse ? "detail.switchModelHere" : "detail.switchModelThere", {
1203
+ name: nameOf(activeProvider)
1204
+ });
1205
+ const currentModel = currentModelFor(selection, activeProvider);
1206
+ const sessionFace = lookupSessionFace(ctx);
1207
+ const canSwitch = typeof sessionId === "string" && sessionId !== "" && sessionFace !== void 0 && typeof sessionFace.selectModel === "function";
1041
1208
  const view = rawView?.provider === activeProvider ? rawView : null;
1042
1209
  React.useEffect(() => {
1043
1210
  if (open) return;
1044
1211
  setPicked(null);
1045
1212
  setMenuOpen(false);
1213
+ setModelMenuOpen(false);
1046
1214
  }, [open]);
1047
1215
  React.useEffect(() => {
1048
1216
  if (!open) return;
@@ -1063,7 +1231,12 @@ function BalanceBadge(props) {
1063
1231
  setOpen(false);
1064
1232
  };
1065
1233
  const onKeyDown = (event) => {
1066
- if (event.key === "Escape") setOpen(false);
1234
+ if (event.key !== "Escape") return;
1235
+ if (modelMenuOpen) {
1236
+ setModelMenuOpen(false);
1237
+ return;
1238
+ }
1239
+ setOpen(false);
1067
1240
  };
1068
1241
  document.addEventListener("pointerdown", onPointerDown, true);
1069
1242
  document.addEventListener("keydown", onKeyDown);
@@ -1071,7 +1244,7 @@ function BalanceBadge(props) {
1071
1244
  document.removeEventListener("pointerdown", onPointerDown, true);
1072
1245
  document.removeEventListener("keydown", onKeyDown);
1073
1246
  };
1074
- }, [open]);
1247
+ }, [open, modelMenuOpen]);
1075
1248
  const showingDifference = consumptionFreshAt(view?.consumption, now);
1076
1249
  React.useEffect(() => {
1077
1250
  if (!open && !showingDifference) return;
@@ -1199,15 +1372,8 @@ function BalanceBadge(props) {
1199
1372
  if (!activeProvider) return void 0;
1200
1373
  let cancelled = false;
1201
1374
  const readStatus = async () => {
1202
- try {
1203
- const api = await getBalanceApi();
1204
- if (!api || typeof api.status !== "function") return;
1205
- const result = await api.status(activeProvider);
1206
- if (cancelled || !result.ok) return;
1207
- setProviderStatus(result.value);
1208
- } catch (error) {
1209
- console.error("[model-allowance] reading provider status failed", { error });
1210
- }
1375
+ const status = await readProviderStatus(activeProvider);
1376
+ if (!cancelled) setProviderStatus(status);
1211
1377
  };
1212
1378
  void readStatus();
1213
1379
  const timer = setInterval(readStatus, STATUS_REFRESH_MS);
@@ -1245,10 +1411,8 @@ function BalanceBadge(props) {
1245
1411
  result = await api.refresh(activeProvider, void 0);
1246
1412
  }
1247
1413
  if (result.ok) setView(result.value);
1248
- if (typeof api.status === "function") {
1249
- const status = await api.status(activeProvider);
1250
- if (status.ok) setProviderStatus(status.value);
1251
- }
1414
+ const status = await readProviderStatus(activeProvider);
1415
+ setProviderStatus(status);
1252
1416
  } catch (error) {
1253
1417
  console.error("[model-allowance] refresh failed", {
1254
1418
  provider: activeProvider,
@@ -1259,6 +1423,101 @@ function BalanceBadge(props) {
1259
1423
  setRefreshing(false);
1260
1424
  }
1261
1425
  }, [activeProvider, activeModel]);
1426
+ const showModels = React.useCallback(
1427
+ (catalog) => {
1428
+ const models = modelsForProvider(catalog, activeProvider);
1429
+ setModelList(models);
1430
+ setModelListState(models.length > 0 ? "ready" : "empty");
1431
+ },
1432
+ [activeProvider]
1433
+ );
1434
+ React.useEffect(() => {
1435
+ if (!modelMenuOpen) return void 0;
1436
+ let cancelled = false;
1437
+ (async () => {
1438
+ const face = lookupSessionFace(ctx);
1439
+ if (face === void 0 || typeof face.modelCatalog !== "function") {
1440
+ setModelListState("error");
1441
+ setModelError(t("model.unavailable"));
1442
+ return;
1443
+ }
1444
+ const cached = catalogRef.current;
1445
+ if (cached.value !== null && Date.now() - cached.at < CATALOG_TTL_MS) {
1446
+ if (!cancelled) showModels(cached.value);
1447
+ return;
1448
+ }
1449
+ setModelListState("loading");
1450
+ try {
1451
+ const result = await face.modelCatalog();
1452
+ if (cancelled) return;
1453
+ if (!result?.ok) {
1454
+ setModelListState("error");
1455
+ setModelError(
1456
+ t("model.loadFailed", { error: result?.error?.message ?? result?.error?.code ?? "" })
1457
+ );
1458
+ return;
1459
+ }
1460
+ catalogRef.current = { value: result.value, at: Date.now() };
1461
+ showModels(result.value);
1462
+ } catch (error) {
1463
+ if (cancelled) return;
1464
+ setModelListState("error");
1465
+ setModelError(t("model.loadFailed", { error: describe(error) }));
1466
+ }
1467
+ })();
1468
+ return () => {
1469
+ cancelled = true;
1470
+ };
1471
+ }, [modelMenuOpen, ctx, t, showModels]);
1472
+ React.useEffect(() => {
1473
+ if (!modelMenuOpen) return void 0;
1474
+ const onPointerDown = (event) => {
1475
+ const wrap = modelWrapRef.current;
1476
+ if (wrap && event.target instanceof Node && wrap.contains(event.target)) return;
1477
+ setModelMenuOpen(false);
1478
+ };
1479
+ document.addEventListener("pointerdown", onPointerDown, true);
1480
+ return () => {
1481
+ document.removeEventListener("pointerdown", onPointerDown, true);
1482
+ };
1483
+ }, [modelMenuOpen]);
1484
+ const switchToModel = React.useCallback(
1485
+ async (model) => {
1486
+ const face = lookupSessionFace(ctx);
1487
+ if (face === void 0 || typeof face.selectModel !== "function") {
1488
+ setModelError(t("model.unavailable"));
1489
+ return;
1490
+ }
1491
+ if (typeof sessionId !== "string" || sessionId === "") {
1492
+ setModelError(t("model.unavailable"));
1493
+ return;
1494
+ }
1495
+ setSwitchingModel(model.id);
1496
+ try {
1497
+ const effort = model.reasoning?.defaultEffort;
1498
+ const result = await face.selectModel({
1499
+ sessionId,
1500
+ provider: activeProvider,
1501
+ model: model.id,
1502
+ ...typeof effort === "string" && effort !== "" ? { reasoningEffort: effort } : {}
1503
+ });
1504
+ if (!result?.ok) {
1505
+ setModelError(
1506
+ t("model.failed", { error: result?.error?.message ?? result?.error?.code ?? "" })
1507
+ );
1508
+ return;
1509
+ }
1510
+ setModelError(null);
1511
+ setModelMenuOpen(false);
1512
+ } catch (error) {
1513
+ console.error("[model-allowance] switching the model failed", { error });
1514
+ setModelError(t("model.failed", { error: describe(error) }));
1515
+ } finally {
1516
+ setSwitchingModel(null);
1517
+ }
1518
+ },
1519
+ [ctx, sessionId, activeProvider, t]
1520
+ );
1262
1521
  const label = React.useMemo(() => {
1263
1522
  if (!provider) return "";
1264
1523
  if (!view) return t("badge.loading");
@@ -1295,7 +1554,8 @@ function BalanceBadge(props) {
1295
1554
  return t("badge.failed");
1296
1555
  }, [view, provider, activeProvider, nameOf, t]);
1297
1556
  if (!provider) return null;
1298
- const serviceState = providerStatus && providerStatus.provider === activeProvider ? providerStatus.state : "unknown";
1557
+ const currentStatus = providerStatus?.provider === activeProvider ? providerStatus : null;
1558
+ const serviceState = statusDisplayState(currentStatus);
1299
1559
  const MARK_SIZE = 26;
1300
1560
  const MARK_GAP = 4;
1301
1561
  const MENU_SIZE = 26;
@@ -1569,16 +1829,16 @@ function BalanceBadge(props) {
1569
1829
  // The platform status sits at the left edge of the footer, on
1570
1830
  // the same line as the reading time: the card gains no extra
1571
1831
  // row, and the state and the source page stay together.
1572
- serviceState !== "unknown" ? (0, import_react3.createElement)(
1832
+ (0, import_react3.createElement)(
1573
1833
  "span",
1574
1834
  { className: "dsh-model-allowance-status-wrap" },
1575
1835
  (0, import_react3.createElement)(
1576
- providerStatus.url ? "a" : "span",
1836
+ currentStatus?.url ? "a" : "span",
1577
1837
  {
1578
1838
  className: "dsh-model-allowance-status",
1579
- "data-state": providerStatus.state,
1580
- ...providerStatus.url ? {
1581
- href: providerStatus.url,
1839
+ "data-state": serviceState,
1840
+ ...currentStatus?.url ? {
1841
+ href: currentStatus.url,
1582
1842
  target: "_blank",
1583
1843
  rel: "noreferrer noopener"
1584
1844
  } : {}
@@ -1590,9 +1850,9 @@ function BalanceBadge(props) {
1590
1850
  (0, import_react3.createElement)(
1591
1851
  "span",
1592
1852
  { className: "dsh-model-allowance-status-label" },
1593
- statusLabel(providerStatus.state, t)
1853
+ statusLabel(serviceState, t)
1594
1854
  ),
1595
- ...providerStatus.url ? [
1855
+ ...currentStatus?.url ? [
1596
1856
  (0, import_react3.createElement)(
1597
1857
  "span",
1598
1858
  {
@@ -1606,13 +1866,13 @@ function BalanceBadge(props) {
1606
1866
  // The reason is a warning mark rather than another line:
1607
1867
  // the footer stays one row, and the detail is one hover
1608
1868
  // — or one keyboard focus — away.
1609
- providerStatus.summary ? (0, import_react3.createElement)(
1869
+ currentStatus?.summary ? (0, import_react3.createElement)(
1610
1870
  "span",
1611
1871
  {
1612
1872
  className: "dsh-model-allowance-status-help",
1613
1873
  tabIndex: 0,
1614
1874
  role: "note",
1615
- "aria-label": providerStatus.summary
1875
+ "aria-label": currentStatus.summary
1616
1876
  },
1617
1877
  (0, import_react3.createElement)(
1618
1878
  "span",
@@ -1624,10 +1884,10 @@ function BalanceBadge(props) {
1624
1884
  (0, import_react3.createElement)(
1625
1885
  "span",
1626
1886
  { className: "dsh-model-allowance-status-tip", role: "tooltip" },
1627
- providerStatus.summary
1887
+ currentStatus.summary
1628
1888
  )
1629
1889
  ) : null
1630
- ) : null,
1890
+ ),
1631
1891
  view?.updatedAt ? (0, import_react3.createElement)(
1632
1892
  "span",
1633
1893
  { className: "dsh-model-allowance-updated" },
@@ -1656,6 +1916,102 @@ function BalanceBadge(props) {
1656
1916
  "aria-label": t("detail.console")
1657
1917
  },
1658
1918
  consoleGlyph(16)
1919
+ ) : null,
1920
+ // The switch to another model of this provider. It sits to the
1921
+ // right of the console link, which is the last thing the footer
1922
+ // offered before, so the two never trade places.
1923
+ canSwitch ? (0, import_react3.createElement)(
1924
+ "span",
1925
+ { ref: modelWrapRef, className: "dsh-model-allowance-model-wrap" },
1926
+ (0, import_react3.createElement)(
1927
+ "button",
1928
+ {
1929
+ type: "button",
1930
+ className: "dsh-model-allowance-icon-button",
1931
+ "aria-haspopup": "menu",
1932
+ "aria-expanded": modelMenuOpen ? "true" : "false",
1933
+ // The vendor in use paints the glyph in its own
1934
+ // colour, so the button says which vendor the card is
1935
+ // on instead of the strip having to say it twice.
1936
+ "data-in-use": inUse ? "true" : "false",
1937
+ style: { "--provider-mark": markFor(activeProvider).color },
1938
+ title: switchLabel,
1939
+ "aria-label": switchLabel,
1940
+ onClick: () => {
1941
+ setModelError(null);
1942
+ setModelMenuOpen((value) => !value);
1943
+ }
1944
+ },
1945
+ modelGlyph(16)
1946
+ ),
1947
+ modelMenuOpen ? (0, import_react3.createElement)(
1948
+ "div",
1949
+ {
1950
+ className: "dsh-model-allowance-model-menu",
1951
+ role: "menu",
1952
+ "aria-label": t("model.title")
1953
+ },
1954
+ modelListState === "loading" ? (0, import_react3.createElement)(
1955
+ "div",
1956
+ { className: "dsh-model-allowance-model-message" },
1957
+ t("model.loading")
1958
+ ) : null,
1959
+ modelListState === "empty" ? (0, import_react3.createElement)(
1960
+ "div",
1961
+ { className: "dsh-model-allowance-model-message" },
1962
+ t("model.empty")
1963
+ ) : null,
1964
+ modelListState === "error" ? (0, import_react3.createElement)(
1965
+ "div",
1966
+ {
1967
+ className: "dsh-model-allowance-model-message",
1968
+ "data-tone": "error"
1969
+ },
1970
+ modelError ?? t("model.unavailable")
1971
+ ) : null,
1972
+ ...modelList.map((model) => {
1973
+ const isCurrent = model.id === currentModel;
1974
+ return (0, import_react3.createElement)(
1975
+ "button",
1976
+ {
1977
+ type: "button",
1978
+ key: model.id,
1979
+ role: "menuitemradio",
1980
+ "aria-checked": isCurrent ? "true" : "false",
1981
+ "data-current": isCurrent ? "true" : "false",
1982
+ className: "dsh-model-allowance-model-item",
1983
+ disabled: switchingModel !== null,
1984
+ title: model.id,
1985
+ onClick: () => {
1986
+ void switchToModel(model);
1987
+ }
1988
+ },
1989
+ (0, import_react3.createElement)(
1990
+ "span",
1991
+ { className: "dsh-model-allowance-model-name" },
1992
+ model.name ?? model.id
1993
+ ),
1994
+ switchingModel === model.id ? (0, import_react3.createElement)(
1995
+ "span",
1996
+ { className: "dsh-model-allowance-model-note" },
1997
+ t("model.switching")
1998
+ ) : isCurrent ? (0, import_react3.createElement)(
1999
+ "span",
2000
+ { className: "dsh-model-allowance-model-note" },
2001
+ t("model.current")
2002
+ ) : null
2003
+ );
2004
+ }),
2005
+ // A failed switch keeps the list open and says why.
2006
+ modelError && modelListState !== "error" ? (0, import_react3.createElement)(
2007
+ "div",
2008
+ {
2009
+ className: "dsh-model-allowance-model-message",
2010
+ "data-tone": "error"
2011
+ },
2012
+ modelError
2013
+ ) : null
2014
+ ) : null
1659
2015
  ) : null
1660
2016
  )
1661
2017
  ) : null
package/lib/balance.js CHANGED
@@ -112,7 +112,11 @@ function withConsumption(view, before) {
112
112
  if (typeof view.amount !== 'number' || typeof before.amount !== 'number') return view
113
113
  return {
114
114
  ...view,
115
- consumption: { kind: 'amount', delta: roundAmount(view.amount - before.amount), at: Date.now() },
115
+ consumption: {
116
+ kind: 'amount',
117
+ delta: roundAmount(view.amount - before.amount),
118
+ at: Date.now(),
119
+ },
116
120
  }
117
121
  }
118
122
  // A plan meters several windows at once, and the one the badge speaks for is
@@ -126,7 +130,11 @@ function withConsumption(view, before) {
126
130
  if (typeof view.percent !== 'number' || typeof before.percent !== 'number') return view
127
131
  return {
128
132
  ...view,
129
- consumption: { kind: 'percent', delta: roundPercent(view.percent - before.percent), at: Date.now() },
133
+ consumption: {
134
+ kind: 'percent',
135
+ delta: roundPercent(view.percent - before.percent),
136
+ at: Date.now(),
137
+ },
130
138
  }
131
139
  }
132
140
 
package/lib/status.js CHANGED
@@ -90,8 +90,9 @@ export class StatusService {
90
90
  * Read one provider's platform status.
91
91
  *
92
92
  * A route whose vendor publishes no status page answers `unknown` without
93
- * touching the network: the browser hides the row, and a route that later
94
- * gains a source starts working without any other change.
93
+ * touching the network: the browser presents that source-free reading as
94
+ * "no status service", and a route that later gains a source starts working
95
+ * without any other change.
95
96
  *
96
97
  * @param {string} provider Provider route.
97
98
  * @returns {Promise<StatusView>} The reading.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-model-allowance",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "type": "module",
5
5
  "description": "Shows the current conversation model's provider balance or remaining allowance in the composer.",
6
6
  "license": "MIT",