Skip to content

Commit c35dea8

Browse files
committed
refactor: refine a2a ping route
(cherry picked from commit 386d53feca3bcbc69de1d177d9811354bd47d2c1)
1 parent 43f5191 commit c35dea8

File tree

3 files changed

+51
-62
lines changed

3 files changed

+51
-62
lines changed

agentkit/apps/a2a_app/a2a_app.py

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
from a2a.server.tasks import InMemoryTaskStore
2727
from a2a.server.tasks.task_store import TaskStore
2828
from a2a.types import AgentCard
29-
from starlette.requests import Request
3029
from starlette.applications import Starlette
3130
from starlette.responses import JSONResponse, Response
3231
from starlette.routing import Route
32+
from starlette.requests import Request
3333

3434
from agentkit.apps.a2a_app.telemetry import telemetry
3535
from agentkit.apps.base_app import BaseAgentkitApp
@@ -43,9 +43,7 @@ async def wrapper(*args, **kwargs):
4343
context: RequestContext = args[1]
4444
event_queue: EventQueue = args[2]
4545

46-
with telemetry.tracer.start_as_current_span(
47-
name="a2a_invocation"
48-
) as span:
46+
with telemetry.tracer.start_as_current_span(name="a2a_invocation") as span:
4947
exception = None
5048
try:
5149
result = await execute_func(
@@ -89,9 +87,7 @@ def wrapper(cls: type) -> type[AgentExecutor]:
8987
)
9088

9189
if self._agent_executor:
92-
raise RuntimeError(
93-
"An executor is already bound to this app instance."
94-
)
90+
raise RuntimeError("An executor is already bound to this app instance.")
9591

9692
# Wrap the execute method for intercepting context and event_queue
9793
cls.execute = _wrap_agent_executor_execute_func(cls.execute)
@@ -122,23 +118,6 @@ def wrapper(cls: type) -> type[TaskStore]:
122118

123119
return wrapper
124120

125-
def add_env_detect_route(self, app: Starlette):
126-
def is_agentkit_runtime() -> bool:
127-
if os.getenv("RUNTIME_IAM_ROLE_TRN", ""):
128-
return True
129-
else:
130-
return False
131-
132-
route = Route(
133-
"/env",
134-
endpoint=lambda request: JSONResponse(
135-
{"env": "agentkit" if is_agentkit_runtime() else "veadk"}
136-
),
137-
methods=["GET"],
138-
name="env_detect",
139-
)
140-
app.routes.append(route)
141-
142121
def ping(self, func: Callable) -> Callable:
143122
"""Register a zero-argument health check function and expose it via GET /ping.
144123
@@ -163,6 +142,46 @@ def _format_ping_status(self, result: str | dict) -> dict:
163142
)
164143
return {"status": "error", "message": "Invalid response type."}
165144

145+
async def ping_endpoint(self, request: Request) -> Response:
146+
if not self._ping_func:
147+
logger.error("Ping handler function is not set")
148+
return Response(status_code=404)
149+
150+
try:
151+
result = (
152+
await self._ping_func()
153+
if inspect.iscoroutinefunction(self._ping_func)
154+
else self._ping_func()
155+
)
156+
payload = self._format_ping_status(result)
157+
return JSONResponse(content=payload)
158+
except Exception as e:
159+
logger.exception("Ping handler function failed: %s", e)
160+
return JSONResponse(
161+
content={"status": "error", "message": str(e)},
162+
status_code=500,
163+
)
164+
165+
def add_env_detect_route(self, app: Starlette):
166+
def is_agentkit_runtime() -> bool:
167+
if os.getenv("RUNTIME_IAM_ROLE_TRN", ""):
168+
return True
169+
else:
170+
return False
171+
172+
route = Route(
173+
"/env",
174+
endpoint=lambda request: JSONResponse(
175+
{"env": "agentkit" if is_agentkit_runtime() else "veadk"}
176+
),
177+
methods=["GET"],
178+
name="env_detect",
179+
)
180+
app.routes.append(route)
181+
182+
def add_ping_route(self, app: Starlette):
183+
app.add_route("/ping", self.ping_endpoint, methods=["GET"])
184+
166185
@override
167186
def run(self, agent_card: AgentCard, host: str, port: int = 8000):
168187
if not self._agent_executor:
@@ -182,30 +201,8 @@ def run(self, agent_card: AgentCard, host: str, port: int = 8000):
182201
),
183202
).build()
184203

185-
# Register /ping route consistent with SimpleApp behavior
186-
async def _ping_handler(request: Request):
187-
if not self._ping_func:
188-
logger.error("Ping handler function is not set")
189-
return Response(status_code=404)
190-
191-
try:
192-
result = (
193-
await self._ping_func()
194-
if inspect.iscoroutinefunction(self._ping_func)
195-
else self._ping_func()
196-
)
197-
payload = self._format_ping_status(result)
198-
return JSONResponse(content=payload, media_type="application/json")
199-
except Exception as e:
200-
logger.exception("Ping handler function failed: %s", e)
201-
return JSONResponse(
202-
content={"status": "error", "message": str(e)},
203-
media_type="application/json",
204-
status_code=500,
205-
)
206-
207-
a2a_app.add_route("/ping", _ping_handler, methods=["GET"])
208-
204+
# Register routes in the same style
205+
self.add_ping_route(a2a_app)
209206
self.add_env_detect_route(a2a_app)
210207

211208
uvicorn.run(a2a_app, host=host, port=port)

agentkit/apps/mcp_app/mcp_app.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ def tool(self, func: Callable) -> Callable:
4040
@wraps(func)
4141
async def async_wrapper(*args, **kwargs) -> Any:
4242
# with tracer.start_as_current_span("tool") as span:
43-
with telemetry.tracer.start_as_current_span(
44-
name="tool"
45-
) as span:
43+
with telemetry.tracer.start_as_current_span(name="tool") as span:
4644
exception = None
4745
try:
4846
result = await func(*args, **kwargs)
@@ -70,9 +68,7 @@ async def async_wrapper(*args, **kwargs) -> Any:
7068
@wraps(func)
7169
def sync_wrapper(*args, **kwargs) -> Any:
7270
# with tracer.start_as_current_span("tool") as span:
73-
with telemetry.tracer.start_as_current_span(
74-
name="tool"
75-
) as span:
71+
with telemetry.tracer.start_as_current_span(name="tool") as span:
7672
exception = None
7773
try:
7874
result = func(*args, **kwargs)
@@ -100,9 +96,7 @@ def agent_as_a_tool(self, func: Callable) -> Callable:
10096

10197
@wraps(func)
10298
async def async_wrapper(*args, **kwargs) -> Any:
103-
with telemetry.tracer.start_as_current_span(
104-
name="tool"
105-
) as span:
99+
with telemetry.tracer.start_as_current_span(name="tool") as span:
106100
exception = None
107101
try:
108102
result = await func(*args, **kwargs)
@@ -126,9 +120,7 @@ async def async_wrapper(*args, **kwargs) -> Any:
126120

127121
@wraps(func)
128122
def sync_wrapper(*args, **kwargs) -> Any:
129-
with telemetry.tracer.start_as_current_span(
130-
name="tool"
131-
) as span:
123+
with telemetry.tracer.start_as_current_span(name="tool") as span:
132124
exception = None
133125
try:
134126
result = func(*args, **kwargs)

agentkit/toolkit/resources/samples/a2a.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def ping() -> str:
7474
capabilities=AgentCapabilities(streaming=True),
7575
description=agent.description,
7676
name=agent.name,
77-
defaultInputModes=["text"],
78-
defaultOutputModes=["text"],
77+
default_input_modes=["text"],
78+
default_output_modes=["text"],
7979
provider=AgentProvider(organization="veadk", url=""),
8080
skills=[AgentSkill(id="0", name="chat", description="Chat", tags=["chat"])],
8181
url="http://0.0.0.0:8000",

0 commit comments

Comments
 (0)