forked from themanojdesai/python-a2a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.py
More file actions
223 lines (188 loc) · 7.69 KB
/
Copy pathhttp.py
File metadata and controls
223 lines (188 loc) · 7.69 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
HTTP server implementation for the A2A protocol.
"""
import json
import traceback
from typing import Type, Optional, Dict, Any, Callable, Union
try:
from flask import Flask, request, jsonify, Response, render_template_string, make_response
except ImportError:
Flask = None
from ..models.message import Message, MessageRole
from ..models.conversation import Conversation
from ..models.content import TextContent, ErrorContent
from .base import BaseA2AServer
from ..exceptions import A2AImportError, A2ARequestError
from .ui_templates import AGENT_INDEX_HTML, JSON_HTML_TEMPLATE
def create_flask_app(agent: BaseA2AServer) -> Flask:
"""
Create a Flask application that serves an A2A agent
Args:
agent: The A2A agent server
Returns:
A Flask application
Raises:
A2AImportError: If Flask is not installed
"""
if Flask is None:
raise A2AImportError(
"Flask is not installed. "
"Install it with 'pip install flask'"
)
app = Flask(__name__)
# Allow CORS for all routes
@app.after_request
def add_cors_headers(response):
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
return response
# Handle OPTIONS requests for CORS preflight
@app.route('/', methods=['OPTIONS'])
@app.route('/<path:path>', methods=['OPTIONS'])
def options_handler(path=None):
return '', 200
# Define a function to render beautiful HTML UI
def get_agent_data():
"""Get basic agent data for rendering"""
if hasattr(agent, 'agent_card'):
return agent.agent_card.to_dict()
else:
# Fallback for agents without agent_card
return {
"name": "A2A Agent",
"description": "Agent details not available",
"version": "1.0.0",
"skills": []
}
# IMPORTANT: Register our enhanced routes FIRST to ensure they take precedence
# Enhanced routes for beautiful UI
@app.route("/a2a", methods=["GET"])
def enhanced_a2a_index():
"""A2A index with beautiful UI"""
# Check if this is a browser request by looking at headers
user_agent = request.headers.get('User-Agent', '')
accept_header = request.headers.get('Accept', '')
# Force JSON if explicitly requested
format_param = request.args.get('format', '')
# Return JSON if explicitly requested or doesn't look like a browser
if format_param == 'json' or (
'application/json' in accept_header and
not any(browser in user_agent.lower() for browser in ['mozilla', 'chrome', 'safari', 'edge'])
):
return jsonify({
"name": agent.agent_card.name if hasattr(agent, 'agent_card') else "A2A Agent",
"description": agent.agent_card.description if hasattr(agent, 'agent_card') else "",
"agent_card_url": "/a2a/agent.json",
"protocol": "a2a"
})
# Otherwise serve HTML by default
response = make_response(render_template_string(
AGENT_INDEX_HTML,
agent=agent,
request=request
))
response.headers['Content-Type'] = 'text/html; charset=utf-8'
return response
@app.route("/", methods=["GET"])
def enhanced_root_index():
"""Root endpoint with beautiful UI"""
return enhanced_a2a_index()
@app.route("/agent", methods=["GET"])
def enhanced_agent_index():
"""Agent endpoint with beautiful UI"""
return enhanced_a2a_index()
@app.route("/a2a/agent.json", methods=["GET"])
def enhanced_a2a_agent_json():
"""Agent card JSON with beautiful UI"""
# Get agent data
agent_data = get_agent_data()
# Check request format preferences
user_agent = request.headers.get('User-Agent', '')
accept_header = request.headers.get('Accept', '')
format_param = request.args.get('format', '')
# Return JSON if explicitly requested or doesn't look like a browser
if format_param == 'json' or (
'application/json' in accept_header and
not any(browser in user_agent.lower() for browser in ['mozilla', 'chrome', 'safari', 'edge'])
):
return jsonify(agent_data)
# Otherwise serve HTML with pretty JSON visualization
formatted_json = json.dumps(agent_data, indent=2)
response = make_response(render_template_string(
JSON_HTML_TEMPLATE,
title=agent_data.get('name', 'A2A Agent'),
description="Agent Card JSON Data",
json_data=formatted_json
))
response.headers['Content-Type'] = 'text/html; charset=utf-8'
return response
@app.route("/agent.json", methods=["GET"])
def enhanced_root_agent_json():
"""Root agent.json endpoint"""
return enhanced_a2a_agent_json()
# Only AFTER registering our enhanced routes, set up the agent's routes
if hasattr(agent, 'setup_routes'):
agent.setup_routes(app)
# Legacy routes for backward compatibility
@app.route("/a2a", methods=["POST"])
def handle_a2a_request() -> Union[Response, tuple]:
"""Handle A2A protocol requests"""
try:
data = request.json
# Check if this is a single message or a conversation
if "messages" in data:
# This is a conversation
conversation = Conversation.from_dict(data)
response = agent.handle_conversation(conversation)
return jsonify(response.to_dict())
else:
# This is a single message
message = Message.from_dict(data)
response = agent.handle_message(message)
return jsonify(response.to_dict())
except Exception as e:
# Return an error response for any exceptions
error_dict = {
"content": {
"type": "error",
"message": f"Error processing request: {str(e)}"
},
"role": "system"
}
return jsonify(error_dict), 500
@app.route("/a2a/metadata", methods=["GET"])
def get_agent_metadata() -> Response:
"""Return metadata about the agent"""
return jsonify(agent.get_metadata())
@app.route("/a2a/health", methods=["GET"])
def health_check() -> Response:
"""Health check endpoint"""
return jsonify({"status": "ok"})
# If we reach here and no routes matched, set up a catch-all route
@app.route('/<path:path>')
def catch_all(path):
# Only for GET requests that didn't match other routes
if request.method == 'GET':
# Redirect to the A2A index
return enhanced_a2a_index()
return app
def run_server(
agent: BaseA2AServer,
host: str = "0.0.0.0",
port: int = 5000,
debug: bool = False
) -> None:
"""
Run an A2A agent as a Flask server
Args:
agent: The A2A agent server
host: Host to bind to (default: "0.0.0.0")
port: Port to listen on (default: 5000)
debug: Enable debug mode (default: False)
Raises:
A2AImportError: If Flask is not installed
"""
app = create_flask_app(agent)
print(f"Starting A2A server on http://{host}:{port}/a2a")
app.run(host=host, port=port, debug=debug)