I am currently trying out Koog 0.7.2. As I underst...
# koog-agentic-framework
c
I am currently trying out Koog 0.7.2. As I understand it the Koog Storage is not (yet) persisted into the memory + persistence feature, right? So if we have multiple graphs and graph 2 uses something stored in graph 1 then the memory does not matter because the graph is broken? Is the mentioned scenarion even best practice? Should we use something else instead the storage?
v
We are aware of the issue and currently that's indeed happening with storage + persistence. there are some technical difficulties with serializing/deserializing arbitrary typed maps using kotlinx.serialization (cc @Andrey Bragin ). But I think we will solve this quite soon before KotlinConf
👍 1
As a workaround, you can either persist the storage manually (for example, using event handler — you can get access to all events and AgentContext which has storage inside) or alternatively add some fictional nodes within the second subgraph that would essentially just load the storage and place the contents somewhere in the actual typed output (of node). The difference is — then you know the type in compile time (node type), and it can be serialised properly
Planned this task now. Will be available before KotlinConf actually
🙌 1
c
Good to hear, definitely our biggest blocker (manual workaround). We have a complex combination of graphs and a lot of storage usage for the Structured outputs. 😊
v
Wow, sounds impressive! You are definitely deep down the rabbit hole of Koog :)
cc @Mark Tkachenko
c
Copy code
val storageKeysCache: MutableMap<String, AIAgentStorageKey<Any>> = mutableMapOf()

override fun <T : Any> createReusableStorageKey(name: String): AIAgentStorageKey<T> {
    @Suppress("UNCHECKED_CAST") // Safe cast
    return storageKeysCache.getOrPut(name) { createStorageKey(name) } as AIAgentStorageKey<T>
}
If in the same move its possible to use a data class (again) for the StorageKey then we can also remove our custom storageKey Cache. 😃
a
> If in the same move its possible to use a data class (again) for the StorageKey Can you elaborate, please? Do you mean using data classes as keys like
AIAgentStorageKeyMyDataClass
, or do you mean making the
AIAgentStorageKey
itself a data class?
c
There was already a discussion about it somewhere here. 🙂 But yes, making the AIAgentStorageKey itself a data class again. Because if you use the storage in graph A (seperate file) and graph B (Another file) then the key is bascially lost / not accessible because only the reference is equals. So the suggestion here was to build an own cache (for now). It think the argument for not using a data class was that 2 keys with the same name but different types will then throw an exception. But maybe you can use a data class and the key is the combination of the class type + key name? That would solve both problems.
a
But maybe you can use a data class and the key is the combination of the class type + key name? That would solve both problems.
Not quite. The rationale is to prevent accidental storage overwrites when you use built-in/external components in your strategy. So you don’t have to worry about someone or you accidentally overwriting a storage entry because you used the same key name
🤔 1
d
Hello @Vadim Briliantov, I’m a colleague of Mark. Are there any updates regarding your persistent task? From when onward could this feature be available in a version? Thanks in advance for the information 🙂
r
I use this:
Copy code
package org.ronsmits.nestor.memory

import ai.koog.agents.chatMemory.feature.ChatHistoryProvider
import ai.koog.prompt.message.Message
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.slf4j.LoggerFactory
import java.io.File

/**
 * Vault-backed ChatHistoryProvider that persists conversation history as JSON files
 * in the Nestor vault under nestor/conversations/<conversationId>.json.
 *
 * Koog's [ai.koog.agents.chatMemory.feature.ChatMemoryConfig.windowSize] applies to **both**
 * load and store, which would truncate the vault file. Nestor therefore keeps the sliding
 * window **only in [load]**: only the last [maxMessagesForModel] messages are injected into
 * the model; [store] receives the **full** [messages] list from Koog and persists it entirely.
 *
 * Use [loadTranscript] for HTTP `/history` or tooling that needs the full day thread.
 */
class VaultChatHistoryProvider(
    private val vaultPath: String,
    private val conversationsSubdir: String = "nestor/conversations",
    /** Max messages (after any Koog filter preprocessors) passed to the LLM; full thread stays on disk. */
    private val maxMessagesForModel: Int = 50,
) : ChatHistoryProvider {

    private val log = LoggerFactory.getLogger(VaultChatHistoryProvider::class.java)
    private val mutex = Mutex()
    private val cache = mutableMapOf<String, List<Message>>()

    private val json = Json {
        prettyPrint = true
        ignoreUnknownKeys = true
        encodeDefaults = true
    }

    private fun conversationDir(): File =
        File(vaultPath, conversationsSubdir).also { it.mkdirs() }

    private fun conversationFile(conversationId: String): File =
        File(conversationDir(), "$conversationId.json")

    private fun readFullLocked(conversationId: String): List<Message> {
        cache[conversationId]?.let { return it }
        val file = conversationFile(conversationId)
        if (!file.exists()) {
            return emptyList()
        }
        return try {
            json.decodeFromString<List<Message>>(file.readText()).also {
                cache[conversationId] = it
            }
        } catch (e: Exception) {
            log.error("Failed to load conversation '{}' from vault: {}", conversationId, e.message)
            emptyList()
        }
    }

    override suspend fun store(conversationId: String, messages: List<Message>) {
        mutex.withLock {
            cache[conversationId] = messages
            try {
                val file = conversationFile(conversationId)
                file.writeText(json.encodeToString(messages))
                log.debug("Stored {} messages for conversation '{}'", messages.size, conversationId)
            } catch (e: Exception) {
                log.error("Failed to persist conversation '{}' to vault: {}", conversationId, e.message)
            }
        }
    }

    override suspend fun load(conversationId: String): List<Message> {
        return mutex.withLock {
            val full = readFullLocked(conversationId)
            cache[conversationId] = full
            val windowed = when {
                maxMessagesForModel <= 0 || full.size <= maxMessagesForModel -> full
                else -> full.takeLast(maxMessagesForModel)
            }
            log.debug(
                "load: conversation '{}': fullSize={} -> modelWindow={}",
                conversationId,
                full.size,
                windowed.size,
            )
            windowed
        }
    }

    /** Full persisted thread for this [conversationId] (not truncated for the LLM). */
    suspend fun loadTranscript(conversationId: String): List<Message> {
        return mutex.withLock { readFullLocked(conversationId) }
    }
}
v
Hi! Thanks for reaching out to us. @Mark Tkachenko and @Andrey Bragin are already working on this functionality