File Handling System
Last Updated: 2025-01-16
Primary Source: docs/project_status.md, docs/PROVIDER_AGNOSTIC_FILE_REFERENCE_IMPLEMENTATION.md
Service File: backend/src/services/fileProcessing.service.ts, backend/src/services/fileReference.service.ts
Overview
The File Handling System implements a provider-agnostic file reference system that abstracts provider-specific differences, enabling clean, maintainable code while supporting multiple LLM providers and storage backends.
Architecture
File Processing Pipeline
Provider-Agnostic Design
Canonical File References
Concept: All files are stored with canonical internalFileId that maps to provider-specific IDs.
Model: backend/src/models/fileReference.model.ts
Table: file_references
Fields:
id- Primary key (UUID)internal_file_id- Canonical ID (unique)firebase_url- Firebase storage URLmime_type- File MIME typefile_size- File size in byteschecksum- File checksumopenai_file_id- OpenAI file ID (if uploaded)gemini_file_uri- Gemini file URI (if uploaded)claude_file_id- Claude file ID (if uploaded)provider_used- Provider that was usedupload_method- 'one-api' or 'direct'
Normalization Layer
Service: backend/src/services/fileNormalization.service.ts
Function: normalizeToProviderFormat(contentItems, provider)
Process:
- Receives canonical format (
file_reference,image_reference,image) - Detects provider from model
- Converts to provider-specific format
- Returns normalized payload
Supported Providers:
- OpenAI (GPT-4 Vision, etc.)
- Gemini (Gemini Pro Vision)
- DeepSeek
- Grok
- Fallback for unknown providers
File Upload Flow
1. File Upload
Endpoint: POST /api/files/upload
Process:
- Client uploads file (multipart form)
- Backend stores file in Firebase/S3
- Creates canonical file reference
- Returns
uploadContextIdand URLs
2. File Reference Creation
Service: backend/src/services/fileReference.service.ts
Function: createFileReference(fileData)
Process:
- Generate
internalFileId(UUID) - Store file metadata
- Map to provider IDs (if uploaded to provider)
- Store in
file_referencestable
3. Provider Upload
Service: backend/src/services/llmFileUpload.service.ts
Hybrid Approach:
- Try One-API first (
/filesendpoint) - Fallback to direct provider if One-API unavailable
- Store both internal ID and provider ID
- Track upload method
Multimodal Payload Building
Service
File: backend/src/services/multimodalPayload.service.ts
Functions:
buildMultimodalPayload()- Build payload with canonical formatsbuildMultimodalPayloadNormalized()- Build and normalize to providerbuildStructuredMultimodalPayloadNormalized()- Structured format
Process
Gather Content Items:
- Text messages
- File references (
internalFileId) - Image references
- Public image URLs
Normalize to Provider:
- Convert canonical formats to provider format
- Map
internalFileIdto provider file ID - Format images for provider (base64, URL, etc.)
Build Payload:
- Combine text and media
- Format for provider API
- Return ready-to-send payload
File Types and Processing
Images
Processing:
- Instant Vision: Sent as Base64 for immediate AI vision
- Long-term Storage: Uploaded to Firebase for persistence
- Vision Analysis:
analyzeImageContent()extracts context - Memory Storage: Vision context saved with memory
Normalization:
- OpenAI:
image_urlwith base64 or URL - Gemini:
inline_datawith base64 - DeepSeek: Base64 in content
Documents
Supported Formats:
- DOCX
- TXT
- RTF
Processing:
- Document Ingestion: Processed via RAG pipeline
- Text Extraction: Extracted text stored in memory
- Provider Upload: Large documents uploaded to provider (if supported)
- Memory Storage: Document content ingested into ASO memory
Screenshots
Tool: screenshot_ocr
Process:
- User uploads screenshot
- OCR extracts text
- Text stored as user-scoped memory
- OCR text appended to chat
Canonical Content Types
1. file_reference
Format:
{
type: 'file_reference',
internalFileId: string,
mimeType?: string
}Usage: Uploaded files with canonical ID
2. image_reference
Format:
{
type: 'image_reference',
internalFileId: string,
mimeType?: string
}Usage: Uploaded images with canonical ID
3. image
Format:
{
type: 'image',
url: string,
mimeType?: string
}Usage: Public image URLs
Data Flow
API Endpoints
File Management
POST /api/files/upload- Upload fileGET /api/files/:id- Get file referenceDELETE /api/files/:id- Delete file
Source Files
Primary Sources:
backend/src/services/fileProcessing.service.ts- File processingbackend/src/services/fileReference.service.ts- File reference managementbackend/src/services/fileNormalization.service.ts- Provider normalizationbackend/src/services/multimodalPayload.service.ts- Payload buildingdocs/PROVIDER_AGNOSTIC_FILE_REFERENCE_IMPLEMENTATION.md- Complete implementation guide
Related Files:
backend/src/services/llmFileUpload.service.ts- Provider uploadbackend/src/services/vision.service.ts- Vision analysisbackend/src/services/imageOcr.service.ts- OCR processing
Related Documentation
- Memory System - Memory storage and document ingestion
- 03-PLATFORMS/Web-Portal.md - File upload in web portal
- 03-PLATFORMS/Discord-Bot.md - File handling in Discord
- 06-ARCHITECTURE/Data-Flow.md - File processing flow
- 00-OVERVIEW.md - Complete system overview