@@ -121,8 +121,6 @@ def send_message(
121
121
# )
122
122
# )
123
123
124
- if recipient .namespace not in self ._known_namespaces :
125
- self ._prepare_namespace (recipient .namespace )
126
124
127
125
future = asyncio .get_event_loop ().create_future ()
128
126
if recipient .name not in self ._known_agent_names :
@@ -131,6 +129,8 @@ def send_message(
131
129
if sender is not None and sender .namespace != recipient .namespace :
132
130
raise ValueError ("Sender and recipient must be in the same namespace to communicate." )
133
131
132
+ self ._process_seen_namespace (recipient .namespace )
133
+
134
134
logger .info (f"Sending message of type { type (message ).__name__ } to { recipient .name } : { message .__dict__ } " )
135
135
136
136
self ._message_queue .append (
@@ -180,8 +180,8 @@ def publish_message(
180
180
181
181
assert explicit_namespace is not None or sender_namespace is not None
182
182
namespace = cast (str , explicit_namespace or sender_namespace )
183
- if namespace not in self ._known_namespaces :
184
- self . _prepare_namespace ( namespace )
183
+ self ._process_seen_namespace ( namespace )
184
+
185
185
186
186
self ._message_queue .append (
187
187
PublishMessageEnvelope (
@@ -389,6 +389,11 @@ def register(
389
389
else :
390
390
self ._valid_namespaces [name ] = []
391
391
392
+ # For all already prepared namespaces we need to prepare this agent
393
+ for namespace in self ._known_namespaces :
394
+ if self ._type_valid_for_namespace (AgentId (name = name , namespace = namespace )):
395
+ self ._get_agent (AgentId (name = name , namespace = namespace ))
396
+
392
397
def _invoke_agent_factory (
393
398
self , agent_factory : Callable [[], T ] | Callable [[AgentRuntime , AgentId ], T ], agent_id : AgentId
394
399
) -> T :
@@ -419,13 +424,13 @@ def _type_valid_for_namespace(self, agent_id: AgentId) -> bool:
419
424
return agent_id .namespace in valid_namespaces
420
425
421
426
def _get_agent (self , agent_id : AgentId ) -> Agent :
427
+ self ._process_seen_namespace (agent_id .namespace )
422
428
if agent_id in self ._instantiated_agents :
423
429
return self ._instantiated_agents [agent_id ]
424
430
425
431
if not self ._type_valid_for_namespace (agent_id ):
426
432
raise ValueError (f"Agent with name { agent_id .name } not valid for namespace { agent_id .namespace } ." )
427
433
428
- self ._known_namespaces .add (agent_id .namespace )
429
434
if agent_id .name not in self ._agent_factories :
430
435
raise ValueError (f"Agent with name { agent_id .name } not found." )
431
436
@@ -446,7 +451,11 @@ def get_proxy(self, name: str, *, namespace: str = "default") -> AgentProxy:
446
451
447
452
# Hydrate the agent instances in a namespace. The primary reason for this is
448
453
# to ensure message type subscriptions are set up.
449
- def _prepare_namespace (self , namespace : str ) -> None :
454
+ def _process_seen_namespace (self , namespace : str ) -> None :
455
+ if namespace in self ._known_namespaces :
456
+ return
457
+
458
+ self ._known_namespaces .add (namespace )
450
459
for name in self ._known_agent_names :
451
460
if self ._type_valid_for_namespace (AgentId (name = name , namespace = namespace )):
452
461
self ._get_agent (AgentId (name = name , namespace = namespace ))
0 commit comments