From e3294ec3022a53277c3e1b4e6e6db186751066a2 Mon Sep 17 00:00:00 2001 From: slhafzjw Date: Mon, 29 Dec 2025 18:26:30 +0800 Subject: [PATCH] refactor(LocalRunnerClient): move system execution methods into SystemExecHelper --- .../core/action/runner/LocalRunnerClient.java | 154 +++++++++--------- 1 file changed, 79 insertions(+), 75 deletions(-) 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 93682706..b7457a69 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 @@ -125,33 +125,13 @@ public class LocalRunnerClient extends RunnerClient { response.setData("未知文件类型"); return response; } - String[] commands = buildCommands(ext, metaAction.getParams(), file.getAbsolutePath()); - SystemExecResult execResult = exec(commands); + String[] commands = SystemExecHelper.buildCommands(ext, metaAction.getParams(), file.getAbsolutePath()); + SystemExecResult execResult = SystemExecHelper.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.toString(); - }); - return commands; - } - private RunnerResponse doRunWithMcp(MetaAction metaAction) { RunnerResponse response = new RunnerResponse(); McpSyncClient mcpClient = mcpClients.get(metaAction.getLocation()); @@ -193,7 +173,7 @@ public class LocalRunnerClient extends RunnerClient { JSONObject sysDependencies = new JSONObject(); sysDependencies.put("language", "Python"); JSONArray dependencies = sysDependencies.putArray("dependencies"); - SystemExecResult pyResult = exec("pip", "list", "--format=freeze"); + SystemExecResult pyResult = SystemExecHelper.exec("pip", "list", "--format=freeze"); System.out.println(pyResult); if (pyResult.isOk()) { List resultList = pyResult.getResultList(); @@ -210,58 +190,6 @@ public class LocalRunnerClient extends RunnerClient { return sysDependencies; } - private SystemExecResult exec(String... command) { - SystemExecResult result = new SystemExecResult(); - List output = new ArrayList<>(); - List error = new ArrayList<>(); - - try { - Process process = new ProcessBuilder(command) - .redirectErrorStream(false) // 分开读 - .start(); - - Thread stdoutThread = new Thread(() -> { - try (BufferedReader r = new BufferedReader( - new InputStreamReader(process.getInputStream()))) { - String line; - while ((line = r.readLine()) != null) { - output.add(line); - } - } catch (Exception ignored) { - } - }); - - Thread stderrThread = new Thread(() -> { - try (BufferedReader r = new BufferedReader( - new InputStreamReader(process.getErrorStream()))) { - String line; - while ((line = r.readLine()) != null) { - error.add(line); - } - } catch (Exception ignored) { - } - }); - - stdoutThread.start(); - stderrThread.start(); - - int exitCode = process.waitFor(); - stdoutThread.join(); - stderrThread.join(); - - result.setOk(exitCode == 0); - result.setResultList(output.isEmpty() ? error : output); - result.setTotal(String.join("\n", - output.isEmpty() ? error : output)); - - } catch (Exception e) { - result.setOk(false); - result.setTotal(e.getMessage()); - } - - return result; - } - /** * 该部分主要发生在扫描到新的MCP Server描述文件时出现的注册逻辑 * @@ -631,6 +559,82 @@ public class LocalRunnerClient extends RunnerClient { } } + private static class SystemExecHelper { + + //TODO 后续需在加载时、或者通过配置文件获取可用命令并注册匹配 + private static 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; + } + int paramSize = params == null ? 0 : params.size(); + String[] commands = new String[paramSize + 2]; + commands[0] = command; + commands[1] = absolutePath; + AtomicInteger paramCount = new AtomicInteger(2); + if (params != null) { + params.forEach((param, value) -> commands[paramCount.getAndIncrement()] = "--" + param + "=" + value); + } + return commands; + } + + private static SystemExecResult exec(String... command) { + SystemExecResult result = new SystemExecResult(); + List output = new ArrayList<>(); + List error = new ArrayList<>(); + + try { + Process process = new ProcessBuilder(command) + .redirectErrorStream(false) // 分开读 + .start(); + + Thread stdoutThread = new Thread(() -> { + try (BufferedReader r = new BufferedReader( + new InputStreamReader(process.getInputStream()))) { + String line; + while ((line = r.readLine()) != null) { + output.add(line); + } + } catch (Exception ignored) { + } + }); + + Thread stderrThread = new Thread(() -> { + try (BufferedReader r = new BufferedReader( + new InputStreamReader(process.getErrorStream()))) { + String line; + while ((line = r.readLine()) != null) { + error.add(line); + } + } catch (Exception ignored) { + } + }); + + stdoutThread.start(); + stderrThread.start(); + + int exitCode = process.waitFor(); + stdoutThread.join(); + stderrThread.join(); + + result.setOk(exitCode == 0); + result.setResultList(output.isEmpty() ? error : output); + result.setTotal(String.join("\n", + output.isEmpty() ? error : output)); + + } catch (Exception e) { + result.setOk(false); + result.setTotal(e.getMessage()); + } + + return result; + } + } + @Data private static class SystemExecResult { private boolean ok;