@zifeng1023/dsh-bot 0.3.7 → 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/README.md +109 -108
- package/assets/screenshot-1.png +0 -0
- package/assets/screenshot-2.png +0 -0
- package/assets/screenshot-3.png +0 -0
- package/dsh-plugin.json +1 -1
- package/lib/client.js +6633 -4967
- package/lib/index.js +109 -0
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -5893,6 +5893,17 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5893
5893
|
cadencePatch = body.cadence.trim();
|
|
5894
5894
|
if (cadencePatch && !isValidCron(cadencePatch)) throw new BotError("INVALID_ARGS", "执行周期必须是合法的 5 段 cron,例如 0 0,12 * * *(留空表示不自动跑)");
|
|
5895
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
|
+
}
|
|
5896
5907
|
if (Object.keys(patch).length === 0 && cadencePatch === void 0) throw new BotError("INVALID_ARGS", "没有可更新的字段");
|
|
5897
5908
|
const next = await service.update(botId, patch);
|
|
5898
5909
|
if (cadencePatch !== void 0) {
|
|
@@ -5913,6 +5924,104 @@ function makeBotApiRoutes(service, runAction) {
|
|
|
5913
5924
|
}
|
|
5914
5925
|
}
|
|
5915
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
|
+
},
|
|
5916
6025
|
{
|
|
5917
6026
|
kind: "exact",
|
|
5918
6027
|
path: `${API_PREFIX}/bot`,
|