agentic-blocks 0.1.18__py3-none-any.whl → 0.1.20__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.
- agentic_blocks/tracing/__init__.py +13 -0
- agentic_blocks/tracing/config.py +111 -0
- agentic_blocks/tracing/core.py +287 -0
- agentic_blocks/tracing/decorator.py +316 -0
- agentic_blocks/visualization/visualize.py +1014 -0
- {agentic_blocks-0.1.18.dist-info → agentic_blocks-0.1.20.dist-info}/METADATA +3 -1
- agentic_blocks-0.1.20.dist-info/RECORD +16 -0
- agentic_blocks-0.1.18.dist-info/RECORD +0 -11
- {agentic_blocks-0.1.18.dist-info → agentic_blocks-0.1.20.dist-info}/WHEEL +0 -0
- {agentic_blocks-0.1.18.dist-info → agentic_blocks-0.1.20.dist-info}/licenses/LICENSE +0 -0
- {agentic_blocks-0.1.18.dist-info → agentic_blocks-0.1.20.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,316 @@
|
|
1
|
+
"""
|
2
|
+
Decorator for tracing PocketFlow workflows with Langfuse.
|
3
|
+
"""
|
4
|
+
|
5
|
+
import functools
|
6
|
+
import inspect
|
7
|
+
import uuid
|
8
|
+
from typing import Any, Callable, Dict, Optional, Union
|
9
|
+
|
10
|
+
from .config import TracingConfig
|
11
|
+
from .core import LangfuseTracer
|
12
|
+
|
13
|
+
|
14
|
+
def trace_flow(
|
15
|
+
config: Optional[TracingConfig] = None,
|
16
|
+
flow_name: Optional[str] = None,
|
17
|
+
session_id: Optional[str] = None,
|
18
|
+
user_id: Optional[str] = None,
|
19
|
+
):
|
20
|
+
"""
|
21
|
+
Decorator to add Langfuse tracing to PocketFlow flows.
|
22
|
+
|
23
|
+
This decorator automatically traces:
|
24
|
+
- Flow execution start/end
|
25
|
+
- Each node's prep, exec, and post phases
|
26
|
+
- Input and output data for each phase
|
27
|
+
- Errors and exceptions
|
28
|
+
|
29
|
+
Args:
|
30
|
+
config: TracingConfig instance. If None, loads from environment.
|
31
|
+
flow_name: Custom name for the flow. If None, uses the flow class name.
|
32
|
+
session_id: Session ID for grouping related traces.
|
33
|
+
user_id: User ID for the trace.
|
34
|
+
|
35
|
+
Returns:
|
36
|
+
Decorated flow class or function.
|
37
|
+
|
38
|
+
Example:
|
39
|
+
```python
|
40
|
+
from tracing import trace_flow
|
41
|
+
|
42
|
+
@trace_flow()
|
43
|
+
class MyFlow(Flow):
|
44
|
+
def __init__(self):
|
45
|
+
super().__init__(start=MyNode())
|
46
|
+
|
47
|
+
# Or with custom configuration
|
48
|
+
config = TracingConfig.from_env()
|
49
|
+
|
50
|
+
@trace_flow(config=config, flow_name="CustomFlow")
|
51
|
+
class MyFlow(Flow):
|
52
|
+
pass
|
53
|
+
```
|
54
|
+
"""
|
55
|
+
|
56
|
+
def decorator(flow_class_or_func):
|
57
|
+
# Handle both class and function decoration
|
58
|
+
if inspect.isclass(flow_class_or_func):
|
59
|
+
return _trace_flow_class(
|
60
|
+
flow_class_or_func, config, flow_name, session_id, user_id
|
61
|
+
)
|
62
|
+
else:
|
63
|
+
return _trace_flow_function(
|
64
|
+
flow_class_or_func, config, flow_name, session_id, user_id
|
65
|
+
)
|
66
|
+
|
67
|
+
return decorator
|
68
|
+
|
69
|
+
|
70
|
+
def _trace_flow_class(flow_class, config, flow_name, session_id, user_id):
|
71
|
+
"""Trace a Flow class by wrapping its methods."""
|
72
|
+
|
73
|
+
# Get or create config
|
74
|
+
if config is None:
|
75
|
+
config = TracingConfig.from_env()
|
76
|
+
|
77
|
+
# Override session/user if provided
|
78
|
+
if session_id:
|
79
|
+
config.session_id = session_id
|
80
|
+
if user_id:
|
81
|
+
config.user_id = user_id
|
82
|
+
|
83
|
+
# Get flow name
|
84
|
+
if flow_name is None:
|
85
|
+
flow_name = flow_class.__name__
|
86
|
+
|
87
|
+
# Store original methods
|
88
|
+
original_init = flow_class.__init__
|
89
|
+
original_run = getattr(flow_class, "run", None)
|
90
|
+
original_run_async = getattr(flow_class, "run_async", None)
|
91
|
+
|
92
|
+
def traced_init(self, *args, **kwargs):
|
93
|
+
"""Initialize the flow with tracing capabilities."""
|
94
|
+
# Call original init
|
95
|
+
original_init(self, *args, **kwargs)
|
96
|
+
|
97
|
+
# Add tracing attributes
|
98
|
+
self._tracer = LangfuseTracer(config)
|
99
|
+
self._flow_name = flow_name
|
100
|
+
self._trace_id = None
|
101
|
+
|
102
|
+
# Patch all nodes in the flow
|
103
|
+
self._patch_nodes()
|
104
|
+
|
105
|
+
def traced_run(self, shared):
|
106
|
+
"""Traced version of the run method."""
|
107
|
+
if not hasattr(self, "_tracer"):
|
108
|
+
# Fallback if not properly initialized
|
109
|
+
return original_run(self, shared) if original_run else None
|
110
|
+
|
111
|
+
# Start trace
|
112
|
+
self._trace_id = self._tracer.start_trace(self._flow_name, shared)
|
113
|
+
|
114
|
+
try:
|
115
|
+
# Run the original flow
|
116
|
+
result = original_run(self, shared) if original_run else None
|
117
|
+
|
118
|
+
# End trace successfully
|
119
|
+
self._tracer.end_trace(shared, "success")
|
120
|
+
|
121
|
+
return result
|
122
|
+
|
123
|
+
except Exception as e:
|
124
|
+
# End trace with error
|
125
|
+
self._tracer.end_trace(shared, "error")
|
126
|
+
raise
|
127
|
+
finally:
|
128
|
+
# Ensure cleanup
|
129
|
+
self._tracer.flush()
|
130
|
+
|
131
|
+
async def traced_run_async(self, shared):
|
132
|
+
"""Traced version of the async run method."""
|
133
|
+
if not hasattr(self, "_tracer"):
|
134
|
+
# Fallback if not properly initialized
|
135
|
+
return (
|
136
|
+
await original_run_async(self, shared) if original_run_async else None
|
137
|
+
)
|
138
|
+
|
139
|
+
# Start trace
|
140
|
+
self._trace_id = self._tracer.start_trace(self._flow_name, shared)
|
141
|
+
|
142
|
+
try:
|
143
|
+
# Run the original flow
|
144
|
+
result = (
|
145
|
+
await original_run_async(self, shared) if original_run_async else None
|
146
|
+
)
|
147
|
+
|
148
|
+
# End trace successfully
|
149
|
+
self._tracer.end_trace(shared, "success")
|
150
|
+
|
151
|
+
return result
|
152
|
+
|
153
|
+
except Exception as e:
|
154
|
+
# End trace with error
|
155
|
+
self._tracer.end_trace(shared, "error")
|
156
|
+
raise
|
157
|
+
finally:
|
158
|
+
# Ensure cleanup
|
159
|
+
self._tracer.flush()
|
160
|
+
|
161
|
+
def patch_nodes(self):
|
162
|
+
"""Patch all nodes in the flow to add tracing."""
|
163
|
+
if not hasattr(self, "start_node") or not self.start_node:
|
164
|
+
return
|
165
|
+
|
166
|
+
visited = set()
|
167
|
+
nodes_to_patch = [self.start_node]
|
168
|
+
|
169
|
+
while nodes_to_patch:
|
170
|
+
node = nodes_to_patch.pop(0)
|
171
|
+
if id(node) in visited:
|
172
|
+
continue
|
173
|
+
|
174
|
+
visited.add(id(node))
|
175
|
+
|
176
|
+
# Patch this node
|
177
|
+
self._patch_node(node)
|
178
|
+
|
179
|
+
# Add successors to patch list
|
180
|
+
if hasattr(node, "successors"):
|
181
|
+
for successor in node.successors.values():
|
182
|
+
if successor and id(successor) not in visited:
|
183
|
+
nodes_to_patch.append(successor)
|
184
|
+
|
185
|
+
def patch_node(self, node):
|
186
|
+
"""Patch a single node to add tracing."""
|
187
|
+
if hasattr(node, "_pocketflow_traced"):
|
188
|
+
return # Already patched
|
189
|
+
|
190
|
+
node_id = str(uuid.uuid4())
|
191
|
+
node_name = type(node).__name__
|
192
|
+
|
193
|
+
# Store original methods
|
194
|
+
original_prep = getattr(node, "prep", None)
|
195
|
+
original_exec = getattr(node, "exec", None)
|
196
|
+
original_post = getattr(node, "post", None)
|
197
|
+
original_prep_async = getattr(node, "prep_async", None)
|
198
|
+
original_exec_async = getattr(node, "exec_async", None)
|
199
|
+
original_post_async = getattr(node, "post_async", None)
|
200
|
+
|
201
|
+
# Create traced versions
|
202
|
+
if original_prep:
|
203
|
+
node.prep = self._create_traced_method(
|
204
|
+
original_prep, node_id, node_name, "prep"
|
205
|
+
)
|
206
|
+
if original_exec:
|
207
|
+
node.exec = self._create_traced_method(
|
208
|
+
original_exec, node_id, node_name, "exec"
|
209
|
+
)
|
210
|
+
if original_post:
|
211
|
+
node.post = self._create_traced_method(
|
212
|
+
original_post, node_id, node_name, "post"
|
213
|
+
)
|
214
|
+
if original_prep_async:
|
215
|
+
node.prep_async = self._create_traced_async_method(
|
216
|
+
original_prep_async, node_id, node_name, "prep"
|
217
|
+
)
|
218
|
+
if original_exec_async:
|
219
|
+
node.exec_async = self._create_traced_async_method(
|
220
|
+
original_exec_async, node_id, node_name, "exec"
|
221
|
+
)
|
222
|
+
if original_post_async:
|
223
|
+
node.post_async = self._create_traced_async_method(
|
224
|
+
original_post_async, node_id, node_name, "post"
|
225
|
+
)
|
226
|
+
|
227
|
+
# Mark as traced
|
228
|
+
node._pocketflow_traced = True
|
229
|
+
|
230
|
+
def create_traced_method(self, original_method, node_id, node_name, phase):
|
231
|
+
"""Create a traced version of a synchronous method."""
|
232
|
+
|
233
|
+
@functools.wraps(original_method)
|
234
|
+
def traced_method(*args, **kwargs):
|
235
|
+
span_id = self._tracer.start_node_span(node_name, node_id, phase)
|
236
|
+
|
237
|
+
try:
|
238
|
+
result = original_method(*args, **kwargs)
|
239
|
+
self._tracer.end_node_span(span_id, input_data=args, output_data=result)
|
240
|
+
return result
|
241
|
+
except Exception as e:
|
242
|
+
self._tracer.end_node_span(span_id, input_data=args, error=e)
|
243
|
+
raise
|
244
|
+
|
245
|
+
return traced_method
|
246
|
+
|
247
|
+
def create_traced_async_method(self, original_method, node_id, node_name, phase):
|
248
|
+
"""Create a traced version of an asynchronous method."""
|
249
|
+
|
250
|
+
@functools.wraps(original_method)
|
251
|
+
async def traced_async_method(*args, **kwargs):
|
252
|
+
span_id = self._tracer.start_node_span(node_name, node_id, phase)
|
253
|
+
|
254
|
+
try:
|
255
|
+
result = await original_method(*args, **kwargs)
|
256
|
+
self._tracer.end_node_span(span_id, input_data=args, output_data=result)
|
257
|
+
return result
|
258
|
+
except Exception as e:
|
259
|
+
self._tracer.end_node_span(span_id, input_data=args, error=e)
|
260
|
+
raise
|
261
|
+
|
262
|
+
return traced_async_method
|
263
|
+
|
264
|
+
# Replace methods on the class
|
265
|
+
flow_class.__init__ = traced_init
|
266
|
+
flow_class._patch_nodes = patch_nodes
|
267
|
+
flow_class._patch_node = patch_node
|
268
|
+
flow_class._create_traced_method = create_traced_method
|
269
|
+
flow_class._create_traced_async_method = create_traced_async_method
|
270
|
+
|
271
|
+
if original_run:
|
272
|
+
flow_class.run = traced_run
|
273
|
+
if original_run_async:
|
274
|
+
flow_class.run_async = traced_run_async
|
275
|
+
|
276
|
+
return flow_class
|
277
|
+
|
278
|
+
|
279
|
+
def _trace_flow_function(flow_func, config, flow_name, session_id, user_id):
|
280
|
+
"""Trace a flow function (for functional-style flows)."""
|
281
|
+
|
282
|
+
# Get or create config
|
283
|
+
if config is None:
|
284
|
+
config = TracingConfig.from_env()
|
285
|
+
|
286
|
+
# Override session/user if provided
|
287
|
+
if session_id:
|
288
|
+
config.session_id = session_id
|
289
|
+
if user_id:
|
290
|
+
config.user_id = user_id
|
291
|
+
|
292
|
+
# Get flow name
|
293
|
+
if flow_name is None:
|
294
|
+
flow_name = flow_func.__name__
|
295
|
+
|
296
|
+
tracer = LangfuseTracer(config)
|
297
|
+
|
298
|
+
@functools.wraps(flow_func)
|
299
|
+
def traced_flow_func(*args, **kwargs):
|
300
|
+
# Assume first argument is shared data
|
301
|
+
shared = args[0] if args else {}
|
302
|
+
|
303
|
+
# Start trace
|
304
|
+
trace_id = tracer.start_trace(flow_name, shared)
|
305
|
+
|
306
|
+
try:
|
307
|
+
result = flow_func(*args, **kwargs)
|
308
|
+
tracer.end_trace(shared, "success")
|
309
|
+
return result
|
310
|
+
except Exception as e:
|
311
|
+
tracer.end_trace(shared, "error")
|
312
|
+
raise
|
313
|
+
finally:
|
314
|
+
tracer.flush()
|
315
|
+
|
316
|
+
return traced_flow_func
|