memory_providers.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. """Memory-provider setup dashboard routes (schema, existing values, external dependency install).
  2. Extracted from ``hermes_cli.web_server``; helpers/state that tests monkeypatch on
  3. ``web_server`` stay there and are late-bound (cycle-safe).
  4. """
  5. import contextlib
  6. import json
  7. import logging
  8. import math
  9. import re
  10. import shlex
  11. import subprocess
  12. from pathlib import Path
  13. from typing import Any, Dict, List, Optional
  14. from fastapi import APIRouter, HTTPException
  15. from hermes_cli.web_deps import late
  16. from hermes_cli.web_server_dashboard import _invalidate_plugins_hub_cache
  17. from hermes_cli.web_server_memory import (
  18. _coerce_bool, _field_default, _field_is_set, _field_value, _field_visible, _load_memory_provider, _memory_provider_manifest, _memory_provider_setup_info, _memory_provider_setup_manifest, _normalize_memory_provider_schema, _read_memory_provider_existing_values, _require_memory_provider_ready, _run_setup_command,
  19. )
  20. from hermes_cli.web_models import MemoryProviderConfigUpdate, MemoryProviderSetupRequest
  21. from hermes_cli.web_routers._common import _CONFIG_MUTATION_LOCK, scoped_to_thread
  22. from plugins.memory.config_schema import (
  23. STORAGE_HONCHO_HOST_BLOCK, STORAGE_PROVIDER, ProviderConfigSchema, ProviderField, get_provider_config_schema,
  24. )
  25. _log = logging.getLogger("hermes_cli.web_server")
  26. router = APIRouter()
  27. # Late-bound so a test's monkeypatch on the owning module wins at call time.
  28. _discover_memory_provider_statuses = late("_discover_memory_provider_statuses", "hermes_cli.web_server_memory")
  29. get_hermes_home = late("get_hermes_home", "hermes_cli.config")
  30. load_config = late("load_config", "hermes_cli.config")
  31. save_config = late("save_config", "hermes_cli.config")
  32. save_env_value = late("save_env_value", "hermes_cli.config")
  33. load_env = late("load_env", "hermes_cli.config")
  34. # Sentinel: remove this key so it falls back to the host or built-in default.
  35. _UNSET: Any = object()
  36. _MEMORY_PROVIDER_NAME_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$")
  37. def _unknown_provider(name: str) -> HTTPException:
  38. return HTTPException(status_code=404, detail=f"Unknown memory provider: {name}")
  39. @contextlib.contextmanager
  40. def _value_errors_as_http(log_msg: str, name: str, *, passthrough_http: bool = True):
  41. """``ValueError`` -> 400 with its text; any other error -> logged 500 (an
  42. ``HTTPException`` passes through unless ``passthrough_http`` is False)."""
  43. try:
  44. yield
  45. except HTTPException:
  46. if passthrough_http:
  47. raise
  48. _log.exception(log_msg, name)
  49. raise HTTPException(status_code=500, detail="Internal server error")
  50. except ValueError as exc:
  51. raise HTTPException(status_code=400, detail=str(exc)) from exc
  52. except Exception:
  53. _log.exception(log_msg, name)
  54. raise HTTPException(status_code=500, detail="Internal server error")
  55. # ── Declared-schema surface (plugins.memory.config_schema) ────────────────────
  56. def _coerce_field_value(field: ProviderField, raw: str) -> Any:
  57. """Coerce a submitted non-secret string to its native JSON type.
  58. A bool is stored as JSON ``false`` rather than ``"false"`` (truthy). Blank
  59. number/json/text clears the key (``_UNSET``); raises ``ValueError`` on malformed input.
  60. """
  61. value = (raw or "").strip()
  62. kind = field.kind
  63. if kind == "select":
  64. value = value or field.default
  65. if value not in field.allowed_values():
  66. raise ValueError(f"Invalid value for '{field.key}'")
  67. return value
  68. if kind == "bool":
  69. from utils import is_truthy_value
  70. return is_truthy_value(value)
  71. if not value:
  72. return _UNSET
  73. if kind == "number":
  74. try:
  75. number = float(value)
  76. except ValueError as exc:
  77. raise ValueError(f"Invalid number for '{field.key}'") from exc
  78. return int(number) if number.is_integer() else number
  79. if kind == "json":
  80. try:
  81. parsed = json.loads(value)
  82. except (ValueError, TypeError) as exc:
  83. raise ValueError(f"Invalid JSON for '{field.key}'") from exc
  84. if isinstance(parsed, (dict, list)):
  85. return parsed
  86. raise ValueError(f"'{field.key}' must be a JSON object or array")
  87. return value
  88. def _read_json_dict(path: Path, what: str) -> dict[str, Any]:
  89. """Read a JSON object from ``path``; missing/unreadable/non-dict -> ``{}``."""
  90. if not path.exists():
  91. return {}
  92. try:
  93. data = json.loads(path.read_text(encoding="utf-8-sig"))
  94. except Exception:
  95. _log.warning("Failed to read %s from %s", what, path, exc_info=True)
  96. return {}
  97. return data if isinstance(data, dict) else {}
  98. def _flat_json_path(provider: ProviderConfigSchema) -> Path:
  99. return get_hermes_home() / provider.name / "config.json"
  100. def _read_flat_json(provider: ProviderConfigSchema) -> dict[str, Any]:
  101. return _read_json_dict(_flat_json_path(provider), "memory provider config")
  102. def _honcho_resolvers(name: str):
  103. """Host-block resolvers of provider *name*'s own ``client`` module, wherever the provider is
  104. installed (bundled or ``$HERMES_HOME/plugins/``)."""
  105. from plugins.memory import import_provider_module
  106. client = import_provider_module(name, "client")
  107. return client.resolve_active_host, client.resolve_config_path, client._host_block
  108. def _provider_instance(provider: ProviderConfigSchema):
  109. """The loaded provider behind a ``STORAGE_PROVIDER`` schema (it owns its config layout)."""
  110. instance = _load_memory_provider(provider.name, register_skills=False) # the panel never registers skills
  111. if instance is None:
  112. raise _unknown_provider(provider.name)
  113. return instance
  114. def _submitted_secrets(provider: ProviderConfigSchema, values: dict[str, str]) -> list:
  115. """The ``(field, submitted)`` pairs of each non-blank secret submission."""
  116. pairs = ((field, (values.get(field.key) or "").strip()) for field in provider.fields if field.is_secret)
  117. return [(field, submitted) for field, submitted in pairs if submitted]
  118. def _save_submitted_secrets(provider: ProviderConfigSchema, values: dict[str, str]) -> list:
  119. """Persist each non-blank secret submission to the env store (when the field has an
  120. ``env_key``); return the ``(field, submitted)`` pairs for backend-specific handling."""
  121. saved = _submitted_secrets(provider, values)
  122. for field, submitted in saved:
  123. if field.env_key:
  124. save_env_value(field.env_key, submitted)
  125. return saved
  126. def _apply_field_values(provider: ProviderConfigSchema, values: dict[str, str], target_for) -> None:
  127. """Apply submitted non-secret fields to their backend dict, in place.
  128. Only keys present in ``values`` are touched, so a partial save never
  129. clobbers fields owned by another surface. ``_UNSET`` clears the key (and
  130. its aliases) so it falls back to the host/default mapping.
  131. """
  132. natives = _submitted_natives(provider, values)
  133. for field in provider.fields:
  134. if field.key not in natives:
  135. continue
  136. target = target_for(field)
  137. coerced = natives[field.key]
  138. if coerced is _UNSET:
  139. for key in (field.key, *field.aliases):
  140. target.pop(key, None)
  141. else:
  142. target[field.key] = coerced
  143. def _write_json_0600(path: Path, data: dict[str, Any]) -> None:
  144. from utils import atomic_json_write
  145. from hermes_constants import mkdir_under_hermes_home
  146. mkdir_under_hermes_home(path.parent)
  147. atomic_json_write(path, data, mode=0o600)
  148. def _submitted_natives(provider: ProviderConfigSchema, values: dict[str, str]) -> dict[str, Any]:
  149. """The submitted non-secret fields as natives, ``_UNSET`` for a cleared one (only keys present in
  150. ``values``, so a partial save never clobbers fields owned by another surface)."""
  151. return {field.key: _coerce_field_value(field, values[field.key])
  152. for field in provider.fields if not field.is_secret and field.key in values}
  153. def _write_provider_own(provider: ProviderConfigSchema, values: dict[str, str]) -> None:
  154. """Hand the submitted fields to the provider's ``write_config_fields`` (natives, ``None`` = clear,
  155. non-blank secrets included); secrets reach ``.env`` only once the provider accepted the write."""
  156. fields: dict[str, Any] = {field.key: submitted for field, submitted in _submitted_secrets(provider, values)}
  157. fields.update({k: None if v is _UNSET else v for k, v in _submitted_natives(provider, values).items()})
  158. _provider_instance(provider).write_config_fields(fields, str(get_hermes_home()))
  159. _save_submitted_secrets(provider, values)
  160. def _write_provider_flat(provider: ProviderConfigSchema, values: dict[str, str]) -> None:
  161. existing = _read_flat_json(provider)
  162. _save_submitted_secrets(provider, values)
  163. _apply_field_values(provider, values, lambda field: existing)
  164. _write_json_0600(_flat_json_path(provider), existing)
  165. def _write_provider_honcho(provider: ProviderConfigSchema, values: dict[str, str]) -> None:
  166. """Persist submitted fields to Honcho's real config for the active host (partial
  167. saves touch only submitted keys; blank text clears a key — see ``_apply_field_values``)."""
  168. from plugins.memory import import_provider_module
  169. oauth = import_provider_module(provider.name, "oauth")
  170. resolve_active_host, resolve_config_path, host_block_of = _honcho_resolvers(provider.name)
  171. host = resolve_active_host()
  172. # Write the file reads resolve, or a save shadows it with a sparse copy.
  173. path = resolve_config_path()
  174. # OAuth rotation is single-use; an unlocked RMW here can revoke the grant.
  175. with oauth._refresh_lock, oauth._config_refresh_lock(path):
  176. # Strict: a file that exists but does not parse must not be replaced by this host's block alone.
  177. cfg = oauth._read_config_strict(path)
  178. hosts = cfg.get("hosts")
  179. cfg["hosts"] = hosts = hosts if isinstance(hosts, dict) else {}
  180. # Update the block reads resolve (legacy dot-form included), never shadow it.
  181. existing = host_block_of(cfg, host)
  182. host_key = next((k for k, v in hosts.items() if v is existing), host) if existing else host
  183. host_block = hosts.setdefault(host_key, existing)
  184. for field, submitted in _save_submitted_secrets(provider, values):
  185. # Persist where the client reads first; an OAuth token owns that slot.
  186. stored = host_block.get(field.key)
  187. if not (isinstance(stored, str) and stored.startswith(oauth.ACCESS_TOKEN_PREFIX)):
  188. host_block[field.key] = submitted
  189. _apply_field_values(provider, values, lambda field: host_block if field.scope == "host" else cfg)
  190. _write_json_0600(path, cfg)
  191. def _serialize_field_value(field: ProviderField, value: Any) -> str:
  192. """Render a stored native value as the string the generic UI edits (``None`` = key
  193. absent -> declared default; bools -> "true"/"false"; JSON containers re-encoded)."""
  194. if value is None:
  195. return field.default
  196. if field.kind == "bool":
  197. from utils import is_truthy_value
  198. return "true" if is_truthy_value(value) else "false"
  199. if field.kind == "json" and isinstance(value, (dict, list)):
  200. return json.dumps(value)
  201. return str(value)
  202. def _read_field(field: ProviderField, sources: tuple, env: dict[str, str]) -> Any:
  203. """Stored native value from the first source holding it, else ``None``.
  204. Presence (``key in source``) decides, not truthiness, so a stored ``False``
  205. or ``0`` survives instead of being mistaken for "unset".
  206. """
  207. for source in sources:
  208. for source_key in (field.key, *field.aliases):
  209. if source_key in source and source[source_key] is not None:
  210. return source[source_key]
  211. for env_key in field.env_fallbacks:
  212. if env.get(env_key):
  213. return env[env_key]
  214. return None
  215. def _declared_field_is_set(field: ProviderField, sources: tuple, env: dict[str, str]) -> bool:
  216. if any(env_key and env.get(env_key) for env_key in (field.env_key, *field.env_fallbacks)):
  217. return True
  218. return any(source.get(k) for source in sources for k in (field.key, *field.aliases))
  219. def _declared_provider_payload(provider: ProviderConfigSchema) -> dict[str, Any]:
  220. env = load_env()
  221. is_honcho = provider.storage == STORAGE_HONCHO_HOST_BLOCK
  222. # A provider-owned layout reports presence (a stored False/0 is set), like the host block.
  223. own = provider.storage == STORAGE_PROVIDER
  224. placeholders: dict[str, Any] = {}
  225. if own:
  226. stored = _provider_instance(provider).read_config_fields(str(get_hermes_home())) or {}
  227. data = dict(stored.get("values") or {})
  228. placeholders = dict(stored.get("placeholders") or {})
  229. def sources_for(field: ProviderField) -> tuple:
  230. return (data,)
  231. elif is_honcho:
  232. resolve_active_host, resolve_config_path, host_block_of = _honcho_resolvers(provider.name)
  233. host = resolve_active_host()
  234. # Blank fields surface the resolved host Honcho will actually use.
  235. placeholders = {"workspace": host, "aiPeer": host}
  236. raw = _read_json_dict(resolve_config_path(), "Honcho config")
  237. host_block = host_block_of(raw, host)
  238. def sources_for(field: ProviderField) -> tuple:
  239. return (host_block, raw) if field.scope == "host" else (raw,)
  240. else:
  241. data = _read_flat_json(provider)
  242. def sources_for(field: ProviderField) -> tuple:
  243. return (data,)
  244. fields: list[dict[str, Any]] = []
  245. for field in provider.fields:
  246. entry = {k: getattr(field, k) for k in ("key", "label", "kind", "description", "info", "placeholder", "inline", "group")}
  247. entry["options"] = [{"value": o.value, "label": o.label, "description": o.description} for o in field.options]
  248. sources = sources_for(field)
  249. if not field.placeholder and placeholders.get(field.key):
  250. entry["placeholder"] = str(placeholders[field.key])
  251. if field.is_secret:
  252. entry["value"] = "" # secrets are write-only over the API
  253. entry["is_set"] = _declared_field_is_set(field, sources, env)
  254. fields.append(entry)
  255. continue
  256. native = _read_field(field, sources, env)
  257. value = _serialize_field_value(field, native)
  258. if field.kind == "select" and value not in field.allowed_values():
  259. value = field.default
  260. entry["value"] = value
  261. # Presence, not truthiness — a stored False/0 is still "set".
  262. entry["is_set"] = native is not None if (is_honcho or own) else bool(value)
  263. fields.append(entry)
  264. return {"name": provider.name, "label": provider.label, "docs_url": provider.docs_url, "fields": fields}
  265. def _stringify_submitted(value: Any) -> str:
  266. """The declared-schema path edits strings; the dashboard may send natives."""
  267. if value is None:
  268. return ""
  269. if isinstance(value, bool):
  270. return "true" if value else "false"
  271. if isinstance(value, (dict, list)):
  272. return json.dumps(value)
  273. return str(value)
  274. def _memory_section(config: dict[str, Any]) -> dict[str, Any]:
  275. """Return ``config["memory"]`` as a dict, creating/replacing a non-dict value."""
  276. memory_config = config.get("memory")
  277. if not isinstance(memory_config, dict):
  278. memory_config = config["memory"] = {}
  279. return memory_config
  280. def _update_memory_provider_config(provider: ProviderConfigSchema, values: dict[str, str]) -> None:
  281. writer = {STORAGE_PROVIDER: _write_provider_own,
  282. STORAGE_HONCHO_HOST_BLOCK: _write_provider_honcho}.get(provider.storage, _write_provider_flat)
  283. writer(provider, values)
  284. with _CONFIG_MUTATION_LOCK: # RMW span vs. the dashboard's config autosave
  285. config = load_config()
  286. memory_config = _memory_section(config)
  287. if memory_config.get("provider") != provider.name:
  288. memory_config["provider"] = provider.name
  289. save_config(config)
  290. # ── Setup: dependency installation ────────────────────────────────────────────
  291. def _trim_setup_output(value: Optional[str], limit: int = 4000) -> str:
  292. text = str(value or "")
  293. return text if len(text) <= limit else f"{text[:limit]}\n... truncated ..."
  294. def _command_result(
  295. *, kind: str, name: str, status: str, command: str = "",
  296. completed: Optional[subprocess.CompletedProcess] = None, error: Optional[str] = None,
  297. ) -> dict[str, Any]:
  298. return {
  299. "kind": kind, "name": name, "status": status, "command": command,
  300. "returncode": None if completed is None else completed.returncode,
  301. "stdout": "" if completed is None else _trim_setup_output(completed.stdout),
  302. "stderr": _trim_setup_output(error or ("" if completed is None else completed.stderr)),
  303. }
  304. def _install_memory_provider_python_dependencies(name: str) -> list[dict[str, Any]]:
  305. from hermes_cli.memory_setup import prepare_memory_provider_dependencies
  306. command = "hermes pm install"
  307. try:
  308. _manifest, status = prepare_memory_provider_dependencies(name)
  309. except Exception as exc:
  310. return [_command_result(kind="pip", name=name, status="failed", command=command, error=str(exc))]
  311. return [_command_result(kind="pip", name=name, status=status, command=command)] if status else []
  312. def _run_setup_step(results: list, kind: str, name: str, command: str, status_of, **kwargs) -> Optional[int]:
  313. """Run a setup command, append its result row; returncode or None on spawn failure."""
  314. try:
  315. completed = _run_setup_command(command if kwargs.get("shell") else shlex.split(command), display=command, **kwargs)
  316. except Exception as exc:
  317. results.append(_command_result(kind=kind, name=name, status=status_of(None), command=command, error=str(exc)))
  318. return None
  319. results.append(_command_result(kind=kind, name=name, status=status_of(completed.returncode == 0), command=command, completed=completed))
  320. return completed.returncode
  321. def _install_memory_provider_external_dependencies(dependencies: list[dict[str, str]]) -> list[dict[str, Any]]:
  322. results: list[dict[str, Any]] = []
  323. for dep in dependencies:
  324. name = dep.get("name") or "dependency"
  325. check_cmd = dep.get("check") or ""
  326. install_cmd = dep.get("install") or ""
  327. # Check first: "already_installed" short-circuits; a failed check is
  328. # "missing" when an install step can fix it, "failed" otherwise.
  329. if check_cmd and _run_setup_step(
  330. results, "external_check", name, check_cmd,
  331. lambda ok: "already_installed" if ok else ("missing" if install_cmd else "failed"), timeout=20,
  332. ) == 0:
  333. continue
  334. if not install_cmd:
  335. continue
  336. rc = _run_setup_step(
  337. results, "external_install", name, install_cmd, lambda ok: "installed" if ok else "failed",
  338. shell=True, timeout=300,
  339. )
  340. if check_cmd and rc == 0:
  341. _run_setup_step(results, "external_check", name, check_cmd, lambda ok: "verified" if ok else "failed", timeout=20)
  342. return results
  343. def _install_memory_provider_setup(name: str) -> dict[str, Any]:
  344. provider = _load_memory_provider(name)
  345. manifest = _memory_provider_manifest(name)
  346. if provider is None and not manifest:
  347. raise _unknown_provider(name)
  348. results = _install_memory_provider_python_dependencies(name)
  349. try:
  350. setup, _inputs = _memory_provider_setup_manifest(name)
  351. except Exception as exc:
  352. results.append(_command_result(kind="setup", name=name, status="failed", error=str(exc)))
  353. setup = {"external_dependencies": []}
  354. results.extend(_install_memory_provider_external_dependencies(setup["external_dependencies"]))
  355. if not results:
  356. results.append(_command_result(kind="setup", name=name, status="no_declared_steps"))
  357. ok = all(result["status"] != "failed" for result in results)
  358. statuses = {row["name"]: row for row in _discover_memory_provider_statuses()}
  359. return {"ok": ok, "provider": name, "results": results, "status": statuses.get(name)}
  360. # ── Legacy provider surface (provider.config_schema()) ────────────────────────
  361. def _memory_provider_payload(name: str, provider: Any) -> dict[str, Any]:
  362. data = _read_memory_provider_existing_values(name)
  363. fields = [
  364. {
  365. **{k: field[k] for k in ("key", "label", "kind", "description", "placeholder", "required")},
  366. "value": "" if field["kind"] == "secret" else _field_value(field, data),
  367. "is_set": _field_is_set(field, data), "options": field.get("options", []), "url": field.get("url", ""),
  368. **{k: field.get(k) for k in ("when", "minimum", "maximum", "step")},
  369. }
  370. for field in _normalize_memory_provider_schema(name, provider)
  371. ]
  372. return {
  373. "name": name, "label": name.replace("_", " ").replace("-", " ").title(), "fields": fields,
  374. "setup": _memory_provider_setup_info(name),
  375. }
  376. def _coerce_schema_number(field: dict[str, Any], raw: Any) -> int | float:
  377. value = raw if raw is not None and raw != "" else _field_default(field)
  378. try:
  379. if isinstance(value, bool) or not math.isfinite(result := float(value)):
  380. raise ValueError
  381. if field["kind"] == "integer":
  382. if not result.is_integer():
  383. raise ValueError
  384. result = int(result)
  385. except (TypeError, ValueError, OverflowError) as exc:
  386. raise ValueError(f"Invalid numeric value for '{field['key']}'") from exc
  387. minimum, maximum = field.get("minimum"), field.get("maximum")
  388. if minimum is not None and result < minimum:
  389. raise ValueError(f"'{field['key']}' must be at least {minimum}")
  390. if maximum is not None and result > maximum:
  391. raise ValueError(f"'{field['key']}' must be at most {maximum}")
  392. return result
  393. def _coerce_schema_field(field: dict[str, Any], raw: Any) -> Any:
  394. kind = field["kind"]
  395. if kind == "boolean":
  396. return _coerce_bool(raw, default=_coerce_bool(_field_default(field), default=False))
  397. if kind in {"integer", "number"}:
  398. return _coerce_schema_number(field, raw)
  399. value = str(raw if raw is not None else "").strip()
  400. if kind == "select":
  401. value = value or str(_field_default(field))
  402. if value not in {opt["value"] for opt in field.get("options", [])}:
  403. raise ValueError(f"Invalid value for '{field['key']}'")
  404. return value
  405. return value or _field_default(field)
  406. def _save_memory_provider_native_config(name: str, provider: Any, values: dict[str, Any]) -> None:
  407. if provider is not None and hasattr(provider, "save_config"):
  408. try:
  409. from agent.memory_provider import MemoryProvider as _BaseMemoryProvider
  410. except Exception:
  411. _BaseMemoryProvider = None
  412. if _BaseMemoryProvider is None or type(provider).save_config is not _BaseMemoryProvider.save_config:
  413. provider.save_config(values, str(get_hermes_home()))
  414. return
  415. with _CONFIG_MUTATION_LOCK: # RMW span vs. the dashboard's config autosave
  416. cfg = load_config()
  417. memory_cfg = _memory_section(cfg)
  418. current = memory_cfg.get(name)
  419. memory_cfg[name] = {**(current if isinstance(current, dict) else {}), **values}
  420. save_config(cfg)
  421. def _write_memory_provider_config_values(name: str, provider: Any, values: dict[str, Any]) -> None:
  422. existing = _read_memory_provider_existing_values(name)
  423. fields = _normalize_memory_provider_schema(name, provider)
  424. fields_by_key = {field["key"]: field for field in fields}
  425. config_values: dict[str, Any] = {}
  426. secrets: dict[str, str] = {}
  427. for field in fields:
  428. if not _field_visible(field, {**existing, **config_values}, fields_by_key):
  429. continue
  430. key = field["key"]
  431. if field["kind"] == "secret":
  432. submitted = str(values.get(key) or "").strip()
  433. if submitted and field.get("_env_key"):
  434. secrets[str(field["_env_key"])] = submitted
  435. continue
  436. raw = values[key] if key in values else existing.get(key, _field_default(field))
  437. config_values[key] = _coerce_schema_field(field, raw)
  438. _save_memory_provider_native_config(name, provider, config_values)
  439. for env_key, secret in secrets.items():
  440. save_env_value(env_key, secret)
  441. # ── Routes ────────────────────────────────────────────────────────────────────
  442. def _require_valid_memory_provider_name(name: str) -> None:
  443. """Reject provider names that could traverse outside the plugin dirs.
  444. ``name`` is interpolated into filesystem paths by ``find_provider_dir()``
  445. and gates which plugin manifest's setup commands run; a strict charset
  446. allowlist (no path separators, no dots) makes traversal impossible.
  447. """
  448. if not _MEMORY_PROVIDER_NAME_RE.fullmatch(name or ""):
  449. raise _unknown_provider(name)
  450. @router.get("/api/memory/providers/{name}/config")
  451. async def get_memory_provider_config(name: str, surface: Optional[str] = None, profile: Optional[str] = None):
  452. _require_valid_memory_provider_name(name)
  453. def _run():
  454. # Undeclared providers (e.g. builtin) have no config surface; an
  455. # empty schema makes the generic panel render nothing.
  456. if surface == "declared":
  457. declared = get_provider_config_schema(name)
  458. if declared is None:
  459. return {"name": name, "label": name, "docs_url": "", "fields": []}
  460. return _declared_provider_payload(declared)
  461. provider = _load_memory_provider(name)
  462. if provider is None:
  463. return {"name": name, "label": name, "fields": [], "setup": _memory_provider_setup_info(name)}
  464. return _memory_provider_payload(name, provider)
  465. with _value_errors_as_http("GET /api/memory/providers/%s/config failed", name):
  466. return await scoped_to_thread(profile, _run)
  467. @router.post("/api/memory/providers/{name}/setup")
  468. async def setup_memory_provider(name: str, body: MemoryProviderSetupRequest,
  469. profile: Optional[str] = None):
  470. _require_valid_memory_provider_name(name)
  471. def _run():
  472. provider = _load_memory_provider(name)
  473. if provider is None and not _memory_provider_manifest(name):
  474. # No discoverable plugin directory -> no manifest that could declare
  475. # setup commands; refuse before the command-running path. (provider
  476. # may be None with a manifest present when its pip deps aren't
  477. # installed yet — that's the setup use case.)
  478. raise _unknown_provider(name)
  479. if provider is not None and body.values:
  480. with _value_errors_as_http("Failed to persist memory provider setup values for %s", name,
  481. passthrough_http=False):
  482. _write_memory_provider_config_values(name, provider, body.values)
  483. _invalidate_plugins_hub_cache()
  484. return _install_memory_provider_setup(name)
  485. return await scoped_to_thread(profile, _run)
  486. @router.put("/api/memory/providers/{name}/config")
  487. async def update_memory_provider_config(
  488. name: str, body: MemoryProviderConfigUpdate, surface: Optional[str] = None, profile: Optional[str] = None
  489. ):
  490. _require_valid_memory_provider_name(name)
  491. values = body.values or {}
  492. def _run():
  493. if surface == "declared":
  494. declared = get_provider_config_schema(name)
  495. if declared is None:
  496. raise _unknown_provider(name)
  497. _update_memory_provider_config(declared, {k: _stringify_submitted(v) for k, v in values.items()})
  498. _invalidate_plugins_hub_cache()
  499. return {"ok": True}
  500. provider = _load_memory_provider(name)
  501. if provider is None:
  502. raise _unknown_provider(name)
  503. _write_memory_provider_config_values(name, provider, values)
  504. _require_memory_provider_ready(name)
  505. with _CONFIG_MUTATION_LOCK: # RMW span vs. the dashboard's config autosave
  506. config = load_config()
  507. _memory_section(config)["provider"] = name
  508. save_config(config)
  509. _invalidate_plugins_hub_cache()
  510. return {"ok": True, "active": name}
  511. with _value_errors_as_http("PUT /api/memory/providers/%s/config failed", name):
  512. return await scoped_to_thread(profile, _run)