Chat Worker
The chat worker is the most complex worker, handling AI-powered chat, real-time WebSocket connections, site search, and admin dashboard endpoints.
API Endpoints
Chat
| Method | Path | Auth | Description |
|---|---|---|---|
GET | /api/ws?roomId=xxx | — | WebSocket upgrade for chat room |
GET | /api/chat/rooms | — | List chat rooms |
POST | /api/chat/rooms | — | Create or update chat room |
GET | /api/chat/history?roomId=xxx | — | Fetch room message history |
Search
| Method | Path | Auth | Description |
|---|---|---|---|
GET | /api/search?q=...&category=all&limit=10 | — | Site-wide search |
GET | /api/bucket-search?prefix=&type=all&limit=50 | — | R2 bucket object search |
GET | /api/page-content?url=/path | — | Extract page content from R2 or origin |
GET | /api/search/categories | — | List search index categories |
Admin
All admin endpoints require the X-Admin-Token header.
| Method | Path | Description |
|---|---|---|
POST | /api/admin/login | Admin login |
POST | /api/admin/logout | Invalidate admin session |
GET | /api/admin/stats | Dashboard stats |
GET | /api/admin/users | List users (paginated, searchable) |
GET | /api/admin/listings | List listings (paginated, searchable) |
GET | /api/admin/rooms | List chat rooms |
GET | /api/admin/messages | List chat messages |
Utilities
| Method | Path | Auth | Description |
|---|---|---|---|
GET | /api/check-link?url=https://... | — | HEAD check a URL (5s timeout) |
WebSocket: GET /api/ws?roomId=xxx
Real-time chat via WebSocket upgrade. Uses Cloudflare Durable Objects (ChatRoom).
- Room ID sanitized to max 200 characters
- No auth required for connection
- Messages broadcast to all connected clients in the room
Search Endpoints
GET /api/search?q=...&category=all&limit=10
Site-wide search across indexed content.
GET /api/bucket-search?prefix=&type=all&limit=50
Search R2 bucket objects.
GET /api/page-content?url=/path
Extract rendered page content from R2 or origin for AI context.
GET /api/search/categories
List available search index categories.
Admin Authentication
- Login:
POST /api/admin/loginwith password (SHA-256 hashed constant) - Token stored in KV as
admin:session:{token}with 24h TTL - Include
X-Admin-Token: <token>in all admin requests
Architecture
Chat Room System
src/chat-room.ts manages:
- WebSocket connections
- Message broadcasting
- AI provider integration
- Intent guarding (preventing off-topic queries)
AI Providers
src/ai-providers.ts abstracts multiple AI backends:
- DeepSeek (
deepseek-flash, the default whenAI_PROVIDERis unset) - OpenAI (
AI_PROVIDER=openai) - Kimi, OpenRouter, and custom OpenAI-compatible endpoints
- Cloudflare Workers AI (
AI_PROVIDER=cloudflare)
AI_FALLBACK_PROVIDER can name one secondary provider; requests are retried there if the primary throws. The intent classifier uses the same provider, base URL, key, and thinking-mode setting as the chat path.
Intent Guard
src/intent-guard.ts ensures chat responses stay on-topic for Brazilian real estate. It filters out:
- Off-topic political discussions
- Non-real-estate investment advice
- Harmful content
Search
src/search/ contains the site search implementation:
- Index management
- Query parsing
- Result ranking
Site Context
src/site-context.ts provides the AI with context about RedeCasas pages, scopes, and geographic data so it can answer questions accurately.
Agents
src/agents/ contains specialized AI agents for different tasks:
- Property recommendation
- Market analysis
- Location guidance
Development
cd chat-workernpx wrangler d1 migrations apply redecasas-chat --localnpm run devDeployment
cd chat-worker && npm run deployNote
Full authentication details: see Auth Worker or chat-worker/AUTH.md.