mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(context): support skip modules by adding specific moduleName into runningFlowContext
This commit is contained in:
@@ -9,7 +9,7 @@ import java.util.concurrent.locks.Lock;
|
||||
@Capability("cognition")
|
||||
public interface CognitionCapability {
|
||||
|
||||
String initiateTurn(String input, String target);
|
||||
String initiateTurn(String input, String target, String... skippedModules);
|
||||
|
||||
ContextWorkspace contextWorkspace();
|
||||
|
||||
|
||||
@@ -46,9 +46,14 @@ public class CognitionCore extends PartnerCore<CognitionCore> {
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public String initiateTurn(String input, String target) {
|
||||
public String initiateTurn(String input, String target, String... skippedModules) {
|
||||
PartnerRunningFlowContext primaryContext = PartnerRunningFlowContext.Companion.fromSelf(input);
|
||||
primaryContext.setTarget(target);
|
||||
if (skippedModules != null) {
|
||||
for (String skippedModule : skippedModules) {
|
||||
primaryContext.addSkippedModule(skippedModule);
|
||||
}
|
||||
}
|
||||
PartnerRunningFlowContext executedContext = AgentRuntime.INSTANCE.submit(primaryContext);
|
||||
return executedContext.getCoreResponse().getString("text");
|
||||
}
|
||||
|
||||
@@ -41,9 +41,6 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
|
||||
private static final String IMMEDIATE_WATCHER_CRON = "0/5 * * * * ?";
|
||||
private static final String BLOCK_SOURCE = "action_planner_pending";
|
||||
private static final String TENDENCIES_EVALUATING_BLOCK_NAME = "tendencies_in_evaluating";
|
||||
private static final double PENDING_REPLACE_FADE_FACTOR = 10.0;
|
||||
private static final double PENDING_TIME_FADE_FACTOR = 10.0;
|
||||
private static final double PENDING_ACTIVATE_FACTOR = 0.0;
|
||||
|
||||
private final ActionAssemblyHelper assemblyHelper = new ActionAssemblyHelper();
|
||||
|
||||
@@ -65,6 +62,7 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
|
||||
|
||||
@Init
|
||||
public void init() {
|
||||
this.setModuleName("action_planner");
|
||||
executor = actionCapability.getExecutor(ActionCore.ExecutorType.VIRTUAL);
|
||||
}
|
||||
|
||||
@@ -99,7 +97,7 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
|
||||
}
|
||||
|
||||
private @NotNull BlockContent buildTendenciesEvaluatingAbstractBlock(List<String> tendencies, String datetime, String input) {
|
||||
return new BlockContent(TENDENCIES_EVALUATING_BLOCK_NAME, BLOCK_SOURCE, BlockContent.Urgency.HIGH) {
|
||||
return new BlockContent(TENDENCIES_EVALUATING_BLOCK_NAME, getModuleName(), BlockContent.Urgency.HIGH) {
|
||||
@Override
|
||||
protected void fillXml(@NotNull Document document, @NotNull Element root) {
|
||||
appendTextElement(document, root, "datetime", datetime);
|
||||
@@ -110,7 +108,7 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
|
||||
}
|
||||
|
||||
private @NotNull BlockContent buildTendenciesEvaluatingCompactBlock(List<String> tendencies, String datetime, String input) {
|
||||
return new BlockContent(TENDENCIES_EVALUATING_BLOCK_NAME, BLOCK_SOURCE, BlockContent.Urgency.HIGH) {
|
||||
return new BlockContent(TENDENCIES_EVALUATING_BLOCK_NAME, getModuleName(), BlockContent.Urgency.HIGH) {
|
||||
@Override
|
||||
protected void fillXml(@NotNull Document document, @NotNull Element root) {
|
||||
int size = tendencies.size();
|
||||
@@ -124,7 +122,7 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
|
||||
}
|
||||
|
||||
private @NotNull BlockContent buildTendenciesEvaluatingFullBlock(List<String> tendencies) {
|
||||
return new CommunicationBlockContent(TENDENCIES_EVALUATING_BLOCK_NAME, BLOCK_SOURCE, BlockContent.Urgency.HIGH, CommunicationBlockContent.Projection.SUPPLY) {
|
||||
return new CommunicationBlockContent(TENDENCIES_EVALUATING_BLOCK_NAME, getModuleName(), BlockContent.Urgency.HIGH, CommunicationBlockContent.Projection.SUPPLY) {
|
||||
@Override
|
||||
protected void fillXml(@NotNull Document document, @NotNull Element root) {
|
||||
appendRepeatedElements(document, root, "tendency", tendencies);
|
||||
@@ -139,7 +137,7 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
|
||||
handleEvaluatorResults(evaluatorResults, source, input);
|
||||
updateTendencyCache(evaluatorResults, input, extractorResult);
|
||||
|
||||
cognitionCapability.contextWorkspace().expire(TENDENCIES_EVALUATING_BLOCK_NAME, BLOCK_SOURCE);
|
||||
cognitionCapability.contextWorkspace().expire(TENDENCIES_EVALUATING_BLOCK_NAME, getModuleName());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -201,9 +199,9 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
|
||||
buildPendingCompactBlock(blockName, executableAction, evaluatorResult, input),
|
||||
buildPendingAbstractBlock(blockName, executableAction, evaluatorResult, input),
|
||||
Set.of(ContextBlock.VisibleDomain.ACTION),
|
||||
PENDING_REPLACE_FADE_FACTOR,
|
||||
PENDING_TIME_FADE_FACTOR,
|
||||
PENDING_ACTIVATE_FACTOR
|
||||
30,
|
||||
10,
|
||||
5
|
||||
);
|
||||
cognitionCapability.contextWorkspace().register(block);
|
||||
}
|
||||
|
||||
@@ -9,13 +9,14 @@ import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.AgentComponent
|
||||
import work.slhaf.partner.api.agent.factory.component.exception.ModuleFactoryInitFailedException
|
||||
import work.slhaf.partner.api.agent.factory.config.pojo.ModelConfig
|
||||
import work.slhaf.partner.api.agent.factory.context.AgentContext
|
||||
import work.slhaf.partner.api.agent.factory.context.AgentRegisterContext
|
||||
import work.slhaf.partner.api.agent.factory.context.ModuleContextData
|
||||
import java.lang.reflect.Modifier
|
||||
import java.time.ZonedDateTime
|
||||
|
||||
/**
|
||||
* 扫描并实例化 `@AgentComponent` 具体类,写入 [work.slhaf.partner.api.agent.factory.context.AgentContext]。
|
||||
* 扫描并实例化 `@AgentComponent` 具体类,写入 [AgentContext]。
|
||||
*
|
||||
* 行为:
|
||||
* - 若实例是 [AbstractAgentModule],按 Running/Sub/Standalone 构造 `ModuleContextData` 并注册到 modules。
|
||||
@@ -63,7 +64,7 @@ class ComponentRegisterFactory : AgentBaseFactory() {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private fun registerModule(
|
||||
agentContext: work.slhaf.partner.api.agent.factory.context.AgentContext,
|
||||
agentContext: AgentContext,
|
||||
componentClass: Class<*>,
|
||||
module: AbstractAgentModule,
|
||||
modelConfigMap: Map<String, ModelConfig>,
|
||||
@@ -124,7 +125,7 @@ class ComponentRegisterFactory : AgentBaseFactory() {
|
||||
}
|
||||
|
||||
private fun addAdditionalComponent(
|
||||
agentContext: work.slhaf.partner.api.agent.factory.context.AgentContext,
|
||||
agentContext: AgentContext,
|
||||
componentClass: Class<*>,
|
||||
componentInstance: Any
|
||||
) {
|
||||
|
||||
@@ -65,6 +65,9 @@ object AgentRuntime {
|
||||
coroutineScope {
|
||||
val jobs = modules.map { module ->
|
||||
async {
|
||||
if (runningFlowContext.skippedModules.contains(module.instance.moduleName)) {
|
||||
return@async
|
||||
}
|
||||
module.instance.execute(runningFlowContext)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,9 +28,17 @@ abstract class RunningFlowContext {
|
||||
val additionalUserInfo: Map<String, String>
|
||||
get() = _additionalUserInfo
|
||||
|
||||
private val _skippedModules = mutableSetOf<String>()
|
||||
val skippedModules: Set<String>
|
||||
get() = _skippedModules
|
||||
|
||||
val status = Status()
|
||||
val info = Info()
|
||||
|
||||
fun addSkippedModule(moduleName: String) {
|
||||
_skippedModules.add(moduleName)
|
||||
}
|
||||
|
||||
fun putUserInfo(key: String, value: String) {
|
||||
_additionalUserInfo[key] = value
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user