feat: Add flow_id, self.name for instance names of Flows and Nodes and easy isolated storage for nested flows#5
Draft
johnr14 wants to merge 1 commit intoThe-Pocket:mainfrom
Draft
Conversation
Author
|
For performance and tracing flow history, I think a decorator could do.
Also I didn't get to the point where I would need to manage concurrent connections, requests per minutes/flow/batch, and token per node/flow... But it would be nice to have it planned at this stage. Like infinite loop prevention other than logging to But am I getting to far from a minimal implementation of PocketFlow ? Some example decorator from AI: def trace_node(debug=False):
def decorator(func):
if not debug:
# Return original function if debug is disabled
return func
def wrapper(node, *args, **kwargs):
# Initialize trace if not exists
if not hasattr(node, '_trace'):
node._trace = []
# Create trace entry
trace_entry = {
'timestamp': time.time(),
'node': node.name,
'action': func.__name__,
'args': args,
'kwargs': kwargs,
'status': 'started'
}
# Add initial entry
node._trace.append(trace_entry)
try:
# Execute the original method
result = func(node, *args, **kwargs)
# Update entry with success status
trace_entry.update({
'status': 'success',
'duration': time.time() - trace_entry['timestamp'],
'result': result
})
return result
except Exception as e:
# Update entry with error status
trace_entry.update({
'status': 'error',
'duration': time.time() - trace_entry['timestamp'],
'exception': str(e),
'traceback': traceback.format_exc()
})
raise
finally:
# Finalize entry
trace_entry['end_time'] = time.time()
return wrapper
return decorator
...
DEBUG = True # Set this globally or via configuration
class Node(BaseNode):
@trace_node(debug=DEBUG)
def prep(self, shared):
... # Access tracing information
for node in flow.get_all_nodes():
print(f"Node: {node.name}")
for entry in node._trace:
print(f" {entry['action']}: {entry['status']} in {entry['duration']:.2f}s") |
|
I support this PR to add |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WIP to get instance names and flow names.
For discussion as in Issue #4
THIS IS JUST A DRAFT, NOT TESTED EXTENSIVELY.
Now when inside a
prep(),exec()orpost(), it's possible to print the instance name withWill now print :
Todo : get feedback and more ideas + review ?
Questions: This does add complexity and more lines of code... Is it all useful ?
So I think that :
pocketflowandpocketflowextrato separate basic and advanced framework that are still somewhat compatible but with missing features ? Users could just cherry pick some parts fromextrato add features quickly.pocketflowdevfor development and experimental features that may break compatibility ?Try this code
I added
rework.pyas example, look atclass GetOpinion(Node):for actual use.EDIT: Now with updated
build_mermaidcode !graph LR subgraph rework2_Flow["rework2_Flow (Flow)"] load2_Node["load2_Node (LoadFile)"] opinion2_Node["opinion2_Node (GetOpinion)"] load2_Node --> opinion2_Node rework2_Node["rework2_Node (ReworkFile)"] opinion2_Node --> rework2_Node valid2_Node["valid2_Node (GetValidation)"] rework2_Node --> valid2_Node valid2_Node --> opinion2_Node save2_Node["save2_Node (SaveFile)"] valid2_Node --> save2_Node rework2_Node --> opinion2_Node endPR comment generated with AI assistance :
This change introduces several improvements to BaseNode and Flow classes:
These changes enable:
The improvements include:
This is particularly useful for: