Files
hermes-agent/plugins/memory/config_schema.py
kshitijk4poor 6933155eaf refactor(memory): take the Honcho-specific code out of core
Honcho left core on 2026-10-01 (7e53b3ef82) and ships from plastic-labs/honcho, but core kept
four places that know its file layout: profile create and rename edited Honcho's host blocks,
`hermes update` called the plugin's `sync_honcho_profiles_quiet()`, and the dashboard config
panel had a Honcho-only storage kind that imported the plugin's private `client` and `oauth`
modules (including its lock internals).

Replace them with seams any memory provider can use:

- MemoryProvider.on_profile_created / on_profile_renamed: called for the provider the profile's
  config selects, with that profile's home and its own .env secrets bound, from create_profile
  (CLI, dashboard, Desktop/TUI) and rename_profile. A failing hook, or an unreadable config, is
  logged and never fails the create or rename.
- STORAGE_PROVIDER for config_schema.py, with read_config_fields / write_config_fields on the
  provider: the provider owns its file layout; core keeps coercion, the memory.provider switch,
  the 400 on a refused write, and saves secrets to .env only after the provider accepted them.

Delete: hermes_cli/profiles_honcho.py, the CLI create's Honcho step, the update-time sync,
STORAGE_HONCHO_HOST_BLOCK and the host-block reader/writer, the empty `honcho: {}` config
root and its template block, HONCHO_API_KEY in _ENV_CONFIG_KEYS (the *_API_KEY rule covers
it), the dead check_config_yaml_readers exemption, and "honcho" as a hardcoded reserved
profile/subcommand name (alias collision now checks installed memory-provider names).
_record_profile_rename moves to profile_identity.py (profiles.py line budget).

Kept: honcho in the left-core migration (moves existing users onto the catalog plugin), its
env-var metadata, and comments that use it as an example.

Until the Honcho plugin implements these methods (catalog pin 7dd3702 does not), its dashboard
panel is empty, --clone creates no host block for the new profile and rename does not re-key
it; `hermes honcho sync` still creates missing host blocks by hand.
2026-11-10 00:16:18 +05:30

144 lines
5.5 KiB
Python

"""Declarative config schema for memory provider plugins: each declares its fields in
a ``config_schema.py`` beside ``__init__.py``; one generic renderer and one
``GET/PUT /api/memory/providers/{name}/config`` pair drive the UI. Schema files are
loaded by path, never imported as a package, so DISCOVERING a panel never pulls the
provider's runtime into the web server; they may import only this pure-data module.
Only ``STORAGE_PROVIDER`` reads/writes load the provider itself (in the plugin host when
``plugins.isolation`` is ``host``). ``web_server`` owns read/write, dispatching on ``storage``: a flat JSON file
core reads and writes, or ``provider``, where the provider's ``read_config_fields`` /
``write_config_fields`` own the file layout (``agent.memory_provider.MemoryProvider``).
"""
from __future__ import annotations
import importlib.util
import logging
from dataclasses import dataclass, field as dataclass_field
_log = logging.getLogger(__name__)
# Field kinds understood by the generic renderer.
KIND_TEXT = "text"
KIND_SELECT = "select"
KIND_SECRET = "secret"
KIND_BOOL = "bool"
KIND_NUMBER = "number"
KIND_JSON = "json"
# Storage backends understood by web_server (see its read/write dispatch).
STORAGE_FLAT_JSON = "flat_json"
STORAGE_PROVIDER = "provider"
@dataclass(frozen=True)
class ProviderFieldOption:
"""A single choice for a ``select`` field."""
value: str
label: str
description: str = ""
@dataclass(frozen=True)
class ProviderField:
"""One configurable field. Non-secret kinds live under ``key`` in the provider's storage;
``secret`` in the env store under ``env_key`` (a ``STORAGE_PROVIDER`` writer also receives it
and may persist it too), never read back (only ``is_set``). ``aliases``/``env_fallbacks`` read
legacy values; ``inline`` marks the compact-panel subset (rest: modal, by ``group``)."""
key: str
label: str
kind: str = KIND_TEXT
default: str = ""
description: str = ""
placeholder: str = ""
options: tuple[ProviderFieldOption, ...] = ()
env_key: str | None = None
aliases: tuple[str, ...] = ()
env_fallbacks: tuple[str, ...] = ()
inline: bool = False
group: str = ""
# Longer help text surfaced as an info tooltip next to the field label.
info: str = ""
# Placement hint a STORAGE_PROVIDER provider may read from its own schema; core ignores it (the
# "host" default keeps existing schema files valid).
scope: str = "host"
@property
def is_secret(self) -> bool:
return self.kind == KIND_SECRET
def allowed_values(self) -> set[str]:
return {opt.value for opt in self.options}
@dataclass(frozen=True)
class ProviderConfigSchema:
"""A provider plugin's declared config surface."""
name: str
label: str
storage: str = STORAGE_FLAT_JSON
# Optional link to the provider's config docs, shown in the full-config modal.
docs_url: str = ""
fields: tuple[ProviderField, ...] = dataclass_field(default_factory=tuple)
def inline_fields(self) -> tuple[ProviderField, ...]:
return tuple(f for f in self.fields if f.inline)
_SCHEMA_CACHE: dict[str, ProviderConfigSchema] = {}
_SCHEMA_FAILURES_LOGGED: set[str] = set()
def get_provider_config_schema(name: str) -> ProviderConfigSchema | None:
"""``CONFIG_SCHEMA`` of provider ``name``; None (no panel) without a ``config_schema.py``.
Cached by resolved file path, not name: user plugins are per-profile."""
from plugins.memory import find_provider_dir
provider_dir = find_provider_dir(name)
path = provider_dir / "config_schema.py" if provider_dir else None
if path is None or not path.is_file():
return None
key = str(path)
if key in _SCHEMA_CACHE:
return _SCHEMA_CACHE[key]
try:
from plugins.memory import _is_bundled
from hermes_cli.plugin_isolation import user_plugin_host
host = None if _is_bundled(provider_dir) else user_plugin_host()
if host is not None: # plugins.isolation: host — a user schema file is user code too
schema = _schema_from_record(host.config_schema(path))
else:
spec = importlib.util.spec_from_file_location(f"_hermes_memory_config_schema.{name}", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
schema = getattr(module, "CONFIG_SCHEMA", None)
except Exception:
# Never cache a failed load: it would pin an empty panel until restart. Log the traceback
# once per file, though: the panel polls, and an outdated plugin fails the same way each time.
if key in _SCHEMA_FAILURES_LOGGED:
_log.debug("failed to load config schema for memory provider %r", name, exc_info=True)
else:
_SCHEMA_FAILURES_LOGGED.add(key)
_log.exception("failed to load config schema for memory provider %r", name)
return None
if schema is not None:
_SCHEMA_CACHE[key] = schema
return schema
def _schema_from_record(record) -> ProviderConfigSchema | None:
"""Rebuild a schema that crossed the plugin-host boundary as plain data."""
if not record:
return None
fields = tuple(
ProviderField(**{**f, "options": tuple(ProviderFieldOption(**o) for o in f.get("options") or ()),
"aliases": tuple(f.get("aliases") or ()),
"env_fallbacks": tuple(f.get("env_fallbacks") or ())})
for f in record.get("fields") or ())
return ProviderConfigSchema(**{**record, "fields": fields})