Skip to content

Commit 97c9460

Browse files
committed
feat: add markdown negotiation and release metadata
1 parent 39b77d7 commit 97c9460

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

examples/safe-agent-api/app/main.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
from fastapi import FastAPI
1+
from fastapi import FastAPI, Request
22
from fastapi.responses import HTMLResponse, PlainTextResponse, Response
33

44
from app.discovery import (
55
AGENTS_MD,
6+
DISCOVERY_BODY_HTML,
67
HEAD_DISCOVERY_HTML,
78
LLMS_FULL_TXT,
89
LLMS_TXT,
910
PLAYGROUND_MD,
11+
RELEASE_VERSION,
1012
ROBOTS_TXT,
1113
SITEMAP_MD,
1214
SITEMAP_XML,
@@ -23,32 +25,49 @@
2325

2426
app = FastAPI(
2527
title="Safe Agent API",
26-
version="0.5.0",
28+
version=RELEASE_VERSION,
2729
description="Deterministic policy, approval and audit layer for agent tool execution demos.",
2830
)
2931

3032

31-
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
32-
def playground() -> HTMLResponse:
33+
MARKDOWN_HEADERS = {
34+
"Link": '</>; rel="canonical", </sitemap.md>; rel="sitemap"',
35+
"X-Robots-Tag": "index, follow, max-snippet:-1, max-image-preview:large",
36+
}
37+
38+
39+
@app.api_route("/", methods=["GET", "HEAD"], response_class=HTMLResponse, include_in_schema=False)
40+
def playground(request: Request) -> Response:
41+
accept = request.headers.get("accept", "").lower()
42+
if request.method == "GET" and "text/markdown" in accept:
43+
return PlainTextResponse(
44+
PLAYGROUND_MD,
45+
media_type="text/markdown",
46+
headers=MARKDOWN_HEADERS,
47+
)
48+
3349
html = PLAYGROUND_HTML.replace("</head>", f"{HEAD_DISCOVERY_HTML}</head>")
50+
html = html.replace('<p class="footer">', f'{DISCOVERY_BODY_HTML}<p class="footer">')
3451
return HTMLResponse(
3552
html,
3653
headers={
3754
"Cache-Control": "no-store",
3855
"Content-Security-Policy": (
3956
"default-src 'self'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; "
40-
"connect-src 'self'; img-src 'self' data:; frame-ancestors 'none'"
57+
"connect-src 'self'; img-src 'self' data: https://raw.githubusercontent.com; "
58+
"frame-ancestors 'none'"
4159
),
42-
"Link": '</llms.txt>; rel="describedby", </playground.md>; rel="alternate"; type="text/markdown"',
60+
"Link": '</llms.txt>; rel="describedby", </index.md>; rel="alternate"; type="text/markdown"',
4361
"Referrer-Policy": "no-referrer",
4462
"X-Content-Type-Options": "nosniff",
63+
"X-Robots-Tag": "index, follow, max-snippet:-1, max-image-preview:large",
4564
},
4665
)
4766

4867

4968
@app.get("/health")
5069
def health() -> dict[str, str]:
51-
return {"status": "ok", "service": "safe-agent-api", "version": "0.5.0"}
70+
return {"status": "ok", "service": "safe-agent-api", "version": RELEASE_VERSION}
5271

5372

5473
@app.get("/llms.txt", response_class=PlainTextResponse, include_in_schema=False)
@@ -81,12 +100,13 @@ def agents_md() -> PlainTextResponse:
81100
return PlainTextResponse(AGENTS_MD, media_type="text/markdown")
82101

83102

103+
@app.get("/index.md", response_class=PlainTextResponse, include_in_schema=False)
84104
@app.get("/playground.md", response_class=PlainTextResponse, include_in_schema=False)
85105
def playground_md() -> PlainTextResponse:
86106
return PlainTextResponse(
87107
PLAYGROUND_MD,
88108
media_type="text/markdown",
89-
headers={"Link": '</sitemap.md>; rel="sitemap"'},
109+
headers=MARKDOWN_HEADERS,
90110
)
91111

92112

0 commit comments

Comments
 (0)