abstractflow 0.1.0__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.
- abstractflow/__init__.py +75 -95
- abstractflow/__main__.py +2 -0
- abstractflow/adapters/__init__.py +11 -0
- abstractflow/adapters/agent_adapter.py +124 -0
- abstractflow/adapters/control_adapter.py +615 -0
- abstractflow/adapters/effect_adapter.py +645 -0
- abstractflow/adapters/event_adapter.py +307 -0
- abstractflow/adapters/function_adapter.py +97 -0
- abstractflow/adapters/subflow_adapter.py +74 -0
- abstractflow/adapters/variable_adapter.py +317 -0
- abstractflow/cli.py +2 -0
- abstractflow/compiler.py +2027 -0
- abstractflow/core/__init__.py +5 -0
- abstractflow/core/flow.py +247 -0
- abstractflow/py.typed +2 -0
- abstractflow/runner.py +348 -0
- abstractflow/visual/__init__.py +43 -0
- abstractflow/visual/agent_ids.py +29 -0
- abstractflow/visual/builtins.py +789 -0
- abstractflow/visual/code_executor.py +214 -0
- abstractflow/visual/event_ids.py +33 -0
- abstractflow/visual/executor.py +2789 -0
- abstractflow/visual/interfaces.py +347 -0
- abstractflow/visual/models.py +252 -0
- abstractflow/visual/session_runner.py +168 -0
- abstractflow/visual/workspace_scoped_tools.py +261 -0
- abstractflow-0.3.0.dist-info/METADATA +413 -0
- abstractflow-0.3.0.dist-info/RECORD +32 -0
- {abstractflow-0.1.0.dist-info → abstractflow-0.3.0.dist-info}/licenses/LICENSE +2 -0
- abstractflow-0.1.0.dist-info/METADATA +0 -238
- abstractflow-0.1.0.dist-info/RECORD +0 -10
- {abstractflow-0.1.0.dist-info → abstractflow-0.3.0.dist-info}/WHEEL +0 -0
- {abstractflow-0.1.0.dist-info → abstractflow-0.3.0.dist-info}/entry_points.txt +0 -0
- {abstractflow-0.1.0.dist-info → abstractflow-0.3.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,2789 @@
|
|
|
1
|
+
"""Portable visual-flow execution utilities.
|
|
2
|
+
|
|
3
|
+
This module converts visual-editor flow JSON into an `abstractflow.Flow` and
|
|
4
|
+
provides a convenience `create_visual_runner()` that wires an AbstractRuntime
|
|
5
|
+
instance with the right integrations (LLM/MEMORY/SUBFLOW) for execution.
|
|
6
|
+
|
|
7
|
+
The goal is host portability: the same visual flow should run from non-web
|
|
8
|
+
hosts (AbstractCode, CLI) without importing the web backend implementation.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import os
|
|
14
|
+
from typing import Any, Dict, List, Optional
|
|
15
|
+
|
|
16
|
+
from ..core.flow import Flow
|
|
17
|
+
from ..runner import FlowRunner
|
|
18
|
+
|
|
19
|
+
from .builtins import get_builtin_handler
|
|
20
|
+
from .code_executor import create_code_handler
|
|
21
|
+
from .agent_ids import visual_react_workflow_id
|
|
22
|
+
from .models import NodeType, VisualEdge, VisualFlow
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# Type alias for data edge mapping
|
|
26
|
+
# Maps target_node_id -> { target_pin -> (source_node_id, source_pin) }
|
|
27
|
+
DataEdgeMap = Dict[str, Dict[str, tuple[str, str]]]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def create_visual_runner(
|
|
31
|
+
visual_flow: VisualFlow,
|
|
32
|
+
*,
|
|
33
|
+
flows: Dict[str, VisualFlow],
|
|
34
|
+
run_store: Optional[Any] = None,
|
|
35
|
+
ledger_store: Optional[Any] = None,
|
|
36
|
+
artifact_store: Optional[Any] = None,
|
|
37
|
+
tool_executor: Optional[Any] = None,
|
|
38
|
+
) -> FlowRunner:
|
|
39
|
+
"""Create a FlowRunner for a visual run with a correctly wired runtime.
|
|
40
|
+
|
|
41
|
+
Responsibilities:
|
|
42
|
+
- Build a WorkflowRegistry containing the root flow and any referenced subflows.
|
|
43
|
+
- Create a runtime with an ArtifactStore (required for MEMORY_* effects).
|
|
44
|
+
- If any LLM_CALL / Agent nodes exist in the flow tree, wire AbstractCore-backed
|
|
45
|
+
effect handlers (via AbstractRuntime's integration module).
|
|
46
|
+
"""
|
|
47
|
+
# Be resilient to different AbstractRuntime install layouts: not all exports
|
|
48
|
+
# are guaranteed to be re-exported from `abstractruntime.__init__`.
|
|
49
|
+
try:
|
|
50
|
+
from abstractruntime import Runtime # type: ignore
|
|
51
|
+
except Exception: # pragma: no cover
|
|
52
|
+
from abstractruntime.core.runtime import Runtime # type: ignore
|
|
53
|
+
|
|
54
|
+
try:
|
|
55
|
+
from abstractruntime import InMemoryRunStore, InMemoryLedgerStore # type: ignore
|
|
56
|
+
except Exception: # pragma: no cover
|
|
57
|
+
from abstractruntime.storage.in_memory import InMemoryRunStore, InMemoryLedgerStore # type: ignore
|
|
58
|
+
|
|
59
|
+
# Workflow registry is used for START_SUBWORKFLOW composition (subflows + Agent nodes).
|
|
60
|
+
#
|
|
61
|
+
# This project supports different AbstractRuntime distributions; some older installs
|
|
62
|
+
# may not expose WorkflowRegistry. In that case, fall back to a tiny in-process
|
|
63
|
+
# dict-based registry with the same `.register()` + `.get()` surface.
|
|
64
|
+
try:
|
|
65
|
+
from abstractruntime import WorkflowRegistry # type: ignore
|
|
66
|
+
except Exception: # pragma: no cover
|
|
67
|
+
try:
|
|
68
|
+
from abstractruntime.scheduler.registry import WorkflowRegistry # type: ignore
|
|
69
|
+
except Exception: # pragma: no cover
|
|
70
|
+
from abstractruntime.core.spec import WorkflowSpec # type: ignore
|
|
71
|
+
|
|
72
|
+
class WorkflowRegistry(dict): # type: ignore[no-redef]
|
|
73
|
+
def register(self, workflow: "WorkflowSpec") -> None:
|
|
74
|
+
self[str(workflow.workflow_id)] = workflow
|
|
75
|
+
|
|
76
|
+
from ..compiler import compile_flow
|
|
77
|
+
from .event_ids import visual_event_listener_workflow_id
|
|
78
|
+
from .session_runner import VisualSessionRunner
|
|
79
|
+
|
|
80
|
+
def _node_type(node: Any) -> str:
|
|
81
|
+
t = getattr(node, "type", None)
|
|
82
|
+
return t.value if hasattr(t, "value") else str(t)
|
|
83
|
+
|
|
84
|
+
def _reachable_exec_node_ids(vf: VisualFlow) -> set[str]:
|
|
85
|
+
"""Return execution-reachable node ids (within this VisualFlow only).
|
|
86
|
+
|
|
87
|
+
We consider only the *execution graph* (exec edges: targetHandle=exec-in).
|
|
88
|
+
Disconnected/isolated execution nodes are ignored (Blueprint-style).
|
|
89
|
+
"""
|
|
90
|
+
EXEC_TYPES: set[str] = {
|
|
91
|
+
# Triggers / core exec
|
|
92
|
+
"on_flow_start",
|
|
93
|
+
"on_user_request",
|
|
94
|
+
"on_agent_message",
|
|
95
|
+
"on_schedule",
|
|
96
|
+
"on_event",
|
|
97
|
+
"on_flow_end",
|
|
98
|
+
"agent",
|
|
99
|
+
"function",
|
|
100
|
+
"code",
|
|
101
|
+
"subflow",
|
|
102
|
+
# Workflow variables (execution setter)
|
|
103
|
+
"set_var",
|
|
104
|
+
"set_vars",
|
|
105
|
+
"set_var_property",
|
|
106
|
+
# Control exec
|
|
107
|
+
"if",
|
|
108
|
+
"switch",
|
|
109
|
+
"loop",
|
|
110
|
+
"while",
|
|
111
|
+
"for",
|
|
112
|
+
"sequence",
|
|
113
|
+
"parallel",
|
|
114
|
+
# Effects
|
|
115
|
+
"ask_user",
|
|
116
|
+
"answer_user",
|
|
117
|
+
"llm_call",
|
|
118
|
+
"tool_calls",
|
|
119
|
+
"wait_until",
|
|
120
|
+
"wait_event",
|
|
121
|
+
"emit_event",
|
|
122
|
+
"read_file",
|
|
123
|
+
"write_file",
|
|
124
|
+
"memory_note",
|
|
125
|
+
"memory_query",
|
|
126
|
+
"memory_rehydrate",
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
node_types: Dict[str, str] = {n.id: _node_type(n) for n in vf.nodes}
|
|
130
|
+
exec_ids = {nid for nid, t in node_types.items() if t in EXEC_TYPES}
|
|
131
|
+
if not exec_ids:
|
|
132
|
+
return set()
|
|
133
|
+
|
|
134
|
+
incoming_exec = {e.target for e in vf.edges if getattr(e, "targetHandle", None) == "exec-in"}
|
|
135
|
+
|
|
136
|
+
roots: list[str] = []
|
|
137
|
+
if isinstance(vf.entryNode, str) and vf.entryNode in exec_ids:
|
|
138
|
+
roots.append(vf.entryNode)
|
|
139
|
+
# Custom events are independent entrypoints; include them as roots for "executable" reachability.
|
|
140
|
+
for n in vf.nodes:
|
|
141
|
+
if n.id in exec_ids and node_types.get(n.id) == "on_event":
|
|
142
|
+
roots.append(n.id)
|
|
143
|
+
|
|
144
|
+
if not roots:
|
|
145
|
+
# Fallback: infer a single root as "exec node with no incoming edge".
|
|
146
|
+
for n in vf.nodes:
|
|
147
|
+
if n.id in exec_ids and n.id not in incoming_exec:
|
|
148
|
+
roots.append(n.id)
|
|
149
|
+
break
|
|
150
|
+
if not roots:
|
|
151
|
+
roots.append(next(iter(exec_ids)))
|
|
152
|
+
|
|
153
|
+
adj: Dict[str, list[str]] = {}
|
|
154
|
+
for e in vf.edges:
|
|
155
|
+
if getattr(e, "targetHandle", None) != "exec-in":
|
|
156
|
+
continue
|
|
157
|
+
if e.source not in exec_ids or e.target not in exec_ids:
|
|
158
|
+
continue
|
|
159
|
+
adj.setdefault(e.source, []).append(e.target)
|
|
160
|
+
|
|
161
|
+
reachable: set[str] = set()
|
|
162
|
+
stack2 = list(dict.fromkeys([r for r in roots if isinstance(r, str) and r]))
|
|
163
|
+
while stack2:
|
|
164
|
+
cur = stack2.pop()
|
|
165
|
+
if cur in reachable:
|
|
166
|
+
continue
|
|
167
|
+
reachable.add(cur)
|
|
168
|
+
for nxt in adj.get(cur, []):
|
|
169
|
+
if nxt not in reachable:
|
|
170
|
+
stack2.append(nxt)
|
|
171
|
+
return reachable
|
|
172
|
+
|
|
173
|
+
# Collect all reachable flows (root + transitive subflows).
|
|
174
|
+
#
|
|
175
|
+
# Important: subflows are executed via runtime `START_SUBWORKFLOW` by workflow id.
|
|
176
|
+
# This means subflow cycles (including self-recursion) are valid and should not be
|
|
177
|
+
# rejected at runner-wiring time; we only need to register each workflow id once.
|
|
178
|
+
ordered: list[VisualFlow] = []
|
|
179
|
+
visited: set[str] = set()
|
|
180
|
+
|
|
181
|
+
def _dfs(vf: VisualFlow) -> None:
|
|
182
|
+
if vf.id in visited:
|
|
183
|
+
return
|
|
184
|
+
visited.add(vf.id)
|
|
185
|
+
ordered.append(vf)
|
|
186
|
+
|
|
187
|
+
reachable = _reachable_exec_node_ids(vf)
|
|
188
|
+
for n in vf.nodes:
|
|
189
|
+
node_type = _node_type(n)
|
|
190
|
+
if node_type != "subflow":
|
|
191
|
+
continue
|
|
192
|
+
if reachable and n.id not in reachable:
|
|
193
|
+
continue
|
|
194
|
+
subflow_id = n.data.get("subflowId") or n.data.get("flowId") # legacy
|
|
195
|
+
if not isinstance(subflow_id, str) or not subflow_id.strip():
|
|
196
|
+
raise ValueError(f"Subflow node '{n.id}' missing subflowId")
|
|
197
|
+
subflow_id = subflow_id.strip()
|
|
198
|
+
child = flows.get(subflow_id)
|
|
199
|
+
# Self-recursion should work even if `flows` does not redundantly include this vf.
|
|
200
|
+
if child is None and subflow_id == vf.id:
|
|
201
|
+
child = vf
|
|
202
|
+
if child is None:
|
|
203
|
+
raise ValueError(f"Referenced subflow '{subflow_id}' not found")
|
|
204
|
+
_dfs(child)
|
|
205
|
+
|
|
206
|
+
_dfs(visual_flow)
|
|
207
|
+
|
|
208
|
+
# Detect optional runtime features needed by this flow tree.
|
|
209
|
+
# These flags keep `create_visual_runner()` resilient to older AbstractRuntime installs.
|
|
210
|
+
needs_registry = False
|
|
211
|
+
needs_artifacts = False
|
|
212
|
+
for vf in ordered:
|
|
213
|
+
reachable = _reachable_exec_node_ids(vf)
|
|
214
|
+
for n in vf.nodes:
|
|
215
|
+
if reachable and n.id not in reachable:
|
|
216
|
+
continue
|
|
217
|
+
t = _node_type(n)
|
|
218
|
+
if t in {"subflow", "agent"}:
|
|
219
|
+
needs_registry = True
|
|
220
|
+
if t in {"on_event", "emit_event"}:
|
|
221
|
+
needs_registry = True
|
|
222
|
+
if t in {"memory_note", "memory_query", "memory_rehydrate"}:
|
|
223
|
+
needs_artifacts = True
|
|
224
|
+
|
|
225
|
+
# Detect whether this flow tree needs AbstractCore LLM integration.
|
|
226
|
+
# Provider/model can be supplied either via node config *or* via connected input pins.
|
|
227
|
+
has_llm_nodes = False
|
|
228
|
+
llm_configs: set[tuple[str, str]] = set()
|
|
229
|
+
default_llm: tuple[str, str] | None = None
|
|
230
|
+
provider_hints: list[str] = []
|
|
231
|
+
|
|
232
|
+
def _pin_connected(vf: VisualFlow, *, node_id: str, pin_id: str) -> bool:
|
|
233
|
+
for e in vf.edges:
|
|
234
|
+
try:
|
|
235
|
+
if e.target == node_id and e.targetHandle == pin_id:
|
|
236
|
+
return True
|
|
237
|
+
except Exception:
|
|
238
|
+
continue
|
|
239
|
+
return False
|
|
240
|
+
|
|
241
|
+
def _add_pair(provider_raw: Any, model_raw: Any) -> None:
|
|
242
|
+
nonlocal default_llm
|
|
243
|
+
if not isinstance(provider_raw, str) or not provider_raw.strip():
|
|
244
|
+
return
|
|
245
|
+
if not isinstance(model_raw, str) or not model_raw.strip():
|
|
246
|
+
return
|
|
247
|
+
pair = (provider_raw.strip().lower(), model_raw.strip())
|
|
248
|
+
llm_configs.add(pair)
|
|
249
|
+
if default_llm is None:
|
|
250
|
+
default_llm = pair
|
|
251
|
+
|
|
252
|
+
for vf in ordered:
|
|
253
|
+
reachable = _reachable_exec_node_ids(vf)
|
|
254
|
+
for n in vf.nodes:
|
|
255
|
+
node_type = _node_type(n)
|
|
256
|
+
if reachable and n.id not in reachable:
|
|
257
|
+
continue
|
|
258
|
+
if node_type in {"llm_call", "agent", "tool_calls"}:
|
|
259
|
+
has_llm_nodes = True
|
|
260
|
+
|
|
261
|
+
if node_type == "llm_call":
|
|
262
|
+
cfg = n.data.get("effectConfig", {}) if isinstance(n.data, dict) else {}
|
|
263
|
+
cfg = cfg if isinstance(cfg, dict) else {}
|
|
264
|
+
provider = cfg.get("provider")
|
|
265
|
+
model = cfg.get("model")
|
|
266
|
+
|
|
267
|
+
provider_ok = isinstance(provider, str) and provider.strip()
|
|
268
|
+
model_ok = isinstance(model, str) and model.strip()
|
|
269
|
+
provider_connected = _pin_connected(vf, node_id=n.id, pin_id="provider")
|
|
270
|
+
model_connected = _pin_connected(vf, node_id=n.id, pin_id="model")
|
|
271
|
+
|
|
272
|
+
if not provider_ok and not provider_connected:
|
|
273
|
+
raise ValueError(
|
|
274
|
+
f"LLM_CALL node '{n.id}' in flow '{vf.id}' missing provider "
|
|
275
|
+
"(set effectConfig.provider or connect the provider input pin)"
|
|
276
|
+
)
|
|
277
|
+
if not model_ok and not model_connected:
|
|
278
|
+
raise ValueError(
|
|
279
|
+
f"LLM_CALL node '{n.id}' in flow '{vf.id}' missing model "
|
|
280
|
+
"(set effectConfig.model or connect the model input pin)"
|
|
281
|
+
)
|
|
282
|
+
_add_pair(provider, model)
|
|
283
|
+
|
|
284
|
+
elif node_type == "agent":
|
|
285
|
+
cfg = n.data.get("agentConfig", {}) if isinstance(n.data, dict) else {}
|
|
286
|
+
cfg = cfg if isinstance(cfg, dict) else {}
|
|
287
|
+
provider = cfg.get("provider")
|
|
288
|
+
model = cfg.get("model")
|
|
289
|
+
|
|
290
|
+
provider_ok = isinstance(provider, str) and provider.strip()
|
|
291
|
+
model_ok = isinstance(model, str) and model.strip()
|
|
292
|
+
provider_connected = _pin_connected(vf, node_id=n.id, pin_id="provider")
|
|
293
|
+
model_connected = _pin_connected(vf, node_id=n.id, pin_id="model")
|
|
294
|
+
|
|
295
|
+
if not provider_ok and not provider_connected:
|
|
296
|
+
raise ValueError(
|
|
297
|
+
f"Agent node '{n.id}' in flow '{vf.id}' missing provider "
|
|
298
|
+
"(set agentConfig.provider or connect the provider input pin)"
|
|
299
|
+
)
|
|
300
|
+
if not model_ok and not model_connected:
|
|
301
|
+
raise ValueError(
|
|
302
|
+
f"Agent node '{n.id}' in flow '{vf.id}' missing model "
|
|
303
|
+
"(set agentConfig.model or connect the model input pin)"
|
|
304
|
+
)
|
|
305
|
+
_add_pair(provider, model)
|
|
306
|
+
|
|
307
|
+
elif node_type == "provider_models":
|
|
308
|
+
cfg = n.data.get("providerModelsConfig", {}) if isinstance(n.data, dict) else {}
|
|
309
|
+
cfg = cfg if isinstance(cfg, dict) else {}
|
|
310
|
+
provider = cfg.get("provider")
|
|
311
|
+
if isinstance(provider, str) and provider.strip():
|
|
312
|
+
provider_hints.append(provider.strip().lower())
|
|
313
|
+
allowed = cfg.get("allowedModels")
|
|
314
|
+
if not isinstance(allowed, list):
|
|
315
|
+
allowed = cfg.get("allowed_models")
|
|
316
|
+
if isinstance(allowed, list):
|
|
317
|
+
for m in allowed:
|
|
318
|
+
_add_pair(provider, m)
|
|
319
|
+
|
|
320
|
+
if has_llm_nodes:
|
|
321
|
+
provider_model = default_llm
|
|
322
|
+
if provider_model is None and provider_hints:
|
|
323
|
+
# If the graph contains a provider selection node, prefer it for the runtime default.
|
|
324
|
+
try:
|
|
325
|
+
from abstractcore.providers.registry import get_available_models_for_provider
|
|
326
|
+
except Exception:
|
|
327
|
+
get_available_models_for_provider = None # type: ignore[assignment]
|
|
328
|
+
if callable(get_available_models_for_provider):
|
|
329
|
+
for p in provider_hints:
|
|
330
|
+
try:
|
|
331
|
+
models = get_available_models_for_provider(p)
|
|
332
|
+
except Exception:
|
|
333
|
+
models = []
|
|
334
|
+
if isinstance(models, list):
|
|
335
|
+
first = next((m for m in models if isinstance(m, str) and m.strip()), None)
|
|
336
|
+
if first:
|
|
337
|
+
provider_model = (p, first.strip())
|
|
338
|
+
break
|
|
339
|
+
|
|
340
|
+
if provider_model is None:
|
|
341
|
+
# Fall back to the first available provider/model from AbstractCore.
|
|
342
|
+
try:
|
|
343
|
+
from abstractcore.providers.registry import get_all_providers_with_models
|
|
344
|
+
|
|
345
|
+
providers_meta = get_all_providers_with_models(include_models=True)
|
|
346
|
+
for p in providers_meta:
|
|
347
|
+
if not isinstance(p, dict):
|
|
348
|
+
continue
|
|
349
|
+
if p.get("status") != "available":
|
|
350
|
+
continue
|
|
351
|
+
name = p.get("name")
|
|
352
|
+
models = p.get("models")
|
|
353
|
+
if not isinstance(name, str) or not name.strip():
|
|
354
|
+
continue
|
|
355
|
+
if not isinstance(models, list):
|
|
356
|
+
continue
|
|
357
|
+
first = next((m for m in models if isinstance(m, str) and m.strip()), None)
|
|
358
|
+
if first:
|
|
359
|
+
provider_model = (name.strip().lower(), first.strip())
|
|
360
|
+
break
|
|
361
|
+
except Exception:
|
|
362
|
+
provider_model = None
|
|
363
|
+
|
|
364
|
+
if provider_model is None:
|
|
365
|
+
raise RuntimeError(
|
|
366
|
+
"This flow uses LLM nodes (llm_call/agent), but no provider/model could be determined. "
|
|
367
|
+
"Either set provider/model on a node, connect provider+model pins, or ensure AbstractCore "
|
|
368
|
+
"has at least one available provider with models."
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
provider, model = provider_model
|
|
372
|
+
try:
|
|
373
|
+
from abstractruntime.integrations.abstractcore.factory import create_local_runtime
|
|
374
|
+
# Older/newer AbstractRuntime distributions expose tool executors differently.
|
|
375
|
+
# Tool execution is not required for plain LLM_CALL-only flows, so we make
|
|
376
|
+
# this optional and fall back to the factory defaults.
|
|
377
|
+
try:
|
|
378
|
+
from abstractruntime.integrations.abstractcore import MappingToolExecutor # type: ignore
|
|
379
|
+
except Exception: # pragma: no cover
|
|
380
|
+
try:
|
|
381
|
+
from abstractruntime.integrations.abstractcore.tool_executor import MappingToolExecutor # type: ignore
|
|
382
|
+
except Exception: # pragma: no cover
|
|
383
|
+
MappingToolExecutor = None # type: ignore[assignment]
|
|
384
|
+
try:
|
|
385
|
+
from abstractruntime.integrations.abstractcore.default_tools import get_default_tools # type: ignore
|
|
386
|
+
except Exception: # pragma: no cover
|
|
387
|
+
get_default_tools = None # type: ignore[assignment]
|
|
388
|
+
except Exception as e: # pragma: no cover
|
|
389
|
+
raise RuntimeError(
|
|
390
|
+
"This flow uses LLM nodes (llm_call/agent), but the installed AbstractRuntime "
|
|
391
|
+
"does not provide the AbstractCore integration. Install/enable the integration "
|
|
392
|
+
"or remove LLM nodes from the flow."
|
|
393
|
+
) from e
|
|
394
|
+
|
|
395
|
+
effective_tool_executor = tool_executor
|
|
396
|
+
if effective_tool_executor is None and MappingToolExecutor is not None and callable(get_default_tools):
|
|
397
|
+
try:
|
|
398
|
+
effective_tool_executor = MappingToolExecutor.from_tools(get_default_tools()) # type: ignore[attr-defined]
|
|
399
|
+
except Exception:
|
|
400
|
+
effective_tool_executor = None
|
|
401
|
+
|
|
402
|
+
# LLM timeout policy (web-hosted workflow execution).
|
|
403
|
+
#
|
|
404
|
+
# Contract:
|
|
405
|
+
# - AbstractRuntime (the orchestrator) is the authority for execution policy such as timeouts.
|
|
406
|
+
# - This host can *override* that policy via env for deployments that want a different SLO.
|
|
407
|
+
#
|
|
408
|
+
# Env overrides:
|
|
409
|
+
# - ABSTRACTFLOW_LLM_TIMEOUT_S (float seconds)
|
|
410
|
+
# - ABSTRACTFLOW_LLM_TIMEOUT (alias)
|
|
411
|
+
#
|
|
412
|
+
# Set to 0 or a negative value to opt into "unlimited".
|
|
413
|
+
llm_kwargs: Dict[str, Any] = {}
|
|
414
|
+
timeout_raw = os.getenv("ABSTRACTFLOW_LLM_TIMEOUT_S") or os.getenv("ABSTRACTFLOW_LLM_TIMEOUT")
|
|
415
|
+
if timeout_raw is None or not str(timeout_raw).strip():
|
|
416
|
+
# No override: let the orchestrator (AbstractRuntime) apply its default.
|
|
417
|
+
pass
|
|
418
|
+
else:
|
|
419
|
+
raw = str(timeout_raw).strip().lower()
|
|
420
|
+
if raw in {"none", "null", "inf", "infinite", "unlimited"}:
|
|
421
|
+
# Explicit override: opt back into unlimited HTTP requests.
|
|
422
|
+
llm_kwargs["timeout"] = None
|
|
423
|
+
else:
|
|
424
|
+
try:
|
|
425
|
+
timeout_s = float(raw)
|
|
426
|
+
except Exception:
|
|
427
|
+
timeout_s = None
|
|
428
|
+
# Only override when parsing succeeded; otherwise fall back to AbstractCore config default.
|
|
429
|
+
if timeout_s is None:
|
|
430
|
+
pass
|
|
431
|
+
elif isinstance(timeout_s, (int, float)) and timeout_s <= 0:
|
|
432
|
+
# Consistent with the documented behavior: <=0 => unlimited.
|
|
433
|
+
llm_kwargs["timeout"] = None
|
|
434
|
+
else:
|
|
435
|
+
llm_kwargs["timeout"] = timeout_s
|
|
436
|
+
|
|
437
|
+
# Default output token cap for web-hosted runs.
|
|
438
|
+
#
|
|
439
|
+
# Without an explicit max_output_tokens, agent-style loops can produce very long
|
|
440
|
+
# responses that are both slow (local inference) and unhelpful for a visual UI
|
|
441
|
+
# (tools should write files; the model should not dump huge blobs into chat).
|
|
442
|
+
max_out_raw = os.getenv("ABSTRACTFLOW_LLM_MAX_OUTPUT_TOKENS") or os.getenv("ABSTRACTFLOW_MAX_OUTPUT_TOKENS")
|
|
443
|
+
max_out: Optional[int] = None
|
|
444
|
+
if max_out_raw is None or not str(max_out_raw).strip():
|
|
445
|
+
max_out = 4096
|
|
446
|
+
else:
|
|
447
|
+
try:
|
|
448
|
+
max_out = int(str(max_out_raw).strip())
|
|
449
|
+
except Exception:
|
|
450
|
+
max_out = 4096
|
|
451
|
+
if isinstance(max_out, int) and max_out <= 0:
|
|
452
|
+
max_out = None
|
|
453
|
+
|
|
454
|
+
# Pass runtime config to initialize `_limits.max_output_tokens`.
|
|
455
|
+
try:
|
|
456
|
+
from abstractruntime.core.config import RuntimeConfig
|
|
457
|
+
runtime_config = RuntimeConfig(max_output_tokens=max_out)
|
|
458
|
+
except Exception: # pragma: no cover
|
|
459
|
+
runtime_config = None
|
|
460
|
+
|
|
461
|
+
runtime = create_local_runtime(
|
|
462
|
+
provider=provider,
|
|
463
|
+
model=model,
|
|
464
|
+
llm_kwargs=llm_kwargs,
|
|
465
|
+
tool_executor=effective_tool_executor,
|
|
466
|
+
run_store=run_store,
|
|
467
|
+
ledger_store=ledger_store,
|
|
468
|
+
artifact_store=artifact_store,
|
|
469
|
+
config=runtime_config,
|
|
470
|
+
)
|
|
471
|
+
else:
|
|
472
|
+
runtime_kwargs: Dict[str, Any] = {
|
|
473
|
+
"run_store": run_store or InMemoryRunStore(),
|
|
474
|
+
"ledger_store": ledger_store or InMemoryLedgerStore(),
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if needs_artifacts:
|
|
478
|
+
# MEMORY_* effects require an ArtifactStore. Only configure it when needed.
|
|
479
|
+
artifact_store_obj: Any = artifact_store
|
|
480
|
+
if artifact_store_obj is None:
|
|
481
|
+
try:
|
|
482
|
+
from abstractruntime import InMemoryArtifactStore # type: ignore
|
|
483
|
+
artifact_store_obj = InMemoryArtifactStore()
|
|
484
|
+
except Exception: # pragma: no cover
|
|
485
|
+
try:
|
|
486
|
+
from abstractruntime.storage.artifacts import InMemoryArtifactStore # type: ignore
|
|
487
|
+
artifact_store_obj = InMemoryArtifactStore()
|
|
488
|
+
except Exception as e: # pragma: no cover
|
|
489
|
+
raise RuntimeError(
|
|
490
|
+
"This flow uses MEMORY_* nodes, but the installed AbstractRuntime "
|
|
491
|
+
"does not provide an ArtifactStore implementation."
|
|
492
|
+
) from e
|
|
493
|
+
|
|
494
|
+
# Only pass artifact_store if the runtime supports it (older runtimes may not).
|
|
495
|
+
try:
|
|
496
|
+
from inspect import signature
|
|
497
|
+
|
|
498
|
+
if "artifact_store" in signature(Runtime).parameters:
|
|
499
|
+
runtime_kwargs["artifact_store"] = artifact_store_obj
|
|
500
|
+
except Exception: # pragma: no cover
|
|
501
|
+
# Best-effort: attempt to set via method if present.
|
|
502
|
+
pass
|
|
503
|
+
|
|
504
|
+
runtime = Runtime(**runtime_kwargs)
|
|
505
|
+
|
|
506
|
+
# Best-effort: configure artifact store via setter if supported.
|
|
507
|
+
if needs_artifacts and "artifact_store" not in runtime_kwargs and hasattr(runtime, "set_artifact_store"):
|
|
508
|
+
try:
|
|
509
|
+
runtime.set_artifact_store(artifact_store_obj) # type: ignore[name-defined]
|
|
510
|
+
except Exception:
|
|
511
|
+
pass
|
|
512
|
+
|
|
513
|
+
flow = visual_to_flow(visual_flow)
|
|
514
|
+
# Build and register custom event listener workflows (On Event nodes).
|
|
515
|
+
event_listener_specs: list[Any] = []
|
|
516
|
+
if needs_registry:
|
|
517
|
+
try:
|
|
518
|
+
from .agent_ids import visual_react_workflow_id
|
|
519
|
+
except Exception: # pragma: no cover
|
|
520
|
+
visual_react_workflow_id = None # type: ignore[assignment]
|
|
521
|
+
|
|
522
|
+
for vf in ordered:
|
|
523
|
+
reachable = _reachable_exec_node_ids(vf)
|
|
524
|
+
for n in vf.nodes:
|
|
525
|
+
if _node_type(n) != "on_event":
|
|
526
|
+
continue
|
|
527
|
+
# On Event nodes are roots by definition (even if disconnected from the main entry).
|
|
528
|
+
if reachable and n.id not in reachable:
|
|
529
|
+
continue
|
|
530
|
+
|
|
531
|
+
workflow_id = visual_event_listener_workflow_id(flow_id=vf.id, node_id=n.id)
|
|
532
|
+
|
|
533
|
+
# Create a derived VisualFlow for this listener workflow:
|
|
534
|
+
# - workflow id is unique (so it can be registered)
|
|
535
|
+
# - entryNode is the on_event node
|
|
536
|
+
derived = vf.model_copy(deep=True)
|
|
537
|
+
derived.id = workflow_id
|
|
538
|
+
derived.entryNode = n.id
|
|
539
|
+
|
|
540
|
+
# Ensure Agent nodes inside this derived workflow reference the canonical
|
|
541
|
+
# ReAct workflow IDs based on the *source* flow id, not the derived id.
|
|
542
|
+
if callable(visual_react_workflow_id):
|
|
543
|
+
for dn in derived.nodes:
|
|
544
|
+
if _node_type(dn) != "agent":
|
|
545
|
+
continue
|
|
546
|
+
raw_cfg = dn.data.get("agentConfig", {}) if isinstance(dn.data, dict) else {}
|
|
547
|
+
cfg = dict(raw_cfg) if isinstance(raw_cfg, dict) else {}
|
|
548
|
+
cfg.setdefault(
|
|
549
|
+
"_react_workflow_id",
|
|
550
|
+
visual_react_workflow_id(flow_id=vf.id, node_id=dn.id),
|
|
551
|
+
)
|
|
552
|
+
dn.data["agentConfig"] = cfg
|
|
553
|
+
|
|
554
|
+
listener_flow = visual_to_flow(derived)
|
|
555
|
+
listener_spec = compile_flow(listener_flow)
|
|
556
|
+
event_listener_specs.append(listener_spec)
|
|
557
|
+
runner: FlowRunner
|
|
558
|
+
if event_listener_specs:
|
|
559
|
+
runner = VisualSessionRunner(flow, runtime=runtime, event_listener_specs=event_listener_specs)
|
|
560
|
+
else:
|
|
561
|
+
runner = FlowRunner(flow, runtime=runtime)
|
|
562
|
+
|
|
563
|
+
if needs_registry:
|
|
564
|
+
registry = WorkflowRegistry()
|
|
565
|
+
registry.register(runner.workflow)
|
|
566
|
+
for vf in ordered[1:]:
|
|
567
|
+
child_flow = visual_to_flow(vf)
|
|
568
|
+
child_spec = compile_flow(child_flow)
|
|
569
|
+
registry.register(child_spec)
|
|
570
|
+
for spec in event_listener_specs:
|
|
571
|
+
registry.register(spec)
|
|
572
|
+
|
|
573
|
+
# Register per-Agent-node subworkflows (canonical AbstractAgent ReAct).
|
|
574
|
+
#
|
|
575
|
+
# Visual Agent nodes compile into START_SUBWORKFLOW effects that reference a
|
|
576
|
+
# deterministic workflow_id. The registry must contain those WorkflowSpecs.
|
|
577
|
+
#
|
|
578
|
+
# This keeps VisualFlow JSON portable across hosts: any host can run a
|
|
579
|
+
# VisualFlow document by registering these derived specs alongside the flow.
|
|
580
|
+
agent_nodes: list[tuple[str, Dict[str, Any]]] = []
|
|
581
|
+
for vf in ordered:
|
|
582
|
+
for n in vf.nodes:
|
|
583
|
+
node_type = _node_type(n)
|
|
584
|
+
if node_type != "agent":
|
|
585
|
+
continue
|
|
586
|
+
cfg = n.data.get("agentConfig", {})
|
|
587
|
+
agent_nodes.append((visual_react_workflow_id(flow_id=vf.id, node_id=n.id), cfg if isinstance(cfg, dict) else {}))
|
|
588
|
+
|
|
589
|
+
if agent_nodes:
|
|
590
|
+
try:
|
|
591
|
+
from abstractagent.adapters.react_runtime import create_react_workflow
|
|
592
|
+
from abstractagent.logic.react import ReActLogic
|
|
593
|
+
except Exception as e: # pragma: no cover
|
|
594
|
+
raise RuntimeError(
|
|
595
|
+
"Visual Agent nodes require AbstractAgent to be installed/importable."
|
|
596
|
+
) from e
|
|
597
|
+
|
|
598
|
+
from abstractcore.tools import ToolDefinition
|
|
599
|
+
from abstractruntime.integrations.abstractcore.default_tools import list_default_tool_specs
|
|
600
|
+
|
|
601
|
+
def _tool_defs_from_specs(specs: list[dict[str, Any]]) -> list[ToolDefinition]:
|
|
602
|
+
out: list[ToolDefinition] = []
|
|
603
|
+
for s in specs:
|
|
604
|
+
if not isinstance(s, dict):
|
|
605
|
+
continue
|
|
606
|
+
name = s.get("name")
|
|
607
|
+
if not isinstance(name, str) or not name.strip():
|
|
608
|
+
continue
|
|
609
|
+
desc = s.get("description")
|
|
610
|
+
params = s.get("parameters")
|
|
611
|
+
out.append(
|
|
612
|
+
ToolDefinition(
|
|
613
|
+
name=name.strip(),
|
|
614
|
+
description=str(desc or ""),
|
|
615
|
+
parameters=dict(params) if isinstance(params, dict) else {},
|
|
616
|
+
)
|
|
617
|
+
)
|
|
618
|
+
return out
|
|
619
|
+
|
|
620
|
+
def _normalize_tool_names(raw: Any) -> list[str]:
|
|
621
|
+
if not isinstance(raw, list):
|
|
622
|
+
return []
|
|
623
|
+
out: list[str] = []
|
|
624
|
+
for t in raw:
|
|
625
|
+
if isinstance(t, str) and t.strip():
|
|
626
|
+
out.append(t.strip())
|
|
627
|
+
return out
|
|
628
|
+
|
|
629
|
+
all_tool_defs = _tool_defs_from_specs(list_default_tool_specs())
|
|
630
|
+
# Add schema-only runtime tools (executed as runtime effects by AbstractAgent adapters).
|
|
631
|
+
try:
|
|
632
|
+
from abstractagent.logic.builtins import ( # type: ignore
|
|
633
|
+
ASK_USER_TOOL,
|
|
634
|
+
COMPACT_MEMORY_TOOL,
|
|
635
|
+
INSPECT_VARS_TOOL,
|
|
636
|
+
RECALL_MEMORY_TOOL,
|
|
637
|
+
REMEMBER_TOOL,
|
|
638
|
+
)
|
|
639
|
+
|
|
640
|
+
builtin_defs = [ASK_USER_TOOL, RECALL_MEMORY_TOOL, INSPECT_VARS_TOOL, REMEMBER_TOOL, COMPACT_MEMORY_TOOL]
|
|
641
|
+
seen_names = {t.name for t in all_tool_defs if getattr(t, "name", None)}
|
|
642
|
+
for t in builtin_defs:
|
|
643
|
+
if getattr(t, "name", None) and t.name not in seen_names:
|
|
644
|
+
all_tool_defs.append(t)
|
|
645
|
+
seen_names.add(t.name)
|
|
646
|
+
except Exception:
|
|
647
|
+
pass
|
|
648
|
+
|
|
649
|
+
for workflow_id, cfg in agent_nodes:
|
|
650
|
+
provider_raw = cfg.get("provider")
|
|
651
|
+
model_raw = cfg.get("model")
|
|
652
|
+
# NOTE: Provider/model are injected durably through the Agent node's
|
|
653
|
+
# START_SUBWORKFLOW vars (see compiler `_build_sub_vars`). We keep the
|
|
654
|
+
# registered workflow spec provider/model-agnostic so Agent pins can
|
|
655
|
+
# override without breaking persistence/resume.
|
|
656
|
+
provider = None
|
|
657
|
+
model = None
|
|
658
|
+
|
|
659
|
+
tools_selected = _normalize_tool_names(cfg.get("tools"))
|
|
660
|
+
logic = ReActLogic(tools=all_tool_defs)
|
|
661
|
+
registry.register(
|
|
662
|
+
create_react_workflow(
|
|
663
|
+
logic=logic,
|
|
664
|
+
workflow_id=workflow_id,
|
|
665
|
+
provider=provider,
|
|
666
|
+
model=model,
|
|
667
|
+
allowed_tools=tools_selected,
|
|
668
|
+
)
|
|
669
|
+
)
|
|
670
|
+
|
|
671
|
+
if hasattr(runtime, "set_workflow_registry"):
|
|
672
|
+
runtime.set_workflow_registry(registry) # type: ignore[name-defined]
|
|
673
|
+
else: # pragma: no cover
|
|
674
|
+
raise RuntimeError(
|
|
675
|
+
"This flow requires subworkflows (agent/subflow nodes), but the installed "
|
|
676
|
+
"AbstractRuntime does not support workflow registries."
|
|
677
|
+
)
|
|
678
|
+
|
|
679
|
+
return runner
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
def _build_data_edge_map(edges: List[VisualEdge]) -> DataEdgeMap:
|
|
683
|
+
"""Build a mapping of data edges for input resolution."""
|
|
684
|
+
data_edges: DataEdgeMap = {}
|
|
685
|
+
|
|
686
|
+
for edge in edges:
|
|
687
|
+
# Skip execution edges
|
|
688
|
+
if edge.sourceHandle == "exec-out" or edge.targetHandle == "exec-in":
|
|
689
|
+
continue
|
|
690
|
+
|
|
691
|
+
if edge.target not in data_edges:
|
|
692
|
+
data_edges[edge.target] = {}
|
|
693
|
+
|
|
694
|
+
data_edges[edge.target][edge.targetHandle] = (edge.source, edge.sourceHandle)
|
|
695
|
+
|
|
696
|
+
return data_edges
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
def visual_to_flow(visual: VisualFlow) -> Flow:
|
|
700
|
+
"""Convert a visual flow definition to an AbstractFlow `Flow`."""
|
|
701
|
+
import datetime
|
|
702
|
+
|
|
703
|
+
flow = Flow(visual.id)
|
|
704
|
+
|
|
705
|
+
data_edge_map = _build_data_edge_map(visual.edges)
|
|
706
|
+
|
|
707
|
+
# Store node outputs during execution (visual data-edge evaluation cache)
|
|
708
|
+
flow._node_outputs = {} # type: ignore[attr-defined]
|
|
709
|
+
flow._data_edge_map = data_edge_map # type: ignore[attr-defined]
|
|
710
|
+
flow._pure_node_ids = set() # type: ignore[attr-defined]
|
|
711
|
+
flow._volatile_pure_node_ids = set() # type: ignore[attr-defined]
|
|
712
|
+
# Snapshot of "static" node outputs (literals, schemas, etc.). This is used to
|
|
713
|
+
# reset the in-memory cache when the same compiled VisualFlow is executed by
|
|
714
|
+
# multiple runs (e.g. recursive/mutual subflows). See compiler._sync_effect_results_to_node_outputs.
|
|
715
|
+
flow._static_node_outputs = {} # type: ignore[attr-defined]
|
|
716
|
+
flow._active_run_id = None # type: ignore[attr-defined]
|
|
717
|
+
|
|
718
|
+
def _normalize_pin_defaults(raw: Any) -> Dict[str, Any]:
|
|
719
|
+
if not isinstance(raw, dict):
|
|
720
|
+
return {}
|
|
721
|
+
out: Dict[str, Any] = {}
|
|
722
|
+
for k, v in raw.items():
|
|
723
|
+
if not isinstance(k, str) or not k:
|
|
724
|
+
continue
|
|
725
|
+
# Allow JSON-serializable values (including arrays/objects) for defaults.
|
|
726
|
+
# These are cloned at use-sites to avoid cross-run mutation.
|
|
727
|
+
if v is None or isinstance(v, (str, int, float, bool, dict, list)):
|
|
728
|
+
out[k] = v
|
|
729
|
+
return out
|
|
730
|
+
|
|
731
|
+
def _clone_default(value: Any) -> Any:
|
|
732
|
+
# Prevent accidental shared-mutation of dict/list defaults across runs.
|
|
733
|
+
if isinstance(value, (dict, list)):
|
|
734
|
+
try:
|
|
735
|
+
import copy
|
|
736
|
+
|
|
737
|
+
return copy.deepcopy(value)
|
|
738
|
+
except Exception:
|
|
739
|
+
return value
|
|
740
|
+
return value
|
|
741
|
+
|
|
742
|
+
pin_defaults_by_node_id: Dict[str, Dict[str, Any]] = {}
|
|
743
|
+
for node in visual.nodes:
|
|
744
|
+
raw_defaults = node.data.get("pinDefaults") if isinstance(node.data, dict) else None
|
|
745
|
+
normalized = _normalize_pin_defaults(raw_defaults)
|
|
746
|
+
if normalized:
|
|
747
|
+
pin_defaults_by_node_id[node.id] = normalized
|
|
748
|
+
|
|
749
|
+
LITERAL_NODE_TYPES = {
|
|
750
|
+
"literal_string",
|
|
751
|
+
"literal_number",
|
|
752
|
+
"literal_boolean",
|
|
753
|
+
"literal_json",
|
|
754
|
+
"json_schema",
|
|
755
|
+
"literal_array",
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
pure_base_handlers: Dict[str, Any] = {}
|
|
759
|
+
pure_node_ids: set[str] = set()
|
|
760
|
+
|
|
761
|
+
def _has_execution_pins(type_str: str, node_data: Dict[str, Any]) -> bool:
|
|
762
|
+
pins: list[Any] = []
|
|
763
|
+
inputs = node_data.get("inputs")
|
|
764
|
+
outputs = node_data.get("outputs")
|
|
765
|
+
if isinstance(inputs, list):
|
|
766
|
+
pins.extend(inputs)
|
|
767
|
+
if isinstance(outputs, list):
|
|
768
|
+
pins.extend(outputs)
|
|
769
|
+
|
|
770
|
+
if pins:
|
|
771
|
+
for p in pins:
|
|
772
|
+
if isinstance(p, dict) and p.get("type") == "execution":
|
|
773
|
+
return True
|
|
774
|
+
return False
|
|
775
|
+
|
|
776
|
+
if type_str in LITERAL_NODE_TYPES:
|
|
777
|
+
return False
|
|
778
|
+
# These nodes are pure (data-only) even if the JSON document omitted template pins.
|
|
779
|
+
# This keeps programmatic tests and host-built VisualFlows portable.
|
|
780
|
+
if type_str in {"get_var", "bool_var", "var_decl"}:
|
|
781
|
+
return False
|
|
782
|
+
if type_str == "break_object":
|
|
783
|
+
return False
|
|
784
|
+
if get_builtin_handler(type_str) is not None:
|
|
785
|
+
return False
|
|
786
|
+
return True
|
|
787
|
+
|
|
788
|
+
evaluating: set[str] = set()
|
|
789
|
+
volatile_pure_node_ids: set[str] = getattr(flow, "_volatile_pure_node_ids", set()) # type: ignore[attr-defined]
|
|
790
|
+
|
|
791
|
+
def _ensure_node_output(node_id: str) -> None:
|
|
792
|
+
if node_id in flow._node_outputs and node_id not in volatile_pure_node_ids: # type: ignore[attr-defined]
|
|
793
|
+
return
|
|
794
|
+
|
|
795
|
+
handler = pure_base_handlers.get(node_id)
|
|
796
|
+
if handler is None:
|
|
797
|
+
return
|
|
798
|
+
|
|
799
|
+
if node_id in evaluating:
|
|
800
|
+
raise ValueError(f"Data edge cycle detected at '{node_id}'")
|
|
801
|
+
|
|
802
|
+
evaluating.add(node_id)
|
|
803
|
+
try:
|
|
804
|
+
resolved_input: Dict[str, Any] = {}
|
|
805
|
+
|
|
806
|
+
for target_pin, (source_node, source_pin) in data_edge_map.get(node_id, {}).items():
|
|
807
|
+
_ensure_node_output(source_node)
|
|
808
|
+
if source_node not in flow._node_outputs: # type: ignore[attr-defined]
|
|
809
|
+
continue
|
|
810
|
+
source_output = flow._node_outputs[source_node] # type: ignore[attr-defined]
|
|
811
|
+
if isinstance(source_output, dict) and source_pin in source_output:
|
|
812
|
+
resolved_input[target_pin] = source_output[source_pin]
|
|
813
|
+
elif source_pin in ("result", "output"):
|
|
814
|
+
resolved_input[target_pin] = source_output
|
|
815
|
+
|
|
816
|
+
defaults = pin_defaults_by_node_id.get(node_id)
|
|
817
|
+
if defaults:
|
|
818
|
+
for pin_id, value in defaults.items():
|
|
819
|
+
if pin_id in data_edge_map.get(node_id, {}):
|
|
820
|
+
continue
|
|
821
|
+
if pin_id not in resolved_input:
|
|
822
|
+
resolved_input[pin_id] = _clone_default(value)
|
|
823
|
+
|
|
824
|
+
result = handler(resolved_input if resolved_input else {})
|
|
825
|
+
flow._node_outputs[node_id] = result # type: ignore[attr-defined]
|
|
826
|
+
finally:
|
|
827
|
+
# IMPORTANT: even if an upstream pure node raises (bad input / parse_json failure),
|
|
828
|
+
# we must not leave `node_id` in `evaluating`, otherwise later evaluations can
|
|
829
|
+
# surface as a misleading "data edge cycle" at this node.
|
|
830
|
+
try:
|
|
831
|
+
evaluating.remove(node_id)
|
|
832
|
+
except KeyError:
|
|
833
|
+
pass
|
|
834
|
+
|
|
835
|
+
EFFECT_NODE_TYPES = {
|
|
836
|
+
"ask_user",
|
|
837
|
+
"answer_user",
|
|
838
|
+
"llm_call",
|
|
839
|
+
"tool_calls",
|
|
840
|
+
"wait_until",
|
|
841
|
+
"wait_event",
|
|
842
|
+
"emit_event",
|
|
843
|
+
"memory_note",
|
|
844
|
+
"memory_query",
|
|
845
|
+
"memory_rehydrate",
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
literal_node_ids: set[str] = set()
|
|
849
|
+
# Pre-evaluate literal nodes and store their values
|
|
850
|
+
for node in visual.nodes:
|
|
851
|
+
type_str = node.type.value if hasattr(node.type, "value") else str(node.type)
|
|
852
|
+
if type_str in LITERAL_NODE_TYPES:
|
|
853
|
+
literal_value = node.data.get("literalValue")
|
|
854
|
+
flow._node_outputs[node.id] = {"value": literal_value} # type: ignore[attr-defined]
|
|
855
|
+
literal_node_ids.add(node.id)
|
|
856
|
+
# Capture baseline outputs (typically only literal nodes). This baseline must
|
|
857
|
+
# remain stable across runs so we can safely reset `_node_outputs` when switching
|
|
858
|
+
# between different `RunState.run_id` contexts (self-recursive subflows).
|
|
859
|
+
try:
|
|
860
|
+
flow._static_node_outputs = dict(flow._node_outputs) # type: ignore[attr-defined]
|
|
861
|
+
except Exception:
|
|
862
|
+
flow._static_node_outputs = {} # type: ignore[attr-defined]
|
|
863
|
+
|
|
864
|
+
# Compute execution reachability and ignore disconnected execution nodes.
|
|
865
|
+
#
|
|
866
|
+
# Visual editors often contain experimentation / orphan nodes. These should not
|
|
867
|
+
# prevent execution of the reachable pipeline.
|
|
868
|
+
exec_node_ids: set[str] = set()
|
|
869
|
+
for node in visual.nodes:
|
|
870
|
+
type_str = node.type.value if hasattr(node.type, "value") else str(node.type)
|
|
871
|
+
if type_str in LITERAL_NODE_TYPES:
|
|
872
|
+
continue
|
|
873
|
+
if _has_execution_pins(type_str, node.data):
|
|
874
|
+
exec_node_ids.add(node.id)
|
|
875
|
+
|
|
876
|
+
def _pick_entry() -> Optional[str]:
|
|
877
|
+
# Prefer explicit entryNode if it is an execution node.
|
|
878
|
+
if isinstance(getattr(visual, "entryNode", None), str) and visual.entryNode in exec_node_ids:
|
|
879
|
+
return visual.entryNode
|
|
880
|
+
# Otherwise, infer entry as a node with no incoming execution edges.
|
|
881
|
+
targets = {e.target for e in visual.edges if getattr(e, "targetHandle", None) == "exec-in"}
|
|
882
|
+
for node in visual.nodes:
|
|
883
|
+
if node.id in exec_node_ids and node.id not in targets:
|
|
884
|
+
return node.id
|
|
885
|
+
# Fallback: first exec node in document order
|
|
886
|
+
for node in visual.nodes:
|
|
887
|
+
if node.id in exec_node_ids:
|
|
888
|
+
return node.id
|
|
889
|
+
return None
|
|
890
|
+
|
|
891
|
+
entry_exec = _pick_entry()
|
|
892
|
+
reachable_exec: set[str] = set()
|
|
893
|
+
if entry_exec:
|
|
894
|
+
adj: Dict[str, list[str]] = {}
|
|
895
|
+
for e in visual.edges:
|
|
896
|
+
if getattr(e, "targetHandle", None) != "exec-in":
|
|
897
|
+
continue
|
|
898
|
+
if e.source not in exec_node_ids or e.target not in exec_node_ids:
|
|
899
|
+
continue
|
|
900
|
+
adj.setdefault(e.source, []).append(e.target)
|
|
901
|
+
stack = [entry_exec]
|
|
902
|
+
while stack:
|
|
903
|
+
cur = stack.pop()
|
|
904
|
+
if cur in reachable_exec:
|
|
905
|
+
continue
|
|
906
|
+
reachable_exec.add(cur)
|
|
907
|
+
for nxt in adj.get(cur, []):
|
|
908
|
+
if nxt not in reachable_exec:
|
|
909
|
+
stack.append(nxt)
|
|
910
|
+
|
|
911
|
+
ignored_exec = sorted([nid for nid in exec_node_ids if nid not in reachable_exec])
|
|
912
|
+
if ignored_exec:
|
|
913
|
+
# Runtime-local metadata for hosts/UIs that want to show warnings.
|
|
914
|
+
flow._ignored_exec_nodes = ignored_exec # type: ignore[attr-defined]
|
|
915
|
+
|
|
916
|
+
def _decode_separator(value: str) -> str:
|
|
917
|
+
return value.replace("\\n", "\n").replace("\\t", "\t").replace("\\r", "\r")
|
|
918
|
+
|
|
919
|
+
def _create_read_file_handler(_data: Dict[str, Any]):
|
|
920
|
+
import json
|
|
921
|
+
from pathlib import Path
|
|
922
|
+
|
|
923
|
+
def handler(input_data: Any) -> Dict[str, Any]:
|
|
924
|
+
payload = input_data if isinstance(input_data, dict) else {}
|
|
925
|
+
raw_path = payload.get("file_path")
|
|
926
|
+
if not isinstance(raw_path, str) or not raw_path.strip():
|
|
927
|
+
raise ValueError("read_file requires a non-empty 'file_path' input.")
|
|
928
|
+
|
|
929
|
+
file_path = raw_path.strip()
|
|
930
|
+
path = Path(file_path).expanduser()
|
|
931
|
+
if not path.is_absolute():
|
|
932
|
+
path = Path.cwd() / path
|
|
933
|
+
|
|
934
|
+
if not path.exists():
|
|
935
|
+
raise FileNotFoundError(f"File not found: {file_path}")
|
|
936
|
+
if not path.is_file():
|
|
937
|
+
raise ValueError(f"Not a file: {file_path}")
|
|
938
|
+
|
|
939
|
+
try:
|
|
940
|
+
text = path.read_text(encoding="utf-8")
|
|
941
|
+
except UnicodeDecodeError as e:
|
|
942
|
+
raise ValueError(f"Cannot read '{file_path}' as UTF-8: {e}") from e
|
|
943
|
+
|
|
944
|
+
# Detect JSON primarily from file extension; also opportunistically parse
|
|
945
|
+
# when the content looks like JSON. Markdown and text are returned as-is.
|
|
946
|
+
lower_name = path.name.lower()
|
|
947
|
+
content_stripped = text.lstrip()
|
|
948
|
+
looks_like_json = bool(content_stripped) and content_stripped[0] in "{["
|
|
949
|
+
|
|
950
|
+
if lower_name.endswith(".json"):
|
|
951
|
+
try:
|
|
952
|
+
return {"content": json.loads(text)}
|
|
953
|
+
except Exception as e:
|
|
954
|
+
raise ValueError(f"Invalid JSON in '{file_path}': {e}") from e
|
|
955
|
+
|
|
956
|
+
if looks_like_json:
|
|
957
|
+
try:
|
|
958
|
+
return {"content": json.loads(text)}
|
|
959
|
+
except Exception:
|
|
960
|
+
pass
|
|
961
|
+
|
|
962
|
+
return {"content": text}
|
|
963
|
+
|
|
964
|
+
return handler
|
|
965
|
+
|
|
966
|
+
def _create_write_file_handler(_data: Dict[str, Any]):
|
|
967
|
+
import json
|
|
968
|
+
from pathlib import Path
|
|
969
|
+
|
|
970
|
+
def handler(input_data: Any) -> Dict[str, Any]:
|
|
971
|
+
payload = input_data if isinstance(input_data, dict) else {}
|
|
972
|
+
raw_path = payload.get("file_path")
|
|
973
|
+
if not isinstance(raw_path, str) or not raw_path.strip():
|
|
974
|
+
raise ValueError("write_file requires a non-empty 'file_path' input.")
|
|
975
|
+
|
|
976
|
+
file_path = raw_path.strip()
|
|
977
|
+
path = Path(file_path).expanduser()
|
|
978
|
+
if not path.is_absolute():
|
|
979
|
+
path = Path.cwd() / path
|
|
980
|
+
|
|
981
|
+
raw_content = payload.get("content")
|
|
982
|
+
|
|
983
|
+
if path.name.lower().endswith(".json"):
|
|
984
|
+
if isinstance(raw_content, str):
|
|
985
|
+
try:
|
|
986
|
+
raw_content = json.loads(raw_content)
|
|
987
|
+
except Exception as e:
|
|
988
|
+
raise ValueError(f"write_file JSON content must be valid JSON: {e}") from e
|
|
989
|
+
text = json.dumps(raw_content, indent=2, ensure_ascii=False)
|
|
990
|
+
if not text.endswith("\n"):
|
|
991
|
+
text += "\n"
|
|
992
|
+
else:
|
|
993
|
+
if raw_content is None:
|
|
994
|
+
text = ""
|
|
995
|
+
elif isinstance(raw_content, str):
|
|
996
|
+
text = raw_content
|
|
997
|
+
elif isinstance(raw_content, (dict, list)):
|
|
998
|
+
text = json.dumps(raw_content, indent=2, ensure_ascii=False)
|
|
999
|
+
else:
|
|
1000
|
+
text = str(raw_content)
|
|
1001
|
+
|
|
1002
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
1003
|
+
path.write_text(text, encoding="utf-8")
|
|
1004
|
+
|
|
1005
|
+
return {"bytes": len(text.encode("utf-8")), "file_path": str(path)}
|
|
1006
|
+
|
|
1007
|
+
return handler
|
|
1008
|
+
|
|
1009
|
+
def _create_concat_handler(data: Dict[str, Any]):
|
|
1010
|
+
config = data.get("concatConfig", {}) if isinstance(data, dict) else {}
|
|
1011
|
+
separator = " "
|
|
1012
|
+
if isinstance(config, dict):
|
|
1013
|
+
sep_raw = config.get("separator")
|
|
1014
|
+
if isinstance(sep_raw, str):
|
|
1015
|
+
separator = sep_raw
|
|
1016
|
+
separator = _decode_separator(separator)
|
|
1017
|
+
|
|
1018
|
+
pin_order: list[str] = []
|
|
1019
|
+
pins = data.get("inputs") if isinstance(data, dict) else None
|
|
1020
|
+
if isinstance(pins, list):
|
|
1021
|
+
for p in pins:
|
|
1022
|
+
if not isinstance(p, dict):
|
|
1023
|
+
continue
|
|
1024
|
+
if p.get("type") == "execution":
|
|
1025
|
+
continue
|
|
1026
|
+
pid = p.get("id")
|
|
1027
|
+
if isinstance(pid, str) and pid:
|
|
1028
|
+
pin_order.append(pid)
|
|
1029
|
+
|
|
1030
|
+
if not pin_order:
|
|
1031
|
+
# Backward-compat: programmatic/test-created VisualNodes may omit template pins.
|
|
1032
|
+
# In that case, infer a stable pin order from the provided input keys at runtime
|
|
1033
|
+
# (prefer a..z single-letter pins), so `a`, `b`, ... behave as expected.
|
|
1034
|
+
pin_order = []
|
|
1035
|
+
|
|
1036
|
+
def handler(input_data: Any) -> str:
|
|
1037
|
+
if not isinstance(input_data, dict):
|
|
1038
|
+
return str(input_data or "")
|
|
1039
|
+
|
|
1040
|
+
parts: list[str] = []
|
|
1041
|
+
if pin_order:
|
|
1042
|
+
order = pin_order
|
|
1043
|
+
else:
|
|
1044
|
+
# Stable inference for missing pin metadata.
|
|
1045
|
+
keys = [k for k in input_data.keys() if isinstance(k, str)]
|
|
1046
|
+
letter = sorted([k for k in keys if len(k) == 1 and "a" <= k <= "z"])
|
|
1047
|
+
other = sorted([k for k in keys if k not in set(letter)])
|
|
1048
|
+
order = letter + other
|
|
1049
|
+
|
|
1050
|
+
for pid in order:
|
|
1051
|
+
if pid in input_data:
|
|
1052
|
+
v = input_data.get(pid)
|
|
1053
|
+
parts.append("" if v is None else str(v))
|
|
1054
|
+
return separator.join(parts)
|
|
1055
|
+
|
|
1056
|
+
return handler
|
|
1057
|
+
|
|
1058
|
+
def _create_make_array_handler(data: Dict[str, Any]):
|
|
1059
|
+
"""Build an array from 1+ inputs in pin order.
|
|
1060
|
+
|
|
1061
|
+
Design:
|
|
1062
|
+
- We treat missing/unset pins as absent (skip None) to avoid surprising `null`
|
|
1063
|
+
elements when a pin is present but unconnected.
|
|
1064
|
+
- We do NOT flatten arrays/tuples; if you want flattening/concatenation,
|
|
1065
|
+
use `array_concat`.
|
|
1066
|
+
"""
|
|
1067
|
+
pin_order: list[str] = []
|
|
1068
|
+
pins = data.get("inputs") if isinstance(data, dict) else None
|
|
1069
|
+
if isinstance(pins, list):
|
|
1070
|
+
for p in pins:
|
|
1071
|
+
if not isinstance(p, dict):
|
|
1072
|
+
continue
|
|
1073
|
+
if p.get("type") == "execution":
|
|
1074
|
+
continue
|
|
1075
|
+
pid = p.get("id")
|
|
1076
|
+
if isinstance(pid, str) and pid:
|
|
1077
|
+
pin_order.append(pid)
|
|
1078
|
+
|
|
1079
|
+
if not pin_order:
|
|
1080
|
+
pin_order = ["a", "b"]
|
|
1081
|
+
|
|
1082
|
+
def handler(input_data: Any) -> list[Any]:
|
|
1083
|
+
if not isinstance(input_data, dict):
|
|
1084
|
+
if input_data is None:
|
|
1085
|
+
return []
|
|
1086
|
+
if isinstance(input_data, list):
|
|
1087
|
+
return list(input_data)
|
|
1088
|
+
if isinstance(input_data, tuple):
|
|
1089
|
+
return list(input_data)
|
|
1090
|
+
return [input_data]
|
|
1091
|
+
|
|
1092
|
+
out: list[Any] = []
|
|
1093
|
+
for pid in pin_order:
|
|
1094
|
+
if pid not in input_data:
|
|
1095
|
+
continue
|
|
1096
|
+
v = input_data.get(pid)
|
|
1097
|
+
if v is None:
|
|
1098
|
+
continue
|
|
1099
|
+
out.append(v)
|
|
1100
|
+
return out
|
|
1101
|
+
|
|
1102
|
+
return handler
|
|
1103
|
+
|
|
1104
|
+
def _create_array_concat_handler(data: Dict[str, Any]):
|
|
1105
|
+
pin_order: list[str] = []
|
|
1106
|
+
pins = data.get("inputs") if isinstance(data, dict) else None
|
|
1107
|
+
if isinstance(pins, list):
|
|
1108
|
+
for p in pins:
|
|
1109
|
+
if not isinstance(p, dict):
|
|
1110
|
+
continue
|
|
1111
|
+
if p.get("type") == "execution":
|
|
1112
|
+
continue
|
|
1113
|
+
pid = p.get("id")
|
|
1114
|
+
if isinstance(pid, str) and pid:
|
|
1115
|
+
pin_order.append(pid)
|
|
1116
|
+
|
|
1117
|
+
if not pin_order:
|
|
1118
|
+
pin_order = ["a", "b"]
|
|
1119
|
+
|
|
1120
|
+
def handler(input_data: Any) -> list[Any]:
|
|
1121
|
+
if not isinstance(input_data, dict):
|
|
1122
|
+
if input_data is None:
|
|
1123
|
+
return []
|
|
1124
|
+
if isinstance(input_data, list):
|
|
1125
|
+
return list(input_data)
|
|
1126
|
+
if isinstance(input_data, tuple):
|
|
1127
|
+
return list(input_data)
|
|
1128
|
+
return [input_data]
|
|
1129
|
+
|
|
1130
|
+
out: list[Any] = []
|
|
1131
|
+
for pid in pin_order:
|
|
1132
|
+
if pid not in input_data:
|
|
1133
|
+
continue
|
|
1134
|
+
v = input_data.get(pid)
|
|
1135
|
+
if v is None:
|
|
1136
|
+
continue
|
|
1137
|
+
if isinstance(v, list):
|
|
1138
|
+
out.extend(v)
|
|
1139
|
+
continue
|
|
1140
|
+
if isinstance(v, tuple):
|
|
1141
|
+
out.extend(list(v))
|
|
1142
|
+
continue
|
|
1143
|
+
out.append(v)
|
|
1144
|
+
return out
|
|
1145
|
+
|
|
1146
|
+
return handler
|
|
1147
|
+
|
|
1148
|
+
def _create_break_object_handler(data: Dict[str, Any]):
|
|
1149
|
+
config = data.get("breakConfig", {}) if isinstance(data, dict) else {}
|
|
1150
|
+
selected = config.get("selectedPaths", []) if isinstance(config, dict) else []
|
|
1151
|
+
selected_paths = [p.strip() for p in selected if isinstance(p, str) and p.strip()]
|
|
1152
|
+
|
|
1153
|
+
def _get_path(value: Any, path: str) -> Any:
|
|
1154
|
+
current = value
|
|
1155
|
+
for part in path.split("."):
|
|
1156
|
+
if current is None:
|
|
1157
|
+
return None
|
|
1158
|
+
if isinstance(current, dict):
|
|
1159
|
+
current = current.get(part)
|
|
1160
|
+
continue
|
|
1161
|
+
if isinstance(current, list) and part.isdigit():
|
|
1162
|
+
idx = int(part)
|
|
1163
|
+
if idx < 0 or idx >= len(current):
|
|
1164
|
+
return None
|
|
1165
|
+
current = current[idx]
|
|
1166
|
+
continue
|
|
1167
|
+
return None
|
|
1168
|
+
return current
|
|
1169
|
+
|
|
1170
|
+
def handler(input_data):
|
|
1171
|
+
src_obj = None
|
|
1172
|
+
if isinstance(input_data, dict):
|
|
1173
|
+
src_obj = input_data.get("object")
|
|
1174
|
+
|
|
1175
|
+
# Best-effort: tolerate JSON-ish strings (common when breaking LLM outputs).
|
|
1176
|
+
if isinstance(src_obj, str) and src_obj.strip():
|
|
1177
|
+
try:
|
|
1178
|
+
parser = get_builtin_handler("parse_json")
|
|
1179
|
+
if parser is not None:
|
|
1180
|
+
src_obj = parser({"text": src_obj, "wrap_scalar": True})
|
|
1181
|
+
except Exception:
|
|
1182
|
+
pass
|
|
1183
|
+
|
|
1184
|
+
out: Dict[str, Any] = {}
|
|
1185
|
+
for path in selected_paths:
|
|
1186
|
+
out[path] = _get_path(src_obj, path)
|
|
1187
|
+
return out
|
|
1188
|
+
|
|
1189
|
+
return handler
|
|
1190
|
+
|
|
1191
|
+
def _get_by_path(value: Any, path: str) -> Any:
|
|
1192
|
+
"""Best-effort dotted-path lookup supporting dicts and numeric list indices."""
|
|
1193
|
+
current = value
|
|
1194
|
+
for part in path.split("."):
|
|
1195
|
+
if current is None:
|
|
1196
|
+
return None
|
|
1197
|
+
if isinstance(current, dict):
|
|
1198
|
+
current = current.get(part)
|
|
1199
|
+
continue
|
|
1200
|
+
if isinstance(current, list) and part.isdigit():
|
|
1201
|
+
idx = int(part)
|
|
1202
|
+
if idx < 0 or idx >= len(current):
|
|
1203
|
+
return None
|
|
1204
|
+
current = current[idx]
|
|
1205
|
+
continue
|
|
1206
|
+
return None
|
|
1207
|
+
return current
|
|
1208
|
+
|
|
1209
|
+
def _create_get_var_handler(_data: Dict[str, Any]):
|
|
1210
|
+
# Pure node: reads from the current run vars (attached onto the Flow by the compiler).
|
|
1211
|
+
# Mark as volatile so it is recomputed whenever requested (avoids stale cached reads).
|
|
1212
|
+
def handler(input_data: Any) -> Dict[str, Any]:
|
|
1213
|
+
payload = input_data if isinstance(input_data, dict) else {}
|
|
1214
|
+
raw_name = payload.get("name")
|
|
1215
|
+
name = (raw_name if isinstance(raw_name, str) else str(raw_name or "")).strip()
|
|
1216
|
+
run_vars = getattr(flow, "_run_vars", None) # type: ignore[attr-defined]
|
|
1217
|
+
if not isinstance(run_vars, dict) or not name:
|
|
1218
|
+
return {"value": None}
|
|
1219
|
+
return {"value": _get_by_path(run_vars, name)}
|
|
1220
|
+
|
|
1221
|
+
return handler
|
|
1222
|
+
|
|
1223
|
+
def _create_bool_var_handler(data: Dict[str, Any]):
|
|
1224
|
+
"""Pure node: reads a workflow-level boolean variable from run.vars with a default.
|
|
1225
|
+
|
|
1226
|
+
Config is stored in the visual node's `literalValue` as either:
|
|
1227
|
+
- a string: variable name
|
|
1228
|
+
- an object: { "name": "...", "default": true|false }
|
|
1229
|
+
"""
|
|
1230
|
+
raw_cfg = data.get("literalValue")
|
|
1231
|
+
name_cfg = ""
|
|
1232
|
+
default_cfg = False
|
|
1233
|
+
if isinstance(raw_cfg, str):
|
|
1234
|
+
name_cfg = raw_cfg.strip()
|
|
1235
|
+
elif isinstance(raw_cfg, dict):
|
|
1236
|
+
n = raw_cfg.get("name")
|
|
1237
|
+
if isinstance(n, str):
|
|
1238
|
+
name_cfg = n.strip()
|
|
1239
|
+
d = raw_cfg.get("default")
|
|
1240
|
+
if isinstance(d, bool):
|
|
1241
|
+
default_cfg = d
|
|
1242
|
+
|
|
1243
|
+
def handler(input_data: Any) -> Dict[str, Any]:
|
|
1244
|
+
del input_data
|
|
1245
|
+
run_vars = getattr(flow, "_run_vars", None) # type: ignore[attr-defined]
|
|
1246
|
+
if not isinstance(run_vars, dict) or not name_cfg:
|
|
1247
|
+
return {"name": name_cfg, "value": bool(default_cfg)}
|
|
1248
|
+
|
|
1249
|
+
raw = _get_by_path(run_vars, name_cfg)
|
|
1250
|
+
if isinstance(raw, bool):
|
|
1251
|
+
return {"name": name_cfg, "value": raw}
|
|
1252
|
+
return {"name": name_cfg, "value": bool(default_cfg)}
|
|
1253
|
+
|
|
1254
|
+
return handler
|
|
1255
|
+
|
|
1256
|
+
def _create_var_decl_handler(data: Dict[str, Any]):
|
|
1257
|
+
"""Pure node: typed workflow variable declaration (name + type + default).
|
|
1258
|
+
|
|
1259
|
+
Config is stored in `literalValue`:
|
|
1260
|
+
{ "name": "...", "type": "boolean|number|string|object|array|any", "default": ... }
|
|
1261
|
+
|
|
1262
|
+
Runtime semantics:
|
|
1263
|
+
- Read `run.vars[name]` (via `flow._run_vars`), and return it if it matches the declared type.
|
|
1264
|
+
- Otherwise fall back to the declared default.
|
|
1265
|
+
"""
|
|
1266
|
+
raw_cfg = data.get("literalValue")
|
|
1267
|
+
name_cfg = ""
|
|
1268
|
+
type_cfg = "any"
|
|
1269
|
+
default_cfg: Any = None
|
|
1270
|
+
if isinstance(raw_cfg, dict):
|
|
1271
|
+
n = raw_cfg.get("name")
|
|
1272
|
+
if isinstance(n, str):
|
|
1273
|
+
name_cfg = n.strip()
|
|
1274
|
+
t = raw_cfg.get("type")
|
|
1275
|
+
if isinstance(t, str) and t.strip():
|
|
1276
|
+
type_cfg = t.strip()
|
|
1277
|
+
default_cfg = raw_cfg.get("default")
|
|
1278
|
+
|
|
1279
|
+
allowed_types = {"boolean", "number", "string", "object", "array", "any"}
|
|
1280
|
+
if type_cfg not in allowed_types:
|
|
1281
|
+
type_cfg = "any"
|
|
1282
|
+
|
|
1283
|
+
def _matches(v: Any) -> bool:
|
|
1284
|
+
if type_cfg == "any":
|
|
1285
|
+
return True
|
|
1286
|
+
if type_cfg == "boolean":
|
|
1287
|
+
return isinstance(v, bool)
|
|
1288
|
+
if type_cfg == "number":
|
|
1289
|
+
return isinstance(v, (int, float)) and not isinstance(v, bool)
|
|
1290
|
+
if type_cfg == "string":
|
|
1291
|
+
return isinstance(v, str)
|
|
1292
|
+
if type_cfg == "array":
|
|
1293
|
+
return isinstance(v, list)
|
|
1294
|
+
if type_cfg == "object":
|
|
1295
|
+
return isinstance(v, dict)
|
|
1296
|
+
return True
|
|
1297
|
+
|
|
1298
|
+
def _default_for_type() -> Any:
|
|
1299
|
+
if type_cfg == "boolean":
|
|
1300
|
+
return False
|
|
1301
|
+
if type_cfg == "number":
|
|
1302
|
+
return 0
|
|
1303
|
+
if type_cfg == "string":
|
|
1304
|
+
return ""
|
|
1305
|
+
if type_cfg == "array":
|
|
1306
|
+
return []
|
|
1307
|
+
if type_cfg == "object":
|
|
1308
|
+
return {}
|
|
1309
|
+
return None
|
|
1310
|
+
|
|
1311
|
+
def handler(input_data: Any) -> Dict[str, Any]:
|
|
1312
|
+
del input_data
|
|
1313
|
+
run_vars = getattr(flow, "_run_vars", None) # type: ignore[attr-defined]
|
|
1314
|
+
if not isinstance(run_vars, dict) or not name_cfg:
|
|
1315
|
+
v = default_cfg if _matches(default_cfg) else _default_for_type()
|
|
1316
|
+
return {"name": name_cfg, "value": v}
|
|
1317
|
+
|
|
1318
|
+
raw = _get_by_path(run_vars, name_cfg)
|
|
1319
|
+
if _matches(raw):
|
|
1320
|
+
return {"name": name_cfg, "value": raw}
|
|
1321
|
+
|
|
1322
|
+
v = default_cfg if _matches(default_cfg) else _default_for_type()
|
|
1323
|
+
return {"name": name_cfg, "value": v}
|
|
1324
|
+
|
|
1325
|
+
return handler
|
|
1326
|
+
|
|
1327
|
+
def _create_set_var_handler(_data: Dict[str, Any]):
|
|
1328
|
+
# Execution node: does not mutate run.vars here (handled by compiler adapter).
|
|
1329
|
+
# This handler exists to participate in data-edge resolution and expose outputs.
|
|
1330
|
+
#
|
|
1331
|
+
# Important UX contract:
|
|
1332
|
+
# - In the visual editor, primitive pins (boolean/number/string) show default UI controls
|
|
1333
|
+
# even when the user hasn't explicitly edited them.
|
|
1334
|
+
# - If we treat "missing" as None here, `Set Variable` would write None and this can
|
|
1335
|
+
# cause typed `Variable` (`var_decl`) to fall back to its default (e.g. staying True).
|
|
1336
|
+
# - Therefore we default missing primitive values to their natural defaults.
|
|
1337
|
+
pins = _data.get("inputs") if isinstance(_data, dict) else None
|
|
1338
|
+
value_pin_type: Optional[str] = None
|
|
1339
|
+
if isinstance(pins, list):
|
|
1340
|
+
for p in pins:
|
|
1341
|
+
if not isinstance(p, dict):
|
|
1342
|
+
continue
|
|
1343
|
+
if p.get("id") != "value":
|
|
1344
|
+
continue
|
|
1345
|
+
t = p.get("type")
|
|
1346
|
+
if isinstance(t, str) and t:
|
|
1347
|
+
value_pin_type = t
|
|
1348
|
+
break
|
|
1349
|
+
|
|
1350
|
+
def handler(input_data: Any) -> Dict[str, Any]:
|
|
1351
|
+
payload = input_data if isinstance(input_data, dict) else {}
|
|
1352
|
+
value_specified = isinstance(payload, dict) and "value" in payload
|
|
1353
|
+
value = payload.get("value")
|
|
1354
|
+
|
|
1355
|
+
if not value_specified:
|
|
1356
|
+
if value_pin_type == "boolean":
|
|
1357
|
+
value = False
|
|
1358
|
+
elif value_pin_type == "number":
|
|
1359
|
+
value = 0
|
|
1360
|
+
elif value_pin_type == "string":
|
|
1361
|
+
value = ""
|
|
1362
|
+
|
|
1363
|
+
return {"name": payload.get("name"), "value": value}
|
|
1364
|
+
|
|
1365
|
+
return handler
|
|
1366
|
+
|
|
1367
|
+
def _wrap_builtin(handler, data: Dict[str, Any]):
|
|
1368
|
+
literal_value = data.get("literalValue")
|
|
1369
|
+
# Preserve pin order for builtins that need deterministic input selection (e.g. coalesce).
|
|
1370
|
+
pin_order: list[str] = []
|
|
1371
|
+
pins = data.get("inputs") if isinstance(data, dict) else None
|
|
1372
|
+
if isinstance(pins, list):
|
|
1373
|
+
for p in pins:
|
|
1374
|
+
if not isinstance(p, dict):
|
|
1375
|
+
continue
|
|
1376
|
+
if p.get("type") == "execution":
|
|
1377
|
+
continue
|
|
1378
|
+
pid = p.get("id")
|
|
1379
|
+
if isinstance(pid, str) and pid:
|
|
1380
|
+
pin_order.append(pid)
|
|
1381
|
+
|
|
1382
|
+
def wrapped(input_data):
|
|
1383
|
+
if isinstance(input_data, dict):
|
|
1384
|
+
inputs = input_data.copy()
|
|
1385
|
+
else:
|
|
1386
|
+
inputs = {"value": input_data, "a": input_data, "text": input_data}
|
|
1387
|
+
|
|
1388
|
+
if literal_value is not None:
|
|
1389
|
+
inputs["_literalValue"] = literal_value
|
|
1390
|
+
if pin_order:
|
|
1391
|
+
inputs["_pin_order"] = list(pin_order)
|
|
1392
|
+
|
|
1393
|
+
return handler(inputs)
|
|
1394
|
+
|
|
1395
|
+
return wrapped
|
|
1396
|
+
|
|
1397
|
+
def _create_agent_input_handler(data: Dict[str, Any]):
|
|
1398
|
+
cfg = data.get("agentConfig", {}) if isinstance(data, dict) else {}
|
|
1399
|
+
cfg = cfg if isinstance(cfg, dict) else {}
|
|
1400
|
+
|
|
1401
|
+
def _normalize_response_schema(raw: Any) -> Optional[Dict[str, Any]]:
|
|
1402
|
+
"""Normalize a structured-output schema input into a JSON Schema dict.
|
|
1403
|
+
|
|
1404
|
+
Supported inputs (best-effort):
|
|
1405
|
+
- JSON Schema dict: {"type":"object","properties":{...}, ...}
|
|
1406
|
+
- LMStudio/OpenAI-style wrapper: {"type":"json_schema","json_schema": {"schema": {...}}}
|
|
1407
|
+
"""
|
|
1408
|
+
if raw is None:
|
|
1409
|
+
return None
|
|
1410
|
+
if isinstance(raw, dict):
|
|
1411
|
+
if raw.get("type") == "json_schema" and isinstance(raw.get("json_schema"), dict):
|
|
1412
|
+
inner = raw.get("json_schema")
|
|
1413
|
+
if isinstance(inner, dict) and isinstance(inner.get("schema"), dict):
|
|
1414
|
+
return dict(inner.get("schema") or {})
|
|
1415
|
+
return dict(raw)
|
|
1416
|
+
return None
|
|
1417
|
+
|
|
1418
|
+
def _normalize_tool_names(raw: Any) -> list[str]:
|
|
1419
|
+
if raw is None:
|
|
1420
|
+
return []
|
|
1421
|
+
items: list[Any]
|
|
1422
|
+
if isinstance(raw, list):
|
|
1423
|
+
items = raw
|
|
1424
|
+
elif isinstance(raw, tuple):
|
|
1425
|
+
items = list(raw)
|
|
1426
|
+
else:
|
|
1427
|
+
items = [raw]
|
|
1428
|
+
out: list[str] = []
|
|
1429
|
+
for t in items:
|
|
1430
|
+
if isinstance(t, str) and t.strip():
|
|
1431
|
+
out.append(t.strip())
|
|
1432
|
+
# preserve order, remove duplicates
|
|
1433
|
+
seen: set[str] = set()
|
|
1434
|
+
uniq: list[str] = []
|
|
1435
|
+
for t in out:
|
|
1436
|
+
if t in seen:
|
|
1437
|
+
continue
|
|
1438
|
+
seen.add(t)
|
|
1439
|
+
uniq.append(t)
|
|
1440
|
+
return uniq
|
|
1441
|
+
|
|
1442
|
+
def handler(input_data):
|
|
1443
|
+
task = ""
|
|
1444
|
+
if isinstance(input_data, dict):
|
|
1445
|
+
raw_task = input_data.get("task")
|
|
1446
|
+
if raw_task is None:
|
|
1447
|
+
raw_task = input_data.get("prompt")
|
|
1448
|
+
task = "" if raw_task is None else str(raw_task)
|
|
1449
|
+
else:
|
|
1450
|
+
task = str(input_data)
|
|
1451
|
+
|
|
1452
|
+
context_raw = input_data.get("context", {}) if isinstance(input_data, dict) else {}
|
|
1453
|
+
context = context_raw if isinstance(context_raw, dict) else {}
|
|
1454
|
+
provider = input_data.get("provider") if isinstance(input_data, dict) else None
|
|
1455
|
+
model = input_data.get("model") if isinstance(input_data, dict) else None
|
|
1456
|
+
|
|
1457
|
+
system_raw = input_data.get("system") if isinstance(input_data, dict) else ""
|
|
1458
|
+
system = system_raw if isinstance(system_raw, str) else str(system_raw or "")
|
|
1459
|
+
|
|
1460
|
+
tools_specified = isinstance(input_data, dict) and "tools" in input_data
|
|
1461
|
+
tools_raw = input_data.get("tools") if isinstance(input_data, dict) else None
|
|
1462
|
+
tools = _normalize_tool_names(tools_raw) if tools_specified else []
|
|
1463
|
+
if not tools_specified:
|
|
1464
|
+
tools = _normalize_tool_names(cfg.get("tools"))
|
|
1465
|
+
|
|
1466
|
+
out: Dict[str, Any] = {
|
|
1467
|
+
"task": task,
|
|
1468
|
+
"context": context,
|
|
1469
|
+
"provider": provider if isinstance(provider, str) else None,
|
|
1470
|
+
"model": model if isinstance(model, str) else None,
|
|
1471
|
+
"system": system,
|
|
1472
|
+
"tools": tools,
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
# Optional pin overrides (passed through for compiler/runtime consumption).
|
|
1476
|
+
if isinstance(input_data, dict) and "max_iterations" in input_data:
|
|
1477
|
+
out["max_iterations"] = input_data.get("max_iterations")
|
|
1478
|
+
|
|
1479
|
+
if isinstance(input_data, dict) and "response_schema" in input_data:
|
|
1480
|
+
schema = _normalize_response_schema(input_data.get("response_schema"))
|
|
1481
|
+
if isinstance(schema, dict) and schema:
|
|
1482
|
+
out["response_schema"] = schema
|
|
1483
|
+
|
|
1484
|
+
include_context_specified = isinstance(input_data, dict) and (
|
|
1485
|
+
"include_context" in input_data or "use_context" in input_data
|
|
1486
|
+
)
|
|
1487
|
+
if include_context_specified:
|
|
1488
|
+
raw_inc = (
|
|
1489
|
+
input_data.get("include_context")
|
|
1490
|
+
if isinstance(input_data, dict) and "include_context" in input_data
|
|
1491
|
+
else input_data.get("use_context") if isinstance(input_data, dict) else None
|
|
1492
|
+
)
|
|
1493
|
+
out["include_context"] = _coerce_bool(raw_inc)
|
|
1494
|
+
|
|
1495
|
+
return out
|
|
1496
|
+
|
|
1497
|
+
return handler
|
|
1498
|
+
|
|
1499
|
+
def _create_subflow_effect_builder(data: Dict[str, Any]):
|
|
1500
|
+
input_pin_ids: list[str] = []
|
|
1501
|
+
pins = data.get("inputs") if isinstance(data, dict) else None
|
|
1502
|
+
if isinstance(pins, list):
|
|
1503
|
+
for p in pins:
|
|
1504
|
+
if not isinstance(p, dict):
|
|
1505
|
+
continue
|
|
1506
|
+
if p.get("type") == "execution":
|
|
1507
|
+
continue
|
|
1508
|
+
pid = p.get("id")
|
|
1509
|
+
if isinstance(pid, str) and pid:
|
|
1510
|
+
# Control pin (not forwarded into child vars).
|
|
1511
|
+
if pid in {"inherit_context", "inheritContext"}:
|
|
1512
|
+
continue
|
|
1513
|
+
input_pin_ids.append(pid)
|
|
1514
|
+
|
|
1515
|
+
inherit_cfg = None
|
|
1516
|
+
if isinstance(data, dict):
|
|
1517
|
+
cfg = data.get("effectConfig")
|
|
1518
|
+
if isinstance(cfg, dict):
|
|
1519
|
+
inherit_cfg = cfg.get("inherit_context")
|
|
1520
|
+
if inherit_cfg is None:
|
|
1521
|
+
inherit_cfg = cfg.get("inheritContext")
|
|
1522
|
+
inherit_context_default = bool(inherit_cfg) if inherit_cfg is not None else False
|
|
1523
|
+
|
|
1524
|
+
def handler(input_data):
|
|
1525
|
+
subflow_id = (
|
|
1526
|
+
data.get("subflowId")
|
|
1527
|
+
or data.get("flowId") # legacy
|
|
1528
|
+
or data.get("workflowId")
|
|
1529
|
+
or data.get("workflow_id")
|
|
1530
|
+
)
|
|
1531
|
+
|
|
1532
|
+
sub_vars_dict: Dict[str, Any] = {}
|
|
1533
|
+
if isinstance(input_data, dict):
|
|
1534
|
+
base: Dict[str, Any] = {}
|
|
1535
|
+
if isinstance(input_data.get("vars"), dict):
|
|
1536
|
+
base.update(dict(input_data["vars"]))
|
|
1537
|
+
elif isinstance(input_data.get("input"), dict):
|
|
1538
|
+
base.update(dict(input_data["input"]))
|
|
1539
|
+
|
|
1540
|
+
if input_pin_ids:
|
|
1541
|
+
for pid in input_pin_ids:
|
|
1542
|
+
if pid in ("vars", "input") and isinstance(input_data.get(pid), dict):
|
|
1543
|
+
continue
|
|
1544
|
+
if pid in input_data:
|
|
1545
|
+
base[pid] = input_data.get(pid)
|
|
1546
|
+
sub_vars_dict = base
|
|
1547
|
+
else:
|
|
1548
|
+
if base:
|
|
1549
|
+
sub_vars_dict = base
|
|
1550
|
+
else:
|
|
1551
|
+
sub_vars_dict = dict(input_data)
|
|
1552
|
+
else:
|
|
1553
|
+
if input_pin_ids and len(input_pin_ids) == 1:
|
|
1554
|
+
sub_vars_dict = {input_pin_ids[0]: input_data}
|
|
1555
|
+
else:
|
|
1556
|
+
sub_vars_dict = {"input": input_data}
|
|
1557
|
+
|
|
1558
|
+
# Never forward control pins into the child run vars.
|
|
1559
|
+
sub_vars_dict.pop("inherit_context", None)
|
|
1560
|
+
sub_vars_dict.pop("inheritContext", None)
|
|
1561
|
+
|
|
1562
|
+
inherit_context_specified = isinstance(input_data, dict) and (
|
|
1563
|
+
"inherit_context" in input_data or "inheritContext" in input_data
|
|
1564
|
+
)
|
|
1565
|
+
if inherit_context_specified:
|
|
1566
|
+
raw_inherit = (
|
|
1567
|
+
input_data.get("inherit_context")
|
|
1568
|
+
if isinstance(input_data, dict) and "inherit_context" in input_data
|
|
1569
|
+
else input_data.get("inheritContext") if isinstance(input_data, dict) else None
|
|
1570
|
+
)
|
|
1571
|
+
inherit_context_value = _coerce_bool(raw_inherit)
|
|
1572
|
+
else:
|
|
1573
|
+
inherit_context_value = inherit_context_default
|
|
1574
|
+
|
|
1575
|
+
return {
|
|
1576
|
+
"output": None,
|
|
1577
|
+
"_pending_effect": (
|
|
1578
|
+
{
|
|
1579
|
+
"type": "start_subworkflow",
|
|
1580
|
+
"workflow_id": subflow_id,
|
|
1581
|
+
"vars": sub_vars_dict,
|
|
1582
|
+
# Start subworkflows in async+wait mode so hosts (notably AbstractFlow Web)
|
|
1583
|
+
# can tick child runs incrementally and stream their node_start/node_complete
|
|
1584
|
+
# events for better observability (nested/recursive subflows).
|
|
1585
|
+
#
|
|
1586
|
+
# Non-interactive hosts (tests/CLI) still complete synchronously because
|
|
1587
|
+
# FlowRunner.run() auto-drives WAITING(SUBWORKFLOW) children and resumes
|
|
1588
|
+
# parents until completion.
|
|
1589
|
+
"async": True,
|
|
1590
|
+
"wait": True,
|
|
1591
|
+
**({"inherit_context": True} if inherit_context_value else {}),
|
|
1592
|
+
}
|
|
1593
|
+
),
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
return handler
|
|
1597
|
+
|
|
1598
|
+
def _create_event_handler(event_type: str, data: Dict[str, Any]):
|
|
1599
|
+
# Event nodes are special: they bridge external inputs / runtime vars into the graph.
|
|
1600
|
+
#
|
|
1601
|
+
# Critical constraint: RunState.vars must remain JSON-serializable for durable execution.
|
|
1602
|
+
# The runtime persists per-node outputs in `vars["_temp"]["node_outputs"]`. If an event node
|
|
1603
|
+
# returns the full `run.vars` dict (which contains `_temp`), we create a self-referential
|
|
1604
|
+
# cycle: `_temp -> node_outputs -> <start_output>['_temp'] -> _temp`, which explodes during
|
|
1605
|
+
# persistence (e.g. JsonFileRunStore uses dataclasses.asdict()).
|
|
1606
|
+
#
|
|
1607
|
+
# Therefore, `on_flow_start` must *not* leak internal namespaces like `_temp` into outputs.
|
|
1608
|
+
start_pin_ids: list[str] = []
|
|
1609
|
+
pins = data.get("outputs") if isinstance(data, dict) else None
|
|
1610
|
+
if isinstance(pins, list):
|
|
1611
|
+
for p in pins:
|
|
1612
|
+
if not isinstance(p, dict):
|
|
1613
|
+
continue
|
|
1614
|
+
if p.get("type") == "execution":
|
|
1615
|
+
continue
|
|
1616
|
+
pid = p.get("id")
|
|
1617
|
+
if isinstance(pid, str) and pid:
|
|
1618
|
+
start_pin_ids.append(pid)
|
|
1619
|
+
|
|
1620
|
+
def handler(input_data):
|
|
1621
|
+
if event_type == "on_flow_start":
|
|
1622
|
+
# Prefer explicit pins: the visual editor treats non-exec output pins as
|
|
1623
|
+
# "Flow Start Parameters" (initial vars). Only expose those by default.
|
|
1624
|
+
if isinstance(input_data, dict):
|
|
1625
|
+
defaults_raw = data.get("pinDefaults") if isinstance(data, dict) else None
|
|
1626
|
+
defaults = defaults_raw if isinstance(defaults_raw, dict) else {}
|
|
1627
|
+
if start_pin_ids:
|
|
1628
|
+
out: Dict[str, Any] = {}
|
|
1629
|
+
for pid in start_pin_ids:
|
|
1630
|
+
if pid in input_data:
|
|
1631
|
+
out[pid] = input_data.get(pid)
|
|
1632
|
+
continue
|
|
1633
|
+
if isinstance(pid, str) and pid in defaults:
|
|
1634
|
+
dv = defaults.get(pid)
|
|
1635
|
+
out[pid] = _clone_default(dv)
|
|
1636
|
+
# Also seed run.vars for downstream Get Variable / debugging.
|
|
1637
|
+
if not pid.startswith("_") and pid not in input_data:
|
|
1638
|
+
input_data[pid] = _clone_default(dv)
|
|
1639
|
+
continue
|
|
1640
|
+
out[pid] = None
|
|
1641
|
+
return out
|
|
1642
|
+
# Backward-compat: older/test-created flows may omit pin metadata.
|
|
1643
|
+
# In that case, expose non-internal keys only (avoid `_temp`, `_limits`, ...).
|
|
1644
|
+
out2 = {k: v for k, v in input_data.items() if isinstance(k, str) and not k.startswith("_")}
|
|
1645
|
+
# If pinDefaults exist, apply them for missing non-internal keys.
|
|
1646
|
+
for k, dv in defaults.items():
|
|
1647
|
+
if not isinstance(k, str) or not k or k.startswith("_"):
|
|
1648
|
+
continue
|
|
1649
|
+
if k in out2 or k in input_data:
|
|
1650
|
+
continue
|
|
1651
|
+
out2[k] = _clone_default(dv)
|
|
1652
|
+
input_data[k] = _clone_default(dv)
|
|
1653
|
+
return out2
|
|
1654
|
+
|
|
1655
|
+
# Non-dict input: if there is a single declared pin, map into it; otherwise
|
|
1656
|
+
# keep a generic `input` key.
|
|
1657
|
+
if start_pin_ids and len(start_pin_ids) == 1:
|
|
1658
|
+
return {start_pin_ids[0]: input_data}
|
|
1659
|
+
return {"input": input_data}
|
|
1660
|
+
if event_type == "on_user_request":
|
|
1661
|
+
message = input_data.get("message", "") if isinstance(input_data, dict) else str(input_data)
|
|
1662
|
+
context = input_data.get("context", {}) if isinstance(input_data, dict) else {}
|
|
1663
|
+
return {"message": message, "context": context}
|
|
1664
|
+
if event_type == "on_agent_message":
|
|
1665
|
+
sender = input_data.get("sender", "unknown") if isinstance(input_data, dict) else "unknown"
|
|
1666
|
+
message = input_data.get("message", "") if isinstance(input_data, dict) else str(input_data)
|
|
1667
|
+
channel = data.get("eventConfig", {}).get("channel", "")
|
|
1668
|
+
return {"sender": sender, "message": message, "channel": channel}
|
|
1669
|
+
return input_data
|
|
1670
|
+
|
|
1671
|
+
return handler
|
|
1672
|
+
|
|
1673
|
+
def _create_flow_end_handler(data: Dict[str, Any]):
|
|
1674
|
+
pin_ids: list[str] = []
|
|
1675
|
+
pins = data.get("inputs") if isinstance(data, dict) else None
|
|
1676
|
+
if isinstance(pins, list):
|
|
1677
|
+
for p in pins:
|
|
1678
|
+
if not isinstance(p, dict):
|
|
1679
|
+
continue
|
|
1680
|
+
if p.get("type") == "execution":
|
|
1681
|
+
continue
|
|
1682
|
+
pid = p.get("id")
|
|
1683
|
+
if isinstance(pid, str) and pid:
|
|
1684
|
+
pin_ids.append(pid)
|
|
1685
|
+
|
|
1686
|
+
def handler(input_data: Any):
|
|
1687
|
+
if not pin_ids:
|
|
1688
|
+
if isinstance(input_data, dict):
|
|
1689
|
+
return dict(input_data)
|
|
1690
|
+
return {"result": input_data}
|
|
1691
|
+
|
|
1692
|
+
if not isinstance(input_data, dict):
|
|
1693
|
+
if len(pin_ids) == 1:
|
|
1694
|
+
return {pin_ids[0]: input_data}
|
|
1695
|
+
return {"result": input_data}
|
|
1696
|
+
|
|
1697
|
+
return {pid: input_data.get(pid) for pid in pin_ids}
|
|
1698
|
+
|
|
1699
|
+
return handler
|
|
1700
|
+
|
|
1701
|
+
def _create_expression_handler(expression: str):
|
|
1702
|
+
def handler(input_data):
|
|
1703
|
+
namespace = {"x": input_data, "input": input_data}
|
|
1704
|
+
if isinstance(input_data, dict):
|
|
1705
|
+
namespace.update(input_data)
|
|
1706
|
+
try:
|
|
1707
|
+
return eval(expression, {"__builtins__": {}}, namespace)
|
|
1708
|
+
except Exception as e:
|
|
1709
|
+
return {"error": str(e)}
|
|
1710
|
+
|
|
1711
|
+
return handler
|
|
1712
|
+
|
|
1713
|
+
def _create_if_handler(data: Dict[str, Any]):
|
|
1714
|
+
def handler(input_data):
|
|
1715
|
+
condition = input_data.get("condition") if isinstance(input_data, dict) else bool(input_data)
|
|
1716
|
+
return {"branch": "true" if condition else "false", "condition": condition}
|
|
1717
|
+
|
|
1718
|
+
return handler
|
|
1719
|
+
|
|
1720
|
+
def _create_switch_handler(data: Dict[str, Any]):
|
|
1721
|
+
def handler(input_data):
|
|
1722
|
+
value = input_data.get("value") if isinstance(input_data, dict) else input_data
|
|
1723
|
+
|
|
1724
|
+
config = data.get("switchConfig", {}) if isinstance(data, dict) else {}
|
|
1725
|
+
raw_cases = config.get("cases", []) if isinstance(config, dict) else []
|
|
1726
|
+
|
|
1727
|
+
value_str = "" if value is None else str(value)
|
|
1728
|
+
if isinstance(raw_cases, list):
|
|
1729
|
+
for case in raw_cases:
|
|
1730
|
+
if not isinstance(case, dict):
|
|
1731
|
+
continue
|
|
1732
|
+
case_id = case.get("id")
|
|
1733
|
+
case_value = case.get("value")
|
|
1734
|
+
if not isinstance(case_id, str) or not case_id:
|
|
1735
|
+
continue
|
|
1736
|
+
if case_value is None:
|
|
1737
|
+
continue
|
|
1738
|
+
if value_str == str(case_value):
|
|
1739
|
+
return {"branch": f"case:{case_id}", "value": value, "matched": str(case_value)}
|
|
1740
|
+
|
|
1741
|
+
return {"branch": "default", "value": value}
|
|
1742
|
+
|
|
1743
|
+
return handler
|
|
1744
|
+
|
|
1745
|
+
def _create_while_handler(data: Dict[str, Any]):
|
|
1746
|
+
def handler(input_data):
|
|
1747
|
+
condition = input_data.get("condition") if isinstance(input_data, dict) else bool(input_data)
|
|
1748
|
+
return {"condition": bool(condition)}
|
|
1749
|
+
|
|
1750
|
+
return handler
|
|
1751
|
+
|
|
1752
|
+
def _create_for_handler(data: Dict[str, Any]):
|
|
1753
|
+
def handler(input_data):
|
|
1754
|
+
payload = input_data if isinstance(input_data, dict) else {}
|
|
1755
|
+
start = payload.get("start")
|
|
1756
|
+
end = payload.get("end")
|
|
1757
|
+
step = payload.get("step")
|
|
1758
|
+
return {"start": start, "end": end, "step": step}
|
|
1759
|
+
|
|
1760
|
+
return handler
|
|
1761
|
+
|
|
1762
|
+
def _create_loop_handler(data: Dict[str, Any]):
|
|
1763
|
+
def handler(input_data):
|
|
1764
|
+
items = input_data.get("items") if isinstance(input_data, dict) else input_data
|
|
1765
|
+
if items is None:
|
|
1766
|
+
items = []
|
|
1767
|
+
if not isinstance(items, (list, tuple)):
|
|
1768
|
+
items = [items]
|
|
1769
|
+
items_list = list(items) if isinstance(items, tuple) else list(items) # type: ignore[arg-type]
|
|
1770
|
+
return {"items": items_list, "count": len(items_list)}
|
|
1771
|
+
|
|
1772
|
+
return handler
|
|
1773
|
+
|
|
1774
|
+
def _coerce_bool(value: Any) -> bool:
|
|
1775
|
+
"""Best-effort boolean parsing (handles common string forms)."""
|
|
1776
|
+
if value is None:
|
|
1777
|
+
return False
|
|
1778
|
+
if isinstance(value, bool):
|
|
1779
|
+
return value
|
|
1780
|
+
if isinstance(value, (int, float)):
|
|
1781
|
+
try:
|
|
1782
|
+
return float(value) != 0.0
|
|
1783
|
+
except Exception:
|
|
1784
|
+
return False
|
|
1785
|
+
if isinstance(value, str):
|
|
1786
|
+
s = value.strip().lower()
|
|
1787
|
+
if not s:
|
|
1788
|
+
return False
|
|
1789
|
+
if s in {"false", "0", "no", "off"}:
|
|
1790
|
+
return False
|
|
1791
|
+
if s in {"true", "1", "yes", "on"}:
|
|
1792
|
+
return True
|
|
1793
|
+
return False
|
|
1794
|
+
|
|
1795
|
+
def _create_effect_handler(effect_type: str, data: Dict[str, Any]):
|
|
1796
|
+
effect_config = data.get("effectConfig", {})
|
|
1797
|
+
|
|
1798
|
+
if effect_type == "ask_user":
|
|
1799
|
+
return _create_ask_user_handler(data, effect_config)
|
|
1800
|
+
if effect_type == "answer_user":
|
|
1801
|
+
return _create_answer_user_handler(data, effect_config)
|
|
1802
|
+
if effect_type == "llm_call":
|
|
1803
|
+
return _create_llm_call_handler(data, effect_config)
|
|
1804
|
+
if effect_type == "tool_calls":
|
|
1805
|
+
return _create_tool_calls_handler(data, effect_config)
|
|
1806
|
+
if effect_type == "wait_until":
|
|
1807
|
+
return _create_wait_until_handler(data, effect_config)
|
|
1808
|
+
if effect_type == "wait_event":
|
|
1809
|
+
return _create_wait_event_handler(data, effect_config)
|
|
1810
|
+
if effect_type == "memory_note":
|
|
1811
|
+
return _create_memory_note_handler(data, effect_config)
|
|
1812
|
+
if effect_type == "memory_query":
|
|
1813
|
+
return _create_memory_query_handler(data, effect_config)
|
|
1814
|
+
if effect_type == "memory_rehydrate":
|
|
1815
|
+
return _create_memory_rehydrate_handler(data, effect_config)
|
|
1816
|
+
|
|
1817
|
+
return lambda x: x
|
|
1818
|
+
|
|
1819
|
+
def _create_tool_calls_handler(data: Dict[str, Any], config: Dict[str, Any]):
|
|
1820
|
+
import json
|
|
1821
|
+
|
|
1822
|
+
allowed_default = None
|
|
1823
|
+
if isinstance(config, dict):
|
|
1824
|
+
raw = config.get("allowed_tools")
|
|
1825
|
+
if raw is None:
|
|
1826
|
+
raw = config.get("allowedTools")
|
|
1827
|
+
allowed_default = raw
|
|
1828
|
+
|
|
1829
|
+
def _normalize_str_list(raw: Any) -> list[str]:
|
|
1830
|
+
if not isinstance(raw, list):
|
|
1831
|
+
return []
|
|
1832
|
+
out: list[str] = []
|
|
1833
|
+
for x in raw:
|
|
1834
|
+
if isinstance(x, str) and x.strip():
|
|
1835
|
+
out.append(x.strip())
|
|
1836
|
+
return out
|
|
1837
|
+
|
|
1838
|
+
def _normalize_tool_calls(raw: Any) -> list[Dict[str, Any]]:
|
|
1839
|
+
if raw is None:
|
|
1840
|
+
return []
|
|
1841
|
+
if isinstance(raw, dict):
|
|
1842
|
+
return [dict(raw)]
|
|
1843
|
+
if isinstance(raw, list):
|
|
1844
|
+
out: list[Dict[str, Any]] = []
|
|
1845
|
+
for x in raw:
|
|
1846
|
+
if isinstance(x, dict):
|
|
1847
|
+
out.append(dict(x))
|
|
1848
|
+
return out
|
|
1849
|
+
if isinstance(raw, str) and raw.strip():
|
|
1850
|
+
# Best-effort: tolerate JSON strings coming from parse_json/text nodes.
|
|
1851
|
+
try:
|
|
1852
|
+
parsed = json.loads(raw)
|
|
1853
|
+
except Exception:
|
|
1854
|
+
return []
|
|
1855
|
+
return _normalize_tool_calls(parsed)
|
|
1856
|
+
return []
|
|
1857
|
+
|
|
1858
|
+
def handler(input_data: Any):
|
|
1859
|
+
payload = input_data if isinstance(input_data, dict) else {}
|
|
1860
|
+
|
|
1861
|
+
tool_calls_raw = payload.get("tool_calls")
|
|
1862
|
+
tool_calls = _normalize_tool_calls(tool_calls_raw)
|
|
1863
|
+
|
|
1864
|
+
allow_specified = "allowed_tools" in payload or "allowedTools" in payload
|
|
1865
|
+
allowed_raw = payload.get("allowed_tools")
|
|
1866
|
+
if allowed_raw is None:
|
|
1867
|
+
allowed_raw = payload.get("allowedTools")
|
|
1868
|
+
allowed_tools = _normalize_str_list(allowed_raw) if allow_specified else []
|
|
1869
|
+
if not allow_specified:
|
|
1870
|
+
allowed_tools = _normalize_str_list(allowed_default)
|
|
1871
|
+
|
|
1872
|
+
pending: Dict[str, Any] = {"type": "tool_calls", "tool_calls": tool_calls}
|
|
1873
|
+
# Only include allowlist when explicitly provided (empty list means "allow none").
|
|
1874
|
+
if allow_specified or isinstance(allowed_default, list):
|
|
1875
|
+
pending["allowed_tools"] = allowed_tools
|
|
1876
|
+
|
|
1877
|
+
return {
|
|
1878
|
+
"results": None,
|
|
1879
|
+
"success": None,
|
|
1880
|
+
"_pending_effect": pending,
|
|
1881
|
+
}
|
|
1882
|
+
|
|
1883
|
+
return handler
|
|
1884
|
+
|
|
1885
|
+
def _create_ask_user_handler(data: Dict[str, Any], config: Dict[str, Any]):
|
|
1886
|
+
def handler(input_data):
|
|
1887
|
+
prompt = input_data.get("prompt", "Please respond:") if isinstance(input_data, dict) else str(input_data)
|
|
1888
|
+
choices = input_data.get("choices", []) if isinstance(input_data, dict) else []
|
|
1889
|
+
allow_free_text = config.get("allowFreeText", True)
|
|
1890
|
+
|
|
1891
|
+
return {
|
|
1892
|
+
"response": f"[User prompt: {prompt}]",
|
|
1893
|
+
"prompt": prompt,
|
|
1894
|
+
"choices": choices,
|
|
1895
|
+
"allow_free_text": allow_free_text,
|
|
1896
|
+
"_pending_effect": {
|
|
1897
|
+
"type": "ask_user",
|
|
1898
|
+
"prompt": prompt,
|
|
1899
|
+
"choices": choices,
|
|
1900
|
+
"allow_free_text": allow_free_text,
|
|
1901
|
+
},
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
return handler
|
|
1905
|
+
|
|
1906
|
+
def _create_answer_user_handler(data: Dict[str, Any], config: Dict[str, Any]):
|
|
1907
|
+
def handler(input_data):
|
|
1908
|
+
message = input_data.get("message", "") if isinstance(input_data, dict) else str(input_data or "")
|
|
1909
|
+
return {"message": message, "_pending_effect": {"type": "answer_user", "message": message}}
|
|
1910
|
+
|
|
1911
|
+
return handler
|
|
1912
|
+
|
|
1913
|
+
def _create_llm_call_handler(data: Dict[str, Any], config: Dict[str, Any]):
|
|
1914
|
+
provider_default = config.get("provider", "")
|
|
1915
|
+
model_default = config.get("model", "")
|
|
1916
|
+
temperature = config.get("temperature", 0.7)
|
|
1917
|
+
tools_default_raw = config.get("tools")
|
|
1918
|
+
include_context_cfg = config.get("include_context")
|
|
1919
|
+
if include_context_cfg is None:
|
|
1920
|
+
include_context_cfg = config.get("use_context")
|
|
1921
|
+
include_context_default = _coerce_bool(include_context_cfg) if include_context_cfg is not None else False
|
|
1922
|
+
|
|
1923
|
+
# Tool definitions (ToolSpecs) are required for tool calling. In the visual editor we
|
|
1924
|
+
# store tools as a portable `string[]` allowlist; at execution time we translate to
|
|
1925
|
+
# strict ToolSpecs `{name, description, parameters}` expected by AbstractCore.
|
|
1926
|
+
def _strip_tool_spec(raw: Any) -> Optional[Dict[str, Any]]:
|
|
1927
|
+
if not isinstance(raw, dict):
|
|
1928
|
+
return None
|
|
1929
|
+
name = raw.get("name")
|
|
1930
|
+
if not isinstance(name, str) or not name.strip():
|
|
1931
|
+
return None
|
|
1932
|
+
desc = raw.get("description")
|
|
1933
|
+
params = raw.get("parameters")
|
|
1934
|
+
out: Dict[str, Any] = {
|
|
1935
|
+
"name": name.strip(),
|
|
1936
|
+
"description": str(desc or ""),
|
|
1937
|
+
"parameters": dict(params) if isinstance(params, dict) else {},
|
|
1938
|
+
}
|
|
1939
|
+
return out
|
|
1940
|
+
|
|
1941
|
+
def _normalize_tool_names(raw: Any) -> list[str]:
|
|
1942
|
+
if not isinstance(raw, list):
|
|
1943
|
+
return []
|
|
1944
|
+
out: list[str] = []
|
|
1945
|
+
for t in raw:
|
|
1946
|
+
if isinstance(t, str) and t.strip():
|
|
1947
|
+
out.append(t.strip())
|
|
1948
|
+
return out
|
|
1949
|
+
|
|
1950
|
+
# Precompute a best-effort "available ToolSpecs by name" map so we can turn tool names
|
|
1951
|
+
# into ToolSpecs without going through the web backend.
|
|
1952
|
+
tool_specs_by_name: Dict[str, Dict[str, Any]] = {}
|
|
1953
|
+
try:
|
|
1954
|
+
from abstractruntime.integrations.abstractcore.default_tools import list_default_tool_specs
|
|
1955
|
+
|
|
1956
|
+
base_specs = list_default_tool_specs()
|
|
1957
|
+
if not isinstance(base_specs, list):
|
|
1958
|
+
base_specs = []
|
|
1959
|
+
for s in base_specs:
|
|
1960
|
+
stripped = _strip_tool_spec(s)
|
|
1961
|
+
if stripped is not None:
|
|
1962
|
+
tool_specs_by_name[stripped["name"]] = stripped
|
|
1963
|
+
except Exception:
|
|
1964
|
+
pass
|
|
1965
|
+
|
|
1966
|
+
# Optional schema-only runtime tools (used by AbstractAgent). These are useful for
|
|
1967
|
+
# "state machine" autonomy where the graph can route tool-like requests to effect nodes.
|
|
1968
|
+
try:
|
|
1969
|
+
from abstractagent.logic.builtins import ( # type: ignore
|
|
1970
|
+
ASK_USER_TOOL,
|
|
1971
|
+
COMPACT_MEMORY_TOOL,
|
|
1972
|
+
INSPECT_VARS_TOOL,
|
|
1973
|
+
RECALL_MEMORY_TOOL,
|
|
1974
|
+
REMEMBER_TOOL,
|
|
1975
|
+
)
|
|
1976
|
+
|
|
1977
|
+
builtin_defs = [ASK_USER_TOOL, RECALL_MEMORY_TOOL, INSPECT_VARS_TOOL, REMEMBER_TOOL, COMPACT_MEMORY_TOOL]
|
|
1978
|
+
for tool_def in builtin_defs:
|
|
1979
|
+
try:
|
|
1980
|
+
d = tool_def.to_dict()
|
|
1981
|
+
except Exception:
|
|
1982
|
+
d = None
|
|
1983
|
+
stripped = _strip_tool_spec(d)
|
|
1984
|
+
if stripped is not None and stripped["name"] not in tool_specs_by_name:
|
|
1985
|
+
tool_specs_by_name[stripped["name"]] = stripped
|
|
1986
|
+
except Exception:
|
|
1987
|
+
pass
|
|
1988
|
+
|
|
1989
|
+
def _normalize_tools(raw: Any) -> list[Dict[str, Any]]:
|
|
1990
|
+
# Already ToolSpecs (from pins): accept and strip UI-only fields.
|
|
1991
|
+
if isinstance(raw, list) and raw and all(isinstance(x, dict) for x in raw):
|
|
1992
|
+
out: list[Dict[str, Any]] = []
|
|
1993
|
+
for x in raw:
|
|
1994
|
+
stripped = _strip_tool_spec(x)
|
|
1995
|
+
if stripped is not None:
|
|
1996
|
+
out.append(stripped)
|
|
1997
|
+
return out
|
|
1998
|
+
|
|
1999
|
+
# Tool names (portable representation): resolve against known tool specs.
|
|
2000
|
+
names = _normalize_tool_names(raw)
|
|
2001
|
+
out: list[Dict[str, Any]] = []
|
|
2002
|
+
for name in names:
|
|
2003
|
+
spec = tool_specs_by_name.get(name)
|
|
2004
|
+
if spec is not None:
|
|
2005
|
+
out.append(spec)
|
|
2006
|
+
return out
|
|
2007
|
+
|
|
2008
|
+
def _normalize_response_schema(raw: Any) -> Optional[Dict[str, Any]]:
|
|
2009
|
+
"""Normalize a structured-output schema input into a JSON Schema dict.
|
|
2010
|
+
|
|
2011
|
+
Supported inputs (best-effort):
|
|
2012
|
+
- JSON Schema dict: {"type":"object","properties":{...}, ...}
|
|
2013
|
+
- LMStudio/OpenAI-style wrapper: {"type":"json_schema","json_schema": {"schema": {...}}}
|
|
2014
|
+
"""
|
|
2015
|
+
if raw is None:
|
|
2016
|
+
return None
|
|
2017
|
+
if isinstance(raw, dict):
|
|
2018
|
+
# Wrapper form (OpenAI "response_format": {type:"json_schema", json_schema:{schema:{...}}})
|
|
2019
|
+
if raw.get("type") == "json_schema" and isinstance(raw.get("json_schema"), dict):
|
|
2020
|
+
inner = raw.get("json_schema")
|
|
2021
|
+
if isinstance(inner, dict) and isinstance(inner.get("schema"), dict):
|
|
2022
|
+
return dict(inner.get("schema") or {})
|
|
2023
|
+
# Plain JSON Schema dict
|
|
2024
|
+
return dict(raw)
|
|
2025
|
+
return None
|
|
2026
|
+
|
|
2027
|
+
def handler(input_data):
|
|
2028
|
+
prompt = input_data.get("prompt", "") if isinstance(input_data, dict) else str(input_data)
|
|
2029
|
+
system = input_data.get("system", "") if isinstance(input_data, dict) else ""
|
|
2030
|
+
|
|
2031
|
+
tools_specified = isinstance(input_data, dict) and "tools" in input_data
|
|
2032
|
+
tools_raw = input_data.get("tools") if isinstance(input_data, dict) else None
|
|
2033
|
+
tools = _normalize_tools(tools_raw) if tools_specified else []
|
|
2034
|
+
if not tools_specified:
|
|
2035
|
+
tools = _normalize_tools(tools_default_raw)
|
|
2036
|
+
|
|
2037
|
+
include_context_specified = isinstance(input_data, dict) and (
|
|
2038
|
+
"include_context" in input_data or "use_context" in input_data
|
|
2039
|
+
)
|
|
2040
|
+
if include_context_specified:
|
|
2041
|
+
raw_inc = (
|
|
2042
|
+
input_data.get("include_context")
|
|
2043
|
+
if isinstance(input_data, dict) and "include_context" in input_data
|
|
2044
|
+
else input_data.get("use_context") if isinstance(input_data, dict) else None
|
|
2045
|
+
)
|
|
2046
|
+
include_context_value = _coerce_bool(raw_inc)
|
|
2047
|
+
else:
|
|
2048
|
+
include_context_value = include_context_default
|
|
2049
|
+
|
|
2050
|
+
provider = (
|
|
2051
|
+
input_data.get("provider")
|
|
2052
|
+
if isinstance(input_data, dict) and isinstance(input_data.get("provider"), str)
|
|
2053
|
+
else provider_default
|
|
2054
|
+
)
|
|
2055
|
+
model = (
|
|
2056
|
+
input_data.get("model")
|
|
2057
|
+
if isinstance(input_data, dict) and isinstance(input_data.get("model"), str)
|
|
2058
|
+
else model_default
|
|
2059
|
+
)
|
|
2060
|
+
|
|
2061
|
+
if not provider or not model:
|
|
2062
|
+
return {
|
|
2063
|
+
"response": "[LLM Call: missing provider/model]",
|
|
2064
|
+
"_pending_effect": {
|
|
2065
|
+
"type": "llm_call",
|
|
2066
|
+
"prompt": prompt,
|
|
2067
|
+
"system_prompt": system,
|
|
2068
|
+
"tools": tools,
|
|
2069
|
+
"params": {"temperature": temperature},
|
|
2070
|
+
"include_context": include_context_value,
|
|
2071
|
+
},
|
|
2072
|
+
"error": "Missing provider or model configuration",
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
response_schema = (
|
|
2076
|
+
_normalize_response_schema(input_data.get("response_schema"))
|
|
2077
|
+
if isinstance(input_data, dict) and "response_schema" in input_data
|
|
2078
|
+
else None
|
|
2079
|
+
)
|
|
2080
|
+
|
|
2081
|
+
pending: Dict[str, Any] = {
|
|
2082
|
+
"type": "llm_call",
|
|
2083
|
+
"prompt": prompt,
|
|
2084
|
+
"system_prompt": system,
|
|
2085
|
+
"tools": tools,
|
|
2086
|
+
"params": {"temperature": temperature},
|
|
2087
|
+
"provider": provider,
|
|
2088
|
+
"model": model,
|
|
2089
|
+
"include_context": include_context_value,
|
|
2090
|
+
}
|
|
2091
|
+
if isinstance(response_schema, dict) and response_schema:
|
|
2092
|
+
pending["response_schema"] = response_schema
|
|
2093
|
+
# Name is optional; AbstractRuntime will fall back to a safe default.
|
|
2094
|
+
pending["response_schema_name"] = "LLM_StructuredOutput"
|
|
2095
|
+
|
|
2096
|
+
return {
|
|
2097
|
+
"response": None,
|
|
2098
|
+
"_pending_effect": pending,
|
|
2099
|
+
}
|
|
2100
|
+
|
|
2101
|
+
return handler
|
|
2102
|
+
|
|
2103
|
+
def _create_model_catalog_handler(data: Dict[str, Any]):
|
|
2104
|
+
cfg = data.get("modelCatalogConfig", {}) if isinstance(data, dict) else {}
|
|
2105
|
+
cfg = dict(cfg) if isinstance(cfg, dict) else {}
|
|
2106
|
+
|
|
2107
|
+
allowed_providers_default = cfg.get("allowedProviders")
|
|
2108
|
+
allowed_models_default = cfg.get("allowedModels")
|
|
2109
|
+
index_default = cfg.get("index", 0)
|
|
2110
|
+
|
|
2111
|
+
def _as_str_list(raw: Any) -> list[str]:
|
|
2112
|
+
if not isinstance(raw, list):
|
|
2113
|
+
return []
|
|
2114
|
+
out: list[str] = []
|
|
2115
|
+
for x in raw:
|
|
2116
|
+
if isinstance(x, str) and x.strip():
|
|
2117
|
+
out.append(x.strip())
|
|
2118
|
+
return out
|
|
2119
|
+
|
|
2120
|
+
def handler(input_data: Any):
|
|
2121
|
+
# Allow pin-based overrides (data edges) while keeping node config as defaults.
|
|
2122
|
+
allowed_providers = _as_str_list(
|
|
2123
|
+
input_data.get("allowed_providers") if isinstance(input_data, dict) else None
|
|
2124
|
+
) or _as_str_list(allowed_providers_default)
|
|
2125
|
+
allowed_models = _as_str_list(
|
|
2126
|
+
input_data.get("allowed_models") if isinstance(input_data, dict) else None
|
|
2127
|
+
) or _as_str_list(allowed_models_default)
|
|
2128
|
+
|
|
2129
|
+
idx_raw = input_data.get("index") if isinstance(input_data, dict) else None
|
|
2130
|
+
try:
|
|
2131
|
+
idx = int(idx_raw) if idx_raw is not None else int(index_default or 0)
|
|
2132
|
+
except Exception:
|
|
2133
|
+
idx = 0
|
|
2134
|
+
if idx < 0:
|
|
2135
|
+
idx = 0
|
|
2136
|
+
|
|
2137
|
+
try:
|
|
2138
|
+
from abstractcore.providers.registry import get_all_providers_with_models, get_available_models_for_provider
|
|
2139
|
+
except Exception:
|
|
2140
|
+
return {"providers": [], "models": [], "pair": None, "provider": "", "model": ""}
|
|
2141
|
+
|
|
2142
|
+
providers_meta = get_all_providers_with_models(include_models=False)
|
|
2143
|
+
available_providers: list[str] = []
|
|
2144
|
+
for p in providers_meta:
|
|
2145
|
+
if not isinstance(p, dict):
|
|
2146
|
+
continue
|
|
2147
|
+
if p.get("status") != "available":
|
|
2148
|
+
continue
|
|
2149
|
+
name = p.get("name")
|
|
2150
|
+
if isinstance(name, str) and name.strip():
|
|
2151
|
+
available_providers.append(name.strip())
|
|
2152
|
+
|
|
2153
|
+
if allowed_providers:
|
|
2154
|
+
allow = {x.lower(): x for x in allowed_providers}
|
|
2155
|
+
available_providers = [p for p in available_providers if p.lower() in allow]
|
|
2156
|
+
|
|
2157
|
+
pairs: list[dict[str, str]] = []
|
|
2158
|
+
model_ids: list[str] = []
|
|
2159
|
+
|
|
2160
|
+
allow_models_norm = {m.strip() for m in allowed_models if isinstance(m, str) and m.strip()}
|
|
2161
|
+
|
|
2162
|
+
for provider in available_providers:
|
|
2163
|
+
try:
|
|
2164
|
+
models = get_available_models_for_provider(provider)
|
|
2165
|
+
except Exception:
|
|
2166
|
+
models = []
|
|
2167
|
+
if not isinstance(models, list):
|
|
2168
|
+
models = []
|
|
2169
|
+
for m in models:
|
|
2170
|
+
if not isinstance(m, str) or not m.strip():
|
|
2171
|
+
continue
|
|
2172
|
+
model = m.strip()
|
|
2173
|
+
mid = f"{provider}/{model}"
|
|
2174
|
+
if allow_models_norm:
|
|
2175
|
+
# Accept either full ids or raw model names.
|
|
2176
|
+
if mid not in allow_models_norm and model not in allow_models_norm:
|
|
2177
|
+
continue
|
|
2178
|
+
pairs.append({"provider": provider, "model": model, "id": mid})
|
|
2179
|
+
model_ids.append(mid)
|
|
2180
|
+
|
|
2181
|
+
selected = pairs[idx] if pairs and idx < len(pairs) else (pairs[0] if pairs else None)
|
|
2182
|
+
return {
|
|
2183
|
+
"providers": available_providers,
|
|
2184
|
+
"models": model_ids,
|
|
2185
|
+
"pair": selected,
|
|
2186
|
+
"provider": selected.get("provider", "") if isinstance(selected, dict) else "",
|
|
2187
|
+
"model": selected.get("model", "") if isinstance(selected, dict) else "",
|
|
2188
|
+
}
|
|
2189
|
+
|
|
2190
|
+
return handler
|
|
2191
|
+
|
|
2192
|
+
def _create_provider_catalog_handler(data: Dict[str, Any]):
|
|
2193
|
+
def _as_str_list(raw: Any) -> list[str]:
|
|
2194
|
+
if not isinstance(raw, list):
|
|
2195
|
+
return []
|
|
2196
|
+
out: list[str] = []
|
|
2197
|
+
for x in raw:
|
|
2198
|
+
if isinstance(x, str) and x.strip():
|
|
2199
|
+
out.append(x.strip())
|
|
2200
|
+
return out
|
|
2201
|
+
|
|
2202
|
+
def handler(input_data: Any):
|
|
2203
|
+
allowed_providers = _as_str_list(
|
|
2204
|
+
input_data.get("allowed_providers") if isinstance(input_data, dict) else None
|
|
2205
|
+
)
|
|
2206
|
+
|
|
2207
|
+
try:
|
|
2208
|
+
from abstractcore.providers.registry import get_all_providers_with_models
|
|
2209
|
+
except Exception:
|
|
2210
|
+
return {"providers": []}
|
|
2211
|
+
|
|
2212
|
+
providers_meta = get_all_providers_with_models(include_models=False)
|
|
2213
|
+
available: list[str] = []
|
|
2214
|
+
for p in providers_meta:
|
|
2215
|
+
if not isinstance(p, dict):
|
|
2216
|
+
continue
|
|
2217
|
+
if p.get("status") != "available":
|
|
2218
|
+
continue
|
|
2219
|
+
name = p.get("name")
|
|
2220
|
+
if isinstance(name, str) and name.strip():
|
|
2221
|
+
available.append(name.strip())
|
|
2222
|
+
|
|
2223
|
+
if allowed_providers:
|
|
2224
|
+
allow = {x.lower() for x in allowed_providers}
|
|
2225
|
+
available = [p for p in available if p.lower() in allow]
|
|
2226
|
+
|
|
2227
|
+
return {"providers": available}
|
|
2228
|
+
|
|
2229
|
+
return handler
|
|
2230
|
+
|
|
2231
|
+
def _create_provider_models_handler(data: Dict[str, Any]):
|
|
2232
|
+
cfg = data.get("providerModelsConfig", {}) if isinstance(data, dict) else {}
|
|
2233
|
+
cfg = dict(cfg) if isinstance(cfg, dict) else {}
|
|
2234
|
+
|
|
2235
|
+
def _as_str_list(raw: Any) -> list[str]:
|
|
2236
|
+
if not isinstance(raw, list):
|
|
2237
|
+
return []
|
|
2238
|
+
out: list[str] = []
|
|
2239
|
+
for x in raw:
|
|
2240
|
+
if isinstance(x, str) and x.strip():
|
|
2241
|
+
out.append(x.strip())
|
|
2242
|
+
return out
|
|
2243
|
+
|
|
2244
|
+
def handler(input_data: Any):
|
|
2245
|
+
provider = None
|
|
2246
|
+
if isinstance(input_data, dict) and isinstance(input_data.get("provider"), str):
|
|
2247
|
+
provider = input_data.get("provider")
|
|
2248
|
+
if not provider and isinstance(cfg.get("provider"), str):
|
|
2249
|
+
provider = cfg.get("provider")
|
|
2250
|
+
|
|
2251
|
+
provider = str(provider or "").strip()
|
|
2252
|
+
if not provider:
|
|
2253
|
+
return {"provider": "", "models": []}
|
|
2254
|
+
|
|
2255
|
+
allowed_models = _as_str_list(
|
|
2256
|
+
input_data.get("allowed_models") if isinstance(input_data, dict) else None
|
|
2257
|
+
)
|
|
2258
|
+
if not allowed_models:
|
|
2259
|
+
# Optional allowlist from node config when the pin isn't connected.
|
|
2260
|
+
allowed_models = _as_str_list(cfg.get("allowedModels")) or _as_str_list(cfg.get("allowed_models"))
|
|
2261
|
+
allow = {m for m in allowed_models if m}
|
|
2262
|
+
|
|
2263
|
+
try:
|
|
2264
|
+
from abstractcore.providers.registry import get_available_models_for_provider
|
|
2265
|
+
except Exception:
|
|
2266
|
+
return {"provider": provider, "models": []}
|
|
2267
|
+
|
|
2268
|
+
try:
|
|
2269
|
+
models = get_available_models_for_provider(provider)
|
|
2270
|
+
except Exception:
|
|
2271
|
+
models = []
|
|
2272
|
+
if not isinstance(models, list):
|
|
2273
|
+
models = []
|
|
2274
|
+
|
|
2275
|
+
out: list[str] = []
|
|
2276
|
+
for m in models:
|
|
2277
|
+
if not isinstance(m, str) or not m.strip():
|
|
2278
|
+
continue
|
|
2279
|
+
name = m.strip()
|
|
2280
|
+
mid = f"{provider}/{name}"
|
|
2281
|
+
if allow and (name not in allow and mid not in allow):
|
|
2282
|
+
continue
|
|
2283
|
+
out.append(name)
|
|
2284
|
+
|
|
2285
|
+
return {"provider": provider, "models": out}
|
|
2286
|
+
|
|
2287
|
+
return handler
|
|
2288
|
+
|
|
2289
|
+
def _create_wait_until_handler(data: Dict[str, Any], config: Dict[str, Any]):
|
|
2290
|
+
from datetime import datetime as _dt, timedelta, timezone
|
|
2291
|
+
|
|
2292
|
+
duration_type = config.get("durationType", "seconds")
|
|
2293
|
+
|
|
2294
|
+
def handler(input_data):
|
|
2295
|
+
duration = input_data.get("duration", 0) if isinstance(input_data, dict) else 0
|
|
2296
|
+
|
|
2297
|
+
try:
|
|
2298
|
+
amount = float(duration)
|
|
2299
|
+
except (TypeError, ValueError):
|
|
2300
|
+
amount = 0
|
|
2301
|
+
|
|
2302
|
+
now = _dt.now(timezone.utc)
|
|
2303
|
+
if duration_type == "timestamp":
|
|
2304
|
+
until = str(duration or "")
|
|
2305
|
+
elif duration_type == "minutes":
|
|
2306
|
+
until = (now + timedelta(minutes=amount)).isoformat()
|
|
2307
|
+
elif duration_type == "hours":
|
|
2308
|
+
until = (now + timedelta(hours=amount)).isoformat()
|
|
2309
|
+
else:
|
|
2310
|
+
until = (now + timedelta(seconds=amount)).isoformat()
|
|
2311
|
+
|
|
2312
|
+
return {"_pending_effect": {"type": "wait_until", "until": until}}
|
|
2313
|
+
|
|
2314
|
+
return handler
|
|
2315
|
+
|
|
2316
|
+
def _create_wait_event_handler(data: Dict[str, Any], config: Dict[str, Any]):
|
|
2317
|
+
def handler(input_data):
|
|
2318
|
+
# `wait_event` is a durable pause that waits for an external signal.
|
|
2319
|
+
#
|
|
2320
|
+
# Input shape (best-effort):
|
|
2321
|
+
# - event_key: str (required; defaults to "default" for backward-compat)
|
|
2322
|
+
# - prompt: str (optional; enables human-in-the-loop UX for EVENT waits)
|
|
2323
|
+
# - choices: list[str] (optional)
|
|
2324
|
+
# - allow_free_text: bool (optional; default True)
|
|
2325
|
+
#
|
|
2326
|
+
# NOTE: The compiler will wrap `_pending_effect` into an AbstractRuntime Effect payload.
|
|
2327
|
+
event_key = input_data.get("event_key", "default") if isinstance(input_data, dict) else str(input_data)
|
|
2328
|
+
prompt = None
|
|
2329
|
+
choices = None
|
|
2330
|
+
allow_free_text = True
|
|
2331
|
+
if isinstance(input_data, dict):
|
|
2332
|
+
p = input_data.get("prompt")
|
|
2333
|
+
if isinstance(p, str) and p.strip():
|
|
2334
|
+
prompt = p
|
|
2335
|
+
ch = input_data.get("choices")
|
|
2336
|
+
if isinstance(ch, list):
|
|
2337
|
+
# Keep choices JSON-safe and predictable.
|
|
2338
|
+
choices = [str(c) for c in ch if isinstance(c, str) and str(c).strip()]
|
|
2339
|
+
aft = input_data.get("allow_free_text")
|
|
2340
|
+
if aft is None:
|
|
2341
|
+
aft = input_data.get("allowFreeText")
|
|
2342
|
+
if aft is not None:
|
|
2343
|
+
allow_free_text = bool(aft)
|
|
2344
|
+
|
|
2345
|
+
pending: Dict[str, Any] = {"type": "wait_event", "wait_key": event_key}
|
|
2346
|
+
if prompt is not None:
|
|
2347
|
+
pending["prompt"] = prompt
|
|
2348
|
+
if isinstance(choices, list):
|
|
2349
|
+
pending["choices"] = choices
|
|
2350
|
+
# Always include allow_free_text so hosts can render consistent UX.
|
|
2351
|
+
pending["allow_free_text"] = allow_free_text
|
|
2352
|
+
return {
|
|
2353
|
+
"event_data": {},
|
|
2354
|
+
"event_key": event_key,
|
|
2355
|
+
"_pending_effect": pending,
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
return handler
|
|
2359
|
+
|
|
2360
|
+
def _create_memory_note_handler(data: Dict[str, Any], config: Dict[str, Any]):
|
|
2361
|
+
def handler(input_data):
|
|
2362
|
+
content = input_data.get("content", "") if isinstance(input_data, dict) else str(input_data)
|
|
2363
|
+
tags = input_data.get("tags") if isinstance(input_data, dict) else None
|
|
2364
|
+
sources = input_data.get("sources") if isinstance(input_data, dict) else None
|
|
2365
|
+
location = input_data.get("location") if isinstance(input_data, dict) else None
|
|
2366
|
+
scope = input_data.get("scope") if isinstance(input_data, dict) else None
|
|
2367
|
+
|
|
2368
|
+
pending: Dict[str, Any] = {"type": "memory_note", "note": content, "tags": tags if isinstance(tags, dict) else {}}
|
|
2369
|
+
if isinstance(sources, dict):
|
|
2370
|
+
pending["sources"] = sources
|
|
2371
|
+
if isinstance(location, str) and location.strip():
|
|
2372
|
+
pending["location"] = location.strip()
|
|
2373
|
+
if isinstance(scope, str) and scope.strip():
|
|
2374
|
+
pending["scope"] = scope.strip()
|
|
2375
|
+
|
|
2376
|
+
keep_in_context_specified = isinstance(input_data, dict) and (
|
|
2377
|
+
"keep_in_context" in input_data or "keepInContext" in input_data
|
|
2378
|
+
)
|
|
2379
|
+
if keep_in_context_specified:
|
|
2380
|
+
raw_keep = (
|
|
2381
|
+
input_data.get("keep_in_context")
|
|
2382
|
+
if isinstance(input_data, dict) and "keep_in_context" in input_data
|
|
2383
|
+
else input_data.get("keepInContext") if isinstance(input_data, dict) else None
|
|
2384
|
+
)
|
|
2385
|
+
keep_in_context = _coerce_bool(raw_keep)
|
|
2386
|
+
else:
|
|
2387
|
+
# Visual-editor config (checkbox) default.
|
|
2388
|
+
keep_cfg = None
|
|
2389
|
+
if isinstance(config, dict):
|
|
2390
|
+
keep_cfg = config.get("keep_in_context")
|
|
2391
|
+
if keep_cfg is None:
|
|
2392
|
+
keep_cfg = config.get("keepInContext")
|
|
2393
|
+
keep_in_context = _coerce_bool(keep_cfg)
|
|
2394
|
+
if keep_in_context:
|
|
2395
|
+
pending["keep_in_context"] = True
|
|
2396
|
+
|
|
2397
|
+
return {"note_id": None, "_pending_effect": pending}
|
|
2398
|
+
|
|
2399
|
+
return handler
|
|
2400
|
+
|
|
2401
|
+
def _create_memory_query_handler(data: Dict[str, Any], config: Dict[str, Any]):
|
|
2402
|
+
def handler(input_data):
|
|
2403
|
+
query = input_data.get("query", "") if isinstance(input_data, dict) else str(input_data)
|
|
2404
|
+
limit = input_data.get("limit", 10) if isinstance(input_data, dict) else 10
|
|
2405
|
+
tags = input_data.get("tags") if isinstance(input_data, dict) else None
|
|
2406
|
+
tags_mode = input_data.get("tags_mode") if isinstance(input_data, dict) else None
|
|
2407
|
+
usernames = input_data.get("usernames") if isinstance(input_data, dict) else None
|
|
2408
|
+
locations = input_data.get("locations") if isinstance(input_data, dict) else None
|
|
2409
|
+
since = input_data.get("since") if isinstance(input_data, dict) else None
|
|
2410
|
+
until = input_data.get("until") if isinstance(input_data, dict) else None
|
|
2411
|
+
scope = input_data.get("scope") if isinstance(input_data, dict) else None
|
|
2412
|
+
try:
|
|
2413
|
+
limit_int = int(limit) if limit is not None else 10
|
|
2414
|
+
except Exception:
|
|
2415
|
+
limit_int = 10
|
|
2416
|
+
|
|
2417
|
+
pending: Dict[str, Any] = {"type": "memory_query", "query": query, "limit_spans": limit_int, "return": "both"}
|
|
2418
|
+
if isinstance(tags, dict):
|
|
2419
|
+
pending["tags"] = tags
|
|
2420
|
+
if isinstance(tags_mode, str) and tags_mode.strip():
|
|
2421
|
+
pending["tags_mode"] = tags_mode.strip()
|
|
2422
|
+
if isinstance(usernames, list):
|
|
2423
|
+
pending["usernames"] = [str(x).strip() for x in usernames if isinstance(x, str) and str(x).strip()]
|
|
2424
|
+
if isinstance(locations, list):
|
|
2425
|
+
pending["locations"] = [str(x).strip() for x in locations if isinstance(x, str) and str(x).strip()]
|
|
2426
|
+
if since is not None:
|
|
2427
|
+
pending["since"] = since
|
|
2428
|
+
if until is not None:
|
|
2429
|
+
pending["until"] = until
|
|
2430
|
+
if isinstance(scope, str) and scope.strip():
|
|
2431
|
+
pending["scope"] = scope.strip()
|
|
2432
|
+
|
|
2433
|
+
return {"results": [], "rendered": "", "_pending_effect": pending}
|
|
2434
|
+
|
|
2435
|
+
return handler
|
|
2436
|
+
|
|
2437
|
+
def _create_memory_rehydrate_handler(data: Dict[str, Any], config: Dict[str, Any]):
|
|
2438
|
+
def handler(input_data):
|
|
2439
|
+
raw = input_data.get("span_ids") if isinstance(input_data, dict) else None
|
|
2440
|
+
if raw is None and isinstance(input_data, dict):
|
|
2441
|
+
raw = input_data.get("span_id")
|
|
2442
|
+
span_ids: list[Any] = []
|
|
2443
|
+
if isinstance(raw, list):
|
|
2444
|
+
span_ids = list(raw)
|
|
2445
|
+
elif raw is not None:
|
|
2446
|
+
span_ids = [raw]
|
|
2447
|
+
|
|
2448
|
+
placement = input_data.get("placement") if isinstance(input_data, dict) else None
|
|
2449
|
+
placement_str = str(placement).strip() if isinstance(placement, str) else "after_summary"
|
|
2450
|
+
if placement_str not in {"after_summary", "after_system", "end"}:
|
|
2451
|
+
placement_str = "after_summary"
|
|
2452
|
+
|
|
2453
|
+
max_messages = input_data.get("max_messages") if isinstance(input_data, dict) else None
|
|
2454
|
+
|
|
2455
|
+
pending: Dict[str, Any] = {"type": "memory_rehydrate", "span_ids": span_ids, "placement": placement_str}
|
|
2456
|
+
if max_messages is not None:
|
|
2457
|
+
pending["max_messages"] = max_messages
|
|
2458
|
+
return {"inserted": 0, "skipped": 0, "_pending_effect": pending}
|
|
2459
|
+
|
|
2460
|
+
return handler
|
|
2461
|
+
|
|
2462
|
+
def _create_handler(node_type: NodeType, data: Dict[str, Any]) -> Any:
|
|
2463
|
+
type_str = node_type.value if isinstance(node_type, NodeType) else str(node_type)
|
|
2464
|
+
|
|
2465
|
+
if type_str == "get_var":
|
|
2466
|
+
return _create_get_var_handler(data)
|
|
2467
|
+
|
|
2468
|
+
if type_str == "bool_var":
|
|
2469
|
+
return _create_bool_var_handler(data)
|
|
2470
|
+
|
|
2471
|
+
if type_str == "var_decl":
|
|
2472
|
+
return _create_var_decl_handler(data)
|
|
2473
|
+
|
|
2474
|
+
if type_str == "set_var":
|
|
2475
|
+
return _create_set_var_handler(data)
|
|
2476
|
+
|
|
2477
|
+
if type_str == "concat":
|
|
2478
|
+
return _create_concat_handler(data)
|
|
2479
|
+
|
|
2480
|
+
if type_str == "make_array":
|
|
2481
|
+
return _create_make_array_handler(data)
|
|
2482
|
+
|
|
2483
|
+
if type_str == "array_concat":
|
|
2484
|
+
return _create_array_concat_handler(data)
|
|
2485
|
+
|
|
2486
|
+
if type_str == "read_file":
|
|
2487
|
+
return _create_read_file_handler(data)
|
|
2488
|
+
|
|
2489
|
+
if type_str == "write_file":
|
|
2490
|
+
return _create_write_file_handler(data)
|
|
2491
|
+
|
|
2492
|
+
# Sequence / Parallel are scheduler nodes compiled specially by `compile_flow`.
|
|
2493
|
+
# Their runtime semantics are handled in `abstractflow.adapters.control_adapter`.
|
|
2494
|
+
if type_str in ("sequence", "parallel"):
|
|
2495
|
+
return lambda x: x
|
|
2496
|
+
|
|
2497
|
+
builtin = get_builtin_handler(type_str)
|
|
2498
|
+
if builtin:
|
|
2499
|
+
return _wrap_builtin(builtin, data)
|
|
2500
|
+
|
|
2501
|
+
if type_str == "code":
|
|
2502
|
+
code = data.get("code", "def transform(input):\n return input")
|
|
2503
|
+
function_name = data.get("functionName", "transform")
|
|
2504
|
+
return create_code_handler(code, function_name)
|
|
2505
|
+
|
|
2506
|
+
if type_str == "agent":
|
|
2507
|
+
return _create_agent_input_handler(data)
|
|
2508
|
+
|
|
2509
|
+
if type_str == "model_catalog":
|
|
2510
|
+
return _create_model_catalog_handler(data)
|
|
2511
|
+
|
|
2512
|
+
if type_str == "provider_catalog":
|
|
2513
|
+
return _create_provider_catalog_handler(data)
|
|
2514
|
+
|
|
2515
|
+
if type_str == "provider_models":
|
|
2516
|
+
return _create_provider_models_handler(data)
|
|
2517
|
+
|
|
2518
|
+
if type_str == "subflow":
|
|
2519
|
+
return _create_subflow_effect_builder(data)
|
|
2520
|
+
|
|
2521
|
+
if type_str == "break_object":
|
|
2522
|
+
return _create_break_object_handler(data)
|
|
2523
|
+
|
|
2524
|
+
if type_str == "function":
|
|
2525
|
+
if "code" in data:
|
|
2526
|
+
return create_code_handler(data["code"], data.get("functionName", "transform"))
|
|
2527
|
+
if "expression" in data:
|
|
2528
|
+
return _create_expression_handler(data["expression"])
|
|
2529
|
+
return lambda x: x
|
|
2530
|
+
|
|
2531
|
+
if type_str == "on_flow_end":
|
|
2532
|
+
return _create_flow_end_handler(data)
|
|
2533
|
+
|
|
2534
|
+
if type_str in ("on_flow_start", "on_user_request", "on_agent_message"):
|
|
2535
|
+
return _create_event_handler(type_str, data)
|
|
2536
|
+
|
|
2537
|
+
if type_str == "if":
|
|
2538
|
+
return _create_if_handler(data)
|
|
2539
|
+
if type_str == "switch":
|
|
2540
|
+
return _create_switch_handler(data)
|
|
2541
|
+
if type_str == "while":
|
|
2542
|
+
return _create_while_handler(data)
|
|
2543
|
+
if type_str == "for":
|
|
2544
|
+
return _create_for_handler(data)
|
|
2545
|
+
if type_str == "loop":
|
|
2546
|
+
return _create_loop_handler(data)
|
|
2547
|
+
|
|
2548
|
+
if type_str in EFFECT_NODE_TYPES:
|
|
2549
|
+
return _create_effect_handler(type_str, data)
|
|
2550
|
+
|
|
2551
|
+
return lambda x: x
|
|
2552
|
+
|
|
2553
|
+
for node in visual.nodes:
|
|
2554
|
+
type_str = node.type.value if hasattr(node.type, "value") else str(node.type)
|
|
2555
|
+
|
|
2556
|
+
if type_str in LITERAL_NODE_TYPES:
|
|
2557
|
+
continue
|
|
2558
|
+
|
|
2559
|
+
base_handler = _create_handler(node.type, node.data)
|
|
2560
|
+
|
|
2561
|
+
if not _has_execution_pins(type_str, node.data):
|
|
2562
|
+
pure_base_handlers[node.id] = base_handler
|
|
2563
|
+
pure_node_ids.add(node.id)
|
|
2564
|
+
if type_str in {"get_var", "bool_var", "var_decl"}:
|
|
2565
|
+
volatile_pure_node_ids.add(node.id)
|
|
2566
|
+
continue
|
|
2567
|
+
|
|
2568
|
+
# Ignore disconnected/unreachable execution nodes.
|
|
2569
|
+
if reachable_exec and node.id not in reachable_exec:
|
|
2570
|
+
continue
|
|
2571
|
+
|
|
2572
|
+
wrapped_handler = _create_data_aware_handler(
|
|
2573
|
+
node_id=node.id,
|
|
2574
|
+
base_handler=base_handler,
|
|
2575
|
+
data_edges=data_edge_map.get(node.id, {}),
|
|
2576
|
+
pin_defaults=pin_defaults_by_node_id.get(node.id),
|
|
2577
|
+
node_outputs=flow._node_outputs, # type: ignore[attr-defined]
|
|
2578
|
+
ensure_node_output=_ensure_node_output,
|
|
2579
|
+
volatile_node_ids=volatile_pure_node_ids,
|
|
2580
|
+
)
|
|
2581
|
+
|
|
2582
|
+
input_key = node.data.get("inputKey")
|
|
2583
|
+
output_key = node.data.get("outputKey")
|
|
2584
|
+
|
|
2585
|
+
effect_type: Optional[str] = None
|
|
2586
|
+
effect_config: Optional[Dict[str, Any]] = None
|
|
2587
|
+
if type_str in EFFECT_NODE_TYPES:
|
|
2588
|
+
effect_type = type_str
|
|
2589
|
+
effect_config = node.data.get("effectConfig", {})
|
|
2590
|
+
elif type_str == "on_schedule":
|
|
2591
|
+
# Schedule trigger: compiles into WAIT_UNTIL under the hood.
|
|
2592
|
+
effect_type = "on_schedule"
|
|
2593
|
+
effect_config = node.data.get("eventConfig", {})
|
|
2594
|
+
elif type_str == "on_event":
|
|
2595
|
+
# Custom event listener (Blueprint-style "Custom Event").
|
|
2596
|
+
# Compiles into WAIT_EVENT under the hood.
|
|
2597
|
+
effect_type = "on_event"
|
|
2598
|
+
effect_config = node.data.get("eventConfig", {})
|
|
2599
|
+
elif type_str == "agent":
|
|
2600
|
+
effect_type = "agent"
|
|
2601
|
+
raw_cfg = node.data.get("agentConfig", {})
|
|
2602
|
+
cfg = dict(raw_cfg) if isinstance(raw_cfg, dict) else {}
|
|
2603
|
+
cfg.setdefault(
|
|
2604
|
+
"_react_workflow_id",
|
|
2605
|
+
visual_react_workflow_id(flow_id=visual.id, node_id=node.id),
|
|
2606
|
+
)
|
|
2607
|
+
effect_config = cfg
|
|
2608
|
+
elif type_str in ("sequence", "parallel"):
|
|
2609
|
+
# Control-flow scheduler nodes. Store pin order so compilation can
|
|
2610
|
+
# execute branches deterministically (Blueprint-style).
|
|
2611
|
+
effect_type = type_str
|
|
2612
|
+
|
|
2613
|
+
pins = node.data.get("outputs") if isinstance(node.data, dict) else None
|
|
2614
|
+
exec_ids: list[str] = []
|
|
2615
|
+
if isinstance(pins, list):
|
|
2616
|
+
for p in pins:
|
|
2617
|
+
if not isinstance(p, dict):
|
|
2618
|
+
continue
|
|
2619
|
+
if p.get("type") != "execution":
|
|
2620
|
+
continue
|
|
2621
|
+
pid = p.get("id")
|
|
2622
|
+
if isinstance(pid, str) and pid:
|
|
2623
|
+
exec_ids.append(pid)
|
|
2624
|
+
|
|
2625
|
+
def _then_key(h: str) -> int:
|
|
2626
|
+
try:
|
|
2627
|
+
if h.startswith("then:"):
|
|
2628
|
+
return int(h.split(":", 1)[1])
|
|
2629
|
+
except Exception:
|
|
2630
|
+
pass
|
|
2631
|
+
return 10**9
|
|
2632
|
+
|
|
2633
|
+
then_handles = sorted([h for h in exec_ids if h.startswith("then:")], key=_then_key)
|
|
2634
|
+
cfg = {"then_handles": then_handles}
|
|
2635
|
+
if type_str == "parallel":
|
|
2636
|
+
cfg["completed_handle"] = "completed"
|
|
2637
|
+
effect_config = cfg
|
|
2638
|
+
elif type_str == "loop":
|
|
2639
|
+
# Control-flow scheduler node (Blueprint-style foreach).
|
|
2640
|
+
# Runtime semantics are handled in `abstractflow.adapters.control_adapter`.
|
|
2641
|
+
effect_type = type_str
|
|
2642
|
+
effect_config = {}
|
|
2643
|
+
elif type_str == "while":
|
|
2644
|
+
# Control-flow scheduler node (Blueprint-style while).
|
|
2645
|
+
# Runtime semantics are handled in `abstractflow.adapters.control_adapter`.
|
|
2646
|
+
effect_type = type_str
|
|
2647
|
+
effect_config = {}
|
|
2648
|
+
elif type_str == "for":
|
|
2649
|
+
# Control-flow scheduler node (Blueprint-style numeric for).
|
|
2650
|
+
# Runtime semantics are handled in `abstractflow.adapters.control_adapter`.
|
|
2651
|
+
effect_type = type_str
|
|
2652
|
+
effect_config = {}
|
|
2653
|
+
elif type_str == "subflow":
|
|
2654
|
+
effect_type = "start_subworkflow"
|
|
2655
|
+
subflow_id = node.data.get("subflowId") or node.data.get("flowId")
|
|
2656
|
+
output_pin_ids: list[str] = []
|
|
2657
|
+
outs = node.data.get("outputs")
|
|
2658
|
+
if isinstance(outs, list):
|
|
2659
|
+
for p in outs:
|
|
2660
|
+
if not isinstance(p, dict):
|
|
2661
|
+
continue
|
|
2662
|
+
if p.get("type") == "execution":
|
|
2663
|
+
continue
|
|
2664
|
+
pid = p.get("id")
|
|
2665
|
+
if isinstance(pid, str) and pid and pid != "output":
|
|
2666
|
+
output_pin_ids.append(pid)
|
|
2667
|
+
effect_config = {"workflow_id": subflow_id, "output_pins": output_pin_ids}
|
|
2668
|
+
|
|
2669
|
+
# Always attach minimal visual metadata for downstream compilation/wrapping.
|
|
2670
|
+
meta_cfg: Dict[str, Any] = {"_visual_type": type_str}
|
|
2671
|
+
if isinstance(effect_config, dict):
|
|
2672
|
+
meta_cfg.update(effect_config)
|
|
2673
|
+
effect_config = meta_cfg
|
|
2674
|
+
|
|
2675
|
+
flow.add_node(
|
|
2676
|
+
node_id=node.id,
|
|
2677
|
+
handler=wrapped_handler,
|
|
2678
|
+
input_key=input_key,
|
|
2679
|
+
output_key=output_key,
|
|
2680
|
+
effect_type=effect_type,
|
|
2681
|
+
effect_config=effect_config,
|
|
2682
|
+
)
|
|
2683
|
+
|
|
2684
|
+
for edge in visual.edges:
|
|
2685
|
+
if edge.targetHandle == "exec-in":
|
|
2686
|
+
if edge.source in flow.nodes and edge.target in flow.nodes:
|
|
2687
|
+
flow.add_edge(edge.source, edge.target, source_handle=edge.sourceHandle)
|
|
2688
|
+
|
|
2689
|
+
if visual.entryNode and visual.entryNode in flow.nodes:
|
|
2690
|
+
flow.set_entry(visual.entryNode)
|
|
2691
|
+
else:
|
|
2692
|
+
targets = {e.target for e in visual.edges if e.targetHandle == "exec-in"}
|
|
2693
|
+
for node_id in flow.nodes:
|
|
2694
|
+
if node_id not in targets:
|
|
2695
|
+
flow.set_entry(node_id)
|
|
2696
|
+
break
|
|
2697
|
+
if not flow.entry_node and flow.nodes:
|
|
2698
|
+
flow.set_entry(next(iter(flow.nodes)))
|
|
2699
|
+
|
|
2700
|
+
# Pure (no-exec) nodes are cached in `flow._node_outputs` for data-edge resolution.
|
|
2701
|
+
# Some schedulers (While, On Event, On Schedule) must invalidate these caches between iterations.
|
|
2702
|
+
flow._pure_node_ids = pure_node_ids # type: ignore[attr-defined]
|
|
2703
|
+
|
|
2704
|
+
return flow
|
|
2705
|
+
|
|
2706
|
+
|
|
2707
|
+
def _create_data_aware_handler(
|
|
2708
|
+
node_id: str,
|
|
2709
|
+
base_handler,
|
|
2710
|
+
data_edges: Dict[str, tuple[str, str]],
|
|
2711
|
+
pin_defaults: Optional[Dict[str, Any]],
|
|
2712
|
+
node_outputs: Dict[str, Dict[str, Any]],
|
|
2713
|
+
*,
|
|
2714
|
+
ensure_node_output=None,
|
|
2715
|
+
volatile_node_ids: Optional[set[str]] = None,
|
|
2716
|
+
):
|
|
2717
|
+
"""Wrap a handler to resolve data edge inputs before execution."""
|
|
2718
|
+
|
|
2719
|
+
volatile: set[str] = volatile_node_ids if isinstance(volatile_node_ids, set) else set()
|
|
2720
|
+
|
|
2721
|
+
def wrapped_handler(input_data):
|
|
2722
|
+
resolved_input: Dict[str, Any] = {}
|
|
2723
|
+
|
|
2724
|
+
if isinstance(input_data, dict):
|
|
2725
|
+
resolved_input.update(input_data)
|
|
2726
|
+
|
|
2727
|
+
for target_pin, (source_node, source_pin) in data_edges.items():
|
|
2728
|
+
if ensure_node_output is not None and (source_node not in node_outputs or source_node in volatile):
|
|
2729
|
+
ensure_node_output(source_node)
|
|
2730
|
+
if source_node in node_outputs:
|
|
2731
|
+
source_output = node_outputs[source_node]
|
|
2732
|
+
if isinstance(source_output, dict) and source_pin in source_output:
|
|
2733
|
+
resolved_input[target_pin] = source_output[source_pin]
|
|
2734
|
+
elif source_pin in ("result", "output"):
|
|
2735
|
+
resolved_input[target_pin] = source_output
|
|
2736
|
+
|
|
2737
|
+
if pin_defaults:
|
|
2738
|
+
for pin_id, value in pin_defaults.items():
|
|
2739
|
+
# Connected pins always win (even if the upstream value is None).
|
|
2740
|
+
if pin_id in data_edges:
|
|
2741
|
+
continue
|
|
2742
|
+
if pin_id not in resolved_input:
|
|
2743
|
+
# Clone object/array defaults so handlers can't mutate the shared default.
|
|
2744
|
+
if isinstance(value, (dict, list)):
|
|
2745
|
+
try:
|
|
2746
|
+
import copy
|
|
2747
|
+
|
|
2748
|
+
resolved_input[pin_id] = copy.deepcopy(value)
|
|
2749
|
+
except Exception:
|
|
2750
|
+
resolved_input[pin_id] = value
|
|
2751
|
+
else:
|
|
2752
|
+
resolved_input[pin_id] = value
|
|
2753
|
+
|
|
2754
|
+
result = base_handler(resolved_input if resolved_input else input_data)
|
|
2755
|
+
node_outputs[node_id] = result
|
|
2756
|
+
return result
|
|
2757
|
+
|
|
2758
|
+
return wrapped_handler
|
|
2759
|
+
|
|
2760
|
+
|
|
2761
|
+
def execute_visual_flow(visual_flow: VisualFlow, input_data: Dict[str, Any], *, flows: Dict[str, VisualFlow]) -> Dict[str, Any]:
|
|
2762
|
+
"""Execute a visual flow with a correctly wired runtime (LLM/MEMORY/SUBFLOW)."""
|
|
2763
|
+
runner = create_visual_runner(visual_flow, flows=flows)
|
|
2764
|
+
result = runner.run(input_data)
|
|
2765
|
+
|
|
2766
|
+
if isinstance(result, dict) and result.get("waiting"):
|
|
2767
|
+
state = runner.get_state()
|
|
2768
|
+
wait = state.waiting if state else None
|
|
2769
|
+
return {
|
|
2770
|
+
"success": False,
|
|
2771
|
+
"waiting": True,
|
|
2772
|
+
"error": "Flow is waiting for input. Use a host resume mechanism to continue.",
|
|
2773
|
+
"run_id": runner.run_id,
|
|
2774
|
+
"wait_key": wait.wait_key if wait else None,
|
|
2775
|
+
"prompt": wait.prompt if wait else None,
|
|
2776
|
+
"choices": list(wait.choices) if wait and isinstance(wait.choices, list) else [],
|
|
2777
|
+
"allow_free_text": bool(wait.allow_free_text) if wait else None,
|
|
2778
|
+
}
|
|
2779
|
+
|
|
2780
|
+
if isinstance(result, dict):
|
|
2781
|
+
return {
|
|
2782
|
+
"success": bool(result.get("success", True)),
|
|
2783
|
+
"waiting": False,
|
|
2784
|
+
"result": result.get("result"),
|
|
2785
|
+
"error": result.get("error"),
|
|
2786
|
+
"run_id": runner.run_id,
|
|
2787
|
+
}
|
|
2788
|
+
|
|
2789
|
+
return {"success": True, "waiting": False, "result": result, "run_id": runner.run_id}
|