@guandata/decidex 0.1.2 → 0.3.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.
@@ -0,0 +1,94 @@
1
+ export const ADAPTED_SERVER_VERSION = "0.0.6";
2
+ export const ADAPTED_SERVER_COMMIT = "73901fa03b18d2cb00484ffa951bf99b19af4161";
3
+
4
+ // Version boundaries must be backed by release tags, not the current main version.
5
+ // /api/auth/context first appears between tags 0.0.4 and 0.0.5 (a860e97ad).
6
+ export const COMMAND_COMPATIBILITY = {
7
+ "auth whoami": { minVersion: "0.0.5", protocol: "identity-context" },
8
+ "auth status": { protocol: "identity-context", legacy: { before: "0.0.5", protocol: "agent-list" } },
9
+ "auth login": { protocol: "identity-context", legacy: { before: "0.0.5", protocol: "agent-list" } },
10
+ "context show": { protocol: "identity-context", legacy: { before: "0.0.5", protocol: "local-context" } },
11
+ // 0.0.6 changes the automation schedule contract; 0.0.5 strips unknown fields.
12
+ "automation apply": { protocol: "schedule-v2", legacy: { before: "0.0.6", protocol: "schedule-v1" } },
13
+ };
14
+
15
+ export function adaptAutomationSchedule(input, protocol, now = Date.now()) {
16
+ if (!input || typeof input !== "object" || Array.isArray(input)) throw new Error("schedule 必须是 JSON 对象");
17
+ const schedule = { ...input };
18
+ const fields = {
19
+ HOURLY: ["timeOfDayMinutes", "weekday"], DAILY: ["timeOfDayMinutes", "weekday"],
20
+ WEEKLY: ["timeOfDayMinutes", "weekday", "weekdays"], MONTHLY: ["timeOfDayMinutes", "monthDays"],
21
+ INTERVAL: ["intervalMinutes"], ONCE: ["runOnceAt"],
22
+ };
23
+ if (!Object.hasOwn(fields, schedule.kind)) throw new Error("schedule.kind 只支持 HOURLY、DAILY、WEEKLY、MONTHLY、INTERVAL、ONCE");
24
+ const unknown = Object.keys(schedule).filter((key) => key !== "kind" && !fields[schedule.kind].includes(key));
25
+ if (unknown.length) throw new Error(`${schedule.kind} 调度不支持字段:${unknown.join(", ")}`);
26
+ const integer = (value, name, min, max) => {
27
+ if (!Number.isInteger(value) || value < min || value > max) throw new Error(`${name} 必须是 ${min}..${max} 的整数`);
28
+ };
29
+ if (schedule.timeOfDayMinutes !== undefined) integer(schedule.timeOfDayMinutes, "timeOfDayMinutes", 0, 1439);
30
+ if (schedule.weekday !== undefined) integer(schedule.weekday, "weekday", 0, 6);
31
+ for (const [name, min, max] of [["weekdays", 0, 6], ["monthDays", 1, 31]]) {
32
+ if (schedule[name] === undefined && !(name === "monthDays" && schedule.kind === "MONTHLY")) continue;
33
+ if (!Array.isArray(schedule[name]) || !schedule[name].length) throw new Error(`${name} 必须是非空数组`);
34
+ schedule[name].forEach((value) => integer(value, name, min, max));
35
+ schedule[name] = [...new Set(schedule[name])].sort((a, b) => a - b);
36
+ }
37
+ if (schedule.kind === "INTERVAL") integer(schedule.intervalMinutes, "intervalMinutes", 5, 2_147_483_647);
38
+ if (schedule.kind === "ONCE" && (typeof schedule.runOnceAt !== "string" || !Number.isFinite(Date.parse(schedule.runOnceAt)) || Date.parse(schedule.runOnceAt) <= now)) throw new Error("runOnceAt 必须是晚于当前时间的有效日期,建议使用带时区的 ISO 8601 时间");
39
+ if (protocol === "schedule-v1") {
40
+ if (!["HOURLY", "DAILY", "WEEKLY"].includes(schedule.kind)) throw new Error(`${schedule.kind} 调度需要服务端 >= 0.0.6`);
41
+ if (schedule.weekdays) {
42
+ if (schedule.weekdays.length !== 1) throw new Error("0.0.5 调度仅支持单个 weekday;多星期调度需要服务端 >= 0.0.6");
43
+ schedule.weekday = schedule.weekdays[0];
44
+ delete schedule.weekdays;
45
+ }
46
+ return schedule;
47
+ }
48
+ if (schedule.kind === "HOURLY") return { kind: "INTERVAL", intervalMinutes: 60 };
49
+ if (schedule.kind === "WEEKLY") {
50
+ if (schedule.weekdays === undefined && schedule.weekday !== undefined) schedule.weekdays = [schedule.weekday];
51
+ delete schedule.weekday;
52
+ }
53
+ return schedule;
54
+ }
55
+
56
+ function parseVersion(value) {
57
+ const match = typeof value === "string" && value.match(/^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/);
58
+ if (!match) throw new Error(`无法比较服务端版本:${JSON.stringify(value)};需要 SemVer 版本号`);
59
+ const release = match.slice(1, 4).map(Number);
60
+ const prerelease = match[4]?.split(".") ?? [];
61
+ if (release.some((n) => !Number.isSafeInteger(n)) || prerelease.some((part) => /^\d+$/.test(part) && /^0\d/.test(part))) {
62
+ throw new Error(`不合法的 SemVer 版本号:${value}`);
63
+ }
64
+ return { release, prerelease };
65
+ }
66
+
67
+ export function compareVersions(left, right) {
68
+ const a = parseVersion(left), b = parseVersion(right);
69
+ for (let i = 0; i < 3; i++) {
70
+ if (a.release[i] !== b.release[i]) return a.release[i] > b.release[i] ? 1 : -1;
71
+ }
72
+ if (!a.prerelease.length || !b.prerelease.length) return a.prerelease.length === b.prerelease.length ? 0 : a.prerelease.length ? -1 : 1;
73
+ for (let i = 0; i < Math.max(a.prerelease.length, b.prerelease.length); i++) {
74
+ const x = a.prerelease[i], y = b.prerelease[i];
75
+ if (x === undefined || y === undefined) return x === undefined ? -1 : 1;
76
+ if (x === y) continue;
77
+ const nx = /^\d+$/.test(x), ny = /^\d+$/.test(y);
78
+ if (nx && ny) return BigInt(x) > BigInt(y) ? 1 : -1;
79
+ if (nx !== ny) return nx ? -1 : 1;
80
+ return x > y ? 1 : -1;
81
+ }
82
+ return 0;
83
+ }
84
+
85
+ export function resolveCommandCompatibility(command, version, rules = COMMAND_COMPATIBILITY) {
86
+ const rule = rules[command];
87
+ if (!rule) return { command, supported: null, protocol: null, reason: "no_version_rule" };
88
+ parseVersion(version);
89
+ if (rule.minVersion && compareVersions(version, rule.minVersion) < 0) {
90
+ return { command, supported: false, minVersion: rule.minVersion, protocol: null, reason: "server_too_old" };
91
+ }
92
+ const legacy = Boolean(rule.legacy && compareVersions(version, rule.legacy.before) < 0);
93
+ return { command, supported: true, minVersion: rule.minVersion ?? null, protocol: legacy ? rule.legacy.protocol : rule.protocol ?? "current", legacy };
94
+ }