File size: 13,086 Bytes
097fb32 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | /**
* test/unit-tolerant-parse.mjs
*
* ๅๅ
ๆต่ฏ๏ผtolerantParse ๅ parseToolCalls ็ๅ็ง่พน็ๅบๆฏ
* ่ฟ่กๆนๅผ๏ผnode test/unit-tolerant-parse.mjs
*
* ๆ ้ๆๅกๅจ๏ผๅฎๅ
จ็ฆป็บฟ่ฟ่กใ
*/
// โโโ ไป dist/ ไธญ็ดๆฅๅผๅ
ฅๅทฒ็ผ่ฏ็ converter๏ผ้่ฆๅ
npm run build๏ผโโโโโโโโโโ
// ๅฆๆๆฒกๆ dist๏ผไนๅฏไปฅๆ tolerantParse ็ๅฎ็ฐๅคๅถๅฐๆญคๅคๅๆต่ฏ
// โโโ ๅ
่ tolerantParse๏ผไธ src/converter.ts ไฟๆๅๆญฅ๏ผโโโโโโโโโโโโโโโโโโโโโโ
function tolerantParse(jsonStr) {
try {
return JSON.parse(jsonStr);
} catch (_e1) { /* pass */ }
let inString = false;
let escaped = false;
let fixed = '';
const bracketStack = [];
for (let i = 0; i < jsonStr.length; i++) {
const char = jsonStr[i];
if (char === '\\' && !escaped) {
escaped = true;
fixed += char;
} else if (char === '"' && !escaped) {
inString = !inString;
fixed += char;
escaped = false;
} else {
if (inString) {
if (char === '\n') fixed += '\\n';
else if (char === '\r') fixed += '\\r';
else if (char === '\t') fixed += '\\t';
else fixed += char;
} else {
if (char === '{' || char === '[') bracketStack.push(char === '{' ? '}' : ']');
else if (char === '}' || char === ']') { if (bracketStack.length > 0) bracketStack.pop(); }
fixed += char;
}
escaped = false;
}
}
if (inString) fixed += '"';
while (bracketStack.length > 0) fixed += bracketStack.pop();
fixed = fixed.replace(/,\s*([}\]])/g, '$1');
try {
return JSON.parse(fixed);
} catch (_e2) {
const lastBrace = fixed.lastIndexOf('}');
if (lastBrace > 0) {
try { return JSON.parse(fixed.substring(0, lastBrace + 1)); } catch { /* ignore */ }
}
throw _e2;
}
}
// โโโ ๅ
่ parseToolCalls๏ผไธ src/converter.ts ไฟๆๅๆญฅ๏ผโโโโโโโโโโโโโโโโโโโโ
function parseToolCalls(responseText) {
const toolCalls = [];
let cleanText = responseText;
const fullBlockRegex = /```json(?:\s+action)?\s*([\s\S]*?)\s*```/g;
let match;
while ((match = fullBlockRegex.exec(responseText)) !== null) {
let isToolCall = false;
try {
const parsed = tolerantParse(match[1]);
if (parsed.tool || parsed.name) {
toolCalls.push({
name: parsed.tool || parsed.name,
arguments: parsed.parameters || parsed.arguments || parsed.input || {}
});
isToolCall = true;
}
} catch (e) {
console.error(` โ tolerantParse ๅคฑ่ดฅ:`, e.message);
}
if (isToolCall) cleanText = cleanText.replace(match[0], '');
}
return { toolCalls, cleanText: cleanText.trim() };
}
// โโโ ๆต่ฏๆกๆถ๏ผๆ็ฎ๏ผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
let passed = 0;
let failed = 0;
function test(name, fn) {
try {
fn();
console.log(` โ
${name}`);
passed++;
} catch (e) {
console.error(` โ ${name}`);
console.error(` ${e.message}`);
failed++;
}
}
function assert(condition, msg) {
if (!condition) throw new Error(msg || 'Assertion failed');
}
function assertEqual(a, b, msg) {
const as = JSON.stringify(a), bs = JSON.stringify(b);
if (as !== bs) throw new Error(msg || `Expected ${bs}, got ${as}`);
}
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// 1. tolerantParse โ ๆญฃๅธธ JSON
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log('\n๐ฆ [1] tolerantParse โ ๆญฃๅธธ JSON\n');
test('ๆ ๅ JSON ๅฏน่ฑก', () => {
const r = tolerantParse('{"tool":"Read","parameters":{"path":"/foo"}}');
assertEqual(r.tool, 'Read');
assertEqual(r.parameters.path, '/foo');
});
test('ๅธฆๆข่ก็ผฉ่ฟ็ JSON', () => {
const r = tolerantParse(`{
"tool": "Write",
"parameters": {
"file_path": "src/index.ts",
"content": "hello world"
}
}`);
assertEqual(r.tool, 'Write');
});
test('็ฉบๅฏน่ฑก', () => {
const r = tolerantParse('{}');
assertEqual(r, {});
});
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// 2. tolerantParse โ ๅญ็ฌฆไธฒๅ
ๅซ่ฃธๆข่ก๏ผๆตๅผ่พๅบๅธธ่งๅบๆฏ๏ผ
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log('\n๐ฆ [2] tolerantParse โ ๅญ็ฌฆไธฒๅ
ๅซ่ฃธๆข่ก\n');
test('value ไธญๅซ่ฃธ \\n', () => {
// ๆจกๆ๏ผcontent ๅญๆฎตๅผ้ๆๅค่กๆๆฌ๏ผไฝ JSON ๆฒกๆ่ฝฌไนๆข่ก
const raw = '{"tool":"Write","parameters":{"content":"line1\nline2\nline3"}}';
const r = tolerantParse(raw);
assert(r.parameters.content.includes('\n') || r.parameters.content.includes('\\n'),
'content ๅบๅ
ๅซๆข่กไฟกๆฏ');
});
test('value ไธญๅซ่ฃธ \\t', () => {
const raw = '{"tool":"Bash","parameters":{"command":"echo\there"}}';
const r = tolerantParse(raw);
assert(r.parameters.command !== undefined);
});
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// 3. tolerantParse โ ๆชๆญ JSON๏ผๆ ธๅฟไฟฎๅคๅบๆฏ๏ผ
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log('\n๐ฆ [3] tolerantParse โ ๆชๆญ JSON๏ผๆช้ญๅๅญ็ฌฆไธฒ / ๆฌๅท๏ผ\n');
test('ๅญ็ฌฆไธฒๅจๅผไธญ้ดๆชๆญ', () => {
// ๆจกๆ๏ผ็ฝ็ปไธญๆญ๏ผ"content" ๅญๆฎตๅผๅชไผ ไบไธๅ
const truncated = '{"tool":"Write","parameters":{"content":"# Accrual Backfill Start Date Implementation Plan\\n\\n> **For Claude:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task.\\n\\n**Goal:** Add an optional `backfillStartDate` parameter to the company-level accrual recalculate feature, allowing admins to specify a';
const r = tolerantParse(truncated);
// ่ฝ่งฃๆๅบๆฅๅฐฑ่ก๏ผcontent ๅฏ่ฝ่ขซๆชๆญไฝ tool ๅญๆฎตๅญๅจ
assertEqual(r.tool, 'Write');
assert(r.parameters !== undefined);
});
test('ๅช็ผบๅฐๆๅ็ }}', () => {
const truncated = '{"tool":"Read","parameters":{"file_path":"/Users/rain/project/src/index.ts"';
const r = tolerantParse(truncated);
assertEqual(r.tool, 'Read');
});
test('ๅช็ผบๅฐๆๅ็ }', () => {
const truncated = '{"name":"Bash","input":{"command":"ls -la"}';
const r = tolerantParse(truncated);
assertEqual(r.name, 'Bash');
});
test('ๅตๅฅๅฏน่ฑกๆชๆญ', () => {
const truncated = '{"tool":"Write","parameters":{"path":"a.ts","content":"export function foo() {\n return 42;\n}';
const r = tolerantParse(truncated);
assertEqual(r.tool, 'Write');
});
test('ๅธฆๅฐพ้จ้ๅท', () => {
const withComma = '{"tool":"Read","parameters":{"path":"/foo",},}';
const r = tolerantParse(withComma);
assertEqual(r.tool, 'Read');
});
test('ๆจกๆ issue #13 ๅๅง้่ฏฏ โ position 813 ๆชๆญ', () => {
// ๆจกๆไธไธช็บฆ813ๅญ่็ content ๅญๆฎตๅจๅญ็ฌฆไธฒไธญ้ดๆชๆญ
const longContent = 'A'.repeat(700);
const truncated = `{"tool":"Write","parameters":{"file_path":"/docs/plan.md","content":"${longContent}`;
const r = tolerantParse(truncated);
assertEqual(r.tool, 'Write');
// content ๅญๆฎตๅผๅฏ่ฝ่ขซๆชๆญ๏ผไฝๆดไฝ JSON ๅบๅฝ่ฝ่งฃๆ
assert(typeof r.parameters.content === 'string', 'content ๅบไธบๅญ็ฌฆไธฒ');
});
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// 4. parseToolCalls โ ๅฎๆด ```json action ๅ
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log('\n๐ฆ [4] parseToolCalls โ ๅฎๆดไปฃ็ ๅ\n');
test('ๅไธชๅทฅๅ
ท่ฐ็จๅ (tool ๅญๆฎต)', () => {
const text = `I'll read the file now.
\`\`\`json action
{
"tool": "Read",
"parameters": {
"file_path": "src/index.ts"
}
}
\`\`\``;
const { toolCalls, cleanText } = parseToolCalls(text);
assertEqual(toolCalls.length, 1);
assertEqual(toolCalls[0].name, 'Read');
assertEqual(toolCalls[0].arguments.file_path, 'src/index.ts');
assert(!cleanText.includes('```'), 'ไปฃ็ ๅๅบ่ขซ็งป้ค');
});
test('ๅไธชๅทฅๅ
ท่ฐ็จๅ (name ๅญๆฎต)', () => {
const text = `\`\`\`json action
{"name":"Bash","input":{"command":"npm run build"}}
\`\`\``;
const { toolCalls } = parseToolCalls(text);
assertEqual(toolCalls.length, 1);
assertEqual(toolCalls[0].name, 'Bash');
assertEqual(toolCalls[0].arguments.command, 'npm run build');
});
test('ๅคไธช่ฟ็ปญๅทฅๅ
ท่ฐ็จๅ', () => {
const text = `\`\`\`json action
{"tool":"Read","parameters":{"file_path":"a.ts"}}
\`\`\`
\`\`\`json action
{"tool":"Write","parameters":{"file_path":"b.ts","content":"hello"}}
\`\`\``;
const { toolCalls } = parseToolCalls(text);
assertEqual(toolCalls.length, 2);
assertEqual(toolCalls[0].name, 'Read');
assertEqual(toolCalls[1].name, 'Write');
});
test('ๅทฅๅ
ท่ฐ็จๅๆ่งฃ้ๆๆฌ', () => {
const text = `Let me first read the existing file to understand the structure.
\`\`\`json action
{"tool":"Read","parameters":{"file_path":"src/handler.ts"}}
\`\`\``;
const { toolCalls, cleanText } = parseToolCalls(text);
assertEqual(toolCalls.length, 1);
assert(cleanText.includes('Let me first read'), '่งฃ้ๆๆฌๅบไฟ็');
});
test('ไธๅซๅทฅๅ
ท่ฐ็จ็็บฏๆๆฌ', () => {
const text = 'Here is the answer: 42. No tool calls needed.';
const { toolCalls, cleanText } = parseToolCalls(text);
assertEqual(toolCalls.length, 0);
assertEqual(cleanText, text);
});
test('json ๅไฝไธๆฏ tool call๏ผๆฎ้ json๏ผ', () => {
const text = `Here is an example:
\`\`\`json
{"key":"value","count":42}
\`\`\``;
const { toolCalls } = parseToolCalls(text);
assertEqual(toolCalls.length, 0, 'ๆ tool/name ๅญๆฎต็ JSON ไธๅบ่ขซ่ฏๅซไธบๅทฅๅ
ท่ฐ็จ');
});
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// 5. ๆชๆญๅบๆฏไธ็ parseToolCalls
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log('\n๐ฆ [5] parseToolCalls โ ๆชๆญๅบๆฏ\n');
test('ไปฃ็ ๅๅ
ๅฎน่ขซๆตไธญๆญ๏ผblock ๅฎๆดไฝ JSON ๆชๆญ๏ผ', () => {
// ๅฎๆด็ ``` ๅ
่ฃน๏ผไฝ JSON ๅ
ๅฎน่ขซๆชๆญ
const text = `\`\`\`json action
{"tool":"Write","parameters":{"file_path":"/docs/plan.md","content":"# Plan\n\nThis is a very long document that got cut at position 813 in the strea
\`\`\``;
const { toolCalls } = parseToolCalls(text);
// ๅบๅฝ่ฝ่งฃๆๅบๅทฅๅ
ท่ฐ็จ๏ผๅณไฝฟ content ่ขซๆชๆญ๏ผ
assertEqual(toolCalls.length, 1);
assertEqual(toolCalls[0].name, 'Write');
console.log(` โ ่งฃๆๅบ็ content ๅ30ๅญ็ฌฆ: "${String(toolCalls[0].arguments.content).substring(0, 30)}..."`);
});
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// ๆฑๆป
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log('\n' + 'โ'.repeat(55));
console.log(` ็ปๆ: ${passed} ้่ฟ / ${failed} ๅคฑ่ดฅ / ${passed + failed} ๆป่ฎก`);
console.log('โ'.repeat(55) + '\n');
if (failed > 0) process.exit(1);
|