mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(action): simplify corrector 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.CorrectorInput;
|
||||
import work.slhaf.partner.module.action.executor.entity.CorrectorResult;
|
||||
|
||||
@@ -14,27 +20,55 @@ import java.util.List;
|
||||
* 负责在单组行动执行后,根据行动意图与结果检查后续行动是否符合目的,必要时直接调整行动链,或发起自对话请求进行干预
|
||||
*/
|
||||
public class ActionCorrector extends AbstractAgentModule.Sub<CorrectorInput, CorrectorResult> implements ActivateModel {
|
||||
|
||||
@InjectCapability
|
||||
private CognitionCapability cognitionCapability;
|
||||
|
||||
@Override
|
||||
public CorrectorResult execute(CorrectorInput input) {
|
||||
val prompt = buildPrompt(input);
|
||||
return formattedChat(List.of(new Message(Message.Character.USER, prompt)), CorrectorResult.class);
|
||||
List<Message> messages = List.of(
|
||||
resolveContextMessage(),
|
||||
resolveTaskMessage(input)
|
||||
);
|
||||
return formattedChat(messages, CorrectorResult.class);
|
||||
}
|
||||
|
||||
private String buildPrompt(CorrectorInput input) {
|
||||
val prompt = new JSONObject();
|
||||
prompt.put("[行动来源]", input.getSource());
|
||||
prompt.put("[行动倾向]", input.getTendency());
|
||||
prompt.put("[行动描述]", input.getDescription());
|
||||
prompt.put("[行动原因]", input.getReason());
|
||||
val messages = prompt.putArray("[近期对话]");
|
||||
messages.addAll(input.getRecentMessages());
|
||||
val memory = prompt.putArray("[已激活记忆]");
|
||||
memory.addAll(input.getActivatedSlices());
|
||||
val history = prompt.putArray("[已执行情况]");
|
||||
history.addAll(input.getHistory());
|
||||
return prompt.toJSONString();
|
||||
private Message resolveTaskMessage(CorrectorInput 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;
|
||||
});
|
||||
|
||||
appendListElement(document, root, "current_action_chain_overview", "action_chain_stage", input.getActionChainOverview().entrySet(), (stageElement, stageData) -> {
|
||||
stageElement.setAttribute("stage_count", String.valueOf(stageData.getKey()));
|
||||
appendRepeatedElements(document, stageElement, "meta_action", stageData.getValue(), (metaActionElement, metaActionData) -> {
|
||||
appendTextElement(document, metaActionElement, "action_key", metaActionData.getActionKey());
|
||||
appendTextElement(document, metaActionElement, "description", metaActionData.getDescription());
|
||||
appendTextElement(document, metaActionElement, "status", metaActionData.getStatus());
|
||||
return Unit.INSTANCE;
|
||||
});
|
||||
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_corrector";
|
||||
|
||||
@@ -14,8 +14,7 @@ import work.slhaf.partner.core.cognition.CognitionCapability;
|
||||
import work.slhaf.partner.core.memory.MemoryCapability;
|
||||
import work.slhaf.partner.module.action.executor.entity.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@@ -466,15 +465,24 @@ public class ActionExecutor extends AbstractAgentModule.Standalone {
|
||||
}
|
||||
|
||||
private CorrectorInput buildCorrectorInput(ExecutableAction executableAction) {
|
||||
Map<Integer, List<CorrectorInput.ActionChainItem>> overview = new LinkedHashMap<>();
|
||||
executableAction.getActionChain().forEach((stage, list) -> {
|
||||
List<CorrectorInput.ActionChainItem> overviewItems = list.stream()
|
||||
.map(metaAction -> new CorrectorInput.ActionChainItem(
|
||||
metaAction.getKey(),
|
||||
actionCapability.loadMetaActionInfo(metaAction.getKey()).getDescription(),
|
||||
metaAction.getResult().getStatus().name().toLowerCase(Locale.ROOT)
|
||||
))
|
||||
.toList();
|
||||
overview.put(stage, overviewItems);
|
||||
});
|
||||
return CorrectorInput.builder()
|
||||
.tendency(executableAction.getTendency())
|
||||
.source(executableAction.getSource())
|
||||
.reason(executableAction.getReason())
|
||||
.description(executableAction.getDescription())
|
||||
.history(executableAction.getHistory().get(executableAction.getExecutingStage()))
|
||||
.status(executableAction.getStatus())
|
||||
.recentMessages(cognitionCapability.getChatMessages())
|
||||
.activatedSlices(memoryCapability.getActivatedSlices())
|
||||
.actionId(executableAction.getUuid())
|
||||
.actionChainOverview(overview)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
package work.slhaf.partner.module.action.executor.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.entity.ExecutableAction;
|
||||
import work.slhaf.partner.core.memory.pojo.ActivatedMemorySlice;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@@ -15,10 +14,15 @@ public class CorrectorInput {
|
||||
private String source;
|
||||
private String reason;
|
||||
private String description;
|
||||
private String actionId;
|
||||
|
||||
private List<HistoryAction> history;
|
||||
private ExecutableAction.Status status;
|
||||
private Map<Integer, List<ActionChainItem>> actionChainOverview;
|
||||
|
||||
private List<Message> recentMessages;
|
||||
private List<ActivatedMemorySlice> activatedSlices;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class ActionChainItem {
|
||||
private String actionKey;
|
||||
private String description;
|
||||
private String status;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user