Skip to content

Commit

Permalink
feat(Checkpoint): store the nextNodeId
Browse files Browse the repository at this point in the history
work on #14
  • Loading branch information
bsorrentino committed Aug 26, 2024
1 parent 05c293f commit 6e1ca60
Showing 1 changed file with 53 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package org.bsc.langgraph4j.checkpoint;

import lombok.Data;
import lombok.*;
import org.bsc.langgraph4j.state.AgentState;
import org.bsc.langgraph4j.state.Channel;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.*;


/**
* Represents a checkpoint of an agent state.
*
Expand All @@ -21,32 +18,65 @@
* @see AgentState
* @see Externalizable
*/
@Getter
@ToString
public class Checkpoint {

@lombok.Value(staticConstructor="of")
public static class Value {
AgentState state;
String nodeId;
}
private String id = UUID.randomUUID().toString();
private AgentState state = null;
private String nodeId = null ;
private String nextNodeId = null;

String id;
Value value;
private Checkpoint() {
}

public final String getId() {
return id;
private Checkpoint( Checkpoint checkpoint ) {
this.id = checkpoint.id;
this.state = checkpoint.state;
this.nodeId = checkpoint.nodeId;
this.nextNodeId = checkpoint.nextNodeId;
}
public final Value getValue() {
return value;

public static Builder builder() {
return new Builder();
}

public Checkpoint( Value value ) {
this(UUID.randomUUID().toString(), value );
public static class Builder {
private final Checkpoint result = new Checkpoint();;

public Builder id( String id ) {
result.id = id;
return this;
}
public Builder state( AgentState state ) {
result.state = state;
return this;
}
public Builder nodeId( String nodeId ) {
result.nodeId = nodeId;
return this;
}
public Builder nextNodeId( String nextNodeId ) {
result.nextNodeId = nextNodeId;
return this;
}

public Checkpoint build() {
Objects.requireNonNull( result.id, "Checkpoint.id cannot be null" );
Objects.requireNonNull( result.state, "Checkpoint.state cannot be null" );
Objects.requireNonNull( result.nodeId, "Checkpoint.nodeId cannot be null" );
Objects.requireNonNull( result.nextNodeId, "Checkpoint.nextNodeId cannot be null" );

return result;

}
}
public Checkpoint(String id, Value value) {
Objects.requireNonNull(id, "id cannot be null");
Objects.requireNonNull(value, "value cannot be null");
this.id = id;
this.value = value;
public Checkpoint updateState(Map<String,Object> values, Map<String, Channel<?>> channels ) {

Checkpoint result = new Checkpoint( this );
Map<String,Object> data = AgentState.updateState( state, values, channels );
result.state = new AgentState(data);
return result;
}

}

0 comments on commit 6e1ca60

Please sign in to comment.