mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(action): support recover schedulable executable actions together, and ActionExecutor will emit a context block to explain recovering result
This commit is contained in:
@@ -17,6 +17,7 @@ import java.util.*;
|
|||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class ActionExecutor extends AbstractAgentModule.Standalone {
|
public class ActionExecutor extends AbstractAgentModule.Standalone {
|
||||||
|
|
||||||
@@ -49,8 +50,14 @@ public class ActionExecutor extends AbstractAgentModule.Standalone {
|
|||||||
runnerClient = actionCapability.runnerClient();
|
runnerClient = actionCapability.runnerClient();
|
||||||
blockManager = new ExecutingActionBlockManager(cognitionCapability.contextWorkspace());
|
blockManager = new ExecutingActionBlockManager(cognitionCapability.contextWorkspace());
|
||||||
|
|
||||||
actionCapability.listActions(Action.Status.EXECUTING, null)
|
Set<ExecutableAction> recoveredActions = new HashSet<>();
|
||||||
.forEach(this::execute);
|
recoveredActions.addAll(actionCapability.listActions(Action.Status.EXECUTING, null));
|
||||||
|
recoveredActions.addAll(actionCapability.listActions(Action.Status.INTERRUPTED, null).stream().map(executableAction -> {
|
||||||
|
executableAction.setStatus(Action.Status.EXECUTING);
|
||||||
|
return executableAction;
|
||||||
|
}).collect(Collectors.toSet()));
|
||||||
|
recoveredActions.forEach(this::execute);
|
||||||
|
blockManager.emitActionRecoveredBlock(recoveredActions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Action action) {
|
public void execute(Action action) {
|
||||||
|
|||||||
@@ -28,6 +28,64 @@ class ExecutingActionBlockManager {
|
|||||||
this.contextWorkspace = contextWorkspace;
|
this.contextWorkspace = contextWorkspace;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void emitActionRecoveredBlock(Set<ExecutableAction> recoveredActions) {
|
||||||
|
Set<ExecutableActionSnapshot> snapshots = recoveredActions.stream().map(ExecutableAction::snapshot).collect(Collectors.toSet());
|
||||||
|
|
||||||
|
String blockName = "actions_recovered";
|
||||||
|
String emittedAt = emittedAt();
|
||||||
|
String event = "actions_recovered";
|
||||||
|
|
||||||
|
contextWorkspace.register(new ContextBlock(
|
||||||
|
buildExecutingActionRecoveredFullBlock(snapshots, blockName, emittedAt, event),
|
||||||
|
buildExecutingActionRecoveredCompactBlock(snapshots, blockName, emittedAt, event),
|
||||||
|
buildExecutingActionRecoveredAbstractBlock(snapshots, blockName, event),
|
||||||
|
Set.of(ContextBlock.VisibleDomain.ACTION),
|
||||||
|
100,
|
||||||
|
12,
|
||||||
|
1
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
private @NotNull BlockContent buildExecutingActionRecoveredAbstractBlock(Set<ExecutableActionSnapshot> recoveredExecutingActions, String blockName, String event) {
|
||||||
|
return new ActionBlockContent(blockName, SOURCE) {
|
||||||
|
@Override
|
||||||
|
protected void fillXml(@NotNull Document document, @NotNull Element root) {
|
||||||
|
appendEventElement(document, root, event);
|
||||||
|
appendTextElement(document, root, "abstract", recoveredExecutingActions.size() + " executing actions recovered.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private @NotNull BlockContent buildExecutingActionRecoveredCompactBlock(Set<ExecutableActionSnapshot> recoveredExecutingActions, String blockName, String emittedAt, String event) {
|
||||||
|
return new ActionBlockContent(blockName, SOURCE) {
|
||||||
|
@Override
|
||||||
|
protected void fillXml(@NotNull Document document, @NotNull Element root) {
|
||||||
|
appendEventElement(document, root, event);
|
||||||
|
appendTextElement(document, root, "emitted_at", emittedAt);
|
||||||
|
appendListElement(document, root, "recovered_actions", "action", recoveredExecutingActions, (actionElement, action) -> {
|
||||||
|
appendTextElement(document, actionElement, "description", action.getDescription());
|
||||||
|
return Unit.INSTANCE;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private @NotNull BlockContent buildExecutingActionRecoveredFullBlock(Set<ExecutableActionSnapshot> recoveredExecutingActions, String blockName, String emittedAt, String event) {
|
||||||
|
return new ActionBlockContent(blockName, SOURCE) {
|
||||||
|
@Override
|
||||||
|
protected void fillXml(@NotNull Document document, @NotNull Element root) {
|
||||||
|
appendEventElement(document, root, event);
|
||||||
|
appendTextElement(document, root, "emitted_at", emittedAt);
|
||||||
|
appendListElement(document, root, "recovered_actions", "action", recoveredExecutingActions, (actionElement, action) -> {
|
||||||
|
appendTextElement(document, actionElement, "description", action.getDescription());
|
||||||
|
appendTextElement(document, actionElement, "source", action.getSource());
|
||||||
|
appendTextElement(document, actionElement, "executing_stage", action.getExecutingStage());
|
||||||
|
return Unit.INSTANCE;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
void emitStateActionTriggeredBlock(StateAction stateAction) {
|
void emitStateActionTriggeredBlock(StateAction stateAction) {
|
||||||
StateActionSnapshot snapshot = stateAction.snapshot();
|
StateActionSnapshot snapshot = stateAction.snapshot();
|
||||||
|
|
||||||
@@ -351,7 +409,6 @@ class ExecutingActionBlockManager {
|
|||||||
return "executing_action-" + actionId;
|
return "executing_action-" + actionId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static abstract class ActionBlockContent extends BlockContent {
|
private static abstract class ActionBlockContent extends BlockContent {
|
||||||
|
|
||||||
private ActionBlockContent(@NotNull String blockName, @NotNull String source, @NotNull Urgency urgency) {
|
private ActionBlockContent(@NotNull String blockName, @NotNull String source, @NotNull Urgency urgency) {
|
||||||
|
|||||||
Reference in New Issue
Block a user