| name | docs-ai-integration |
| description | Use when integrating a docs-ai chat widget into a documentation site or web app. Implement the floating chat chip, streaming chat panel, session management, search integration, and mobile bottom sheet for any framework (React, Astro, MKDocs, Docusaurus, Vue, vanilla JS, etc.) |
docs-ai Integration
Integrate a docs-ai-powered chat assistant into a documentation site. Implement idiomatically for the project's existing framework — this spec defines what to build and how it should behave, not how to code it.
Setup — ask first
Before writing any code, ask the user:
"What is the URL of your deployed docs-ai instance? (e.g. https://docs-ai.yoursite.com)"
Then wire it in using the framework's idiomatic environment variable pattern:
| Framework | Env var | How it's read |
|---|
| Vite / Astro / SvelteKit | PUBLIC_DOCS_AI_URL | import.meta.env.PUBLIC_DOCS_AI_URL |
| Next.js | NEXT_PUBLIC_DOCS_AI_URL | process.env.NEXT_PUBLIC_DOCS_AI_URL |
| MkDocs / plain HTML | DOCS_AI_URL | injected at build time or hardcoded |
| Docusaurus | DOCS_AI_URL | process.env.DOCS_AI_URL via docusaurus.config.js |
Never hardcode the URL. Always fall back gracefully (log a warning) if the env var is missing.
User Journey
- User lands on a docs page → sees a floating "Ask AI" chip fixed to the bottom-right corner
- User clicks chip → chat panel slides open (bottom sheet on mobile)
- User types a question, presses Enter → user bubble appears, assistant typing indicator shows immediately
- First token arrives → typing indicator transitions smoothly to streaming text
- Stream ends → text is rendered as markdown
- User asks a follow-up → conversation continues in the same session
- User clicks new conversation → session cleared, ready to start fresh
- User closes the panel → chip stays visible for re-opening
Search integration path (optional but recommended):
User types ≥ 2 chars in the site search dialog → an "Ask AI assistant" chip appears at the bottom of results. Clicking it closes search and opens the chat panel with the query pre-filled.
Features Checklist
Floating Chip (trigger)
Chat Panel — desktop
Mobile Bottom Sheet — ≤640px
Message Bubbles
Loading & Streaming
Input Behavior
Session Management
Page Context
Public JavaScript Bridge
Accessibility
Dark Mode
Error Handling
| Situation | User-facing message |
|---|
| HTTP 429 | "Rate limit reached. Please wait a moment before asking again." |
| Network failure | "Network error. Check your connection." |
| Non-2xx response | "Error {status}. Please try again." |
{"error": "..."} SSE event | Show the error text in a red bubble; remove the in-progress assistant bubble |
API Contract
POST /chat
Body: { message, session_id, current_page_slug }
Headers: Content-Type: application/json
Response headers:
Content-Type: text/event-stream
X-Session-ID: <uuid> ← read this; pass back as session_id on next request
Cache-Control: no-cache
SSE events (blank line between each):
data: {"chunk": "text delta"}
data: {"message_over": true} ← current assistant turn ended; more content may follow after tool execution
data: {"error": "error message"}
data: [DONE]
GET /chat?q=<query>
Response: text/plain, stateless, no session
SSE implementation note: Use fetch + ReadableStream + TextDecoder, not EventSource — EventSource does not support POST. Buffer partial lines across network chunks before parsing.
CORS: allow_origins=["*"]; X-Session-ID is an exposed CORS header, readable by the browser from response.headers.get('X-Session-ID').
Search Integration
Add to the site's existing search dialog:
- Show an "Ask AI" section when the search query is ≥ 2 characters
- Small muted label: "Ask AI assistant"
- Chip button that reads: "Can you tell me about {query}?"
- On click → close search dialog → call
window.__docsAiChat.open(query)