waldiez 0.2.2__py3-none-any.whl → 0.3.0__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.
Potentially problematic release.
This version of waldiez might be problematic. Click here for more details.
- waldiez/__init__.py +2 -0
- waldiez/__main__.py +2 -0
- waldiez/_version.py +3 -1
- waldiez/cli.py +13 -3
- waldiez/cli_extras.py +4 -3
- waldiez/conflict_checker.py +4 -3
- waldiez/exporter.py +28 -105
- waldiez/exporting/__init__.py +8 -9
- waldiez/exporting/agent/__init__.py +7 -0
- waldiez/exporting/agent/agent_exporter.py +279 -0
- waldiez/exporting/agent/utils/__init__.py +23 -0
- waldiez/exporting/agent/utils/agent_class_name.py +34 -0
- waldiez/exporting/agent/utils/agent_imports.py +50 -0
- waldiez/exporting/{agents → agent/utils}/code_execution.py +9 -11
- waldiez/exporting/{agents → agent/utils}/group_manager.py +47 -35
- waldiez/exporting/{agents → agent/utils}/rag_user/__init__.py +2 -0
- waldiez/exporting/{agents → agent/utils}/rag_user/chroma_utils.py +22 -17
- waldiez/exporting/{agents → agent/utils}/rag_user/mongo_utils.py +14 -10
- waldiez/exporting/{agents → agent/utils}/rag_user/pgvector_utils.py +12 -8
- waldiez/exporting/{agents → agent/utils}/rag_user/qdrant_utils.py +11 -8
- waldiez/exporting/{agents → agent/utils}/rag_user/rag_user.py +78 -55
- waldiez/exporting/{agents → agent/utils}/rag_user/vector_db.py +10 -8
- waldiez/exporting/agent/utils/swarm_agent.py +463 -0
- waldiez/exporting/{agents → agent/utils}/teachability.py +10 -6
- waldiez/exporting/{agents → agent/utils}/termination_message.py +7 -8
- waldiez/exporting/base/__init__.py +25 -0
- waldiez/exporting/base/agent_position.py +75 -0
- waldiez/exporting/base/base_exporter.py +118 -0
- waldiez/exporting/base/export_position.py +48 -0
- waldiez/exporting/base/import_position.py +23 -0
- waldiez/exporting/base/mixin.py +134 -0
- waldiez/exporting/base/utils/__init__.py +18 -0
- waldiez/exporting/{utils → base/utils}/comments.py +12 -55
- waldiez/exporting/{utils → base/utils}/naming.py +14 -4
- waldiez/exporting/base/utils/path_check.py +68 -0
- waldiez/exporting/{utils/object_string.py → base/utils/to_string.py} +21 -20
- waldiez/exporting/chats/__init__.py +5 -12
- waldiez/exporting/chats/chats_exporter.py +240 -0
- waldiez/exporting/chats/utils/__init__.py +15 -0
- waldiez/exporting/chats/utils/common.py +81 -0
- waldiez/exporting/chats/{nested.py → utils/nested.py} +125 -86
- waldiez/exporting/chats/utils/sequential.py +244 -0
- waldiez/exporting/chats/utils/single_chat.py +313 -0
- waldiez/exporting/chats/utils/swarm.py +207 -0
- waldiez/exporting/flow/__init__.py +5 -3
- waldiez/exporting/flow/flow_exporter.py +503 -0
- waldiez/exporting/flow/utils/__init__.py +47 -0
- waldiez/exporting/flow/utils/agent_utils.py +204 -0
- waldiez/exporting/flow/utils/chat_utils.py +71 -0
- waldiez/exporting/flow/utils/def_main.py +62 -0
- waldiez/exporting/flow/utils/flow_content.py +112 -0
- waldiez/exporting/flow/utils/flow_names.py +115 -0
- waldiez/exporting/flow/utils/importing_utils.py +179 -0
- waldiez/exporting/{utils → flow/utils}/logging_utils.py +34 -31
- waldiez/exporting/models/__init__.py +7 -242
- waldiez/exporting/models/models_exporter.py +192 -0
- waldiez/exporting/models/utils.py +166 -0
- waldiez/exporting/skills/__init__.py +7 -161
- waldiez/exporting/skills/skills_exporter.py +169 -0
- waldiez/exporting/skills/utils.py +281 -0
- waldiez/models/__init__.py +25 -7
- waldiez/models/agents/__init__.py +70 -0
- waldiez/models/agents/agent/__init__.py +11 -1
- waldiez/models/agents/agent/agent.py +9 -4
- waldiez/models/agents/agent/agent_data.py +3 -1
- waldiez/models/agents/agent/code_execution.py +2 -0
- waldiez/models/agents/agent/linked_skill.py +2 -0
- waldiez/models/agents/agent/nested_chat.py +2 -0
- waldiez/models/agents/agent/teachability.py +2 -0
- waldiez/models/agents/agent/termination_message.py +49 -13
- waldiez/models/agents/agents.py +15 -3
- waldiez/models/agents/assistant/__init__.py +2 -0
- waldiez/models/agents/assistant/assistant.py +2 -0
- waldiez/models/agents/assistant/assistant_data.py +2 -0
- waldiez/models/agents/group_manager/__init__.py +9 -1
- waldiez/models/agents/group_manager/group_manager.py +2 -0
- waldiez/models/agents/group_manager/group_manager_data.py +2 -0
- waldiez/models/agents/group_manager/speakers.py +49 -13
- waldiez/models/agents/rag_user/__init__.py +21 -4
- waldiez/models/agents/rag_user/rag_user.py +3 -1
- waldiez/models/agents/rag_user/rag_user_data.py +2 -0
- waldiez/models/agents/rag_user/retrieve_config.py +268 -17
- waldiez/models/agents/rag_user/vector_db_config.py +5 -3
- waldiez/models/agents/swarm_agent/__init__.py +49 -0
- waldiez/models/agents/swarm_agent/after_work.py +178 -0
- waldiez/models/agents/swarm_agent/on_condition.py +103 -0
- waldiez/models/agents/swarm_agent/on_condition_available.py +140 -0
- waldiez/models/agents/swarm_agent/on_condition_target.py +40 -0
- waldiez/models/agents/swarm_agent/swarm_agent.py +107 -0
- waldiez/models/agents/swarm_agent/swarm_agent_data.py +125 -0
- waldiez/models/agents/swarm_agent/update_system_message.py +144 -0
- waldiez/models/agents/user_proxy/__init__.py +2 -0
- waldiez/models/agents/user_proxy/user_proxy.py +2 -0
- waldiez/models/agents/user_proxy/user_proxy_data.py +2 -0
- waldiez/models/chat/__init__.py +21 -3
- waldiez/models/chat/chat.py +241 -7
- waldiez/models/chat/chat_data.py +192 -48
- waldiez/models/chat/chat_message.py +153 -144
- waldiez/models/chat/chat_nested.py +33 -53
- waldiez/models/chat/chat_summary.py +2 -0
- waldiez/models/common/__init__.py +6 -6
- waldiez/models/common/base.py +4 -1
- waldiez/models/common/method_utils.py +163 -83
- waldiez/models/flow/__init__.py +2 -0
- waldiez/models/flow/flow.py +176 -40
- waldiez/models/flow/flow_data.py +63 -2
- waldiez/models/flow/utils.py +172 -0
- waldiez/models/model/__init__.py +2 -0
- waldiez/models/model/model.py +25 -6
- waldiez/models/model/model_data.py +3 -1
- waldiez/models/skill/__init__.py +4 -1
- waldiez/models/skill/skill.py +30 -2
- waldiez/models/skill/skill_data.py +2 -0
- waldiez/models/waldiez.py +28 -4
- waldiez/runner.py +142 -228
- waldiez/running/__init__.py +33 -0
- waldiez/running/environment.py +83 -0
- waldiez/running/gen_seq_diagram.py +185 -0
- waldiez/running/running.py +300 -0
- {waldiez-0.2.2.dist-info → waldiez-0.3.0.dist-info}/METADATA +32 -26
- waldiez-0.3.0.dist-info/RECORD +125 -0
- waldiez-0.3.0.dist-info/licenses/LICENSE +201 -0
- waldiez/exporting/agents/__init__.py +0 -5
- waldiez/exporting/agents/agent.py +0 -236
- waldiez/exporting/agents/agent_skills.py +0 -67
- waldiez/exporting/agents/llm_config.py +0 -53
- waldiez/exporting/chats/chats.py +0 -46
- waldiez/exporting/chats/helpers.py +0 -420
- waldiez/exporting/flow/def_main.py +0 -32
- waldiez/exporting/flow/flow.py +0 -189
- waldiez/exporting/utils/__init__.py +0 -36
- waldiez/exporting/utils/importing.py +0 -265
- waldiez/exporting/utils/method_utils.py +0 -35
- waldiez/exporting/utils/path_check.py +0 -51
- waldiez-0.2.2.dist-info/RECORD +0 -92
- waldiez-0.2.2.dist-info/licenses/LICENSE +0 -21
- {waldiez-0.2.2.dist-info → waldiez-0.3.0.dist-info}/WHEEL +0 -0
- {waldiez-0.2.2.dist-info → waldiez-0.3.0.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0.
|
|
2
|
+
# Copyright (c) 2024 - 2025 Waldiez and contributors.
|
|
3
|
+
# flake8: noqa E501
|
|
4
|
+
# pylint: disable=line-too-long
|
|
5
|
+
"""Chats exporter."""
|
|
6
|
+
|
|
7
|
+
from typing import Callable, Dict, List, Optional, Tuple, Union
|
|
8
|
+
|
|
9
|
+
from waldiez.models import WaldiezAgent, WaldiezChat
|
|
10
|
+
|
|
11
|
+
from ..base import (
|
|
12
|
+
AgentPosition,
|
|
13
|
+
AgentPositions,
|
|
14
|
+
BaseExporter,
|
|
15
|
+
ExporterMixin,
|
|
16
|
+
ExporterReturnType,
|
|
17
|
+
ExportPosition,
|
|
18
|
+
ExportPositions,
|
|
19
|
+
ImportPosition,
|
|
20
|
+
)
|
|
21
|
+
from .utils import (
|
|
22
|
+
export_nested_chat_registration,
|
|
23
|
+
export_sequential_chat,
|
|
24
|
+
export_single_chat,
|
|
25
|
+
export_swarm_chat,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ChatsExporter(BaseExporter, ExporterMixin):
|
|
30
|
+
"""Chats exporter."""
|
|
31
|
+
|
|
32
|
+
_chat_string: Optional[str]
|
|
33
|
+
_before_chat: Optional[str]
|
|
34
|
+
_generated: bool
|
|
35
|
+
|
|
36
|
+
def __init__(
|
|
37
|
+
self,
|
|
38
|
+
get_swarm_members: Callable[
|
|
39
|
+
[WaldiezAgent], Tuple[List[WaldiezAgent], Optional[WaldiezAgent]]
|
|
40
|
+
],
|
|
41
|
+
all_agents: List[WaldiezAgent],
|
|
42
|
+
agent_names: Dict[str, str],
|
|
43
|
+
all_chats: List[WaldiezChat],
|
|
44
|
+
chat_names: Dict[str, str],
|
|
45
|
+
main_chats: List[Tuple[WaldiezChat, WaldiezAgent, WaldiezAgent]],
|
|
46
|
+
for_notebook: bool,
|
|
47
|
+
is_async: bool,
|
|
48
|
+
):
|
|
49
|
+
"""Initialize the chats exporter.
|
|
50
|
+
|
|
51
|
+
Parameters
|
|
52
|
+
----------
|
|
53
|
+
get_swarm_members : Callable[
|
|
54
|
+
[WaldiezAgent],
|
|
55
|
+
Tuple[List[WaldiezAgent], Optional[WaldiezAgent]]
|
|
56
|
+
]
|
|
57
|
+
The function to use to resolve the swarm members.
|
|
58
|
+
all_agents : List[WaldiezAgent]
|
|
59
|
+
All the agents in the flow.
|
|
60
|
+
agent_names : Dict[str, str]
|
|
61
|
+
A mapping of agent id to agent name.
|
|
62
|
+
all_chats : List[WaldiezChat]
|
|
63
|
+
All the chats in the flow.
|
|
64
|
+
chat_names : Dict[str, str]
|
|
65
|
+
A mapping of chat id to chat name.
|
|
66
|
+
main_chats : List[Tuple[WaldiezChat, WaldiezAgent, WaldiezAgent]]
|
|
67
|
+
The main chats in the flow.
|
|
68
|
+
for_notebook : bool
|
|
69
|
+
Whether the export is for a notebook.
|
|
70
|
+
is_async : bool
|
|
71
|
+
Whether the chat is asynchronous.
|
|
72
|
+
"""
|
|
73
|
+
self.all_agents = all_agents
|
|
74
|
+
self.agent_names = agent_names
|
|
75
|
+
self.main_chats = main_chats
|
|
76
|
+
self.all_chats = all_chats
|
|
77
|
+
self.chat_names = chat_names
|
|
78
|
+
self.get_swarm_members = get_swarm_members
|
|
79
|
+
self.for_notebook = for_notebook
|
|
80
|
+
self.is_async = is_async
|
|
81
|
+
self._chat_string = None
|
|
82
|
+
self._before_chat = None
|
|
83
|
+
self._generated = False
|
|
84
|
+
|
|
85
|
+
def _export_chats(self) -> None:
|
|
86
|
+
"""Export the chats content."""
|
|
87
|
+
if len(self.main_chats) == 1:
|
|
88
|
+
main_chat = self.main_chats[0]
|
|
89
|
+
chat, sender, recipient = main_chat
|
|
90
|
+
if sender.agent_type == "swarm" or recipient.agent_type == "swarm":
|
|
91
|
+
self._chat_string, self._before_chat = export_swarm_chat(
|
|
92
|
+
get_swarm_members=self.get_swarm_members,
|
|
93
|
+
chat=chat,
|
|
94
|
+
agent_names=self.agent_names,
|
|
95
|
+
chat_names=self.chat_names,
|
|
96
|
+
sender=sender,
|
|
97
|
+
recipient=recipient,
|
|
98
|
+
serializer=self.serializer,
|
|
99
|
+
string_escape=self.string_escape,
|
|
100
|
+
tabs=0 if self.for_notebook else 1,
|
|
101
|
+
is_async=self.is_async,
|
|
102
|
+
)
|
|
103
|
+
return
|
|
104
|
+
self._chat_string, self._before_chat = export_single_chat(
|
|
105
|
+
sender=sender,
|
|
106
|
+
recipient=recipient,
|
|
107
|
+
chat=chat,
|
|
108
|
+
agent_names=self.agent_names,
|
|
109
|
+
chat_names=self.chat_names,
|
|
110
|
+
serializer=self.serializer,
|
|
111
|
+
string_escape=self.string_escape,
|
|
112
|
+
tabs=0 if self.for_notebook else 1,
|
|
113
|
+
is_async=self.is_async,
|
|
114
|
+
)
|
|
115
|
+
return
|
|
116
|
+
self._chat_string, self._before_chat = export_sequential_chat(
|
|
117
|
+
main_chats=self.main_chats,
|
|
118
|
+
agent_names=self.agent_names,
|
|
119
|
+
chat_names=self.chat_names,
|
|
120
|
+
serializer=self.serializer,
|
|
121
|
+
string_escape=self.string_escape,
|
|
122
|
+
tabs=0 if self.for_notebook else 1,
|
|
123
|
+
is_async=self.is_async,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
def get_imports(self) -> Optional[List[Tuple[str, ImportPosition]]]:
|
|
127
|
+
"""Get the imports string.
|
|
128
|
+
|
|
129
|
+
Returns
|
|
130
|
+
-------
|
|
131
|
+
str
|
|
132
|
+
The imports string.
|
|
133
|
+
"""
|
|
134
|
+
if len(self.main_chats) == 1:
|
|
135
|
+
_, sender, recipient = self.main_chats[0]
|
|
136
|
+
if sender.agent_type == "swarm" or recipient.agent_type == "swarm":
|
|
137
|
+
import_string = "from autogen import initiate_swarm_chat"
|
|
138
|
+
if self.is_async:
|
|
139
|
+
import_string = "from autogen import a_initiate_swarm_chat"
|
|
140
|
+
return [(import_string, ImportPosition.THIRD_PARTY)]
|
|
141
|
+
# no additional imports, it is `sender.initiate_chat(....)`
|
|
142
|
+
return None
|
|
143
|
+
if self.is_async:
|
|
144
|
+
import_string = (
|
|
145
|
+
"from autogen.agentchat.chat import a_initiate_chats"
|
|
146
|
+
)
|
|
147
|
+
else:
|
|
148
|
+
import_string = "from autogen.agentchat.chat import initiate_chats"
|
|
149
|
+
return [(import_string, ImportPosition.THIRD_PARTY)]
|
|
150
|
+
|
|
151
|
+
def generate(self) -> str:
|
|
152
|
+
"""Generate the chats content.
|
|
153
|
+
|
|
154
|
+
Returns
|
|
155
|
+
-------
|
|
156
|
+
str
|
|
157
|
+
The chats content.
|
|
158
|
+
"""
|
|
159
|
+
if self._generated is False:
|
|
160
|
+
self._export_chats()
|
|
161
|
+
self._generated = True
|
|
162
|
+
return self._chat_string or ""
|
|
163
|
+
|
|
164
|
+
def get_before_export(
|
|
165
|
+
self,
|
|
166
|
+
) -> Optional[List[Tuple[str, Union[ExportPosition, AgentPosition]]]]:
|
|
167
|
+
"""Generate the content before the main export.
|
|
168
|
+
|
|
169
|
+
Returns
|
|
170
|
+
-------
|
|
171
|
+
Optional[List[Tuple[str, Union[ExportPosition, AgentPosition]]]]
|
|
172
|
+
The exported content before the main export and its position.
|
|
173
|
+
"""
|
|
174
|
+
before: List[Tuple[str, Union[ExportPosition, AgentPosition]]] = []
|
|
175
|
+
if self._generated is False:
|
|
176
|
+
self._export_chats()
|
|
177
|
+
self._generated = True
|
|
178
|
+
if self._before_chat:
|
|
179
|
+
before.append(
|
|
180
|
+
(self._before_chat, ExportPosition(ExportPositions.CHATS))
|
|
181
|
+
)
|
|
182
|
+
return before
|
|
183
|
+
|
|
184
|
+
def get_after_export(
|
|
185
|
+
self,
|
|
186
|
+
) -> Optional[List[Tuple[str, Union[ExportPosition, AgentPosition]]]]:
|
|
187
|
+
"""Generate the content after the main export.
|
|
188
|
+
|
|
189
|
+
Returns
|
|
190
|
+
-------
|
|
191
|
+
Optional[List[Tuple[str, Union[ExportPosition, AgentPosition]]]]
|
|
192
|
+
The exported content after the main export and its position.
|
|
193
|
+
"""
|
|
194
|
+
after: List[Tuple[str, Union[ExportPosition, AgentPosition]]] = []
|
|
195
|
+
# not per agent, we might have references to agents not yet defined.
|
|
196
|
+
# let's use one string for all nested chat registrations
|
|
197
|
+
nested_chat_registrations = ""
|
|
198
|
+
for agent in self.all_agents:
|
|
199
|
+
if agent.agent_type != "swarm":
|
|
200
|
+
registration_string = export_nested_chat_registration(
|
|
201
|
+
agent=agent,
|
|
202
|
+
all_chats=self.all_chats,
|
|
203
|
+
chat_names=self.chat_names,
|
|
204
|
+
agent_names=self.agent_names,
|
|
205
|
+
string_escape=self.string_escape,
|
|
206
|
+
serializer=self.serializer,
|
|
207
|
+
is_async=self.is_async,
|
|
208
|
+
)
|
|
209
|
+
if registration_string:
|
|
210
|
+
nested_chat_registrations += "\n" + registration_string
|
|
211
|
+
if nested_chat_registrations:
|
|
212
|
+
# let's place it before the chats (after all agents are defined)
|
|
213
|
+
after.append(
|
|
214
|
+
(
|
|
215
|
+
nested_chat_registrations,
|
|
216
|
+
AgentPosition(None, AgentPositions.AFTER_ALL, 2),
|
|
217
|
+
)
|
|
218
|
+
)
|
|
219
|
+
return after
|
|
220
|
+
|
|
221
|
+
def export(self) -> ExporterReturnType:
|
|
222
|
+
"""Export the chats.
|
|
223
|
+
|
|
224
|
+
Returns
|
|
225
|
+
-------
|
|
226
|
+
ExporterReturnType
|
|
227
|
+
The exported chats, the imports, the before export strings,
|
|
228
|
+
the after export strings, and the environment variables.
|
|
229
|
+
"""
|
|
230
|
+
exported_string = self.generate()
|
|
231
|
+
imports = self.get_imports()
|
|
232
|
+
before_export = self.get_before_export()
|
|
233
|
+
after_export = self.get_after_export()
|
|
234
|
+
return {
|
|
235
|
+
"content": exported_string,
|
|
236
|
+
"imports": imports,
|
|
237
|
+
"before_export": before_export,
|
|
238
|
+
"after_export": after_export,
|
|
239
|
+
"environment_variables": None,
|
|
240
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0.
|
|
2
|
+
# Copyright (c) 2024 - 2025 Waldiez and contributors.
|
|
3
|
+
"""Utils for exporting chats."""
|
|
4
|
+
|
|
5
|
+
from .nested import export_nested_chat_registration
|
|
6
|
+
from .sequential import export_sequential_chat
|
|
7
|
+
from .single_chat import export_single_chat
|
|
8
|
+
from .swarm import export_swarm_chat
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"export_nested_chat_registration",
|
|
12
|
+
"export_sequential_chat",
|
|
13
|
+
"export_single_chat",
|
|
14
|
+
"export_swarm_chat",
|
|
15
|
+
]
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0.
|
|
2
|
+
# Copyright (c) 2024 - 2025 Waldiez and contributors.
|
|
3
|
+
"""Common utilities for exporting chats."""
|
|
4
|
+
|
|
5
|
+
from typing import Any, Callable, Dict, Optional, Tuple
|
|
6
|
+
|
|
7
|
+
from waldiez.models import WaldiezAgent, WaldiezChat
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def update_summary_chat_args(
|
|
11
|
+
chat_args: Dict[str, Any],
|
|
12
|
+
string_escape: Callable[[str], str],
|
|
13
|
+
) -> Dict[str, Any]:
|
|
14
|
+
"""Escape quotes in the summary args if they are strings.
|
|
15
|
+
|
|
16
|
+
Parameters
|
|
17
|
+
----------
|
|
18
|
+
chat_args : Dict[str, Any]
|
|
19
|
+
The chat arguments.
|
|
20
|
+
string_escape : Callable[[str], str]
|
|
21
|
+
The function to escape the string
|
|
22
|
+
|
|
23
|
+
Returns
|
|
24
|
+
-------
|
|
25
|
+
Dict[str, Any]
|
|
26
|
+
The chat arguments with the summary prompt escaped.
|
|
27
|
+
"""
|
|
28
|
+
if "summary_args" in chat_args and isinstance(
|
|
29
|
+
chat_args["summary_args"], dict
|
|
30
|
+
):
|
|
31
|
+
for key, value in chat_args["summary_args"].items():
|
|
32
|
+
if isinstance(value, str):
|
|
33
|
+
chat_args["summary_args"][key] = string_escape(value)
|
|
34
|
+
return chat_args
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def get_chat_message_string(
|
|
38
|
+
sender: WaldiezAgent,
|
|
39
|
+
chat: WaldiezChat,
|
|
40
|
+
chat_names: Dict[str, str],
|
|
41
|
+
string_escape: Callable[[str], str],
|
|
42
|
+
) -> Tuple[str, Optional[str]]:
|
|
43
|
+
"""Get the agent's message as a string.
|
|
44
|
+
|
|
45
|
+
Parameters
|
|
46
|
+
----------
|
|
47
|
+
sender : WaldiezAgent
|
|
48
|
+
The sender.
|
|
49
|
+
chat : WaldiezChat
|
|
50
|
+
The chat.
|
|
51
|
+
chat_names : Dict[str, str]
|
|
52
|
+
A mapping of chat id to chat name with all the chats in the flow.
|
|
53
|
+
string_escape : Callable[[str], str]
|
|
54
|
+
The function to escape the string.
|
|
55
|
+
|
|
56
|
+
Returns
|
|
57
|
+
-------
|
|
58
|
+
Tuple[str, Optional[str]]
|
|
59
|
+
If the message is a string, the message content and None.
|
|
60
|
+
If the message is a method, the method name and the method content.
|
|
61
|
+
If the message is None, 'None' and None.
|
|
62
|
+
"""
|
|
63
|
+
if (
|
|
64
|
+
not chat.message
|
|
65
|
+
or chat.message.type == "none"
|
|
66
|
+
or chat.message.content is None
|
|
67
|
+
or chat.message_content is None
|
|
68
|
+
):
|
|
69
|
+
return "None", None
|
|
70
|
+
if chat.message.type == "string":
|
|
71
|
+
return string_escape(chat.message.content), None
|
|
72
|
+
|
|
73
|
+
is_rag_with_carryover = (
|
|
74
|
+
sender.agent_type == "rag_user" and chat.message.use_carryover
|
|
75
|
+
)
|
|
76
|
+
chat_name = chat_names[chat.id]
|
|
77
|
+
function_content, function_name = chat.get_message_function(
|
|
78
|
+
name_suffix=chat_name,
|
|
79
|
+
is_rag=is_rag_with_carryover,
|
|
80
|
+
)
|
|
81
|
+
return function_name, function_content
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0.
|
|
2
|
+
# Copyright (c) 2024 - 2025 Waldiez and contributors.
|
|
3
|
+
# pylint: disable=too-many-locals
|
|
1
4
|
"""Nested chats exporting."""
|
|
2
5
|
|
|
3
|
-
from typing import Dict, List, Optional, Tuple
|
|
6
|
+
from typing import Callable, Dict, List, Optional, Tuple
|
|
4
7
|
|
|
5
8
|
from waldiez.models import (
|
|
6
9
|
WaldiezAgent,
|
|
@@ -9,8 +12,90 @@ from waldiez.models import (
|
|
|
9
12
|
WaldiezChat,
|
|
10
13
|
)
|
|
11
14
|
|
|
12
|
-
from
|
|
13
|
-
|
|
15
|
+
from .common import update_summary_chat_args
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def export_nested_chat_registration(
|
|
19
|
+
agent: WaldiezAgent,
|
|
20
|
+
all_chats: List[WaldiezChat],
|
|
21
|
+
chat_names: Dict[str, str],
|
|
22
|
+
agent_names: Dict[str, str],
|
|
23
|
+
serializer: Callable[..., str],
|
|
24
|
+
string_escape: Callable[[str], str],
|
|
25
|
+
is_async: bool,
|
|
26
|
+
) -> str:
|
|
27
|
+
"""Get the nested chat string.
|
|
28
|
+
|
|
29
|
+
Parameters
|
|
30
|
+
----------
|
|
31
|
+
agent : WaldiezAgent
|
|
32
|
+
The agent.
|
|
33
|
+
all_chats : List[WaldiezChat]
|
|
34
|
+
All the chats in the flow.
|
|
35
|
+
chat_names : Dict[str, str]
|
|
36
|
+
The chat names.
|
|
37
|
+
agent_names : Dict[str, str]
|
|
38
|
+
The agent names.
|
|
39
|
+
serializer : Callable[..., str]
|
|
40
|
+
The serializer to use to escape quotes in a string.
|
|
41
|
+
string_escape : Callable[[str], str]
|
|
42
|
+
The string escape function.
|
|
43
|
+
is_async : bool
|
|
44
|
+
Whether the chat is asynchronous.
|
|
45
|
+
|
|
46
|
+
Returns
|
|
47
|
+
-------
|
|
48
|
+
str
|
|
49
|
+
The nested chat string.
|
|
50
|
+
"""
|
|
51
|
+
if not agent.data.nested_chats:
|
|
52
|
+
return ""
|
|
53
|
+
content = ""
|
|
54
|
+
extra_contents = []
|
|
55
|
+
agent_name = agent_names[agent.id]
|
|
56
|
+
use_suffix = len(agent.data.nested_chats) > 1
|
|
57
|
+
for index, entry in enumerate(agent.data.nested_chats):
|
|
58
|
+
trigger_names = get_nested_chat_trigger_agent_names(
|
|
59
|
+
nested_chat=entry, agent_names=agent_names
|
|
60
|
+
)
|
|
61
|
+
chat_queue, extra_methods = get_nested_chat_queue(
|
|
62
|
+
nested_chat=entry,
|
|
63
|
+
agent=agent,
|
|
64
|
+
agent_names=agent_names,
|
|
65
|
+
chat_names=chat_names,
|
|
66
|
+
all_chats=all_chats,
|
|
67
|
+
serializer=serializer,
|
|
68
|
+
string_escape=string_escape,
|
|
69
|
+
)
|
|
70
|
+
if not chat_queue: # pragma: no cover
|
|
71
|
+
continue
|
|
72
|
+
extra_contents.extend(extra_methods)
|
|
73
|
+
var_name = (
|
|
74
|
+
f"{agent_name}_chat_queue_{index}"
|
|
75
|
+
if use_suffix
|
|
76
|
+
else f"{agent_name}_chat_queue"
|
|
77
|
+
)
|
|
78
|
+
content += f"{var_name} = {chat_queue}" + "\n"
|
|
79
|
+
content += f"""
|
|
80
|
+
{agent_name}.register_nested_chats(
|
|
81
|
+
trigger={trigger_names},
|
|
82
|
+
chat_queue={var_name},
|
|
83
|
+
use_async={is_async},
|
|
84
|
+
ignore_async_in_sync_chat=True,
|
|
85
|
+
)
|
|
86
|
+
"""
|
|
87
|
+
functions_string = "\n".join(sorted(extra_contents))
|
|
88
|
+
if functions_string:
|
|
89
|
+
functions_string = functions_string + "\n"
|
|
90
|
+
content = f"{functions_string}{content}"
|
|
91
|
+
return (
|
|
92
|
+
content.replace('"None"', "None")
|
|
93
|
+
.replace("'None'", "None")
|
|
94
|
+
.replace('"False"', "False")
|
|
95
|
+
.replace("'False'", "False")
|
|
96
|
+
.replace("'True'", "True")
|
|
97
|
+
.replace('"True"', "True")
|
|
98
|
+
)
|
|
14
99
|
|
|
15
100
|
|
|
16
101
|
def get_nested_chat_trigger_agent_names(
|
|
@@ -32,7 +117,8 @@ def get_nested_chat_trigger_agent_names(
|
|
|
32
117
|
The trigger agent names.
|
|
33
118
|
"""
|
|
34
119
|
agents = [agent_names[agent_id] for agent_id in nested_chat.triggered_by]
|
|
35
|
-
|
|
120
|
+
agents_string = [", ".join(agents)]
|
|
121
|
+
trigger_string = f"{agents_string}"
|
|
36
122
|
return trigger_string.replace("'", '"')
|
|
37
123
|
|
|
38
124
|
|
|
@@ -42,6 +128,8 @@ def get_nested_chat_message_string(
|
|
|
42
128
|
agent: WaldiezAgent,
|
|
43
129
|
agent_names: Dict[str, str],
|
|
44
130
|
chat_names: Dict[str, str],
|
|
131
|
+
serializer: Callable[..., str],
|
|
132
|
+
string_escape: Callable[[str], str],
|
|
45
133
|
) -> Tuple[str, Optional[str]]:
|
|
46
134
|
"""Get the nested chat message string.
|
|
47
135
|
|
|
@@ -57,6 +145,10 @@ def get_nested_chat_message_string(
|
|
|
57
145
|
A mapping of agent id to agent name.
|
|
58
146
|
chat_names : Dict[str, str]
|
|
59
147
|
A mapping of chat id to chat name.
|
|
148
|
+
serializer : Callable[..., str]
|
|
149
|
+
The function to serialize the chat arguments.
|
|
150
|
+
string_escape : Callable[[str], str]
|
|
151
|
+
The function to escape the string.
|
|
60
152
|
|
|
61
153
|
Returns
|
|
62
154
|
-------
|
|
@@ -71,16 +163,19 @@ def get_nested_chat_message_string(
|
|
|
71
163
|
if sender_id != agent.id:
|
|
72
164
|
sender_name = agent_names[sender_id]
|
|
73
165
|
recipient_name = agent_names[recipient_id]
|
|
74
|
-
chat_dict = waldiez_chat.get_chat_args()
|
|
75
|
-
chat_dict =
|
|
166
|
+
chat_dict = waldiez_chat.get_chat_args(for_queue=True)
|
|
167
|
+
chat_dict = update_summary_chat_args(chat_dict, string_escape)
|
|
76
168
|
chat_dict["recipient"] = recipient_name
|
|
77
169
|
if sender_name:
|
|
78
170
|
chat_dict["sender"] = sender_name
|
|
79
171
|
message_value, message_source = get_chat_nested_string(
|
|
80
|
-
chat=waldiez_chat,
|
|
172
|
+
chat=waldiez_chat,
|
|
173
|
+
is_reply=message.is_reply,
|
|
174
|
+
chat_names=chat_names,
|
|
175
|
+
string_escape=string_escape,
|
|
81
176
|
)
|
|
82
177
|
chat_dict["message"] = message_value
|
|
83
|
-
message_dict_str =
|
|
178
|
+
message_dict_str = serializer(chat_dict, tabs=1)
|
|
84
179
|
if message_source:
|
|
85
180
|
# it's not a string, its the name of the function
|
|
86
181
|
message_dict_str = message_dict_str.replace(
|
|
@@ -103,6 +198,8 @@ def get_nested_chat_queue(
|
|
|
103
198
|
agent_names: Dict[str, str],
|
|
104
199
|
chat_names: Dict[str, str],
|
|
105
200
|
all_chats: List[WaldiezChat],
|
|
201
|
+
serializer: Callable[..., str],
|
|
202
|
+
string_escape: Callable[[str], str],
|
|
106
203
|
) -> Tuple[str, List[str]]:
|
|
107
204
|
"""Get the nested chat queue.
|
|
108
205
|
|
|
@@ -118,11 +215,16 @@ def get_nested_chat_queue(
|
|
|
118
215
|
A mapping of chat id to chat name.
|
|
119
216
|
all_chats : List[WaldiezChat]
|
|
120
217
|
All the chats in the flow.
|
|
218
|
+
serializer : Callable[..., str]
|
|
219
|
+
The serializer to use to escape quotes in a string.
|
|
220
|
+
string_escape : Callable[[str], str]
|
|
221
|
+
The function to escape the string.
|
|
121
222
|
|
|
122
223
|
Returns
|
|
123
224
|
-------
|
|
124
225
|
Tuple[str, List[str]]
|
|
125
|
-
The nested chat queue and the methods to include
|
|
226
|
+
The nested chat queue and the methods to include
|
|
227
|
+
(methods: message string and method name if the message is a method).
|
|
126
228
|
"""
|
|
127
229
|
message_methods_to_include = []
|
|
128
230
|
chat_messages_str = "[\n"
|
|
@@ -134,6 +236,8 @@ def get_nested_chat_queue(
|
|
|
134
236
|
agent=agent,
|
|
135
237
|
agent_names=agent_names,
|
|
136
238
|
chat_names=chat_names,
|
|
239
|
+
serializer=serializer,
|
|
240
|
+
string_escape=string_escape,
|
|
137
241
|
)
|
|
138
242
|
if message_source:
|
|
139
243
|
message_methods_to_include.append(message_source)
|
|
@@ -148,6 +252,7 @@ def get_chat_nested_string(
|
|
|
148
252
|
chat: WaldiezChat,
|
|
149
253
|
is_reply: bool,
|
|
150
254
|
chat_names: Dict[str, str],
|
|
255
|
+
string_escape: Callable[[str], str],
|
|
151
256
|
) -> Tuple[str, Optional[str]]:
|
|
152
257
|
"""Get the nested chat message.
|
|
153
258
|
|
|
@@ -158,7 +263,9 @@ def get_chat_nested_string(
|
|
|
158
263
|
is_reply : bool
|
|
159
264
|
Whether to use the nested chat's reply message or not.
|
|
160
265
|
chat_names : Dict[str, str]
|
|
161
|
-
A mapping of chat id to chat name
|
|
266
|
+
A mapping of chat id to chat name.
|
|
267
|
+
string_escape : Callable[[str], str]
|
|
268
|
+
The function to escape the string.
|
|
162
269
|
|
|
163
270
|
Returns
|
|
164
271
|
-------
|
|
@@ -175,82 +282,14 @@ def get_chat_nested_string(
|
|
|
175
282
|
if not message or message.type == "none" or message.content is None:
|
|
176
283
|
return "None", None
|
|
177
284
|
if message.type == "string":
|
|
178
|
-
return
|
|
285
|
+
return string_escape(message.content), None
|
|
179
286
|
chat_name = chat_names[chat.id]
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
function_def = f"\ndef {new_function_name}({method_args}):"
|
|
184
|
-
attribute_name = "reply_content" if is_reply else "message_content"
|
|
185
|
-
function_content = getattr(chat.data.nested_chat, attribute_name)
|
|
186
|
-
return new_function_name, function_def + "\n" + function_content + "\n"
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
def export_nested_chat(
|
|
190
|
-
agent: WaldiezAgent,
|
|
191
|
-
all_chats: List[WaldiezChat],
|
|
192
|
-
chat_names: Dict[str, str],
|
|
193
|
-
agent_names: Dict[str, str],
|
|
194
|
-
) -> str:
|
|
195
|
-
"""Get the nested chat string.
|
|
196
|
-
|
|
197
|
-
Parameters
|
|
198
|
-
----------
|
|
199
|
-
agent : WaldiezAgent
|
|
200
|
-
The agent.
|
|
201
|
-
all_chats : List[WaldiezChat]
|
|
202
|
-
All the chats in the flow.
|
|
203
|
-
chat_names : Dict[str, str]
|
|
204
|
-
The chat names.
|
|
205
|
-
agent_names : Dict[str, str]
|
|
206
|
-
The agent names.
|
|
207
|
-
|
|
208
|
-
Returns
|
|
209
|
-
-------
|
|
210
|
-
str
|
|
211
|
-
The nested chat string.
|
|
212
|
-
"""
|
|
213
|
-
if not agent.data.nested_chats:
|
|
214
|
-
return ""
|
|
215
|
-
content = ""
|
|
216
|
-
extra_contents = []
|
|
217
|
-
agent_name = agent_names[agent.id]
|
|
218
|
-
use_suffix = len(agent.data.nested_chats) > 1
|
|
219
|
-
for index, entry in enumerate(agent.data.nested_chats):
|
|
220
|
-
trigger_names = get_nested_chat_trigger_agent_names(
|
|
221
|
-
nested_chat=entry, agent_names=agent_names
|
|
222
|
-
)
|
|
223
|
-
chat_queue, extra_methods = get_nested_chat_queue(
|
|
224
|
-
nested_chat=entry,
|
|
225
|
-
agent=agent,
|
|
226
|
-
agent_names=agent_names,
|
|
227
|
-
chat_names=chat_names,
|
|
228
|
-
all_chats=all_chats,
|
|
287
|
+
if is_reply:
|
|
288
|
+
function_content, function_name = chat.get_nested_chat_message_function(
|
|
289
|
+
name_suffix=chat_name
|
|
229
290
|
)
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
var_name = (
|
|
234
|
-
f"{agent_name}_chat_queue_{index}"
|
|
235
|
-
if use_suffix
|
|
236
|
-
else f"{agent_name}_chat_queue"
|
|
291
|
+
else:
|
|
292
|
+
function_content, function_name = chat.get_nested_chat_message_function(
|
|
293
|
+
name_suffix=chat_name
|
|
237
294
|
)
|
|
238
|
-
|
|
239
|
-
content += f"""\n
|
|
240
|
-
{agent_name}.register_nested_chats(
|
|
241
|
-
trigger={trigger_names},
|
|
242
|
-
chat_queue={var_name},
|
|
243
|
-
)\n
|
|
244
|
-
"""
|
|
245
|
-
functions_string = "\n".join(sorted(extra_contents))
|
|
246
|
-
if functions_string:
|
|
247
|
-
functions_string = functions_string + "\n"
|
|
248
|
-
content = f"{functions_string}{content}"
|
|
249
|
-
return (
|
|
250
|
-
content.replace('"None"', "None")
|
|
251
|
-
.replace("'None'", "None")
|
|
252
|
-
.replace('"False"', "False")
|
|
253
|
-
.replace("'False'", "False")
|
|
254
|
-
.replace("'True'", "True")
|
|
255
|
-
.replace('"True"', "True")
|
|
256
|
-
)
|
|
295
|
+
return function_name, function_content
|