Data Import & Export
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The Data Import & Export system in OS Chat provides users with full control over their conversation history and personal data. It enables users to migrate their chat sessions between different environments, create offline backups, and manage their data footprint within the application. This feature is a core component of the project's "Privacy-First" philosophy, ensuring data portability and user autonomy.
The system supports exporting selected chats as JSON files and importing backups to restore or merge conversations. It handles complex data structures including message roles, multi-modal parts, and AI model metadata, ensuring that the context and fidelity of conversations are preserved during the transfer process.
Sources: README.md:16-24, src/routes/settings/history.tsx:392-395
Architecture and Data Flow
The architecture follows a client-server pattern using a React-based frontend and a Convex serverless backend. The frontend handles file orchestration and data transformation, while the backend provides atomic mutations for bulk data insertion.
Export Workflow
The export process is initiated by the user selecting specific chats from the history interface. The application fetches the full message history for each selected chat and bundles them into a single JSON file.
The system uses superjson for serialization to maintain Type-safety and handle complex JavaScript objects like Dates or BigInts during the transition to a JSON string.
Sources: src/routes/settings/history.tsx:181-224, CLAUDE.md:126-130
Import Workflow
The import system utilizes a defined schema to validate incoming files, ensuring compatibility with both current and legacy data formats.
Data Structures and Schema
Import Validation Schema
The system uses Zod to enforce a strict schema for imported data. This prevents malformed data from entering the database and provides backward compatibility for legacy fields like model and reasoningText.
| Field | Type | Description |
|---|---|---|
exportedAt | number (optional) | Timestamp of when the export was created. |
data | array | List of objects containing chat and message data. |
chat.title | string (max 100) | The title of the conversation. |
messages.role | enum | user, assistant, or system. |
messages.content | string | The text content of the message. |
messages.metadata | object | Includes modelName, tokens, and duration. |
Sources: src/routes/settings/history.tsx:25-56
Storage and Security
During export, the system bundles chat metadata (title, model, personaId) with a full list of messages. For security, while the exported JSON is clear-text for portability, the internal application storage encrypts sensitive information like API keys using AES-GCM 256-bit encryption.
Sources: src/routes/security.tsx:24-27, src/routes/settings/history.tsx:187-203
Key Components and Functions
Frontend History Page
Located at src/routes/settings/history.tsx, this component manages the UI for selecting, exporting, and importing chats.
handleExport: Orchestrates the collection of chat and message data via Convex queries and triggers the file download.processImportFile: Reads the uploaded file, parses it withsuperjson, and validates it against theImportSchema.confirmImport: Iterates through the validated data and calls the backend mutation for each chat.
Sources: src/routes/settings/history.tsx:181-330
Backend Mutations
The backend provides internal and public mutations to handle the actual database writes during import.
bulkImportChat: (Found inconvex/import_export.ts) An atomic mutation that inserts a new chat record and its associated messages. It processes message parts and metadata to ensure they match the application's current data model.
Sources: convex/import_export.ts, src/routes/settings/history.tsx:288-305
Data Management & Retention
Users have granular control over their data lifecycle. In addition to import and export, the history settings provide "Danger Zone" actions.
| Feature | Description | File Path |
|---|---|---|
| Bulk Delete | Deletes selected chat IDs from the database. | src/routes/settings/history.tsx:162-179 |
| Delete All | Permanently wipes all chat history for the authenticated user. | src/routes/settings/history.tsx:332-350 |
| Unshare | Revokes public access to a previously shared conversation link. | src/routes/settings/history.tsx:352-385 |
Sources: src/routes/settings/history.tsx:430-550, src/routes/privacy.tsx:105-115
Conclusion
The Data Import & Export module is essential for OS Chat's commitment to user data ownership. By utilizing robust serialization with superjson, strict validation with Zod, and atomic backend operations via Convex, the system provides a reliable and secure way for users to manage their digital conversations.