feat: add FEATURE_NOT_INSTALLED guards to API tool routes

Return 501 with structured error when an AI tool's feature bundle
is not installed, preventing Python ImportError crashes. Guards
added to tool-factory, batch, pipeline (both validation loops),
and restore-photo custom route.
This commit is contained in:
ashim-hq
2026-04-18 02:40:16 +08:00
parent 7ffbd5e3c6
commit 3c3aa74e98
4 changed files with 67 additions and 0 deletions
@@ -2,10 +2,12 @@ import { randomUUID } from "node:crypto";
import { writeFile } from "node:fs/promises";
import { basename, join } from "node:path";
import { restorePhoto } from "@ashim/ai";
import { getBundleForTool } from "@ashim/shared";
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import sharp from "sharp";
import { z } from "zod";
import { autoOrient } from "../../lib/auto-orient.js";
import { isToolInstalled } from "../../lib/feature-status.js";
import { validateImageBuffer } from "../../lib/file-validation.js";
import { decodeHeic } from "../../lib/heic-converter.js";
import { resolveOutputFormat } from "../../lib/output-format.js";
@@ -67,6 +69,18 @@ export function registerRestorePhoto(app: FastifyInstance) {
return reply.status(400).send({ error: `Invalid image: ${validation.reason}` });
}
// Guard: check if the photo restoration feature bundle is installed
if (!isToolInstalled("restore-photo")) {
const bundle = getBundleForTool("restore-photo");
return reply.status(501).send({
error: "Feature not installed",
code: "FEATURE_NOT_INSTALLED",
feature: "photo-restoration",
featureName: bundle?.name ?? "Photo Restoration",
estimatedSize: bundle?.estimatedSize ?? "unknown",
});
}
try {
const settings = settingsSchema.parse(settingsRaw ? JSON.parse(settingsRaw) : {});