1
1
from ....src .realtimenodes .control_base import ControlNodeBase
2
+ import copy
3
+ from ....src .utils .general_utils import AlwaysEqualProxy
2
4
3
5
class StateResetNode (ControlNodeBase ):
4
6
"""Node that resets all control node states when triggered"""
@@ -21,12 +23,8 @@ def INPUT_TYPES(cls):
21
23
CATEGORY = "Realtime Nodes/control/utility"
22
24
23
25
def update (self , trigger , always_execute = True ):
24
- print (f"\n === StateResetNode UPDATE - node_id: { self .node_id } ===" )
25
- print (f"States before potential reset: { self .state_manager ._states } " )
26
26
if trigger :
27
- print ("RESETTING ALL STATES" )
28
27
self .state_manager .clear_all_states ()
29
- print (f"States after reset: { self .state_manager ._states } " )
30
28
return (True ,)
31
29
return (False ,)
32
30
@@ -52,19 +50,87 @@ def INPUT_TYPES(cls):
52
50
CATEGORY = "Realtime Nodes/control/utility"
53
51
54
52
def update (self , increment , always_execute = True ):
55
- print (f"\n === StateTestNode UPDATE - node_id: { self .node_id } ===" )
56
- print (f"All states before get: { self .state_manager ._states } " )
57
-
58
53
state = self .get_state ({
59
54
"counter" : 0
60
55
})
61
- print (f"Retrieved state: { state } " )
62
56
63
57
state ["counter" ] += increment
64
- print (f"Updated state before save: { state } " )
65
-
58
+
66
59
self .set_state (state )
67
- print (f"All states after save: { self .state_manager ._states } " )
68
60
69
61
return (state ["counter" ],)
70
62
63
+
64
+
65
+ class GetStateNode (ControlNodeBase ):
66
+ """
67
+ Node that retrieves a value from the global state using a user-specified key.
68
+ """
69
+ CATEGORY = "utils"
70
+ RETURN_TYPES = (AlwaysEqualProxy ("*" ),)
71
+ RETURN_NAMES = ("value" ,)
72
+ FUNCTION = "update"
73
+ DESCRIPTION = "Retrieve a value from the global state using the given key. If the key is not found, return the default value."
74
+ @classmethod
75
+ def INPUT_TYPES (cls ):
76
+ inputs = super ().INPUT_TYPES ()
77
+ inputs ["required" ].update ({
78
+ "key" : ("STRING" , {"default" : "default_key" , "tooltip" : "The key to retrieve the value from. If not provided, the default value will be returned." }),
79
+ "default_value" : (AlwaysEqualProxy ("*" ), {"tooltip" : "The value to return if the key is not found." }),
80
+ "use_default" : ("BOOLEAN" , {"default" : False , "tooltip" : "If True, the default value will be returned if the key is not found." }),
81
+ })
82
+ return inputs
83
+
84
+ def update (self , key : str , default_value , use_default : bool , always_execute = True ):
85
+ """
86
+ Retrieve a value from the global state using the given key.
87
+ """
88
+ if not key or use_default :
89
+ return (default_value ,)
90
+
91
+ # Get the shared state dictionary
92
+ shared_state = self .state_manager .get_state ("__shared_keys__" , {})
93
+
94
+ # Check if the key exists
95
+ if key in shared_state :
96
+ return (shared_state [key ],)
97
+
98
+ # Return default value if key not found
99
+ return (default_value ,)
100
+
101
+ class SetStateNode (ControlNodeBase ):
102
+ """
103
+ Node that stores a value in the global state with a user-specified key.
104
+ The value will be accessible in future workflow runs through GetStateNode.
105
+ """
106
+ CATEGORY = "utils"
107
+ RETURN_TYPES = (AlwaysEqualProxy ("*" ),)
108
+ RETURN_NAMES = ("value" ,)
109
+ FUNCTION = "update"
110
+ OUTPUT_NODE = True
111
+ DESCRIPTION = "Store a value in the global state with the given key. The value will be accessible in future workflow runs through GetStateNode."
112
+ @classmethod
113
+ def INPUT_TYPES (cls ):
114
+ inputs = super ().INPUT_TYPES ()
115
+ inputs ["required" ].update ({
116
+ "key" : ("STRING" , {"default" : "default_key" , "tooltip" : "The key to store the value under. If not provided, the value will not be stored." }),
117
+ "value" : (AlwaysEqualProxy ("*" ), {"tooltip" : "The value to store in the global state." }),
118
+ })
119
+ return inputs
120
+
121
+ def update (self , key : str , value , always_execute = True ):
122
+ """
123
+ Store a value in the global state with the given key.
124
+ """
125
+ if not key :
126
+ return (value ,)
127
+
128
+ try :
129
+ shared_state = self .state_manager .get_state ("__shared_keys__" , {})
130
+ shared_state [key ] = copy .deepcopy (value )
131
+ self .state_manager .set_state ("__shared_keys__" , shared_state )
132
+ except Exception as e :
133
+ print (f"[State Node] Error storing value: { str (e )} " )
134
+
135
+ return (value ,)
136
+
0 commit comments