-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40a910f
commit 1407b41
Showing
4 changed files
with
17 additions
and
69 deletions.
There are no files selected for viewing
56 changes: 2 additions & 54 deletions
56
core-jdk8/src/main/java/org/bsc/langgraph4j/serializer/AgentStateSerializer.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,74 +1,22 @@ | ||
package org.bsc.langgraph4j.serializer; | ||
|
||
import lombok.extern.log4j.Log4j; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.bsc.langgraph4j.state.AgentState; | ||
|
||
import java.io.*; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
public class AgentStateSerializer implements Serializer<AgentState> { | ||
public static final AgentStateSerializer INSTANCE = new AgentStateSerializer(); | ||
private AgentStateSerializer() {} | ||
|
||
@Override | ||
public void write(AgentState object, ObjectOutput out) throws IOException { | ||
try( ByteArrayOutputStream baos = new ByteArrayOutputStream() ) { | ||
int actuoalSize = 0; | ||
|
||
final ObjectOutputStream tupleStream = new ObjectOutputStream( baos ); | ||
for( Map.Entry<String,Object> e : object.data().entrySet() ) { | ||
try { | ||
tupleStream.writeUTF(e.getKey()); | ||
tupleStream.writeObject(e.getValue()); | ||
++actuoalSize; | ||
} catch (IOException ex) { | ||
log.error( "Error writing state key '{}' - {}", e.getKey(), ex.getMessage() ); | ||
throw ex; | ||
} | ||
} | ||
|
||
out.writeInt( object.data().size() ); | ||
out.writeInt( actuoalSize ); // actual size | ||
byte[] data = baos.toByteArray(); | ||
out.writeInt( data.length ); | ||
out.write( data ); | ||
|
||
} | ||
|
||
MapSerializer.INSTANCE.write( object.data(), out ); | ||
} | ||
|
||
@Override | ||
public AgentState read(ObjectInput in) throws IOException, ClassNotFoundException { | ||
Map<String, Object> data = new HashMap<>(); | ||
|
||
int expectedSize = in.readInt(); | ||
int actualSize = in.readInt(); | ||
if( expectedSize > 0 && actualSize > 0 ) { | ||
|
||
if( expectedSize != actualSize ) { | ||
final String message = String.format( "Deserialize State: Expected size %d and actual size %d do not match!", expectedSize, actualSize ) ; | ||
log.error( message ) ; | ||
throw new IOException( message ) ; | ||
} | ||
|
||
int byteLen = in.readInt(); | ||
byte[] bytes = new byte[byteLen]; | ||
in.readFully(bytes); | ||
|
||
try( ByteArrayInputStream bais = new ByteArrayInputStream( bytes ) ) { | ||
ObjectInputStream ois = new ObjectInputStream( bais ); | ||
|
||
for( int i = 0; i < actualSize; i++ ) { | ||
String key = ois.readUTF(); | ||
Object value = ois.readObject(); | ||
data.put(key, value); | ||
} | ||
} | ||
|
||
} | ||
Map<String, Object> data = MapSerializer.INSTANCE.read( in ); | ||
return new AgentState(data); | ||
} | ||
} |
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