CheckpointCodec
Converts the checkpoints of a thread to and from a string, so a Checkpointer for any storage only has to read and write strings:
class PreferencesCheckpointer<State>(
private val preferences: Preferences,
private val codec: CheckpointCodec<State>,
) : Checkpointer<State> {
// Adds the checkpoint to the history that is stored, and keeps the last 20.
override suspend fun save(threadId: String, checkpoint: Checkpoint<State>) =
preferences.put(threadId, codec.append(preferences.get(threadId), checkpoint, maxHistory = 20))
override suspend fun load(threadId: String): Checkpoint<State>? =
preferences.get(threadId)?.let { codec.decode(threadId, it) }
override suspend fun history(threadId: String): List<Checkpoint<State>> =
preferences.get(threadId)?.let { codec.decodeHistory(threadId, it) }.orEmpty()
override suspend fun delete(threadId: String) = preferences.remove(threadId)
}The format is versioned and shared by every checkpointer built on this class, including FileCheckpointer. A checkpoint is one line of JSON, and a history is its checkpoints, one on each line, oldest first. A checkpoint of a run that paused inside a subgraph is written as version 2, which a library before subgraphs refuses to read. Every other checkpoint is still version 1.
Parameters
converts the graph state to and from a string.
Functions
Returns the history stored with checkpoint added, for a Checkpointer that keeps the checkpoints of a thread in one string. checkpoint replaces the checkpoints with its step or a later one, as Checkpointer.save asks, and of the result the last maxHistory are kept.
Returns checkpoint as a JSON string.