microoflow 0.1.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.
- microoflow/__init__.py +38 -0
- microoflow/__main__.py +7 -0
- microoflow/app.py +165 -0
- microoflow/cli.py +150 -0
- microoflow/events.py +135 -0
- microoflow/graph.py +194 -0
- microoflow/py.typed +1 -0
- microoflow/registry.py +319 -0
- microoflow/reloader.py +462 -0
- microoflow/runner.py +775 -0
- microoflow/server.py +208 -0
- microoflow/static/index.html +282 -0
- microoflow/static/sdk/canvas.js +990 -0
- microoflow/static/sdk/geometry.js +32 -0
- microoflow/static/sdk/minimap.js +208 -0
- microoflow/static/sdk/node_menu.js +480 -0
- microoflow/static/sdk/output_viewer.js +109 -0
- microoflow/static/sdk/renderers/base.js +174 -0
- microoflow/static/sdk/renderers/data_view.js +162 -0
- microoflow/static/sdk/renderers/html.js +694 -0
- microoflow/static/sdk/renderers/index.js +35 -0
- microoflow/static/sdk/renderers/markdown.js +622 -0
- microoflow/static/sdk/renderers/raw_csv.js +50 -0
- microoflow/static/sdk/renderers/raw_dict.js +123 -0
- microoflow/static/sdk/renderers/raw_json.js +71 -0
- microoflow/static/sdk/renderers/raw_list.js +84 -0
- microoflow/static/sdk/renderers/syntax.js +585 -0
- microoflow/static/sdk/renderers/table.js +323 -0
- microoflow/static/sdk/store.js +507 -0
- microoflow/static/sdk/viewport.js +165 -0
- microoflow/static/studio/app.js +689 -0
- microoflow/static/studio.css +1818 -0
- microoflow/views.py +102 -0
- microoflow-0.1.0.dist-info/METADATA +165 -0
- microoflow-0.1.0.dist-info/RECORD +38 -0
- microoflow-0.1.0.dist-info/WHEEL +4 -0
- microoflow-0.1.0.dist-info/entry_points.txt +2 -0
- microoflow-0.1.0.dist-info/licenses/LICENSE +22 -0
microoflow/registry.py
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import inspect
|
|
4
|
+
import json
|
|
5
|
+
import math
|
|
6
|
+
import types
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from typing import Annotated, Any, Callable, Union, get_args, get_origin
|
|
9
|
+
|
|
10
|
+
from pydantic import BaseModel, TypeAdapter
|
|
11
|
+
from pydantic_core import PydanticUndefined
|
|
12
|
+
|
|
13
|
+
from .views import _OutputView, find_view_classes
|
|
14
|
+
|
|
15
|
+
MAX_PORTS_PER_SIDE = 5
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class NodeContractError(ValueError):
|
|
19
|
+
"""Raised when a Pydantic node violates the Microoflow contract."""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(slots=True)
|
|
23
|
+
class PortSpec:
|
|
24
|
+
name: str
|
|
25
|
+
label: str
|
|
26
|
+
direction: str # "input" | "output"
|
|
27
|
+
type_kind: str # "str" | "int" | "float" | "bool" | "list" | "dict" | "any"
|
|
28
|
+
type_label: str
|
|
29
|
+
required: bool
|
|
30
|
+
has_default: bool
|
|
31
|
+
default: Any
|
|
32
|
+
adapter: TypeAdapter[Any]
|
|
33
|
+
default_factory: Callable[..., Any] | None = None
|
|
34
|
+
default_factory_takes_data: bool = False
|
|
35
|
+
|
|
36
|
+
def to_schema(self) -> dict[str, Any]:
|
|
37
|
+
return {
|
|
38
|
+
"name": self.name,
|
|
39
|
+
"label": self.label,
|
|
40
|
+
"direction": self.direction,
|
|
41
|
+
"type_kind": self.type_kind,
|
|
42
|
+
"type_label": self.type_label,
|
|
43
|
+
"required": self.required,
|
|
44
|
+
"has_default": self.has_default,
|
|
45
|
+
"default": _json_safe_default(self.default) if self.has_default else None,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass(slots=True)
|
|
50
|
+
class NodeSpec:
|
|
51
|
+
type_name: str
|
|
52
|
+
cls: type[BaseModel]
|
|
53
|
+
description: str
|
|
54
|
+
inputs: dict[str, PortSpec]
|
|
55
|
+
outputs: dict[str, PortSpec]
|
|
56
|
+
is_async: bool
|
|
57
|
+
view_kind: str | None = None
|
|
58
|
+
view_input_port: str | None = None
|
|
59
|
+
|
|
60
|
+
def to_schema(self) -> dict[str, Any]:
|
|
61
|
+
return {
|
|
62
|
+
"type_name": self.type_name,
|
|
63
|
+
"description": self.description,
|
|
64
|
+
"is_async": self.is_async,
|
|
65
|
+
"view_kind": self.view_kind,
|
|
66
|
+
"view_input_port": self.view_input_port,
|
|
67
|
+
"inputs": [p.to_schema() for p in self.inputs.values()],
|
|
68
|
+
"outputs": [p.to_schema() for p in self.outputs.values()],
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _strip_port_label(field_name: str, prefix: str) -> str:
|
|
73
|
+
if field_name.startswith(f"{prefix}_") and len(field_name) > len(prefix) + 1:
|
|
74
|
+
return field_name[len(prefix) + 1 :]
|
|
75
|
+
return field_name
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _classify_type(annotation: Any) -> tuple[str, str]:
|
|
79
|
+
if annotation is None or annotation is Any:
|
|
80
|
+
return "any", "Any"
|
|
81
|
+
|
|
82
|
+
origin = get_origin(annotation)
|
|
83
|
+
if origin is not None:
|
|
84
|
+
if origin is Annotated:
|
|
85
|
+
ann_args = get_args(annotation)
|
|
86
|
+
if ann_args:
|
|
87
|
+
return _classify_type(ann_args[0])
|
|
88
|
+
return "any", _format_type_label(annotation)
|
|
89
|
+
if origin is list:
|
|
90
|
+
return "list", _format_type_label(annotation)
|
|
91
|
+
if origin is dict:
|
|
92
|
+
return "dict", _format_type_label(annotation)
|
|
93
|
+
# Union / Optional (e.g., str | None) -> inspect non-None branch
|
|
94
|
+
if origin in (Union, types.UnionType):
|
|
95
|
+
args = [a for a in get_args(annotation) if a is not type(None)]
|
|
96
|
+
if len(args) == 1:
|
|
97
|
+
kind, _ = _classify_type(args[0])
|
|
98
|
+
return kind, _format_type_label(annotation)
|
|
99
|
+
return "any", _format_type_label(annotation)
|
|
100
|
+
|
|
101
|
+
if isinstance(annotation, type):
|
|
102
|
+
# Check bool before int because bool subclasses int in Python
|
|
103
|
+
if issubclass(annotation, bool):
|
|
104
|
+
return "bool", "bool"
|
|
105
|
+
if issubclass(annotation, int):
|
|
106
|
+
return "int", "int"
|
|
107
|
+
if issubclass(annotation, float):
|
|
108
|
+
return "float", "float"
|
|
109
|
+
if issubclass(annotation, str):
|
|
110
|
+
return "str", "str"
|
|
111
|
+
if issubclass(annotation, list):
|
|
112
|
+
return "list", "list"
|
|
113
|
+
if issubclass(annotation, dict):
|
|
114
|
+
return "dict", "dict"
|
|
115
|
+
return "any", annotation.__name__
|
|
116
|
+
|
|
117
|
+
return "any", _format_type_label(annotation)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _format_type_label(annotation: Any) -> str:
|
|
121
|
+
text = str(annotation)
|
|
122
|
+
return text.replace("typing.", "").replace("<class '", "").replace("'>", "")
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _unwrap_annotated(annotation: Any) -> Any:
|
|
126
|
+
while get_origin(annotation) is Annotated:
|
|
127
|
+
ann_args = get_args(annotation)
|
|
128
|
+
if not ann_args:
|
|
129
|
+
return Any
|
|
130
|
+
annotation = ann_args[0]
|
|
131
|
+
return annotation
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _validate_single_view_annotation(
|
|
135
|
+
cls_name: str,
|
|
136
|
+
port_name: str,
|
|
137
|
+
port_label: str,
|
|
138
|
+
annotation: Any,
|
|
139
|
+
view_cls: type[_OutputView],
|
|
140
|
+
) -> None:
|
|
141
|
+
unwrapped = _unwrap_annotated(annotation)
|
|
142
|
+
if unwrapped is Any:
|
|
143
|
+
return
|
|
144
|
+
|
|
145
|
+
origin = get_origin(unwrapped)
|
|
146
|
+
if origin in (Union, types.UnionType):
|
|
147
|
+
members = [a for a in get_args(unwrapped) if a is not type(None)]
|
|
148
|
+
if not members:
|
|
149
|
+
raise NodeContractError(
|
|
150
|
+
f"View node '{cls_name}' ({view_cls.__name__}) input '{port_name}' "
|
|
151
|
+
f"cannot accept None-only type '{port_label}'."
|
|
152
|
+
)
|
|
153
|
+
for member in members:
|
|
154
|
+
_validate_single_view_annotation(cls_name, port_name, port_label, member, view_cls)
|
|
155
|
+
return
|
|
156
|
+
|
|
157
|
+
kind, _ = _classify_type(unwrapped)
|
|
158
|
+
allowed = view_cls._microoflow_allowed_kinds
|
|
159
|
+
if kind not in allowed:
|
|
160
|
+
allowed_list = ", ".join(sorted(allowed))
|
|
161
|
+
raise NodeContractError(
|
|
162
|
+
f"View node '{cls_name}' ({view_cls.__name__}) input '{port_name}' "
|
|
163
|
+
f"type '{port_label}' ({kind}) is incompatible; allowed kinds: {allowed_list}."
|
|
164
|
+
)
|
|
165
|
+
if kind == "any" and not view_cls._microoflow_allow_any_object:
|
|
166
|
+
allowed_list = ", ".join(sorted(allowed))
|
|
167
|
+
raise NodeContractError(
|
|
168
|
+
f"View node '{cls_name}' ({view_cls.__name__}) input '{port_name}' "
|
|
169
|
+
f"type '{port_label}' is incompatible; allowed kinds: {allowed_list}."
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _json_safe_default(value: Any) -> Any:
|
|
174
|
+
if value is None or isinstance(value, (str, int, bool)):
|
|
175
|
+
return value
|
|
176
|
+
if isinstance(value, float):
|
|
177
|
+
return value if math.isfinite(value) else repr(value)
|
|
178
|
+
if isinstance(value, (list, dict)):
|
|
179
|
+
try:
|
|
180
|
+
json.dumps(value, allow_nan=False)
|
|
181
|
+
return value
|
|
182
|
+
except (TypeError, ValueError):
|
|
183
|
+
return repr(value)
|
|
184
|
+
return repr(value)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class NodeRegistry:
|
|
188
|
+
"""Stores introspected Pydantic node definitions and cached TypeAdapters."""
|
|
189
|
+
|
|
190
|
+
def __init__(self) -> None:
|
|
191
|
+
self._specs: dict[str, NodeSpec] = {}
|
|
192
|
+
|
|
193
|
+
def register(self, cls: type[BaseModel]) -> NodeSpec:
|
|
194
|
+
if not isinstance(cls, type) or not issubclass(cls, BaseModel):
|
|
195
|
+
raise NodeContractError(
|
|
196
|
+
f"'{cls}' must inherit from pydantic.BaseModel."
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
view_classes = find_view_classes(cls)
|
|
200
|
+
if len(view_classes) > 1:
|
|
201
|
+
names = ", ".join(v.__name__ for v in view_classes)
|
|
202
|
+
raise NodeContractError(
|
|
203
|
+
f"Node '{cls.__name__}' cannot inherit multiple output views: {names}."
|
|
204
|
+
)
|
|
205
|
+
view_cls = view_classes[0] if view_classes else None
|
|
206
|
+
|
|
207
|
+
execute_fn = getattr(cls, "execute", None)
|
|
208
|
+
if execute_fn is None or not callable(execute_fn):
|
|
209
|
+
raise NodeContractError(
|
|
210
|
+
f"Node '{cls.__name__}' must define an execute(self) method."
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
inputs: dict[str, PortSpec] = {}
|
|
214
|
+
outputs: dict[str, PortSpec] = {}
|
|
215
|
+
|
|
216
|
+
for name, field_info in cls.model_fields.items():
|
|
217
|
+
if name.startswith("input"):
|
|
218
|
+
direction = "input"
|
|
219
|
+
target_map = inputs
|
|
220
|
+
elif name.startswith("output"):
|
|
221
|
+
direction = "output"
|
|
222
|
+
target_map = outputs
|
|
223
|
+
else:
|
|
224
|
+
raise NodeContractError(
|
|
225
|
+
f"Node '{cls.__name__}' field '{name}' must start with "
|
|
226
|
+
f"'input' or 'output'."
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
annotation = field_info.annotation if field_info.annotation is not None else Any
|
|
230
|
+
type_kind, type_label = _classify_type(annotation)
|
|
231
|
+
|
|
232
|
+
has_default = field_info.default is not PydanticUndefined
|
|
233
|
+
default_factory = field_info.default_factory
|
|
234
|
+
has_factory = default_factory is not None
|
|
235
|
+
takes_data = bool(
|
|
236
|
+
getattr(field_info, "default_factory_takes_validated_data", False)
|
|
237
|
+
)
|
|
238
|
+
if has_default:
|
|
239
|
+
default_val = field_info.default
|
|
240
|
+
elif has_factory and not takes_data and default_factory is not None:
|
|
241
|
+
try:
|
|
242
|
+
default_val = default_factory()
|
|
243
|
+
except Exception:
|
|
244
|
+
default_val = None
|
|
245
|
+
else:
|
|
246
|
+
default_val = None
|
|
247
|
+
|
|
248
|
+
target_map[name] = PortSpec(
|
|
249
|
+
name=name,
|
|
250
|
+
label=_strip_port_label(name, direction),
|
|
251
|
+
direction=direction,
|
|
252
|
+
type_kind=type_kind,
|
|
253
|
+
type_label=type_label,
|
|
254
|
+
required=not (has_default or has_factory),
|
|
255
|
+
has_default=has_default or has_factory,
|
|
256
|
+
default=default_val,
|
|
257
|
+
adapter=TypeAdapter(annotation),
|
|
258
|
+
default_factory=default_factory,
|
|
259
|
+
default_factory_takes_data=takes_data,
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
if len(inputs) > MAX_PORTS_PER_SIDE:
|
|
263
|
+
raise NodeContractError(
|
|
264
|
+
f"Node '{cls.__name__}' defines {len(inputs)} inputs "
|
|
265
|
+
f"(maximum allowed is {MAX_PORTS_PER_SIDE})."
|
|
266
|
+
)
|
|
267
|
+
if len(outputs) > MAX_PORTS_PER_SIDE:
|
|
268
|
+
raise NodeContractError(
|
|
269
|
+
f"Node '{cls.__name__}' defines {len(outputs)} outputs "
|
|
270
|
+
f"(maximum allowed is {MAX_PORTS_PER_SIDE})."
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
view_kind: str | None = None
|
|
274
|
+
view_input_port: str | None = None
|
|
275
|
+
if view_cls is not None:
|
|
276
|
+
if not inputs:
|
|
277
|
+
raise NodeContractError(
|
|
278
|
+
f"View node '{cls.__name__}' ({view_cls.__name__}) must declare "
|
|
279
|
+
f"at least one 'input*' field."
|
|
280
|
+
)
|
|
281
|
+
view_input_port = "input_data" if "input_data" in inputs else next(iter(inputs))
|
|
282
|
+
primary_port = inputs[view_input_port]
|
|
283
|
+
primary_field = cls.model_fields[view_input_port]
|
|
284
|
+
primary_ann = (
|
|
285
|
+
primary_field.annotation if primary_field.annotation is not None else Any
|
|
286
|
+
)
|
|
287
|
+
_validate_single_view_annotation(
|
|
288
|
+
cls.__name__,
|
|
289
|
+
primary_port.name,
|
|
290
|
+
primary_port.type_label,
|
|
291
|
+
primary_ann,
|
|
292
|
+
view_cls,
|
|
293
|
+
)
|
|
294
|
+
view_kind = view_cls._microoflow_view_kind
|
|
295
|
+
|
|
296
|
+
spec = NodeSpec(
|
|
297
|
+
type_name=cls.__name__,
|
|
298
|
+
cls=cls,
|
|
299
|
+
description=inspect.getdoc(cls) or "",
|
|
300
|
+
inputs=inputs,
|
|
301
|
+
outputs=outputs,
|
|
302
|
+
is_async=inspect.iscoroutinefunction(execute_fn),
|
|
303
|
+
view_kind=view_kind,
|
|
304
|
+
view_input_port=view_input_port,
|
|
305
|
+
)
|
|
306
|
+
self._specs[spec.type_name] = spec
|
|
307
|
+
return spec
|
|
308
|
+
|
|
309
|
+
def get(self, type_name: str) -> NodeSpec:
|
|
310
|
+
try:
|
|
311
|
+
return self._specs[type_name]
|
|
312
|
+
except KeyError as exc:
|
|
313
|
+
raise NodeContractError(f"Unknown node type: '{type_name}'.") from exc
|
|
314
|
+
|
|
315
|
+
def all_specs(self) -> list[NodeSpec]:
|
|
316
|
+
return list(self._specs.values())
|
|
317
|
+
|
|
318
|
+
def export_schema(self) -> dict[str, Any]:
|
|
319
|
+
return {"nodes": [spec.to_schema() for spec in self._specs.values()]}
|