@feiyang666/dsh-usage-plugin 1.9.4 → 1.10.0

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
@@ -23,6 +23,15 @@
23
23
 
24
24
  ---
25
25
 
26
+ ## v1.10.0 (2026-08-23)
27
+
28
+ - **新增 DeepSeek 官方模型 `deepseek-v4-flash-vision-exp`(DeepSeek-V4-Flash-Vision-Exp)**:官方价格表、概览「按模型」表、按 API 服务商 × 模型明细、用量日历、缓存命中列表与 PNG 报告均自动按该模型计价;「价格表」页新增该模型一行,`modelKey` 识别优先匹配 `vision`(避免被误归入 `deepseek-v4-flash`)。
29
+ - **价格**(元 / 百万 tokens,与官方最新公布一致):空闲时段 缓存命中 0.05 / 输入未命中 1.5 / 输出 4.5;高峰时段 缓存命中 0.10 / 输入未命中 3.0 / 输出 9.0(与 `deepseek-v4-flash` 同价)。
30
+ - **核对确认**:`deepseek-v4-flash` / `deepseek-v4-pro` 的峰谷价与官方最新公布完全一致,无价格变动;发送给该模型的图片按其尺寸换算成 token,与文本 token 一并计费。
31
+ - **周末统一按空闲(低谷)价计费(2026-08-23 00:00 起)**:新规则生效后,周末(周六、周日)全天不再区分峰谷时段,统一按空闲价计费;工作日仍按北京时间 9:00–12:00、14:00–18:00 高峰分段。新规则生效前的调用仍按原规则结算。插件的高峰时段判定、面板「当前时段」徽标、概览 / 日历 / 缓存列表的高峰与空闲分列统计、PNG 报告均按新规则自动判断。
32
+
33
+ ---
34
+
26
35
  ## v1.9.4 (2026-08-20)
27
36
 
28
37
  - **持久化不再落到工作目录(修复「数据又进了工作区」的反馈)**:之前插件的落盘走的是模型侧沙箱化的 `fs` 服务,在 `workspace-write` 模式下会被限制只能写工作区;当系统/用户目录因沙箱不可写时,数据会兜底写进 `<工作区>/dsh-usage`,于是切换工作区又出现「数据没了」的现象(即 issue #4 的本质)。
package/README.md CHANGED
@@ -32,10 +32,10 @@ dsh-usage-plugin is a **usage & cost tracker** plugin in the DeepSeek Harness ec
32
32
 
33
33
  > Supports **Windows / macOS / Linux**: paths are handled per platform (`node:path`), and the folder picker / "reveal in file manager" use each OS's native mechanism (macOS: `osascript` / `open`; Linux: `zenity` / `xdg-open`). Balance query and export do not depend on Windows-only commands.
34
34
 
35
- - **Usage & Cost**: records each model call's token usage and cache hits (input miss / cache hit / cache write / output / reasoning / finish reason), and computes cost using DeepSeek's peak/valley or base pricing (peak hours are automatically priced by Beijing time 09:00–12:00 and 14:00–18:00). Model names come from the actual request parameters, so non-DeepSeek models are shown truthfully instead of "unknown model"; models without an official price are counted as 0. The overview shows a by-model table plus a by-API-provider × model drill-down (each provider grouped with every model's calls and peak/off-peak cost split) and a grand total row. The overview also supports **date filtering** (Today / Last 7 days / Last 30 days / All, plus a custom start–end range), so the aggregate stats can be scoped to any single day or date range.
35
+ - **Usage & Cost**: records each model call's token usage and cache hits (input miss / cache hit / cache write / output / reasoning / finish reason), and computes cost using DeepSeek's peak/valley or base pricing (peak hours on weekdays are automatically priced by Beijing time 09:00–12:00 and 14:00–18:00; since 2026-08-23 weekends are billed entirely at the off-peak rate). Model names come from the actual request parameters, so non-DeepSeek models are shown truthfully instead of "unknown model"; models without an official price are counted as 0. The overview shows a by-model table plus a by-API-provider × model drill-down (each provider grouped with every model's calls and peak/off-peak cost split) and a grand total row. The overview also supports **date filtering** (Today / Last 7 days / Last 30 days / All, plus a custom start–end range), so the aggregate stats can be scoped to any single day or date range.
36
36
  - **Usage Calendar**: a monthly daily-usage heatmap (colored by cost or call count), hover for details including the peak/off-peak cost split, click a day for its call list and peak/off-peak totals, plus a per-day statistics table with peak cost / off-peak cost / total columns and monthly rollups.
37
37
  - **Cache Hit List**: newest-first, fully scrollable, with quick filters (Today / 7 days / 30 days / All) and custom date ranges; the summary line and footer total split peak vs off-peak consumption with a grand cost total. The list is paginated (100 rows per page), so it stays smooth even with large data volumes.
38
- - **Price Table**: the official DeepSeek API price table — base and peak/valley unit prices shown side by side (peak vs off-peak), editable in-panel and persisted to `pricing.json`, with a reset-to-default option.
38
+ - **Price Table**: the official DeepSeek API price table (covering `deepseek-v4-flash` / `deepseek-v4-flash-vision-exp` / `deepseek-v4-pro`) — base and peak/valley unit prices shown side by side (peak vs off-peak), editable in-panel and persisted to `pricing.json`, with a reset-to-default option.
39
39
  - **Balance Query**: queries your DeepSeek account balance using the configured `DEEPSEEK_API_KEY`.
40
40
  - **Export**: CSV / JSON / **PNG long image** (newest-first, up to the latest 2000 records, warns if exceeded; the PNG report includes peak/off-peak cost columns), to any directory (native picker), auto-opens the folder after export.
41
41
  - **Import**: merge-imports JSON / CSV files, deduplicated by time.
package/README.zh.md CHANGED
@@ -32,10 +32,10 @@ dsh-usage-plugin 是 DeepSeek Harness 生态的**用量与消耗统计插件**
32
32
 
33
33
  > 支持 **Windows / macOS / Linux**:路径按当前平台处理(`node:path`),目录选择与「打开所在目录」均调用系统原生方式(macOS 用 `osascript` / `open`,Linux 用 `zenity` / `xdg-open`),余额查询与导出不依赖 Windows 专用命令。
34
34
 
35
- - **用量与消耗**:记录每次模型调用的 token 用量与缓存命中(输入·未命中 / 缓存命中 / 缓存写入 / 输出 / 推理 / 结束原因),按 DeepSeek 峰谷/基础价格计算消耗(高峰时段自动按北京时间 9:00–12:00、14:00–18:00 计价)。模型名以请求参数为准如实显示(非 DeepSeek 模型不再显示为「未知模型」,无官方价格的模型消耗按 0 统计)。概览含「按模型」表与「按 API 服务商 × 模型」明细表(每个服务商一组,组内列出各模型的调用与高峰/空闲分列消耗),底部有总费用合计。概览还支持**日期筛选**(今天 / 近 7 天 / 近 30 天 / 全部,外加自定义起止区间),可将聚合统计限定到任意一天或日期范围。
35
+ - **用量与消耗**:记录每次模型调用的 token 用量与缓存命中(输入·未命中 / 缓存命中 / 缓存写入 / 输出 / 推理 / 结束原因),按 DeepSeek 峰谷/基础价格计算消耗(工作日高峰时段按北京时间 9:00–12:00、14:00–18:00 计价;自 2026-08-23 起周末全天按空闲价计费)。模型名以请求参数为准如实显示(非 DeepSeek 模型不再显示为「未知模型」,无官方价格的模型消耗按 0 统计)。概览含「按模型」表与「按 API 服务商 × 模型」明细表(每个服务商一组,组内列出各模型的调用与高峰/空闲分列消耗),底部有总费用合计。概览还支持**日期筛选**(今天 / 近 7 天 / 近 30 天 / 全部,外加自定义起止区间),可将聚合统计限定到任意一天或日期范围。
36
36
  - **用量日历**:按月查看每日用量热力图(按消耗或调用数着色),悬停查看详情(含高峰 / 空闲消耗拆分)、点击某天查看当日调用明细与高峰/空闲消耗统计,附本月每日统计表(高峰消耗 / 空闲消耗 / 总消耗分列)与月度汇总。
37
37
  - **缓存命中列表**:最新记录排在最前,支持 今天 / 近7天 / 近30天 / 全部 快捷筛选与自定义起止日期区间,汇总行与表尾合计区分高峰消耗 / 空闲消耗 / 总费用合计;列表分页渲染(每页 100 条),记录量大也不卡顿。
38
- - **价格表**:**DeepSeek 官方 API 价格表**,展示基础价与峰谷价(高峰/空闲)单价表,高峰价与空闲价分列展示,支持在面板内直接编辑价格并持久化(数据目录 `pricing.json`),也可一键恢复默认。
38
+ - **价格表**:**DeepSeek 官方 API 价格表**(覆盖官方模型 `deepseek-v4-flash` / `deepseek-v4-flash-vision-exp` / `deepseek-v4-pro`),展示基础价与峰谷价(高峰/空闲)单价表,高峰价与空闲价分列展示,支持在面板内直接编辑价格并持久化(数据目录 `pricing.json`),也可一键恢复默认。
39
39
  - **剩余余额查询**:用当前配置的 `DEEPSEEK_API_KEY` 查询 DeepSeek 账户余额。
40
40
  - **导出**:CSV / JSON / **PNG 长图**(按最新在前展示,最多含最近 2000 条,超出会提示;PNG 报告含高峰 / 空闲消耗分列统计),可导出到任意目录(原生目录选择器),导出后自动打开所在目录。
41
41
  - **导入**:选择文件(JSON / CSV)合并导入,按时间去重。
package/lib/client.js CHANGED
@@ -87,7 +87,7 @@ window.__ModuleLoader__.load({
87
87
  var fmtTime = function (ts) { var d = new Date(ts + 8 * 3600 * 1000); return pad2(d.getUTCMonth() + 1) + "-" + pad2(d.getUTCDate()) + " " + pad2(d.getUTCHours()) + ":" + pad2(d.getUTCMinutes()) + ":" + pad2(d.getUTCSeconds()); };
88
88
  var fmtMoney = function (n) { if (!n) return "¥0.0000"; if (n < 0.0001) return "¥" + n.toExponential(2); if (n < 1) return "¥" + n.toFixed(4); return "¥" + n.toFixed(2); };
89
89
  var fmtPrice = function (n) { if (n == null || isNaN(n)) return "—"; return String(parseFloat(Number(n).toFixed(4))); };
90
- var modelLabel = function (mk) { if (mk === "deepseek-v4-flash") return "deepseek-v4-flash"; if (mk === "deepseek-v4-pro") return "deepseek-v4-pro"; return "未知模型"; };
90
+ var modelLabel = function (mk) { if (mk === "deepseek-v4-flash") return "deepseek-v4-flash"; if (mk === "deepseek-v4-flash-vision-exp") return "deepseek-v4-flash-vision-exp"; if (mk === "deepseek-v4-pro") return "deepseek-v4-pro"; return "未知模型"; };
91
91
  // 模型显示名:以请求参数里的真实模型名为准(非 DeepSeek 模型也如实显示),
92
92
  // 空模型名时退回已知档位名 / provider / 未知。
93
93
  var modelName = function (r) {
@@ -118,7 +118,15 @@ window.__ModuleLoader__.load({
118
118
  };
119
119
  var finishLabel = function (f) { if (f === "stop") return "完成"; if (f === "tool-calls") return "工具调用"; if (f === "max-tokens") return "超长"; if (f === "error") return "错误"; return f || "—"; };
120
120
  var pct = function (a, b) { return b > 0 ? (a / b * 100).toFixed(1) + "%" : "—"; };
121
- var isPeakNow = function (ts) { var d = new Date(ts + 8 * 3600 * 1000); var t = d.getUTCHours() * 60 + d.getUTCMinutes(); return (t >= 9 * 60 && t < 12 * 60) || (t >= 14 * 60 && t < 18 * 60); };
121
+ // 周末统一按空闲价规则生效时间:北京时间 2026-08-23 00:00(周日)。此前(含周末)仍按原峰谷分段。
122
+ var WEEKEND_FLAT_AT = Date.parse("2026-08-23T00:00:00+08:00");
123
+ var isPeakNow = function (ts) {
124
+ var d = new Date(ts + 8 * 3600 * 1000);
125
+ var wd = d.getUTCDay(); // 0=周日 1=周一 … 6=周六
126
+ if (ts >= WEEKEND_FLAT_AT && (wd === 0 || wd === 6)) return false;
127
+ var t = d.getUTCHours() * 60 + d.getUTCMinutes();
128
+ return (t >= 9 * 60 && t < 12 * 60) || (t >= 14 * 60 && t < 18 * 60);
129
+ };
122
130
  // 计费档位选择:auto 用 autoCost(按生效日期自动切换),base 用 baseCost,其余用 peakValleyCost
123
131
  // 计费档位显示文案
124
132
  var regimeLabel = function (regime) {
@@ -175,7 +183,7 @@ window.__ModuleLoader__.load({
175
183
  var api = function (payload) {
176
184
  return fetch("/usage/api", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload) }).then(function (r) { return r.json(); });
177
185
  };
178
- var PRICE_MODELS = ["deepseek-v4-flash", "deepseek-v4-pro"];
186
+ var PRICE_MODELS = ["deepseek-v4-flash", "deepseek-v4-flash-vision-exp", "deepseek-v4-pro"];
179
187
 
180
188
  // ── Card ──
181
189
  function Card(props) {
@@ -567,7 +575,7 @@ window.__ModuleLoader__.load({
567
575
  el(Card, { label: "输入 · 未命中", value: fmtInt(totalMiss), hint: "token" }),
568
576
  el(Card, { label: "输入 · 缓存命中", value: fmtInt(totalHit), hint: "命中率 " + hitRate }),
569
577
  el(Card, { label: "输出", value: fmtInt(totalOut), hint: "token" }),
570
- el(Card, { label: "高峰消耗", value: fmtMoney(peakTotal), hint: "高峰时段 9:00–12:00、14:00–18:00" }),
578
+ el(Card, { label: "高峰消耗", value: fmtMoney(peakTotal), hint: "工作日高峰 9:00–12:00、14:00–18:00 · 周末全天空闲价" }),
571
579
  el(Card, { label: "空闲消耗", value: fmtMoney(offTotal), hint: "其余空闲时段" }),
572
580
  el(Card, { label: "总消耗", value: fmtMoney(totalCost), hint: "高峰 + 空闲 · " + regimeLabel(regime) })
573
581
  ),
@@ -755,7 +763,7 @@ window.__ModuleLoader__.load({
755
763
  el(Card, { label: "输入 · 未命中", value: fmtInt(monthMiss), hint: "token" }),
756
764
  el(Card, { label: "输入 · 缓存命中", value: fmtInt(monthHit), hint: "token" }),
757
765
  el(Card, { label: "输出", value: fmtInt(monthOut), hint: "token" }),
758
- el(Card, { label: "高峰消耗", value: fmtMoney(monthPeak), hint: "高峰时段 9:00–12:00、14:00–18:00" }),
766
+ el(Card, { label: "高峰消耗", value: fmtMoney(monthPeak), hint: "工作日高峰 9:00–12:00、14:00–18:00 · 周末全天空闲价" }),
759
767
  el(Card, { label: "空闲消耗", value: fmtMoney(monthOff), hint: "其余空闲时段" }),
760
768
  el(Card, { label: "本月消耗", value: fmtMoney(monthCost), hint: "高峰 + 空闲 · " + costHint(regime) })
761
769
  ),
@@ -1018,7 +1026,7 @@ window.__ModuleLoader__.load({
1018
1026
  el("div", { style: st.note, marginTop: 6 }, effectiveIn()),
1019
1027
  regime === "auto"
1020
1028
  ? el("div", { style: st.note, marginTop: 6 },
1021
- "自动模式:按调用时间自动选择计费档位——新价格表生效前的调用按基础价(旧价格表),生效后的调用按峰谷价(高峰时段 9:00–12:00、14:00–18:00 用高峰价,其余空闲时段用空闲价)。当前显示:",
1029
+ "自动模式:按调用时间自动选择计费档位——新价格表生效前的调用按基础价(旧价格表),生效后的调用按峰谷价(工作日高峰时段 9:00–12:00、14:00–18:00 用高峰价,其余时间——含周末全天——用空闲价)。当前显示:",
1022
1030
  el("strong", null, showRegime(regime) === "base" ? "基础价表(旧价格表,新价格生效前)" : "峰谷价表(新价格表)"),
1023
1031
  "。价格按官方公布固定,不可编辑。"
1024
1032
  )
@@ -1043,7 +1051,7 @@ window.__ModuleLoader__.load({
1043
1051
  )
1044
1052
  ),
1045
1053
  el("div", { style: st.note, marginTop: 8 },
1046
- "单位:元 / 百万 tokens。高峰时段(北京时间 9:00–12:00、14:00–18:00)用高峰价,其余空闲时段用空闲价(空闲价 = 高峰价的一半)。概览、用量日历、缓存命中列表中的消耗均已按高峰 / 空闲分列统计。"
1054
+ "单位:元 / 百万 tokens。工作日高峰时段(北京时间 9:00–12:00、14:00–18:00)用高峰价,其余时间用空闲价(空闲价 = 高峰价的一半);自 2026-08-23 起周末(周六、周日)全天按空闲价计费,新规则生效前仍按原规则。概览、用量日历、缓存命中列表中的消耗均已按高峰 / 空闲分列统计。"
1047
1055
  ),
1048
1056
  el("div", { style: st.sec, marginTop: 18 }, "第三方平台价格与覆盖状态(2026-08-19 核验)"),
1049
1057
  el("div", { style: st.note, marginTop: 6 }, "只把能与记录中的 provider/model 可靠匹配的价格用于自动计费;美元价格先按 USD 计算,再乘 USD/CNY 汇率统一折算成人民币。"),
package/lib/index.js CHANGED
@@ -49,6 +49,7 @@ export default {
49
49
  const PRICING = {
50
50
  base: {
51
51
  'deepseek-v4-flash': { cacheHit: 0.02, cacheMiss: 1.0, output: 2.0 },
52
+ 'deepseek-v4-flash-vision-exp': { cacheHit: 0.02, cacheMiss: 1.0, output: 2.0 },
52
53
  'deepseek-v4-pro': { cacheHit: 0.025, cacheMiss: 3.0, output: 6.0 }
53
54
  },
54
55
  peakValley: {
@@ -56,6 +57,10 @@ export default {
56
57
  offPeak: { cacheHit: 0.05, cacheMiss: 1.5, output: 4.5 },
57
58
  peak: { cacheHit: 0.1, cacheMiss: 3.0, output: 9.0 }
58
59
  },
60
+ 'deepseek-v4-flash-vision-exp': {
61
+ offPeak: { cacheHit: 0.05, cacheMiss: 1.5, output: 4.5 },
62
+ peak: { cacheHit: 0.1, cacheMiss: 3.0, output: 9.0 }
63
+ },
59
64
  'deepseek-v4-pro': {
60
65
  offPeak: { cacheHit: 0.15, cacheMiss: 4.5, output: 13.5 },
61
66
  peak: { cacheHit: 0.3, cacheMiss: 9.0, output: 27.0 }
@@ -63,7 +68,7 @@ export default {
63
68
  }
64
69
  }
65
70
  const DEFAULT_PRICING = JSON.parse(JSON.stringify(PRICING))
66
- const PRICE_MODELS = ['deepseek-v4-flash', 'deepseek-v4-pro']
71
+ const PRICE_MODELS = ['deepseek-v4-flash', 'deepseek-v4-flash-vision-exp', 'deepseek-v4-pro']
67
72
  const SILICONFLOW_PRICING = {
68
73
  'deepseek-ai/deepseek-v4-flash': { cacheHit: 0.02, cacheMiss: 1.0, output: 2.0 },
69
74
  'deepseek-ai/deepseek-v4-pro': { cacheHit: 1.0, cacheMiss: 12.0, output: 24.0 },
@@ -80,16 +85,24 @@ export default {
80
85
  // 新价格表(峰谷价)生效时间:北京时间 2026-08-17 00:00。
81
86
  // 在此之前的调用按旧价格表(基础价 base)计费;之后按新价格表(峰谷价)计费。
82
87
  const EFFECTIVE_AT = Date.parse('2026-08-17T00:00:00+08:00')
88
+ // 周末统一按低谷(空闲)价计费规则生效时间:北京时间 2026-08-23 00:00(周日)。
89
+ // 生效前(含周末)仍按原峰谷分段计费;生效后周末(周六、周日)全天统一按空闲价,工作日仍按峰谷分段。
90
+ const WEEKEND_FLAT_AT = Date.parse('2026-08-23T00:00:00+08:00')
83
91
 
84
92
  function modelKey(model) {
85
93
  const m = String(model || '').toLowerCase()
94
+ if (m.indexOf('vision') >= 0) return 'deepseek-v4-flash-vision-exp'
86
95
  if (m.indexOf('flash') >= 0) return 'deepseek-v4-flash'
87
96
  if (m.indexOf('pro') >= 0) return 'deepseek-v4-pro'
88
97
  return 'unknown'
89
98
  }
90
99
 
100
+ // 高峰时段判定:新规则(2026-08-23 起)周末(周六、周日)全天不再区分峰谷,统一按空闲价;
101
+ // 工作日仍按北京时间 9:00–12:00、14:00–18:00 为高峰;新规则生效前的调用仍按原规则判定。
91
102
  function isPeak(ts) {
92
103
  const d = new Date(ts + 8 * 3600 * 1000)
104
+ const wd = d.getUTCDay() // 0=周日 1=周一 … 6=周六
105
+ if (ts >= WEEKEND_FLAT_AT && (wd === 0 || wd === 6)) return false
93
106
  const t = d.getUTCHours() * 60 + d.getUTCMinutes()
94
107
  return (t >= 9 * 60 && t < 12 * 60) || (t >= 14 * 60 && t < 18 * 60)
95
108
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feiyang666/dsh-usage-plugin",
3
- "version": "1.9.4",
3
+ "version": "1.10.0",
4
4
  "description": "DeepSeek Harness usage & cost tracker plugin: per-call token/cache-hit stats, peak/off-peak billing, DeepSeek balance query, CSV/JSON/PNG export with custom destination, and persistent local storage. Ships a host half plus a web client half in one npm package; installs into a DSH profile as a dsh.bundle with one command (dsh plugin --profile web add @feiyang666/dsh-usage-plugin).",
5
5
  "keywords": [
6
6
  "deepseek",
@@ -0,0 +1,94 @@
1
+ import { spawnSync } from 'node:child_process'
2
+ import https from 'node:https'
3
+ import fs from 'node:fs'
4
+ import path from 'node:path'
5
+
6
+ const REPO = 'feiyang-dev/dsh-usage-plugin'
7
+ const TAG = 'v1.10.0'
8
+
9
+ function getCredential() {
10
+ const input = 'protocol=https\nhost=github.com\n\n'
11
+ const res = spawnSync('git', ['credential', 'fill'], { input, encoding: 'utf8' })
12
+ const cred = {}
13
+ for (const l of (res.stdout || '').split('\n')) {
14
+ const idx = l.indexOf('=')
15
+ if (idx > 0) cred[l.slice(0, idx).trim()] = l.slice(idx + 1).trim()
16
+ }
17
+ return cred
18
+ }
19
+
20
+ function api(method, apiPath, body, contentType = 'application/json', host = 'api.github.com') {
21
+ return new Promise((resolve) => {
22
+ const cred = getCredential()
23
+ const isBuffer = Buffer.isBuffer(body)
24
+ const req = https.request({
25
+ host,
26
+ path: apiPath,
27
+ method,
28
+ headers: {
29
+ 'Authorization': `Bearer ${cred.password || ''}`,
30
+ 'User-Agent': 'dsh-usage-plugin-publish',
31
+ 'Accept': 'application/vnd.github+json',
32
+ 'Content-Type': contentType,
33
+ ...(body != null ? { 'Content-Length': isBuffer ? body.length : Buffer.byteLength(body) } : {}),
34
+ },
35
+ rejectUnauthorized: false,
36
+ }, (res) => {
37
+ let data = ''
38
+ res.on('data', (d) => data += d)
39
+ res.on('end', () => resolve({ status: res.statusCode, body: data }))
40
+ })
41
+ req.on('error', (e) => resolve({ status: 0, body: e.message }))
42
+ if (body != null) req.write(body)
43
+ req.end()
44
+ })
45
+ }
46
+
47
+ function extractChangelog(tag) {
48
+ const text = fs.readFileSync(path.join(process.cwd(), 'CHANGELOG.md'), 'utf8')
49
+ const lines = text.split('\n')
50
+ const re = new RegExp('^## ' + tag.replace(/\./g, '\\.') + '\\b')
51
+ const start = lines.findIndex((l) => re.test(l))
52
+ if (start < 0) return ''
53
+ let end = lines.length
54
+ for (let i = start + 1; i < lines.length; i++) {
55
+ if (/^## v\d/.test(lines[i])) { end = i; break }
56
+ }
57
+ return lines.slice(start, end).join('\n').trim()
58
+ }
59
+
60
+ const body = extractChangelog(TAG)
61
+ if (!body) { console.error('changelog section not found for', TAG); process.exit(1) }
62
+
63
+ console.log('Creating release', TAG, 'for', REPO)
64
+ const created = await api('POST', `/repos/${REPO}/releases`, JSON.stringify({
65
+ tag_name: TAG,
66
+ name: TAG,
67
+ body,
68
+ draft: false,
69
+ prerelease: false,
70
+ }))
71
+ console.log('create status:', created.status)
72
+ if (created.status !== 201) {
73
+ console.error('create failed:', created.body.slice(0, 600))
74
+ process.exit(1)
75
+ }
76
+ const release = JSON.parse(created.body)
77
+ console.log('release id:', release.id, 'url:', release.html_url)
78
+
79
+ // 打包并上传 tarball 作为附件
80
+ const pack = spawnSync('npm', ['pack'], { encoding: 'utf8' })
81
+ const tgzName = (pack.stdout || '').trim().split('\n').pop()
82
+ if (tgzName && fs.existsSync(tgzName)) {
83
+ const buf = fs.readFileSync(tgzName)
84
+ const assetPath = `/repos/${REPO}/releases/${release.id}/assets?name=${encodeURIComponent(tgzName)}`
85
+ console.log('uploading asset', tgzName, buf.length)
86
+ const asset = await api('POST', assetPath, buf, 'application/octet-stream', 'uploads.github.com')
87
+ console.log('asset status:', asset.status)
88
+ if (asset.status === 201) console.log('asset url:', JSON.parse(asset.body).browser_download_url)
89
+ else console.error('asset body:', asset.body.slice(0, 400))
90
+ try { fs.unlinkSync(tgzName) } catch (e) {}
91
+ } else {
92
+ console.log('no tarball produced; skip asset upload')
93
+ }
94
+ console.log('DONE')
@@ -0,0 +1,50 @@
1
+ import { spawnSync } from 'node:child_process'
2
+ import https from 'node:https'
3
+ import fs from 'node:fs'
4
+
5
+ const REPO = 'feiyang-dev/dsh-usage-plugin'
6
+ const RELEASE_ID = 373691852
7
+
8
+ function getCredential() {
9
+ const input = 'protocol=https\nhost=github.com\n\n'
10
+ const res = spawnSync('git', ['credential', 'fill'], { input, encoding: 'utf8' })
11
+ const cred = {}
12
+ for (const l of (res.stdout || '').split('\n')) {
13
+ const idx = l.indexOf('=')
14
+ if (idx > 0) cred[l.slice(0, idx).trim()] = l.slice(idx + 1).trim()
15
+ }
16
+ return cred
17
+ }
18
+
19
+ function api(method, apiPath, body, contentType, host = 'api.github.com') {
20
+ return new Promise((resolve) => {
21
+ const cred = getCredential()
22
+ const isBuffer = Buffer.isBuffer(body)
23
+ const req = https.request({
24
+ host, path: apiPath, method,
25
+ headers: {
26
+ 'Authorization': `Bearer ${cred.password || ''}`,
27
+ 'User-Agent': 'dsh-usage-plugin-publish',
28
+ 'Accept': 'application/vnd.github+json',
29
+ 'Content-Type': contentType,
30
+ ...(body != null ? { 'Content-Length': isBuffer ? body.length : Buffer.byteLength(body) } : {}),
31
+ },
32
+ rejectUnauthorized: false,
33
+ }, (res) => { let d = ''; res.on('data', (c) => d += c); res.on('end', () => resolve({ status: res.statusCode, body: d })) })
34
+ req.on('error', (e) => resolve({ status: 0, body: e.message }))
35
+ if (body != null) req.write(body)
36
+ req.end()
37
+ })
38
+ }
39
+
40
+ const tgz = fs.readdirSync('.').find((f) => f.endsWith('.tgz'))
41
+ if (!tgz) { console.error('no tarball found'); process.exit(1) }
42
+ const buf = fs.readFileSync(tgz)
43
+ const assetPath = `/repos/${REPO}/releases/${RELEASE_ID}/assets?name=${encodeURIComponent(tgz)}`
44
+ console.log('uploading', tgz, buf.length)
45
+ const asset = await api('POST', assetPath, buf, 'application/octet-stream', 'uploads.github.com')
46
+ console.log('asset status:', asset.status)
47
+ if (asset.status === 201) console.log('asset url:', JSON.parse(asset.body).browser_download_url)
48
+ else console.error('asset body:', asset.body.slice(0, 400))
49
+ try { fs.unlinkSync(tgz) } catch (e) {}
50
+ console.log('DONE')