Skip to content

Commit

Permalink
Merge branch 'release/3.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Aug 15, 2024
2 parents a205941 + 2887a29 commit 974f610
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
To use the LangGraph for Swift library in a [SwiftPM] project, add the following line to the dependencies in your `Package.swift` file:

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

Expand Down Expand Up @@ -200,4 +200,4 @@ In the [LangChainDemo](LangChainDemo) project, you can find the porting of [Agen
[langgraph]: https://github.com/langchain-ai/langgraph
[AgentExecutor]: https://github.com/buhe/langchain-swift/blob/main/Sources/LangChain/agents/Agent.swift
[PlantUML]: https://plantuml.com
[Mermaid]: https://mermaid.js.org
[Mermaid]: https://mermaid.js.org
53 changes: 32 additions & 21 deletions Sources/LangGraph/LangGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public typealias Reducer<Value> = (Value?, Value) -> Value
- Returns: A default value.
*/
public typealias DefaultProvider<Value> = () -> Value
public typealias DefaultProvider<Value> = () throws -> Value

/**
A typealias representing a factory for creating agent states.
Expand All @@ -64,12 +64,13 @@ public protocol ChannelProtocol {
Updates the channel with a new value.
- Parameters:
- name: The name of attribute that will be updated.
- oldValue: The old value of the channel.
- newValue: The new value to update the channel with.
- Throws: An error if the update fails.
- Returns: The updated value.
*/
func update(oldValue: Any?, newValue: Any) throws -> Any
func updateAttribute(_ name: String, oldValue: Any?, newValue: Any) throws -> Any
}
/**
A class representing a communication channel that conforms to `ChannelProtocol`.
Expand Down Expand Up @@ -108,29 +109,38 @@ public class Channel<T> : ChannelProtocol {
mismatches and provides default values when necessary.
- Parameters:
- name: The name of attribute that will be updated.
- oldValue: The old value of the channel, which can be `nil`.
- newValue: The new value to update the channel with.
- Throws: An error if the update fails due to type mismatches.
- Returns: The updated value.
*/
public func update( oldValue: Any?, newValue: Any ) throws -> Any {
public func updateAttribute( _ name: String, oldValue: Any?, newValue: Any ) throws -> Any {
guard let new = newValue as? T else {
throw CompiledGraphError.executionError( "Channel update 'newValue' type mismatch!")
}

throw CompiledGraphError.executionError( "Channel: Type mismatch updating 'newValue' for property \(name)!")
}

// var old:T?
// if oldValue == nil {
// if let `default` {
// old = try `default`()
// }
// }
// else {
// guard let _old = oldValue as? T else {
// throw CompiledGraphError.executionError( "Channel update 'oldValue' type mismatch!")
// }
// old = _old
// }

var old:T?
if oldValue == nil {
if let `default` {
old = `default`()
}
}
else {
if( oldValue != nil ) {
guard let _old = oldValue as? T else {
throw CompiledGraphError.executionError( "Channel update 'oldValue' type mismatch!")
}
old = _old
}

if let reducer {
return reducer( old, new )
}
Expand Down Expand Up @@ -177,16 +187,17 @@ public class AppenderChannel<T> : Channel<[T]> {
If the new value is a single element, it is converted to an array before appending.
- Parameters:
- name: The name of attribute that will be updated.
- oldValue: The old value of the channel, which can be `nil`.
- newValue: The new value to update the channel with.
- Throws: An error if the update fails due to type mismatches.
- Returns: The updated value.
*/
public override func update(oldValue: Any?, newValue: Any) throws -> Any {
public override func updateAttribute( _ name: String, oldValue: Any?, newValue: Any) throws -> Any {
if let new = newValue as? T {
return try super.update(oldValue: oldValue, newValue: [new])
return try super.updateAttribute( name, oldValue: oldValue, newValue: [new])
}
return try super.update(oldValue: oldValue, newValue: newValue)
return try super.updateAttribute( name, oldValue: oldValue, newValue: newValue)
}
}

Expand Down Expand Up @@ -731,10 +742,10 @@ extension StateGraph {
- Returns: A dictionary representing the initial state data.
*/
private func initStateDataFromSchema() -> [String: Any] {
let mappedValues = schema.compactMap { key, channel in
private func initStateDataFromSchema() throws -> [String: Any] {
let mappedValues = try schema.compactMap { key, channel in
if let def = channel.`default` {
return (key, def())
return (key, try def())
}
return nil
}
Expand All @@ -755,7 +766,7 @@ extension StateGraph {
let mappedValues = try partialState.map { key, value in
if let channel = schema[key] {
do {
let newValue = try channel.update(oldValue: currentState.data[key], newValue: value)
let newValue = try channel.updateAttribute( key, oldValue: currentState.data[key], newValue: value)
return (key, newValue)
} catch CompiledGraphError.executionError(let message) {
throw CompiledGraphError.executionError("error processing property: '\(key)' - \(message)")
Expand Down Expand Up @@ -853,7 +864,7 @@ extension StateGraph {

Task {
do {
let initData = initStateDataFromSchema()
let initData = try initStateDataFromSchema()
var currentState = try mergeState(currentState: self.stateFactory(initData), partialState: inputs)
var currentNodeId = try await self.getEntryPoint(agentState: currentState)

Expand Down

0 comments on commit 974f610

Please sign in to comment.