mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-30 16:01:49 +08:00
Completes the cfg_get migration started in PR #17304. Covers the remaining hermes_cli/ and plugins/ config-access sites that the first PR intentionally left opportunistic. Migrated (33 sites across 14 files): hermes_cli/setup.py 13 sites (terminal.*, agent.*, display.*, compression.*, tts.*) hermes_cli/tools_config.py 7 sites (tts.*, browser.*, web.*, platform_toolsets.*) hermes_cli/plugins_cmd.py 3 sites (plugins.*, memory.*, context.*) plugins/memory/honcho/cli.py 3 sites (hosts.*) hermes_cli/web_server.py 1 site (dashboard.*) hermes_cli/skills_config.py 1 site (platform_disabled) hermes_cli/plugins.py 1 site (plugins.disabled) hermes_cli/status.py 1 site (terminal.backend) hermes_cli/mcp_config.py 1 site (mcp_servers.*) hermes_cli/webhook.py 1 site (platforms.webhook) plugins/memory/__init__.py 1 site (memory.provider) plugins/memory/hindsight/ 1 site (banks.hermes) plugins/memory/holographic/ 1 site (plugins.hermes-memory-store) run_agent.py 1 site (auxiliary.compression) The helper supports non-literal keys too, so e.g. cfg.get('hosts', {}).get(HOST, {}) becomes cfg_get(cfg, 'hosts', HOST, default={}) Migration bugs caught and fixed during this PR: 1. An AST-based batch rewrite naïvely captured the first word token in a chain, which corrupted 'self._config.get(...).get(...)' into 'self.cfg_get(_config, ...)' (dropping 'self.', creating a broken method call). Plugins/memory/hindsight caught it via its test suite. Fixed manually to 'cfg_get(self._config, ...)'. 2. Import-extension heuristic rewrote multi-line parenthesized imports ('from X import (\n A,\n B,\n)') as 'from X import cfg_get, (' — syntactically broken. Fixed by inserting cfg_get as the first name inside the parentheses. Combined with PR #17304, the cfg_get migration now covers: PR #17304 (first batch): 20 sites in tools/ + gateway/ PR #17317 (this one): 33 sites in hermes_cli/ + plugins/ + run_agent.py Total: 53 sites migrated. Remaining ~8 sites are either: - Function-call chains (e.g. '_load_stt_config().get(...).get(...)') that would need double-evaluation or a local binding to migrate cleanly — intentionally deferred. - JSON response-navigation (e.g. 'response_data.get('data',{}).get('web')) which is unrelated to config access and shouldn't use cfg_get. Verified: - 412/412 tests/plugins/ pass (including the hindsight test that caught the self.X regex bug before commit) - 3181/3189 tests/hermes_cli/ pass (8 pre-existing failures on main, verified by git-stash comparison) - Live 'hermes status' and 'hermes config' render correctly (exercise the migrated terminal.backend, tts.provider, browser.cloud_provider, compression.threshold, display.tool_progress sites) - Live 'hermes chat': 1 turn + /quit, zero errors in 11-line log window No semantic changes — cfg_get was already proven to be a 1:1 match for the original .get("X",{}).get("Y",default) pattern in PR #17304.
178 lines
7.0 KiB
Python
178 lines
7.0 KiB
Python
"""
|
|
Skills configuration for Hermes Agent.
|
|
`hermes skills` enters this module.
|
|
|
|
Toggle individual skills or categories on/off, globally or per-platform.
|
|
Config stored in ~/.hermes/config.yaml under:
|
|
|
|
skills:
|
|
disabled: [skill-a, skill-b] # global disabled list
|
|
platform_disabled: # per-platform overrides
|
|
telegram: [skill-c]
|
|
cli: []
|
|
"""
|
|
from typing import List, Optional, Set
|
|
|
|
from hermes_cli.config import cfg_get, load_config, save_config
|
|
from hermes_cli.colors import Colors, color
|
|
from hermes_cli.platforms import PLATFORMS as _PLATFORMS
|
|
|
|
# Backward-compatible view: {key: label_string} so existing code that
|
|
# iterates ``PLATFORMS.items()`` or calls ``PLATFORMS.get(key)`` keeps
|
|
# working without changes to every call site.
|
|
PLATFORMS = {k: info.label for k, info in _PLATFORMS.items() if k != "api_server"}
|
|
|
|
# ─── Config Helpers ───────────────────────────────────────────────────────────
|
|
|
|
def get_disabled_skills(config: dict, platform: Optional[str] = None) -> Set[str]:
|
|
"""Return disabled skill names. Platform-specific list falls back to global."""
|
|
skills_cfg = config.get("skills", {})
|
|
global_disabled = set(skills_cfg.get("disabled", []))
|
|
if platform is None:
|
|
return global_disabled
|
|
platform_disabled = cfg_get(skills_cfg, "platform_disabled", platform)
|
|
if platform_disabled is None:
|
|
return global_disabled
|
|
return set(platform_disabled)
|
|
|
|
|
|
def save_disabled_skills(config: dict, disabled: Set[str], platform: Optional[str] = None):
|
|
"""Persist disabled skill names to config."""
|
|
config.setdefault("skills", {})
|
|
if platform is None:
|
|
config["skills"]["disabled"] = sorted(disabled)
|
|
else:
|
|
config["skills"].setdefault("platform_disabled", {})
|
|
config["skills"]["platform_disabled"][platform] = sorted(disabled)
|
|
save_config(config)
|
|
|
|
|
|
# ─── Skill Discovery ─────────────────────────────────────────────────────────
|
|
|
|
def _list_all_skills() -> List[dict]:
|
|
"""Return all installed skills (ignoring disabled state)."""
|
|
try:
|
|
from tools.skills_tool import _find_all_skills
|
|
return _find_all_skills(skip_disabled=True)
|
|
except Exception:
|
|
return []
|
|
|
|
|
|
def _get_categories(skills: List[dict]) -> List[str]:
|
|
"""Return sorted unique category names (None -> 'uncategorized')."""
|
|
return sorted({s["category"] or "uncategorized" for s in skills})
|
|
|
|
|
|
# ─── Platform Selection ──────────────────────────────────────────────────────
|
|
|
|
def _select_platform() -> Optional[str]:
|
|
"""Ask user which platform to configure, or global."""
|
|
options = [("global", "All platforms (global default)")] + list(PLATFORMS.items())
|
|
print()
|
|
print(color(" Configure skills for:", Colors.BOLD))
|
|
for i, (key, label) in enumerate(options, 1):
|
|
print(f" {i}. {label}")
|
|
print()
|
|
try:
|
|
raw = input(color(" Select [1]: ", Colors.YELLOW)).strip()
|
|
except (KeyboardInterrupt, EOFError):
|
|
return None
|
|
if not raw:
|
|
return None # global
|
|
try:
|
|
idx = int(raw) - 1
|
|
if 0 <= idx < len(options):
|
|
key = options[idx][0]
|
|
return None if key == "global" else key
|
|
except ValueError:
|
|
pass
|
|
return None
|
|
|
|
|
|
# ─── Category Toggle ─────────────────────────────────────────────────────────
|
|
|
|
def _toggle_by_category(skills: List[dict], disabled: Set[str]) -> Set[str]:
|
|
"""Toggle all skills in a category at once."""
|
|
from hermes_cli.curses_ui import curses_checklist
|
|
|
|
categories = _get_categories(skills)
|
|
cat_labels = []
|
|
# A category is "enabled" (checked) when NOT all its skills are disabled
|
|
pre_selected = set()
|
|
for i, cat in enumerate(categories):
|
|
cat_skills = [s["name"] for s in skills if (s["category"] or "uncategorized") == cat]
|
|
cat_labels.append(f"{cat} ({len(cat_skills)} skills)")
|
|
if not all(s in disabled for s in cat_skills):
|
|
pre_selected.add(i)
|
|
|
|
chosen = curses_checklist(
|
|
"Categories — toggle entire categories",
|
|
cat_labels, pre_selected, cancel_returns=pre_selected,
|
|
)
|
|
|
|
new_disabled = set(disabled)
|
|
for i, cat in enumerate(categories):
|
|
cat_skills = {s["name"] for s in skills if (s["category"] or "uncategorized") == cat}
|
|
if i in chosen:
|
|
new_disabled -= cat_skills # category enabled → remove from disabled
|
|
else:
|
|
new_disabled |= cat_skills # category disabled → add to disabled
|
|
return new_disabled
|
|
|
|
|
|
# ─── Entry Point ──────────────────────────────────────────────────────────────
|
|
|
|
def skills_command(args=None):
|
|
"""Entry point for `hermes skills`."""
|
|
from hermes_cli.curses_ui import curses_checklist
|
|
|
|
config = load_config()
|
|
skills = _list_all_skills()
|
|
|
|
if not skills:
|
|
print(color(" No skills installed.", Colors.DIM))
|
|
return
|
|
|
|
# Step 1: Select platform
|
|
platform = _select_platform()
|
|
platform_label = PLATFORMS.get(platform, "All platforms") if platform else "All platforms"
|
|
|
|
# Step 2: Select mode — individual or by category
|
|
print()
|
|
print(color(f" Configure for: {platform_label}", Colors.DIM))
|
|
print()
|
|
print(" 1. Toggle individual skills")
|
|
print(" 2. Toggle by category")
|
|
print()
|
|
try:
|
|
mode = input(color(" Select [1]: ", Colors.YELLOW)).strip() or "1"
|
|
except (KeyboardInterrupt, EOFError):
|
|
return
|
|
|
|
disabled = get_disabled_skills(config, platform)
|
|
|
|
if mode == "2":
|
|
new_disabled = _toggle_by_category(skills, disabled)
|
|
else:
|
|
# Build labels and map indices → skill names
|
|
labels = [
|
|
f"{s['name']} ({s['category'] or 'uncategorized'}) — {s['description'][:55]}"
|
|
for s in skills
|
|
]
|
|
# "selected" = enabled (not disabled) — matches the [✓] convention
|
|
pre_selected = {i for i, s in enumerate(skills) if s["name"] not in disabled}
|
|
chosen = curses_checklist(
|
|
f"Skills for {platform_label}",
|
|
labels, pre_selected, cancel_returns=pre_selected,
|
|
)
|
|
# Anything NOT chosen is disabled
|
|
new_disabled = {skills[i]["name"] for i in range(len(skills)) if i not in chosen}
|
|
|
|
if new_disabled == disabled:
|
|
print(color(" No changes.", Colors.DIM))
|
|
return
|
|
|
|
save_disabled_skills(config, new_disabled, platform)
|
|
enabled_count = len(skills) - len(new_disabled)
|
|
print(color(f"✓ Saved: {enabled_count} enabled, {len(new_disabled)} disabled ({platform_label}).", Colors.GREEN))
|