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,204 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0.
|
|
2
|
+
# Copyright (c) 2024 - 2025 Waldiez and contributors.
|
|
3
|
+
"""Handle agent contents before and after the agent(s) exports."""
|
|
4
|
+
|
|
5
|
+
from typing import List, Tuple, Union
|
|
6
|
+
|
|
7
|
+
from waldiez.exporting.base import (
|
|
8
|
+
AgentPosition,
|
|
9
|
+
AgentPositions,
|
|
10
|
+
ExporterReturnType,
|
|
11
|
+
ExportPosition,
|
|
12
|
+
)
|
|
13
|
+
from waldiez.models import WaldiezAgent
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def add_after_all_agents_content(
|
|
17
|
+
agents_contents: str,
|
|
18
|
+
after_export: List[Tuple[str, Union[ExportPosition, AgentPosition]]],
|
|
19
|
+
) -> str:
|
|
20
|
+
"""Add the after all agents content.
|
|
21
|
+
|
|
22
|
+
Parameters
|
|
23
|
+
----------
|
|
24
|
+
agents_contents : str
|
|
25
|
+
The agents content.
|
|
26
|
+
after_export : List[Tuple[str, Union[ExportPosition, AgentPosition]]]
|
|
27
|
+
The after export.
|
|
28
|
+
|
|
29
|
+
Returns
|
|
30
|
+
-------
|
|
31
|
+
str
|
|
32
|
+
The agents content with the after all agents content.
|
|
33
|
+
"""
|
|
34
|
+
new_content = str(agents_contents)
|
|
35
|
+
# let's get the list first, and sort it by the order
|
|
36
|
+
after_all_agent_exports = [
|
|
37
|
+
(content, position)
|
|
38
|
+
for content, position in after_export
|
|
39
|
+
if isinstance(position, AgentPosition)
|
|
40
|
+
and position.position == AgentPositions.AFTER_ALL
|
|
41
|
+
]
|
|
42
|
+
by_order = sorted(after_all_agent_exports, key=lambda x: x[1].order)
|
|
43
|
+
for content, _ in by_order:
|
|
44
|
+
new_content += content + "\n"
|
|
45
|
+
if not new_content.endswith("\n"):
|
|
46
|
+
new_content += "\n"
|
|
47
|
+
return new_content
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def add_before_all_agents_content(
|
|
51
|
+
agents_contents: str,
|
|
52
|
+
before_export: List[Tuple[str, Union[ExportPosition, AgentPosition]]],
|
|
53
|
+
) -> str:
|
|
54
|
+
"""Add the before all agents content.
|
|
55
|
+
|
|
56
|
+
Parameters
|
|
57
|
+
----------
|
|
58
|
+
agents_contents : str
|
|
59
|
+
The agents content.
|
|
60
|
+
before_export : List[Tuple[str, Union[ExportPosition, AgentPosition]]]
|
|
61
|
+
The before export.
|
|
62
|
+
|
|
63
|
+
Returns
|
|
64
|
+
-------
|
|
65
|
+
str
|
|
66
|
+
The agents content with the before all agents content.
|
|
67
|
+
"""
|
|
68
|
+
new_content = str(agents_contents)
|
|
69
|
+
before_all_agents_exports = [
|
|
70
|
+
(content, position)
|
|
71
|
+
for content, position in before_export
|
|
72
|
+
if isinstance(position, AgentPosition)
|
|
73
|
+
and position.position == AgentPositions.BEFORE_ALL
|
|
74
|
+
]
|
|
75
|
+
for content, _ in before_all_agents_exports:
|
|
76
|
+
new_content = content + "\n" + new_content
|
|
77
|
+
if not new_content.startswith("\n"):
|
|
78
|
+
new_content = "\n" + new_content
|
|
79
|
+
return new_content
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def add_before_agent_content(
|
|
83
|
+
agent_content: str,
|
|
84
|
+
before_export: List[Tuple[str, Union[ExportPosition, AgentPosition]]],
|
|
85
|
+
agent: WaldiezAgent,
|
|
86
|
+
) -> str:
|
|
87
|
+
"""Add the before agent content.
|
|
88
|
+
|
|
89
|
+
Parameters
|
|
90
|
+
----------
|
|
91
|
+
agent_content : str
|
|
92
|
+
The agent content.
|
|
93
|
+
before_export : List[Tuple[str, Union[ExportPosition, AgentPosition]]]
|
|
94
|
+
The before export.
|
|
95
|
+
agent : WaldiezAgent
|
|
96
|
+
The agent.
|
|
97
|
+
|
|
98
|
+
Returns
|
|
99
|
+
-------
|
|
100
|
+
str
|
|
101
|
+
The agent content with the before agent content.
|
|
102
|
+
"""
|
|
103
|
+
new_content = str(agent_content)
|
|
104
|
+
for content, position in before_export:
|
|
105
|
+
if (
|
|
106
|
+
isinstance(position, AgentPosition)
|
|
107
|
+
and position.agent == agent
|
|
108
|
+
and position.position == AgentPositions.BEFORE
|
|
109
|
+
):
|
|
110
|
+
new_content = content + "\n" + new_content
|
|
111
|
+
if not new_content.startswith("\n"):
|
|
112
|
+
new_content = "\n" + new_content
|
|
113
|
+
return new_content
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def add_after_agent_content(
|
|
117
|
+
agent_content: str,
|
|
118
|
+
after_export: List[Tuple[str, Union[ExportPosition, AgentPosition]]],
|
|
119
|
+
agent: WaldiezAgent,
|
|
120
|
+
) -> str:
|
|
121
|
+
"""Add the after agent content.
|
|
122
|
+
|
|
123
|
+
Parameters
|
|
124
|
+
----------
|
|
125
|
+
agent_content : str
|
|
126
|
+
The agent content.
|
|
127
|
+
after_export : List[Tuple[str, Union[ExportPosition, AgentPosition]]]
|
|
128
|
+
The after export.
|
|
129
|
+
agent : WaldiezAgent
|
|
130
|
+
The agent.
|
|
131
|
+
|
|
132
|
+
Returns
|
|
133
|
+
-------
|
|
134
|
+
str
|
|
135
|
+
The agent content with the after agent content.
|
|
136
|
+
"""
|
|
137
|
+
new_content = str(agent_content)
|
|
138
|
+
for content, position in after_export:
|
|
139
|
+
if (
|
|
140
|
+
isinstance(position, AgentPosition)
|
|
141
|
+
and position.agent == agent
|
|
142
|
+
and position.position == AgentPositions.AFTER
|
|
143
|
+
):
|
|
144
|
+
new_content += content + "\n"
|
|
145
|
+
if not new_content.endswith("\n"):
|
|
146
|
+
new_content += "\n"
|
|
147
|
+
return new_content
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def gather_agent_outputs(
|
|
151
|
+
before_export: List[Tuple[str, Union[ExportPosition, AgentPosition]]],
|
|
152
|
+
after_export: List[Tuple[str, Union[ExportPosition, AgentPosition]]],
|
|
153
|
+
agent_outputs: List[ExporterReturnType],
|
|
154
|
+
) -> ExporterReturnType:
|
|
155
|
+
"""Gather all the agent outputs.
|
|
156
|
+
|
|
157
|
+
Parameters
|
|
158
|
+
----------
|
|
159
|
+
before_export : List[Tuple[str, Union[ExportPosition, AgentPosition]]]
|
|
160
|
+
The before export.
|
|
161
|
+
after_export : List[Tuple[str, Union[ExportPosition, AgentPosition]]]
|
|
162
|
+
The after export.
|
|
163
|
+
agent_outputs : List[ExporterReturnType]
|
|
164
|
+
The agent outputs.
|
|
165
|
+
|
|
166
|
+
Returns
|
|
167
|
+
-------
|
|
168
|
+
ExporterReturnType
|
|
169
|
+
The gathered agent outputs.
|
|
170
|
+
"""
|
|
171
|
+
agents_contents = ""
|
|
172
|
+
agents_imports = []
|
|
173
|
+
agents_before_export = []
|
|
174
|
+
agents_after_export = []
|
|
175
|
+
agents_env_vars = []
|
|
176
|
+
for output in agent_outputs:
|
|
177
|
+
if output["content"]:
|
|
178
|
+
agents_contents += output["content"]
|
|
179
|
+
if output["imports"]:
|
|
180
|
+
agents_imports.extend(output["imports"])
|
|
181
|
+
if output["before_export"]:
|
|
182
|
+
agents_before_export.extend(output["before_export"])
|
|
183
|
+
if output["after_export"]:
|
|
184
|
+
agents_after_export.extend(output["after_export"])
|
|
185
|
+
if output["environment_variables"]:
|
|
186
|
+
agents_env_vars.extend(output["environment_variables"])
|
|
187
|
+
agents_contents = add_before_all_agents_content(
|
|
188
|
+
agents_contents,
|
|
189
|
+
before_export,
|
|
190
|
+
)
|
|
191
|
+
agents_contents = add_after_all_agents_content(
|
|
192
|
+
agents_contents,
|
|
193
|
+
after_export,
|
|
194
|
+
)
|
|
195
|
+
agents_contents = agents_contents.replace("\n\n\n\n", "\n\n\n")
|
|
196
|
+
while agents_contents.endswith("\n\n\n"):
|
|
197
|
+
agents_contents = agents_contents[: -len("\n")]
|
|
198
|
+
return {
|
|
199
|
+
"content": agents_contents,
|
|
200
|
+
"imports": agents_imports,
|
|
201
|
+
"before_export": agents_before_export,
|
|
202
|
+
"after_export": agents_after_export,
|
|
203
|
+
"environment_variables": agents_env_vars,
|
|
204
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0.
|
|
2
|
+
# Copyright (c) 2024 - 2025 Waldiez and contributors.
|
|
3
|
+
"""Utilities for chat content after exporting."""
|
|
4
|
+
|
|
5
|
+
from typing import List, Tuple, Union
|
|
6
|
+
|
|
7
|
+
from waldiez.exporting.base import (
|
|
8
|
+
AgentPosition,
|
|
9
|
+
ExportPosition,
|
|
10
|
+
ExportPositions,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def add_before_chat_content(
|
|
15
|
+
chat_content: str,
|
|
16
|
+
before_export: List[Tuple[str, Union[ExportPosition, AgentPosition]]],
|
|
17
|
+
) -> str:
|
|
18
|
+
"""Add the before chat content.
|
|
19
|
+
|
|
20
|
+
Parameters
|
|
21
|
+
----------
|
|
22
|
+
chat_content : str
|
|
23
|
+
The chat content.
|
|
24
|
+
before_export : List[Tuple[str, Union[ExportPosition, AgentPosition]]]
|
|
25
|
+
The before export.
|
|
26
|
+
|
|
27
|
+
Returns
|
|
28
|
+
-------
|
|
29
|
+
str
|
|
30
|
+
The chat content with the before chat content.
|
|
31
|
+
"""
|
|
32
|
+
new_content = str(chat_content)
|
|
33
|
+
for content, position in before_export:
|
|
34
|
+
if (
|
|
35
|
+
isinstance(position, ExportPosition)
|
|
36
|
+
and position.position == ExportPositions.CHATS
|
|
37
|
+
):
|
|
38
|
+
new_content = content + "\n" + new_content
|
|
39
|
+
if not new_content.endswith("\n"):
|
|
40
|
+
new_content += "\n"
|
|
41
|
+
return new_content
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def add_after_chat_content(
|
|
45
|
+
chat_content: str,
|
|
46
|
+
after_export: List[Tuple[str, Union[ExportPosition, AgentPosition]]],
|
|
47
|
+
) -> str:
|
|
48
|
+
"""Add the after chat content.
|
|
49
|
+
|
|
50
|
+
Parameters
|
|
51
|
+
----------
|
|
52
|
+
chat_content : str
|
|
53
|
+
The chat content.
|
|
54
|
+
after_export : List[Tuple[str, Union[ExportPosition, AgentPosition]]]
|
|
55
|
+
The after export.
|
|
56
|
+
|
|
57
|
+
Returns
|
|
58
|
+
-------
|
|
59
|
+
str
|
|
60
|
+
The chat content with the after chat content.
|
|
61
|
+
"""
|
|
62
|
+
new_content = str(chat_content)
|
|
63
|
+
for content, position in after_export:
|
|
64
|
+
if (
|
|
65
|
+
isinstance(position, ExportPosition)
|
|
66
|
+
and position.position == ExportPositions.CHATS
|
|
67
|
+
):
|
|
68
|
+
new_content += content + "\n"
|
|
69
|
+
if not new_content.endswith("\n"):
|
|
70
|
+
new_content += "\n"
|
|
71
|
+
return new_content
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0.
|
|
2
|
+
# Copyright (c) 2024 - 2025 Waldiez and contributors.
|
|
3
|
+
"""Get the main function."""
|
|
4
|
+
|
|
5
|
+
from .logging_utils import get_logging_stop_string, get_sqlite_out_call
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_def_main(flow_chats: str, is_async: bool) -> str:
|
|
9
|
+
"""Get the main function.
|
|
10
|
+
|
|
11
|
+
When exporting to python, waldiez_chats string will be the
|
|
12
|
+
content of the main function. It contains either a
|
|
13
|
+
`{sender.initiate_chat(recipient, ...)}` (if there is only one chat)
|
|
14
|
+
or `initiate_chats([..])`, with the list of chats to initiate.
|
|
15
|
+
If async: (sender.a_initiate_chat, a_initiate_chats)
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
flow_chats : str
|
|
20
|
+
The content of the main function.
|
|
21
|
+
is_async : bool
|
|
22
|
+
Whether the main function is asynchronous.
|
|
23
|
+
Returns
|
|
24
|
+
-------
|
|
25
|
+
str
|
|
26
|
+
The main function.
|
|
27
|
+
"""
|
|
28
|
+
content = ""
|
|
29
|
+
if is_async:
|
|
30
|
+
content += "async "
|
|
31
|
+
content += "def main():\n"
|
|
32
|
+
content += " # type: () -> Union[ChatResult, List[ChatResult]]\n"
|
|
33
|
+
content += ' """Start chatting."""\n'
|
|
34
|
+
content += f"{flow_chats}" + "\n"
|
|
35
|
+
content += get_logging_stop_string(1) + "\n"
|
|
36
|
+
content += get_sqlite_out_call(1) + "\n"
|
|
37
|
+
content += " return results\n\n\n"
|
|
38
|
+
if is_async:
|
|
39
|
+
content += "async def call_main():\n"
|
|
40
|
+
else:
|
|
41
|
+
content += "def call_main() -> None:\n"
|
|
42
|
+
content += ' """Run the main function and print the results."""\n'
|
|
43
|
+
# fmt: off
|
|
44
|
+
if is_async:
|
|
45
|
+
content += (
|
|
46
|
+
" results: Union[ChatResult, List[ChatResult]] = await main()\n"
|
|
47
|
+
)
|
|
48
|
+
else:
|
|
49
|
+
content += (
|
|
50
|
+
" results: Union[ChatResult, List[ChatResult]] = main()\n"
|
|
51
|
+
)
|
|
52
|
+
# fmt: on
|
|
53
|
+
content += " if not isinstance(results, list):\n"
|
|
54
|
+
content += " results = [results]\n"
|
|
55
|
+
content += " for result in results:\n"
|
|
56
|
+
content += " pprint(asdict(result))\n\n\n"
|
|
57
|
+
content += 'if __name__ == "__main__":\n'
|
|
58
|
+
if is_async:
|
|
59
|
+
content += " anyio.run(call_main)\n"
|
|
60
|
+
else:
|
|
61
|
+
content += " call_main()\n"
|
|
62
|
+
return content
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0.
|
|
2
|
+
# Copyright (c) 2024 - 2025 Waldiez and contributors.
|
|
3
|
+
"""Utils to generate the content of a flow."""
|
|
4
|
+
|
|
5
|
+
from typing import Callable, List, Optional
|
|
6
|
+
|
|
7
|
+
from waldiez.models import Waldiez
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def get_py_content_start(waldiez: Waldiez) -> str:
|
|
11
|
+
"""Get the first part of the python script.
|
|
12
|
+
|
|
13
|
+
Parameters
|
|
14
|
+
----------
|
|
15
|
+
waldiez : Waldiez
|
|
16
|
+
The waldiez object.
|
|
17
|
+
Returns
|
|
18
|
+
-------
|
|
19
|
+
str
|
|
20
|
+
The first part of the python script.
|
|
21
|
+
"""
|
|
22
|
+
content = "#!/usr/bin/env python\n"
|
|
23
|
+
content += "# flake8: noqa E501\n"
|
|
24
|
+
content += get_pylint_ignore_comment(False)
|
|
25
|
+
content += "# cspell: disable\n"
|
|
26
|
+
content += f'"""{waldiez.name}.' + "\n\n"
|
|
27
|
+
content += f"{waldiez.description}" + "\n\n"
|
|
28
|
+
tags = ", ".join(waldiez.tags)
|
|
29
|
+
content += f"Tags: {tags}" + "\n\n"
|
|
30
|
+
requirements = " ".join(waldiez.requirements)
|
|
31
|
+
content += f"Requirements: {requirements}" + "\n\n"
|
|
32
|
+
content += '"""\n\n'
|
|
33
|
+
return content
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def get_ipynb_content_start(
|
|
37
|
+
waldiez: Waldiez, comment: Callable[[bool, int], str]
|
|
38
|
+
) -> str:
|
|
39
|
+
"""Get the first part of the ipynb file.
|
|
40
|
+
|
|
41
|
+
Parameters
|
|
42
|
+
----------
|
|
43
|
+
waldiez : Waldiez
|
|
44
|
+
The waldiez object.
|
|
45
|
+
comment : Callable[[bool, int], str]
|
|
46
|
+
The function to create a comment.
|
|
47
|
+
Returns
|
|
48
|
+
-------
|
|
49
|
+
str
|
|
50
|
+
The first part of the ipynb file.
|
|
51
|
+
"""
|
|
52
|
+
content = f"{comment(True, 1)}{waldiez.name}." + "\n\n"
|
|
53
|
+
content += f"{comment(True, 2)}{waldiez.description}" + "\n\n"
|
|
54
|
+
content += f"{comment(True, 2)}Dependencies" + "\n\n"
|
|
55
|
+
content += "import sys\n"
|
|
56
|
+
requirements = " ".join(waldiez.requirements)
|
|
57
|
+
if requirements:
|
|
58
|
+
# pylint: disable=line-too-long
|
|
59
|
+
# fmt: off
|
|
60
|
+
content += "# " + f"!{{sys.executable}} -m pip install -q {requirements}" + "\n" # noqa: E501
|
|
61
|
+
# fmt: on
|
|
62
|
+
content += "# flake8: noqa E501"
|
|
63
|
+
content += get_pylint_ignore_comment(True)
|
|
64
|
+
content += "# cspell: disable\n"
|
|
65
|
+
return content
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
PYLINT_RULES = [
|
|
69
|
+
"line-too-long",
|
|
70
|
+
"unknown-option-value",
|
|
71
|
+
"unused-argument",
|
|
72
|
+
"unused-import",
|
|
73
|
+
"invalid-name",
|
|
74
|
+
"import-error",
|
|
75
|
+
"inconsistent-quotes",
|
|
76
|
+
"missing-function-docstring",
|
|
77
|
+
"missing-param-doc",
|
|
78
|
+
"missing-return-doc",
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def get_pylint_ignore_comment(
|
|
83
|
+
notebook: bool, rules: Optional[List[str]] = None
|
|
84
|
+
) -> str:
|
|
85
|
+
"""Get the pylint ignore comment string.
|
|
86
|
+
|
|
87
|
+
Parameters
|
|
88
|
+
----------
|
|
89
|
+
notebook : bool
|
|
90
|
+
Whether the comment is for a notebook.
|
|
91
|
+
rules : Optional[List[str]], optional
|
|
92
|
+
The pylint rules to ignore, by default None.
|
|
93
|
+
|
|
94
|
+
Returns
|
|
95
|
+
-------
|
|
96
|
+
str
|
|
97
|
+
The pylint ignore comment string.
|
|
98
|
+
|
|
99
|
+
Example
|
|
100
|
+
-------
|
|
101
|
+
```python
|
|
102
|
+
>>> get_pylint_ignore_comment(True, ["invalid-name", "line-too-long"])
|
|
103
|
+
|
|
104
|
+
# pylint: disable=invalid-name, line-too-long
|
|
105
|
+
```
|
|
106
|
+
"""
|
|
107
|
+
if not rules:
|
|
108
|
+
rules = PYLINT_RULES
|
|
109
|
+
line = "# pylint: disable=" + ",".join(rules)
|
|
110
|
+
if notebook is True:
|
|
111
|
+
line = "\n" + line
|
|
112
|
+
return line + "\n"
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0.
|
|
2
|
+
# Copyright (c) 2024 - 2025 Waldiez and contributors.
|
|
3
|
+
"""Ensure unique names for agents, models, skills, and chats."""
|
|
4
|
+
|
|
5
|
+
from typing import Callable, Dict, List, TypedDict
|
|
6
|
+
|
|
7
|
+
from waldiez.models import (
|
|
8
|
+
Waldiez,
|
|
9
|
+
WaldiezAgent,
|
|
10
|
+
WaldiezChat,
|
|
11
|
+
WaldiezModel,
|
|
12
|
+
WaldiezSkill,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ResultType(TypedDict):
|
|
17
|
+
"""The result type for ensure_unique_names."""
|
|
18
|
+
|
|
19
|
+
agent_names: Dict[str, str]
|
|
20
|
+
model_names: Dict[str, str]
|
|
21
|
+
skill_names: Dict[str, str]
|
|
22
|
+
chat_names: Dict[str, str]
|
|
23
|
+
agents: List[WaldiezAgent]
|
|
24
|
+
models: List[WaldiezModel]
|
|
25
|
+
skills: List[WaldiezSkill]
|
|
26
|
+
chats: List[WaldiezChat]
|
|
27
|
+
flow_name: str
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# pylint: disable=too-many-locals
|
|
31
|
+
def ensure_unique_names(
|
|
32
|
+
waldiez: Waldiez,
|
|
33
|
+
get_valid_instance_name: Callable[..., Dict[str, str]],
|
|
34
|
+
max_length: int = 64,
|
|
35
|
+
flow_name_max_length: int = 20,
|
|
36
|
+
) -> ResultType:
|
|
37
|
+
"""Ensure unique names for agents, models, skills, and chats and flow.
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
waldiez : Waldiez
|
|
42
|
+
The Waldiez instance.
|
|
43
|
+
get_valid_instance_name : Callable[..., Dict[str, str]]
|
|
44
|
+
The function to get a valid instance name.
|
|
45
|
+
max_length : int, optional
|
|
46
|
+
The maximum length of the name, by default 64
|
|
47
|
+
flow_name_max_length : int, optional
|
|
48
|
+
The maximum length of the flow name, by default 20
|
|
49
|
+
Returns
|
|
50
|
+
-------
|
|
51
|
+
ResultType
|
|
52
|
+
The result with unique names for agents, models, skills, chats, flow.
|
|
53
|
+
"""
|
|
54
|
+
all_names: Dict[str, str] = {}
|
|
55
|
+
agent_names: Dict[str, str] = {}
|
|
56
|
+
model_names: Dict[str, str] = {}
|
|
57
|
+
skill_names: Dict[str, str] = {}
|
|
58
|
+
chat_names: Dict[str, str] = {}
|
|
59
|
+
agents: List[WaldiezAgent] = []
|
|
60
|
+
models: List[WaldiezModel] = []
|
|
61
|
+
skills: List[WaldiezSkill] = []
|
|
62
|
+
chats: List[WaldiezChat] = []
|
|
63
|
+
|
|
64
|
+
for agent in waldiez.agents:
|
|
65
|
+
all_names = get_valid_instance_name(
|
|
66
|
+
(agent.id, agent.name),
|
|
67
|
+
all_names,
|
|
68
|
+
prefix="wa",
|
|
69
|
+
max_length=max_length,
|
|
70
|
+
)
|
|
71
|
+
agent_names[agent.id] = all_names[agent.id]
|
|
72
|
+
agents.append(agent)
|
|
73
|
+
for model in waldiez.models:
|
|
74
|
+
all_names = get_valid_instance_name(
|
|
75
|
+
(model.id, model.name),
|
|
76
|
+
all_names,
|
|
77
|
+
prefix="wm",
|
|
78
|
+
max_length=max_length,
|
|
79
|
+
)
|
|
80
|
+
model_names[model.id] = all_names[model.id]
|
|
81
|
+
models.append(model)
|
|
82
|
+
for skill in waldiez.skills:
|
|
83
|
+
all_names = get_valid_instance_name(
|
|
84
|
+
(skill.id, skill.name),
|
|
85
|
+
all_names,
|
|
86
|
+
prefix="ws",
|
|
87
|
+
max_length=max_length,
|
|
88
|
+
)
|
|
89
|
+
skill_names[skill.id] = all_names[skill.id]
|
|
90
|
+
skills.append(skill)
|
|
91
|
+
for chat in waldiez.flow.data.chats:
|
|
92
|
+
all_names = get_valid_instance_name(
|
|
93
|
+
(chat.id, chat.name), all_names, prefix="wc", max_length=max_length
|
|
94
|
+
)
|
|
95
|
+
chat_names[chat.id] = all_names[chat.id]
|
|
96
|
+
chats.append(chat)
|
|
97
|
+
all_names = get_valid_instance_name(
|
|
98
|
+
(waldiez.flow.id, waldiez.flow.name),
|
|
99
|
+
all_names,
|
|
100
|
+
prefix="wf",
|
|
101
|
+
max_length=flow_name_max_length,
|
|
102
|
+
)
|
|
103
|
+
flow_name = all_names[waldiez.flow.id]
|
|
104
|
+
result: ResultType = {
|
|
105
|
+
"agent_names": agent_names,
|
|
106
|
+
"model_names": model_names,
|
|
107
|
+
"skill_names": skill_names,
|
|
108
|
+
"chat_names": chat_names,
|
|
109
|
+
"agents": agents,
|
|
110
|
+
"models": models,
|
|
111
|
+
"skills": skills,
|
|
112
|
+
"chats": chats,
|
|
113
|
+
"flow_name": flow_name,
|
|
114
|
+
}
|
|
115
|
+
return result
|