fix: prevent ClassCastException in RestAI chat when making consecutive requests#1819
Open
octo-patch wants to merge 1 commit intoCodePhiliaX:mainfrom
Open
Conversation
…tion (fixes CodePhiliaX#1796, CodePhiliaX#1812) chatWithRestAi was storing messages as a JSON string via JSONUtil.toJsonStr() but getFastChatMessage() cast the cached value directly to List<FastChatMessage>. On the second consecutive request the cast threw a ClassCastException causing a 500 error. Fix: store the List object directly, consistent with other AI providers. Also add instanceof guard in getFastChatMessage for defensive type safety. Co-Authored-By: Octopus <liyuan851277048@icloud.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1796
Fixes #1812
Problem
When using a custom REST AI provider (
chatWithRestAi), making two consecutiverequests to
/api/ai/chatcauses aClassCastExceptionand a 500 error on thesecond request. Refreshing the page (which generates a new
uid) restoresfunctionality temporarily.
Root cause:
chatWithRestAistores the message history as a JSON string viaJSONUtil.toJsonStr(messages), butgetFastChatMessage()later retrieves thatvalue and casts it directly to
List<FastChatMessage>. The cast succeeds on thefirst call (cache miss → empty list created), but fails with
ClassCastExceptionon every subsequent call when the cached JSON string is retrieved.
Stack trace from #1796:
Solution
List<FastChatMessage>object directly in the cache (removing theJSONUtil.toJsonStr()call), consistent with all other AI provider methods.instanceof Listcheck ingetFastChatMessageto avoidunchecked casts and handle any stale cache entries gracefully.
Testing
instanceofguard ensures stale entries (from previous deployments) fallback to a fresh empty list instead of crashing.