feat(memory-gateway): merge memory mode with main
This commit is contained in:
@ -1,12 +1,14 @@
|
||||
"""Configuration models and loaders."""
|
||||
|
||||
from .loader import default_config_path, load_config
|
||||
from .loader import default_config_path, default_memory_config_path, load_config
|
||||
from .schema import (
|
||||
AgentDefaultsConfig,
|
||||
AuthzConfig,
|
||||
BackendIdentityConfig,
|
||||
BeaverConfig,
|
||||
EmbeddingConfig,
|
||||
MemoryConfig,
|
||||
MemoryGatewayConfig,
|
||||
MCPServerConfig,
|
||||
PluginsConfig,
|
||||
ProviderConfig,
|
||||
@ -19,10 +21,13 @@ __all__ = [
|
||||
"BackendIdentityConfig",
|
||||
"BeaverConfig",
|
||||
"EmbeddingConfig",
|
||||
"MemoryConfig",
|
||||
"MemoryGatewayConfig",
|
||||
"MCPServerConfig",
|
||||
"PluginsConfig",
|
||||
"ProviderConfig",
|
||||
"ToolsConfig",
|
||||
"default_config_path",
|
||||
"default_memory_config_path",
|
||||
"load_config",
|
||||
]
|
||||
|
||||
@ -15,6 +15,8 @@ from .schema import (
|
||||
BeaverConfig,
|
||||
ChannelConfig,
|
||||
EmbeddingConfig,
|
||||
MemoryConfig,
|
||||
MemoryGatewayConfig,
|
||||
MCPServerConfig,
|
||||
PluginsConfig,
|
||||
ProviderConfig,
|
||||
@ -54,6 +56,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,
|
||||
@ -62,24 +74,39 @@ 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")
|
||||
),
|
||||
plugins=_parse_plugins((data or {}).get("plugins")),
|
||||
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")),
|
||||
plugins=_parse_plugins(data.get("plugins")),
|
||||
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")),
|
||||
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:
|
||||
@ -264,6 +291,46 @@ def _parse_backend_identity(raw: Any) -> BackendIdentityConfig:
|
||||
)
|
||||
|
||||
|
||||
def _parse_memory(data: dict[str, Any]) -> MemoryConfig:
|
||||
explicit = "memory" in data
|
||||
raw = _as_dict(data.get("memory"))
|
||||
mode = (_string(raw.get("mode")) or "hybrid").lower()
|
||||
if mode not in {"curated", "hybrid"}:
|
||||
raise ValueError("memory.mode must be 'curated' or 'hybrid'")
|
||||
|
||||
gateway_raw = _as_dict(raw.get("gateway"))
|
||||
parsed_top_k = _int(_first_config_value(gateway_raw.get("topK"), gateway_raw.get("top_k")))
|
||||
parsed_timeout = _float(
|
||||
_first_config_value(gateway_raw.get("timeoutSeconds"), gateway_raw.get("timeout_seconds"))
|
||||
)
|
||||
scope = (
|
||||
_string_list(gateway_raw.get("scope"))
|
||||
if "scope" in gateway_raw
|
||||
else MemoryGatewayConfig().scope
|
||||
)
|
||||
gateway = MemoryGatewayConfig(
|
||||
base_url=_string(gateway_raw.get("baseUrl") or gateway_raw.get("base_url")) 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,
|
||||
top_k=8 if parsed_top_k is None else parsed_top_k,
|
||||
timeout_seconds=10.0 if parsed_timeout is None else parsed_timeout,
|
||||
)
|
||||
|
||||
if mode == "hybrid" and explicit:
|
||||
if not gateway.base_url:
|
||||
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")
|
||||
if gateway.top_k < 1 or gateway.top_k > 100:
|
||||
raise ValueError("memory.gateway.topK must be between 1 and 100")
|
||||
if gateway.timeout_seconds <= 0:
|
||||
raise ValueError("memory.gateway.timeoutSeconds must be positive")
|
||||
|
||||
return MemoryConfig(mode=mode, explicit=explicit, gateway=gateway)
|
||||
|
||||
|
||||
def _as_dict(value: Any) -> dict[str, Any]:
|
||||
return value if isinstance(value, dict) else {}
|
||||
|
||||
|
||||
@ -6,6 +6,8 @@ from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from beaver.memory.gateway import MemoryConfig, MemoryGatewayConfig
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class ProviderConfig:
|
||||
@ -135,6 +137,7 @@ class BeaverConfig:
|
||||
authz: AuthzConfig = field(default_factory=AuthzConfig)
|
||||
channels: dict[str, ChannelConfig] = field(default_factory=dict)
|
||||
backend_identity: BackendIdentityConfig = field(default_factory=BackendIdentityConfig)
|
||||
memory: MemoryConfig = field(default_factory=MemoryConfig)
|
||||
config_path: Path | None = None
|
||||
|
||||
@property
|
||||
|
||||
Reference in New Issue
Block a user