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

serializer

converts the graph state to and from a string.

Constructors

Link copied to clipboard
constructor(serializer: StateSerializer<State>)

Functions

Link copied to clipboard
fun append(stored: String?, checkpoint: Checkpoint<State>, maxHistory: Int = Int.MAX_VALUE): String

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.

Link copied to clipboard
fun decode(threadId: String, data: String): Checkpoint<State>

Reads the latest checkpoint of what encode or append returned.

Link copied to clipboard
fun decodeHistory(threadId: String, data: String): List<Checkpoint<State>>

Reads every checkpoint of what append or encode returned, oldest first.

Link copied to clipboard
fun encode(checkpoint: Checkpoint<State>): String

Returns checkpoint as a JSON string.