File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -26,12 +26,25 @@ pub struct StreamTokenUsage {
2626 pub total_tokens : u32 ,
2727}
2828
29+ /// Safely convert an i64 token count to u32 with saturation.
30+ /// Negative values clamp to 0, values > u32::MAX clamp to u32::MAX.
31+ #[ inline]
32+ fn saturating_i64_to_u32 ( value : i64 ) -> u32 {
33+ if value <= 0 {
34+ 0
35+ } else if value > u32:: MAX as i64 {
36+ u32:: MAX
37+ } else {
38+ value as u32
39+ }
40+ }
41+
2942impl From < crate :: client:: TokenUsage > for StreamTokenUsage {
3043 fn from ( usage : crate :: client:: TokenUsage ) -> Self {
3144 Self {
32- prompt_tokens : usage. input_tokens as u32 ,
33- completion_tokens : usage. output_tokens as u32 ,
34- total_tokens : usage. total_tokens as u32 ,
45+ prompt_tokens : saturating_i64_to_u32 ( usage. input_tokens ) ,
46+ completion_tokens : saturating_i64_to_u32 ( usage. output_tokens ) ,
47+ total_tokens : saturating_i64_to_u32 ( usage. total_tokens ) ,
3548 }
3649 }
3750}
You can’t perform that action at this time.
0 commit comments