#!/usr/bin/env python3
"""Extrahiert lesbaren Gesprächsverlauf (user+assistant Text) aus einer Claude-Session-JSONL.
Redaktiert offensichtliche Secrets vor Ausgabe. Tool-Calls/Notifications werden übersprungen.
Nutzung: extract_conv.py <session.jsonl> [--md > out.md]"""
import json, re, sys, os

def redact(t):
    t = re.sub(r'\b(sk-[A-Za-z0-9]{20,}|tskey-[A-Za-z0-9-]{20,}|ghp_[A-Za-z0-9]{20,})\b', '[REDACTED-KEY]', t)
    t = re.sub(r'\b[A-Fa-f0-9]{32,}\b', '[REDACTED-HEX]', t)
    t = re.sub(r'\b[A-Za-z0-9_\-]{8}~[A-Za-z0-9_\-.]{18,}\b', '[REDACTED-SECRET]', t)
    return t

def get_text(content):
    if isinstance(content, str): return content
    if isinstance(content, list):
        return "\n".join(b.get("text","") for b in content if isinstance(b, dict) and b.get("type")=="text")
    return ""

def extract(path):
    out=[]
    for line in open(path, encoding="utf-8", errors="ignore"):
        try: o=json.loads(line)
        except: continue
        m=o.get("message") or o
        role=m.get("role") or o.get("type","")
        if role not in ("user","assistant","human"): continue
        txt=get_text(m.get("content"))
        if not txt or not txt.strip(): continue
        if txt.startswith("[Request interrupted") or "<task-notification>" in txt or "tool_use_id" in txt or "<local-command" in txt: continue
        txt=re.sub(r'[ \t]+\n','\n',txt).strip()
        if len(txt)<2: continue
        out.append((role, o.get("timestamp",""), redact(txt)))
    return out

if __name__=="__main__":
    path=sys.argv[1]; md="--md" in sys.argv
    conv=extract(path)
    if md:
        print(f"# Gesprächsverlauf — {os.path.basename(path)}\n")
        for role,ts,t in conv:
            who = "🧑 Nemanja" if role in ("user","human") else "🤖 Otto"
            head = f"### {who}" + (f"  ·  {ts[:19]}" if ts else "")
            print(head+"\n\n"+t+"\n")
    else:
        users=sum(1 for r,_,_ in conv if r in ("user","human"))
        asst=sum(1 for r,_,_ in conv if r=="assistant")
        chars=sum(len(t) for _,_,t in conv)
        print(f"{os.path.basename(path)}: {len(conv)} Nachrichten (user={users}, assistant={asst}), ~{chars//1000}k Zeichen")
