refactor(context): support skip modules by adding specific moduleName into runningFlowContext

This commit is contained in:
2026-03-29 22:26:10 +08:00
parent b6c86c6640
commit 6e37ed032b
6 changed files with 30 additions and 15 deletions

View File

@@ -9,7 +9,7 @@ import java.util.concurrent.locks.Lock;
@Capability("cognition") @Capability("cognition")
public interface CognitionCapability { public interface CognitionCapability {
String initiateTurn(String input, String target); String initiateTurn(String input, String target, String... skippedModules);
ContextWorkspace contextWorkspace(); ContextWorkspace contextWorkspace();

View File

@@ -46,9 +46,14 @@ public class CognitionCore extends PartnerCore<CognitionCore> {
} }
@CapabilityMethod @CapabilityMethod
public String initiateTurn(String input, String target) { public String initiateTurn(String input, String target, String... skippedModules) {
PartnerRunningFlowContext primaryContext = PartnerRunningFlowContext.Companion.fromSelf(input); PartnerRunningFlowContext primaryContext = PartnerRunningFlowContext.Companion.fromSelf(input);
primaryContext.setTarget(target); primaryContext.setTarget(target);
if (skippedModules != null) {
for (String skippedModule : skippedModules) {
primaryContext.addSkippedModule(skippedModule);
}
}
PartnerRunningFlowContext executedContext = AgentRuntime.INSTANCE.submit(primaryContext); PartnerRunningFlowContext executedContext = AgentRuntime.INSTANCE.submit(primaryContext);
return executedContext.getCoreResponse().getString("text"); return executedContext.getCoreResponse().getString("text");
} }

View File

@@ -41,9 +41,6 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
private static final String IMMEDIATE_WATCHER_CRON = "0/5 * * * * ?"; private static final String IMMEDIATE_WATCHER_CRON = "0/5 * * * * ?";
private static final String BLOCK_SOURCE = "action_planner_pending"; private static final String BLOCK_SOURCE = "action_planner_pending";
private static final String TENDENCIES_EVALUATING_BLOCK_NAME = "tendencies_in_evaluating"; 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(); private final ActionAssemblyHelper assemblyHelper = new ActionAssemblyHelper();
@@ -65,6 +62,7 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
@Init @Init
public void init() { public void init() {
this.setModuleName("action_planner");
executor = actionCapability.getExecutor(ActionCore.ExecutorType.VIRTUAL); 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) { 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 @Override
protected void fillXml(@NotNull Document document, @NotNull Element root) { protected void fillXml(@NotNull Document document, @NotNull Element root) {
appendTextElement(document, root, "datetime", datetime); 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) { 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 @Override
protected void fillXml(@NotNull Document document, @NotNull Element root) { protected void fillXml(@NotNull Document document, @NotNull Element root) {
int size = tendencies.size(); int size = tendencies.size();
@@ -124,7 +122,7 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
} }
private @NotNull BlockContent buildTendenciesEvaluatingFullBlock(List<String> tendencies) { 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 @Override
protected void fillXml(@NotNull Document document, @NotNull Element root) { protected void fillXml(@NotNull Document document, @NotNull Element root) {
appendRepeatedElements(document, root, "tendency", tendencies); appendRepeatedElements(document, root, "tendency", tendencies);
@@ -139,7 +137,7 @@ public class ActionPlanner extends AbstractAgentModule.Running<PartnerRunningFlo
handleEvaluatorResults(evaluatorResults, source, input); handleEvaluatorResults(evaluatorResults, source, input);
updateTendencyCache(evaluatorResults, input, extractorResult); 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), buildPendingCompactBlock(blockName, executableAction, evaluatorResult, input),
buildPendingAbstractBlock(blockName, executableAction, evaluatorResult, input), buildPendingAbstractBlock(blockName, executableAction, evaluatorResult, input),
Set.of(ContextBlock.VisibleDomain.ACTION), Set.of(ContextBlock.VisibleDomain.ACTION),
PENDING_REPLACE_FADE_FACTOR, 30,
PENDING_TIME_FADE_FACTOR, 10,
PENDING_ACTIVATE_FACTOR 5
); );
cognitionCapability.contextWorkspace().register(block); cognitionCapability.contextWorkspace().register(block);
} }

View File

@@ -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.annotation.AgentComponent
import work.slhaf.partner.api.agent.factory.component.exception.ModuleFactoryInitFailedException 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.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.AgentRegisterContext
import work.slhaf.partner.api.agent.factory.context.ModuleContextData import work.slhaf.partner.api.agent.factory.context.ModuleContextData
import java.lang.reflect.Modifier import java.lang.reflect.Modifier
import java.time.ZonedDateTime import java.time.ZonedDateTime
/** /**
* 扫描并实例化 `@AgentComponent` 具体类,写入 [work.slhaf.partner.api.agent.factory.context.AgentContext]。 * 扫描并实例化 `@AgentComponent` 具体类,写入 [AgentContext]。
* *
* 行为: * 行为:
* - 若实例是 [AbstractAgentModule],按 Running/Sub/Standalone 构造 `ModuleContextData` 并注册到 modules。 * - 若实例是 [AbstractAgentModule],按 Running/Sub/Standalone 构造 `ModuleContextData` 并注册到 modules。
@@ -63,7 +64,7 @@ class ComponentRegisterFactory : AgentBaseFactory() {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
private fun registerModule( private fun registerModule(
agentContext: work.slhaf.partner.api.agent.factory.context.AgentContext, agentContext: AgentContext,
componentClass: Class<*>, componentClass: Class<*>,
module: AbstractAgentModule, module: AbstractAgentModule,
modelConfigMap: Map<String, ModelConfig>, modelConfigMap: Map<String, ModelConfig>,
@@ -124,7 +125,7 @@ class ComponentRegisterFactory : AgentBaseFactory() {
} }
private fun addAdditionalComponent( private fun addAdditionalComponent(
agentContext: work.slhaf.partner.api.agent.factory.context.AgentContext, agentContext: AgentContext,
componentClass: Class<*>, componentClass: Class<*>,
componentInstance: Any componentInstance: Any
) { ) {

View File

@@ -65,6 +65,9 @@ object AgentRuntime {
coroutineScope { coroutineScope {
val jobs = modules.map { module -> val jobs = modules.map { module ->
async { async {
if (runningFlowContext.skippedModules.contains(module.instance.moduleName)) {
return@async
}
module.instance.execute(runningFlowContext) module.instance.execute(runningFlowContext)
} }
} }

View File

@@ -28,9 +28,17 @@ abstract class RunningFlowContext {
val additionalUserInfo: Map<String, String> val additionalUserInfo: Map<String, String>
get() = _additionalUserInfo get() = _additionalUserInfo
private val _skippedModules = mutableSetOf<String>()
val skippedModules: Set<String>
get() = _skippedModules
val status = Status() val status = Status()
val info = Info() val info = Info()
fun addSkippedModule(moduleName: String) {
_skippedModules.add(moduleName)
}
fun putUserInfo(key: String, value: String) { fun putUserInfo(key: String, value: String) {
_additionalUserInfo[key] = value _additionalUserInfo[key] = value
} }