-
Notifications
You must be signed in to change notification settings - Fork 3.7k
docs: Ask AI chat grounded in the docs vector store #5172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ouiliame
wants to merge
14
commits into
simstudioai:staging
Choose a base branch
from
ouiliame:feat/docs-ask-ai
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+535
−1
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d62e861
docs: Ask AI chat grounded in the docs vector store
ouiliame c9c01c1
docs: default Ask AI to gpt-5.4-mini
ouiliame 0b3d362
docs: render Ask AI answers as markdown + harden the chat endpoint
ouiliame 7e78c20
docs: relax Ask AI request caps; count only user-authored input
ouiliame 4d010fe
docs: address Greptile + Cursor review on Ask AI
ouiliame 55c6b0e
docs: validate message shape on Ask AI route (400, not 500)
ouiliame d807260
Merge remote-tracking branch 'origin/staging' into feat/docs-ask-ai
ouiliame c214bf6
docs: add per-IP rate limiting to the Ask AI route
ouiliame f53cdbd
docs: fix Ask AI message handling (Cursor review)
ouiliame 6fb1878
docs: keyword search for non-English Ask AI retrieval
ouiliame a50a8a0
docs: address Cursor review (locale SQL filter, empty history, origin…
ouiliame c311ea8
docs: stop the Ask AI stream when the panel is closed
ouiliame 14f9fcb
docs: send Ask AI locale per-message to avoid stale transport
ouiliame 25a0972
docs: apply similarity threshold to English Ask AI retrieval
ouiliame File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,302 @@ | ||
| import { openai } from '@ai-sdk/openai' | ||
| import { convertToModelMessages, stepCountIs, streamText, tool, type UIMessage } from 'ai' | ||
| import { sql } from 'drizzle-orm' | ||
| import { z } from 'zod' | ||
| import { db, docsEmbeddings } from '@/lib/db' | ||
| import { generateSearchEmbedding } from '@/lib/embeddings' | ||
|
|
||
| export const runtime = 'nodejs' | ||
| export const maxDuration = 30 | ||
|
|
||
| /** Model used for the Ask AI chat. Override with OPENAI_CHAT_MODEL in the environment. */ | ||
| const CHAT_MODEL = process.env.OPENAI_CHAT_MODEL || 'gpt-5.4-mini' | ||
|
|
||
| /** Max documentation chunks returned per search to ground an answer. */ | ||
| const SEARCH_LIMIT = 6 | ||
|
|
||
| /** Candidates pulled before locale filtering, so a locale still yields SEARCH_LIMIT results. */ | ||
| const SEARCH_CANDIDATES = SEARCH_LIMIT * 4 | ||
|
|
||
| /** Minimum cosine similarity for an English vector match (mirrors the site search route). */ | ||
| const SIMILARITY_THRESHOLD = 0.6 | ||
|
|
||
| /** Locales the docs are published in (mirrors the site search route). */ | ||
| const KNOWN_LOCALES = ['en', 'es', 'fr', 'de', 'ja', 'zh'] | ||
| const DEFAULT_LOCALE = 'en' | ||
|
|
||
| /** Postgres full-text config per locale (mirrors the site search route). */ | ||
| const TS_CONFIG: Record<string, string> = { | ||
| en: 'english', | ||
| es: 'spanish', | ||
| fr: 'french', | ||
| de: 'german', | ||
| ja: 'simple', | ||
| zh: 'simple', | ||
| } | ||
|
|
||
| /** | ||
| * Abuse guards. This endpoint proxies a paid LLM, so an unauthenticated public | ||
| * route is a target for scripted "free inference". These bounds cap the cost of | ||
| * any single request; an in-memory per-IP rate limit (below) caps volume on the | ||
| * hot path. A shared-store rate limit, a provider spend cap, and edge bot | ||
| * protection remain the durable controls (see the PR checklist). | ||
| * | ||
| * The size cap counts only user-authored text — NOT the conversation history, | ||
| * assistant turns, or retrieved doc chunks we add via the searchDocs tool, which | ||
| * legitimately grow large over a multi-turn chat. | ||
| */ | ||
| const MAX_MESSAGES = 200 | ||
| const MAX_USER_INPUT_CHARS = 400_000 | ||
| const MAX_OUTPUT_TOKENS = 4000 | ||
| const MAX_STEPS = 6 | ||
| /** Backstop on the whole serialized payload — blocks stuffing assistant/tool parts past the user-text cap. */ | ||
| const MAX_TOTAL_CHARS = 1_000_000 | ||
|
|
||
| /** | ||
| * Per-IP rate limit. Fixed window, in-memory: this bounds volume from a single | ||
| * source on a warm instance without external infra. It is best-effort on | ||
| * serverless (state is per-instance, not shared across regions/cold starts); | ||
| * a shared store (e.g. Vercel KV) and an edge WAF remain the durable controls, | ||
| * but this closes the "no volume limit at all" gap on the hot path. | ||
| */ | ||
| const RATE_LIMIT_MAX = 20 | ||
| const RATE_LIMIT_WINDOW_MS = 60_000 | ||
| const rateLimitHits = new Map<string, { count: number; resetAt: number }>() | ||
|
|
||
| /** Resolve the client IP from forwarding headers, falling back to a shared bucket. */ | ||
| function getClientIp(req: Request): string { | ||
| const forwarded = req.headers.get('x-forwarded-for') | ||
| if (forwarded) return forwarded.split(',')[0].trim() | ||
| return req.headers.get('x-real-ip') ?? 'unknown' | ||
| } | ||
|
|
||
| /** Fixed-window check. Returns retry-after seconds when the caller is over the limit, else null. */ | ||
| function rateLimit(ip: string, now: number): number | null { | ||
| const entry = rateLimitHits.get(ip) | ||
| if (!entry || now >= entry.resetAt) { | ||
| rateLimitHits.set(ip, { count: 1, resetAt: now + RATE_LIMIT_WINDOW_MS }) | ||
| return null | ||
| } | ||
| if (entry.count >= RATE_LIMIT_MAX) { | ||
| return Math.ceil((entry.resetAt - now) / 1000) | ||
| } | ||
| entry.count += 1 | ||
| return null | ||
| } | ||
|
|
||
| /** Drop expired buckets so the Map doesn't grow unbounded on a long-lived instance. */ | ||
| function sweepRateLimit(now: number): void { | ||
| if (rateLimitHits.size < 10_000) return | ||
| for (const [ip, entry] of rateLimitHits) { | ||
| if (now >= entry.resetAt) rateLimitHits.delete(ip) | ||
| } | ||
| } | ||
|
|
||
| /** A structurally valid UI message: has a role and a parts array. */ | ||
| function isValidMessage(message: unknown): message is UIMessage { | ||
| return ( | ||
| typeof message === 'object' && | ||
| message !== null && | ||
| typeof (message as { role?: unknown }).role === 'string' && | ||
| Array.isArray((message as { parts?: unknown }).parts) | ||
| ) | ||
| } | ||
|
|
||
| /** Total length of user-authored text across the conversation. */ | ||
| function userInputChars(messages: UIMessage[]): number { | ||
| let total = 0 | ||
| for (const message of messages) { | ||
| if (message.role !== 'user') continue | ||
| for (const part of message.parts) { | ||
| if (part.type === 'text' && typeof part.text === 'string') total += part.text.length | ||
| } | ||
| } | ||
| return total | ||
| } | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Strip everything the model shouldn't trust from client-supplied history: | ||
| * drop `system` messages (client-injected instructions) and every non-text part | ||
| * (e.g. crafted tool results faking searchDocs output). Only user/assistant text | ||
| * survives, so grounding comes from the server-run searchDocs tool — not the | ||
| * client's payload. | ||
| */ | ||
| function sanitizeMessages(messages: UIMessage[]): UIMessage[] { | ||
| return messages | ||
| .filter((message) => message.role === 'user' || message.role === 'assistant') | ||
| .map((message) => ({ | ||
| ...message, | ||
| parts: message.parts.filter((part) => part.type === 'text' && typeof part.text === 'string'), | ||
| })) | ||
| .filter((message) => message.parts.length > 0) | ||
| } | ||
|
|
||
| /** | ||
| * Reject obvious cross-origin calls. Same-origin browser requests send an | ||
| * `Origin` header matching the host; we allow those, plus any host in | ||
| * DOCS_ALLOWED_ORIGINS (comma-separated). Requests with no Origin (e.g. curl) | ||
| * are allowed through to the cost caps rather than blocked, since Origin is | ||
| * trivially spoofable and is a filter, not a security boundary. | ||
| */ | ||
| function isAllowedOrigin(req: Request): boolean { | ||
| const origin = req.headers.get('origin') | ||
| if (!origin) return true | ||
|
|
||
| let originHost: string | ||
| try { | ||
| originHost = new URL(origin).host.toLowerCase() | ||
| } catch { | ||
| return false | ||
| } | ||
|
|
||
| const forwardedHost = req.headers.get('x-forwarded-host') ?? req.headers.get('host') | ||
| const requestHost = forwardedHost?.split(',')[0].trim().toLowerCase() | ||
| if (requestHost && originHost === requestHost) return true | ||
|
|
||
| const allowlist = (process.env.DOCS_ALLOWED_ORIGINS ?? '') | ||
| .split(',') | ||
| .map((value) => value.trim().toLowerCase()) | ||
| .filter(Boolean) | ||
| return allowlist.includes(originHost) | ||
| } | ||
|
|
||
| const SYSTEM_PROMPT = `You are the documentation assistant for Sim — the open-source AI workspace where teams build, deploy, and manage AI agents. | ||
|
|
||
| Answer questions about Sim using the documentation. Always call the searchDocs tool before answering anything specific about Sim's features, configuration, or usage — do not answer from memory. Base your answer only on the returned documentation; if the docs do not cover the question, say so plainly rather than guessing. | ||
|
|
||
| Guidelines: | ||
| - Be direct and concrete. Lead with the answer, then the detail. | ||
| - Reference the relevant pages by their titles so the user knows where to read more. | ||
| - When you show configuration or code, keep it minimal and correct. | ||
| - The agent is called "Sim" and the chat surface is "Chat" — never say "Mothership" or "copilot". | ||
| - If a question is unrelated to Sim, briefly say it's outside the docs' scope.` | ||
|
|
||
| const SEARCH_COLUMNS = { | ||
| title: docsEmbeddings.headerText, | ||
| url: docsEmbeddings.sourceLink, | ||
| content: docsEmbeddings.chunkText, | ||
| sourceDocument: docsEmbeddings.sourceDocument, | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve candidate chunks for grounding. English docs are embedded, so they | ||
| * use vector similarity; other locales rely on Postgres full-text keyword search | ||
| * (vector search over English-trained embeddings does not serve them) — the same | ||
| * split the site search route makes. | ||
| */ | ||
| /** | ||
| * SQL predicate selecting only the locale's documents, so the row limit applies | ||
| * to matching rows (mirrors `matchesLocale`): non-English docs are prefixed with | ||
| * their locale segment; English is everything not prefixed with another locale. | ||
| */ | ||
| function localeFilter(locale: string) { | ||
| const firstSegment = sql`split_part(${docsEmbeddings.sourceDocument}, '/', 1)` | ||
| if (locale === DEFAULT_LOCALE) { | ||
| const others = KNOWN_LOCALES.filter((l) => l !== DEFAULT_LOCALE) | ||
| return sql`${firstSegment} not in (${sql.join( | ||
| others.map((l) => sql`${l}`), | ||
| sql`, ` | ||
| )})` | ||
| } | ||
| return sql`${firstSegment} = ${locale}` | ||
| } | ||
|
|
||
| async function searchDocs(query: string, locale: string) { | ||
| let rows: Array<{ title: string; url: string; content: string; sourceDocument: string }> | ||
|
|
||
| if (locale === DEFAULT_LOCALE) { | ||
| const embedding = await generateSearchEmbedding(query) | ||
| const vectorLiteral = JSON.stringify(embedding) | ||
| rows = await db | ||
| .select(SEARCH_COLUMNS) | ||
| .from(docsEmbeddings) | ||
| .where( | ||
| sql`1 - (${docsEmbeddings.embedding} <=> ${vectorLiteral}::vector) >= ${SIMILARITY_THRESHOLD} and ${localeFilter(locale)}` | ||
| ) | ||
| .orderBy(sql`${docsEmbeddings.embedding} <=> ${vectorLiteral}::vector`) | ||
| .limit(SEARCH_CANDIDATES) | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } else { | ||
| const tsConfig = TS_CONFIG[locale] ?? 'simple' | ||
| rows = await db | ||
| .select(SEARCH_COLUMNS) | ||
| .from(docsEmbeddings) | ||
| .where( | ||
| sql`${docsEmbeddings.chunkTextTsv} @@ plainto_tsquery(${tsConfig}, ${query}) and ${localeFilter(locale)}` | ||
| ) | ||
| .orderBy( | ||
| sql`ts_rank(${docsEmbeddings.chunkTextTsv}, plainto_tsquery(${tsConfig}, ${query})) DESC` | ||
| ) | ||
| .limit(SEARCH_CANDIDATES) | ||
| } | ||
|
|
||
| return rows.slice(0, SEARCH_LIMIT).map((row) => ({ | ||
| title: row.title, | ||
| url: row.url, | ||
| content: row.content, | ||
| })) | ||
| } | ||
|
|
||
| export async function POST(req: Request) { | ||
| if (!isAllowedOrigin(req)) { | ||
| return new Response('Forbidden', { status: 403 }) | ||
| } | ||
|
|
||
| const now = Date.now() | ||
| sweepRateLimit(now) | ||
| const retryAfter = rateLimit(getClientIp(req), now) | ||
| if (retryAfter !== null) { | ||
| return new Response('Too many requests', { | ||
| status: 429, | ||
| headers: { 'Retry-After': String(retryAfter) }, | ||
| }) | ||
| } | ||
|
|
||
| let body: { messages: UIMessage[]; locale?: string } | ||
| try { | ||
| body = await req.json() | ||
| } catch { | ||
| return new Response('Invalid JSON', { status: 400 }) | ||
| } | ||
| const { messages } = body | ||
| const locale = KNOWN_LOCALES.includes(body.locale ?? '') | ||
| ? (body.locale as string) | ||
| : DEFAULT_LOCALE | ||
|
|
||
| if (!Array.isArray(messages) || messages.length === 0 || messages.length > MAX_MESSAGES) { | ||
| return new Response('Invalid request', { status: 400 }) | ||
| } | ||
| if (!messages.every(isValidMessage)) { | ||
| return new Response('Invalid request', { status: 400 }) | ||
| } | ||
| if (userInputChars(messages) > MAX_USER_INPUT_CHARS) { | ||
| return new Response('Request too large', { status: 413 }) | ||
| } | ||
| if (JSON.stringify(messages).length > MAX_TOTAL_CHARS) { | ||
| return new Response('Request too large', { status: 413 }) | ||
| } | ||
|
|
||
| const modelMessages = sanitizeMessages(messages) | ||
| if (modelMessages.length === 0) { | ||
| return new Response('Invalid request', { status: 400 }) | ||
| } | ||
|
|
||
| const result = streamText({ | ||
| model: openai(CHAT_MODEL), | ||
| system: SYSTEM_PROMPT, | ||
| messages: convertToModelMessages(modelMessages), | ||
| stopWhen: stepCountIs(MAX_STEPS), | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| maxOutputTokens: MAX_OUTPUT_TOKENS, | ||
| tools: { | ||
| searchDocs: tool({ | ||
| description: | ||
| 'Search the Sim documentation for relevant content. Use this before answering any question about Sim.', | ||
| inputSchema: z.object({ | ||
| query: z.string().describe('A focused natural-language search query.'), | ||
| }), | ||
| execute: async ({ query }) => searchDocs(query, locale), | ||
| }), | ||
| }, | ||
| }) | ||
|
|
||
| return result.toUIMessageStreamResponse() | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.