- Streaming AI endpoint at /campanha/api/ai-chat with DeepSeek V4 Flash - 8 tools: calculate, getMunicipalityVotes, getTopDeputies, getDobradinhas, getMunicipalityOverview, getLeaderships, getOrganizations, searchEntities - In-memory rate limiter (50 msgs / 15 min per user) - Desktop: resizable right sidebar via react-resizable-panels - Mobile: full-screen Drawer opened by FAB (2/3 size of quick-actions FAB) - Markdown rendering with react-markdown + remark-gfm (tables, bold, lists) - Session-bound context (clean slate each open) - Auth via existing campaign-token cookie, tools respect user RBAC - Architecture doc: docs/plans/ai-chat-sollinha.md - E2E tests: campaignAiChat.e2e.spec.ts (mocked endpoint)
46 lines
1 KiB
TypeScript
46 lines
1 KiB
TypeScript
/**
|
|
* In-memory rate limiter for the AI chat endpoint.
|
|
* Survives a single instance; resets on deploy. Sufficient for v1 abuse prevention.
|
|
*/
|
|
|
|
type RateLimitWindow = {
|
|
count: number
|
|
resetAt: number
|
|
}
|
|
|
|
const store = new Map<number, RateLimitWindow>()
|
|
|
|
const MAX_MESSAGES = 50
|
|
const WINDOW_MS = 15 * 60 * 1000 // 15 minutes
|
|
|
|
/** Cleans up expired windows. Called on every check to prevent memory leaks. */
|
|
const cleanup = (now: number) => {
|
|
for (const [userId, window] of store) {
|
|
if (now >= window.resetAt) {
|
|
store.delete(userId)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns `true` if the user is within their rate limit.
|
|
* Returns `false` if they've exceeded it.
|
|
*/
|
|
export const checkRateLimit = (userId: number): boolean => {
|
|
const now = Date.now()
|
|
cleanup(now)
|
|
|
|
const existing = store.get(userId)
|
|
|
|
if (!existing || now >= existing.resetAt) {
|
|
store.set(userId, { count: 1, resetAt: now + WINDOW_MS })
|
|
return true
|
|
}
|
|
|
|
if (existing.count >= MAX_MESSAGES) {
|
|
return false
|
|
}
|
|
|
|
existing.count++
|
|
return true
|
|
}
|