#!/usr/bin/env python """Optional persistence — bank each research brief to the Hermes vault so studio research COMPOUNDS. Local/internal only: the vault lives on the LAN (Hermes Pi), so the public cloud demo simply can't reach it and skips silently. Enabled with RESEARCH_SAVE=1. Uses urllib + json (NOT shell `curl -d`, which mangles apostrophes/%/dashes per the studio vault rule) so text is encoded safely. """ import json import os import urllib.request VAULT_URL = os.environ.get("RESEARCH_VAULT_URL", "http://127.0.0.1:8500") VAULT_KEY = os.environ.get("HERMES_API_KEY", "") # env-only — never hardcode a key into the repo ENABLED = os.environ.get("RESEARCH_SAVE", "0") == "1" def save(question: str, report: str, sources: list, area: str = "web") -> bool: """Ingest a brief into the vault. Returns True on success; never raises.""" if not ENABLED: return False src_lines = "\n".join(f"[{s.get('n')}] {s.get('title', '')} — {s.get('url', '')}" for s in sources) text = (f"RESEARCH BRIEF — {question}\nAREA: {area}\n\n{report}\n\nSOURCES:\n{src_lines}") body = json.dumps({"text": text, "domain": "research", "source": "web-research-agent"}).encode() req = urllib.request.Request(VAULT_URL + "/ingest", data=body, method="POST", headers={"Content-Type": "application/json", "X-API-Key": VAULT_KEY}) try: urllib.request.urlopen(req, timeout=6) return True except Exception as e: print(f"vault save skipped: {type(e).__name__}") # TYPE only — never key/repr return False if __name__ == "__main__": os.environ["RESEARCH_SAVE"] = "1" ENABLED = True ok = save("TEST — vault wiring for web-research-agent", "This is a connectivity test brief.", [{"n": 1, "title": "t", "url": "http://x"}]) print("saved:", ok)