dsh-token-dash 1.0.6 → 1.0.7

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/AGENTS.md CHANGED
@@ -1,85 +1,84 @@
1
- # dsh-token-dash 项目规则
1
+ # AGENTS.md
2
2
 
3
- > 面向 AI agent 与维护者。本仓库是 DSH 插件代码仓;当前插件的计划在 `docs/plan/`、checklist 在 `docs/checklist/` 维护,`Context-Engineer` 不再维护本插件文档。规则以本文件为准。
3
+ ## 文档定位
4
4
 
5
- ## 构建与测试
5
+ - 本文件只保留仓库协作与执行规则,不承担开发总览、目录索引或版本现状。
6
+ - 开发/架构入口:`docs/design.md`;先读架构、Provider/VM/UI 边界、模块职责,再看代码。
7
+ - 验证命令以 `package.json` 的 scripts 为准;不要在本文件复制测试规模、构建命令或版本号。
6
8
 
7
- - Node `>=20`;纯 ESM;首版零构建,源码直接发布。
8
- - 宿主入口 `index.js`,客户端 `client.js`(手写 `window.__ModuleLoader__` bundle,id 必须等于包名)。
9
- - 不提交 `lib/`;`prepublishOnly` 跑 `npm test`。
9
+ ### 重点文档读取位置
10
10
 
11
- ```bash
12
- npm test # L1:node --test
13
- npm run e2e # L2 + L3:临时 DSH_HOME + Playwright
14
- npm pack --dry-run
15
- ```
11
+ | 场景 | 先读 | 再看 |
12
+ |---|---|---|
13
+ | 改代码 / 架构 | `docs/design.md` | `src/`、`client.js`、`test/unit/`、对应 active plan |
14
+ | 需求 / 规划 | `docs/plan/AGENTS.md` | `docs/plan/active/`(先看文件头状态)、`docs/plan/archived/`(历史) |
15
+ | 施工 / 走查 | `docs/checklist/AGENTS.md` | `docs/checklist/active/dsh/`、`docs/checklist/archived/dsh/` |
16
+ | 发布 / 版本 | `docs/release.md` | `.agents/skills/dsh-token-dash-release/SKILL.md`、版本记录 `.agents/skills/dsh-token-dash-release/references/official-release-version.md` |
17
+ | 用户说明 / 变更历史 | `README.md` / `README.en.md` | `CHANGELOG.md`、`docs/pic/` |
18
+ | Skill 维护 | `.agents/skills/`(项目 skill) | `D:\Projects\Context-Engineer\skills\`(全局 skill 真源) |
16
19
 
17
- ## 文档位置
20
+ - 当前 active plan / checklist 不在此复制;进入目录后按文件头状态判断,避免本文件与文档状态漂移。
18
21
 
19
- - 稳定文档:`docs/design.md`、`docs/release.md`。
20
- - 计划:`docs/plan/active/feature_token_dash.md`(遵循全局 `plan-workflow` 的 `docs/plan/` 约定;单项目仓不再分项目域)。
21
- - checklist:`docs/checklist/active/dsh/`、`docs/checklist/archived/dsh/`。
22
- - 本插件文档只在本仓维护;`Context-Engineer` 不保留同名副本。
22
+ ## 修改原则
23
23
 
24
- ## 规划
24
+ - 纯 ESM、零构建;宿主编排入口 `index.js`,客户端 `client.js`;client bundle id 必须等于包名。
25
+ - 源码即产物,不提交 `lib/`;npm 包内容由 `package.json#files` 控制。
26
+ - 保持现有五家 provider 边界:adapter 只输出 `ProviderSnapshot`,VM 只输出 `SummaryVM`,UI 只消费 VM;不引入 provider 插件系统或 per-provider 文件拆分。
27
+ - 不做无关重构;改动范围以 plan / checklist 声明为准,越界先更新文档并确认。
28
+ - 日志用 `[TD:<module>]` 语义前缀;错误不吞,关键链路带 `errorKind` / `httpStatus`。
29
+ - 临时产物、一次性脚本和 MCP 输出统一放 `.tmp/`;不在仓库根或 `src/` 下新建临时文件。
25
30
 
26
- - 计划目录遵循全局 `plan-workflow`:`docs/plan/active/<feature>.md`、`docs/plan/tracking/<topic>/`、`docs/plan/archived/`。
27
- - 当前主计划:`docs/plan/active/feature_token_dash.md`。
31
+ ## Skill 归属
28
32
 
29
- ## 临时目录
33
+ - 项目 skill 只放在仓库 `.agents/skills/`,Codex 与 DSH 原生识别,不复制到用户层。
34
+ - 全局 skill 真源只放在 `D:\Projects\Context-Engineer\skills\`;`~/.agents/skills/<name>` 用 junction 指向 CE,不保留副本。
35
+ - 新增或修改全局 skill 在 CE 内完成,再建 junction;项目 skill 与全局 skill 不混放。
30
36
 
31
- - 统一使用仓库根 `.tmp/`,禁止在仓库根或 `src/` 下创建临时文件。
32
- - 子目录固定:`.tmp/dsh-home`(隔离 DSH_HOME)、`.tmp/pack`(tgz)、`.tmp/e2e`(截图 / 日志 / summary)。
33
- - 可用环境变量 `TOKEN_DASH_TMP` 覆盖;E2E 默认 teardown 清理,`--keep` 保留现场。
34
- - 测试工具脚本放 `scripts/`,本地与 CI 复用,不写进 `tests/`。
37
+ ## 提交前规则
35
38
 
36
- ## Git 管理边界
39
+ 1. 更新相关文档:plan / checklist / design / README,以改动范围为准。
40
+ 2. 执行受影响验证:L1 `npm test`,涉及 UI/宿主链路时加 `npm run e2e`。
41
+ 3. 涉及发布时再执行 `npm run pack`,确认 tarball 内容。
42
+ 4. 确认无 `.tmp/`、`build/`、真实会话或凭证进入提交。
43
+ 5. 再 `commit` + `push`;用户说“提交”默认包含推送,除非明确“只本地提交”。
37
44
 
38
- ### 进入版本管理
45
+ - 纯文档更新且不涉及代码、测试脚本、发布资产时,可跳过测试和打包,但提交说明必须写明“仅文档更新,未启动测试/打包”。
46
+ - 文档互引使用以仓库根为基准的相对路径。
47
+ - 完成本地打包后,立即回写当前 active plan 的进度;本地打包本身必须先有用户显式指令。
39
48
 
40
- | 类别 | 文件 |
41
- |---|---|
42
- | 包与配置 | `package.json`、`pnpm-lock.yaml`、`tsconfig.json`、`tsdown.config.ts`、`playwright.config.ts`、`cordis.patch.yml`、`.gitignore` |
43
- | 源码 | `src/**` |
44
- | 文案资源 | `locale/**`、`icon.svg` |
45
- | 测试脚本 | `tests/unit/**`、`tests/e2e/**/*.spec.ts`、`tests/e2e/fixtures/**`(仅合成数据) |
46
- | 工具脚本 | `scripts/**` |
47
- | 工程与发布 | `.github/workflows/**`、`README.md`、`LICENSE`、`AGENTS.md`、`CHANGELOG.md` |
48
- | 文档 | `docs/**` |
49
+ ## 版本与发布
49
50
 
50
- ### 不进入版本管理
51
+ - 版本号遵循 `MAJOR.MINOR.PATCH`;当前正式版本以 `.agents/skills/dsh-token-dash-release/references/official-release-version.md` 为准,不在此复制。
52
+ - 发版流程唯一来源:`docs/release.md` + `.agents/skills/dsh-token-dash-release/SKILL.md`;GitHub Release、npm Trusted Publishing 均由 tag 触发。
53
+ - **本地打包必须由用户显式指令触发**(`本地打包` / `本地版本` / `候选包`)。完成 issue、修复、测试或提交后,不得自动改版本号、写 CHANGELOG local 块、`npm run pack` 或安装本地包。
54
+ - 本地构建记录写 `CHANGELOG.md` 顶部 `## [X.Y.Z-local.N]`,标题下加 `> 本地构建,未发布`;不 tag、不发布。正式发布时合并进正式块并删除,最终历史不保留 local 块。
55
+ - 正式发布:确认版本 → CHANGELOG 正式块 → L1 + E2E → pack → commit/push → tag → 双 workflow → 核验 npm latest 与 Release body;随后更新 `official-release-version.md`。
56
+ - Release body 按 tag 版本从 CHANGELOG 提取,不按“文件第一块”提取;local 块不得进入 Release body。
57
+ - 未获用户明确授权,不执行 `npm publish`、不推 tag、不声称已上线。
51
58
 
52
- | 类别 | 文件 |
53
- |---|---|
54
- | 依赖与产物 | `node_modules/`、`lib/`、`dist/`、`build/` |
55
- | 临时目录 | `.tmp/`、`artifacts/`、`coverage/`、`playwright-report/`、`test-results/` |
56
- | 打包产物 | `*.tgz`、`*.tar.gz` |
57
- | 日志与缓存 | `*.log`、`.cache/` |
58
- | 敏感信息 | `.env`、`.env.*`、`*.pem`、`*.key`、`.credentials*` |
59
- | 系统与编辑器 | `.DS_Store`、`Thumbs.db`、`.vscode/` |
59
+ ## 规划工作流
60
60
 
61
- 规则:
61
+ - 复杂功能、跨模块修改、架构调整先用 `plan-workflow` skill 维护 `docs/plan/active/`;已归档内容只读,重开走受控流程。
62
+ - 同一时间只推进一个主 feature;优先恢复已有 plan,不新建重复计划。
63
+ - 施工与走查用 `checklist` skill 维护 `docs/checklist/active/dsh/`;回写完成 + 用户明确指令后才归档。
64
+ - 每次实现完成、测试通过或发布后,回写 plan 的 §3.0/§5 与 checklist 的阶段/测试/执行日志,避免状态只在对话里。
62
65
 
63
- 1. E2E **测试脚本与合成 fixture 必须入库**;截图、日志、报告、trace 一律忽略。
64
- 2. 真实 `$DSH_HOME/sessions`、真实会话 JSONL、任何凭证**永不入库**。
65
- 3. `pnpm-lock.yaml` 必须提交,保证可复现构建。
66
- 4. 提交前至少跑 `pnpm test` 与 `pnpm pack:check`。
66
+ ## Git 管理与临时文件
67
67
 
68
- ## 发布
69
-
70
- - AI 只准备到“可发布”,不执行 `npm publish`、不推 tag;发布由用户明确授权。
71
- - **本地打包必须由用户显式指令触发**(`本地打包` / `本地版本` / `候选包`)。完成 issue、修复、测试或提交后,不得自动改版本号、写 CHANGELOG local 块、`npm run pack` 或安装本地包。本地构建记录写 CHANGELOG 顶部 `-local.N` 块(中间产物);正式发布时合并进正式块并删除,最终历史不保留 local 块。
72
- - npm 包只含 `lib` / `cordis.patch.yml` / `locale` / `README.md` / `LICENSE`。
73
- - 发布流程见 `docs/release.md` 与 `.agents/skills/dsh-token-dash-release/SKILL.md`;历史发布 checklist 见 `docs/checklist/archived/dsh/token-dash-release-checklist.md`。
68
+ - 入库:`src/`、`client.js`、`index.js`、`test/`、`scripts/`、`docs/`、`locale/`、`.agents/`、`.github/`、`README*`、`CHANGELOG.md`、`AGENTS.md`、`package.json`、`pnpm-lock.yaml`、`cordis.patch.yml`。
69
+ - 不入库:`node_modules/`、`build/`、`.tmp/`、`artifacts/`、`coverage/`、`playwright-report/`、`test-results/`、`*.tgz`、`*.log`、真实会话 JSONL、凭证与密钥。
70
+ - E2E 测试脚本与合成 fixture 必须入库;截图、日志、报告、trace 一律忽略。
71
+ - `pnpm-lock.yaml` 必须提交,保证可复现安装。
74
72
 
75
73
  ## 安全
76
74
 
77
75
  - 不读取 `~/.dsh/.credentials.yaml`;凭据只经宿主 `ctx.credentials.resolve(...)`。
78
- - 余额查询仅限 DeepSeek / Kimi / GLM 官方接口;第三方显示“第三方不支持”且不请求。
76
+ - 余额查询仅限 DeepSeek / Kimi / GLM / OpenRouter / Qwen 官方接口;第三方显示“第三方不支持”且不请求。
79
77
  - 密钥、cookie、token 不得出现在源码、日志、fixture 或提交信息中。
80
78
 
81
- ## 变更纪律
79
+ ## 完成后的汇报
80
+
81
+ - 汇报“本次改了什么 / 测试改了什么”前,先读取对应 commit:`git show --stat <commit>`、`git show --name-status <commit>`,并核对受影响测试与文档 diff。
82
+ - 已提交内容、未提交工作区、运行态结果分开说明;不能用记忆或推测代替 commit 事实。
83
+ - 本轮尚未形成 commit 时,明确标注“未提交”,不得说成“已完成修改”。
82
84
 
83
- - 先更新 plan / checklist,再改代码;改动范围以施工稿声明为准。
84
- - 不顺手改边界外文件;需要越界时先更新文档并重新确认。
85
- - 新增依赖前说明理由;运行时依赖保持最少(当前仅 `zod`)。
package/README.md CHANGED
@@ -66,7 +66,7 @@ P2 · 浮层看板(pill 正上方弹出;卡片与行随 API Key 侦测动态
66
66
  │ Token 看板 刷新 设置 关闭│
67
67
  │ API 按量(已检测 3) 刚刚 │
68
68
  │ ┌──────────────────────────────────────────────────────┐ │
69
- │ │ DeepSeek 官方 预算 ¥91.90/¥100.00 · 91.9% 今日 余额 │ │
69
+ │ │ DeepSeek 预算 ¥91.90/¥100.00 · 91.9% 今日 余额 │ │
70
70
  │ │ [=========================================-------] │ │
71
71
  │ └──────────────────────────────────────────────────────┘ │
72
72
  │ ┌──────────────────────────────────────────────────────┐ │
@@ -85,13 +85,13 @@ P2 · 浮层看板(pill 正上方弹出;卡片与行随 API Key 侦测动态
85
85
  │ └────────────────────────────┘ └────────────────────────┘ │
86
86
  │ API 费用明细 / 本月 DSH 本机估算 │
87
87
  │ CNY 合计 ¥92.30 │
88
- │ DeepSeek 官方 / DeepSeek-V4.1-Flash 99.6% ¥91.90 │
88
+ │ DeepSeek / DeepSeek-V4.1-Flash 99.6% ¥91.90 │
89
89
  │ 通义千问 / Qwen3.8-Max 0.4% ¥0.40 │
90
90
  │ USD 合计 $22.80 │
91
91
  │ OpenRouter / DeepSeek-V4.1-Flash 100.0% $22.80 │
92
92
  │ 模型用量明细 / 本月 合计 1.31M│
93
93
  │ 模型 请求 输入 命中 输出 │
94
- │ DeepSeek 官方 / V4.1-Flash 48 0.11M 0.20M 0.02M│
94
+ │ DeepSeek / V4.1-Flash 48 0.11M 0.20M 0.02M│
95
95
  │ OpenRouter / V4.1-Flash 12 0.02M 0.01M 0.01M│
96
96
  │ 通义千问 / Qwen3.8-Max 24 0.05M 0.02M 0.01M│
97
97
  │ GLM Coding / GLM-5.3 64 0.30M 0.52M 0.04M│
@@ -102,7 +102,7 @@ P3 · 设置
102
102
  │ ← 返回 Token 看板 [关闭] │
103
103
  │ API 计费(已检测 3) │
104
104
  │ ┌──────────────────────────────────────────────────────┐ │
105
- │ │ DeepSeek 官方 月度预算 [ 100.00 ] CNY │ │
105
+ │ │ DeepSeek 月度预算 [ 100.00 ] CNY │ │
106
106
  │ │ OpenRouter 月度预算 [ 60.00 ] USD │ │
107
107
  │ │ 通义千问 月度预算 [ 5.00 ] CNY │ │
108
108
  │ └──────────────────────────────────────────────────────┘ │
package/client.js CHANGED
@@ -11,7 +11,7 @@ window.__ModuleLoader__.load({
11
11
 
12
12
  const API = '/token-dash/api'
13
13
  const PKG = 'dsh-token-dash'
14
- const COLORS = { primary: '#6ea8ff', warn: '#f0a020', over: '#e05a5a' }
14
+ const COLORS = { primary: 'var(--dsw-alias-state-business-primary, #6ea8ff)', warn: '#f0a020', over: '#e05a5a' }
15
15
  const budgetColor = (state) => (state === 'over' ? COLORS.over : state === 'warn' ? COLORS.warn : COLORS.primary)
16
16
  const percentColor = (value) => (value >= 100 ? COLORS.over : value >= 80 ? COLORS.warn : COLORS.primary)
17
17
 
@@ -104,7 +104,7 @@ window.__ModuleLoader__.load({
104
104
  titleBar: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8, minHeight: 36 },
105
105
  buttonGroup: { display: 'flex', gap: 12 },
106
106
  bar: { height: 6, borderRadius: 3, background: 'rgba(128,128,128,.25)', overflow: 'hidden', margin: '8px 0' },
107
- barFill: { height: '100%', background: '#6ea8ff' },
107
+ barFill: { height: '100%', background: COLORS.primary },
108
108
  barNeutral: { height: '100%', background: 'rgba(128,128,128,.45)' },
109
109
  cols: { display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 0, marginTop: 8 },
110
110
  subGrid: { display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(240px, 1fr))', gap: 8 },
@@ -336,8 +336,10 @@ window.__ModuleLoader__.load({
336
336
  h('span', null, `余额 ${balanceText(provider, true) ?? '--'}`))),
337
337
  h('div', { style: { ...styles.bar, marginTop: 6, marginBottom: 0 } },
338
338
  h('div', {
339
+ 'data-testid': `token-dash-budget-bar-${provider.id}`,
339
340
  style: {
340
- ...(hasBudget ? { background: color } : styles.barNeutral),
341
+ ...styles.barFill,
342
+ background: hasBudget ? color : styles.barNeutral.background,
341
343
  width: hasBudget ? `${Math.min(100, (budget.percent ?? 0) * 100)}%` : '100%',
342
344
  },
343
345
  })))
@@ -611,7 +613,7 @@ window.__ModuleLoader__.load({
611
613
  const res = await fetch(`${API}/update/install`, { method: 'POST', headers: { accept: 'application/json' } })
612
614
  const body = await res.json().catch(() => ({}))
613
615
  const data = body?.data
614
- if (body?.ok === true) setUpdate({ phase: 'done', text: '已更新,重启 DSH 后生效' })
616
+ if (body?.ok === true) setUpdate({ phase: 'done', text: data?.to ? `已更新到 ${data.to},重启 DSH 后生效` : '已更新,重启 DSH 后生效' })
615
617
  else setUpdate({ phase: 'error', text: `更新失败(${data?.reason ?? 'unknown'})`, command: data?.command })
616
618
  } catch (error) {
617
619
  setUpdate({ phase: 'error', text: `更新失败(${error.message})` })
package/index.js CHANGED
@@ -7,10 +7,11 @@ import { spawn } from 'node:child_process'
7
7
  import { dirname, join } from 'node:path'
8
8
  import { buildSummary, emptyState, num, repriceState } from './src/core.js'
9
9
  import { ingest, backfill } from './src/collect.js'
10
- import { createFlusher, dataDir, loadPrefs, loadState, logsDir, recoverState, saveJson, saveMonths } from './src/store.js'
10
+ import { createFlusher, dataDir, loadPrefs, loadState, logsDir, recoverState, resolveHome, saveJson, saveMonths } from './src/store.js'
11
11
  import { logAdapterEvents, readProviders, readSubscriptions } from './src/adapters.js'
12
12
  import { catalogInfo, loadCommunityPricing, loadRouterPricing } from './src/pricing.js'
13
13
  import { clearLog, createLogger, exportLog } from './src/diagnostics.js'
14
+ import { isNewer } from './src/version.js'
14
15
 
15
16
  export const name = 'dsh-token-dash'
16
17
 
@@ -19,20 +20,6 @@ const CURRENT_VERSION = JSON.parse(readFileSync(new URL('./package.json', import
19
20
  const PROVIDER_TTL = 30000
20
21
  let updateCache = { at: 0, data: null }
21
22
 
22
- function versionParts(value) {
23
- return String(value).split('.').map((part) => Number.parseInt(part, 10) || 0)
24
- }
25
-
26
- function isNewer(latest, current) {
27
- const left = versionParts(latest)
28
- const right = versionParts(current)
29
- for (let index = 0; index < Math.max(left.length, right.length); index += 1) {
30
- const diff = (left[index] ?? 0) - (right[index] ?? 0)
31
- if (diff !== 0) return diff > 0
32
- }
33
- return false
34
- }
35
-
36
23
  /** Locate the bundled dsh CLI (same layout scripts/e2e.mjs relies on). */
37
24
  function findDshCli() {
38
25
  const fromEnv = process.env.DSH_CLI
@@ -46,16 +33,40 @@ function findDshCli() {
46
33
  return null
47
34
  }
48
35
 
49
- /** Run `dsh plugin --profile <p> update dsh-token-dash`; resolves with { ok, reason?, command, output }. */
50
- function runPluginUpdate(profile) {
36
+ /** Installed version of this plugin inside a DSH profile; null when the manifest is unreadable. */
37
+ function installedVersion(home, profile) {
38
+ try {
39
+ const file = join(home, 'profiles', profile, 'node_modules', 'dsh-token-dash', 'package.json')
40
+ return JSON.parse(readFileSync(file, 'utf8')).version ?? null
41
+ } catch {
42
+ return null
43
+ }
44
+ }
45
+
46
+ /** Ask the npm registry for the published latest; 404 means the package is not published yet. */
47
+ async function fetchLatest() {
48
+ const response = await fetch('https://registry.npmjs.org/dsh-token-dash/latest', {
49
+ headers: { accept: 'application/json' },
50
+ signal: AbortSignal.timeout(5000),
51
+ })
52
+ if (response.status === 404) return { current: CURRENT_VERSION, latest: null, hasUpdate: false, published: false }
53
+ if (!response.ok) throw new Error(`registry-${response.status}`)
54
+ const meta = await response.json()
55
+ if (typeof meta?.version !== 'string') throw new Error('invalid-registry-response')
56
+ return { current: CURRENT_VERSION, latest: meta.version, hasUpdate: isNewer(meta.version, CURRENT_VERSION), published: true }
57
+ }
58
+
59
+ /** Run `dsh plugin --profile <p> add dsh-token-dash@<version> --save-exact`; resolves with { ok, reason?, command, output }. */
60
+ function runPluginUpdate(profile, version) {
51
61
  return new Promise((resolve) => {
52
- const command = `dsh plugin --profile ${profile} update dsh-token-dash`
62
+ const spec = `dsh-token-dash@${version}`
63
+ const command = `dsh plugin --profile ${profile} add ${spec} --save-exact`
53
64
  const cli = findDshCli()
54
65
  if (cli === null) {
55
66
  resolve({ ok: false, reason: 'cli-not-found', command, output: '' })
56
67
  return
57
68
  }
58
- const child = spawn(process.execPath, ['--expose-internals', cli, 'plugin', '--profile', profile, 'update', 'dsh-token-dash'], {
69
+ const child = spawn(process.execPath, ['--expose-internals', cli, 'plugin', '--profile', profile, 'add', spec, '--save-exact'], {
59
70
  env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' },
60
71
  windowsHide: true,
61
72
  })
@@ -287,13 +298,33 @@ export function apply(ctx) {
287
298
  }
288
299
  if (url.pathname === `${ROUTE_PREFIX}/update/install` && req.method === 'POST') {
289
300
  const profile = typeof process.env.DSH_PROFILE === 'string' && process.env.DSH_PROFILE !== '' ? process.env.DSH_PROFILE : 'desktop'
290
- const result = await runPluginUpdate(profile)
291
- logger.info('update', result.ok ? 'install.ok' : 'install.fail', {
301
+ const latest = (await fetchLatest()).latest
302
+ if (latest === null) {
303
+ logger.info('update', 'install.fail', { profile, reason: 'unpublished' })
304
+ json(res, 200, { ok: false, reason: 'unpublished', data: { reason: 'unpublished', command: '' } })
305
+ return
306
+ }
307
+ if (/^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/.test(latest) === false) throw new Error('invalid-latest-version')
308
+ const home = resolveHome(scoped)
309
+ const from = installedVersion(home, profile) ?? CURRENT_VERSION
310
+ const result = await runPluginUpdate(profile, latest)
311
+ const to = installedVersion(home, profile)
312
+ const changed = to === latest
313
+ const ok = result.ok === true && changed
314
+ const failReason = result.ok === true ? 'version-unchanged' : result.reason ?? 'install-failed'
315
+ logger.info('update', ok ? 'install.ok' : 'install.fail', {
292
316
  profile,
293
- reason: result.reason ?? null,
294
- ...(result.ok ? {} : { output: String(result.output ?? '').slice(-500) }),
317
+ from,
318
+ to,
319
+ reason: ok ? null : failReason,
320
+ ...(ok ? {} : { output: String(result.output ?? '').slice(-500) }),
321
+ })
322
+ updateCache = { at: 0, data: null }
323
+ json(res, 200, {
324
+ ok,
325
+ data: { ...result, from, to, latest, ...(ok ? {} : { reason: failReason }) },
326
+ ...(ok ? {} : { reason: failReason }),
295
327
  })
296
- json(res, 200, { ok: result.ok, data: result, ...(result.ok ? {} : { reason: 'install-failed' }) })
297
328
  return
298
329
  }
299
330
  if (url.pathname === `${ROUTE_PREFIX}/update` && req.method === 'GET') {
@@ -301,20 +332,7 @@ export function apply(ctx) {
301
332
  json(res, 200, { ok: true, data: updateCache.data })
302
333
  return
303
334
  }
304
- const response = await fetch('https://registry.npmjs.org/dsh-token-dash/latest', {
305
- headers: { accept: 'application/json' },
306
- signal: AbortSignal.timeout(5000),
307
- })
308
- if (response.status === 404) {
309
- const data = { current: CURRENT_VERSION, latest: null, hasUpdate: false, published: false }
310
- updateCache = { at: Date.now(), data }
311
- json(res, 200, { ok: true, data })
312
- return
313
- }
314
- if (!response.ok) throw new Error(`registry-${response.status}`)
315
- const meta = await response.json()
316
- if (typeof meta?.version !== 'string') throw new Error('invalid-registry-response')
317
- const data = { current: CURRENT_VERSION, latest: meta.version, hasUpdate: isNewer(meta.version, CURRENT_VERSION), published: true }
335
+ const data = await fetchLatest()
318
336
  updateCache = { at: Date.now(), data }
319
337
  json(res, 200, { ok: true, data })
320
338
  return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-token-dash",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "DSH Token 看板:API/订阅动态 provider、预算、配额、模型费用/用量与诊断日志。",
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/src/providers.js CHANGED
@@ -28,7 +28,7 @@ export const PROVIDER_ORDER = ['deepseek-official', 'zhipu', 'moonshot', 'openro
28
28
  const PROVIDERS = {
29
29
  'deepseek-official': {
30
30
  id: 'deepseek-official',
31
- label: 'DeepSeek 官方',
31
+ label: 'DeepSeek',
32
32
  currency: 'CNY',
33
33
  subscription: false,
34
34
  defaultModel: 'deepseek-v4.1-flash',
package/src/version.js ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Version ordering for the update check.
3
+ *
4
+ * Local builds are published as `X.Y.Z-local.N`; that is a prerelease of the same
5
+ * X.Y.Z, so `1.0.7-local.1 < 1.0.7` and local numbers grow numerically.
6
+ */
7
+
8
+ function parseVersion(value) {
9
+ const text = String(value ?? '')
10
+ const match = /^(\d+)\.(\d+)\.(\d+)(?:-local\.(\d+))?$/.exec(text)
11
+ if (match === null) {
12
+ const parts = text.split('.').map((part) => Number.parseInt(part, 10) || 0)
13
+ return { base: [parts[0] ?? 0, parts[1] ?? 0, parts[2] ?? 0], local: null }
14
+ }
15
+ return {
16
+ base: [Number(match[1]), Number(match[2]), Number(match[3])],
17
+ local: match[4] === undefined ? null : Number(match[4]),
18
+ }
19
+ }
20
+
21
+ /** -1 / 0 / 1 for left older than / equal to / newer than right. */
22
+ export function compareVersions(left, right) {
23
+ const a = parseVersion(left)
24
+ const b = parseVersion(right)
25
+ for (let index = 0; index < 3; index += 1) {
26
+ if (a.base[index] !== b.base[index]) return a.base[index] > b.base[index] ? 1 : -1
27
+ }
28
+ if (a.local === null && b.local === null) return 0
29
+ if (a.local === null) return 1
30
+ if (b.local === null) return -1
31
+ return a.local === b.local ? 0 : a.local > b.local ? 1 : -1
32
+ }
33
+
34
+ /** True when `latest` is strictly newer than `current`. */
35
+ export function isNewer(latest, current) {
36
+ return compareVersions(latest, current) > 0
37
+ }