|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +from agent_framework import AgentSession, BaseAgent, SupportsAgentRun |
| 4 | +from agent_framework._telemetry import user_agent_prefix |
| 5 | +from azure.ai.agentserver.invocations import InvocationAgentServerHost |
| 6 | +from starlette.requests import Request |
| 7 | +from starlette.responses import JSONResponse, Response, StreamingResponse |
| 8 | +from typing_extensions import Any, AsyncGenerator |
| 9 | + |
| 10 | + |
| 11 | +class InvocationsHostServer(InvocationAgentServerHost): |
| 12 | + """An invocations server host for an agent.""" |
| 13 | + |
| 14 | + USER_AGENT_PREFIX = "foundry-hosting" |
| 15 | + |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + agent: BaseAgent, |
| 19 | + *, |
| 20 | + openapi_spec: dict[str, Any] | None = None, |
| 21 | + **kwargs: Any, |
| 22 | + ) -> None: |
| 23 | + """Initialize an InvocationsHostServer. |
| 24 | +
|
| 25 | + Args: |
| 26 | + agent: The agent to handle responses for. |
| 27 | + openapi_spec: The OpenAPI specification for the server. |
| 28 | + **kwargs: Additional keyword arguments. |
| 29 | +
|
| 30 | + This host will expect the request to be a JSON body with a "message" field. |
| 31 | + The response from the host will be a JSON object with a "response" field containing |
| 32 | + the agent's response and a "session_id" field containing the session ID. |
| 33 | + """ |
| 34 | + super().__init__(openapi_spec=openapi_spec, **kwargs) |
| 35 | + |
| 36 | + if not isinstance(agent, SupportsAgentRun): |
| 37 | + raise TypeError("Agent must support the SupportsAgentRun interface") |
| 38 | + |
| 39 | + self._agent = agent |
| 40 | + self._sessions: dict[str, AgentSession] = {} |
| 41 | + self.invoke_handler(self._handle_invoke) # pyright: ignore[reportUnknownMemberType] |
| 42 | + |
| 43 | + async def _handle_invoke(self, request: Request) -> Response: |
| 44 | + """Invoke the agent with the given request.""" |
| 45 | + with user_agent_prefix(self.USER_AGENT_PREFIX): |
| 46 | + return await self._handle_invoke_inner(request) |
| 47 | + |
| 48 | + async def _handle_invoke_inner(self, request: Request) -> Response: |
| 49 | + """Core invoke handler logic.""" |
| 50 | + data = await request.json() |
| 51 | + session_id: str = request.state.session_id |
| 52 | + |
| 53 | + stream = data.get("stream", False) |
| 54 | + user_message = data.get("message", None) |
| 55 | + if user_message is None: |
| 56 | + error = "Missing 'message' in request" |
| 57 | + if stream: |
| 58 | + return StreamingResponse(content=error, status_code=400) |
| 59 | + return Response(content=error, status_code=400) |
| 60 | + |
| 61 | + session = self._sessions.setdefault(session_id, AgentSession(session_id=session_id)) |
| 62 | + |
| 63 | + if stream: |
| 64 | + |
| 65 | + async def stream_response() -> AsyncGenerator[str]: |
| 66 | + async for update in self._agent.run(user_message, session=session, stream=True): |
| 67 | + if update.text: |
| 68 | + yield update.text |
| 69 | + |
| 70 | + return StreamingResponse( |
| 71 | + stream_response(), |
| 72 | + media_type="text/event-stream", |
| 73 | + headers={"Cache-Control": "no-cache", "Connection": "keep-alive"}, |
| 74 | + ) |
| 75 | + |
| 76 | + response = await self._agent.run([user_message], session=session, stream=stream) |
| 77 | + return JSONResponse({ |
| 78 | + "response": response.text, |
| 79 | + "session_id": session_id, |
| 80 | + }) |
0 commit comments