mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
调整 MetaAction 的执行方式,将交给 ActionCapability、SandBoxRunnerClient 执行
This commit is contained in:
2
.idea/encodings.xml
generated
2
.idea/encodings.xml
generated
@@ -3,11 +3,11 @@
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/Partner-Api/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Api/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Executor/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Main/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Main/src/main/java/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Main/src/main/java/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Main/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-SandboxRunner/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Test-Demo/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/Partner-Test-Demo/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
|
||||
@@ -45,4 +45,6 @@ public interface ActionCapability {
|
||||
MetaActionInfo loadMetaActionInfo(@NonNull String actionKey);
|
||||
|
||||
boolean checkExists(String... actionKeys);
|
||||
|
||||
void execute(MetaAction metaAction);
|
||||
}
|
||||
|
||||
@@ -55,6 +55,7 @@ public class ActionCore extends PartnerCore<ActionCore> {
|
||||
*/
|
||||
private final LinkedHashMap<String, MetaActionInfo> existedMetaActions = new LinkedHashMap<>();
|
||||
private final List<PhaserRecord> phaserRecords = new ArrayList<>();
|
||||
private final SandboxRunnerClient sandboxRunnerClient = new SandboxRunnerClient();
|
||||
|
||||
public ActionCore() throws IOException, ClassNotFoundException {
|
||||
new ActionWatchService(existedMetaActions, virtualExecutor).launch();
|
||||
@@ -241,6 +242,11 @@ public class ActionCore extends PartnerCore<ActionCore> {
|
||||
return existedMetaActions.keySet().containsAll(Arrays.asList(actionKeys));
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public void execute(MetaAction metaAction) {
|
||||
sandboxRunnerClient.run(metaAction);
|
||||
}
|
||||
|
||||
/**
|
||||
* 命中缓存且评估通过时
|
||||
*
|
||||
|
||||
@@ -17,6 +17,7 @@ import java.util.concurrent.ExecutorService;
|
||||
|
||||
import static work.slhaf.partner.common.Constant.Path.ACTION_PROGRAM;
|
||||
|
||||
// TODO 后续需迁移至 SandboxRunner,作为容器内的监听逻辑
|
||||
@SuppressWarnings("unchecked")
|
||||
@Slf4j
|
||||
class ActionWatchService {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package work.slhaf.partner.core.action;
|
||||
|
||||
import work.slhaf.partner.core.action.entity.MetaAction;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* 基于 Http 与 WebSocket 的沙盒执行器客户端,负责:
|
||||
* <ul>
|
||||
* <li>
|
||||
* 发送行动单元数据
|
||||
* </li>
|
||||
* <li>
|
||||
* 实时更新获取已存在行动列表
|
||||
* </li>
|
||||
* <li>
|
||||
* 向传入的 MetaAction 回写执行结果
|
||||
* </li>
|
||||
* </ul>
|
||||
*/
|
||||
class SandboxRunnerClient {
|
||||
|
||||
public SandboxRunnerClient() {
|
||||
// 连接沙盒执行器(websocket)
|
||||
}
|
||||
|
||||
public void run(MetaAction metaAction) {
|
||||
// 获取已存在行动列表
|
||||
Path path = metaAction.checkAndGetPath();
|
||||
if (!metaAction.getResult().isSuccess()) {
|
||||
return;
|
||||
}
|
||||
// 调用沙盒执行器
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package work.slhaf.partner.core.action.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import work.slhaf.partner.common.Constant;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
@@ -10,10 +9,10 @@ import java.nio.file.Path;
|
||||
import static work.slhaf.partner.common.Constant.Path.ACTION_PROGRAM;
|
||||
|
||||
/**
|
||||
* 行动链中的单一元素,实现{@link Runnable}接口,封装了调用外部行动程序的必要信息,可被执行
|
||||
* 行动链中的单一元素,封装了调用外部行动程序的必要信息与结果容器,可被{@link work.slhaf.partner.core.action.ActionCapability}执行
|
||||
*/
|
||||
@Data
|
||||
public class MetaAction implements Comparable<MetaAction>, Runnable {
|
||||
public class MetaAction implements Comparable<MetaAction> {
|
||||
|
||||
/**
|
||||
* 行动key,用于标识与定位行动程序
|
||||
@@ -40,48 +39,23 @@ public class MetaAction implements Comparable<MetaAction>, Runnable {
|
||||
*/
|
||||
private MetaActionType type;
|
||||
|
||||
@Override
|
||||
public int compareTo(@NotNull MetaAction metaAction) {
|
||||
return this.order - metaAction.order;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
File action = loadFromFile();
|
||||
public Path checkAndGetPath() {
|
||||
Path path = switch (type) {
|
||||
case PLUGIN -> Path.of(ACTION_PROGRAM, key, "action.jar");
|
||||
case SCRIPT -> Path.of(ACTION_PROGRAM, key, "action.py");
|
||||
case MCP -> Path.of(ACTION_PROGRAM, key, "action.json");
|
||||
};
|
||||
File action = path.toFile();
|
||||
if (!action.exists()) {
|
||||
result.setSuccess(false);
|
||||
result.setData("Action file not found: " + action.getAbsolutePath());
|
||||
}
|
||||
try {
|
||||
switch (type) {
|
||||
case PLUGIN -> executePlugin(action);
|
||||
case MCP -> executeMcp(action);
|
||||
case SCRIPT -> executeScript(action);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result.setSuccess(false);
|
||||
result.setData(e.getMessage());
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
private File loadFromFile() {
|
||||
return switch (type) {
|
||||
case PLUGIN -> Path.of(Constant.Path.ACTION_PROGRAM, key, "action.jar").toFile();
|
||||
case SCRIPT -> Path.of(ACTION_PROGRAM, key, "action.py").toFile();
|
||||
case MCP -> Path.of(ACTION_PROGRAM, key, "action.json").toFile();
|
||||
};
|
||||
}
|
||||
|
||||
private void executePlugin(File actionFile) {
|
||||
|
||||
}
|
||||
|
||||
private void executeMcp(File actionFile) {
|
||||
|
||||
}
|
||||
|
||||
private void executeScript(File actionFile) {
|
||||
|
||||
@Override
|
||||
public int compareTo(@NotNull MetaAction metaAction) {
|
||||
return this.order - metaAction.order;
|
||||
}
|
||||
|
||||
@Data
|
||||
|
||||
@@ -51,9 +51,12 @@ public class ActionExecutor extends AgentRunningSubModule<List<ImmediateActionDa
|
||||
Phaser phaser = new Phaser();
|
||||
phaser.register();
|
||||
actionCapability.putPhaserRecord(phaser, actionData);
|
||||
List<Integer> orderList = new ArrayList<>(actionChain.keySet().stream().toList());
|
||||
orderList.sort(Integer::compareTo);
|
||||
try {
|
||||
actionChain.forEach((k, v) -> {
|
||||
for (MetaAction metaAction : v) {
|
||||
for (Integer order : orderList) {
|
||||
List<MetaAction> metaActions = actionChain.get(order);
|
||||
for (MetaAction metaAction : metaActions) {
|
||||
// 根据io类型放入合适的列表
|
||||
if (metaAction.isIo()) {
|
||||
virtual.add(metaAction);
|
||||
@@ -65,7 +68,7 @@ public class ActionExecutor extends AgentRunningSubModule<List<ImmediateActionDa
|
||||
phaser.arriveAndAwaitAdvance();
|
||||
virtual.clear();
|
||||
platform.clear();
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
phaser.arriveAndDeregister();
|
||||
actionCapability.removePhaserRecord(phaser);
|
||||
@@ -97,9 +100,10 @@ public class ActionExecutor extends AgentRunningSubModule<List<ImmediateActionDa
|
||||
String input = getInput(result.getData());
|
||||
// 执行时不可使用`for in`和`forEach`,因为在`Intervention`相关模块存在动态调整
|
||||
}
|
||||
action.run();
|
||||
actionCapability.execute(action);
|
||||
} while (!result.isSuccess());
|
||||
// TODO 将执行结果写入特定对话角色记忆(cognationCore暴露方法)
|
||||
|
||||
} finally {
|
||||
phaser.arriveAndDeregister();
|
||||
}
|
||||
|
||||
@@ -143,7 +143,7 @@ public class InterventionHandler extends AgentRunningSubModule<HandlerInput, Voi
|
||||
executor = action.isIo() ? virtualExecutor : platformExecutor;
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
action.run();
|
||||
actionCapability.execute(action);
|
||||
} finally {
|
||||
phaser.arriveAndDeregister();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user