refactor(action): simplify ActionCorrectionRecognizer input and build messages with context/task block

This commit is contained in:
2026-03-27 15:01:45 +08:00
parent dbfd0b1fc3
commit d806693e08
3 changed files with 43 additions and 62 deletions

View File

@@ -1,10 +1,16 @@
package work.slhaf.partner.module.action.executor; package work.slhaf.partner.module.action.executor;
import com.alibaba.fastjson2.JSONObject; import kotlin.Unit;
import lombok.val; 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.AbstractAgentModule;
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel; import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
import work.slhaf.partner.api.chat.pojo.Message; 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.CorrectionRecognizerInput;
import work.slhaf.partner.module.action.executor.entity.CorrectionRecognizerResult; import work.slhaf.partner.module.action.executor.entity.CorrectionRecognizerResult;
@@ -14,36 +20,45 @@ import java.util.List;
* 负责在行动链执行过程中判断当前进度是否异常,是否需要引入 corrector 介入。 * 负责在行动链执行过程中判断当前进度是否异常,是否需要引入 corrector 介入。
*/ */
public class ActionCorrectionRecognizer extends AbstractAgentModule.Sub<CorrectionRecognizerInput, CorrectionRecognizerResult> implements ActivateModel { public class ActionCorrectionRecognizer extends AbstractAgentModule.Sub<CorrectionRecognizerInput, CorrectionRecognizerResult> implements ActivateModel {
@InjectCapability
private CognitionCapability cognitionCapability;
@Override @Override
public CorrectionRecognizerResult execute(CorrectionRecognizerInput input) { public CorrectionRecognizerResult execute(CorrectionRecognizerInput input) {
val prompt = buildPrompt(input); List<Message> messages = List.of(
return formattedChat(List.of(new Message(Message.Character.USER, prompt)), CorrectionRecognizerResult.class); resolveContextMessage(),
resolveTaskMessage(input)
);
return formattedChat(messages, CorrectionRecognizerResult.class);
} }
private String buildPrompt(CorrectionRecognizerInput input) { private Message resolveTaskMessage(CorrectionRecognizerInput input) {
val prompt = new JSONObject(); return new TaskBlock() {
prompt.put("[行动来源]", input.getSource()); @Override
prompt.put("[行动倾向]", input.getTendency()); protected void fillXml(@NotNull Document document, @NotNull Element root) {
prompt.put("[行动描述]", input.getDescription()); appendChildElement(document, root, "executable_action_info", block -> {
prompt.put("[行动原因]", input.getReason()); appendTextElement(document, block, "executing_action_id", input.getActionId());
prompt.put("[当前阶段]", input.getCurrentStage()); appendTextElement(document, block, "original_tendency", input.getTendency());
prompt.put("[当前阶段位置]", input.getCurrentStageIndex()); appendTextElement(document, block, "evaluation_passed_reason", input.getReason());
prompt.put("[是否最后阶段]", input.isLastStage()); appendTextElement(document, block, "description", input.getDescription());
val stageList = prompt.putArray("[当前行动链阶段列表]"); appendTextElement(document, block, "from_who", input.getSource());
stageList.addAll(input.getOrderedStages()); return Unit.INSTANCE;
val history = prompt.putArray("[当前阶段已执行情况]"); });
if (input.getHistory() != null) {
history.addAll(input.getHistory());
} }
val metaActions = prompt.putArray("[当前阶段行动结果]"); }.encodeToMessage();
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 resolveContextMessage() {
return cognitionCapability.contextWorkspace().resolve(List.of(
ContextBlock.VisibleDomain.ACTION,
ContextBlock.VisibleDomain.COGNITION,
ContextBlock.VisibleDomain.MEMORY
)).encodeToMessage();
}
@NotNull
@Override @Override
public String modelKey() { public String modelKey() {
return "action_correction_recognizer"; return "action_correction_recognizer";

View File

@@ -487,33 +487,12 @@ public class ActionExecutor extends AbstractAgentModule.Standalone {
} }
private CorrectionRecognizerInput buildRecognizerInput(ExecutableAction executableAction) { 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() return CorrectionRecognizerInput.builder()
.tendency(executableAction.getTendency()) .tendency(executableAction.getTendency())
.source(executableAction.getSource()) .source(executableAction.getSource())
.reason(executableAction.getReason()) .reason(executableAction.getReason())
.description(executableAction.getDescription()) .description(executableAction.getDescription())
.history(executableAction.getHistory().get(executableAction.getExecutingStage())) .actionId(executableAction.getUuid())
.currentStageMetaActions(currentStageMetaActions)
.orderedStages(orderedStages)
.currentStage(executableAction.getExecutingStage())
.currentStageIndex(currentStageIndex)
.lastStage(currentStageIndex == orderedStages.size() - 1)
.recentMessages(cognitionCapability.getChatMessages())
.activatedSlices(memoryCapability.getActivatedSlices())
.build(); .build();
} }
} }

View File

@@ -2,10 +2,6 @@ package work.slhaf.partner.module.action.executor.entity;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import work.slhaf.partner.api.chat.pojo.Message;
import work.slhaf.partner.core.memory.pojo.ActivatedMemorySlice;
import java.util.List;
@Data @Data
@Builder @Builder
@@ -14,14 +10,5 @@ public class CorrectionRecognizerInput {
private String source; private String source;
private String reason; private String reason;
private String description; private String description;
private String actionId;
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;
} }