diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/ActionInterventor.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/ActionInterventor.java index 92f68ba8..2e64f934 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/ActionInterventor.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/ActionInterventor.java @@ -2,13 +2,13 @@ package work.slhaf.partner.module.modules.action.interventor; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; +import lombok.val; import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability; import work.slhaf.partner.api.agent.factory.module.annotation.AgentModule; import work.slhaf.partner.api.agent.factory.module.annotation.InjectModule; import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.ActivateModel; import work.slhaf.partner.core.action.ActionCapability; -import work.slhaf.partner.core.action.entity.ActionData; -import work.slhaf.partner.core.action.entity.PhaserRecord; +import work.slhaf.partner.core.action.ActionCore; import work.slhaf.partner.core.cognation.CognationCapability; import work.slhaf.partner.core.memory.MemoryCapability; import work.slhaf.partner.module.common.module.PreRunningModule; @@ -18,11 +18,6 @@ import work.slhaf.partner.module.modules.action.interventor.evaluator.Interventi import work.slhaf.partner.module.modules.action.interventor.evaluator.entity.EvaluatorInput; import work.slhaf.partner.module.modules.action.interventor.evaluator.entity.EvaluatorResult; import work.slhaf.partner.module.modules.action.interventor.evaluator.entity.EvaluatorResult.EvaluatedInterventionData; -import work.slhaf.partner.module.modules.action.interventor.handler.InterventionHandler; -import work.slhaf.partner.module.modules.action.interventor.handler.entity.HandlerInput; -import work.slhaf.partner.module.modules.action.interventor.handler.entity.HandlerInput.ExecutingInterventionData; -import work.slhaf.partner.module.modules.action.interventor.handler.entity.HandlerInput.InterventionData; -import work.slhaf.partner.module.modules.action.interventor.handler.entity.HandlerInput.PreparedInterventionData; import work.slhaf.partner.module.modules.action.interventor.recognizer.InterventionRecognizer; import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.RecognizerInput; import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.RecognizerResult; @@ -32,7 +27,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.function.Supplier; import java.util.stream.Stream; /** @@ -45,8 +39,6 @@ public class ActionInterventor extends PreRunningModule implements ActivateModel private InterventionRecognizer interventionRecognizer; @InjectModule private InterventionEvaluator interventionEvaluator; - @InjectModule - private InterventionHandler interventionHandler; @InjectCapability private ActionCapability actionCapability; @@ -92,21 +84,33 @@ public class ActionInterventor extends PreRunningModule implements ActivateModel invalidActionKeysFilter(executingDataList); invalidActionKeysFilter(preparedDataList); - // 同步写入prompt,异步处理干预行为,‘异步’在 interventionHandler 中体现 + // 同步写入prompt,异步处理干预行为,‘异步’在处理流程中体现 promptHelper.setupInterventionPrompt(uuid, executingDataList, preparedDataList); - interventionHandler.execute(assemblyHelper.buildHandlerInput(executingDataList, preparedDataList, recognizerResult)); + handleInterventions(executingDataList, recognizerResult.getExecutingInterventions()); + handleInterventions(preparedDataList, recognizerResult.getPreparedInterventions()); } else { promptHelper.setupInterventionIgnoredPrompt(uuid, executingDataList, preparedDataList); } } + private void handleInterventions(List interventionDataList, Map interventionDataMap) { + val executor = actionCapability.getExecutor(ActionCore.ExecutorType.PLATFORM); + executor.execute(() -> { + for (EvaluatedInterventionData interventionData : interventionDataList) { + // 此处拿到的为 ActionData 或者 PhaserRecord, 来自 Recognizer 的封装 + val data = interventionDataMap.get(interventionData.getTendency()); + actionCapability.handleInterventions(interventionData.getMetaInterventionList(), data); + } + }); + } + private void invalidActionKeysFilter(List interventions) { List toRemove = new ArrayList<>(); for (EvaluatedInterventionData intervention : interventions) { - List interventionData = intervention.getInterventionData(); + List interventionData = intervention.getMetaInterventionList(); List actions = new ArrayList<>(); for (MetaIntervention metaData : interventionData) { actions.addAll(metaData.getActions()); @@ -163,64 +167,6 @@ public class ActionInterventor extends PreRunningModule implements ActivateModel private AssemblyHelper() { } - /** - * @param executingDataList 对应评估结果中的‘执行中行动’ - * @param preparedDataList 对应评估结果中的‘待执行行动’ - * @param recognizerResult 干预识别结果,包含‘执行中’‘待执行’两类评估结果各自对应的行动数据 - * @return 处理器输入 - */ - private HandlerInput buildHandlerInput(List executingDataList, - List preparedDataList, RecognizerResult recognizerResult) { - HandlerInput input = new HandlerInput(); - Map executingInterventions = recognizerResult.getExecutingInterventions(); - Map preparedInterventions = recognizerResult.getPreparedInterventions(); - - List executing = setupInputDataList(executingDataList, executingInterventions, - ExecutingInterventionData::new); - List prepared = setupInputDataList(preparedDataList, preparedInterventions, - PreparedInterventionData::new); - - input.setExecuting(executing); - input.setPrepared(prepared); - - return input; - } - - /** - * @param HandlerInput 中 List 对应的泛型 - * @param evaluatedDataList 评估结果列表 - * @param interventionMap 干预识别结果中的 tendency:data 映射 - * @param factory 输入类型构建工厂 - * @return 处理器输入(干预列表) - */ - private List setupInputDataList(List evaluatedDataList, - Map interventionMap, Supplier factory) { - - List result = new ArrayList<>(); - - for (EvaluatedInterventionData interventionData : evaluatedDataList) { - - I data = factory.get(); - - if (data instanceof InterventionData inputData) { - inputData.setTendency(interventionData.getTendency()); - inputData.setDescription(interventionData.getDescription()); - inputData.setInterventions(interventionData.getInterventionData()); - } - - if (data instanceof ExecutingInterventionData inputData) { - inputData.setRecord((PhaserRecord) interventionMap.get(interventionData.getTendency())); - } else if (data instanceof PreparedInterventionData inputData) { - inputData.setActionData((ActionData) interventionMap.get(interventionData.getTendency())); - } - - result.add(data); - } - - return result; - } - - private RecognizerInput buildRecognizerInput(String userId, String input) { RecognizerInput recognizerInput = new RecognizerInput(); recognizerInput.setInput(input); @@ -279,7 +225,7 @@ public class ActionInterventor extends PreRunningModule implements ActivateModel newElement.put("[干预倾向]", tendency); JSONArray changes = newElement.putArray("[行动链变动情况]"); - for (MetaIntervention intervention : data.getInterventionData()) { + for (MetaIntervention intervention : data.getMetaInterventionList()) { JSONObject change = changes.addObject(); change.put("[干预类型]", intervention.getType()); change.put("[干预序号]", intervention.getOrder()); diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/evaluator/entity/EvaluatorResult.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/evaluator/entity/EvaluatorResult.java index 5b927992..bd249426 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/evaluator/entity/EvaluatorResult.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/evaluator/entity/EvaluatorResult.java @@ -28,6 +28,6 @@ public class EvaluatorResult { * 描述信息(包括通过、失败原因) */ private String description; - private List interventionData; + private List metaInterventionList; } } diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/handler/InterventionHandler.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/handler/InterventionHandler.java deleted file mode 100644 index dad7402b..00000000 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/handler/InterventionHandler.java +++ /dev/null @@ -1,58 +0,0 @@ -package work.slhaf.partner.module.modules.action.interventor.handler; - -import lombok.extern.slf4j.Slf4j; -import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability; -import work.slhaf.partner.api.agent.factory.module.annotation.AgentSubModule; -import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.AgentRunningSubModule; -import work.slhaf.partner.core.action.ActionCapability; -import work.slhaf.partner.core.action.ActionCore.ExecutorType; -import work.slhaf.partner.module.modules.action.interventor.entity.MetaIntervention; -import work.slhaf.partner.module.modules.action.interventor.handler.entity.HandlerInput; -import work.slhaf.partner.module.modules.action.interventor.handler.entity.HandlerInput.ExecutingInterventionData; -import work.slhaf.partner.module.modules.action.interventor.handler.entity.HandlerInput.InterventionData; -import work.slhaf.partner.module.modules.action.interventor.handler.entity.HandlerInput.PreparedInterventionData; - -import java.util.List; -import java.util.concurrent.ExecutorService; - -@Slf4j -@AgentSubModule -public class InterventionHandler extends AgentRunningSubModule { - - @InjectCapability - private ActionCapability actionCapability; - - /** - * 针对‘行动干预’做出处理 - * - * @param data 行动干预输入 - * @return 无返回值 - */ - @Override - public Void execute(HandlerInput data) { - ExecutorService executor = actionCapability.getExecutor(ExecutorType.VIRTUAL); - handle(data.getExecuting(), executor); - handle(data.getPrepared(), executor); - return null; - } - - private void handle(List executing, ExecutorService executor) { - executor.execute(() -> { - executing.forEach(interventionData -> { - // 干预逻辑一致 - // 同步操作不同 - // HandlerAction 抽取同步逻辑 - // 此处进行遍历 intervention - // 根据Intervention类型进行分发 - - List interventions = interventionData.getInterventions(); - if (interventionData instanceof ExecutingInterventionData data) { - actionCapability.handleInterventions(interventions, data.getRecord()); - } else if (interventionData instanceof PreparedInterventionData data) { - actionCapability.handleInterventions(interventions, data.getActionData()); - } - - }); - }); - } -} diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/handler/entity/HandlerInput.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/handler/entity/HandlerInput.java deleted file mode 100644 index 851e618d..00000000 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/interventor/handler/entity/HandlerInput.java +++ /dev/null @@ -1,39 +0,0 @@ -package work.slhaf.partner.module.modules.action.interventor.handler.entity; - -import lombok.Data; -import lombok.EqualsAndHashCode; -import work.slhaf.partner.core.action.entity.ActionData; -import work.slhaf.partner.core.action.entity.PhaserRecord; -import work.slhaf.partner.module.modules.action.interventor.entity.MetaIntervention; - -import java.util.List; - -@Data -public class HandlerInput { - - private List executing; - private List prepared; - - @Data - @EqualsAndHashCode(callSuper = true) - public static class ExecutingInterventionData extends InterventionData { - private PhaserRecord record; - } - - @Data - @EqualsAndHashCode(callSuper = true) - public static class PreparedInterventionData extends InterventionData { - private ActionData actionData; - } - - /** - * 针对一个干预倾向而言,有可能针对一个行动数据做出多种、不同类型的干预操作,即封装为 InterventionData 内部的 MetaIntervention 列表 - */ - @Data - public static abstract class InterventionData { - protected String tendency; - protected String description; - protected List interventions; - } - -}