Sheyda Kiani Mehr commited on
Commit
943646d
·
1 Parent(s): 026a6f0

fix on GET

Browse files
Files changed (1) hide show
  1. main.py +36 -16
main.py CHANGED
@@ -6,31 +6,33 @@ from a2a.server.apps import A2AStarletteApplication
6
  from a2a.server.request_handlers import DefaultRequestHandler
7
  from a2a.server.tasks import InMemoryTaskStore
8
 
9
- # use the typed models from the SDK
10
  from a2a.types import AgentCard, AgentCapabilities, AgentSkill
11
 
12
- # Minimal executor
 
13
  class HelloWorldAgentExecutor:
14
  async def call(self, message: str, **kwargs):
 
15
  return {"text": f"Hello from A2A on HF Spaces! You said: {message}"}
16
 
 
17
  def build_app():
18
- # Define at least one skill (IDs are free-form but should be stable)
19
  hello_skill = AgentSkill(
20
  id="hello",
21
  name="Say Hello",
22
  description="Return a friendly greeting",
23
  tags=["hello", "demo"],
24
- examples=["hi", "hello there"],
25
  )
26
 
27
- # HF Spaces gives you a PORT; URL here is informational for clients
28
- public_url = os.getenv("SPACE_HOST") # e.g. "username-space.hf.space"
29
  if public_url:
30
  public_url = f"https://{public_url}/"
31
  else:
32
- # local/dev fallback
33
- public_url = f"http://0.0.0.0:{os.getenv('PORT', '7860')}/"
34
 
35
  agent_card = AgentCard(
36
  name="HF A2A Agent",
@@ -38,24 +40,42 @@ def build_app():
38
  version="1.0.0",
39
  url=public_url,
40
  protocol_version="0.3.0",
41
- default_input_modes=["text"],
42
- default_output_modes=["text"],
43
  capabilities=AgentCapabilities(streaming=False),
44
  skills=[hello_skill],
45
- # keep it simple: no authenticated extended card for now
46
  supports_authenticated_extended_card=False,
47
  )
48
 
49
- app_builder = A2AStarletteApplication(
50
  agent_card=agent_card,
51
  http_handler=DefaultRequestHandler(
52
  agent_executor=HelloWorldAgentExecutor(),
53
  task_store=InMemoryTaskStore(),
54
  ),
55
- # extended_agent_card=... # only if you actually support it
56
- )
57
- return app_builder.build()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  if __name__ == "__main__":
60
- port = int(os.getenv("PORT", "7860"))
61
  uvicorn.run(build_app(), host="0.0.0.0", port=port)
 
6
  from a2a.server.request_handlers import DefaultRequestHandler
7
  from a2a.server.tasks import InMemoryTaskStore
8
 
9
+ # Typed models expected by the server
10
  from a2a.types import AgentCard, AgentCapabilities, AgentSkill
11
 
12
+
13
+ # ---- Minimal agent executor --------------------------------------------------
14
  class HelloWorldAgentExecutor:
15
  async def call(self, message: str, **kwargs):
16
+ # Return whatever your A2A client expects (text is fine for a demo)
17
  return {"text": f"Hello from A2A on HF Spaces! You said: {message}"}
18
 
19
+
20
  def build_app():
21
+ # At least one skill is recommended
22
  hello_skill = AgentSkill(
23
  id="hello",
24
  name="Say Hello",
25
  description="Return a friendly greeting",
26
  tags=["hello", "demo"],
27
+ examples=["hi", "hello there", "yo"],
28
  )
29
 
30
+ # Optional public URL (HF sets SPACE_HOST, but this is just metadata)
31
+ public_url = os.getenv("SPACE_HOST")
32
  if public_url:
33
  public_url = f"https://{public_url}/"
34
  else:
35
+ public_url = ""
 
36
 
37
  agent_card = AgentCard(
38
  name="HF A2A Agent",
 
40
  version="1.0.0",
41
  url=public_url,
42
  protocol_version="0.3.0",
43
+ default_input_modes=["text/plain", "application/json"],
44
+ default_output_modes=["text/plain", "application/json"],
45
  capabilities=AgentCapabilities(streaming=False),
46
  skills=[hello_skill],
47
+ # Keep it simple; no extended card
48
  supports_authenticated_extended_card=False,
49
  )
50
 
51
+ app = A2AStarletteApplication(
52
  agent_card=agent_card,
53
  http_handler=DefaultRequestHandler(
54
  agent_executor=HelloWorldAgentExecutor(),
55
  task_store=InMemoryTaskStore(),
56
  ),
57
+ ).build()
58
+
59
+ # ---- Add GET endpoints so HF/browser doesn't 405 on "/" ------------------
60
+ from starlette.responses import HTMLResponse, PlainTextResponse
61
+
62
+ async def landing(_):
63
+ return HTMLResponse(
64
+ "<h3>HF A2A Agent</h3>"
65
+ "<p>Health: <a href=\"/health\">/health</a></p>"
66
+ "<p>Send a message (POST JSON) to <code>/message/send</code>:</p>"
67
+ "<pre>{\"message\": \"hello\"}</pre>"
68
+ )
69
+
70
+ async def health(_):
71
+ return PlainTextResponse("ok")
72
+
73
+ app.router.add_route("/", landing, methods=["GET"])
74
+ app.router.add_route("/health", health, methods=["GET"])
75
+
76
+ return app
77
+
78
 
79
  if __name__ == "__main__":
80
+ port = int(os.getenv("PORT", "7860")) # HF provides PORT
81
  uvicorn.run(build_app(), host="0.0.0.0", port=port)