A programming framework for agentic AI
Impl register and add sub RPC (#5191)
Jack Gerrits55e929db9818
diff --git a/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_utils.py b/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_utils.pynew file mode 100644index 000000000..21c62be59--- /dev/null+++ b/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_utils.py@@ -0,0 +1,45 @@+from autogen_core._subscription import Subscription+from autogen_core._type_prefix_subscription import TypePrefixSubscription+from autogen_core._type_subscription import TypeSubscription++from .protos import agent_worker_pb2+++def subscription_to_proto(subscription: Subscription) -> agent_worker_pb2.Subscription:+ match subscription:+ case TypeSubscription(topic_type=topic_type, agent_type=agent_type, id=id):+ return agent_worker_pb2.Subscription(+ id=id,+ typeSubscription=agent_worker_pb2.TypeSubscription(topic_type=topic_type, agent_type=agent_type),+ )+ case TypePrefixSubscription(topic_type_prefix=topic_type_prefix, agent_type=agent_type, id=id):+ return agent_worker_pb2.Subscription(+ id=id,+ typePrefixSubscription=agent_worker_pb2.TypePrefixSubscription(+ topic_type_prefix=topic_type_prefix, agent_type=agent_type+ ),+ )+ case _:+ raise ValueError("Unsupported subscription type.")+++def subscription_from_proto(subscription: agent_worker_pb2.Subscription) -> Subscription:+ oneofcase = subscription.WhichOneof("subscription")+ match oneofcase:+ case "typeSubscription":+ type_subscription_msg: agent_worker_pb2.TypeSubscription = subscription.typeSubscription+ return TypeSubscription(+ topic_type=type_subscription_msg.topic_type,+ agent_type=type_subscription_msg.agent_type,+ id=subscription.id,+ )++ case "typePrefixSubscription":+ type_prefix_subscription_msg: agent_worker_pb2.TypePrefixSubscription = subscription.typePrefixSubscription+ return TypePrefixSubscription(+ topic_type_prefix=type_prefix_subscription_msg.topic_type_prefix,+ agent_type=type_prefix_subscription_msg.agent_type,+ id=subscription.id,+ )+ case None:+ raise ValueError("Invalid subscription message.")diff --git a/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime.py b/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime.pyindex afe2fcb09..ee41a1f58 100644--- a/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime.py+++ b/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime.py@@ -1,3 +1,5 @@+from __future__ import annotations+import asyncioimport inspectimport json@@ -23,6 +25,7 @@ from typing import (ParamSpec,Sequence,Set,+ Tuple,Type,TypeVar,cast,@@ -43,8 +46,6 @@ from autogen_core import (MessageSerializer,Subscription,TopicId,- TypePrefixSubscription,- TypeSubscription,)from autogen_core._runtime_impl_helpers import SubscriptionManager, get_implfrom autogen_core._serialization import (@@ -55,6 +56,8 @@ from google.protobuf import any_pb2from opentelemetry.trace import TracerProviderfrom typing_extensions import Self+from autogen_ext.runtimes.grpc._utils import subscription_to_proto+from . import _constantsfrom ._constants import GRPC_IMPORT_ERROR_STRfrom ._type_helpers import ChannelArgumentType@@ -112,13 +115,22 @@ class HostConnection:)]- def __init__(self, channel: grpc.aio.Channel) -> None: # type: ignore+ def __init__(self, channel: grpc.aio.Channel, stub: Any) -> None: # type: ignoreself._channel = channelself._send_queue = asyncio.Queue[agent_worker_pb2.Message]()self._recv_queue = asyncio.Queue[agent_worker_pb2.Message]()self._connection_task: Task[None] | None = None+ self._stub: AgentRpcAsyncStub = stubself._client_id = str(uuid.uuid4())+ @property+ def stub(self) -> Any:+ return self._stub++ @property+ def metadata(self) -> Sequence[Tuple[str, str]]:+ return [("client-id", self._client_id)]+@classmethoddef from_host_address(cls, host_address: str, extra_grpc_config: ChannelArgumentType = DEFAULT_GRPC_CONFIG) -> Self:logger.info("Connecting to %s", host_address)@@ -131,9 +143,10 @@ class HostConnection:host_address,options=merged_options,)- instance = cls(channel)+ stub: AgentRpcAsyncStub = agent_worker_pb2_grpc.AgentRpcStub(channel) # type: ignore+ instance = cls(channel, stub)instance._connection_task = asyncio.create_task(- instance._connect(channel, instance._send_queue, instance._recv_queue, instance._client_id)+ instance._connect(stub, instance._send_queue, instance._recv_queue, instance._client_id))return instance@@ -144,28 +157,25 @@ class HostConnection:await self._connection_task@staticmethod- async def _connect( # type: ignore- channel: grpc.aio.Channel,+ async def _connect(+ stub: Any, # AgentRpcAsyncStubsend_queue: asyncio.Queue[agent_worker_pb2.Message],receive_queue: asyncio.Queue[agent_worker_pb2.Message],client_id: str,) -> None:- stub: AgentRpcAsyncStub = agent_worker_pb2_grpc.AgentRpcStub(channel) # type: ignore-from grpc.aio import StreamStreamCall# TODO: where do exceptions from reading the iterable go? How do we recover from those?recv_stream: StreamStreamCall[agent_worker_pb2.Message, agent_worker_pb2.Message] = stub.OpenChannel( # type: ignoreQueueAsyncIterable(send_queue), metadata=[("client-id", client_id)]- ) # type: ignore+ )while True:logger.info("Waiting for message from host")- message = await recv_stream.read() # type: ignore+ message = cast(agent_worker_pb2.Message, await recv_stream.read()) # type: ignoreif message == grpc.aio.EOF: # type: ignorelogger.info("EOF")break- message = cast(agent_worker_pb2.Message, message)logger.info(f"Received a message from host: {message}")await receive_queue.put(message)logger.info("Put message in receive queue")@@ -258,10 +268,11 @@ class GrpcWorkerAgentRuntime(AgentRuntime):async def _run_read_loop(self) -> None:logger.info("Starting read loop")+ assert self._host_connection is not None# TODO: catch exceptions and reconnectwhile self._running:try:- message = await self._host_connection.recv() # type: ignore+ message = await self._host_connection.recv()oneofcase = agent_worker_pb2.Message.WhichOneof(message, "message")match oneofcase:case "registerAgentTypeRequest" | "addSubscriptionRequest":@@ -277,9 +288,7 @@ class GrpcWorkerAgentRuntime(AgentRuntime):task.add_done_callback(self._raise_on_exception)task.add_done_callback(self._background_tasks.discard)case "cloudEvent":- # The proto typing doesnt resolve this one- cloud_event = cast(cloudevent_pb2.CloudEvent, message.cloudEvent) # type: ignore- task = asyncio.create_task(self._process_event(cloud_event))+ task = asyncio.create_task(self._process_event(message.cloudEvent))self._background_tasks.add(task)task.add_done_callback(self._raise_on_exception)task.add_done_callback(self._background_tasks.discard)@@ -734,14 +743,13 @@ class GrpcWorkerAgentRuntime(AgentRuntime):self._pending_requests[request_id] = future# Send the registration request message to the host.- message = agent_worker_pb2.Message(- registerAgentTypeRequest=agent_worker_pb2.RegisterAgentTypeRequest(request_id=request_id, type=type.type)+ message = agent_worker_pb2.RegisterAgentTypeRequest(request_id=request_id, type=type.type)+ response: agent_worker_pb2.RegisterAgentTypeResponse = await self._host_connection.stub.RegisterAgent(+ message, metadata=self._host_connection.metadata)- await self._host_connection.send(message)-- # Wait for the registration response.- await future-+ # TODO: just use grpc error handling+ if not response.success:+ raise RuntimeError(response.error)return typeasync def _process_register_agent_type_response(self, response: agent_worker_pb2.RegisterAgentTypeResponse) -> None:@@ -805,49 +813,20 @@ class GrpcWorkerAgentRuntime(AgentRuntime):raise RuntimeError("Host connection is not set.")# Create a future for the subscription response.- future = asyncio.get_event_loop().create_future()request_id = await self._get_new_request_id()- match subscription:- case TypeSubscription(topic_type=topic_type, agent_type=agent_type, id=id):- message = agent_worker_pb2.Message(- addSubscriptionRequest=agent_worker_pb2.AddSubscriptionRequest(- request_id=request_id,- subscription=agent_worker_pb2.Subscription(- id=id,- typeSubscription=agent_worker_pb2.TypeSubscription(- topic_type=topic_type, agent_type=agent_type- ),- ),- )- )- case TypePrefixSubscription(topic_type_prefix=topic_type_prefix, agent_type=agent_type, id=id):- message = agent_worker_pb2.Message(- addSubscriptionRequest=agent_worker_pb2.AddSubscriptionRequest(- request_id=request_id,- subscription=agent_worker_pb2.Subscription(- id=id,- typePrefixSubscription=agent_worker_pb2.TypePrefixSubscription(- topic_type_prefix=topic_type_prefix, agent_type=agent_type- ),- ),- )- )- case _:- raise ValueError("Unsupported subscription type.")-- # Add the future to the pending requests.- self._pending_requests[request_id] = future+ message = agent_worker_pb2.AddSubscriptionRequest(+ request_id=request_id, subscription=subscription_to_proto(subscription)+ )+ response: agent_worker_pb2.AddSubscriptionResponse = await self._host_connection.stub.AddSubscription(+ message, metadata=self._host_connection.metadata+ )+ if not response.success:+ raise RuntimeError(response.error)# Add to local subscription manager.await self._subscription_manager.add_subscription(subscription)- # Send the subscription to the host.- await self._host_connection.send(message)-- # Wait for the subscription response.- await future-async def _process_add_subscription_response(self, response: agent_worker_pb2.AddSubscriptionResponse) -> None:future = self._pending_requests.pop(response.request_id)if response.HasField("error") and response.error != "":diff --git a/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime_host_servicer.py b/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime_host_servicer.pyindex f6567b5d9..e5c7d53df 100644--- a/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime_host_servicer.py+++ b/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime_host_servicer.py@@ -1,13 +1,14 @@import asyncioimport logging-from _collections_abc import AsyncIterator, Iterator+from _collections_abc import AsyncIteratorfrom asyncio import Future, Task-from typing import Any, Dict, Sequence, Set, Tuple, cast+from typing import Any, Dict, Sequence, Set, Tuple-from autogen_core import Subscription, TopicId, TypePrefixSubscription, TypeSubscription+from autogen_core import TopicIdfrom autogen_core._runtime_impl_helpers import SubscriptionManagerfrom ._constants import GRPC_IMPORT_ERROR_STR+from ._utils import subscription_from_prototry:import grpc@@ -28,6 +29,15 @@ def metadata_to_dict(metadata: Sequence[Tuple[str, str]] | None) -> Dict[str, streturn {key: value for key, value in metadata}+async def get_client_id_or_abort(context: grpc.aio.ServicerContext[Any, Any]) -> str: # type: ignore+ # The type hint on context.invocation_metadata() is incorrect.+ metadata = metadata_to_dict(context.invocation_metadata()) # type: ignore+ if (client_id := metadata.get("client-id")) is None:+ await context.abort(grpc.StatusCode.INVALID_ARGUMENT, "client-id metadata not found.")++ return client_id # type: ignore++class GrpcWorkerAgentRuntimeHostServicer(agent_worker_pb2_grpc.AgentRpcServicer):"""A gRPC servicer that hosts message delivery service for agents."""@@ -44,11 +54,8 @@ class GrpcWorkerAgentRuntimeHostServicer(agent_worker_pb2_grpc.AgentRpcServicer)self,request_iterator: AsyncIterator[agent_worker_pb2.Message],context: grpc.aio.ServicerContext[agent_worker_pb2.Message, agent_worker_pb2.Message],- ) -> Iterator[agent_worker_pb2.Message] | AsyncIterator[agent_worker_pb2.Message]: # type: ignore- metadata = metadata_to_dict(context.invocation_metadata()) # type: ignore- if (client_id := cast(ClientConnectionId | None, metadata.get("client-id"))) is None: # type: ignore- logger.error("Client id not found in metadata. Refusing connection.")- return+ ) -> AsyncIterator[agent_worker_pb2.Message]:+ client_id = await get_client_id_or_abort(context)# Register the client with the server and create a send queue for the client.send_queue: asyncio.Queue[agent_worker_pb2.Message] = asyncio.Queue()@@ -119,9 +126,7 @@ class GrpcWorkerAgentRuntimeHostServicer(agent_worker_pb2_grpc.AgentRpcServicer)task.add_done_callback(self._raise_on_exception)task.add_done_callback(self._background_tasks.discard)case "cloudEvent":- # The proto typing doesnt resolve this one- event = cast(cloudevent_pb2.CloudEvent, message.cloudEvent) # type: ignore- task = asyncio.create_task(self._process_event(event))+ task = asyncio.create_task(self._process_event(message.cloudEvent))self._background_tasks.add(task)task.add_done_callback(self._raise_on_exception)task.add_done_callback(self._background_tasks.discard)@@ -227,96 +232,98 @@ class GrpcWorkerAgentRuntimeHostServicer(agent_worker_pb2_grpc.AgentRpcServicer)async def _process_add_subscription_request(self, add_subscription_req: agent_worker_pb2.AddSubscriptionRequest, client_id: ClientConnectionId) -> None:- oneofcase = add_subscription_req.subscription.WhichOneof("subscription")- subscription: Subscription | None = None- match oneofcase:- case "typeSubscription":- type_subscription_msg: agent_worker_pb2.TypeSubscription = (- add_subscription_req.subscription.typeSubscription- )- subscription = TypeSubscription(- topic_type=type_subscription_msg.topic_type,- agent_type=type_subscription_msg.agent_type,- id=add_subscription_req.subscription.id,- )-- case "typePrefixSubscription":- type_prefix_subscription_msg: agent_worker_pb2.TypePrefixSubscription = (- add_subscription_req.subscription.typePrefixSubscription- )- subscription = TypePrefixSubscription(- topic_type_prefix=type_prefix_subscription_msg.topic_type_prefix,- agent_type=type_prefix_subscription_msg.agent_type,- id=add_subscription_req.subscription.id,- )- case None:- logger.warning("Received empty subscription message")-- if subscription is not None:- try:- await self._subscription_manager.add_subscription(subscription)- subscription_ids = self._client_id_to_subscription_id_mapping.setdefault(client_id, set())- subscription_ids.add(subscription.id)- success = True- error = None- except ValueError as e:- success = False- error = str(e)- # Send a response back to the client.- await self._send_queues[client_id].put(- agent_worker_pb2.Message(- addSubscriptionResponse=agent_worker_pb2.AddSubscriptionResponse(- request_id=add_subscription_req.request_id, success=success, error=error- )+ subscription = subscription_from_proto(add_subscription_req.subscription)+ try:+ await self._subscription_manager.add_subscription(subscription)+ subscription_ids = self._client_id_to_subscription_id_mapping.setdefault(client_id, set())+ subscription_ids.add(subscription.id)+ success = True+ error = None+ except ValueError as e:+ success = False+ error = str(e)+ # Send a response back to the client.+ await self._send_queues[client_id].put(+ agent_worker_pb2.Message(+ addSubscriptionResponse=agent_worker_pb2.AddSubscriptionResponse(+ request_id=add_subscription_req.request_id, success=success, error=error))+ )- def RegisterAgent( # type: ignore+ async def RegisterAgent( # type: ignoreself,request: agent_worker_pb2.RegisterAgentTypeRequest,context: grpc.aio.ServicerContext[agent_worker_pb2.RegisterAgentTypeRequest, agent_worker_pb2.RegisterAgentTypeResponse],) -> agent_worker_pb2.RegisterAgentTypeResponse:- raise NotImplementedError("Method not implemented.")+ client_id = await get_client_id_or_abort(context)++ async with self._agent_type_to_client_id_lock:+ if request.type in self._agent_type_to_client_id:+ existing_client_id = self._agent_type_to_client_id[request.type]+ logger.error(f"Agent type {request.type} already registered with client {existing_client_id}.")+ success = False+ error = f"Agent type {request.type} already registered."+ else:+ self._agent_type_to_client_id[request.type] = client_id+ success = True+ error = None+ return agent_worker_pb2.RegisterAgentTypeResponse(request_id=request.request_id, success=success, error=error)- def AddSubscription( # type: ignore+ async def AddSubscription( # type: ignoreself,request: agent_worker_pb2.AddSubscriptionRequest,context: grpc.aio.ServicerContext[agent_worker_pb2.AddSubscriptionRequest, agent_worker_pb2.AddSubscriptionResponse],) -> agent_worker_pb2.AddSubscriptionResponse:- raise NotImplementedError("Method not implemented.")+ client_id = await get_client_id_or_abort(context)++ subscription = subscription_from_proto(request.subscription)+ try:+ await self._subscription_manager.add_subscription(subscription)+ subscription_ids = self._client_id_to_subscription_id_mapping.setdefault(client_id, set())+ subscription_ids.add(subscription.id)+ success = True+ error = None+ except ValueError as e:+ success = False+ error = str(e)+ # Send a response back to the client.+ return agent_worker_pb2.AddSubscriptionResponse(request_id=request.request_id, success=success, error=error)- def RemoveSubscription( # type: ignore+ async def RemoveSubscription( # type: ignoreself,request: agent_worker_pb2.RemoveSubscriptionRequest,context: grpc.aio.ServicerContext[agent_worker_pb2.RemoveSubscriptionRequest, agent_worker_pb2.RemoveSubscriptionResponse],) -> agent_worker_pb2.RemoveSubscriptionResponse:+ _client_id = await get_client_id_or_abort(context)raise NotImplementedError("Method not implemented.")- def GetSubscriptions( # type: ignore+ async def GetSubscriptions( # type: ignoreself,request: agent_worker_pb2.GetSubscriptionsRequest,context: grpc.aio.ServicerContext[agent_worker_pb2.GetSubscriptionsRequest, agent_worker_pb2.GetSubscriptionsResponse],) -> agent_worker_pb2.GetSubscriptionsResponse:+ _client_id = await get_client_id_or_abort(context)raise NotImplementedError("Method not implemented.")async def GetState( # type: ignoreself,request: agent_worker_pb2.AgentId,context: grpc.aio.ServicerContext[agent_worker_pb2.AgentId, agent_worker_pb2.GetStateResponse],- ) -> agent_worker_pb2.GetStateResponse: # type: ignore+ ) -> agent_worker_pb2.GetStateResponse:raise NotImplementedError("Method not implemented!")async def SaveState( # type: ignoreself,request: agent_worker_pb2.AgentState,context: grpc.aio.ServicerContext[agent_worker_pb2.AgentId, agent_worker_pb2.SaveStateResponse],- ) -> agent_worker_pb2.SaveStateResponse: # type: ignore+ ) -> agent_worker_pb2.SaveStateResponse:raise NotImplementedError("Method not implemented!")