fix: handle truncated tool call JSON — guide model to use bash heredoc for large files

When file_write gets a 15KB+ HTML payload, the streaming JSON gets
truncated. Now catches JSON parse errors and returns a specific
hint to use bash heredoc instead of silently failing.
This commit is contained in:
admin
2026-05-05 20:12:15 +00:00
Unverified
parent 91165bf6d1
commit 7d82eff753

View File

@@ -426,7 +426,20 @@ export async function initBot(config, api, tools, skills, agents) {
if (!handler) { if (!handler) {
result = `❌ Unknown tool: ${fn.name}`; result = `❌ Unknown tool: ${fn.name}`;
} else { } else {
const args = JSON.parse(fn.arguments || '{}'); let args;
try {
args = JSON.parse(fn.arguments || '{}');
} catch (parseErr) {
// Tool call JSON was truncated (common with large file content in file_write)
const argLen = (fn.arguments || '').length;
result = `${fn.name} failed: Tool call arguments JSON was truncated (${argLen} chars). ` +
(fn.name === 'file_write'
? 'The file content is too large for a single tool call. Use bash with heredoc instead: bash({ command: "cat > /path/to/file << \'EOF\'\\ncontent here\\nEOF" })'
: 'Retry with shorter arguments or split into smaller calls.');
logger.error(`${fn.name} failed: ${parseErr.message} (args length: ${argLen})`);
loopMessages.push({ role: 'tool', tool_call_id: tc.id, content: result });
continue;
}
logger.info(`${fn.name}(${fn.arguments?.slice(0, 100)})`); logger.info(`${fn.name}(${fn.arguments?.slice(0, 100)})`);
result = String(await handler(args)).slice(0, 8000); result = String(await handler(args)).slice(0, 8000);
} }