rag_saas_system / app /models /source_registry.py
GodRad's picture
Upload 51 files
0c8aadf verified
Raw History Blame Contribute Delete
3.9 kB
"""
Source Registry Models and Service
Tracks active sources per tenant and prevents source suppression
"""
from pydantic import BaseModel, Field
from typing import List, Dict, Optional, Set
from datetime import datetime
from enum import Enum
from app.models.domain import SourceType
class SourceState(BaseModel):
"""State of a single source for a tenant"""
source_id: str
tenant_id: str
source_type: SourceType
enabled: bool = True
name: str
uri: str
metadata: Dict = Field(default_factory=dict)
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: datetime = Field(default_factory=datetime.utcnow)
class SourceRegistry:
"""
Registry tracking all sources per tenant
Ensures multi-source retrieval without suppression
"""
def __init__(self):
# In-memory registry: tenant_id -> {source_id -> SourceState}
self._registry: Dict[str, Dict[str, SourceState]] = {}
def register_source(self, source_state: SourceState) -> None:
"""Register or update a source"""
if source_state.tenant_id not in self._registry:
self._registry[source_state.tenant_id] = {}
source_state.updated_at = datetime.utcnow()
self._registry[source_state.tenant_id][source_state.source_id] = source_state
def get_active_sources(self, tenant_id: str, source_types: Optional[List[SourceType]] = None) -> List[SourceState]:
"""Get all active (enabled) sources for a tenant, optionally filtered by type"""
if tenant_id not in self._registry:
return []
sources = [
source for source in self._registry[tenant_id].values()
if source.enabled
]
if source_types:
sources = [s for s in sources if s.source_type in source_types]
return sources
def get_all_sources(self, tenant_id: str) -> List[SourceState]:
"""Get all sources (enabled and disabled) for a tenant"""
if tenant_id not in self._registry:
return []
return list(self._registry[tenant_id].values())
def is_source_enabled(self, tenant_id: str, source_id: str) -> bool:
"""Check if a specific source is enabled"""
if tenant_id not in self._registry:
return False
if source_id not in self._registry[tenant_id]:
return False
return self._registry[tenant_id][source_id].enabled
def toggle_source(self, tenant_id: str, source_id: str, enabled: bool) -> bool:
"""Enable or disable a source. Returns True if successful."""
if tenant_id not in self._registry:
return False
if source_id not in self._registry[tenant_id]:
return False
self._registry[tenant_id][source_id].enabled = enabled
self._registry[tenant_id][source_id].updated_at = datetime.utcnow()
return True
def get_source_types_present(self, tenant_id: str) -> Set[SourceType]:
"""Get set of all source types that have at least one enabled source"""
active_sources = self.get_active_sources(tenant_id)
return {source.source_type for source in active_sources}
def remove_source(self, tenant_id: str, source_id: str) -> bool:
"""Remove a source from the registry"""
if tenant_id not in self._registry:
return False
if source_id not in self._registry[tenant_id]:
return False
del self._registry[tenant_id][source_id]
return True
# Global singleton instance
_source_registry = SourceRegistry()
def get_source_registry() -> SourceRegistry:
"""Get the global source registry instance"""
return _source_registry