Skip to content

Commit 974f610

Browse files
committed
Merge branch 'release/3.1.0'
2 parents a205941 + 2887a29 commit 974f610

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
To use the LangGraph for Swift library in a [SwiftPM] project, add the following line to the dependencies in your `Package.swift` file:
3939

4040
```Swift
41-
.package(url: "https://github.com/bsorrentino/LangGraph-Swift.git", from: "3.0.1"),
41+
.package(url: "https://github.com/bsorrentino/LangGraph-Swift.git", from: "<last version>"),
4242
```
4343
Include `LangGraph` as a dependency for your executable target:
4444

@@ -200,4 +200,4 @@ In the [LangChainDemo](LangChainDemo) project, you can find the porting of [Agen
200200
[langgraph]: https://github.com/langchain-ai/langgraph
201201
[AgentExecutor]: https://github.com/buhe/langchain-swift/blob/main/Sources/LangChain/agents/Agent.swift
202202
[PlantUML]: https://plantuml.com
203-
[Mermaid]: https://mermaid.js.org
203+
[Mermaid]: https://mermaid.js.org

Sources/LangGraph/LangGraph.swift

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public typealias Reducer<Value> = (Value?, Value) -> Value
3737

3838
- Returns: A default value.
3939
*/
40-
public typealias DefaultProvider<Value> = () -> Value
40+
public typealias DefaultProvider<Value> = () throws -> Value
4141

4242
/**
4343
A typealias representing a factory for creating agent states.
@@ -64,12 +64,13 @@ public protocol ChannelProtocol {
6464
Updates the channel with a new value.
6565

6666
- Parameters:
67+
- name: The name of attribute that will be updated.
6768
- oldValue: The old value of the channel.
6869
- newValue: The new value to update the channel with.
6970
- Throws: An error if the update fails.
7071
- Returns: The updated value.
7172
*/
72-
func update(oldValue: Any?, newValue: Any) throws -> Any
73+
func updateAttribute(_ name: String, oldValue: Any?, newValue: Any) throws -> Any
7374
}
7475
/**
7576
A class representing a communication channel that conforms to `ChannelProtocol`.
@@ -108,29 +109,38 @@ public class Channel<T> : ChannelProtocol {
108109
mismatches and provides default values when necessary.
109110

110111
- Parameters:
112+
- name: The name of attribute that will be updated.
111113
- oldValue: The old value of the channel, which can be `nil`.
112114
- newValue: The new value to update the channel with.
113115
- Throws: An error if the update fails due to type mismatches.
114116
- Returns: The updated value.
115117
*/
116-
public func update( oldValue: Any?, newValue: Any ) throws -> Any {
118+
public func updateAttribute( _ name: String, oldValue: Any?, newValue: Any ) throws -> Any {
117119
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+
121136
var old:T?
122-
if oldValue == nil {
123-
if let `default` {
124-
old = `default`()
125-
}
126-
}
127-
else {
137+
if( oldValue != nil ) {
128138
guard let _old = oldValue as? T else {
129139
throw CompiledGraphError.executionError( "Channel update 'oldValue' type mismatch!")
130140
}
131141
old = _old
132142
}
133-
143+
134144
if let reducer {
135145
return reducer( old, new )
136146
}
@@ -177,16 +187,17 @@ public class AppenderChannel<T> : Channel<[T]> {
177187
If the new value is a single element, it is converted to an array before appending.
178188

179189
- Parameters:
190+
- name: The name of attribute that will be updated.
180191
- oldValue: The old value of the channel, which can be `nil`.
181192
- newValue: The new value to update the channel with.
182193
- Throws: An error if the update fails due to type mismatches.
183194
- Returns: The updated value.
184195
*/
185-
public override func update(oldValue: Any?, newValue: Any) throws -> Any {
196+
public override func updateAttribute( _ name: String, oldValue: Any?, newValue: Any) throws -> Any {
186197
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])
188199
}
189-
return try super.update(oldValue: oldValue, newValue: newValue)
200+
return try super.updateAttribute( name, oldValue: oldValue, newValue: newValue)
190201
}
191202
}
192203

@@ -731,10 +742,10 @@ extension StateGraph {
731742

732743
- Returns: A dictionary representing the initial state data.
733744
*/
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
736747
if let def = channel.`default` {
737-
return (key, def())
748+
return (key, try def())
738749
}
739750
return nil
740751
}
@@ -755,7 +766,7 @@ extension StateGraph {
755766
let mappedValues = try partialState.map { key, value in
756767
if let channel = schema[key] {
757768
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)
759770
return (key, newValue)
760771
} catch CompiledGraphError.executionError(let message) {
761772
throw CompiledGraphError.executionError("error processing property: '\(key)' - \(message)")
@@ -853,7 +864,7 @@ extension StateGraph {
853864

854865
Task {
855866
do {
856-
let initData = initStateDataFromSchema()
867+
let initData = try initStateDataFromSchema()
857868
var currentState = try mergeState(currentState: self.stateFactory(initData), partialState: inputs)
858869
var currentNodeId = try await self.getEntryPoint(agentState: currentState)
859870

0 commit comments

Comments
 (0)