| 1234567891011121314151617181920212223242526272829303132333435 |
- """按服务端会话身份映射文件根,虚拟路径保持不变。"""
- import hashlib
- from pathlib import Path
- import config
- def session_root(user_name: str, session_id: str) -> Path:
- # 哈希避免用户名或外部 ID 成为路径片段;不迁移旧共享文件以免猜错归属。
- user_key = hashlib.sha256(user_name.encode()).hexdigest()
- session_key = hashlib.sha256(session_id.encode()).hexdigest()
- root = config.ROOT / "data" / "workspaces" / user_key / session_key
- root.mkdir(parents=True, exist_ok=True)
- return root
- def backend_for_runtime(runtime):
- from deepagents.backends import CompositeBackend, FilesystemBackend
- from langgraph.config import get_config
- # 文件工具传 ToolRuntime;模型中间件传不含 config 的 Runtime。
- runtime_config = getattr(runtime, "config", None)
- if runtime_config is None:
- runtime_config = get_config()
- configurable = runtime_config.get("configurable", {})
- user = configurable.get("workspace_user")
- session = configurable.get("workspace_session")
- if not user or not session:
- raise ValueError("文件后端缺少服务端会话身份")
- root = session_root(user, session)
- for directory in ("workspace", "conversation_history"):
- (root / directory).mkdir(exist_ok=True)
- return CompositeBackend(
- default=FilesystemBackend(root_dir=root, virtual_mode=True),
- routes={"/skills/": FilesystemBackend(root_dir=config.ROOT / "skills", virtual_mode=True)},
- )
|