-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add reducer, default value provider add AppenderChannel to manage accumulated list of values deprecate AppendableValue work on #13
- Loading branch information
1 parent
c88be6f
commit cd50013
Showing
7 changed files
with
193 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
* | ||
* @param <T> the type of the value | ||
*/ | ||
@Deprecated | ||
public interface AppendableValue<T> { | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
core-jdk8/src/main/java/org/bsc/langgraph4j/state/AppenderChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.bsc.langgraph4j.state; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
import static java.util.Optional.ofNullable; | ||
import static org.bsc.langgraph4j.utils.CollectionsUtils.listOf; | ||
|
||
|
||
/* | ||
* AppenderChannel is a {@link Channel} implementation that | ||
* is used to accumulate a list of values. | ||
* | ||
* @param <T> the type of the values being accumulated | ||
* @see Channel | ||
*/ | ||
@Slf4j | ||
public class AppenderChannel<T> implements Channel<List<T>> { | ||
|
||
private final Reducer<List<T>> reducer; | ||
private final Supplier<List<T>> defaultProvider; | ||
|
||
@Override | ||
public Optional<Reducer<List<T>>> getReducer() { | ||
return ofNullable(reducer); | ||
} | ||
|
||
@Override | ||
public Optional<Supplier<List<T>>> getDefault() { | ||
return ofNullable(defaultProvider); | ||
} | ||
|
||
public static <T> AppenderChannel<T> of( Supplier<List<T>> defaultProvider ) { | ||
return new AppenderChannel<>(defaultProvider); | ||
} | ||
|
||
private AppenderChannel( Supplier<List<T>> defaultProvider) { | ||
this.reducer = new Reducer<List<T>>() { | ||
@Override | ||
public List<T> apply(List<T> left, List<T> right) { | ||
if( left == null ) { | ||
return right; | ||
} | ||
left.addAll(right); | ||
return left; | ||
} | ||
}; | ||
this.defaultProvider = defaultProvider; | ||
} | ||
|
||
public Object update( String key, Object oldValue, Object newValue) { | ||
try { | ||
try { // this is to allow single value other than | ||
T typedValue = (T) newValue; | ||
return Channel.super.update(key, oldValue, listOf(typedValue)); | ||
} catch (ClassCastException e) { | ||
return Channel.super.update(key, oldValue, newValue); | ||
} | ||
} catch (UnsupportedOperationException ex) { | ||
log.error("Unsupported operation: probably because the appendable channel has been initialized with a immutable List. Check please !"); | ||
throw ex; | ||
} | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
core-jdk8/src/main/java/org/bsc/langgraph4j/state/Channel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.bsc.langgraph4j.state; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A Channel is a mechanism used to maintain a state property. | ||
* <p> | ||
* A Channel is associated with a key and a value. The Channel is updated | ||
* by calling the {@link #update(String, Object, Object)} method. The update | ||
* operation is applied to the channel's value. | ||
* <p> | ||
* The Channel may be initialized with a default value. This default value | ||
* is provided by a {@link Supplier}. The {@link #getDefault()} method returns | ||
* an optional containing the default supplier. | ||
* <p> | ||
* The Channel may also be associated with a Reducer. The Reducer is a | ||
* function that combines the current value of the channel with a new value | ||
* and returns the updated value. | ||
* <p> | ||
* The {@link #update(String, Object, Object)} method updates the channel's | ||
* value with the provided key, old value and new value. The update operation | ||
* is applied to the channel's value. If the channel is not initialized, the | ||
* default value is used. If the channel is initialized, the reducer is used | ||
* to compute the new value. | ||
* | ||
* @param <T> the type of the state property | ||
*/ | ||
public interface Channel<T> { | ||
|
||
/** | ||
* The Reducer, if provided, is invoked for each state property to compute value. | ||
* | ||
* @return An optional containing the reducer, if it exists. | ||
*/ | ||
Optional<Reducer<T>> getReducer() ; | ||
|
||
/** | ||
* a Supplier that provide a default value. The result must be mutable. | ||
* | ||
* @return an Optional containing the default Supplier | ||
*/ | ||
Optional<Supplier<T>> getDefault(); | ||
|
||
|
||
/** | ||
* Update the state property with the given key and returns the new value. | ||
* | ||
* @param key the key of the state property to be updated | ||
* @param oldValue the current value of the state property | ||
* @param newValue the new value to be set | ||
* @return the new value of the state property | ||
*/ | ||
default Object update(String key, Object oldValue, Object newValue) { | ||
T _new = (T)newValue; | ||
|
||
final T _old = (oldValue == null) ? | ||
getDefault().map(Supplier::get).orElse(null) : | ||
(T)oldValue; | ||
|
||
return getReducer().map( reducer -> reducer.apply( _old, _new)).orElse(_new); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
core-jdk8/src/main/java/org/bsc/langgraph4j/state/Reducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.bsc.langgraph4j.state; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
public interface Reducer<T> extends BiFunction<T,T,T> { | ||
} |