waldiez 0.4.6__py3-none-any.whl → 0.4.8__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.

Files changed (244) hide show
  1. waldiez/__init__.py +5 -5
  2. waldiez/_version.py +1 -1
  3. waldiez/cli.py +112 -73
  4. waldiez/exporter.py +61 -19
  5. waldiez/exporting/__init__.py +25 -6
  6. waldiez/exporting/agent/__init__.py +7 -3
  7. waldiez/exporting/agent/code_execution.py +114 -0
  8. waldiez/exporting/agent/exporter.py +354 -0
  9. waldiez/exporting/agent/extras/__init__.py +15 -0
  10. waldiez/exporting/agent/extras/captain_agent_extras.py +315 -0
  11. waldiez/exporting/agent/extras/group/target.py +178 -0
  12. waldiez/exporting/agent/extras/group_manager_agent_extas.py +500 -0
  13. waldiez/exporting/agent/extras/group_member_extras.py +181 -0
  14. waldiez/exporting/agent/extras/handoffs/__init__.py +19 -0
  15. waldiez/exporting/agent/extras/handoffs/after_work.py +78 -0
  16. waldiez/exporting/agent/extras/handoffs/available.py +74 -0
  17. waldiez/exporting/agent/extras/handoffs/condition.py +158 -0
  18. waldiez/exporting/agent/extras/handoffs/handoff.py +171 -0
  19. waldiez/exporting/agent/extras/handoffs/target.py +189 -0
  20. waldiez/exporting/agent/extras/rag/__init__.py +10 -0
  21. waldiez/exporting/agent/{utils/rag_user/chroma_utils.py → extras/rag/chroma_extras.py} +16 -15
  22. waldiez/exporting/agent/{utils/rag_user/mongo_utils.py → extras/rag/mongo_extras.py} +10 -10
  23. waldiez/exporting/agent/{utils/rag_user/pgvector_utils.py → extras/rag/pgvector_extras.py} +13 -13
  24. waldiez/exporting/agent/{utils/rag_user/qdrant_utils.py → extras/rag/qdrant_extras.py} +13 -13
  25. waldiez/exporting/agent/{utils/rag_user/vector_db.py → extras/rag/vector_db_extras.py} +59 -46
  26. waldiez/exporting/agent/extras/rag_user_proxy_agent_extras.py +245 -0
  27. waldiez/exporting/agent/extras/reasoning_agent_extras.py +88 -0
  28. waldiez/exporting/agent/factory.py +95 -0
  29. waldiez/exporting/agent/processor.py +150 -0
  30. waldiez/exporting/agent/system_message.py +36 -0
  31. waldiez/exporting/agent/termination.py +50 -0
  32. waldiez/exporting/chats/__init__.py +7 -3
  33. waldiez/exporting/chats/exporter.py +97 -0
  34. waldiez/exporting/chats/factory.py +65 -0
  35. waldiez/exporting/chats/processor.py +226 -0
  36. waldiez/exporting/chats/utils/__init__.py +6 -5
  37. waldiez/exporting/chats/utils/common.py +11 -45
  38. waldiez/exporting/chats/utils/group.py +55 -0
  39. waldiez/exporting/chats/utils/nested.py +37 -52
  40. waldiez/exporting/chats/utils/sequential.py +72 -61
  41. waldiez/exporting/chats/utils/{single_chat.py → single.py} +48 -50
  42. waldiez/exporting/core/__init__.py +196 -0
  43. waldiez/exporting/core/constants.py +17 -0
  44. waldiez/exporting/core/content.py +69 -0
  45. waldiez/exporting/core/context.py +244 -0
  46. waldiez/exporting/core/enums.py +89 -0
  47. waldiez/exporting/core/errors.py +19 -0
  48. waldiez/exporting/core/exporter.py +390 -0
  49. waldiez/exporting/core/exporters.py +67 -0
  50. waldiez/exporting/core/extras/__init__.py +39 -0
  51. waldiez/exporting/core/extras/agent_extras/__init__.py +27 -0
  52. waldiez/exporting/core/extras/agent_extras/captain_extras.py +57 -0
  53. waldiez/exporting/core/extras/agent_extras/group_manager_extras.py +102 -0
  54. waldiez/exporting/core/extras/agent_extras/rag_user_extras.py +53 -0
  55. waldiez/exporting/core/extras/agent_extras/reasoning_extras.py +68 -0
  56. waldiez/exporting/core/extras/agent_extras/standard_extras.py +263 -0
  57. waldiez/exporting/core/extras/base.py +241 -0
  58. waldiez/exporting/core/extras/chat_extras.py +118 -0
  59. waldiez/exporting/core/extras/flow_extras.py +70 -0
  60. waldiez/exporting/core/extras/model_extras.py +73 -0
  61. waldiez/exporting/core/extras/path_resolver.py +93 -0
  62. waldiez/exporting/core/extras/serializer.py +138 -0
  63. waldiez/exporting/core/extras/tool_extras.py +82 -0
  64. waldiez/exporting/core/protocols.py +259 -0
  65. waldiez/exporting/core/result.py +705 -0
  66. waldiez/exporting/core/types.py +329 -0
  67. waldiez/exporting/core/utils/__init__.py +11 -0
  68. waldiez/exporting/core/utils/comment.py +33 -0
  69. waldiez/exporting/core/utils/llm_config.py +117 -0
  70. waldiez/exporting/core/validation.py +96 -0
  71. waldiez/exporting/flow/__init__.py +6 -2
  72. waldiez/exporting/flow/execution_generator.py +193 -0
  73. waldiez/exporting/flow/exporter.py +107 -0
  74. waldiez/exporting/flow/factory.py +94 -0
  75. waldiez/exporting/flow/file_generator.py +214 -0
  76. waldiez/exporting/flow/merger.py +387 -0
  77. waldiez/exporting/flow/orchestrator.py +411 -0
  78. waldiez/exporting/flow/utils/__init__.py +9 -36
  79. waldiez/exporting/flow/utils/common.py +206 -0
  80. waldiez/exporting/flow/utils/importing.py +373 -0
  81. waldiez/exporting/flow/utils/linting.py +200 -0
  82. waldiez/exporting/flow/utils/{logging_utils.py → logging.py} +23 -9
  83. waldiez/exporting/models/__init__.py +3 -1
  84. waldiez/exporting/models/exporter.py +233 -0
  85. waldiez/exporting/models/factory.py +66 -0
  86. waldiez/exporting/models/processor.py +139 -0
  87. waldiez/exporting/tools/__init__.py +11 -0
  88. waldiez/exporting/tools/exporter.py +207 -0
  89. waldiez/exporting/tools/factory.py +57 -0
  90. waldiez/exporting/tools/processor.py +248 -0
  91. waldiez/exporting/tools/registration.py +133 -0
  92. waldiez/io/__init__.py +128 -0
  93. waldiez/io/_ws.py +199 -0
  94. waldiez/io/models/__init__.py +60 -0
  95. waldiez/io/models/base.py +66 -0
  96. waldiez/io/models/constants.py +78 -0
  97. waldiez/io/models/content/__init__.py +23 -0
  98. waldiez/io/models/content/audio.py +43 -0
  99. waldiez/io/models/content/base.py +45 -0
  100. waldiez/io/models/content/file.py +43 -0
  101. waldiez/io/models/content/image.py +96 -0
  102. waldiez/io/models/content/text.py +37 -0
  103. waldiez/io/models/content/video.py +43 -0
  104. waldiez/io/models/user_input.py +269 -0
  105. waldiez/io/models/user_response.py +215 -0
  106. waldiez/io/mqtt.py +681 -0
  107. waldiez/io/redis.py +782 -0
  108. waldiez/io/structured.py +419 -0
  109. waldiez/io/utils.py +184 -0
  110. waldiez/io/ws.py +298 -0
  111. waldiez/logger.py +481 -0
  112. waldiez/models/__init__.py +108 -51
  113. waldiez/models/agents/__init__.py +34 -70
  114. waldiez/models/agents/agent/__init__.py +10 -4
  115. waldiez/models/agents/agent/agent.py +466 -65
  116. waldiez/models/agents/agent/agent_data.py +119 -47
  117. waldiez/models/agents/agent/agent_type.py +13 -2
  118. waldiez/models/agents/agent/code_execution.py +12 -12
  119. waldiez/models/agents/agent/human_input_mode.py +8 -0
  120. waldiez/models/agents/agent/{linked_skill.py → linked_tool.py} +7 -7
  121. waldiez/models/agents/agent/nested_chat.py +35 -7
  122. waldiez/models/agents/agent/termination_message.py +30 -22
  123. waldiez/models/agents/{swarm_agent → agent}/update_system_message.py +22 -22
  124. waldiez/models/agents/agents.py +58 -63
  125. waldiez/models/agents/assistant/assistant.py +4 -4
  126. waldiez/models/agents/assistant/assistant_data.py +13 -1
  127. waldiez/models/agents/{captain_agent → captain}/captain_agent.py +5 -5
  128. waldiez/models/agents/{captain_agent → captain}/captain_agent_data.py +5 -5
  129. waldiez/models/agents/extra_requirements.py +11 -16
  130. waldiez/models/agents/group_manager/group_manager.py +103 -13
  131. waldiez/models/agents/group_manager/group_manager_data.py +36 -14
  132. waldiez/models/agents/group_manager/speakers.py +77 -24
  133. waldiez/models/agents/{rag_user → rag_user_proxy}/__init__.py +16 -16
  134. waldiez/models/agents/rag_user_proxy/rag_user_proxy.py +64 -0
  135. waldiez/models/agents/{rag_user/rag_user_data.py → rag_user_proxy/rag_user_proxy_data.py} +6 -5
  136. waldiez/models/agents/{rag_user → rag_user_proxy}/retrieve_config.py +182 -114
  137. waldiez/models/agents/{rag_user → rag_user_proxy}/vector_db_config.py +13 -13
  138. waldiez/models/agents/reasoning/reasoning_agent.py +6 -6
  139. waldiez/models/agents/reasoning/reasoning_agent_data.py +110 -63
  140. waldiez/models/agents/reasoning/reasoning_agent_reason_config.py +38 -10
  141. waldiez/models/agents/user_proxy/user_proxy.py +11 -7
  142. waldiez/models/agents/user_proxy/user_proxy_data.py +2 -2
  143. waldiez/models/chat/__init__.py +2 -1
  144. waldiez/models/chat/chat.py +166 -87
  145. waldiez/models/chat/chat_data.py +99 -136
  146. waldiez/models/chat/chat_message.py +33 -23
  147. waldiez/models/chat/chat_nested.py +31 -30
  148. waldiez/models/chat/chat_summary.py +10 -8
  149. waldiez/models/common/__init__.py +52 -2
  150. waldiez/models/common/ag2_version.py +1 -1
  151. waldiez/models/common/base.py +38 -7
  152. waldiez/models/common/dict_utils.py +42 -17
  153. waldiez/models/common/handoff.py +459 -0
  154. waldiez/models/common/id_generator.py +19 -0
  155. waldiez/models/common/method_utils.py +130 -68
  156. waldiez/{exporting/base/utils → models/common}/naming.py +38 -61
  157. waldiez/models/common/waldiez_version.py +37 -0
  158. waldiez/models/flow/__init__.py +9 -2
  159. waldiez/models/flow/connection.py +18 -0
  160. waldiez/models/flow/flow.py +311 -215
  161. waldiez/models/flow/flow_data.py +207 -40
  162. waldiez/models/flow/info.py +85 -0
  163. waldiez/models/flow/naming.py +131 -0
  164. waldiez/models/model/__init__.py +7 -1
  165. waldiez/models/model/extra_requirements.py +3 -12
  166. waldiez/models/model/model.py +76 -21
  167. waldiez/models/model/model_data.py +108 -20
  168. waldiez/models/tool/__init__.py +16 -0
  169. waldiez/models/tool/extra_requirements.py +36 -0
  170. waldiez/models/{skill/skill.py → tool/tool.py} +88 -88
  171. waldiez/models/tool/tool_data.py +51 -0
  172. waldiez/models/tool/tool_type.py +8 -0
  173. waldiez/models/waldiez.py +97 -80
  174. waldiez/runner.py +114 -49
  175. waldiez/running/__init__.py +1 -1
  176. waldiez/running/environment.py +49 -68
  177. waldiez/running/gen_seq_diagram.py +16 -14
  178. waldiez/running/running.py +53 -34
  179. waldiez/utils/__init__.py +0 -4
  180. waldiez/utils/cli_extras/jupyter.py +5 -3
  181. waldiez/utils/cli_extras/runner.py +6 -4
  182. waldiez/utils/cli_extras/studio.py +6 -4
  183. waldiez/utils/conflict_checker.py +15 -9
  184. waldiez/utils/flaml_warnings.py +5 -5
  185. {waldiez-0.4.6.dist-info → waldiez-0.4.8.dist-info}/METADATA +235 -91
  186. waldiez-0.4.8.dist-info/RECORD +200 -0
  187. waldiez/exporting/agent/agent_exporter.py +0 -297
  188. waldiez/exporting/agent/utils/__init__.py +0 -23
  189. waldiez/exporting/agent/utils/captain_agent.py +0 -263
  190. waldiez/exporting/agent/utils/code_execution.py +0 -65
  191. waldiez/exporting/agent/utils/group_manager.py +0 -220
  192. waldiez/exporting/agent/utils/rag_user/__init__.py +0 -7
  193. waldiez/exporting/agent/utils/rag_user/rag_user.py +0 -209
  194. waldiez/exporting/agent/utils/reasoning.py +0 -36
  195. waldiez/exporting/agent/utils/swarm_agent.py +0 -469
  196. waldiez/exporting/agent/utils/teachability.py +0 -41
  197. waldiez/exporting/agent/utils/termination_message.py +0 -44
  198. waldiez/exporting/base/__init__.py +0 -25
  199. waldiez/exporting/base/agent_position.py +0 -75
  200. waldiez/exporting/base/base_exporter.py +0 -118
  201. waldiez/exporting/base/export_position.py +0 -48
  202. waldiez/exporting/base/import_position.py +0 -23
  203. waldiez/exporting/base/mixin.py +0 -137
  204. waldiez/exporting/base/utils/__init__.py +0 -18
  205. waldiez/exporting/base/utils/comments.py +0 -96
  206. waldiez/exporting/base/utils/path_check.py +0 -68
  207. waldiez/exporting/base/utils/to_string.py +0 -84
  208. waldiez/exporting/chats/chats_exporter.py +0 -240
  209. waldiez/exporting/chats/utils/swarm.py +0 -210
  210. waldiez/exporting/flow/flow_exporter.py +0 -528
  211. waldiez/exporting/flow/utils/agent_utils.py +0 -204
  212. waldiez/exporting/flow/utils/chat_utils.py +0 -71
  213. waldiez/exporting/flow/utils/def_main.py +0 -77
  214. waldiez/exporting/flow/utils/flow_content.py +0 -202
  215. waldiez/exporting/flow/utils/flow_names.py +0 -116
  216. waldiez/exporting/flow/utils/importing_utils.py +0 -227
  217. waldiez/exporting/models/models_exporter.py +0 -199
  218. waldiez/exporting/models/utils.py +0 -174
  219. waldiez/exporting/skills/__init__.py +0 -9
  220. waldiez/exporting/skills/skills_exporter.py +0 -176
  221. waldiez/exporting/skills/utils.py +0 -369
  222. waldiez/models/agents/agent/teachability.py +0 -70
  223. waldiez/models/agents/rag_user/rag_user.py +0 -60
  224. waldiez/models/agents/swarm_agent/__init__.py +0 -50
  225. waldiez/models/agents/swarm_agent/after_work.py +0 -179
  226. waldiez/models/agents/swarm_agent/on_condition.py +0 -105
  227. waldiez/models/agents/swarm_agent/on_condition_available.py +0 -142
  228. waldiez/models/agents/swarm_agent/on_condition_target.py +0 -40
  229. waldiez/models/agents/swarm_agent/swarm_agent.py +0 -107
  230. waldiez/models/agents/swarm_agent/swarm_agent_data.py +0 -124
  231. waldiez/models/flow/utils.py +0 -232
  232. waldiez/models/skill/__init__.py +0 -16
  233. waldiez/models/skill/extra_requirements.py +0 -36
  234. waldiez/models/skill/skill_data.py +0 -53
  235. waldiez/models/skill/skill_type.py +0 -8
  236. waldiez/utils/pysqlite3_checker.py +0 -308
  237. waldiez/utils/rdps_checker.py +0 -122
  238. waldiez-0.4.6.dist-info/RECORD +0 -149
  239. /waldiez/models/agents/{captain_agent → captain}/__init__.py +0 -0
  240. /waldiez/models/agents/{captain_agent → captain}/captain_agent_lib_entry.py +0 -0
  241. {waldiez-0.4.6.dist-info → waldiez-0.4.8.dist-info}/WHEEL +0 -0
  242. {waldiez-0.4.6.dist-info → waldiez-0.4.8.dist-info}/entry_points.txt +0 -0
  243. {waldiez-0.4.6.dist-info → waldiez-0.4.8.dist-info}/licenses/LICENSE +0 -0
  244. {waldiez-0.4.6.dist-info → waldiez-0.4.8.dist-info}/licenses/NOTICE.md +0 -0
@@ -1,240 +0,0 @@
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=1 if self.for_notebook else 2,
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=1 if self.for_notebook else 2,
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=1 if self.for_notebook else 2,
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
- }
@@ -1,210 +0,0 @@
1
- # SPDX-License-Identifier: Apache-2.0.
2
- # Copyright (c) 2024 - 2025 Waldiez and contributors.
3
- # flake8: noqa: E501
4
- """Swarm chat exporting functions."""
5
-
6
- from typing import Callable, Dict, List, Optional, Tuple
7
-
8
- from waldiez.models import WaldiezAgent, WaldiezChat
9
-
10
-
11
- # pylint: disable=too-many-locals,line-too-long
12
- def export_swarm_chat(
13
- chat: WaldiezChat,
14
- agent_names: Dict[str, str],
15
- chat_names: Dict[str, str],
16
- sender: WaldiezAgent,
17
- recipient: WaldiezAgent,
18
- get_swarm_members: Callable[
19
- [WaldiezAgent], Tuple[List[WaldiezAgent], WaldiezAgent | None]
20
- ],
21
- serializer: Callable[..., str],
22
- string_escape: Callable[[str], str],
23
- tabs: int,
24
- is_async: bool,
25
- ) -> Tuple[str, str]:
26
- """Get the swarm chat message string.
27
-
28
- Parameters
29
- ----------
30
- chat : WaldiezChat
31
- The chat.
32
- agent_names : Dict[str, str]
33
- A mapping of agent id to agent name.
34
- chat_names : Dict[str, str]
35
- A mapping of chat id to chat name.
36
- sender : WaldiezAgent
37
- The sender.
38
- recipient : WaldiezAgent
39
- The recipient.
40
- get_swarm_members: Callable[[WaldiezAgent], Tuple[List[WaldiezAgent], WaldiezAgent | None]],
41
- The function to get the swarm members.
42
- serializer : Callable[..., str]
43
- The function to generate the string representation of an object.
44
- string_escape : Callable[[str], str]
45
- The function to escape the string.
46
- tabs : int
47
- The number of tabs to use for indentation.
48
- is_async : bool
49
- Whether the chat is asynchronous.
50
-
51
- Returns
52
- -------
53
- Tuple[str, str]
54
- The `initiate_swarm_chat` message string and additional methods string.
55
-
56
- Raises
57
- ------
58
- ValueError
59
- If neither the sender nor the recipient is a swarm agent.
60
- """
61
- # either the sender or the recipient can be the swarm agent
62
- # one of them MUST be the swarm agent.
63
- if sender.agent_type != "swarm" and recipient.agent_type != "swarm":
64
- raise ValueError("One of the agents must be a swarm agent")
65
- initial_agent = sender if sender.agent_type == "swarm" else recipient
66
- tab = " " * tabs if tabs > 0 else ""
67
- swarm_members = get_swarm_members(initial_agent)
68
- swarm_agents_string, user_agent_string = get_swarm_agents_strings(
69
- swarm_members=swarm_members,
70
- sender=sender,
71
- recipient=recipient,
72
- agent_names=agent_names,
73
- user_agent=None,
74
- )
75
- messages_string = get_swarm_messages_string(
76
- chat=chat, string_escape=string_escape
77
- )
78
- context_vars_string = (
79
- serializer(chat.context_variables, tabs=tabs + 1)
80
- if chat.context_variables
81
- else "{}"
82
- )
83
- chat_name = chat_names[chat.id]
84
- after_work_string, additional_methods = get_swarm_after_work_string(
85
- chat=chat,
86
- agent_names=agent_names,
87
- name_suffix=chat_name,
88
- )
89
- agent_name = agent_names[initial_agent.id]
90
- results_is = f"{tab}results, _, __ = "
91
- initiate = "initiate_swarm_chat"
92
- if is_async:
93
- results_is += "await "
94
- initiate = "a_initiate_swarm_chat"
95
- initiate_chat = "\n" + f"{results_is}{initiate}(" + "\n"
96
- initiate_chat += f"{tab} initial_agent={agent_name}," + "\n"
97
- initiate_chat += f"{tab} agents=[{swarm_agents_string}]," + "\n"
98
- initiate_chat += f"{tab} messages={messages_string}," + "\n"
99
- initiate_chat += f"{tab} context_variables={context_vars_string}," + "\n"
100
- initiate_chat += f"{tab} user_agent={user_agent_string}," + "\n"
101
- initiate_chat += f"{tab} after_work={after_work_string}," + "\n"
102
- initiate_chat += f"{tab} max_rounds={chat.max_rounds}," + "\n"
103
- initiate_chat += f"{tab})" + "\n"
104
- return initiate_chat, additional_methods
105
-
106
-
107
- def get_swarm_agents_strings(
108
- swarm_members: Tuple[List[WaldiezAgent], WaldiezAgent | None],
109
- sender: WaldiezAgent,
110
- recipient: WaldiezAgent,
111
- agent_names: Dict[str, str],
112
- user_agent: Optional[WaldiezAgent],
113
- ) -> Tuple[str, str]:
114
- """Get the swarm agent strings to use in `initiate_swarm_chat`.
115
-
116
- Parameters
117
- ----------
118
- swarm_members : Tuple[List[WaldiezAgent], WaldiezAgent | None]
119
- The swarm agents and the user agent.
120
- sender : WaldiezAgent
121
- The chat initiator.
122
- recipient : WaldiezAgent
123
- The chat recipient.
124
- agent_names : Dict[str, str]
125
- A mapping of agent id to agent name.
126
- user_agent : Optional[WaldiezAgent]
127
- The user agent.
128
-
129
- Returns
130
- -------
131
- Tuple[str, str]
132
- The swarm agents string and the user agent string.
133
- """
134
- swarm_agents, user_member = swarm_members
135
- if user_agent is None and user_member is not None:
136
- user_agent = user_member
137
- if user_agent is None: # pragma: no cover
138
- if sender.agent_type in ("user", "rag_user"):
139
- user_agent = sender
140
- elif recipient.agent_type in ("user", "rag_user"):
141
- user_agent = recipient
142
- agents_string = ", ".join(
143
- [f"{agent_names[agent.id]}" for agent in swarm_agents]
144
- )
145
- user_agent_string = "None"
146
- if user_agent:
147
- user_agent_string = agent_names[user_agent.id]
148
- return agents_string, user_agent_string
149
-
150
-
151
- def get_swarm_messages_string(
152
- chat: WaldiezChat,
153
- string_escape: Callable[[str], str],
154
- ) -> str:
155
- """Get the swarm chat messages string to use in `initiate_swarm_chat`.
156
-
157
- Parameters
158
- ----------
159
- chat : WaldiezChat
160
- The chat.
161
- string_escape : Callable[[str], str]
162
- The string escape function.
163
-
164
- Returns
165
- -------
166
- str
167
- The swarm chat messages string.
168
- """
169
- chat_message = chat.message
170
- if chat.message.type == "string" and chat_message.content:
171
- messages_string = '[{"role": "user", "content": '
172
- escaped_message = string_escape(chat_message.content)
173
- messages_string += f'"{escaped_message}"'
174
- messages_string += "}]"
175
- else:
176
- messages_string = '"Start the chat."'
177
- return messages_string
178
-
179
-
180
- def get_swarm_after_work_string(
181
- chat: WaldiezChat,
182
- agent_names: Dict[str, str],
183
- name_suffix: str,
184
- ) -> Tuple[str, str]:
185
- """Get the swarm after work string.
186
-
187
- Parameters
188
- ----------
189
- chat : WaldiezChat
190
- The chat.
191
- agent_names : Dict[str, str]
192
- A mapping of agent id to agent name.
193
- name_suffix : str
194
- The suffix to use for the function name.
195
-
196
- Returns
197
- -------
198
- Tuple[str, str]
199
- The after work string and the additional methods string.
200
- """
201
- if not chat.after_work:
202
- return "AfterWork(AfterWorkOption.TERMINATE)", ""
203
- additional_methods = ""
204
- after_work_string, function_content = chat.after_work.get_recipient(
205
- agent_names=agent_names,
206
- name_suffix=name_suffix,
207
- )
208
- if chat.after_work.recipient_type == "callable" and function_content:
209
- additional_methods = "\n" + f"{function_content}" + "\n"
210
- return after_work_string, additional_methods