From 9fd2ebd69c10edbeb4e6cf3abd610688817470f0 Mon Sep 17 00:00:00 2001 From: Wenjie Zhang Date: Mon, 18 May 2026 09:40:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(services):=20=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=B1=82=E9=80=82=E9=85=8D=E7=94=A8=E6=88=B7=E9=9A=94=E7=A6=BB?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - chat_service 从 department_id 改为 uid - 优化 agent_config 解析和验证逻辑 - 更新 filesystem_service 和 workspace_service --- .../yuxi/services/agent_run_service.py | 2 +- backend/package/yuxi/services/chat_service.py | 37 +++++++------------ .../yuxi/services/filesystem_service.py | 8 ++-- .../yuxi/services/workspace_service.py | 2 +- 4 files changed, 19 insertions(+), 30 deletions(-) diff --git a/backend/package/yuxi/services/agent_run_service.py b/backend/package/yuxi/services/agent_run_service.py index 747c79a4..ecb45f07 100644 --- a/backend/package/yuxi/services/agent_run_service.py +++ b/backend/package/yuxi/services/agent_run_service.py @@ -68,7 +68,7 @@ async def create_agent_run_view( config_repo = AgentConfigRepository(db) config_item = await config_repo.get_by_id(config_id=int(agent_config_id)) - if config_item is None: + if config_item is None or config_item.uid != str(current_uid): raise HTTPException(status_code=404, detail="配置不存在") agent_id = config_item.agent_id diff --git a/backend/package/yuxi/services/chat_service.py b/backend/package/yuxi/services/chat_service.py index a32da940..2b2866ed 100644 --- a/backend/package/yuxi/services/chat_service.py +++ b/backend/package/yuxi/services/chat_service.py @@ -419,11 +419,11 @@ def _extract_ai_message(messages: list[Any] | None) -> AIMessage | None: async def get_agent_config_by_id(db, user: User, agent_config_id: int): """按配置 ID 解析 AgentConfig 记录。""" - department_id = user.department_id + uid = str(user.uid) agent_config_repo = AgentConfigRepository(db) config_item = await agent_config_repo.get_by_id(config_id=int(agent_config_id)) - if config_item is None or config_item.department_id != department_id: + if config_item is None or config_item.uid != uid: raise ValueError("配置不存在") return config_item @@ -431,7 +431,7 @@ async def get_agent_config_by_id(db, user: User, agent_config_id: int): async def _resolve_agent_config(db, agent_id: str, user: User, agent_config_id): """解析 agent_config,返回 agent_config""" - department_id = user.department_id + uid = str(user.uid) agent_config_repo = AgentConfigRepository(db) config_item = None @@ -441,9 +441,7 @@ async def _resolve_agent_config(db, agent_id: str, user: User, agent_config_id): config_item = None if config_item is None: - config_item = await agent_config_repo.get_or_create_default( - department_id=department_id, agent_id=agent_id, created_by=str(user.id) - ) + config_item = await agent_config_repo.get_or_create_default(uid=uid, agent_id=agent_id, created_by=uid) return (config_item.config_json or {}).get("context", {}) @@ -479,7 +477,6 @@ async def _ensure_thread_bound_agent_config( agent_config_repo: AgentConfigRepository, thread_id: str, uid: str, - department_id: int, agent_id: str, agent_config_id: int, ) -> None: @@ -502,13 +499,21 @@ async def _ensure_thread_bound_agent_config( f"switching to default config for agent {agent_id}" ) default_config = await agent_config_repo.get_or_create_default( - department_id=department_id, + uid=uid, agent_id=agent_id, created_by=uid, ) await conv_repo.bind_agent_config(thread_id, default_config.id) else: - await conv_repo.bind_agent_config(thread_id, agent_config_id) + if config_item.uid != uid or config_item.agent_id != agent_id: + default_config = await agent_config_repo.get_or_create_default( + uid=uid, + agent_id=agent_id, + created_by=uid, + ) + await conv_repo.bind_agent_config(thread_id, default_config.id) + else: + await conv_repo.bind_agent_config(thread_id, agent_config_id) async def agent_chat( @@ -544,14 +549,6 @@ async def agent_chat( "request_id": meta.get("request_id"), } - if not current_user.department_id: - return { - "status": "error", - "error_type": "invalid_config", - "error_message": "当前用户未绑定部门", - "request_id": meta.get("request_id"), - } - uid = str(current_user.uid) meta = dict(meta or {}) if "request_id" not in meta or not meta.get("request_id"): @@ -618,7 +615,6 @@ async def agent_chat( agent_config_repo=agent_config_repo, thread_id=thread_id, uid=uid, - department_id=current_user.department_id, agent_id=agent_id, agent_config_id=agent_config_id, ) @@ -767,10 +763,6 @@ async def stream_agent_chat( ) return - if not current_user.department_id: - yield make_chunk(status="error", error_type="invalid_config", error_message="当前用户未绑定部门", meta=meta) - return - meta = dict(meta or {}) if "request_id" not in meta or not meta.get("request_id"): logger.warning("请求缺少 request_id,已自动生成一个新的 request_id") @@ -837,7 +829,6 @@ async def stream_agent_chat( agent_config_repo=agent_config_repo, thread_id=thread_id, uid=uid, - department_id=current_user.department_id, agent_id=agent_id, agent_config_id=agent_config_id, ) diff --git a/backend/package/yuxi/services/filesystem_service.py b/backend/package/yuxi/services/filesystem_service.py index 62e1845b..8f582e71 100644 --- a/backend/package/yuxi/services/filesystem_service.py +++ b/backend/package/yuxi/services/filesystem_service.py @@ -30,16 +30,14 @@ async def _resolve_filesystem_context( config_item = None if agent_config_id is not None: config_item = await repo.get_by_id(config_id=int(agent_config_id)) - if config_item is not None and ( - config_item.department_id != user.department_id or config_item.agent_id != agent_id - ): + if config_item is not None and (config_item.uid != str(user.uid) or config_item.agent_id != agent_id): config_item = None if config_item is None: config_item = await repo.get_or_create_default( - department_id=user.department_id, + uid=str(user.uid), agent_id=agent_id, - created_by=str(user.id), + created_by=str(user.uid), ) context.update_from_dict((config_item.config_json or {}).get("context", {})) diff --git a/backend/package/yuxi/services/workspace_service.py b/backend/package/yuxi/services/workspace_service.py index bdf2556c..b399ac20 100644 --- a/backend/package/yuxi/services/workspace_service.py +++ b/backend/package/yuxi/services/workspace_service.py @@ -24,7 +24,7 @@ MAX_WORKSPACE_UPLOAD_SIZE_BYTES = 100 * 1024 * 1024 def _workspace_root(user: User) -> Path: try: - user_data_root = _global_user_data_dir(str(user.id)).resolve() + user_data_root = _global_user_data_dir(str(user.uid)).resolve() root = user_data_root / WORKSPACE_DIR_NAME except ValueError as exc: raise HTTPException(status_code=403, detail="Access denied") from exc