Files
SnapOtter/packages/ai/src/restoration.ts
T
SnapOtterandGitHub 6a0768b39d fix(telemetry): surface AI sidecar and DOMException failure reasons in Sentry (#612)
AI sidecar failures reached Sentry as 'Error: Error': the scrubber type-onlys plain Errors and the tool wrappers threw them from result.error. The bridge now exports toSidecarError(), wrapping the sidecar reason in a SafeError (memory-allocation text classifies as operational, the rest as bug); all 14 wrappers use it, plus the dispatcher crash/stdin/spawn rejection paths and parseStdoutJson. toBgRemovalError from #535 delegates to the shared helper.

On the web side, DOMExceptions report their specific name via err.name, so the NATIVE_ERRORS allowlist dropped the whole family's browser-authored messages. It now carries the full WebIDL DOMException name table; messages still pass through url/path redaction.

Bridge-mocking test files switched to importOriginal passthrough mocks.
2026-07-21 23:18:50 +08:00

67 lines
1.7 KiB
TypeScript

import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import sharp from "sharp";
import {
type ProgressCallback,
parseStdoutJson,
runPythonWithProgress,
toSidecarError,
} from "./bridge.js";
export interface RestorePhotoOptions {
scratchRemoval?: boolean;
faceEnhancement?: boolean;
fidelity?: number;
denoise?: boolean;
denoiseStrength?: number;
colorize?: boolean;
colorizeStrength?: number;
}
export interface RestorePhotoResult {
buffer: Buffer;
width: number;
height: number;
steps: string[];
scratchCoverage: number;
facesEnhanced: number;
isGrayscale: boolean;
colorized: boolean;
}
export async function restorePhoto(
inputBuffer: Buffer,
outputDir: string,
options: RestorePhotoOptions = {},
onProgress?: ProgressCallback,
): Promise<RestorePhotoResult> {
const inputPath = join(outputDir, "input_restore.png");
const outputPath = join(outputDir, "output_restore.png");
const pngBuffer = await sharp(inputBuffer).png().toBuffer();
await writeFile(inputPath, pngBuffer);
const { stdout } = await runPythonWithProgress(
"restore.py",
[inputPath, outputPath, JSON.stringify(options)],
{ onProgress },
);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw toSidecarError(result.error, "Photo restoration failed");
}
const actualOutputPath = result.output_path || outputPath;
const buffer = await readFile(actualOutputPath);
return {
buffer,
width: result.width,
height: result.height,
steps: result.steps ?? [],
scratchCoverage: result.scratchCoverage ?? 0,
facesEnhanced: result.facesEnhanced ?? 0,
isGrayscale: result.isGrayscale ?? false,
colorized: result.colorized ?? false,
};
}