qwenproxy-cli 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +14 -14
- package/README.md +906 -906
- package/bin/qwenproxy.js +5 -1
- package/package.json +77 -78
- package/src/api/error-classifier.ts +159 -159
- package/src/api/error-helpers.ts +118 -118
- package/src/api/models.ts +261 -261
- package/src/api/server.ts +860 -859
- package/src/cache/memory-cache.ts +385 -385
- package/src/clean-cache.ts +204 -204
- package/src/core/account-concurrency.ts +671 -671
- package/src/core/account-manager.ts +301 -297
- package/src/core/account-priority.ts +163 -163
- package/src/core/accounts.ts +186 -186
- package/src/core/config.ts +383 -383
- package/src/core/crypto-utils.ts +79 -79
- package/src/core/database.ts +276 -276
- package/src/core/errors.ts +118 -118
- package/src/core/logger.ts +269 -269
- package/src/core/memory-usage.ts +84 -84
- package/src/core/metrics.ts +291 -291
- package/src/core/model-alias.ts +77 -77
- package/src/core/model-registry.ts +544 -544
- package/src/core/mutex.ts +119 -119
- package/src/core/paths.ts +199 -199
- package/src/core/prompt-limits.ts +214 -214
- package/src/core/reasoning-effort.ts +102 -102
- package/src/core/stream-registry.ts +96 -96
- package/src/core/waf-isolation.ts +117 -117
- package/src/core/watchdog.ts +195 -195
- package/src/delete-chats.ts +23 -23
- package/src/index.ts +65 -64
- package/src/login.ts +147 -147
- package/src/reset-cooldowns.ts +11 -11
- package/src/routes/anthropic/index.ts +355 -355
- package/src/routes/anthropic/translate.ts +522 -522
- package/src/routes/anthropic/types.ts +154 -154
- package/src/routes/anthropic/validation.ts +144 -144
- package/src/routes/chat/account.ts +1817 -1817
- package/src/routes/chat/context.ts +241 -241
- package/src/routes/chat/errors.ts +85 -85
- package/src/routes/chat/helpers.ts +268 -268
- package/src/routes/chat/index.ts +618 -618
- package/src/routes/chat/media.ts +285 -285
- package/src/routes/chat/retry-policy.ts +754 -754
- package/src/routes/chat/stop.ts +98 -98
- package/src/routes/chat/streaming.ts +2710 -2710
- package/src/routes/chat/validation.ts +526 -526
- package/src/routes/chat.ts +2 -2
- package/src/routes/completions.ts +290 -290
- package/src/routes/images.ts +139 -139
- package/src/routes/responses/adapter.ts +503 -503
- package/src/routes/responses/index.ts +405 -405
- package/src/routes/responses/state.ts +230 -230
- package/src/routes/responses/streaming.ts +528 -528
- package/src/routes/responses/types.ts +285 -285
- package/src/routes/responses/validation.ts +202 -202
- package/src/routes/upload.ts +731 -731
- package/src/routes/videos.ts +214 -214
- package/src/services/auth-playwright.ts +173 -173
- package/src/services/captcha-coordinator.ts +161 -161
- package/src/services/captcha-solver.ts +553 -553
- package/src/services/chat-cleanup.ts +80 -80
- package/src/services/context-meter.ts +317 -317
- package/src/services/fingerprint.ts +242 -242
- package/src/services/human-behavior.ts +173 -173
- package/src/services/media-generation.ts +1748 -1748
- package/src/services/playwright.ts +2878 -2800
- package/src/services/qwen-chat-pool.ts +345 -345
- package/src/services/qwen-errors.ts +133 -133
- package/src/services/qwen-headers.ts +79 -79
- package/src/services/qwen-thread-state.ts +393 -393
- package/src/services/qwen-url.ts +19 -19
- package/src/services/qwen.ts +3126 -3126
- package/src/services/session-keeper.ts +88 -88
- package/src/services/token-estimation-metrics.ts +118 -118
- package/src/sync/claude-code.ts +75 -75
- package/src/sync/codex.ts +123 -123
- package/src/sync/index.ts +362 -362
- package/src/sync/omp.ts +105 -105
- package/src/sync/opencode.ts +214 -214
- package/src/sync/types.ts +53 -53
- package/src/sync/utils.ts +27 -27
- package/src/sync-clients.ts +189 -189
- package/src/tools/instructions.ts +137 -137
- package/src/tools/manifest.ts +81 -81
- package/src/tools/parser.ts +2989 -2989
- package/src/tools/toolcall-tags.ts +142 -142
- package/src/tui/app.ts +259 -264
- package/src/tui/index.ts +61 -61
- package/src/tui/markdown.ts +258 -258
- package/src/tui/proxy-client.ts +331 -326
- package/src/tui/screen.ts +294 -278
- package/src/tui/server-manager.ts +270 -270
- package/src/tui/theme.ts +432 -432
- package/src/tui/types.ts +33 -33
- package/src/tui/views/accounts-view.ts +656 -656
- package/src/tui/views/chat-view.ts +1018 -823
- package/src/tui/views/logs-view.ts +479 -413
- package/src/tui/views/status-view.ts +204 -204
- package/src/tui/views/storage-view.ts +304 -291
- package/src/tui/views/sync-view.ts +409 -409
- package/src/types/ali-oss.d.ts +32 -32
- package/src/update-cli.ts +121 -0
- package/src/utils/context-truncation.ts +84 -84
- package/src/utils/json.ts +380 -380
- package/src/utils/session-id.ts +37 -37
- package/src/utils/tool-call-guard.ts +84 -84
- package/src/utils/types.ts +109 -109
package/src/utils/json.ts
CHANGED
|
@@ -1,380 +1,380 @@
|
|
|
1
|
-
import { logger } from "../core/logger.js";
|
|
2
|
-
|
|
3
|
-
const isDebug = process.env.TOOLCALL_DEBUG === "1";
|
|
4
|
-
|
|
5
|
-
function sanitizeAndBalance(input: string): {
|
|
6
|
-
result: string;
|
|
7
|
-
openBraces: number;
|
|
8
|
-
openBrackets: number;
|
|
9
|
-
recoveredUnclosedString: boolean;
|
|
10
|
-
/** Opening tokens `{`/`[` in the order they were opened (LIFO close order). */
|
|
11
|
-
openStack: string[];
|
|
12
|
-
} {
|
|
13
|
-
let out = "";
|
|
14
|
-
let openBraces = 0;
|
|
15
|
-
let openBrackets = 0;
|
|
16
|
-
let openStack: string[] = [];
|
|
17
|
-
let inString = false;
|
|
18
|
-
let escaped = false;
|
|
19
|
-
|
|
20
|
-
for (let i = 0; i < input.length; i++) {
|
|
21
|
-
const char = input[i];
|
|
22
|
-
if (escaped) {
|
|
23
|
-
const validEscapes = ["n", "r", "t", "u", '"', "\\", "/"];
|
|
24
|
-
if (validEscapes.includes(char)) {
|
|
25
|
-
if (char === "u") {
|
|
26
|
-
const next4 = input.substring(i + 1, i + 5);
|
|
27
|
-
out += /^[0-9a-fA-F]{4}$/.test(next4) ? "\\" + char : "\\\\" + char;
|
|
28
|
-
} else if (["n", "r", "t"].includes(char)) {
|
|
29
|
-
// Local check: only double-escape if the 2 chars before this
|
|
30
|
-
// backslash form a Windows drive letter + colon (e.g. C:\, D:/).
|
|
31
|
-
// Scanning the whole input causes false positives with URLs
|
|
32
|
-
// like https:// or paths embedded elsewhere in the string.
|
|
33
|
-
const isWinPath = i >= 2 && /[a-zA-Z]:/.test(input.substring(i - 2, i));
|
|
34
|
-
const nextChar = input[i + 1] || "";
|
|
35
|
-
out +=
|
|
36
|
-
isWinPath && /^[a-zA-Z0-9]/.test(nextChar)
|
|
37
|
-
? "\\\\" + char
|
|
38
|
-
: "\\" + char;
|
|
39
|
-
} else {
|
|
40
|
-
out += "\\" + char;
|
|
41
|
-
}
|
|
42
|
-
} else {
|
|
43
|
-
out += "\\\\" + char;
|
|
44
|
-
}
|
|
45
|
-
escaped = false;
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
if (char === "\\") {
|
|
49
|
-
escaped = true;
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
if (char === '"') {
|
|
53
|
-
inString = !inString;
|
|
54
|
-
out += char;
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
if (inString) {
|
|
58
|
-
if (char === "\n") out += "\\n";
|
|
59
|
-
else if (char === "\r") out += "\\r";
|
|
60
|
-
else if (char === "\t") out += "\\t";
|
|
61
|
-
else if (char.charCodeAt(0) < 32)
|
|
62
|
-
out += "\\u" + char.charCodeAt(0).toString(16).padStart(4, "0");
|
|
63
|
-
else out += char;
|
|
64
|
-
} else {
|
|
65
|
-
out += char;
|
|
66
|
-
if (char === "{") {
|
|
67
|
-
openBraces++;
|
|
68
|
-
openStack.push("{");
|
|
69
|
-
}
|
|
70
|
-
if (char === "}") {
|
|
71
|
-
openBraces--;
|
|
72
|
-
openStack.pop();
|
|
73
|
-
}
|
|
74
|
-
if (char === "[") {
|
|
75
|
-
openBrackets++;
|
|
76
|
-
openStack.push("[");
|
|
77
|
-
}
|
|
78
|
-
if (char === "]") {
|
|
79
|
-
openBrackets--;
|
|
80
|
-
openStack.pop();
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
let recoveredUnclosedString = false;
|
|
86
|
-
if (escaped) {
|
|
87
|
-
out += "\\";
|
|
88
|
-
}
|
|
89
|
-
if (inString) {
|
|
90
|
-
out += '"';
|
|
91
|
-
recoveredUnclosedString = true;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return { result: out, openBraces, openBrackets, recoveredUnclosedString, openStack };
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Close unterminated containers in LIFO order (reverse of how they were
|
|
99
|
-
* opened). Appending all `]` then all `}` (the old counting-only approach)
|
|
100
|
-
* produces invalid JSON whenever an array is opened and then an object is
|
|
101
|
-
* opened inside it before truncation, e.g.
|
|
102
|
-
* `{"a":[{"old_text":"start` needs `}],}}` and not `]}}}`.
|
|
103
|
-
*/
|
|
104
|
-
function closeBraces(
|
|
105
|
-
input: string,
|
|
106
|
-
openBraces: number,
|
|
107
|
-
openBrackets: number,
|
|
108
|
-
openStack: string[],
|
|
109
|
-
): string {
|
|
110
|
-
let out = input;
|
|
111
|
-
for (let i = openStack.length - 1; i >= 0; i--) {
|
|
112
|
-
out += openStack[i] === "{" ? "}" : "]";
|
|
113
|
-
}
|
|
114
|
-
if (openBraces > 0 || openBrackets > 0) {
|
|
115
|
-
// Open tokens shorter than the counted opens (no stack entries for them)
|
|
116
|
-
// cannot happen since the stack mirrors the counts; keep the historical
|
|
117
|
-
// count-based close as a safety net for callers without a stack.
|
|
118
|
-
if (openStack.length === 0) {
|
|
119
|
-
if (openBrackets > 0) out += "]".repeat(openBrackets);
|
|
120
|
-
if (openBraces > 0) out += "}".repeat(openBraces);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
return out;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Fixes missing opening quotes in JSON values.
|
|
128
|
-
* Handles cases like: {"key": value_without_quotes"}
|
|
129
|
-
* Upstream: a63f054, 9328bde
|
|
130
|
-
*/
|
|
131
|
-
function fixMissingOpeningQuotes(input: string): string {
|
|
132
|
-
let out = input;
|
|
133
|
-
let prev: string;
|
|
134
|
-
do {
|
|
135
|
-
prev = out;
|
|
136
|
-
// Pattern 1: {"key": value"} or {key: value"}
|
|
137
|
-
out = out.replace(
|
|
138
|
-
/([{,[]\s*"[a-zA-Z_][\w]*"\s*:\s*)([^"\s,}\]][^"\n]*?)"([\s,}\]])/g,
|
|
139
|
-
'$1"$2"$3',
|
|
140
|
-
);
|
|
141
|
-
out = out.replace(
|
|
142
|
-
/([{,[]\s*[a-zA-Z_][\w]*\s*:\s*)([^"\s,}\]][^"\n]*?)"([\s,}\]])/g,
|
|
143
|
-
'$1"$2"$3',
|
|
144
|
-
);
|
|
145
|
-
// Pattern 2: {: value"} - upstream 9328bde
|
|
146
|
-
out = out.replace(/(:\s*)([A-Za-z_][\w.-]*?)"([\s,}\]])/g, '$1"$2"$3');
|
|
147
|
-
} while (out !== prev);
|
|
148
|
-
return out;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export function robustParseJSON(str: string): any {
|
|
152
|
-
if (isDebug) {
|
|
153
|
-
logger.debug("[json] robustParseJSON: starting", {
|
|
154
|
-
inputLength: str.length,
|
|
155
|
-
inputPreview: str.substring(0, 200),
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
let sanitized = str.trim();
|
|
160
|
-
sanitized = sanitized
|
|
161
|
-
.replace(/^```json\s*/, "")
|
|
162
|
-
.replace(/```$/, "")
|
|
163
|
-
.trim();
|
|
164
|
-
|
|
165
|
-
const firstBrace = sanitized.indexOf("{");
|
|
166
|
-
if (firstBrace === -1) {
|
|
167
|
-
if (isDebug) {
|
|
168
|
-
logger.debug("[json] robustParseJSON: no opening brace found");
|
|
169
|
-
}
|
|
170
|
-
return null;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
let jsonPart = sanitized.substring(firstBrace);
|
|
174
|
-
try {
|
|
175
|
-
const result = JSON.parse(jsonPart);
|
|
176
|
-
if (isDebug) {
|
|
177
|
-
logger.debug("[json] robustParseJSON: direct parse succeeded", {
|
|
178
|
-
resultType: typeof result,
|
|
179
|
-
resultPreview: JSON.stringify(result).substring(0, 200),
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
return result;
|
|
183
|
-
} catch (e) {
|
|
184
|
-
if (isDebug) {
|
|
185
|
-
logger.debug("[json] robustParseJSON: direct parse failed", {
|
|
186
|
-
error: e instanceof Error ? e.message : String(e),
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
// Fix double-escaped quotes (e.g., \\" -> ") only after direct parse
|
|
190
|
-
// fails, to avoid corrupting valid JSON with legitimate \\ sequences.
|
|
191
|
-
jsonPart = jsonPart.replace(/\\\\"/g, '\\"');
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
let currentJson = jsonPart.replace(
|
|
195
|
-
/([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)(\s*:)/g,
|
|
196
|
-
'$1"$2"$3',
|
|
197
|
-
);
|
|
198
|
-
currentJson = currentJson.replace(
|
|
199
|
-
/([{,]\s*)"([a-zA-Z0-9_]+)"\s*:\s*"\2"\s*:/g,
|
|
200
|
-
'$1"$2":',
|
|
201
|
-
);
|
|
202
|
-
currentJson = currentJson.replace(
|
|
203
|
-
/([{,]\s*)([a-zA-Z0-9_]+)\s*:\s*\2\s*:/g,
|
|
204
|
-
"$1$2:",
|
|
205
|
-
);
|
|
206
|
-
|
|
207
|
-
// Fix missing opening quotes in values (upstream: a63f054, 9328bde)
|
|
208
|
-
currentJson = fixMissingOpeningQuotes(currentJson);
|
|
209
|
-
|
|
210
|
-
// Handle unquoted string values after colon (e.g., "command":export CI=true)
|
|
211
|
-
// Matches: "key":value_without_quotes until comma, closing brace, or end
|
|
212
|
-
currentJson = currentJson.replace(
|
|
213
|
-
/"([a-zA-Z0-9_]+)":\s*([^"\s{\[\],}][^,}\]]*)/g,
|
|
214
|
-
(match, key, value) => {
|
|
215
|
-
// Don't quote if it's a number, boolean, or null
|
|
216
|
-
if (/^(true|false|null|\d+(\.\d+)?)$/.test(value.trim())) {
|
|
217
|
-
return match;
|
|
218
|
-
}
|
|
219
|
-
return `"${key}":"${value.trim()}"`;
|
|
220
|
-
},
|
|
221
|
-
);
|
|
222
|
-
|
|
223
|
-
try {
|
|
224
|
-
const result = JSON.parse(currentJson);
|
|
225
|
-
if (isDebug) {
|
|
226
|
-
logger.debug("[json] robustParseJSON: quote-fix parse succeeded", {
|
|
227
|
-
resultType: typeof result,
|
|
228
|
-
resultPreview: JSON.stringify(result).substring(0, 200),
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
return result;
|
|
232
|
-
} catch (e) {
|
|
233
|
-
if (isDebug) {
|
|
234
|
-
logger.debug("[json] robustParseJSON: quote-fix parse failed", {
|
|
235
|
-
error: e instanceof Error ? e.message : String(e),
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
/* continue */
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
let cleaned = currentJson.trim();
|
|
242
|
-
while (
|
|
243
|
-
cleaned.length > 0 &&
|
|
244
|
-
!/[}\]"0-9a-z]/i.test(cleaned[cleaned.length - 1])
|
|
245
|
-
) {
|
|
246
|
-
cleaned = cleaned.slice(0, -1).trim();
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const {
|
|
250
|
-
result: fixedJson,
|
|
251
|
-
openBraces,
|
|
252
|
-
openBrackets,
|
|
253
|
-
recoveredUnclosedString,
|
|
254
|
-
openStack: fixedJsonOpenStack,
|
|
255
|
-
} = sanitizeAndBalance(cleaned);
|
|
256
|
-
|
|
257
|
-
if (isDebug && recoveredUnclosedString) {
|
|
258
|
-
logger.debug(
|
|
259
|
-
"[json] robustParseJSON: recovered unclosed string at end of input",
|
|
260
|
-
{
|
|
261
|
-
originalPreview: cleaned.substring(0, 150),
|
|
262
|
-
},
|
|
263
|
-
);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
let lastBalancedIndex = -1;
|
|
267
|
-
|
|
268
|
-
{
|
|
269
|
-
let ob = 0,
|
|
270
|
-
bk = 0,
|
|
271
|
-
ins = false,
|
|
272
|
-
esc = false;
|
|
273
|
-
for (let i = 0; i < fixedJson.length; i++) {
|
|
274
|
-
const c = fixedJson[i];
|
|
275
|
-
if (esc) {
|
|
276
|
-
esc = false;
|
|
277
|
-
continue;
|
|
278
|
-
}
|
|
279
|
-
if (c === "\\") {
|
|
280
|
-
esc = true;
|
|
281
|
-
continue;
|
|
282
|
-
}
|
|
283
|
-
if (c === '"') {
|
|
284
|
-
ins = !ins;
|
|
285
|
-
continue;
|
|
286
|
-
}
|
|
287
|
-
if (!ins) {
|
|
288
|
-
if (c === "{") ob++;
|
|
289
|
-
if (c === "}") ob--;
|
|
290
|
-
if (c === "[") bk++;
|
|
291
|
-
if (c === "]") bk--;
|
|
292
|
-
if (ob === 0 && bk === 0) lastBalancedIndex = i;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
let tempJson = fixedJson;
|
|
298
|
-
if (
|
|
299
|
-
lastBalancedIndex !== -1 &&
|
|
300
|
-
(openBraces !== 0 ||
|
|
301
|
-
openBrackets !== 0 ||
|
|
302
|
-
fixedJson.length > lastBalancedIndex + 1)
|
|
303
|
-
) {
|
|
304
|
-
tempJson = fixedJson.substring(0, lastBalancedIndex + 1);
|
|
305
|
-
if (isDebug) {
|
|
306
|
-
logger.debug("[json] robustParseJSON: truncated to balanced index", {
|
|
307
|
-
lastBalancedIndex,
|
|
308
|
-
originalLength: fixedJson.length,
|
|
309
|
-
truncatedLength: tempJson.length,
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
} else if (openBraces > 0 || openBrackets > 0) {
|
|
313
|
-
tempJson = closeBraces(
|
|
314
|
-
fixedJson,
|
|
315
|
-
openBraces,
|
|
316
|
-
openBrackets,
|
|
317
|
-
fixedJsonOpenStack,
|
|
318
|
-
);
|
|
319
|
-
if (isDebug) {
|
|
320
|
-
logger.debug("[json] robustParseJSON: closed braces", {
|
|
321
|
-
openBraces,
|
|
322
|
-
openBrackets,
|
|
323
|
-
resultLength: tempJson.length,
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
try {
|
|
329
|
-
const result = JSON.parse(tempJson);
|
|
330
|
-
if (isDebug) {
|
|
331
|
-
logger.debug("[json] robustParseJSON: balanced-parse succeeded", {
|
|
332
|
-
resultType: typeof result,
|
|
333
|
-
resultPreview: JSON.stringify(result).substring(0, 200),
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
return result;
|
|
337
|
-
} catch (e) {
|
|
338
|
-
if (isDebug) {
|
|
339
|
-
logger.debug("[json] robustParseJSON: balanced-parse failed", {
|
|
340
|
-
error: e instanceof Error ? e.message : String(e),
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
let aggressive = fixedJson.trim();
|
|
344
|
-
if (aggressive.endsWith(",")) aggressive = aggressive.slice(0, -1);
|
|
345
|
-
const {
|
|
346
|
-
result: aggFixed,
|
|
347
|
-
openBraces: ob,
|
|
348
|
-
openBrackets: bk,
|
|
349
|
-
recoveredUnclosedString: aggRecovered,
|
|
350
|
-
openStack: aggOpenStack,
|
|
351
|
-
} = sanitizeAndBalance(aggressive);
|
|
352
|
-
|
|
353
|
-
if (isDebug && aggRecovered) {
|
|
354
|
-
logger.debug(
|
|
355
|
-
"[json] robustParseJSON: aggressive recovery of unclosed string",
|
|
356
|
-
{
|
|
357
|
-
originalPreview: aggressive.substring(0, 150),
|
|
358
|
-
},
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
try {
|
|
363
|
-
const result = JSON.parse(closeBraces(aggFixed, ob, bk, aggOpenStack));
|
|
364
|
-
if (isDebug) {
|
|
365
|
-
logger.debug("[json] robustParseJSON: aggressive-parse succeeded", {
|
|
366
|
-
resultType: typeof result,
|
|
367
|
-
resultPreview: JSON.stringify(result).substring(0, 200),
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
return result;
|
|
371
|
-
} catch {
|
|
372
|
-
if (isDebug) {
|
|
373
|
-
logger.debug("[json] robustParseJSON: all parse attempts failed", {
|
|
374
|
-
originalError: e instanceof Error ? e.message : String(e),
|
|
375
|
-
});
|
|
376
|
-
}
|
|
377
|
-
throw e;
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
}
|
|
1
|
+
import { logger } from "../core/logger.js";
|
|
2
|
+
|
|
3
|
+
const isDebug = process.env.TOOLCALL_DEBUG === "1";
|
|
4
|
+
|
|
5
|
+
function sanitizeAndBalance(input: string): {
|
|
6
|
+
result: string;
|
|
7
|
+
openBraces: number;
|
|
8
|
+
openBrackets: number;
|
|
9
|
+
recoveredUnclosedString: boolean;
|
|
10
|
+
/** Opening tokens `{`/`[` in the order they were opened (LIFO close order). */
|
|
11
|
+
openStack: string[];
|
|
12
|
+
} {
|
|
13
|
+
let out = "";
|
|
14
|
+
let openBraces = 0;
|
|
15
|
+
let openBrackets = 0;
|
|
16
|
+
let openStack: string[] = [];
|
|
17
|
+
let inString = false;
|
|
18
|
+
let escaped = false;
|
|
19
|
+
|
|
20
|
+
for (let i = 0; i < input.length; i++) {
|
|
21
|
+
const char = input[i];
|
|
22
|
+
if (escaped) {
|
|
23
|
+
const validEscapes = ["n", "r", "t", "u", '"', "\\", "/"];
|
|
24
|
+
if (validEscapes.includes(char)) {
|
|
25
|
+
if (char === "u") {
|
|
26
|
+
const next4 = input.substring(i + 1, i + 5);
|
|
27
|
+
out += /^[0-9a-fA-F]{4}$/.test(next4) ? "\\" + char : "\\\\" + char;
|
|
28
|
+
} else if (["n", "r", "t"].includes(char)) {
|
|
29
|
+
// Local check: only double-escape if the 2 chars before this
|
|
30
|
+
// backslash form a Windows drive letter + colon (e.g. C:\, D:/).
|
|
31
|
+
// Scanning the whole input causes false positives with URLs
|
|
32
|
+
// like https:// or paths embedded elsewhere in the string.
|
|
33
|
+
const isWinPath = i >= 2 && /[a-zA-Z]:/.test(input.substring(i - 2, i));
|
|
34
|
+
const nextChar = input[i + 1] || "";
|
|
35
|
+
out +=
|
|
36
|
+
isWinPath && /^[a-zA-Z0-9]/.test(nextChar)
|
|
37
|
+
? "\\\\" + char
|
|
38
|
+
: "\\" + char;
|
|
39
|
+
} else {
|
|
40
|
+
out += "\\" + char;
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
out += "\\\\" + char;
|
|
44
|
+
}
|
|
45
|
+
escaped = false;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (char === "\\") {
|
|
49
|
+
escaped = true;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (char === '"') {
|
|
53
|
+
inString = !inString;
|
|
54
|
+
out += char;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (inString) {
|
|
58
|
+
if (char === "\n") out += "\\n";
|
|
59
|
+
else if (char === "\r") out += "\\r";
|
|
60
|
+
else if (char === "\t") out += "\\t";
|
|
61
|
+
else if (char.charCodeAt(0) < 32)
|
|
62
|
+
out += "\\u" + char.charCodeAt(0).toString(16).padStart(4, "0");
|
|
63
|
+
else out += char;
|
|
64
|
+
} else {
|
|
65
|
+
out += char;
|
|
66
|
+
if (char === "{") {
|
|
67
|
+
openBraces++;
|
|
68
|
+
openStack.push("{");
|
|
69
|
+
}
|
|
70
|
+
if (char === "}") {
|
|
71
|
+
openBraces--;
|
|
72
|
+
openStack.pop();
|
|
73
|
+
}
|
|
74
|
+
if (char === "[") {
|
|
75
|
+
openBrackets++;
|
|
76
|
+
openStack.push("[");
|
|
77
|
+
}
|
|
78
|
+
if (char === "]") {
|
|
79
|
+
openBrackets--;
|
|
80
|
+
openStack.pop();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let recoveredUnclosedString = false;
|
|
86
|
+
if (escaped) {
|
|
87
|
+
out += "\\";
|
|
88
|
+
}
|
|
89
|
+
if (inString) {
|
|
90
|
+
out += '"';
|
|
91
|
+
recoveredUnclosedString = true;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return { result: out, openBraces, openBrackets, recoveredUnclosedString, openStack };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Close unterminated containers in LIFO order (reverse of how they were
|
|
99
|
+
* opened). Appending all `]` then all `}` (the old counting-only approach)
|
|
100
|
+
* produces invalid JSON whenever an array is opened and then an object is
|
|
101
|
+
* opened inside it before truncation, e.g.
|
|
102
|
+
* `{"a":[{"old_text":"start` needs `}],}}` and not `]}}}`.
|
|
103
|
+
*/
|
|
104
|
+
function closeBraces(
|
|
105
|
+
input: string,
|
|
106
|
+
openBraces: number,
|
|
107
|
+
openBrackets: number,
|
|
108
|
+
openStack: string[],
|
|
109
|
+
): string {
|
|
110
|
+
let out = input;
|
|
111
|
+
for (let i = openStack.length - 1; i >= 0; i--) {
|
|
112
|
+
out += openStack[i] === "{" ? "}" : "]";
|
|
113
|
+
}
|
|
114
|
+
if (openBraces > 0 || openBrackets > 0) {
|
|
115
|
+
// Open tokens shorter than the counted opens (no stack entries for them)
|
|
116
|
+
// cannot happen since the stack mirrors the counts; keep the historical
|
|
117
|
+
// count-based close as a safety net for callers without a stack.
|
|
118
|
+
if (openStack.length === 0) {
|
|
119
|
+
if (openBrackets > 0) out += "]".repeat(openBrackets);
|
|
120
|
+
if (openBraces > 0) out += "}".repeat(openBraces);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Fixes missing opening quotes in JSON values.
|
|
128
|
+
* Handles cases like: {"key": value_without_quotes"}
|
|
129
|
+
* Upstream: a63f054, 9328bde
|
|
130
|
+
*/
|
|
131
|
+
function fixMissingOpeningQuotes(input: string): string {
|
|
132
|
+
let out = input;
|
|
133
|
+
let prev: string;
|
|
134
|
+
do {
|
|
135
|
+
prev = out;
|
|
136
|
+
// Pattern 1: {"key": value"} or {key: value"}
|
|
137
|
+
out = out.replace(
|
|
138
|
+
/([{,[]\s*"[a-zA-Z_][\w]*"\s*:\s*)([^"\s,}\]][^"\n]*?)"([\s,}\]])/g,
|
|
139
|
+
'$1"$2"$3',
|
|
140
|
+
);
|
|
141
|
+
out = out.replace(
|
|
142
|
+
/([{,[]\s*[a-zA-Z_][\w]*\s*:\s*)([^"\s,}\]][^"\n]*?)"([\s,}\]])/g,
|
|
143
|
+
'$1"$2"$3',
|
|
144
|
+
);
|
|
145
|
+
// Pattern 2: {: value"} - upstream 9328bde
|
|
146
|
+
out = out.replace(/(:\s*)([A-Za-z_][\w.-]*?)"([\s,}\]])/g, '$1"$2"$3');
|
|
147
|
+
} while (out !== prev);
|
|
148
|
+
return out;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function robustParseJSON(str: string): any {
|
|
152
|
+
if (isDebug) {
|
|
153
|
+
logger.debug("[json] robustParseJSON: starting", {
|
|
154
|
+
inputLength: str.length,
|
|
155
|
+
inputPreview: str.substring(0, 200),
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let sanitized = str.trim();
|
|
160
|
+
sanitized = sanitized
|
|
161
|
+
.replace(/^```json\s*/, "")
|
|
162
|
+
.replace(/```$/, "")
|
|
163
|
+
.trim();
|
|
164
|
+
|
|
165
|
+
const firstBrace = sanitized.indexOf("{");
|
|
166
|
+
if (firstBrace === -1) {
|
|
167
|
+
if (isDebug) {
|
|
168
|
+
logger.debug("[json] robustParseJSON: no opening brace found");
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let jsonPart = sanitized.substring(firstBrace);
|
|
174
|
+
try {
|
|
175
|
+
const result = JSON.parse(jsonPart);
|
|
176
|
+
if (isDebug) {
|
|
177
|
+
logger.debug("[json] robustParseJSON: direct parse succeeded", {
|
|
178
|
+
resultType: typeof result,
|
|
179
|
+
resultPreview: JSON.stringify(result).substring(0, 200),
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return result;
|
|
183
|
+
} catch (e) {
|
|
184
|
+
if (isDebug) {
|
|
185
|
+
logger.debug("[json] robustParseJSON: direct parse failed", {
|
|
186
|
+
error: e instanceof Error ? e.message : String(e),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
// Fix double-escaped quotes (e.g., \\" -> ") only after direct parse
|
|
190
|
+
// fails, to avoid corrupting valid JSON with legitimate \\ sequences.
|
|
191
|
+
jsonPart = jsonPart.replace(/\\\\"/g, '\\"');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
let currentJson = jsonPart.replace(
|
|
195
|
+
/([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)(\s*:)/g,
|
|
196
|
+
'$1"$2"$3',
|
|
197
|
+
);
|
|
198
|
+
currentJson = currentJson.replace(
|
|
199
|
+
/([{,]\s*)"([a-zA-Z0-9_]+)"\s*:\s*"\2"\s*:/g,
|
|
200
|
+
'$1"$2":',
|
|
201
|
+
);
|
|
202
|
+
currentJson = currentJson.replace(
|
|
203
|
+
/([{,]\s*)([a-zA-Z0-9_]+)\s*:\s*\2\s*:/g,
|
|
204
|
+
"$1$2:",
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
// Fix missing opening quotes in values (upstream: a63f054, 9328bde)
|
|
208
|
+
currentJson = fixMissingOpeningQuotes(currentJson);
|
|
209
|
+
|
|
210
|
+
// Handle unquoted string values after colon (e.g., "command":export CI=true)
|
|
211
|
+
// Matches: "key":value_without_quotes until comma, closing brace, or end
|
|
212
|
+
currentJson = currentJson.replace(
|
|
213
|
+
/"([a-zA-Z0-9_]+)":\s*([^"\s{\[\],}][^,}\]]*)/g,
|
|
214
|
+
(match, key, value) => {
|
|
215
|
+
// Don't quote if it's a number, boolean, or null
|
|
216
|
+
if (/^(true|false|null|\d+(\.\d+)?)$/.test(value.trim())) {
|
|
217
|
+
return match;
|
|
218
|
+
}
|
|
219
|
+
return `"${key}":"${value.trim()}"`;
|
|
220
|
+
},
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
try {
|
|
224
|
+
const result = JSON.parse(currentJson);
|
|
225
|
+
if (isDebug) {
|
|
226
|
+
logger.debug("[json] robustParseJSON: quote-fix parse succeeded", {
|
|
227
|
+
resultType: typeof result,
|
|
228
|
+
resultPreview: JSON.stringify(result).substring(0, 200),
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
return result;
|
|
232
|
+
} catch (e) {
|
|
233
|
+
if (isDebug) {
|
|
234
|
+
logger.debug("[json] robustParseJSON: quote-fix parse failed", {
|
|
235
|
+
error: e instanceof Error ? e.message : String(e),
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
/* continue */
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
let cleaned = currentJson.trim();
|
|
242
|
+
while (
|
|
243
|
+
cleaned.length > 0 &&
|
|
244
|
+
!/[}\]"0-9a-z]/i.test(cleaned[cleaned.length - 1])
|
|
245
|
+
) {
|
|
246
|
+
cleaned = cleaned.slice(0, -1).trim();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const {
|
|
250
|
+
result: fixedJson,
|
|
251
|
+
openBraces,
|
|
252
|
+
openBrackets,
|
|
253
|
+
recoveredUnclosedString,
|
|
254
|
+
openStack: fixedJsonOpenStack,
|
|
255
|
+
} = sanitizeAndBalance(cleaned);
|
|
256
|
+
|
|
257
|
+
if (isDebug && recoveredUnclosedString) {
|
|
258
|
+
logger.debug(
|
|
259
|
+
"[json] robustParseJSON: recovered unclosed string at end of input",
|
|
260
|
+
{
|
|
261
|
+
originalPreview: cleaned.substring(0, 150),
|
|
262
|
+
},
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
let lastBalancedIndex = -1;
|
|
267
|
+
|
|
268
|
+
{
|
|
269
|
+
let ob = 0,
|
|
270
|
+
bk = 0,
|
|
271
|
+
ins = false,
|
|
272
|
+
esc = false;
|
|
273
|
+
for (let i = 0; i < fixedJson.length; i++) {
|
|
274
|
+
const c = fixedJson[i];
|
|
275
|
+
if (esc) {
|
|
276
|
+
esc = false;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (c === "\\") {
|
|
280
|
+
esc = true;
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
if (c === '"') {
|
|
284
|
+
ins = !ins;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (!ins) {
|
|
288
|
+
if (c === "{") ob++;
|
|
289
|
+
if (c === "}") ob--;
|
|
290
|
+
if (c === "[") bk++;
|
|
291
|
+
if (c === "]") bk--;
|
|
292
|
+
if (ob === 0 && bk === 0) lastBalancedIndex = i;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
let tempJson = fixedJson;
|
|
298
|
+
if (
|
|
299
|
+
lastBalancedIndex !== -1 &&
|
|
300
|
+
(openBraces !== 0 ||
|
|
301
|
+
openBrackets !== 0 ||
|
|
302
|
+
fixedJson.length > lastBalancedIndex + 1)
|
|
303
|
+
) {
|
|
304
|
+
tempJson = fixedJson.substring(0, lastBalancedIndex + 1);
|
|
305
|
+
if (isDebug) {
|
|
306
|
+
logger.debug("[json] robustParseJSON: truncated to balanced index", {
|
|
307
|
+
lastBalancedIndex,
|
|
308
|
+
originalLength: fixedJson.length,
|
|
309
|
+
truncatedLength: tempJson.length,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
} else if (openBraces > 0 || openBrackets > 0) {
|
|
313
|
+
tempJson = closeBraces(
|
|
314
|
+
fixedJson,
|
|
315
|
+
openBraces,
|
|
316
|
+
openBrackets,
|
|
317
|
+
fixedJsonOpenStack,
|
|
318
|
+
);
|
|
319
|
+
if (isDebug) {
|
|
320
|
+
logger.debug("[json] robustParseJSON: closed braces", {
|
|
321
|
+
openBraces,
|
|
322
|
+
openBrackets,
|
|
323
|
+
resultLength: tempJson.length,
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
try {
|
|
329
|
+
const result = JSON.parse(tempJson);
|
|
330
|
+
if (isDebug) {
|
|
331
|
+
logger.debug("[json] robustParseJSON: balanced-parse succeeded", {
|
|
332
|
+
resultType: typeof result,
|
|
333
|
+
resultPreview: JSON.stringify(result).substring(0, 200),
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
return result;
|
|
337
|
+
} catch (e) {
|
|
338
|
+
if (isDebug) {
|
|
339
|
+
logger.debug("[json] robustParseJSON: balanced-parse failed", {
|
|
340
|
+
error: e instanceof Error ? e.message : String(e),
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
let aggressive = fixedJson.trim();
|
|
344
|
+
if (aggressive.endsWith(",")) aggressive = aggressive.slice(0, -1);
|
|
345
|
+
const {
|
|
346
|
+
result: aggFixed,
|
|
347
|
+
openBraces: ob,
|
|
348
|
+
openBrackets: bk,
|
|
349
|
+
recoveredUnclosedString: aggRecovered,
|
|
350
|
+
openStack: aggOpenStack,
|
|
351
|
+
} = sanitizeAndBalance(aggressive);
|
|
352
|
+
|
|
353
|
+
if (isDebug && aggRecovered) {
|
|
354
|
+
logger.debug(
|
|
355
|
+
"[json] robustParseJSON: aggressive recovery of unclosed string",
|
|
356
|
+
{
|
|
357
|
+
originalPreview: aggressive.substring(0, 150),
|
|
358
|
+
},
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
try {
|
|
363
|
+
const result = JSON.parse(closeBraces(aggFixed, ob, bk, aggOpenStack));
|
|
364
|
+
if (isDebug) {
|
|
365
|
+
logger.debug("[json] robustParseJSON: aggressive-parse succeeded", {
|
|
366
|
+
resultType: typeof result,
|
|
367
|
+
resultPreview: JSON.stringify(result).substring(0, 200),
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
return result;
|
|
371
|
+
} catch {
|
|
372
|
+
if (isDebug) {
|
|
373
|
+
logger.debug("[json] robustParseJSON: all parse attempts failed", {
|
|
374
|
+
originalError: e instanceof Error ? e.message : String(e),
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
throw e;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|