librebot-tools 0.0.0-stage → 0.4.5
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/LICENSE +21 -0
- package/README.md +277 -2
- package/bin/librebot.js +262 -0
- package/package.json +38 -4
- package/schema/v1/bot-yaml-schema.json +193 -0
- package/schema/v1/skills-sources-schema.json +31 -0
- package/src/contract.d.ts +25 -0
- package/src/contract.js +23 -0
- package/src/errors.js +28 -0
- package/src/icon.js +130 -0
- package/src/index.d.ts +271 -0
- package/src/index.js +57 -0
- package/src/parse.js +702 -0
- package/src/scaffold.js +279 -0
- package/src/schema.js +14 -0
- package/src/skills-import.js +50 -0
- package/src/skills-lock.js +697 -0
- package/src/util.js +27 -0
- package/src/validate.js +835 -0
- package/test/assets.test.mjs +82 -0
- package/test/export.test.mjs +155 -0
- package/test/gitlab-preview.test.mjs +23 -0
- package/test/gitlab-triggers.test.mjs +53 -0
- package/test/icon.test.mjs +137 -0
- package/test/lock.test.mjs +92 -0
- package/test/malformed.test.mjs +79 -0
- package/test/readme.test.mjs +109 -0
- package/test/semantics.test.mjs +186 -0
- package/test/standard-skills.test.mjs +41 -0
- package/test/vendor.test.mjs +135 -0
- package/test/whatsapp-actions.test.mjs +31 -0
package/src/validate.js
ADDED
|
@@ -0,0 +1,835 @@
|
|
|
1
|
+
// Semantic validation mirroring the platform catalog validators
|
|
2
|
+
// (services/shared: catalog-validation, workspace, trigger-types,
|
|
3
|
+
// repository). bot.yaml shape errors use file "bot.yaml"; cross-file and
|
|
4
|
+
// platform-parity limits live here so local `validate`, server ingestion,
|
|
5
|
+
// and UI saves share one rule set. `tools` is intentionally absent: builtin
|
|
6
|
+
// tools are always on (runtime behavior, no contract field).
|
|
7
|
+
|
|
8
|
+
import { isRecord } from "./errors.js";
|
|
9
|
+
import { sha256Hex } from "./util.js";
|
|
10
|
+
import {
|
|
11
|
+
MODEL_ROLES, REPO_CAPABILITIES, GITREPO_TRIGGER_EVENT_TYPES,
|
|
12
|
+
WHATSAPP_CAPABILITIES, WHATSAPP_TRIGGER_EVENT_TYPES,
|
|
13
|
+
} from "./contract.js";
|
|
14
|
+
|
|
15
|
+
const ENTRY_ID_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
16
|
+
const INPUT_KEY_RE = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/;
|
|
17
|
+
const MOUNT_SEGMENT_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
18
|
+
const EVENT_TYPE_RE = /^[A-Za-z0-9._-]+$/;
|
|
19
|
+
const REF_RE = /^[A-Za-z0-9._/][A-Za-z0-9._/-]*$/;
|
|
20
|
+
const RESERVED_TOP = {
|
|
21
|
+
".git": true,
|
|
22
|
+
".librebot": true,
|
|
23
|
+
".omp": true,
|
|
24
|
+
".config": true,
|
|
25
|
+
".ssh": true,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @typedef {{ file: string; field: string; message: string }} SourceIssue
|
|
30
|
+
* @typedef {{ errors: SourceIssue[]; warnings: SourceIssue[] }} IssueSink
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
function err(sink, file, field, message) {
|
|
34
|
+
sink.errors.push({ file, field, message });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function warn(sink, file, field, message) {
|
|
38
|
+
sink.warnings.push({ file, field, message });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function requiredString(value, field, max, sink, file) {
|
|
42
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
43
|
+
err(sink, file, field, "is required");
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
if (value.trim().length > max) {
|
|
47
|
+
err(sink, file, field, `is too long (max ${max} chars)`);
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
return value.trim();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function optionalString(value, field, max, sink, file) {
|
|
54
|
+
if (value === undefined || value === null) return "";
|
|
55
|
+
if (typeof value !== "string") {
|
|
56
|
+
err(sink, file, field, "must be a string");
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
if (value.trim().length > max) {
|
|
60
|
+
err(sink, file, field, `is too long (max ${max} chars)`);
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
return value.trim();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function optionalText(value, field, max, sink, file) {
|
|
67
|
+
if (value === undefined || value === null) return undefined;
|
|
68
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
69
|
+
err(sink, file, field, "must be a non-empty string");
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
if (value.trim().length > max) {
|
|
73
|
+
err(sink, file, field, `is too long (max ${max} chars)`);
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
return value.trim();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function optionalUrl(value, field, sink, file) {
|
|
80
|
+
if (value === undefined || value === null || value === "") return undefined;
|
|
81
|
+
if (typeof value !== "string" || value.trim().length > 500) {
|
|
82
|
+
err(sink, file, field, "must be a URL string up to 500 chars");
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
let parsed;
|
|
86
|
+
try {
|
|
87
|
+
parsed = new URL(value.trim());
|
|
88
|
+
} catch {
|
|
89
|
+
err(sink, file, field, "must be a valid URL");
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
|
|
93
|
+
err(sink, file, field, "must be an http(s) URL");
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
return value.trim();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function entryId(value, field, sink, file) {
|
|
100
|
+
if (typeof value !== "string" || !ENTRY_ID_RE.test(value.trim())) {
|
|
101
|
+
err(sink, file, field, "has an invalid id");
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
return value.trim();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function checkKeys(value, allowed, field, sink, file) {
|
|
108
|
+
for (const key of Object.keys(value)) {
|
|
109
|
+
if (!allowed.includes(key)) {
|
|
110
|
+
err(sink, file, field, `contains unsupported field ${key}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** https-only repository URL without credentials or local hosts. */
|
|
116
|
+
export function validateRepositoryUrl(raw, field, sink, file) {
|
|
117
|
+
const url = (raw ?? "").trim();
|
|
118
|
+
if (!url) {
|
|
119
|
+
err(sink, file, field, "is required");
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
if (url.length > 2048) {
|
|
123
|
+
err(sink, file, field, "is too long");
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
let parsed;
|
|
127
|
+
try {
|
|
128
|
+
parsed = new URL(url);
|
|
129
|
+
} catch {
|
|
130
|
+
err(sink, file, field, "must be a valid https URL");
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
if (parsed.protocol !== "https:") {
|
|
134
|
+
err(sink, file, field, "must use https");
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
if (parsed.username || parsed.password) {
|
|
138
|
+
err(sink, file, field, "must not embed credentials");
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
if (!parsed.hostname) {
|
|
142
|
+
err(sink, file, field, "must include a host");
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
if (
|
|
146
|
+
parsed.hostname === "localhost" ||
|
|
147
|
+
parsed.hostname.startsWith("127.") ||
|
|
148
|
+
parsed.hostname === "::1"
|
|
149
|
+
) {
|
|
150
|
+
err(sink, file, field, "must not target local hosts");
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
return parsed.toString();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function validateRef(raw, field, sink, file) {
|
|
157
|
+
if (raw === undefined || raw === null || raw === "") return undefined;
|
|
158
|
+
if (typeof raw !== "string") {
|
|
159
|
+
err(sink, file, field, "must be a string");
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
const ref = raw.trim();
|
|
163
|
+
if (!ref || ref.length > 256 || !REF_RE.test(ref) || ref.includes("..")) {
|
|
164
|
+
err(sink, file, field, "is invalid");
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
return ref;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function stringList(value, field, sink, file) {
|
|
171
|
+
if (value === undefined) return [];
|
|
172
|
+
if (!Array.isArray(value) || value.length > 100) {
|
|
173
|
+
err(sink, file, field, "must be an array of at most 100 strings");
|
|
174
|
+
return [];
|
|
175
|
+
}
|
|
176
|
+
const out = [];
|
|
177
|
+
value.forEach((entry, i) => {
|
|
178
|
+
const s = requiredString(entry, `${field}[${i}]`, 200, sink, file);
|
|
179
|
+
if (s !== undefined) out.push(s);
|
|
180
|
+
});
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function validateModelRoles(input, sink, file) {
|
|
185
|
+
if (!Array.isArray(input) || input.length === 0 || input.length > 4) {
|
|
186
|
+
err(sink, file, "modelRoles", "must declare 1-4 roles including default");
|
|
187
|
+
return [];
|
|
188
|
+
}
|
|
189
|
+
const seen = {};
|
|
190
|
+
const roles = [];
|
|
191
|
+
input.forEach((entry, i) => {
|
|
192
|
+
const field = `modelRoles[${i}]`;
|
|
193
|
+
if (!isRecord(entry)) {
|
|
194
|
+
err(sink, file, field, "must be an object");
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
checkKeys(entry, ["role", "label", "requirements"], field, sink, file);
|
|
198
|
+
if (typeof entry.role !== "string" || !MODEL_ROLES.includes(entry.role)) {
|
|
199
|
+
err(sink, file, `${field}.role`, "must be one of default, smol, slow, plan");
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (seen[entry.role]) {
|
|
203
|
+
err(sink, file, field, `contains duplicate role ${entry.role}`);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
seen[entry.role] = true;
|
|
207
|
+
const label = requiredString(entry.label, `${field}.label`, 100, sink, file);
|
|
208
|
+
if (label === undefined) return;
|
|
209
|
+
const requirements = validateRequirements(entry.requirements, `${field}.requirements`, sink, file);
|
|
210
|
+
if (requirements === undefined) return;
|
|
211
|
+
roles.push({ role: entry.role, label, requirements });
|
|
212
|
+
});
|
|
213
|
+
if (!seen["default"]) {
|
|
214
|
+
err(sink, file, "modelRoles", "must declare a default role");
|
|
215
|
+
}
|
|
216
|
+
return roles;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Model capability requirements. Passed through (validated) so bot.yaml
|
|
221
|
+
* preserves the full platform ModelRequirements contract: tools, vision,
|
|
222
|
+
* reasoning, and minContextWindow.
|
|
223
|
+
*/
|
|
224
|
+
export function validateRequirements(input, field, sink, file) {
|
|
225
|
+
if (input === undefined) return {};
|
|
226
|
+
if (!isRecord(input)) {
|
|
227
|
+
err(sink, file, field, "must be an object");
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
checkKeys(input, ["tools", "vision", "reasoning", "minContextWindow"], field, sink, file);
|
|
231
|
+
const out = {};
|
|
232
|
+
for (const key of ["tools", "vision", "reasoning"]) {
|
|
233
|
+
if (input[key] !== undefined) {
|
|
234
|
+
if (typeof input[key] !== "boolean") {
|
|
235
|
+
err(sink, file, `${field}.${key}`, "must be a boolean");
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
if (input[key]) out[key] = true;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (input.minContextWindow !== undefined) {
|
|
242
|
+
if (typeof input.minContextWindow !== "number" || !Number.isFinite(input.minContextWindow) || input.minContextWindow <= 0) {
|
|
243
|
+
err(sink, file, `${field}.minContextWindow`, "must be a positive number");
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
out.minContextWindow = input.minContextWindow;
|
|
247
|
+
}
|
|
248
|
+
return out;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Structured external knowledge references (BotKnowledge resource/repo
|
|
253
|
+
* kinds). Human-readable links in knowledge/README.md stay documentation
|
|
254
|
+
* only; only these entries become draft knowledge. File-backed knowledge
|
|
255
|
+
* comes from the knowledge/ tree.
|
|
256
|
+
*/
|
|
257
|
+
export function validateKnowledgeSources(input, sink, file) {
|
|
258
|
+
if (input === undefined) return [];
|
|
259
|
+
if (!Array.isArray(input)) {
|
|
260
|
+
err(sink, file, "knowledgeSources", "must be an array");
|
|
261
|
+
return [];
|
|
262
|
+
}
|
|
263
|
+
if (input.length > 100) {
|
|
264
|
+
err(sink, file, "knowledgeSources", "has too many entries");
|
|
265
|
+
return [];
|
|
266
|
+
}
|
|
267
|
+
const ids = {};
|
|
268
|
+
const out = [];
|
|
269
|
+
input.forEach((entry, i) => {
|
|
270
|
+
const field = `knowledgeSources[${i}]`;
|
|
271
|
+
if (!isRecord(entry)) {
|
|
272
|
+
err(sink, file, field, "must be an object");
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
checkKeys(entry, ["id", "name", "kind", "url", "ref"], field, sink, file);
|
|
276
|
+
const id = entryId(entry.id, field, sink, file);
|
|
277
|
+
if (id === undefined) return;
|
|
278
|
+
if (ids[id]) {
|
|
279
|
+
err(sink, file, field, `contains duplicate id ${id}`);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
ids[id] = true;
|
|
283
|
+
if (entry.kind !== "resource" && entry.kind !== "repo") {
|
|
284
|
+
err(sink, file, `${field}.kind`, "must be resource or repo");
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
const name = requiredString(entry.name, `${field}.name`, 200, sink, file);
|
|
288
|
+
if (name === undefined) return;
|
|
289
|
+
const url = optionalUrl(entry.url, `${field}.url`, sink, file);
|
|
290
|
+
if (url === undefined) {
|
|
291
|
+
err(sink, file, `${field}.url`, entry.kind === "repo" ? "repo entries require url" : "resource entries require url");
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const item = { id, name, kind: entry.kind, url };
|
|
295
|
+
if (entry.kind === "repo") {
|
|
296
|
+
const validated = validateRepositoryUrl(url, `${field}.url`, sink, file);
|
|
297
|
+
if (validated === undefined) return;
|
|
298
|
+
item.url = validated;
|
|
299
|
+
const ref = validateRef(entry.ref, `${field}.ref`, sink, file);
|
|
300
|
+
if (ref !== undefined) item.ref = ref;
|
|
301
|
+
}
|
|
302
|
+
out.push(item);
|
|
303
|
+
});
|
|
304
|
+
return out;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function validateConnectors(input, sink, file) {
|
|
308
|
+
if (input === undefined) return [];
|
|
309
|
+
if (!Array.isArray(input)) {
|
|
310
|
+
err(sink, file, "connectors", "must be an array");
|
|
311
|
+
return [];
|
|
312
|
+
}
|
|
313
|
+
if (input.length > 50) {
|
|
314
|
+
err(sink, file, "connectors", "has too many entries");
|
|
315
|
+
return [];
|
|
316
|
+
}
|
|
317
|
+
const ids = {};
|
|
318
|
+
const out = [];
|
|
319
|
+
input.forEach((entry, i) => {
|
|
320
|
+
const field = `connectors[${i}]`;
|
|
321
|
+
if (!isRecord(entry)) {
|
|
322
|
+
err(sink, file, field, "must be an object");
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
checkKeys(
|
|
326
|
+
entry,
|
|
327
|
+
["label", "description", "required", "tools", "resources", "endpoint"],
|
|
328
|
+
field,
|
|
329
|
+
sink,
|
|
330
|
+
file,
|
|
331
|
+
);
|
|
332
|
+
const label = requiredString(entry.label, `${field}.label`, 200, sink, file);
|
|
333
|
+
if (label === undefined) return;
|
|
334
|
+
const id = `connector-${sha256Hex(label).slice(0, 32)}`;
|
|
335
|
+
if (ids[id]) {
|
|
336
|
+
err(sink, file, `${field}.label`, `contains duplicate connector label ${label}`);
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
ids[id] = true;
|
|
340
|
+
if (entry.required !== undefined && typeof entry.required !== "boolean") {
|
|
341
|
+
err(sink, file, `${field}.required`, "must be a boolean");
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if (entry.resources !== undefined && !Array.isArray(entry.resources)) {
|
|
345
|
+
err(sink, file, `${field}.resources`, "must be an array");
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const connectorTools = stringList(entry.tools, `${field}.tools`, sink, file);
|
|
349
|
+
if (connectorTools.length === 0) {
|
|
350
|
+
err(sink, file, field, "must declare at least one tool");
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const resourceKeys = {};
|
|
354
|
+
const resources = [];
|
|
355
|
+
let resourcesOk = true;
|
|
356
|
+
(Array.isArray(entry.resources) ? entry.resources : []).forEach((res, j) => {
|
|
357
|
+
const rfield = `${field}.resources[${j}]`;
|
|
358
|
+
if (!isRecord(res)) {
|
|
359
|
+
err(sink, file, rfield, "must be an object");
|
|
360
|
+
resourcesOk = false;
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
checkKeys(res, ["key", "label", "tools"], rfield, sink, file);
|
|
364
|
+
const key = entryId(res.key, rfield, sink, file);
|
|
365
|
+
if (key === undefined) {
|
|
366
|
+
resourcesOk = false;
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
if (resourceKeys[key]) {
|
|
370
|
+
err(sink, file, rfield, `contains duplicate key ${key}`);
|
|
371
|
+
resourcesOk = false;
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
resourceKeys[key] = true;
|
|
375
|
+
const resourceTools = stringList(res.tools, `${rfield}.tools`, sink, file);
|
|
376
|
+
if (
|
|
377
|
+
resourceTools.length === 0 ||
|
|
378
|
+
resourceTools.some((tool) => !connectorTools.includes(tool))
|
|
379
|
+
) {
|
|
380
|
+
err(sink, file, rfield, "must constrain at least one declared connector tool");
|
|
381
|
+
resourcesOk = false;
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const label = requiredString(res.label, `${rfield}.label`, 200, sink, file);
|
|
385
|
+
if (label === undefined) {
|
|
386
|
+
resourcesOk = false;
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
resources.push({ key, label, tools: resourceTools });
|
|
390
|
+
});
|
|
391
|
+
if (!resourcesOk) return;
|
|
392
|
+
const description = optionalString(
|
|
393
|
+
entry.description,
|
|
394
|
+
`${field}.description`,
|
|
395
|
+
1000,
|
|
396
|
+
sink,
|
|
397
|
+
file,
|
|
398
|
+
);
|
|
399
|
+
if (description === undefined) return;
|
|
400
|
+
const connector = {
|
|
401
|
+
id,
|
|
402
|
+
label,
|
|
403
|
+
description,
|
|
404
|
+
required: entry.required === true,
|
|
405
|
+
tools: connectorTools,
|
|
406
|
+
resources,
|
|
407
|
+
};
|
|
408
|
+
// Remote MCP only: an optional https endpoint declares the single server
|
|
409
|
+
// identity the runtime may bind. Local-execution servers (stdio/command)
|
|
410
|
+
// are rejected: declaring one must never authorize host-level execution.
|
|
411
|
+
if (entry.command !== undefined || entry.args !== undefined || entry.transport === "stdio") {
|
|
412
|
+
err(sink, file, field, "local-execution MCP servers are unsupported: declare a remote endpoint only");
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
if (entry.endpoint !== undefined) {
|
|
416
|
+
const endpoint = optionalUrl(entry.endpoint, `${field}.endpoint`, sink, file);
|
|
417
|
+
if (endpoint === undefined) return;
|
|
418
|
+
if (!endpoint.startsWith("https://")) {
|
|
419
|
+
err(sink, file, `${field}.endpoint`, "must be an https URL");
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
connector.endpoint = endpoint;
|
|
423
|
+
}
|
|
424
|
+
out.push(connector);
|
|
425
|
+
});
|
|
426
|
+
return out;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function normalizeMountPath(raw, sink, file) {
|
|
430
|
+
if (typeof raw !== "string") {
|
|
431
|
+
err(sink, file, "workspace mount path", "must be a string");
|
|
432
|
+
return undefined;
|
|
433
|
+
}
|
|
434
|
+
const path = raw.trim();
|
|
435
|
+
if (path === "./") return "./";
|
|
436
|
+
if (!path.startsWith("./")) {
|
|
437
|
+
err(sink, file, "workspace mount path", "must be canonical ./ or ./name");
|
|
438
|
+
return undefined;
|
|
439
|
+
}
|
|
440
|
+
const rest = path.slice(2);
|
|
441
|
+
if (!rest || rest.length > 200) {
|
|
442
|
+
err(sink, file, "workspace mount path", "must be canonical ./ or ./name");
|
|
443
|
+
return undefined;
|
|
444
|
+
}
|
|
445
|
+
if (rest.endsWith("/")) {
|
|
446
|
+
err(sink, file, "workspace mount path", "must not end with /");
|
|
447
|
+
return undefined;
|
|
448
|
+
}
|
|
449
|
+
const segments = rest.split("/");
|
|
450
|
+
if (segments.length > 5) {
|
|
451
|
+
err(sink, file, "workspace mount path", "is too deep");
|
|
452
|
+
return undefined;
|
|
453
|
+
}
|
|
454
|
+
for (const segment of segments) {
|
|
455
|
+
if (!MOUNT_SEGMENT_RE.test(segment) || segment === "." || segment === "..") {
|
|
456
|
+
err(sink, file, "workspace mount path", "must be canonical ./ or ./name");
|
|
457
|
+
return undefined;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
const lowered = segments.map((s) => s.toLowerCase());
|
|
461
|
+
if (lowered.includes("..")) {
|
|
462
|
+
err(sink, file, "workspace mount path", "must not contain traversal");
|
|
463
|
+
return undefined;
|
|
464
|
+
}
|
|
465
|
+
if (RESERVED_TOP[lowered[0]] === true) {
|
|
466
|
+
err(sink, file, "workspace mount path", "is reserved");
|
|
467
|
+
return undefined;
|
|
468
|
+
}
|
|
469
|
+
if (segments.some((s) => s.startsWith("."))) {
|
|
470
|
+
err(sink, file, "workspace mount path", "is reserved");
|
|
471
|
+
return undefined;
|
|
472
|
+
}
|
|
473
|
+
return "./" + segments.join("/");
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function isPrefixPath(parent, child) {
|
|
477
|
+
if (parent === "./") return true;
|
|
478
|
+
if (parent === child) return true;
|
|
479
|
+
return child.startsWith(parent + "/");
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
export function validateWorkspaceMounts(input, sink, file) {
|
|
483
|
+
if (input === undefined) return [];
|
|
484
|
+
if (!Array.isArray(input)) {
|
|
485
|
+
err(sink, file, "workspace", "must be an array");
|
|
486
|
+
return [];
|
|
487
|
+
}
|
|
488
|
+
if (input.length > 10) {
|
|
489
|
+
err(sink, file, "workspace", "has too many mounts");
|
|
490
|
+
return [];
|
|
491
|
+
}
|
|
492
|
+
const mounts = [];
|
|
493
|
+
input.forEach((entry, i) => {
|
|
494
|
+
const field = `workspace[${i}]`;
|
|
495
|
+
if (!isRecord(entry)) {
|
|
496
|
+
err(sink, file, field, "must be an object");
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
checkKeys(entry, ["key", "label", "path"], field, sink, file);
|
|
500
|
+
const key = entryId(entry.key, `${field}.key`, sink, file);
|
|
501
|
+
const label = optionalText(entry.label, `${field}.label`, 100, sink, file);
|
|
502
|
+
const path = normalizeMountPath(entry.path, sink, file);
|
|
503
|
+
if (path === undefined || key === undefined) return;
|
|
504
|
+
if (mounts.some((mount) => mount.key === key)) {
|
|
505
|
+
err(sink, file, `${field}.key`, `contains duplicate key ${key}`);
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
mounts.push({ key, ...(label === undefined ? {} : { label }), path });
|
|
509
|
+
});
|
|
510
|
+
const paths = {};
|
|
511
|
+
for (const mount of mounts) {
|
|
512
|
+
if (paths[mount.path] === true) {
|
|
513
|
+
err(sink, file, "workspace", `contains duplicate mount path ${mount.path}`);
|
|
514
|
+
}
|
|
515
|
+
paths[mount.path] = true;
|
|
516
|
+
}
|
|
517
|
+
if (mounts.some((m) => m.path === "./") && mounts.length > 1) {
|
|
518
|
+
err(sink, file, "workspace", "mount ./ is exclusive and cannot be combined");
|
|
519
|
+
}
|
|
520
|
+
for (let i = 0; i < mounts.length; i++) {
|
|
521
|
+
for (let j = 0; j < mounts.length; j++) {
|
|
522
|
+
if (i === j) continue;
|
|
523
|
+
if (isPrefixPath(mounts[i].path, mounts[j].path)) {
|
|
524
|
+
err(
|
|
525
|
+
sink,
|
|
526
|
+
file,
|
|
527
|
+
"workspace",
|
|
528
|
+
`mounts ${mounts[i].key} and ${mounts[j].key} overlap`,
|
|
529
|
+
);
|
|
530
|
+
i = mounts.length;
|
|
531
|
+
break;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return mounts;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
export function validateActions(input, sink, file) {
|
|
539
|
+
if (input === undefined) return [];
|
|
540
|
+
if (!Array.isArray(input) || input.length > 20) {
|
|
541
|
+
err(sink, file, "actions", "must be an array of at most 20 groups");
|
|
542
|
+
return [];
|
|
543
|
+
}
|
|
544
|
+
const seen = new Set();
|
|
545
|
+
const out = [];
|
|
546
|
+
input.forEach((entry, i) => {
|
|
547
|
+
const field = `actions[${i}]`;
|
|
548
|
+
if (!isRecord(entry)) {
|
|
549
|
+
err(sink, file, field, "must be an object");
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
checkKeys(entry, ["key", "kind", "label", "capabilities"], field, sink, file);
|
|
553
|
+
const key = entryId(entry.key, `${field}.key`, sink, file);
|
|
554
|
+
const label = optionalText(entry.label, `${field}.label`, 100, sink, file);
|
|
555
|
+
if (entry.kind !== "gitrepo" && entry.kind !== "whatsapp") err(sink, file, `${field}.kind`, "must be gitrepo or whatsapp");
|
|
556
|
+
const supported = entry.kind === "whatsapp" ? WHATSAPP_CAPABILITIES : REPO_CAPABILITIES;
|
|
557
|
+
if (key === undefined) return;
|
|
558
|
+
if (seen.has(key)) err(sink, file, `${field}.key`, `contains duplicate key ${key}`);
|
|
559
|
+
seen.add(key);
|
|
560
|
+
if (!Array.isArray(entry.capabilities) || entry.capabilities.length === 0 ||
|
|
561
|
+
entry.capabilities.length > supported.length) {
|
|
562
|
+
err(sink, file, `${field}.capabilities`, "must be a non-empty array of capabilities");
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
const capabilities = new Set();
|
|
566
|
+
entry.capabilities.forEach((capability, j) => {
|
|
567
|
+
if (!supported.includes(capability)) {
|
|
568
|
+
err(sink, file, `${field}.capabilities[${j}]`, "must be a supported capability for the action kind");
|
|
569
|
+
} else if (capabilities.has(capability)) {
|
|
570
|
+
err(sink, file, `${field}.capabilities`, "contains duplicates");
|
|
571
|
+
} else capabilities.add(capability);
|
|
572
|
+
});
|
|
573
|
+
out.push({ key, kind: entry.kind, ...(label === undefined ? {} : { label }), capabilities: [...capabilities] });
|
|
574
|
+
});
|
|
575
|
+
return out;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function validateInputField(entry, i, sink, file) {
|
|
579
|
+
const field = `triggers.inputs[${i}]`;
|
|
580
|
+
if (!isRecord(entry)) {
|
|
581
|
+
err(sink, file, field, "must be an object");
|
|
582
|
+
return undefined;
|
|
583
|
+
}
|
|
584
|
+
checkKeys(
|
|
585
|
+
entry,
|
|
586
|
+
["key", "label", "type", "required", "default", "options", "help"],
|
|
587
|
+
field,
|
|
588
|
+
sink,
|
|
589
|
+
file,
|
|
590
|
+
);
|
|
591
|
+
if (typeof entry.key !== "string" || !INPUT_KEY_RE.test(entry.key)) {
|
|
592
|
+
err(sink, file, `${field}.key`, "is invalid");
|
|
593
|
+
return undefined;
|
|
594
|
+
}
|
|
595
|
+
if (
|
|
596
|
+
entry.type !== "string" &&
|
|
597
|
+
entry.type !== "number" &&
|
|
598
|
+
entry.type !== "boolean" &&
|
|
599
|
+
entry.type !== "select"
|
|
600
|
+
) {
|
|
601
|
+
err(sink, file, `${field}.type`, "must be string, number, boolean, or select");
|
|
602
|
+
return undefined;
|
|
603
|
+
}
|
|
604
|
+
const out = { key: entry.key, type: entry.type };
|
|
605
|
+
const label = optionalText(entry.label, `${field}.label`, 100, sink, file);
|
|
606
|
+
if (label !== undefined) out.label = label;
|
|
607
|
+
if (entry.required !== undefined) {
|
|
608
|
+
if (typeof entry.required !== "boolean") {
|
|
609
|
+
err(sink, file, `${field}.required`, "must be a boolean");
|
|
610
|
+
return undefined;
|
|
611
|
+
}
|
|
612
|
+
if (entry.required) out.required = true;
|
|
613
|
+
}
|
|
614
|
+
if (entry.default !== undefined) {
|
|
615
|
+
const d = entry.default;
|
|
616
|
+
const ok =
|
|
617
|
+
(entry.type === "string" && typeof d === "string" && d.length <= 2000) ||
|
|
618
|
+
(entry.type === "number" && typeof d === "number" && Number.isFinite(d)) ||
|
|
619
|
+
(entry.type === "boolean" && typeof d === "boolean") ||
|
|
620
|
+
(entry.type === "select" && typeof d === "string" && d.length <= 200);
|
|
621
|
+
if (!ok) {
|
|
622
|
+
err(sink, file, `${field}.default`, "does not match the input type");
|
|
623
|
+
return undefined;
|
|
624
|
+
}
|
|
625
|
+
out.default = d;
|
|
626
|
+
}
|
|
627
|
+
if (entry.options !== undefined || entry.type === "select") {
|
|
628
|
+
if (
|
|
629
|
+
!Array.isArray(entry.options) ||
|
|
630
|
+
entry.options.length === 0 ||
|
|
631
|
+
entry.options.length > 50
|
|
632
|
+
) {
|
|
633
|
+
err(
|
|
634
|
+
sink,
|
|
635
|
+
file,
|
|
636
|
+
`${field}.options`,
|
|
637
|
+
"must be a non-empty array of at most 50 strings for select inputs",
|
|
638
|
+
);
|
|
639
|
+
return undefined;
|
|
640
|
+
}
|
|
641
|
+
const seen = {};
|
|
642
|
+
const options = [];
|
|
643
|
+
for (let j = 0; j < entry.options.length; j++) {
|
|
644
|
+
const option = entry.options[j];
|
|
645
|
+
if (typeof option !== "string" || !option.trim() || option.length > 200) {
|
|
646
|
+
err(sink, file, `${field}.options[${j}]`, "must be a non-empty string up to 200 characters");
|
|
647
|
+
return undefined;
|
|
648
|
+
}
|
|
649
|
+
if (seen[option]) {
|
|
650
|
+
err(sink, file, `${field}.options`, "contains duplicates");
|
|
651
|
+
return undefined;
|
|
652
|
+
}
|
|
653
|
+
seen[option] = true;
|
|
654
|
+
options.push(option);
|
|
655
|
+
}
|
|
656
|
+
out.options = options;
|
|
657
|
+
if (
|
|
658
|
+
out.default !== undefined &&
|
|
659
|
+
typeof out.default === "string" &&
|
|
660
|
+
!seen[out.default]
|
|
661
|
+
) {
|
|
662
|
+
err(sink, file, `${field}.default`, "must be one of the select options");
|
|
663
|
+
return undefined;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
const help = optionalText(entry.help, `${field}.help`, 500, sink, file);
|
|
667
|
+
if (help !== undefined) out.help = help;
|
|
668
|
+
return out;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
export function validateTriggerDefinitions(input, sink, file) {
|
|
672
|
+
if (input === undefined) return [];
|
|
673
|
+
if (!Array.isArray(input)) {
|
|
674
|
+
err(sink, file, "triggers", "must be an array");
|
|
675
|
+
return [];
|
|
676
|
+
}
|
|
677
|
+
if (input.length > 20) {
|
|
678
|
+
err(sink, file, "triggers", "has too many entries");
|
|
679
|
+
return [];
|
|
680
|
+
}
|
|
681
|
+
const seen = {};
|
|
682
|
+
const out = [];
|
|
683
|
+
input.forEach((entry, i) => {
|
|
684
|
+
const field = `triggers[${i}]`;
|
|
685
|
+
if (!isRecord(entry)) {
|
|
686
|
+
err(sink, file, field, "must be an object");
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
checkKeys(
|
|
690
|
+
entry,
|
|
691
|
+
[
|
|
692
|
+
"key",
|
|
693
|
+
"label",
|
|
694
|
+
"description",
|
|
695
|
+
"kind",
|
|
696
|
+
"eventTypes",
|
|
697
|
+
"inputs",
|
|
698
|
+
"deciderRole",
|
|
699
|
+
"deciderInstructions",
|
|
700
|
+
"enabledByDefault",
|
|
701
|
+
],
|
|
702
|
+
field,
|
|
703
|
+
sink,
|
|
704
|
+
file,
|
|
705
|
+
);
|
|
706
|
+
let ok = true;
|
|
707
|
+
if (entry.kind !== "gitrepo" && entry.kind !== "whatsapp") {
|
|
708
|
+
err(sink, file, `${field}.kind`, "must be gitrepo or whatsapp");
|
|
709
|
+
ok = false;
|
|
710
|
+
}
|
|
711
|
+
if (
|
|
712
|
+
!Array.isArray(entry.eventTypes) ||
|
|
713
|
+
entry.eventTypes.length === 0 ||
|
|
714
|
+
entry.eventTypes.length > 20
|
|
715
|
+
) {
|
|
716
|
+
err(
|
|
717
|
+
sink,
|
|
718
|
+
file,
|
|
719
|
+
`${field}.eventTypes`,
|
|
720
|
+
"must be a non-empty array of at most 20 event types",
|
|
721
|
+
);
|
|
722
|
+
ok = false;
|
|
723
|
+
}
|
|
724
|
+
const seenTypes = {};
|
|
725
|
+
const eventTypes = [];
|
|
726
|
+
if (Array.isArray(entry.eventTypes)) {
|
|
727
|
+
entry.eventTypes.forEach((type, j) => {
|
|
728
|
+
if (
|
|
729
|
+
typeof type !== "string" ||
|
|
730
|
+
!type.trim() ||
|
|
731
|
+
type.length > 120 ||
|
|
732
|
+
!EVENT_TYPE_RE.test(type.trim())
|
|
733
|
+
) {
|
|
734
|
+
err(sink, file, `${field}.eventTypes[${j}]`, "is invalid");
|
|
735
|
+
ok = false;
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
const normalized = type.trim();
|
|
739
|
+
if (!(entry.kind === "whatsapp" ? WHATSAPP_TRIGGER_EVENT_TYPES : GITREPO_TRIGGER_EVENT_TYPES).includes(normalized)) {
|
|
740
|
+
err(sink, file, `${field}.eventTypes[${j}]`, "must be a supported semantic event type for the trigger kind");
|
|
741
|
+
ok = false;
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
if (seenTypes[normalized]) {
|
|
745
|
+
err(sink, file, `${field}.eventTypes`, "contains duplicates");
|
|
746
|
+
ok = false;
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
seenTypes[normalized] = true;
|
|
750
|
+
eventTypes.push(normalized);
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
const label = requiredString(entry.label, `${field}.label`, 100, sink, file);
|
|
754
|
+
const deciderInstructions = requiredString(
|
|
755
|
+
entry.deciderInstructions,
|
|
756
|
+
`${field}.deciderInstructions`,
|
|
757
|
+
20000,
|
|
758
|
+
sink,
|
|
759
|
+
file,
|
|
760
|
+
);
|
|
761
|
+
if (label === undefined || deciderInstructions === undefined) ok = false;
|
|
762
|
+
if (!ok) return;
|
|
763
|
+
const key = entryId(entry.key, `${field}.key`, sink, file);
|
|
764
|
+
if (key === undefined) return;
|
|
765
|
+
if (seen[key]) {
|
|
766
|
+
err(sink, file, `${field}.key`, `contains duplicate trigger key ${key}`);
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
seen[key] = true;
|
|
770
|
+
const definition = {
|
|
771
|
+
key,
|
|
772
|
+
label,
|
|
773
|
+
kind: entry.kind,
|
|
774
|
+
eventTypes,
|
|
775
|
+
deciderInstructions,
|
|
776
|
+
};
|
|
777
|
+
const description = optionalText(
|
|
778
|
+
entry.description,
|
|
779
|
+
`${field}.description`,
|
|
780
|
+
2000,
|
|
781
|
+
sink,
|
|
782
|
+
file,
|
|
783
|
+
);
|
|
784
|
+
if (description !== undefined) definition.description = description;
|
|
785
|
+
if (entry.inputs !== undefined) {
|
|
786
|
+
if (!Array.isArray(entry.inputs) || entry.inputs.length > 50) {
|
|
787
|
+
err(
|
|
788
|
+
sink,
|
|
789
|
+
file,
|
|
790
|
+
`${field}.inputs`,
|
|
791
|
+
"must be an array of at most 50 inputs",
|
|
792
|
+
);
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
const keys = {};
|
|
796
|
+
const inputs = [];
|
|
797
|
+
for (let j = 0; j < entry.inputs.length; j++) {
|
|
798
|
+
const validated = validateInputField(entry.inputs[j], j, sink, file);
|
|
799
|
+
if (!validated) return;
|
|
800
|
+
if (keys[validated.key]) {
|
|
801
|
+
err(sink, file, `${field}.inputs`, `contains duplicate key ${validated.key}`);
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
804
|
+
keys[validated.key] = true;
|
|
805
|
+
inputs.push(validated);
|
|
806
|
+
}
|
|
807
|
+
definition.inputs = inputs;
|
|
808
|
+
}
|
|
809
|
+
if (entry.deciderRole !== undefined) {
|
|
810
|
+
if (typeof entry.deciderRole !== "string" || !MODEL_ROLES.includes(entry.deciderRole)) {
|
|
811
|
+
err(
|
|
812
|
+
sink,
|
|
813
|
+
file,
|
|
814
|
+
`${field}.deciderRole`,
|
|
815
|
+
"must be one of default, smol, slow, plan",
|
|
816
|
+
);
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
definition.deciderRole = entry.deciderRole;
|
|
820
|
+
}
|
|
821
|
+
if (entry.enabledByDefault !== undefined) {
|
|
822
|
+
if (typeof entry.enabledByDefault !== "boolean") {
|
|
823
|
+
err(sink, file, `${field}.enabledByDefault`, "must be a boolean");
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
definition.enabledByDefault = entry.enabledByDefault;
|
|
827
|
+
}
|
|
828
|
+
out.push(definition);
|
|
829
|
+
});
|
|
830
|
+
return out;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
export { err, warn };
|
|
835
|
+
export { requiredString, optionalString, optionalText, entryId };
|