dsh-composer-ux 0.1.2
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/CHANGELOG.md +47 -0
- package/LICENSE +21 -0
- package/README.md +148 -0
- package/cordis.patch.yml +12 -0
- package/lib/client.js +1626 -0
- package/lib/client.js.map +7 -0
- package/lib/index.js +1029 -0
- package/lib/index.js.map +7 -0
- package/package.json +54 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,1029 @@
|
|
|
1
|
+
// ../../../DeepSeek Harness/vendor/cosmokit/lib/index.js
|
|
2
|
+
function isNullable(value) {
|
|
3
|
+
return value === null || value === void 0;
|
|
4
|
+
}
|
|
5
|
+
function isPlainObject(data) {
|
|
6
|
+
return data && typeof data === "object" && !Array.isArray(data);
|
|
7
|
+
}
|
|
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
|
+
throw new Error("Binary.fromBase64: 本发行产物只在 Node 环境(Buffer 可用)下运行");
|
|
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
|
+
// ../../../DeepSeek Harness/vendor/schemastery/lib/index.mjs
|
|
188
|
+
var kSchema = Symbol.for("schemastery");
|
|
189
|
+
var kValidationError = 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);
|
|
222
|
+
}
|
|
223
|
+
return refs[options.uid];
|
|
224
|
+
}
|
|
225
|
+
Object.assign(schema, options);
|
|
226
|
+
if (typeof schema.callback === "string") try {
|
|
227
|
+
schema.callback = null;
|
|
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
|
+
}
|
|
252
|
+
}
|
|
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;
|
|
435
|
+
}
|
|
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
|
+
};
|
|
478
|
+
}
|
|
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) => {
|
|
503
|
+
try {
|
|
504
|
+
return new RegExp(value, flag);
|
|
505
|
+
} catch (e) {
|
|
506
|
+
throw new ValidationError(e.message, options);
|
|
507
|
+
}
|
|
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];
|
|
599
|
+
}
|
|
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);
|
|
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;
|
|
646
|
+
try {
|
|
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;
|
|
744
|
+
}
|
|
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
|
+
// src/settings-contract.ts
|
|
790
|
+
var NAMESPACE = "composer-ux";
|
|
791
|
+
var ENABLED_FIELD = "enabled";
|
|
792
|
+
var SEND_KEY_FIELD = "sendKey";
|
|
793
|
+
var NEWLINE_KEY_FIELD = "newlineKey";
|
|
794
|
+
var PANEL_SCROLL_FIELD = "panelScroll";
|
|
795
|
+
var PANEL_RESIZE_FIELD = "panelResize";
|
|
796
|
+
var PANEL_WIDTH_FIELD = "panelWidth";
|
|
797
|
+
var PANEL_HEIGHT_FIELD = "panelHeight";
|
|
798
|
+
var MENU_FIELDS = [
|
|
799
|
+
"menuUndo",
|
|
800
|
+
"menuRedo",
|
|
801
|
+
"menuCut",
|
|
802
|
+
"menuCopy",
|
|
803
|
+
"menuPaste",
|
|
804
|
+
"menuDelete",
|
|
805
|
+
"menuSelectAll"
|
|
806
|
+
];
|
|
807
|
+
var MENU_NATIVE_FIELD = "menuNative";
|
|
808
|
+
var HEADER_ENABLED_FIELD = "headerEnabled";
|
|
809
|
+
var HEADER_NAME_FIELD = "headerName";
|
|
810
|
+
var HEADER_VALUE_FIELD = "headerValue";
|
|
811
|
+
var HEADER_ROUTES_FIELD = "headerRoutes";
|
|
812
|
+
var HEADER_APPLIED_NAME_FIELD = "headerAppliedName";
|
|
813
|
+
var HEADER_APPLIED_VALUE_FIELD = "headerAppliedValue";
|
|
814
|
+
var HEADER_STATUS_FIELD = "headerStatus";
|
|
815
|
+
var LLM_NAMESPACE = "llm-pi-ai";
|
|
816
|
+
var OPENCODE_ROUTE_PREFIX = "opencode";
|
|
817
|
+
var DEFAULT_HEADER_NAME = "x-opencode-session";
|
|
818
|
+
var HEADER_VALUE_MAX = 200;
|
|
819
|
+
var HEADER_NAME_MAX = 64;
|
|
820
|
+
var DEFAULT_SETTINGS = {
|
|
821
|
+
enabled: true,
|
|
822
|
+
sendKey: "Enter",
|
|
823
|
+
newlineKey: "Shift+Enter",
|
|
824
|
+
menuUndo: true,
|
|
825
|
+
menuRedo: true,
|
|
826
|
+
menuCut: true,
|
|
827
|
+
menuCopy: true,
|
|
828
|
+
menuPaste: true,
|
|
829
|
+
menuDelete: true,
|
|
830
|
+
menuSelectAll: true,
|
|
831
|
+
menuNative: false,
|
|
832
|
+
panelScroll: true,
|
|
833
|
+
panelResize: true,
|
|
834
|
+
headerEnabled: false,
|
|
835
|
+
headerName: DEFAULT_HEADER_NAME,
|
|
836
|
+
headerValue: "",
|
|
837
|
+
headerRoutes: "",
|
|
838
|
+
headerAppliedName: "",
|
|
839
|
+
headerAppliedValue: "",
|
|
840
|
+
headerStatus: ""
|
|
841
|
+
};
|
|
842
|
+
function newSessionId() {
|
|
843
|
+
const bag = globalThis;
|
|
844
|
+
if (typeof bag.crypto?.randomUUID === "function") return bag.crypto.randomUUID();
|
|
845
|
+
const hex = (count) => Array.from(
|
|
846
|
+
{ length: count },
|
|
847
|
+
() => Math.floor(Math.random() * 16).toString(16)
|
|
848
|
+
).join("");
|
|
849
|
+
return `${hex(8)}-${hex(4)}-4${hex(3)}-a${hex(3)}-${hex(12)}`;
|
|
850
|
+
}
|
|
851
|
+
function parseRouteList(text) {
|
|
852
|
+
const parts = text.split(/[\s,,、]+/).map((item) => item.trim()).filter((item) => item !== "");
|
|
853
|
+
return [...new Set(parts)];
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
// src/host.ts
|
|
857
|
+
var name = "composer-ux";
|
|
858
|
+
var PANEL_MIN = 560;
|
|
859
|
+
var PANEL_MAX = 4e3;
|
|
860
|
+
function objectOf(value) {
|
|
861
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
862
|
+
}
|
|
863
|
+
function textOf(value) {
|
|
864
|
+
return typeof value === "string" ? value : "";
|
|
865
|
+
}
|
|
866
|
+
function nameIsValid(name2) {
|
|
867
|
+
if (name2 === "" || name2.length > HEADER_NAME_MAX) return false;
|
|
868
|
+
try {
|
|
869
|
+
new Headers([[name2, "probe"]]);
|
|
870
|
+
return true;
|
|
871
|
+
} catch {
|
|
872
|
+
return false;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
function valueIsValid(value) {
|
|
876
|
+
if (value === "" || value.length > HEADER_VALUE_MAX) return false;
|
|
877
|
+
try {
|
|
878
|
+
new Headers([["x-dsh-probe", value]]);
|
|
879
|
+
return true;
|
|
880
|
+
} catch {
|
|
881
|
+
return false;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
function pickRoutes(providers, listed) {
|
|
885
|
+
const keys = Object.keys(providers);
|
|
886
|
+
if (listed.length === 0) return keys.filter((key) => key.startsWith(OPENCODE_ROUTE_PREFIX));
|
|
887
|
+
return listed.filter((key) => Object.prototype.hasOwnProperty.call(providers, key));
|
|
888
|
+
}
|
|
889
|
+
function errorText(error) {
|
|
890
|
+
if (error instanceof Error && error.message !== "") return error.message;
|
|
891
|
+
return String(error);
|
|
892
|
+
}
|
|
893
|
+
async function mirrorHeader(settings) {
|
|
894
|
+
const own = objectOf(settings.get(NAMESPACE));
|
|
895
|
+
if (own === void 0) return;
|
|
896
|
+
const enabled = own[ENABLED_FIELD] === true && own[HEADER_ENABLED_FIELD] === true;
|
|
897
|
+
const name2 = textOf(own[HEADER_NAME_FIELD]);
|
|
898
|
+
const nameOk = nameIsValid(name2);
|
|
899
|
+
const routesText = parseRouteList(textOf(own[HEADER_ROUTES_FIELD]));
|
|
900
|
+
let value = textOf(own[HEADER_VALUE_FIELD]);
|
|
901
|
+
if (enabled && nameOk && value === "") {
|
|
902
|
+
value = newSessionId();
|
|
903
|
+
await settings.mutate(NAMESPACE, [{ op: "set", path: [HEADER_VALUE_FIELD], value }]);
|
|
904
|
+
}
|
|
905
|
+
const llm = objectOf(settings.get(LLM_NAMESPACE));
|
|
906
|
+
const providers = llm === void 0 ? void 0 : objectOf(llm.providers);
|
|
907
|
+
const writes = [];
|
|
908
|
+
const applied = [];
|
|
909
|
+
let status = "";
|
|
910
|
+
if (!enabled) {
|
|
911
|
+
status = "";
|
|
912
|
+
} else if (providers === void 0) {
|
|
913
|
+
status = "\u672A\u6302\u8F7D llm-pi-ai\uFF08\u6A21\u578B\u8DEF\u7531\uFF09\u8BBE\u7F6E\uFF0C\u6682\u672A\u5199\u5165";
|
|
914
|
+
} else if (!nameOk) {
|
|
915
|
+
status = "\u5934\u540D\u4E0D\u5408\u6CD5\uFF0C\u672A\u5199\u5165";
|
|
916
|
+
} else if (!valueIsValid(value)) {
|
|
917
|
+
status = `\u5934\u503C\u4E0D\u5408\u6CD5\uFF08\u9700 1\u2013${HEADER_VALUE_MAX} \u5B57\u7B26\u4E14\u5355\u884C\uFF09\uFF0C\u672A\u5199\u5165`;
|
|
918
|
+
} else {
|
|
919
|
+
for (const route of pickRoutes(providers, routesText)) {
|
|
920
|
+
const headers = objectOf(objectOf(providers[route])?.headers);
|
|
921
|
+
if (headers?.[name2] !== value) {
|
|
922
|
+
writes.push({ op: "set", path: ["providers", route, "headers", name2], value });
|
|
923
|
+
}
|
|
924
|
+
applied.push(route);
|
|
925
|
+
}
|
|
926
|
+
status = applied.length === 0 ? `\u5DF2\u542F\u7528\uFF0C\u4F46\u6CA1\u6709\u53EF\u5199\u5165\u7684\u8DEF\u7531\uFF08\u76EE\u6807\uFF1A${routesText.length === 0 ? `${OPENCODE_ROUTE_PREFIX}*` : routesText.join("\u3001")}\uFF09` : `\u5DF2\u5199\u5165 ${applied.join("\u3001")}`;
|
|
927
|
+
}
|
|
928
|
+
const stale = [];
|
|
929
|
+
if (providers !== void 0) {
|
|
930
|
+
const previousName = textOf(own[HEADER_APPLIED_NAME_FIELD]);
|
|
931
|
+
const previousValue = textOf(own[HEADER_APPLIED_VALUE_FIELD]);
|
|
932
|
+
const ours = /* @__PURE__ */ new Set([textOf(own[HEADER_VALUE_FIELD]), previousValue]);
|
|
933
|
+
ours.delete("");
|
|
934
|
+
const targets = new Set(applied.map((route) => `${route}\0${name2}`));
|
|
935
|
+
const candidates = /* @__PURE__ */ new Set([previousName, nameOk ? name2 : ""]);
|
|
936
|
+
candidates.delete("");
|
|
937
|
+
for (const candidate of candidates) {
|
|
938
|
+
for (const [route, profile] of Object.entries(providers)) {
|
|
939
|
+
if (targets.has(`${route}\0${candidate}`)) continue;
|
|
940
|
+
const headers = objectOf(objectOf(profile)?.headers);
|
|
941
|
+
const current = headers?.[candidate];
|
|
942
|
+
if (typeof current !== "string" || !ours.has(current)) continue;
|
|
943
|
+
stale.push({ op: "unset", path: ["providers", route, "headers", candidate] });
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
const providerOps = [...writes, ...stale];
|
|
948
|
+
let failed = false;
|
|
949
|
+
if (providerOps.length > 0) {
|
|
950
|
+
try {
|
|
951
|
+
await settings.mutate(LLM_NAMESPACE, providerOps);
|
|
952
|
+
} catch (error) {
|
|
953
|
+
failed = true;
|
|
954
|
+
status = `\u5199\u5165\u5931\u8D25\uFF1A${errorText(error)}`;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
const nextAppliedName = !failed && applied.length > 0 ? name2 : "";
|
|
958
|
+
const nextAppliedValue = !failed && applied.length > 0 ? value : "";
|
|
959
|
+
const ownOps = [];
|
|
960
|
+
if (textOf(own[HEADER_APPLIED_NAME_FIELD]) !== nextAppliedName) {
|
|
961
|
+
ownOps.push({ op: "set", path: [HEADER_APPLIED_NAME_FIELD], value: nextAppliedName });
|
|
962
|
+
}
|
|
963
|
+
if (textOf(own[HEADER_APPLIED_VALUE_FIELD]) !== nextAppliedValue) {
|
|
964
|
+
ownOps.push({ op: "set", path: [HEADER_APPLIED_VALUE_FIELD], value: nextAppliedValue });
|
|
965
|
+
}
|
|
966
|
+
if (textOf(own[HEADER_STATUS_FIELD]) !== status) {
|
|
967
|
+
ownOps.push({ op: "set", path: [HEADER_STATUS_FIELD], value: status });
|
|
968
|
+
}
|
|
969
|
+
if (ownOps.length > 0) await settings.mutate(NAMESPACE, ownOps);
|
|
970
|
+
}
|
|
971
|
+
function apply(ctx) {
|
|
972
|
+
ctx.inject(["settings"], (settingsCtx) => {
|
|
973
|
+
settingsCtx.settings.register(
|
|
974
|
+
NAMESPACE,
|
|
975
|
+
Schema.object({
|
|
976
|
+
[ENABLED_FIELD]: Schema.boolean().default(DEFAULT_SETTINGS.enabled),
|
|
977
|
+
[SEND_KEY_FIELD]: Schema.string().default(DEFAULT_SETTINGS.sendKey),
|
|
978
|
+
[NEWLINE_KEY_FIELD]: Schema.string().default(DEFAULT_SETTINGS.newlineKey),
|
|
979
|
+
...Object.fromEntries(MENU_FIELDS.map((field) => [
|
|
980
|
+
field,
|
|
981
|
+
Schema.boolean().default(DEFAULT_SETTINGS[field])
|
|
982
|
+
])),
|
|
983
|
+
[MENU_NATIVE_FIELD]: Schema.boolean().default(DEFAULT_SETTINGS.menuNative),
|
|
984
|
+
[PANEL_SCROLL_FIELD]: Schema.boolean().default(DEFAULT_SETTINGS.panelScroll),
|
|
985
|
+
[PANEL_RESIZE_FIELD]: Schema.boolean().default(DEFAULT_SETTINGS.panelResize),
|
|
986
|
+
// schemastery 无 .optional():可选键用 .required(false)。
|
|
987
|
+
[PANEL_WIDTH_FIELD]: Schema.number().min(PANEL_MIN).max(PANEL_MAX).required(false),
|
|
988
|
+
[PANEL_HEIGHT_FIELD]: Schema.number().min(320).max(PANEL_MAX).required(false),
|
|
989
|
+
// OpenCode 请求头栏目(值由宿主半按 llm-pi-ai 的 Fetch 规则自校验后再写入)。
|
|
990
|
+
[HEADER_ENABLED_FIELD]: Schema.boolean().default(DEFAULT_SETTINGS.headerEnabled),
|
|
991
|
+
[HEADER_NAME_FIELD]: Schema.string().default(DEFAULT_HEADER_NAME),
|
|
992
|
+
[HEADER_VALUE_FIELD]: Schema.string().default(DEFAULT_SETTINGS.headerValue),
|
|
993
|
+
[HEADER_ROUTES_FIELD]: Schema.string().default(DEFAULT_SETTINGS.headerRoutes),
|
|
994
|
+
[HEADER_APPLIED_NAME_FIELD]: Schema.string().default(DEFAULT_SETTINGS.headerAppliedName),
|
|
995
|
+
[HEADER_APPLIED_VALUE_FIELD]: Schema.string().default(DEFAULT_SETTINGS.headerAppliedValue),
|
|
996
|
+
[HEADER_STATUS_FIELD]: Schema.string().default(DEFAULT_SETTINGS.headerStatus)
|
|
997
|
+
})
|
|
998
|
+
);
|
|
999
|
+
const settings = settingsCtx.settings;
|
|
1000
|
+
let busy = false;
|
|
1001
|
+
let again = false;
|
|
1002
|
+
const sync = () => {
|
|
1003
|
+
if (busy) {
|
|
1004
|
+
again = true;
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
1007
|
+
busy = true;
|
|
1008
|
+
void mirrorHeader(settings).catch((error) => {
|
|
1009
|
+
console.error("[composer-ux] \u8BF7\u6C42\u5934\u955C\u50CF\u5931\u8D25", error);
|
|
1010
|
+
}).finally(() => {
|
|
1011
|
+
busy = false;
|
|
1012
|
+
if (again) {
|
|
1013
|
+
again = false;
|
|
1014
|
+
sync();
|
|
1015
|
+
}
|
|
1016
|
+
});
|
|
1017
|
+
};
|
|
1018
|
+
const onSettingsUpdated = (ns) => {
|
|
1019
|
+
if (ns === NAMESPACE || ns === LLM_NAMESPACE) sync();
|
|
1020
|
+
};
|
|
1021
|
+
ctx.on("settings/updated", onSettingsUpdated);
|
|
1022
|
+
sync();
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
export {
|
|
1026
|
+
apply,
|
|
1027
|
+
name
|
|
1028
|
+
};
|
|
1029
|
+
//# sourceMappingURL=index.js.map
|