mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(ActionInterventor): remove InterventionHandler and related data class
Context: Since last commit, the logic of interventions has been moved into ActionCapability, the InterventionHandler is not needed.
This commit is contained in:
@@ -2,13 +2,13 @@ package work.slhaf.partner.module.modules.action.interventor;
|
|||||||
|
|
||||||
import com.alibaba.fastjson2.JSONArray;
|
import com.alibaba.fastjson2.JSONArray;
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
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.capability.annotation.InjectCapability;
|
||||||
import work.slhaf.partner.api.agent.factory.module.annotation.AgentModule;
|
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.factory.module.annotation.InjectModule;
|
||||||
import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.ActivateModel;
|
import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.ActivateModel;
|
||||||
import work.slhaf.partner.core.action.ActionCapability;
|
import work.slhaf.partner.core.action.ActionCapability;
|
||||||
import work.slhaf.partner.core.action.entity.ActionData;
|
import work.slhaf.partner.core.action.ActionCore;
|
||||||
import work.slhaf.partner.core.action.entity.PhaserRecord;
|
|
||||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||||
import work.slhaf.partner.core.memory.MemoryCapability;
|
import work.slhaf.partner.core.memory.MemoryCapability;
|
||||||
import work.slhaf.partner.module.common.module.PreRunningModule;
|
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.EvaluatorInput;
|
||||||
import work.slhaf.partner.module.modules.action.interventor.evaluator.entity.EvaluatorResult;
|
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.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.InterventionRecognizer;
|
||||||
import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.RecognizerInput;
|
import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.RecognizerInput;
|
||||||
import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.RecognizerResult;
|
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.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Supplier;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,8 +39,6 @@ public class ActionInterventor extends PreRunningModule implements ActivateModel
|
|||||||
private InterventionRecognizer interventionRecognizer;
|
private InterventionRecognizer interventionRecognizer;
|
||||||
@InjectModule
|
@InjectModule
|
||||||
private InterventionEvaluator interventionEvaluator;
|
private InterventionEvaluator interventionEvaluator;
|
||||||
@InjectModule
|
|
||||||
private InterventionHandler interventionHandler;
|
|
||||||
|
|
||||||
@InjectCapability
|
@InjectCapability
|
||||||
private ActionCapability actionCapability;
|
private ActionCapability actionCapability;
|
||||||
@@ -92,21 +84,33 @@ public class ActionInterventor extends PreRunningModule implements ActivateModel
|
|||||||
invalidActionKeysFilter(executingDataList);
|
invalidActionKeysFilter(executingDataList);
|
||||||
invalidActionKeysFilter(preparedDataList);
|
invalidActionKeysFilter(preparedDataList);
|
||||||
|
|
||||||
// 同步写入prompt,异步处理干预行为,‘异步’在 interventionHandler 中体现
|
// 同步写入prompt,异步处理干预行为,‘异步’在处理流程中体现
|
||||||
promptHelper.setupInterventionPrompt(uuid, executingDataList, preparedDataList);
|
promptHelper.setupInterventionPrompt(uuid, executingDataList, preparedDataList);
|
||||||
interventionHandler.execute(assemblyHelper.buildHandlerInput(executingDataList, preparedDataList, recognizerResult));
|
handleInterventions(executingDataList, recognizerResult.getExecutingInterventions());
|
||||||
|
handleInterventions(preparedDataList, recognizerResult.getPreparedInterventions());
|
||||||
} else {
|
} else {
|
||||||
promptHelper.setupInterventionIgnoredPrompt(uuid, executingDataList, preparedDataList);
|
promptHelper.setupInterventionIgnoredPrompt(uuid, executingDataList, preparedDataList);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleInterventions(List<EvaluatedInterventionData> interventionDataList, Map<String, ?> 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<EvaluatedInterventionData> interventions) {
|
private void invalidActionKeysFilter(List<EvaluatedInterventionData> interventions) {
|
||||||
|
|
||||||
List<EvaluatedInterventionData> toRemove = new ArrayList<>();
|
List<EvaluatedInterventionData> toRemove = new ArrayList<>();
|
||||||
|
|
||||||
for (EvaluatedInterventionData intervention : interventions) {
|
for (EvaluatedInterventionData intervention : interventions) {
|
||||||
List<MetaIntervention> interventionData = intervention.getInterventionData();
|
List<MetaIntervention> interventionData = intervention.getMetaInterventionList();
|
||||||
List<String> actions = new ArrayList<>();
|
List<String> actions = new ArrayList<>();
|
||||||
for (MetaIntervention metaData : interventionData) {
|
for (MetaIntervention metaData : interventionData) {
|
||||||
actions.addAll(metaData.getActions());
|
actions.addAll(metaData.getActions());
|
||||||
@@ -163,64 +167,6 @@ public class ActionInterventor extends PreRunningModule implements ActivateModel
|
|||||||
private AssemblyHelper() {
|
private AssemblyHelper() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param executingDataList 对应评估结果中的‘执行中行动’
|
|
||||||
* @param preparedDataList 对应评估结果中的‘待执行行动’
|
|
||||||
* @param recognizerResult 干预识别结果,包含‘执行中’‘待执行’两类评估结果各自对应的行动数据
|
|
||||||
* @return 处理器输入
|
|
||||||
*/
|
|
||||||
private HandlerInput buildHandlerInput(List<EvaluatedInterventionData> executingDataList,
|
|
||||||
List<EvaluatedInterventionData> preparedDataList, RecognizerResult recognizerResult) {
|
|
||||||
HandlerInput input = new HandlerInput();
|
|
||||||
Map<String, PhaserRecord> executingInterventions = recognizerResult.getExecutingInterventions();
|
|
||||||
Map<String, ActionData> preparedInterventions = recognizerResult.getPreparedInterventions();
|
|
||||||
|
|
||||||
List<ExecutingInterventionData> executing = setupInputDataList(executingDataList, executingInterventions,
|
|
||||||
ExecutingInterventionData::new);
|
|
||||||
List<PreparedInterventionData> prepared = setupInputDataList(preparedDataList, preparedInterventions,
|
|
||||||
PreparedInterventionData::new);
|
|
||||||
|
|
||||||
input.setExecuting(executing);
|
|
||||||
input.setPrepared(prepared);
|
|
||||||
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param <I> HandlerInput 中 List 对应的泛型
|
|
||||||
* @param evaluatedDataList 评估结果列表
|
|
||||||
* @param interventionMap 干预识别结果中的 tendency:data 映射
|
|
||||||
* @param factory 输入类型构建工厂
|
|
||||||
* @return 处理器输入(干预列表)
|
|
||||||
*/
|
|
||||||
private <I> List<I> setupInputDataList(List<EvaluatedInterventionData> evaluatedDataList,
|
|
||||||
Map<String, ?> interventionMap, Supplier<I> factory) {
|
|
||||||
|
|
||||||
List<I> 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) {
|
private RecognizerInput buildRecognizerInput(String userId, String input) {
|
||||||
RecognizerInput recognizerInput = new RecognizerInput();
|
RecognizerInput recognizerInput = new RecognizerInput();
|
||||||
recognizerInput.setInput(input);
|
recognizerInput.setInput(input);
|
||||||
@@ -279,7 +225,7 @@ public class ActionInterventor extends PreRunningModule implements ActivateModel
|
|||||||
newElement.put("[干预倾向]", tendency);
|
newElement.put("[干预倾向]", tendency);
|
||||||
JSONArray changes = newElement.putArray("[行动链变动情况]");
|
JSONArray changes = newElement.putArray("[行动链变动情况]");
|
||||||
|
|
||||||
for (MetaIntervention intervention : data.getInterventionData()) {
|
for (MetaIntervention intervention : data.getMetaInterventionList()) {
|
||||||
JSONObject change = changes.addObject();
|
JSONObject change = changes.addObject();
|
||||||
change.put("[干预类型]", intervention.getType());
|
change.put("[干预类型]", intervention.getType());
|
||||||
change.put("[干预序号]", intervention.getOrder());
|
change.put("[干预序号]", intervention.getOrder());
|
||||||
|
|||||||
@@ -28,6 +28,6 @@ public class EvaluatorResult {
|
|||||||
* 描述信息(包括通过、失败原因)
|
* 描述信息(包括通过、失败原因)
|
||||||
*/
|
*/
|
||||||
private String description;
|
private String description;
|
||||||
private List<MetaIntervention> interventionData;
|
private List<MetaIntervention> metaInterventionList;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<HandlerInput, Void> {
|
|
||||||
|
|
||||||
@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<? extends InterventionData> executing, ExecutorService executor) {
|
|
||||||
executor.execute(() -> {
|
|
||||||
executing.forEach(interventionData -> {
|
|
||||||
// 干预逻辑一致
|
|
||||||
// 同步操作不同
|
|
||||||
// HandlerAction 抽取同步逻辑
|
|
||||||
// 此处进行遍历 intervention
|
|
||||||
// 根据Intervention类型进行分发
|
|
||||||
|
|
||||||
List<MetaIntervention> interventions = interventionData.getInterventions();
|
|
||||||
if (interventionData instanceof ExecutingInterventionData data) {
|
|
||||||
actionCapability.handleInterventions(interventions, data.getRecord());
|
|
||||||
} else if (interventionData instanceof PreparedInterventionData data) {
|
|
||||||
actionCapability.handleInterventions(interventions, data.getActionData());
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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<ExecutingInterventionData> executing;
|
|
||||||
private List<PreparedInterventionData> 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<MetaIntervention> interventions;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user