mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(runner): remove legacy abstract for command execution
This commit is contained in:
@@ -8,7 +8,6 @@ import work.slhaf.partner.core.action.entity.MetaAction;
|
|||||||
import work.slhaf.partner.core.action.entity.MetaActionInfo;
|
import work.slhaf.partner.core.action.entity.MetaActionInfo;
|
||||||
import work.slhaf.partner.core.action.exception.ActionInitFailedException;
|
import work.slhaf.partner.core.action.exception.ActionInitFailedException;
|
||||||
import work.slhaf.partner.core.action.runner.execution.CommandExecutionService;
|
import work.slhaf.partner.core.action.runner.execution.CommandExecutionService;
|
||||||
import work.slhaf.partner.core.action.runner.execution.LocalProcessCommandExecutionService;
|
|
||||||
import work.slhaf.partner.core.action.runner.execution.McpActionExecutor;
|
import work.slhaf.partner.core.action.runner.execution.McpActionExecutor;
|
||||||
import work.slhaf.partner.core.action.runner.execution.OriginExecutionService;
|
import work.slhaf.partner.core.action.runner.execution.OriginExecutionService;
|
||||||
import work.slhaf.partner.core.action.runner.mcp.*;
|
import work.slhaf.partner.core.action.runner.mcp.*;
|
||||||
@@ -59,7 +58,7 @@ public class LocalRunnerClient extends RunnerClient implements AutoCloseable {
|
|||||||
|
|
||||||
McpClientRegistry clientRegistry = new McpClientRegistry();
|
McpClientRegistry clientRegistry = new McpClientRegistry();
|
||||||
McpTransportFactory transportFactory = new McpTransportFactory();
|
McpTransportFactory transportFactory = new McpTransportFactory();
|
||||||
CommandExecutionService commandService = new LocalProcessCommandExecutionService();
|
CommandExecutionService commandService = new CommandExecutionService();
|
||||||
ActionSerializer serializer = new ActionSerializer(tmpActionPath, dynamicActionPath);
|
ActionSerializer serializer = new ActionSerializer(tmpActionPath, dynamicActionPath);
|
||||||
OriginExecutionService originService = new OriginExecutionService(commandService);
|
OriginExecutionService originService = new OriginExecutionService(commandService);
|
||||||
McpActionExecutor actionExecutor = new McpActionExecutor(clientRegistry);
|
McpActionExecutor actionExecutor = new McpActionExecutor(clientRegistry);
|
||||||
|
|||||||
@@ -2,17 +2,85 @@ package work.slhaf.partner.core.action.runner.execution;
|
|||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public interface CommandExecutionService {
|
public class CommandExecutionService {
|
||||||
|
|
||||||
String[] buildCommands(String ext, Map<String, Object> params, String absolutePath);
|
public String[] buildCommands(String ext, Map<String, Object> 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;
|
||||||
|
}
|
||||||
|
|
||||||
Result exec(String... command);
|
public Result exec(String... command) {
|
||||||
|
Result result = new Result();
|
||||||
|
List<String> output = new ArrayList<>();
|
||||||
|
List<String> error = new ArrayList<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Process process = new ProcessBuilder(command)
|
||||||
|
.redirectErrorStream(false)
|
||||||
|
.start();
|
||||||
|
|
||||||
|
Thread stdoutThread = new Thread(() -> {
|
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
output.add(line);
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Thread stderrThread = new Thread(() -> {
|
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.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
|
@Data
|
||||||
class Result {
|
public static class Result {
|
||||||
private boolean ok;
|
private boolean ok;
|
||||||
private String total;
|
private String total;
|
||||||
private List<String> resultList;
|
private List<String> resultList;
|
||||||
|
|||||||
@@ -1,81 +0,0 @@
|
|||||||
package work.slhaf.partner.core.action.runner.execution;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
|
|
||||||
public class LocalProcessCommandExecutionService implements CommandExecutionService {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String[] buildCommands(String ext, Map<String, Object> 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Result exec(String... command) {
|
|
||||||
Result result = new Result();
|
|
||||||
List<String> output = new ArrayList<>();
|
|
||||||
List<String> error = new ArrayList<>();
|
|
||||||
|
|
||||||
try {
|
|
||||||
Process process = new ProcessBuilder(command)
|
|
||||||
.redirectErrorStream(false)
|
|
||||||
.start();
|
|
||||||
|
|
||||||
Thread stdoutThread = new Thread(() -> {
|
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
|
||||||
String line;
|
|
||||||
while ((line = reader.readLine()) != null) {
|
|
||||||
output.add(line);
|
|
||||||
}
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Thread stderrThread = new Thread(() -> {
|
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
|
|
||||||
String line;
|
|
||||||
while ((line = reader.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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user