Rewise: AI Chat Assistant
Relevant source files
The following files were used as context for generating this wiki page:
Rewise is a core AI-powered learning application within the Tuturuuu ecosystem. It serves as an intelligent aggregator of leading artificial intelligence models, providing users with a unified conversational interface that supports multi-model integration, voice support, and advanced pedagogical features like automated quizzes and flashcards. Sources: README.md:29-30, README.md:58-59, packages/ai/src/chat/google/route.ts:503-518
The assistant is personified as Mira, an empathetic AI partner engineered to eliminate digital friction and amplify human creativity. Within the system architecture, Rewise acts as a bridge between user interactions, workspace storage, and external AI providers such as Google (Gemini/Vertex), OpenAI, and Anthropic. Sources: README.md:104-106, packages/ai/src/chat/google/route.ts:474-500, packages/ai/src/chat/openai/route.ts:16-30
Core Architecture and Multi-Model Integration
Rewise employs a modular routing system to handle requests across different AI providers. Each provider is implemented as an edge-compatible route that manages authentication, message validation, database persistence, and streaming responses. Sources: packages/ai/src/chat/google/route.ts:12-25, packages/ai/src/chat/openai/route.ts:12-25
Request Flow and Processing
When a user sends a message, the system performs several pre-flight checks:
- Authentication: Verifies the user's session via Supabase. Sources: packages/ai/src/chat/google/route.ts:192-200
- Message Validation: Ensures content does not exceed the
MAX_CHAT_MESSAGE_LENGTH. Sources: packages/ai/src/chat/google/route.ts:271-295 - Credit Verification: Checks if the workspace has sufficient AI credits to proceed with the selected model. Sources: packages/ai/src/chat/google/route.ts:348-360
- Resource Management: For file-attached chats, the system moves files from temporary user storage to the specific chat thread storage. Sources: packages/ai/src/chat/google/route.ts:227-268
The diagram shows the standard flow of a chat request from the user interface through the backend infrastructure to the AI provider. Sources: packages/ai/src/chat/google/route.ts:175-450
Pedagogical Features and System Instructions
Rewise is governed by a strict set of system instructions that transform standard AI responses into structured learning experiences. The "Mira" persona is instructed to utilize specific Markdown formatting and custom XML-like tags to render interactive components. Sources: packages/ai/src/chat/google/route.ts:503-535
Interactive Learning Tags
The system supports three primary interactive formats:
- Follow-up Prompts: Predictions of what the user might ask next, wrapped in
@<FOLLOWUP>tags. - Quizzes: Interactive multiple-choice questions using
@<QUIZ>,<QUESTION>, and<OPTION isCorrect>tags. - Flashcards: Definition or concept review using
@<FLASHCARD>,<QUESTION>, and<ANSWER>tags.
Sources: packages/ai/src/chat/google-vertex/route.ts:153-176
AI Personality Guidelines
| Attribute | Requirement |
|---|---|
| Name | Mira |
| Formatting | Mandatory GFM Tables, Markdown Headings, and Mermaid (if requested) |
| Language | Match the user's most recent message |
| Tone | Polite, respectful, professional, and helpful |
| Mathematics | Inline LaTeX for formulas (Anthropic/OpenAI specific) |
Sources: packages/ai/src/chat/google/route.ts:503-515, packages/ai/src/chat/anthropic/route.ts:171-175
File and Multimedia Analysis
A significant capability of Rewise is its ability to process uploaded files. The google route implementation includes specialized logic to retrieve all chat-related files from Supabase storage and inject them into the AI model's context. Sources: packages/ai/src/chat/google/route.ts:28-87
Processing Logic
The system identifies files stored at ${wsId}/chats/ai/resources/${chatId} and categorizes them by media type.
- Text/JSON: Read as strings and converted to
FilePart. - Images: Converted to
Uint8Arrayand passed asImagePart. - Binary/PDFs: Processed as
ArrayBufferand sent asFilePart.
This flowchart illustrates how file attachments are processed and integrated into the model messages before the AI request is initiated. Sources: packages/ai/src/chat/google/route.ts:90-130
Data Persistence and Credit Management
Rewise ensures every interaction is tracked for both user history and resource consumption.
Database Schema (ai_chat_messages)
| Field | Type | Description |
|---|---|---|
chat_id | UUID | Reference to the chat thread |
creator_id | UUID | ID of the user who sent/received the message |
content | Text | The actual message text |
role | Enum | ASSISTANT or USER |
model | Text | The specific model used (e.g., gemini-2.5-flash) |
prompt_tokens | Integer | Tokens used in the input |
completion_tokens | Integer | Tokens generated in the output |
Sources: packages/ai/src/chat/google/route.ts:420-435, packages/ai/src/chat/google-vertex/route.ts:114-124
Credit Deduction
After a successful stream (onFinish), the system calls deductAiCredits. This function calculates the cost based on inputTokens, outputTokens, and reasoningTokens specific to the model and feature used. Sources: packages/ai/src/chat/google/route.ts:446-465
Conclusion
Rewise: AI Chat Assistant (Mira) is a sophisticated integration of multi-model AI capabilities and structured learning tools. By combining robust backend validation, dynamic file injection, and a specialized pedagogical instruction set, it provides an intelligent partner for users within the Tuturuuu platform to manage complex workflows and educational tasks. Sources: README.md:10-15, packages/ai/src/chat/google/route.ts:503-518