@zifeng1023/dsh-bot 0.3.7 → 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/README.md +112 -108
- package/assets/screenshot-v038-1.png +0 -0
- package/assets/screenshot-v038-2.png +0 -0
- package/assets/screenshot-v038-3.png +0 -0
- package/dsh-plugin.json +1 -1
- package/lib/client.js +5238 -4967
- package/lib/index.js +228 -15
- package/package.json +5 -4
- package/screenshots.json +3 -3
- package/assets/screenshot-1.png +0 -0
- package/assets/screenshot-2.png +0 -0
- package/assets/screenshot-3.png +0 -0
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
|
-
|
|
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
|
-
...
|
|
2688
|
+
...stored,
|
|
2678
2689
|
...sanitizeSettings(patch)
|
|
2679
2690
|
};
|
|
2680
|
-
await
|
|
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);
|
|
@@ -5893,7 +5910,37 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5893
5910
|
cadencePatch = body.cadence.trim();
|
|
5894
5911
|
if (cadencePatch && !isValidCron(cadencePatch)) throw new BotError("INVALID_ARGS", "执行周期必须是合法的 5 段 cron,例如 0 0,12 * * *(留空表示不自动跑)");
|
|
5895
5912
|
}
|
|
5896
|
-
if (
|
|
5913
|
+
if (body?.budget && typeof body.budget === "object") {
|
|
5914
|
+
const daily = Number(body.budget.dailyCny);
|
|
5915
|
+
const monthly = Number(body.budget.monthlyCny);
|
|
5916
|
+
if (!Number.isFinite(daily) || daily <= 0 || daily > 1e3) throw new BotError("INVALID_ARGS", "每日预算需在 0–1000 元之间");
|
|
5917
|
+
if (!Number.isFinite(monthly) || monthly < daily) throw new BotError("INVALID_ARGS", "每月预算不能小于每日预算");
|
|
5918
|
+
patch.budget = {
|
|
5919
|
+
dailyCny: daily,
|
|
5920
|
+
monthlyCny: monthly,
|
|
5921
|
+
onExceed: body.budget.onExceed === "downgrade" ? "downgrade" : "stop"
|
|
5922
|
+
};
|
|
5923
|
+
}
|
|
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
|
+
}
|
|
5897
5944
|
const next = await service.update(botId, patch);
|
|
5898
5945
|
if (cadencePatch !== void 0) {
|
|
5899
5946
|
const goal = await service.getGoal(botId);
|
|
@@ -5913,6 +5960,104 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5913
5960
|
}
|
|
5914
5961
|
}
|
|
5915
5962
|
},
|
|
5963
|
+
{
|
|
5964
|
+
kind: "exact",
|
|
5965
|
+
path: `${API_PREFIX}/routines`,
|
|
5966
|
+
handler: async (req, res) => {
|
|
5967
|
+
if (req.method !== "GET") {
|
|
5968
|
+
writeJson(res, 405, {
|
|
5969
|
+
ok: false,
|
|
5970
|
+
code: "METHOD_NOT_ALLOWED"
|
|
5971
|
+
});
|
|
5972
|
+
return;
|
|
5973
|
+
}
|
|
5974
|
+
try {
|
|
5975
|
+
const url = new URL(req.url ?? "/", "http://x");
|
|
5976
|
+
const botId = url.searchParams.get("botId");
|
|
5977
|
+
const routineId = url.searchParams.get("routineId");
|
|
5978
|
+
if (!botId) {
|
|
5979
|
+
writeJson(res, 400, {
|
|
5980
|
+
ok: false,
|
|
5981
|
+
code: "INVALID_ARGS",
|
|
5982
|
+
message: "缺少 botId"
|
|
5983
|
+
});
|
|
5984
|
+
return;
|
|
5985
|
+
}
|
|
5986
|
+
if (routineId) {
|
|
5987
|
+
const routine = await service.getRoutine(botId, routineId);
|
|
5988
|
+
writeJson(res, 200, {
|
|
5989
|
+
ok: true,
|
|
5990
|
+
value: {
|
|
5991
|
+
routine,
|
|
5992
|
+
yaml: stringifyYaml(routine)
|
|
5993
|
+
}
|
|
5994
|
+
});
|
|
5995
|
+
return;
|
|
5996
|
+
}
|
|
5997
|
+
writeJson(res, 200, {
|
|
5998
|
+
ok: true,
|
|
5999
|
+
value: { routines: await service.listRoutines(botId) }
|
|
6000
|
+
});
|
|
6001
|
+
} catch (e) {
|
|
6002
|
+
writeError(res, e);
|
|
6003
|
+
}
|
|
6004
|
+
}
|
|
6005
|
+
},
|
|
6006
|
+
{
|
|
6007
|
+
kind: "exact",
|
|
6008
|
+
path: `${API_PREFIX}/routines/save`,
|
|
6009
|
+
handler: async (req, res) => {
|
|
6010
|
+
if (req.method !== "POST") {
|
|
6011
|
+
writeJson(res, 405, {
|
|
6012
|
+
ok: false,
|
|
6013
|
+
code: "METHOD_NOT_ALLOWED"
|
|
6014
|
+
});
|
|
6015
|
+
return;
|
|
6016
|
+
}
|
|
6017
|
+
try {
|
|
6018
|
+
const body = await readJsonBody(req);
|
|
6019
|
+
const botId = String(body?.botId ?? "").trim();
|
|
6020
|
+
const yaml = String(body?.yaml ?? "").trim();
|
|
6021
|
+
if (!botId) throw new BotError("INVALID_ARGS", "缺少 botId");
|
|
6022
|
+
if (!yaml) throw new BotError("INVALID_ARGS", "流程内容不能为空");
|
|
6023
|
+
if (yaml.length > 2e5) throw new BotError("INVALID_ARGS", "流程内容过大(上限 200KB)");
|
|
6024
|
+
const routine = validateRoutine(parseYaml(yaml));
|
|
6025
|
+
await service.saveRoutine(botId, routine);
|
|
6026
|
+
writeJson(res, 200, {
|
|
6027
|
+
ok: true,
|
|
6028
|
+
value: { id: routine.metadata.id }
|
|
6029
|
+
});
|
|
6030
|
+
} catch (e) {
|
|
6031
|
+
writeError(res, e);
|
|
6032
|
+
}
|
|
6033
|
+
}
|
|
6034
|
+
},
|
|
6035
|
+
{
|
|
6036
|
+
kind: "exact",
|
|
6037
|
+
path: `${API_PREFIX}/routines/remove`,
|
|
6038
|
+
handler: async (req, res) => {
|
|
6039
|
+
if (req.method !== "POST") {
|
|
6040
|
+
writeJson(res, 405, {
|
|
6041
|
+
ok: false,
|
|
6042
|
+
code: "METHOD_NOT_ALLOWED"
|
|
6043
|
+
});
|
|
6044
|
+
return;
|
|
6045
|
+
}
|
|
6046
|
+
try {
|
|
6047
|
+
const body = await readJsonBody(req);
|
|
6048
|
+
const botId = String(body?.botId ?? "").trim();
|
|
6049
|
+
const routineId = String(body?.routineId ?? "").trim();
|
|
6050
|
+
if (!botId || !routineId) throw new BotError("INVALID_ARGS", "需要 botId / routineId");
|
|
6051
|
+
await service.removeRoutine(botId, routineId);
|
|
6052
|
+
writeJson(res, 200, {
|
|
6053
|
+
ok: true,
|
|
6054
|
+
value: { removed: routineId }
|
|
6055
|
+
});
|
|
6056
|
+
} catch (e) {
|
|
6057
|
+
writeError(res, e);
|
|
6058
|
+
}
|
|
6059
|
+
}
|
|
6060
|
+
},
|
|
5916
6061
|
{
|
|
5917
6062
|
kind: "exact",
|
|
5918
6063
|
path: `${API_PREFIX}/bot`,
|
|
@@ -6511,12 +6656,17 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6511
6656
|
try {
|
|
6512
6657
|
if (req.method === "GET") {
|
|
6513
6658
|
const botId = new URL(req.url ?? "/", "http://x").searchParams.get("botId") ?? void 0;
|
|
6514
|
-
const [effective, global] = await Promise.all([
|
|
6659
|
+
const [effective, global, overrides] = await Promise.all([
|
|
6660
|
+
runAction.getSettings?.(botId),
|
|
6661
|
+
runAction.getGlobalSettings?.(),
|
|
6662
|
+
runAction.getSettingsOverrides?.(botId)
|
|
6663
|
+
]);
|
|
6515
6664
|
writeJson(res, 200, {
|
|
6516
6665
|
ok: true,
|
|
6517
6666
|
value: {
|
|
6518
6667
|
effective: effective ?? null,
|
|
6519
|
-
global: global ?? null
|
|
6668
|
+
global: global ?? null,
|
|
6669
|
+
overrides: overrides ?? []
|
|
6520
6670
|
}
|
|
6521
6671
|
});
|
|
6522
6672
|
return;
|
|
@@ -6530,9 +6680,10 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6530
6680
|
}
|
|
6531
6681
|
const body = await readJsonBody(req);
|
|
6532
6682
|
const target = body?.botId ? String(body.botId) : void 0;
|
|
6683
|
+
const clear = Array.isArray(body?.clear) ? body.clear.map((k) => String(k)) : [];
|
|
6533
6684
|
writeJson(res, 200, {
|
|
6534
6685
|
ok: true,
|
|
6535
|
-
value: await runAction.saveSettings?.(target, body?.settings ?? {}) ?? null
|
|
6686
|
+
value: await runAction.saveSettings?.(target, body?.settings ?? {}, clear) ?? null
|
|
6536
6687
|
});
|
|
6537
6688
|
} catch (e) {
|
|
6538
6689
|
writeError(res, e);
|
|
@@ -6619,6 +6770,67 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
6619
6770
|
}
|
|
6620
6771
|
}
|
|
6621
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
|
+
},
|
|
6622
6834
|
{
|
|
6623
6835
|
kind: "exact",
|
|
6624
6836
|
path: `${API_PREFIX}/runs/clear`,
|
|
@@ -7753,8 +7965,9 @@ function applyInner(ctx, rawConfig) {
|
|
|
7753
7965
|
getAvailableModels: () => listAvailableModels(ctx),
|
|
7754
7966
|
getSettings: (botId) => settingsStore.resolve(botId),
|
|
7755
7967
|
getGlobalSettings: () => settingsStore.getGlobal(),
|
|
7756
|
-
|
|
7757
|
-
|
|
7968
|
+
getSettingsOverrides: (botId) => botId ? settingsStore.overridesFor(botId) : Promise.resolve([]),
|
|
7969
|
+
saveSettings: async (botId, patch, clear) => {
|
|
7970
|
+
if (botId) return settingsStore.setForBot(botId, patch, clear ?? []);
|
|
7758
7971
|
return settingsStore.setGlobal(patch);
|
|
7759
7972
|
},
|
|
7760
7973
|
doctor: doctorReport
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zifeng1023/dsh-bot",
|
|
3
|
-
"version": "0.3.
|
|
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
package/assets/screenshot-1.png
DELETED
|
Binary file
|
package/assets/screenshot-2.png
DELETED
|
Binary file
|
package/assets/screenshot-3.png
DELETED
|
Binary file
|