|
1 | 1 | package org.bsc.langgraph4j.checkpoint;
|
2 | 2 |
|
| 3 | +import org.bsc.langgraph4j.RunnableConfig; |
3 | 4 | import org.bsc.langgraph4j.serializer.CheckpointSerializer;
|
4 | 5 |
|
5 | 6 | import java.util.*;
|
| 7 | +import java.util.stream.IntStream; |
6 | 8 |
|
| 9 | +import static java.lang.String.format; |
7 | 10 | import static java.util.Collections.unmodifiableCollection;
|
8 |
| -import static java.util.Collections.unmodifiableSet; |
| 11 | +import static java.util.Optional.ofNullable; |
9 | 12 |
|
10 | 13 | public class MemorySaver implements BaseCheckpointSaver {
|
| 14 | + private final Map<String, LinkedList<Checkpoint>> _checkpointsByThread = new HashMap<>(); |
| 15 | + private final LinkedList<Checkpoint> _defaultCheckpoints = new LinkedList<>(); |
11 | 16 |
|
12 |
| - private final Stack<Checkpoint> checkpoints = new Stack<>(); |
| 17 | + public MemorySaver() { |
| 18 | + } |
13 | 19 |
|
| 20 | + private LinkedList<Checkpoint> getCheckpoints( RunnableConfig config ) { |
| 21 | + return config.threadId() |
| 22 | + .map( threadId -> _checkpointsByThread.computeIfAbsent(threadId, k -> new LinkedList<>()) ) |
| 23 | + .orElse( _defaultCheckpoints ); |
| 24 | + } |
14 | 25 |
|
15 | 26 | @Override
|
16 |
| - public Collection<Checkpoint> list() { |
| 27 | + public Collection<Checkpoint> list( RunnableConfig config ) { |
| 28 | + final LinkedList<Checkpoint> checkpoints = getCheckpoints(config); |
17 | 29 | return unmodifiableCollection(checkpoints); // immutable checkpoints;
|
18 | 30 | }
|
19 | 31 |
|
20 |
| - @Override |
21 |
| - public Optional<Checkpoint> getLast() { |
22 |
| - if( checkpoints.isEmpty() ) { |
23 |
| - return Optional.empty(); |
24 |
| - } |
25 |
| - return Optional.ofNullable( checkpoints.peek() ); |
| 32 | + private Optional<Checkpoint> getLast( RunnableConfig config ) { |
| 33 | + final LinkedList<Checkpoint> checkpoints = getCheckpoints(config); |
| 34 | + return (checkpoints.isEmpty() ) ? Optional.empty() : ofNullable(checkpoints.peek()); |
26 | 35 | }
|
27 | 36 |
|
28 |
| - public Optional<Checkpoint> get(String id) { |
29 |
| - return checkpoints.stream() |
30 |
| - .filter( checkpoint -> checkpoint.getId().equals(id) ) |
31 |
| - .findFirst(); |
| 37 | + @Override |
| 38 | + public Optional<Checkpoint> get(RunnableConfig config) { |
| 39 | + final LinkedList<Checkpoint> checkpoints = getCheckpoints(config); |
| 40 | + if( config.checkPointId().isPresent() ) { |
| 41 | + return config.checkPointId() |
| 42 | + .flatMap( id -> checkpoints.stream() |
| 43 | + .filter( checkpoint -> checkpoint.getId().equals(id) ) |
| 44 | + .findFirst()); |
| 45 | + } |
| 46 | + return getLast(config); |
32 | 47 | }
|
33 | 48 |
|
34 | 49 | @Override
|
35 |
| - public void put(Checkpoint checkpoint) throws Exception { |
36 |
| - checkpoints.add( CheckpointSerializer.INSTANCE.cloneObject(checkpoint) ); |
| 50 | + public RunnableConfig put(RunnableConfig config, Checkpoint checkpoint) throws Exception { |
| 51 | + final LinkedList<Checkpoint> checkpoints = getCheckpoints(config); |
| 52 | + |
| 53 | + final Checkpoint clonedCheckpoint = CheckpointSerializer.INSTANCE.cloneObject(checkpoint); |
| 54 | + |
| 55 | + if( config.checkPointId().isPresent() ) { // Replace Checkpoint |
| 56 | + String checkPointId = config.checkPointId().get(); |
| 57 | + int index = IntStream.range(0, checkpoints.size()) |
| 58 | + .filter(i -> checkpoints.get(i).getId().equals(checkPointId)) |
| 59 | + .findFirst() |
| 60 | + .orElseThrow( () -> (new NoSuchElementException( format("Checkpoint with id %s not found!", checkPointId))) ); |
| 61 | + checkpoints.set( index, clonedCheckpoint); |
| 62 | + return config; |
| 63 | + } |
| 64 | + |
| 65 | + checkpoints.push( clonedCheckpoint ); // Add Checkpoint |
| 66 | + |
| 67 | + return RunnableConfig.builder(config) |
| 68 | + .checkPointId( clonedCheckpoint.getId() ) |
| 69 | + .build(); |
37 | 70 | }
|
38 | 71 |
|
39 | 72 | }
|
0 commit comments