|
@@ -738,18 +738,25 @@ public class PythonExecutor {
|
|
|
/**
|
|
/**
|
|
|
* 捕获沙箱内生成的文件:移动到 py-out/{execId}/。
|
|
* 捕获沙箱内生成的文件:移动到 py-out/{execId}/。
|
|
|
*
|
|
*
|
|
|
- * <p>图片返回可内联展示的 URL({@code imageUrls});HTML、Word、Excel、PDF、CSV 等
|
|
|
|
|
- * 非图片文件也一并保留,供对话结束后的产物扫描登记(否则随沙箱目录一起删除)。
|
|
|
|
|
|
|
+ * <p>收集范围:沙箱根目录 + {@code outputs/} 子树(工具描述约定交付物写
|
|
|
|
|
+ * {@code ./outputs},cwd 就是沙箱根)。只收根目录会让 {@code ./outputs} 下的交付物
|
|
|
|
|
+ * 随沙箱删除——这是“模型写 outputs 但产物栏看不见”的直接原因之一。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>图片返回可内联展示的 URL({@code imageUrls},py-out 里扁平存放,
|
|
|
|
|
+ * 子目录用 {@code __} 连接避免 URL 多段路径);HTML、Word、Excel、PDF、CSV 等
|
|
|
|
|
+ * 非图片文件保留原来的相对路径({@code outputs/xxx.csv})供产物扫描登记。
|
|
|
* 图片数量仍受 {@code maxImages} 限制;非图片最多保留 50 个。
|
|
* 图片数量仍受 {@code maxImages} 限制;非图片最多保留 50 个。
|
|
|
*/
|
|
*/
|
|
|
private List<String> collectGeneratedFiles(long workspaceId, String execId, Path sandbox,
|
|
private List<String> collectGeneratedFiles(long workspaceId, String execId, Path sandbox,
|
|
|
String userId, String sessionId) {
|
|
String userId, String sessionId) {
|
|
|
List<String> urls = new ArrayList<>();
|
|
List<String> urls = new ArrayList<>();
|
|
|
- try (Stream<Path> files = Files.list(sandbox)) {
|
|
|
|
|
|
|
+ try (Stream<Path> files = Files.walk(sandbox, 6)) {
|
|
|
List<Path> generated = files
|
|
List<Path> generated = files
|
|
|
.filter(Files::isRegularFile)
|
|
.filter(Files::isRegularFile)
|
|
|
|
|
+ .filter(p -> !Files.isSymbolicLink(p))
|
|
|
.filter(p -> ArtifactFileTypes.isSupported(p.getFileName().toString()))
|
|
.filter(p -> ArtifactFileTypes.isSupported(p.getFileName().toString()))
|
|
|
- .sorted(Comparator.comparing(p -> p.getFileName().toString()))
|
|
|
|
|
|
|
+ .filter(p -> isDeliverableInSandbox(sandbox, p))
|
|
|
|
|
+ .sorted(Comparator.comparing(p -> relativePath(sandbox, p)))
|
|
|
.toList();
|
|
.toList();
|
|
|
if (generated.isEmpty()) {
|
|
if (generated.isEmpty()) {
|
|
|
return urls;
|
|
return urls;
|
|
@@ -766,6 +773,7 @@ public class PythonExecutor {
|
|
|
int maxImages = etlProperties.getPython().getMaxImages();
|
|
int maxImages = etlProperties.getPython().getMaxImages();
|
|
|
for (Path file : generated) {
|
|
for (Path file : generated) {
|
|
|
String name = file.getFileName().toString();
|
|
String name = file.getFileName().toString();
|
|
|
|
|
+ String relative = relativePath(sandbox, file);
|
|
|
boolean image = ArtifactFileTypes.isImage(name);
|
|
boolean image = ArtifactFileTypes.isImage(name);
|
|
|
if (image && imageCount >= maxImages) {
|
|
if (image && imageCount >= maxImages) {
|
|
|
continue;
|
|
continue;
|
|
@@ -773,11 +781,24 @@ public class PythonExecutor {
|
|
|
if (!image && otherCount >= 50) {
|
|
if (!image && otherCount >= 50) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
- Files.move(file, outDir.resolve(name));
|
|
|
|
|
if (image) {
|
|
if (image) {
|
|
|
|
|
+ // 图片走 /py/files/{case}/{execId}/{filename},接口只认单段文件名
|
|
|
|
|
+ String flatName = relative.replace('/', '_').replace('\\', '_');
|
|
|
|
|
+ Path target = uniqueTarget(outDir, flatName);
|
|
|
|
|
+ Files.move(file, target);
|
|
|
imageCount++;
|
|
imageCount++;
|
|
|
- urls.add(IMAGE_URL_PREFIX + workspaceId + "/" + execId + "/" + name);
|
|
|
|
|
|
|
+ urls.add(IMAGE_URL_PREFIX + workspaceId + "/" + execId + "/"
|
|
|
|
|
+ + target.getFileName());
|
|
|
} else {
|
|
} else {
|
|
|
|
|
+ // 非图片保留相对路径(outputs/xxx.csv),产物扫描按目录递归认领
|
|
|
|
|
+ Path target = outDir.resolve(relative).normalize();
|
|
|
|
|
+ if (!target.startsWith(outDir)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (target.getParent() != null) {
|
|
|
|
|
+ Files.createDirectories(target.getParent());
|
|
|
|
|
+ }
|
|
|
|
|
+ Files.move(file, target);
|
|
|
otherCount++;
|
|
otherCount++;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -789,6 +810,43 @@ public class PythonExecutor {
|
|
|
return urls;
|
|
return urls;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 沙箱内哪些文件算交付物:根目录文件(历史行为)+ {@code outputs/} 子树
|
|
|
|
|
+ * (工具描述约定的交付物目录)。其它中间目录(如 {@code work/})不收集,
|
|
|
|
|
+ * 避免把模型的过程文件一股脑登记成产物。
|
|
|
|
|
+ */
|
|
|
|
|
+ private static boolean isDeliverableInSandbox(Path sandbox, Path file) {
|
|
|
|
|
+ Path parent = file.getParent();
|
|
|
|
|
+ if (parent == null || parent.equals(sandbox)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ Path relative = sandbox.relativize(file);
|
|
|
|
|
+ return relative.getNameCount() > 1
|
|
|
|
|
+ && "outputs".equalsIgnoreCase(relative.getName(0).toString());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static String relativePath(Path root, Path file) {
|
|
|
|
|
+ return root.relativize(file).toString().replace('\\', '/');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** py-out 内的同名文件兜底:图片扁平化后可能与根目录同名图片冲突 */
|
|
|
|
|
+ private static Path uniqueTarget(Path dir, String name) {
|
|
|
|
|
+ Path target = dir.resolve(name);
|
|
|
|
|
+ if (!Files.exists(target)) {
|
|
|
|
|
+ return target;
|
|
|
|
|
+ }
|
|
|
|
|
+ int dot = name.lastIndexOf('.');
|
|
|
|
|
+ String base = dot > 0 ? name.substring(0, dot) : name;
|
|
|
|
|
+ String ext = dot > 0 ? name.substring(dot) : "";
|
|
|
|
|
+ for (int i = 1; i < 1000; i++) {
|
|
|
|
|
+ Path candidate = dir.resolve(base + "-" + i + ext);
|
|
|
|
|
+ if (!Files.exists(candidate)) {
|
|
|
|
|
+ return candidate;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return dir.resolve(base + "-" + UUID.randomUUID() + ext);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* LRU 清理 py-out 超额执行目录。
|
|
* LRU 清理 py-out 超额执行目录。
|
|
|
* <b>跳过在途执行</b>(ACTIVE_EXEC_IDS):并发执行时,刚产出图片的目录
|
|
* <b>跳过在途执行</b>(ACTIVE_EXEC_IDS):并发执行时,刚产出图片的目录
|