Skip to content

Commit 11dea88

Browse files
committed
lint, type, fmt fixes
1 parent 88113d3 commit 11dea88

File tree

8 files changed

+10
-66
lines changed

8 files changed

+10
-66
lines changed

python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_sequential_routed_agent.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def __init__(self, description: str) -> None:
4343
super().__init__(description=description)
4444
self._fifo_lock = FIFOLock()
4545

46-
async def on_message(self, message: Any, ctx: MessageContext) -> Any | None:
46+
async def on_message_impl(self, message: Any, ctx: MessageContext) -> None:
4747
await self._fifo_lock.acquire()
4848
try:
49-
return await super().on_message(message, ctx)
49+
await super().on_message_impl(message, ctx)
5050
finally:
5151
self._fifo_lock.release()

python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/local-llms-ollama-litellm.ipynb

+1-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
},
175175
{
176176
"cell_type": "code",
177-
"execution_count": 6,
177+
"execution_count": null,
178178
"metadata": {},
179179
"outputs": [
180180
{
@@ -211,7 +211,6 @@
211211
"await runtime.send_message(\n",
212212
" Message(\"Joe, tell me a joke.\"),\n",
213213
" recipient=AgentId(joe, \"default\"),\n",
214-
" sender=AgentId(cathy, \"default\"),\n",
215214
")\n",
216215
"await runtime.stop_when_idle()"
217216
]

python/packages/autogen-core/src/autogen_core/application/_worker_runtime_host_servicer.py

-49
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,6 @@ async def _receive_messages(
102102
logger.info(f"Received message from client {client_id}: {message}")
103103
oneofcase = message.WhichOneof("message")
104104
match oneofcase:
105-
case "request":
106-
request: agent_worker_pb2.RpcRequest = message.request
107-
task = asyncio.create_task(self._process_request(request, client_id))
108-
self._background_tasks.add(task)
109-
task.add_done_callback(self._raise_on_exception)
110-
task.add_done_callback(self._background_tasks.discard)
111-
case "response":
112-
response: agent_worker_pb2.RpcResponse = message.response
113-
task = asyncio.create_task(self._process_response(response, client_id))
114-
self._background_tasks.add(task)
115-
task.add_done_callback(self._raise_on_exception)
116-
task.add_done_callback(self._background_tasks.discard)
117105
case "cloudEvent":
118106
# The proto typing doesnt resolve this one
119107
event = cast(cloudevent_pb2.CloudEvent, message.cloudEvent) # type: ignore
@@ -140,43 +128,6 @@ async def _receive_messages(
140128
case None:
141129
logger.warning("Received empty message")
142130

143-
async def _process_request(self, request: agent_worker_pb2.RpcRequest, client_id: int) -> None:
144-
# Deliver the message to a client given the target agent type.
145-
async with self._agent_type_to_client_id_lock:
146-
target_client_id = self._agent_type_to_client_id.get(request.target.type)
147-
if target_client_id is None:
148-
logger.error(f"Agent {request.target.type} not found, failed to deliver message.")
149-
return
150-
target_send_queue = self._send_queues.get(target_client_id)
151-
if target_send_queue is None:
152-
logger.error(f"Client {target_client_id} not found, failed to deliver message.")
153-
return
154-
await target_send_queue.put(agent_worker_pb2.Message(request=request))
155-
156-
# Create a future to wait for the response from the target.
157-
future = asyncio.get_event_loop().create_future()
158-
self._pending_responses.setdefault(target_client_id, {})[request.request_id] = future
159-
160-
# Create a task to wait for the response and send it back to the client.
161-
send_response_task = asyncio.create_task(self._wait_and_send_response(future, client_id))
162-
self._background_tasks.add(send_response_task)
163-
send_response_task.add_done_callback(self._raise_on_exception)
164-
send_response_task.add_done_callback(self._background_tasks.discard)
165-
166-
async def _wait_and_send_response(self, future: Future[agent_worker_pb2.RpcResponse], client_id: int) -> None:
167-
response = await future
168-
message = agent_worker_pb2.Message(response=response)
169-
send_queue = self._send_queues.get(client_id)
170-
if send_queue is None:
171-
logger.error(f"Client {client_id} not found, failed to send response message.")
172-
return
173-
await send_queue.put(message)
174-
175-
async def _process_response(self, response: agent_worker_pb2.RpcResponse, client_id: int) -> None:
176-
# Setting the result of the future will send the response back to the original sender.
177-
future = self._pending_responses[client_id].pop(response.request_id)
178-
future.set_result(response)
179-
180131
async def _process_event(self, event: cloudevent_pb2.CloudEvent) -> None:
181132
topic_id = TopicId(type=event.type, source=event.source)
182133
recipients = await self._subscription_manager.get_subscribed_recipients(topic_id)

python/packages/autogen-core/src/autogen_core/base/_agent_proxy.py

-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ async def send_message(
2929
self,
3030
message: Any,
3131
*,
32-
sender: AgentId,
3332
cancellation_token: CancellationToken | None = None,
3433
) -> Any:
3534
return await self._runtime.send_message(
3635
message,
3736
recipient=self._agent,
38-
sender=sender,
3937
cancellation_token=cancellation_token,
4038
)
4139

python/packages/autogen-core/src/autogen_core/base/_base_agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ async def on_message_impl(self, message: Any, ctx: MessageContext) -> None: ...
126126
@final
127127
async def on_message(self, message: Any, ctx: MessageContext) -> None:
128128
# Intercept RPC responses
129-
if ctx.topic_id is not None and (request_id := is_rpc_response(ctx.topic_id.type)) is not None:
129+
if (request_id := is_rpc_response(ctx.topic_id.type)) is not None:
130130
if request_id in self._pending_rpc_requests:
131131
self._pending_rpc_requests[request_id].set_result(message)
132132
del self._pending_rpc_requests[request_id]

python/packages/autogen-core/src/autogen_core/components/_routed_agent.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ def __init__(self, description: str) -> None:
515515

516516
super().__init__(description)
517517

518-
async def on_message_impl(self, message: Any, ctx: MessageContext):
518+
async def on_message_impl(self, message: Any, ctx: MessageContext) -> None:
519519
"""Handle a message by routing it to the appropriate message handler.
520520
Do not override this method in subclasses. Instead, add message handlers as methods decorated with
521521
either the :func:`event` or :func:`rpc` decorator."""
@@ -526,8 +526,8 @@ async def on_message_impl(self, message: Any, ctx: MessageContext):
526526
# Call the first handler whose router returns True and then return the result.
527527
for h in handlers:
528528
if h.router(message, ctx):
529-
return await h(self, message, ctx)
530-
return await self.on_unhandled_message(message, ctx) # type: ignore
529+
await h(self, message, ctx)
530+
await self.on_unhandled_message(message, ctx)
531531

532532
async def on_unhandled_message(self, message: Any, ctx: MessageContext) -> None:
533533
"""Called when a message is received that does not have a matching message handler.

python/packages/autogen-core/tests/test_routed_agent.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66
from autogen_core.application import SingleThreadedAgentRuntime
77
from autogen_core.base import AgentId, MessageContext, TopicId
8+
from autogen_core.base._rpc import is_rpc_request
89
from autogen_core.components import RoutedAgent, TypeSubscription, event, message_handler, rpc
910
from test_utils import LoopbackAgent
1011

@@ -23,12 +24,12 @@ def __init__(self) -> None:
2324
self.num_calls_rpc = 0
2425
self.num_calls_broadcast = 0
2526

26-
@message_handler(match=lambda _, ctx: ctx.is_rpc)
27+
@message_handler(match=lambda _, ctx: is_rpc_request(ctx.topic_id.type) is not None)
2728
async def on_rpc_message(self, message: MessageType, ctx: MessageContext) -> MessageType:
2829
self.num_calls_rpc += 1
2930
return message
3031

31-
@message_handler(match=lambda _, ctx: not ctx.is_rpc)
32+
@message_handler(match=lambda _, ctx: is_rpc_request(ctx.topic_id.type) is None)
3233
async def on_broadcast_message(self, message: MessageType, ctx: MessageContext) -> None:
3334
self.num_calls_broadcast += 1
3435

python/packages/autogen-magentic-one/tests/headless_web_surfer/test_web_surfer.py

-5
Original file line numberDiff line numberDiff line change
@@ -218,27 +218,22 @@ async def test_web_surfer_oai() -> None:
218218
)
219219
),
220220
recipient=web_surfer.id,
221-
sender=user_proxy.id,
222221
)
223222
await runtime.send_message(
224223
BroadcastMessage(content=UserMessage(content="Please scroll down.", source="user")),
225224
recipient=web_surfer.id,
226-
sender=user_proxy.id,
227225
)
228226
await runtime.send_message(
229227
BroadcastMessage(content=UserMessage(content="Please scroll up.", source="user")),
230228
recipient=web_surfer.id,
231-
sender=user_proxy.id,
232229
)
233230
await runtime.send_message(
234231
BroadcastMessage(content=UserMessage(content="When was it founded?", source="user")),
235232
recipient=web_surfer.id,
236-
sender=user_proxy.id,
237233
)
238234
await runtime.send_message(
239235
BroadcastMessage(content=UserMessage(content="What's this page about?", source="user")),
240236
recipient=web_surfer.id,
241-
sender=user_proxy.id,
242237
)
243238
await runtime.stop_when_idle()
244239

0 commit comments

Comments
 (0)