fix: AI feature install failures — missing rembg session and Fastify 415 (#102, #103)

Register custom BiRefNet-matting ONNX session in install_feature.py so
rembg.new_session("birefnet-matting") no longer raises ValueError during
on-demand installs. The session was already registered in remove_bg.py
(runtime) and download_models.py (build-time) but was missed in the
install path, causing background-removal bundle installs to always fail.

Send JSON body on install/uninstall POST requests to avoid Fastify 5's
strict content-type parser rejecting body-less POSTs with 415.

Fix error message extraction to preserve structured {"error": ...} JSON
from the Python script and filter out pthread_setaffinity_np noise.
This commit is contained in:
SnapOtter
2026-04-27 02:38:17 +08:00
parent 02a84b8741
commit 4f81b29fbc
4 changed files with 75 additions and 9 deletions
+26 -3
View File
@@ -202,9 +202,32 @@ export async function registerFeatureRoutes(app: FastifyInstance): Promise<void>
duration_ms: Date.now() - installStartTime,
});
} else {
const errorDetail =
lastStderrLines.filter((l) => !l.startsWith("{")).join("\n") || stdoutBuffer.trim();
const errorMsg = errorDetail || `Install failed with exit code ${code}`;
// Extract the structured error from Python's fail() function first.
// fail() writes {"error": "..."} to stderr — prefer this over raw lines.
let errorMsg: string | undefined;
for (let i = lastStderrLines.length - 1; i >= 0; i--) {
const line = lastStderrLines[i];
if (line.startsWith("{")) {
try {
const parsed = JSON.parse(line) as Record<string, unknown>;
if (typeof parsed.error === "string") {
errorMsg = parsed.error;
break;
}
} catch {
// Not valid JSON
}
}
}
if (!errorMsg) {
const meaningful = lastStderrLines.filter(
(l) => !l.startsWith("{") && !l.includes("pthread_setaffinity_np"),
);
errorMsg =
meaningful.join("\n") ||
stdoutBuffer.trim() ||
`Install failed with exit code ${code}`;
}
setInstallProgress(bundleId, null, errorMsg);
updateSingleFileProgress({ jobId, phase: "failed", percent: 0, error: errorMsg });
}