Skip to content

Commit 6c7915e

Browse files
committed
chore: update implementation.md
1 parent 46bda32 commit 6c7915e

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

examples/react-chatbot/implementation.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
- ✅ Override Stream Chat React styles via CSS variables
1515
- ✅ Automatic conversation summarization (first 5 messages)
1616
- ✅ Server-side token generation for secure authentication
17+
- ✅ Rate limiting: 10 messages per conversation per 4 hours
18+
- ✅ UUID-based random user generation with localStorage persistence
1719

1820
---
1921

@@ -56,6 +58,8 @@ examples/react-chatbot/
5658
│ │ ├── EmptyState.tsx # ✅ Empty placeholder
5759
│ │ └── EmptyState.scss # ✅ Theme-aware styling
5860
│ ├── ThemeContext.tsx # ✅ Client-side theme state with localStorage
61+
│ ├── UserProvider.tsx # ✅ Client-side user initialization and URL sync
62+
│ ├── rateLimitUtils.ts # ✅ Rate limiting utilities for message tracking
5963
│ ├── api.ts # ✅ API functions (startAiAgent, summarizeConversation)
6064
│ └── index.scss # ✅ Global styles + CSS variables for both themes
6165
├── public/ # ✅ Static assets
@@ -126,9 +130,11 @@ examples/react-chatbot/
126130
✅ Implemented features:
127131

128132
- **Server-side token generation**: Generates Stream user token using `STREAM_API_KEY` and `STREAM_API_SECRET` environment variables
133+
- **UUID-based user generation**: Generates random UUID for each user (replaces hardcoded 'jane')
134+
- **User ID override**: Supports `?user_id=` URL parameter for testing/debugging
129135
- Defines channel filters, options, and sorting configuration
130136
- Extracts `conversation_id` from URL params
131-
- Renders `AIChatApp` wrapped in `ThemeProvider`
137+
- Renders `AIChatApp` wrapped in `ThemeProvider` and `UserProvider`
132138
- Passes authentication and configuration props to client component
133139

134140
### 2. **AIChatApp.tsx (Client Component)**
@@ -238,6 +244,12 @@ examples/react-chatbot/
238244
- Theme-aware input field and submit button
239245
- Focus states with accent color
240246
- Responsive padding
247+
- **Rate limiting**: 10 messages per conversation in 4-hour window
248+
- Tracks message count per conversation in localStorage
249+
- Disables all inputs when limit reached
250+
- Shows informative banner with reset countdown
251+
- Automatically resets after 4 hours
252+
- Updates countdown every minute
241253

242254
### 12. **EmptyState**
243255

@@ -258,7 +270,36 @@ examples/react-chatbot/
258270
- Sets `data-theme` attribute on document root
259271
- `useTheme` hook for accessing theme state and toggle function
260272

261-
### 14. **api.ts**
273+
### 14. **UserProvider (Client Component)**
274+
275+
✅ Implemented features:
276+
277+
- Client-side user initialization and URL synchronization
278+
- Gets or generates UUID for user on first load
279+
- Syncs user ID with URL parameters (`?user_id=`)
280+
- Persists user ID to localStorage for session continuity
281+
- Shows loading state during initialization
282+
283+
### 15. **rateLimitUtils.ts**
284+
285+
✅ Implemented features:
286+
287+
- **getUserId()**: Gets or creates random UUID user ID
288+
- Checks URL params first for `?user_id=` override
289+
- Falls back to localStorage for persistence
290+
- Generates new UUID if neither exists
291+
- **checkRateLimit()**: Checks if conversation has hit rate limit
292+
- Returns `isLimited`, `resetTime`, and `remainingMessages`
293+
- Automatically resets after 4-hour window expires
294+
- **recordMessage()**: Records message sent to conversation
295+
- Tracks message count and first message timestamp
296+
- Stores data in localStorage per conversation
297+
- Resets counter when 4-hour window expires
298+
- **formatTimeRemaining()**: Formats reset time as human-readable string
299+
- Returns format like "3h 45m" or "25m"
300+
- Used in rate limit banner message
301+
302+
### 16. **api.ts**
262303

263304
✅ Implemented features:
264305

@@ -302,6 +343,14 @@ STREAM_API_SECRET=your_api_secret_here
302343

303344
These are accessed server-side only in `app/page.tsx` using `process.env`.
304345

346+
### localStorage Keys
347+
348+
The app uses the following localStorage keys for client-side persistence:
349+
350+
- `ai-demo-theme` - Stores user's theme preference ('light' | 'dark')
351+
- `user_id` - Stores randomly generated UUID for user identification
352+
- `rate_limit_{conversationId}` - Stores rate limit data per conversation (message count, first message timestamp)
353+
305354
---
306355

307356
## Stream Chat React CSS Variable Overrides
@@ -354,6 +403,27 @@ The app uses `window.history.pushState()` to update the URL when switching conve
354403

355404
Theme state is managed via React Context and persisted to localStorage. The theme is applied by setting a `data-theme` attribute on the document root, which allows CSS variables to be scoped appropriately.
356405

406+
### Rate Limiting System
407+
408+
The app implements client-side rate limiting to control message sending:
409+
410+
- **Per-conversation limits**: Each conversation tracks its own message count independently
411+
- **4-hour rolling window**: Limits reset automatically 4 hours after the first message
412+
- **localStorage persistence**: Rate limit data persists across page refreshes
413+
- **Real-time updates**: Countdown updates every minute to show time remaining
414+
- **Graceful UI**:
415+
- Disabled fieldset prevents all input interactions
416+
- Amber warning banner shows informative message with countdown
417+
- Material Symbols info icon for visual clarity
418+
- **UUID-based users**: Each user gets a random UUID stored in localStorage
419+
- **Override support**: `?user_id=` URL parameter allows testing with specific user IDs
420+
421+
**Implementation details:**
422+
- Rate limit state checked on mount and when channel changes
423+
- Message recorded after successful send
424+
- Form submission blocked when limit reached
425+
- All inputs (text, file, speech-to-text, model selector, submit) disabled via fieldset
426+
357427
### Automatic Conversation Summarization
358428

359429
The app automatically generates summaries for the first 5 messages of a conversation:

0 commit comments

Comments
 (0)