From 55cf0139fd381cf5e0ea894d08bd14d4d41edc71 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Tue, 8 Jul 2025 01:29:00 +0200 Subject: [PATCH] Don't crash on assert when a tool call is missing a parameter. --- profiler/src/profiler/TracyLlmTools.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/profiler/src/profiler/TracyLlmTools.cpp b/profiler/src/profiler/TracyLlmTools.cpp index ac786bb3..394a2006 100644 --- a/profiler/src/profiler/TracyLlmTools.cpp +++ b/profiler/src/profiler/TracyLlmTools.cpp @@ -205,8 +205,20 @@ TracyLlmTools::~TracyLlmTools() CancelManualEmbeddings(); } -#define Param(name) json[name].get_ref() -#define ParamU32(name) json[name].get() +static const std::string& GetParam( const nlohmann::json& json, const char* name ) +{ + if( !json.contains( name ) ) throw std::runtime_error( "Error: missing parameter: " + std::string( name ) ); + return json[name].get_ref(); +} + +static uint32_t GetParamU32( const nlohmann::json& json, const char* name ) +{ + if( !json.contains( name ) ) throw std::runtime_error( "Error: missing parameter: " + std::string( name ) ); + return json[name].get(); +} + +#define Param(name) GetParam( json, name ) +#define ParamU32(name) GetParamU32( json, name ) TracyLlmTools::ToolReply TracyLlmTools::HandleToolCalls( const nlohmann::json& json, TracyLlmApi& api, int contextSize, bool hasEmbeddingsModel ) { @@ -245,7 +257,7 @@ TracyLlmTools::ToolReply TracyLlmTools::HandleToolCalls( const nlohmann::json& j } return { .reply = "Unknown tool call: " + name }; } - catch( const nlohmann::json::exception& e ) + catch( const std::exception& e ) { return { .reply = e.what() }; }