J
2026-04-13 11:48:15 -04:00
|
|
|
"""Agent configuration stored at ``~/.ria/agent.json``.
|
|
|
|
|
|
|
|
|
|
Schema::
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
"hub_url": "https://riahub.example.com",
|
|
|
|
|
"agent_id": "agent-abc123",
|
|
|
|
|
"token": "rha_xxxx",
|
|
|
|
|
"name": "lab-bench-1",
|
|
|
|
|
"insecure": false
|
|
|
|
|
}
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
from dataclasses import asdict, dataclass, field
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
_DEFAULT_PATH = Path(os.environ.get("RIA_AGENT_CONFIG", str(Path.home() / ".ria" / "agent.json")))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class AgentConfig:
|
|
|
|
|
hub_url: str = ""
|
|
|
|
|
agent_id: str = ""
|
|
|
|
|
token: str = ""
|
|
|
|
|
name: str = ""
|
|
|
|
|
insecure: bool = False
|
J
2026-04-13 12:54:05 -04:00
|
|
|
api_key: str = ""
|
J
2026-04-13 11:48:15 -04:00
|
|
|
extra: dict = field(default_factory=dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def default_path() -> Path:
|
|
|
|
|
return _DEFAULT_PATH
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load(path: Path | None = None) -> AgentConfig:
|
|
|
|
|
p = path or _DEFAULT_PATH
|
|
|
|
|
if not p.exists():
|
|
|
|
|
return AgentConfig()
|
|
|
|
|
data = json.loads(p.read_text())
|
|
|
|
|
known = {f for f in AgentConfig.__dataclass_fields__ if f != "extra"}
|
|
|
|
|
extra = {k: v for k, v in data.items() if k not in known}
|
|
|
|
|
return AgentConfig(
|
|
|
|
|
hub_url=data.get("hub_url", ""),
|
|
|
|
|
agent_id=data.get("agent_id", ""),
|
|
|
|
|
token=data.get("token", ""),
|
|
|
|
|
name=data.get("name", ""),
|
|
|
|
|
insecure=bool(data.get("insecure", False)),
|
J
2026-04-13 12:54:05 -04:00
|
|
|
api_key=data.get("api_key", ""),
|
J
2026-04-13 11:48:15 -04:00
|
|
|
extra=extra,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save(cfg: AgentConfig, path: Path | None = None) -> Path:
|
|
|
|
|
p = path or _DEFAULT_PATH
|
|
|
|
|
p.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
data = asdict(cfg)
|
|
|
|
|
extra = data.pop("extra", {}) or {}
|
|
|
|
|
data.update(extra)
|
|
|
|
|
p.write_text(json.dumps(data, indent=2))
|
|
|
|
|
os.chmod(p, 0o600)
|
|
|
|
|
return p
|