mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(ActionInterventor): redefine DTOs in ActionInterventor to adapt the actual intervention logic
This commit is contained in:
@@ -10,6 +10,7 @@ import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.ActivateM
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore;
|
||||
import work.slhaf.partner.core.action.entity.ActionData;
|
||||
import work.slhaf.partner.core.action.entity.PhaserRecord;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.memory.MemoryCapability;
|
||||
import work.slhaf.partner.module.common.module.PreRunningModule;
|
||||
@@ -174,7 +175,7 @@ public class ActionInterventor extends PreRunningModule implements ActivateModel
|
||||
recognizerInput.setUserDialogMapStr(memoryCapability.getUserDialogMapStr(userId));
|
||||
// 参考的对话列表大小或需调整
|
||||
recognizerInput.setRecentMessages(cognationCapability.getChatMessages());
|
||||
recognizerInput.setExecutingActions(actionCapability.listPhaserRecords());
|
||||
recognizerInput.setExecutingActions(actionCapability.listPhaserRecords().stream().map(PhaserRecord::actionData).toList());
|
||||
recognizerInput.setPreparedActions(actionCapability.listPreparedAction(userId));
|
||||
return recognizerInput;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore.ExecutorType;
|
||||
import work.slhaf.partner.core.action.entity.ActionData;
|
||||
import work.slhaf.partner.core.action.entity.PhaserRecord;
|
||||
import work.slhaf.partner.core.memory.pojo.EvaluatedSlice;
|
||||
import work.slhaf.partner.module.modules.action.interventor.evaluator.entity.EvaluatorInput;
|
||||
import work.slhaf.partner.module.modules.action.interventor.evaluator.entity.EvaluatorResult;
|
||||
@@ -37,7 +36,7 @@ public class InterventionEvaluator extends AgentRunningSubModule<EvaluatorInput,
|
||||
public EvaluatorResult execute(EvaluatorInput input) {
|
||||
// 获取必须数据
|
||||
ExecutorService executor = actionCapability.getExecutor(ExecutorType.VIRTUAL);
|
||||
Map<String, PhaserRecord> executingInterventions = input.getExecutingInterventions();
|
||||
Map<String, ActionData> executingInterventions = input.getExecutingInterventions();
|
||||
Map<String, ActionData> preparedInterventions = input.getPreparedInterventions();
|
||||
CountDownLatch latch = new CountDownLatch(executingInterventions.size() + preparedInterventions.size());
|
||||
|
||||
@@ -59,30 +58,23 @@ public class InterventionEvaluator extends AgentRunningSubModule<EvaluatorInput,
|
||||
return result;
|
||||
}
|
||||
|
||||
private <T> void evaluateIntervention(List<EvaluatedInterventionData> evaluatedDataList, Map<String, T> interventionMap, EvaluatorInput input, ExecutorService executor, CountDownLatch latch) {
|
||||
interventionMap.forEach((tendency, data) -> {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
ActionData actionData = switch (data) {
|
||||
case PhaserRecord record -> record.actionData();
|
||||
case ActionData tempData -> tempData;
|
||||
default -> null;
|
||||
};
|
||||
String prompt = buildPrompt(input.getRecentMessages(), input.getActivatedSlices(), actionData, tendency);
|
||||
private void evaluateIntervention(List<EvaluatedInterventionData> evaluatedDataList, Map<String, ActionData> interventionMap, EvaluatorInput input, ExecutorService executor, CountDownLatch latch) {
|
||||
interventionMap.forEach((tendency, actionData) -> executor.execute(() -> {
|
||||
try {
|
||||
String prompt = buildPrompt(input.getRecentMessages(), input.getActivatedSlices(), actionData, tendency);
|
||||
|
||||
ChatResponse response = this.singleChat(prompt);
|
||||
EvaluatedInterventionData evaluatedData = JSONObject.parseObject(response.getMessage(),
|
||||
EvaluatedInterventionData.class);
|
||||
synchronized (evaluatedDataList) {
|
||||
evaluatedDataList.add(evaluatedData);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("干预意图评估出错: {}", tendency, e);
|
||||
} finally {
|
||||
latch.countDown();
|
||||
ChatResponse response = this.singleChat(prompt);
|
||||
EvaluatedInterventionData evaluatedData = JSONObject.parseObject(response.getMessage(),
|
||||
EvaluatedInterventionData.class);
|
||||
synchronized (evaluatedDataList) {
|
||||
evaluatedDataList.add(evaluatedData);
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("干预意图评估出错: {}", tendency, e);
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
private String buildPrompt(List<Message> recentMessages, List<EvaluatedSlice> activatedSlices,
|
||||
|
||||
@@ -3,7 +3,6 @@ package work.slhaf.partner.module.modules.action.interventor.evaluator.entity;
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.entity.ActionData;
|
||||
import work.slhaf.partner.core.action.entity.PhaserRecord;
|
||||
import work.slhaf.partner.core.memory.pojo.EvaluatedSlice;
|
||||
|
||||
import java.util.List;
|
||||
@@ -11,7 +10,7 @@ import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class EvaluatorInput {
|
||||
private Map<String, PhaserRecord> executingInterventions;
|
||||
private Map<String, ActionData> executingInterventions;
|
||||
private Map<String, ActionData> preparedInterventions;
|
||||
private List<EvaluatedSlice> activatedSlices;
|
||||
private List<Message> recentMessages;
|
||||
|
||||
@@ -10,7 +10,6 @@ import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore;
|
||||
import work.slhaf.partner.core.action.entity.ActionData;
|
||||
import work.slhaf.partner.core.action.entity.PhaserRecord;
|
||||
import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.MetaRecognizerResult;
|
||||
import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.RecognizerInput;
|
||||
import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.RecognizerResult;
|
||||
@@ -31,13 +30,13 @@ public class InterventionRecognizer extends AgentRunningSubModule<RecognizerInpu
|
||||
public RecognizerResult execute(RecognizerInput input) {
|
||||
// 获取必须数据
|
||||
ExecutorService executor = actionCapability.getExecutor(ActionCore.ExecutorType.VIRTUAL);
|
||||
List<PhaserRecord> executingActions = input.getExecutingActions();
|
||||
List<ActionData> executingActions = input.getExecutingActions();
|
||||
List<ActionData> preparedActions = input.getPreparedActions();
|
||||
CountDownLatch countDownLatch = new CountDownLatch(executingActions.size() + preparedActions.size());
|
||||
|
||||
// 创建结果容器
|
||||
RecognizerResult recognizerResult = new RecognizerResult();
|
||||
Map<String, PhaserRecord> executingInterventions = recognizerResult.getExecutingInterventions();
|
||||
Map<String, ActionData> executingInterventions = recognizerResult.getExecutingInterventions();
|
||||
Map<String, ActionData> preparedInterventions = recognizerResult.getPreparedInterventions();
|
||||
|
||||
// 执行识别操作
|
||||
@@ -52,8 +51,8 @@ public class InterventionRecognizer extends AgentRunningSubModule<RecognizerInpu
|
||||
return recognizerResult;
|
||||
}
|
||||
|
||||
private <T> void recognizeIntervention(Map<String, T> interventionsMap, List<T> actions, ExecutorService executor, RecognizerInput input, CountDownLatch latch) {
|
||||
for (T data : actions) {
|
||||
private void recognizeIntervention(Map<String, ActionData> interventionsMap, List<ActionData> actions, ExecutorService executor, RecognizerInput input, CountDownLatch latch) {
|
||||
for (ActionData data : actions) {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
String prompt = buildPrompt(data, input);
|
||||
@@ -73,15 +72,7 @@ public class InterventionRecognizer extends AgentRunningSubModule<RecognizerInpu
|
||||
}
|
||||
}
|
||||
|
||||
private <T> String buildPrompt(T data, RecognizerInput input) {
|
||||
ActionData actionData = switch (data) {
|
||||
case PhaserRecord record -> record.actionData();
|
||||
case ActionData tempData -> tempData;
|
||||
default -> null;
|
||||
};
|
||||
if (actionData == null) {
|
||||
return null;
|
||||
}
|
||||
private String buildPrompt(ActionData actionData, RecognizerInput input) {
|
||||
JSONObject json = new JSONObject();
|
||||
|
||||
JSONObject actionInfo = json.putObject("行动信息");
|
||||
|
||||
@@ -3,7 +3,6 @@ package work.slhaf.partner.module.modules.action.interventor.recognizer.entity;
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.entity.ActionData;
|
||||
import work.slhaf.partner.core.action.entity.PhaserRecord;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -18,6 +17,6 @@ public class RecognizerInput {
|
||||
/**
|
||||
* 正在执行的行动-Phaser记录列表,在Recognizer中结合本次输入并发评估(考虑到不同行动链之间对LLM的影响)
|
||||
*/
|
||||
private List<PhaserRecord> executingActions;
|
||||
private List<ActionData> executingActions;
|
||||
private List<ActionData> preparedActions;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package work.slhaf.partner.module.modules.action.interventor.recognizer.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.core.action.entity.ActionData;
|
||||
import work.slhaf.partner.core.action.entity.PhaserRecord;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -16,9 +15,9 @@ public class RecognizerResult {
|
||||
* <h4>将被干预的‘执行中行动’</h4>
|
||||
* key: 干预倾向
|
||||
* <br/>
|
||||
* value: 干预倾向将作用的 phaser 记录
|
||||
* value: 干预倾向将作用的行动数据
|
||||
*/
|
||||
private Map<String, PhaserRecord> executingInterventions = new HashMap<>();
|
||||
private Map<String, ActionData> executingInterventions = new HashMap<>();
|
||||
|
||||
/**
|
||||
* <h4>将被干预的‘等待中行动’</h4>
|
||||
|
||||
Reference in New Issue
Block a user