-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
187 lines (151 loc) · 6.09 KB
/
Copy pathmain.py
File metadata and controls
187 lines (151 loc) · 6.09 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, PlainTextResponse, Response
from app.discovery import (
AGENTS_MD,
DISCOVERY_BODY_HTML,
GLOSSARY_HTML,
GLOSSARY_MD,
HEAD_DISCOVERY_HTML,
LLMS_FULL_TXT,
LLMS_TXT,
PLAYGROUND_MD,
RELEASE_VERSION,
ROBOTS_TXT,
SITEMAP_MD,
SITEMAP_XML,
)
from app.models import (
AuditEvent,
RunDemoRequest,
RunDemoResponse,
ToolCheckRequest,
ToolDecision,
)
from app.playground import PLAYGROUND_HTML
from app.policy import evaluate_tool
app = FastAPI(
title="Safe Agent API",
version=RELEASE_VERSION,
description="Deterministic policy, approval and audit layer for agent tool execution demos.",
)
MARKDOWN_HEADERS = {
"Link": '</>; rel="canonical", </sitemap.md>; rel="sitemap"',
"X-Robots-Tag": "index, follow, max-snippet:-1, max-image-preview:large",
}
@app.api_route("/", methods=["GET", "HEAD"], response_class=HTMLResponse, include_in_schema=False)
def playground(request: Request) -> Response:
accept = request.headers.get("accept", "").lower()
if request.method == "GET" and "text/markdown" in accept:
return PlainTextResponse(
PLAYGROUND_MD,
media_type="text/markdown",
headers=MARKDOWN_HEADERS,
)
html = PLAYGROUND_HTML.replace("</head>", f"{HEAD_DISCOVERY_HTML}</head>")
html = html.replace('<p class="footer">', f'{DISCOVERY_BODY_HTML}<p class="footer">')
return HTMLResponse(
html,
headers={
"Cache-Control": "no-store",
"Content-Security-Policy": (
"default-src 'self'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; "
"connect-src 'self'; img-src 'self' data: https://raw.githubusercontent.com; "
"frame-ancestors 'none'"
),
"Link": '</llms.txt>; rel="describedby", </index.md>; rel="alternate"; type="text/markdown"',
"Referrer-Policy": "no-referrer",
"X-Content-Type-Options": "nosniff",
"X-Robots-Tag": "index, follow, max-snippet:-1, max-image-preview:large",
},
)
@app.get("/glossary", response_class=HTMLResponse, include_in_schema=False)
def glossary() -> HTMLResponse:
return HTMLResponse(
GLOSSARY_HTML,
headers={
"Cache-Control": "public, max-age=300",
"Content-Security-Policy": "default-src 'self'; style-src 'unsafe-inline'; frame-ancestors 'none'",
"Link": '</llms.txt>; rel="describedby", </glossary.md>; rel="alternate"; type="text/markdown"',
"Referrer-Policy": "no-referrer",
"X-Content-Type-Options": "nosniff",
"X-Robots-Tag": "index, follow, max-snippet:-1, max-image-preview:large",
},
)
@app.get("/glossary.md", response_class=PlainTextResponse, include_in_schema=False)
def glossary_md() -> PlainTextResponse:
return PlainTextResponse(
GLOSSARY_MD,
media_type="text/markdown",
headers={
"Link": '</glossary>; rel="canonical", </sitemap.md>; rel="sitemap"',
"X-Robots-Tag": "index, follow, max-snippet:-1, max-image-preview:large",
},
)
@app.get("/health")
def health() -> dict[str, str]:
return {"status": "ok", "service": "safe-agent-api", "version": RELEASE_VERSION}
@app.get("/llms.txt", response_class=PlainTextResponse, include_in_schema=False)
def llms_txt() -> PlainTextResponse:
return PlainTextResponse(LLMS_TXT, media_type="text/plain")
@app.get("/llms-full.txt", response_class=PlainTextResponse, include_in_schema=False)
def llms_full_txt() -> PlainTextResponse:
return PlainTextResponse(LLMS_FULL_TXT, media_type="text/plain")
@app.get("/robots.txt", response_class=PlainTextResponse, include_in_schema=False)
def robots_txt() -> PlainTextResponse:
return PlainTextResponse(ROBOTS_TXT, media_type="text/plain")
@app.get("/sitemap.xml", include_in_schema=False)
def sitemap_xml() -> Response:
return Response(SITEMAP_XML, media_type="application/xml")
@app.get("/sitemap.md", response_class=PlainTextResponse, include_in_schema=False)
def sitemap_md() -> PlainTextResponse:
return PlainTextResponse(SITEMAP_MD, media_type="text/markdown")
@app.get("/AGENTS.md", response_class=PlainTextResponse, include_in_schema=False)
def agents_md() -> PlainTextResponse:
return PlainTextResponse(AGENTS_MD, media_type="text/markdown")
@app.get("/index.md", response_class=PlainTextResponse, include_in_schema=False)
@app.get("/playground.md", response_class=PlainTextResponse, include_in_schema=False)
def playground_md() -> PlainTextResponse:
return PlainTextResponse(
PLAYGROUND_MD,
media_type="text/markdown",
headers=MARKDOWN_HEADERS,
)
@app.post("/v1/tool-check", response_model=ToolDecision)
def tool_check(request: ToolCheckRequest) -> ToolDecision:
return evaluate_tool(request)
def build_audit_event(
request: RunDemoRequest,
decision: ToolDecision,
executed: bool,
) -> AuditEvent:
return AuditEvent(
correlation_id=request.request_id,
actor_tenant_id=request.actor_tenant_id,
resource_tenant_id=request.resource_tenant_id,
tool=request.tool,
allowed=decision.allowed,
executed=executed,
reason=decision.reason,
human_approved=request.human_approved,
)
@app.post("/v1/run-demo", response_model=RunDemoResponse)
def run_demo(request: RunDemoRequest) -> RunDemoResponse:
decision = evaluate_tool(request)
if not decision.allowed:
return RunDemoResponse(
request_id=request.request_id,
decision=decision,
executed=False,
audit_event=build_audit_event(request, decision, executed=False),
)
simulated_result = {
"read_record": "demo_record_returned",
"send_notification": "demo_notification_sent",
}.get(request.tool, "no_action")
return RunDemoResponse(
request_id=request.request_id,
decision=decision,
executed=True,
result=simulated_result,
audit_event=build_audit_event(request, decision, executed=True),
)