File size: 5,432 Bytes
726d519
 
d49e34a
 
726d519
 
 
 
 
 
 
 
 
 
 
 
 
d49e34a
 
726d519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d49e34a
 
 
 
726d519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d49e34a
 
726d519
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""Fake backend for testing web/ locally. Stdlib only, no model calls. Mirrors the contract of app/main.py.

Run: python web/dev/fake_api.py [turns] [--turnstile]  (http://localhost:8787, default 20 turns)
  --turnstile: /session and /contact need a turnstile_token (like a backend with TURNSTILE_SECRET set)

Test triggers inside a chat message:
  !409 / !429 / !500 -> that status   !401 -> forget the session (next call gets 401 session_expired)
  contact / reach / kontakt / erreichen -> show_form true
  <b>html</b> -> echoed back, so you can check it renders as plain text
In the contact form, a message containing !503 or !500 gives that status.
"""
import json
import secrets
import sys
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

TURNS = next((int(a) for a in sys.argv[1:] if a.isdigit()), 20)  # e.g. 6 to see the turns-left hint at once
TURNSTILE = "--turnstile" in sys.argv
sessions: dict[str, int] = {}  # token -> user turns used


class Handler(BaseHTTPRequestHandler):
    def _send(self, code, body):
        data = json.dumps(body).encode()
        self.send_response(code)
        self.send_header("Content-Type", "application/json")
        self.send_header("Access-Control-Allow-Origin", "*")
        self.send_header("Content-Length", str(len(data)))
        self.end_headers()
        self.wfile.write(data)

    def do_OPTIONS(self):
        self.send_response(204)
        self.send_header("Access-Control-Allow-Origin", "*")
        self.send_header("Access-Control-Allow-Methods", "GET, POST")
        self.send_header("Access-Control-Allow-Headers", "Authorization, Content-Type")
        self.end_headers()

    def do_GET(self):
        if self.path == "/health":
            return self._send(200, {"status": "ok", "prompt_version": "fake"})
        self._send(404, {"detail": "not found"})

    def do_POST(self):
        body = json.loads(self.rfile.read(int(self.headers.get("Content-Length", 0))) or b"{}")
        if self.path == "/session":
            if not set(body) <= {"turnstile_token"}:
                return self._send(422, {"detail": "extra fields"})
            if TURNSTILE and not body.get("turnstile_token"):
                return self._send(403, {"detail": "turnstile_failed"})
            token = secrets.token_urlsafe(16)
            sessions[token] = 0
            return self._send(200, {"token": token, "expires_in": 1800, "turns_left": TURNS})
        auth = self.headers.get("Authorization", "")
        if not auth.startswith("Bearer "):
            return self._send(401, {"detail": "invalid_session"})
        token = auth[7:]
        if token not in sessions:
            return self._send(401, {"detail": "session_expired"})
        if self.path == "/chat":
            return self.chat(token, body)
        if self.path == "/contact":
            return self.contact(body)
        self._send(404, {"detail": "not found"})

    def chat(self, token, body):
        if set(body) != {"message"}:
            return self._send(422, {"detail": "extra fields"})
        msg = str(body["message"]).strip()
        time.sleep(0.6)  # feel like a model call
        for code in (409, 429, 500):
            if f"!{code}" in msg:
                return self._send(code, {"detail": "test"})
        if len(msg) > 1000:
            return self._send(413, {"detail": "message_too_long"})
        low = msg.lower()
        de = any(w in low for w in ("ist", "er ", "welche", "wie", "kann", "kontakt", "ä", "ö", "ü"))
        lang = "de" if de else "en"
        if sessions[token] >= TURNS:
            return self._send(200, {"reply": "This chat has reached its limit.", "show_form": False, "lang": lang, "turns_left": 0})
        sessions[token] += 1
        form = any(w in low for w in ("contact", "reach", "kontakt", "erreichen"))
        if form:
            reply = ("Gern. Bitte nutzen Sie das Kontaktformular unten, um Christopher eine Nachricht zu senden." if de
                     else "Happy to help. Please use the contact form below to send Christopher a message.")
        elif "<" in msg:
            reply = f"Echo: {msg}"
        else:
            reply = ("Christopher arbeitet als Entwickler.\nDies ist eine Testantwort der Fake-API." if de
                     else "Christopher works as a developer.\nThis is a test reply from the fake API.")
        turns_left = TURNS - sessions[token]
        if "!401" in msg:
            sessions.pop(token)
        self._send(200, {"reply": reply, "show_form": form, "lang": lang, "turns_left": turns_left})

    def contact(self, body):
        time.sleep(0.6)
        if not set(body) <= {"email", "message", "turnstile_token"}:
            return self._send(422, {"detail": "extra fields"})
        if TURNSTILE and not body.get("turnstile_token"):
            return self._send(403, {"detail": "turnstile_failed"})
        for code in (500, 503):
            if f"!{code}" in str(body.get("message", "")):
                return self._send(code, {"detail": "test"})
        if "@" not in str(body.get("email", "")) or not body.get("message"):
            return self._send(422, {"detail": "invalid"})
        self._send(200, {"sent": True})

    def log_message(self, fmt, *args):  # request line only
        print(self.command, self.path, args[1] if len(args) > 1 else "")


if __name__ == "__main__":
    ThreadingHTTPServer(("127.0.0.1", 8787), Handler).serve_forever()