function store_uploaded_document_v1
Stores an uploaded document in a thread-safe global application state dictionary, organizing documents by user email and document ID with metadata including name, content, file type, and upload timestamp.
/tf/active/vicechatdev/vice_ai/complex_app.py
530 - 541
simple
Purpose
This function manages the storage of user-uploaded documents in memory for use as chat context. It maintains a nested dictionary structure where documents are organized by user email, then by document ID. The function uses thread locking to ensure safe concurrent access to the shared application state. Documents are stored with their content and metadata for later retrieval during chat interactions.
Source Code
def store_uploaded_document(user_email, document_id, name, content, file_type):
"""Store an uploaded document for chat context"""
with app_state['locks']['uploaded_documents']:
if user_email not in app_state['uploaded_documents']:
app_state['uploaded_documents'][user_email] = {}
app_state['uploaded_documents'][user_email][document_id] = {
'name': name,
'content': content,
'file_type': file_type,
'upload_time': datetime.now().isoformat()
}
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
user_email |
- | - | positional_or_keyword |
document_id |
- | - | positional_or_keyword |
name |
- | - | positional_or_keyword |
content |
- | - | positional_or_keyword |
file_type |
- | - | positional_or_keyword |
Parameter Details
user_email: String identifier for the user uploading the document. Used as the primary key to organize documents by user. Expected to be a valid email address string.
document_id: Unique identifier for the document. Used as the secondary key within a user's document collection. Typically a UUID or other unique string to prevent collisions.
name: Human-readable name of the document. Usually the original filename. String value that helps users identify the document.
content: The actual content of the document. Can be text content (string) or binary data depending on file_type. This is the main payload that will be used for chat context.
file_type: String indicating the type/format of the document (e.g., 'pdf', 'txt', 'docx', 'md'). Used to determine how to process or display the document content.
Return Value
This function does not return any value (implicitly returns None). It performs an in-place modification of the global app_state dictionary.
Dependencies
datetimethreading
Required Imports
from datetime import datetime
from threading import Lock
Usage Example
import threading
from datetime import datetime
# Initialize required global state
app_state = {
'locks': {
'uploaded_documents': threading.Lock()
},
'uploaded_documents': {}
}
# Store a document
store_uploaded_document(
user_email='user@example.com',
document_id='doc-12345',
name='project_report.pdf',
content='This is the document content...',
file_type='pdf'
)
# Access the stored document
user_docs = app_state['uploaded_documents']['user@example.com']
doc = user_docs['doc-12345']
print(f"Document: {doc['name']}, uploaded at {doc['upload_time']}")
Best Practices
- Ensure app_state global dictionary is properly initialized before calling this function
- The function assumes app_state['locks']['uploaded_documents'] is a valid Lock object - verify this exists
- Consider implementing document size limits to prevent memory exhaustion
- This stores documents in memory - data will be lost on application restart unless persisted elsewhere
- For production use, consider adding validation for user_email format and document_id uniqueness
- The content parameter can be large - monitor memory usage when storing many or large documents
- Consider implementing a cleanup mechanism to remove old documents based on upload_time
- Thread-safe for concurrent access, but the entire uploaded_documents dictionary is locked during write operations
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function store_uploaded_document 90.2% similar
-
function store_document 83.2% similar
-
function get_user_uploaded_documents 79.0% similar
-
function get_uploaded_document_v1 78.5% similar
-
function get_uploaded_document 76.2% similar