wfpy 1.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. wfpy/__init__.py +89 -0
  2. wfpy/_action_runtime.py +293 -0
  3. wfpy/_agent_cli_runtime.py +1374 -0
  4. wfpy/_agent_decorator.py +228 -0
  5. wfpy/_agent_io_runtime.py +1156 -0
  6. wfpy/_agent_prompt_runtime.py +713 -0
  7. wfpy/_agent_request_runtime.py +320 -0
  8. wfpy/_agent_staging_runtime.py +409 -0
  9. wfpy/_agent_tools_runtime.py +844 -0
  10. wfpy/_agent_validation_runtime.py +665 -0
  11. wfpy/_ask_user_mcp.py +180 -0
  12. wfpy/_checkpoint_runtime.py +507 -0
  13. wfpy/_context_runtime.py +410 -0
  14. wfpy/_elicitation_runtime.py +346 -0
  15. wfpy/_graph_context.py +12 -0
  16. wfpy/_graph_export.py +900 -0
  17. wfpy/_hierarchy_export.py +125 -0
  18. wfpy/_invoke_runtime.py +867 -0
  19. wfpy/_journal_runtime.py +298 -0
  20. wfpy/_lsp_client.py +378 -0
  21. wfpy/_mcp_client.py +219 -0
  22. wfpy/_mock_agent_runtime.py +110 -0
  23. wfpy/_plan_outputs_runtime.py +226 -0
  24. wfpy/_rewrite_transformers.py +123 -0
  25. wfpy/_run_artifacts.py +668 -0
  26. wfpy/_run_event_stream.py +211 -0
  27. wfpy/_run_finalization.py +363 -0
  28. wfpy/_step_external_runtime.py +379 -0
  29. wfpy/_step_streamblocks_runtime.py +253 -0
  30. wfpy/_validation_runtime.py +710 -0
  31. wfpy/_workflow_instance.py +197 -0
  32. wfpy/acp_client.py +729 -0
  33. wfpy/cli.py +768 -0
  34. wfpy/connectors.py +178 -0
  35. wfpy/core.py +1752 -0
  36. wfpy/graph.py +544 -0
  37. wfpy/package_exports.py +160 -0
  38. wfpy/partial.py +602 -0
  39. wfpy/py.typed +1 -0
  40. wfpy/rewrite.py +2452 -0
  41. wfpy/runner.py +3202 -0
  42. wfpy/sidecar.py +652 -0
  43. wfpy/sidecar_cli.py +49 -0
  44. wfpy/types.py +344 -0
  45. wfpy/workflow_identity.py +85 -0
  46. wfpy-1.1.0.dist-info/METADATA +400 -0
  47. wfpy-1.1.0.dist-info/RECORD +51 -0
  48. wfpy-1.1.0.dist-info/WHEEL +5 -0
  49. wfpy-1.1.0.dist-info/entry_points.txt +4 -0
  50. wfpy-1.1.0.dist-info/licenses/LICENSE +201 -0
  51. wfpy-1.1.0.dist-info/top_level.txt +1 -0
wfpy/__init__.py ADDED
@@ -0,0 +1,89 @@
1
+ """wfpy — Pythonic agentic workflows on actor-dataflow semantics.
2
+
3
+ FIFO-queued dataflow scheduling with round-robin actor execution,
4
+ multi-action tasks with guards, and external tool / LLM agent integration.
5
+
6
+ Public API::
7
+
8
+ from wfpy import task, workflow, connect, if_, loop, Port, Resource, File, Map
9
+ from wfpy import action, guard, agent, tool, viewer, streamblocks, source
10
+ from wfpy import streaming, pipeline, keep, config
11
+ from wfpy import run
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ # Types
17
+ from wfpy.types import (
18
+ Port,
19
+ Resource,
20
+ File,
21
+ Map,
22
+ PortInstance,
23
+ infer_resource_kind,
24
+ is_url_resource,
25
+ is_http_resource,
26
+ is_folder_resource,
27
+ is_file_resource,
28
+ )
29
+
30
+ # Decorators
31
+ from wfpy.core import (
32
+ task,
33
+ action,
34
+ guard,
35
+ workflow,
36
+ agent,
37
+ tool,
38
+ viewer,
39
+ streamblocks,
40
+ source,
41
+ context,
42
+ streaming,
43
+ pipeline,
44
+ keep,
45
+ config,
46
+ )
47
+
48
+ # Wiring
49
+ from wfpy.graph import connect, if_, loop
50
+
51
+ # Execution
52
+ from wfpy.runner import run
53
+
54
+ __all__ = [
55
+ # Types
56
+ "Port",
57
+ "Resource",
58
+ "File",
59
+ "Map",
60
+ "PortInstance",
61
+ "infer_resource_kind",
62
+ "is_url_resource",
63
+ "is_http_resource",
64
+ "is_folder_resource",
65
+ "is_file_resource",
66
+ # Decorators
67
+ "task",
68
+ "action",
69
+ "guard",
70
+ "workflow",
71
+ "agent",
72
+ "tool",
73
+ "viewer",
74
+ "streamblocks",
75
+ "source",
76
+ "context",
77
+ "streaming",
78
+ "pipeline",
79
+ "keep",
80
+ "config",
81
+ # Wiring
82
+ "connect",
83
+ "if_",
84
+ "loop",
85
+ # Execution
86
+ "run",
87
+ ]
88
+
89
+ __version__ = "0.1.0"
@@ -0,0 +1,293 @@
1
+ """Internal helpers for internal (dataflow) action execution."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ import shutil
7
+ from pathlib import Path
8
+ from typing import Any
9
+
10
+ from wfpy._agent_validation_runtime import _is_resource_type
11
+ from wfpy._context_runtime import (
12
+ _ActionContextFacade,
13
+ _action_accepts_ctx,
14
+ _actor_context_policy,
15
+ _apply_context_patch,
16
+ _build_context_view,
17
+ )
18
+ from wfpy._run_artifacts import _record_edge_token
19
+ from wfpy.core import ActionDef, _active_wf_config, _WorkflowEnvConfig
20
+ from wfpy.types import File, Resource
21
+
22
+ logger = logging.getLogger("wfpy")
23
+
24
+
25
+ def _ordered_internal_actions(actor: Any) -> list[ActionDef]:
26
+ """Return candidate internal actions after schedule / priority filtering."""
27
+ meta = actor.meta
28
+ actions = list(meta.actions)
29
+
30
+ schedule = meta.schedule
31
+ if schedule is not None:
32
+ current_state = getattr(actor.instance, "_wfpy_schedule_state", schedule.initial)
33
+ if not isinstance(current_state, str) or not current_state.strip():
34
+ current_state = schedule.initial
35
+ setattr(actor.instance, "_wfpy_schedule_state", current_state)
36
+ allowed = schedule.by_state.get(current_state, {})
37
+ if not allowed:
38
+ return []
39
+ actions = [adef for adef in actions if adef.name in allowed]
40
+
41
+ priority = meta.priority
42
+ if priority is None:
43
+ return actions
44
+
45
+ max_group = len(priority.rules)
46
+ max_rank = sum(len(rule) for rule in priority.rules)
47
+
48
+ def _priority_key(adef: ActionDef) -> tuple[int, int, int]:
49
+ group_rank = priority.rank.get(adef.name)
50
+ if group_rank is None:
51
+ return (max_group, max_rank, adef.order)
52
+ return (group_rank[0], group_rank[1], adef.order)
53
+
54
+ return sorted(actions, key=_priority_key)
55
+
56
+
57
+ def _try_fire_action(
58
+ actor: Any,
59
+ adef: ActionDef,
60
+ out_dir: Path,
61
+ plan: Any,
62
+ verbose: bool,
63
+ ) -> tuple[bool, Any | None]:
64
+ """Attempt to fire a single action. Returns (fired, result)."""
65
+ # Determine required tokens per input port
66
+ consumes = adef.consumes
67
+ if consumes is None:
68
+ # Auto-infer: one token per input port that has a queue
69
+ consumes = {}
70
+ for port_name, queues in actor.in_queues.items():
71
+ consumes[port_name] = 1
72
+
73
+ # Check token availability (peek)
74
+ peeked_values: dict[str, list[Any]] = {}
75
+ for port_name, count in consumes.items():
76
+ queues = actor.in_queues.get(port_name, [])
77
+ if not queues:
78
+ return False, None # no queue for this port → can't fire
79
+ q = queues[0] # take from first queue for this port
80
+ if q.size() < count:
81
+ return False, None # not enough tokens
82
+ peeked_values[port_name] = q.peek(count)
83
+
84
+ # Flatten peeked values for function args
85
+ flat_args: list[Any] = []
86
+ for port_name in consumes:
87
+ flat_args.extend(peeked_values[port_name])
88
+
89
+ consumed_values = [value for values in peeked_values.values() for value in values]
90
+
91
+ # Check guard
92
+ if adef.guard_fn is not None:
93
+ try:
94
+ guard_result = adef.guard_fn(actor.instance, *flat_args)
95
+ except Exception:
96
+ logger.warning(
97
+ "Guard %s.%s raised; treating as not fireable",
98
+ actor.name,
99
+ adef.name,
100
+ exc_info=True,
101
+ )
102
+ return False, None
103
+ if not guard_result:
104
+ return False, None
105
+
106
+ # Guard passed — commit consumption (dequeue tokens)
107
+ for port_name, count in consumes.items():
108
+ queues = actor.in_queues.get(port_name, [])
109
+ if queues:
110
+ q = queues[0]
111
+ for _ in range(count):
112
+ q.dequeue()
113
+
114
+ policy = _actor_context_policy(actor)
115
+ view = _build_context_view(plan, actor, policy)
116
+ ctx_facade = _ActionContextFacade(plan, actor, policy, view)
117
+
118
+ # Expose workflow @config env/path so function-style @tool wrappers
119
+ # can merge them into their subprocess environment.
120
+ _wf_token = _active_wf_config.set(
121
+ _WorkflowEnvConfig(env=dict(plan.env), search_paths=list(plan.search_paths))
122
+ )
123
+
124
+ # Execute the action
125
+ try:
126
+ if _action_accepts_ctx(adef.fn, len(flat_args)):
127
+ result = adef.fn(actor.instance, *flat_args, ctx_facade)
128
+ else:
129
+ result = adef.fn(actor.instance, *flat_args)
130
+ except Exception as e:
131
+ logger.error("Action %s.%s raised: %s", actor.name, adef.name, e)
132
+ raise
133
+ finally:
134
+ _active_wf_config.reset(_wf_token)
135
+
136
+ patch = ctx_facade.context_patch()
137
+ if patch is not None:
138
+ _apply_context_patch(plan, actor, policy, patch, source="internal-action")
139
+
140
+ schedule = actor.meta.schedule
141
+ schedule_state_changed = False
142
+ if schedule is not None:
143
+ current_state = getattr(actor.instance, "_wfpy_schedule_state", schedule.initial)
144
+ next_state = schedule.by_state.get(current_state, {}).get(adef.name)
145
+ if next_state is not None:
146
+ setattr(actor.instance, "_wfpy_schedule_state", next_state)
147
+ schedule_state_changed = next_state != current_state
148
+
149
+ def _is_within(base: Path, candidate: Path) -> bool:
150
+ try:
151
+ candidate.resolve().relative_to(base.resolve())
152
+ return True
153
+ except (ValueError, OSError, RuntimeError):
154
+ return False
155
+
156
+ def _remember_preserved_resource_path(locator: str) -> None:
157
+ if not locator:
158
+ return
159
+ try:
160
+ plan.wf_input_resource_paths.add(str(Path(locator).resolve()))
161
+ except OSError:
162
+ plan.wf_input_resource_paths.add(locator)
163
+
164
+ def _normalize_internal_output(port_name: str, value: Any) -> Any:
165
+ if value is None:
166
+ return None
167
+ pd = actor.meta.output_ports.get(port_name)
168
+ if pd is None or not _is_resource_type(pd.port_type):
169
+ return value
170
+
171
+ locator: str | None = None
172
+ if isinstance(value, (File, Resource)):
173
+ locator = value.path or None
174
+ elif isinstance(value, str):
175
+ locator = value
176
+ if not locator:
177
+ return value
178
+
179
+ # Preserve pass-through file/resource tokens that already came from an
180
+ # upstream queue. Only materialize genuinely new outputs produced by the
181
+ # current action.
182
+ for consumed in consumed_values:
183
+ if consumed is value:
184
+ return value
185
+ if isinstance(consumed, (File, Resource)) and consumed.path == locator:
186
+ return value
187
+ if isinstance(consumed, str) and consumed == locator:
188
+ return value
189
+
190
+ # Preserve configured file/resource paths that tasks re-emit directly,
191
+ # such as FileSource outputs. Re-materializing them into the run workdir
192
+ # can break sibling-relative includes/imports for source files.
193
+ actor_path = getattr(actor.instance, "path", None)
194
+ if isinstance(actor_path, (File, Resource)) and actor_path.path == locator:
195
+ _remember_preserved_resource_path(locator)
196
+ return value
197
+ if isinstance(actor_path, str) and actor_path == locator:
198
+ _remember_preserved_resource_path(locator)
199
+ return value
200
+
201
+ path = Path(locator)
202
+ try:
203
+ resolved_locator = str(path.resolve())
204
+ except OSError:
205
+ resolved_locator = locator
206
+ if resolved_locator in plan.wf_input_resource_paths:
207
+ _remember_preserved_resource_path(locator)
208
+ return value
209
+ if not path.exists() or _is_within(out_dir, path):
210
+ return value
211
+
212
+ ext = path.suffix or (pd.ext or "")
213
+ target = out_dir / f"{actor.name}__{port_name}__{actor.fire_count}{ext}"
214
+ target.parent.mkdir(parents=True, exist_ok=True)
215
+
216
+ if path.is_dir():
217
+ shutil.copytree(path, target, dirs_exist_ok=True)
218
+ else:
219
+ shutil.copy2(path, target)
220
+
221
+ if isinstance(value, File):
222
+ return File(str(target), ext=value.ext, validate=list(value.validate))
223
+ if isinstance(value, Resource):
224
+ return Resource(
225
+ str(target),
226
+ ext=value.ext,
227
+ validate=list(value.validate),
228
+ kind=value.kind,
229
+ )
230
+ return str(target)
231
+
232
+ effective_produces = adef.produces
233
+ if effective_produces is None:
234
+ effective_produces = {
235
+ port_name: 1
236
+ for port_name, pd in actor.meta.output_ports.items()
237
+ if pd.direction in ("out",)
238
+ or (pd.name or pd.attr_name).lower() in ("out", "output", "result", "report", "summary")
239
+ }
240
+
241
+ # Enqueue outputs (fan-out to all connected queues)
242
+ if result is not None:
243
+ produces = adef.produces
244
+ if produces is None:
245
+ # Auto: push to all output ports
246
+ for port_name, queues in actor.out_queues.items():
247
+ normalized = _normalize_internal_output(port_name, result)
248
+ for q in queues:
249
+ q.enqueue(normalized)
250
+ _record_edge_token(plan, q, normalized)
251
+ else:
252
+ if isinstance(result, dict) and len(produces) > 1:
253
+ # Dict result → distribute by port name
254
+ for port_name in produces:
255
+ val = _normalize_internal_output(port_name, result.get(port_name))
256
+ if val is not None:
257
+ for q in actor.out_queues.get(port_name, []):
258
+ q.enqueue(val)
259
+ _record_edge_token(plan, q, val)
260
+ elif isinstance(result, (tuple, list)) and len(produces) > 1:
261
+ # Positional result → distribute by index
262
+ port_names = list(produces.keys())
263
+ for i, port_name in enumerate(port_names):
264
+ if i < len(result):
265
+ val = _normalize_internal_output(port_name, result[i])
266
+ for q in actor.out_queues.get(port_name, []):
267
+ q.enqueue(val)
268
+ _record_edge_token(plan, q, val)
269
+ elif isinstance(result, dict) and len(produces) == 1:
270
+ # Single declared output may still be returned as {port_name: value}
271
+ port_name = next(iter(produces))
272
+ val = _normalize_internal_output(port_name, result.get(port_name))
273
+ if val is not None:
274
+ for q in actor.out_queues.get(port_name, []):
275
+ q.enqueue(val)
276
+ _record_edge_token(plan, q, val)
277
+ else:
278
+ # Single output → all produced ports
279
+ for port_name in produces:
280
+ val = _normalize_internal_output(port_name, result)
281
+ for q in actor.out_queues.get(port_name, []):
282
+ q.enqueue(val)
283
+ _record_edge_token(plan, q, val)
284
+
285
+ # If the action consumed input tokens or advanced schedule state, it already
286
+ # made observable progress even when it intentionally emits no output.
287
+ fired = (
288
+ result is not None
289
+ or not bool(effective_produces)
290
+ or bool(consumed_values)
291
+ or schedule_state_changed
292
+ )
293
+ return fired, result