Skip to content

Commit b0aa587

Browse files
authored
Merge pull request #165 from RanaElwaseef21/feature/fixing-agent-name
fixing agent key to accept numbers - issue #80
2 parents 8811d7c + d714989 commit b0aa587

File tree

4 files changed

+80
-14
lines changed

4 files changed

+80
-14
lines changed

python/src/multi_agent_orchestrator/agents/agent.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from multi_agent_orchestrator.types import ConversationMessage
55
from multi_agent_orchestrator.utils import Logger
66

7+
78
@dataclass
89
class AgentProcessingResult:
910
user_input: str
@@ -13,6 +14,7 @@ class AgentProcessingResult:
1314
session_id: str
1415
additional_params: Dict[str, any] = field(default_factory=dict)
1516

17+
1618
@dataclass
1719
class AgentResponse:
1820
metadata: AgentProcessingResult
@@ -25,6 +27,7 @@ def on_llm_new_token(self, token: str) -> None:
2527
# Default implementation
2628
pass
2729

30+
2831
@dataclass
2932
class AgentOptions:
3033
name: str
@@ -44,18 +47,25 @@ def __init__(self, options: AgentOptions):
4447
self.id = self.generate_key_from_name(options.name)
4548
self.description = options.description
4649
self.save_chat = options.save_chat
47-
self.callbacks = options.callbacks if options.callbacks is not None else AgentCallbacks()
48-
self.LOG_AGENT_DEBUG_TRACE = options.LOG_AGENT_DEBUG_TRACE if options.LOG_AGENT_DEBUG_TRACE is not None else False
50+
self.callbacks = (
51+
options.callbacks if options.callbacks is not None else AgentCallbacks()
52+
)
53+
self.LOG_AGENT_DEBUG_TRACE = (
54+
options.LOG_AGENT_DEBUG_TRACE
55+
if options.LOG_AGENT_DEBUG_TRACE is not None
56+
else False
57+
)
4958

5059
def is_streaming_enabled(self) -> bool:
5160
return False
5261

5362
@staticmethod
5463
def generate_key_from_name(name: str) -> str:
5564
import re
65+
5666
# Remove special characters and replace spaces with hyphens
57-
key = re.sub(r'[^a-zA-Z\s-]', '', name)
58-
key = re.sub(r'\s+', '-', key)
67+
key = re.sub(r"[^a-zA-Z0-9\s-]", "", name)
68+
key = re.sub(r"\s+", "-", key)
5969
return key.lower()
6070

6171
@abstractmethod
@@ -65,7 +75,7 @@ async def process_request(
6575
user_id: str,
6676
session_id: str,
6777
chat_history: List[ConversationMessage],
68-
additional_params: Optional[Dict[str, str]] = None
78+
additional_params: Optional[Dict[str, str]] = None,
6979
) -> Union[ConversationMessage, AsyncIterable[any]]:
7080
pass
7181

python/src/tests/agents/test_agent.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
from typing import Dict, List
33
from unittest.mock import Mock
44
from multi_agent_orchestrator.types import ConversationMessage
5-
from multi_agent_orchestrator.agents import AgentProcessingResult, AgentResponse, AgentCallbacks, AgentOptions, Agent
5+
from multi_agent_orchestrator.agents import (
6+
AgentProcessingResult,
7+
AgentResponse,
8+
AgentCallbacks,
9+
AgentOptions,
10+
Agent,
11+
)
12+
613

714
class TestAgent:
815
@pytest.fixture
@@ -13,7 +20,7 @@ def mock_agent_options(self):
1320
model_id="test-model",
1421
region="us-west-2",
1522
save_chat=True,
16-
callbacks=None
23+
callbacks=None,
1724
)
1825

1926
@pytest.fixture
@@ -25,7 +32,7 @@ async def process_request(
2532
user_id: str,
2633
session_id: str,
2734
chat_history: List[ConversationMessage],
28-
additional_params: Dict[str, str] = None
35+
additional_params: Dict[str, str] = None,
2936
):
3037
return ConversationMessage(role="assistant", content="Mock response")
3138

@@ -37,7 +44,7 @@ def test_agent_processing_result(self):
3744
agent_id="test-agent",
3845
agent_name="Test Agent",
3946
user_id="user123",
40-
session_id="session456"
47+
session_id="session456",
4148
)
4249
assert result.user_input == "Hello"
4350
assert result.agent_id == "test-agent"
@@ -53,9 +60,11 @@ def test_agent_response(self):
5360
agent_id="test-agent",
5461
agent_name="Test Agent",
5562
user_id="user123",
56-
session_id="session456"
63+
session_id="session456",
64+
)
65+
response = AgentResponse(
66+
metadata=metadata, output="Hello, user!", streaming=False
5767
)
58-
response = AgentResponse(metadata=metadata, output="Hello, user!", streaming=False)
5968
assert response.metadata == metadata
6069
assert response.output == "Hello, user!"
6170
assert response.streaming is False
@@ -82,6 +91,17 @@ def test_agent_initialization(self, mock_agent, mock_agent_options):
8291
def test_generate_key_from_name(self):
8392
assert Agent.generate_key_from_name("Test Agent") == "test-agent"
8493
assert Agent.generate_key_from_name("Complex Name! @#$%") == "complex-name-"
94+
assert Agent.generate_key_from_name("Agent123") == "agent123"
95+
assert Agent.generate_key_from_name("Agent2-test") == "agent2-test"
96+
assert Agent.generate_key_from_name("Agent4-test") == "agent4-test"
97+
assert Agent.generate_key_from_name("Agent 123!") == "agent-123"
98+
assert Agent.generate_key_from_name("Agent@#$%^&*()") == "agent"
99+
assert Agent.generate_key_from_name("Trailing Space ") == "trailing-space-"
100+
assert (
101+
Agent.generate_key_from_name("123 Mixed Content 456!")
102+
== "123-mixed-content-456"
103+
)
104+
assert Agent.generate_key_from_name("Mix@of123Symbols$") == "mixof123symbols"
85105

86106
@pytest.mark.asyncio
87107
async def test_process_request(self, mock_agent):
@@ -90,8 +110,8 @@ async def test_process_request(self, mock_agent):
90110
input_text="Hi",
91111
user_id="user123",
92112
session_id="session456",
93-
chat_history=chat_history
113+
chat_history=chat_history,
94114
)
95115
assert isinstance(result, ConversationMessage)
96116
assert result.role == "assistant"
97-
assert result.content == "Mock response"
117+
assert result.content == "Mock response"

typescript/src/agents/agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export abstract class Agent {
115115
private generateKeyFromName(name: string): string {
116116
// Remove special characters and replace spaces with hyphens
117117
const key = name
118-
.replace(/[^a-zA-Z\s-]/g, "")
118+
.replace(/[^a-zA-Z0-9\s-]/g, "")
119119
.replace(/\s+/g, "-")
120120
.toLowerCase();
121121
return key;

typescript/tests/agents/Agents.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,42 @@ describe('Agents', () => {
1515
const key = agent['generateKeyFromName']('UPPERCASE');
1616
expect(key).toBe('uppercase');
1717
});
18+
19+
it('should convert the key to lowercase and keep the numbers', () => {
20+
const agent = new MockAgent({ name: 'Test Agent', description: 'Test description' });
21+
const key = agent['generateKeyFromName']('Agent123');
22+
expect(key).toBe('agent123');
23+
});
24+
25+
it('should convert the key to lowercase', () => {
26+
const agent = new MockAgent({ name: 'Test Agent', description: 'Test description' });
27+
const key = agent['generateKeyFromName']('Agent2-test');
28+
expect(key).toBe('agent2-test');
29+
});
30+
31+
it('should handle multiple spaces', () => {
32+
const agent = new MockAgent({ name: 'Test Agent', description: 'Test description' });
33+
const key = agent['generateKeyFromName']('Agent 123!');
34+
expect(key).toBe('agent-123');
35+
});
36+
37+
it('should remove special characters', () => {
38+
const agent = new MockAgent({ name: 'Test Agent', description: 'Test description' });
39+
const key = agent['generateKeyFromName']('Agent@#$%^&*()');
40+
expect(key).toBe('agent');
41+
});
42+
43+
it('should handle mixed content with numbers', () => {
44+
const agent = new MockAgent({ name: 'Test Agent', description: 'Test description' });
45+
const key = agent['generateKeyFromName']('123 Mixed Content 456!');
46+
expect(key).toBe('123-mixed-content-456');
47+
});
48+
49+
it('should remove special characters from mixed symbols', () => {
50+
const agent = new MockAgent({ name: 'Test Agent', description: 'Test description' });
51+
const key = agent['generateKeyFromName']('Mix@of123Symbols$');
52+
expect(key).toBe('mixof123symbols');
53+
});
1854
});
1955

2056
describe('constructor', () => {

0 commit comments

Comments
 (0)