@vunk/plus 1.3.2 → 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) {
@@ -5,6 +5,7 @@ import { useDeferred } from '@vunk/core/composables';
5
5
  import { ElIcon } from 'element-plus';
6
6
  import { noop } from '@vunk/shared/function';
7
7
  import { Deferred } from '@vunk/shared/promise';
8
+ import { c as consola } from '../browser.mjs';
8
9
  import { _ as _export_sfc } from '../_plugin-vue_export-helper.mjs';
9
10
 
10
11
  function markdownToPlainText(md) {
@@ -270,6 +271,10 @@ var _sfc_main$2 = /* @__PURE__ */ defineComponent({
270
271
  processing: {
271
272
  type: Function,
272
273
  default: noop
274
+ },
275
+ retryTimes: {
276
+ type: Number,
277
+ default: 10
273
278
  }
274
279
  },
275
280
  emits: ["update:status"],
@@ -277,6 +282,7 @@ var _sfc_main$2 = /* @__PURE__ */ defineComponent({
277
282
  const props = __props;
278
283
  const emit = __emit;
279
284
  const theDef = shallowRef();
285
+ let retryTimes = props.retryTimes;
280
286
  const processingDef = useDeferred();
281
287
  onMounted(async () => {
282
288
  emit("update:status", ParagraphStatus.processing);
@@ -299,8 +305,15 @@ var _sfc_main$2 = /* @__PURE__ */ defineComponent({
299
305
  emit("update:status", ParagraphStatus.pending);
300
306
  deferred.promise.then(() => {
301
307
  emit("update:status", ParagraphStatus.fulfilled);
302
- }).catch((err) => {
303
- console.error(err);
308
+ }).catch(async (err) => {
309
+ consola.error("error", err);
310
+ if (retryTimes > 0) {
311
+ retryTimes--;
312
+ await props.processing?.();
313
+ consola.info("retryTimes", retryTimes);
314
+ init();
315
+ return;
316
+ }
304
317
  emit("update:status", ParagraphStatus.rejected);
305
318
  });
306
319
  theDef.value = void 0;
@@ -173,6 +173,10 @@ declare const _default: import("vue").DefineComponent<import("vue").ExtractPropT
173
173
  type: import("vue").PropType<import("@vunk/shared").AnyFunc>;
174
174
  default: import("@vunk/shared").AnyFunc;
175
175
  };
176
+ retryTimes: {
177
+ type: NumberConstructor;
178
+ default: number;
179
+ };
176
180
  }>, {
177
181
  props: import("@vue/shared").LooseRequired<Readonly<import("vue").ExtractPropTypes<{
178
182
  enable: {
@@ -191,11 +195,16 @@ declare const _default: import("vue").DefineComponent<import("vue").ExtractPropT
191
195
  type: import("vue").PropType<import("@vunk/shared").AnyFunc>;
192
196
  default: import("@vunk/shared").AnyFunc;
193
197
  };
198
+ retryTimes: {
199
+ type: NumberConstructor;
200
+ default: number;
201
+ };
194
202
  }>> & Readonly<{
195
203
  "onUpdate:status"?: ((...args: any[]) => any) | undefined;
196
204
  }> & {}>;
197
205
  emit: (event: "update:status", ...args: any[]) => void;
198
206
  theDef: import("vue").ShallowRef<import("@vunk/shared/promise").Deferred<any> | undefined, import("@vunk/shared/promise").Deferred<any> | undefined>;
207
+ retryTimes: number;
199
208
  processingDef: {
200
209
  _ref: import("vue").ShallowRef<unknown, unknown>;
201
210
  readonly value: unknown;
@@ -221,6 +230,10 @@ declare const _default: import("vue").DefineComponent<import("vue").ExtractPropT
221
230
  type: import("vue").PropType<import("@vunk/shared").AnyFunc>;
222
231
  default: import("@vunk/shared").AnyFunc;
223
232
  };
233
+ retryTimes: {
234
+ type: NumberConstructor;
235
+ default: number;
236
+ };
224
237
  }>> & Readonly<{
225
238
  "onUpdate:status"?: ((...args: any[]) => any) | undefined;
226
239
  }>, {
@@ -228,6 +241,7 @@ declare const _default: import("vue").DefineComponent<import("vue").ExtractPropT
228
241
  pause: boolean;
229
242
  enable: boolean;
230
243
  status: ParagraphStatus;
244
+ retryTimes: number;
231
245
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
232
246
  VkTypingMarkdown: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
233
247
  source: {