AI Infrastructure & Providers
Relevant source files
The following files were used as context for generating this wiki page:
The AI infrastructure within the Tuturuuu platform is a multi-provider ecosystem designed to integrate leading large language models (LLMs) into a unified workspace environment. The system utilizes the Vercel AI SDK to orchestrate interactions across different providers including OpenAI, Anthropic, and Google (both Generative AI and Vertex AI). This infrastructure supports core platform features like Rewise (multi-model aggregation) and Mira (the platform's central AI companion).
Sources: README.md:46-52, GEMINI.md:32-34, conductor/tech-stack.md:27-31
Core Architecture & Request Flow
The AI infrastructure is built on a "gateway" pattern where different model providers are accessed through standardized internal API routes. Every request follows a structured lifecycle involving authentication, message validation, state persistence in Supabase, and credit management.
Request Processing Lifecycle
When a chat request is initiated, the platform performs the following steps:
- Authentication: Verifies the user's Supabase session.
- Chat Management: Checks for an existing chat ID or retrieves the most recent chat for the user.
- Message Validation: Ensures individual message parts do not exceed
MAX_CHAT_MESSAGE_LENGTH. - Persistence (Prompt): Saves the user's latest prompt to the database before calling the AI provider.
- Execution: Streams the AI response using the Vercel AI SDK.
- Persistence (Completion): Records the AI's response, token usage, and finish reason upon completion.
Sources: packages/ai/src/chat/anthropic/route.ts:25-100, packages/ai/src/chat/google/route.ts:220-300
AI Request Sequence
The following diagram illustrates the sequence of operations for an AI chat request:
Model Providers & Implementation
Tuturuuu integrates multiple AI providers through specialized API routes. Each route is configured with provider-specific options such as safety settings and caching.
| Provider | Implementation Path | Specific Features |
|---|---|---|
packages/ai/src/chat/google/route.ts | Multi-modal support (images/files), AI credit deduction, and dynamic storage retrieval. | |
| Anthropic | packages/ai/src/chat/anthropic/route.ts | Uses Anthropic gateway with cacheControl enabled. |
| Google Vertex | packages/ai/src/chat/google-vertex/route.ts | Native Vertex AI integration on the edge. |
| OpenAI | packages/ai/src/chat/openai/route.ts | Standardized OpenAI gateway integration. |
Sources: packages/ai/src/chat/google/route.ts:394-406, packages/ai/src/chat/anthropic/route.ts:114-118, packages/ai/src/chat/google-vertex/route.ts:1-20
Multi-Modal Data Handling
The Google implementation specifically handles file attachments. It retrieves files from Supabase storage based on a workspace ID (wsId) and chat ID, converting them into appropriate FilePart, ImagePart, or TextPart formats for the model.
Sources: packages/ai/src/chat/google/route.ts:25-112
System Instructions & Personality (Mira)
All internal AI providers are configured with a standardized systemInstruction. This defines "Mira," the platform's central AI personality.
Mira Guidelines
- Formatting: Mira must use Markdown and LaTeX for math formulas.
- Special Tags: The infrastructure supports custom UI components via special tags:
@<QUIZ>: For interactive learning quizzes.@<FLASHCARD>: For study flashcards.@<FOLLOWUP>: For suggested user responses.
- Visualizations: Mira is instructed to use Mermaid diagrams only when specifically requested by the user.
Sources: packages/ai/src/chat/anthropic/route.ts:168-300, packages/ai/src/chat/google/route.ts:420-550
Credit and Resource Management
To prevent resource abuse and manage costs, the AI infrastructure implements credit checks and token capping.
AI Credit Workflow
For specific providers (currently visible in the Google route), the system performs:
- Pre-flight Check: Validates if the workspace has sufficient credits for the requested model and feature.
- Capping: Dynamically adjusts
maxOutputTokensbased on the remaining credit budget to prevent overspending. - Deduction: Asynchronously deducts credits after a successful completion based on actual input, output, and reasoning tokens.
Sources: packages/ai/src/chat/google/route.ts:328-360, packages/ai/src/chat/google/route.ts:413-433
Data Models (AI Chat)
The infrastructure relies on specific database tables in Supabase for persistence.
| Table | Purpose |
|---|---|
ai_chats | Stores the metadata and ownership of a chat session. |
ai_chat_messages | Stores individual messages, including content, role (USER/ASSISTANT), model name, and token usage statistics. |
Sources: packages/ai/src/chat/anthropic/route.ts:51-140, packages/ai/src/chat/google/route.ts:382-410
The AI Infrastructure & Providers system provides a robust, scalable foundation that abstracts the complexities of multiple LLM providers while ensuring consistent personality, security, and resource management across the Tuturuuu platform.