mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(MetaAction): separate key attribute into name and location
Context: This change adapts MetaAction locating to support different MetaAction types, including loading from the local filesystem and from MCP tools.
This commit is contained in:
@@ -228,6 +228,12 @@ public class ActionCore extends PartnerCore<ActionCore> {
|
||||
metaAction.setParams(metaActionInfo.getParams());
|
||||
metaAction.setType(metaActionInfo.getType());
|
||||
metaAction.setIo(metaActionInfo.isIo());
|
||||
String[] split = actionKey.split("::");
|
||||
if (split.length < 2) {
|
||||
throw new MetaActionNotFoundException("未找到对应的行动程序,原因: 传入的 actionKey(" + actionKey + ") 存在异常");
|
||||
}
|
||||
metaAction.setLocation(split[0]);
|
||||
metaAction.setName(split[1]);
|
||||
return metaAction;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,8 @@ package work.slhaf.partner.core.action.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
import static work.slhaf.partner.common.Constant.Path.ACTION_PROGRAM;
|
||||
|
||||
/**
|
||||
* 行动链中的单一元素,封装了调用外部行动程序的必要信息与结果容器,可被{@link work.slhaf.partner.core.action.ActionCapability}执行
|
||||
*/
|
||||
@@ -14,9 +11,9 @@ import static work.slhaf.partner.common.Constant.Path.ACTION_PROGRAM;
|
||||
public class MetaAction {
|
||||
|
||||
/**
|
||||
* 行动key,用于标识与定位行动程序
|
||||
* 行动name,用于标识行动程序
|
||||
*/
|
||||
private String key;
|
||||
private String name;
|
||||
/**
|
||||
* 行动程序可接受的参数,由调用处设置
|
||||
*/
|
||||
@@ -34,13 +31,19 @@ public class MetaAction {
|
||||
*/
|
||||
private MetaActionType type;
|
||||
|
||||
private Path path;
|
||||
/**
|
||||
* 当类型为 MCP 时,该字段对应相应 MCP Client 注册时生成的 id;
|
||||
* 当类型为 ORIGIN 时,该字段对应相应的磁盘路径字符串
|
||||
*/
|
||||
private String location;
|
||||
|
||||
public void resetPath() {
|
||||
path = switch (type) {
|
||||
case ORIGIN -> path;
|
||||
case MCP -> Path.of(ACTION_PROGRAM, key, "action.json");
|
||||
};
|
||||
/**
|
||||
* actionKey 将由 location+name 共同定位
|
||||
*
|
||||
* @return actionKey
|
||||
*/
|
||||
public String getKey() {
|
||||
return location + "::" + name;
|
||||
}
|
||||
|
||||
@Data
|
||||
|
||||
@@ -57,7 +57,7 @@ public class LocalRunnerClient extends RunnerClient {
|
||||
|
||||
private RunnerResponse doRunWithOrigin(MetaAction metaAction) {
|
||||
RunnerResponse response = new RunnerResponse();
|
||||
File file = metaAction.getPath().toFile();
|
||||
File file = new File(metaAction.getLocation());
|
||||
String ext = FileUtil.getSuffix(file);
|
||||
if (ext == null || ext.isEmpty()) {
|
||||
response.setOk(false);
|
||||
@@ -98,13 +98,13 @@ public class LocalRunnerClient extends RunnerClient {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path buildTmpPath(MetaAction tempAction, String codeType) {
|
||||
return Path.of(TMP_ACTION_DIR_LOCAL, System.currentTimeMillis() + "-" + tempAction.getKey() + codeType);
|
||||
public String buildTmpPath(MetaAction tempAction, String codeType) {
|
||||
return Path.of(TMP_ACTION_DIR_LOCAL, System.currentTimeMillis() + "-" + tempAction.getKey() + codeType).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tmpSerialize(MetaAction tempAction, String code, String codeType) throws IOException {
|
||||
Path path = tempAction.getPath();
|
||||
Path path = Path.of(tempAction.getLocation());
|
||||
File file = path.toFile();
|
||||
file.createNewFile();
|
||||
Files.writeString(path, code);
|
||||
|
||||
@@ -27,7 +27,6 @@ import work.slhaf.partner.core.action.entity.MetaAction.ResultStatus;
|
||||
import work.slhaf.partner.core.action.entity.MetaActionInfo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -152,7 +151,7 @@ public abstract class RunnerClient {
|
||||
|
||||
protected abstract RunnerResponse doRun(MetaAction metaAction);
|
||||
|
||||
public abstract Path buildTmpPath(MetaAction tempAction, String codeType);
|
||||
public abstract String buildTmpPath(MetaAction tempAction, String codeType);
|
||||
|
||||
public abstract void tmpSerialize(MetaAction tempAction, String code, String codeType) throws IOException;
|
||||
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package work.slhaf.partner.core.action.runner;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
import work.slhaf.partner.core.action.entity.McpData;
|
||||
import work.slhaf.partner.core.action.entity.MetaAction;
|
||||
import work.slhaf.partner.core.action.entity.MetaActionInfo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
@@ -42,7 +40,7 @@ public class SandboxRunnerClient extends RunnerClient {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path buildTmpPath(MetaAction tempAction, String codeType) {
|
||||
public String buildTmpPath(MetaAction tempAction, String codeType) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'buildTmpPath'");
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ public class DynamicActionGenerator extends AgentRunningSubModule<GeneratorInput
|
||||
// 将临时行动单元序列化至临时文件夹,并设置程序路径、放置在队列中,等待执行状态变化,并根据序列化选项选择是否补充 MetaActionInfo 并持久序列化
|
||||
// 通过 ActionCapability 暴露的接口,序列化至临时文件夹,同时返回Path对象并设置。队列建议交给 SandboxRunner
|
||||
// 持有,包括监听与序列化线程
|
||||
tempAction.setPath(runnerClient.buildTmpPath(tempAction, generatorData.getCodeType()));
|
||||
tempAction.setLocation(runnerClient.buildTmpPath(tempAction, generatorData.getCodeType()));
|
||||
runnerClient.tmpSerialize(tempAction, generatorData.getCode(), generatorData.getCodeType());
|
||||
if (generatorData.isSerialize()) {
|
||||
waitingSerialize();
|
||||
@@ -66,7 +66,7 @@ public class DynamicActionGenerator extends AgentRunningSubModule<GeneratorInput
|
||||
|
||||
private MetaAction buildAction(GeneratorInput input) {
|
||||
MetaAction tempAction = new MetaAction();
|
||||
tempAction.setKey(input.getKey());
|
||||
tempAction.setName(input.getActionName());
|
||||
tempAction.setParams(input.getParams());
|
||||
tempAction.setIo(true);
|
||||
tempAction.setType(MetaActionType.ORIGIN);
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class GeneratorInput {
|
||||
private String key;
|
||||
private String actionName;
|
||||
private Map<String, String> params;
|
||||
private String description;
|
||||
private Map<String, String> paramsDescription;
|
||||
|
||||
@@ -6,7 +6,6 @@ import org.junit.jupiter.api.Test;
|
||||
import work.slhaf.partner.core.action.entity.MetaAction;
|
||||
import work.slhaf.partner.core.action.entity.MetaActionType;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@@ -30,10 +29,10 @@ public class LocalRunnerClientTest {
|
||||
private static @NotNull MetaAction buildTmpMetaAction() {
|
||||
MetaAction metaAction = new MetaAction();
|
||||
metaAction.setIo(false);
|
||||
metaAction.setKey("hello_world");
|
||||
metaAction.setName("hello_world");
|
||||
metaAction.setParams(Map.of("name", "origin_run"));
|
||||
metaAction.setType(MetaActionType.ORIGIN);
|
||||
metaAction.setPath(Path.of("/home/slhaf/Projects/IdeaProjects/Projects/Partner/Partner-Main/src/test/java/resources/action/tmp/hello_world.py"));
|
||||
metaAction.setLocation("/home/slhaf/Projects/IdeaProjects/Projects/Partner/Partner-Main/src/test/java/resources/action/tmp/hello_world.py");
|
||||
return metaAction;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import work.slhaf.partner.core.action.entity.MetaAction;
|
||||
import work.slhaf.partner.core.action.entity.MetaActionInfo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
@@ -86,7 +85,7 @@ public class RunnerClientTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path buildTmpPath(MetaAction tempAction, String codeType) {
|
||||
public String buildTmpPath(MetaAction tempAction, String codeType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user