mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(runner): refactor CommandExecutionService into single instance
This commit is contained in:
@@ -9,7 +9,6 @@ import work.slhaf.partner.core.action.entity.ActionFileMetaData;
|
||||
import work.slhaf.partner.core.action.entity.MetaAction;
|
||||
import work.slhaf.partner.core.action.entity.MetaActionInfo;
|
||||
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.McpActionExecutor;
|
||||
import work.slhaf.partner.core.action.runner.execution.OriginExecutionService;
|
||||
import work.slhaf.partner.core.action.runner.mcp.*;
|
||||
@@ -38,7 +37,6 @@ public class LocalRunnerClient extends RunnerClient implements AutoCloseable {
|
||||
|
||||
private final McpClientRegistry mcpClientRegistry;
|
||||
private final McpTransportFactory mcpTransportFactory;
|
||||
private final CommandExecutionService commandExecutionService;
|
||||
private final ActionSerializer actionSerializer;
|
||||
private final OriginExecutionService originExecutionService;
|
||||
private final McpActionExecutor mcpActionExecutor;
|
||||
@@ -62,9 +60,8 @@ public class LocalRunnerClient extends RunnerClient implements AutoCloseable {
|
||||
|
||||
McpClientRegistry clientRegistry = new McpClientRegistry();
|
||||
McpTransportFactory transportFactory = new McpTransportFactory();
|
||||
CommandExecutionService commandService = new CommandExecutionService();
|
||||
ActionSerializer serializer = new ActionSerializer(tmpActionPath, dynamicActionPath);
|
||||
OriginExecutionService originService = new OriginExecutionService(commandService);
|
||||
OriginExecutionService originService = new OriginExecutionService();
|
||||
McpActionExecutor actionExecutor = new McpActionExecutor(clientRegistry);
|
||||
|
||||
McpMetaRegistry metaRegistry = null;
|
||||
@@ -86,8 +83,7 @@ public class LocalRunnerClient extends RunnerClient implements AutoCloseable {
|
||||
dynamicManager = new DynamicActionMcpManager(
|
||||
Path.of(dynamicActionPath),
|
||||
existedMetaActions,
|
||||
executor,
|
||||
commandService
|
||||
executor
|
||||
);
|
||||
registerMcpClient(clientRegistry, transportFactory, MCP_NAME_DYNAMIC, dynamicManager.clientConfig(10));
|
||||
log.info("DynamicActionMcp 注册完毕");
|
||||
@@ -114,7 +110,6 @@ public class LocalRunnerClient extends RunnerClient implements AutoCloseable {
|
||||
|
||||
this.mcpClientRegistry = clientRegistry;
|
||||
this.mcpTransportFactory = transportFactory;
|
||||
this.commandExecutionService = commandService;
|
||||
this.actionSerializer = serializer;
|
||||
this.originExecutionService = originService;
|
||||
this.mcpActionExecutor = actionExecutor;
|
||||
|
||||
@@ -13,7 +13,12 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class CommandExecutionService {
|
||||
|
||||
private ExecutorService readerExecutor = Executors.newVirtualThreadPerTaskExecutor();
|
||||
public static final CommandExecutionService INSTANCE = new CommandExecutionService();
|
||||
|
||||
private final ExecutorService readerExecutor = Executors.newVirtualThreadPerTaskExecutor();
|
||||
|
||||
private CommandExecutionService() {
|
||||
}
|
||||
|
||||
public String[] buildFileExecutionCommands(String launcher, Map<String, Object> params, String absolutePath) {
|
||||
int paramSize = params == null ? 0 : params.size();
|
||||
|
||||
@@ -12,21 +12,18 @@ import java.util.List;
|
||||
|
||||
public class OriginExecutionService {
|
||||
|
||||
private final CommandExecutionService commandExecutionService;
|
||||
|
||||
public OriginExecutionService(CommandExecutionService commandExecutionService) {
|
||||
this.commandExecutionService = commandExecutionService;
|
||||
public OriginExecutionService() {
|
||||
}
|
||||
|
||||
public RunnerClient.RunnerResponse run(MetaAction metaAction) {
|
||||
RunnerClient.RunnerResponse response = new RunnerClient.RunnerResponse();
|
||||
File file = new File(metaAction.getLocation());
|
||||
String[] commands = commandExecutionService.buildFileExecutionCommands(metaAction.getLauncher(), metaAction.getParams(), file.getAbsolutePath());
|
||||
String[] commands = CommandExecutionService.INSTANCE.buildFileExecutionCommands(metaAction.getLauncher(), metaAction.getParams(), file.getAbsolutePath());
|
||||
WrappedLaunchSpec wrapped = ExecutionPolicyRegistry.INSTANCE.prepare(Arrays.stream(commands).toList());
|
||||
List<String> wrappedCommands = new ArrayList<>();
|
||||
wrappedCommands.add(wrapped.getCommand());
|
||||
wrappedCommands.addAll(wrapped.getArgs());
|
||||
CommandExecutionService.Result execResult = commandExecutionService.exec(wrappedCommands);
|
||||
CommandExecutionService.Result execResult = CommandExecutionService.INSTANCE.exec(wrappedCommands);
|
||||
response.setOk(execResult.isOk());
|
||||
response.setData(execResult.getTotal());
|
||||
return response;
|
||||
|
||||
@@ -45,11 +45,10 @@ public class DynamicActionMcpManager implements AutoCloseable {
|
||||
|
||||
public DynamicActionMcpManager(Path root,
|
||||
ConcurrentHashMap<String, MetaActionInfo> existedMetaActions,
|
||||
ExecutorService executor,
|
||||
CommandExecutionService commandExecutionService) throws IOException {
|
||||
ExecutorService executor) throws IOException {
|
||||
this.root = root;
|
||||
this.existedMetaActions = existedMetaActions;
|
||||
this.commandExecutionService = commandExecutionService;
|
||||
this.commandExecutionService = CommandExecutionService.INSTANCE;
|
||||
InProcessMcpTransport.Pair pair = InProcessMcpTransport.pair();
|
||||
this.clientTransport = pair.clientSide();
|
||||
McpSchema.ServerCapabilities serverCapabilities = McpSchema.ServerCapabilities.builder()
|
||||
|
||||
@@ -890,7 +890,7 @@ public class LocalRunnerClientTest {
|
||||
LocalRunnerClient client = new LocalRunnerClient(existedMetaActions, executor, tempDir.toString());
|
||||
BuiltinActionRegistry registry = new BuiltinActionRegistry();
|
||||
client.setBuiltinActionRegistry(registry);
|
||||
registry.defineBuiltinAction("echo", buildMetaActionInfo("echo"), params -> params.get("value"));
|
||||
registry.defineBuiltinAction("echo", buildMetaActionInfo("echo"), params -> params.get("value").toString());
|
||||
|
||||
try {
|
||||
MetaAction metaAction = buildMetaAction(MetaAction.Type.BUILTIN, "builtin", "echo", Map.of("value", "ok"));
|
||||
|
||||
@@ -12,7 +12,7 @@ import java.util.Map;
|
||||
|
||||
class CommandExecutionServiceTest {
|
||||
|
||||
private final CommandExecutionService service = new CommandExecutionService();
|
||||
private final CommandExecutionService service = CommandExecutionService.INSTANCE;
|
||||
|
||||
@Test
|
||||
void testBuildFileExecutionCommandsWithOrderedParams() {
|
||||
|
||||
Reference in New Issue
Block a user