Display short snippet of thinking process.

This commit is contained in:
Bartosz Taudul
2025-12-31 18:22:13 +01:00
parent 523d2fb8bf
commit 34faaeb314
3 changed files with 51 additions and 4 deletions

View File

@@ -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 )
{

View File

@@ -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<const std::string&>();
ThinkScope( !roleChange );
if( m_thinkOpen )
{
auto& reasoning = json["reasoning_content"].get_ref<const std::string&>();
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" ) )
{

View File

@@ -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();