fix: update tool installation checks and refactor stdout JSON parsing in AI modules

This commit is contained in:
ashim-hq
2026-04-19 12:13:26 +08:00
parent dc7ba119f8
commit 12c4d4de6f
14 changed files with 43 additions and 39 deletions
+2 -2
View File
@@ -2,7 +2,7 @@ import { randomUUID } from "node:crypto";
import { readFile, unlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export interface RemoveBackgroundOptions {
model?: string;
@@ -29,7 +29,7 @@ export async function removeBackground(
{ onProgress, timeout },
);
const result = JSON.parse(stdout);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Background removal failed");
}
+8 -3
View File
@@ -100,9 +100,7 @@ function startDispatcher(): ChildProcess | null {
if (parsed.ready === true) {
dispatcherReady = true;
dispatcherGpuAvailable = parsed.gpu === true;
console.log(
`[bridge] Python dispatcher ready (GPU: ${parsed.gpu === true})`,
);
console.log(`[bridge] Python dispatcher ready (GPU: ${parsed.gpu === true})`);
continue;
}
@@ -408,3 +406,10 @@ export function runPythonWithProgress(
// Fall back to per-request spawning
return runPythonPerRequest(scriptName, args, options);
}
// biome-ignore lint/suspicious/noExplicitAny: matches JSON.parse return type
export function parseStdoutJson(stdout: string): any {
const match = stdout.match(/\{[\s\S]*\}$/);
if (!match) throw new Error("No JSON response from Python script");
return JSON.parse(match[0]);
}
+2 -2
View File
@@ -1,6 +1,6 @@
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export interface ColorizeOptions {
intensity?: number;
@@ -30,7 +30,7 @@ export async function colorize(
{ onProgress },
);
const result = JSON.parse(stdout);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Colorization failed");
}
+3 -3
View File
@@ -1,7 +1,7 @@
import { readFile, unlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export interface BlurFacesOptions {
blurRadius?: number;
@@ -46,7 +46,7 @@ export async function blurFaces(
{ onProgress },
);
const result = JSON.parse(stdout);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Face detection failed");
}
@@ -74,7 +74,7 @@ export async function detectFaces(
{ onProgress },
);
const result = JSON.parse(stdout);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Face detection failed");
}
+2 -2
View File
@@ -1,6 +1,6 @@
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export interface EnhanceFacesOptions {
model?: "auto" | "gfpgan" | "codeformer";
@@ -32,7 +32,7 @@ export async function enhanceFaces(
{ onProgress },
);
const result = JSON.parse(stdout);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Face enhancement failed");
}
+2 -2
View File
@@ -1,7 +1,7 @@
import { unlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export interface FaceLandmarkPoint {
x: number;
@@ -40,7 +40,7 @@ export async function detectFaceLandmarks(
{ onProgress },
);
const result = JSON.parse(stdout);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Face landmark detection failed");
}
+2 -2
View File
@@ -1,6 +1,6 @@
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export async function inpaint(
inputBuffer: Buffer,
@@ -19,7 +19,7 @@ export async function inpaint(
onProgress,
});
const result = JSON.parse(stdout);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Inpainting failed");
}
+2 -2
View File
@@ -1,6 +1,6 @@
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export interface NoiseRemovalOptions {
tier?: string;
@@ -35,7 +35,7 @@ export async function noiseRemoval(
{ onProgress },
);
const result = JSON.parse(stdout);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Noise removal failed");
}
+2 -2
View File
@@ -1,7 +1,7 @@
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import sharp from "sharp";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export type OcrQuality = "fast" | "balanced" | "best";
@@ -36,7 +36,7 @@ export async function extractText(
timeout: 600_000, // 10 min timeout for VLM on CPU
});
const result = JSON.parse(stdout);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "OCR failed");
}
+2 -2
View File
@@ -1,6 +1,6 @@
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export interface RedEyeRemovalOptions {
sensitivity?: number;
@@ -34,7 +34,7 @@ export async function removeRedEye(
{ onProgress },
);
const result = JSON.parse(stdout);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Red eye removal failed");
}
+2 -2
View File
@@ -1,6 +1,6 @@
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export interface RestorePhotoOptions {
mode?: string;
@@ -39,7 +39,7 @@ export async function restorePhoto(
{ onProgress },
);
const result = JSON.parse(stdout);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Photo restoration failed");
}
+2 -2
View File
@@ -1,6 +1,6 @@
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export interface UpscaleOptions {
scale?: number;
@@ -35,7 +35,7 @@ export async function upscale(
{ onProgress },
);
const result = JSON.parse(stdout);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Upscaling failed");
}