refactor(memory): simplify config retrieval and remove redundant functions
- Remove get_memory_config_id function from end_user_repository.py as it's no longer needed - Remove get_end_user_memory_config_id function from memory_agent_service.py to reduce duplication - Simplify get_end_user_connected_config to use MemoryConfigService.get_config_with_fallback - Update get_config_with_fallback signature to accept memory_config_id directly instead of end_user_id - Remove unnecessary AppRelease query and config parsing logic from get_end_user_connected_config - Streamline memory config retrieval flow to use service layer abstraction - Improves code maintainability by centralizing config fallback logic in MemoryConfigService
This commit is contained in:
@@ -503,17 +503,3 @@ def update_memory_config_id(db: Session, end_user_id: uuid.UUID, memory_config_i
|
||||
"""
|
||||
repo = EndUserRepository(db)
|
||||
return repo.update_memory_config_id(end_user_id, memory_config_id)
|
||||
|
||||
|
||||
def get_memory_config_id(db: Session, end_user_id: uuid.UUID) -> Optional[uuid.UUID]:
|
||||
"""获取终端用户的 memory_config_id。
|
||||
|
||||
Args:
|
||||
db: 数据库会话
|
||||
end_user_id: 终端用户ID
|
||||
|
||||
Returns:
|
||||
Optional[uuid.UUID]: memory_config_id 或 None
|
||||
"""
|
||||
repo = EndUserRepository(db)
|
||||
return repo.get_memory_config_id(end_user_id)
|
||||
|
||||
@@ -1148,37 +1148,13 @@ class MemoryAgentService:
|
||||
# LogStreamer uses context manager for file handling, so cleanup is automatic
|
||||
|
||||
|
||||
def get_end_user_memory_config_id(end_user_id: str, db: Session) -> Optional[uuid.UUID]:
|
||||
"""快速获取终端用户的 memory_config_id(直接从 end_user 表读取)。
|
||||
|
||||
如果 end_user 已有缓存的 memory_config_id,直接返回;
|
||||
否则返回 None,调用方应使用 get_end_user_connected_config 获取完整配置。
|
||||
|
||||
Args:
|
||||
end_user_id: 终端用户ID
|
||||
db: 数据库会话
|
||||
|
||||
Returns:
|
||||
Optional[uuid.UUID]: memory_config_id 或 None
|
||||
"""
|
||||
from app.repositories.end_user_repository import get_memory_config_id
|
||||
|
||||
try:
|
||||
end_user_uuid = uuid.UUID(end_user_id) if isinstance(end_user_id, str) else end_user_id
|
||||
return get_memory_config_id(db, end_user_uuid)
|
||||
except (ValueError, TypeError) as e:
|
||||
logger.warning(f"Invalid end_user_id format: {end_user_id}, error: {e}")
|
||||
return None
|
||||
|
||||
|
||||
# TODO: move to memory_config_service.py
|
||||
def get_end_user_connected_config(end_user_id: str, db: Session) -> Dict[str, Any]:
|
||||
"""
|
||||
获取终端用户关联的记忆配置
|
||||
|
||||
通过以下流程获取配置:
|
||||
1. 根据 end_user_id 获取用户的 app_id
|
||||
2. 获取该应用的最新发布版本
|
||||
3. 从发布版本的 config 字段中提取 memory_config_id
|
||||
使用 MemoryConfigService.get_config_with_fallback 获取配置,
|
||||
支持终端用户已分配配置和工作空间默认配置的回退机制。
|
||||
|
||||
Args:
|
||||
end_user_id: 终端用户ID
|
||||
@@ -1191,8 +1167,8 @@ def get_end_user_connected_config(end_user_id: str, db: Session) -> Dict[str, An
|
||||
ValueError: 当终端用户不存在或应用未发布时
|
||||
"""
|
||||
from app.models.app_model import App
|
||||
from app.models.app_release_model import AppRelease
|
||||
from app.models.end_user_model import EndUser
|
||||
from app.services.memory_config_service import MemoryConfigService
|
||||
|
||||
logger.info(f"Getting connected config for end_user: {end_user_id}")
|
||||
|
||||
@@ -1203,9 +1179,8 @@ def get_end_user_connected_config(end_user_id: str, db: Session) -> Dict[str, An
|
||||
raise ValueError(f"终端用户不存在: {end_user_id}")
|
||||
|
||||
app_id = end_user.app_id
|
||||
logger.debug(f"Found end_user app_id: {app_id}")
|
||||
|
||||
# 2. 获取应用的当前发布版本(通过 apps.current_release_id)
|
||||
# 2. 获取应用以确定 workspace_id
|
||||
app = db.query(App).filter(App.id == app_id).first()
|
||||
if not app:
|
||||
logger.warning(f"App not found: {app_id}")
|
||||
@@ -1215,33 +1190,19 @@ def get_end_user_connected_config(end_user_id: str, db: Session) -> Dict[str, An
|
||||
logger.warning(f"No current release for app: {app_id}")
|
||||
raise ValueError(f"应用未发布: {app_id}")
|
||||
|
||||
current_release = db.query(AppRelease).filter(AppRelease.id == app.current_release_id).first()
|
||||
if not current_release:
|
||||
logger.warning(f"Current release not found: {app.current_release_id}")
|
||||
raise ValueError(f"应用发布版本不存在: {app.current_release_id}")
|
||||
# 3. 使用 get_config_with_fallback 获取记忆配置
|
||||
memory_config_service = MemoryConfigService(db)
|
||||
memory_config = memory_config_service.get_config_with_fallback(
|
||||
memory_config_id=end_user.memory_config_id,
|
||||
workspace_id=app.workspace_id
|
||||
)
|
||||
|
||||
logger.debug(f"Found current release: version={current_release.version}, id={current_release.id}")
|
||||
|
||||
# 3. 从 config 中提取 memory_config_id
|
||||
config = current_release.config or {}
|
||||
|
||||
# 如果 config 是字符串,解析为字典
|
||||
if isinstance(config, str):
|
||||
import json
|
||||
try:
|
||||
config = json.loads(config)
|
||||
except json.JSONDecodeError:
|
||||
logger.warning(f"Failed to parse config JSON for release {current_release.id}")
|
||||
config = {}
|
||||
|
||||
memory_obj = config.get('memory', {})
|
||||
memory_config_id = memory_obj.get('memory_content') if isinstance(memory_obj, dict) else None
|
||||
memory_config_id = str(memory_config.id) if memory_config else None
|
||||
|
||||
result = {
|
||||
"end_user_id": str(end_user_id),
|
||||
"app_id": str(app_id),
|
||||
"release_id": str(current_release.id),
|
||||
"release_version": current_release.version,
|
||||
"release_id": str(app.current_release_id),
|
||||
"memory_config_id": memory_config_id
|
||||
}
|
||||
|
||||
|
||||
@@ -503,35 +503,31 @@ class MemoryConfigService:
|
||||
|
||||
def get_config_with_fallback(
|
||||
self,
|
||||
end_user_id: UUID,
|
||||
memory_config_id: Optional[UUID],
|
||||
workspace_id: UUID
|
||||
) -> Optional["MemoryConfigModel"]:
|
||||
"""Get memory config for end user with fallback to workspace default.
|
||||
"""Get memory config with fallback to workspace default.
|
||||
|
||||
Implements graceful degradation: if the end user's assigned config
|
||||
doesn't exist, falls back to the workspace's default active config.
|
||||
Implements graceful degradation: if the provided config_id is None or
|
||||
the config doesn't exist, falls back to the workspace's default config.
|
||||
|
||||
Args:
|
||||
end_user_id: End user ID
|
||||
memory_config_id: Memory config ID (can be None)
|
||||
workspace_id: Workspace ID for fallback lookup
|
||||
|
||||
Returns:
|
||||
Optional[MemoryConfigModel]: Memory config or None if no fallback available
|
||||
"""
|
||||
from app.models.memory_config_model import MemoryConfig as MemoryConfigModel
|
||||
from app.repositories.end_user_repository import EndUserRepository
|
||||
|
||||
end_user_repo = EndUserRepository(self.db)
|
||||
end_user = end_user_repo.get_by_id(end_user_id)
|
||||
|
||||
if not end_user or not end_user.memory_config_id:
|
||||
if not memory_config_id:
|
||||
logger.debug(
|
||||
"End user has no memory config assigned",
|
||||
extra={"end_user_id": str(end_user_id)}
|
||||
"No memory config ID provided, using workspace default",
|
||||
extra={"workspace_id": str(workspace_id)}
|
||||
)
|
||||
return self.get_workspace_default_config(workspace_id)
|
||||
return self._get_workspace_default_config(workspace_id)
|
||||
|
||||
config = self.db.get(MemoryConfigModel, end_user.memory_config_id)
|
||||
config = self.db.get(MemoryConfigModel, memory_config_id)
|
||||
|
||||
if config:
|
||||
return config
|
||||
@@ -539,24 +535,12 @@ class MemoryConfigService:
|
||||
logger.warning(
|
||||
"Memory config not found, falling back to workspace default",
|
||||
extra={
|
||||
"end_user_id": str(end_user_id),
|
||||
"missing_config_id": str(end_user.memory_config_id),
|
||||
"missing_config_id": str(memory_config_id),
|
||||
"workspace_id": str(workspace_id)
|
||||
}
|
||||
)
|
||||
|
||||
fallback_config = self.get_workspace_default_config(workspace_id)
|
||||
|
||||
if fallback_config:
|
||||
logger.info(
|
||||
"Using fallback memory config",
|
||||
extra={
|
||||
"end_user_id": str(end_user_id),
|
||||
"fallback_config_id": str(fallback_config.config_id)
|
||||
}
|
||||
)
|
||||
|
||||
return fallback_config
|
||||
return self._get_workspace_default_config(workspace_id)
|
||||
|
||||
def delete_config(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user