#!/usr/bin/env python3
"""
Lean Context Proxy — Run big LLMs fast on weak hardware.

Strips irrelevant context, injects only what's needed, on-demand.
Drops in as an OpenAI-compatible replacement for LM Studio.

Usage:
  python3 lean-context.py --port 1234 --target http://localhost:1235/v1
  python3 lean-context.py --build-index  # Rebuild skill index
  python3 lean-context.py --help

Architecture:
  Client (Hermes/Any) → LeanProxy (:1234) → LM Studio (:1235)

How it works:
  1. Strips the bloated system prompt to minimum
  2. Keeps a skill index (name, description, tags)
  3. On each user message, analyzes intent via keyword matching
  4. Injects ONLY the 1-2 most relevant skill descriptions
  5. Compresses conversation history to a summary
  6. Forwards minimal context to the big model
"""

import argparse
import json
import os
import re
import sys
import time
import urllib.request
import urllib.error
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
from pathlib import Path

# ============================
# CONFIG
# ============================
VERSION = "1.2.0"
DEFAULT_PORT = 1234
DEFAULT_TARGET = "http://localhost:1235/v1"
def _default_skill_dirs():
    """Hermes + Lean Context skill roots (any profile, not a hard-coded name)."""
    dirs = [
        os.path.expanduser("~/.hermes/skills/"),
        os.path.expanduser("~/.lean-context/skills/"),
    ]
    profiles = os.path.expanduser("~/.hermes/profiles")
    if os.path.isdir(profiles):
        try:
            for name in sorted(os.listdir(profiles)):
                candidate = os.path.join(profiles, name, "skills")
                if os.path.isdir(candidate):
                    dirs.append(candidate)
        except OSError:
            pass
    return dirs


SKILL_DIRS = _default_skill_dirs()
GENERIC_SKILL_DIRS = [
    os.path.expanduser("~/.lean-context/skills/"),
]
CONFIG_DIR = os.path.expanduser("~/.config/lean-context")
INDEX_PATH = os.path.join(CONFIG_DIR, "skill-index.json")
MEMORY_PATH = os.path.join(CONFIG_DIR, "memory.json")
HISTORY_PATH = os.path.join(CONFIG_DIR, "history.json")
CONFIG_PATH = os.path.join(CONFIG_DIR, "config.json")

# Minimal system prompt — NO skill lists, NO tool defs, NO memory dumps
MINIMAL_SYSTEM = (
    "You are a helpful AI assistant. Be concise and accurate. "
    "When the user asks you to do something specific (code, search, image gen, etc.), "
    "use the tools described in this prompt if they match the request. "
    "If the user asks 'can you do X?' and a tool exists, say 'Yes!' and use it. "
    "If no tool exists, say 'Let me try my best without specialized tools.'"
)

# ============================
# SKILL INDEX BUILDER
# ============================
def build_skill_index():
    """Scan skill directories and build a searchable JSON index."""
    all_skills = []
    
    for skill_dir in SKILL_DIRS:
        if not os.path.isdir(skill_dir):
            continue
        for root, dirs, files in os.walk(skill_dir):
            if "SKILL.md" not in files:
                continue
            skill_path = os.path.join(root, "SKILL.md")
            try:
                with open(skill_path, "r", encoding="utf-8", errors="replace") as f:
                    content = f.read(4096)  # Read first 4K
            except Exception:
                continue
            
            # Parse YAML frontmatter (simple parser)
            meta = {"name": "", "description": "", "tags": [], "category": ""}
            if content.startswith("---"):
                end = content.find("---", 3)
                if end != -1:
                    front = content[3:end]
                    for line in front.strip().split("\n"):
                        if ":" not in line:
                            continue
                        key, _, val = line.partition(":")
                        key = key.strip().lower()
                        val = val.strip().strip('"').strip("'")
                        
                        if key == "name":
                            meta["name"] = val
                        elif key == "description":
                            meta["description"] = val
                        elif key == "tags":
                            # Parse [tag1, tag2, ...] format
                            tags = re.findall(r'[\w-]+', val)
                            meta["tags"] = tags
                        elif key == "category":
                            meta["category"] = val
            
            if meta["name"]:
                # Derive category from path
                rel = os.path.relpath(root, skill_dir)
                parts = rel.split(os.sep)
                category = parts[0] if len(parts) > 1 else ""
                meta["category"] = meta["category"] or category
                
                all_skills.append({
                    "name": meta["name"],
                    "description": meta["description"][:200],
                    "tags": meta["tags"],
                    "category": meta["category"],
                    "path": root,
                })
    
    # Write index
    os.makedirs(CONFIG_DIR, exist_ok=True)
    with open(INDEX_PATH, "w") as f:
        json.dump({"skills": all_skills, "count": len(all_skills), "version": VERSION}, f, indent=2)
    
    return all_skills


def load_skill_index():
    """Load the skill index from disk, rebuild if missing."""
    if not os.path.exists(INDEX_PATH):
        return build_skill_index()
    try:
        with open(INDEX_PATH) as f:
            data = json.load(f)
        return data.get("skills", [])
    except (json.JSONDecodeError, KeyError):
        return build_skill_index()


STOPWORDS = {"a", "an", "the", "is", "are", "was", "were", "be", "been",
              "in", "on", "at", "to", "for", "of", "with", "by", "from",
              "and", "or", "but", "if", "so", "as", "do", "does", "did",
              "i", "me", "my", "we", "our", "you", "your", "he", "she",
              "it", "its", "they", "them", "can", "will", "would", "could",
              "should", "may", "might", "shall", "about", "up", "out",
              "just", "not", "no", "yes", "this", "that", "these", "those"}

def search_skills(query, skills, max_results=2):
    """Find skills relevant to a query using keyword matching."""
    if not query or not skills:
        return []
    
    query_lower = query.lower()
    # Filter out stopwords and short words
    query_words = {w for w in re.findall(r'[\w-]+', query_lower)
                   if w not in STOPWORDS and len(w) > 2}
    
    if not query_words:
        return []  # Only stopwords = no relevant skills needed
    
    # Small talk / generic queries: if no action-oriented words, skip skill injection
    action_words = {"generate", "create", "make", "write", "build", "code", "send",
                    "search", "find", "look", "download", "upload", "deploy",
                    "play", "trade", "buy", "sell", "convert", "edit", "analyze",
                    "translate", "summarize", "extract", "compress", "fix",
                    "debug", "test", "monitor", "schedule", "cron", "email",
                    "pay", "transfer", "invest", "render", "draw", "paint",
                    "install", "setup", "configure", "run", "execute",
                    "fetch", "grab", "pull", "need", "help", "troubleshoot"}
    if not any(w in action_words for w in query_words):
        return []  # No action = pure conversation, no skills needed
    
    scored = []
    for skill in skills:
        score = 0
        text = f"{skill['name']} {skill['description']} {' '.join(skill['tags'])} {skill['category']}".lower()
        
        # Word-boundary matching (whole word only)
        for word in query_words:
            if re.search(rf'\b{re.escape(word)}\b', text):
                score += 3  # Whole-word match in text
            if re.search(rf'\b{re.escape(word)}\b', skill['name'].lower()):
                score += 8  # Name match = very relevant
        
        # Multi-word phrase match
        if query_lower in text:
            score += 10
        
        # Intent detection patterns (only check both query AND text)
        intent_patterns = {
            "image|generate|picture|render|create image|draw|art": 8,
            "code|python|script|program|function|implement": 6,
            "search|web|find|look up|research": 7,
            "email|mail|send|inbox": 8,
            "git|github|push|commit|pull": 8,
            "trade|stock|invest|robinhood|market": 8,
            "music|song|audio|sound": 7,
            "translate|language": 7,
            "deploy|server|host|launch": 6,
            "test|debug|fix|error|bug": 5,
            "cron|schedule|timer|job": 7,
            "note|obsidian|write down|remember": 7,
            "money|finance|payment|invoice|stripe": 8,
            "download|video|youtube|media": 7,
            "analyze|data|stat|chart|graph": 6,
        }
        
        for pattern, weight in intent_patterns.items():
            if re.search(pattern, query_lower) and re.search(pattern, text):
                score += weight
        
        if score >= 3:  # Minimum threshold — ignore noise matches
            scored.append((score, skill))
    
    scored.sort(key=lambda x: -x[0])
    return [s[1] for s in scored[:max_results]]


# ============================
# MEMORY / PREFERENCES
# ============================
def load_memory():
    """Load saved user preferences and facts."""
    if not os.path.exists(MEMORY_PATH):
        return {"user_preferences": [], "facts": []}
    try:
        with open(MEMORY_PATH) as f:
            return json.load(f)
    except (json.JSONDecodeError, FileNotFoundError):
        return {"user_preferences": [], "facts": []}


def search_memory(query, memory, max_results=3):
    """Find relevant memory entries for a query."""
    if not query or not memory:
        return ""
    
    query_lower = query.lower()
    query_words = set(re.findall(r'[\w-]+', query_lower))
    
    relevant = []
    for entry in memory.get("user_preferences", []) + memory.get("facts", []):
        if isinstance(entry, str):
            text = entry.lower()
        elif isinstance(entry, dict):
            text = json.dumps(entry).lower()
        else:
            continue
        
        if any(word in text for word in query_words) or query_lower in text:
            relevant.append(str(entry)[:200])
    
    if relevant:
        return "User context: " + "; ".join(relevant[:max_results])
    return ""


# ============================
# HISTORY COMPRESSION
# ============================
def load_history():
    """Load compressed conversation history."""
    if not os.path.exists(HISTORY_PATH):
        return {"summary": "", "decisions": [], "files_changed": []}
    try:
        with open(HISTORY_PATH) as f:
            return json.load(f)
    except (json.JSONDecodeError, FileNotFoundError):
        return {"summary": "", "decisions": [], "files_changed": []}


def compress_messages(messages):
    """Extract key info from conversation history for compression."""
    # Extract user messages and assistant responses
    key_facts = []
    decisions = []
    
    user_msgs = [m for m in messages if m.get("role") == "user" and m.get("content")]
    asst_msgs = [m for m in messages if m.get("role") == "assistant" and m.get("content")]
    
    # Keep only the last 3 user messages and their responses
    recent = []
    for m in messages[-20:]:  # Look at last 20 messages
        if m.get("role") in ("user", "assistant") and m.get("content"):
            content = m["content"]
            if isinstance(content, list):
                # Handle multi-part messages (images, etc.)
                texts = [p.get("text", "") for p in content if isinstance(p, dict) and "text" in p]
                content = " ".join(texts)
            recent.append({"role": m["role"], "content": str(content)[:500]})
    
    return recent


# ============================
# PROXY SERVER
# ============================
class LeanContextHandler(BaseHTTPRequestHandler):
    """HTTP handler that proxies to LM Studio with lean context."""
    
    target_url = DEFAULT_TARGET
    skills = []
    verbose = False
    persona = ""
    use_persona = False
    no_skills = False
    
    def do_GET(self):
        if self.path == "/v1/models":
            self._proxy_get()
        elif self.path == "/health" or self.path == "/v1/health":
            self._json_response({"status": "ok", "version": VERSION, "mode": "lean-context"})
        elif self.path == "/v1/info":
            self._json_response({
                "version": VERSION,
                "target": self.target_url,
                "skills_indexed": len(self.skills),
                "mode": "Strips context, injects skills on-demand",
            })
        elif self.path == "/v1/rebuild-index":
            self.skills = build_skill_index()
            self._json_response({"status": "ok", "skills_indexed": len(self.skills)})
        else:
            self._proxy_get()
    
    def do_POST(self):
        if self.path in ("/v1/chat/completions", "/chat/completions"):
            self._handle_chat()
        elif self.path == "/v1/completions":
            self._proxy_post()
        else:
            self._proxy_post()
    
    def _handle_chat(self):
        """Main handler — strips context, injects relevant skills, proxies."""
        content_length = int(self.headers.get("Content-Length", 0))
        body = self.rfile.read(content_length) if content_length else b""
        
        try:
            req = json.loads(body)
        except json.JSONDecodeError:
            self._json_response({"error": "Invalid JSON"}, 400)
            return
        
        messages = req.get("messages", [])
        if not messages:
            self._proxy_post_raw(body)
            return
        
        # Separate system message from rest
        system_msgs = [m for m in messages if m.get("role") == "system"]
        other_msgs = [m for m in messages if m.get("role") != "system"]
        
        # Get the latest user message for intent analysis
        latest_user = ""
        for m in reversed(other_msgs):
            if m.get("role") == "user":
                content = m.get("content", "")
                if isinstance(content, list):
                    texts = [p.get("text", "") for p in content if isinstance(p, dict) and "text" in p]
                    content = " ".join(texts)
                latest_user = str(content)
                break
        
        # Build minimal context
        if self.use_persona and self.persona:
            context_parts = [self.persona]
            context_parts.append("\n[System: Inject skills only when user asks for specific actions.]")
        else:
            context_parts = [MINIMAL_SYSTEM]
        
        # 1. Search for relevant skills (skip if --no-skills)
        if not self.no_skills:
            relevant_skills = search_skills(latest_user, self.skills, max_results=2)
            for skill in relevant_skills:
                context_parts.append(
                    f"[Tool available: {skill['name']} — {skill['description'][:150]}]"
                )
        else:
            relevant_skills = []
        
        # 2. Search for relevant memory/preferences
        memory = load_memory()
        mem_text = search_memory(latest_user, memory)
        if mem_text:
            context_parts.append(mem_text)
        
        # 3. Compress conversation history
        compressed = compress_messages(other_msgs)
        
        # Build the lean message list
        lean_messages = [
            {"role": "system", "content": "\n".join(context_parts)}
        ]
        
        # Add compressed history
        if len(compressed) > 6:
            # If long history, add summary note and last 4 exchanges
            summary = load_history().get("summary", "")
            if summary:
                lean_messages.append({"role": "system", "content": f"[Session context: {summary[:300]}]"})
            # Add last 4 exchanges (8 messages = 4 user + 4 assistant)
            lean_messages.extend(compressed[-8:])
        else:
            lean_messages.extend(compressed)
        
        if self.verbose:
            print(f"\n{'='*50}")
            print(f"📝 User query: {latest_user[:100]}")
            print(f"🔧 Skills injected: {[s['name'] for s in relevant_skills]}")
            print(f"📏 Messages sent: {len(lean_messages)} (vs {len(messages)} original)")
            print(f"📊 Context tokens saved: ~{sum(len(str(m.get('content',''))) // 4 for m in system_msgs) - sum(len(str(m.get('content',''))) // 4 for m in lean_messages if m['role']=='system')}")
            print(f"{'='*50}\n")
        
        # Build the modified request
        req["messages"] = lean_messages
        
        # Remove response_format if system prompt no longer needs it
        # (lean system prompt can handle any format)
        
        # Proxy to target
        modified_body = json.dumps(req).encode()
        self._proxy_post_raw(modified_body)
    
    def _proxy_get(self):
        """Proxy a GET request to the target."""
        try:
            url = f"{self.target_url.rstrip('/')}{self.path}"
            req = urllib.request.Request(url, headers={
                "Accept": "application/json",
            })
            with urllib.request.urlopen(req, timeout=30) as resp:
                data = resp.read()
                self.send_response(resp.status)
                for key, val in resp.headers.items():
                    if key.lower() not in ("content-encoding", "transfer-encoding", "content-length", "connection"):
                        self.send_header(key, val)
                self.send_header("Access-Control-Allow-Origin", "*")
                self.end_headers()
                self.wfile.write(data)
        except Exception as e:
            self._json_response({"error": str(e)}, 502)
    
    def _proxy_post(self):
        content_length = int(self.headers.get("Content-Length", 0))
        body = self.rfile.read(content_length) if content_length else b"{}"
        self._proxy_post_raw(body)
    
    def _proxy_post_raw(self, body):
        """Forward a raw POST body to the target using subprocess curl for speed."""
        import subprocess as sp
        
        url = f"{self.target_url.rstrip('/')}{self.path}"
        
        try:
            # Use curl subprocess for reliable streaming and speed
            proc = sp.run(
                ["curl", "-s", "--max-time", "300", "-X", "POST", url,
                 "-H", "Content-Type: application/json",
                 "-d", body.decode() if isinstance(body, bytes) else body],
                capture_output=True, timeout=310
            )
            
            if proc.returncode != 0 and proc.returncode != 28:  # 28 = timeout
                raise Exception(f"curl exited {proc.returncode}: {proc.stderr.decode()[:200]}")
            
            raw = proc.stdout
            if not raw:
                raise Exception("Empty response from target")
            
            # Parse and potentially fix reasoning_content → content
            try:
                resp = json.loads(raw)
                if "choices" in resp:
                    for choice in resp["choices"]:
                        msg = choice.get("message", {})
                        # If content is empty but reasoning_content exists, merge
                        if not msg.get("content", "").strip() and msg.get("reasoning_content", ""):
                            rc = msg["reasoning_content"]
                            msg["content"] = rc
                        # Strip reasoning_content from response — wastes tokens in history
                        if "reasoning_content" in msg:
                            del msg["reasoning_content"]
                raw = json.dumps(resp).encode()
            except json.JSONDecodeError:
                pass  # Pass through raw if not JSON
            
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.send_header("Access-Control-Allow-Origin", "*")
            self.end_headers()
            self.wfile_write_safe(raw)
        except sp.TimeoutExpired:
            self._json_response({"error": "Upstream timeout"}, 504)
        except Exception as e:
            self._json_response({"error": f"Proxy error: {str(e)}"}, 502)
    
    def send_response(self, code, message=None):
        try:
            super().send_response(code, message)
        except (BrokenPipeError, ConnectionResetError):
            pass
    
    def send_header(self, keyword, value):
        try:
            super().send_header(keyword, value)
        except (BrokenPipeError, ConnectionResetError):
            pass
    
    def end_headers(self):
        try:
            super().end_headers()
        except (BrokenPipeError, ConnectionResetError):
            pass
    
    def wfile_write_safe(self, data):
        try:
            self.wfile.write(data)
        except (BrokenPipeError, ConnectionResetError):
            pass
    
    def _json_response(self, data, status=200):
        self.send_response(status)
        self.send_header("Content-Type", "application/json")
        self.send_header("Access-Control-Allow-Origin", "*")
        self.end_headers()
        raw = json.dumps(data).encode()
        if hasattr(self, 'wfile'):
            self.wfile_write_safe(raw)
    
    def log_message(self, format, *args):
        if self.verbose:
            super().log_message(format, *args)


def create_default_config():
    """Generate a default config file."""
    config = {
        "version": VERSION,
        "port": 1237,
        "target": "http://localhost:1234",
        "verbose": False,
        "no_skills": False,
        "persona": "persona.txt",
        "persona_enabled": False,
        "skill_dirs": [
            "~/.lean-context/skills/",
        ],
    }
    os.makedirs(CONFIG_DIR, exist_ok=True)
    config_path = os.path.join(CONFIG_DIR, "config.json")
    with open(config_path, "w") as f:
        json.dump(config, f, indent=2)
    print(f"✅ Default config created: {config_path}")
    print(f"   Edit it to customize proxy settings.")
    return config_path


def load_config(config_path):
    """Load proxy config from JSON file."""
    with open(config_path) as f:
        return json.load(f)


def run_proxy(port, target, verbose=False, persona_path=None, no_skills=False, skill_dirs=None, config_path=None):
    """Start the proxy server."""
    LeanContextHandler.target_url = target
    LeanContextHandler.verbose = verbose
    LeanContextHandler.no_skills = no_skills
    
    # Load persona if provided
    if persona_path and os.path.exists(persona_path):
        with open(persona_path, "r", encoding="utf-8") as f:
            persona = f.read().strip()
        LeanContextHandler.persona = persona
        LeanContextHandler.use_persona = True
        print(f"💋 Loaded persona: {os.path.basename(persona_path)} ({len(persona)} chars)")
    else:
        LeanContextHandler.persona = ""
        LeanContextHandler.use_persona = False
    
    # Build/load skill index (skip if --no-skills)
    skills = []
    if not no_skills:
        dirs = skill_dirs or SKILL_DIRS
        print(f"🔍 Scanning skill directories...")
        for d in dirs:
            print(f"   - {d}")
        skills = build_skill_index()
        LeanContextHandler.skills = skills
        print(f"   ✅ Indexed {len(skills)} skills from {len(dirs)} directories")
    else:
        LeanContextHandler.skills = []
        print(f"   ⚡ No-skills mode: context stripping only (no skill injection)")
    
    # Start server
    server = ThreadingHTTPServer(("0.0.0.0", port), LeanContextHandler)
    print(f"\n🚀 Lean Context Proxy v{VERSION}")
    print(f"   Listening:     http://localhost:{port}")
    print(f"   Forwarding to: {target}")
    if not no_skills:
        print(f"   Skills indexed: {len(skills)}")
    if LeanContextHandler.use_persona:
        print(f"   Persona:       Loaded ✅")
    base_url = f"http://localhost:{port}"
    print(f"   Config: {config_path or 'none (CLI args)'}")
    print(f"\n   Point your LLM client to:")
    print(f"   -> {base_url}/v1/chat/completions")
    verbose_str = "ON" if verbose else "OFF"
    print(f"   Verbose: {verbose_str} (use --verbose for debug output)\n")

    
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        print("\n👋 Shutting down...")
        server.server_close()


# ============================
# CLI
# ============================
def main():
    parser = argparse.ArgumentParser(
        description="Lean Context Proxy — Run big LLMs fast on weak hardware",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""\
Examples:
  # Run proxy forwarding to LM Studio (context stripping + skill injection)
  python3 lean-context.py --port 1237 --target http://localhost:1234

  # No-skills mode: context stripping only (no Hermes/HyDE integration needed)
  python3 lean-context.py --port 1237 --target http://localhost:1234 --no-skills

  # With custom persona and config file
  python3 lean-context.py --config ~/.config/lean-context/config.json

  # Verbose mode (see what's being stripped/injected)
  python3 lean-context.py --port 1237 --target http://localhost:1234 --verbose

  # Rebuild skill index
  python3 lean-context.py --build-index

  # Generate default config
  python3 lean-context.py --generate-config
        """
    )
    parser.add_argument("--port", type=int, default=DEFAULT_PORT, help=f"Proxy port (default: {DEFAULT_PORT})")
    parser.add_argument("--target", default=DEFAULT_TARGET, help=f"Upstream LLM API URL (default: {DEFAULT_TARGET})")
    parser.add_argument("--verbose", "-v", action="store_true", help="Show debug output")
    parser.add_argument("--persona", help="Path to persona file (always injected as system prompt)")
    parser.add_argument("--no-skills", action="store_true", help="Disable skill injection (context stripping only)")
    parser.add_argument("--skills-dir", action="append", dest="skill_dirs", help="Add skill directory (can be used multiple times)")
    parser.add_argument("--config", help="Path to JSON config file (overrides CLI defaults)")
    parser.add_argument("--build-index", action="store_true", help="Rebuild skill index and exit")
    parser.add_argument("--generate-config", action="store_true", help="Create default config file and exit")
    
    args = parser.parse_args()
    
    # Load config file if provided
    config_path = None
    if args.config:
        config_path = args.config
        try:
            cfg = load_config(args.config)
        except (FileNotFoundError, json.JSONDecodeError) as e:
            print(f"❌ Error loading config: {e}", file=sys.stderr)
            sys.exit(1)
        port = cfg.get("port", args.port)
        target = cfg.get("target", args.target)
        verbose = cfg.get("verbose", args.verbose)
        persona_path = cfg.get("persona", args.persona)
        no_skills = cfg.get("no_skills", args.no_skills)
        skill_dirs = cfg.get("skill_dirs", args.skill_dirs)
        if persona_path and not os.path.isabs(persona_path):
            persona_path = os.path.join(CONFIG_DIR, persona_path)
    else:
        port = args.port
        target = args.target
        verbose = args.verbose
        persona_path = args.persona
        no_skills = args.no_skills
        skill_dirs = args.skill_dirs
    
    if args.generate_config:
        path = create_default_config()
        print(f"   Run with: python3 lean-context.py --config {path}")
        return
    
    if args.build_index:
        skills = build_skill_index()
        print(f"✅ Built index: {len(skills)} skills indexed")
        print(f"   Saved to: {INDEX_PATH}")
        return
    
    run_proxy(port, target, verbose, persona_path, no_skills, skill_dirs, config_path)


if __name__ == "__main__":
    main()
