feat(memory-gateway): 引入 Memory Gateway 配置、凭据存储和服务编排
* 新增 MemoryGatewayConfig 和 MemoryConfig dataclass,用于配置管理。 * 实现 MemoryGatewayUserCredential 和 MemoryGatewayCredentialStore,用于处理用户凭据。 * 创建 MemoryGatewayService,用于管理与 Memory Gateway 的交互。 * 开发用于记忆设置的 JSON 配置文件。 * 增强单元测试,覆盖新功能,包括凭据存储和服务行为。 * 更新 entrypoint 和实例创建脚本,以初始化 Memory Gateway 用户存储。
This commit is contained in:
@ -55,6 +55,16 @@ def default_config_path(*, workspace: str | Path | None = None) -> Path:
|
||||
return root / ".beaver" / "config.json"
|
||||
|
||||
|
||||
def default_memory_config_path() -> Path:
|
||||
"""Resolve the shared Memory Gateway config path."""
|
||||
|
||||
explicit = os.getenv("BEAVER_MEMORY_CONFIG_PATH")
|
||||
if explicit:
|
||||
return Path(explicit).expanduser()
|
||||
|
||||
return Path(__file__).resolve().parents[3] / "memory" / "config.json"
|
||||
|
||||
|
||||
def load_config(
|
||||
*,
|
||||
workspace: str | Path | None = None,
|
||||
@ -63,24 +73,38 @@ def load_config(
|
||||
"""Load backend config; missing config is treated as an empty config."""
|
||||
|
||||
path = Path(config_path).expanduser() if config_path is not None else default_config_path(workspace=workspace)
|
||||
data: dict[str, Any] | None = None
|
||||
if path.exists():
|
||||
loaded = json.loads(path.read_text(encoding="utf-8"))
|
||||
if not isinstance(loaded, dict):
|
||||
raise ValueError(f"Beaver config must be a JSON object: {path}")
|
||||
data = loaded
|
||||
memory_data = _load_memory_config_data()
|
||||
|
||||
return BeaverConfig(
|
||||
agents_defaults=_parse_agent_defaults(data or {}),
|
||||
providers=_parse_providers((data or {}).get("providers")),
|
||||
embedding=_parse_embedding(data or {}),
|
||||
tools=_parse_tools((data or {}).get("tools")) if data is not None else ToolsConfig(),
|
||||
authz=_parse_authz((data or {}).get("authz")),
|
||||
channels=_parse_channels((data or {}).get("channels")),
|
||||
backend_identity=_parse_backend_identity(
|
||||
(data or {}).get("backend_identity") or (data or {}).get("backendIdentity")
|
||||
),
|
||||
memory=_parse_memory(memory_data),
|
||||
config_path=path,
|
||||
)
|
||||
|
||||
|
||||
def _load_memory_config_data() -> dict[str, Any]:
|
||||
path = default_memory_config_path()
|
||||
if not path.exists():
|
||||
return BeaverConfig(config_path=path)
|
||||
return {}
|
||||
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
if not isinstance(data, dict):
|
||||
raise ValueError(f"Beaver config must be a JSON object: {path}")
|
||||
|
||||
return BeaverConfig(
|
||||
agents_defaults=_parse_agent_defaults(data),
|
||||
providers=_parse_providers(data.get("providers")),
|
||||
embedding=_parse_embedding(data),
|
||||
tools=_parse_tools(data.get("tools")),
|
||||
authz=_parse_authz(data.get("authz")),
|
||||
channels=_parse_channels(data.get("channels")),
|
||||
backend_identity=_parse_backend_identity(data.get("backend_identity") or data.get("backendIdentity")),
|
||||
memory=_parse_memory(data),
|
||||
config_path=path,
|
||||
)
|
||||
raise ValueError(f"Beaver memory config must be a JSON object: {path}")
|
||||
return data
|
||||
|
||||
|
||||
def _parse_agent_defaults(data: dict[str, Any]) -> AgentDefaultsConfig:
|
||||
@ -269,12 +293,10 @@ def _parse_memory(data: dict[str, Any]) -> MemoryConfig:
|
||||
scope = (
|
||||
_string_list(gateway_raw.get("scope"))
|
||||
if "scope" in gateway_raw
|
||||
else ["current_chat", "resources"]
|
||||
else MemoryGatewayConfig().scope
|
||||
)
|
||||
gateway = MemoryGatewayConfig(
|
||||
base_url=_string(gateway_raw.get("baseUrl") or gateway_raw.get("base_url")) or "",
|
||||
user_id=_string(gateway_raw.get("userId") or gateway_raw.get("user_id")) or "",
|
||||
user_key=_string(gateway_raw.get("userKey") or gateway_raw.get("user_key")) or "",
|
||||
app_id=_string(gateway_raw.get("appId") or gateway_raw.get("app_id")) or "default",
|
||||
project_id=_string(gateway_raw.get("projectId") or gateway_raw.get("project_id")) or "default",
|
||||
scope=scope,
|
||||
@ -283,15 +305,8 @@ def _parse_memory(data: dict[str, Any]) -> MemoryConfig:
|
||||
)
|
||||
|
||||
if mode == "hybrid" and explicit:
|
||||
missing: list[str] = []
|
||||
if not gateway.base_url:
|
||||
missing.append("baseUrl")
|
||||
if not gateway.user_id:
|
||||
missing.append("userId")
|
||||
if not gateway.user_key:
|
||||
missing.append("userKey")
|
||||
if missing:
|
||||
raise ValueError(f"Explicit hybrid memory requires gateway fields: {', '.join(missing)}")
|
||||
raise ValueError("Explicit hybrid memory requires gateway.baseUrl")
|
||||
allowed_scopes = {"current_chat", "resources", "all_user_memory"}
|
||||
if not gateway.scope or any(scope not in allowed_scopes for scope in gateway.scope):
|
||||
raise ValueError("memory.gateway.scope contains an unsupported value")
|
||||
|
||||
Reference in New Issue
Block a user