mirror of
https://github.com/7836246/cursor2api.git
synced 2026-05-07 22:27:15 +08:00
避免短参数 tool_use 在已可恢复时继续隐式续写,减少 OpenClaw/Telegram 场景下的长时间挂起与 terminated。 同时统一 Anthropic 与 OpenAI 兼容接口的截断续写判定,并补充对应回归测试。 Made-with: Cursor
77 lines
2.0 KiB
JavaScript
77 lines
2.0 KiB
JavaScript
import { shouldAutoContinueTruncatedToolResponse } from '../dist/handler.js';
|
|
|
|
let passed = 0;
|
|
let failed = 0;
|
|
|
|
function test(name, fn) {
|
|
try {
|
|
fn();
|
|
console.log(` ✅ ${name}`);
|
|
passed++;
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
console.error(` ❌ ${name}`);
|
|
console.error(` ${message}`);
|
|
failed++;
|
|
}
|
|
}
|
|
|
|
function assertEqual(actual, expected, message) {
|
|
if (actual !== expected) {
|
|
throw new Error(message || `Expected ${expected}, got ${actual}`);
|
|
}
|
|
}
|
|
|
|
console.log('\n📦 handler 截断续写判定\n');
|
|
|
|
test('短参数工具调用可恢复时不再继续续写', () => {
|
|
const text = [
|
|
'我先读取配置文件。',
|
|
'',
|
|
'```json action',
|
|
'{',
|
|
' "tool": "Read",',
|
|
' "parameters": {',
|
|
' "file_path": "/app/config.yaml"',
|
|
' }',
|
|
].join('\n');
|
|
|
|
assertEqual(
|
|
shouldAutoContinueTruncatedToolResponse(text, true),
|
|
false,
|
|
'Read 这类短参数工具不应继续续写',
|
|
);
|
|
});
|
|
|
|
test('大参数写入工具仍然继续续写', () => {
|
|
const longContent = 'A'.repeat(4000);
|
|
const text = [
|
|
'```json action',
|
|
'{',
|
|
' "tool": "Write",',
|
|
' "parameters": {',
|
|
' "file_path": "/tmp/large.txt",',
|
|
` "content": "${longContent}`,
|
|
].join('\n');
|
|
|
|
assertEqual(
|
|
shouldAutoContinueTruncatedToolResponse(text, true),
|
|
true,
|
|
'Write 大内容仍应继续续写以补全参数',
|
|
);
|
|
});
|
|
|
|
test('无工具代码块但文本明显截断时继续续写', () => {
|
|
const text = '```ts\nexport const answer = {';
|
|
|
|
assertEqual(
|
|
shouldAutoContinueTruncatedToolResponse(text, true),
|
|
true,
|
|
'未形成可恢复工具调用时应继续续写',
|
|
);
|
|
});
|
|
|
|
console.log(`\n结果: ${passed} 通过 / ${failed} 失败 / ${passed + failed} 总计\n`);
|
|
|
|
if (failed > 0) process.exit(1);
|