@zifeng1023/dsh-bot 0.3.6 → 0.3.8

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
@@ -2586,7 +2586,8 @@ const DEFAULT_SETTINGS = {
2586
2586
  defaultCadence: "",
2587
2587
  costSensitiveScheduling: true,
2588
2588
  snapshotKeepRecent: 10,
2589
- snapshotEnabled: true
2589
+ snapshotEnabled: true,
2590
+ sidebarEntry: true
2590
2591
  };
2591
2592
  function readJsonSync(file) {
2592
2593
  try {
@@ -2628,6 +2629,7 @@ function sanitizeSettings(raw) {
2628
2629
  bool("autoPropose", raw.autoPropose);
2629
2630
  bool("costSensitiveScheduling", raw.costSensitiveScheduling);
2630
2631
  bool("snapshotEnabled", raw.snapshotEnabled);
2632
+ bool("sidebarEntry", raw.sidebarEntry);
2631
2633
  if ([
2632
2634
  "low",
2633
2635
  "high",
@@ -5759,6 +5761,52 @@ function writeError(res, e) {
5759
5761
  writeJson(res, statusForCode(payload.code), payload);
5760
5762
  }
5761
5763
  /**
5764
+ * cron → 人话短语(只覆盖本面板生成的三类;自定义返回 null)
5765
+ * 用于在改执行周期时,同步目标描述里的「每 12 小时 / 每天 09:00」等措辞。
5766
+ */
5767
+ function cadencePhrase(cron) {
5768
+ const raw = String(cron || "").trim();
5769
+ if (!raw) return null;
5770
+ const parts = raw.split(/\s+/);
5771
+ if (parts.length !== 5) return null;
5772
+ const [mi, ho, dom, mon, dow] = parts;
5773
+ const num = (s) => /^\d+$/.test(s);
5774
+ const hm = (h, m) => `${h.padStart(2, "0")}:${m.padStart(2, "0")}`;
5775
+ const WEEK = [
5776
+ "日",
5777
+ "一",
5778
+ "二",
5779
+ "三",
5780
+ "四",
5781
+ "五",
5782
+ "六"
5783
+ ];
5784
+ if (mi === "0" && /^\*\/\d+$/.test(ho) && dom === "*" && mon === "*" && dow === "*") {
5785
+ const n = Number(ho.slice(2));
5786
+ return n >= 24 ? "每天 00:00" : `每 ${n} 小时`;
5787
+ }
5788
+ if (num(mi) && num(ho) && dom === "*" && mon === "*" && dow === "*") return `每天 ${hm(ho, mi)}`;
5789
+ if (num(mi) && num(ho) && dom === "*" && mon === "*" && /^[0-6](,[0-6])*$/.test(dow)) return `每${dow.split(",").map(Number).map((d) => `周${WEEK[d]}`).join("、")} ${hm(ho, mi)}`;
5790
+ return null;
5791
+ }
5792
+ /**
5793
+ * 把目标描述里的旧节奏措辞替换成新 cron 对应的人话短语。
5794
+ * 只替换「节奏片段」,其余用户文案原样保留;匹配不到就不动(不硬塞)。
5795
+ */
5796
+ function syncCadenceInDescription(desc, cron) {
5797
+ const text = String(desc || "");
5798
+ const phrase = cadencePhrase(cron);
5799
+ if (!phrase) return text;
5800
+ const withSpace = () => /\d$/.test(phrase) ? phrase + " " : phrase;
5801
+ const rules = [
5802
+ [/每过?\s*\d+\s*小时\s*/, withSpace()],
5803
+ [/每天\s*\d{1,2}:\d{2}\s*/, withSpace()],
5804
+ [/每周[^,。;\s]{0,12}\s*/, withSpace()]
5805
+ ];
5806
+ for (const [re, repl] of rules) if (re.test(text)) return text.replace(re, repl);
5807
+ return text;
5808
+ }
5809
+ /**
5762
5810
  * 构造全部路由。
5763
5811
  *
5764
5812
  * @param service BotService 实例
@@ -5813,6 +5861,167 @@ function makeBotApiRoutes(service, runAction) {
5813
5861
  }
5814
5862
  }
5815
5863
  },
5864
+ {
5865
+ kind: "exact",
5866
+ path: `${API_PREFIX}/bot/update`,
5867
+ handler: async (req, res) => {
5868
+ if (req.method !== "POST") {
5869
+ writeJson(res, 405, {
5870
+ ok: false,
5871
+ code: "METHOD_NOT_ALLOWED"
5872
+ });
5873
+ return;
5874
+ }
5875
+ try {
5876
+ const body = await readJsonBody(req);
5877
+ const botId = String(body?.botId ?? "").trim();
5878
+ if (!botId) throw new BotError("INVALID_ARGS", "缺少 botId");
5879
+ const patch = {};
5880
+ if (typeof body?.name === "string") {
5881
+ const name$1 = body.name.trim();
5882
+ if (!name$1) throw new BotError("INVALID_ARGS", "名称不能为空");
5883
+ patch.name = name$1.slice(0, 40);
5884
+ }
5885
+ if (typeof body?.role === "string") {
5886
+ const role = body.role.trim();
5887
+ if (!role) throw new BotError("INVALID_ARGS", "职责不能为空");
5888
+ patch.role = role.slice(0, 400);
5889
+ }
5890
+ if (typeof body?.systemPromptExtra === "string") patch.systemPromptExtra = body.systemPromptExtra.slice(0, 4e3);
5891
+ let cadencePatch;
5892
+ if (typeof body?.cadence === "string") {
5893
+ cadencePatch = body.cadence.trim();
5894
+ if (cadencePatch && !isValidCron(cadencePatch)) throw new BotError("INVALID_ARGS", "执行周期必须是合法的 5 段 cron,例如 0 0,12 * * *(留空表示不自动跑)");
5895
+ }
5896
+ if (body?.budget && typeof body.budget === "object") {
5897
+ const daily = Number(body.budget.dailyCny);
5898
+ const monthly = Number(body.budget.monthlyCny);
5899
+ if (!Number.isFinite(daily) || daily <= 0 || daily > 1e3) throw new BotError("INVALID_ARGS", "每日预算需在 0–1000 元之间");
5900
+ if (!Number.isFinite(monthly) || monthly < daily) throw new BotError("INVALID_ARGS", "每月预算不能小于每日预算");
5901
+ patch.budget = {
5902
+ dailyCny: daily,
5903
+ monthlyCny: monthly,
5904
+ onExceed: body.budget.onExceed === "downgrade" ? "downgrade" : "stop"
5905
+ };
5906
+ }
5907
+ if (Object.keys(patch).length === 0 && cadencePatch === void 0) throw new BotError("INVALID_ARGS", "没有可更新的字段");
5908
+ const next = await service.update(botId, patch);
5909
+ if (cadencePatch !== void 0) {
5910
+ const goal = await service.getGoal(botId);
5911
+ if (goal) await service.saveGoal(botId, {
5912
+ ...goal,
5913
+ description: cadencePatch ? syncCadenceInDescription(goal.description, cadencePatch) : goal.description,
5914
+ cadence: cadencePatch || void 0,
5915
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
5916
+ });
5917
+ }
5918
+ writeJson(res, 200, {
5919
+ ok: true,
5920
+ value: next
5921
+ });
5922
+ } catch (e) {
5923
+ writeError(res, e);
5924
+ }
5925
+ }
5926
+ },
5927
+ {
5928
+ kind: "exact",
5929
+ path: `${API_PREFIX}/routines`,
5930
+ handler: async (req, res) => {
5931
+ if (req.method !== "GET") {
5932
+ writeJson(res, 405, {
5933
+ ok: false,
5934
+ code: "METHOD_NOT_ALLOWED"
5935
+ });
5936
+ return;
5937
+ }
5938
+ try {
5939
+ const url = new URL(req.url ?? "/", "http://x");
5940
+ const botId = url.searchParams.get("botId");
5941
+ const routineId = url.searchParams.get("routineId");
5942
+ if (!botId) {
5943
+ writeJson(res, 400, {
5944
+ ok: false,
5945
+ code: "INVALID_ARGS",
5946
+ message: "缺少 botId"
5947
+ });
5948
+ return;
5949
+ }
5950
+ if (routineId) {
5951
+ const routine = await service.getRoutine(botId, routineId);
5952
+ writeJson(res, 200, {
5953
+ ok: true,
5954
+ value: {
5955
+ routine,
5956
+ yaml: stringifyYaml(routine)
5957
+ }
5958
+ });
5959
+ return;
5960
+ }
5961
+ writeJson(res, 200, {
5962
+ ok: true,
5963
+ value: { routines: await service.listRoutines(botId) }
5964
+ });
5965
+ } catch (e) {
5966
+ writeError(res, e);
5967
+ }
5968
+ }
5969
+ },
5970
+ {
5971
+ kind: "exact",
5972
+ path: `${API_PREFIX}/routines/save`,
5973
+ handler: async (req, res) => {
5974
+ if (req.method !== "POST") {
5975
+ writeJson(res, 405, {
5976
+ ok: false,
5977
+ code: "METHOD_NOT_ALLOWED"
5978
+ });
5979
+ return;
5980
+ }
5981
+ try {
5982
+ const body = await readJsonBody(req);
5983
+ const botId = String(body?.botId ?? "").trim();
5984
+ const yaml = String(body?.yaml ?? "").trim();
5985
+ if (!botId) throw new BotError("INVALID_ARGS", "缺少 botId");
5986
+ if (!yaml) throw new BotError("INVALID_ARGS", "流程内容不能为空");
5987
+ if (yaml.length > 2e5) throw new BotError("INVALID_ARGS", "流程内容过大(上限 200KB)");
5988
+ const routine = validateRoutine(parseYaml(yaml));
5989
+ await service.saveRoutine(botId, routine);
5990
+ writeJson(res, 200, {
5991
+ ok: true,
5992
+ value: { id: routine.metadata.id }
5993
+ });
5994
+ } catch (e) {
5995
+ writeError(res, e);
5996
+ }
5997
+ }
5998
+ },
5999
+ {
6000
+ kind: "exact",
6001
+ path: `${API_PREFIX}/routines/remove`,
6002
+ handler: async (req, res) => {
6003
+ if (req.method !== "POST") {
6004
+ writeJson(res, 405, {
6005
+ ok: false,
6006
+ code: "METHOD_NOT_ALLOWED"
6007
+ });
6008
+ return;
6009
+ }
6010
+ try {
6011
+ const body = await readJsonBody(req);
6012
+ const botId = String(body?.botId ?? "").trim();
6013
+ const routineId = String(body?.routineId ?? "").trim();
6014
+ if (!botId || !routineId) throw new BotError("INVALID_ARGS", "需要 botId / routineId");
6015
+ await service.removeRoutine(botId, routineId);
6016
+ writeJson(res, 200, {
6017
+ ok: true,
6018
+ value: { removed: routineId }
6019
+ });
6020
+ } catch (e) {
6021
+ writeError(res, e);
6022
+ }
6023
+ }
6024
+ },
5816
6025
  {
5817
6026
  kind: "exact",
5818
6027
  path: `${API_PREFIX}/bot`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zifeng1023/dsh-bot",
3
- "version": "0.3.6",
3
+ "version": "0.3.8",
4
4
  "description": "常驻 AI 队友:记得住、守得住、收得回。默认 dry-run 演习、执行前自动快照、可一键回滚,并支持错峰调度省钱。",
5
5
  "repository": {
6
6
  "type": "git",