Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, json, time
|
| 2 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 3 |
+
from fastapi.responses import HTMLResponse
|
| 4 |
+
|
| 5 |
+
app = FastAPI(title="OpenClaw PoC")
|
| 6 |
+
|
| 7 |
+
API_KEY = os.environ.get("API_KEY")
|
| 8 |
+
DATA_DIR = os.environ.get("DATA_DIR", "/data")
|
| 9 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 10 |
+
LOG_PATH = os.path.join(DATA_DIR, "log.jsonl")
|
| 11 |
+
WH_PATH = os.path.join(DATA_DIR, "webhook.jsonl")
|
| 12 |
+
|
| 13 |
+
def require_key(req: Request):
|
| 14 |
+
if not API_KEY:
|
| 15 |
+
raise HTTPException(status_code=500, detail="API_KEY is not set")
|
| 16 |
+
if req.headers.get("authorization", "") != f"Bearer {API_KEY}":
|
| 17 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 18 |
+
|
| 19 |
+
def append_jsonl(path: str, obj: dict):
|
| 20 |
+
with open(path, "a", encoding="utf-8") as f:
|
| 21 |
+
f.write(json.dumps(obj, ensure_ascii=False) + "\n")
|
| 22 |
+
|
| 23 |
+
@app.get("/", response_class=HTMLResponse)
|
| 24 |
+
async def home(request: Request):
|
| 25 |
+
base = str(request.base_url).rstrip("/")
|
| 26 |
+
return f"""
|
| 27 |
+
<html><body style="font-family:sans-serif;max-width:760px;margin:40px auto;">
|
| 28 |
+
<h1>OpenClaw PoC 🧫</h1>
|
| 29 |
+
<p><b>Base URL:</b> {base}</p>
|
| 30 |
+
<p><b>API_KEY configured:</b> {bool(API_KEY)}</p>
|
| 31 |
+
<ul>
|
| 32 |
+
<li>GET <code>/health</code></li>
|
| 33 |
+
<li>POST <code>/api/agent</code> (Bearer API_KEY)</li>
|
| 34 |
+
<li>POST <code>/webhook</code> (Bearer API_KEY)</li>
|
| 35 |
+
</ul>
|
| 36 |
+
<p>Agent payload supports <code>{{"prompt":"..."}}</code> or <code>{{"input":"..."}}</code>.</p>
|
| 37 |
+
</body></html>
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
@app.get("/health")
|
| 41 |
+
async def health():
|
| 42 |
+
return {"ok": True, "ts": int(time.time())}
|
| 43 |
+
|
| 44 |
+
@app.post("/api/agent")
|
| 45 |
+
async def agent(req: Request):
|
| 46 |
+
require_key(req)
|
| 47 |
+
body = await req.json()
|
| 48 |
+
prompt = body.get("prompt") or body.get("input") or ""
|
| 49 |
+
reply = f"🧫 heard: {prompt}" if prompt else "missing prompt"
|
| 50 |
+
append_jsonl(LOG_PATH, {"t": int(time.time()), "prompt": prompt, "reply": reply})
|
| 51 |
+
return {"reply": reply, "mode": "echo"}
|
| 52 |
+
|
| 53 |
+
@app.post("/webhook")
|
| 54 |
+
async def webhook(req: Request):
|
| 55 |
+
require_key(req)
|
| 56 |
+
body = await req.json()
|
| 57 |
+
append_jsonl(WH_PATH, {"t": int(time.time()), "body": body})
|
| 58 |
+
return {"ok": True}
|