Claude: Improve error handling and reporting for tool use

This commit is contained in:
Alessandro Pignotti 2024-12-20 11:13:15 +01:00
parent 8256807697
commit d4f632e76c
2 changed files with 18 additions and 6 deletions

View File

@ -412,8 +412,8 @@
// Delay at most 3 times // Delay at most 3 times
if(delayCount < 3) if(delayCount < 3)
{ {
// TODO: Defensive check, validate and remove // TODO: Defensive message, validate and remove
debugger; console.warn("Identical screenshot, rate limiting");
delayCount++; delayCount++;
// Wait some time and retry // Wait some time and retry
await new Promise(function(f, r) { setTimeout(f, 5000); }); await new Promise(function(f, r) { setTimeout(f, 5000); });
@ -557,11 +557,12 @@
break; break;
} }
} }
debugger; return new Error("Error: Invalid action");
} }
else else
{ {
debugger; // We can get there due to model hallucinations
return new Error("Error: Invalid tool syntax");
} }
} }
</script> </script>

View File

@ -63,13 +63,24 @@ async function sendMessages(handleTool)
var commandResponse = await handleTool(c.input); var commandResponse = await handleTool(c.input);
var responseObj = {type: "tool_result", tool_use_id: c.id }; var responseObj = {type: "tool_result", tool_use_id: c.id };
if(commandResponse != null) if(commandResponse != null)
responseObj.content = commandResponse; {
if(commandResponse instanceof Error)
{
console.warn(`Tool error: ${commandResponse.message}`);
responseObj.content = commandResponse.message;
responseObj.is_error = true;
}
else
{
responseObj.content = commandResponse;
}
}
addMessageInternal("user", [responseObj]); addMessageInternal("user", [responseObj]);
sendMessages(handleTool); sendMessages(handleTool);
} }
else else
{ {
debugger; console.warn(`Invalid response type: ${c.type}`);
} }
} }
if(response.stop_reason == "end_turn") if(response.stop_reason == "end_turn")