File size: 2,446 Bytes
732e77c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# /anon_bot/rules.py
"""
Lightweight rule set for an anonymous chatbot.
No external providers required. Pure-Python, deterministic.
"""
"""Compatibility shim: expose `route` from rules_new for legacy imports."""

from __future__ import annotations
from dataclasses import dataclass
from typing import Dict, List, Tuple
from .rules_new import route  # noqa: F401

__all__ = ["route"]

# ---- Types ----
History = List[Tuple[str, str]]  # e.g., [("user","hi"), ("bot","hello!")]

@dataclass(frozen=True)
class Reply:
    text: str
    meta: Dict[str, str] | None = None


def normalize(s: str) -> str:
    return " ".join((s or "").strip().split()).lower()


def capabilities() -> List[str]:
    return [
        "help",
        "reverse <text>",
        "echo <text>",
        "small talk (hi/hello/hey)",
    ]


def intent_of(text: str) -> str:
    t = normalize(text)
    if not t:
        return "empty"
    if t in {"help", "/help", "capabilities"}:
        return "help"
    if t.startswith("reverse "):
        return "reverse"
    if t.startswith("echo "):
        return "echo"
    if t in {"hi", "hello", "hey"}:
        return "greet"
    return "chat"


def handle_help() -> Reply:
    lines = ["I can:"]
    for c in capabilities():
        lines.append(f"- {c}")
    return Reply("\n".join(lines))


def handle_reverse(t: str) -> Reply:
    payload = t.split(" ", 1)[1] if " " in t else ""
    return Reply(payload[::-1] if payload else "(nothing to reverse)")


def handle_echo(t: str) -> Reply:
    payload = t.split(" ", 1)[1] if " " in t else ""
    return Reply(payload or "(nothing to echo)")


def handle_greet() -> Reply:
    return Reply("Hello! 👋  Type 'help' to see what I can do.")


def handle_chat(t: str, history: History) -> Reply:
    # Very simple “ELIZA-ish” fallback.
    if "help" in t:
        return handle_help()
    if "you" in t and "who" in t:
        return Reply("I'm a tiny anonymous chatbot kernel.")
    return Reply("Noted. (anonymous mode)  Type 'help' for commands.")


def reply_for(text: str, history: History) -> Reply:
    it = intent_of(text)
    if it == "empty":
        return Reply("Please type something. Try 'help'.")
    if it == "help":
        return handle_help()
    if it == "reverse":
        return handle_reverse(text)
    if it == "echo":
        return handle_echo(text)
    if it == "greet":
        return handle_greet()
    return handle_chat(text.lower(), history)