dsh-tokenlens 1.5.2 → 1.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,14 @@
4
4
 
5
5
  **版本记录约定**:同一发布日只保留一个章节,标题取当日**最后一个**已发布版本号;当日多次发布的改动合并到该章节,并在标题下注明当日实际发布过的版本号(npm 上仍可安装这些独立版本)。若当日已有发布,后续小改动不再单独增加版本号,而是并入下一篇日志。这样日志按天规整、不出现同日多个小版本。
6
6
 
7
+ ## 1.5.3 — 2026-11-03
8
+
9
+ - **feat(open-design): 初次配置只需一个 Control Key**——工作区 ID 改为**可选**。Control Key 本身就能确定它属于哪个工作区,插件改用 `GET /api/v1/workspaces/current` 自动识别(回退 `GET /api/v1/workspaces`,仅当可见工作区恰好一个时采纳)
10
+ - feat(open-design): **多工作区不猜**——`/workspaces/current` 没有 active 且有多个可见工作区时返回 `missing-key` 并提示手动填写「工作区 ID」,因为猜错会把别的团队的用量当成自己的
11
+ - feat(open-design): 显式配置的工作区 ID 优先于自动识别(管理页 / `providers.open-design.workspaceId` / `OPENDESIGN_WORKSPACE_ID`),多工作区账号据此指定目标
12
+ - feat(ui): 管理页「工作区 ID」文案改为可选,说明留空即自动识别、仅在多工作区时需要填
13
+ - 测试:317 → 318(仅凭 Key 自动识别工作区并用于后续所有计费读取、多工作区拒绝猜测)
14
+
7
15
  ## 1.5.2 — 2026-11-02
8
16
 
9
17
  (当日已发布版本:v1.3.5 / v1.3.6 / v1.3.7 / v1.3.8 / v1.3.9 / v1.4.0 / v1.5.0 / v1.5.1 / v1.5.2)
package/PROTOCOL.md CHANGED
@@ -113,17 +113,27 @@ A `windows` snapshot may also carry a non-null `balance` (Open Design reports
113
113
  allowance windows and a wallet independently); the Client renders it as one extra
114
114
  row at the end of the capacity card and appends the amount to the chip tooltip.
115
115
 
116
- `open-design` needs two credentials that the API-keys page does not provide: a
117
- **workspace id** (the console URL's `?workspaceId=` value) and a **Control Key**
118
- (the `profiles.prod.controlKey` the desktop app / `vela login` writes to
119
- `~/.amr/config.json`). The inference key from the Cloud API Keys page answers
120
- HTTP 401 on the billing endpoints. Missing either value is a `missing-key` error.
121
- Since v1.3.9 both may be entered on the management page: `workspaceId` is
122
- persisted in `providers.json`, and the Control Key is written through the same
123
- `apiKey` override field the store already supported (write-only — the Host never
124
- returns it, only `hasKey`/`keySource`). The environment/credential sources
125
- (`OPENDESIGN_CONTROL_KEY`, `VELA_CONTROL_KEY`, `OPENDESIGN_WORKSPACE_ID`) still
126
- work and the page override wins.
116
+ `open-design` needs a **Control Key** (the `profiles.prod.controlKey` the desktop
117
+ app / `vela login` writes to `~/.amr/config.json`). The inference key from the
118
+ Cloud API Keys page answers HTTP 401 on the billing endpoints, so it cannot be
119
+ used here. A missing Control Key is a `missing-key` error.
120
+
121
+ **The workspace id is optional (v1.5.3).** A Control Key identifies the workspace
122
+ it belongs to, so the plugin discovers it:
123
+
124
+ 1. `GET /api/v1/workspaces/current` — the key's active workspace
125
+ (`workspaceId`, else `activeWorkspaceId`); this is the normal path.
126
+ 2. `GET /api/v1/workspaces` — fallback: accepted only when exactly one workspace
127
+ is visible. Several visible workspaces with none marked active is a
128
+ `missing-key` error rather than a guess, because picking the wrong one would
129
+ report another team's usage as this account's.
130
+
131
+ A configured id always wins over discovery (management page → 工作区 ID, persisted
132
+ in `providers.json`; or `OPENDESIGN_WORKSPACE_ID`), which is how a multi-workspace
133
+ account selects one. The Control Key is written through the same `apiKey` override
134
+ field the store already supported (write-only — the Host never returns it, only
135
+ `hasKey`/`keySource`). `OPENDESIGN_CONTROL_KEY` / `VELA_CONTROL_KEY` are still
136
+ honoured and the page override wins.
127
137
 
128
138
  Reads, in independent layers (v1.4.0) — each may fail without voiding the others:
129
139
 
package/README.md CHANGED
@@ -326,7 +326,7 @@ Control Key 是 **Open Design 桌面端/CLI 登录时写入本机的会话凭据
326
326
 
327
327
  - OpenCode 的额度定义见[官方文档](https://opencode.ai/docs/go/#usage-limits)(5h = 月度额度的 20%、周 = 50%、月 = 100%);接口只返回百分比,插件也只画百分比。
328
328
  - Command Code 的 `/alpha/*` 是官方 CLI 自用的私有计费路由;免费 / 无方案账户返回空窗口(显示 `—`),插件不会用方案目录猜额度。
329
- - Open Design 的窗口按账户实际返回的时长渲染:Go 同时有 5 小时和 7 天窗口,Plus / Pro / Max 只有 7 天窗口;钱包余额是独立于窗口的 USD 额度,用尽后继续扣钱包。查询要求工作区权限,鉴权失败会明确显示错误而不是 0;工作区 ID 与 Control Key 缺任一项时,弹窗会指出缺哪一项并给出「去配置」入口(v1.3.5)。窗口、余额、套餐档位是**三层独立读取**(v1.4.0):任一接口不可用时只少那一块数据,不会整套报错。
329
+ - Open Design 的窗口按账户实际返回的时长渲染:Go 同时有 5 小时和 7 天窗口,Plus / Pro / Max 只有 7 天窗口;钱包余额是独立于窗口的 USD 额度,用尽后继续扣钱包。查询要求工作区权限,鉴权失败会明确显示错误而不是 0。**只需填一个 Control Key**:工作区 ID 是可选项(v1.5.3),插件会用 Key 调 `/api/v1/workspaces/current` 自动识别你的工作区;只有账号有多个工作区、需要指定其中一个时才在管理页填「工作区 ID」。真正缺 Key 时弹窗会指出并给出「去配置」入口。窗口、余额、套餐档位是**三层独立读取**(v1.4.0):任一接口不可用时只少那一块数据,不会整套报错。
330
330
  - **chip 的「月 N%」与「本期 Design Plan 累计」是插件本机算的估算值**(v1.5.0 / v1.5.1)。Open Design 有月度额度(Go 为每月 $120 = 4 × 每周 $30),但**没有「本账期已用」接口**——控制台的 `/billing/usage`、`/billing/coding-plan-usage` 都靠浏览器 Cookie 鉴权,Control Key 一律 401。于是插件累加它观测到的 **7 天窗口**(只记 7 天,因为 5 小时窗口测量的是同一笔消耗,相加会重复计数),再按官方「月度 = 4 × 周额度」的比例折算成百分比填进 chip 的「月」槽位,并按 `creditsPerUsd` 折算美元。弹窗会标注覆盖情况(「已覆盖本账期全部窗口」或「仅统计已观测的 N 个窗口」);覆盖不完整时百分比仍显示但**不会触发告警配色**,插件没运行期间错过的窗口无法计入。
331
331
  - 未配置凭据的供应商 chip 静默隐藏;`zai`、`minimax`、`deepinfra` 属 best-effort 适配(未公开接口,失败时按可重试错误优雅降级)。
332
332
  - 各供应商的元数据(`kind` / `label` / `shortLabel` / `baseURL` / `apiKeyRef` / `consoleUrl`)集中在 `lib/providers.mjs` 的 `PROVIDER_DEFAULTS` 与 `ADAPTERS` 中,新增适配器只需在该文件登记并补测试。
@@ -439,7 +439,7 @@ Host 半注册 `tokenlens_usage`,参数 `action`(默认 `snapshot`)与可
439
439
 
440
440
  ## 验证
441
441
 
442
- - `node --test`:**317 个自动化用例**,覆盖 Host 数据层、供应商适配器、统计折叠、各 harness 读取、Design Plan 周期账本、窗口锚点切片、插件集成与浏览器端渲染 / 交互。
442
+ - `node --test`:**318 个自动化用例**,覆盖 Host 数据层、供应商适配器、统计折叠、各 harness 读取、Design Plan 周期账本、窗口锚点切片、工作区自动识别、插件集成与浏览器端渲染 / 交互。
443
443
  - 在真实环境中验证过 **5 个 harness** 的数据源;本机统计与独立的 SQLite 查询、独立的日志解析逐项对账(差额为 0),跨 harness 合并与供应商映射算术自洽。
444
444
  - 供应商接口按各官方文档与参考客户端行为验证;未配置凭据时 chip 静默隐藏,接口异常时按可重试错误降级。
445
445
 
package/client.js CHANGED
@@ -329,9 +329,9 @@ window.__ModuleLoader__.load({
329
329
  presetCollapse: '收起',
330
330
  providerConsoleURL: '控制台地址(Console URL)',
331
331
  providerConsoleURLPlaceholder: '留空则使用内置默认地址',
332
- providerWorkspaceId: '工作区 ID(Workspace ID)',
333
- providerWorkspaceIdHint: 'Open Design Cloud 工作区标识;额度查询需要',
334
- providerWorkspaceIdPlaceholder: '粘贴工作区 ID,或直接粘贴控制台地址(自动提取)',
332
+ providerWorkspaceId: '工作区 ID(Workspace ID,可选)',
333
+ providerWorkspaceIdHint: '可留空——插件会用 Control Key 自动识别你的工作区;仅在账号有多个工作区、需要指定其中一个时才填',
334
+ providerWorkspaceIdPlaceholder: '留空自动识别;或粘贴工作区 ID / 控制台地址',
335
335
  providerWorkspaceIdTooLong: '工作区 ID 最多 120 个字符',
336
336
  providerControlKey: 'Control Key',
337
337
  providerControlKeyHint: '本机 Open Design 登录后写在 ~/.amr/config.json 的 profiles.prod.controlKey;不是 Cloud API Keys 页的推理 Key',
@@ -590,9 +590,10 @@ window.__ModuleLoader__.load({
590
590
  presetExpand: 'Expand',
591
591
  presetCollapse: 'Collapse',
592
592
  providerConsoleURL: 'Console URL',
593
- providerWorkspaceId: 'Workspace ID',
594
- providerWorkspaceIdHint: 'The Open Design Cloud workspace identifier the quota query is scoped to',
595
- providerWorkspaceIdPlaceholder: 'Paste the workspace ID, or the console URL (it is extracted)',
593
+ providerWorkspaceId: 'Workspace ID (optional)',
594
+ providerWorkspaceIdHint:
595
+ 'Leave empty — the plugin finds your workspace from the Control Key. Fill it only when the account has several workspaces and you need a specific one.',
596
+ providerWorkspaceIdPlaceholder: 'Auto-detected; or paste a workspace ID / console URL',
596
597
  providerWorkspaceIdTooLong: 'The workspace ID may be at most 120 characters',
597
598
  providerControlKey: 'Control Key',
598
599
  providerControlKeyHint: 'Written to ~/.amr/config.json as profiles.prod.controlKey after signing in to Open Design; not the inference key from the Cloud API Keys page',
package/index.js CHANGED
@@ -83,7 +83,7 @@ export const inject = []
83
83
  export const PLUGIN_NAME = 'dsh-tokenlens'
84
84
 
85
85
  /** Plugin version; mirrors package.json (kept here so diagnostics need no fs read). */
86
- export const PLUGIN_VERSION = '1.5.2'
86
+ export const PLUGIN_VERSION = '1.5.3'
87
87
 
88
88
  /** Documented defaults; every field of the raw row config is optional. */
89
89
  const DEFAULTS = {
@@ -1097,6 +1097,10 @@ export function apply(ctx, rawConfig) {
1097
1097
  provider = { ...provider, accessKeyId: accessValue, secretAccessKey: secretValue }
1098
1098
  keyInfo = { ref: accessRef, source: access.source, detail: access.detail }
1099
1099
  } else if (adapterId === 'open-design') {
1100
+ // v1.5.3: the workspace id is OPTIONAL. A Control Key identifies its own
1101
+ // active workspace, so the adapter discovers it when none is configured;
1102
+ // an explicit value (management page / config / env) always wins for
1103
+ // accounts with several workspaces.
1100
1104
  const workspaceId =
1101
1105
  String(provider.workspaceId ?? '').trim() || String(process.env.OPENDESIGN_WORKSPACE_ID ?? '').trim()
1102
1106
  const refs = []
@@ -1125,14 +1129,6 @@ export function apply(ctx, rawConfig) {
1125
1129
  break
1126
1130
  }
1127
1131
  }
1128
- if (workspaceId === '') {
1129
- throw new UsageError(
1130
- 'missing-key',
1131
- 'OpenDesign quota needs a workspace id: set it on the plugin management page (open-design → 工作区 ID), ' +
1132
- 'in providers.open-design.workspaceId, or in OPENDESIGN_WORKSPACE_ID',
1133
- { retryable: false },
1134
- )
1135
- }
1136
1132
  if (controlKey === '') {
1137
1133
  throw new UsageError(
1138
1134
  'missing-key',
@@ -1140,6 +1136,7 @@ export function apply(ctx, rawConfig) {
1140
1136
  { retryable: false },
1141
1137
  )
1142
1138
  }
1139
+ // An empty workspaceId is fine: the adapter resolves it from the key.
1143
1140
  provider = { ...provider, workspaceId, controlKey }
1144
1141
  keyInfo = { ref: controlRef, source: controlSource, detail: controlDetail }
1145
1142
  } else if (typeof provider.apiKeyRef === 'string' && provider.apiKeyRef.trim() !== '') {
package/lib/providers.mjs CHANGED
@@ -783,6 +783,82 @@ const OPEN_DESIGN_WINDOW_SECONDS = {
783
783
  2592000: 'monthly',
784
784
  }
785
785
 
786
+ /**
787
+ * Find the workspace a Control Key belongs to.
788
+ *
789
+ * `GET /api/v1/workspaces/current` answers with the key's active workspace
790
+ * (`workspaceId` / `activeWorkspaceId`), and `GET /api/v1/workspaces` lists every
791
+ * workspace the key can see. A Control Key is workspace-scoped in practice (this
792
+ * plugin talks to one billing account at a time), so the active workspace is the
793
+ * right default; the list is only a fallback for an account without an "active"
794
+ * one, and a single-entry list is unambiguous.
795
+ *
796
+ * @returns `{ workspaceId, name, source, error, retryable }`; `workspaceId` is ''
797
+ * when the workspace could not be determined (the caller turns that into a
798
+ * `missing-key` error carrying `error`).
799
+ */
800
+ export async function discoverOpenDesignWorkspace(baseURL, controlKey, httpJson) {
801
+ const headers = { accept: 'application/json', authorization: `Bearer ${controlKey}` }
802
+ const failed = (error, retryable = false) => ({ workspaceId: '', name: '', source: '', error, retryable })
803
+ try {
804
+ const current = await httpJson(joinUrl(baseURL, '/api/v1/workspaces/current'), { method: 'GET', headers })
805
+ const parsed = parseOpenDesignWorkspace(current)
806
+ if (parsed.workspaceId !== '') {
807
+ return { workspaceId: parsed.workspaceId, name: parsed.name, source: 'current', error: '', retryable: false }
808
+ }
809
+ } catch (error) {
810
+ // A 404/401 here is not fatal: the list endpoint may still answer.
811
+ if (error instanceof UsageError && error.code === 'network') {
812
+ return failed(String(error.message ?? 'network error'), true)
813
+ }
814
+ }
815
+ try {
816
+ const list = await httpJson(joinUrl(baseURL, '/api/v1/workspaces'), { method: 'GET', headers })
817
+ const items = Array.isArray(list?.items) ? list.items : []
818
+ const usable = items
819
+ .filter((item) => item !== null && typeof item === 'object' && typeof item.workspaceId === 'string')
820
+ .filter((item) => item.workspaceId.trim() !== '')
821
+ if (usable.length === 1) {
822
+ const only = usable[0]
823
+ return {
824
+ workspaceId: only.workspaceId.trim(),
825
+ name: typeof only.workspaceName === 'string' ? only.workspaceName : '',
826
+ source: 'list',
827
+ error: '',
828
+ retryable: false,
829
+ }
830
+ }
831
+ if (usable.length > 1) {
832
+ // Never guess between several workspaces: picking the wrong one would
833
+ // report another team's usage as this account's.
834
+ return failed(`${usable.length} workspaces are visible and none is marked active`, false)
835
+ }
836
+ } catch (error) {
837
+ return failed(String(error?.message ?? 'the workspace list request failed'), true)
838
+ }
839
+ return failed('no workspace is available to this Control Key', false)
840
+ }
841
+
842
+ /**
843
+ * Read a workspace id from a `/workspaces/current` or `/workspaces` document.
844
+ *
845
+ * @returns `{ workspaceId, name }` where `workspaceId` is '' when absent.
846
+ */
847
+ export function parseOpenDesignWorkspace(payload) {
848
+ const document = payload !== null && typeof payload === 'object' && !Array.isArray(payload) ? payload : null
849
+ if (document === null) return { workspaceId: '', name: '' }
850
+ for (const key of ['workspaceId', 'activeWorkspaceId']) {
851
+ const value = document[key]
852
+ if (typeof value === 'string' && value.trim() !== '') {
853
+ return {
854
+ workspaceId: value.trim(),
855
+ name: typeof document.workspaceName === 'string' ? document.workspaceName.trim() : '',
856
+ }
857
+ }
858
+ }
859
+ return { workspaceId: '', name: '' }
860
+ }
861
+
786
862
  /** A non-negative integer credit amount serialized as a decimal string. */
787
863
  function openDesignCredits(value) {
788
864
  return typeof value === 'string' && /^\d{1,40}$/.test(value) ? BigInt(value) : null
@@ -966,20 +1042,31 @@ export const ADAPTERS = {
966
1042
  kind: 'windows',
967
1043
  async read({ providerId = 'open-design', provider, httpJson, now }) {
968
1044
  const config = providerConfig(providerId, provider)
969
- const workspaceId = String(config.workspaceId ?? '').trim()
970
- if (workspaceId === '') {
971
- throw new UsageError(
972
- 'missing-key',
973
- 'OpenDesign quota needs a workspace id (providers.open-design.workspaceId or OPENDESIGN_WORKSPACE_ID)',
974
- { retryable: false },
975
- )
976
- }
977
1045
  const controlKey = typeof provider.controlKey === 'string' ? provider.controlKey : ''
978
1046
  if (controlKey === '') {
979
1047
  throw new UsageError('missing-key', 'OpenDesign quota needs a Control Key (OPENDESIGN_CONTROL_KEY or VELA_CONTROL_KEY)', {
980
1048
  retryable: false,
981
1049
  })
982
1050
  }
1051
+ // v1.5.3: the workspace id is DERIVABLE from a Control Key, so a configured
1052
+ // id is only a preference. `GET /api/v1/workspaces/current` answers with the
1053
+ // key's active workspace, which removes the second setup field entirely.
1054
+ let workspaceId = String(config.workspaceId ?? '').trim()
1055
+ if (workspaceId === '') {
1056
+ const adopted = await discoverOpenDesignWorkspace(config.baseURL, controlKey, httpJson)
1057
+ workspaceId = adopted.workspaceId
1058
+ if (workspaceId === '') {
1059
+ throw new UsageError(
1060
+ 'missing-key',
1061
+ 'OpenDesign could not determine the workspace for this Control Key: ' +
1062
+ (adopted.error === ''
1063
+ ? 'the key is not valid for any workspace'
1064
+ : `the discovery request failed (${adopted.error})`) +
1065
+ '. Set 工作区 ID on the plugin management page only if your account has several workspaces.',
1066
+ { retryable: adopted.retryable },
1067
+ )
1068
+ }
1069
+ }
983
1070
  const workspaceBase = joinUrl(config.baseURL, `/api/v1/workspaces/${encodeURIComponent(workspaceId)}`)
984
1071
  const headers = {
985
1072
  accept: 'application/json',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-tokenlens",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "type": "module",
5
5
  "description": "dsh-TokenLens (词元透镜) — provider-aware plan capacity plus local token statistics (today/7d/30d/all-time, by harness/provider/model/workspace) for DSH.",
6
6
  "license": "MIT",