refactor(memory): extract workspace default config logic to service
- Extract default memory config retrieval logic from AppService to MemoryConfigService - Make get_workspace_default_config method public (remove underscore prefix) - Update AppService to delegate to MemoryConfigService for cleaner separation of concerns - Add legacy int config_id handling in delete_config method with appropriate warnings - Update delete_config signature to accept UUID or int types for backward compatibility - Improve code reusability and maintainability by centralizing memory config operations
This commit is contained in:
@@ -1316,43 +1316,18 @@ class AppService:
|
|||||||
Returns:
|
Returns:
|
||||||
Optional[uuid.UUID]: 默认记忆配置ID,如果不存在则返回 None
|
Optional[uuid.UUID]: 默认记忆配置ID,如果不存在则返回 None
|
||||||
"""
|
"""
|
||||||
from app.models.memory_config_model import MemoryConfig as MemoryConfigModel
|
from app.services.memory_config_service import MemoryConfigService
|
||||||
|
|
||||||
# 查找工作空间的默认记忆配置
|
service = MemoryConfigService(self.db)
|
||||||
stmt = (
|
config = service.get_workspace_default_config(workspace_id)
|
||||||
select(MemoryConfigModel.id)
|
|
||||||
.where(
|
|
||||||
MemoryConfigModel.workspace_id == workspace_id,
|
|
||||||
MemoryConfigModel.is_default.is_(True),
|
|
||||||
MemoryConfigModel.state.is_(True),
|
|
||||||
)
|
|
||||||
.limit(1)
|
|
||||||
)
|
|
||||||
|
|
||||||
config_id = self.db.scalars(stmt).first()
|
if not config:
|
||||||
|
|
||||||
if config_id:
|
|
||||||
return config_id
|
|
||||||
|
|
||||||
# 回退:获取最早创建的活跃配置
|
|
||||||
stmt = (
|
|
||||||
select(MemoryConfigModel.id)
|
|
||||||
.where(
|
|
||||||
MemoryConfigModel.workspace_id == workspace_id,
|
|
||||||
MemoryConfigModel.state.is_(True),
|
|
||||||
)
|
|
||||||
.order_by(MemoryConfigModel.created_at.asc())
|
|
||||||
.limit(1)
|
|
||||||
)
|
|
||||||
|
|
||||||
config_id = self.db.scalars(stmt).first()
|
|
||||||
|
|
||||||
if not config_id:
|
|
||||||
logger.warning(
|
logger.warning(
|
||||||
f"工作空间没有可用的记忆配置: workspace_id={workspace_id}"
|
f"工作空间没有可用的记忆配置: workspace_id={workspace_id}"
|
||||||
)
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
return config_id
|
return config.id
|
||||||
|
|
||||||
def _update_endusers_memory_config(
|
def _update_endusers_memory_config(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -445,7 +445,7 @@ class MemoryConfigService:
|
|||||||
"pruning_threshold": memory_config.pruning_threshold,
|
"pruning_threshold": memory_config.pruning_threshold,
|
||||||
}
|
}
|
||||||
|
|
||||||
def _get_workspace_default_config(
|
def get_workspace_default_config(
|
||||||
self,
|
self,
|
||||||
workspace_id: UUID
|
workspace_id: UUID
|
||||||
) -> Optional["MemoryConfigModel"]:
|
) -> Optional["MemoryConfigModel"]:
|
||||||
@@ -529,7 +529,7 @@ class MemoryConfigService:
|
|||||||
"End user has no memory config assigned",
|
"End user has no memory config assigned",
|
||||||
extra={"end_user_id": str(end_user_id)}
|
extra={"end_user_id": str(end_user_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, end_user.memory_config_id)
|
||||||
|
|
||||||
@@ -545,7 +545,7 @@ class MemoryConfigService:
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
fallback_config = self._get_workspace_default_config(workspace_id)
|
fallback_config = self.get_workspace_default_config(workspace_id)
|
||||||
|
|
||||||
if fallback_config:
|
if fallback_config:
|
||||||
logger.info(
|
logger.info(
|
||||||
@@ -560,7 +560,7 @@ class MemoryConfigService:
|
|||||||
|
|
||||||
def delete_config(
|
def delete_config(
|
||||||
self,
|
self,
|
||||||
config_id: UUID,
|
config_id: UUID | int,
|
||||||
force: bool = False
|
force: bool = False
|
||||||
) -> dict:
|
) -> dict:
|
||||||
"""Delete memory config with protection against in-use configs.
|
"""Delete memory config with protection against in-use configs.
|
||||||
@@ -569,7 +569,7 @@ class MemoryConfigService:
|
|||||||
that are actively being used by end users or marked as default.
|
that are actively being used by end users or marked as default.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
config_id: Memory config ID to delete
|
config_id: Memory config ID to delete (UUID or legacy int)
|
||||||
force: If True, delete even if end users are connected
|
force: If True, delete even if end users are connected
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -581,6 +581,18 @@ class MemoryConfigService:
|
|||||||
from app.core.exceptions import ResourceNotFoundException
|
from app.core.exceptions import ResourceNotFoundException
|
||||||
from app.models.memory_config_model import MemoryConfig as MemoryConfigModel
|
from app.models.memory_config_model import MemoryConfig as MemoryConfigModel
|
||||||
|
|
||||||
|
# 处理旧格式 int 类型的 config_id
|
||||||
|
if isinstance(config_id, int):
|
||||||
|
logger.warning(
|
||||||
|
"Attempted to delete legacy int config_id",
|
||||||
|
extra={"config_id": config_id}
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
"status": "error",
|
||||||
|
"message": "旧格式配置ID不支持删除操作,请使用新版配置",
|
||||||
|
"legacy_int_id": config_id
|
||||||
|
}
|
||||||
|
|
||||||
config = self.db.get(MemoryConfigModel, config_id)
|
config = self.db.get(MemoryConfigModel, config_id)
|
||||||
if not config:
|
if not config:
|
||||||
raise ResourceNotFoundException("MemoryConfig", str(config_id))
|
raise ResourceNotFoundException("MemoryConfig", str(config_id))
|
||||||
|
|||||||
Reference in New Issue
Block a user