-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
33 lines (24 loc) · 928 Bytes
/
Copy pathhandler.py
File metadata and controls
33 lines (24 loc) · 928 Bytes
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
import json
import logging
from fastapi import FastAPI, Request
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
app = FastAPI(title="agent-shield-handler-demo")
@app.post("/handler")
async def handler(request: Request):
payload = await request.json()
logging.info("handler processing: %s", json.dumps(payload, ensure_ascii=False))
text = payload.get("primary_text")
phase = payload.get("phase")
direction = payload.get("direction")
should_modify = bool(text) and (
(phase == "http.request" and direction == "out")
or (phase == "ws.message.out" and direction == "out")
or (phase == "sse.event.in" and direction == "in")
)
if not should_modify:
return {"action": "allow", "reason": "demo_no_text"}
return {
"action": "modify",
"reason": "demo_append_hello",
"text": f"{text} hello",
}