@nocobase/plugin-ai 2.1.0-beta.16 → 2.1.0-beta.19

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.
Files changed (66) hide show
  1. package/dist/ai/ai-employees/atlas/prompt.md +28 -22
  2. package/dist/ai/ai-employees/viz.js +14 -88
  3. package/dist/ai/skills/business-analysis-report/SKILLS.md +8 -3
  4. package/dist/ai/skills/business-analysis-report/tools/businessReportGenerator.js +49 -8
  5. package/dist/client/280.003b9e9788599244.js +10 -0
  6. package/dist/client/486.afbed6b132b3c0dd.js +10 -0
  7. package/dist/client/646.cba98d80e9e6ea74.js +10 -0
  8. package/dist/client/ai-employees/chatbox/conversations/WorkflowTasksList.d.ts +2 -0
  9. package/dist/client/ai-employees/chatbox/hooks/useWorkflowTasks.d.ts +3 -0
  10. package/dist/client/ai-employees/chatbox/stores/workflow-tasks.d.ts +4 -0
  11. package/dist/client/index.js +5 -5
  12. package/dist/client/workflow/nodes/employee/flow-models/feedback.d.ts +1 -0
  13. package/dist/common/recommended-models.js +1 -25
  14. package/dist/externalVersion.js +14 -14
  15. package/dist/locale/en-US.json +15 -1
  16. package/dist/locale/zh-CN.json +15 -2
  17. package/dist/node_modules/fast-glob/package.json +1 -1
  18. package/dist/node_modules/flexsearch/package.json +1 -1
  19. package/dist/node_modules/fs-extra/package.json +1 -1
  20. package/dist/node_modules/jsonrepair/bin/cli.js +179 -0
  21. package/dist/node_modules/jsonrepair/lib/cjs/index.js +1 -0
  22. package/dist/node_modules/jsonrepair/lib/cjs/package.json +3 -0
  23. package/dist/node_modules/jsonrepair/lib/cjs/regular/jsonrepair.js +746 -0
  24. package/dist/node_modules/jsonrepair/lib/cjs/stream.js +13 -0
  25. package/dist/node_modules/jsonrepair/lib/cjs/streaming/buffer/InputBuffer.js +75 -0
  26. package/dist/node_modules/jsonrepair/lib/cjs/streaming/buffer/OutputBuffer.js +117 -0
  27. package/dist/node_modules/jsonrepair/lib/cjs/streaming/core.js +824 -0
  28. package/dist/node_modules/jsonrepair/lib/cjs/streaming/stack.js +51 -0
  29. package/dist/node_modules/jsonrepair/lib/cjs/streaming/stream.js +37 -0
  30. package/dist/node_modules/jsonrepair/lib/cjs/utils/JSONRepairError.js +14 -0
  31. package/dist/node_modules/jsonrepair/lib/cjs/utils/stringUtils.js +174 -0
  32. package/dist/node_modules/jsonrepair/lib/esm/index.js +4 -0
  33. package/dist/node_modules/jsonrepair/lib/esm/regular/jsonrepair.js +740 -0
  34. package/dist/node_modules/jsonrepair/lib/esm/stream.js +3 -0
  35. package/dist/node_modules/jsonrepair/lib/esm/streaming/buffer/InputBuffer.js +69 -0
  36. package/dist/node_modules/jsonrepair/lib/esm/streaming/buffer/OutputBuffer.js +111 -0
  37. package/dist/node_modules/jsonrepair/lib/esm/streaming/core.js +818 -0
  38. package/dist/node_modules/jsonrepair/lib/esm/streaming/stack.js +44 -0
  39. package/dist/node_modules/jsonrepair/lib/esm/streaming/stream.js +31 -0
  40. package/dist/node_modules/jsonrepair/lib/esm/utils/JSONRepairError.js +7 -0
  41. package/dist/node_modules/jsonrepair/lib/esm/utils/stringUtils.js +147 -0
  42. package/dist/node_modules/jsonrepair/lib/types/index.d.ts +3 -0
  43. package/dist/node_modules/jsonrepair/lib/types/regular/jsonrepair.d.ts +18 -0
  44. package/dist/node_modules/jsonrepair/lib/types/stream.d.ts +2 -0
  45. package/dist/node_modules/jsonrepair/lib/types/streaming/buffer/InputBuffer.d.ts +14 -0
  46. package/dist/node_modules/jsonrepair/lib/types/streaming/buffer/OutputBuffer.d.ts +18 -0
  47. package/dist/node_modules/jsonrepair/lib/types/streaming/core.d.ts +11 -0
  48. package/dist/node_modules/jsonrepair/lib/types/streaming/stack.d.ts +20 -0
  49. package/dist/node_modules/jsonrepair/lib/types/streaming/stream.d.ts +7 -0
  50. package/dist/node_modules/jsonrepair/lib/types/utils/JSONRepairError.d.ts +5 -0
  51. package/dist/node_modules/jsonrepair/lib/types/utils/stringUtils.d.ts +65 -0
  52. package/dist/node_modules/jsonrepair/lib/umd/jsonrepair.js +903 -0
  53. package/dist/node_modules/jsonrepair/lib/umd/jsonrepair.min.js +3 -0
  54. package/dist/node_modules/jsonrepair/lib/umd/package.json +3 -0
  55. package/dist/node_modules/jsonrepair/package.json +1 -0
  56. package/dist/node_modules/nodejs-snowflake/package.json +1 -1
  57. package/dist/node_modules/openai/package.json +1 -1
  58. package/dist/node_modules/zod/package.json +1 -1
  59. package/dist/server/collections/ai-workflow-tasks.js +1 -1
  60. package/dist/server/resource/aiEmployees.js +15 -1
  61. package/dist/server/resource/aiSkills.js +1 -1
  62. package/dist/server/workflow/nodes/employee/index.js +13 -0
  63. package/package.json +2 -2
  64. package/dist/client/280.029428b83d62155f.js +0 -10
  65. package/dist/client/486.77c26e2e7f8daf28.js +0 -10
  66. package/dist/client/646.ef9d7c2ea8641044.js +0 -10
@@ -0,0 +1,740 @@
1
+ import { JSONRepairError } from '../utils/JSONRepairError.js';
2
+ import { endsWithCommaOrNewline, insertBeforeLastWhitespace, isControlCharacter, isDelimiter, isDigit, isDoubleQuote, isDoubleQuoteLike, isFunctionNameChar, isFunctionNameCharStart, isHex, isQuote, isSingleQuote, isSingleQuoteLike, isSpecialWhitespace, isStartOfValue, isUnquotedStringDelimiter, isValidStringCharacter, isWhitespace, isWhitespaceExceptNewline, regexUrlChar, regexUrlStart, removeAtIndex, stripLastOccurrence } from '../utils/stringUtils.js';
3
+ const controlCharacters = {
4
+ '\b': '\\b',
5
+ '\f': '\\f',
6
+ '\n': '\\n',
7
+ '\r': '\\r',
8
+ '\t': '\\t'
9
+ };
10
+
11
+ // map with all escape characters
12
+ const escapeCharacters = {
13
+ '"': '"',
14
+ '\\': '\\',
15
+ '/': '/',
16
+ b: '\b',
17
+ f: '\f',
18
+ n: '\n',
19
+ r: '\r',
20
+ t: '\t'
21
+ // note that \u is handled separately in parseString()
22
+ };
23
+
24
+ /**
25
+ * Repair a string containing an invalid JSON document.
26
+ * For example changes JavaScript notation into JSON notation.
27
+ *
28
+ * Example:
29
+ *
30
+ * try {
31
+ * const json = "{name: 'John'}"
32
+ * const repaired = jsonrepair(json)
33
+ * console.log(repaired)
34
+ * // '{"name": "John"}'
35
+ * } catch (err) {
36
+ * console.error(err)
37
+ * }
38
+ *
39
+ */
40
+ export function jsonrepair(text) {
41
+ let i = 0; // current index in text
42
+ let output = ''; // generated output
43
+
44
+ parseMarkdownCodeBlock(['```', '[```', '{```']);
45
+ const processed = parseValue();
46
+ if (!processed) {
47
+ throwUnexpectedEnd();
48
+ }
49
+ parseMarkdownCodeBlock(['```', '```]', '```}']);
50
+ const processedComma = parseCharacter(',');
51
+ if (processedComma) {
52
+ parseWhitespaceAndSkipComments();
53
+ }
54
+ if (isStartOfValue(text[i]) && endsWithCommaOrNewline(output)) {
55
+ // start of a new value after end of the root level object: looks like
56
+ // newline delimited JSON -> turn into a root level array
57
+ if (!processedComma) {
58
+ // repair missing comma
59
+ output = insertBeforeLastWhitespace(output, ',');
60
+ }
61
+ parseNewlineDelimitedJSON();
62
+ } else if (processedComma) {
63
+ // repair: remove trailing comma
64
+ output = stripLastOccurrence(output, ',');
65
+ }
66
+
67
+ // repair redundant end quotes
68
+ while (text[i] === '}' || text[i] === ']') {
69
+ i++;
70
+ parseWhitespaceAndSkipComments();
71
+ }
72
+ if (i >= text.length) {
73
+ // reached the end of the document properly
74
+ return output;
75
+ }
76
+ throwUnexpectedCharacter();
77
+ function parseValue() {
78
+ parseWhitespaceAndSkipComments();
79
+ const processed = parseObject() || parseArray() || parseString() || parseNumber() || parseKeywords() || parseUnquotedString(false) || parseRegex();
80
+ parseWhitespaceAndSkipComments();
81
+ return processed;
82
+ }
83
+ function parseWhitespaceAndSkipComments() {
84
+ let skipNewline = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
85
+ const start = i;
86
+ let changed = parseWhitespace(skipNewline);
87
+ do {
88
+ changed = parseComment();
89
+ if (changed) {
90
+ changed = parseWhitespace(skipNewline);
91
+ }
92
+ } while (changed);
93
+ return i > start;
94
+ }
95
+ function parseWhitespace(skipNewline) {
96
+ const _isWhiteSpace = skipNewline ? isWhitespace : isWhitespaceExceptNewline;
97
+ let whitespace = '';
98
+ while (true) {
99
+ if (_isWhiteSpace(text, i)) {
100
+ whitespace += text[i];
101
+ i++;
102
+ } else if (isSpecialWhitespace(text, i)) {
103
+ // repair special whitespace
104
+ whitespace += ' ';
105
+ i++;
106
+ } else {
107
+ break;
108
+ }
109
+ }
110
+ if (whitespace.length > 0) {
111
+ output += whitespace;
112
+ return true;
113
+ }
114
+ return false;
115
+ }
116
+ function parseComment() {
117
+ // find a block comment '/* ... */'
118
+ if (text[i] === '/' && text[i + 1] === '*') {
119
+ // repair block comment by skipping it
120
+ while (i < text.length && !atEndOfBlockComment(text, i)) {
121
+ i++;
122
+ }
123
+ i += 2;
124
+ return true;
125
+ }
126
+
127
+ // find a line comment '// ...'
128
+ if (text[i] === '/' && text[i + 1] === '/') {
129
+ // repair line comment by skipping it
130
+ while (i < text.length && text[i] !== '\n') {
131
+ i++;
132
+ }
133
+ return true;
134
+ }
135
+ return false;
136
+ }
137
+ function parseMarkdownCodeBlock(blocks) {
138
+ // find and skip over a Markdown fenced code block:
139
+ // ``` ... ```
140
+ // or
141
+ // ```json ... ```
142
+ if (skipMarkdownCodeBlock(blocks)) {
143
+ if (isFunctionNameCharStart(text[i])) {
144
+ // strip the optional language specifier like "json"
145
+ while (i < text.length && isFunctionNameChar(text[i])) {
146
+ i++;
147
+ }
148
+ }
149
+ parseWhitespaceAndSkipComments();
150
+ return true;
151
+ }
152
+ return false;
153
+ }
154
+ function skipMarkdownCodeBlock(blocks) {
155
+ parseWhitespace(true);
156
+ for (const block of blocks) {
157
+ const end = i + block.length;
158
+ if (text.slice(i, end) === block) {
159
+ i = end;
160
+ return true;
161
+ }
162
+ }
163
+ return false;
164
+ }
165
+ function parseCharacter(char) {
166
+ if (text[i] === char) {
167
+ output += text[i];
168
+ i++;
169
+ return true;
170
+ }
171
+ return false;
172
+ }
173
+ function skipCharacter(char) {
174
+ if (text[i] === char) {
175
+ i++;
176
+ return true;
177
+ }
178
+ return false;
179
+ }
180
+ function skipEscapeCharacter() {
181
+ return skipCharacter('\\');
182
+ }
183
+
184
+ /**
185
+ * Skip ellipsis like "[1,2,3,...]" or "[1,2,3,...,9]" or "[...,7,8,9]"
186
+ * or a similar construct in objects.
187
+ */
188
+ function skipEllipsis() {
189
+ parseWhitespaceAndSkipComments();
190
+ if (text[i] === '.' && text[i + 1] === '.' && text[i + 2] === '.') {
191
+ // repair: remove the ellipsis (three dots) and optionally a comma
192
+ i += 3;
193
+ parseWhitespaceAndSkipComments();
194
+ skipCharacter(',');
195
+ return true;
196
+ }
197
+ return false;
198
+ }
199
+
200
+ /**
201
+ * Parse an object like '{"key": "value"}'
202
+ */
203
+ function parseObject() {
204
+ if (text[i] === '{') {
205
+ output += '{';
206
+ i++;
207
+ parseWhitespaceAndSkipComments();
208
+
209
+ // repair: skip leading comma like in {, message: "hi"}
210
+ if (skipCharacter(',')) {
211
+ parseWhitespaceAndSkipComments();
212
+ }
213
+ let initial = true;
214
+ while (i < text.length && text[i] !== '}') {
215
+ let processedComma;
216
+ if (!initial) {
217
+ processedComma = parseCharacter(',');
218
+ if (!processedComma) {
219
+ // repair missing comma
220
+ output = insertBeforeLastWhitespace(output, ',');
221
+ }
222
+ parseWhitespaceAndSkipComments();
223
+ } else {
224
+ processedComma = true;
225
+ initial = false;
226
+ }
227
+ skipEllipsis();
228
+ const processedKey = parseString() || parseUnquotedString(true);
229
+ if (!processedKey) {
230
+ if (text[i] === '}' || text[i] === '{' || text[i] === ']' || text[i] === '[' || text[i] === undefined) {
231
+ // repair trailing comma
232
+ output = stripLastOccurrence(output, ',');
233
+ } else {
234
+ throwObjectKeyExpected();
235
+ }
236
+ break;
237
+ }
238
+ parseWhitespaceAndSkipComments();
239
+ const processedColon = parseCharacter(':');
240
+ const truncatedText = i >= text.length;
241
+ if (!processedColon) {
242
+ if (isStartOfValue(text[i]) || truncatedText) {
243
+ // repair missing colon
244
+ output = insertBeforeLastWhitespace(output, ':');
245
+ } else {
246
+ throwColonExpected();
247
+ }
248
+ }
249
+ const processedValue = parseValue();
250
+ if (!processedValue) {
251
+ if (processedColon || truncatedText) {
252
+ // repair missing object value
253
+ output += 'null';
254
+ } else {
255
+ throwColonExpected();
256
+ }
257
+ }
258
+ }
259
+ if (text[i] === '}') {
260
+ output += '}';
261
+ i++;
262
+ } else {
263
+ // repair missing end bracket
264
+ output = insertBeforeLastWhitespace(output, '}');
265
+ }
266
+ return true;
267
+ }
268
+ return false;
269
+ }
270
+
271
+ /**
272
+ * Parse an array like '["item1", "item2", ...]'
273
+ */
274
+ function parseArray() {
275
+ if (text[i] === '[') {
276
+ output += '[';
277
+ i++;
278
+ parseWhitespaceAndSkipComments();
279
+
280
+ // repair: skip leading comma like in [,1,2,3]
281
+ if (skipCharacter(',')) {
282
+ parseWhitespaceAndSkipComments();
283
+ }
284
+ let initial = true;
285
+ while (i < text.length && text[i] !== ']') {
286
+ if (!initial) {
287
+ const processedComma = parseCharacter(',');
288
+ if (!processedComma) {
289
+ // repair missing comma
290
+ output = insertBeforeLastWhitespace(output, ',');
291
+ }
292
+ } else {
293
+ initial = false;
294
+ }
295
+ skipEllipsis();
296
+ const processedValue = parseValue();
297
+ if (!processedValue) {
298
+ // repair trailing comma
299
+ output = stripLastOccurrence(output, ',');
300
+ break;
301
+ }
302
+ }
303
+ if (text[i] === ']') {
304
+ output += ']';
305
+ i++;
306
+ } else {
307
+ // repair missing closing array bracket
308
+ output = insertBeforeLastWhitespace(output, ']');
309
+ }
310
+ return true;
311
+ }
312
+ return false;
313
+ }
314
+
315
+ /**
316
+ * Parse and repair Newline Delimited JSON (NDJSON):
317
+ * multiple JSON objects separated by a newline character
318
+ */
319
+ function parseNewlineDelimitedJSON() {
320
+ // repair NDJSON
321
+ let initial = true;
322
+ let processedValue = true;
323
+ while (processedValue) {
324
+ if (!initial) {
325
+ // parse optional comma, insert when missing
326
+ const processedComma = parseCharacter(',');
327
+ if (!processedComma) {
328
+ // repair: add missing comma
329
+ output = insertBeforeLastWhitespace(output, ',');
330
+ }
331
+ } else {
332
+ initial = false;
333
+ }
334
+ processedValue = parseValue();
335
+ }
336
+ if (!processedValue) {
337
+ // repair: remove trailing comma
338
+ output = stripLastOccurrence(output, ',');
339
+ }
340
+
341
+ // repair: wrap the output inside array brackets
342
+ output = `[\n${output}\n]`;
343
+ }
344
+
345
+ /**
346
+ * Parse a string enclosed by double quotes "...". Can contain escaped quotes
347
+ * Repair strings enclosed in single quotes or special quotes
348
+ * Repair an escaped string
349
+ *
350
+ * The function can run in two stages:
351
+ * - First, it assumes the string has a valid end quote
352
+ * - If it turns out that the string does not have a valid end quote followed
353
+ * by a delimiter (which should be the case), the function runs again in a
354
+ * more conservative way, stopping the string at the first next delimiter
355
+ * and fixing the string by inserting a quote there, or stopping at a
356
+ * stop index detected in the first iteration.
357
+ */
358
+ function parseString() {
359
+ let stopAtDelimiter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
360
+ let stopAtIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1;
361
+ let skipEscapeChars = text[i] === '\\';
362
+ if (skipEscapeChars) {
363
+ // repair: remove the first escape character
364
+ i++;
365
+ skipEscapeChars = true;
366
+ }
367
+ if (isQuote(text[i])) {
368
+ // double quotes are correct JSON,
369
+ // single quotes come from JavaScript for example, we assume it will have a correct single end quote too
370
+ // otherwise, we will match any double-quote-like start with a double-quote-like end,
371
+ // or any single-quote-like start with a single-quote-like end
372
+ const isEndQuote = isDoubleQuote(text[i]) ? isDoubleQuote : isSingleQuote(text[i]) ? isSingleQuote : isSingleQuoteLike(text[i]) ? isSingleQuoteLike : isDoubleQuoteLike;
373
+ const iBefore = i;
374
+ const oBefore = output.length;
375
+ let str = '"';
376
+ i++;
377
+ while (true) {
378
+ if (i >= text.length) {
379
+ // end of text, we are missing an end quote
380
+
381
+ const iPrev = prevNonWhitespaceIndex(i - 1);
382
+ if (!stopAtDelimiter && isDelimiter(text.charAt(iPrev))) {
383
+ // if the text ends with a delimiter, like ["hello],
384
+ // so the missing end quote should be inserted before this delimiter
385
+ // retry parsing the string, stopping at the first next delimiter
386
+ i = iBefore;
387
+ output = output.substring(0, oBefore);
388
+ return parseString(true);
389
+ }
390
+
391
+ // repair missing quote
392
+ str = insertBeforeLastWhitespace(str, '"');
393
+ output += str;
394
+ return true;
395
+ }
396
+ if (i === stopAtIndex) {
397
+ // use the stop index detected in the first iteration, and repair end quote
398
+ str = insertBeforeLastWhitespace(str, '"');
399
+ output += str;
400
+ return true;
401
+ }
402
+ if (isEndQuote(text[i])) {
403
+ // end quote
404
+ // let us check what is before and after the quote to verify whether this is a legit end quote
405
+ const iQuote = i;
406
+ const oQuote = str.length;
407
+ str += '"';
408
+ i++;
409
+ output += str;
410
+ parseWhitespaceAndSkipComments(false);
411
+ if (stopAtDelimiter || i >= text.length || isDelimiter(text[i]) || isQuote(text[i]) || isDigit(text[i])) {
412
+ // The quote is followed by the end of the text, a delimiter,
413
+ // or a next value. So the quote is indeed the end of the string.
414
+ parseConcatenatedString();
415
+ return true;
416
+ }
417
+ const iPrevChar = prevNonWhitespaceIndex(iQuote - 1);
418
+ const prevChar = text.charAt(iPrevChar);
419
+ if (prevChar === ',') {
420
+ // A comma followed by a quote, like '{"a":"b,c,"d":"e"}'.
421
+ // We assume that the quote is a start quote, and that the end quote
422
+ // should have been located right before the comma but is missing.
423
+ i = iBefore;
424
+ output = output.substring(0, oBefore);
425
+ return parseString(false, iPrevChar);
426
+ }
427
+ if (isDelimiter(prevChar)) {
428
+ // This is not the right end quote: it is preceded by a delimiter,
429
+ // and NOT followed by a delimiter. So, there is an end quote missing
430
+ // parse the string again and then stop at the first next delimiter
431
+ i = iBefore;
432
+ output = output.substring(0, oBefore);
433
+ return parseString(true);
434
+ }
435
+
436
+ // revert to right after the quote but before any whitespace, and continue parsing the string
437
+ output = output.substring(0, oBefore);
438
+ i = iQuote + 1;
439
+
440
+ // repair unescaped quote
441
+ str = `${str.substring(0, oQuote)}\\${str.substring(oQuote)}`;
442
+ } else if (stopAtDelimiter && isUnquotedStringDelimiter(text[i])) {
443
+ // we're in the mode to stop the string at the first delimiter
444
+ // because there is an end quote missing
445
+
446
+ // test start of an url like "https://..." (this would be parsed as a comment)
447
+ if (text[i - 1] === ':' && regexUrlStart.test(text.substring(iBefore + 1, i + 2))) {
448
+ while (i < text.length && regexUrlChar.test(text[i])) {
449
+ str += text[i];
450
+ i++;
451
+ }
452
+ }
453
+
454
+ // repair missing quote
455
+ str = insertBeforeLastWhitespace(str, '"');
456
+ output += str;
457
+ parseConcatenatedString();
458
+ return true;
459
+ } else if (text[i] === '\\') {
460
+ // handle escaped content like \n or \u2605
461
+ const char = text.charAt(i + 1);
462
+ const escapeChar = escapeCharacters[char];
463
+ if (escapeChar !== undefined) {
464
+ str += text.slice(i, i + 2);
465
+ i += 2;
466
+ } else if (char === 'u') {
467
+ let j = 2;
468
+ while (j < 6 && isHex(text[i + j])) {
469
+ j++;
470
+ }
471
+ if (j === 6) {
472
+ str += text.slice(i, i + 6);
473
+ i += 6;
474
+ } else if (i + j >= text.length) {
475
+ // repair invalid or truncated unicode char at the end of the text
476
+ // by removing the unicode char and ending the string here
477
+ i = text.length;
478
+ } else {
479
+ throwInvalidUnicodeCharacter();
480
+ }
481
+ } else {
482
+ // repair invalid escape character: remove it
483
+ str += char;
484
+ i += 2;
485
+ }
486
+ } else {
487
+ // handle regular characters
488
+ const char = text.charAt(i);
489
+ if (char === '"' && text[i - 1] !== '\\') {
490
+ // repair unescaped double quote
491
+ str += `\\${char}`;
492
+ i++;
493
+ } else if (isControlCharacter(char)) {
494
+ // unescaped control character
495
+ str += controlCharacters[char];
496
+ i++;
497
+ } else {
498
+ if (!isValidStringCharacter(char)) {
499
+ throwInvalidCharacter(char);
500
+ }
501
+ str += char;
502
+ i++;
503
+ }
504
+ }
505
+ if (skipEscapeChars) {
506
+ // repair: skipped escape character (nothing to do)
507
+ skipEscapeCharacter();
508
+ }
509
+ }
510
+ }
511
+ return false;
512
+ }
513
+
514
+ /**
515
+ * Repair concatenated strings like "hello" + "world", change this into "helloworld"
516
+ */
517
+ function parseConcatenatedString() {
518
+ let processed = false;
519
+ parseWhitespaceAndSkipComments();
520
+ while (text[i] === '+') {
521
+ processed = true;
522
+ i++;
523
+ parseWhitespaceAndSkipComments();
524
+
525
+ // repair: remove the end quote of the first string
526
+ output = stripLastOccurrence(output, '"', true);
527
+ const start = output.length;
528
+ const parsedStr = parseString();
529
+ if (parsedStr) {
530
+ // repair: remove the start quote of the second string
531
+ output = removeAtIndex(output, start, 1);
532
+ } else {
533
+ // repair: remove the + because it is not followed by a string
534
+ output = insertBeforeLastWhitespace(output, '"');
535
+ }
536
+ }
537
+ return processed;
538
+ }
539
+
540
+ /**
541
+ * Parse a number like 2.4 or 2.4e6
542
+ */
543
+ function parseNumber() {
544
+ const start = i;
545
+ if (text[i] === '-') {
546
+ i++;
547
+ if (atEndOfNumber()) {
548
+ repairNumberEndingWithNumericSymbol(start);
549
+ return true;
550
+ }
551
+ if (!isDigit(text[i])) {
552
+ i = start;
553
+ return false;
554
+ }
555
+ }
556
+
557
+ // Note that in JSON leading zeros like "00789" are not allowed.
558
+ // We will allow all leading zeros here though and at the end of parseNumber
559
+ // check against trailing zeros and repair that if needed.
560
+ // Leading zeros can have meaning, so we should not clear them.
561
+ while (isDigit(text[i])) {
562
+ i++;
563
+ }
564
+ if (text[i] === '.') {
565
+ i++;
566
+ if (atEndOfNumber()) {
567
+ repairNumberEndingWithNumericSymbol(start);
568
+ return true;
569
+ }
570
+ if (!isDigit(text[i])) {
571
+ i = start;
572
+ return false;
573
+ }
574
+ while (isDigit(text[i])) {
575
+ i++;
576
+ }
577
+ }
578
+ if (text[i] === 'e' || text[i] === 'E') {
579
+ i++;
580
+ if (text[i] === '-' || text[i] === '+') {
581
+ i++;
582
+ }
583
+ if (atEndOfNumber()) {
584
+ repairNumberEndingWithNumericSymbol(start);
585
+ return true;
586
+ }
587
+ if (!isDigit(text[i])) {
588
+ i = start;
589
+ return false;
590
+ }
591
+ while (isDigit(text[i])) {
592
+ i++;
593
+ }
594
+ }
595
+
596
+ // if we're not at the end of the number by this point, allow this to be parsed as another type
597
+ if (!atEndOfNumber()) {
598
+ i = start;
599
+ return false;
600
+ }
601
+ if (i > start) {
602
+ // repair a number with leading zeros like "00789"
603
+ const num = text.slice(start, i);
604
+ const hasInvalidLeadingZero = /^0\d/.test(num);
605
+ output += hasInvalidLeadingZero ? `"${num}"` : num;
606
+ return true;
607
+ }
608
+ return false;
609
+ }
610
+
611
+ /**
612
+ * Parse keywords true, false, null
613
+ * Repair Python keywords True, False, None
614
+ */
615
+ function parseKeywords() {
616
+ return parseKeyword('true', 'true') || parseKeyword('false', 'false') || parseKeyword('null', 'null') ||
617
+ // repair Python keywords True, False, None
618
+ parseKeyword('True', 'true') || parseKeyword('False', 'false') || parseKeyword('None', 'null');
619
+ }
620
+ function parseKeyword(name, value) {
621
+ if (text.slice(i, i + name.length) === name) {
622
+ output += value;
623
+ i += name.length;
624
+ return true;
625
+ }
626
+ return false;
627
+ }
628
+
629
+ /**
630
+ * Repair an unquoted string by adding quotes around it
631
+ * Repair a MongoDB function call like NumberLong("2")
632
+ * Repair a JSONP function call like callback({...});
633
+ */
634
+ function parseUnquotedString(isKey) {
635
+ // note that the symbol can end with whitespaces: we stop at the next delimiter
636
+ // also, note that we allow strings to contain a slash / in order to support repairing regular expressions
637
+ const start = i;
638
+ if (isFunctionNameCharStart(text[i])) {
639
+ while (i < text.length && isFunctionNameChar(text[i])) {
640
+ i++;
641
+ }
642
+ let j = i;
643
+ while (isWhitespace(text, j)) {
644
+ j++;
645
+ }
646
+ if (text[j] === '(') {
647
+ // repair a MongoDB function call like NumberLong("2")
648
+ // repair a JSONP function call like callback({...});
649
+ i = j + 1;
650
+ parseValue();
651
+ if (text[i] === ')') {
652
+ // repair: skip close bracket of function call
653
+ i++;
654
+ if (text[i] === ';') {
655
+ // repair: skip semicolon after JSONP call
656
+ i++;
657
+ }
658
+ }
659
+ return true;
660
+ }
661
+ }
662
+ while (i < text.length && !isUnquotedStringDelimiter(text[i]) && !isQuote(text[i]) && (!isKey || text[i] !== ':')) {
663
+ i++;
664
+ }
665
+
666
+ // test start of an url like "https://..." (this would be parsed as a comment)
667
+ if (text[i - 1] === ':' && regexUrlStart.test(text.substring(start, i + 2))) {
668
+ while (i < text.length && regexUrlChar.test(text[i])) {
669
+ i++;
670
+ }
671
+ }
672
+ if (i > start) {
673
+ // repair unquoted string
674
+ // also, repair undefined into null
675
+
676
+ // first, go back to prevent getting trailing whitespaces in the string
677
+ while (isWhitespace(text, i - 1) && i > 0) {
678
+ i--;
679
+ }
680
+ const symbol = text.slice(start, i);
681
+ output += symbol === 'undefined' ? 'null' : JSON.stringify(symbol);
682
+ if (text[i] === '"') {
683
+ // we had a missing start quote, but now we encountered the end quote, so we can skip that one
684
+ i++;
685
+ }
686
+ return true;
687
+ }
688
+ }
689
+ function parseRegex() {
690
+ if (text[i] === '/') {
691
+ const start = i;
692
+ i++;
693
+ while (i < text.length && (text[i] !== '/' || text[i - 1] === '\\')) {
694
+ i++;
695
+ }
696
+ i++;
697
+ output += `"${text.substring(start, i)}"`;
698
+ return true;
699
+ }
700
+ }
701
+ function prevNonWhitespaceIndex(start) {
702
+ let prev = start;
703
+ while (prev > 0 && isWhitespace(text, prev)) {
704
+ prev--;
705
+ }
706
+ return prev;
707
+ }
708
+ function atEndOfNumber() {
709
+ return i >= text.length || isDelimiter(text[i]) || isWhitespace(text, i);
710
+ }
711
+ function repairNumberEndingWithNumericSymbol(start) {
712
+ // repair numbers cut off at the end
713
+ // this will only be called when we end after a '.', '-', or 'e' and does not
714
+ // change the number more than it needs to make it valid JSON
715
+ output += `${text.slice(start, i)}0`;
716
+ }
717
+ function throwInvalidCharacter(char) {
718
+ throw new JSONRepairError(`Invalid character ${JSON.stringify(char)}`, i);
719
+ }
720
+ function throwUnexpectedCharacter() {
721
+ throw new JSONRepairError(`Unexpected character ${JSON.stringify(text[i])}`, i);
722
+ }
723
+ function throwUnexpectedEnd() {
724
+ throw new JSONRepairError('Unexpected end of json string', text.length);
725
+ }
726
+ function throwObjectKeyExpected() {
727
+ throw new JSONRepairError('Object key expected', i);
728
+ }
729
+ function throwColonExpected() {
730
+ throw new JSONRepairError('Colon expected', i);
731
+ }
732
+ function throwInvalidUnicodeCharacter() {
733
+ const chars = text.slice(i, i + 6);
734
+ throw new JSONRepairError(`Invalid unicode character "${chars}"`, i);
735
+ }
736
+ }
737
+ function atEndOfBlockComment(text, i) {
738
+ return text[i] === '*' && text[i + 1] === '/';
739
+ }
740
+ //# sourceMappingURL=jsonrepair.js.map