Skip to content

Commit 1c69c3a

Browse files
authored
Fix namespace registration (#92)
1 parent 4cebf72 commit 1c69c3a

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/agnext/application/_single_threaded_agent_runtime.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ def send_message(
121121
# )
122122
# )
123123

124-
if recipient.namespace not in self._known_namespaces:
125-
self._prepare_namespace(recipient.namespace)
126124

127125
future = asyncio.get_event_loop().create_future()
128126
if recipient.name not in self._known_agent_names:
@@ -131,6 +129,8 @@ def send_message(
131129
if sender is not None and sender.namespace != recipient.namespace:
132130
raise ValueError("Sender and recipient must be in the same namespace to communicate.")
133131

132+
self._process_seen_namespace(recipient.namespace)
133+
134134
logger.info(f"Sending message of type {type(message).__name__} to {recipient.name}: {message.__dict__}")
135135

136136
self._message_queue.append(
@@ -180,8 +180,8 @@ def publish_message(
180180

181181
assert explicit_namespace is not None or sender_namespace is not None
182182
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+
185185

186186
self._message_queue.append(
187187
PublishMessageEnvelope(
@@ -389,6 +389,11 @@ def register(
389389
else:
390390
self._valid_namespaces[name] = []
391391

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+
392397
def _invoke_agent_factory(
393398
self, agent_factory: Callable[[], T] | Callable[[AgentRuntime, AgentId], T], agent_id: AgentId
394399
) -> T:
@@ -419,13 +424,13 @@ def _type_valid_for_namespace(self, agent_id: AgentId) -> bool:
419424
return agent_id.namespace in valid_namespaces
420425

421426
def _get_agent(self, agent_id: AgentId) -> Agent:
427+
self._process_seen_namespace(agent_id.namespace)
422428
if agent_id in self._instantiated_agents:
423429
return self._instantiated_agents[agent_id]
424430

425431
if not self._type_valid_for_namespace(agent_id):
426432
raise ValueError(f"Agent with name {agent_id.name} not valid for namespace {agent_id.namespace}.")
427433

428-
self._known_namespaces.add(agent_id.namespace)
429434
if agent_id.name not in self._agent_factories:
430435
raise ValueError(f"Agent with name {agent_id.name} not found.")
431436

@@ -446,7 +451,11 @@ def get_proxy(self, name: str, *, namespace: str = "default") -> AgentProxy:
446451

447452
# Hydrate the agent instances in a namespace. The primary reason for this is
448453
# 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)
450459
for name in self._known_agent_names:
451460
if self._type_valid_for_namespace(AgentId(name=name, namespace=namespace)):
452461
self._get_agent(AgentId(name=name, namespace=namespace))

0 commit comments

Comments
 (0)