@carljia/omd-dsh 0.1.7 → 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/README.md +14 -11
- package/lib/boot.d.ts +4 -4
- package/lib/boot.js +6 -11
- package/lib/cli.js +9 -21
- package/lib/index.js +4 -5
- package/lib/mode.js +1 -4
- package/lib/plan.js +1 -4
- package/lib/startwork.js +1 -4
- package/lib/sync.d.ts +13 -22
- package/lib/sync.js +9 -213
- package/lib/task.js +1 -4
- package/lib/vendor/omd-mode-switch.mjs +967 -127
- package/lib/vendor/omd-mode.mjs +889 -174
- package/lib/vendor/omd-plan.mjs +95 -177
- package/lib/vendor/omd-start-work.mjs +96 -132
- package/lib/vendor/omd-task.mjs +20735 -265
- package/lib/vendor/omd-ulw.mjs +44 -49
- package/package.json +6 -5
|
@@ -1,145 +1,985 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @module @carljia/omd-dsh/mode
|
|
5
|
-
*
|
|
6
|
-
* omd-mode-switch: human-facing `/mode` command -- switch the CURRENT
|
|
7
|
-
* session to another OMD agent preset, including mid-conversation.
|
|
8
|
-
*
|
|
9
|
-
* DSH's native `agentPreset.select` host API refuses to recompose a
|
|
10
|
-
* session that has already started (its preset is fixed at the UI level),
|
|
11
|
-
* and the AgentPresets.recompose method itself performs no history check
|
|
12
|
-
* ("the CALLER owns that check"). This row deliberately performs the
|
|
13
|
-
* in-session recompose, then keeps the log honest:
|
|
14
|
-
* - appends `agent-preset/selected`, so resume/fork rebuild the same
|
|
15
|
-
* composition ("model-visible <-> logged" rule);
|
|
16
|
-
* - appends `plan/mode { active: false }` when plan mode is still
|
|
17
|
-
* active, since the switch itself is a mode decision;
|
|
18
|
-
* - steers a notice message so the model knows the tool set changed.
|
|
19
|
-
*
|
|
20
|
-
* Mitigations for the swapped tool catalog: the omd-planner catalog is a
|
|
21
|
-
* subset of omd-executor's (the executor preset also mounts the plan-mode
|
|
22
|
-
* row), so logged planner tool calls stay renderable under the executor
|
|
23
|
-
* composition. Switching between other omd presets follows the same rule
|
|
24
|
-
* and the model simply receives the new catalog on the next request.
|
|
25
|
-
*/
|
|
26
|
-
/** Cordis plugin name. */
|
|
27
|
-
const name = "omd-mode-switch";
|
|
28
|
-
/**
|
|
29
|
-
* No mount-time injection: the roster service is resolved at runtime so a
|
|
30
|
-
* rosterless deployment fails only the /mode command, never the preset
|
|
31
|
-
* mount itself.
|
|
32
|
-
*/
|
|
33
|
-
const inject = [];
|
|
34
|
-
/** The OMD presets /mode may switch to. */
|
|
35
|
-
const OMD_PRESET_IDS = [
|
|
36
|
-
"omd-executor",
|
|
37
|
-
"omd-ultraworker",
|
|
38
|
-
"omd-planner",
|
|
39
|
-
"omd-reviewer",
|
|
40
|
-
"omd-explorer",
|
|
41
|
-
"omd-librarian",
|
|
42
|
-
"omd-chat",
|
|
43
|
-
];
|
|
44
|
-
/** Normalize the command input to a valid omd preset id, or undefined. */
|
|
45
|
-
function normalizeTarget(rawInput) {
|
|
46
|
-
const trimmed = String(rawInput).trim().toLowerCase();
|
|
47
|
-
if (trimmed === "")
|
|
48
|
-
return undefined;
|
|
49
|
-
const candidate = trimmed.startsWith("omd-") ? trimmed : "omd-" + trimmed;
|
|
50
|
-
return OMD_PRESET_IDS.includes(candidate) ? candidate : undefined;
|
|
1
|
+
// ../../node_modules/@deepseek-ai/cosmokit/lib/index.js
|
|
2
|
+
function isNullable(value) {
|
|
3
|
+
return value === null || value === void 0;
|
|
51
4
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
let active = false;
|
|
55
|
-
for (const event of events ?? []) {
|
|
56
|
-
if (event !== undefined && event.type === "plan/mode") {
|
|
57
|
-
active = event.data !== undefined && event.data !== null && event.data.active === true;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return active;
|
|
5
|
+
function isPlainObject(data) {
|
|
6
|
+
return data && typeof data === "object" && !Array.isArray(data);
|
|
61
7
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
8
|
+
function filterKeys(object, filter) {
|
|
9
|
+
return Object.fromEntries(Object.entries(object).filter(([key, value]) => filter(key, value)));
|
|
10
|
+
}
|
|
11
|
+
function mapValues(object, transform) {
|
|
12
|
+
return Object.fromEntries(Object.entries(object).map(([key, value]) => [key, transform(value, key)]));
|
|
13
|
+
}
|
|
14
|
+
function pick(source, keys, forced) {
|
|
15
|
+
if (!keys) return { ...source };
|
|
16
|
+
const result = {};
|
|
17
|
+
for (const key of keys) if (forced || source[key] !== void 0) result[key] = source[key];
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
function is(type, value) {
|
|
21
|
+
if (arguments.length === 1) return (value2) => is(type, value2);
|
|
22
|
+
return type in globalThis && value instanceof globalThis[type] || Object.prototype.toString.call(value).slice(8, -1) === type;
|
|
23
|
+
}
|
|
24
|
+
function isArrayBufferLike(value) {
|
|
25
|
+
return is("ArrayBuffer", value) || is("SharedArrayBuffer", value);
|
|
26
|
+
}
|
|
27
|
+
function isArrayBufferSource(value) {
|
|
28
|
+
return isArrayBufferLike(value) || ArrayBuffer.isView(value);
|
|
29
|
+
}
|
|
30
|
+
var Binary;
|
|
31
|
+
(function(Binary2) {
|
|
32
|
+
Binary2.is = isArrayBufferLike;
|
|
33
|
+
Binary2.isSource = isArrayBufferSource;
|
|
34
|
+
function fromSource(source) {
|
|
35
|
+
if (ArrayBuffer.isView(source)) return source.buffer.slice(source.byteOffset, source.byteOffset + source.byteLength);
|
|
36
|
+
else return source;
|
|
37
|
+
}
|
|
38
|
+
Binary2.fromSource = fromSource;
|
|
39
|
+
function toBase64(source) {
|
|
40
|
+
source = fromSource(source);
|
|
41
|
+
if (typeof Buffer !== "undefined") return Buffer.from(source).toString("base64");
|
|
42
|
+
let binary = "";
|
|
43
|
+
const bytes = new Uint8Array(source);
|
|
44
|
+
for (let i = 0; i < bytes.byteLength; i++) binary += String.fromCharCode(bytes[i]);
|
|
45
|
+
return btoa(binary);
|
|
46
|
+
}
|
|
47
|
+
Binary2.toBase64 = toBase64;
|
|
48
|
+
function fromBase64(source) {
|
|
49
|
+
if (typeof Buffer !== "undefined") return fromSource(Buffer.from(source, "base64"));
|
|
50
|
+
return Uint8Array.from(atob(source), (c) => c.charCodeAt(0));
|
|
51
|
+
}
|
|
52
|
+
Binary2.fromBase64 = fromBase64;
|
|
53
|
+
function toHex(source) {
|
|
54
|
+
source = fromSource(source);
|
|
55
|
+
if (typeof Buffer !== "undefined") return Buffer.from(source).toString("hex");
|
|
56
|
+
return Array.from(new Uint8Array(source), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
57
|
+
}
|
|
58
|
+
Binary2.toHex = toHex;
|
|
59
|
+
function fromHex(source) {
|
|
60
|
+
if (typeof Buffer !== "undefined") return fromSource(Buffer.from(source, "hex"));
|
|
61
|
+
const hex = source.length % 2 === 0 ? source : source.slice(0, source.length - 1);
|
|
62
|
+
const buffer = [];
|
|
63
|
+
for (let i = 0; i < hex.length; i += 2) buffer.push(parseInt(`${hex[i]}${hex[i + 1]}`, 16));
|
|
64
|
+
return Uint8Array.from(buffer).buffer;
|
|
65
|
+
}
|
|
66
|
+
Binary2.fromHex = fromHex;
|
|
67
|
+
})(Binary || (Binary = {}));
|
|
68
|
+
var base64ToArrayBuffer = Binary.fromBase64;
|
|
69
|
+
var arrayBufferToBase64 = Binary.toBase64;
|
|
70
|
+
var hexToArrayBuffer = Binary.fromHex;
|
|
71
|
+
var arrayBufferToHex = Binary.toHex;
|
|
72
|
+
function clone(source, refs = /* @__PURE__ */ new Map()) {
|
|
73
|
+
if (!source || typeof source !== "object") return source;
|
|
74
|
+
if (is("Date", source)) return new Date(source.valueOf());
|
|
75
|
+
if (is("RegExp", source)) return new RegExp(source.source, source.flags);
|
|
76
|
+
if (isArrayBufferLike(source)) return source.slice(0);
|
|
77
|
+
if (ArrayBuffer.isView(source)) return source.buffer.slice(source.byteOffset, source.byteOffset + source.byteLength);
|
|
78
|
+
const cached = refs.get(source);
|
|
79
|
+
if (cached) return cached;
|
|
80
|
+
if (Array.isArray(source)) {
|
|
81
|
+
const result2 = [];
|
|
82
|
+
refs.set(source, result2);
|
|
83
|
+
source.forEach((value, index) => {
|
|
84
|
+
result2[index] = Reflect.apply(clone, null, [value, refs]);
|
|
85
|
+
});
|
|
86
|
+
return result2;
|
|
87
|
+
}
|
|
88
|
+
const result = Object.create(Object.getPrototypeOf(source));
|
|
89
|
+
refs.set(source, result);
|
|
90
|
+
for (const key of Reflect.ownKeys(source)) {
|
|
91
|
+
const descriptor = { ...Reflect.getOwnPropertyDescriptor(source, key) };
|
|
92
|
+
if ("value" in descriptor) descriptor.value = Reflect.apply(clone, null, [descriptor.value, refs]);
|
|
93
|
+
Reflect.defineProperty(result, key, descriptor);
|
|
94
|
+
}
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
function deepEqual(a, b, strict) {
|
|
98
|
+
if (a === b) return true;
|
|
99
|
+
if (!strict && isNullable(a) && isNullable(b)) return true;
|
|
100
|
+
if (typeof a !== typeof b) return false;
|
|
101
|
+
if (typeof a !== "object") return false;
|
|
102
|
+
if (!a || !b) return false;
|
|
103
|
+
function check(test, then) {
|
|
104
|
+
return test(a) ? test(b) ? then(a, b) : false : test(b) ? false : void 0;
|
|
105
|
+
}
|
|
106
|
+
return check(Array.isArray, (a2, b2) => a2.length === b2.length && a2.every((item, index) => deepEqual(item, b2[index]))) ?? check(is("Date"), (a2, b2) => a2.valueOf() === b2.valueOf()) ?? check(is("RegExp"), (a2, b2) => a2.source === b2.source && a2.flags === b2.flags) ?? check(isArrayBufferLike, (a2, b2) => {
|
|
107
|
+
if (a2.byteLength !== b2.byteLength) return false;
|
|
108
|
+
const viewA = new Uint8Array(a2);
|
|
109
|
+
const viewB = new Uint8Array(b2);
|
|
110
|
+
for (let i = 0; i < viewA.length; i++) if (viewA[i] !== viewB[i]) return false;
|
|
111
|
+
return true;
|
|
112
|
+
}) ?? Object.keys({
|
|
113
|
+
...a,
|
|
114
|
+
...b
|
|
115
|
+
}).every((key) => deepEqual(a[key], b[key], strict));
|
|
116
|
+
}
|
|
117
|
+
var Time;
|
|
118
|
+
(function(Time2) {
|
|
119
|
+
Time2.millisecond = 1;
|
|
120
|
+
Time2.second = 1e3;
|
|
121
|
+
Time2.minute = Time2.second * 60;
|
|
122
|
+
Time2.hour = Time2.minute * 60;
|
|
123
|
+
Time2.day = Time2.hour * 24;
|
|
124
|
+
Time2.week = Time2.day * 7;
|
|
125
|
+
let timezoneOffset = (/* @__PURE__ */ new Date()).getTimezoneOffset();
|
|
126
|
+
function setTimezoneOffset(offset) {
|
|
127
|
+
timezoneOffset = offset;
|
|
128
|
+
}
|
|
129
|
+
Time2.setTimezoneOffset = setTimezoneOffset;
|
|
130
|
+
function getTimezoneOffset() {
|
|
131
|
+
return timezoneOffset;
|
|
132
|
+
}
|
|
133
|
+
Time2.getTimezoneOffset = getTimezoneOffset;
|
|
134
|
+
function getDateNumber(date2 = /* @__PURE__ */ new Date(), offset) {
|
|
135
|
+
if (typeof date2 === "number") date2 = new Date(date2);
|
|
136
|
+
if (offset === void 0) offset = timezoneOffset;
|
|
137
|
+
return Math.floor((date2.valueOf() / Time2.minute - offset) / 1440);
|
|
138
|
+
}
|
|
139
|
+
Time2.getDateNumber = getDateNumber;
|
|
140
|
+
function fromDateNumber(value, offset) {
|
|
141
|
+
const date2 = new Date(value * Time2.day);
|
|
142
|
+
if (offset === void 0) offset = timezoneOffset;
|
|
143
|
+
return new Date(+date2 + offset * Time2.minute);
|
|
144
|
+
}
|
|
145
|
+
Time2.fromDateNumber = fromDateNumber;
|
|
146
|
+
const numeric = /\d+(?:\.\d+)?/.source;
|
|
147
|
+
const timeRegExp = new RegExp(`^${[
|
|
148
|
+
"w(?:eek(?:s)?)?",
|
|
149
|
+
"d(?:ay(?:s)?)?",
|
|
150
|
+
"h(?:our(?:s)?)?",
|
|
151
|
+
"m(?:in(?:ute)?(?:s)?)?",
|
|
152
|
+
"s(?:ec(?:ond)?(?:s)?)?"
|
|
153
|
+
].map((unit) => `(${numeric}${unit})?`).join("")}$`);
|
|
154
|
+
function parseTime(source) {
|
|
155
|
+
const capture = timeRegExp.exec(source);
|
|
156
|
+
if (!capture) return 0;
|
|
157
|
+
return (parseFloat(capture[1]) * Time2.week || 0) + (parseFloat(capture[2]) * Time2.day || 0) + (parseFloat(capture[3]) * Time2.hour || 0) + (parseFloat(capture[4]) * Time2.minute || 0) + (parseFloat(capture[5]) * Time2.second || 0);
|
|
158
|
+
}
|
|
159
|
+
Time2.parseTime = parseTime;
|
|
160
|
+
function parseDate(date2) {
|
|
161
|
+
const parsed = parseTime(date2);
|
|
162
|
+
if (parsed) date2 = Date.now() + parsed;
|
|
163
|
+
else if (/^\d{1,2}(:\d{1,2}){1,2}$/.test(date2)) date2 = `${(/* @__PURE__ */ new Date()).toLocaleDateString()}-${date2}`;
|
|
164
|
+
else if (/^\d{1,2}-\d{1,2}-\d{1,2}(:\d{1,2}){1,2}$/.test(date2)) date2 = `${(/* @__PURE__ */ new Date()).getFullYear()}-${date2}`;
|
|
165
|
+
return date2 ? new Date(date2) : /* @__PURE__ */ new Date();
|
|
166
|
+
}
|
|
167
|
+
Time2.parseDate = parseDate;
|
|
168
|
+
function format(ms) {
|
|
169
|
+
const abs = Math.abs(ms);
|
|
170
|
+
if (abs >= Time2.day - Time2.hour / 2) return Math.round(ms / Time2.day) + "d";
|
|
171
|
+
else if (abs >= Time2.hour - Time2.minute / 2) return Math.round(ms / Time2.hour) + "h";
|
|
172
|
+
else if (abs >= Time2.minute - Time2.second / 2) return Math.round(ms / Time2.minute) + "m";
|
|
173
|
+
else if (abs >= Time2.second) return Math.round(ms / Time2.second) + "s";
|
|
174
|
+
return ms + "ms";
|
|
175
|
+
}
|
|
176
|
+
Time2.format = format;
|
|
177
|
+
function toDigits(source, length = 2) {
|
|
178
|
+
return source.toString().padStart(length, "0");
|
|
179
|
+
}
|
|
180
|
+
Time2.toDigits = toDigits;
|
|
181
|
+
function template(template2, time = /* @__PURE__ */ new Date()) {
|
|
182
|
+
return template2.replace("yyyy", time.getFullYear().toString()).replace("yy", time.getFullYear().toString().slice(2)).replace("MM", toDigits(time.getMonth() + 1)).replace("dd", toDigits(time.getDate())).replace("hh", toDigits(time.getHours())).replace("mm", toDigits(time.getMinutes())).replace("ss", toDigits(time.getSeconds())).replace("SSS", toDigits(time.getMilliseconds(), 3));
|
|
183
|
+
}
|
|
184
|
+
Time2.template = template;
|
|
185
|
+
})(Time || (Time = {}));
|
|
186
|
+
|
|
187
|
+
// ../../node_modules/@deepseek-ai/schemastery/lib/index.mjs
|
|
188
|
+
var kSchema = /* @__PURE__ */ Symbol.for("schemastery");
|
|
189
|
+
var kValidationError = /* @__PURE__ */ Symbol.for("ValidationError");
|
|
190
|
+
globalThis.__schemastery_index__ ??= 0;
|
|
191
|
+
globalThis.__schemastery_refs__ = void 0;
|
|
192
|
+
var ValidationError = class extends TypeError {
|
|
193
|
+
options;
|
|
194
|
+
name = "ValidationError";
|
|
195
|
+
constructor(message, options) {
|
|
196
|
+
let prefix = "$";
|
|
197
|
+
for (const segment of options.path || []) if (typeof segment === "string") prefix += "." + segment;
|
|
198
|
+
else if (typeof segment === "number") prefix += "[" + segment + "]";
|
|
199
|
+
else if (typeof segment === "symbol") prefix += `[Symbol(${segment.toString()})]`;
|
|
200
|
+
if (prefix.startsWith(".")) prefix = prefix.slice(1);
|
|
201
|
+
super((prefix === "$" ? "" : `${prefix} `) + message);
|
|
202
|
+
this.options = options;
|
|
203
|
+
}
|
|
204
|
+
static is(error) {
|
|
205
|
+
return !!error?.[kValidationError];
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
Object.defineProperty(ValidationError.prototype, kValidationError, { value: true });
|
|
209
|
+
var Schema = function(options) {
|
|
210
|
+
const schema = function(data, options2 = {}) {
|
|
211
|
+
return Schema.resolve(data, schema, options2)[0];
|
|
212
|
+
};
|
|
213
|
+
if (options.refs) {
|
|
214
|
+
const refs = mapValues(options.refs, (options2) => new Schema(options2));
|
|
215
|
+
const getRef = (uid) => refs[uid];
|
|
216
|
+
for (const key in refs) {
|
|
217
|
+
const options2 = refs[key];
|
|
218
|
+
options2.sKey = getRef(options2.sKey);
|
|
219
|
+
options2.inner = getRef(options2.inner);
|
|
220
|
+
options2.list = options2.list && options2.list.map(getRef);
|
|
221
|
+
options2.dict = options2.dict && mapValues(options2.dict, getRef);
|
|
71
222
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
223
|
+
return refs[options.uid];
|
|
224
|
+
}
|
|
225
|
+
Object.assign(schema, options);
|
|
226
|
+
if (typeof schema.callback === "string") try {
|
|
227
|
+
schema.callback = new Function("return " + schema.callback)();
|
|
228
|
+
} catch {
|
|
229
|
+
}
|
|
230
|
+
Object.defineProperty(schema, "uid", { value: globalThis.__schemastery_index__++ });
|
|
231
|
+
Object.setPrototypeOf(schema, Schema.prototype);
|
|
232
|
+
schema.meta ||= {};
|
|
233
|
+
schema.toString = schema.toString.bind(schema);
|
|
234
|
+
return schema;
|
|
235
|
+
};
|
|
236
|
+
Schema.prototype = Object.create(Function.prototype);
|
|
237
|
+
Schema.prototype[kSchema] = true;
|
|
238
|
+
Object.defineProperty(Schema.prototype, "~standard", { get() {
|
|
239
|
+
return {
|
|
240
|
+
version: 1,
|
|
241
|
+
vendor: "schemastery",
|
|
242
|
+
validate: (value) => {
|
|
243
|
+
try {
|
|
244
|
+
return { value: Schema.resolve(value, this, {})[0] };
|
|
245
|
+
} catch (error) {
|
|
246
|
+
if (ValidationError.is(error)) return { issues: [{
|
|
247
|
+
message: error.message,
|
|
248
|
+
path: error.options.path
|
|
249
|
+
}] };
|
|
250
|
+
throw error;
|
|
251
|
+
}
|
|
75
252
|
}
|
|
76
|
-
|
|
77
|
-
|
|
253
|
+
};
|
|
254
|
+
} });
|
|
255
|
+
Schema.ValidationError = ValidationError;
|
|
256
|
+
Schema.prototype.toJSON = function toJSON() {
|
|
257
|
+
if (globalThis.__schemastery_refs__) {
|
|
258
|
+
globalThis.__schemastery_refs__[this.uid] ??= JSON.parse(JSON.stringify({ ...this }));
|
|
259
|
+
return this.uid;
|
|
260
|
+
}
|
|
261
|
+
globalThis.__schemastery_refs__ = { [this.uid]: { ...this } };
|
|
262
|
+
globalThis.__schemastery_refs__[this.uid] = JSON.parse(JSON.stringify({ ...this }));
|
|
263
|
+
const result = {
|
|
264
|
+
uid: this.uid,
|
|
265
|
+
refs: globalThis.__schemastery_refs__
|
|
266
|
+
};
|
|
267
|
+
globalThis.__schemastery_refs__ = void 0;
|
|
268
|
+
return result;
|
|
269
|
+
};
|
|
270
|
+
Schema.prototype.set = function set(key, value) {
|
|
271
|
+
this.dict[key] = value;
|
|
272
|
+
return this;
|
|
273
|
+
};
|
|
274
|
+
Schema.prototype.push = function push(value) {
|
|
275
|
+
this.list.push(value);
|
|
276
|
+
return this;
|
|
277
|
+
};
|
|
278
|
+
function mergeDesc(original, messages) {
|
|
279
|
+
const result = typeof original === "string" ? { "": original } : { ...original };
|
|
280
|
+
for (const locale in messages) {
|
|
281
|
+
const value = messages[locale];
|
|
282
|
+
if (value?.$description || value?.$desc) result[locale] = value.$description || value.$desc;
|
|
283
|
+
else if (typeof value === "string") result[locale] = value;
|
|
284
|
+
}
|
|
285
|
+
return result;
|
|
286
|
+
}
|
|
287
|
+
function getInner(value) {
|
|
288
|
+
return value?.$value ?? value?.$inner;
|
|
289
|
+
}
|
|
290
|
+
function extractKeys(data) {
|
|
291
|
+
return filterKeys(data ?? {}, (key) => !key.startsWith("$"));
|
|
292
|
+
}
|
|
293
|
+
Schema.prototype.i18n = function i18n(messages) {
|
|
294
|
+
const schema = Schema(this);
|
|
295
|
+
const desc = mergeDesc(schema.meta.description, messages);
|
|
296
|
+
if (Object.keys(desc).length) schema.meta.description = desc;
|
|
297
|
+
if (schema.dict) schema.dict = mapValues(schema.dict, (inner, key) => {
|
|
298
|
+
return inner.i18n(mapValues(messages, (data) => getInner(data)?.[key] ?? data?.[key]));
|
|
299
|
+
});
|
|
300
|
+
if (schema.list) schema.list = schema.list.map((inner, index) => {
|
|
301
|
+
return inner.i18n(mapValues(messages, (data = {}) => {
|
|
302
|
+
if (Array.isArray(getInner(data))) return getInner(data)[index];
|
|
303
|
+
if (Array.isArray(data)) return data[index];
|
|
304
|
+
return extractKeys(data);
|
|
305
|
+
}));
|
|
306
|
+
});
|
|
307
|
+
if (schema.inner) schema.inner = schema.inner.i18n(mapValues(messages, (data) => {
|
|
308
|
+
if (getInner(data)) return getInner(data);
|
|
309
|
+
return extractKeys(data);
|
|
310
|
+
}));
|
|
311
|
+
if (schema.sKey) schema.sKey = schema.sKey.i18n(mapValues(messages, (data) => data?.$key));
|
|
312
|
+
return schema;
|
|
313
|
+
};
|
|
314
|
+
Schema.prototype.extra = function extra(key, value) {
|
|
315
|
+
const schema = Schema(this);
|
|
316
|
+
schema.meta = {
|
|
317
|
+
...schema.meta,
|
|
318
|
+
[key]: value
|
|
319
|
+
};
|
|
320
|
+
return schema;
|
|
321
|
+
};
|
|
322
|
+
for (const key of [
|
|
323
|
+
"required",
|
|
324
|
+
"disabled",
|
|
325
|
+
"collapse",
|
|
326
|
+
"hidden",
|
|
327
|
+
"loose"
|
|
328
|
+
]) Object.assign(Schema.prototype, { [key](value = true) {
|
|
329
|
+
const schema = Schema(this);
|
|
330
|
+
schema.meta = {
|
|
331
|
+
...schema.meta,
|
|
332
|
+
[key]: value
|
|
333
|
+
};
|
|
334
|
+
return schema;
|
|
335
|
+
} });
|
|
336
|
+
Schema.prototype.deprecated = function deprecated() {
|
|
337
|
+
const schema = Schema(this);
|
|
338
|
+
schema.meta.badges ||= [];
|
|
339
|
+
schema.meta.badges.push({
|
|
340
|
+
text: "deprecated",
|
|
341
|
+
type: "danger"
|
|
342
|
+
});
|
|
343
|
+
return schema;
|
|
344
|
+
};
|
|
345
|
+
Schema.prototype.experimental = function experimental() {
|
|
346
|
+
const schema = Schema(this);
|
|
347
|
+
schema.meta.badges ||= [];
|
|
348
|
+
schema.meta.badges.push({
|
|
349
|
+
text: "experimental",
|
|
350
|
+
type: "warning"
|
|
351
|
+
});
|
|
352
|
+
return schema;
|
|
353
|
+
};
|
|
354
|
+
Schema.prototype.pattern = function pattern(regexp) {
|
|
355
|
+
const schema = Schema(this);
|
|
356
|
+
const pattern2 = pick(regexp, ["source", "flags"]);
|
|
357
|
+
schema.meta = {
|
|
358
|
+
...schema.meta,
|
|
359
|
+
pattern: pattern2
|
|
360
|
+
};
|
|
361
|
+
return schema;
|
|
362
|
+
};
|
|
363
|
+
Schema.prototype.simplify = function simplify(value) {
|
|
364
|
+
if (deepEqual(value, this.meta.default, this.type === "dict")) return null;
|
|
365
|
+
if (isNullable(value)) return value;
|
|
366
|
+
if (this.type === "object" || this.type === "dict") {
|
|
367
|
+
const result = {};
|
|
368
|
+
for (const key in value) {
|
|
369
|
+
const item = (this.type === "object" ? this.dict[key] : this.inner)?.simplify(value[key]);
|
|
370
|
+
if (this.type === "dict" || !isNullable(item)) result[key] = item;
|
|
371
|
+
}
|
|
372
|
+
if (deepEqual(result, this.meta.default, this.type === "dict")) return null;
|
|
373
|
+
return result;
|
|
374
|
+
} else if (this.type === "array" || this.type === "tuple") {
|
|
375
|
+
const result = [];
|
|
376
|
+
value.forEach((value2, index) => {
|
|
377
|
+
const schema = this.type === "array" ? this.inner : this.list[index];
|
|
378
|
+
const item = schema ? schema.simplify(value2) : value2;
|
|
379
|
+
result.push(item);
|
|
380
|
+
});
|
|
381
|
+
return result;
|
|
382
|
+
} else if (this.type === "intersect") {
|
|
383
|
+
const result = {};
|
|
384
|
+
for (const item of this.list) Object.assign(result, item.simplify(value));
|
|
385
|
+
return result;
|
|
386
|
+
} else if (this.type === "union") for (const schema of this.list) try {
|
|
387
|
+
Schema.resolve(value, schema, {});
|
|
388
|
+
return schema.simplify(value);
|
|
389
|
+
} catch {
|
|
390
|
+
}
|
|
391
|
+
return value;
|
|
392
|
+
};
|
|
393
|
+
Schema.prototype.toString = function toString(inline) {
|
|
394
|
+
return formatters[this.type]?.(this, inline) ?? `Schema<${this.type}>`;
|
|
395
|
+
};
|
|
396
|
+
Schema.prototype.role = function role(role, extra2) {
|
|
397
|
+
const schema = Schema(this);
|
|
398
|
+
schema.meta = {
|
|
399
|
+
...schema.meta,
|
|
400
|
+
role,
|
|
401
|
+
extra: extra2
|
|
402
|
+
};
|
|
403
|
+
return schema;
|
|
404
|
+
};
|
|
405
|
+
for (const key of [
|
|
406
|
+
"default",
|
|
407
|
+
"link",
|
|
408
|
+
"comment",
|
|
409
|
+
"description",
|
|
410
|
+
"max",
|
|
411
|
+
"min",
|
|
412
|
+
"step"
|
|
413
|
+
]) Object.assign(Schema.prototype, { [key](value) {
|
|
414
|
+
const schema = Schema(this);
|
|
415
|
+
schema.meta = {
|
|
416
|
+
...schema.meta,
|
|
417
|
+
[key]: value
|
|
418
|
+
};
|
|
419
|
+
return schema;
|
|
420
|
+
} });
|
|
421
|
+
var resolvers = {};
|
|
422
|
+
Schema.extend = function extend(type, resolve2) {
|
|
423
|
+
resolvers[type] = resolve2;
|
|
424
|
+
};
|
|
425
|
+
Schema.resolve = function resolve(data, schema, options = {}, strict = false) {
|
|
426
|
+
if (!schema) return [data];
|
|
427
|
+
if (options.ignore?.(data, schema)) return [data];
|
|
428
|
+
if (isNullable(data) && schema.type !== "lazy") {
|
|
429
|
+
if (schema.meta.required) throw new ValidationError(`missing required value`, options);
|
|
430
|
+
let current = schema;
|
|
431
|
+
let fallback = schema.meta.default;
|
|
432
|
+
while (current?.type === "intersect" && isNullable(fallback)) {
|
|
433
|
+
current = current.list[0];
|
|
434
|
+
fallback = current?.meta.default;
|
|
78
435
|
}
|
|
79
|
-
if (
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
436
|
+
if (isNullable(fallback)) return [data];
|
|
437
|
+
data = clone(fallback);
|
|
438
|
+
}
|
|
439
|
+
const callback = resolvers[schema.type];
|
|
440
|
+
if (!callback) throw new ValidationError(`unsupported type "${schema.type}"`, options);
|
|
441
|
+
try {
|
|
442
|
+
return callback(data, schema, options, strict);
|
|
443
|
+
} catch (error) {
|
|
444
|
+
if (!schema.meta.loose) throw error;
|
|
445
|
+
return [schema.meta.default];
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
Schema.from = function from(source) {
|
|
449
|
+
if (isNullable(source)) return Schema.any();
|
|
450
|
+
else if ([
|
|
451
|
+
"string",
|
|
452
|
+
"number",
|
|
453
|
+
"boolean"
|
|
454
|
+
].includes(typeof source)) return Schema.const(source).required();
|
|
455
|
+
else if (source[kSchema]) return source;
|
|
456
|
+
else if (typeof source === "function") switch (source) {
|
|
457
|
+
case String:
|
|
458
|
+
return Schema.string().required();
|
|
459
|
+
case Number:
|
|
460
|
+
return Schema.number().required();
|
|
461
|
+
case Boolean:
|
|
462
|
+
return Schema.boolean().required();
|
|
463
|
+
case Function:
|
|
464
|
+
return Schema.function().required();
|
|
465
|
+
default:
|
|
466
|
+
return Schema.is(source).required();
|
|
467
|
+
}
|
|
468
|
+
else throw new TypeError(`cannot infer schema from ${source}`);
|
|
469
|
+
};
|
|
470
|
+
Schema.lazy = function lazy(builder) {
|
|
471
|
+
const toJSON2 = () => {
|
|
472
|
+
if (!schema.inner[kSchema]) {
|
|
473
|
+
schema.inner = schema.builder();
|
|
474
|
+
schema.inner.meta = {
|
|
475
|
+
...schema.meta,
|
|
476
|
+
...schema.inner.meta
|
|
477
|
+
};
|
|
84
478
|
}
|
|
85
|
-
|
|
479
|
+
return schema.inner.toJSON();
|
|
480
|
+
};
|
|
481
|
+
const schema = new Schema({
|
|
482
|
+
type: "lazy",
|
|
483
|
+
builder,
|
|
484
|
+
inner: { toJSON: toJSON2 }
|
|
485
|
+
});
|
|
486
|
+
return schema;
|
|
487
|
+
};
|
|
488
|
+
Schema.natural = function natural() {
|
|
489
|
+
return Schema.number().step(1).min(0);
|
|
490
|
+
};
|
|
491
|
+
Schema.percent = function percent() {
|
|
492
|
+
return Schema.number().step(0.01).min(0).max(1).role("slider");
|
|
493
|
+
};
|
|
494
|
+
Schema.date = function date() {
|
|
495
|
+
return Schema.union([Schema.is(Date), Schema.transform(Schema.string().role("datetime"), (value, options) => {
|
|
496
|
+
const date2 = new Date(value);
|
|
497
|
+
if (isNaN(+date2)) throw new ValidationError(`invalid date "${value}"`, options);
|
|
498
|
+
return date2;
|
|
499
|
+
}, true)]);
|
|
500
|
+
};
|
|
501
|
+
Schema.regExp = function regExp(flag = "") {
|
|
502
|
+
return Schema.union([Schema.is(RegExp), Schema.transform(Schema.string().role("regexp", { flag }), (value, options) => {
|
|
86
503
|
try {
|
|
87
|
-
|
|
504
|
+
return new RegExp(value, flag);
|
|
505
|
+
} catch (e) {
|
|
506
|
+
throw new ValidationError(e.message, options);
|
|
88
507
|
}
|
|
89
|
-
|
|
90
|
-
|
|
508
|
+
}, true)]);
|
|
509
|
+
};
|
|
510
|
+
Schema.arrayBuffer = function arrayBuffer(encoding) {
|
|
511
|
+
return Schema.union([
|
|
512
|
+
Schema.is(ArrayBuffer),
|
|
513
|
+
Schema.is(SharedArrayBuffer),
|
|
514
|
+
Schema.transform(Schema.any(), (value, options) => {
|
|
515
|
+
if (Binary.isSource(value)) return Binary.fromSource(value);
|
|
516
|
+
throw new ValidationError(`expected ArrayBufferSource but got ${value}`, options);
|
|
517
|
+
}, true),
|
|
518
|
+
...encoding ? [Schema.transform(Schema.string(), (value, options) => {
|
|
519
|
+
try {
|
|
520
|
+
return encoding === "base64" ? Binary.fromBase64(value) : Binary.fromHex(value);
|
|
521
|
+
} catch (e) {
|
|
522
|
+
throw new ValidationError(e.message, options);
|
|
523
|
+
}
|
|
524
|
+
}, true)] : []
|
|
525
|
+
]);
|
|
526
|
+
};
|
|
527
|
+
Schema.extend("lazy", (data, schema, options, strict) => {
|
|
528
|
+
if (!schema.inner[kSchema]) {
|
|
529
|
+
schema.inner = schema.builder();
|
|
530
|
+
schema.inner.meta = {
|
|
531
|
+
...schema.meta,
|
|
532
|
+
...schema.inner.meta
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
return Schema.resolve(data, schema.inner, options, strict);
|
|
536
|
+
});
|
|
537
|
+
Schema.extend("any", (data) => {
|
|
538
|
+
return [data];
|
|
539
|
+
});
|
|
540
|
+
Schema.extend("never", (data, _, options) => {
|
|
541
|
+
throw new ValidationError(`expected nullable but got ${data}`, options);
|
|
542
|
+
});
|
|
543
|
+
Schema.extend("const", (data, { value }, options) => {
|
|
544
|
+
if (deepEqual(data, value)) return [value];
|
|
545
|
+
throw new ValidationError(`expected ${value} but got ${data}`, options);
|
|
546
|
+
});
|
|
547
|
+
function checkWithinRange(data, meta, description, options, skipMin = false) {
|
|
548
|
+
const { max = Infinity, min = -Infinity } = meta;
|
|
549
|
+
if (data > max) throw new ValidationError(`expected ${description} <= ${max} but got ${data}`, options);
|
|
550
|
+
if (data < min && !skipMin) throw new ValidationError(`expected ${description} >= ${min} but got ${data}`, options);
|
|
551
|
+
}
|
|
552
|
+
Schema.extend("string", (data, { meta }, options) => {
|
|
553
|
+
if (typeof data !== "string") throw new ValidationError(`expected string but got ${data}`, options);
|
|
554
|
+
if (meta.pattern) {
|
|
555
|
+
const regexp = new RegExp(meta.pattern.source, meta.pattern.flags);
|
|
556
|
+
if (!regexp.test(data)) throw new ValidationError(`expect string to match regexp ${regexp}`, options);
|
|
557
|
+
}
|
|
558
|
+
checkWithinRange(data.length, meta, "string length", options);
|
|
559
|
+
return [data];
|
|
560
|
+
});
|
|
561
|
+
function decimalShift(data, digits) {
|
|
562
|
+
const str = data.toString();
|
|
563
|
+
if (str.includes("e")) return data * Math.pow(10, digits);
|
|
564
|
+
const index = str.indexOf(".");
|
|
565
|
+
if (index === -1) return data * Math.pow(10, digits);
|
|
566
|
+
const frac = str.slice(index + 1);
|
|
567
|
+
const integer = str.slice(0, index);
|
|
568
|
+
if (frac.length <= digits) return +(integer + frac.padEnd(digits, "0"));
|
|
569
|
+
return +(integer + frac.slice(0, digits) + "." + frac.slice(digits));
|
|
570
|
+
}
|
|
571
|
+
function isMultipleOf(data, min, step) {
|
|
572
|
+
step = Math.abs(step);
|
|
573
|
+
if (!/^\d+\.\d+$/.test(step.toString())) return (data - min) % step === 0;
|
|
574
|
+
const index = step.toString().indexOf(".");
|
|
575
|
+
const digits = step.toString().slice(index + 1).length;
|
|
576
|
+
return Math.abs(decimalShift(data, digits) - decimalShift(min, digits)) % decimalShift(step, digits) === 0;
|
|
577
|
+
}
|
|
578
|
+
Schema.extend("number", (data, { meta }, options) => {
|
|
579
|
+
if (typeof data !== "number") throw new ValidationError(`expected number but got ${data}`, options);
|
|
580
|
+
checkWithinRange(data, meta, "number", options);
|
|
581
|
+
const { step } = meta;
|
|
582
|
+
if (step && !isMultipleOf(data, meta.min ?? 0, step)) throw new ValidationError(`expected number multiple of ${step} but got ${data}`, options);
|
|
583
|
+
return [data];
|
|
584
|
+
});
|
|
585
|
+
Schema.extend("boolean", (data, _, options) => {
|
|
586
|
+
if (typeof data === "boolean") return [data];
|
|
587
|
+
throw new ValidationError(`expected boolean but got ${data}`, options);
|
|
588
|
+
});
|
|
589
|
+
Schema.extend("bitset", (data, { bits, meta }, options) => {
|
|
590
|
+
let value = 0, keys = [];
|
|
591
|
+
if (typeof data === "number") {
|
|
592
|
+
value = data;
|
|
593
|
+
for (const key in bits) if (data & bits[key]) keys.push(key);
|
|
594
|
+
} else if (Array.isArray(data)) {
|
|
595
|
+
keys = data;
|
|
596
|
+
for (const key of keys) {
|
|
597
|
+
if (typeof key !== "string") throw new ValidationError(`expected string but got ${key}`, options);
|
|
598
|
+
if (key in bits) value |= bits[key];
|
|
91
599
|
}
|
|
92
|
-
|
|
93
|
-
|
|
600
|
+
} else throw new ValidationError(`expected number or array but got ${data}`, options);
|
|
601
|
+
if (value === meta.default) return [value];
|
|
602
|
+
return [value, keys];
|
|
603
|
+
});
|
|
604
|
+
Schema.extend("function", (data, _, options) => {
|
|
605
|
+
if (typeof data === "function") return [data];
|
|
606
|
+
throw new ValidationError(`expected function but got ${data}`, options);
|
|
607
|
+
});
|
|
608
|
+
Schema.extend("is", (data, { constructor }, options) => {
|
|
609
|
+
if (typeof constructor === "function") {
|
|
610
|
+
if (data instanceof constructor) return [data];
|
|
611
|
+
throw new ValidationError(`expected ${constructor.name} but got ${data}`, options);
|
|
612
|
+
} else {
|
|
613
|
+
if (isNullable(data)) throw new ValidationError(`expected ${constructor} but got ${data}`, options);
|
|
614
|
+
let prototype = Object.getPrototypeOf(data);
|
|
615
|
+
while (prototype) {
|
|
616
|
+
if (prototype.constructor?.name === constructor) return [data];
|
|
617
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
94
618
|
}
|
|
619
|
+
throw new ValidationError(`expected ${constructor} but got ${data}`, options);
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
function property(data, key, schema, options) {
|
|
623
|
+
try {
|
|
624
|
+
const [value, adapted] = Schema.resolve(data[key], schema, {
|
|
625
|
+
...options,
|
|
626
|
+
path: [...options.path || [], key]
|
|
627
|
+
});
|
|
628
|
+
if (adapted !== void 0) data[key] = adapted;
|
|
629
|
+
return value;
|
|
630
|
+
} catch (e) {
|
|
631
|
+
if (!options?.autofix) throw e;
|
|
632
|
+
delete data[key];
|
|
633
|
+
return schema.meta.default;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
Schema.extend("array", (data, { inner, meta }, options) => {
|
|
637
|
+
if (!Array.isArray(data)) throw new ValidationError(`expected array but got ${data}`, options);
|
|
638
|
+
checkWithinRange(data.length, meta, "array length", options, !isNullable(inner.meta.default));
|
|
639
|
+
return [data.map((_, index) => property(data, index, inner, options))];
|
|
640
|
+
});
|
|
641
|
+
Schema.extend("dict", (data, { inner, sKey }, options, strict) => {
|
|
642
|
+
if (!isPlainObject(data)) throw new ValidationError(`expected object but got ${data}`, options);
|
|
643
|
+
const result = {};
|
|
644
|
+
for (const key in data) {
|
|
645
|
+
let rKey;
|
|
95
646
|
try {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
647
|
+
rKey = Schema.resolve(key, sKey, options)[0];
|
|
648
|
+
} catch (error) {
|
|
649
|
+
if (strict) continue;
|
|
650
|
+
throw error;
|
|
651
|
+
}
|
|
652
|
+
result[rKey] = property(data, key, inner, options);
|
|
653
|
+
data[rKey] = data[key];
|
|
654
|
+
if (key !== rKey) delete data[key];
|
|
655
|
+
}
|
|
656
|
+
return [result];
|
|
657
|
+
});
|
|
658
|
+
Schema.extend("tuple", (data, { list }, options, strict) => {
|
|
659
|
+
if (!Array.isArray(data)) throw new ValidationError(`expected array but got ${data}`, options);
|
|
660
|
+
const result = list.map((inner, index) => property(data, index, inner, options));
|
|
661
|
+
if (strict) return [result];
|
|
662
|
+
result.push(...data.slice(list.length));
|
|
663
|
+
return [result];
|
|
664
|
+
});
|
|
665
|
+
function merge(result, data) {
|
|
666
|
+
for (const key in data) {
|
|
667
|
+
if (key in result) continue;
|
|
668
|
+
result[key] = data[key];
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
Schema.extend("object", (data, { dict }, options, strict) => {
|
|
672
|
+
if (!isPlainObject(data)) throw new ValidationError(`expected object but got ${data}`, options);
|
|
673
|
+
const result = {};
|
|
674
|
+
for (const key in dict) {
|
|
675
|
+
const value = property(data, key, dict[key], options);
|
|
676
|
+
if (!isNullable(value) || key in data) result[key] = value;
|
|
677
|
+
}
|
|
678
|
+
if (!strict) merge(result, data);
|
|
679
|
+
return [result];
|
|
680
|
+
});
|
|
681
|
+
Schema.extend("union", (data, { list, toString: toString2 }, options, strict) => {
|
|
682
|
+
const messages = [];
|
|
683
|
+
for (const inner of list) try {
|
|
684
|
+
return Schema.resolve(data, inner, options, strict);
|
|
685
|
+
} catch (error) {
|
|
686
|
+
messages.push(error);
|
|
687
|
+
}
|
|
688
|
+
throw new ValidationError(`expected ${toString2()} but got ${JSON.stringify(data)}`, options);
|
|
689
|
+
});
|
|
690
|
+
Schema.extend("intersect", (data, { list, toString: toString2 }, options, strict) => {
|
|
691
|
+
if (!list.length) return [data];
|
|
692
|
+
let result;
|
|
693
|
+
for (const inner of list) {
|
|
694
|
+
const value = Schema.resolve(data, inner, options, true)[0];
|
|
695
|
+
if (isNullable(value)) continue;
|
|
696
|
+
if (isNullable(result)) result = value;
|
|
697
|
+
else if (typeof result !== typeof value) throw new ValidationError(`expected ${toString2()} but got ${JSON.stringify(data)}`, options);
|
|
698
|
+
else if (typeof value === "object") merge(result ??= {}, value);
|
|
699
|
+
else if (result !== value) throw new ValidationError(`expected ${toString2()} but got ${JSON.stringify(data)}`, options);
|
|
700
|
+
}
|
|
701
|
+
if (!strict && isPlainObject(data)) merge(result, data);
|
|
702
|
+
return [result];
|
|
703
|
+
});
|
|
704
|
+
Schema.extend("transform", (data, { inner, callback, preserve }, options) => {
|
|
705
|
+
const [result, adapted = data] = Schema.resolve(data, inner, options, true);
|
|
706
|
+
if (preserve) return [callback(result)];
|
|
707
|
+
else return [callback(result), callback(adapted)];
|
|
708
|
+
});
|
|
709
|
+
var formatters = {};
|
|
710
|
+
function defineMethod(name2, keys, format) {
|
|
711
|
+
formatters[name2] = format;
|
|
712
|
+
Object.assign(Schema, { [name2](...args) {
|
|
713
|
+
const schema = new Schema({ type: name2 });
|
|
714
|
+
keys.forEach((key, index) => {
|
|
715
|
+
switch (key) {
|
|
716
|
+
case "sKey":
|
|
717
|
+
schema.sKey = args[index] ?? Schema.string();
|
|
718
|
+
break;
|
|
719
|
+
case "inner":
|
|
720
|
+
schema.inner = Schema.from(args[index]);
|
|
721
|
+
break;
|
|
722
|
+
case "list":
|
|
723
|
+
schema.list = args[index].map(Schema.from);
|
|
724
|
+
break;
|
|
725
|
+
case "dict":
|
|
726
|
+
schema.dict = mapValues(args[index], Schema.from);
|
|
727
|
+
break;
|
|
728
|
+
case "bits":
|
|
729
|
+
schema.bits = {};
|
|
730
|
+
for (const key2 in args[index]) {
|
|
731
|
+
if (typeof args[index][key2] !== "number") continue;
|
|
732
|
+
schema.bits[key2] = args[index][key2];
|
|
733
|
+
}
|
|
734
|
+
break;
|
|
735
|
+
case "callback": {
|
|
736
|
+
const callback = schema.callback = args[index];
|
|
737
|
+
callback["toJSON"] ||= () => callback.toString();
|
|
738
|
+
break;
|
|
739
|
+
}
|
|
740
|
+
case "constructor": {
|
|
741
|
+
const constructor = schema.constructor = args[index];
|
|
742
|
+
if (typeof constructor === "function") constructor["toJSON"] ||= () => constructor["name"];
|
|
743
|
+
break;
|
|
100
744
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
745
|
+
default:
|
|
746
|
+
schema[key] = args[index];
|
|
747
|
+
}
|
|
748
|
+
});
|
|
749
|
+
if (name2 === "object" || name2 === "dict") schema.meta.default = {};
|
|
750
|
+
else if (name2 === "array" || name2 === "tuple") schema.meta.default = [];
|
|
751
|
+
else if (name2 === "bitset") schema.meta.default = 0;
|
|
752
|
+
return schema;
|
|
753
|
+
} });
|
|
754
|
+
}
|
|
755
|
+
defineMethod("is", ["constructor"], ({ constructor }) => {
|
|
756
|
+
if (typeof constructor === "function") return constructor.name;
|
|
757
|
+
else return constructor;
|
|
758
|
+
});
|
|
759
|
+
defineMethod("any", [], () => "any");
|
|
760
|
+
defineMethod("never", [], () => "never");
|
|
761
|
+
defineMethod("const", ["value"], ({ value }) => typeof value === "string" ? JSON.stringify(value) : value);
|
|
762
|
+
defineMethod("string", [], () => "string");
|
|
763
|
+
defineMethod("number", [], () => "number");
|
|
764
|
+
defineMethod("boolean", [], () => "boolean");
|
|
765
|
+
defineMethod("bitset", ["bits"], () => "bitset");
|
|
766
|
+
defineMethod("function", [], () => "function");
|
|
767
|
+
defineMethod("array", ["inner"], ({ inner }) => `${inner.toString(true)}[]`);
|
|
768
|
+
defineMethod("dict", ["inner", "sKey"], ({ inner, sKey }) => `{ [key: ${sKey.toString()}]: ${inner.toString()} }`);
|
|
769
|
+
defineMethod("tuple", ["list"], ({ list }) => `[${list.map((inner) => inner.toString()).join(", ")}]`);
|
|
770
|
+
defineMethod("object", ["dict"], ({ dict }) => {
|
|
771
|
+
if (Object.keys(dict).length === 0) return "{}";
|
|
772
|
+
return `{ ${Object.entries(dict).map(([key, inner]) => {
|
|
773
|
+
return `${key}${inner.meta.required ? "" : "?"}: ${inner.toString()}`;
|
|
774
|
+
}).join(", ")} }`;
|
|
775
|
+
});
|
|
776
|
+
defineMethod("union", ["list"], ({ list }, inline) => {
|
|
777
|
+
const result = list.map(({ toString: format }) => format()).join(" | ");
|
|
778
|
+
return inline ? `(${result})` : result;
|
|
779
|
+
});
|
|
780
|
+
defineMethod("intersect", ["list"], ({ list }) => {
|
|
781
|
+
return `${list.map((inner) => inner.toString(true)).join(" & ")}`;
|
|
782
|
+
});
|
|
783
|
+
defineMethod("transform", [
|
|
784
|
+
"inner",
|
|
785
|
+
"callback",
|
|
786
|
+
"preserve"
|
|
787
|
+
], ({ inner }, isInner) => inner.toString(isInner));
|
|
788
|
+
|
|
789
|
+
// ../../node_modules/@deepseek-ai/dsh-timeout/lib/index.js
|
|
790
|
+
var MAX_TIMER_DELAY_MS = 2147483647;
|
|
791
|
+
|
|
792
|
+
// ../../node_modules/@deepseek-ai/dsh-llm/lib/index.js
|
|
793
|
+
function MessageId(id) {
|
|
794
|
+
return id;
|
|
795
|
+
}
|
|
796
|
+
function deepFreeze(value) {
|
|
797
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
798
|
+
const pending = [{
|
|
799
|
+
kind: "visit",
|
|
800
|
+
node: value
|
|
801
|
+
}];
|
|
802
|
+
while (pending.length > 0) {
|
|
803
|
+
const task = pending.pop();
|
|
804
|
+
if (task === void 0) continue;
|
|
805
|
+
if (task.kind === "property") {
|
|
806
|
+
pending.push({
|
|
807
|
+
kind: "visit",
|
|
808
|
+
node: task.source[task.key]
|
|
809
|
+
});
|
|
810
|
+
continue;
|
|
121
811
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
812
|
+
const node = task.node;
|
|
813
|
+
if (node === null || typeof node !== "object") continue;
|
|
814
|
+
if (node instanceof AbortSignal) continue;
|
|
815
|
+
if (seen.has(node)) continue;
|
|
816
|
+
seen.add(node);
|
|
817
|
+
Object.freeze(node);
|
|
818
|
+
const keys = Object.keys(node);
|
|
819
|
+
for (let index = keys.length - 1; index >= 0; index--) {
|
|
820
|
+
const key = keys[index];
|
|
821
|
+
if (key === void 0) continue;
|
|
822
|
+
pending.push({
|
|
823
|
+
kind: "property",
|
|
824
|
+
source: node,
|
|
825
|
+
key
|
|
826
|
+
});
|
|
127
827
|
}
|
|
828
|
+
}
|
|
829
|
+
return value;
|
|
128
830
|
}
|
|
129
|
-
function
|
|
130
|
-
|
|
131
|
-
|
|
831
|
+
function freezeMessage(message) {
|
|
832
|
+
return deepFreeze(structuredClone(message));
|
|
833
|
+
}
|
|
834
|
+
function createMessage(input) {
|
|
835
|
+
return freezeMessage({
|
|
836
|
+
...input,
|
|
837
|
+
id: MessageId(crypto.randomUUID())
|
|
838
|
+
});
|
|
839
|
+
}
|
|
840
|
+
function createUserMessage(input) {
|
|
841
|
+
return createMessage({
|
|
842
|
+
...input,
|
|
843
|
+
role: "user"
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
var EMPTY_RESPONSE_CODE = "EMPTY_RESPONSE";
|
|
847
|
+
var STRUCTURED_CONTEXT_OVERFLOW = new RegExp(String.raw`(?:^|[^a-z0-9])context[\s_-](?:length|window)[\s_-]` + String.raw`(?:exceed(?:ed|s)?|overflow(?:ed)?|limit[\s_-]exceeded)(?:$|[^a-z0-9])`, "i");
|
|
848
|
+
var TOO_LARGE_FOR_CONTEXT = new RegExp(String.raw`\b(?:request|prompt|input|messages?)\s+(?:is\s+|are\s+)?` + String.raw`too\s+(?:large|long)\s+for\s+(?:(?:this|the)\s+)?` + String.raw`(?:model(?:'s)?\s+)?context(?:\s+window)?\b`, "i");
|
|
849
|
+
var EXCEEDS_MODEL_CONTEXT = new RegExp(String.raw`\b(?:input|prompt|request|messages?)\b.{0,40}` + String.raw`\b(?:exceed(?:s|ed)?|overflows?|is\s+larger\s+than)\b.{0,40}` + String.raw`\b(?:the\s+)?(?:model(?:'s)?\s+)?context(?:\s+(?:length|window))?\b`, "i");
|
|
850
|
+
var DEFAULT_MAX_RETRIES = 5;
|
|
851
|
+
var DEFAULT_INITIAL_DELAY_MS = 500;
|
|
852
|
+
var DEFAULT_MAX_DELAY_MS = 1e4;
|
|
853
|
+
var DEFAULT_JITTER_RATIO = 0.1;
|
|
854
|
+
var DEFAULT_RETRYABLE_CODES = Object.freeze([
|
|
855
|
+
EMPTY_RESPONSE_CODE,
|
|
856
|
+
"RATE_LIMIT",
|
|
857
|
+
"SERVER",
|
|
858
|
+
"TIMEOUT",
|
|
859
|
+
"TRANSPORT"
|
|
860
|
+
]);
|
|
861
|
+
var backoffSchema = Schema.object({
|
|
862
|
+
initialDelayMs: Schema.number().max(MAX_TIMER_DELAY_MS).default(DEFAULT_INITIAL_DELAY_MS),
|
|
863
|
+
maxDelayMs: Schema.number().max(MAX_TIMER_DELAY_MS).default(DEFAULT_MAX_DELAY_MS),
|
|
864
|
+
jitterRatio: Schema.number().min(0).max(1).default(DEFAULT_JITTER_RATIO)
|
|
865
|
+
});
|
|
866
|
+
var normalPolicySchema = Schema.object({
|
|
867
|
+
mode: Schema.const("normal").required(),
|
|
868
|
+
maxRetries: Schema.number().step(1).min(0).max(Number.MAX_SAFE_INTEGER).default(DEFAULT_MAX_RETRIES),
|
|
869
|
+
retryableCodes: Schema.array(Schema.string()).default([...DEFAULT_RETRYABLE_CODES]),
|
|
870
|
+
backoff: backoffSchema
|
|
871
|
+
});
|
|
872
|
+
var alwaysPolicySchema = Schema.object({
|
|
873
|
+
mode: Schema.const("always").required(),
|
|
874
|
+
backoff: backoffSchema
|
|
875
|
+
});
|
|
876
|
+
var RetryPolicySchema = Schema.union([normalPolicySchema, alwaysPolicySchema]);
|
|
877
|
+
var { version } = { version: "0.0.0" };
|
|
878
|
+
|
|
879
|
+
// lib/mode.js
|
|
880
|
+
var name = "omd-mode-switch";
|
|
881
|
+
var inject = [];
|
|
882
|
+
var OMD_PRESET_IDS = [
|
|
883
|
+
"omd-executor",
|
|
884
|
+
"omd-ultraworker",
|
|
885
|
+
"omd-planner",
|
|
886
|
+
"omd-reviewer",
|
|
887
|
+
"omd-explorer",
|
|
888
|
+
"omd-librarian",
|
|
889
|
+
"omd-chat"
|
|
890
|
+
];
|
|
891
|
+
function normalizeTarget(rawInput) {
|
|
892
|
+
const trimmed = String(rawInput).trim().toLowerCase();
|
|
893
|
+
if (trimmed === "")
|
|
894
|
+
return void 0;
|
|
895
|
+
const candidate = trimmed.startsWith("omd-") ? trimmed : "omd-" + trimmed;
|
|
896
|
+
return OMD_PRESET_IDS.includes(candidate) ? candidate : void 0;
|
|
897
|
+
}
|
|
898
|
+
function planModeActive(events) {
|
|
899
|
+
let active = false;
|
|
900
|
+
for (const event of events ?? []) {
|
|
901
|
+
if (event !== void 0 && event.type === "plan/mode") {
|
|
902
|
+
active = event.data !== void 0 && event.data !== null && event.data.active === true;
|
|
132
903
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
904
|
+
}
|
|
905
|
+
return active;
|
|
906
|
+
}
|
|
907
|
+
async function executeSwitch(ctx, invocation) {
|
|
908
|
+
const agent = invocation.agent;
|
|
909
|
+
const target = normalizeTarget(invocation.rawInput);
|
|
910
|
+
if (target === void 0) {
|
|
911
|
+
return {
|
|
912
|
+
kind: "error",
|
|
913
|
+
text: "Usage: /mode <preset> \u2014 valid: " + OMD_PRESET_IDS.join(", ")
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
let presets;
|
|
917
|
+
try {
|
|
918
|
+
presets = ctx.get("agentPresets");
|
|
919
|
+
} catch {
|
|
920
|
+
presets = void 0;
|
|
921
|
+
}
|
|
922
|
+
if (presets === void 0 || presets === null) {
|
|
923
|
+
return {
|
|
924
|
+
kind: "error",
|
|
925
|
+
text: "/mode is unavailable: this deployment composes no agent presets."
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
let current;
|
|
929
|
+
try {
|
|
930
|
+
current = presets.composedPreset(agent.ctx);
|
|
931
|
+
} catch {
|
|
932
|
+
current = void 0;
|
|
933
|
+
}
|
|
934
|
+
if (current === target) {
|
|
935
|
+
return { kind: "success", text: "Already running " + target + "." };
|
|
936
|
+
}
|
|
937
|
+
try {
|
|
938
|
+
const preset = await presets.recompose(agent.ctx, target);
|
|
939
|
+
agent.session.append("agent-preset/selected", { agentPreset: preset.id });
|
|
940
|
+
if (planModeActive(agent.session.events)) {
|
|
941
|
+
agent.session.append("plan/mode", { active: false });
|
|
942
|
+
}
|
|
943
|
+
agent.steer(createUserMessage({
|
|
944
|
+
content: [
|
|
945
|
+
{
|
|
946
|
+
type: "text",
|
|
947
|
+
text: "The session switched to the " + preset.id + " agent preset. Continue in this mode with its tool set, persona, and model routing."
|
|
948
|
+
}
|
|
949
|
+
],
|
|
950
|
+
source: {
|
|
951
|
+
kind: "plugin",
|
|
952
|
+
plugin: "omd-mode-switch",
|
|
953
|
+
form: "notice",
|
|
954
|
+
summary: "Session mode switched to " + preset.id
|
|
955
|
+
}
|
|
956
|
+
}));
|
|
957
|
+
return {
|
|
958
|
+
kind: "success",
|
|
959
|
+
text: "Session preset switched to " + preset.id + " \u2014 the next turn runs with that mode's tools and model."
|
|
960
|
+
};
|
|
961
|
+
} catch (error) {
|
|
962
|
+
return {
|
|
963
|
+
kind: "error",
|
|
964
|
+
text: "/mode failed: " + (error instanceof Error ? error.message : String(error))
|
|
965
|
+
};
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
function apply(ctx) {
|
|
969
|
+
ctx.inject(["commands"], (commandCtx) => {
|
|
970
|
+
commandCtx.commands.register({
|
|
971
|
+
name: "mode",
|
|
972
|
+
description: "switch this session to another omd agent preset (tool set + model)",
|
|
973
|
+
input: {
|
|
974
|
+
hint: "<omd-* preset id>",
|
|
975
|
+
images: false
|
|
976
|
+
},
|
|
977
|
+
handler: (invocation) => executeSwitch(ctx, invocation)
|
|
143
978
|
});
|
|
979
|
+
});
|
|
144
980
|
}
|
|
145
|
-
export {
|
|
981
|
+
export {
|
|
982
|
+
apply,
|
|
983
|
+
inject,
|
|
984
|
+
name
|
|
985
|
+
};
|