Multi-modal Inputs & Outputs
Relevant source files
The following files were used as context for generating this wiki page:
OS Chat supports a wide range of multi-modal capabilities, allowing users to interact with AI models using text, images, and file attachments. The system is designed to handle both multi-modal inputs (such as uploading images for analysis) and multi-modal outputs (such as AI-generated images or structured reasoning blocks). This functionality is integrated across the real-time chat interface and background automation agents.
The architecture leverages the Vercel AI SDK to stream multi-modal parts, including text, tool calls, and media, while utilizing Cloudflare R2 for persistent object storage and Convex for real-time state management.
Sources: README.md:46-51, src/routes/api.chat.ts:466-470
Multi-modal Input Architecture
The input system processes user messages that consist of multiple parts. A single user message can contain a combination of text and file attachments (images, documents, etc.).
Client-Side Processing
When a user submits a message with files, the frontend creates "optimistic attachments" to provide immediate visual feedback. These files are then uploaded in parallel to Cloudflare R2 via a server-side helper. Once uploaded, the blob URLs are replaced with permanent storage URLs before the message is sent to the AI provider.
Sources: src/components/chat/chat.tsx:556-578, src/components/chat/chat.tsx:644-656
Data Structure
Multi-modal inputs are represented as a collection of UIPart objects. The system differentiates between text parts and file parts.
| Part Type | Description | Key Fields |
|---|---|---|
text | Standard string-based input | text |
file | Attached media or documents | url, mediaType, filename |
Sources: src/routes/api.chat.ts:98-105, src/routes/api.chat.ts:251-255
Input Processing Flow
The following diagram illustrates how multi-modal inputs are handled from the UI to the AI API.
Multi-modal Output Generation
OS Chat supports generating diverse outputs, ranging from streaming text to high-fidelity images generated by specific models.
Image Generation
The system identifies models with the image-generation feature (e.g., GPT Image 1.5, Flux Schnell). For these models, the backend intercepts the standard chat flow to call the generateImage function. The resulting image buffer is uploaded to R2, and a file part is saved as the assistant's response.
Sources: src/routes/api.chat.ts:223-261, README.md:104-108
Reasoning and Thinking
Certain models (OpenAI o1/o3, DeepSeek R1, Claude Thinking) provide "reasoning" parts. The system tracks reasoningTokens and can render the AI's thinking process. This is configured via a reasoningEffort parameter (low, medium, high) which adjusts the thinkingBudget for the provider.
Sources: src/routes/api.chat.ts:168-185, src/routes/api.chat.ts:951-955
Output Part Management
Assistant responses are stored in Convex with a parts array to maintain the multi-modal structure. To prevent database performance issues, the system implements a depth limiter to ensure the parts object does not exceed Convex's nesting limits.
// Example of limiting depth for complex multi-modal parts
const depthLimitedParts = limitDepth(sanitizedParts, 14);
await client.mutation(api.messages.saveAssistantMessage, {
chatId,
role: "assistant",
content: capturedText,
parts: depthLimitedParts,
metadata: finalMetadata,
});
Sources: src/routes/api.chat.ts:1003-1011, convex/scheduled_ai.ts:251-254
Scheduled Multi-modal Tasks
Background agents also utilize multi-modal structures. When a scheduled_task executes, it can access tools and generate responses that include text and data summaries.
Sequence of Scheduled Execution
The executeTask action manages the autonomous generation of content, which may include tool outputs and memory updates.
Formatting and Rendering
The project uses a specialized markdown component to render multi-modal outputs. This includes support for:
- LaTeX: Mathematical expressions wrapped in
$$. - Mermaid: Architectural and flow diagrams.
- Code Blocks: Syntax-highlighted snippets via
streamdown. - Sources: Interactive link previews for web search results.
Sources: src/lib/prompt_config.ts:225-240, src/components/prompt-kit/markdown.tsx:14-25
Database Schema for Multi-modal Messages
The messages table in the Convex schema is designed to support the parts array, which is the core of multi-modal storage.
| Field | Type | Description |
|---|---|---|
role | string | "user" or "assistant" |
content | string | Plain text fallback/summary |
parts | array | List of UIPart (text, file, reasoning, etc.) |
metadata | object | Model info, token usage, search status |
Sources: convex/schema.ts (Generic Table Structure), src/routes/api.chat.ts:91-118, src/routes/api.chat.ts:1003-1011
Conclusion
Multi-modal inputs and outputs in OS Chat are handled through a robust pipeline that bridges the frontend UI, server-side storage, and various AI providers. By leveraging a part-based message structure, the system ensures that complex interactions involving files, reasoning, and image generation are stored consistently and rendered effectively for the user.