Pular para o conteúdo

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

MethodPathAuthDescription
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
MethodPathAuthDescription
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.

MethodPathDescription
POST/api/admin/loginAdmin login
POST/api/admin/logoutInvalidate admin session
GET/api/admin/statsDashboard stats
GET/api/admin/usersList users (paginated, searchable)
GET/api/admin/listingsList listings (paginated, searchable)
GET/api/admin/roomsList chat rooms
GET/api/admin/messagesList chat messages

Utilities

MethodPathAuthDescription
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/login with 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 when AI_PROVIDER is 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

Terminal window
cd chat-worker
npx wrangler d1 migrations apply redecasas-chat --local
npm run dev

Deployment

Terminal window
cd chat-worker && npm run deploy

Note

Full authentication details: see Auth Worker or chat-worker/AUTH.md.