Chat Experience & Streaming
Relevant source files
The following files were used as context for generating this wiki page:
The Chat Experience in OS Chat is designed to provide a real-time, highly responsive interface for interacting with various Large Language Models (LLMs). It leverages a modern tech stack centered around TanStack Start for server-side rendering, Convex for real-time data persistence, and the Vercel AI SDK for managing complex streaming states and tool-calling logic. Sources: CLAUDE.md:38-41, src/components/chat/chat.tsx:645-660
This system handles everything from initial message submission and optimistic UI updates to server-side streaming via WebSockets or SSE, multi-modal content processing, and advanced chat management features like branching and editing. The architecture ensures that users experience minimal latency while maintaining a consistent conversation state across different devices and sessions. Sources: src/routes/api.chat.ts:634-650, README.md:65-75
Core Chat Architecture
The chat system follows a client-server-backend pattern. The frontend (React) manages the UI state and streaming lifecycle, the API route handles model-specific logic and provider integration, and Convex serves as the authoritative real-time database.
The diagram above illustrates the high-level data flow from a user request to the AI provider and back to the client, with parallel persistence in Convex.
Chat Instance Management
Chat instances are managed through a registry to preserve state across navigation. The chatRegistry stores ChatInstance objects, allowing in-flight streams to continue even if a user navigates away and back. Sources: src/components/chat/chat.tsx:55-65
- Registry Size: Limited to 20 instances to prevent memory leaks. Sources: src/components/chat/chat.tsx:75
- Draft Promotion: When a "Draft Chat" (new conversation) receives its first response, it is promoted to a real chat ID in the registry. Sources: src/components/chat/chat.tsx:62-73
Message Lifecycle & Streaming
The message lifecycle begins when a user submits input via the ChatInput component. The system performs several checks and transformations before the message reaches the AI provider.
1. Submission & Validation
Before sending, the system validates the input string length and checks rate limits based on the user's authentication status (e.g., 5 messages/day for anonymous, 20 for registered). Sources: src/components/chat/chat.tsx:431-440, src/routes/security.tsx:42-45
2. Optimistic UI Updates
For attachments, the system creates "optimistic" versions of files to show immediate progress in the UI before the actual upload to R2 storage is complete. Sources: src/components/chat/chat.tsx:380-390
3. Server-Side Execution (/api/chat)
The API route orchestrates the interaction between the user context and the AI model. It performs the following sequence:
- Auth & Context: Retrieves user profile and active connectors. Sources: src/routes/api.chat.ts:460-475
- System Prompt Construction: Builds a dynamic prompt including date/time, user traits, and available tools. Sources: src/lib/prompt_config.ts:316-350
- Transport Selection: Uses
OpenAIWebSocketFetchfor eligible models or standard SSE for others. Sources: src/routes/api.chat.ts:635-650 - Streaming: Uses
streamTextfrom Vercel AI SDK to handle deltas and tool calls. Sources: src/routes/api.chat.ts:690-710
| Parameter | Type | Description |
|---|---|---|
chatId | Id<"chats"> | Authoritative ID for the conversation. |
model | string | The specific model ID (e.g., gpt-4o). |
personaId | string (Optional) | Preset personality (e.g., software-engineer). |
enableSearch | boolean | Whether to allow web search tools. |
reasoningEffort | "low" | "medium" | "high" | Depth of thought for reasoning models. |
Sources: src/routes/api.chat.ts:140-150, src/components/chat/chat.tsx:112-120
Advanced Interaction Features
Chat Branching and Editing
OS Chat supports creating alternative conversation paths and modifying existing messages.
- Editing: When a message is edited, the system deletes all subsequent messages (descendants) in that thread and triggers a new completion from the edited point. Sources: src/routes/api.chat.ts:575-595, convex/messages.ts:310-330
- Branching: Users can create a new chat starting from any assistant message, preserving the context up to that point. Sources: src/components/chat/chat.tsx:515-520
Reasoning Models
For models supporting "Thinking" (e.g., Claude 3.5 Sonnet, Gemini 2.0 Flash Thinking), the system handles a "reasoning" stream. This is configured via REASONING_EFFORT_CONFIG, which maps UI settings (low/medium/high) to specific provider tokens or effort levels. Sources: src/routes/api.chat.ts:110-135, src/lib/model-utils.ts
Multi-modal Support
The chat handles images both as input and output.
- Input: Files are uploaded to R2 and stored as
partsin the Convexmessagestable. Sources: convex/messages.ts:80-100 - Output: For image generation models, the API route calls
generateImage, uploads the result, and saves it as an assistant message with afilepart. Sources: src/routes/api.chat.ts:335-375
Background & Scheduled Streaming
Beyond real-time user interaction, the system supports "Background Agents" which execute autonomously.
This sequence shows the autonomous execution flow of scheduled tasks, utilizing Redis for long-term memory.
- Memory Integration: Scheduled agents use
ScheduledAgentMemory(backed by Redis) to maintain state across different execution runs. Sources: convex/scheduled_ai.ts:250-270 - Autonomous Tools: Unlike the real-time chat, these agents are configured to make decisions without user intervention, summarized in a final report or email. Sources: convex/scheduled_ai.ts:200-220, src/lib/prompt_config.ts:225-240
Conclusion
The Chat Experience and Streaming module serves as the primary interface for the OS Chat platform. By combining persistent storage in Convex with volatile streaming states in the AI SDK, it provides a "best of both worlds" experience: the reliability of a traditional database and the speed of modern streaming architectures. Key features like reasoning effort management, branching, and autonomous background agents further distinguish the system as a comprehensive AI assistant rather than a simple chat interface. Sources: README.md:20-35, src/components/chat/chat.tsx:645-670