fix: load more messages to show recent history after streaming and on refresh
Fixed issue where recent messages weren't loading after streaming completion
or on app refresh, showing only ~10 old messages from the middle of history.
Root causes:
1. Stale closure: Stream completion callback used old 'messages' state
to calculate fetch limit, resulting in fetching too few messages
2. Initial load limit too low: INITIAL_LOAD_LIMIT was only 20, so refresh
only loaded 20 messages (less after filtering system messages)
3. Insufficient buffer: Only adding 5 extra messages after streaming
Solutions:
- Use setMessages callback to access current state (fixes stale closure)
- Increase INITIAL_LOAD_LIMIT from 20 to 100 for better initial load
- Increase stream completion minimum from 50 to 100 messages
- Increase buffer from +5 to +10 messages
- Add detailed logging to debug message fetching
Now users see:
- 100 most recent messages on refresh (instead of 20)
- Proper message count after streaming completes
- All recent messages including newly created ones
Added console logs to track:
- Current message count before fetching
- How many messages will be fetched
- How many messages received from server
- First and last message IDs in response