mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(action): simplify ActionCorrectionRecognizer input and build messages with context/task block
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
package work.slhaf.partner.module.action.executor;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.val;
|
||||
import kotlin.Unit;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.cognition.ContextBlock;
|
||||
import work.slhaf.partner.module.TaskBlock;
|
||||
import work.slhaf.partner.module.action.executor.entity.CorrectionRecognizerInput;
|
||||
import work.slhaf.partner.module.action.executor.entity.CorrectionRecognizerResult;
|
||||
|
||||
@@ -14,36 +20,45 @@ import java.util.List;
|
||||
* 负责在行动链执行过程中判断当前进度是否异常,是否需要引入 corrector 介入。
|
||||
*/
|
||||
public class ActionCorrectionRecognizer extends AbstractAgentModule.Sub<CorrectionRecognizerInput, CorrectionRecognizerResult> implements ActivateModel {
|
||||
|
||||
@InjectCapability
|
||||
private CognitionCapability cognitionCapability;
|
||||
|
||||
@Override
|
||||
public CorrectionRecognizerResult execute(CorrectionRecognizerInput input) {
|
||||
val prompt = buildPrompt(input);
|
||||
return formattedChat(List.of(new Message(Message.Character.USER, prompt)), CorrectionRecognizerResult.class);
|
||||
List<Message> messages = List.of(
|
||||
resolveContextMessage(),
|
||||
resolveTaskMessage(input)
|
||||
);
|
||||
return formattedChat(messages, CorrectionRecognizerResult.class);
|
||||
}
|
||||
|
||||
private String buildPrompt(CorrectionRecognizerInput input) {
|
||||
val prompt = new JSONObject();
|
||||
prompt.put("[行动来源]", input.getSource());
|
||||
prompt.put("[行动倾向]", input.getTendency());
|
||||
prompt.put("[行动描述]", input.getDescription());
|
||||
prompt.put("[行动原因]", input.getReason());
|
||||
prompt.put("[当前阶段]", input.getCurrentStage());
|
||||
prompt.put("[当前阶段位置]", input.getCurrentStageIndex());
|
||||
prompt.put("[是否最后阶段]", input.isLastStage());
|
||||
val stageList = prompt.putArray("[当前行动链阶段列表]");
|
||||
stageList.addAll(input.getOrderedStages());
|
||||
val history = prompt.putArray("[当前阶段已执行情况]");
|
||||
if (input.getHistory() != null) {
|
||||
history.addAll(input.getHistory());
|
||||
}
|
||||
val metaActions = prompt.putArray("[当前阶段行动结果]");
|
||||
metaActions.addAll(input.getCurrentStageMetaActions());
|
||||
val messages = prompt.putArray("[近期对话]");
|
||||
messages.addAll(input.getRecentMessages());
|
||||
val memory = prompt.putArray("[已激活记忆]");
|
||||
memory.addAll(input.getActivatedSlices());
|
||||
return prompt.toJSONString();
|
||||
private Message resolveTaskMessage(CorrectionRecognizerInput input) {
|
||||
return new TaskBlock() {
|
||||
@Override
|
||||
protected void fillXml(@NotNull Document document, @NotNull Element root) {
|
||||
appendChildElement(document, root, "executable_action_info", block -> {
|
||||
appendTextElement(document, block, "executing_action_id", input.getActionId());
|
||||
appendTextElement(document, block, "original_tendency", input.getTendency());
|
||||
appendTextElement(document, block, "evaluation_passed_reason", input.getReason());
|
||||
appendTextElement(document, block, "description", input.getDescription());
|
||||
appendTextElement(document, block, "from_who", input.getSource());
|
||||
return Unit.INSTANCE;
|
||||
});
|
||||
|
||||
}
|
||||
}.encodeToMessage();
|
||||
}
|
||||
|
||||
private Message resolveContextMessage() {
|
||||
return cognitionCapability.contextWorkspace().resolve(List.of(
|
||||
ContextBlock.VisibleDomain.ACTION,
|
||||
ContextBlock.VisibleDomain.COGNITION,
|
||||
ContextBlock.VisibleDomain.MEMORY
|
||||
)).encodeToMessage();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String modelKey() {
|
||||
return "action_correction_recognizer";
|
||||
|
||||
@@ -487,33 +487,12 @@ public class ActionExecutor extends AbstractAgentModule.Standalone {
|
||||
}
|
||||
|
||||
private CorrectionRecognizerInput buildRecognizerInput(ExecutableAction executableAction) {
|
||||
val orderedStages = new ArrayList<>(executableAction.getActionChain().keySet());
|
||||
orderedStages.sort(Integer::compareTo);
|
||||
int currentStageIndex = orderedStages.indexOf(executableAction.getExecutingStage());
|
||||
List<CorrectionRecognizerMetaActionSnapshot> currentStageMetaActions = executableAction.getActionChain()
|
||||
.getOrDefault(executableAction.getExecutingStage(), List.of())
|
||||
.stream()
|
||||
.map(metaAction -> CorrectionRecognizerMetaActionSnapshot.builder()
|
||||
.key(metaAction.getKey())
|
||||
.name(metaAction.getName())
|
||||
.io(metaAction.getIo())
|
||||
.resultStatus(metaAction.getResult().getStatus().name())
|
||||
.resultData(metaAction.getResult().getData())
|
||||
.build())
|
||||
.toList();
|
||||
return CorrectionRecognizerInput.builder()
|
||||
.tendency(executableAction.getTendency())
|
||||
.source(executableAction.getSource())
|
||||
.reason(executableAction.getReason())
|
||||
.description(executableAction.getDescription())
|
||||
.history(executableAction.getHistory().get(executableAction.getExecutingStage()))
|
||||
.currentStageMetaActions(currentStageMetaActions)
|
||||
.orderedStages(orderedStages)
|
||||
.currentStage(executableAction.getExecutingStage())
|
||||
.currentStageIndex(currentStageIndex)
|
||||
.lastStage(currentStageIndex == orderedStages.size() - 1)
|
||||
.recentMessages(cognitionCapability.getChatMessages())
|
||||
.activatedSlices(memoryCapability.getActivatedSlices())
|
||||
.actionId(executableAction.getUuid())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,6 @@ package work.slhaf.partner.module.action.executor.entity;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.memory.pojo.ActivatedMemorySlice;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@@ -14,14 +10,5 @@ public class CorrectionRecognizerInput {
|
||||
private String source;
|
||||
private String reason;
|
||||
private String description;
|
||||
|
||||
private List<HistoryAction> history;
|
||||
private List<CorrectionRecognizerMetaActionSnapshot> currentStageMetaActions;
|
||||
private List<Integer> orderedStages;
|
||||
private int currentStage;
|
||||
private int currentStageIndex;
|
||||
private boolean lastStage;
|
||||
|
||||
private List<Message> recentMessages;
|
||||
private List<ActivatedMemorySlice> activatedSlices;
|
||||
private String actionId;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user