"""06 TEMPLATE ROUTER. The prompts you keep retyping are templates waiting for a name. Give each template aliases, keyword signatures, and {{slots}}; route a new terse ask through the cheapest tier first: exact alias -> keyword signature -> (optionally) an LLM for the long tail. Mined from Ray's life repo, where 18 templates cover 56% of real asks by keyword alone. ES: Los prompts que repites son plantillas esperando nombre. Enrutador de 3 niveles. Run: python 06_router.py "fix the login page, it 500s on submit" python 06_router.py "reverse https://app.example.com/dashboard" python 06_router.py deck (alias hit) No model needed for tiers 1-2. Pure stdlib. """ import re, sys PATTERNS = { "fix-surface": { "aliases": ["fix", "debug", "broken"], "signature": ["fix", "broken", "500", "error", "crash", "bug"], "slots": ["symptom"], "template": ( "Something shipped is misbehaving: {{symptom}}. Reproduce it first, state the " "root cause in one sentence before changing code, then make the smallest fix " "that passes. Verify by driving the real surface, not just the tests." ), }, "reverse-api": { "aliases": ["reverse", "scrape"], "signature": ["reverse", "scrape", "automate", "logged-in", "https://"], "slots": ["target_url"], "template": ( "Reverse {{target_url}}. I will help with login. Capture the real endpoints from " "the network tab, build a typed client, and smoke-test one low-stakes call before " "any bulk operation." ), }, "brand-deck": { "aliases": ["deck", "pitch", "brand"], "signature": ["deck", "pitch", "slides", "presentation", "brand"], "slots": ["subject"], "template": ( "Build a deck about {{subject}}. Derive the aesthetic from the subject, not a " "menu. Mobile first. One deliberate rule-break, one signature feature, and run " "every slide through an overflow check before calling it done." ), }, } def route(ask: str): words = ask.lower().split() # Tier 1 - exact alias (the ask IS the pattern name) for name, p in PATTERNS.items(): if ask.lower().strip() in p["aliases"]: return name, "alias" # Tier 2 - keyword signature (cheap, transparent, measurable) best, best_hits = None, 0 for name, p in PATTERNS.items(): hits = sum(1 for kw in p["signature"] if kw in ask.lower()) if hits > best_hits: best, best_hits = name, hits if best_hits >= 1: return best, f"keyword ({best_hits} hits)" # Tier 3 - in production this falls through to a small LLM given the pattern catalog. return None, "no match (LLM tier in production)" def expand(name: str, ask: str) -> str: p = PATTERNS[name] body = p["template"] url = re.search(r"https?://\S+", ask) fills = {"target_url": url.group(0) if url else "[?]", "surface": ask, "symptom": ask, "subject": ask} for slot in p["slots"]: body = body.replace("{{" + slot + "}}", fills.get(slot, "[?]")) return body def main(): ask = " ".join(sys.argv[1:]) or "reverse https://app.example.com/dashboard" name, tier = route(ask) print(f"ask: {ask}\npattern: {name or '(none)'} via {tier}") if name: print(f"\n== EXPANDED PROMPT ==\n{expand(name, ask)}") print("\nMeasure your router honestly: some patterns route 36/36, some 0/8. Knowing which is the point.") if __name__ == "__main__": main()