Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions agentkit/apps/agent_server_app/agent_server_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import json
import logging
from contextlib import asynccontextmanager
from typing import Any
from typing_extensions import override

import uvicorn
Expand Down Expand Up @@ -67,6 +68,19 @@ def load_agent(self, agent_name: str) -> BaseAgent:
def list_agents(self) -> list[str]:
return [self.agent.name]

@override
def list_agents_detailed(self) -> list[dict[str, Any]]:
name = self.agent.name
description = getattr(self.agent, "description", "") or ""
return [
{
"name": name,
"root_agent_name": name,
"description": description,
"language": "python",
}
]


class AgentkitAgentServerApp(BaseAgentkitApp):
def __init__(
Expand Down Expand Up @@ -110,8 +124,6 @@ async def lifespan(app: FastAPI):

self.app = self.server.get_fast_api_app(lifespan=lifespan)

self.app.mount("/", _a2a_server_app)

# Attach ASGI middleware for unified telemetry across all routes
self.app.add_middleware(AgentkitTelemetryHTTPMiddleware)

Expand All @@ -121,9 +133,12 @@ async def _invoke_compat(request: Request):

# Extract headers (fallback keys supported)
headers = request.headers
user_id = (
headers.get("user_id") or headers.get("x-user-id") or "agentkit_user"
)
telemetry_headers = {
k: v
for k, v in dict(headers).items()
if k.lower() not in {"authorization", "token"}
}
user_id = headers.get("user_id") or "agentkit_user"
session_id = headers.get("session_id") or ""

# Determine app_name from loader
Expand Down Expand Up @@ -157,7 +172,7 @@ async def _invoke_compat(request: Request):
telemetry.trace_agent_server(
func_name="_invoke_compat",
span=span,
headers=dict(headers),
headers=telemetry_headers,
text=text or "",
)

Expand Down Expand Up @@ -210,6 +225,9 @@ async def event_generator():
# Compatibility route for AgentKit CLI invoke
self.app.add_api_route("/invoke", _invoke_compat, methods=["POST"])

# Mount A2A server app last to avoid shadowing API routes like `/invoke`.
self.app.mount("/", _a2a_server_app)

def run(self, host: str, port: int = 8000) -> None:
"""Run the app with Uvicorn server."""
uvicorn.run(self.app, host=host, port=port)
11 changes: 7 additions & 4 deletions agentkit/apps/agent_server_app/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def __init__(self, app: Callable):
self.app = app

async def __call__(self, scope, receive, send):
print(f"test: {scope}")
if scope["type"] != "http":
return await self.app(scope, receive, send)

Expand All @@ -37,16 +36,18 @@ async def __call__(self, scope, receive, send):
headers = {k.decode("latin-1"): v.decode("latin-1") for k, v in headers_list}
span = telemetry.tracer.start_span(name="agent_server_request")
ctx = trace.set_span_in_context(span)
context_api.attach(ctx)
token = context_api.attach(ctx)
headers = {
k: v for k, v in headers.items() if k.lower() not in _EXCLUDED_HEADERS
}

# Currently unable to retrieve user_id and session_id from headers; keep logic for future use
user_id = headers.get("user_id")
session_id = headers.get("session_id")
headers["user_id"] = user_id
headers["session_id"] = session_id
if user_id:
headers["user_id"] = user_id
if session_id:
headers["session_id"] = session_id
telemetry.trace_agent_server(
func_name=f"{method} {path}",
span=span,
Expand All @@ -73,3 +74,5 @@ async def send_wrapper(message):
except Exception as e:
telemetry.trace_agent_server_finish(path=path, func_result="", exception=e)
raise
finally:
context_api.detach(token)
2 changes: 1 addition & 1 deletion agentkit/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

VERSION = "0.4.2"
VERSION = "0.4.3"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "agentkit-sdk-python"
version = "0.4.2"
version = "0.4.3"
description = "Python SDK for transforming any AI agent into a production-ready application. Framework-agnostic primitives for runtime, memory, authentication, and tools with volcengine-managed infrastructure."
readme = "README.md"
requires-python = ">=3.10"
Expand Down