#!/usr/bin/env python3 """Export one Hermes session from the box's state.db as a public-safe transcript for the hermes-runs dataset. Keeps: the user sentence, assistant text, reasoning, tool calls (name + args), tool outputs, timestamps, token counts. DROPS the system prompt (it carries this box's full skill list and memory) and any session metadata beyond model id, timings, token and call counts. usage: hermes-run-export.py """ import json, sqlite3, sys, os sid, out = sys.argv[1], sys.argv[2] os.makedirs(out, exist_ok=True) c = sqlite3.connect("file:" + os.path.expanduser("~/.hermes/state.db") + "?mode=ro", uri=True) c.row_factory = sqlite3.Row s = c.execute("select model, started_at, ended_at, end_reason, message_count, tool_call_count, input_tokens, " "output_tokens, reasoning_tokens, api_call_count from sessions where id=?", (sid,)).fetchone() meta = {k: s[k] for k in s.keys()} meta["session_id"] = sid msgs = [] for m in c.execute("select role, content, tool_calls, tool_name, tool_call_id, timestamp, reasoning, token_count " "from messages where session_id=? and role != 'system' order by id", (sid,)): d = {"role": m["role"], "t": m["timestamp"]} if m["content"]: if m["role"] == "tool": try: d["output"] = json.loads(json.loads(m["content"])["output"]) except Exception: d["output"] = m["content"] else: d["content"] = m["content"] if m["tool_calls"]: tc = json.loads(m["tool_calls"]) d["tool_calls"] = [{"name": x.get("function", {}).get("name"), "arguments": json.loads(x.get("function", {}).get("arguments") or "{}")} for x in tc] if m["tool_name"]: d["tool_name"] = m["tool_name"] if m["reasoning"]: d["reasoning"] = m["reasoning"] msgs.append(d) json.dump({"meta": meta, "messages": msgs}, open(f"{out}/transcript.json", "w"), indent=1) # readable version t0 = meta["started_at"] L = [f"# Hermes session {sid}", "", f"model id on the box: {meta['model']} (see run.json for the GGUF actually served)", ""] for d in msgs: ts = f"+{d['t'] - t0:5.1f}s" if d.get("t") else "" if d["role"] == "user": L += [f"**user** {ts}", "", d.get("content", ""), ""] elif d["role"] == "assistant": if d.get("reasoning"): L += [f"*reasoning* {ts}", "", "> " + d["reasoning"].strip().replace("\n", "\n> "), ""] for tcall in d.get("tool_calls", []): L += [f"**tool call** {ts} `{tcall['name']}`", "", "```", json.dumps(tcall["arguments"], indent=1), "```", ""] if d.get("content"): L += [f"**assistant** {ts}", "", d["content"], ""] elif d["role"] == "tool": o = d.get("output") L += [f"**tool output** {ts}", "", "```", (json.dumps(o, indent=1) if not isinstance(o, str) else o)[:4000], "```", ""] open(f"{out}/transcript.md", "w").write("\n".join(L)) print(f"{sid}: {len(msgs)} messages -> {out}")