fix: handle Map type in getChatUsage() after deserialization#1118
fix: handle Map type in getChatUsage() after deserialization#1118mvanhorn wants to merge 2 commits intoagentscope-ai:mainfrom
Conversation
When messages are persisted and reloaded, the _chat_usage metadata value may be deserialized as a Map instead of a ChatUsage object. The getter now detects this case, converts the Map to ChatUsage, and caches the result back in metadata for subsequent calls. Fixes agentscope-ai#1115
|
Hey @mvanhorn, great fix 👍 I noticed the CI build is failing—this might be due to type casting issues when converting values from the Map. Since JSON deserialization can produce Maybe using Happy to help test or contribute improvements if needed 🚀 |
|
Thanks for the review! The The CI failure was a spotless formatting issue with the builder chain indentation. Fixed in 536b3c8. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Summary
getChatUsage()returns null when_chat_usagemetadata is a Map (after session persistence/reload) instead of a ChatUsage object.Why this matters
When messages are persisted via
agent.loadFrom()and reloaded, JSON deserialization converts the_chat_usagemetadata value to aMap<String, Object>instead of aChatUsageinstance. The existinginstanceof ChatUsagecheck fails, returning null even though the token usage data exists in the metadata.Changes
agentscope-core/src/main/java/io/agentscope/core/message/Msg.java:getChatUsage()(line 413): when the metadata value is a Map, extractinputTokens,outputTokens, andtimefields, build a ChatUsage via the builder, and cache it back in metadata for subsequent callstoInt()andtoDouble()helper methods for safe numeric conversion from deserialized values (handles Integer, Long, Double from JSON)Testing
_chat_usageis a ChatUsage object: unchanged behavior_chat_usageis a Map (post-deserialization): converts to ChatUsage and returns it_chat_usageis null or absent: returns null (unchanged)Fixes #1115
This contribution was developed with AI assistance (Claude Code).