dsh-sidebar-usage 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/lib/client.js CHANGED
@@ -1683,11 +1683,18 @@ window.__ModuleLoader__.load({
1683
1683
  return "";
1684
1684
  }
1685
1685
  }
1686
+ /** 看板与设置页共用的「当前看的是哪家」变更事件。 */
1687
+ const PRESET_EVENT = "dsh-usage:preset-provider-changed";
1686
1688
  function writePresetProvider(id) {
1689
+ const next = typeof id === "string" && id !== "" ? id : "";
1687
1690
  try {
1688
- if (typeof id === "string" && id !== "") window.localStorage.setItem(PRESET_PROVIDER_KEY, id);
1691
+ if (next !== "") window.localStorage.setItem(PRESET_PROVIDER_KEY, next);
1689
1692
  else window.localStorage.removeItem(PRESET_PROVIDER_KEY);
1690
1693
  } catch (e) { /* 隐私模式写不进去:本次会话内仍然生效,只是刷新后回到跟随模型 */ }
1694
+ // 同一个页面里的看板与设置页都要跟着变:localStorage 的 storage 事件不派发给自己。
1695
+ try {
1696
+ window.dispatchEvent(new CustomEvent(PRESET_EVENT, { detail: { provider: next } }));
1697
+ } catch (e) { /* 老环境没有 CustomEvent:至少 localStorage 已写入 */ }
1691
1698
  }
1692
1699
 
1693
1700
  /**
@@ -2069,6 +2076,9 @@ window.__ModuleLoader__.load({
2069
2076
  "settings.intro": "已添加的提供商会显示在侧栏看板的右键菜单里。这里只管理账户:映射本插件已支持的提供商,或用自定义 URL 接入中转站、自建网关。",
2070
2077
  "providers.title": "模型提供商",
2071
2078
  "providers.intro": "每项只占一行摘要:点「编辑」展开改配置,点「删除」移除。",
2079
+ "providers.watching": "看板显示这一家(单选)",
2080
+ "providers.watchingFollow": "跟随当前模型(不指定)",
2081
+ "providers.watchingGone": "这一项配置里已经没有了,看板会回落",
2072
2082
  "providers.empty": "还没有添加提供商。侧栏看板只会显示这里已添加的项,用下面的按钮添加。",
2073
2083
  "providers.edit": "编辑",
2074
2084
  "providers.cancel": "收起",
@@ -2123,6 +2133,7 @@ window.__ModuleLoader__.load({
2123
2133
  "custom.preset": "接口类型",
2124
2134
  "custom.preset.json": "通用 JSON(自己填路径)",
2125
2135
  "custom.preset.jichuanai": "济川AI(订阅/余额)",
2136
+ "custom.preset.newapi-account": "new-api 账号接口(余额+已用)",
2126
2137
  "custom.preset.newapi": "new-api / one-api 中转",
2127
2138
  "custom.preset.openaiBilling": "OpenAI 计费接口",
2128
2139
  "custom.preset.deepseek": "DeepSeek /user/balance",
@@ -2247,6 +2258,9 @@ window.__ModuleLoader__.load({
2247
2258
  "settings.intro": "Added providers show up in the sidebar card's right-click menu. This page only manages accounts: map a provider this plugin already supports, or connect a relay / self-hosted gateway by custom URL.",
2248
2259
  "providers.title": "Model providers",
2249
2260
  "providers.intro": "Each provider takes one summary row: click Edit to change it, Remove to delete it.",
2261
+ "providers.watching": "Show this provider on the card (single choice)",
2262
+ "providers.watchingFollow": "Follow the current model (unspecified)",
2263
+ "providers.watchingGone": "This provider is no longer configured; the card falls back",
2250
2264
  "providers.empty": "No providers yet. The sidebar card lists only what is added here — use the buttons below.",
2251
2265
  "providers.edit": "Edit",
2252
2266
  "providers.cancel": "Collapse",
@@ -2301,6 +2315,7 @@ window.__ModuleLoader__.load({
2301
2315
  "custom.preset": "Endpoint type",
2302
2316
  "custom.preset.json": "Generic JSON (paths in YAML)",
2303
2317
  "custom.preset.jichuanai": "JichuanAI (subscription/balance)",
2318
+ "custom.preset.newapi-account": "new-api account API (balance + used)",
2304
2319
  "custom.preset.newapi": "new-api / one-api relay",
2305
2320
  "custom.preset.openaiBilling": "OpenAI billing API",
2306
2321
  "custom.preset.deepseek": "DeepSeek /user/balance",
@@ -2925,6 +2940,16 @@ window.__ModuleLoader__.load({
2925
2940
  const presetRef = useRef(preset);
2926
2941
  presetRef.current = preset;
2927
2942
 
2943
+ // 设置页里切了服务商 → 看板立刻跟着切(反之亦然)。
2944
+ useEffect(() => {
2945
+ const onPreset = (event) => {
2946
+ const next = event && event.detail && typeof event.detail.provider === "string" ? event.detail.provider : readPresetProvider();
2947
+ if (next !== presetRef.current) setPreset(next);
2948
+ };
2949
+ window.addEventListener(PRESET_EVENT, onPreset);
2950
+ return () => window.removeEventListener(PRESET_EVENT, onPreset);
2951
+ }, []);
2952
+
2928
2953
  // loadRef 让下面两个回调可以互相引用而不必谁先声明。
2929
2954
  const loadRef = useRef(null);
2930
2955
 
@@ -4301,7 +4326,7 @@ menu !== null && h(ProviderMenu, {
4301
4326
  // ---- 设置页:模型提供商账户 --------------------------------------
4302
4327
  // 已保存的项只占一行摘要(provider + 查询口径),点「编辑」才展开表单;
4303
4328
  // 新增与编辑共用同一入口,页面里不再并排两套列表 —— 控件少一半。
4304
- const CUSTOM_PRESET_OPTIONS = ["newapi", "openai-billing", "deepseek-balance", "jichuanai", "json"];
4329
+ const CUSTOM_PRESET_OPTIONS = ["newapi", "newapi-account", "openai-billing", "deepseek-balance", "jichuanai", "json"];
4305
4330
  const CUSTOM_KIND_OPTIONS = ["balance", "usage"];
4306
4331
  const presetLabelKey = (id) => (id === "openai-billing"
4307
4332
  ? "custom.preset.openaiBilling"
@@ -4362,6 +4387,18 @@ menu !== null && h(ProviderMenu, {
4362
4387
  const [saving, setSaving] = useState(false);
4363
4388
  const mountedRef = useRef(true);
4364
4389
  const focusId = useProviderFocus();
4390
+ // 看板当前显示的是哪一家(localStorage 里的选择,和卡片右键菜单同一份)。
4391
+ // 设置页必须能看见它:不然"看板没数字"时用户不知道卡片到底在查谁。
4392
+ const [watching, setWatching] = useState(() => readPresetProvider());
4393
+ useEffect(() => {
4394
+ const onPreset = () => setWatching(readPresetProvider());
4395
+ window.addEventListener(PRESET_EVENT, onPreset);
4396
+ window.addEventListener("storage", onPreset);
4397
+ return () => {
4398
+ window.removeEventListener(PRESET_EVENT, onPreset);
4399
+ window.removeEventListener("storage", onPreset);
4400
+ };
4401
+ }, []);
4365
4402
 
4366
4403
  useEffect(() => () => { mountedRef.current = false; }, []);
4367
4404
 
@@ -4783,6 +4820,21 @@ menu !== null && h(ProviderMenu, {
4783
4820
  style: { ...rowStyleOf(rowStyle, provider, open ? provider : focusId), display: "flex", flexDirection: "column", gap: 8 },
4784
4821
  },
4785
4822
  h("div", { style: { display: "flex", alignItems: "center", gap: 8, minWidth: 0, flexWrap: "wrap" } },
4823
+ // 勾选=看板在看这一家。单选语义:勾一个,其他自动取消。
4824
+ // 与看板右键菜单共用 writePresetProvider,双向同步。
4825
+ h("input", {
4826
+ type: "radio",
4827
+ name: "dsh-usage-watching",
4828
+ checked: watching === provider,
4829
+ disabled: disabled || provider === "",
4830
+ title: t("providers.watching"),
4831
+ "aria-label": t("providers.watching"),
4832
+ onChange: () => {
4833
+ setWatching(provider);
4834
+ writePresetProvider(provider);
4835
+ },
4836
+ style: { margin: 0, flex: "none", cursor: disabled ? "default" : "pointer" },
4837
+ }),
4786
4838
  h("span", { style: { fontSize: 12, fontWeight: 600 } }, provider === "" ? t("alias.providerRequired") : provider),
4787
4839
  h("span", { style: { ...mutedStyle, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" } }, summaryOf(type, row)),
4788
4840
  h("span", { style: { marginLeft: "auto", display: "flex", gap: 6, flex: "none" } },
package/lib/custom.js CHANGED
@@ -27,7 +27,7 @@ const REF_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
27
27
 
28
28
  export const CUSTOM_KINDS = ['balance', 'usage'];
29
29
  export const CUSTOM_AUTHS = ['bearer', 'raw', 'none'];
30
- export const CUSTOM_PRESETS = ['json', 'deepseek-balance', 'openai-billing', 'newapi', 'jichuanai'];
30
+ export const CUSTOM_PRESETS = ['json', 'deepseek-balance', 'openai-billing', 'newapi', 'newapi-account', 'jichuanai'];
31
31
 
32
32
  /**
33
33
  * 预设默认值。`path` 是主请求,`usagePath` 是第二段请求(可选)。
@@ -49,6 +49,17 @@ const PRESET_DEFAULTS = {
49
49
  currency: 'CNY',
50
50
  scale: 1,
51
51
  },
52
+ // new-api 账号接口:GET /api/user/self → data.quota(剩余额度)/ data.used_quota(已用)
53
+ // 换算率每家不同(1 元 = 500000 配额是 new-api 默认),由 currency/scale 决定怎么显示。
54
+ // availablePath 用 data.status:status 为 1 表示账号可用,0 表示被禁用。
55
+ 'newapi-account': {
56
+ path: '/api/user/self',
57
+ totalPath: 'data.quota',
58
+ usedPath: 'data.used_quota',
59
+ availablePath: 'data.status',
60
+ currency: 'CNY',
61
+ scale: 1 / 500000,
62
+ },
52
63
  // 济川AI(jc.jichuanai.com):实测 GET /api/v1/subscriptions →
53
64
  // data[0].group.weekly_limit_usd(额度)/ weekly_usage_usd(已用)/ expires_at(到期)
54
65
  // 余额口径:GET /api/v1/user/profile → data.balance
@@ -73,6 +84,11 @@ function text(value) {
73
84
  return typeof value === 'string' ? value.trim() : '';
74
85
  }
75
86
 
87
+ /** 同 text(),但 undefined/null 也给空串以外的东西不炸:专供预设默认值。 */
88
+ function orEmpty(value) {
89
+ return typeof value === 'string' ? value : '';
90
+ }
91
+
76
92
  /** 接受字符串或数字,返回有限数或 null(绝不把缺失变成 0)。 */
77
93
  function num(value) {
78
94
  if (typeof value === 'number') return Number.isFinite(value) ? value : null;
@@ -105,7 +121,8 @@ function normalizeBaseUrl(raw) {
105
121
  /** 路径:缺省用预设值;必须以 / 开头。 */
106
122
  function normalizePath(raw, fallback) {
107
123
  const value = text(raw);
108
- const path = value === '' ? fallback : value;
124
+ const fb = typeof fallback === 'string' ? fallback : '';
125
+ const path = value === '' ? fb : value;
109
126
  if (path === '') return '';
110
127
  return path.startsWith('/') ? path : `/${path}`;
111
128
  }
@@ -225,7 +242,10 @@ export function normalizeCustomProviders(raw) {
225
242
  const subUsedPath = text(item.usedPath) || (defaults.usedPath ?? '');
226
243
  const subExpiresPath = text(item.expiresPath) || (defaults.expiresPath ?? '');
227
244
  const pickedTotal = useSubscription ? subTotalPath : (balanceTotalPath !== '' ? balanceTotalPath : subTotalPath);
228
- const pickedUsed = useSubscription ? subUsedPath : '';
245
+ // 余额形态也要认 usedPath:new-api 账号接口同时给"剩余 quota"和"已用 used_quota",
246
+ // 有了两段才能算出百分比条;订阅形态才需要到期时间。
247
+ const balUsedPath = text(item.balanceUsedPath) || (defaults.usedPath ?? '');
248
+ const pickedUsed = useSubscription ? subUsedPath : balUsedPath;
229
249
  const pickedExpires = useSubscription ? subExpiresPath : '';
230
250
  if (pickedPath === '') {
231
251
  warnings.push(`忽略自定义 provider ${provider}:path / balancePath 至少要给一个`);
package/lib/index.js CHANGED
@@ -130,7 +130,9 @@ function buildConfig(z) {
130
130
  provider: z.string().required(),
131
131
  baseUrl: z.string().required(),
132
132
  label: z.string(),
133
- kind: z.union(CUSTOM_KINDS.map((id) => z.const(id))),
133
+ // 容错:`kind` 写错不该让整条插件配置校验失败(那会把看板整个弄没)。
134
+ // 认不出的值在 custom.js 里已经回落成 balance,这里只要放行不认识的字符串。
135
+ kind: z.string(),
134
136
  preset: z.union(CUSTOM_PRESETS.map((id) => z.const(id))),
135
137
  path: z.string(),
136
138
  usagePath: z.string(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-sidebar-usage",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Sidebar usage HUD for DeepSeek Harness: provider balance / subscription quota, cumulative tokens & calls, drag-to-resize card, hover-out drawer that collects bottom-bar entries.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",