API Reference
Complete API reference for @tour-kit/ai: client hooks, server utilities, chat components, and configuration types
Complete public API surface for @tour-kit/ai, split by entry point. Server-only utilities live under @tour-kit/ai/server so they don't leak into the browser bundle. Types referenced here are exported from the same entry point unless otherwise noted.
Entry points
| Import path | Runs in | Contains |
|---|---|---|
@tour-kit/ai | Browser + Node | Provider, hooks, components, types, client-side rate limiter |
@tour-kit/ai/server | Node / Edge only | createChatRouteHandler, RAG pipeline, embedding utilities, server rate limiter |
@tour-kit/ai/headless | Browser + Node | Headless primitive variants (no styling) |
Client Exports (@tour-kit/ai)
Provider
<AiChatProvider config={...}>
Root provider. Must wrap any component that uses useAiChat or useTourAssistant. Connects to the server route specified in config.endpoint.
| Config field | Type | Default | Notes |
|---|---|---|---|
endpoint | string | — | Required. URL of the createChatRouteHandler route (e.g. /api/chat). |
tourContext | boolean | false | When true, includes the active tour's TourAssistantContext in every request. Required for useTourAssistant server-side context. |
initialMessages | UIMessage[] | [] | Pre-populate the chat history. |
rateLimiter | SlidingWindowRateLimiter | null | null | Optional client-side rate limiter. Returns 429 to sendMessage when over budget. |
Hooks
useAiChat(): UseAiChatReturn
Core chat hook. Must be used within AiChatProvider.
| Property | Type | Description |
|---|---|---|
messages | UIMessage[] | Chat message history |
sendMessage | (input: { text: string }) => void | Send a user message |
status | ChatStatus | 'ready' | 'submitted' | 'streaming' | 'error' |
error | Error | null | Current error, if any |
stop | () => void | Stop the current generation |
reload | () => void | Regenerate the last response |
setMessages | (messages: UIMessage[]) => void | Replace message history (pass [] to clear) |
isOpen | boolean | Whether the chat panel is open |
open | () => void | Open the chat panel |
close | () => void | Close the chat panel |
toggle | () => void | Toggle the chat panel |
Throws if called outside an <AiChatProvider>.
useAiChatContext(): AiChatContextValue
Lower-level escape hatch — returns the raw context value. Use useAiChat instead unless you're building a custom hook.
useTourAssistant()
Tour-aware chat hook. Extends useAiChat with tour context.
Returns UseTourAssistantReturn (extends UseAiChatReturn):
| Property | Type | Description |
|---|---|---|
tourContext | TourAssistantContext | Current tour state |
isLoading | boolean | Whether the assistant is loading |
suggestions | string[] | Contextual suggestions based on current step |
askAboutStep | () => void | Ask the AI about the current tour step |
askForHelp | (topic?: string) => void | Ask for help, optionally on a topic |
...all UseAiChatReturn properties |
useSuggestions(): UseSuggestionsReturn
AI-generated follow-up suggestions based on the current conversation. Throws if no provider is mounted.
| Property | Type | Description |
|---|---|---|
suggestions | string[] | Current suggestions |
isLoading | boolean | Loading state |
useOptionalSuggestions(): UseSuggestionsReturn | null
Same as useSuggestions but returns null instead of throwing when no provider is mounted. Use in optional/conditional UI.
Components
All components are shadcn/ui-compatible and accept standard className overrides.
| Component | Purpose |
|---|---|
<AiChatPanel> | Full chat surface — header, message list, input, suggestions |
<AiChatToggle> | Floating button that toggles the panel |
<AiChatHeader> | Header bar — title, close button, optional menu |
<AiChatMessageList> | Renders messages[] with autoscroll |
<AiChatMessage> | Single message row (user or assistant) |
<AiChatInput> | Text input + send button + status indicator |
<AiChatSuggestions> | Renders suggested follow-up chips |
<AiChatPortal> | Portal wrapper for the panel (used internally by <AiChatPanel>) |
See Components for prop tables.
Client utilities
SlidingWindowRateLimiter
Class implementing sliding-window rate limiting. Pass an instance to AiChatProvider.config.rateLimiter to enforce per-user limits before the request leaves the browser.
createRateLimiter(config): SlidingWindowRateLimiter
Factory function. config accepts { maxRequests, windowMs }.
createAnalyticsBridge(config)
Subscribes to AI chat events (message_sent, message_received, error) and forwards them to the @tour-kit/analytics tracker.
Server Exports (@tour-kit/ai/server)
Route handling
createChatRouteHandler(options): { POST }
Returns an object with a Next.js / Edge-compatible POST handler. The handler:
- Parses the incoming
{ messages, tourContext? }payload. - Runs the configured rate limiter (if any).
- Builds the system prompt from
instructions+ retrieved context. - Streams the model response back as Server-Sent Events.
| Option | Type | Required | Notes |
|---|---|---|---|
model | LanguageModelV1 | yes | Any AI SDK model — openai('gpt-4o-mini'), anthropic('claude-sonnet-4-5'), etc. |
context.strategy | 'context-stuffing' | 'rag' | yes | Picks CAG or RAG mode. |
context.documents | Document[] | yes for CAG | All docs to inject (CAG) or to index (RAG seed). |
context.embedding | Embedding | yes for RAG | Output of createAiSdkEmbedding. |
context.vectorStore | VectorStoreAdapter | yes for RAG | Output of createInMemoryVectorStore or your own adapter. |
context.topK | number | no (RAG only) | Chunks to retrieve per query. Default 5. |
instructions.productName | string | no | Used in the default system prompt. |
instructions.tone | 'friendly' | 'professional' | 'concise' | no | Hints to the model. |
instructions.boundaries | string[] | no | Hard rules ("only answer X", "never reveal Y"). |
rateLimiter | ServerRateLimiter | no | Pre-check before model call. Returns 429 when exceeded. |
// app/api/chat/route.ts
import { createChatRouteHandler } from '@tour-kit/ai/server'
import { openai } from '@ai-sdk/openai'
const { POST } = createChatRouteHandler({
model: openai('gpt-4o-mini'),
context: {
strategy: 'context-stuffing',
documents: [{ id: 'doc-1', content: '...' }],
},
instructions: {
productName: 'My App',
tone: 'friendly',
boundaries: ['Only answer questions about My App onboarding.'],
},
})
export { POST }RAG pipeline
createInMemoryVectorStore(): VectorStoreAdapter
In-memory vector store. Re-built on every server restart — for dev and prototypes only. Implements the VectorStoreAdapter interface so it's swappable with pgvector, Pinecone, or your own store.
createAiSdkEmbedding(options): Embedding
Embedding adapter wrapping the AI SDK's embedding API. Options:
| Option | Type | Notes |
|---|---|---|
model | string | Embedding model id (e.g. 'text-embedding-3-small'). See the RAG guide for recommendations. |
provider | EmbeddingProvider | Optional explicit provider; defaults to OpenAI. |
createRetriever(options): Retriever
Convenience wrapper around { vectorStore, embedding, topK } for use outside createChatRouteHandler (e.g. background jobs that surface "you might be interested in" suggestions).
chunkDocument(doc, options) / chunkDocuments(docs, options)
Splits documents into chunks suitable for embedding. Options: { chunkSize, overlap }. Recommended starting values: chunkSize: 512, overlap: 50.
createRAGMiddleware(options)
Lower-level building block — produces middleware that injects retrieved context into a chat request. Use when you need to compose RAG with custom request handling outside createChatRouteHandler.
Server utilities
createSystemPrompt(config): string
Builds the structured system prompt that createChatRouteHandler uses internally. Export it if you want to log, audit, or modify the final prompt before forwarding to the model.
createServerRateLimiter(config): ServerRateLimiter
Server-side rate limiter. Pass an instance to createChatRouteHandler.rateLimiter. Pairs with createInMemoryRateLimitStore or your own Redis-backed store.
createInMemoryRateLimitStore(): RateLimitStore
In-memory rate-limit store. Scoped to a single Node process — for production use a Redis-backed store across instances.
generateSuggestions(options) / parseSuggestions(text)
Generate and parse AI follow-up suggestion chips. generateSuggestions calls the model with a structured prompt; parseSuggestions extracts the suggestion array from a raw response.
Types
The full type definitions live in packages/ai/src/types. The most commonly imported types:
import type {
// Provider config
AiChatConfig,
// Hook returns
UseAiChatReturn,
UseTourAssistantReturn,
UseSuggestionsReturn,
// Context shapes
AiChatContextValue,
TourAssistantContext,
// Server
ChatRouteHandlerOptions,
VectorStoreAdapter,
Document,
Embedding,
Retriever,
// Status
ChatStatus,
} from '@tour-kit/ai'Ship onboarding, not config.
npm i @tour-kit/core is MIT and free. The Pro packages work unlicensed too — a one-time $99 license removes the production watermark when you ship.
MIT-licensed — no signup, no credit card. Pay once, only when you ship.
Hooks
React hooks for @tour-kit/ai — useAiChat, useAiChatContext, useSuggestions/useOptionalSuggestions, useTourAssistant
Build with LLMs
Use Claude Code, ChatGPT, Cursor, and other LLM coding assistants to build with userTourKit faster — Claude Code plugin, llms.txt, per-package context files, and MCP.