rusticai-testing 0.0.4__tar.gz → 1.0.0__tar.gz
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.
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/PKG-INFO +4 -5
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/pyproject.toml +11 -10
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/agent_ext/base_test_kvstore.py +4 -12
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/agents/sample_agents.py +1 -16
- rusticai_testing-1.0.0/src/rustic_ai/testing/agents/simple_agent.py +86 -0
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/execution/base_test_integration.py +15 -12
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/execution/integration_agents.py +2 -10
- rusticai_testing-1.0.0/src/rustic_ai/testing/helpers.py +144 -0
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/messaging/base_test_messaging.py +18 -8
- rusticai_testing-1.0.0/src/rustic_ai/testing/state/__init__.py +0 -0
- rusticai_testing-1.0.0/src/rustic_ai/testing/state/base_test_state_mgr.py +242 -0
- rusticai_testing-0.0.4/src/rustic_ai/testing/agents/simple_agent.py +0 -35
- rusticai_testing-0.0.4/src/rustic_ai/testing/helpers.py +0 -99
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/README.md +0 -0
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/__init__.py +0 -0
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/agent_ext/__init__.py +0 -0
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/agents/__init__.py +0 -0
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/execution/__init__.py +0 -0
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/messaging/__init__.py +0 -0
- {rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/messaging/base_test_backend.py +0 -0
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: rusticai-testing
|
|
3
|
-
Version: 0.0
|
|
3
|
+
Version: 1.0.0
|
|
4
4
|
Summary: Test Utilities for Rustic AI Framework
|
|
5
|
-
License: Apache-2.0
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
6
|
Author: Dragonscale Industries Inc.
|
|
7
7
|
Author-email: dev@dragonscale.ai
|
|
8
8
|
Requires-Python: >=3.12,<3.13
|
|
9
|
-
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
9
|
Classifier: Programming Language :: Python :: 3
|
|
11
10
|
Classifier: Programming Language :: Python :: 3.12
|
|
12
11
|
Requires-Dist: pytest (>=8.3.5,<9.0.0)
|
|
13
|
-
Requires-Dist: rusticai-core (>=0.0
|
|
12
|
+
Requires-Dist: rusticai-core (>=1.0.0,<1.1.0)
|
|
14
13
|
Project-URL: Homepage, https://www.rustic.ai/
|
|
15
14
|
Project-URL: Repository, https://github.com/rustic-ai/rustic-ai
|
|
16
15
|
Project-URL: Rustic AI Core, https://pypi.org/project/rusticai-core/
|
|
@@ -2,23 +2,24 @@
|
|
|
2
2
|
requires = ["poetry-core"]
|
|
3
3
|
build-backend = "poetry.core.masonry.api"
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
[project]
|
|
6
7
|
name = "rusticai-testing"
|
|
7
|
-
version = "0.0
|
|
8
|
+
version = "1.0.0"
|
|
8
9
|
description = "Test Utilities for Rustic AI Framework"
|
|
9
|
-
authors = ["Dragonscale Industries Inc.
|
|
10
|
+
authors = [{name = "Dragonscale Industries Inc.", email = "dev@dragonscale.ai"}]
|
|
10
11
|
license = "Apache-2.0"
|
|
11
12
|
readme = "README.md"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
requires-python = ">=3.12,<3.13"
|
|
14
|
+
dynamic = ["dependencies"]
|
|
15
|
+
urls = { Homepage = "https://www.rustic.ai/", Repository = "https://github.com/rustic-ai/rustic-ai", "Rustic AI Core" = "https://pypi.org/project/rusticai-core/" }
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
|
|
18
|
+
[tool.poetry]
|
|
19
|
+
packages = [{ include = "rustic_ai", from = "src" }]
|
|
18
20
|
|
|
19
21
|
[tool.poetry.dependencies]
|
|
20
|
-
|
|
21
|
-
rusticai-core = { version = "0.0.8"}
|
|
22
|
+
rusticai-core = { version = "1.0.0"}
|
|
22
23
|
pytest = "^8.3.5"
|
|
23
24
|
|
|
24
25
|
[tool.poetry-monorepo.deps]
|
|
@@ -9,8 +9,6 @@ from rustic_ai.core.guild import agent
|
|
|
9
9
|
from rustic_ai.core.guild.agent import (
|
|
10
10
|
Agent,
|
|
11
11
|
AgentDependency,
|
|
12
|
-
AgentMode,
|
|
13
|
-
AgentType,
|
|
14
12
|
ProcessContext,
|
|
15
13
|
)
|
|
16
14
|
from rustic_ai.core.guild.agent_ext.depends.kvstore.base import BaseKVStore
|
|
@@ -54,13 +52,6 @@ class KVStoreAgent(Agent):
|
|
|
54
52
|
Agent for testing KVStore dependencies
|
|
55
53
|
"""
|
|
56
54
|
|
|
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
55
|
@agent.processor(
|
|
65
56
|
KVPut,
|
|
66
57
|
depends_on=[
|
|
@@ -181,7 +172,7 @@ class BaseTestKVStore(ABC):
|
|
|
181
172
|
"""
|
|
182
173
|
raise NotImplementedError("This fixture should be overridden in subclasses.")
|
|
183
174
|
|
|
184
|
-
def test_kvstore(self,
|
|
175
|
+
def test_kvstore(self, probe_spec, dep_map: dict, org_id):
|
|
185
176
|
agent_spec: AgentSpec = (
|
|
186
177
|
AgentBuilder(KVStoreAgent).set_description("KV Store Agent").set_name("KVStoreAgent").build_spec()
|
|
187
178
|
)
|
|
@@ -191,14 +182,14 @@ class BaseTestKVStore(ABC):
|
|
|
191
182
|
.add_agent_spec(agent_spec)
|
|
192
183
|
.set_dependency_map(dep_map)
|
|
193
184
|
)
|
|
194
|
-
guild = guild_builder.launch()
|
|
185
|
+
guild = guild_builder.launch(organization_id=org_id)
|
|
195
186
|
|
|
196
187
|
if "kvstore" in dep_map:
|
|
197
188
|
guild.dependency_map["kvstore"].class_name = dep_map["kvstore"].class_name
|
|
198
189
|
else:
|
|
199
190
|
guild.dependency_map["kvstore"].class_name = InMemoryKVStoreResolver.get_qualified_class_name()
|
|
200
191
|
|
|
201
|
-
guild._add_local_agent(
|
|
192
|
+
probe_agent: ProbeAgent = guild._add_local_agent(probe_spec) # type: ignore
|
|
202
193
|
|
|
203
194
|
probe_agent.publish_dict(
|
|
204
195
|
topic="default_topic",
|
|
@@ -278,6 +269,7 @@ class BaseTestKVStore(ABC):
|
|
|
278
269
|
format=KVGet,
|
|
279
270
|
)
|
|
280
271
|
|
|
272
|
+
time.sleep(0.05)
|
|
281
273
|
messages = probe_agent.get_messages()
|
|
282
274
|
|
|
283
275
|
assert len(messages) == 3
|
{rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/agents/sample_agents.py
RENAMED
|
@@ -2,7 +2,7 @@ from typing import Generic, TypeVar
|
|
|
2
2
|
|
|
3
3
|
from pydantic import BaseModel, Field
|
|
4
4
|
|
|
5
|
-
from rustic_ai.core.guild import Agent,
|
|
5
|
+
from rustic_ai.core.guild import Agent, agent
|
|
6
6
|
from rustic_ai.core.guild.dsl import BaseAgentProps
|
|
7
7
|
|
|
8
8
|
T = TypeVar("T")
|
|
@@ -26,45 +26,30 @@ class MessageDataType(BaseModel):
|
|
|
26
26
|
|
|
27
27
|
|
|
28
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
29
|
@agent.processor(MessageDataType)
|
|
33
30
|
def handle_message(self, ctx: agent.ProcessContext[MessageDataType]):
|
|
34
31
|
print(f"Received message: {ctx.payload.data}")
|
|
35
32
|
|
|
36
33
|
|
|
37
34
|
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
35
|
@agent.processor(MessageDataType)
|
|
42
36
|
def handle_message(self, ctx: agent.ProcessContext[MessageDataType]):
|
|
43
37
|
print(f"Received message: {ctx.payload.data}")
|
|
44
38
|
|
|
45
39
|
|
|
46
40
|
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
41
|
@agent.processor(MessageDataType)
|
|
51
42
|
def handle_message(self, ctx: agent.ProcessContext[MessageDataType]):
|
|
52
43
|
print(f"Received message: {ctx.payload.data}")
|
|
53
44
|
|
|
54
45
|
|
|
55
46
|
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
47
|
@agent.processor(MessageDataType)
|
|
60
48
|
def handle_message(self, ctx: agent.ProcessContext[MessageDataType]):
|
|
61
49
|
print(f"Received message: {ctx.payload.data}")
|
|
62
50
|
|
|
63
51
|
|
|
64
52
|
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
53
|
@agent.processor(MessageDataType)
|
|
69
54
|
def handle_message(self, ctx: agent.ProcessContext[MessageDataType]):
|
|
70
55
|
print(f"Received message: {ctx.payload.data}")
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
from typing import List
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
|
|
5
|
+
from rustic_ai.core.guild import Agent, BaseAgentProps, agent
|
|
6
|
+
from rustic_ai.core.messaging.core import JsonDict
|
|
7
|
+
from rustic_ai.core.messaging.core.message import Message
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SimpleAgent(Agent):
|
|
11
|
+
def __init__(self):
|
|
12
|
+
self.received_messages: List[Message] = []
|
|
13
|
+
|
|
14
|
+
@agent.processor(JsonDict)
|
|
15
|
+
def collect_message(self, ctx: agent.ProcessContext[JsonDict]) -> None:
|
|
16
|
+
self.received_messages.append(ctx.message.model_copy(deep=True))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class SimpleAgentProps(BaseAgentProps):
|
|
20
|
+
prop1: str
|
|
21
|
+
prop2: int
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class SimpleAgentWithProps(Agent[SimpleAgentProps]):
|
|
25
|
+
def __init__(self):
|
|
26
|
+
self.received_messages: List[Message] = []
|
|
27
|
+
|
|
28
|
+
@agent.processor(JsonDict)
|
|
29
|
+
def collect_message(self, ctx: agent.ProcessContext[JsonDict]) -> None:
|
|
30
|
+
self.received_messages.append(ctx.message.model_copy(deep=True))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ErrorMessage(BaseModel):
|
|
34
|
+
error_message: str
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class SimpleErrorAgent(Agent):
|
|
38
|
+
def __init__(self):
|
|
39
|
+
self.received_messages: List[Message] = []
|
|
40
|
+
|
|
41
|
+
@agent.processor(JsonDict)
|
|
42
|
+
def error_producer(self, ctx: agent.ProcessContext[JsonDict]) -> None:
|
|
43
|
+
ctx.send_error(ErrorMessage(error_message="An error occurred"))
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class DummyMessage(BaseModel):
|
|
47
|
+
key1: str
|
|
48
|
+
key2: int
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class DummyResponseOne(BaseModel):
|
|
52
|
+
key1: str
|
|
53
|
+
key2: int
|
|
54
|
+
key3: str
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class DummyResponseTwo(BaseModel):
|
|
58
|
+
key1: str
|
|
59
|
+
key2: int
|
|
60
|
+
value1: str
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class MultiProcessAgent(Agent):
|
|
64
|
+
@agent.processor(DummyMessage)
|
|
65
|
+
def process_all_messages(self, ctx: agent.ProcessContext[DummyMessage]) -> None:
|
|
66
|
+
payload = ctx.payload
|
|
67
|
+
result = DummyResponseOne(key1=payload.key1, key2=payload.key2, key3="Agent One")
|
|
68
|
+
ctx.send(result)
|
|
69
|
+
|
|
70
|
+
@agent.processor(DummyMessage, predicate=lambda x, m: m.payload["key2"] > 10)
|
|
71
|
+
def process_filtered_messages(self, ctx: agent.ProcessContext[DummyMessage]) -> None:
|
|
72
|
+
payload = ctx.payload
|
|
73
|
+
result = DummyResponseTwo(key1=payload.key1, key2=payload.key2, value1="Agent Two")
|
|
74
|
+
ctx.send(result)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class AccountableAgent(Agent):
|
|
78
|
+
|
|
79
|
+
@agent.processor(JsonDict)
|
|
80
|
+
def custom_handler(self, ctx: agent.ProcessContext[JsonDict]) -> None:
|
|
81
|
+
try:
|
|
82
|
+
payload = DummyMessage.model_validate(ctx.payload)
|
|
83
|
+
result = DummyResponseOne(key1=payload.key1, key2=payload.key2, key3="Agent One")
|
|
84
|
+
ctx.send(result, reason="Identified payload as DummyMessage")
|
|
85
|
+
except Exception:
|
|
86
|
+
ctx.send_error(ErrorMessage(error_message="An error occurred"), reason="Cannot read payload")
|
|
@@ -28,16 +28,17 @@ class IntegrationTestABC(ABC):
|
|
|
28
28
|
|
|
29
29
|
@pytest.fixture
|
|
30
30
|
def wait_time(self) -> float:
|
|
31
|
-
return 0.
|
|
31
|
+
return 0.1
|
|
32
32
|
|
|
33
33
|
@pytest.fixture
|
|
34
|
-
def guild(self, wait_time, messaging, execution_engine, guild_id):
|
|
34
|
+
def guild(self, wait_time, messaging, execution_engine, guild_id, org_id):
|
|
35
35
|
guild = Guild(
|
|
36
36
|
id=guild_id,
|
|
37
37
|
name="Test Guild",
|
|
38
38
|
description="A guild for integration testing",
|
|
39
39
|
messaging_config=messaging,
|
|
40
40
|
execution_engine_clz=execution_engine,
|
|
41
|
+
organization_id=org_id,
|
|
41
42
|
)
|
|
42
43
|
yield guild
|
|
43
44
|
time.sleep(wait_time * 5) # Allow some time for cleanup
|
|
@@ -64,10 +65,10 @@ class IntegrationTestABC(ABC):
|
|
|
64
65
|
)
|
|
65
66
|
|
|
66
67
|
@pytest.fixture
|
|
67
|
-
def
|
|
68
|
+
def local_test_agent_spec(self) -> AgentSpec:
|
|
68
69
|
# Instantiate the local test agent with the same message bus
|
|
69
|
-
return
|
|
70
|
-
|
|
70
|
+
return (
|
|
71
|
+
AgentBuilder(LocalTestAgent)
|
|
71
72
|
.set_id("local_test")
|
|
72
73
|
.set_name("LocalTestAgent")
|
|
73
74
|
.set_description("Local test agent for integration testing")
|
|
@@ -78,7 +79,7 @@ class IntegrationTestABC(ABC):
|
|
|
78
79
|
self,
|
|
79
80
|
wait_time: float,
|
|
80
81
|
guild: Guild,
|
|
81
|
-
|
|
82
|
+
local_test_agent_spec: AgentSpec,
|
|
82
83
|
initiator_agent: AgentSpec,
|
|
83
84
|
responder_agent: AgentSpec,
|
|
84
85
|
):
|
|
@@ -103,16 +104,18 @@ class IntegrationTestABC(ABC):
|
|
|
103
104
|
rair = execution_engine.is_agent_running(guild.id, ra[0].id)
|
|
104
105
|
assert rair is True
|
|
105
106
|
|
|
106
|
-
local_exec_engine = SyncExecutionEngine(guild_id=guild.id)
|
|
107
|
-
guild._add_local_agent(
|
|
107
|
+
local_exec_engine = SyncExecutionEngine(guild_id=guild.id, organization_id=guild.organization_id)
|
|
108
|
+
local_test_agent = guild._add_local_agent(local_test_agent_spec, local_exec_engine)
|
|
109
|
+
|
|
110
|
+
# Give more time for all agents to initialize fully, especially for Ray/distributed execution
|
|
111
|
+
time.sleep(max(wait_time * 10, 1.0))
|
|
108
112
|
|
|
109
|
-
time.sleep(wait_time)
|
|
110
113
|
# Initiator sends a message to trigger communication
|
|
111
114
|
local_test_agent.publish_initial_message()
|
|
112
115
|
|
|
113
|
-
# Allow
|
|
116
|
+
# Allow more time for message processing in distributed scenarios
|
|
114
117
|
# Consider using a more sophisticated synchronization mechanism for real-world scenarios
|
|
115
|
-
time.sleep(
|
|
118
|
+
time.sleep(max(wait_time * 30, 3.0))
|
|
116
119
|
|
|
117
120
|
# Assertions to verify communication flow
|
|
118
121
|
# Ensure the local test agent captured the messages as expected
|
|
@@ -154,7 +157,7 @@ class IntegrationTestABC(ABC):
|
|
|
154
157
|
# Send a new message to ensure that the responder agent is no longer processing messages
|
|
155
158
|
local_test_agent.publish_initial_message()
|
|
156
159
|
|
|
157
|
-
time.sleep(
|
|
160
|
+
time.sleep(max(wait_time * 30, 3.0))
|
|
158
161
|
|
|
159
162
|
# Ensure that the responder agent did not process the message
|
|
160
163
|
assert len(local_test_agent.captured_messages) == 1
|
|
@@ -5,15 +5,13 @@ from pydantic import JsonValue
|
|
|
5
5
|
|
|
6
6
|
from rustic_ai.core.agents.testutils.probe_agent import PublishMixin
|
|
7
7
|
from rustic_ai.core.guild import agent
|
|
8
|
-
from rustic_ai.core.guild.agent import Agent,
|
|
9
|
-
from rustic_ai.core.guild.dsl import AgentSpec
|
|
8
|
+
from rustic_ai.core.guild.agent import Agent, ProcessContext
|
|
10
9
|
from rustic_ai.core.messaging.core import JsonDict
|
|
11
10
|
from rustic_ai.core.messaging.core.message import Message
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
class LocalTestAgent(Agent, PublishMixin):
|
|
15
|
-
def __init__(self
|
|
16
|
-
super().__init__(agent_spec=agent_spec, agent_type=AgentType.BOT, agent_mode=AgentMode.LOCAL)
|
|
14
|
+
def __init__(self):
|
|
17
15
|
self.captured_messages: List[Message] = []
|
|
18
16
|
|
|
19
17
|
@agent.processor(JsonDict)
|
|
@@ -33,9 +31,6 @@ class InitiatorProbeAgent(Agent):
|
|
|
33
31
|
An agent that initiates communication.
|
|
34
32
|
"""
|
|
35
33
|
|
|
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
34
|
@agent.processor(JsonDict, lambda me, message: message.topics == "default_topic")
|
|
40
35
|
def initiate_flow(self, ctx: ProcessContext[JsonDict]) -> None:
|
|
41
36
|
payload = ctx.payload
|
|
@@ -48,9 +43,6 @@ class ResponderProbeAgent(Agent):
|
|
|
48
43
|
An agent that responds to received messages.
|
|
49
44
|
"""
|
|
50
45
|
|
|
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
46
|
@agent.processor(JsonDict, lambda me, message: message.topics == "default_topic")
|
|
55
47
|
def ack_response(self, ctx: ProcessContext) -> None:
|
|
56
48
|
"""
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
from functools import partial
|
|
2
|
+
from typing import Dict, List, Optional, Tuple, TypeVar
|
|
3
|
+
from unittest.mock import MagicMock
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel
|
|
6
|
+
|
|
7
|
+
from rustic_ai.core.guild.agent import Agent, ProcessContext
|
|
8
|
+
from rustic_ai.core.guild.agent_ext.depends.dependency_resolver import DependencySpec
|
|
9
|
+
from rustic_ai.core.guild.builders import GuildBuilder
|
|
10
|
+
from rustic_ai.core.guild.dsl import AgentSpec
|
|
11
|
+
from rustic_ai.core.guild.execution.utils import build_agent_from_spec
|
|
12
|
+
from rustic_ai.core.messaging.core import JsonDict
|
|
13
|
+
from rustic_ai.core.messaging.core.message import (
|
|
14
|
+
Message,
|
|
15
|
+
MessageConstants,
|
|
16
|
+
ProcessEntry,
|
|
17
|
+
)
|
|
18
|
+
from rustic_ai.core.utils.basic_class_utils import get_qualified_class_name
|
|
19
|
+
from rustic_ai.core.utils.gemstone_id import GemstoneID
|
|
20
|
+
|
|
21
|
+
T = TypeVar("T", bound=Agent)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def wrap_agent_for_testing(
|
|
25
|
+
agent_spec: AgentSpec, dependency_map: Dict[str, DependencySpec] = {}
|
|
26
|
+
) -> Tuple[Agent, List[Message]]:
|
|
27
|
+
"""
|
|
28
|
+
Wrap agent for testing.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
results: List[Message] = []
|
|
32
|
+
|
|
33
|
+
class MockProcessContext(ProcessContext):
|
|
34
|
+
|
|
35
|
+
def __init__(self, agent: T, message: Message, method_name: str, payload_type: type):
|
|
36
|
+
super().__init__(agent, message, method_name, payload_type)
|
|
37
|
+
|
|
38
|
+
def send(
|
|
39
|
+
self: ProcessContext,
|
|
40
|
+
data: BaseModel,
|
|
41
|
+
new_thread: bool = False,
|
|
42
|
+
forwarding: bool = False,
|
|
43
|
+
error_message: bool = False,
|
|
44
|
+
reason: Optional[str] = None,
|
|
45
|
+
) -> List[GemstoneID]:
|
|
46
|
+
id_obj = self.agent._generate_id(self.message.priority)
|
|
47
|
+
|
|
48
|
+
self.send_dict(
|
|
49
|
+
data=data.model_dump(mode="json"),
|
|
50
|
+
format=get_qualified_class_name(data.__class__),
|
|
51
|
+
new_thread=new_thread,
|
|
52
|
+
forwarding=forwarding,
|
|
53
|
+
error_message=error_message,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
return [id_obj]
|
|
57
|
+
|
|
58
|
+
def send_dict(
|
|
59
|
+
self,
|
|
60
|
+
data: JsonDict,
|
|
61
|
+
format: str = MessageConstants.RAW_JSON_FORMAT,
|
|
62
|
+
new_thread: bool = False,
|
|
63
|
+
forwarding: bool = False,
|
|
64
|
+
error_message: bool = False,
|
|
65
|
+
reason: Optional[str] = None,
|
|
66
|
+
) -> List[GemstoneID]:
|
|
67
|
+
id_obj = self.agent._generate_id(self.message.priority)
|
|
68
|
+
|
|
69
|
+
process_entry = ProcessEntry(
|
|
70
|
+
agent=self.agent.get_agent_tag(),
|
|
71
|
+
from_topic=self.message.topic_published_to,
|
|
72
|
+
origin=self.message.id,
|
|
73
|
+
result=id_obj.to_int(),
|
|
74
|
+
processor=self.method_name,
|
|
75
|
+
to_topics=self.message.topics if isinstance(self.message.topics, list) else [self.message.topics],
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
session_state = self.get_context()
|
|
79
|
+
|
|
80
|
+
# Ensure nested payload is JSON-serializable (pydantic JsonValue). Datetime/AnyUrl -> string
|
|
81
|
+
def _coerce_jsonable(obj):
|
|
82
|
+
import datetime
|
|
83
|
+
|
|
84
|
+
from pydantic import AnyUrl
|
|
85
|
+
|
|
86
|
+
if isinstance(obj, dict):
|
|
87
|
+
return {k: _coerce_jsonable(v) for k, v in obj.items()}
|
|
88
|
+
if isinstance(obj, list):
|
|
89
|
+
return [_coerce_jsonable(v) for v in obj]
|
|
90
|
+
if isinstance(obj, (datetime.datetime, datetime.date)):
|
|
91
|
+
try:
|
|
92
|
+
return obj.isoformat()
|
|
93
|
+
except Exception:
|
|
94
|
+
return str(obj)
|
|
95
|
+
if isinstance(obj, AnyUrl):
|
|
96
|
+
return str(obj)
|
|
97
|
+
return obj
|
|
98
|
+
|
|
99
|
+
data = _coerce_jsonable(data)
|
|
100
|
+
|
|
101
|
+
msg = Message(
|
|
102
|
+
topics=self.message.topics,
|
|
103
|
+
format=format,
|
|
104
|
+
sender=self.agent.get_agent_tag(),
|
|
105
|
+
payload=data,
|
|
106
|
+
id_obj=id_obj,
|
|
107
|
+
in_response_to=self.message.id,
|
|
108
|
+
thread=self.message.thread,
|
|
109
|
+
recipient_list=[],
|
|
110
|
+
message_history=[process_entry],
|
|
111
|
+
session_state=session_state,
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
results.append(msg)
|
|
115
|
+
|
|
116
|
+
return [id_obj]
|
|
117
|
+
|
|
118
|
+
guild_spec = GuildBuilder(f"{agent_spec.id}_test_guild", "Test Guild", "Test Guild").build_spec()
|
|
119
|
+
client_class = MagicMock()
|
|
120
|
+
client_props: JsonDict = {}
|
|
121
|
+
|
|
122
|
+
agent = build_agent_from_spec(
|
|
123
|
+
agent_spec,
|
|
124
|
+
guild_spec,
|
|
125
|
+
dependency_map,
|
|
126
|
+
client_class,
|
|
127
|
+
client_props,
|
|
128
|
+
1,
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
agent._make_process_context = MagicMock() # type: ignore
|
|
132
|
+
agent._make_process_context.side_effect = lambda message, mname, payload_type, sf, ef, omf: MockProcessContext(
|
|
133
|
+
agent, message, mname, payload_type
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
def process_message(self, message: Message):
|
|
137
|
+
message.topic_published_to = message.topics[0]
|
|
138
|
+
self._original_on_message(message)
|
|
139
|
+
|
|
140
|
+
agent._original_on_message = agent._on_message # type: ignore
|
|
141
|
+
|
|
142
|
+
agent._on_message = partial(process_message, agent) # type: ignore
|
|
143
|
+
|
|
144
|
+
return agent, results
|
|
@@ -140,7 +140,7 @@ class BaseTestMessagingABC(ABC):
|
|
|
140
140
|
message_id = generator.get_id(Priority.NORMAL)
|
|
141
141
|
sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value"}, message_id)
|
|
142
142
|
|
|
143
|
-
time.sleep(
|
|
143
|
+
time.sleep(0.01) # Give the message time to be processed
|
|
144
144
|
|
|
145
145
|
assert len(messages_received) == 1
|
|
146
146
|
assert messages_received[0].payload == {"data": "value"}
|
|
@@ -168,7 +168,7 @@ class BaseTestMessagingABC(ABC):
|
|
|
168
168
|
message_id1,
|
|
169
169
|
)
|
|
170
170
|
|
|
171
|
-
time.sleep(
|
|
171
|
+
time.sleep(0.01)
|
|
172
172
|
assert len(messages_received) == 2
|
|
173
173
|
|
|
174
174
|
assert messages_received[0].payload == {"data": "value1"}
|
|
@@ -223,7 +223,7 @@ class BaseTestMessagingABC(ABC):
|
|
|
223
223
|
message_id = generator.get_id(Priority.NORMAL)
|
|
224
224
|
sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value"}, message_id)
|
|
225
225
|
|
|
226
|
-
time.sleep(
|
|
226
|
+
time.sleep(0.01) # Give the message time to be processed
|
|
227
227
|
|
|
228
228
|
assert len(messages_received1) == 1
|
|
229
229
|
assert len(messages_received2) == 1
|
|
@@ -248,7 +248,7 @@ class BaseTestMessagingABC(ABC):
|
|
|
248
248
|
sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value1"}, message_id1)
|
|
249
249
|
sender.send_message("topic2", MessageConstants.RAW_JSON_FORMAT, {"data": "value2"}, message_id2)
|
|
250
250
|
|
|
251
|
-
time.sleep(
|
|
251
|
+
time.sleep(0.01) # Give the message time to be processed
|
|
252
252
|
|
|
253
253
|
assert len(messages_received) == 2
|
|
254
254
|
assert messages_received[0].payload == {"data": "value1"}
|
|
@@ -419,7 +419,7 @@ class BaseTestMessagingABC(ABC):
|
|
|
419
419
|
message_id = generator.get_id(Priority.NORMAL)
|
|
420
420
|
sender.send_message("topic1", MessageConstants.RAW_JSON_FORMAT, {"data": "value"}, message_id)
|
|
421
421
|
|
|
422
|
-
time.sleep(
|
|
422
|
+
time.sleep(0.01) # Give the message time to be processed
|
|
423
423
|
|
|
424
424
|
assert len(messages_received) == 1
|
|
425
425
|
assert len(alt_messages_received) == 0
|
|
@@ -442,17 +442,27 @@ class BaseTestMessagingABC(ABC):
|
|
|
442
442
|
|
|
443
443
|
message_id2 = generator.get_id(Priority.NORMAL)
|
|
444
444
|
history = [
|
|
445
|
-
ProcessEntry(
|
|
445
|
+
ProcessEntry(
|
|
446
|
+
agent=AgentTag(name="test-sender"),
|
|
447
|
+
origin=message_id1.to_int(),
|
|
448
|
+
result=message_id2.to_int(),
|
|
449
|
+
processor="test_processor",
|
|
450
|
+
)
|
|
446
451
|
]
|
|
447
452
|
sender.send_message(topic, MessageConstants.RAW_JSON_FORMAT, {"data": "value2"}, message_id2, history)
|
|
448
453
|
|
|
449
454
|
message_id3 = generator.get_id(Priority.NORMAL)
|
|
450
455
|
history.append(
|
|
451
|
-
ProcessEntry(
|
|
456
|
+
ProcessEntry(
|
|
457
|
+
agent=AgentTag(name="test-sender"),
|
|
458
|
+
origin=message_id2.to_int(),
|
|
459
|
+
result=message_id3.to_int(),
|
|
460
|
+
processor="test_processor",
|
|
461
|
+
)
|
|
452
462
|
)
|
|
453
463
|
sender.send_message(topic, MessageConstants.RAW_JSON_FORMAT, {"data": "value2"}, message_id3, history, 2)
|
|
454
464
|
|
|
455
|
-
time.sleep(
|
|
465
|
+
time.sleep(0.01) # Give the message time to be processed
|
|
456
466
|
|
|
457
467
|
assert len(messages_received) == 3
|
|
458
468
|
|
|
File without changes
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from rustic_ai.core.messaging import JsonDict
|
|
6
|
+
from rustic_ai.core.state.manager.state_manager import StateManager
|
|
7
|
+
from rustic_ai.core.state.models import (
|
|
8
|
+
StateFetchRequest,
|
|
9
|
+
StateOwner,
|
|
10
|
+
StateUpdateFormat,
|
|
11
|
+
StateUpdateRequest,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class BaseTestStateManager(ABC):
|
|
16
|
+
|
|
17
|
+
@pytest.fixture
|
|
18
|
+
@abstractmethod
|
|
19
|
+
def state_manager(self, request) -> StateManager:
|
|
20
|
+
"""
|
|
21
|
+
Fixture that returns StateManager Implementation being tested.
|
|
22
|
+
This should be overridden in subclasses that test specific StateManager implementations.
|
|
23
|
+
"""
|
|
24
|
+
raise NotImplementedError("This fixture should be overridden in subclasses.")
|
|
25
|
+
|
|
26
|
+
@pytest.fixture
|
|
27
|
+
def base_state(self) -> JsonDict:
|
|
28
|
+
return {
|
|
29
|
+
"GUILD001": {
|
|
30
|
+
"state": {
|
|
31
|
+
"some": {
|
|
32
|
+
"key1": "value1",
|
|
33
|
+
"key2": "value2",
|
|
34
|
+
"nkey": {
|
|
35
|
+
"nkey1": "nvalue1",
|
|
36
|
+
"nkey2": "nvalue2",
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
"version": 0,
|
|
41
|
+
"timestamp": 0,
|
|
42
|
+
},
|
|
43
|
+
"GUILD001#AGENT001": {
|
|
44
|
+
"state": {
|
|
45
|
+
"other": {
|
|
46
|
+
"key1": "value1",
|
|
47
|
+
"key2": "value2",
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
"version": 0,
|
|
51
|
+
"timestamp": 0,
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
state_fetch_data = [
|
|
56
|
+
pytest.param(
|
|
57
|
+
StateFetchRequest(
|
|
58
|
+
state_owner=StateOwner.GUILD,
|
|
59
|
+
guild_id="GUILD001",
|
|
60
|
+
),
|
|
61
|
+
{
|
|
62
|
+
"some": {
|
|
63
|
+
"key1": "value1",
|
|
64
|
+
"key2": "value2",
|
|
65
|
+
"nkey": {
|
|
66
|
+
"nkey1": "nvalue1",
|
|
67
|
+
"nkey2": "nvalue2",
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
id="FetchGuildState",
|
|
72
|
+
),
|
|
73
|
+
pytest.param(
|
|
74
|
+
StateFetchRequest(
|
|
75
|
+
state_owner=StateOwner.GUILD,
|
|
76
|
+
guild_id="GUILD001",
|
|
77
|
+
state_path="some.key1",
|
|
78
|
+
),
|
|
79
|
+
{"key1": "value1"},
|
|
80
|
+
id="FetchGuildStatePath",
|
|
81
|
+
),
|
|
82
|
+
pytest.param(
|
|
83
|
+
StateFetchRequest(
|
|
84
|
+
state_owner=StateOwner.GUILD,
|
|
85
|
+
guild_id="GUILD001",
|
|
86
|
+
state_path="some.nkey.nkey1",
|
|
87
|
+
),
|
|
88
|
+
{"nkey1": "nvalue1"},
|
|
89
|
+
id="FetchGuildStateNestedPath",
|
|
90
|
+
),
|
|
91
|
+
pytest.param(
|
|
92
|
+
StateFetchRequest(
|
|
93
|
+
state_owner=StateOwner.AGENT,
|
|
94
|
+
guild_id="GUILD001",
|
|
95
|
+
agent_id="AGENT001",
|
|
96
|
+
),
|
|
97
|
+
{
|
|
98
|
+
"other": {
|
|
99
|
+
"key1": "value1",
|
|
100
|
+
"key2": "value2",
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
id="FetchAgentState",
|
|
104
|
+
),
|
|
105
|
+
pytest.param(
|
|
106
|
+
StateFetchRequest(
|
|
107
|
+
state_owner=StateOwner.AGENT,
|
|
108
|
+
guild_id="GUILD001",
|
|
109
|
+
agent_id="AGENT001",
|
|
110
|
+
state_path="other.key1",
|
|
111
|
+
),
|
|
112
|
+
{"key1": "value1"},
|
|
113
|
+
id="FetchAgentStatePath",
|
|
114
|
+
),
|
|
115
|
+
]
|
|
116
|
+
|
|
117
|
+
@pytest.mark.parametrize("sfr, expected_response", state_fetch_data)
|
|
118
|
+
def test_get_state(self, state_manager: StateManager, base_state: JsonDict, sfr, expected_response):
|
|
119
|
+
state_manager.load(base_state)
|
|
120
|
+
assert state_manager.get_state(sfr).state == expected_response
|
|
121
|
+
|
|
122
|
+
state_update_data = [
|
|
123
|
+
pytest.param(
|
|
124
|
+
StateUpdateRequest(
|
|
125
|
+
state_owner=StateOwner.GUILD,
|
|
126
|
+
guild_id="GUILD001",
|
|
127
|
+
update_format=StateUpdateFormat.JSON_MERGE_PATCH,
|
|
128
|
+
state_update={"some": {"key1": "new_value"}},
|
|
129
|
+
),
|
|
130
|
+
{
|
|
131
|
+
"some": {
|
|
132
|
+
"key1": "new_value",
|
|
133
|
+
"key2": "value2",
|
|
134
|
+
"nkey": {
|
|
135
|
+
"nkey1": "nvalue1",
|
|
136
|
+
"nkey2": "nvalue2",
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
id="MergePatchGuildState",
|
|
141
|
+
),
|
|
142
|
+
pytest.param(
|
|
143
|
+
StateUpdateRequest(
|
|
144
|
+
state_owner=StateOwner.GUILD,
|
|
145
|
+
guild_id="GUILD001",
|
|
146
|
+
update_format=StateUpdateFormat.JSON_MERGE_PATCH,
|
|
147
|
+
state_update={
|
|
148
|
+
"some": {
|
|
149
|
+
"key1": "new_value",
|
|
150
|
+
"key3": "value3",
|
|
151
|
+
"nkey": {
|
|
152
|
+
"nkey1": "new_nvalue1",
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
),
|
|
157
|
+
{
|
|
158
|
+
"some": {
|
|
159
|
+
"key1": "new_value",
|
|
160
|
+
"key2": "value2",
|
|
161
|
+
"key3": "value3",
|
|
162
|
+
"nkey": {
|
|
163
|
+
"nkey1": "new_nvalue1",
|
|
164
|
+
"nkey2": "nvalue2",
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
id="MergePatchGuildStateNested",
|
|
169
|
+
),
|
|
170
|
+
pytest.param(
|
|
171
|
+
StateUpdateRequest(
|
|
172
|
+
state_owner=StateOwner.GUILD,
|
|
173
|
+
guild_id="GUILD001",
|
|
174
|
+
update_format=StateUpdateFormat.JSON_PATCH,
|
|
175
|
+
state_update={
|
|
176
|
+
"operations": [
|
|
177
|
+
{"op": "replace", "path": "/some/key1", "value": "new_value"},
|
|
178
|
+
]
|
|
179
|
+
},
|
|
180
|
+
),
|
|
181
|
+
{
|
|
182
|
+
"some": {
|
|
183
|
+
"key1": "new_value",
|
|
184
|
+
"key2": "value2",
|
|
185
|
+
"nkey": {
|
|
186
|
+
"nkey1": "nvalue1",
|
|
187
|
+
"nkey2": "nvalue2",
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
id="JsonPatchGuildState",
|
|
192
|
+
),
|
|
193
|
+
pytest.param(
|
|
194
|
+
StateUpdateRequest(
|
|
195
|
+
state_owner=StateOwner.AGENT,
|
|
196
|
+
guild_id="GUILD001",
|
|
197
|
+
agent_id="AGENT001",
|
|
198
|
+
update_format=StateUpdateFormat.JSON_PATCH,
|
|
199
|
+
state_update={
|
|
200
|
+
"operations": [
|
|
201
|
+
{"op": "replace", "path": "/other/key1", "value": "new_value"},
|
|
202
|
+
{"op": "add", "path": "/other/key3", "value": "value3"},
|
|
203
|
+
]
|
|
204
|
+
},
|
|
205
|
+
),
|
|
206
|
+
{
|
|
207
|
+
"other": {
|
|
208
|
+
"key1": "new_value",
|
|
209
|
+
"key2": "value2",
|
|
210
|
+
"key3": "value3",
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
id="JsonPatchAgentState",
|
|
214
|
+
),
|
|
215
|
+
]
|
|
216
|
+
|
|
217
|
+
@pytest.mark.parametrize("sur, expected_state", state_update_data)
|
|
218
|
+
def test_update_state(
|
|
219
|
+
self,
|
|
220
|
+
state_manager: StateManager,
|
|
221
|
+
base_state: JsonDict,
|
|
222
|
+
sur: StateUpdateRequest,
|
|
223
|
+
expected_state,
|
|
224
|
+
):
|
|
225
|
+
state_manager.load(base_state)
|
|
226
|
+
updated_state = state_manager.update_state(sur)
|
|
227
|
+
|
|
228
|
+
assert updated_state.state == expected_state
|
|
229
|
+
assert updated_state.version == 1
|
|
230
|
+
|
|
231
|
+
sfr = state_manager.get_state(
|
|
232
|
+
StateFetchRequest(
|
|
233
|
+
state_owner=sur.state_owner,
|
|
234
|
+
guild_id=sur.guild_id,
|
|
235
|
+
agent_id=sur.agent_id,
|
|
236
|
+
state_path=sur.update_path,
|
|
237
|
+
version=sur.update_version,
|
|
238
|
+
timestamp=sur.update_timestamp,
|
|
239
|
+
)
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
assert sfr.state == expected_state
|
|
@@ -1,35 +0,0 @@
|
|
|
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))
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
from typing import Dict, List, Tuple, TypeVar
|
|
2
|
-
from unittest.mock import MagicMock
|
|
3
|
-
|
|
4
|
-
from pydantic import BaseModel
|
|
5
|
-
|
|
6
|
-
from rustic_ai.core.guild.agent import Agent, ProcessContext
|
|
7
|
-
from rustic_ai.core.guild.agent_ext.depends.dependency_resolver import (
|
|
8
|
-
DependencyResolver,
|
|
9
|
-
DependencySpec,
|
|
10
|
-
)
|
|
11
|
-
from rustic_ai.core.guild.builders import GuildBuilder
|
|
12
|
-
from rustic_ai.core.messaging.core import JsonDict
|
|
13
|
-
from rustic_ai.core.messaging.core.message import Message, MessageConstants
|
|
14
|
-
from rustic_ai.core.utils.basic_class_utils import get_qualified_class_name
|
|
15
|
-
from rustic_ai.core.utils.gemstone_id import GemstoneGenerator, GemstoneID
|
|
16
|
-
|
|
17
|
-
T = TypeVar("T", bound=Agent)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def wrap_agent_for_testing(
|
|
21
|
-
agent: T, generator: GemstoneGenerator, dependency_map: Dict[str, DependencySpec] = {}
|
|
22
|
-
) -> Tuple[T, List[Message]]:
|
|
23
|
-
"""
|
|
24
|
-
Wrap agent for testing.
|
|
25
|
-
"""
|
|
26
|
-
|
|
27
|
-
results: List[Message] = []
|
|
28
|
-
|
|
29
|
-
class MockProcessContext(ProcessContext):
|
|
30
|
-
|
|
31
|
-
def __init__(self, agent: T, message: Message, method_name: str, payload_type: type):
|
|
32
|
-
super().__init__(agent, message, method_name, payload_type)
|
|
33
|
-
|
|
34
|
-
def send(
|
|
35
|
-
self: ProcessContext,
|
|
36
|
-
data: BaseModel,
|
|
37
|
-
new_thread: bool = False,
|
|
38
|
-
forwarding: bool = False,
|
|
39
|
-
error_message: bool = False,
|
|
40
|
-
) -> List[GemstoneID]:
|
|
41
|
-
id_obj = self.agent._generate_id(self.message.priority)
|
|
42
|
-
msg = Message(
|
|
43
|
-
topics=self.message.topics,
|
|
44
|
-
format=get_qualified_class_name(data.__class__),
|
|
45
|
-
sender=self.agent.get_agent_tag(),
|
|
46
|
-
payload=data.model_dump(),
|
|
47
|
-
id_obj=id_obj,
|
|
48
|
-
in_response_to=self.message.id,
|
|
49
|
-
thread=self.message.thread,
|
|
50
|
-
recipient_list=[],
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
results.append(msg)
|
|
54
|
-
|
|
55
|
-
return [id_obj]
|
|
56
|
-
|
|
57
|
-
def send_dict(
|
|
58
|
-
self,
|
|
59
|
-
data: JsonDict,
|
|
60
|
-
format: str = MessageConstants.RAW_JSON_FORMAT,
|
|
61
|
-
new_thread: bool = False,
|
|
62
|
-
forwarding: bool = False,
|
|
63
|
-
error_message: bool = False,
|
|
64
|
-
) -> List[GemstoneID]:
|
|
65
|
-
id_obj = self.agent._generate_id(self.message.priority)
|
|
66
|
-
msg = Message(
|
|
67
|
-
topics=self.message.topics,
|
|
68
|
-
format=format,
|
|
69
|
-
sender=self.agent.get_agent_tag(),
|
|
70
|
-
payload=data,
|
|
71
|
-
id_obj=id_obj,
|
|
72
|
-
in_response_to=self.message.id,
|
|
73
|
-
thread=self.message.thread,
|
|
74
|
-
recipient_list=[],
|
|
75
|
-
)
|
|
76
|
-
|
|
77
|
-
results.append(msg)
|
|
78
|
-
|
|
79
|
-
return [id_obj]
|
|
80
|
-
|
|
81
|
-
agent._make_process_context = MagicMock() # type: ignore
|
|
82
|
-
agent._make_process_context.side_effect = lambda message, mname, payload_type, sf, ef, omf: MockProcessContext(
|
|
83
|
-
agent, message, mname, payload_type
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
agent._set_generator(generator)
|
|
87
|
-
|
|
88
|
-
resolvers: Dict[str, DependencyResolver] = {}
|
|
89
|
-
for dep_name, dep_spec in dependency_map.items():
|
|
90
|
-
resolver = dep_spec.to_resolver()
|
|
91
|
-
resolver.set_dependency_specs(dependency_map)
|
|
92
|
-
resolvers[dep_name] = resolver
|
|
93
|
-
|
|
94
|
-
agent._set_dependency_resolvers(resolvers)
|
|
95
|
-
|
|
96
|
-
guild_spec = GuildBuilder(f"{agent.id}_test_guild", "Test Guild", "Test Guild").build_spec()
|
|
97
|
-
agent._set_guild_spec(guild_spec)
|
|
98
|
-
|
|
99
|
-
return agent, results
|
|
File without changes
|
|
File without changes
|
{rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/agent_ext/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/execution/__init__.py
RENAMED
|
File without changes
|
{rusticai_testing-0.0.4 → rusticai_testing-1.0.0}/src/rustic_ai/testing/messaging/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|