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