From ca5c04818b49d25ac74cc09a892463c6a3ff43d1 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Sat, 15 Aug 2026 01:49:48 -0700 Subject: [PATCH] feat: bare /save shows a usage card instead of exporting Per review: /save with no arguments prints usage (formats, filename, redact, examples) and writes nothing; the export requires an explicit format. Unknown formats print the error plus the same usage card. One shared SAVE_USAGE string in hermes_cli/session_export.py serves both the CLI and gateway handlers. args_hint updated to to reflect the now-required format. Tests: bare-save and bad-format usage cases added; location tests pass an explicit format. --- cli.py | 12 +++++-- gateway/slash_commands.py | 26 +++++++++------ hermes_cli/commands.py | 4 +-- hermes_cli/session_export.py | 20 +++++++++++ tests/cli/test_save_conversation_location.py | 35 ++++++++++++++++++-- 5 files changed, 81 insertions(+), 16 deletions(-) diff --git a/cli.py b/cli.py index c174fd8fcb..51959deeb0 100644 --- a/cli.py +++ b/cli.py @@ -9355,20 +9355,28 @@ class HermesCLI(CLIAgentSetupMixin, CLICommandsMixin, CLIBillingMixin): redaction pass before writing. """ from hermes_cli.session_export import ( + SAVE_USAGE, normalize_save_format, render_session_for_save, ) parts = cmd.split()[1:] + if not parts: + print(SAVE_USAGE) + return redact = False - if parts and parts[-1].lower() in ("redact", "--redact"): + if parts[-1].lower() in ("redact", "--redact"): redact = True parts = parts[:-1] + if not parts: + print(SAVE_USAGE) + return try: - fmt = normalize_save_format(parts[0] if parts else None) + fmt = normalize_save_format(parts[0]) except ValueError as e: print(f"(._.) {e}") + print(SAVE_USAGE) return filename = parts[1] if len(parts) > 1 else None diff --git a/gateway/slash_commands.py b/gateway/slash_commands.py index 0d39589277..523af310e9 100644 --- a/gateway/slash_commands.py +++ b/gateway/slash_commands.py @@ -4537,27 +4537,33 @@ class GatewaySlashCommandsMixin: Usage: ``/save [json|md|html] [filename] [redact]`` """ from hermes_cli.session_export import ( + SAVE_USAGE, default_save_filename, normalize_save_format, render_session_for_save, ) + parts = event.get_command_args().split() + if not parts: + return SAVE_USAGE + redact = False + if parts[-1].lower() in ("redact", "--redact"): + redact = True + parts = parts[:-1] + if not parts: + return SAVE_USAGE + + try: + fmt = normalize_save_format(parts[0]) + except ValueError as e: + return f"{e}\n\n{SAVE_USAGE}" + source = event.source session_entry = await self.async_session_store.get_or_create_session(source) session_id = session_entry.session_id if not self._session_db: return "Session database not available." - - parts = event.get_command_args().split() - redact = False - if parts and parts[-1].lower() in ("redact", "--redact"): - redact = True - parts = parts[:-1] - try: - fmt = normalize_save_format(parts[0] if parts else None) - except ValueError as e: - return str(e) filename = parts[1] if len(parts) > 1 else default_save_filename(session_id, fmt) # The filename is echoed to the platform only — never trust path # separators from chat input. diff --git a/hermes_cli/commands.py b/hermes_cli/commands.py index 8b246f6248..c1865dfad7 100644 --- a/hermes_cli/commands.py +++ b/hermes_cli/commands.py @@ -154,8 +154,8 @@ COMMAND_REGISTRY: list[CommandDef] = [ cli_only=True), CommandDef("history", "Show conversation history", "Session", cli_only=True), - CommandDef("save", "Save/export the current conversation (json, md, or html)", "Session", - args_hint="[json|md|html] [filename] [redact]"), + CommandDef("save", "Export the current conversation (bare /save shows usage)", "Session", + args_hint=" [filename] [redact]"), CommandDef("retry", "Retry the last message (resend to agent)", "Session"), CommandDef("prompt", "Compose your next prompt in $EDITOR (markdown), then send it", "Session", cli_only=True, args_hint="[initial text]", aliases=("compose",)), diff --git a/hermes_cli/session_export.py b/hermes_cli/session_export.py index 59c52cf44d..f8695b8d62 100644 --- a/hermes_cli/session_export.py +++ b/hermes_cli/session_export.py @@ -323,6 +323,26 @@ def _finish_markdown(lines: List[str]) -> str: SAVE_FORMATS = ("json", "md", "html") +SAVE_USAGE = """/save — export the current session to a file +Usage: /save [filename] [redact] + +Formats: + json full session snapshot (canonical export shape) + md readable Markdown transcript + html standalone single-file HTML page (shareable, no dependencies) + +Options: + filename optional output name/path (default: auto-named; + CLI saves under ~/.hermes/sessions/saved/) + redact scrub API keys, tokens, and credentials before writing + +Examples: + /save json + /save html + /save md notes.md + /save html session.html redact""" + + def normalize_save_format(fmt: Optional[str]) -> str: """Map a user-typed /save format token to a canonical format.""" diff --git a/tests/cli/test_save_conversation_location.py b/tests/cli/test_save_conversation_location.py index 714ddabddb..cea981e1de 100644 --- a/tests/cli/test_save_conversation_location.py +++ b/tests/cli/test_save_conversation_location.py @@ -61,7 +61,7 @@ def test_save_conversation_writes_under_hermes_home(hermes_home, tmp_path, monke ]) # Call the unbound method against our stub. - cli.HermesCLI.save_conversation(stub) + cli.HermesCLI.save_conversation(stub, "/save json") # File must NOT be in CWD cwd_leak = list(work.glob("hermes_conversation_*.json")) @@ -95,9 +95,40 @@ def test_save_conversation_empty_history_does_nothing(hermes_home, capsys): import cli stub = _make_stub_cli([]) - cli.HermesCLI.save_conversation(stub) + cli.HermesCLI.save_conversation(stub, "/save json") saved_dir = hermes_home / "sessions" / "saved" assert not saved_dir.exists() or not list(saved_dir.iterdir()) out = capsys.readouterr().out assert "No conversation to save" in out + + +def test_save_conversation_bare_shows_usage(hermes_home, capsys): + """Bare /save prints the usage card and writes nothing.""" + for mod in [m for m in sys.modules if m.startswith("cli") or m == "hermes_constants"]: + sys.modules.pop(mod, None) + import cli + + stub = _make_stub_cli([{"role": "user", "content": "hi"}]) + cli.HermesCLI.save_conversation(stub, "/save") + + saved_dir = hermes_home / "sessions" / "saved" + assert not saved_dir.exists() or not list(saved_dir.iterdir()) + out = capsys.readouterr().out + # Usage card lists every format and the redact option + for token in ("json", "md", "html", "redact", "Usage:"): + assert token in out, (token, out) + + +def test_save_conversation_bad_format_shows_usage(hermes_home, capsys): + for mod in [m for m in sys.modules if m.startswith("cli") or m == "hermes_constants"]: + sys.modules.pop(mod, None) + import cli + + stub = _make_stub_cli([{"role": "user", "content": "hi"}]) + cli.HermesCLI.save_conversation(stub, "/save pdf") + + saved_dir = hermes_home / "sessions" / "saved" + assert not saved_dir.exists() or not list(saved_dir.iterdir()) + out = capsys.readouterr().out + assert "Usage:" in out