refactor(ActionExecutor): use HistoryAction record and track scheduled history reset

This commit is contained in:
2026-02-07 15:35:34 +08:00
parent 050c39cbc7
commit 0eb4765235
5 changed files with 43 additions and 24 deletions

View File

@@ -2,6 +2,12 @@ package work.slhaf.partner.core.action.entity;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import work.slhaf.partner.module.modules.action.dispatcher.executor.entity.HistoryAction;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/** /**
* 计划行动数据类,继承自{@link ActionData},扩展了属性{@link ScheduledActionData#type}和{@link ScheduledActionData#scheduleContent},用于标识计划类型(单次还是周期性任务)和计划内容 * 计划行动数据类,继承自{@link ActionData},扩展了属性{@link ScheduledActionData#type}和{@link ScheduledActionData#scheduleContent},用于标识计划类型(单次还是周期性任务)和计划内容
@@ -10,9 +16,29 @@ import lombok.EqualsAndHashCode;
@Data @Data
public class ScheduledActionData extends ActionData { public class ScheduledActionData extends ActionData {
private ScheduledType type; private ScheduledType type;
private String scheduleContent; //如果为周期则对应cron表达式如果为一次性则对应为LocalDateTime字符串 private String scheduleContent; //如果为周期则对应cron表达式如果为一次性则对应为ZonedDateTime字符串
private List<ScheduledHistory> scheduledHistories = new ArrayList<>();
enum ScheduledType { public void recordAndReset() {
this.scheduledHistories.add(new ScheduledHistory(ZonedDateTime.now(), this.result, Map.copyOf(this.history)));
// 清理执行时内容
this.additionalContext.clear();
this.executingStage = 0;
this.getActionChain().forEach((stage, actions) -> {
for (MetaAction action : actions) {
action.getParams().clear();
action.setResult(new MetaAction.Result());
}
});
this.setStatus(ActionStatus.PREPARE);
}
public enum ScheduledType {
CYCLE, ONCE CYCLE, ONCE
} }
private record ScheduledHistory(ZonedDateTime endTime, String result, Map<Integer, List<HistoryAction>> history) {
}
} }

View File

@@ -9,11 +9,8 @@ import work.slhaf.partner.api.agent.factory.module.annotation.InjectModule;
import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.AgentRunningSubModule; import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.AgentRunningSubModule;
import work.slhaf.partner.core.action.ActionCapability; import work.slhaf.partner.core.action.ActionCapability;
import work.slhaf.partner.core.action.ActionCore; import work.slhaf.partner.core.action.ActionCore;
import work.slhaf.partner.core.action.entity.ActionData; import work.slhaf.partner.core.action.entity.*;
import work.slhaf.partner.core.action.entity.ActionData.ActionStatus; import work.slhaf.partner.core.action.entity.ActionData.ActionStatus;
import work.slhaf.partner.core.action.entity.MetaAction;
import work.slhaf.partner.core.action.entity.MetaActionInfo;
import work.slhaf.partner.core.action.entity.PhaserRecord;
import work.slhaf.partner.core.action.runner.RunnerClient; import work.slhaf.partner.core.action.runner.RunnerClient;
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;
@@ -157,8 +154,13 @@ public class ActionExecutor extends AgentRunningSubModule<ActionExecutorInput, V
// 结束 // 结束
actionCapability.removePhaserRecord(phaser); actionCapability.removePhaserRecord(phaser);
if (actionData.getStatus() != ActionData.ActionStatus.FAILED) { if (actionData.getStatus() != ActionData.ActionStatus.FAILED) {
// 如果是 ScheduledActionData, 则重置 ActionData 内容,记录执行历史与最终结果
if (actionData instanceof ScheduledActionData scheduledActionData) {
scheduledActionData.recordAndReset();
} else {
actionData.setStatus(ActionStatus.SUCCESS); actionData.setStatus(ActionStatus.SUCCESS);
} }
}
}); });
} }
@@ -233,10 +235,7 @@ public class ActionExecutor extends AgentRunningSubModule<ActionExecutorInput, V
if (extractorResult.isOk()) { if (extractorResult.isOk()) {
metaAction.setParams(extractorResult.getParams()); metaAction.setParams(extractorResult.getParams());
runnerClient.submit(metaAction); runnerClient.submit(metaAction);
val historyAction = new HistoryAction(); val historyAction = new HistoryAction(actionKey, actionCapability.loadMetaActionInfo(actionKey).getDescription(), metaAction.getResult().getData());
historyAction.setActionKey(actionKey);
historyAction.setDescription(actionCapability.loadMetaActionInfo(actionKey).getDescription());
historyAction.setResult(metaAction.getResult().getData());
actionData.getHistory() actionData.getHistory()
.computeIfAbsent(executingStage, integer -> new ArrayList<>()) .computeIfAbsent(executingStage, integer -> new ArrayList<>())
.add(historyAction); .add(historyAction);

View File

@@ -209,9 +209,9 @@ public class ActionRepairer extends AgentRunningSubModule<RepairerInput, Repaire
JSONArray historyData = prompt.putArray("[历史行动执行结果]"); JSONArray historyData = prompt.putArray("[历史行动执行结果]");
data.getHistoryActionResults().forEach(historyAction -> { data.getHistoryActionResults().forEach(historyAction -> {
JSONObject historyItem = new JSONObject(); JSONObject historyItem = new JSONObject();
historyItem.put("[行动Key]", historyAction.getActionKey()); historyItem.put("[行动Key]", historyAction.actionKey());
historyItem.put("[行动描述]", historyAction.getDescription()); historyItem.put("[行动描述]", historyAction.description());
historyItem.put("[行动结果]", historyAction.getResult()); historyItem.put("[行动结果]", historyAction.result());
historyData.add(historyItem); historyData.add(historyItem);
}); });

View File

@@ -50,9 +50,9 @@ public class ParamsExtractor extends AgentRunningSubModule<ExtractorInput, Extra
List<HistoryAction> historyActions = input.getHistoryActionResults(); List<HistoryAction> historyActions = input.getHistoryActionResults();
for (HistoryAction historyAction : historyActions) { for (HistoryAction historyAction : historyActions) {
JSONObject historyItem = new JSONObject(); JSONObject historyItem = new JSONObject();
historyItem.put("[行动Key]", historyAction.getActionKey()); historyItem.put("[行动Key]", historyAction.actionKey());
historyItem.put("[行动描述]", historyAction.getDescription()); historyItem.put("[行动描述]", historyAction.description());
historyItem.put("[行动结果]", historyAction.getResult()); historyItem.put("[行动结果]", historyAction.result());
historyData.add(historyItem); historyData.add(historyItem);
} }

View File

@@ -1,10 +1,4 @@
package work.slhaf.partner.module.modules.action.dispatcher.executor.entity; package work.slhaf.partner.module.modules.action.dispatcher.executor.entity;
import lombok.Data; public record HistoryAction(String actionKey, String description, String result) {
@Data
public class HistoryAction {
private String actionKey;
private String description;
private String result;
} }