diff --git a/server/context.py b/server/context.py index 6fc10f5..810a930 100644 --- a/server/context.py +++ b/server/context.py @@ -8,6 +8,7 @@ from core.web_terminal import WebTerminal from modules.gui_file_manager import GuiFileManager from modules.upload_security import UploadQuarantineManager, UploadSecurityError from modules.personalization_manager import load_personalization_config +from modules.skills_manager import sync_workspace_skills import json from pathlib import Path from modules.usage_tracker import UsageTracker @@ -48,6 +49,32 @@ def _make_terminal_key(username: str, workspace_id: Optional[str] = None) -> str return f"{username}::{workspace_id}" if workspace_id else username +def _ensure_workspace_skills_synced(terminal: WebTerminal, workspace) -> None: + """ + 确保工作区 skills 已按当前个性化配置完成同步。 + 使用终端实例上的路径标记避免每次请求都重复全量拷贝。 + """ + try: + project_path = str(Path(workspace.project_path).resolve()) + except Exception: + project_path = str(workspace.project_path) + + if getattr(terminal, "_skills_synced_project_path", None) == project_path: + return + + try: + config = load_personalization_config(workspace.data_dir) + enabled_skills = config.get("enabled_skills") if isinstance(config, dict) else None + result = sync_workspace_skills(workspace.project_path, enabled_skills) + if not result.get("success"): + debug_log(f"[Skills] 工作区同步失败: {result.get('error')}") + return + terminal._skills_synced_project_path = project_path + debug_log(f"[Skills] 工作区技能已同步: {project_path} ({result.get('copied', 0)} 项)") + except Exception as exc: + debug_log(f"[Skills] 工作区同步异常: {exc}") + + def get_user_resources(username: Optional[str] = None, workspace_id: Optional[str] = None) -> Tuple[Optional[WebTerminal], Optional['modules.user_manager.UserWorkspace']]: from modules.user_manager import UserWorkspace username = (username or get_current_username()) @@ -122,6 +149,7 @@ def get_user_resources(username: Optional[str] = None, workspace_id: Optional[st terminal.user_role = "admin" if has_request_context(): session['workspace_id'] = getattr(workspace, "workspace_id", None) + _ensure_workspace_skills_synced(terminal, workspace) return terminal, workspace is_api_user = bool(session.get("is_api_user")) if has_request_context() else False @@ -229,6 +257,8 @@ def get_user_resources(username: Optional[str] = None, workspace_id: Optional[st continue except Exception as exc: debug_log(f"[admin_policy] 应用失败: {exc}") + + _ensure_workspace_skills_synced(terminal, workspace) return terminal, workspace