Repository navigation
api
AI Knowledge Cloud exposes four API route groups under app/api/. All endpoints are protected by same-origin validation and rate limiting.
| Route | Method | Purpose |
|---|---|---|
/api/chat |
POST | Streaming AI chat with tool context and semantic caching |
/api/rag |
POST | RAG retrieval for tool context |
/api/inngest |
POST | Inngest webhook for background job events |
/api/auth/[...nextauth] |
GET/POST | NextAuth.js authentication handlers |
The primary user-facing API. Accepts a conversation history and returns a streamed AI response powered by Google Gemini.
Request body:
{
"messages": [{ "role": "user", "parts": [{ "type": "text", "text": "..." }] }],
"toolSlug": "optional-tool-slug",
"locale": "en"
}Constraints:
- Max 40 messages per request
- Max 8,000 characters per message text
- Max 50 parts per message
- Locale:
enorvi
Rate limits:
- 10 requests per minute per IP
- 50 requests per day per IP
Flow:
- Same-origin check (reject cross-origin POSTs)
- Rate limit check
- Parse and validate request with Zod schema
- Check semantic cache for cached answer
- If cache miss, stream response from Google Gemini
- Store completed answer in semantic cache asynchronously
The chat supports tool-scoped conversations (when toolSlug is provided, the system prompt includes context about that specific tool) and global conversations.
Key files:
-
app/api/chat/route.ts-- Chat endpoint handler -
lib/rate-limit.ts-- Rate limiting with IP resolution -
lib/semantic-cache.ts-- Answer caching -
services/ai-chat-tools.ts-- YouTube search tool available to the LLM
Provides retrieval-augmented generation for tool-related queries. Uses the fused query router to classify intent and applies the appropriate retrieval strategy.
Key files:
-
app/api/rag/route.ts-- RAG endpoint handler -
lib/rag.ts-- RAG pipeline with query routing -
lib/fused-query-router.ts-- Intent classification
Receives events from Inngest and dispatches them to the registered functions. This is the bridge between Inngest's event system and the application's background job handlers.
Key files:
-
app/api/inngest/route.ts-- Inngest webhook handler -
services/inngest.ts-- Inngest client and function registration -
functions/-- All job function implementations
See Background jobs for details on each job.
NextAuth.js handlers for Google OAuth login. Used by the admin dashboard.
Key files:
-
app/api/auth/[...nextauth]/route.ts-- NextAuth route handler -
lib/auth.ts-- Auth configuration -
lib/auth.config.ts-- Auth provider config
All API endpoints enforce:
- Same-origin validation -- POST requests must originate from the same domain
- Rate limiting -- per-IP limits with sliding window
- Input validation -- Zod schemas on all request bodies
- Security headers -- applied by Caddy reverse proxy (HSTS, X-Frame-Options, CSP, etc.)
See Deployment for header configuration and Background jobs for Inngest security.