ait
06/23/2025, 8:03 AMDidier Villevalois
06/23/2025, 9:55 AMmemoryId
as LangChain calls it). So you'd have to handle that yourself if you need long-running conversations. Also, Agent contains all of its state, so indeed you'd need to have a pool of agents, and modify their state as needed before running them. @Vadim Briliantov please correct me, if I am wrong.Mark Tkachenko
06/23/2025, 10:34 AMait
06/23/2025, 10:35 AMfun agentPool(maxAvailable: Int, factory: () -> AIAgent): AIAgentBase {
return PooledAgent(Semaphore(maxAvailable), factory)
}
private class PooledAgent(
private val semaphore: Semaphore,
private val factory: () -> AIAgent
)
: AIAgentBase
{
private val queue = ConcurrentLinkedQueue<AIAgent>()
override suspend fun run(agentInput: String) {
throw UnsupportedOperationException("PooledAgent does not support run()")
}
override suspend fun runAndGetResult(agentInput: String): String? =
semaphore.withPermit {
val agent = queue.poll() ?: factory()
try {
agent.runAndGetResult(agentInput)
}
finally {
queue.offer(agent)
}
}
}
Mark Tkachenko
06/23/2025, 10:49 AMait
06/23/2025, 11:26 AM