A map of the code for contributors: what runs when a site builds, what runs when someone asks a question, and which folder does what. CONTRIBUTING.md has the setup and the checks.
ondocs is one package with several entries. At build time it turns a site's pages into one file, ask-index.json. At request time two Web-standard handlers answer from that file: the ask endpoint for the dialog, and the MCP endpoint for agents. There is no database and no server of ondocs's own.
the site's pages ─▶ a SourceDocument per page ─▶ chunks ─▶ vectors ─▶ ask-index.json
src/loaders src/chunk.ts src/build.ts src/index-file.ts
- The pages. The framework plugins read the HTML the framework built, so every URL is one the site serves:
src/integration/pages.ts, withsrc/loaders/html.ts.ondocs indexreads Markdown, MDX and HTML from a folder instead, and works out the URLs from the framework's rules:src/node/index.ts(loadDirectory), withsrc/loaders/markdown.ts. - The chunks.
src/chunk.tssplits each page by its headings, then packs each section into chunks of at most 1,200 characters by default. Each chunk keeps its heading path and the anchor that citations link to. - The vectors.
src/build.ts(buildIndex) embeds the chunks with any AI SDK embedding model, reusing the previous build's vectors for chunks whose text has not changed, andsrc/quantize.tsstores each vector as int8. With no model, the index is keyword-only. - The file.
src/index-file.tswrites it deterministically, one chunk per line, so it diffs well; The index file describes the format and its compatibility rules.src/llms.tsmakesllms.txtand the pages' Markdown copies from the same pages.
The four framework plugins are thin. src/docusaurus, src/astro, src/starlight (built on src/astro) and src/vitepress each hook into their framework's build and dev server, and share the rest through src/integration (published as ondocs/integration): reading the built pages, the embedding model from its name, writing the index and the llms files, the option checks, and the launcher script that adds the dialog to every page. A new plugin starts there; see Writing an integration.
createAskHandler in src/server/handler.ts takes a Request and returns a streamed Response. In order:
- The request. The handler answers a CORS preflight itself. Then
requestGuardinsrc/server/shared.ts, which the MCP handler shares, checks that the request is a JSON POST, runs the rate limit (src/server/rate-limit.ts) and reads the body within its size limit. Then the question is validated, and the bot check runs (src/server/challenge.ts). - The index. It is loaded once per instance, or fetched by
remoteIndex(src/server/remote-index.ts), and parsed into BM25 and vector indexes byloadIndexinsrc/search/retrieve.ts. - The follow-up. A question that leans on the thread is rewritten into one that stands on its own, with one short model call:
src/server/follow-up.ts. - The cache and the budget. A question asked before is answered from the answer cache (
src/server/cache.ts). Any other is admitted by the day'sbudget(src/server/budget.ts). - Retrieval. The question is embedded (
embedQueryinsrc/server/shared.ts), thenretrieveinsrc/search/retrieve.tsfuses BM25 (src/search/bm25.ts) with the cosine scan (src/search/vectors.ts) by reciprocal rank (src/search/rrf.ts). - The relevance gate.
measureandgateHitsinsrc/search/retrieve.tskeep only chunks that clearminSimilarityorminKeywordCoverage. When none does, the handler streams "I don't know" and calls no model. Thererankandretrievehooks run insrc/server/retrieval.ts, and their hits go through the same gate. - The answer.
src/server/prompt.tsnumbers the sources and writes the prompt, the budget reserves the answer's worst case in tokens, andstreamTextfrom the AI SDK streams the answer. The sources go out first, assource-urlparts, then the text, as an AI SDK UI message stream (src/protocol.tshas the shapes both sides share).
createMcpHandler in src/server/mcp.ts runs the same guard, then answers MCP's JSON-RPC with the search, fetch and list_pages tools in src/server/mcp-tools.ts. search uses the same retrieval and gate, with a looser keyword gate, and nothing calls a language model. createOndocs (src/server/ondocs.ts) makes both handlers from one set of options.
In the browser, src/react has the dialog: AskDialog (ask-dialog.tsx) is the shortcut and the button, and loads dialog-panel.tsx, the dialog itself, the first time it is wanted. use-ask.ts posts the question and reads the stream (stream.ts), and answer.tsx renders the answer with its citations. src/embed mounts the same dialog on pages that are not a React app: mountAskDialog, with the site's React, and the script tag and ondocs/embed/standalone, which bring their own.
| Folder or file | What it is |
|---|---|
src/index.ts |
ondocs: building and searching an index, in any runtime. |
src/loaders, src/text |
Markdown, MDX and HTML into documents; slugs, links and the tokenizer. |
src/chunk.ts, src/build.ts |
Chunking and buildIndex. |
src/search |
BM25, the vector scan, fusion, the relevance gate, and finding a page by its URL. |
src/server |
ondocs/server: the ask and MCP endpoints and what they use. |
src/react |
ondocs/react: the dialog, the Ask button, useAsk and McpInstall. |
src/embed |
ondocs/embed: the script tag, mountAskDialog and the dialog that brings its own React. |
src/mock |
ondocs/mock: the embedding and language models for tests and demos without a key. |
src/node |
ondocs/node: reading a folder of pages, the index file on disk, and models named by a string. |
src/integration |
ondocs/integration: what the framework plugins share. |
src/docusaurus, src/astro, src/starlight, src/vitepress |
The framework plugins. |
src/cli |
The ondocs command: index, init (with the endpoint templates it writes), dev and eval. |
test/ |
Vitest, one file per behavior; test/corpus.test.ts measures search on docusaurus.io's own docs. |
examples/ |
The live demos; examples/nextjs is also the website, with the docs in content/docs. |
- The core runs anywhere.
ondocs,ondocs/server,ondocs/react,ondocs/embedandondocs/mockrun in edge runtimes, Workers and browsers, and so do the Astro endpoints,ondocs/astro/ask-routeandondocs/astro/mcp-route(Cloudflare Workers included), and the Docusaurus theme, in the browser. Nothing they import may import a Node.js built-in, not even a type, which would reach the published.d.ts. ESLint enforces it forsrc/*.ts,src/{embed,loaders,mock,react,search,server,text},src/astro/{ask-route,mcp-route,route,route-handlers}.ts,src/docusaurus/renamed.tsandsrc/docusaurus/theme. Node.js code goes insrc/node,src/integration,src/clior a plugin's build-time code:src/docusaurus/index.ts,src/astro/integration.ts,src/starlightandsrc/vitepress. - The old name stays where sites depend on it. The index format id, the store key prefixes and the
askMySitekey in the stream's source metadata keep the name from before ondocs, so sites built with either version keep working: see "The old name" in CONTRIBUTING.md. - The docs are written once. The pages are in
examples/nextjs/content/docs; the other three examples' copies are generated from them withpnpm examples:sync.