@@ -68,6 +68,24 @@ use std::sync::{
6868use tokio:: runtime:: Runtime ;
6969use tokio:: sync:: Mutex ;
7070
71+ // ============================================================================
72+ // Utilities
73+ // ============================================================================
74+
75+ /// Truncate a UTF-8 string to at most `max_bytes` bytes, without splitting
76+ /// a multibyte character. Falls back to the full string if it's already
77+ /// within the limit.
78+ fn truncate_utf8 ( s : & str , max_bytes : usize ) -> & str {
79+ if s. len ( ) <= max_bytes {
80+ return s;
81+ }
82+ let mut end = max_bytes;
83+ while end > 0 && !s. is_char_boundary ( end) {
84+ end -= 1 ;
85+ }
86+ & s[ ..end]
87+ }
88+
7189// ============================================================================
7290// Task Module Bindings
7391// ============================================================================
@@ -149,7 +167,7 @@ impl PyAgentResult {
149167 format ! (
150168 "AgentResult(text={:?}, tool_calls={}, tokens={})" ,
151169 if self . text. len( ) > 80 {
152- format!( "{}..." , & self . text[ .. 80 ] )
170+ format!( "{}..." , truncate_utf8 ( & self . text, 80 ) )
153171 } else {
154172 self . text. clone( )
155173 } ,
@@ -204,7 +222,7 @@ impl PyBtwResult {
204222 "BtwResult(question={:?}, answer={:?}, tokens={})" ,
205223 self . question,
206224 if self . answer. len( ) > 60 {
207- format!( "{}..." , & self . answer[ .. 60 ] )
225+ format!( "{}..." , truncate_utf8 ( & self . answer, 60 ) )
208226 } else {
209227 self . answer. clone( )
210228 } ,
@@ -4224,7 +4242,7 @@ impl PySkillInfo {
42244242 self . name,
42254243 self . kind,
42264244 if self . description. len( ) > 60 {
4227- format!( "{}..." , & self . description[ .. 60 ] )
4245+ format!( "{}..." , truncate_utf8 ( & self . description, 60 ) )
42284246 } else {
42294247 self . description. clone( )
42304248 }
@@ -4324,7 +4342,7 @@ impl PyTeamTask {
43244342 self . id,
43254343 self . status,
43264344 if self . description. len( ) > 60 {
4327- format!( "{}..." , & self . description[ .. 60 ] )
4345+ format!( "{}..." , truncate_utf8 ( & self . description, 60 ) )
43284346 } else {
43294347 self . description. clone( )
43304348 }
0 commit comments