fix(lightweight-extension): 输出前缀绑定、拖拽导入与前端入口修复
- runtime_binding: 验证时排除输入 sidecar 输出,目录参数跟随输出前缀重命名 - import-folder: 改 multipart 二进制上传,规避大文件 base64 报 Invalid string length - main: 前端入口切回 index.html,conda 路径兜底 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -135,33 +135,18 @@
|
||||
return null;
|
||||
}
|
||||
|
||||
async function encodeBase64(file) {
|
||||
const buffer = await file.arrayBuffer();
|
||||
const bytes = new Uint8Array(buffer);
|
||||
let binary = '';
|
||||
const chunkSize = 0x8000;
|
||||
for (let index = 0; index < bytes.length; index += chunkSize) {
|
||||
binary += String.fromCharCode(...bytes.subarray(index, index + chunkSize));
|
||||
}
|
||||
return btoa(binary);
|
||||
}
|
||||
|
||||
async function importFolderToJob(hooks, rootName, files) {
|
||||
const payload = {
|
||||
job_id: String(hooks.jobInfo.job_id || '').trim(),
|
||||
root_name: rootName,
|
||||
files: [],
|
||||
relative_paths: [],
|
||||
};
|
||||
const form = new FormData();
|
||||
form.append('job_id', String(hooks.jobInfo.job_id || '').trim());
|
||||
form.append('root_name', rootName);
|
||||
for (const file of files) {
|
||||
payload.files.push(await encodeBase64(file));
|
||||
payload.relative_paths.push(normalizedRelativePath(file));
|
||||
form.append('files', file, normalizedRelativePath(file));
|
||||
form.append('relative_paths', normalizedRelativePath(file));
|
||||
}
|
||||
const response = await hooks.uploadExtensionWithProgress(
|
||||
'/api/job/import-folder',
|
||||
JSON.stringify(payload),
|
||||
form,
|
||||
'正在镜像导入 Job',
|
||||
{ 'Content-Type': 'application/json' },
|
||||
);
|
||||
const data = await response.json();
|
||||
if (!response.ok || !data.success) {
|
||||
|
||||
+22
-24
@@ -6,7 +6,6 @@ BotanicalBaseAgent - FastAPI Backend
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import base64
|
||||
import csv
|
||||
import asyncio
|
||||
import hashlib
|
||||
@@ -8830,7 +8829,7 @@ def run_stage_sync(task_id: str, stage: str, job_id: str, params: dict):
|
||||
async def root():
|
||||
"""返回Vue3前端页面"""
|
||||
return FileResponse(
|
||||
Path(__file__).parent / "index_ml.html",
|
||||
Path(__file__).parent / "index.html",
|
||||
headers={
|
||||
"Cache-Control": "no-cache, no-store, must-revalidate",
|
||||
"Pragma": "no-cache",
|
||||
@@ -9059,34 +9058,29 @@ async def upload_file(files: List[UploadFile] = File(...), job_id: str = Form(""
|
||||
}
|
||||
|
||||
|
||||
class JobImportFolderRequest(BaseModel):
|
||||
job_id: str
|
||||
root_name: str = ""
|
||||
files: List[str] = Field(default_factory=list)
|
||||
relative_paths: List[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
@app.post("/api/job/import-folder")
|
||||
async def import_job_folder(req: JobImportFolderRequest):
|
||||
job_id = str(req.job_id or "").strip()
|
||||
async def import_job_folder(
|
||||
job_id: str = Form(...),
|
||||
root_name: str = Form(""),
|
||||
files: List[UploadFile] = File(...),
|
||||
relative_paths: List[str] = Form(...),
|
||||
):
|
||||
job_id = str(job_id or "").strip()
|
||||
if not job_id:
|
||||
raise HTTPException(status_code=400, detail="job_id is required")
|
||||
if len(req.files) != len(req.relative_paths):
|
||||
if len(files) != len(relative_paths):
|
||||
raise HTTPException(status_code=400, detail="files and relative_paths length mismatch")
|
||||
|
||||
root_name = _safe_upload_name(req.root_name or "lightweight_extension_drop")
|
||||
root_name = _safe_upload_name(root_name or "lightweight_extension_drop")
|
||||
import_root = INPUT_ROOT / job_id / root_name
|
||||
import_root.mkdir(parents=True, exist_ok=True)
|
||||
saved_files: List[str] = []
|
||||
|
||||
for encoded, raw_relative in zip(req.files, req.relative_paths):
|
||||
for upload, raw_relative in zip(files, relative_paths):
|
||||
relative = Path(str(raw_relative or "").strip())
|
||||
if not raw_relative or relative.is_absolute() or any(part in {"", ".", ".."} for part in relative.parts):
|
||||
raise HTTPException(status_code=400, detail=f"invalid relative path: {raw_relative}")
|
||||
try:
|
||||
content = base64.b64decode(str(encoded or "").encode("utf-8"), validate=True)
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=400, detail=f"invalid base64 payload for {raw_relative}: {exc}") from exc
|
||||
content = await upload.read()
|
||||
destination = import_root / relative
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
destination.write_bytes(content)
|
||||
@@ -9185,6 +9179,10 @@ class ExtensionStageEnvResolveRequest(BaseModel):
|
||||
env_declaration: Optional[str] = None
|
||||
|
||||
|
||||
def _conda_exe() -> str:
|
||||
"""Resolve the conda executable even when it is not on the server process's PATH."""
|
||||
return shutil.which("conda") or "/home/xzq/miniconda3/bin/conda"
|
||||
|
||||
|
||||
@app.get("/api/extension-stages/envs")
|
||||
async def list_extension_stage_envs():
|
||||
@@ -9192,7 +9190,7 @@ async def list_extension_stage_envs():
|
||||
import json as _json
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"conda", "env", "list", "--json",
|
||||
_conda_exe(), "env", "list", "--json",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
@@ -9200,7 +9198,7 @@ async def list_extension_stage_envs():
|
||||
if proc.returncode != 0:
|
||||
# fallback: try parsing plain-text output
|
||||
proc2 = await asyncio.create_subprocess_exec(
|
||||
"conda", "env", "list",
|
||||
_conda_exe(), "env", "list",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
@@ -9249,7 +9247,7 @@ async def _available_extension_env_names() -> List[str]:
|
||||
|
||||
async def _python_packages_for_env(env_name: str) -> set[str]:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"conda", "run", "-n", env_name, "python", "-c",
|
||||
_conda_exe(), "run", "-n", env_name, "python", "-c",
|
||||
"import json, importlib.metadata as m; print(json.dumps(sorted({dist.metadata.get('Name', '') for dist in m.distributions() if dist.metadata.get('Name', '')})))",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
@@ -9266,7 +9264,7 @@ async def _python_packages_for_env(env_name: str) -> set[str]:
|
||||
|
||||
async def _r_packages_for_env(env_name: str) -> set[str]:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"conda", "run", "-n", env_name, "Rscript", "-e",
|
||||
_conda_exe(), "run", "-n", env_name, "Rscript", "-e",
|
||||
"cat(paste(sort(rownames(installed.packages())), collapse='\n'))",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
@@ -9283,7 +9281,7 @@ async def resolve_extension_stage_env(req: ExtensionStageEnvResolveRequest):
|
||||
if req.env_declaration:
|
||||
env = req.env_declaration.strip()
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"conda", "run", "-n", env, "python", "--version",
|
||||
_conda_exe(), "run", "-n", env, "python", "--version",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
@@ -9309,7 +9307,7 @@ async def resolve_extension_stage_env(req: ExtensionStageEnvResolveRequest):
|
||||
|
||||
for dep in list(dict.fromkeys(d.lower() for d in python_imports + r_packages)):
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"conda", "run", "-n", dep, "python", "--version",
|
||||
_conda_exe(), "run", "-n", dep, "python", "--version",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user