dsh-model-allowance 1.0.5 → 1.0.6

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
@@ -8,6 +8,13 @@ conversation model.
8
8
  The card also switches the session: the button next to the console link
9
9
  lists the provider's models and moves the conversation to the one you pick.
10
10
 
11
+ ## Usage
12
+
13
+ ![Usage demo](docs/screenshots/demo.gif)
14
+
15
+ The card shows the provider's remaining balance or allowance and the model
16
+ of the current conversation, next to the input box.
17
+
11
18
  ## Supported
12
19
 
13
20
  - DeepSeek
package/README.zh.md CHANGED
@@ -7,6 +7,12 @@
7
7
  卡片同时负责切换:控制台链接旁边的按钮会列出该厂商的模型,选中哪一个,
8
8
  会话就切到哪一个。
9
9
 
10
+ ## 使用
11
+
12
+ ![使用演示](docs/screenshots/demo.gif)
13
+
14
+ 卡片显示厂商的余额或剩余额度,以及当前会话所用的模型,位于输入框旁。
15
+
10
16
  ## 支持
11
17
 
12
18
  - DeepSeek
package/client.js CHANGED
@@ -339,12 +339,13 @@ var zh = {
339
339
  "detail.console": "\u63A7\u5236\u53F0",
340
340
  "detail.error": "\u65E0\u6CD5\u8BFB\u53D6\u5382\u5546\u4F59\u989D\u3002",
341
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",
342
+ "detail.switchModelHere": "\u5207\u6362 {name}",
343
+ "detail.switchModelThere": "\u5207\u6362\u5230 {name}",
344
344
  "model.title": "\u6A21\u578B",
345
345
  "model.loading": "\u52A0\u8F7D\u6A21\u578B\u2026",
346
346
  "model.empty": "\u8BE5\u5382\u5546\u6682\u65E0\u53EF\u7528\u6A21\u578B",
347
347
  "model.current": "\u5F53\u524D",
348
+ "model.effortDefault": "\u9ED8\u8BA4",
348
349
  "model.switching": "\u5207\u6362\u4E2D\u2026",
349
350
  "model.failed": "\u5207\u6362\u5931\u8D25\uFF1A{error}",
350
351
  "model.loadFailed": "\u8BFB\u53D6\u6A21\u578B\u5217\u8868\u5931\u8D25\uFF1A{error}",
@@ -402,12 +403,13 @@ var en = {
402
403
  "detail.console": "Console",
403
404
  "detail.error": "Could not read the provider balance.",
404
405
  "detail.moreProviders": "More providers",
405
- "detail.switchModelHere": "Switch model of {name}",
406
- "detail.switchModelThere": "Switch to a model of {name}",
406
+ "detail.switchModelHere": "Switch within {name}",
407
+ "detail.switchModelThere": "Switch to {name}",
407
408
  "model.title": "Models",
408
409
  "model.loading": "Loading models\u2026",
409
410
  "model.empty": "No models available for this provider",
410
411
  "model.current": "Current",
412
+ "model.effortDefault": "Default",
411
413
  "model.switching": "Switching\u2026",
412
414
  "model.failed": "Switch failed: {error}",
413
415
  "model.loadFailed": "Could not read the model list: {error}",
@@ -477,6 +479,30 @@ function modelsForProvider(catalog, provider) {
477
479
  const models = Array.isArray(group?.models) ? group.models : [];
478
480
  return models.filter((model) => typeof model?.id === "string" && model.id !== "");
479
481
  }
482
+ function providerModelGroup(catalog, routes) {
483
+ const seen = /* @__PURE__ */ new Set();
484
+ for (const route of routes) {
485
+ if (typeof route !== "string" || route === "" || seen.has(route)) continue;
486
+ seen.add(route);
487
+ const models = modelsForProvider(catalog, route);
488
+ if (models.length > 0) return { provider: route, models };
489
+ }
490
+ return void 0;
491
+ }
492
+ function effortChoices(model) {
493
+ const reasoning = model?.reasoning;
494
+ const efforts = Array.isArray(reasoning?.efforts) ? reasoning.efforts : [];
495
+ if (efforts.length === 0) return null;
496
+ const entries = efforts.filter((effort) => typeof effort?.id === "string" && effort.id !== "").map((effort) => ({
497
+ id: effort.id,
498
+ name: typeof effort.name === "string" && effort.name !== "" ? effort.name : effort.id,
499
+ isDefault: effort.id === reasoning.defaultEffort
500
+ }));
501
+ if (reasoning.defaultEffort === void 0) {
502
+ entries.unshift({ id: void 0, providerDefault: true });
503
+ }
504
+ return entries;
505
+ }
480
506
  function currentModelFor(selection, provider) {
481
507
  const next = selection?.next ?? selection?.lastUsed;
482
508
  if (next?.provider !== provider) return void 0;
@@ -867,6 +893,28 @@ var STYLES = [
867
893
  '.dsh-model-allowance-model-item[data-current="true"]{color:var(--dsw-alias-label-secondary)}',
868
894
  ".dsh-model-allowance-model-item[disabled]{opacity:.6;cursor:default}",
869
895
  ".dsh-model-allowance-model-name{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}",
896
+ // The trailing side of a model row carries its state label and, for a
897
+ // model with reasoning efforts, the arrow that says a flyout waits to
898
+ // the right.
899
+ ".dsh-model-allowance-model-side{display:inline-flex;align-items:center;gap:4px;flex:none}",
900
+ ".dsh-model-allowance-model-arrow{color:var(--dsw-alias-label-tertiary);font-size:12px;",
901
+ "line-height:1}",
902
+ // The effort flyout opens to the right of the list, on the row the
903
+ // pointer rests on, as a fourth floating surface of the card.
904
+ ".dsh-model-allowance-effort-menu{position:absolute;left:calc(100% + 4px);z-index:31;",
905
+ "display:flex;flex-direction:column;min-width:130px;max-width:220px;padding:4px;border:0;",
906
+ "border-radius:var(--dsw-radius-md);background:var(--dsw-specific-menu);",
907
+ "backdrop-filter:var(--dsw-menu-backdrop-filter);",
908
+ "box-shadow:var(--dsw-elevation-prominent)}",
909
+ ".dsh-model-allowance-effort-item{display:flex;align-items:center;justify-content:space-between;",
910
+ "gap:10px;width:100%;padding:3px 6px;border:0;border-radius:var(--dsw-radius-xs);",
911
+ "background:0 0;color:var(--dsw-alias-label-primary);font-size:12px;text-align:left;cursor:pointer}",
912
+ ".dsh-model-allowance-effort-item:hover{background:var(--dsw-alias-interactive-bg-hover)}",
913
+ // The provider's own default effort is dimmed like the model in use, so
914
+ // the open submenu points at it before anything is picked.
915
+ '.dsh-model-allowance-effort-item[data-default="true"],',
916
+ '.dsh-model-allowance-effort-item[data-current="true"]{color:var(--dsw-alias-label-secondary)}',
917
+ ".dsh-model-allowance-effort-item[disabled]{opacity:.6;cursor:default}",
870
918
  // The trailing label names the state of one model: the one in use, or the
871
919
  // one being switched to.
872
920
  ".dsh-model-allowance-model-note{flex:none;color:var(--dsw-alias-label-tertiary);font-size:11px}",
@@ -1123,6 +1171,7 @@ function BalanceBadge(props) {
1123
1171
  const selection = useProjection("modelSelection");
1124
1172
  const selected = selection?.next?.provider ?? selection?.lastUsed?.provider ?? null;
1125
1173
  const selectedModel = selection?.next?.model ?? selection?.lastUsed?.model ?? void 0;
1174
+ const selectedEffort = selection?.next?.reasoningEffort ?? selection?.lastUsed?.reasoningEffort ?? void 0;
1126
1175
  const sessionId = props.sessionId;
1127
1176
  const [rawView, setView] = React.useState(null);
1128
1177
  const [loading, setLoading] = React.useState(false);
@@ -1147,9 +1196,13 @@ function BalanceBadge(props) {
1147
1196
  const [providerStatus, setProviderStatus] = React.useState(null);
1148
1197
  const [schedule, setSchedule] = React.useState(null);
1149
1198
  const [modelList, setModelList] = React.useState([]);
1199
+ const [modelListRoute, setModelListRoute] = React.useState(null);
1200
+ const [catalogProvider, setCatalogProvider] = React.useState(null);
1150
1201
  const [modelListState, setModelListState] = React.useState("idle");
1151
1202
  const [modelError, setModelError] = React.useState(null);
1152
1203
  const [modelMenuOpen, setModelMenuOpen] = React.useState(false);
1204
+ const [hoverModel, setHoverModel] = React.useState(null);
1205
+ const hoverModelTimer = React.useRef(null);
1153
1206
  const [switchingModel, setSwitchingModel] = React.useState(null);
1154
1207
  const modelWrapRef = React.useRef(null);
1155
1208
  const catalogRef = React.useRef({ value: null, at: 0 });
@@ -1202,7 +1255,10 @@ function BalanceBadge(props) {
1202
1255
  const switchLabel = t(inUse ? "detail.switchModelHere" : "detail.switchModelThere", {
1203
1256
  name: nameOf(activeProvider)
1204
1257
  });
1205
- const currentModel = currentModelFor(selection, activeProvider);
1258
+ const modelProvider = catalogProvider ?? activeProvider;
1259
+ const currentModel = currentModelFor(selection, modelProvider);
1260
+ const shownModelList = modelListRoute === activeProvider ? modelList : [];
1261
+ const shownModelListState = modelListRoute === activeProvider ? modelListState : "loading";
1206
1262
  const sessionFace = lookupSessionFace(ctx);
1207
1263
  const canSwitch = typeof sessionId === "string" && sessionId !== "" && sessionFace !== void 0 && typeof sessionFace.selectModel === "function";
1208
1264
  const view = rawView?.provider === activeProvider ? rawView : null;
@@ -1425,11 +1481,15 @@ function BalanceBadge(props) {
1425
1481
  }, [activeProvider, activeModel]);
1426
1482
  const showModels = React.useCallback(
1427
1483
  (catalog) => {
1428
- const models = modelsForProvider(catalog, activeProvider);
1484
+ const routes = [activeProvider, ...entryOf(activeProvider)?.modelRoutes ?? []];
1485
+ const group = providerModelGroup(catalog, routes);
1486
+ const models = group?.models ?? [];
1487
+ setCatalogProvider(group?.provider ?? null);
1488
+ setModelListRoute(activeProvider);
1429
1489
  setModelList(models);
1430
1490
  setModelListState(models.length > 0 ? "ready" : "empty");
1431
1491
  },
1432
- [activeProvider]
1492
+ [activeProvider, entryOf]
1433
1493
  );
1434
1494
  React.useEffect(() => {
1435
1495
  if (!modelMenuOpen) return void 0;
@@ -1482,7 +1542,7 @@ function BalanceBadge(props) {
1482
1542
  };
1483
1543
  }, [modelMenuOpen]);
1484
1544
  const switchToModel = React.useCallback(
1485
- async (model) => {
1545
+ async (model, effort) => {
1486
1546
  const face = lookupSessionFace(ctx);
1487
1547
  if (face === void 0 || typeof face.selectModel !== "function") {
1488
1548
  setModelError(t("model.unavailable"));
@@ -1494,10 +1554,9 @@ function BalanceBadge(props) {
1494
1554
  }
1495
1555
  setSwitchingModel(model.id);
1496
1556
  try {
1497
- const effort = model.reasoning?.defaultEffort;
1498
1557
  const result = await face.selectModel({
1499
1558
  sessionId,
1500
- provider: activeProvider,
1559
+ provider: catalogProvider ?? activeProvider,
1501
1560
  model: model.id,
1502
1561
  ...typeof effort === "string" && effort !== "" ? { reasoningEffort: effort } : {}
1503
1562
  });
@@ -1516,8 +1575,18 @@ function BalanceBadge(props) {
1516
1575
  setSwitchingModel(null);
1517
1576
  }
1518
1577
  },
1519
- [ctx, sessionId, activeProvider, t]
1578
+ [ctx, sessionId, activeProvider, catalogProvider, t]
1520
1579
  );
1580
+ const showEffortFlyout = React.useCallback((model, top) => {
1581
+ clearTimeout(hoverModelTimer.current);
1582
+ setHoverModel({ model, top });
1583
+ }, []);
1584
+ const scheduleEffortFlyoutClose = React.useCallback((id) => {
1585
+ clearTimeout(hoverModelTimer.current);
1586
+ hoverModelTimer.current = setTimeout(() => {
1587
+ setHoverModel((current) => current?.model?.id === id ? null : current);
1588
+ }, 150);
1589
+ }, []);
1521
1590
  const label = React.useMemo(() => {
1522
1591
  if (!provider) return "";
1523
1592
  if (!view) return t("badge.loading");
@@ -1939,6 +2008,8 @@ function BalanceBadge(props) {
1939
2008
  "aria-label": switchLabel,
1940
2009
  onClick: () => {
1941
2010
  setModelError(null);
2011
+ clearTimeout(hoverModelTimer.current);
2012
+ setHoverModel(null);
1942
2013
  setModelMenuOpen((value) => !value);
1943
2014
  }
1944
2015
  },
@@ -1951,17 +2022,17 @@ function BalanceBadge(props) {
1951
2022
  role: "menu",
1952
2023
  "aria-label": t("model.title")
1953
2024
  },
1954
- modelListState === "loading" ? (0, import_react3.createElement)(
2025
+ shownModelListState === "loading" ? (0, import_react3.createElement)(
1955
2026
  "div",
1956
2027
  { className: "dsh-model-allowance-model-message" },
1957
2028
  t("model.loading")
1958
2029
  ) : null,
1959
- modelListState === "empty" ? (0, import_react3.createElement)(
2030
+ shownModelListState === "empty" ? (0, import_react3.createElement)(
1960
2031
  "div",
1961
2032
  { className: "dsh-model-allowance-model-message" },
1962
2033
  t("model.empty")
1963
2034
  ) : null,
1964
- modelListState === "error" ? (0, import_react3.createElement)(
2035
+ shownModelListState === "error" ? (0, import_react3.createElement)(
1965
2036
  "div",
1966
2037
  {
1967
2038
  className: "dsh-model-allowance-model-message",
@@ -1969,8 +2040,10 @@ function BalanceBadge(props) {
1969
2040
  },
1970
2041
  modelError ?? t("model.unavailable")
1971
2042
  ) : null,
1972
- ...modelList.map((model) => {
2043
+ ...shownModelList.map((model) => {
1973
2044
  const isCurrent = model.id === currentModel;
2045
+ const efforts = effortChoices(model);
2046
+ const defaultEffortId = efforts?.find((effort) => effort.isDefault)?.id;
1974
2047
  return (0, import_react3.createElement)(
1975
2048
  "button",
1976
2049
  {
@@ -1982,8 +2055,20 @@ function BalanceBadge(props) {
1982
2055
  className: "dsh-model-allowance-model-item",
1983
2056
  disabled: switchingModel !== null,
1984
2057
  title: model.id,
2058
+ ...efforts === null ? {} : { "aria-haspopup": "menu" },
1985
2059
  onClick: () => {
1986
- void switchToModel(model);
2060
+ void switchToModel(model, defaultEffortId);
2061
+ },
2062
+ ...efforts === null ? {} : {
2063
+ onMouseEnter: (event) => {
2064
+ const wrap = modelWrapRef.current;
2065
+ if (!wrap) return;
2066
+ const top = event.currentTarget.getBoundingClientRect().top - wrap.getBoundingClientRect().top;
2067
+ showEffortFlyout(model, top);
2068
+ },
2069
+ onMouseLeave: () => {
2070
+ scheduleEffortFlyoutClose(model.id);
2071
+ }
1987
2072
  }
1988
2073
  },
1989
2074
  (0, import_react3.createElement)(
@@ -1991,19 +2076,31 @@ function BalanceBadge(props) {
1991
2076
  { className: "dsh-model-allowance-model-name" },
1992
2077
  model.name ?? model.id
1993
2078
  ),
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)(
2079
+ (0, import_react3.createElement)(
1999
2080
  "span",
2000
- { className: "dsh-model-allowance-model-note" },
2001
- t("model.current")
2002
- ) : null
2081
+ { className: "dsh-model-allowance-model-side" },
2082
+ switchingModel === model.id ? (0, import_react3.createElement)(
2083
+ "span",
2084
+ { className: "dsh-model-allowance-model-note" },
2085
+ t("model.switching")
2086
+ ) : isCurrent ? (0, import_react3.createElement)(
2087
+ "span",
2088
+ { className: "dsh-model-allowance-model-note" },
2089
+ t("model.current")
2090
+ ) : null,
2091
+ efforts === null ? null : (0, import_react3.createElement)(
2092
+ "span",
2093
+ {
2094
+ className: "dsh-model-allowance-model-arrow",
2095
+ "aria-hidden": true
2096
+ },
2097
+ "\u203A"
2098
+ )
2099
+ )
2003
2100
  );
2004
2101
  }),
2005
2102
  // A failed switch keeps the list open and says why.
2006
- modelError && modelListState !== "error" ? (0, import_react3.createElement)(
2103
+ modelError && shownModelListState !== "error" ? (0, import_react3.createElement)(
2007
2104
  "div",
2008
2105
  {
2009
2106
  className: "dsh-model-allowance-model-message",
@@ -2011,6 +2108,54 @@ function BalanceBadge(props) {
2011
2108
  },
2012
2109
  modelError
2013
2110
  ) : null
2111
+ ) : null,
2112
+ // The effort flyout is a sibling of the list, not a
2113
+ // child: the list scrolls, and a nested panel would be
2114
+ // clipped by it. Sharing the hover with the row keeps
2115
+ // it open while the pointer crosses over.
2116
+ modelMenuOpen && hoverModel !== null && shownModelList.includes(hoverModel.model) ? (0, import_react3.createElement)(
2117
+ "div",
2118
+ {
2119
+ className: "dsh-model-allowance-effort-menu",
2120
+ role: "menu",
2121
+ "aria-label": t("model.title"),
2122
+ style: { top: `${hoverModel.top}px` },
2123
+ onMouseEnter: () => {
2124
+ clearTimeout(hoverModelTimer.current);
2125
+ },
2126
+ onMouseLeave: () => {
2127
+ scheduleEffortFlyoutClose(hoverModel.model.id);
2128
+ }
2129
+ },
2130
+ ...effortChoices(hoverModel.model).map((effort) => {
2131
+ const isCurrentEffort = hoverModel.model.id === currentModel && effort.id === selectedEffort;
2132
+ return (0, import_react3.createElement)(
2133
+ "button",
2134
+ {
2135
+ type: "button",
2136
+ key: effort.id ?? "provider-default",
2137
+ role: "menuitemradio",
2138
+ "aria-checked": isCurrentEffort ? "true" : "false",
2139
+ "data-current": isCurrentEffort ? "true" : "false",
2140
+ "data-default": effort.isDefault ? "true" : "false",
2141
+ className: "dsh-model-allowance-effort-item",
2142
+ disabled: switchingModel !== null,
2143
+ onClick: () => {
2144
+ void switchToModel(hoverModel.model, effort.id);
2145
+ }
2146
+ },
2147
+ (0, import_react3.createElement)(
2148
+ "span",
2149
+ { className: "dsh-model-allowance-model-name" },
2150
+ effort.providerDefault ? t("model.effortDefault") : effort.name
2151
+ ),
2152
+ effort.isDefault && !isCurrentEffort ? (0, import_react3.createElement)(
2153
+ "span",
2154
+ { className: "dsh-model-allowance-model-note" },
2155
+ t("model.effortDefault")
2156
+ ) : null
2157
+ );
2158
+ })
2014
2159
  ) : null
2015
2160
  ) : null
2016
2161
  )
package/lib/balance.js CHANGED
@@ -302,7 +302,7 @@ export class BalanceService {
302
302
  * The routes.
303
303
  */
304
304
  async available() {
305
- /** @type {Array<{route: string, configured: boolean, name: {zh: string, en: string}, color: string, iconHint?: string}>} */
305
+ /** @type {Array<{route: string, configured: boolean, name: {zh: string, en: string}, color: string, iconHint?: string, modelRoutes?: readonly string[]}>} */
306
306
  const routes = []
307
307
  for (const adapter of PROVIDER_ADAPTERS) {
308
308
  for (const route of adapter.routes) {
@@ -315,7 +315,12 @@ export class BalanceService {
315
315
  } else {
316
316
  configured = (await this.routeCredentials.resolve(route, adapter)) !== undefined
317
317
  }
318
- routes.push({ route, configured, ...displayOf(adapter) })
318
+ routes.push({
319
+ route,
320
+ configured,
321
+ ...displayOf(adapter),
322
+ ...(adapter.modelRoutes === undefined ? {} : { modelRoutes: adapter.modelRoutes }),
323
+ })
319
324
  }
320
325
  }
321
326
  return routes
@@ -389,6 +389,11 @@ export const deepseekAdapters = [
389
389
  id: 'deepseek-api-key',
390
390
  routes: ['deepseek'],
391
391
  kind: KIND_AMOUNT,
392
+ // The balance route and the Host provider routes that list this vendor's
393
+ // models are not the same id: the catalog group with the API-key models
394
+ // answers as `deepseek-official` while the pi-ai `deepseek` route may be
395
+ // unconfigured and carry an empty group.
396
+ modelRoutes: ['deepseek', 'deepseek-official'],
392
397
  endpoint: 'https://api.deepseek.com/user/balance',
393
398
  auth: 'bearer',
394
399
  credentialRefs: ['DEEPSEEK_API_KEY'],
@@ -147,6 +147,9 @@ export function bindingWindow(windows) {
147
147
  * @param {object} spec The declaration.
148
148
  * @param {string} spec.id Adapter identity.
149
149
  * @param {readonly string[]} spec.routes Provider routes this adapter serves.
150
+ * @param {readonly string[]} [spec.modelRoutes] Host provider routes whose
151
+ * catalog groups list this vendor's models, when they differ from the
152
+ * balance route.
150
153
  * @param {'amount'|'quota'} spec.kind Quantity dimension of the reading.
151
154
  * @param {boolean} [spec.modelScoped] Whether the reading is per model.
152
155
  * @param {string} spec.consoleUrl Provider console for a manual check.
@@ -248,6 +251,9 @@ export function defineAdapter(spec) {
248
251
  return Object.freeze({
249
252
  id: spec.id,
250
253
  routes: Object.freeze([...spec.routes]),
254
+ ...(spec.modelRoutes === undefined
255
+ ? {}
256
+ : { modelRoutes: Object.freeze([...spec.modelRoutes]) }),
251
257
  kind: spec.kind,
252
258
  ...(spec.modelScoped === true ? { modelScoped: true } : {}),
253
259
  consoleUrl: spec.consoleUrl,
package/lib/providers.js CHANGED
@@ -92,6 +92,9 @@ import { zaiAdapters } from './providers/zai.js'
92
92
  * @typedef {object} ProviderAdapter
93
93
  * @property {string} id Adapter identity.
94
94
  * @property {readonly string[]} routes Provider routes this adapter serves.
95
+ * @property {readonly string[]} [modelRoutes] Host provider routes whose catalog
96
+ * groups list this vendor's models, when they differ from the balance route.
97
+ * The first one that carries models answers.
95
98
  * @property {'amount'|'quota'} kind Quantity dimension of the reading.
96
99
  * @property {boolean} [modelScoped] Whether the reading is per provider-owned model.
97
100
  * @property {string} consoleUrl Provider console for a manual check.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-model-allowance",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
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",