dsh-dpharness 0.6.9 → 0.7.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/lib/changelog.js CHANGED
@@ -37,6 +37,25 @@ export function npmLatestUrl(pkg) {
37
37
  * (用户要看的是行为变化,不是我们的 commit 分类)。
38
38
  */
39
39
  export const CHANGELOG = [
40
+ {
41
+ version: "0.7.0",
42
+ date: "2026-10-30",
43
+ title: "安装与使用情况可以按客户端分开统计了",
44
+ items: [
45
+ "在插件目录里点「安装」并真正装上之后,会上报一条匿名记录(包名 + 你所在的客户端类型)。本站据此能区分「官方 Web / 官方桌面 / 第三方桌面」各装了什么。",
46
+ "新增「用过没有」的匿名信号:某个已装插件在最近 30 天里被实际调用过,就上报一次它的包名。**只报有没有用过,不报调用次数、不报会话内容、不报搜索词。**",
47
+ "这两项上报同样受面板里的「匿名统计」开关控制:关掉之后,浏览器和本地宿主都不会再发出任何一条。开关状态会随页面加载同步给宿主,不会出现「关了还在发」。",
48
+ ],
49
+ },
50
+ {
51
+ version: "0.6.10",
52
+ date: "2026-10-27",
53
+ title: "「今日推荐」点开分享后,卡片不再被撑破",
54
+ items: [
55
+ "侧栏今日推荐卡里点「分享」:按钮不再溢出卡片右边界,「复制命令」也不会被挤到下一行。收起留在原位,复制链接 / 复制文案 / 说明图各占一格。",
56
+ "窗口拉窄成单列时,命令条改成真正的一行排开,不再出现半屏宽的按钮。",
57
+ ],
58
+ },
40
59
  {
41
60
  version: "0.6.9",
42
61
  date: "2026-10-24",
package/lib/client.js CHANGED
@@ -331,10 +331,26 @@ window.__ModuleLoader__.load({
331
331
  ".dph_cardFav{border-left:3px solid var(--dsw-alias-state-business-primary);padding-left:11px}",
332
332
  /* 选中卡里的主按钮改回白底:浅蓝底 + 蓝描边按钮叠在一起会糊成一片 */
333
333
  ".dph_cardOn .dph_btnPrimary{background:var(--dsw-alias-bg-layer-1)}",
334
- /* 右栏命令条是两列网格:命令跨两列,复制/分享各占一格(展开态自然排成 2×2) */
334
+ /* 右栏命令条是两列网格:命令跨两列,复制/分享各占一格 */
335
335
  ".dph_cmdbarCol .dph_cmdbarCode{grid-column:1/-1;font-size:11.5px}",
336
336
  ".dph_cmdbarCol .dph_shareWrap,.dph_cmdbarCol .dph_shareWrap .dph_btn{width:100%}",
337
337
  ".dph_cmdbarCol .dph_shareWrap .dph_btn{height:28px;padding:0 6px;font-size:11.5px}",
338
+ /* 侧栏窄格里的分享展开态(2026-10-27 真机截图:「今日推荐」点开分享后 UI 错乱):
339
+ 原先 4 个动作一起塞进命令条的一个 1fr 单元格,nowrap 按钮的 min-content 把
340
+ 轨道撑开 ⇒ 命令条溢出卡片右边界、「复制命令」被挤到下一行、说明图单独漂在底部。
341
+ 现在收起留在原格(与复制命令同排),三个动作跨满整行按 2 列排,说明图独占末行。
342
+ ⚠️ 只在 .dph_shareWrapStack(stack=true 的唯一调用点:今日推荐侧栏卡)生效,
343
+ 浮窗 372px 与宽命令条 700px 的既有排布不受影响 —— 那两处实测本来就是通的。 */
344
+ ".dph_cmdbarCol .dph_shareToggle{width:100%}",
345
+ ".dph_cmdbarCol .dph_shareWrapStack{grid-column:1/-1;display:grid;grid-template-columns:1fr 1fr;gap:6px;width:100%}",
346
+ ".dph_cmdbarCol .dph_shareWrapStack .dph_shareCard{grid-column:1/-1}",
347
+ /* 窄屏(<900px)时 .dph_rail 变 display:contents,今日推荐卡横跨整行 —— 212px 那一套
348
+ 「命令跨两列 + 复制/分享各占一格」的 2 列网格在这里会变成两颗 500px 的巨型按钮。
349
+ 媒体查询里原本写的 flex-direction:row 对 grid 元素无效(那时候还没改成 grid),
350
+ 这里把它落实成真正的横排:命令弹性收窄,动作按内容宽,放不下就整组换行。
351
+ 顺带收掉同断点的 .dph_instRow .dph_btn min-width:50% —— 那是给 212px 侧栏量的,
352
+ 整行卡片里会把「安装」按钮拉成半屏宽。 */
353
+ "@media (max-width:900px){.dph_cmdbarCol{display:flex;flex-wrap:wrap;align-items:center}.dph_cmdbarCol .dph_cmdbarCode{flex:1;min-width:0}.dph_cmdbarCol .dph_shareWrap{width:auto}.dph_cmdbarCol .dph_shareWrap .dph_btn{width:auto;padding:0 12px;font-size:12px}.dph_cmdbarCol .dph_shareToggle{width:auto;padding:0 12px;font-size:12px}.dph_cmdbarCol .dph_shareWrapStack{display:inline-flex;flex-wrap:wrap;gap:4px;width:auto}.dph_cmdbarCol .dph_shareWrapStack .dph_btn{width:auto;padding:0 12px;font-size:12px}.dph_railCard .dph_instRow .dph_btn{min-width:0}}",
338
354
  /* 响应式(2026-10-21):主列单栏时宽卡也拆单列,操作轨道转横排成一条动作行 */
339
355
  "@media (max-width:900px){.dph_cardLg{grid-template-columns:minmax(0,1fr);gap:10px}.dph_opRail{flex-direction:row;flex-wrap:wrap;margin-top:2px;padding-top:10px;border-top:1px solid var(--dsw-alias-border-l2)}.dph_opRail .dph_btn{flex:1 1 0;min-width:0}.dph_rail{position:static}}",
340
356
  ].join("");
@@ -1057,6 +1073,32 @@ window.__ModuleLoader__.load({
1057
1073
  } catch (error) {
1058
1074
  /* localStorage 不可用时保持默认开 */
1059
1075
  }
1076
+ /* 每次加载都同步一次:老用户上次关掉开关的记录存在 localStorage,
1077
+ host 侧没有 —— 不同步的话 host 会按默认「开」继续上报。 */
1078
+ syncTelemetryToHost(telemetryOn);
1079
+
1080
+ /**
1081
+ * 把「匿名统计」开关同步给 host(2026-10-30)。
1082
+ *
1083
+ * 为什么必须有这一步:host 侧新增的 `hub_plugin_install` / `hub_plugin_use`
1084
+ * 上报,开关状态存在浏览器 localStorage 里,host 读不到。若不同步,
1085
+ * 用户关掉开关后 host 仍会继续上报 —— 那等于面板上的开关是假的。
1086
+ *
1087
+ * host 把它落盘到 `~/.dsh/dpharness/telemetry.json`,每次上报前读一次;
1088
+ * 文件不存在时按「开」处理(与这里 `telemetryOn = true` 的默认值一致)。
1089
+ */
1090
+ function syncTelemetryToHost(on) {
1091
+ try {
1092
+ fetch("/api/dpharness/telemetry", {
1093
+ method: "POST",
1094
+ headers: { "content-type": "application/json" },
1095
+ body: JSON.stringify({ on: !!on }),
1096
+ keepalive: true,
1097
+ }).catch(() => {});
1098
+ } catch (error) {
1099
+ /* 同步失败不影响主流程;host 侧按默认「开」处理 */
1100
+ }
1101
+ }
1060
1102
 
1061
1103
  function visitorId() {
1062
1104
  try {
@@ -1992,6 +2034,9 @@ window.__ModuleLoader__.load({
1992
2034
  }
1993
2035
  setHub({ telemetry: next });
1994
2036
  telemetryOn = next;
2037
+ /* host 侧的安装/使用上报也要跟着关(2026-10-30)——
2038
+ 少了这一行,关掉开关只挡住浏览器事件,挡不住 host 事件。 */
2039
+ syncTelemetryToHost(next);
1995
2040
  }
1996
2041
 
1997
2042
  /**
@@ -3356,6 +3401,12 @@ window.__ModuleLoader__.load({
3356
3401
  const item = props.item || {};
3357
3402
  const fullName = typeof item.fullName === "string" ? item.fullName : "";
3358
3403
  const scope = props.scope || "";
3404
+ /* stack(2026-10-27)只换「动作怎么摆」,不动任何行为:
3405
+ 212px 侧栏的命令条是 2 列 grid,展开后 4 个动作挤在同一个单元格里会撑破轨道
3406
+ (真机截图:按钮溢出卡片右边界、复制命令被挤到下一行)。
3407
+ stack=true 时收起留在原格,另外三个动作作为一整块跨满行 —— 见 CSS 注释。
3408
+ 其余调用点(浮窗 372px / 宽命令条 700px / 窄卡行内)不传,排布与原来逐像素一致。 */
3409
+ const stack = !!props.stack;
3359
3410
  const [open, setOpen] = react.useState(false);
3360
3411
  const [done, setDone] = react.useState("");
3361
3412
  /* 没有 owner/repo 就拼不出详情页(站点 /plugin/<owner>/<repo> 需要两段)——
@@ -3370,7 +3421,7 @@ window.__ModuleLoader__.load({
3370
3421
  track("plugin_hub", { action: kind === "link" ? "share_link" : "share_text", scope });
3371
3422
  });
3372
3423
  const toggle = h("button", {
3373
- className: "dph_btn" + (open ? " dph_btnPrimary" : ""),
3424
+ className: "dph_btn" + (stack ? " dph_shareToggle" : "") + (open ? " dph_btnPrimary" : ""),
3374
3425
  key: "toggle",
3375
3426
  type: "button",
3376
3427
  title: open ? t("share.closeHint") : t("share.toggle"),
@@ -3384,8 +3435,7 @@ window.__ModuleLoader__.load({
3384
3435
  400px 浮窗里多一个按钮就多占一行,而这两个本来就是同一个开关。 */
3385
3436
  }, open ? t("share.collapse") : t("share.action"));
3386
3437
  if (!open) return h("span", { className: "dph_shareWrap", key: "share" }, [toggle]);
3387
- return h("span", { className: "dph_shareWrap dph_shareWrapOpen", key: "share" }, [
3388
- toggle,
3438
+ const acts = [
3389
3439
  h("button", {
3390
3440
  className: "dph_btn dph_btnSm",
3391
3441
  key: "link",
@@ -3401,7 +3451,7 @@ window.__ModuleLoader__.load({
3401
3451
  onClick: () => copy("text", shareTextOf(item)),
3402
3452
  }, done === "text" ? t("action.copied") : t("share.text")),
3403
3453
  h("a", {
3404
- className: "dph_btn dph_btnSm",
3454
+ className: "dph_btn dph_btnSm" + (stack ? " dph_shareCard" : ""),
3405
3455
  key: "card",
3406
3456
  href: shareCardOf(fullName),
3407
3457
  target: "_blank",
@@ -3409,7 +3459,16 @@ window.__ModuleLoader__.load({
3409
3459
  title: t("share.cardHint"),
3410
3460
  onClick: () => track("plugin_hub", { action: "share_card", scope }),
3411
3461
  }, t("share.card")),
3412
- ]);
3462
+ ];
3463
+ if (stack) {
3464
+ /* Fragment 不产生盒子:两个子项直接成为命令条 grid 的项 ——
3465
+ 收起落在「复制命令」右边那一格,动作块因 grid-column:1/-1 自动落到下一行。 */
3466
+ return h(react.Fragment, { key: "share" }, [
3467
+ toggle,
3468
+ h("span", { className: "dph_shareWrap dph_shareWrapOpen dph_shareWrapStack", key: "acts" }, acts),
3469
+ ]);
3470
+ }
3471
+ return h("span", { className: "dph_shareWrap dph_shareWrapOpen", key: "share" }, [toggle].concat(acts));
3413
3472
  }
3414
3473
 
3415
3474
  /**
@@ -4895,8 +4954,10 @@ window.__ModuleLoader__.load({
4895
4954
  }),
4896
4955
  }, copiedCmd === item.cmd ? t("action.copied") : t("action.copy")),
4897
4956
  /* 侧栏这条命令条是**自建**的(不走 renderCard),所以分享要单独接一次。
4898
- 2026-10-21 首版漏了这一处:用户在真机截图里发现"今日推荐卡没有分享"。 */
4899
- h(ShareUnit, { item: item, scope: "rail", key: "share" }),
4957
+ 2026-10-21 首版漏了这一处:用户在真机截图里发现"今日推荐卡没有分享"。
4958
+ stack=true(2026-10-27):这个格子只有 88px,展开的四个动作放不下 ——
4959
+ 改成"收起留在原格 + 三个动作跨满行",否则会撑破卡片。 */
4960
+ h(ShareUnit, { item: item, scope: "rail", stack: true, key: "share" }),
4900
4961
  ])
4901
4962
  : null,
4902
4963
  item.reason ? h("p", { className: "dph_railWhy", key: "why" }, t("daily.why") + ":" + item.reason) : null,
package/lib/index.js CHANGED
@@ -48,7 +48,7 @@ export const name = "dpharness";
48
48
  /** The dsh web server service used to register HTTP routes. */
49
49
  export const inject = ["webServer"];
50
50
 
51
- export const VERSION = "0.6.9";
51
+ export const VERSION = "0.7.0";
52
52
 
53
53
  /**
54
54
  * 本插件在 npm 上的包名 —— 与 dsh 的插件 id(上面的 `name = "dpharness"`)**不是一回事**,
@@ -299,7 +299,17 @@ const EVENT_TIMEOUT_MS = 8 * 1000;
299
299
  * `copy_install` is the site's existing type for a copied install command and
300
300
  * already carries the package name in `value` — the plugin reuses it as is.
301
301
  */
302
- const EVENT_ALLOWLIST = new Set(["copy_install", "plugin_hub"]);
302
+ const EVENT_ALLOWLIST = new Set([
303
+ "copy_install",
304
+ "plugin_hub",
305
+ /* 端维度(2026-10-30):插件**自己发起**的两条上报,不经过浏览器。
306
+ · `hub_plugin_install` —— pnpm 退出码 0 且磁盘核对到版本确实变了才发
307
+ · `hub_plugin_use` —— 加载后延迟发,内容是「30 天窗口内该包有没有被调用」的布尔
308
+ ⚠️ 站点 `app/api/track/event/route.ts` 的 ALLOWED 必须同步加这两个,
309
+ 否则中转直接 400(fail loud,不会静默丢)。 */
310
+ "hub_plugin_install",
311
+ "hub_plugin_use",
312
+ ]);
303
313
  const EVENT_WINDOW_MS = 60 * 1000;
304
314
  const EVENT_MAX_PER_WINDOW = 120;
305
315
 
@@ -623,6 +633,15 @@ function startInstall(target, verb) {
623
633
  ? "none"
624
634
  : (installJob.before === installJob.after ? "same" : "changed");
625
635
  installJob.changed = installJob.outcome === "changed";
636
+ /* 端维度(2026-10-30):只在**磁盘核对到版本真的变了**且是安装动作时上报。
637
+ · `same`(本来就是目标版本)不算新安装 —— 算进来会让「装了不用」的
638
+ 分母虚高,把早就装过的人重复计入
639
+ · `none`(装前装后都查不到)更不算
640
+ · `verb === "remove"` 是卸载,changed 表示「确实卸掉了」,同样不能算安装
641
+ 三态纪律在这里的具体形态:用户点一次安装,只有一种情况能进分子。 */
642
+ if (installJob.outcome === "changed" && installJob.verb === "add" && installJob.pkg) {
643
+ postTrackDirect("hub_plugin_install", { action: "install", pkg: installJob.pkg });
644
+ }
626
645
  /* 装成了就把更新检查的缓存作废:否则界面刷新后拿到的还是装之前那份清单,
627
646
  "升完还显示可升级" —— 用户只会判定"升级没成功"(2026-10-12 实测撞到)。 */
628
647
  updatesCache = { at: 0, value: null };
@@ -1343,7 +1362,10 @@ const clampText = (value, max) => (typeof value === "string" ? value.slice(0, ma
1343
1362
  * - visitorId must match /^[A-Za-z0-9-]{8,64}$/ or it is dropped
1344
1363
  * - the route also filters loopback IPs, bot UAs and SELF_IP_HASHES
1345
1364
  */
1346
- function buildTrackPayload(input) {
1365
+ /* 导出仅为可测(同 hostClientOf 的做法):端维度上报的 value 形状 ——
1366
+ 尤其是 `pkg=` 段与 `c=`/`v=` 的先后顺序 —— 必须能被断言锁住,
1367
+ 否则改一次格式化就会静默改变站点侧能解析出的字段。 */
1368
+ export function buildTrackPayload(input) {
1347
1369
  const type = clampText(input.event, 32);
1348
1370
  const rawVisitor = clampText(input.visitorId, 64);
1349
1371
  const visitorId = /^[A-Za-z0-9-]{8,64}$/.test(rawVisitor) ? rawVisitor : null;
@@ -1366,6 +1388,11 @@ function buildTrackPayload(input) {
1366
1388
  // plugin_hub carries "<action>:<detail>" — query strings would be truncated away.
1367
1389
  const action = clean(detail.action, 24) || "unknown";
1368
1390
  const parts = [action];
1391
+ /* 插件维度事件(2026-10-30):`hub_plugin_install` / `hub_plugin_use` 的包名。
1392
+ **单独成段而不是并进 action** —— 包名含 `/` 与 `@`,与 action 的闭集枚举不是
1393
+ 同一层语义;混在一起后站点侧就没法用同一把尺子切分(同一个字段两种含义,
1394
+ 是本站已经踩过多次的口径事故源头)。 */
1395
+ if (detail.pkg) parts.push("pkg=" + clean(detail.pkg, 64));
1369
1396
  if (Number.isFinite(detail.length)) parts.push("len=" + Number(detail.length));
1370
1397
  if (Number.isFinite(detail.results)) parts.push("hits=" + Number(detail.results));
1371
1398
  // `via` 记录动作发生在哪条路径上(footer/overlay/local/market);
@@ -1399,6 +1426,94 @@ function buildTrackPayload(input) {
1399
1426
  return { type, value, path: "/dsh-plugin", visitorId };
1400
1427
  }
1401
1428
 
1429
+ /* ------------------------------------------- 端维度:host 侧主动上报(2026-10-30) */
1430
+ /**
1431
+ * Host 级稳定标识。
1432
+ *
1433
+ * ## 为什么不复用浏览器的 visitorId
1434
+ * 浏览器的那个存在页面侧(localStorage),host 读不到;让 client 通过参数传进来
1435
+ * 又多一条「可能缺失」的链路。而端维度统计要的其实是「一台机器一个 ID」——
1436
+ * host 级标识比浏览器标识更贴近这个语义。
1437
+ *
1438
+ * 🔴 代价必须写死在这:host 事件(`hub_plugin_*`)的 UV 与浏览器事件
1439
+ * (`plugin_hub`)**分属两套 ID 空间,不得相加**。看板各自去重,混算就是假数。
1440
+ *
1441
+ * 落盘 `~/.dsh/dpharness/host-id`。读不到也写不进时返回 null 并**放弃本次上报** ——
1442
+ * 宁可少一条数据,也不要用每次启动都变的随机 ID 把 UV 灌爆。
1443
+ */
1444
+ function hostVisitorId() {
1445
+ try {
1446
+ const file = path.join(
1447
+ process.env.DSH_HOME || path.join(os.homedir(), ".dsh"),
1448
+ "dpharness",
1449
+ "host-id",
1450
+ );
1451
+ try {
1452
+ const existing = fs.readFileSync(file, "utf8").trim();
1453
+ if (/^[A-Za-z0-9-]{8,64}$/.test(existing)) return existing;
1454
+ } catch {
1455
+ /* 首次运行,落到下面创建 */
1456
+ }
1457
+ const fresh = "h" + randomUUID().replace(/-/g, "");
1458
+ fs.mkdirSync(path.dirname(file), { recursive: true });
1459
+ fs.writeFileSync(file, fresh, "utf8");
1460
+ return fresh;
1461
+ } catch {
1462
+ return null;
1463
+ }
1464
+ }
1465
+
1466
+ /**
1467
+ * 匿名统计开关(2026-10-30)。
1468
+ *
1469
+ * 开关本身存在浏览器 localStorage(`dph-hub-telemetry`),host 读不到 ——
1470
+ * 所以由 client 在加载与切换时 POST 到 `/api/dpharness/telemetry`,host 落盘到
1471
+ * `~/.dsh/dpharness/telemetry.json`。**文件不存在按「开」处理**,与 client 的
1472
+ * 默认值一致;读失败也按「开」——不能因为文件损坏把用户的选择反过来解释成「关」。
1473
+ *
1474
+ * 为什么这件事不能省:面板上那个开关如果只管得住浏览器事件、管不住 host 事件,
1475
+ * 那它就是个假开关。隐私承诺不能打折。
1476
+ */
1477
+ function telemetryAllowed() {
1478
+ try {
1479
+ const file = path.join(
1480
+ process.env.DSH_HOME || path.join(os.homedir(), ".dsh"),
1481
+ "dpharness",
1482
+ "telemetry.json",
1483
+ );
1484
+ const cfg = JSON.parse(fs.readFileSync(file, "utf8"));
1485
+ return !(cfg && cfg.on === false);
1486
+ } catch {
1487
+ return true;
1488
+ }
1489
+ }
1490
+
1491
+ /**
1492
+ * 直接向站点 POST 一条事件。
1493
+ *
1494
+ * 与 relay 端点共用 `buildTrackPayload` —— 两条路必须是同一份构造逻辑。
1495
+ * 分叉一次就等于让同一种事件从浏览器来与从 host 来长成两个样子,而 `c=` 段
1496
+ * 正是在 payload 构造里追加的:分叉 = 端信息丢一半。
1497
+ *
1498
+ * 一切失败静默:埋点绝不能影响插件主流程,尤其不能把已经成功的安装变成一次抛错。
1499
+ */
1500
+ function postTrackDirect(event, detail) {
1501
+ if (!telemetryAllowed()) return;
1502
+ const visitorId = hostVisitorId();
1503
+ if (visitorId === null) return;
1504
+ const payload = buildTrackPayload({ event, detail, visitorId });
1505
+ void fetch(TRACK, {
1506
+ method: "POST",
1507
+ headers: {
1508
+ "content-type": "application/json",
1509
+ accept: "application/json",
1510
+ // 与 relay 用同一个 UA:站点侧已验证它不会被 bot 过滤掉。
1511
+ "user-agent": `dsh-dpharness/${VERSION}`,
1512
+ },
1513
+ body: JSON.stringify(payload),
1514
+ }).catch(() => {});
1515
+ }
1516
+
1402
1517
  function registerRoute(ctx, pathname, handler) {
1403
1518
  ctx.effect(
1404
1519
  () => ctx.webServer.register({ kind: "prefix", path: pathname, handler }),
@@ -1860,6 +1975,78 @@ async function buildUpdates(ctx) {
1860
1975
  };
1861
1976
  }
1862
1977
 
1978
+ /* ------------------------------------------- 端维度:使用上报(2026-10-30) */
1979
+ /** 上报间隔。布尔信号,一天一次足够 —— 更密只会把事件表灌大。 */
1980
+ const USE_REPORT_TTL_MS = 24 * 60 * 60 * 1000;
1981
+ /** 启动后延迟:usage 引擎要扫全量会话,不能在宿主启动的同步路径上跑。 */
1982
+ const USE_REPORT_DELAY_MS = 45 * 1000;
1983
+ /** 单次最多报几个包。超出部分丢弃,但会补一条 `_truncated` 让截断可观测。 */
1984
+ const USE_REPORT_MAX_ITEMS = 10;
1985
+
1986
+ function useReportPath() {
1987
+ return path.join(
1988
+ process.env.DSH_HOME || path.join(os.homedir(), ".dsh"),
1989
+ "dpharness",
1990
+ "use-report.json",
1991
+ );
1992
+ }
1993
+
1994
+ /**
1995
+ * 上报「装了且用过」的插件清单。
1996
+ *
1997
+ * ## 为什么必须延迟
1998
+ * `getUsageReport()` 要扫 `~/.dsh/sessions` 下全部 zstd 会话(实测全量约 1.5s,
1999
+ * 带 6h 文件缓存)。放在 `apply()` 的同步路径上 = 直接拖慢宿主启动,
2000
+ * 用户感知到的就是「装了这个插件之后 dsh 启动变慢」。45s 延迟把这段挪出关键路径。
2001
+ *
2002
+ * ## 为什么是布尔而不是次数
2003
+ * 调用次数属于本机行为明细,出机器就超出既有隐私契约(本插件至今只发事件名、
2004
+ * 包名与关键词长度)。这里只取 `calls > 0` 这一个事实,次数本身不出机器。
2005
+ *
2006
+ * ## 三态纪律
2007
+ * `getUsageReport().plugins` 里**只包含「有可归属工具」的包**(PKGS_WITH_TOOLS)。
2008
+ * 一个纯 UI 插件根本不在这张表里 —— 它属于「无法度量」,**不是「没在用」**。
2009
+ * 所以本函数只报 `calls > 0` 的行,绝不为缺失的包补一条 use=false。
2010
+ */
2011
+ function scheduleUseReport() {
2012
+ const timer = setTimeout(() => {
2013
+ try {
2014
+ const file = useReportPath();
2015
+ const now = Date.now();
2016
+ try {
2017
+ const last = JSON.parse(fs.readFileSync(file, "utf8"));
2018
+ if (last && typeof last.at === "number" && now - last.at < USE_REPORT_TTL_MS) return;
2019
+ } catch {
2020
+ /* 首次运行或文件损坏:照常上报 */
2021
+ }
2022
+
2023
+ const report = getUsageReport();
2024
+ const table = report && report.plugins && typeof report.plugins === "object" ? report.plugins : {};
2025
+ const used = Object.entries(table)
2026
+ .filter(([, slot]) => slot && Number(slot.calls) > 0)
2027
+ .sort((a, b) => Number(b[1].calls) - Number(a[1].calls))
2028
+ .slice(0, USE_REPORT_MAX_ITEMS)
2029
+ .map(([pkg]) => pkg);
2030
+
2031
+ for (const pkg of used) {
2032
+ postTrackDirect("hub_plugin_use", { action: "use", pkg });
2033
+ }
2034
+ /* 截断信号:报表上「只装了 3 个」与「报了 10 个被截断」长得一模一样,
2035
+ 没有这条就无法区分两者。用 `_truncated` 这个不可能撞名的哨兵包名。 */
2036
+ if (used.length === USE_REPORT_MAX_ITEMS) {
2037
+ postTrackDirect("hub_plugin_use", { action: "use", pkg: "_truncated" });
2038
+ }
2039
+
2040
+ fs.mkdirSync(path.dirname(file), { recursive: true });
2041
+ fs.writeFileSync(file, JSON.stringify({ at: now }), "utf8");
2042
+ } catch {
2043
+ /* 埋点失败绝不影响插件主流程 */
2044
+ }
2045
+ }, USE_REPORT_DELAY_MS);
2046
+ /* 不要因为一个待发的定时器把宿主进程吊着不退出 */
2047
+ if (typeof timer.unref === "function") timer.unref();
2048
+ }
2049
+
1863
2050
  export function apply(ctx) {
1864
2051
  // 「分类精选」:站点 /best 专区的同源数据(月度策展,每小类 2~3 个)
1865
2052
  registerRoute(ctx, "/api/dpharness/best", async (req, res) => {
@@ -2351,6 +2538,44 @@ export function apply(ctx) {
2351
2538
  }
2352
2539
  });
2353
2540
 
2541
+ /**
2542
+ * 匿名统计开关同步(2026-10-30)。
2543
+ *
2544
+ * 唯一写入方是 `lib/client.js` 的 `syncTelemetryToHost()`(插件加载时 + 用户切换时各一次)。
2545
+ * 为什么需要 host 记一份:安装 / 使用上报由 **host** 发起,而开关存在浏览器
2546
+ * localStorage —— 不落盘到 host,用户关掉开关后 host 仍会继续发。
2547
+ *
2548
+ * 这里只接受本机回环调用(由 client 直接 fetch 相对路径),不做额外鉴权:
2549
+ * 能改这个文件的进程本来就能改 `~/.dsh` 下的任何东西,加鉴权不增加任何实际保护。
2550
+ */
2551
+ registerRoute(ctx, "/api/dpharness/telemetry", async (req, res) => {
2552
+ if (req.method !== "POST") {
2553
+ res.writeHead(405, { allow: "POST" });
2554
+ res.end();
2555
+ return;
2556
+ }
2557
+ let input;
2558
+ try {
2559
+ input = JSON.parse((await readBody(req, 1024)) || "{}");
2560
+ } catch {
2561
+ sendJson(res, 400, { ok: false, error: "bad json" });
2562
+ return;
2563
+ }
2564
+ const on = !!(input && input.on === true);
2565
+ try {
2566
+ const file = path.join(
2567
+ process.env.DSH_HOME || path.join(os.homedir(), ".dsh"),
2568
+ "dpharness",
2569
+ "telemetry.json",
2570
+ );
2571
+ fs.mkdirSync(path.dirname(file), { recursive: true });
2572
+ fs.writeFileSync(file, JSON.stringify({ on, at: Date.now() }), "utf8");
2573
+ sendJson(res, 200, { ok: true, on });
2574
+ } catch (error) {
2575
+ sendJson(res, 500, { ok: false, error: String((error && error.message) || error) });
2576
+ }
2577
+ });
2578
+
2354
2579
  registerRoute(ctx, "/api/dpharness/meta", (req, res) => {
2355
2580
  if (req.method !== "GET") {
2356
2581
  res.writeHead(405, { allow: "GET" });
@@ -2378,6 +2603,7 @@ export function apply(ctx) {
2378
2603
  "/api/dpharness/toggle",
2379
2604
  "/api/dpharness/changelog",
2380
2605
  "/api/dpharness/updates",
2606
+ "/api/dpharness/telemetry",
2381
2607
  "/api/dpharness/meta",
2382
2608
  ],
2383
2609
  events: Array.from(EVENT_ALLOWLIST),
@@ -2389,4 +2615,8 @@ export function apply(ctx) {
2389
2615
  },
2390
2616
  });
2391
2617
  });
2618
+
2619
+ /* 端维度(2026-10-30):装载后延迟 45s 上报「装了且用过」的清单,24h 幂等。
2620
+ 放在 apply 的最后一行:前面全是路由注册,任何一个抛错都不该让埋点先跑。 */
2621
+ scheduleUseReport();
2392
2622
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-dpharness",
3
3
  "icon": "./icon.svg",
4
- "version": "0.6.9",
4
+ "version": "0.7.0",
5
5
  "description": "Curated dpharness.com plugin catalog inside the DeepSeek Harness Web GUI: a conversation-view tab, a sidebar entry and a dismissible site-wide floating panel, offering a searchable full catalog, a monthly by-category shortlist, a daily pick and an update check (this plugin plus every installed plugin) with a red-dot reminder, with one-click install via dshmarket or the dsh CLI. Includes a settings page (General row + Plugins tab) for the widget visibility, the daily pick, the update check and the opt-out telemetry relay covering the full install funnel.",
6
6
  "author": "zhanghao3693 <zhanghao3693@qq.com>",
7
7
  "repository": {