Store

interface Store

Keeps what an agent learned between conversations: a preference of a user, a fact, an earlier decision. A Checkpointer saves the state of one thread; a store belongs to no thread, so every run that is given the same store reads what the others wrote.

A value is text under a namespace and a key. The namespace is a list of names, such as listOf("users", userId, "preferences"), and list finds everything under a part of it, such as listOf("users", userId). The overloads with a StateSerializer keep other types, and with telar-serialization a @Serializable type needs none.

val store = MemoryStore()
val graph = StateGraph<Trip> {
val plan = node("plan") { trip ->
val diet = store().get(listOf("preferences"), "diet")?.value
trip.copy(diet = diet)
}
START then plan then END
}.compile()

graph.invoke(trip, GraphConfig(threadId = "plan-7", store = store.within("users", userId)))

Give it to a run with GraphConfig.store, and a node or a tool reaches it with store. within gives each user a part of the store of their own.

A write goes straight to the store, and is not part of a checkpoint. When a step runs again, its nodes write again: a put under the same key leaves the value of the last try, which is the try whose state the run continues with. A node that reads a value and writes one made from it, such as a counter, uses effect, as it would with any other service. A step that fails and is never resumed keeps what it wrote.

search finds the items that a query is about. Here it looks for the words of the query; a store that ranks by meaning, such as SemanticStore of telar-agent, replaces it.

Every implementation checks what it is given with checkAddress, checkNamespace and checkLimit, as MemoryStore does: a key and each part of a namespace are not empty, and a limit is positive.

Inheritors

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
abstract suspend fun delete(namespace: List<String>, key: String)

Removes the item under key in namespace. Does nothing when there is none.

Link copied to clipboard
abstract suspend fun get(namespace: List<String>, key: String): StoreItem?

Returns the item under key in namespace, or null when there is none.

Link copied to clipboard
suspend fun <T> Store.get(namespace: List<String>, key: String, serializer: StateSerializer<T>): T?

Returns the value under key in namespace, read with serializer, or null when there is none.

Link copied to clipboard
abstract suspend fun list(namespace: List<String>, limit: Int = DEFAULT_LIMIT): List<StoreItem>

Returns the items of namespace and of the namespaces that start with it, the last changed first, at most limit of them. An empty namespace lists the whole store.

Link copied to clipboard
abstract suspend fun put(namespace: List<String>, key: String, value: String)

Keeps value under key in namespace, in place of the value that was there.

Link copied to clipboard
suspend fun <T> Store.put(namespace: List<String>, key: String, value: T, serializer: StateSerializer<T>)

Keeps value under key in namespace, written with serializer.

Link copied to clipboard
open suspend fun search(namespace: List<String>, query: String, limit: Int = DEFAULT_SEARCH_LIMIT): List<StoreItem>

Returns the items of namespace and of the namespaces under it that query is about, the best first, at most limit of them.

Link copied to clipboard
fun Store.within(vararg namespace: String): Store

Returns a view of this store whose namespaces all start with namespace. Give each user a view of their own, store.within("users", userId), so that a graph compiled once serves every user, and nothing in it can read what another user's runs wrote.