aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / scripts / allen / allen_math.py
| 1 | """POST /allen/api/math: one expression to the Omega math engine, answered as glass pane events. |
| 2 | |
| 3 | The /aien/ page sends {"expression": "...", "title": "..."}; this module forwards it as the |
| 4 | math_compute tool to the math-only allen-science instance (127.0.0.1:18114, started with |
| 5 | --tools math_compute, scripts/allen/science/allen-science-math-fg). That instance refuses every |
| 6 | other science tool, so the public route reaches math and nothing else. It runs the pure-Omega |
| 7 | aien-math engine in its sandbox and answers with at most three v1 pane |
| 8 | events (pane.open, pane.update, pane.close) plus a one-line result. No model call, no network |
| 9 | beyond loopback, nothing stored or logged. The page renders the events with AllenPanes, which |
| 10 | checks every event again against the pane contract before showing it. |
| 11 | """ |
| 12 | import http.client |
| 13 | import json |
| 14 | import socket |
| 15 | |
| 16 | SCIENCE = ("127.0.0.1", 18114) |
| 17 | MAX_EXPRESSION = 4096 |
| 18 | MAX_TITLE = 200 |
| 19 | MAX_REPLY = 262144 |
| 20 | PANE_EVENTS = frozenset(("pane.open", "pane.update", "pane.close")) |
| 21 | PER_MINUTE = 12 |
| 22 | |
| 23 | |
| 24 | def init(app, buckets): |
| 25 | app.math_rate = buckets(PER_MINUTE, 60 / PER_MINUTE) |
| 26 | |
| 27 | |
| 28 | def _bad(text): |
| 29 | return not isinstance(text, str) or "\x00" in text |
| 30 | |
| 31 | |
| 32 | def _compute(expression, title): |
| 33 | body = json.dumps({"tool": "math_compute", "arguments": {"title": title, "expression": expression}}, |
| 34 | separators=(",", ":"), ensure_ascii=False).encode() |
| 35 | conn = http.client.HTTPConnection(*SCIENCE, timeout=20) |
| 36 | try: |
| 37 | conn.request("POST", "/compute", body, {"Content-Type": "application/json", "Connection": "close"}) |
| 38 | resp = conn.getresponse() |
| 39 | raw = resp.read(MAX_REPLY + 1) |
| 40 | finally: |
| 41 | conn.close() |
| 42 | if resp.status != 200 or len(raw) > MAX_REPLY: |
| 43 | return None |
| 44 | out = json.loads(raw) |
| 45 | if type(out) is not dict or set(out) != {"events", "result"} or type(out["events"]) is not list: |
| 46 | return None |
| 47 | if len(out["events"]) > 3 or not isinstance(out["result"], str) or len(out["result"]) > 8000: |
| 48 | return None |
| 49 | for item in out["events"]: |
| 50 | if type(item) is not dict or set(item) != {"event", "data"} or item["event"] not in PANE_EVENTS \ |
| 51 | or type(item["data"]) is not dict: |
| 52 | return None |
| 53 | return out |
| 54 | |
| 55 | |
| 56 | def post(h): |
| 57 | app = h.app |
| 58 | app.stats.add("math_requests") |
| 59 | req = h.read_json() |
| 60 | if req is None: |
| 61 | return |
| 62 | if not isinstance(req, dict) or set(req) - {"expression", "title"}: |
| 63 | return h.refuse(400, "bad_request", "Send {expression, title}.") |
| 64 | expression = req.get("expression") |
| 65 | title = req.get("title", "Math") |
| 66 | if _bad(expression) or not expression.strip() or _bad(title): |
| 67 | return h.refuse(400, "bad_request", "Write an expression, for example Integrate[x^2,{x,0,1}].") |
| 68 | if len(expression) > MAX_EXPRESSION or len(title) > MAX_TITLE: |
| 69 | return h.refuse(413, "too_long", "An expression holds at most %d characters." % MAX_EXPRESSION) |
| 70 | retry = app.math_rate.take(app.client_key(h)) |
| 71 | if retry: |
| 72 | return h.refuse(429, "rate", "That is %d expressions this minute. Try again in %d s." % (PER_MINUTE, retry), |
| 73 | retry) |
| 74 | try: |
| 75 | out = _compute(expression.strip(), title.strip() or "Math") |
| 76 | except (OSError, socket.timeout, ValueError, http.client.HTTPException): |
| 77 | out = None |
| 78 | if out is None: |
| 79 | app.stats.add("math_unavailable") |
| 80 | return h.refuse(503, "unavailable", "The math engine did not answer. Try again in a minute.", 60) |
| 81 | h.send_json(200, out) |