mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(LocalRunnerClient): support remove action temporarily while action is not usable
This commit is contained in:
@@ -448,6 +448,17 @@ public class LocalRunnerClient extends RunnerClient {
|
|||||||
return run == 1 && desc;
|
return run == 1 && desc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addAction(String name, MetaActionInfo info, File program) {
|
||||||
|
String actionKey = "local::" + name;
|
||||||
|
existedMetaActions.put(actionKey, info);
|
||||||
|
dynamicActionMcpServer.addTool(buildAsyncToolSpecification(info, program, actionKey, name)).subscribe();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeAction(String name) {
|
||||||
|
existedMetaActions.remove("local::" + name);
|
||||||
|
dynamicActionMcpServer.removeTool(name).subscribe();
|
||||||
|
}
|
||||||
|
|
||||||
private BiFunction<McpTransportContext, McpSchema.CallToolRequest, Mono<McpSchema.CallToolResult>> buildToolHandler(File finalProgram) {
|
private BiFunction<McpTransportContext, McpSchema.CallToolRequest, Mono<McpSchema.CallToolResult>> buildToolHandler(File finalProgram) {
|
||||||
return (mcpTransportContext, callToolRequest) -> {
|
return (mcpTransportContext, callToolRequest) -> {
|
||||||
Map<String, Object> arguments = callToolRequest.arguments();
|
Map<String, Object> arguments = callToolRequest.arguments();
|
||||||
@@ -507,11 +518,7 @@ public class LocalRunnerClient extends RunnerClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
MetaActionInfo info = JSONUtil.readJSONObject(meta, StandardCharsets.UTF_8).toBean(MetaActionInfo.class);
|
MetaActionInfo info = JSONUtil.readJSONObject(meta, StandardCharsets.UTF_8).toBean(MetaActionInfo.class);
|
||||||
String actionKey = "local::" + dir.getName();
|
addAction(dir.getName(), info, program);
|
||||||
existedMetaActions.put(actionKey, info);
|
|
||||||
|
|
||||||
McpStatelessServerFeatures.AsyncToolSpecification specification = buildAsyncToolSpecification(info, program, actionKey, dir.getName());
|
|
||||||
dynamicActionMcpServer.addTool(specification).subscribe();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -565,9 +572,9 @@ public class LocalRunnerClient extends RunnerClient {
|
|||||||
File meta = Path.of(thisDir.toString(), "desc.json").toFile();
|
File meta = Path.of(thisDir.toString(), "desc.json").toFile();
|
||||||
try {
|
try {
|
||||||
MetaActionInfo info = JSONUtil.readJSONObject(meta, StandardCharsets.UTF_8).toBean(MetaActionInfo.class);
|
MetaActionInfo info = JSONUtil.readJSONObject(meta, StandardCharsets.UTF_8).toBean(MetaActionInfo.class);
|
||||||
dynamicActionMcpServer.addTool(buildAsyncToolSpecification(info, context.toFile(), actionKey, name)).subscribe();
|
addAction(name, info, context.toFile());
|
||||||
existedMetaActions.put(actionKey, info);
|
|
||||||
} catch (IORuntimeException e) {
|
} catch (IORuntimeException e) {
|
||||||
|
removeAction(name);
|
||||||
log.warn("读取 desc.json 失败,请检查字段", e);
|
log.warn("读取 desc.json 失败,请检查字段", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -575,10 +582,18 @@ public class LocalRunnerClient extends RunnerClient {
|
|||||||
private void handleMetaModify(Path thisDir, Path context) {
|
private void handleMetaModify(Path thisDir, Path context) {
|
||||||
// 检查是否除了描述文件外还存在别的可执行文件
|
// 检查是否除了描述文件外还存在别的可执行文件
|
||||||
File meta = Path.of(thisDir.toString(), context.toString()).toFile();
|
File meta = Path.of(thisDir.toString(), context.toString()).toFile();
|
||||||
|
String name = thisDir.getFileName().toString();
|
||||||
try {
|
try {
|
||||||
MetaActionInfo info = JSONUtil.readJSONObject(meta, StandardCharsets.UTF_8).toBean(MetaActionInfo.class);
|
MetaActionInfo info = JSONUtil.readJSONObject(meta, StandardCharsets.UTF_8).toBean(MetaActionInfo.class);
|
||||||
existedMetaActions.put("local::" + thisDir.getFileName().toString(), info);
|
File program = null;
|
||||||
|
//noinspection DataFlowIssue
|
||||||
|
for (File f : thisDir.toFile().listFiles()) {
|
||||||
|
program = f;
|
||||||
|
}
|
||||||
|
addAction(name, info, program);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
// 如果程序已经存在,此时针对 desc.json 的调整可能来自程序的调整,所以此时的失败读取最好将 tool 及对应的 action 移除
|
||||||
|
removeAction(name);
|
||||||
log.warn("读取 desc 失败,可能处于写入中: {}", meta.getAbsolutePath(), e);
|
log.warn("读取 desc 失败,可能处于写入中: {}", meta.getAbsolutePath(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -598,9 +613,9 @@ public class LocalRunnerClient extends RunnerClient {
|
|||||||
if (normalPath(thisDir)) {
|
if (normalPath(thisDir)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 未通过校验则删除对应的 action
|
// 未通过校验则删除对应的 action,并在 DynamicActonMcpServer 中删除对应的工具
|
||||||
String name = thisDir.getFileName().toString();
|
String name = thisDir.getFileName().toString();
|
||||||
existedMetaActions.remove("local::" + name);
|
removeAction(name);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user