mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(framework): cache running modules in AgentInteractionAdapter and add refresh method
This commit is contained in:
@@ -15,6 +15,27 @@ abstract class AgentInteractionAdapter<I : AgentInputData, O : AgentOutputData,
|
|||||||
|
|
||||||
// TODO whether to support message queue, to avoid concurrence problem in data and agent stats
|
// TODO whether to support message queue, to avoid concurrence problem in data and agent stats
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@Volatile
|
||||||
|
private var runningModules:
|
||||||
|
Map<Int, List<ModuleContextData.Running<AbstractAgentModule.Running<RunningFlowContext>>>> = emptyMap()
|
||||||
|
|
||||||
|
fun refreshRunningModules() {
|
||||||
|
val newMap = buildRunningModules()
|
||||||
|
runningModules = newMap
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildRunningModules():
|
||||||
|
Map<Int, List<ModuleContextData.Running<AbstractAgentModule.Running<RunningFlowContext>>>> {
|
||||||
|
return AgentContext.modules
|
||||||
|
.values
|
||||||
|
.filterIsInstance<ModuleContextData.Running<AbstractAgentModule.Running<RunningFlowContext>>>()
|
||||||
|
.filter { it.enabled }
|
||||||
|
.groupBy { it.order }
|
||||||
|
.toSortedMap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun submit(inputData: I): O {
|
fun submit(inputData: I): O {
|
||||||
val finalInputData: C = parseInputData(inputData)
|
val finalInputData: C = parseInputData(inputData)
|
||||||
val outputContext: C = call(finalInputData)
|
val outputContext: C = call(finalInputData)
|
||||||
@@ -22,15 +43,10 @@ abstract class AgentInteractionAdapter<I : AgentInputData, O : AgentOutputData,
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun call(runningFlowContext: C): C = runBlocking {
|
private fun call(runningFlowContext: C): C = runBlocking {
|
||||||
val runningModules =
|
|
||||||
mutableMapOf<Int, MutableList<ModuleContextData.Running<AbstractAgentModule.Running<RunningFlowContext>>>>()
|
|
||||||
|
|
||||||
AgentContext.modules
|
if (runningModules.isEmpty()) {
|
||||||
.filter { ModuleContextData.Running::class.java.isAssignableFrom(it.value.javaClass) }
|
buildRunningModules()
|
||||||
.map { it.value as ModuleContextData.Running<AbstractAgentModule.Running<RunningFlowContext>> }
|
}
|
||||||
.filter { it.enabled }
|
|
||||||
.sortedBy { it.order }
|
|
||||||
.forEach { runningModules.computeIfAbsent(it.order) { mutableListOf() }.add(it) }
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (modules in runningModules.values) {
|
for (modules in runningModules.values) {
|
||||||
@@ -45,8 +61,9 @@ abstract class AgentInteractionAdapter<I : AgentInputData, O : AgentOutputData,
|
|||||||
return@runBlocking runningFlowContext
|
return@runBlocking runningFlowContext
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private suspend fun executeOrder(
|
private suspend fun executeOrder(
|
||||||
modules: MutableList<ModuleContextData.Running<AbstractAgentModule.Running<RunningFlowContext>>>,
|
modules: List<ModuleContextData.Running<AbstractAgentModule.Running<RunningFlowContext>>>,
|
||||||
runningFlowContext: C
|
runningFlowContext: C
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user