@zifeng1023/dsh-bot 0.3.8 → 0.3.9

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/index.js CHANGED
@@ -2106,6 +2106,8 @@ var BotService = class {
2106
2106
  todayCostCny: todayCost,
2107
2107
  lastRunAt: runs[0]?.startedAt,
2108
2108
  lastRunStatus: runs[0]?.status,
2109
+ model: identity.model,
2110
+ budget: identity.budget,
2109
2111
  goalDescription: goal?.description,
2110
2112
  goalStatus: goal?.status,
2111
2113
  goalType: goal?.type,
@@ -2577,13 +2579,10 @@ const DEFAULT_SETTINGS = {
2577
2579
  notifySystem: true,
2578
2580
  notifyOnlyOnDecision: true,
2579
2581
  quietHours: null,
2580
- followSessionModel: true,
2581
2582
  reasoningEffort: "high",
2582
2583
  dailyCny: 5,
2583
2584
  monthlyCny: 100,
2584
2585
  onExceed: "stop",
2585
- autoPropose: true,
2586
- defaultCadence: "",
2587
2586
  costSensitiveScheduling: true,
2588
2587
  snapshotKeepRecent: 10,
2589
2588
  snapshotEnabled: true,
@@ -2625,8 +2624,6 @@ function sanitizeSettings(raw) {
2625
2624
  };
2626
2625
  bool("notifySystem", raw.notifySystem);
2627
2626
  bool("notifyOnlyOnDecision", raw.notifyOnlyOnDecision);
2628
- bool("followSessionModel", raw.followSessionModel);
2629
- bool("autoPropose", raw.autoPropose);
2630
2627
  bool("costSensitiveScheduling", raw.costSensitiveScheduling);
2631
2628
  bool("snapshotEnabled", raw.snapshotEnabled);
2632
2629
  bool("sidebarEntry", raw.sidebarEntry);
@@ -2639,7 +2636,6 @@ function sanitizeSettings(raw) {
2639
2636
  if (Number.isFinite(raw.monthlyCny) && raw.monthlyCny >= 0) out.monthlyCny = Number(raw.monthlyCny);
2640
2637
  if (["stop", "downgrade"].includes(raw.onExceed)) out.onExceed = raw.onExceed;
2641
2638
  if (Number.isFinite(raw.snapshotKeepRecent) && raw.snapshotKeepRecent >= 0) out.snapshotKeepRecent = Math.floor(Number(raw.snapshotKeepRecent));
2642
- if (typeof raw.defaultCadence === "string") out.defaultCadence = raw.defaultCadence.trim();
2643
2639
  if (raw.quietHours === null) out.quietHours = null;
2644
2640
  else if (raw.quietHours && typeof raw.quietHours.start === "string" && typeof raw.quietHours.end === "string" && isValidClock(raw.quietHours.start) && isValidClock(raw.quietHours.end)) out.quietHours = {
2645
2641
  start: raw.quietHours.start,
@@ -2647,6 +2643,12 @@ function sanitizeSettings(raw) {
2647
2643
  };
2648
2644
  return out;
2649
2645
  }
2646
+ /** 可在单 Bot 层覆盖的通知项(其余项始终跟随全局) */
2647
+ const NOTIFY_OVERRIDE_KEYS = [
2648
+ "notifySystem",
2649
+ "notifyOnlyOnDecision",
2650
+ "quietHours"
2651
+ ];
2650
2652
  /** 设置存储 */
2651
2653
  var SettingsStore = class {
2652
2654
  constructor(homeDir) {
@@ -2672,14 +2674,29 @@ var SettingsStore = class {
2672
2674
  async getForBot(botId) {
2673
2675
  return readJsonSync(this.botPath(botId)) ?? {};
2674
2676
  }
2675
- async setForBot(botId, patch) {
2677
+ /**
2678
+ * 单 Bot 设置写入。
2679
+ *
2680
+ * ⭐ v0.3.9 刀 2:`clear` 让「恢复跟随全局」有确定语义——
2681
+ * 直接把该键从单 Bot 文件里删掉,而不是写一个假值。
2682
+ * quietHours 的 null 仍表示「这个 Bot 明确不免打扰」,与「跟随全局」不同。
2683
+ */
2684
+ async setForBot(botId, patch, clear = []) {
2685
+ const stored = { ...await this.getForBot(botId) };
2686
+ for (const key of clear) if (NOTIFY_OVERRIDE_KEYS.includes(key)) delete stored[key];
2676
2687
  const merged = {
2677
- ...await this.getForBot(botId),
2688
+ ...stored,
2678
2689
  ...sanitizeSettings(patch)
2679
2690
  };
2680
- await writeJsonAtomic$1(this.botPath(botId), merged);
2691
+ if (Object.keys(merged).length === 0) await fsp.rm(this.botPath(botId), { force: true });
2692
+ else await writeJsonAtomic$1(this.botPath(botId), merged);
2681
2693
  return this.resolve(botId);
2682
2694
  }
2695
+ /** 该 Bot 覆盖了哪些键(UI 用来显示「跟随全局 / 已覆盖」) */
2696
+ async overridesFor(botId) {
2697
+ const raw = await this.getForBot(botId);
2698
+ return NOTIFY_OVERRIDE_KEYS.filter((k) => raw[k] !== void 0);
2699
+ }
2683
2700
  /** 全局 + 单 Bot 合并后的有效设置 */
2684
2701
  async resolve(botId) {
2685
2702
  return mergeSettings(await this.getGlobal(), botId ? await this.getForBot(botId) : void 0);
@@ -5904,7 +5921,26 @@ function makeBotApiRoutes(service, runAction) {
5904
5921
  onExceed: body.budget.onExceed === "downgrade" ? "downgrade" : "stop"
5905
5922
  };
5906
5923
  }
5907
- if (Object.keys(patch).length === 0 && cadencePatch === void 0) throw new BotError("INVALID_ARGS", "没有可更新的字段");
5924
+ if (Object.keys(patch).length === 0 && cadencePatch === void 0) {
5925
+ if (body?.model && typeof body.model === "object") {
5926
+ const provider = String(body.model.provider ?? "").trim();
5927
+ const model = String(body.model.model ?? "").trim();
5928
+ if (!provider || !model) throw new BotError("INVALID_ARGS", "需要 model.provider 与 model.model");
5929
+ const known = await runAction.getAvailableModels?.() ?? [];
5930
+ if (known.length > 0) {
5931
+ const hit = known.find((x) => x.provider === provider);
5932
+ if (!hit) throw new BotError("INVALID_ARGS", `当前环境没有 provider「${provider}」。可用:` + known.map((x) => x.provider).join(", "));
5933
+ if (hit.models && hit.models.length > 0 && !hit.models.includes(model)) throw new BotError("INVALID_ARGS", `provider「${provider}」下没有模型「${model}」。可用:` + hit.models.join(", "));
5934
+ }
5935
+ const cur = await service.get(botId);
5936
+ patch.model = {
5937
+ provider,
5938
+ model,
5939
+ reasoningEffort: body.model.reasoningEffort ?? cur.model?.reasoningEffort ?? "high"
5940
+ };
5941
+ }
5942
+ throw new BotError("INVALID_ARGS", "没有可更新的字段");
5943
+ }
5908
5944
  const next = await service.update(botId, patch);
5909
5945
  if (cadencePatch !== void 0) {
5910
5946
  const goal = await service.getGoal(botId);
@@ -6620,12 +6656,17 @@ function makeBotApiRoutes(service, runAction) {
6620
6656
  try {
6621
6657
  if (req.method === "GET") {
6622
6658
  const botId = new URL(req.url ?? "/", "http://x").searchParams.get("botId") ?? void 0;
6623
- const [effective, global] = await Promise.all([runAction.getSettings?.(botId), runAction.getGlobalSettings?.()]);
6659
+ const [effective, global, overrides] = await Promise.all([
6660
+ runAction.getSettings?.(botId),
6661
+ runAction.getGlobalSettings?.(),
6662
+ runAction.getSettingsOverrides?.(botId)
6663
+ ]);
6624
6664
  writeJson(res, 200, {
6625
6665
  ok: true,
6626
6666
  value: {
6627
6667
  effective: effective ?? null,
6628
- global: global ?? null
6668
+ global: global ?? null,
6669
+ overrides: overrides ?? []
6629
6670
  }
6630
6671
  });
6631
6672
  return;
@@ -6639,9 +6680,10 @@ function makeBotApiRoutes(service, runAction) {
6639
6680
  }
6640
6681
  const body = await readJsonBody(req);
6641
6682
  const target = body?.botId ? String(body.botId) : void 0;
6683
+ const clear = Array.isArray(body?.clear) ? body.clear.map((k) => String(k)) : [];
6642
6684
  writeJson(res, 200, {
6643
6685
  ok: true,
6644
- value: await runAction.saveSettings?.(target, body?.settings ?? {}) ?? null
6686
+ value: await runAction.saveSettings?.(target, body?.settings ?? {}, clear) ?? null
6645
6687
  });
6646
6688
  } catch (e) {
6647
6689
  writeError(res, e);
@@ -6728,6 +6770,67 @@ function makeBotApiRoutes(service, runAction) {
6728
6770
  }
6729
6771
  }
6730
6772
  },
6773
+ {
6774
+ kind: "exact",
6775
+ path: `${API_PREFIX}/runs/aggregate`,
6776
+ handler: async (req, res) => {
6777
+ if (req.method !== "GET") {
6778
+ writeJson(res, 405, {
6779
+ ok: false,
6780
+ code: "METHOD_NOT_ALLOWED"
6781
+ });
6782
+ return;
6783
+ }
6784
+ try {
6785
+ const url = new URL(req.url ?? "/", "http://x");
6786
+ const limit = Math.min(Math.max(Number(url.searchParams.get("limit")) || 20, 1), 100);
6787
+ const bots = await service.list();
6788
+ const runs = [];
6789
+ const byBot = {};
6790
+ for (const b of bots) {
6791
+ const list = await service.listRuns(b.id, limit);
6792
+ const sample = list.length;
6793
+ const ok = list.filter((r) => r.status === "success").length;
6794
+ byBot[b.id] = {
6795
+ botName: b.name,
6796
+ sample,
6797
+ successRate: sample ? ok / sample : 0
6798
+ };
6799
+ for (const r of list) runs.push({
6800
+ id: r.id,
6801
+ botId: b.id,
6802
+ botName: b.name,
6803
+ status: r.status,
6804
+ trigger: r.trigger,
6805
+ startedAt: r.startedAt,
6806
+ endedAt: r.endedAt,
6807
+ usage: r.usage,
6808
+ steps: (r.steps ?? []).map((s) => ({
6809
+ kind: s.kind,
6810
+ name: s.name
6811
+ }))
6812
+ });
6813
+ }
6814
+ runs.sort((a, b) => String(b.startedAt ?? "").localeCompare(String(a.startedAt ?? "")));
6815
+ const total = runs.length;
6816
+ const success = runs.filter((r) => r.status === "success").length;
6817
+ writeJson(res, 200, {
6818
+ ok: true,
6819
+ value: {
6820
+ runs,
6821
+ byBot,
6822
+ health: {
6823
+ sample: total,
6824
+ successRate: total ? success / total : 0
6825
+ },
6826
+ botCount: bots.length
6827
+ }
6828
+ });
6829
+ } catch (e) {
6830
+ writeError(res, e);
6831
+ }
6832
+ }
6833
+ },
6731
6834
  {
6732
6835
  kind: "exact",
6733
6836
  path: `${API_PREFIX}/runs/clear`,
@@ -7862,8 +7965,9 @@ function applyInner(ctx, rawConfig) {
7862
7965
  getAvailableModels: () => listAvailableModels(ctx),
7863
7966
  getSettings: (botId) => settingsStore.resolve(botId),
7864
7967
  getGlobalSettings: () => settingsStore.getGlobal(),
7865
- saveSettings: async (botId, patch) => {
7866
- if (botId) return settingsStore.setForBot(botId, patch);
7968
+ getSettingsOverrides: (botId) => botId ? settingsStore.overridesFor(botId) : Promise.resolve([]),
7969
+ saveSettings: async (botId, patch, clear) => {
7970
+ if (botId) return settingsStore.setForBot(botId, patch, clear ?? []);
7867
7971
  return settingsStore.setGlobal(patch);
7868
7972
  },
7869
7973
  doctor: doctorReport
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zifeng1023/dsh-bot",
3
- "version": "0.3.8",
3
+ "version": "0.3.9",
4
4
  "description": "常驻 AI 队友:记得住、守得住、收得回。默认 dry-run 演习、执行前自动快照、可一键回滚,并支持错峰调度省钱。",
5
5
  "repository": {
6
6
  "type": "git",
@@ -72,8 +72,8 @@
72
72
  "vitest": "^3.0.0"
73
73
  },
74
74
  "scripts": {
75
- "build": "tsdown",
76
- "prepare": "tsdown",
75
+ "build": "node dev/concat-parts.mjs && tsdown",
76
+ "prepare": "node dev/concat-parts.mjs && tsdown",
77
77
  "dev": "tsdown --watch",
78
78
  "test": "vitest run",
79
79
  "test:watch": "vitest",
@@ -82,7 +82,8 @@
82
82
  "verify:static": "node dev/check-undefined.mjs",
83
83
  "verify:e2e": "node dev/demo-goal-cycle.mjs && node dev/demo-goal-cadence.mjs && node dev/demo-model-binding.mjs",
84
84
  "verify:all": "npm run verify:static && npm run typecheck && npm run test && npm run build && npm run verify:client && npm run verify:e2e",
85
- "gen:tools": "node dev/gen-tools-doc.mjs"
85
+ "gen:tools": "node dev/gen-tools-doc.mjs",
86
+ "concat": "node dev/concat-parts.mjs"
86
87
  },
87
88
  "license": "MIT"
88
89
  }
package/screenshots.json CHANGED
@@ -1,5 +1,5 @@
1
1
  [
2
- "assets/screenshot-1.png",
3
- "assets/screenshot-2.png",
4
- "assets/screenshot-3.png"
2
+ "assets/screenshot-v038-1.png",
3
+ "assets/screenshot-v038-2.png",
4
+ "assets/screenshot-v038-3.png"
5
5
  ]