Skip to content

A programming framework for agentic AI

Update allowed values for agent and topic type (#4484)

Jack Gerrits50fea445395f
diff --git a/docs/design/02 - Topics.md b/docs/design/02 - Topics.md
index bf3ed8d9d..d7c93417c 100644
--- a/docs/design/02 - Topics.md
+++ b/docs/design/02 - Topics.md
@@ -18,13 +18,15 @@ A topic is identified by two components (called a `TopicId`):
- [`type`](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/spec.md#type) - represents the type of event that occurs, this is static and defined in code
- SHOULD use reverse domain name notation to avoid naming conflicts. For example: `com.example.my-topic`.
+ - Allowed values MUST match the regex: `^[\w\-\.\:\=]+\Z`
+ - Notably, this is the same as agent type with the addition of `=` and `:` characters
- [`source`](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/spec.md#source-1) - represents where the event originated from, this is dynamic and based on the message itself
- SHOULD be a URI
Agent instances are identified by two components (called an `AgentId`):
- `type` - represents the type of agent, this is static and defined in code
- - MUST be a valid identifier as defined [here](https://docs.python.org/3/reference/lexical_analysis.html#identifiers) except that only the ASCII range is allowed
+ - Allowed values MUST match the regex: `^[\w\-\.]+\Z`
- `key` - represents the instance of the agent type for the key
- SHOULD be a URI
@@ -61,6 +63,6 @@ For this subscription source should map directly to agent key.
This subscription will therefore receive all events for the following well known topics:
- `{AgentType}:` - General purpose direct messages. These should be routed to the approriate message handler.
-- `{AgentType}:rpc_request` - RPC request messages. These should be routed to the approriate RPC handler.
+- `{AgentType}:rpc_request={RequesterAgentType}` - RPC request messages. These should be routed to the approriate RPC handler, and RequesterAgentType used to publish the response
- `{AgentType}:rpc_response={RequestId}` - RPC response messages. These should be routed back to the response future of the caller.
- `{AgentType}:error={RequestId}` - Error message that corresponds to the given request.
diff --git a/python/packages/autogen-core/src/autogen_core/base/_agent_id.py b/python/packages/autogen-core/src/autogen_core/base/_agent_id.py
index 06f163ed9..9aae843eb 100644
--- a/python/packages/autogen-core/src/autogen_core/base/_agent_id.py
+++ b/python/packages/autogen-core/src/autogen_core/base/_agent_id.py
@@ -1,15 +1,21 @@
+import re
+
from typing_extensions import Self
from ._agent_type import AgentType
+def is_valid_agent_type(value: str) -> bool:
+ return bool(re.match(r"^[\w\-\.]+\Z", value))
+
+
class AgentId:
def __init__(self, type: str | AgentType, key: str) -> None:
if isinstance(type, AgentType):
type = type.type
- if type.isidentifier() is False:
- raise ValueError(f"Invalid type: {type}")
+ if not is_valid_agent_type(type):
+ raise ValueError(rf"Invalid agent type: {type}. Allowed values MUST match the regex: `^[\w\-\.]+\Z`")
self._type = type
self._key = key
diff --git a/python/packages/autogen-core/src/autogen_core/base/_topic.py b/python/packages/autogen-core/src/autogen_core/base/_topic.py
index 80c29d95f..c8965a7da 100644
--- a/python/packages/autogen-core/src/autogen_core/base/_topic.py
+++ b/python/packages/autogen-core/src/autogen_core/base/_topic.py
@@ -1,13 +1,20 @@
+import re
from dataclasses import dataclass
from typing_extensions import Self
+def is_valid_topic_type(value: str) -> bool:
+ return bool(re.match(r"^[\w\-\.\:\=]+\Z", value))
+
+
@dataclass(eq=True, frozen=True)
class TopicId:
type: str
"""Type of the event that this topic_id contains. Adhere's to the cloud event spec.
+ Must match the pattern: ^[\\w\\-\\.\\:\\=]+\\Z
+
Learn more here: https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#type
"""
@@ -17,6 +24,10 @@ class TopicId:
Learn more here: https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#source-1
"""
+ def __post_init__(self) -> None:
+ if is_valid_topic_type(self.type) is False:
+ raise ValueError(f"Invalid topic type: {self.type}. Must match the pattern: ^[\\w\\-\\.\\:\\=]+\\Z")
+
def __str__(self) -> str:
return f"{self.type}/{self.source}"