@coffeexdev/openclaw-sentinel 0.1.6 → 0.1.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/dist/configSchema.js +70 -15
- package/dist/validator.js +69 -54
- package/package.json +1 -1
package/dist/configSchema.js
CHANGED
|
@@ -1,22 +1,77 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
})
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import { Value } from "@sinclair/typebox/value";
|
|
3
|
+
const LimitsSchema = Type.Object({
|
|
4
|
+
maxWatchersTotal: Type.Integer({ minimum: 1 }),
|
|
5
|
+
maxWatchersPerSkill: Type.Integer({ minimum: 1 }),
|
|
6
|
+
maxConditionsPerWatcher: Type.Integer({ minimum: 1 }),
|
|
7
|
+
maxIntervalMsFloor: Type.Integer({ minimum: 1 }),
|
|
8
|
+
}, { additionalProperties: false });
|
|
9
|
+
const ConfigSchema = Type.Object({
|
|
10
|
+
allowedHosts: Type.Array(Type.String()),
|
|
11
|
+
localDispatchBase: Type.String({ minLength: 1 }),
|
|
12
|
+
dispatchAuthToken: Type.Optional(Type.String()),
|
|
13
|
+
stateFilePath: Type.Optional(Type.String()),
|
|
14
|
+
limits: Type.Optional(LimitsSchema),
|
|
15
|
+
}, { additionalProperties: false });
|
|
16
|
+
function withDefaults(value) {
|
|
17
|
+
const limitsIn = value.limits ?? {};
|
|
18
|
+
return {
|
|
19
|
+
allowedHosts: Array.isArray(value.allowedHosts) ? value.allowedHosts : [],
|
|
20
|
+
localDispatchBase: typeof value.localDispatchBase === "string" && value.localDispatchBase.length > 0
|
|
21
|
+
? value.localDispatchBase
|
|
22
|
+
: "http://127.0.0.1:18789",
|
|
23
|
+
dispatchAuthToken: typeof value.dispatchAuthToken === "string" ? value.dispatchAuthToken : undefined,
|
|
24
|
+
stateFilePath: typeof value.stateFilePath === "string" ? value.stateFilePath : undefined,
|
|
25
|
+
limits: {
|
|
26
|
+
maxWatchersTotal: typeof limitsIn.maxWatchersTotal === "number" ? limitsIn.maxWatchersTotal : 200,
|
|
27
|
+
maxWatchersPerSkill: typeof limitsIn.maxWatchersPerSkill === "number" ? limitsIn.maxWatchersPerSkill : 20,
|
|
28
|
+
maxConditionsPerWatcher: typeof limitsIn.maxConditionsPerWatcher === "number"
|
|
29
|
+
? limitsIn.maxConditionsPerWatcher
|
|
30
|
+
: 25,
|
|
31
|
+
maxIntervalMsFloor: typeof limitsIn.maxIntervalMsFloor === "number" ? limitsIn.maxIntervalMsFloor : 1000,
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function issue(path, message) {
|
|
36
|
+
const segments = path.replace(/^\//, "").split("/").filter(Boolean);
|
|
37
|
+
return {
|
|
38
|
+
path: segments,
|
|
39
|
+
message,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
15
42
|
export const sentinelConfigSchema = {
|
|
16
43
|
safeParse: (value) => {
|
|
17
44
|
if (value === undefined)
|
|
18
45
|
return { success: true, data: undefined };
|
|
19
|
-
|
|
46
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
47
|
+
return {
|
|
48
|
+
success: false,
|
|
49
|
+
error: { issues: [issue("/", "Config must be an object")] },
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const candidate = withDefaults(value);
|
|
53
|
+
if (!Value.Check(ConfigSchema, candidate)) {
|
|
54
|
+
const first = [...Value.Errors(ConfigSchema, candidate)][0];
|
|
55
|
+
return {
|
|
56
|
+
success: false,
|
|
57
|
+
error: {
|
|
58
|
+
issues: [issue(String(first?.path || "/"), String(first?.message || "Invalid config"))],
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
// explicit URL validation (TypeBox format validators are not enabled by default)
|
|
63
|
+
try {
|
|
64
|
+
new URL(candidate.localDispatchBase);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return {
|
|
68
|
+
success: false,
|
|
69
|
+
error: {
|
|
70
|
+
issues: [issue("/localDispatchBase", "Invalid URL")],
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return { success: true, data: candidate };
|
|
20
75
|
},
|
|
21
76
|
jsonSchema: {
|
|
22
77
|
type: "object",
|
package/dist/validator.js
CHANGED
|
@@ -1,63 +1,62 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import { Value } from "@sinclair/typebox/value";
|
|
2
3
|
const codeyKeyPattern = /(script|code|eval|handler|function|import|require)/i;
|
|
3
4
|
const codeyValuePattern = /(=>|\bfunction\b|\bimport\s+|\brequire\s*\(|\beval\s*\()/i;
|
|
4
|
-
const
|
|
5
|
-
.
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"changed",
|
|
5
|
+
const ConditionSchema = Type.Object({
|
|
6
|
+
path: Type.String({ minLength: 1 }),
|
|
7
|
+
op: Type.Union([
|
|
8
|
+
Type.Literal("eq"),
|
|
9
|
+
Type.Literal("neq"),
|
|
10
|
+
Type.Literal("gt"),
|
|
11
|
+
Type.Literal("gte"),
|
|
12
|
+
Type.Literal("lt"),
|
|
13
|
+
Type.Literal("lte"),
|
|
14
|
+
Type.Literal("exists"),
|
|
15
|
+
Type.Literal("absent"),
|
|
16
|
+
Type.Literal("contains"),
|
|
17
|
+
Type.Literal("matches"),
|
|
18
|
+
Type.Literal("changed"),
|
|
19
19
|
]),
|
|
20
|
-
value:
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
maxRetries:
|
|
47
|
-
baseMs:
|
|
48
|
-
maxMs:
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
})
|
|
54
|
-
.strict();
|
|
20
|
+
value: Type.Optional(Type.Unknown()),
|
|
21
|
+
}, { additionalProperties: false });
|
|
22
|
+
const WatcherSchema = Type.Object({
|
|
23
|
+
id: Type.String({ minLength: 1 }),
|
|
24
|
+
skillId: Type.String({ minLength: 1 }),
|
|
25
|
+
enabled: Type.Boolean(),
|
|
26
|
+
strategy: Type.Union([
|
|
27
|
+
Type.Literal("http-poll"),
|
|
28
|
+
Type.Literal("websocket"),
|
|
29
|
+
Type.Literal("sse"),
|
|
30
|
+
Type.Literal("http-long-poll"),
|
|
31
|
+
]),
|
|
32
|
+
endpoint: Type.String({ minLength: 1 }),
|
|
33
|
+
method: Type.Optional(Type.Union([Type.Literal("GET"), Type.Literal("POST")])),
|
|
34
|
+
headers: Type.Optional(Type.Record(Type.String(), Type.String())),
|
|
35
|
+
body: Type.Optional(Type.String()),
|
|
36
|
+
intervalMs: Type.Optional(Type.Integer({ minimum: 1 })),
|
|
37
|
+
timeoutMs: Type.Optional(Type.Integer({ minimum: 1 })),
|
|
38
|
+
match: Type.Union([Type.Literal("all"), Type.Literal("any")]),
|
|
39
|
+
conditions: Type.Array(ConditionSchema, { minItems: 1 }),
|
|
40
|
+
fire: Type.Object({
|
|
41
|
+
webhookPath: Type.String({ pattern: "^/" }),
|
|
42
|
+
eventName: Type.String({ minLength: 1 }),
|
|
43
|
+
payloadTemplate: Type.Record(Type.String(), Type.Union([Type.String(), Type.Number(), Type.Boolean(), Type.Null()])),
|
|
44
|
+
}, { additionalProperties: false }),
|
|
45
|
+
retry: Type.Object({
|
|
46
|
+
maxRetries: Type.Integer({ minimum: 0, maximum: 20 }),
|
|
47
|
+
baseMs: Type.Integer({ minimum: 50, maximum: 60000 }),
|
|
48
|
+
maxMs: Type.Integer({ minimum: 100, maximum: 300000 }),
|
|
49
|
+
}, { additionalProperties: false }),
|
|
50
|
+
fireOnce: Type.Optional(Type.Boolean()),
|
|
51
|
+
metadata: Type.Optional(Type.Record(Type.String(), Type.String())),
|
|
52
|
+
}, { additionalProperties: false });
|
|
55
53
|
function scanNoCodeLike(input, parentKey = "") {
|
|
56
54
|
if (input === null || input === undefined)
|
|
57
55
|
return;
|
|
58
56
|
if (typeof input === "string") {
|
|
59
|
-
if (codeyValuePattern.test(input))
|
|
57
|
+
if (codeyValuePattern.test(input)) {
|
|
60
58
|
throw new Error(`Code-like value rejected at ${parentKey || "<root>"}`);
|
|
59
|
+
}
|
|
61
60
|
return;
|
|
62
61
|
}
|
|
63
62
|
if (Array.isArray(input)) {
|
|
@@ -66,13 +65,29 @@ function scanNoCodeLike(input, parentKey = "") {
|
|
|
66
65
|
}
|
|
67
66
|
if (typeof input === "object") {
|
|
68
67
|
for (const [key, value] of Object.entries(input)) {
|
|
69
|
-
if (codeyKeyPattern.test(key))
|
|
68
|
+
if (codeyKeyPattern.test(key)) {
|
|
70
69
|
throw new Error(`Code-like field rejected: ${parentKey ? `${parentKey}.` : ""}${key}`);
|
|
70
|
+
}
|
|
71
71
|
scanNoCodeLike(value, parentKey ? `${parentKey}.${key}` : key);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
export function validateWatcherDefinition(input) {
|
|
76
76
|
scanNoCodeLike(input);
|
|
77
|
-
|
|
77
|
+
if (!Value.Check(WatcherSchema, input)) {
|
|
78
|
+
const first = [...Value.Errors(WatcherSchema, input)][0];
|
|
79
|
+
const where = first?.path || "(root)";
|
|
80
|
+
const why = first?.message || "Invalid watcher definition";
|
|
81
|
+
throw new Error(`Invalid watcher definition at ${where}: ${why}`);
|
|
82
|
+
}
|
|
83
|
+
const endpoint = input.endpoint;
|
|
84
|
+
try {
|
|
85
|
+
if (typeof endpoint !== "string")
|
|
86
|
+
throw new Error("endpoint must be a string");
|
|
87
|
+
new URL(endpoint);
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
throw new Error("Invalid watcher definition at /endpoint: Invalid URL");
|
|
91
|
+
}
|
|
92
|
+
return input;
|
|
78
93
|
}
|