Skip to content

Commit

Permalink
refactor: add throws to DefaultProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Aug 15, 2024
1 parent e52066d commit 660bd63
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 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 Down Expand Up @@ -118,19 +118,27 @@ public class Channel<T> : ChannelProtocol {
throw CompiledGraphError.executionError( "Channel update 'newValue' type mismatch!")
}

// 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 @@ -731,10 +739,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 Down Expand Up @@ -853,7 +861,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 660bd63

Please sign in to comment.