-
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.
refactor(core-jdk8): Agent State Management
- AgentState from interface to concrete class - AppendableValue a readonly interface - Create internal AppendableValueRW to update state
- Loading branch information
1 parent
0d7d09f
commit 7e19f1e
Showing
7 changed files
with
126 additions
and
76 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
65 changes: 57 additions & 8 deletions
65
core-jdk8/src/main/java/org/bsc/langgraph4j/state/AgentState.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 |
---|---|---|
@@ -1,21 +1,70 @@ | ||
package org.bsc.langgraph4j.state; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import lombok.var; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.Collections.unmodifiableMap; | ||
import static java.util.Optional.ofNullable; | ||
|
||
public interface AgentState { | ||
public class AgentState { | ||
|
||
private final java.util.Map<String,Object> data; | ||
|
||
java.util.Map<String,Object> data(); | ||
public AgentState( Map<String,Object> initData ) { | ||
this.data = new HashMap<>(initData); | ||
} | ||
public final java.util.Map<String,Object> data() { | ||
return unmodifiableMap(data); | ||
} | ||
|
||
default <T> Optional<T> value(String key) { | ||
public final <T> Optional<T> value(String key) { | ||
return ofNullable((T) data().get(key)); | ||
}; | ||
|
||
default <T> Optional<List<T>> appendableValue(String key ) { | ||
return ofNullable( ((AppendableValue<T>)data().get(key))) | ||
.map(AppendableValue::values); | ||
public final <T> AppendableValue<T> appendableValue(String key ) { | ||
Object value = this.data.get(key); | ||
|
||
if( value instanceof AppendableValue ) { | ||
return (AppendableValue<T>) value; | ||
} | ||
if( value instanceof Collection) { | ||
return new AppendableValueRW<>((Collection<T>)value); | ||
} | ||
AppendableValueRW<T> rw = new AppendableValueRW<>(); | ||
if ( value != null ) { | ||
rw.append(value); | ||
} | ||
this.data.put(key, rw); | ||
return rw; | ||
|
||
} | ||
|
||
private Object mergeFunction(Object currentValue, Object newValue) { | ||
if (currentValue instanceof AppendableValueRW<?>) { | ||
((AppendableValueRW<?>) currentValue).append( newValue ); | ||
return currentValue; | ||
} | ||
return newValue; | ||
} | ||
public <State extends AgentState> State mergeWith(Map<String,Object> partialState, AgentStateFactory<State> factory) { | ||
|
||
if( partialState == null || partialState.isEmpty() ) { | ||
return factory.apply(data()); | ||
} | ||
var mergedMap = Stream.concat(data().entrySet().stream(), partialState.entrySet().stream()) | ||
.collect(Collectors.toMap( | ||
Map.Entry::getKey, | ||
Map.Entry::getValue, | ||
this::mergeFunction)); | ||
|
||
return factory.apply(mergedMap); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return data.toString(); | ||
} | ||
} |
34 changes: 8 additions & 26 deletions
34
core-jdk8/src/main/java/org/bsc/langgraph4j/state/AppendableValue.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 |
---|---|---|
@@ -1,34 +1,16 @@ | ||
package org.bsc.langgraph4j.state; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.*; | ||
|
||
import static java.util.Collections.unmodifiableList; | ||
|
||
public class AppendableValue<T> { | ||
private final List<T> values; | ||
public AppendableValue( List<T> values) { | ||
this.values = new ArrayList<>(values); | ||
} | ||
public AppendableValue() { | ||
this(Collections.emptyList()); | ||
} | ||
public interface AppendableValue<T> { | ||
|
||
public List<T> values() { | ||
return unmodifiableList(values); | ||
} | ||
public void append(Object value) { | ||
if (value instanceof Collection ) { | ||
this.values.addAll((Collection<? extends T>) value); | ||
} | ||
else { | ||
this.values.add((T)value); | ||
} | ||
} | ||
List<T> values(); | ||
|
||
public String toString() { | ||
return String.valueOf(values); | ||
} | ||
boolean isEmpty() ; | ||
int size() ; | ||
|
||
Optional<T> last() ; | ||
Optional<T> lastMinus( int n ) ; | ||
} |
50 changes: 50 additions & 0 deletions
50
core-jdk8/src/main/java/org/bsc/langgraph4j/state/AppendableValueRW.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,50 @@ | ||
package org.bsc.langgraph4j.state; | ||
|
||
import java.util.*; | ||
|
||
import static java.util.Collections.unmodifiableList; | ||
|
||
public class AppendableValueRW<T> implements AppendableValue<T> { | ||
private final List<T> values; | ||
|
||
public AppendableValueRW( Collection<T> values) { | ||
this.values = new ArrayList<>(values); | ||
} | ||
public AppendableValueRW() { | ||
this(Collections.emptyList()); | ||
} | ||
public void append(Object value) { | ||
if (value instanceof Collection) { | ||
this.values.addAll((Collection<? extends T>) value); | ||
} | ||
else { | ||
this.values.add((T)value); | ||
} | ||
} | ||
|
||
public List<T> values() { | ||
return unmodifiableList(values); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return values().isEmpty(); | ||
} | ||
public int size() { | ||
return values().size(); | ||
} | ||
public Optional<T> last() { | ||
List<T> values = values(); | ||
return ( values == null || values.isEmpty() ) ? Optional.empty() : Optional.of(values.get(values.size()-1)); | ||
} | ||
public Optional<T> lastMinus( int n ) { | ||
if( values == null || values.isEmpty() ) return Optional.empty(); | ||
if( n < 0 ) return Optional.empty(); | ||
if( values.size() - n - 1 < 0 ) return Optional.empty(); | ||
return Optional.of(values.get(values.size()-n-1)); | ||
} | ||
|
||
public String toString() { | ||
return String.valueOf(values); | ||
} | ||
|
||
} |
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
15 changes: 0 additions & 15 deletions
15
core-jdk8/src/test/java/org/bsc/langgraph4j/BaseAgentState.java
This file was deleted.
Oops, something went wrong.
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