diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/CachedTool.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/CachedTool.kt index 583bf0aa6..2585f57d4 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/CachedTool.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/CachedTool.kt @@ -5,29 +5,34 @@ import arrow.fx.coroutines.timeInMillis import kotlin.time.Duration import kotlin.time.Duration.Companion.days +data class CachedToolKey(val value: K, val seed: String) + +data class CachedToolValue(val value: V, val timestamp: Long) + abstract class CachedTool( - private val cache: Atomic>>, + private val cache: Atomic, CachedToolValue>>, + private val seed: String, private val timeCachePolicy: Duration = 1.days ) : Tool { override suspend fun invoke(input: Input): Output { - return cache(input) { onCacheMissed(input) } + return cache(CachedToolKey(input, seed)) { onCacheMissed(input) } } abstract suspend fun onCacheMissed(input: Input): Output - private suspend fun cache(input: Input, block: suspend () -> Output): Output { + private suspend fun cache(input: CachedToolKey, block: suspend () -> Output): Output { val cachedToolInfo = cache.get().get(input) if (cachedToolInfo != null) { val lastTimeInCache = timeInMillis() - timeCachePolicy.inWholeMilliseconds if (lastTimeInCache > cachedToolInfo.timestamp) { cache.get().remove(input) } else { - return cachedToolInfo.response + return cachedToolInfo.value } } val response = block() - cache.get().put(input, CachedToolInfo(response, timeInMillis())) + cache.get().put(input, CachedToolValue(response, timeInMillis())) return response } } diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/CachedToolInfo.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/CachedToolInfo.kt deleted file mode 100644 index 84dd49763..000000000 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/CachedToolInfo.kt +++ /dev/null @@ -1,3 +0,0 @@ -package com.xebia.functional.xef.llm.assistants - -data class CachedToolInfo(val response: Response, val timestamp: Long)