Realtime Systems
Relevant source files
The following files were used as context for generating this wiki page:
Realtime systems in the Tuturuuu platform facilitate instantaneous data synchronization and interactive user experiences across various modules, including AI chat interfaces and collaborative tools. The architecture primarily leverages Supabase Channels for low-latency communication and Vercel AI SDK's streaming capabilities for generative AI responses.
The platform prioritizes high-performance synchronization through two main patterns: streaming generative text for AI interactions and Supabase Broadcast for client-to-client messaging in collaborative environments like Kanban boards. This approach ensures that users receive updates without manual page refreshes, maintaining a seamless "operating system" feel for modern work.
Sources: GEMINI.md, packages/ai/src/chat/google/route.ts
AI Streaming and Chat Integration
The AI chat system implements a realtime streaming architecture using streamText and smoothStream. This allows the UI to render AI responses word-by-word as they are generated, rather than waiting for the entire completion.
Architecture and Data Flow
The flow begins with a user request containing message history and model selection. The backend validates permissions, checks AI credits, and initiates a stream with the selected provider (Google, OpenAI, or Anthropic).
The system includes a persistent storage layer where both user prompts and assistant responses are saved to the ai_chat_messages table upon completion of the stream.
Sources: packages/ai/src/chat/google-vertex/route.ts:74-142, packages/ai/src/chat/google/route.ts:333-380
Streaming Configuration
| Feature | Description | Implementation |
|---|---|---|
| Smooth Stream | Ensures consistent UI rendering speed | experimental_transform: smoothStream() |
| Edge Runtime | Low-latency execution near the user | export const preferredRegion = 'sin1' |
| Credit Capping | Prevents budget overruns in realtime | capMaxOutputTokensByCredits() |
| Response Format | Standardized UI message format | result.toUIMessageStreamResponse() |
Sources: packages/ai/src/chat/google/route.ts:16-17, packages/ai/src/chat/google/route.ts:316-328
Collaborative Realtime Synchronization
For collaborative features such as Kanban boards and task management, the platform utilizes Supabase Broadcast. This method is preferred over standard database polling or postgres_changes for its scalability and lower latency.
Supabase Broadcast Pattern
Broadcast allows client-to-client messaging without the overhead of Write-Ahead Log (WAL) dependency or Row-Level Security (RLS) evaluation for every message.
This pattern ensures that when one user moves a task, all other users viewing the same board see the change immediately.
Sources: GEMINI.md
Realtime Communication Protocols
The platform supports multiple realtime imports for different environments:
- Browser-side: Uses
@tuturuuu/supabase/next/clientfor subscription handling. - Server-side: Uses
@tuturuuu/supabase/next/realtimefor type-safe channel definitions. - Kanban Specific: Uses a
useBoardBroadcast()hook fromboard-broadcast-context.tsxto trigger cross-client sync.
Sources: packages/supabase/QUICKSTART.md, GEMINI.md
File and Media Processing in Realtime
The AI system handles file attachments by retrieving them from Supabase Storage and injecting them into the model's context window in realtime before starting the text stream.
Media Handling Logic
- Detection: The system identifies files stored in
${wsId}/chats/ai/resources/${chatId}. - Processing: Files are converted into
ImagePartorFilePartobjects based on their MIME type. - Injection: These parts are prepended or appended to the latest user message in the
ModelMessagearray.
Conclusion
Realtime systems in Tuturuuu are built on a foundation of streaming AI responses and broadcast-based collaboration. By utilizing Edge functions for AI and direct client-to-client messaging for task management, the platform minimizes latency while ensuring data consistency. The integration of credit management and file processing directly into the realtime flow allows for a robust, interactive user experience that scales across multiple AI providers and collaborative workflows.