From 34faaeb314019c87bc39e4a9f4861999d0a004b7 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Wed, 31 Dec 2025 18:22:13 +0100 Subject: [PATCH] Display short snippet of thinking process. --- profiler/src/profiler/TracyLlm.cpp | 2 +- profiler/src/profiler/TracyLlmChat.cpp | 51 +++++++++++++++++++++++++- profiler/src/profiler/TracyLlmChat.hpp | 2 +- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/profiler/src/profiler/TracyLlm.cpp b/profiler/src/profiler/TracyLlm.cpp index 61d0623e..6dc440d6 100644 --- a/profiler/src/profiler/TracyLlm.cpp +++ b/profiler/src/profiler/TracyLlm.cpp @@ -467,7 +467,7 @@ void TracyLlm::Draw() } ImGui::PushID( turnIdx++ ); - if( !m_chatUi->Turn( role, line, thinkIdx <= turnIdx ) ) + if( !m_chatUi->Turn( role, line, thinkIdx <= turnIdx, 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 633f99ba..29b095d7 100644 --- a/profiler/src/profiler/TracyLlmChat.cpp +++ b/profiler/src/profiler/TracyLlmChat.cpp @@ -8,6 +8,7 @@ #include "TracyLlmChat.hpp" #include "TracyMouse.hpp" #include "../Fonts.hpp" +#include "../../public/common/TracyForceInline.hpp" namespace tracy { @@ -34,6 +35,38 @@ constexpr size_t NumRoles = roles.size(); static_assert( NumRoles == (int)TracyLlmChat::TurnRole::None ); +static tracy_force_inline int codepointlen( char c ) +{ + if( ( c & 0x80 ) == 0 ) return 1; + if( ( c & 0x20 ) == 0 ) return 2; + if( ( c & 0x10 ) == 0 ) return 3; + assert( ( c & 0x08 ) == 0 ); + return 4; +} + +static size_t utflen( const char* str ) +{ + size_t ret = 0; + while( *str != '\0' ) + { + str += codepointlen( *str ); + ret++; + } + return ret; +} + +static const char* utfendl( const char* str, int len ) +{ + int l = 0; + while( l < len && *str != '\0' ) + { + str += codepointlen( *str ); + l++; + } + return str; +} + + TracyLlmChat::TracyLlmChat() : m_width( new float[NumRoles] ) { @@ -71,7 +104,7 @@ void TracyLlmChat::End() } } -bool TracyLlmChat::Turn( TurnRole role, const nlohmann::json& json, bool think ) +bool TracyLlmChat::Turn( TurnRole role, const nlohmann::json& json, bool think, bool last ) { bool keep = true; const auto& roleData = roles[(int)role]; @@ -178,12 +211,26 @@ bool TracyLlmChat::Turn( TurnRole role, const nlohmann::json& json, bool think ) { if( json.contains( "reasoning_content" ) ) { + auto& reasoning = json["reasoning_content"].get_ref(); ThinkScope( !roleChange ); if( m_thinkOpen ) { - auto& reasoning = json["reasoning_content"].get_ref(); 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(); + } } if( json.contains( "tool_calls" ) ) { diff --git a/profiler/src/profiler/TracyLlmChat.hpp b/profiler/src/profiler/TracyLlmChat.hpp index 52f9a188..6d479502 100644 --- a/profiler/src/profiler/TracyLlmChat.hpp +++ b/profiler/src/profiler/TracyLlmChat.hpp @@ -31,7 +31,7 @@ public: void Begin(); void End(); - bool Turn( TurnRole role, const nlohmann::json& json, bool think ); + bool Turn( TurnRole role, const nlohmann::json& json, bool think, bool last ); private: void NormalScope();