File size: 1,867 Bytes
3e0f245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/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)