diff --git a/Partner-Main/src/main/java/work/slhaf/partner/core/action/runner/LocalRunnerClient.java b/Partner-Main/src/main/java/work/slhaf/partner/core/action/runner/LocalRunnerClient.java index 95370f3f..6e30a456 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/core/action/runner/LocalRunnerClient.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/core/action/runner/LocalRunnerClient.java @@ -1,5 +1,6 @@ package work.slhaf.partner.core.action.runner; +import cn.hutool.core.io.FileUtil; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import lombok.Data; @@ -57,10 +58,40 @@ public class LocalRunnerClient extends RunnerClient { private RunnerResponse doRunWithOrigin(MetaAction metaAction) { RunnerResponse response = new RunnerResponse(); - + File file = metaAction.getPath().toFile(); + String ext = FileUtil.getSuffix(file); + if (ext == null || ext.isEmpty()) { + response.setOk(false); + response.setData("未知文件类型"); + return response; + } + String[] commands = buildCommands(ext, metaAction.getParams(), file.getAbsolutePath()); + SystemExecResult execResult = exec(commands); + response.setOk(execResult.isOk()); + response.setData(execResult.getTotal()); return response; } + //TODO 后续需在加载时、或者通过配置文件获取可用命令并注册匹配 + private String[] buildCommands(String ext, Map params, String absolutePath) { + String command = switch (ext) { + case "py" -> "python"; + case "sh" -> "bash"; + default -> null; + }; + if (command == null) { + return null; + } + String[] commands = new String[params.size() + 2]; + commands[0] = command; + commands[1] = absolutePath; + AtomicInteger paramCount = new AtomicInteger(2); + params.forEach((param, value) -> { + commands[paramCount.getAndIncrement()] = "--" + param + "=" + value; + }); + return commands; + } + private RunnerResponse doRunWithMcp(MetaAction metaAction) { RunnerResponse response = new RunnerResponse(); @@ -95,7 +126,7 @@ public class LocalRunnerClient extends RunnerClient { JSONObject sysDependencies = new JSONObject(); sysDependencies.put("language", "Python"); JSONArray dependencies = sysDependencies.putArray("dependencies"); - SystemExecResult pyResult = exec("pip", "li", "--format=feeze"); + SystemExecResult pyResult = exec("pip", "list", "--format=freeze"); System.out.println(pyResult); if (pyResult.isOk()) { List resultList = pyResult.getResultList(); @@ -283,7 +314,7 @@ public class LocalRunnerClient extends RunnerClient { } private void handleParentDirEvent(WatchEvent.Kind kind, Path thisDir, Path context, - WatchService watchService) { + WatchService watchService) { Path path = Path.of(thisDir.toString(), context.toString()); // MODIFY 事件不进行处理 if (kind == StandardWatchEventKinds.ENTRY_CREATE) { diff --git a/Partner-Main/src/test/java/resources/action/tmp/hello_world.py b/Partner-Main/src/test/java/resources/action/tmp/hello_world.py new file mode 100644 index 00000000..d477459e --- /dev/null +++ b/Partner-Main/src/test/java/resources/action/tmp/hello_world.py @@ -0,0 +1,24 @@ +import sys + + +def parse_args(argv): + """ + 将 --key=value 解析成 dict + """ + params = {} + for arg in argv: + if arg.startswith("--") and "=" in arg: + key, value = arg[2:].split("=", 1) + params[key] = value + return params + + +def main(): + params = parse_args(sys.argv[1:]) + + name = params.get("name", "World") + print(f"Hello {name}!") + + +if __name__ == "__main__": + main() diff --git a/Partner-Main/src/test/java/work/slhaf/partner/core/action/runner/LocalRunnerClientTest.java b/Partner-Main/src/test/java/work/slhaf/partner/core/action/runner/LocalRunnerClientTest.java new file mode 100644 index 00000000..77637a34 --- /dev/null +++ b/Partner-Main/src/test/java/work/slhaf/partner/core/action/runner/LocalRunnerClientTest.java @@ -0,0 +1,40 @@ +package work.slhaf.partner.core.action.runner; + +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.BeforeAll; +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; + +public class LocalRunnerClientTest { + + static LocalRunnerClient runnerClient; + + @BeforeAll + static void beforeAll() { + runnerClient = new LocalRunnerClient(Map.of(), Executors.newVirtualThreadPerTaskExecutor(), "/home/slhaf/Projects/IdeaProjects/Projects/Partner/Partner-Main/src/test/java/resources/action/data"); + } + + @Test + void runOrigin() { + MetaAction metaAction = buildTmpMetaAction(); + + RunnerClient.RunnerResponse runnerResponse = runnerClient.doRun(metaAction); + System.out.println(runnerResponse.getData()); + } + + private static @NotNull MetaAction buildTmpMetaAction() { + MetaAction metaAction = new MetaAction(); + metaAction.setIo(false); + metaAction.setKey("hello_world"); + metaAction.setParams(Map.of("name", "origin_run")); + metaAction.setOrder(-1); + 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")); + return metaAction; + } +}