synthflow-py 0.1.1__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.
- synthflow_py-0.1.1/LICENSE +201 -0
- synthflow_py-0.1.1/PKG-INFO +154 -0
- synthflow_py-0.1.1/README.md +129 -0
- synthflow_py-0.1.1/pyproject.toml +52 -0
- synthflow_py-0.1.1/setup.cfg +4 -0
- synthflow_py-0.1.1/synthflow/core/__init__.py +18 -0
- synthflow_py-0.1.1/synthflow/core/condition.py +49 -0
- synthflow_py-0.1.1/synthflow/core/datastore.py +49 -0
- synthflow_py-0.1.1/synthflow/core/dsl.py +21 -0
- synthflow_py-0.1.1/synthflow/core/flow.py +67 -0
- synthflow_py-0.1.1/synthflow/core/node.py +186 -0
- synthflow_py-0.1.1/synthflow/core/parallel.py +17 -0
- synthflow_py-0.1.1/synthflow/execution/__init__.py +0 -0
- synthflow_py-0.1.1/synthflow/execution/context.py +1 -0
- synthflow_py-0.1.1/synthflow/execution/engine.py +1 -0
- synthflow_py-0.1.1/synthflow/execution/scheduler.py +1 -0
- synthflow_py-0.1.1/synthflow/plugins/__init__.py +5 -0
- synthflow_py-0.1.1/synthflow/plugins/cache.py +9 -0
- synthflow_py-0.1.1/synthflow/plugins/retry.py +19 -0
- synthflow_py-0.1.1/synthflow/plugins/timeout.py +9 -0
- synthflow_py-0.1.1/synthflow/types/__init__.py +0 -0
- synthflow_py-0.1.1/synthflow/types/field.py +15 -0
- synthflow_py-0.1.1/synthflow/types/schema.py +1 -0
- synthflow_py-0.1.1/synthflow/types/validator.py +1 -0
- synthflow_py-0.1.1/synthflow/utils/__init__.py +0 -0
- synthflow_py-0.1.1/synthflow/utils/inspect.py +1 -0
- synthflow_py-0.1.1/synthflow/visualization/__init__.py +0 -0
- synthflow_py-0.1.1/synthflow/visualization/graphviz.py +1 -0
- synthflow_py-0.1.1/synthflow/visualization/printer.py +1 -0
- synthflow_py-0.1.1/synthflow_py.egg-info/PKG-INFO +154 -0
- synthflow_py-0.1.1/synthflow_py.egg-info/SOURCES.txt +31 -0
- synthflow_py-0.1.1/synthflow_py.egg-info/dependency_links.txt +1 -0
- synthflow_py-0.1.1/synthflow_py.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: synthflow-py
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Async workflow orchestration framework with a lightweight DSL.
|
|
5
|
+
Author: sszgr
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/sszgr/synthflow
|
|
8
|
+
Project-URL: Repository, https://github.com/sszgr/synthflow
|
|
9
|
+
Project-URL: Issues, https://github.com/sszgr/synthflow/issues
|
|
10
|
+
Keywords: workflow,orchestration,async,dsl,pipeline
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
20
|
+
Classifier: Topic :: System :: Distributed Computing
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# SynthFlow
|
|
27
|
+
|
|
28
|
+
Async workflow orchestration framework with a lightweight DSL.
|
|
29
|
+
|
|
30
|
+
> Experimental project written with Codex. Do not use in production environments.
|
|
31
|
+
|
|
32
|
+
## Why SynthFlow
|
|
33
|
+
|
|
34
|
+
- Lightweight workflow DSL for async orchestration
|
|
35
|
+
- Core control flow: `PARALLEL`, `IF`, `OR`, `SWITCH`
|
|
36
|
+
- Cross-node value passing via `ResultRef`
|
|
37
|
+
- Plugin pipeline for runtime policies (`Retry`, `Timeout`)
|
|
38
|
+
- Readable tree visualization via `flow.visualize()`
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
Requires Python 3.10+
|
|
43
|
+
|
|
44
|
+
### From PyPI (stable)
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install synthflow-py
|
|
48
|
+
````
|
|
49
|
+
|
|
50
|
+
### From GitHub (latest development version)
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install git+https://github.com/sszgr/synthflow.git
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
## Core Concepts
|
|
58
|
+
|
|
59
|
+
- `Node`: execution unit; implement `async def run(...)`
|
|
60
|
+
- `Flow`: workflow runner and visualizer
|
|
61
|
+
- `ResultRef`: reference another node's output in `.input(...)`
|
|
62
|
+
- `PARALLEL`: run branches concurrently
|
|
63
|
+
- `IF` / `OR` / `SWITCH`: basic control flow DSL
|
|
64
|
+
- `Retry` / `Timeout`: node plugins via `.use(...)`
|
|
65
|
+
|
|
66
|
+
## Quick Start
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
import asyncio
|
|
70
|
+
|
|
71
|
+
from synthflow.core.flow import Flow
|
|
72
|
+
from synthflow.core.node import Node, ResultRef
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class A(Node):
|
|
76
|
+
async def run(self, a, b, c):
|
|
77
|
+
return a + b + c
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
flow = Flow(
|
|
81
|
+
A(id="a1").input(1, 2, [3, 4, 5])
|
|
82
|
+
>> A(id="a2").input(
|
|
83
|
+
ResultRef("a1").item(2), # 5
|
|
84
|
+
3,
|
|
85
|
+
4,
|
|
86
|
+
)
|
|
87
|
+
>> A(id="a3").input(
|
|
88
|
+
ResultRef("a2").map(lambda x: x * 2),
|
|
89
|
+
1,
|
|
90
|
+
1,
|
|
91
|
+
)
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
flow.visualize()
|
|
95
|
+
asyncio.run(flow.run())
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## DSL Example (Parallel + IF + OR)
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from synthflow.core.dsl import IF, OR, PARALLEL
|
|
102
|
+
from synthflow.core.flow import Flow
|
|
103
|
+
from synthflow.core.node import Node, ResultRef
|
|
104
|
+
|
|
105
|
+
flow = Flow(
|
|
106
|
+
Seed(id="seed").input([2, 5, 8, 13, 21])
|
|
107
|
+
>> PARALLEL(
|
|
108
|
+
SumNode(id="sum_branch").input(ResultRef("seed")),
|
|
109
|
+
MaxNode(id="max_branch").input(ResultRef("seed")),
|
|
110
|
+
EvenCountNode(id="even_branch").input(ResultRef("seed")),
|
|
111
|
+
id="stats_parallel",
|
|
112
|
+
)
|
|
113
|
+
>> BuildSummary(id="summary").input(
|
|
114
|
+
ResultRef("sum_branch"),
|
|
115
|
+
ResultRef("max_branch"),
|
|
116
|
+
ResultRef("even_branch"),
|
|
117
|
+
)
|
|
118
|
+
>> IF(
|
|
119
|
+
condition=OR(
|
|
120
|
+
lambda store: (store.get_node_result("sum_branch") or 0) > 40,
|
|
121
|
+
lambda store: (store.get_node_result("max_branch") or 0) > 20,
|
|
122
|
+
),
|
|
123
|
+
then_node=Alert(id="alert").input(ResultRef("summary")),
|
|
124
|
+
else_node=Normal(id="normal").input(ResultRef("summary")),
|
|
125
|
+
id="risk_if",
|
|
126
|
+
)
|
|
127
|
+
)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Full runnable example: [`examples/general_pipeline.py`](examples/general_pipeline.py)
|
|
131
|
+
|
|
132
|
+
## Plugins
|
|
133
|
+
|
|
134
|
+
Attach plugins on a node with `.use(...)`:
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
from synthflow.plugins import Retry, Timeout
|
|
138
|
+
|
|
139
|
+
node = SomeNode().use(Retry(retries=2, delay=0.1)).use(Timeout(seconds=2.0))
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Visualize
|
|
143
|
+
|
|
144
|
+
`flow.visualize()` prints a tree-style orchestration view, including branch labels:
|
|
145
|
+
|
|
146
|
+
```text
|
|
147
|
+
Flow
|
|
148
|
+
└── Seed(seed)
|
|
149
|
+
└── Parallel(stats_parallel)
|
|
150
|
+
├── [parallel-1] SumNode(sum_branch)
|
|
151
|
+
├── [parallel-2] MaxNode(max_branch)
|
|
152
|
+
├── [parallel-3] EvenCountNode(even_branch)
|
|
153
|
+
└── BuildSummary(summary)
|
|
154
|
+
```
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# SynthFlow
|
|
2
|
+
|
|
3
|
+
Async workflow orchestration framework with a lightweight DSL.
|
|
4
|
+
|
|
5
|
+
> Experimental project written with Codex. Do not use in production environments.
|
|
6
|
+
|
|
7
|
+
## Why SynthFlow
|
|
8
|
+
|
|
9
|
+
- Lightweight workflow DSL for async orchestration
|
|
10
|
+
- Core control flow: `PARALLEL`, `IF`, `OR`, `SWITCH`
|
|
11
|
+
- Cross-node value passing via `ResultRef`
|
|
12
|
+
- Plugin pipeline for runtime policies (`Retry`, `Timeout`)
|
|
13
|
+
- Readable tree visualization via `flow.visualize()`
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
Requires Python 3.10+
|
|
18
|
+
|
|
19
|
+
### From PyPI (stable)
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install synthflow-py
|
|
23
|
+
````
|
|
24
|
+
|
|
25
|
+
### From GitHub (latest development version)
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install git+https://github.com/sszgr/synthflow.git
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
## Core Concepts
|
|
33
|
+
|
|
34
|
+
- `Node`: execution unit; implement `async def run(...)`
|
|
35
|
+
- `Flow`: workflow runner and visualizer
|
|
36
|
+
- `ResultRef`: reference another node's output in `.input(...)`
|
|
37
|
+
- `PARALLEL`: run branches concurrently
|
|
38
|
+
- `IF` / `OR` / `SWITCH`: basic control flow DSL
|
|
39
|
+
- `Retry` / `Timeout`: node plugins via `.use(...)`
|
|
40
|
+
|
|
41
|
+
## Quick Start
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
import asyncio
|
|
45
|
+
|
|
46
|
+
from synthflow.core.flow import Flow
|
|
47
|
+
from synthflow.core.node import Node, ResultRef
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class A(Node):
|
|
51
|
+
async def run(self, a, b, c):
|
|
52
|
+
return a + b + c
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
flow = Flow(
|
|
56
|
+
A(id="a1").input(1, 2, [3, 4, 5])
|
|
57
|
+
>> A(id="a2").input(
|
|
58
|
+
ResultRef("a1").item(2), # 5
|
|
59
|
+
3,
|
|
60
|
+
4,
|
|
61
|
+
)
|
|
62
|
+
>> A(id="a3").input(
|
|
63
|
+
ResultRef("a2").map(lambda x: x * 2),
|
|
64
|
+
1,
|
|
65
|
+
1,
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
flow.visualize()
|
|
70
|
+
asyncio.run(flow.run())
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## DSL Example (Parallel + IF + OR)
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from synthflow.core.dsl import IF, OR, PARALLEL
|
|
77
|
+
from synthflow.core.flow import Flow
|
|
78
|
+
from synthflow.core.node import Node, ResultRef
|
|
79
|
+
|
|
80
|
+
flow = Flow(
|
|
81
|
+
Seed(id="seed").input([2, 5, 8, 13, 21])
|
|
82
|
+
>> PARALLEL(
|
|
83
|
+
SumNode(id="sum_branch").input(ResultRef("seed")),
|
|
84
|
+
MaxNode(id="max_branch").input(ResultRef("seed")),
|
|
85
|
+
EvenCountNode(id="even_branch").input(ResultRef("seed")),
|
|
86
|
+
id="stats_parallel",
|
|
87
|
+
)
|
|
88
|
+
>> BuildSummary(id="summary").input(
|
|
89
|
+
ResultRef("sum_branch"),
|
|
90
|
+
ResultRef("max_branch"),
|
|
91
|
+
ResultRef("even_branch"),
|
|
92
|
+
)
|
|
93
|
+
>> IF(
|
|
94
|
+
condition=OR(
|
|
95
|
+
lambda store: (store.get_node_result("sum_branch") or 0) > 40,
|
|
96
|
+
lambda store: (store.get_node_result("max_branch") or 0) > 20,
|
|
97
|
+
),
|
|
98
|
+
then_node=Alert(id="alert").input(ResultRef("summary")),
|
|
99
|
+
else_node=Normal(id="normal").input(ResultRef("summary")),
|
|
100
|
+
id="risk_if",
|
|
101
|
+
)
|
|
102
|
+
)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Full runnable example: [`examples/general_pipeline.py`](examples/general_pipeline.py)
|
|
106
|
+
|
|
107
|
+
## Plugins
|
|
108
|
+
|
|
109
|
+
Attach plugins on a node with `.use(...)`:
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from synthflow.plugins import Retry, Timeout
|
|
113
|
+
|
|
114
|
+
node = SomeNode().use(Retry(retries=2, delay=0.1)).use(Timeout(seconds=2.0))
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Visualize
|
|
118
|
+
|
|
119
|
+
`flow.visualize()` prints a tree-style orchestration view, including branch labels:
|
|
120
|
+
|
|
121
|
+
```text
|
|
122
|
+
Flow
|
|
123
|
+
└── Seed(seed)
|
|
124
|
+
└── Parallel(stats_parallel)
|
|
125
|
+
├── [parallel-1] SumNode(sum_branch)
|
|
126
|
+
├── [parallel-2] MaxNode(max_branch)
|
|
127
|
+
├── [parallel-3] EvenCountNode(even_branch)
|
|
128
|
+
└── BuildSummary(summary)
|
|
129
|
+
```
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "synthflow-py"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "Async workflow orchestration framework with a lightweight DSL."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "sszgr" }
|
|
14
|
+
]
|
|
15
|
+
keywords = [
|
|
16
|
+
"workflow",
|
|
17
|
+
"orchestration",
|
|
18
|
+
"async",
|
|
19
|
+
"dsl",
|
|
20
|
+
"pipeline"
|
|
21
|
+
]
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Development Status :: 3 - Alpha",
|
|
24
|
+
"Intended Audience :: Developers",
|
|
25
|
+
"License :: OSI Approved :: MIT License",
|
|
26
|
+
"Programming Language :: Python :: 3",
|
|
27
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
28
|
+
"Programming Language :: Python :: 3.10",
|
|
29
|
+
"Programming Language :: Python :: 3.11",
|
|
30
|
+
"Programming Language :: Python :: 3.12",
|
|
31
|
+
"Topic :: Software Development :: Libraries",
|
|
32
|
+
"Topic :: System :: Distributed Computing"
|
|
33
|
+
]
|
|
34
|
+
dependencies = []
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://github.com/sszgr/synthflow"
|
|
38
|
+
Repository = "https://github.com/sszgr/synthflow"
|
|
39
|
+
Issues = "https://github.com/sszgr/synthflow/issues"
|
|
40
|
+
|
|
41
|
+
[tool.setuptools]
|
|
42
|
+
include-package-data = true
|
|
43
|
+
|
|
44
|
+
[tool.setuptools.packages.find]
|
|
45
|
+
where = ["."]
|
|
46
|
+
include = ["synthflow*"]
|
|
47
|
+
exclude = ["tests*"]
|
|
48
|
+
|
|
49
|
+
[tool.pytest.ini_options]
|
|
50
|
+
testpaths = ["tests"]
|
|
51
|
+
python_files = ["test_*.py"]
|
|
52
|
+
addopts = "-q"
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from synthflow.core.condition import IF, OR, SWITCH, If, Switch
|
|
2
|
+
from synthflow.core.dsl import PARALLEL
|
|
3
|
+
from synthflow.core.flow import Flow
|
|
4
|
+
from synthflow.core.node import Node, ResultRef
|
|
5
|
+
from synthflow.core.parallel import Parallel
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"Flow",
|
|
9
|
+
"Node",
|
|
10
|
+
"ResultRef",
|
|
11
|
+
"Parallel",
|
|
12
|
+
"If",
|
|
13
|
+
"Switch",
|
|
14
|
+
"IF",
|
|
15
|
+
"OR",
|
|
16
|
+
"SWITCH",
|
|
17
|
+
"PARALLEL",
|
|
18
|
+
]
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from synthflow.core.node import Node
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class If(Node):
|
|
5
|
+
def __init__(self, condition, then_node, else_node=None, id=None):
|
|
6
|
+
super().__init__(id=id)
|
|
7
|
+
self.condition = condition
|
|
8
|
+
self.then_node = then_node
|
|
9
|
+
self.else_node = else_node
|
|
10
|
+
|
|
11
|
+
async def execute(self, store):
|
|
12
|
+
target = self.then_node if self.condition(store) else self.else_node
|
|
13
|
+
if target is not None:
|
|
14
|
+
store = await target.execute(store)
|
|
15
|
+
if self.next_node:
|
|
16
|
+
return await self.next_node.execute(store)
|
|
17
|
+
return store
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Switch(Node):
|
|
21
|
+
def __init__(self, selector, cases, default=None, id=None):
|
|
22
|
+
super().__init__(id=id)
|
|
23
|
+
self.selector = selector
|
|
24
|
+
self.cases = cases
|
|
25
|
+
self.default = default
|
|
26
|
+
|
|
27
|
+
async def execute(self, store):
|
|
28
|
+
key = self.selector(store)
|
|
29
|
+
target = self.cases.get(key, self.default)
|
|
30
|
+
if target is not None:
|
|
31
|
+
store = await target.execute(store)
|
|
32
|
+
if self.next_node:
|
|
33
|
+
return await self.next_node.execute(store)
|
|
34
|
+
return store
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def OR(*conditions):
|
|
38
|
+
def _condition(store):
|
|
39
|
+
return any(condition(store) for condition in conditions)
|
|
40
|
+
|
|
41
|
+
return _condition
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def IF(condition, then_node, else_node=None, id=None):
|
|
45
|
+
return If(condition=condition, then_node=then_node, else_node=else_node, id=id)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def SWITCH(selector, cases, default=None, id=None):
|
|
49
|
+
return Switch(selector=selector, cases=cases, default=default, id=id)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
class DataStore:
|
|
2
|
+
def __init__(self):
|
|
3
|
+
self._data = {}
|
|
4
|
+
self._source = {}
|
|
5
|
+
self._by_node = {}
|
|
6
|
+
self._node_result = {}
|
|
7
|
+
|
|
8
|
+
def set(self, dtype, value, source=None):
|
|
9
|
+
self._data[dtype] = value
|
|
10
|
+
self._source[dtype] = source
|
|
11
|
+
node_id = getattr(source, "id", None)
|
|
12
|
+
if node_id:
|
|
13
|
+
bucket = self._by_node.setdefault(node_id, {})
|
|
14
|
+
bucket[dtype] = value
|
|
15
|
+
|
|
16
|
+
def set_node_result(self, node_id, value):
|
|
17
|
+
if node_id:
|
|
18
|
+
self._node_result[node_id] = value
|
|
19
|
+
|
|
20
|
+
def get_node_result(self, node_id):
|
|
21
|
+
return self._node_result.get(node_id)
|
|
22
|
+
|
|
23
|
+
def get(self, dtype):
|
|
24
|
+
return self._data.get(dtype)
|
|
25
|
+
|
|
26
|
+
def has(self, dtype):
|
|
27
|
+
return dtype in self._data
|
|
28
|
+
|
|
29
|
+
def get_from_node(self, node_id, output_type=None):
|
|
30
|
+
node_outputs = self._by_node.get(node_id, {})
|
|
31
|
+
if output_type is not None:
|
|
32
|
+
return node_outputs.get(output_type)
|
|
33
|
+
for _, value in node_outputs.items():
|
|
34
|
+
return value
|
|
35
|
+
return None
|
|
36
|
+
|
|
37
|
+
def merge(self, other):
|
|
38
|
+
for dtype, value in other._data.items():
|
|
39
|
+
source = other._source.get(dtype)
|
|
40
|
+
self.set(dtype, value, source=source)
|
|
41
|
+
self._node_result.update(other._node_result)
|
|
42
|
+
|
|
43
|
+
def copy(self):
|
|
44
|
+
new = DataStore()
|
|
45
|
+
new._data = self._data.copy()
|
|
46
|
+
new._source = self._source.copy()
|
|
47
|
+
new._by_node = {node_id: bucket.copy() for node_id, bucket in self._by_node.items()}
|
|
48
|
+
new._node_result = self._node_result.copy()
|
|
49
|
+
return new
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from synthflow.core.condition import IF, OR, SWITCH, If, Switch
|
|
2
|
+
from synthflow.core.flow import Flow
|
|
3
|
+
from synthflow.core.node import ResultRef
|
|
4
|
+
from synthflow.core.parallel import Parallel
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def PARALLEL(*nodes, id=None):
|
|
8
|
+
return Parallel(*nodes, id=id)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"Flow",
|
|
13
|
+
"IF",
|
|
14
|
+
"OR",
|
|
15
|
+
"PARALLEL",
|
|
16
|
+
"SWITCH",
|
|
17
|
+
"If",
|
|
18
|
+
"Switch",
|
|
19
|
+
"Parallel",
|
|
20
|
+
"ResultRef",
|
|
21
|
+
]
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from synthflow.core.datastore import DataStore
|
|
2
|
+
from synthflow.core.parallel import Parallel
|
|
3
|
+
from synthflow.core.condition import If, Switch
|
|
4
|
+
|
|
5
|
+
class Flow:
|
|
6
|
+
def __init__(self, start_node):
|
|
7
|
+
self.start_node = self._normalize_start(start_node)
|
|
8
|
+
|
|
9
|
+
def _normalize_start(self, start_node):
|
|
10
|
+
if isinstance(start_node, (list, tuple)):
|
|
11
|
+
if not start_node:
|
|
12
|
+
raise ValueError("Flow requires at least one node")
|
|
13
|
+
root = start_node[0]
|
|
14
|
+
for node in start_node[1:]:
|
|
15
|
+
root >> node
|
|
16
|
+
return root
|
|
17
|
+
return start_node
|
|
18
|
+
|
|
19
|
+
async def run(self):
|
|
20
|
+
store = DataStore()
|
|
21
|
+
return await self.start_node.execute(store)
|
|
22
|
+
|
|
23
|
+
def visualize(self):
|
|
24
|
+
print("Flow")
|
|
25
|
+
for line in self._render_node(self.start_node, prefix="", is_last=True):
|
|
26
|
+
print(line)
|
|
27
|
+
|
|
28
|
+
def _label(self, node, edge=None):
|
|
29
|
+
node_label = f"{node.__class__.__name__}({node.id})" if getattr(node, "id", None) else node.__class__.__name__
|
|
30
|
+
if edge is None or edge == "next":
|
|
31
|
+
return node_label
|
|
32
|
+
return f"[{edge}] {node_label}"
|
|
33
|
+
|
|
34
|
+
def _children(self, node):
|
|
35
|
+
children = []
|
|
36
|
+
if isinstance(node, Parallel):
|
|
37
|
+
for idx, sub in enumerate(node.nodes, start=1):
|
|
38
|
+
children.append((f"parallel-{idx}", sub))
|
|
39
|
+
if isinstance(node, If):
|
|
40
|
+
children.append(("then", node.then_node))
|
|
41
|
+
if node.else_node:
|
|
42
|
+
children.append(("else", node.else_node))
|
|
43
|
+
if isinstance(node, Switch):
|
|
44
|
+
for key, sub in node.cases.items():
|
|
45
|
+
children.append((f"case:{key}", sub))
|
|
46
|
+
if node.default:
|
|
47
|
+
children.append(("default", node.default))
|
|
48
|
+
if node.next_node:
|
|
49
|
+
children.append(("next", node.next_node))
|
|
50
|
+
return children
|
|
51
|
+
|
|
52
|
+
def _render_node(self, node, prefix, is_last, edge=None):
|
|
53
|
+
connector = "└── " if is_last else "├── "
|
|
54
|
+
lines = [f"{prefix}{connector}{self._label(node, edge=edge)}"]
|
|
55
|
+
child_prefix = prefix + (" " if is_last else "│ ")
|
|
56
|
+
children = self._children(node)
|
|
57
|
+
for idx, (child_edge, child_node) in enumerate(children):
|
|
58
|
+
child_is_last = idx == len(children) - 1
|
|
59
|
+
lines.extend(
|
|
60
|
+
self._render_node(
|
|
61
|
+
child_node,
|
|
62
|
+
prefix=child_prefix,
|
|
63
|
+
is_last=child_is_last,
|
|
64
|
+
edge=child_edge,
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
return lines
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from .datastore import DataStore
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ResultRef:
|
|
6
|
+
def __init__(self, node_id, output_type=None, output_index=None, transform=None):
|
|
7
|
+
self.node_id = node_id
|
|
8
|
+
self.output_type = output_type
|
|
9
|
+
self.output_index = output_index
|
|
10
|
+
self.transform = transform
|
|
11
|
+
|
|
12
|
+
def map(self, transform):
|
|
13
|
+
if self.transform is None:
|
|
14
|
+
chained = transform
|
|
15
|
+
else:
|
|
16
|
+
def chained(value):
|
|
17
|
+
return transform(self.transform(value))
|
|
18
|
+
return ResultRef(
|
|
19
|
+
node_id=self.node_id,
|
|
20
|
+
output_type=self.output_type,
|
|
21
|
+
output_index=self.output_index,
|
|
22
|
+
transform=chained,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
def item(self, index):
|
|
26
|
+
return self.map(lambda value: value[index])
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Node:
|
|
30
|
+
inputs = []
|
|
31
|
+
outputs = []
|
|
32
|
+
|
|
33
|
+
def __init__(self, id=None, **params):
|
|
34
|
+
self.id = id
|
|
35
|
+
self.params = params
|
|
36
|
+
self.next_node = None
|
|
37
|
+
self.plugins = []
|
|
38
|
+
self._input_args = ()
|
|
39
|
+
self._input_kwargs = {}
|
|
40
|
+
|
|
41
|
+
def __rshift__(self, other):
|
|
42
|
+
tail = self
|
|
43
|
+
while tail.next_node is not None:
|
|
44
|
+
tail = tail.next_node
|
|
45
|
+
tail.next_node = other
|
|
46
|
+
return self
|
|
47
|
+
|
|
48
|
+
def use(self, plugin):
|
|
49
|
+
self.plugins.append(plugin)
|
|
50
|
+
return self
|
|
51
|
+
|
|
52
|
+
def input(self, *args, **kwargs):
|
|
53
|
+
"""Dynamic args/kwargs for run(); values can include ResultRef placeholders."""
|
|
54
|
+
self._input_args = args
|
|
55
|
+
self._input_kwargs = kwargs
|
|
56
|
+
return self
|
|
57
|
+
|
|
58
|
+
async def execute(self, store: DataStore):
|
|
59
|
+
args, kwargs = await self._collect_inputs(store)
|
|
60
|
+
result = await self._invoke_with_plugins(store, args, kwargs)
|
|
61
|
+
store.set_node_result(self.id, result)
|
|
62
|
+
self._persist_result(store, result)
|
|
63
|
+
|
|
64
|
+
if self.next_node:
|
|
65
|
+
return await self.next_node.execute(store)
|
|
66
|
+
return store
|
|
67
|
+
|
|
68
|
+
async def _collect_inputs(self, store: DataStore):
|
|
69
|
+
"""Collect run() args and resolve ResultRef placeholders."""
|
|
70
|
+
kwargs = {dtype.__name__: store.get(dtype) for dtype in self.inputs if store.has(dtype)}
|
|
71
|
+
missing = [dtype.__name__ for dtype in self.inputs if not store.has(dtype)]
|
|
72
|
+
if missing:
|
|
73
|
+
raise Exception(f"{self.id or self.__class__.__name__} missing inputs: {missing}")
|
|
74
|
+
|
|
75
|
+
args = self._resolve_results(self._input_args, store)
|
|
76
|
+
kwargs.update(self.params)
|
|
77
|
+
resolved_kwargs = self._resolve_results(self._input_kwargs, store)
|
|
78
|
+
kwargs.update(resolved_kwargs)
|
|
79
|
+
|
|
80
|
+
return args, kwargs
|
|
81
|
+
|
|
82
|
+
def _resolve_results(self, value, store: DataStore):
|
|
83
|
+
"""Recursively resolve ResultRef placeholders."""
|
|
84
|
+
if isinstance(value, ResultRef):
|
|
85
|
+
return self._resolve_single_result(value, store)
|
|
86
|
+
if isinstance(value, (list, tuple)):
|
|
87
|
+
return type(value)(self._resolve_results(v, store) for v in value)
|
|
88
|
+
if isinstance(value, dict):
|
|
89
|
+
return {k: self._resolve_results(v, store) for k, v in value.items()}
|
|
90
|
+
return value
|
|
91
|
+
|
|
92
|
+
def _resolve_single_result(self, nr: ResultRef, store: DataStore):
|
|
93
|
+
value = store.get_node_result(nr.node_id)
|
|
94
|
+
if value is None:
|
|
95
|
+
value = store.get_from_node(nr.node_id, nr.output_type)
|
|
96
|
+
if value is None:
|
|
97
|
+
value = store.get(nr.output_type) if nr.output_type else None
|
|
98
|
+
|
|
99
|
+
if value is None:
|
|
100
|
+
for dtype, val in store._data.items():
|
|
101
|
+
source = store._source.get(dtype)
|
|
102
|
+
if getattr(source, "id", None) == nr.node_id:
|
|
103
|
+
value = val
|
|
104
|
+
break
|
|
105
|
+
|
|
106
|
+
if value is None:
|
|
107
|
+
raise Exception(f"ResultRef from node '{nr.node_id}' not found in store")
|
|
108
|
+
|
|
109
|
+
if isinstance(value, list) and nr.output_index not in (0, None):
|
|
110
|
+
try:
|
|
111
|
+
value = value[nr.output_index]
|
|
112
|
+
except IndexError:
|
|
113
|
+
raise Exception(f"ResultRef index {nr.output_index} out of range for node '{nr.node_id}'")
|
|
114
|
+
|
|
115
|
+
if nr.transform is not None:
|
|
116
|
+
value = nr.transform(value)
|
|
117
|
+
|
|
118
|
+
return value
|
|
119
|
+
|
|
120
|
+
def _persist_result(self, store: DataStore, result):
|
|
121
|
+
if result is None:
|
|
122
|
+
return
|
|
123
|
+
if isinstance(result, dict):
|
|
124
|
+
for dtype, value in result.items():
|
|
125
|
+
store.set(dtype, value, source=self)
|
|
126
|
+
return
|
|
127
|
+
|
|
128
|
+
if len(self.outputs) == 1:
|
|
129
|
+
store.set(self.outputs[0], result, source=self)
|
|
130
|
+
return
|
|
131
|
+
|
|
132
|
+
if len(self.outputs) > 1:
|
|
133
|
+
if not isinstance(result, (list, tuple)):
|
|
134
|
+
raise TypeError(
|
|
135
|
+
f"{self.id or self.__class__.__name__} must return list/tuple for multiple outputs"
|
|
136
|
+
)
|
|
137
|
+
if len(result) != len(self.outputs):
|
|
138
|
+
raise ValueError(
|
|
139
|
+
f"{self.id or self.__class__.__name__} returned {len(result)} values, expected {len(self.outputs)}"
|
|
140
|
+
)
|
|
141
|
+
for dtype, value in zip(self.outputs, result):
|
|
142
|
+
store.set(dtype, value, source=self)
|
|
143
|
+
|
|
144
|
+
async def _invoke_with_plugins(self, store: DataStore, args, kwargs):
|
|
145
|
+
async def base_call():
|
|
146
|
+
result = self.run(*args, **kwargs)
|
|
147
|
+
if inspect.iscoroutine(result):
|
|
148
|
+
return await result
|
|
149
|
+
return result
|
|
150
|
+
|
|
151
|
+
call_next = base_call
|
|
152
|
+
for plugin in reversed(self.plugins):
|
|
153
|
+
prev = call_next
|
|
154
|
+
|
|
155
|
+
async def wrapped(plugin=plugin, prev=prev):
|
|
156
|
+
return await self._run_plugin(plugin, prev, store)
|
|
157
|
+
|
|
158
|
+
call_next = wrapped
|
|
159
|
+
|
|
160
|
+
return await call_next()
|
|
161
|
+
|
|
162
|
+
async def _run_plugin(self, plugin, call_next, store: DataStore):
|
|
163
|
+
runner = getattr(plugin, "run", None)
|
|
164
|
+
if runner is None and callable(plugin):
|
|
165
|
+
runner = plugin
|
|
166
|
+
if runner is None:
|
|
167
|
+
raise TypeError(f"Plugin {plugin!r} must be callable or define run(...)")
|
|
168
|
+
|
|
169
|
+
sig = inspect.signature(runner)
|
|
170
|
+
argc = len(sig.parameters)
|
|
171
|
+
|
|
172
|
+
if argc >= 3:
|
|
173
|
+
outcome = runner(call_next, store, self)
|
|
174
|
+
elif argc == 2:
|
|
175
|
+
outcome = runner(call_next, store)
|
|
176
|
+
elif argc == 1:
|
|
177
|
+
outcome = runner(call_next)
|
|
178
|
+
else:
|
|
179
|
+
outcome = runner()
|
|
180
|
+
|
|
181
|
+
if inspect.iscoroutine(outcome):
|
|
182
|
+
return await outcome
|
|
183
|
+
return outcome
|
|
184
|
+
|
|
185
|
+
async def run(self, *args, **kwargs):
|
|
186
|
+
raise NotImplementedError
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from .node import Node
|
|
3
|
+
from .datastore import DataStore
|
|
4
|
+
|
|
5
|
+
class Parallel(Node):
|
|
6
|
+
def __init__(self, *nodes, id=None):
|
|
7
|
+
super().__init__(id=id)
|
|
8
|
+
self.nodes = nodes
|
|
9
|
+
|
|
10
|
+
async def execute(self, store: DataStore):
|
|
11
|
+
tasks = [node.execute(store.copy()) for node in self.nodes]
|
|
12
|
+
results = await asyncio.gather(*tasks)
|
|
13
|
+
for branch_store in results:
|
|
14
|
+
store.merge(branch_store)
|
|
15
|
+
if self.next_node:
|
|
16
|
+
return await self.next_node.execute(store)
|
|
17
|
+
return store
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Execution context placeholder
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Async execution engine placeholder
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Scheduler placeholder
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Retry:
|
|
5
|
+
def __init__(self, retries=3, delay=0.5):
|
|
6
|
+
self.retries = retries
|
|
7
|
+
self.delay = delay
|
|
8
|
+
|
|
9
|
+
async def run(self, call_next, store, node=None):
|
|
10
|
+
last_exc = None
|
|
11
|
+
attempts = self.retries + 1
|
|
12
|
+
for i in range(attempts):
|
|
13
|
+
try:
|
|
14
|
+
return await call_next()
|
|
15
|
+
except Exception as e:
|
|
16
|
+
last_exc = e
|
|
17
|
+
if i < attempts - 1 and self.delay > 0:
|
|
18
|
+
await asyncio.sleep(self.delay)
|
|
19
|
+
raise last_exc
|
|
File without changes
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
class Field:
|
|
2
|
+
def __init__(self, type_, required=True):
|
|
3
|
+
self.type_ = type_
|
|
4
|
+
self.required = required
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def validate_params(schema, params):
|
|
8
|
+
for name, field in schema.items():
|
|
9
|
+
if field.required and name not in params:
|
|
10
|
+
raise ValueError(f"Missing parameter: {name}")
|
|
11
|
+
|
|
12
|
+
if name in params and not isinstance(params[name], field.type_):
|
|
13
|
+
raise TypeError(
|
|
14
|
+
f"Parameter {name} must be {field.type_}"
|
|
15
|
+
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Schema system placeholder
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Validator placeholder
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Inspect utils placeholder
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Graphviz output placeholder
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Printer placeholder
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: synthflow-py
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Async workflow orchestration framework with a lightweight DSL.
|
|
5
|
+
Author: sszgr
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/sszgr/synthflow
|
|
8
|
+
Project-URL: Repository, https://github.com/sszgr/synthflow
|
|
9
|
+
Project-URL: Issues, https://github.com/sszgr/synthflow/issues
|
|
10
|
+
Keywords: workflow,orchestration,async,dsl,pipeline
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
20
|
+
Classifier: Topic :: System :: Distributed Computing
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# SynthFlow
|
|
27
|
+
|
|
28
|
+
Async workflow orchestration framework with a lightweight DSL.
|
|
29
|
+
|
|
30
|
+
> Experimental project written with Codex. Do not use in production environments.
|
|
31
|
+
|
|
32
|
+
## Why SynthFlow
|
|
33
|
+
|
|
34
|
+
- Lightweight workflow DSL for async orchestration
|
|
35
|
+
- Core control flow: `PARALLEL`, `IF`, `OR`, `SWITCH`
|
|
36
|
+
- Cross-node value passing via `ResultRef`
|
|
37
|
+
- Plugin pipeline for runtime policies (`Retry`, `Timeout`)
|
|
38
|
+
- Readable tree visualization via `flow.visualize()`
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
Requires Python 3.10+
|
|
43
|
+
|
|
44
|
+
### From PyPI (stable)
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install synthflow-py
|
|
48
|
+
````
|
|
49
|
+
|
|
50
|
+
### From GitHub (latest development version)
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install git+https://github.com/sszgr/synthflow.git
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
## Core Concepts
|
|
58
|
+
|
|
59
|
+
- `Node`: execution unit; implement `async def run(...)`
|
|
60
|
+
- `Flow`: workflow runner and visualizer
|
|
61
|
+
- `ResultRef`: reference another node's output in `.input(...)`
|
|
62
|
+
- `PARALLEL`: run branches concurrently
|
|
63
|
+
- `IF` / `OR` / `SWITCH`: basic control flow DSL
|
|
64
|
+
- `Retry` / `Timeout`: node plugins via `.use(...)`
|
|
65
|
+
|
|
66
|
+
## Quick Start
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
import asyncio
|
|
70
|
+
|
|
71
|
+
from synthflow.core.flow import Flow
|
|
72
|
+
from synthflow.core.node import Node, ResultRef
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class A(Node):
|
|
76
|
+
async def run(self, a, b, c):
|
|
77
|
+
return a + b + c
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
flow = Flow(
|
|
81
|
+
A(id="a1").input(1, 2, [3, 4, 5])
|
|
82
|
+
>> A(id="a2").input(
|
|
83
|
+
ResultRef("a1").item(2), # 5
|
|
84
|
+
3,
|
|
85
|
+
4,
|
|
86
|
+
)
|
|
87
|
+
>> A(id="a3").input(
|
|
88
|
+
ResultRef("a2").map(lambda x: x * 2),
|
|
89
|
+
1,
|
|
90
|
+
1,
|
|
91
|
+
)
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
flow.visualize()
|
|
95
|
+
asyncio.run(flow.run())
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## DSL Example (Parallel + IF + OR)
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from synthflow.core.dsl import IF, OR, PARALLEL
|
|
102
|
+
from synthflow.core.flow import Flow
|
|
103
|
+
from synthflow.core.node import Node, ResultRef
|
|
104
|
+
|
|
105
|
+
flow = Flow(
|
|
106
|
+
Seed(id="seed").input([2, 5, 8, 13, 21])
|
|
107
|
+
>> PARALLEL(
|
|
108
|
+
SumNode(id="sum_branch").input(ResultRef("seed")),
|
|
109
|
+
MaxNode(id="max_branch").input(ResultRef("seed")),
|
|
110
|
+
EvenCountNode(id="even_branch").input(ResultRef("seed")),
|
|
111
|
+
id="stats_parallel",
|
|
112
|
+
)
|
|
113
|
+
>> BuildSummary(id="summary").input(
|
|
114
|
+
ResultRef("sum_branch"),
|
|
115
|
+
ResultRef("max_branch"),
|
|
116
|
+
ResultRef("even_branch"),
|
|
117
|
+
)
|
|
118
|
+
>> IF(
|
|
119
|
+
condition=OR(
|
|
120
|
+
lambda store: (store.get_node_result("sum_branch") or 0) > 40,
|
|
121
|
+
lambda store: (store.get_node_result("max_branch") or 0) > 20,
|
|
122
|
+
),
|
|
123
|
+
then_node=Alert(id="alert").input(ResultRef("summary")),
|
|
124
|
+
else_node=Normal(id="normal").input(ResultRef("summary")),
|
|
125
|
+
id="risk_if",
|
|
126
|
+
)
|
|
127
|
+
)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Full runnable example: [`examples/general_pipeline.py`](examples/general_pipeline.py)
|
|
131
|
+
|
|
132
|
+
## Plugins
|
|
133
|
+
|
|
134
|
+
Attach plugins on a node with `.use(...)`:
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
from synthflow.plugins import Retry, Timeout
|
|
138
|
+
|
|
139
|
+
node = SomeNode().use(Retry(retries=2, delay=0.1)).use(Timeout(seconds=2.0))
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Visualize
|
|
143
|
+
|
|
144
|
+
`flow.visualize()` prints a tree-style orchestration view, including branch labels:
|
|
145
|
+
|
|
146
|
+
```text
|
|
147
|
+
Flow
|
|
148
|
+
└── Seed(seed)
|
|
149
|
+
└── Parallel(stats_parallel)
|
|
150
|
+
├── [parallel-1] SumNode(sum_branch)
|
|
151
|
+
├── [parallel-2] MaxNode(max_branch)
|
|
152
|
+
├── [parallel-3] EvenCountNode(even_branch)
|
|
153
|
+
└── BuildSummary(summary)
|
|
154
|
+
```
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
synthflow/core/__init__.py
|
|
5
|
+
synthflow/core/condition.py
|
|
6
|
+
synthflow/core/datastore.py
|
|
7
|
+
synthflow/core/dsl.py
|
|
8
|
+
synthflow/core/flow.py
|
|
9
|
+
synthflow/core/node.py
|
|
10
|
+
synthflow/core/parallel.py
|
|
11
|
+
synthflow/execution/__init__.py
|
|
12
|
+
synthflow/execution/context.py
|
|
13
|
+
synthflow/execution/engine.py
|
|
14
|
+
synthflow/execution/scheduler.py
|
|
15
|
+
synthflow/plugins/__init__.py
|
|
16
|
+
synthflow/plugins/cache.py
|
|
17
|
+
synthflow/plugins/retry.py
|
|
18
|
+
synthflow/plugins/timeout.py
|
|
19
|
+
synthflow/types/__init__.py
|
|
20
|
+
synthflow/types/field.py
|
|
21
|
+
synthflow/types/schema.py
|
|
22
|
+
synthflow/types/validator.py
|
|
23
|
+
synthflow/utils/__init__.py
|
|
24
|
+
synthflow/utils/inspect.py
|
|
25
|
+
synthflow/visualization/__init__.py
|
|
26
|
+
synthflow/visualization/graphviz.py
|
|
27
|
+
synthflow/visualization/printer.py
|
|
28
|
+
synthflow_py.egg-info/PKG-INFO
|
|
29
|
+
synthflow_py.egg-info/SOURCES.txt
|
|
30
|
+
synthflow_py.egg-info/dependency_links.txt
|
|
31
|
+
synthflow_py.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
synthflow
|