rusticai-testing 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rustic_ai/testing/__init__.py +0 -0
- rustic_ai/testing/agent_ext/__init__.py +0 -0
- rustic_ai/testing/agent_ext/base_test_kvstore.py +323 -0
- rustic_ai/testing/agents/__init__.py +0 -0
- rustic_ai/testing/agents/sample_agents.py +70 -0
- rustic_ai/testing/agents/simple_agent.py +35 -0
- rustic_ai/testing/execution/__init__.py +0 -0
- rustic_ai/testing/execution/base_test_integration.py +165 -0
- rustic_ai/testing/execution/integration_agents.py +63 -0
- rustic_ai/testing/helpers.py +99 -0
- rustic_ai/testing/messaging/__init__.py +0 -0
- rustic_ai/testing/messaging/base_test_backend.py +417 -0
- rustic_ai/testing/messaging/base_test_messaging.py +417 -0
- rusticai_testing-0.0.1.dist-info/METADATA +37 -0
- rusticai_testing-0.0.1.dist-info/RECORD +16 -0
- rusticai_testing-0.0.1.dist-info/WHEEL +4 -0
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
from pydantic import BaseModel, JsonValue
|
|
6
|
+
|
|
7
|
+
from rustic_ai.core.agents.testutils.probe_agent import ProbeAgent
|
|
8
|
+
from rustic_ai.core.guild import agent
|
|
9
|
+
from rustic_ai.core.guild.agent import (
|
|
10
|
+
Agent,
|
|
11
|
+
AgentDependency,
|
|
12
|
+
AgentMode,
|
|
13
|
+
AgentType,
|
|
14
|
+
ProcessContext,
|
|
15
|
+
)
|
|
16
|
+
from rustic_ai.core.guild.agent_ext.depends.kvstore.base import BaseKVStore
|
|
17
|
+
from rustic_ai.core.guild.agent_ext.depends.kvstore.in_memory_kvstore import (
|
|
18
|
+
InMemoryKVStoreResolver,
|
|
19
|
+
)
|
|
20
|
+
from rustic_ai.core.guild.builders import AgentBuilder, GuildBuilder
|
|
21
|
+
from rustic_ai.core.guild.dsl import AgentSpec
|
|
22
|
+
from rustic_ai.core.utils.basic_class_utils import get_qualified_class_name
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class KVPut(BaseModel):
|
|
26
|
+
key: str
|
|
27
|
+
value: JsonValue
|
|
28
|
+
in_guild_store: bool = False
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class KVResponse(BaseModel):
|
|
32
|
+
key: str
|
|
33
|
+
value: JsonValue
|
|
34
|
+
method: str
|
|
35
|
+
guild_store: bool = False
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class KVGet(BaseModel):
|
|
39
|
+
key: str
|
|
40
|
+
from_guild_store: bool = False
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class KVDel(BaseModel):
|
|
44
|
+
key: str
|
|
45
|
+
from_guild_store: bool = False
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class KeyNotFound(BaseModel):
|
|
49
|
+
key: str
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class KVStoreAgent(Agent):
|
|
53
|
+
"""
|
|
54
|
+
Agent for testing KVStore dependencies
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
def __init__(self, agent_spec: AgentSpec):
|
|
58
|
+
super().__init__(
|
|
59
|
+
agent_spec,
|
|
60
|
+
agent_type=AgentType.BOT,
|
|
61
|
+
agent_mode=AgentMode.LOCAL,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
@agent.processor(
|
|
65
|
+
KVPut,
|
|
66
|
+
depends_on=[
|
|
67
|
+
"kvstore",
|
|
68
|
+
AgentDependency(
|
|
69
|
+
dependency_key="kvstore",
|
|
70
|
+
dependency_var="guild_kvstore",
|
|
71
|
+
guild_level=True,
|
|
72
|
+
),
|
|
73
|
+
],
|
|
74
|
+
)
|
|
75
|
+
def store_kv(
|
|
76
|
+
self,
|
|
77
|
+
ctx: ProcessContext[KVPut],
|
|
78
|
+
kvstore: BaseKVStore,
|
|
79
|
+
guild_kvstore: BaseKVStore,
|
|
80
|
+
):
|
|
81
|
+
"""
|
|
82
|
+
Handles messages of type KVPut
|
|
83
|
+
"""
|
|
84
|
+
data: KVPut = ctx.payload
|
|
85
|
+
if data.in_guild_store:
|
|
86
|
+
guild_kvstore.set(data.key, data.value)
|
|
87
|
+
else:
|
|
88
|
+
kvstore.set(data.key, data.value)
|
|
89
|
+
|
|
90
|
+
ctx.send(
|
|
91
|
+
KVResponse(
|
|
92
|
+
key=data.key,
|
|
93
|
+
value=data.value,
|
|
94
|
+
method="PUT",
|
|
95
|
+
guild_store=data.in_guild_store,
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
@agent.processor(
|
|
100
|
+
KVGet,
|
|
101
|
+
depends_on=[
|
|
102
|
+
"kvstore",
|
|
103
|
+
AgentDependency(
|
|
104
|
+
dependency_key="kvstore",
|
|
105
|
+
dependency_var="guild_kvstore",
|
|
106
|
+
guild_level=True,
|
|
107
|
+
),
|
|
108
|
+
],
|
|
109
|
+
)
|
|
110
|
+
def get_kv(
|
|
111
|
+
self,
|
|
112
|
+
ctx: ProcessContext[KVGet],
|
|
113
|
+
kvstore: BaseKVStore,
|
|
114
|
+
guild_kvstore: BaseKVStore,
|
|
115
|
+
):
|
|
116
|
+
"""
|
|
117
|
+
Handles messages of type KVGet
|
|
118
|
+
"""
|
|
119
|
+
data: KVGet = ctx.payload
|
|
120
|
+
if data.from_guild_store:
|
|
121
|
+
value = guild_kvstore.get(data.key)
|
|
122
|
+
else:
|
|
123
|
+
value = kvstore.get(data.key)
|
|
124
|
+
|
|
125
|
+
if value:
|
|
126
|
+
resp = KVResponse(
|
|
127
|
+
key=data.key,
|
|
128
|
+
value=value,
|
|
129
|
+
method="GET",
|
|
130
|
+
guild_store=data.from_guild_store,
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
ctx.send(resp)
|
|
134
|
+
else:
|
|
135
|
+
ctx.send(KeyNotFound(key=data.key))
|
|
136
|
+
|
|
137
|
+
@agent.processor(
|
|
138
|
+
KVDel,
|
|
139
|
+
depends_on=[
|
|
140
|
+
"kvstore",
|
|
141
|
+
AgentDependency(
|
|
142
|
+
dependency_key="kvstore",
|
|
143
|
+
dependency_var="guild_kvstore",
|
|
144
|
+
guild_level=True,
|
|
145
|
+
),
|
|
146
|
+
],
|
|
147
|
+
)
|
|
148
|
+
def del_kv(
|
|
149
|
+
self,
|
|
150
|
+
ctx: ProcessContext[KVDel],
|
|
151
|
+
kvstore: BaseKVStore,
|
|
152
|
+
guild_kvstore: BaseKVStore,
|
|
153
|
+
):
|
|
154
|
+
"""
|
|
155
|
+
Handles messages of type KVDel
|
|
156
|
+
"""
|
|
157
|
+
data: KVDel = ctx.payload
|
|
158
|
+
if data.from_guild_store:
|
|
159
|
+
guild_kvstore.delete(data.key)
|
|
160
|
+
else:
|
|
161
|
+
kvstore.delete(data.key)
|
|
162
|
+
|
|
163
|
+
ctx.send(
|
|
164
|
+
KVResponse(
|
|
165
|
+
key=data.key,
|
|
166
|
+
value=None,
|
|
167
|
+
method="DEL",
|
|
168
|
+
guild_store=data.from_guild_store,
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class BaseTestKVStore(ABC):
|
|
174
|
+
|
|
175
|
+
@pytest.fixture
|
|
176
|
+
@abstractmethod
|
|
177
|
+
def dep_map(self) -> dict:
|
|
178
|
+
"""
|
|
179
|
+
Fixture that returns dependency mapping of the storage implementation being tested.
|
|
180
|
+
This should be overridden in subclasses that test specific storage implementations.
|
|
181
|
+
"""
|
|
182
|
+
raise NotImplementedError("This fixture should be overridden in subclasses.")
|
|
183
|
+
|
|
184
|
+
def test_kvstore(self, probe_agent: ProbeAgent, dep_map: dict):
|
|
185
|
+
agent_spec: AgentSpec = (
|
|
186
|
+
AgentBuilder(KVStoreAgent).set_description("KV Store Agent").set_name("KVStoreAgent").build_spec()
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
guild_builder = (
|
|
190
|
+
GuildBuilder("test_guild", "Test Guild", "Guild to test KV Store Agent")
|
|
191
|
+
.add_agent_spec(agent_spec)
|
|
192
|
+
.set_dependency_map(dep_map)
|
|
193
|
+
)
|
|
194
|
+
guild = guild_builder.launch()
|
|
195
|
+
|
|
196
|
+
if "kvstore" in dep_map:
|
|
197
|
+
guild.dependency_map["kvstore"].class_name = dep_map["kvstore"].class_name
|
|
198
|
+
else:
|
|
199
|
+
guild.dependency_map["kvstore"].class_name = InMemoryKVStoreResolver.get_qualified_class_name()
|
|
200
|
+
|
|
201
|
+
guild._add_local_agent(probe_agent)
|
|
202
|
+
|
|
203
|
+
probe_agent.publish_dict(
|
|
204
|
+
topic="default_topic",
|
|
205
|
+
payload=KVPut(key="key1", value=42).model_dump(),
|
|
206
|
+
format=KVPut,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
time.sleep(0.5)
|
|
210
|
+
|
|
211
|
+
probe_agent.publish_dict(
|
|
212
|
+
topic="default_topic",
|
|
213
|
+
payload=KVGet(key="key1").model_dump(),
|
|
214
|
+
format=KVGet,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
time.sleep(0.5)
|
|
218
|
+
|
|
219
|
+
messages = probe_agent.get_messages()
|
|
220
|
+
|
|
221
|
+
assert len(messages) == 2
|
|
222
|
+
|
|
223
|
+
assert messages[0].payload["key"] == "key1"
|
|
224
|
+
assert messages[0].payload["value"] == 42
|
|
225
|
+
|
|
226
|
+
assert messages[1].payload["key"] == "key1"
|
|
227
|
+
assert messages[1].payload["value"] == 42
|
|
228
|
+
|
|
229
|
+
probe_agent.clear_messages()
|
|
230
|
+
|
|
231
|
+
probe_agent.publish_dict(
|
|
232
|
+
topic="default_topic",
|
|
233
|
+
payload=KVPut(key="key1", value=43, in_guild_store=True).model_dump(),
|
|
234
|
+
format=KVPut,
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
time.sleep(0.5)
|
|
238
|
+
|
|
239
|
+
probe_agent.publish_dict(
|
|
240
|
+
topic="default_topic",
|
|
241
|
+
payload=KVGet(key="key1", from_guild_store=True).model_dump(),
|
|
242
|
+
format=KVGet,
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
time.sleep(0.5)
|
|
246
|
+
|
|
247
|
+
messages = probe_agent.get_messages()
|
|
248
|
+
|
|
249
|
+
assert len(messages) == 2
|
|
250
|
+
|
|
251
|
+
assert messages[0].payload["key"] == "key1"
|
|
252
|
+
assert messages[0].payload["value"] == 43
|
|
253
|
+
|
|
254
|
+
assert messages[1].payload["key"] == "key1"
|
|
255
|
+
assert messages[1].payload["value"] == 43
|
|
256
|
+
|
|
257
|
+
probe_agent.clear_messages()
|
|
258
|
+
|
|
259
|
+
probe_agent.publish_dict(
|
|
260
|
+
topic="default_topic",
|
|
261
|
+
payload=KVDel(key="key1").model_dump(),
|
|
262
|
+
format=KVDel,
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
time.sleep(0.5)
|
|
266
|
+
|
|
267
|
+
probe_agent.publish_dict(
|
|
268
|
+
topic="default_topic",
|
|
269
|
+
payload=KVGet(key="key1").model_dump(),
|
|
270
|
+
format=KVGet,
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
time.sleep(0.5)
|
|
274
|
+
|
|
275
|
+
probe_agent.publish_dict(
|
|
276
|
+
topic="default_topic",
|
|
277
|
+
payload=KVGet(key="key1", from_guild_store=True).model_dump(),
|
|
278
|
+
format=KVGet,
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
messages = probe_agent.get_messages()
|
|
282
|
+
|
|
283
|
+
assert len(messages) == 3
|
|
284
|
+
|
|
285
|
+
assert messages[0].payload["key"] == "key1"
|
|
286
|
+
assert messages[0].payload["value"] is None
|
|
287
|
+
assert messages[0].payload["method"] == "DEL"
|
|
288
|
+
|
|
289
|
+
assert messages[1].payload["key"] == "key1"
|
|
290
|
+
assert messages[1].format == get_qualified_class_name(KeyNotFound)
|
|
291
|
+
|
|
292
|
+
assert messages[2].payload["key"] == "key1"
|
|
293
|
+
assert messages[2].payload["value"] == 43
|
|
294
|
+
|
|
295
|
+
probe_agent.clear_messages()
|
|
296
|
+
|
|
297
|
+
probe_agent.publish_dict(
|
|
298
|
+
topic="default_topic",
|
|
299
|
+
payload=KVDel(key="key1", from_guild_store=True).model_dump(),
|
|
300
|
+
format=KVDel,
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
time.sleep(0.5)
|
|
304
|
+
|
|
305
|
+
probe_agent.publish_dict(
|
|
306
|
+
topic="default_topic",
|
|
307
|
+
payload=KVGet(key="key1", from_guild_store=True).model_dump(),
|
|
308
|
+
format=KVGet,
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
time.sleep(0.5)
|
|
312
|
+
|
|
313
|
+
messages = probe_agent.get_messages()
|
|
314
|
+
|
|
315
|
+
assert len(messages) == 2
|
|
316
|
+
|
|
317
|
+
assert messages[0].payload["key"] == "key1"
|
|
318
|
+
assert messages[0].payload["value"] is None
|
|
319
|
+
|
|
320
|
+
assert messages[1].payload["key"] == "key1"
|
|
321
|
+
assert messages[1].format == get_qualified_class_name(KeyNotFound)
|
|
322
|
+
|
|
323
|
+
guild.shutdown()
|
|
File without changes
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
from typing import Generic, TypeVar
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel, Field
|
|
4
|
+
|
|
5
|
+
from rustic_ai.core.guild import Agent, AgentMode, AgentSpec, AgentType, agent
|
|
6
|
+
from rustic_ai.core.guild.dsl import BaseAgentProps
|
|
7
|
+
|
|
8
|
+
T = TypeVar("T")
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class DemoAgentProps(BaseAgentProps):
|
|
12
|
+
prop1: str = Field(default="default_value")
|
|
13
|
+
prop2: int = Field(default=1)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class SimpleClass:
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class GenericClass(Generic[T]):
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class MessageDataType(BaseModel):
|
|
25
|
+
data: str
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class DemoAgentSimple(Agent):
|
|
29
|
+
def __init__(self, agent_spec: AgentSpec):
|
|
30
|
+
super().__init__(agent_spec=agent_spec, agent_type=AgentType.BOT, agent_mode=AgentMode.LOCAL)
|
|
31
|
+
|
|
32
|
+
@agent.processor(MessageDataType)
|
|
33
|
+
def handle_message(self, ctx: agent.ProcessContext[MessageDataType]):
|
|
34
|
+
print(f"Received message: {ctx.payload.data}")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class DemoAgentGeneric(Agent[DemoAgentProps]):
|
|
38
|
+
def __init__(self, agent_spec: AgentSpec[DemoAgentProps]):
|
|
39
|
+
super().__init__(agent_spec=agent_spec, agent_type=AgentType.BOT, agent_mode=AgentMode.LOCAL)
|
|
40
|
+
|
|
41
|
+
@agent.processor(MessageDataType)
|
|
42
|
+
def handle_message(self, ctx: agent.ProcessContext[MessageDataType]):
|
|
43
|
+
print(f"Received message: {ctx.payload.data}")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class DemoAgentGenericWithoutTypedSpec(Agent[DemoAgentProps]):
|
|
47
|
+
def __init__(self, agent_spec: AgentSpec):
|
|
48
|
+
super().__init__(agent_spec=agent_spec, agent_type=AgentType.BOT, agent_mode=AgentMode.LOCAL)
|
|
49
|
+
|
|
50
|
+
@agent.processor(MessageDataType)
|
|
51
|
+
def handle_message(self, ctx: agent.ProcessContext[MessageDataType]):
|
|
52
|
+
print(f"Received message: {ctx.payload.data}")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class DemoAgentGenericWithoutTypedParams(Agent[DemoAgentProps]):
|
|
56
|
+
def __init__(self, agent_spec):
|
|
57
|
+
super().__init__(agent_spec=agent_spec, agent_type=AgentType.BOT, agent_mode=AgentMode.LOCAL)
|
|
58
|
+
|
|
59
|
+
@agent.processor(MessageDataType)
|
|
60
|
+
def handle_message(self, ctx: agent.ProcessContext[MessageDataType]):
|
|
61
|
+
print(f"Received message: {ctx.payload.data}")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class DemoAgentWithMissingGenericAnnotation(Agent):
|
|
65
|
+
def __init__(self, agent_spec: AgentSpec[DemoAgentProps]):
|
|
66
|
+
super().__init__(agent_spec=agent_spec, agent_type=AgentType.BOT, agent_mode=AgentMode.LOCAL)
|
|
67
|
+
|
|
68
|
+
@agent.processor(MessageDataType)
|
|
69
|
+
def handle_message(self, ctx: agent.ProcessContext[MessageDataType]):
|
|
70
|
+
print(f"Received message: {ctx.payload.data}")
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from typing import List
|
|
2
|
+
|
|
3
|
+
from rustic_ai.core.guild import Agent, AgentMode, AgentType, BaseAgentProps, agent
|
|
4
|
+
from rustic_ai.core.guild.dsl import AgentSpec
|
|
5
|
+
from rustic_ai.core.messaging.core import JsonDict
|
|
6
|
+
from rustic_ai.core.messaging.core.message import Message
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SimpleAgent(Agent):
|
|
10
|
+
def __init__(
|
|
11
|
+
self,
|
|
12
|
+
agent_spec: AgentSpec,
|
|
13
|
+
):
|
|
14
|
+
super().__init__(agent_spec=agent_spec, agent_type=AgentType.BOT, agent_mode=AgentMode.LOCAL)
|
|
15
|
+
self.received_messages: List[Message] = []
|
|
16
|
+
|
|
17
|
+
@agent.processor(JsonDict)
|
|
18
|
+
def collect_message(self, ctx: agent.ProcessContext[JsonDict]) -> None:
|
|
19
|
+
self.received_messages.append(ctx.message.model_copy(deep=True))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class SimpleAgentProps(BaseAgentProps):
|
|
23
|
+
prop1: str
|
|
24
|
+
prop2: int
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class SimpleAgentWithProps(Agent[SimpleAgentProps]):
|
|
28
|
+
def __init__(self, agent_spec: AgentSpec[SimpleAgentProps]):
|
|
29
|
+
super().__init__(agent_spec=agent_spec, agent_type=AgentType.BOT, agent_mode=AgentMode.LOCAL)
|
|
30
|
+
self.received_messages: List[Message] = []
|
|
31
|
+
self._props = agent_spec.props
|
|
32
|
+
|
|
33
|
+
@agent.processor(JsonDict)
|
|
34
|
+
def collect_message(self, ctx: agent.ProcessContext[JsonDict]) -> None:
|
|
35
|
+
self.received_messages.append(ctx.message.model_copy(deep=True))
|
|
File without changes
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from rustic_ai.core.guild.builders import AgentBuilder
|
|
7
|
+
from rustic_ai.core.guild.dsl import AgentSpec
|
|
8
|
+
from rustic_ai.core.guild.execution.sync.sync_exec_engine import SyncExecutionEngine
|
|
9
|
+
from rustic_ai.core.guild.guild import Guild
|
|
10
|
+
|
|
11
|
+
from .integration_agents import InitiatorProbeAgent, LocalTestAgent, ResponderProbeAgent
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class IntegrationTestABC(ABC):
|
|
15
|
+
@abstractmethod
|
|
16
|
+
@pytest.fixture
|
|
17
|
+
def messaging(self, guild_id):
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
@abstractmethod
|
|
21
|
+
@pytest.fixture
|
|
22
|
+
def execution_engine(self) -> str:
|
|
23
|
+
pass
|
|
24
|
+
|
|
25
|
+
@pytest.fixture
|
|
26
|
+
def guild_id(self) -> str:
|
|
27
|
+
return "test_guild_id"
|
|
28
|
+
|
|
29
|
+
@pytest.fixture
|
|
30
|
+
def wait_time(self) -> float:
|
|
31
|
+
return 0.01
|
|
32
|
+
|
|
33
|
+
@pytest.fixture
|
|
34
|
+
def guild(self, wait_time, messaging, execution_engine, guild_id):
|
|
35
|
+
guild = Guild(
|
|
36
|
+
id=guild_id,
|
|
37
|
+
name="Test Guild",
|
|
38
|
+
description="A guild for integration testing",
|
|
39
|
+
messaging_config=messaging,
|
|
40
|
+
execution_engine_clz=execution_engine,
|
|
41
|
+
)
|
|
42
|
+
yield guild
|
|
43
|
+
time.sleep(wait_time * 5) # Allow some time for cleanup
|
|
44
|
+
guild.shutdown()
|
|
45
|
+
|
|
46
|
+
@pytest.fixture
|
|
47
|
+
def initiator_agent(self) -> AgentSpec:
|
|
48
|
+
return (
|
|
49
|
+
AgentBuilder(InitiatorProbeAgent)
|
|
50
|
+
.set_id("initiator")
|
|
51
|
+
.set_name("Initiator Agent")
|
|
52
|
+
.set_description("Initiates communication")
|
|
53
|
+
.build_spec()
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
@pytest.fixture
|
|
57
|
+
def responder_agent(self) -> AgentSpec:
|
|
58
|
+
return (
|
|
59
|
+
AgentBuilder(ResponderProbeAgent)
|
|
60
|
+
.set_id("responder")
|
|
61
|
+
.set_name("Responder Agent")
|
|
62
|
+
.set_description("Responds to communication")
|
|
63
|
+
.build_spec()
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
@pytest.fixture
|
|
67
|
+
def local_test_agent(self) -> LocalTestAgent:
|
|
68
|
+
# Instantiate the local test agent with the same message bus
|
|
69
|
+
return LocalTestAgent(
|
|
70
|
+
agent_spec=AgentBuilder(LocalTestAgent)
|
|
71
|
+
.set_id("local_test")
|
|
72
|
+
.set_name("LocalTestAgent")
|
|
73
|
+
.set_description("Local test agent for integration testing")
|
|
74
|
+
.build_spec()
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
def test_agents_communication(
|
|
78
|
+
self,
|
|
79
|
+
wait_time: float,
|
|
80
|
+
guild: Guild,
|
|
81
|
+
local_test_agent: LocalTestAgent,
|
|
82
|
+
initiator_agent: AgentSpec,
|
|
83
|
+
responder_agent: AgentSpec,
|
|
84
|
+
):
|
|
85
|
+
execution_engine = guild._get_execution_engine()
|
|
86
|
+
# Add both agents to the guild
|
|
87
|
+
guild.launch_agent(initiator_agent)
|
|
88
|
+
guild.launch_agent(responder_agent)
|
|
89
|
+
|
|
90
|
+
# Test if the agent is in the execution engine
|
|
91
|
+
agents = execution_engine.get_agents_in_guild(guild.id)
|
|
92
|
+
assert len(agents) == 2
|
|
93
|
+
|
|
94
|
+
ia = execution_engine.find_agents_by_name(guild.id, "Initiator Agent")
|
|
95
|
+
assert len(ia) == 1
|
|
96
|
+
|
|
97
|
+
iair = execution_engine.is_agent_running(guild.id, ia[0].id)
|
|
98
|
+
assert iair is True
|
|
99
|
+
|
|
100
|
+
ra = execution_engine.find_agents_by_name(guild.id, "Responder Agent")
|
|
101
|
+
assert len(ra) == 1
|
|
102
|
+
|
|
103
|
+
rair = execution_engine.is_agent_running(guild.id, ra[0].id)
|
|
104
|
+
assert rair is True
|
|
105
|
+
|
|
106
|
+
local_exec_engine = SyncExecutionEngine(guild_id=guild.id)
|
|
107
|
+
guild._add_local_agent(local_test_agent, local_exec_engine)
|
|
108
|
+
|
|
109
|
+
time.sleep(wait_time)
|
|
110
|
+
# Initiator sends a message to trigger communication
|
|
111
|
+
local_test_agent.publish_initial_message()
|
|
112
|
+
|
|
113
|
+
# Allow some time for message processing - adjust as necessary for your environment
|
|
114
|
+
# Consider using a more sophisticated synchronization mechanism for real-world scenarios
|
|
115
|
+
time.sleep(2)
|
|
116
|
+
|
|
117
|
+
# Assertions to verify communication flow
|
|
118
|
+
# Ensure the local test agent captured the messages as expected
|
|
119
|
+
assert len(local_test_agent.captured_messages) == 2
|
|
120
|
+
|
|
121
|
+
msg0 = local_test_agent.captured_messages[0]
|
|
122
|
+
msg1 = local_test_agent.captured_messages[1]
|
|
123
|
+
|
|
124
|
+
if msg0.sender.id == "initiator":
|
|
125
|
+
first_msg = msg0
|
|
126
|
+
second_msg = msg1
|
|
127
|
+
else:
|
|
128
|
+
first_msg = msg1
|
|
129
|
+
second_msg = msg0
|
|
130
|
+
|
|
131
|
+
assert first_msg.topics == "default_topic"
|
|
132
|
+
assert first_msg.payload.get("content", "") == "Hello Responder!"
|
|
133
|
+
assert first_msg.sender.id == "initiator"
|
|
134
|
+
assert second_msg.topics == "default_topic"
|
|
135
|
+
assert second_msg.payload.get("content", "") == f"Acknowledged: {first_msg.id}"
|
|
136
|
+
assert second_msg.sender.id == "responder"
|
|
137
|
+
|
|
138
|
+
# Clear the messages for the next test
|
|
139
|
+
local_test_agent.clear_messages()
|
|
140
|
+
|
|
141
|
+
# Remove responder agent
|
|
142
|
+
guild.remove_agent(ra[0].id)
|
|
143
|
+
|
|
144
|
+
# Test if the agent is removed from the execution engine
|
|
145
|
+
agents2 = execution_engine.get_agents_in_guild(guild.id)
|
|
146
|
+
assert len(agents2) >= 1 # Initiator agent and local test agent (in case of local execution engine)
|
|
147
|
+
|
|
148
|
+
ra2 = execution_engine.find_agents_by_name(guild.id, "Responder Agent")
|
|
149
|
+
assert len(ra2) == 0
|
|
150
|
+
|
|
151
|
+
rair2 = execution_engine.is_agent_running(guild.id, ra[0].id)
|
|
152
|
+
assert rair2 is False
|
|
153
|
+
|
|
154
|
+
# Send a new message to ensure that the responder agent is no longer processing messages
|
|
155
|
+
local_test_agent.publish_initial_message()
|
|
156
|
+
|
|
157
|
+
time.sleep(2)
|
|
158
|
+
|
|
159
|
+
# Ensure that the responder agent did not process the message
|
|
160
|
+
assert len(local_test_agent.captured_messages) == 1
|
|
161
|
+
|
|
162
|
+
msgX = local_test_agent.captured_messages[0]
|
|
163
|
+
assert msgX.topics == "default_topic"
|
|
164
|
+
assert msgX.payload.get("content", "") == "Hello Responder!"
|
|
165
|
+
assert msgX.sender.id == "initiator"
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import Dict, List
|
|
3
|
+
|
|
4
|
+
from pydantic import JsonValue
|
|
5
|
+
|
|
6
|
+
from rustic_ai.core.agents.testutils.probe_agent import PublishMixin
|
|
7
|
+
from rustic_ai.core.guild import agent
|
|
8
|
+
from rustic_ai.core.guild.agent import Agent, AgentMode, AgentType, ProcessContext
|
|
9
|
+
from rustic_ai.core.guild.dsl import AgentSpec
|
|
10
|
+
from rustic_ai.core.messaging.core import JsonDict
|
|
11
|
+
from rustic_ai.core.messaging.core.message import Message
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class LocalTestAgent(Agent, PublishMixin):
|
|
15
|
+
def __init__(self, agent_spec: AgentSpec):
|
|
16
|
+
super().__init__(agent_spec=agent_spec, agent_type=AgentType.BOT, agent_mode=AgentMode.LOCAL)
|
|
17
|
+
self.captured_messages: List[Message] = []
|
|
18
|
+
|
|
19
|
+
@agent.processor(JsonDict)
|
|
20
|
+
def collect_message(self, ctx: agent.ProcessContext[JsonDict]) -> None:
|
|
21
|
+
self.captured_messages.append(ctx.message.model_copy(deep=True))
|
|
22
|
+
|
|
23
|
+
def publish_initial_message(self):
|
|
24
|
+
# Publish the initial message to trigger communication between remote agents
|
|
25
|
+
self.publish_dict("default_topic", {"content": "Initiate Flow"})
|
|
26
|
+
|
|
27
|
+
def clear_messages(self):
|
|
28
|
+
self.captured_messages.clear()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class InitiatorProbeAgent(Agent):
|
|
32
|
+
"""
|
|
33
|
+
An agent that initiates communication.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(self, agent_spec: AgentSpec) -> None:
|
|
37
|
+
super().__init__(agent_spec=agent_spec, agent_type=AgentType.BOT, agent_mode=AgentMode.LOCAL)
|
|
38
|
+
|
|
39
|
+
@agent.processor(JsonDict, lambda me, message: message.topics == "default_topic")
|
|
40
|
+
def initiate_flow(self, ctx: ProcessContext[JsonDict]) -> None:
|
|
41
|
+
payload = ctx.payload
|
|
42
|
+
if payload["content"] == "Initiate Flow":
|
|
43
|
+
ctx.send_dict({"content": "Hello Responder!"})
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class ResponderProbeAgent(Agent):
|
|
47
|
+
"""
|
|
48
|
+
An agent that responds to received messages.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def __init__(self, agent_spec: AgentSpec) -> None:
|
|
52
|
+
super().__init__(agent_spec=agent_spec, agent_type=AgentType.BOT, agent_mode=AgentMode.LOCAL)
|
|
53
|
+
|
|
54
|
+
@agent.processor(JsonDict, lambda me, message: message.topics == "default_topic")
|
|
55
|
+
def ack_response(self, ctx: ProcessContext) -> None:
|
|
56
|
+
"""
|
|
57
|
+
Handles incoming messages by responding to them.
|
|
58
|
+
"""
|
|
59
|
+
payload = ctx.payload
|
|
60
|
+
logging.info(f"Received message: {ctx.message}\n")
|
|
61
|
+
if payload["content"] == "Hello Responder!":
|
|
62
|
+
response_content: Dict[str, JsonValue] = {"content": "Acknowledged: " + str(ctx.message.id)}
|
|
63
|
+
ctx.send_dict(response_content)
|