diff --git a/profiler/src/profiler/TracyLlm.cpp b/profiler/src/profiler/TracyLlm.cpp index 58061796..c0215833 100644 --- a/profiler/src/profiler/TracyLlm.cpp +++ b/profiler/src/profiler/TracyLlm.cpp @@ -467,7 +467,16 @@ void TracyLlm::Draw() } ImGui::PushID( turnIdx++ ); - if( !m_chatUi->Turn( role, line, thinkIdx <= turnIdx, turnIdx == m_chat.size() - 1 ) ) + TracyLlmChat::Think think = TracyLlmChat::Think::Hide; + if( thinkIdx <= turnIdx ) + { + think = TracyLlmChat::Think::Show; + } + else if( thinkIdx == turnIdx + 1 && role == TracyLlmChat::TurnRole::Assistant && line.contains( "content" ) ) + { + think = TracyLlmChat::Think::ToolCall; + } + if( !m_chatUi->Turn( role, line, think, turnIdx == m_chat.size() - 1 ) ) { if( role == TracyLlmChat::TurnRole::Assistant ) { diff --git a/profiler/src/profiler/TracyLlmChat.cpp b/profiler/src/profiler/TracyLlmChat.cpp index 6ded56ab..44663795 100644 --- a/profiler/src/profiler/TracyLlmChat.cpp +++ b/profiler/src/profiler/TracyLlmChat.cpp @@ -104,7 +104,7 @@ void TracyLlmChat::End() } } -bool TracyLlmChat::Turn( TurnRole role, const nlohmann::json& json, bool think, bool last ) +bool TracyLlmChat::Turn( TurnRole role, const nlohmann::json& json, Think think, bool last ) { bool keep = true; const auto& roleData = roles[(int)role]; @@ -207,39 +207,27 @@ bool TracyLlmChat::Turn( TurnRole role, const nlohmann::json& json, bool think, } else { - if( think ) + if( think == Think::Show && json.contains( "reasoning_content" ) ) { - if( json.contains( "reasoning_content" ) ) + auto& reasoning = json["reasoning_content"].get_ref(); + ThinkScope( !roleChange ); + if( m_thinkOpen ) { - auto& reasoning = json["reasoning_content"].get_ref(); - ThinkScope( !roleChange ); - if( m_thinkOpen ) - { - PrintThink( reasoning.c_str(), reasoning.size() ); - } - else if( last && !json.contains( "content" ) ) - { - const auto cutlen = std::max( int( utflen( reasoning.c_str() ) ) - 40, 0 ); - const auto cut = utfendl( reasoning.c_str(), cutlen ); - std::string str = cut; - for( auto& c : str ) - { - if( c == '\n' ) c = ' '; - } - ImGui::SameLine(); - ImGui::PushStyleColor( ImGuiCol_Text, 0xFF555555 ); - ImGui::Text( "…%s", str.c_str() ); - ImGui::PopStyleColor(); - } + PrintThink( reasoning.c_str(), reasoning.size() ); } - if( json.contains( "tool_calls" ) ) + else if( last && !json.contains( "content" ) ) { - ThinkScope( !roleChange ); - if( m_thinkOpen ) + const auto cutlen = std::max( int( utflen( reasoning.c_str() ) ) - 40, 0 ); + const auto cut = utfendl( reasoning.c_str(), cutlen ); + std::string str = cut; + for( auto& c : str ) { - auto calls = json["tool_calls"].dump( 2 ); - PrintToolCall( calls.c_str(), calls.size() ); + if( c == '\n' ) c = ' '; } + ImGui::SameLine(); + ImGui::PushStyleColor( ImGuiCol_Text, 0xFF555555 ); + ImGui::Text( "…%s", str.c_str() ); + ImGui::PopStyleColor(); } } if( json.contains( "content" ) ) @@ -247,7 +235,7 @@ bool TracyLlmChat::Turn( TurnRole role, const nlohmann::json& json, bool think, auto& content = json["content"].get_ref(); if( json["role"].get_ref() == "tool" ) { - if( think ) + if( think == Think::Show ) { ThinkScope( !roleChange ); if( m_thinkOpen ) @@ -293,6 +281,15 @@ bool TracyLlmChat::Turn( TurnRole role, const nlohmann::json& json, bool think, } } } + if( think != Think::Hide && json.contains( "tool_calls" ) ) + { + ThinkScope( !roleChange || json.contains( "content" ) ); + if( m_thinkOpen ) + { + auto calls = json["tool_calls"].dump( 2 ); + PrintToolCall( calls.c_str(), calls.size() ); + } + } } ImGui::PopStyleColor(); diff --git a/profiler/src/profiler/TracyLlmChat.hpp b/profiler/src/profiler/TracyLlmChat.hpp index fa8d8fc2..f80dae7c 100644 --- a/profiler/src/profiler/TracyLlmChat.hpp +++ b/profiler/src/profiler/TracyLlmChat.hpp @@ -25,13 +25,20 @@ public: None, }; + enum class Think + { + Hide, + Show, + ToolCall + }; + TracyLlmChat(); ~TracyLlmChat(); void Begin(); void End(); - bool Turn( TurnRole role, const nlohmann::json& json, bool think, bool last ); + bool Turn( TurnRole role, const nlohmann::json& json, Think think, bool last ); private: void NormalScope();