@rebon/cli-linux-x64 0.17.3 → 0.19.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.
- package/bin/compose-runtime/package.json +17 -0
- package/bin/compose-runtime/payload/compose/credentials-runtime.js +38 -0
- package/bin/compose-runtime/payload/compose/llm-runtime.js +330 -0
- package/bin/compose-runtime/payload/compose/loop-assembly.js +149 -0
- package/bin/compose-runtime/payload/compose/serve-dispatch.js +82 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-anonymous-user-id.js +10 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-credentials.js +13 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-launch-environment.js +21 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-llm.js +313 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-settings.js +43 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-tools.js +20 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-web.js +10 -0
- package/bin/compose-runtime/payload/compose/shims/llm-adapter.js +32 -0
- package/bin/compose-runtime/payload/compose/shims/zod-lite.js +49 -0
- package/bin/compose-runtime/payload/compose/systemprompt-runtime.js +74 -0
- package/bin/compose-runtime/payload/compose/tools-runtime.js +228 -0
- package/bin/compose-runtime/payload/compose/web-runtime.js +204 -0
- package/bin/compose-runtime/payload/vendor/cordis/index.js +1530 -0
- package/bin/compose-runtime/payload/vendor/cosmokit/LICENSE +21 -0
- package/bin/compose-runtime/payload/vendor/cosmokit/index.mjs +357 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-agent +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-agent-loop +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-llm-core +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-llm-deepseek +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-logger-console +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-scope +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-session +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-system-prompt +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-timeout +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-timer +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-tool-todo +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-tool-web +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-tools-schema +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-web-search-exa +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/agent-loop.js +1193 -0
- package/bin/compose-runtime/payload/vendor/dsh/agent.js +714 -0
- package/bin/compose-runtime/payload/vendor/dsh/llm-core.js +269 -0
- package/bin/compose-runtime/payload/vendor/dsh/llm-deepseek.js +672 -0
- package/bin/compose-runtime/payload/vendor/dsh/logger-console.js +83 -0
- package/bin/compose-runtime/payload/vendor/dsh/scope.js +287 -0
- package/bin/compose-runtime/payload/vendor/dsh/session.js +1668 -0
- package/bin/compose-runtime/payload/vendor/dsh/system-prompt.js +309 -0
- package/bin/compose-runtime/payload/vendor/dsh/timeout.js +100 -0
- package/bin/compose-runtime/payload/vendor/dsh/timer.js +128 -0
- package/bin/compose-runtime/payload/vendor/dsh/tool-todo.js +143 -0
- package/bin/compose-runtime/payload/vendor/dsh/tool-web.js +1527 -0
- package/bin/compose-runtime/payload/vendor/dsh/tools-schema.js +849 -0
- package/bin/compose-runtime/payload/vendor/dsh/web-search-exa.js +122 -0
- package/bin/compose-runtime/payload/vendor/eventsource-parser/LICENSE +21 -0
- package/bin/compose-runtime/payload/vendor/eventsource-parser/index.js +177 -0
- package/bin/compose-runtime/payload/vendor/eventsource-parser/stream.js +48 -0
- package/bin/compose-runtime/payload/vendor/schemastery/index.mjs +656 -0
- package/bin/compose-runtime/payload-manifests.json +48 -0
- package/bin/compose-runtime/src/bridge.mjs +142 -0
- package/bin/compose-runtime/src/credentials-runtime.mjs +50 -0
- package/bin/compose-runtime/src/eventsource-stream.mjs +48 -0
- package/bin/compose-runtime/src/index.mjs +7 -0
- package/bin/compose-runtime/src/llm-runtime.mjs +326 -0
- package/bin/compose-runtime/src/loader.mjs +78 -0
- package/bin/compose-runtime/src/loop-assembly.mjs +237 -0
- package/bin/compose-runtime/src/payload.mjs +49 -0
- package/bin/compose-runtime/src/plugin.mjs +59 -0
- package/bin/compose-runtime/src/realm.mjs +223 -0
- package/bin/compose-runtime/src/registry.mjs +191 -0
- package/bin/compose-runtime/src/resolve.mjs +115 -0
- package/bin/compose-runtime/src/systemprompt-runtime.mjs +67 -0
- package/bin/compose-runtime/src/tools-runtime.mjs +239 -0
- package/bin/compose-runtime/src/web-runtime.mjs +209 -0
- package/bin/plugin-host/package.json +11 -0
- package/bin/plugin-host/src/bridge.mjs +64 -0
- package/bin/plugin-host/src/cli.mjs +105 -0
- package/bin/plugin-host/src/framing.mjs +113 -0
- package/bin/plugin-host/src/host.mjs +624 -0
- package/bin/plugin-host/src/json.mjs +196 -0
- package/bin/plugin-host/src/lifecycle.mjs +114 -0
- package/bin/plugin-host/src/loader.mjs +142 -0
- package/bin/plugin-host/src/methods.mjs +256 -0
- package/bin/plugin-host/src/protocol.mjs +129 -0
- package/bin/plugin-host/src/sdk.mjs +7 -0
- package/bin/rebon +0 -0
- package/bin/rebon-boa-helper +0 -0
- package/package.json +4 -1
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
5
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// src/index.ts
|
|
9
|
+
import { Binary, clone, deepEqual, filterKeys, isNullable, isPlainObject, pick, valueMap } from "cosmokit";
|
|
10
|
+
var require_index = __commonJS({
|
|
11
|
+
"src/index.ts"(exports, module) {
|
|
12
|
+
var kSchema = Symbol.for("schemastery");
|
|
13
|
+
var kValidationError = Symbol.for("ValidationError");
|
|
14
|
+
globalThis.__schemastery_index__ ??= 0;
|
|
15
|
+
globalThis.__schemastery_refs__ = void 0;
|
|
16
|
+
var ValidationError = class extends TypeError {
|
|
17
|
+
constructor(message, options) {
|
|
18
|
+
let prefix = "$";
|
|
19
|
+
for (const segment of options.path || []) {
|
|
20
|
+
if (typeof segment === "string") {
|
|
21
|
+
prefix += "." + segment;
|
|
22
|
+
} else if (typeof segment === "number") {
|
|
23
|
+
prefix += "[" + segment + "]";
|
|
24
|
+
} else if (typeof segment === "symbol") {
|
|
25
|
+
prefix += `[Symbol(${segment.toString()})]`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (prefix.startsWith(".")) prefix = prefix.slice(1);
|
|
29
|
+
super((prefix === "$" ? "" : `${prefix} `) + message);
|
|
30
|
+
this.options = options;
|
|
31
|
+
}
|
|
32
|
+
static {
|
|
33
|
+
__name(this, "ValidationError");
|
|
34
|
+
}
|
|
35
|
+
name = "ValidationError";
|
|
36
|
+
static is(error) {
|
|
37
|
+
return !!error?.[kValidationError];
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(ValidationError.prototype, kValidationError, {
|
|
41
|
+
value: true
|
|
42
|
+
});
|
|
43
|
+
var Schema = /* @__PURE__ */ __name(function(options) {
|
|
44
|
+
const schema = /* @__PURE__ */ __name(function(data, options2 = {}) {
|
|
45
|
+
return Schema.resolve(data, schema, options2)[0];
|
|
46
|
+
}, "schema");
|
|
47
|
+
if (options.refs) {
|
|
48
|
+
const refs = valueMap(options.refs, (options2) => new Schema(options2));
|
|
49
|
+
const getRef = /* @__PURE__ */ __name((uid) => refs[uid], "getRef");
|
|
50
|
+
for (const key in refs) {
|
|
51
|
+
const options2 = refs[key];
|
|
52
|
+
options2.sKey = getRef(options2.sKey);
|
|
53
|
+
options2.inner = getRef(options2.inner);
|
|
54
|
+
options2.list = options2.list && options2.list.map(getRef);
|
|
55
|
+
options2.dict = options2.dict && valueMap(options2.dict, getRef);
|
|
56
|
+
}
|
|
57
|
+
return refs[options.uid];
|
|
58
|
+
}
|
|
59
|
+
Object.assign(schema, options);
|
|
60
|
+
if (typeof schema.callback === "string") {
|
|
61
|
+
try {
|
|
62
|
+
schema.callback = new Function("return " + schema.callback)();
|
|
63
|
+
} catch {
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
Object.defineProperty(schema, "uid", { value: globalThis.__schemastery_index__++ });
|
|
67
|
+
Object.setPrototypeOf(schema, Schema.prototype);
|
|
68
|
+
schema.meta ||= {};
|
|
69
|
+
schema.toString = schema.toString.bind(schema);
|
|
70
|
+
return schema;
|
|
71
|
+
}, "Schema");
|
|
72
|
+
Schema.prototype = Object.create(Function.prototype);
|
|
73
|
+
Schema.prototype[kSchema] = true;
|
|
74
|
+
Object.defineProperty(Schema.prototype, "~standard", {
|
|
75
|
+
get() {
|
|
76
|
+
return {
|
|
77
|
+
version: 1,
|
|
78
|
+
vendor: "schemastery",
|
|
79
|
+
validate: /* @__PURE__ */ __name((value) => {
|
|
80
|
+
try {
|
|
81
|
+
return { value: Schema.resolve(value, this, {})[0] };
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (ValidationError.is(error)) {
|
|
84
|
+
return { issues: [{ message: error.message, path: error.options.path }] };
|
|
85
|
+
}
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
}, "validate")
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
Schema.ValidationError = ValidationError;
|
|
93
|
+
Schema.prototype.toJSON = /* @__PURE__ */ __name(function toJSON() {
|
|
94
|
+
if (globalThis.__schemastery_refs__) {
|
|
95
|
+
globalThis.__schemastery_refs__[this.uid] ??= JSON.parse(JSON.stringify({ ...this }));
|
|
96
|
+
return this.uid;
|
|
97
|
+
}
|
|
98
|
+
globalThis.__schemastery_refs__ = { [this.uid]: { ...this } };
|
|
99
|
+
globalThis.__schemastery_refs__[this.uid] = JSON.parse(JSON.stringify({ ...this }));
|
|
100
|
+
const result = { uid: this.uid, refs: globalThis.__schemastery_refs__ };
|
|
101
|
+
globalThis.__schemastery_refs__ = void 0;
|
|
102
|
+
return result;
|
|
103
|
+
}, "toJSON");
|
|
104
|
+
Schema.prototype.set = /* @__PURE__ */ __name(function set(key, value) {
|
|
105
|
+
this.dict[key] = value;
|
|
106
|
+
return this;
|
|
107
|
+
}, "set");
|
|
108
|
+
Schema.prototype.push = /* @__PURE__ */ __name(function push(value) {
|
|
109
|
+
this.list.push(value);
|
|
110
|
+
return this;
|
|
111
|
+
}, "push");
|
|
112
|
+
function mergeDesc(original, messages) {
|
|
113
|
+
const result = typeof original === "string" ? { "": original } : { ...original };
|
|
114
|
+
for (const locale in messages) {
|
|
115
|
+
const value = messages[locale];
|
|
116
|
+
if (value?.$description || value?.$desc) {
|
|
117
|
+
result[locale] = value.$description || value.$desc;
|
|
118
|
+
} else if (typeof value === "string") {
|
|
119
|
+
result[locale] = value;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
__name(mergeDesc, "mergeDesc");
|
|
125
|
+
function getInner(value) {
|
|
126
|
+
return value?.$value ?? value?.$inner;
|
|
127
|
+
}
|
|
128
|
+
__name(getInner, "getInner");
|
|
129
|
+
function extractKeys(data) {
|
|
130
|
+
return filterKeys(data ?? {}, (key) => !key.startsWith("$"));
|
|
131
|
+
}
|
|
132
|
+
__name(extractKeys, "extractKeys");
|
|
133
|
+
Schema.prototype.i18n = /* @__PURE__ */ __name(function i18n(messages) {
|
|
134
|
+
const schema = Schema(this);
|
|
135
|
+
const desc = mergeDesc(schema.meta.description, messages);
|
|
136
|
+
if (Object.keys(desc).length) schema.meta.description = desc;
|
|
137
|
+
if (schema.dict) {
|
|
138
|
+
schema.dict = valueMap(schema.dict, (inner, key) => {
|
|
139
|
+
return inner.i18n(valueMap(messages, (data) => getInner(data)?.[key] ?? data?.[key]));
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
if (schema.list) {
|
|
143
|
+
schema.list = schema.list.map((inner, index) => {
|
|
144
|
+
return inner.i18n(valueMap(messages, (data = {}) => {
|
|
145
|
+
if (Array.isArray(getInner(data))) return getInner(data)[index];
|
|
146
|
+
if (Array.isArray(data)) return data[index];
|
|
147
|
+
return extractKeys(data);
|
|
148
|
+
}));
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
if (schema.inner) {
|
|
152
|
+
schema.inner = schema.inner.i18n(valueMap(messages, (data) => {
|
|
153
|
+
if (getInner(data)) return getInner(data);
|
|
154
|
+
return extractKeys(data);
|
|
155
|
+
}));
|
|
156
|
+
}
|
|
157
|
+
if (schema.sKey) {
|
|
158
|
+
schema.sKey = schema.sKey.i18n(valueMap(messages, (data) => data?.$key));
|
|
159
|
+
}
|
|
160
|
+
return schema;
|
|
161
|
+
}, "i18n");
|
|
162
|
+
Schema.prototype.extra = /* @__PURE__ */ __name(function extra(key, value) {
|
|
163
|
+
const schema = Schema(this);
|
|
164
|
+
schema.meta = { ...schema.meta, [key]: value };
|
|
165
|
+
return schema;
|
|
166
|
+
}, "extra");
|
|
167
|
+
for (const key of ["required", "disabled", "collapse", "hidden", "loose"]) {
|
|
168
|
+
Object.assign(Schema.prototype, {
|
|
169
|
+
[key](value = true) {
|
|
170
|
+
const schema = Schema(this);
|
|
171
|
+
schema.meta = { ...schema.meta, [key]: value };
|
|
172
|
+
return schema;
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
Schema.prototype.deprecated = /* @__PURE__ */ __name(function deprecated() {
|
|
177
|
+
const schema = Schema(this);
|
|
178
|
+
schema.meta.badges ||= [];
|
|
179
|
+
schema.meta.badges.push({ text: "deprecated", type: "danger" });
|
|
180
|
+
return schema;
|
|
181
|
+
}, "deprecated");
|
|
182
|
+
Schema.prototype.experimental = /* @__PURE__ */ __name(function experimental() {
|
|
183
|
+
const schema = Schema(this);
|
|
184
|
+
schema.meta.badges ||= [];
|
|
185
|
+
schema.meta.badges.push({ text: "experimental", type: "warning" });
|
|
186
|
+
return schema;
|
|
187
|
+
}, "experimental");
|
|
188
|
+
Schema.prototype.pattern = /* @__PURE__ */ __name(function pattern(regexp) {
|
|
189
|
+
const schema = Schema(this);
|
|
190
|
+
const pattern2 = pick(regexp, ["source", "flags"]);
|
|
191
|
+
schema.meta = { ...schema.meta, pattern: pattern2 };
|
|
192
|
+
return schema;
|
|
193
|
+
}, "pattern");
|
|
194
|
+
Schema.prototype.simplify = /* @__PURE__ */ __name(function simplify(value) {
|
|
195
|
+
if (deepEqual(value, this.meta.default, this.type === "dict")) return null;
|
|
196
|
+
if (isNullable(value)) return value;
|
|
197
|
+
if (this.type === "object" || this.type === "dict") {
|
|
198
|
+
const result = {};
|
|
199
|
+
for (const key in value) {
|
|
200
|
+
const schema = this.type === "object" ? this.dict[key] : this.inner;
|
|
201
|
+
const item = schema?.simplify(value[key]);
|
|
202
|
+
if (this.type === "dict" || !isNullable(item)) result[key] = item;
|
|
203
|
+
}
|
|
204
|
+
if (deepEqual(result, this.meta.default, this.type === "dict")) return null;
|
|
205
|
+
return result;
|
|
206
|
+
} else if (this.type === "array" || this.type === "tuple") {
|
|
207
|
+
const result = [];
|
|
208
|
+
value.forEach((value2, index) => {
|
|
209
|
+
const schema = this.type === "array" ? this.inner : this.list[index];
|
|
210
|
+
const item = schema ? schema.simplify(value2) : value2;
|
|
211
|
+
result.push(item);
|
|
212
|
+
});
|
|
213
|
+
return result;
|
|
214
|
+
} else if (this.type === "intersect") {
|
|
215
|
+
const result = {};
|
|
216
|
+
for (const item of this.list) {
|
|
217
|
+
Object.assign(result, item.simplify(value));
|
|
218
|
+
}
|
|
219
|
+
return result;
|
|
220
|
+
} else if (this.type === "union") {
|
|
221
|
+
for (const schema of this.list) {
|
|
222
|
+
try {
|
|
223
|
+
Schema.resolve(value, schema, {});
|
|
224
|
+
return schema.simplify(value);
|
|
225
|
+
} catch {
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return value;
|
|
230
|
+
}, "simplify");
|
|
231
|
+
Schema.prototype.toString = /* @__PURE__ */ __name(function toString(inline) {
|
|
232
|
+
return formatters[this.type]?.(this, inline) ?? `Schema<${this.type}>`;
|
|
233
|
+
}, "toString");
|
|
234
|
+
Schema.prototype.role = /* @__PURE__ */ __name(function role(role, extra) {
|
|
235
|
+
const schema = Schema(this);
|
|
236
|
+
schema.meta = { ...schema.meta, role, extra };
|
|
237
|
+
return schema;
|
|
238
|
+
}, "role");
|
|
239
|
+
for (const key of ["default", "link", "comment", "description", "max", "min", "step"]) {
|
|
240
|
+
Object.assign(Schema.prototype, {
|
|
241
|
+
[key](value) {
|
|
242
|
+
const schema = Schema(this);
|
|
243
|
+
schema.meta = { ...schema.meta, [key]: value };
|
|
244
|
+
return schema;
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
var resolvers = {};
|
|
249
|
+
Schema.extend = /* @__PURE__ */ __name(function extend(type, resolve) {
|
|
250
|
+
resolvers[type] = resolve;
|
|
251
|
+
}, "extend");
|
|
252
|
+
Schema.resolve = /* @__PURE__ */ __name(function resolve(data, schema, options = {}, strict = false) {
|
|
253
|
+
if (!schema) return [data];
|
|
254
|
+
if (options.ignore?.(data, schema)) return [data];
|
|
255
|
+
if (isNullable(data) && schema.type !== "lazy") {
|
|
256
|
+
if (schema.meta.required) throw new ValidationError(`missing required value`, options);
|
|
257
|
+
let current = schema;
|
|
258
|
+
let fallback = schema.meta.default;
|
|
259
|
+
while (current?.type === "intersect" && isNullable(fallback)) {
|
|
260
|
+
current = current.list[0];
|
|
261
|
+
fallback = current?.meta.default;
|
|
262
|
+
}
|
|
263
|
+
if (isNullable(fallback)) return [data];
|
|
264
|
+
data = clone(fallback);
|
|
265
|
+
}
|
|
266
|
+
const callback = resolvers[schema.type];
|
|
267
|
+
if (!callback) throw new ValidationError(`unsupported type "${schema.type}"`, options);
|
|
268
|
+
try {
|
|
269
|
+
return callback(data, schema, options, strict);
|
|
270
|
+
} catch (error) {
|
|
271
|
+
if (!schema.meta.loose) throw error;
|
|
272
|
+
return [schema.meta.default];
|
|
273
|
+
}
|
|
274
|
+
}, "resolve");
|
|
275
|
+
Schema.from = /* @__PURE__ */ __name(function from(source) {
|
|
276
|
+
if (isNullable(source)) {
|
|
277
|
+
return Schema.any();
|
|
278
|
+
} else if (["string", "number", "boolean"].includes(typeof source)) {
|
|
279
|
+
return Schema.const(source).required();
|
|
280
|
+
} else if (source[kSchema]) {
|
|
281
|
+
return source;
|
|
282
|
+
} else if (typeof source === "function") {
|
|
283
|
+
switch (source) {
|
|
284
|
+
case String:
|
|
285
|
+
return Schema.string().required();
|
|
286
|
+
case Number:
|
|
287
|
+
return Schema.number().required();
|
|
288
|
+
case Boolean:
|
|
289
|
+
return Schema.boolean().required();
|
|
290
|
+
case Function:
|
|
291
|
+
return Schema.function().required();
|
|
292
|
+
default:
|
|
293
|
+
return Schema.is(source).required();
|
|
294
|
+
}
|
|
295
|
+
} else {
|
|
296
|
+
throw new TypeError(`cannot infer schema from ${source}`);
|
|
297
|
+
}
|
|
298
|
+
}, "from");
|
|
299
|
+
Schema.lazy = /* @__PURE__ */ __name(function lazy(builder) {
|
|
300
|
+
const toJSON = /* @__PURE__ */ __name(() => {
|
|
301
|
+
if (!schema.inner[kSchema]) {
|
|
302
|
+
schema.inner = schema.builder();
|
|
303
|
+
schema.inner.meta = { ...schema.meta, ...schema.inner.meta };
|
|
304
|
+
}
|
|
305
|
+
return schema.inner.toJSON();
|
|
306
|
+
}, "toJSON");
|
|
307
|
+
const schema = new Schema({ type: "lazy", builder, inner: { toJSON } });
|
|
308
|
+
return schema;
|
|
309
|
+
}, "lazy");
|
|
310
|
+
Schema.natural = /* @__PURE__ */ __name(function natural() {
|
|
311
|
+
return Schema.number().step(1).min(0);
|
|
312
|
+
}, "natural");
|
|
313
|
+
Schema.percent = /* @__PURE__ */ __name(function percent() {
|
|
314
|
+
return Schema.number().step(0.01).min(0).max(1).role("slider");
|
|
315
|
+
}, "percent");
|
|
316
|
+
Schema.date = /* @__PURE__ */ __name(function date() {
|
|
317
|
+
return Schema.union([
|
|
318
|
+
Schema.is(Date),
|
|
319
|
+
Schema.transform(Schema.string().role("datetime"), (value, options) => {
|
|
320
|
+
const date2 = new Date(value);
|
|
321
|
+
if (isNaN(+date2)) throw new ValidationError(`invalid date "${value}"`, options);
|
|
322
|
+
return date2;
|
|
323
|
+
}, true)
|
|
324
|
+
]);
|
|
325
|
+
}, "date");
|
|
326
|
+
Schema.regExp = /* @__PURE__ */ __name(function regExp(flag = "") {
|
|
327
|
+
return Schema.union([
|
|
328
|
+
Schema.is(RegExp),
|
|
329
|
+
Schema.transform(Schema.string().role("regexp", { flag }), (value, options) => {
|
|
330
|
+
try {
|
|
331
|
+
return new RegExp(value, flag);
|
|
332
|
+
} catch (e) {
|
|
333
|
+
throw new ValidationError(e.message, options);
|
|
334
|
+
}
|
|
335
|
+
}, true)
|
|
336
|
+
]);
|
|
337
|
+
}, "regExp");
|
|
338
|
+
Schema.arrayBuffer = /* @__PURE__ */ __name(function arrayBuffer(encoding) {
|
|
339
|
+
return Schema.union([
|
|
340
|
+
Schema.is(ArrayBuffer),
|
|
341
|
+
Schema.is(SharedArrayBuffer),
|
|
342
|
+
Schema.transform(Schema.any(), (value, options) => {
|
|
343
|
+
if (Binary.isSource(value)) return Binary.fromSource(value);
|
|
344
|
+
throw new ValidationError(`expected ArrayBufferSource but got ${value}`, options);
|
|
345
|
+
}, true),
|
|
346
|
+
...encoding ? [Schema.transform(Schema.string(), (value, options) => {
|
|
347
|
+
try {
|
|
348
|
+
return encoding === "base64" ? Binary.fromBase64(value) : Binary.fromHex(value);
|
|
349
|
+
} catch (e) {
|
|
350
|
+
throw new ValidationError(e.message, options);
|
|
351
|
+
}
|
|
352
|
+
}, true)] : []
|
|
353
|
+
]);
|
|
354
|
+
}, "arrayBuffer");
|
|
355
|
+
Schema.extend("lazy", (data, schema, options, strict) => {
|
|
356
|
+
if (!schema.inner[kSchema]) {
|
|
357
|
+
schema.inner = schema.builder();
|
|
358
|
+
schema.inner.meta = { ...schema.meta, ...schema.inner.meta };
|
|
359
|
+
}
|
|
360
|
+
return Schema.resolve(data, schema.inner, options, strict);
|
|
361
|
+
});
|
|
362
|
+
Schema.extend("any", (data) => {
|
|
363
|
+
return [data];
|
|
364
|
+
});
|
|
365
|
+
Schema.extend("never", (data, _, options) => {
|
|
366
|
+
throw new ValidationError(`expected nullable but got ${data}`, options);
|
|
367
|
+
});
|
|
368
|
+
Schema.extend("const", (data, { value }, options) => {
|
|
369
|
+
if (deepEqual(data, value)) return [value];
|
|
370
|
+
throw new ValidationError(`expected ${value} but got ${data}`, options);
|
|
371
|
+
});
|
|
372
|
+
function checkWithinRange(data, meta, description, options, skipMin = false) {
|
|
373
|
+
const { max = Infinity, min = -Infinity } = meta;
|
|
374
|
+
if (data > max) throw new ValidationError(`expected ${description} <= ${max} but got ${data}`, options);
|
|
375
|
+
if (data < min && !skipMin) throw new ValidationError(`expected ${description} >= ${min} but got ${data}`, options);
|
|
376
|
+
}
|
|
377
|
+
__name(checkWithinRange, "checkWithinRange");
|
|
378
|
+
Schema.extend("string", (data, { meta }, options) => {
|
|
379
|
+
if (typeof data !== "string") throw new ValidationError(`expected string but got ${data}`, options);
|
|
380
|
+
if (meta.pattern) {
|
|
381
|
+
const regexp = new RegExp(meta.pattern.source, meta.pattern.flags);
|
|
382
|
+
if (!regexp.test(data)) throw new ValidationError(`expect string to match regexp ${regexp}`, options);
|
|
383
|
+
}
|
|
384
|
+
checkWithinRange(data.length, meta, "string length", options);
|
|
385
|
+
return [data];
|
|
386
|
+
});
|
|
387
|
+
function decimalShift(data, digits) {
|
|
388
|
+
const str = data.toString();
|
|
389
|
+
if (str.includes("e")) return data * Math.pow(10, digits);
|
|
390
|
+
const index = str.indexOf(".");
|
|
391
|
+
if (index === -1) return data * Math.pow(10, digits);
|
|
392
|
+
const frac = str.slice(index + 1);
|
|
393
|
+
const integer = str.slice(0, index);
|
|
394
|
+
if (frac.length <= digits) return +(integer + frac.padEnd(digits, "0"));
|
|
395
|
+
return +(integer + frac.slice(0, digits) + "." + frac.slice(digits));
|
|
396
|
+
}
|
|
397
|
+
__name(decimalShift, "decimalShift");
|
|
398
|
+
function isMultipleOf(data, min, step) {
|
|
399
|
+
step = Math.abs(step);
|
|
400
|
+
if (!/^\d+\.\d+$/.test(step.toString())) {
|
|
401
|
+
return (data - min) % step === 0;
|
|
402
|
+
}
|
|
403
|
+
const index = step.toString().indexOf(".");
|
|
404
|
+
const digits = step.toString().slice(index + 1).length;
|
|
405
|
+
return Math.abs(decimalShift(data, digits) - decimalShift(min, digits)) % decimalShift(step, digits) === 0;
|
|
406
|
+
}
|
|
407
|
+
__name(isMultipleOf, "isMultipleOf");
|
|
408
|
+
Schema.extend("number", (data, { meta }, options) => {
|
|
409
|
+
if (typeof data !== "number") throw new ValidationError(`expected number but got ${data}`, options);
|
|
410
|
+
checkWithinRange(data, meta, "number", options);
|
|
411
|
+
const { step } = meta;
|
|
412
|
+
if (step && !isMultipleOf(data, meta.min ?? 0, step)) {
|
|
413
|
+
throw new ValidationError(`expected number multiple of ${step} but got ${data}`, options);
|
|
414
|
+
}
|
|
415
|
+
return [data];
|
|
416
|
+
});
|
|
417
|
+
Schema.extend("boolean", (data, _, options) => {
|
|
418
|
+
if (typeof data === "boolean") return [data];
|
|
419
|
+
throw new ValidationError(`expected boolean but got ${data}`, options);
|
|
420
|
+
});
|
|
421
|
+
Schema.extend("bitset", (data, { bits, meta }, options) => {
|
|
422
|
+
let value = 0, keys = [];
|
|
423
|
+
if (typeof data === "number") {
|
|
424
|
+
value = data;
|
|
425
|
+
for (const key in bits) {
|
|
426
|
+
if (data & bits[key]) {
|
|
427
|
+
keys.push(key);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
} else if (Array.isArray(data)) {
|
|
431
|
+
keys = data;
|
|
432
|
+
for (const key of keys) {
|
|
433
|
+
if (typeof key !== "string") throw new ValidationError(`expected string but got ${key}`, options);
|
|
434
|
+
if (key in bits) value |= bits[key];
|
|
435
|
+
}
|
|
436
|
+
} else {
|
|
437
|
+
throw new ValidationError(`expected number or array but got ${data}`, options);
|
|
438
|
+
}
|
|
439
|
+
if (value === meta.default) return [value];
|
|
440
|
+
return [value, keys];
|
|
441
|
+
});
|
|
442
|
+
Schema.extend("function", (data, _, options) => {
|
|
443
|
+
if (typeof data === "function") return [data];
|
|
444
|
+
throw new ValidationError(`expected function but got ${data}`, options);
|
|
445
|
+
});
|
|
446
|
+
Schema.extend("is", (data, { constructor }, options) => {
|
|
447
|
+
if (typeof constructor === "function") {
|
|
448
|
+
if (data instanceof constructor) return [data];
|
|
449
|
+
throw new ValidationError(`expected ${constructor.name} but got ${data}`, options);
|
|
450
|
+
} else {
|
|
451
|
+
if (isNullable(data)) {
|
|
452
|
+
throw new ValidationError(`expected ${constructor} but got ${data}`, options);
|
|
453
|
+
}
|
|
454
|
+
let prototype = Object.getPrototypeOf(data);
|
|
455
|
+
while (prototype) {
|
|
456
|
+
if (prototype.constructor?.name === constructor) return [data];
|
|
457
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
458
|
+
}
|
|
459
|
+
throw new ValidationError(`expected ${constructor} but got ${data}`, options);
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
function property(data, key, schema, options) {
|
|
463
|
+
try {
|
|
464
|
+
const [value, adapted] = Schema.resolve(data[key], schema, {
|
|
465
|
+
...options,
|
|
466
|
+
path: [...options.path || [], key]
|
|
467
|
+
});
|
|
468
|
+
if (adapted !== void 0) data[key] = adapted;
|
|
469
|
+
return value;
|
|
470
|
+
} catch (e) {
|
|
471
|
+
if (!options?.autofix) throw e;
|
|
472
|
+
delete data[key];
|
|
473
|
+
return schema.meta.default;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
__name(property, "property");
|
|
477
|
+
Schema.extend("array", (data, { inner, meta }, options) => {
|
|
478
|
+
if (!Array.isArray(data)) throw new ValidationError(`expected array but got ${data}`, options);
|
|
479
|
+
checkWithinRange(data.length, meta, "array length", options, !isNullable(inner.meta.default));
|
|
480
|
+
return [data.map((_, index) => property(data, index, inner, options))];
|
|
481
|
+
});
|
|
482
|
+
Schema.extend("dict", (data, { inner, sKey }, options, strict) => {
|
|
483
|
+
if (!isPlainObject(data)) throw new ValidationError(`expected object but got ${data}`, options);
|
|
484
|
+
const result = {};
|
|
485
|
+
for (const key in data) {
|
|
486
|
+
let rKey;
|
|
487
|
+
try {
|
|
488
|
+
rKey = Schema.resolve(key, sKey, options)[0];
|
|
489
|
+
} catch (error) {
|
|
490
|
+
if (strict) continue;
|
|
491
|
+
throw error;
|
|
492
|
+
}
|
|
493
|
+
result[rKey] = property(data, key, inner, options);
|
|
494
|
+
data[rKey] = data[key];
|
|
495
|
+
if (key !== rKey) delete data[key];
|
|
496
|
+
}
|
|
497
|
+
return [result];
|
|
498
|
+
});
|
|
499
|
+
Schema.extend("tuple", (data, { list }, options, strict) => {
|
|
500
|
+
if (!Array.isArray(data)) throw new ValidationError(`expected array but got ${data}`, options);
|
|
501
|
+
const result = list.map((inner, index) => property(data, index, inner, options));
|
|
502
|
+
if (strict) return [result];
|
|
503
|
+
result.push(...data.slice(list.length));
|
|
504
|
+
return [result];
|
|
505
|
+
});
|
|
506
|
+
function merge(result, data) {
|
|
507
|
+
for (const key in data) {
|
|
508
|
+
if (key in result) continue;
|
|
509
|
+
result[key] = data[key];
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
__name(merge, "merge");
|
|
513
|
+
Schema.extend("object", (data, { dict }, options, strict) => {
|
|
514
|
+
if (!isPlainObject(data)) throw new ValidationError(`expected object but got ${data}`, options);
|
|
515
|
+
const result = {};
|
|
516
|
+
for (const key in dict) {
|
|
517
|
+
const value = property(data, key, dict[key], options);
|
|
518
|
+
if (!isNullable(value) || key in data) {
|
|
519
|
+
result[key] = value;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
if (!strict) merge(result, data);
|
|
523
|
+
return [result];
|
|
524
|
+
});
|
|
525
|
+
Schema.extend("union", (data, { list, toString }, options, strict) => {
|
|
526
|
+
const messages = [];
|
|
527
|
+
for (const inner of list) {
|
|
528
|
+
try {
|
|
529
|
+
return Schema.resolve(data, inner, options, strict);
|
|
530
|
+
} catch (error) {
|
|
531
|
+
messages.push(error);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
throw new ValidationError(`expected ${toString()} but got ${JSON.stringify(data)}`, options);
|
|
535
|
+
});
|
|
536
|
+
Schema.extend("intersect", (data, { list, toString }, options, strict) => {
|
|
537
|
+
if (!list.length) return [data];
|
|
538
|
+
let result;
|
|
539
|
+
for (const inner of list) {
|
|
540
|
+
const value = Schema.resolve(data, inner, options, true)[0];
|
|
541
|
+
if (isNullable(value)) continue;
|
|
542
|
+
if (isNullable(result)) {
|
|
543
|
+
result = value;
|
|
544
|
+
} else if (typeof result !== typeof value) {
|
|
545
|
+
throw new ValidationError(`expected ${toString()} but got ${JSON.stringify(data)}`, options);
|
|
546
|
+
} else if (typeof value === "object") {
|
|
547
|
+
merge(result ??= {}, value);
|
|
548
|
+
} else if (result !== value) {
|
|
549
|
+
throw new ValidationError(`expected ${toString()} but got ${JSON.stringify(data)}`, options);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
if (!strict && isPlainObject(data)) merge(result, data);
|
|
553
|
+
return [result];
|
|
554
|
+
});
|
|
555
|
+
Schema.extend("transform", (data, { inner, callback, preserve }, options) => {
|
|
556
|
+
const [result, adapted = data] = Schema.resolve(data, inner, options, true);
|
|
557
|
+
if (preserve) {
|
|
558
|
+
return [callback(result)];
|
|
559
|
+
} else {
|
|
560
|
+
return [callback(result), callback(adapted)];
|
|
561
|
+
}
|
|
562
|
+
});
|
|
563
|
+
var formatters = {};
|
|
564
|
+
function defineMethod(name, keys, format) {
|
|
565
|
+
formatters[name] = format;
|
|
566
|
+
Object.assign(Schema, {
|
|
567
|
+
[name](...args) {
|
|
568
|
+
const schema = new Schema({ type: name });
|
|
569
|
+
keys.forEach((key, index) => {
|
|
570
|
+
switch (key) {
|
|
571
|
+
case "sKey":
|
|
572
|
+
schema.sKey = args[index] ?? Schema.string();
|
|
573
|
+
break;
|
|
574
|
+
case "inner":
|
|
575
|
+
schema.inner = Schema.from(args[index]);
|
|
576
|
+
break;
|
|
577
|
+
case "list":
|
|
578
|
+
schema.list = args[index].map(Schema.from);
|
|
579
|
+
break;
|
|
580
|
+
case "dict":
|
|
581
|
+
schema.dict = valueMap(args[index], Schema.from);
|
|
582
|
+
break;
|
|
583
|
+
case "bits": {
|
|
584
|
+
schema.bits = {};
|
|
585
|
+
for (const key2 in args[index]) {
|
|
586
|
+
if (typeof args[index][key2] !== "number") continue;
|
|
587
|
+
schema.bits[key2] = args[index][key2];
|
|
588
|
+
}
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
case "callback": {
|
|
592
|
+
const callback = schema.callback = args[index];
|
|
593
|
+
callback["toJSON"] ||= () => callback.toString();
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
case "constructor": {
|
|
597
|
+
const constructor = schema.constructor = args[index];
|
|
598
|
+
if (typeof constructor === "function") {
|
|
599
|
+
;
|
|
600
|
+
constructor["toJSON"] ||= () => constructor["name"];
|
|
601
|
+
}
|
|
602
|
+
break;
|
|
603
|
+
}
|
|
604
|
+
default:
|
|
605
|
+
schema[key] = args[index];
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
if (name === "object" || name === "dict") {
|
|
609
|
+
schema.meta.default = {};
|
|
610
|
+
} else if (name === "array" || name === "tuple") {
|
|
611
|
+
schema.meta.default = [];
|
|
612
|
+
} else if (name === "bitset") {
|
|
613
|
+
schema.meta.default = 0;
|
|
614
|
+
}
|
|
615
|
+
return schema;
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
__name(defineMethod, "defineMethod");
|
|
620
|
+
defineMethod("is", ["constructor"], ({ constructor }) => {
|
|
621
|
+
if (typeof constructor === "function") {
|
|
622
|
+
return constructor.name;
|
|
623
|
+
} else {
|
|
624
|
+
return constructor;
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
defineMethod("any", [], () => "any");
|
|
628
|
+
defineMethod("never", [], () => "never");
|
|
629
|
+
defineMethod("const", ["value"], ({ value }) => typeof value === "string" ? JSON.stringify(value) : value);
|
|
630
|
+
defineMethod("string", [], () => "string");
|
|
631
|
+
defineMethod("number", [], () => "number");
|
|
632
|
+
defineMethod("boolean", [], () => "boolean");
|
|
633
|
+
defineMethod("bitset", ["bits"], () => "bitset");
|
|
634
|
+
defineMethod("function", [], () => "function");
|
|
635
|
+
defineMethod("array", ["inner"], ({ inner }) => `${inner.toString(true)}[]`);
|
|
636
|
+
defineMethod("dict", ["inner", "sKey"], ({ inner, sKey }) => `{ [key: ${sKey.toString()}]: ${inner.toString()} }`);
|
|
637
|
+
defineMethod("tuple", ["list"], ({ list }) => `[${list.map((inner) => inner.toString()).join(", ")}]`);
|
|
638
|
+
defineMethod("object", ["dict"], ({ dict }) => {
|
|
639
|
+
if (Object.keys(dict).length === 0) return "{}";
|
|
640
|
+
return `{ ${Object.entries(dict).map(([key, inner]) => {
|
|
641
|
+
return `${key}${inner.meta.required ? "" : "?"}: ${inner.toString()}`;
|
|
642
|
+
}).join(", ")} }`;
|
|
643
|
+
});
|
|
644
|
+
defineMethod("union", ["list"], ({ list }, inline) => {
|
|
645
|
+
const result = list.map(({ toString: format }) => format()).join(" | ");
|
|
646
|
+
return inline ? `(${result})` : result;
|
|
647
|
+
});
|
|
648
|
+
defineMethod("intersect", ["list"], ({ list }) => {
|
|
649
|
+
return `${list.map((inner) => inner.toString(true)).join(" & ")}`;
|
|
650
|
+
});
|
|
651
|
+
defineMethod("transform", ["inner", "callback", "preserve"], ({ inner }, isInner) => inner.toString(isInner));
|
|
652
|
+
module.exports = Schema;
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
export default require_index();
|
|
656
|
+
//# sourceMappingURL=index.mjs.map
|