Chrylo
03/24/2026, 4:22 PMVadim Briliantov
03/25/2026, 12:32 AMVadim Briliantov
03/25/2026, 12:35 AMVadim Briliantov
03/25/2026, 5:45 PMChrylo
03/25/2026, 5:47 PMVadim Briliantov
03/25/2026, 5:47 PMVadim Briliantov
03/25/2026, 5:48 PMChrylo
03/25/2026, 5:50 PMval 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. 😃Andrey Bragin
03/26/2026, 8:59 AMAIAgentStorageKeyMyDataClass, or do you mean making the AIAgentStorageKey itself a data class?Chrylo
03/26/2026, 9:10 AMAndrey Bragin
03/26/2026, 9:23 AMBut 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
Daniel
05/08/2026, 9:26 AMron
05/08/2026, 9:52 AMpackage 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) }
}
}Vadim Briliantov
05/12/2026, 12:38 PM