""" Interface to Management database and AI Connectors. Uses the JSON connector for data access with added language support. """ import os import logging import uuid from datetime import datetime from typing import Dict, Any, List, Optional, Union import hashlib from modules.shared.mimeUtils import isTextMimeType from modules.interfaces.serviceManagementAccess import ManagementAccess from modules.interfaces.serviceManagementModel import ( Prompt, FileItem, FileData ) from modules.interfaces.serviceAppModel import User, Mandate, UserPrivilege # DYNAMIC PART: Connectors to the Interface from modules.connectors.connectorDbJson import DatabaseConnector from modules.connectors.connectorAiOpenai import ChatService # Basic Configurations from modules.shared.configuration import APP_CONFIG logger = logging.getLogger(__name__) # Singleton factory for Management instances with AI service per context _instancesManagement = {} # Custom exceptions for file handling class FileError(Exception): """Base class for file handling exceptions.""" pass class FileNotFoundError(FileError): """Exception raised when a file is not found.""" pass class FileStorageError(FileError): """Exception raised when there's an error storing a file.""" pass class FilePermissionError(FileError): """Exception raised when there's a permission issue with a file.""" pass class FileDeletionError(FileError): """Exception raised when there's an error deleting a file.""" pass class ServiceManagement: """ Interface to Management database and AI Connectors. Uses the JSON connector for data access with added language support. """ def __init__(self): """Initializes the Management Interface.""" # Initialize database self._initializeDatabase() # Initialize standard records if needed self._initRecords() # Initialize variables self.currentUser: Optional[User] = None self.userId: Optional[str] = None self.access: Optional[ManagementAccess] = None # Will be set when user context is provided self.aiService: Optional[ChatService] = None # Will be set when user context is provided def setUserContext(self, currentUser: User): """Sets the user context for the interface.""" if not currentUser: logger.info("Initializing interface without user context") return self.currentUser = currentUser self.userId = currentUser.id if not self.userId: raise ValueError("Invalid user context: id is required") # Add language settings self.userLanguage = currentUser.language # Default user language # Initialize access control with user context self.access = ManagementAccess(self.currentUser, self.db) # Initialize AI service self.aiService = ChatService() logger.debug(f"User context set: userId={self.userId}") def _initializeDatabase(self): """Initializes the database connection.""" try: # Get configuration values with defaults dbHost = APP_CONFIG.get("DB_MANAGEMENT_HOST", "_no_config_default_data") dbDatabase = APP_CONFIG.get("DB_MANAGEMENT_DATABASE", "management") dbUser = APP_CONFIG.get("DB_MANAGEMENT_USER") dbPassword = APP_CONFIG.get("DB_MANAGEMENT_PASSWORD_SECRET") # Ensure the database directory exists os.makedirs(dbHost, exist_ok=True) self.db = DatabaseConnector( dbHost=dbHost, dbDatabase=dbDatabase, dbUser=dbUser, dbPassword=dbPassword ) logger.info("Database initialized successfully") except Exception as e: logger.error(f"Failed to initialize database: {str(e)}") raise def _initRecords(self): """Initializes standard records in the database if they don't exist.""" try: # Initialize standard prompts self._initializeStandardPrompts() # Add other record initializations here logger.info("Standard records initialized successfully") except Exception as e: logger.error(f"Failed to initialize standard records: {str(e)}") raise def _initializeStandardPrompts(self): """Creates standard prompts if they don't exist.""" prompts = self.db.getRecordset("prompts") logger.debug(f"Found {len(prompts)} existing prompts") if not prompts: logger.debug("Creating standard prompts") # Define standard prompts standardPrompts = [ { "content": "Research the current market trends and developments in [TOPIC]. Collect information about leading companies, innovative products or services, and current challenges. Present the results in a structured overview with relevant data and sources.", "name": "Web Research: Market Research" }, { "content": "Analyze the attached dataset on [TOPIC] and identify the most important trends, patterns, and anomalies. Perform statistical calculations to support your findings. Present the results in a clearly structured analysis and draw relevant conclusions.", "name": "Analysis: Data Analysis" }, { "content": "Create a detailed protocol of our meeting on [TOPIC]. Capture all discussed points, decisions made, and agreed measures. Structure the protocol clearly with agenda items, participant list, and clear responsibilities for follow-up actions.", "name": "Protocol: Meeting Minutes" }, { "content": "Develop a UI/UX design concept for [APPLICATION/WEBSITE]. Consider the target audience, main functions, and brand identity. Describe the visual design, navigation, interaction patterns, and information architecture. Explain how the design optimizes user-friendliness and user experience.", "name": "Design: UI/UX Design" }, { "content": "Gib mir die ersten 1000 Primzahlen", "name": "Code: Primzahlen" }, { "content": "Bereite mir eine formelle E-Mail an peter.muster@domain.com vor, um meinen Termin von 10 Uhr auf Freitag zu scheiben.", "name": "Mail: Vorbereitung" }, ] # Create prompts for promptData in standardPrompts: createdPrompt = self.db.recordCreate("prompts", promptData) logger.debug(f"Prompt '{promptData.get('name', 'Standard')}' was created with ID {createdPrompt['id']} and context mandate={createdPrompt.get('mandateId')}, user={createdPrompt.get('_createdBy')}") else: logger.debug("Prompts already exist, skipping creation") def _uam(self, table: str, recordset: List[Dict[str, Any]]) -> List[Dict[str, Any]]: """Delegate to access control module.""" return self.access.uam(table, recordset) def _canModify(self, table: str, recordId: Optional[str] = None) -> bool: """Delegate to access control module.""" return self.access.canModify(table, recordId) # Language support method def setUserLanguage(self, languageCode: str): """Set the user's preferred language""" self.userLanguage = languageCode logger.debug(f"User language set to: {languageCode}") # AI Call Root Function async def callAi(self, messages: List[Dict[str, str]], produceUserAnswer: bool = False, temperature: float = None) -> str: """Enhanced AI service call with language support.""" if not self.aiService: logger.error("AI service not set in ServiceManagement") return "Error: AI service not available" # Add language instruction for user-facing responses if produceUserAnswer and self.userLanguage: ltext= f"Please respond in '{self.userLanguage}' language." if messages and messages[0]["role"] == "system": if "language" not in messages[0]["content"].lower(): messages[0]["content"] = f"{ltext} {messages[0]['content']}" else: # Insert a system message with language instruction messages.insert(0, { "role": "system", "content": ltext }) # Call the AI service if temperature is not None: return await self.aiService.callApi(messages, temperature=temperature) else: return await self.aiService.callApi(messages) async def callAi4Image(self, imageData: Union[str, bytes], mimeType: str = None, prompt: str = "Describe this image") -> str: """Enhanced AI service call with language support.""" if not self.aiService: logger.error("AI service not set in ServiceManagement") return "Error: AI service not available" return await self.aiService.analyzeImage(imageData, mimeType, prompt) # Utilities def getInitialId(self, table: str) -> Optional[str]: """Returns the initial ID for a table.""" return self.db.getInitialId(table) def _getCurrentTimestamp(self) -> str: """Returns the current timestamp in ISO format""" return datetime.now().isoformat() # Prompt methods def getAllPrompts(self) -> List[Prompt]: """Returns prompts based on user access level.""" allPrompts = self.db.getRecordset("prompts") filteredPrompts = self._uam("prompts", allPrompts) return [Prompt.from_dict(prompt) for prompt in filteredPrompts] def getPrompt(self, promptId: str) -> Optional[Prompt]: """Returns a prompt by ID if user has access.""" prompts = self.db.getRecordset("prompts", recordFilter={"id": promptId}) if not prompts: return None filteredPrompts = self._uam("prompts", prompts) return Prompt.from_dict(filteredPrompts[0]) if filteredPrompts else None def createPrompt(self, content: str, name: str) -> Prompt: """Creates a new prompt if user has permission.""" if not self._canModify("prompts"): raise PermissionError("No permission to create prompts") promptData = Prompt( content=content, name=name, createdAt=self._getCurrentTimestamp() ) createdRecord = self.db.recordCreate("prompts", promptData.to_dict()) return Prompt.from_dict(createdRecord) def updatePrompt(self, promptId: str, content: str = None, name: str = None) -> Prompt: """Updates a prompt if user has access.""" # Check if the prompt exists and user has access prompt = self.getPrompt(promptId) if not prompt: raise ValueError(f"Prompt {promptId} not found") if not self._canModify("prompts", promptId): raise PermissionError(f"No permission to update prompt {promptId}") # Update prompt data using model updatedData = prompt.model_dump() if content is not None: updatedData["content"] = content if name is not None: updatedData["name"] = name updatedPrompt = Prompt.from_dict(updatedData) # Update prompt self.db.recordModify("prompts", promptId, updatedPrompt.to_dict()) # Get updated prompt updatedPrompt = self.getPrompt(promptId) if not updatedPrompt: raise ValueError("Failed to retrieve updated prompt") return updatedPrompt def deletePrompt(self, promptId: str) -> bool: """Deletes a prompt if user has access.""" # Check if the prompt exists and user has access prompt = self.getPrompt(promptId) if not prompt: return False if not self._canModify("prompts", promptId): raise PermissionError(f"No permission to delete prompt {promptId}") return self.db.recordDelete("prompts", promptId) # File Utilities def calculateFileHash(self, fileContent: bytes) -> str: """Calculates a SHA-256 hash for the file content""" return hashlib.sha256(fileContent).hexdigest() def checkForDuplicateFile(self, fileHash: str) -> Optional[Dict[str, Any]]: """Checks if a file with the same hash already exists for the current user and mandate.""" files = self.db.getRecordset("files", recordFilter={ "fileHash": fileHash, "mandateId": self.currentUser.get("mandateId"), "_createdBy": self.currentUser.get("id") }) if files: return files[0] return None def getMimeType(self, filename: str) -> str: """Determines the MIME type based on the file extension.""" import os ext = os.path.splitext(filename)[1].lower()[1:] extensionToMime = { "pdf": "application/pdf", "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "doc": "application/msword", "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xls": "application/vnd.ms-excel", "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", "ppt": "application/vnd.ms-powerpoint", "csv": "text/csv", "txt": "text/plain", "json": "application/json", "xml": "application/xml", "html": "text/html", "htm": "text/html", "jpg": "image/jpeg", "jpeg": "image/jpeg", "png": "image/png", "gif": "image/gif", "webp": "image/webp", "svg": "image/svg+xml", "py": "text/x-python", "js": "application/javascript", "css": "text/css" } return extensionToMime.get(ext.lower(), "application/octet-stream") # File methods - metadata-based operations def getAllFiles(self) -> List[Dict[str, Any]]: """Returns files based on user access level.""" allFiles = self.db.getRecordset("files") return self._uam("files", allFiles) def getFile(self, fileId: str) -> Optional[Dict[str, Any]]: """Returns a file by ID if user has access.""" files = self.db.getRecordset("files", recordFilter={"id": fileId}) if not files: return None filteredFiles = self._uam("files", files) return filteredFiles[0] if filteredFiles else None def createFile(self, name: str, mimeType: str, size: int = None, fileHash: str = None) -> Dict[str, Any]: """Creates a new file entry if user has permission.""" if not self._canModify("files"): raise PermissionError("No permission to create files") fileData = { "mandateId": self.currentUser.get("mandateId"), "name": name, "mimeType": mimeType, "size": size, "fileHash": fileHash, "creationDate": self._getCurrentTimestamp() } return self.db.recordCreate("files", fileData) def updateFile(self, fileId: str, updateData: Dict[str, Any]) -> Dict[str, Any]: """Updates file metadata if user has access.""" # Check if the file exists and user has access file = self.getFile(fileId) if not file: raise FileNotFoundError(f"File with ID {fileId} not found") if not self._canModify("files", fileId): raise PermissionError(f"No permission to update file {fileId}") # Update file return self.db.recordModify("files", fileId, updateData) def deleteFile(self, fileId: str) -> bool: """Deletes a file if user has access.""" try: # Check if the file exists and user has access file = self.getFile(fileId) if not file: raise FileNotFoundError(f"File with ID {fileId} not found") if not self._canModify("files", fileId): raise PermissionError(f"No permission to delete file {fileId}") # Check for other references to this file (by hash) fileHash = file.get("fileHash") if fileHash: otherReferences = [f for f in self.db.getRecordset("files", recordFilter={"fileHash": fileHash}) if f.get("id") != fileId] # Only delete associated fileData if no other references exist if not otherReferences: try: fileDataEntries = self.db.getRecordset("fileData", recordFilter={"id": fileId}) if fileDataEntries: self.db.recordDelete("fileData", fileId) logger.debug(f"FileData for file {fileId} deleted") except Exception as e: logger.warning(f"Error deleting FileData for file {fileId}: {str(e)}") # Delete the FileItem entry return self.db.recordDelete("files", fileId) except FileNotFoundError as e: raise except FilePermissionError as e: raise except Exception as e: logger.error(f"Error deleting file {fileId}: {str(e)}") raise FileDeletionError(f"Error deleting file: {str(e)}") # FileData methods - data operations def createFileData(self, fileId: str, data: bytes) -> bool: """Stores the binary data of a file in the database.""" try: import base64 # Check file access file = self.getFile(fileId) if not file: logger.error(f"File with ID {fileId} not found when storing data") return False # Determine if this is a text-based format mimeType = file.get("mimeType", "application/octet-stream") isTextFormat = isTextMimeType(mimeType) base64Encoded = False fileData = None if isTextFormat: # Try to decode as text try: textContent = data.decode('utf-8') fileData = textContent base64Encoded = False logger.debug(f"Stored file {fileId} as text") except UnicodeDecodeError: # Fallback to base64 if text decoding fails encodedData = base64.b64encode(data).decode('utf-8') fileData = encodedData base64Encoded = True logger.warning(f"Failed to decode text file {fileId}, falling back to base64") else: # Binary format - always use base64 encodedData = base64.b64encode(data).decode('utf-8') fileData = encodedData base64Encoded = True logger.debug(f"Stored file {fileId} as base64") # Create the fileData record with data and encoding flag fileDataObj = { "id": fileId, "data": fileData, "base64Encoded": base64Encoded } self.db.recordCreate("fileData", fileDataObj) logger.debug(f"Successfully stored data for file {fileId} (base64Encoded: {base64Encoded})") return True except Exception as e: logger.error(f"Error storing data for file {fileId}: {str(e)}") return False def getFileData(self, fileId: str) -> Optional[bytes]: """Returns the binary data of a file if user has access.""" # Check file access file = self.getFile(fileId) if not file: logger.warning(f"No access to file ID {fileId}") return None import base64 fileDataEntries = self.db.getRecordset("fileData", recordFilter={"id": fileId}) if not fileDataEntries: logger.warning(f"No data found for file ID {fileId}") return None fileDataEntry = fileDataEntries[0] if "data" not in fileDataEntry: logger.warning(f"No data field in file data for ID {fileId}") return None data = fileDataEntry["data"] base64Encoded = fileDataEntry.get("base64Encoded", False) try: if base64Encoded: # Decode base64 to bytes return base64.b64decode(data) else: # Convert text to bytes return data.encode('utf-8') except Exception as e: logger.error(f"Error processing file data for {fileId}: {str(e)}") return None def updateFileData(self, fileId: str, data: Union[bytes, str]) -> bool: """Updates file data if user has access.""" # Check file access file = self.getFile(fileId) if not file: logger.error(f"File with ID {fileId} not found when updating data") return False if not self._canModify("files", fileId): logger.error(f"No permission to update file data for {fileId}") return False try: import base64 # Determine if this is a text-based format mimeType = file.get("mimeType", "application/octet-stream") isTextFormat = isTextMimeType(mimeType) base64Encoded = False fileData = None # Convert input data to the right format if isinstance(data, bytes): if isTextFormat: try: # Try to convert bytes to text fileData = data.decode('utf-8') base64Encoded = False except UnicodeDecodeError: # Fallback to base64 if text decoding fails fileData = base64.b64encode(data).decode('utf-8') base64Encoded = True else: # Binary format - use base64 fileData = base64.b64encode(data).decode('utf-8') base64Encoded = True elif isinstance(data, str): if isTextFormat: # Text format - store as text fileData = data base64Encoded = False else: # Check if it's already base64 encoded try: # Try to decode as base64 to validate base64.b64decode(data) fileData = data base64Encoded = True except: # Not valid base64, encode the string fileData = base64.b64encode(data.encode('utf-8')).decode('utf-8') base64Encoded = True else: # Convert to string first stringData = str(data) if isTextFormat: fileData = stringData base64Encoded = False else: fileData = base64.b64encode(stringData.encode('utf-8')).decode('utf-8') base64Encoded = True # Check if a record already exists fileDataEntries = self.db.getRecordset("fileData", recordFilter={"id": fileId}) dataUpdate = { "data": fileData, "base64Encoded": base64Encoded } if fileDataEntries: # Update the existing record self.db.recordModify("fileData", fileId, dataUpdate) logger.debug(f"Updated file data for file ID {fileId} (base64Encoded: {base64Encoded})") else: # Create a new record dataUpdate["id"] = fileId self.db.recordCreate("fileData", dataUpdate) logger.debug(f"Created new file data for file ID {fileId} (base64Encoded: {base64Encoded})") return True except Exception as e: logger.error(f"Error updating data for file {fileId}: {str(e)}") return False def saveUploadedFile(self, fileContent: bytes, fileName: str) -> Dict[str, Any]: """Saves an uploaded file if user has permission.""" try: # Check file creation permission if not self._canModify("files"): raise PermissionError("No permission to upload files") logger.debug(f"Starting upload process for file: {fileName}") if not isinstance(fileContent, bytes): logger.error(f"Invalid fileContent type: {type(fileContent)}") raise ValueError(f"fileContent must be bytes, got {type(fileContent)}") # Calculate file hash for deduplication fileHash = self.calculateFileHash(fileContent) logger.debug(f"Calculated file hash: {fileHash}") # Check for duplicate within same user/mandate existingFile = self.checkForDuplicateFile(fileHash) if existingFile: logger.debug(f"Duplicate found for {fileName}: {existingFile['id']}") return existingFile # Determine MIME type and size mimeType = self.getMimeType(fileName) fileSize = len(fileContent) # Save metadata logger.debug(f"Saving file metadata to database for file: {fileName}") dbFile = self.createFile( name=fileName, mimeType=mimeType, size=fileSize, fileHash=fileHash ) # Save binary data logger.debug(f"Saving file content to database for file: {fileName}") self.createFileData(dbFile["id"], fileContent) logger.debug(f"File upload process completed for: {fileName}") return dbFile except Exception as e: logger.error(f"Error in saveUploadedFile for {fileName}: {str(e)}", exc_info=True) raise FileStorageError(f"Error saving file: {str(e)}") def downloadFile(self, fileId: str) -> Optional[Dict[str, Any]]: """Returns a file for download if user has access.""" try: # Check file access file = self.getFile(fileId) if not file: raise FileNotFoundError(f"File with ID {fileId} not found") # Get binary data fileContent = self.getFileData(fileId) if fileContent is None: raise FileNotFoundError(f"Binary data for file with ID {fileId} not found") return { "id": fileId, "name": file.get("name", f"file_{fileId}"), "contentType": file.get("mimeType", "application/octet-stream"), "size": file.get("size", len(fileContent)), "content": fileContent } except FileNotFoundError as e: raise except Exception as e: logger.error(f"Error downloading file {fileId}: {str(e)}") raise FileError(f"Error downloading file: {str(e)}") def getInterface(currentUser: Optional[User] = None) -> 'ServiceManagement': """ Returns a ServiceManagement instance. If currentUser is provided, initializes with user context. Otherwise, returns an instance with only database access. """ # Create new instance if not exists if "default" not in _instancesManagement: _instancesManagement["default"] = ServiceManagement() interface = _instancesManagement["default"] if currentUser: interface.setUserContext(currentUser) else: logger.info("Returning interface without user context") return interface