@vunk/plus 1.3.1 → 1.3.4

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.
@@ -1,6 +1,7 @@
1
1
  import { computed, provide, inject, defineComponent } from 'vue';
2
2
  import { restFetch } from '@vunk/shared/fetch';
3
3
  import { useXChat, useXAgent } from 'ant-design-x-vue';
4
+ import { c as consola } from '../browser.mjs';
4
5
 
5
6
  async function agentRequest(onmessage, data) {
6
7
  return restFetch.reader({
@@ -48,580 +49,6 @@ const roleMap = roleOptions.reduce((acc, cur) => {
48
49
  return acc;
49
50
  }, {});
50
51
 
51
- const LogLevels = {
52
- fatal: 0,
53
- error: 0,
54
- warn: 1,
55
- log: 2,
56
- info: 3,
57
- success: 3,
58
- fail: 3,
59
- debug: 4,
60
- trace: 5,
61
- verbose: Number.POSITIVE_INFINITY
62
- };
63
- const LogTypes = {
64
- // Silent
65
- silent: {
66
- level: -1
67
- },
68
- // Level 0
69
- fatal: {
70
- level: LogLevels.fatal
71
- },
72
- error: {
73
- level: LogLevels.error
74
- },
75
- // Level 1
76
- warn: {
77
- level: LogLevels.warn
78
- },
79
- // Level 2
80
- log: {
81
- level: LogLevels.log
82
- },
83
- // Level 3
84
- info: {
85
- level: LogLevels.info
86
- },
87
- success: {
88
- level: LogLevels.success
89
- },
90
- fail: {
91
- level: LogLevels.fail
92
- },
93
- ready: {
94
- level: LogLevels.info
95
- },
96
- start: {
97
- level: LogLevels.info
98
- },
99
- box: {
100
- level: LogLevels.info
101
- },
102
- // Level 4
103
- debug: {
104
- level: LogLevels.debug
105
- },
106
- // Level 5
107
- trace: {
108
- level: LogLevels.trace
109
- },
110
- // Verbose
111
- verbose: {
112
- level: LogLevels.verbose
113
- }
114
- };
115
-
116
- function isPlainObject$1(value) {
117
- if (value === null || typeof value !== "object") {
118
- return false;
119
- }
120
- const prototype = Object.getPrototypeOf(value);
121
- if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
122
- return false;
123
- }
124
- if (Symbol.iterator in value) {
125
- return false;
126
- }
127
- if (Symbol.toStringTag in value) {
128
- return Object.prototype.toString.call(value) === "[object Module]";
129
- }
130
- return true;
131
- }
132
-
133
- function _defu(baseObject, defaults, namespace = ".", merger) {
134
- if (!isPlainObject$1(defaults)) {
135
- return _defu(baseObject, {}, namespace);
136
- }
137
- const object = Object.assign({}, defaults);
138
- for (const key in baseObject) {
139
- if (key === "__proto__" || key === "constructor") {
140
- continue;
141
- }
142
- const value = baseObject[key];
143
- if (value === null || value === void 0) {
144
- continue;
145
- }
146
- if (Array.isArray(value) && Array.isArray(object[key])) {
147
- object[key] = [...value, ...object[key]];
148
- } else if (isPlainObject$1(value) && isPlainObject$1(object[key])) {
149
- object[key] = _defu(
150
- value,
151
- object[key],
152
- (namespace ? `${namespace}.` : "") + key.toString());
153
- } else {
154
- object[key] = value;
155
- }
156
- }
157
- return object;
158
- }
159
- function createDefu(merger) {
160
- return (...arguments_) => (
161
- // eslint-disable-next-line unicorn/no-array-reduce
162
- arguments_.reduce((p, c) => _defu(p, c, ""), {})
163
- );
164
- }
165
- const defu = createDefu();
166
-
167
- function isPlainObject(obj) {
168
- return Object.prototype.toString.call(obj) === "[object Object]";
169
- }
170
- function isLogObj(arg) {
171
- if (!isPlainObject(arg)) {
172
- return false;
173
- }
174
- if (!arg.message && !arg.args) {
175
- return false;
176
- }
177
- if (arg.stack) {
178
- return false;
179
- }
180
- return true;
181
- }
182
-
183
- let paused = false;
184
- const queue = [];
185
- class Consola {
186
- options;
187
- _lastLog;
188
- _mockFn;
189
- /**
190
- * Creates an instance of Consola with specified options or defaults.
191
- *
192
- * @param {Partial<ConsolaOptions>} [options={}] - Configuration options for the Consola instance.
193
- */
194
- constructor(options = {}) {
195
- const types = options.types || LogTypes;
196
- this.options = defu(
197
- {
198
- ...options,
199
- defaults: { ...options.defaults },
200
- level: _normalizeLogLevel(options.level, types),
201
- reporters: [...options.reporters || []]
202
- },
203
- {
204
- types: LogTypes,
205
- throttle: 1e3,
206
- throttleMin: 5,
207
- formatOptions: {
208
- date: true,
209
- colors: false,
210
- compact: true
211
- }
212
- }
213
- );
214
- for (const type in types) {
215
- const defaults = {
216
- type,
217
- ...this.options.defaults,
218
- ...types[type]
219
- };
220
- this[type] = this._wrapLogFn(defaults);
221
- this[type].raw = this._wrapLogFn(
222
- defaults,
223
- true
224
- );
225
- }
226
- if (this.options.mockFn) {
227
- this.mockTypes();
228
- }
229
- this._lastLog = {};
230
- }
231
- /**
232
- * Gets the current log level of the Consola instance.
233
- *
234
- * @returns {number} The current log level.
235
- */
236
- get level() {
237
- return this.options.level;
238
- }
239
- /**
240
- * Sets the minimum log level that will be output by the instance.
241
- *
242
- * @param {number} level - The new log level to set.
243
- */
244
- set level(level) {
245
- this.options.level = _normalizeLogLevel(
246
- level,
247
- this.options.types,
248
- this.options.level
249
- );
250
- }
251
- /**
252
- * Displays a prompt to the user and returns the response.
253
- * Throw an error if `prompt` is not supported by the current configuration.
254
- *
255
- * @template T
256
- * @param {string} message - The message to display in the prompt.
257
- * @param {T} [opts] - Optional options for the prompt. See {@link PromptOptions}.
258
- * @returns {promise<T>} A promise that infer with the prompt options. See {@link PromptOptions}.
259
- */
260
- prompt(message, opts) {
261
- if (!this.options.prompt) {
262
- throw new Error("prompt is not supported!");
263
- }
264
- return this.options.prompt(message, opts);
265
- }
266
- /**
267
- * Creates a new instance of Consola, inheriting options from the current instance, with possible overrides.
268
- *
269
- * @param {Partial<ConsolaOptions>} options - Optional overrides for the new instance. See {@link ConsolaOptions}.
270
- * @returns {ConsolaInstance} A new Consola instance. See {@link ConsolaInstance}.
271
- */
272
- create(options) {
273
- const instance = new Consola({
274
- ...this.options,
275
- ...options
276
- });
277
- if (this._mockFn) {
278
- instance.mockTypes(this._mockFn);
279
- }
280
- return instance;
281
- }
282
- /**
283
- * Creates a new Consola instance with the specified default log object properties.
284
- *
285
- * @param {InputLogObject} defaults - Default properties to include in any log from the new instance. See {@link InputLogObject}.
286
- * @returns {ConsolaInstance} A new Consola instance. See {@link ConsolaInstance}.
287
- */
288
- withDefaults(defaults) {
289
- return this.create({
290
- ...this.options,
291
- defaults: {
292
- ...this.options.defaults,
293
- ...defaults
294
- }
295
- });
296
- }
297
- /**
298
- * Creates a new Consola instance with a specified tag, which will be included in every log.
299
- *
300
- * @param {string} tag - The tag to include in each log of the new instance.
301
- * @returns {ConsolaInstance} A new Consola instance. See {@link ConsolaInstance}.
302
- */
303
- withTag(tag) {
304
- return this.withDefaults({
305
- tag: this.options.defaults.tag ? this.options.defaults.tag + ":" + tag : tag
306
- });
307
- }
308
- /**
309
- * Adds a custom reporter to the Consola instance.
310
- * Reporters will be called for each log message, depending on their implementation and log level.
311
- *
312
- * @param {ConsolaReporter} reporter - The reporter to add. See {@link ConsolaReporter}.
313
- * @returns {Consola} The current Consola instance.
314
- */
315
- addReporter(reporter) {
316
- this.options.reporters.push(reporter);
317
- return this;
318
- }
319
- /**
320
- * Removes a custom reporter from the Consola instance.
321
- * If no reporter is specified, all reporters will be removed.
322
- *
323
- * @param {ConsolaReporter} reporter - The reporter to remove. See {@link ConsolaReporter}.
324
- * @returns {Consola} The current Consola instance.
325
- */
326
- removeReporter(reporter) {
327
- if (reporter) {
328
- const i = this.options.reporters.indexOf(reporter);
329
- if (i !== -1) {
330
- return this.options.reporters.splice(i, 1);
331
- }
332
- } else {
333
- this.options.reporters.splice(0);
334
- }
335
- return this;
336
- }
337
- /**
338
- * Replaces all reporters of the Consola instance with the specified array of reporters.
339
- *
340
- * @param {ConsolaReporter[]} reporters - The new reporters to set. See {@link ConsolaReporter}.
341
- * @returns {Consola} The current Consola instance.
342
- */
343
- setReporters(reporters) {
344
- this.options.reporters = Array.isArray(reporters) ? reporters : [reporters];
345
- return this;
346
- }
347
- wrapAll() {
348
- this.wrapConsole();
349
- this.wrapStd();
350
- }
351
- restoreAll() {
352
- this.restoreConsole();
353
- this.restoreStd();
354
- }
355
- /**
356
- * Overrides console methods with Consola logging methods for consistent logging.
357
- */
358
- wrapConsole() {
359
- for (const type in this.options.types) {
360
- if (!console["__" + type]) {
361
- console["__" + type] = console[type];
362
- }
363
- console[type] = this[type].raw;
364
- }
365
- }
366
- /**
367
- * Restores the original console methods, removing Consola overrides.
368
- */
369
- restoreConsole() {
370
- for (const type in this.options.types) {
371
- if (console["__" + type]) {
372
- console[type] = console["__" + type];
373
- delete console["__" + type];
374
- }
375
- }
376
- }
377
- /**
378
- * Overrides standard output and error streams to redirect them through Consola.
379
- */
380
- wrapStd() {
381
- this._wrapStream(this.options.stdout, "log");
382
- this._wrapStream(this.options.stderr, "log");
383
- }
384
- _wrapStream(stream, type) {
385
- if (!stream) {
386
- return;
387
- }
388
- if (!stream.__write) {
389
- stream.__write = stream.write;
390
- }
391
- stream.write = (data) => {
392
- this[type].raw(String(data).trim());
393
- };
394
- }
395
- /**
396
- * Restores the original standard output and error streams, removing the Consola redirection.
397
- */
398
- restoreStd() {
399
- this._restoreStream(this.options.stdout);
400
- this._restoreStream(this.options.stderr);
401
- }
402
- _restoreStream(stream) {
403
- if (!stream) {
404
- return;
405
- }
406
- if (stream.__write) {
407
- stream.write = stream.__write;
408
- delete stream.__write;
409
- }
410
- }
411
- /**
412
- * Pauses logging, queues incoming logs until resumed.
413
- */
414
- pauseLogs() {
415
- paused = true;
416
- }
417
- /**
418
- * Resumes logging, processing any queued logs.
419
- */
420
- resumeLogs() {
421
- paused = false;
422
- const _queue = queue.splice(0);
423
- for (const item of _queue) {
424
- item[0]._logFn(item[1], item[2]);
425
- }
426
- }
427
- /**
428
- * Replaces logging methods with mocks if a mock function is provided.
429
- *
430
- * @param {ConsolaOptions["mockFn"]} mockFn - The function to use for mocking logging methods. See {@link ConsolaOptions["mockFn"]}.
431
- */
432
- mockTypes(mockFn) {
433
- const _mockFn = mockFn || this.options.mockFn;
434
- this._mockFn = _mockFn;
435
- if (typeof _mockFn !== "function") {
436
- return;
437
- }
438
- for (const type in this.options.types) {
439
- this[type] = _mockFn(type, this.options.types[type]) || this[type];
440
- this[type].raw = this[type];
441
- }
442
- }
443
- _wrapLogFn(defaults, isRaw) {
444
- return (...args) => {
445
- if (paused) {
446
- queue.push([this, defaults, args, isRaw]);
447
- return;
448
- }
449
- return this._logFn(defaults, args, isRaw);
450
- };
451
- }
452
- _logFn(defaults, args, isRaw) {
453
- if ((defaults.level || 0) > this.level) {
454
- return false;
455
- }
456
- const logObj = {
457
- date: /* @__PURE__ */ new Date(),
458
- args: [],
459
- ...defaults,
460
- level: _normalizeLogLevel(defaults.level, this.options.types)
461
- };
462
- if (!isRaw && args.length === 1 && isLogObj(args[0])) {
463
- Object.assign(logObj, args[0]);
464
- } else {
465
- logObj.args = [...args];
466
- }
467
- if (logObj.message) {
468
- logObj.args.unshift(logObj.message);
469
- delete logObj.message;
470
- }
471
- if (logObj.additional) {
472
- if (!Array.isArray(logObj.additional)) {
473
- logObj.additional = logObj.additional.split("\n");
474
- }
475
- logObj.args.push("\n" + logObj.additional.join("\n"));
476
- delete logObj.additional;
477
- }
478
- logObj.type = typeof logObj.type === "string" ? logObj.type.toLowerCase() : "log";
479
- logObj.tag = typeof logObj.tag === "string" ? logObj.tag : "";
480
- const resolveLog = (newLog = false) => {
481
- const repeated = (this._lastLog.count || 0) - this.options.throttleMin;
482
- if (this._lastLog.object && repeated > 0) {
483
- const args2 = [...this._lastLog.object.args];
484
- if (repeated > 1) {
485
- args2.push(`(repeated ${repeated} times)`);
486
- }
487
- this._log({ ...this._lastLog.object, args: args2 });
488
- this._lastLog.count = 1;
489
- }
490
- if (newLog) {
491
- this._lastLog.object = logObj;
492
- this._log(logObj);
493
- }
494
- };
495
- clearTimeout(this._lastLog.timeout);
496
- const diffTime = this._lastLog.time && logObj.date ? logObj.date.getTime() - this._lastLog.time.getTime() : 0;
497
- this._lastLog.time = logObj.date;
498
- if (diffTime < this.options.throttle) {
499
- try {
500
- const serializedLog = JSON.stringify([
501
- logObj.type,
502
- logObj.tag,
503
- logObj.args
504
- ]);
505
- const isSameLog = this._lastLog.serialized === serializedLog;
506
- this._lastLog.serialized = serializedLog;
507
- if (isSameLog) {
508
- this._lastLog.count = (this._lastLog.count || 0) + 1;
509
- if (this._lastLog.count > this.options.throttleMin) {
510
- this._lastLog.timeout = setTimeout(
511
- resolveLog,
512
- this.options.throttle
513
- );
514
- return;
515
- }
516
- }
517
- } catch {
518
- }
519
- }
520
- resolveLog(true);
521
- }
522
- _log(logObj) {
523
- for (const reporter of this.options.reporters) {
524
- reporter.log(logObj, {
525
- options: this.options
526
- });
527
- }
528
- }
529
- }
530
- function _normalizeLogLevel(input, types = {}, defaultLevel = 3) {
531
- if (input === void 0) {
532
- return defaultLevel;
533
- }
534
- if (typeof input === "number") {
535
- return input;
536
- }
537
- if (types[input] && types[input].level !== void 0) {
538
- return types[input].level;
539
- }
540
- return defaultLevel;
541
- }
542
- Consola.prototype.add = Consola.prototype.addReporter;
543
- Consola.prototype.remove = Consola.prototype.removeReporter;
544
- Consola.prototype.clear = Consola.prototype.removeReporter;
545
- Consola.prototype.withScope = Consola.prototype.withTag;
546
- Consola.prototype.mock = Consola.prototype.mockTypes;
547
- Consola.prototype.pause = Consola.prototype.pauseLogs;
548
- Consola.prototype.resume = Consola.prototype.resumeLogs;
549
- function createConsola$1(options = {}) {
550
- return new Consola(options);
551
- }
552
-
553
- class BrowserReporter {
554
- options;
555
- defaultColor;
556
- levelColorMap;
557
- typeColorMap;
558
- constructor(options) {
559
- this.options = { ...options };
560
- this.defaultColor = "#7f8c8d";
561
- this.levelColorMap = {
562
- 0: "#c0392b",
563
- // Red
564
- 1: "#f39c12",
565
- // Yellow
566
- 3: "#00BCD4"
567
- // Cyan
568
- };
569
- this.typeColorMap = {
570
- success: "#2ecc71"
571
- // Green
572
- };
573
- }
574
- _getLogFn(level) {
575
- if (level < 1) {
576
- return console.__error || console.error;
577
- }
578
- if (level === 1) {
579
- return console.__warn || console.warn;
580
- }
581
- return console.__log || console.log;
582
- }
583
- log(logObj) {
584
- const consoleLogFn = this._getLogFn(logObj.level);
585
- const type = logObj.type === "log" ? "" : logObj.type;
586
- const tag = logObj.tag || "";
587
- const color = this.typeColorMap[logObj.type] || this.levelColorMap[logObj.level] || this.defaultColor;
588
- const style = `
589
- background: ${color};
590
- border-radius: 0.5em;
591
- color: white;
592
- font-weight: bold;
593
- padding: 2px 0.5em;
594
- `;
595
- const badge = `%c${[tag, type].filter(Boolean).join(":")}`;
596
- if (typeof logObj.args[0] === "string") {
597
- consoleLogFn(
598
- `${badge}%c ${logObj.args[0]}`,
599
- style,
600
- // Empty string as style resets to default console style
601
- "",
602
- ...logObj.args.slice(1)
603
- );
604
- } else {
605
- consoleLogFn(badge, style, ...logObj.args);
606
- }
607
- }
608
- }
609
-
610
- function createConsola(options = {}) {
611
- const consola2 = createConsola$1({
612
- reporters: options.reporters || [new BrowserReporter({})],
613
- prompt(message, options2 = {}) {
614
- if (options2.type === "confirm") {
615
- return Promise.resolve(confirm(message));
616
- }
617
- return Promise.resolve(prompt(message));
618
- },
619
- ...options
620
- });
621
- return consola2;
622
- }
623
- const consola = createConsola();
624
-
625
52
  const ChatAgentInjectKey = "vk_agent_chat_inject_key";
626
53
 
627
54
  function useAgent(request) {