@@ -37,7 +37,7 @@ public typealias Reducer<Value> = (Value?, Value) -> Value
37
37
38
38
- Returns: A default value.
39
39
*/
40
- public typealias DefaultProvider < Value> = ( ) -> Value
40
+ public typealias DefaultProvider < Value> = ( ) throws -> Value
41
41
42
42
/**
43
43
A typealias representing a factory for creating agent states.
@@ -64,12 +64,13 @@ public protocol ChannelProtocol {
64
64
Updates the channel with a new value.
65
65
66
66
- Parameters:
67
+ - name: The name of attribute that will be updated.
67
68
- oldValue: The old value of the channel.
68
69
- newValue: The new value to update the channel with.
69
70
- Throws: An error if the update fails.
70
71
- Returns: The updated value.
71
72
*/
72
- func update ( oldValue: Any ? , newValue: Any ) throws -> Any
73
+ func updateAttribute ( _ name : String , oldValue: Any ? , newValue: Any ) throws -> Any
73
74
}
74
75
/**
75
76
A class representing a communication channel that conforms to `ChannelProtocol`.
@@ -108,29 +109,38 @@ public class Channel<T> : ChannelProtocol {
108
109
mismatches and provides default values when necessary.
109
110
110
111
- Parameters:
112
+ - name: The name of attribute that will be updated.
111
113
- oldValue: The old value of the channel, which can be `nil`.
112
114
- newValue: The new value to update the channel with.
113
115
- Throws: An error if the update fails due to type mismatches.
114
116
- Returns: The updated value.
115
117
*/
116
- public func update ( oldValue: Any ? , newValue: Any ) throws -> Any {
118
+ public func updateAttribute ( _ name : String , oldValue: Any ? , newValue: Any ) throws -> Any {
117
119
guard let new = newValue as? T else {
118
- throw CompiledGraphError . executionError ( " Channel update 'newValue' type mismatch! " )
119
- }
120
-
120
+ throw CompiledGraphError . executionError ( " Channel: Type mismatch updating 'newValue' for property \( name) ! " )
121
+ }
122
+
123
+ // var old:T?
124
+ // if oldValue == nil {
125
+ // if let `default` {
126
+ // old = try `default`()
127
+ // }
128
+ // }
129
+ // else {
130
+ // guard let _old = oldValue as? T else {
131
+ // throw CompiledGraphError.executionError( "Channel update 'oldValue' type mismatch!")
132
+ // }
133
+ // old = _old
134
+ // }
135
+
121
136
var old : T ?
122
- if oldValue == nil {
123
- if let `default` {
124
- old = `default` ( )
125
- }
126
- }
127
- else {
137
+ if ( oldValue != nil ) {
128
138
guard let _old = oldValue as? T else {
129
139
throw CompiledGraphError . executionError ( " Channel update 'oldValue' type mismatch! " )
130
140
}
131
141
old = _old
132
142
}
133
-
143
+
134
144
if let reducer {
135
145
return reducer ( old, new )
136
146
}
@@ -177,16 +187,17 @@ public class AppenderChannel<T> : Channel<[T]> {
177
187
If the new value is a single element, it is converted to an array before appending.
178
188
179
189
- Parameters:
190
+ - name: The name of attribute that will be updated.
180
191
- oldValue: The old value of the channel, which can be `nil`.
181
192
- newValue: The new value to update the channel with.
182
193
- Throws: An error if the update fails due to type mismatches.
183
194
- Returns: The updated value.
184
195
*/
185
- public override func update ( oldValue: Any ? , newValue: Any ) throws -> Any {
196
+ public override func updateAttribute ( _ name : String , oldValue: Any ? , newValue: Any ) throws -> Any {
186
197
if let new = newValue as? T {
187
- return try super. update ( oldValue: oldValue, newValue: [ new] )
198
+ return try super. updateAttribute ( name , oldValue: oldValue, newValue: [ new] )
188
199
}
189
- return try super. update ( oldValue: oldValue, newValue: newValue)
200
+ return try super. updateAttribute ( name , oldValue: oldValue, newValue: newValue)
190
201
}
191
202
}
192
203
@@ -731,10 +742,10 @@ extension StateGraph {
731
742
732
743
- Returns: A dictionary representing the initial state data.
733
744
*/
734
- private func initStateDataFromSchema( ) -> [ String : Any ] {
735
- let mappedValues = schema. compactMap { key, channel in
745
+ private func initStateDataFromSchema( ) throws -> [ String : Any ] {
746
+ let mappedValues = try schema. compactMap { key, channel in
736
747
if let def = channel. `default` {
737
- return ( key, def ( ) )
748
+ return ( key, try def ( ) )
738
749
}
739
750
return nil
740
751
}
@@ -755,7 +766,7 @@ extension StateGraph {
755
766
let mappedValues = try partialState. map { key, value in
756
767
if let channel = schema [ key] {
757
768
do {
758
- let newValue = try channel. update ( oldValue: currentState. data [ key] , newValue: value)
769
+ let newValue = try channel. updateAttribute ( key , oldValue: currentState. data [ key] , newValue: value)
759
770
return ( key, newValue)
760
771
} catch CompiledGraphError . executionError( let message) {
761
772
throw CompiledGraphError . executionError ( " error processing property: ' \( key) ' - \( message) " )
@@ -853,7 +864,7 @@ extension StateGraph {
853
864
854
865
Task {
855
866
do {
856
- let initData = initStateDataFromSchema ( )
867
+ let initData = try initStateDataFromSchema ( )
857
868
var currentState = try mergeState ( currentState: self . stateFactory ( initData) , partialState: inputs)
858
869
var currentNodeId = try await self . getEntryPoint ( agentState: currentState)
859
870
0 commit comments