fix(tools): correct toolpage archetypes across all tools and ensure robust form field mapping

This commit is contained in:
2026-11-11 17:27:23 +05:30
parent 8f4e7913fb
commit 690398f715
22 changed files with 165 additions and 55 deletions
+55 -5
View File
@@ -62,11 +62,61 @@ export async function POST(req: NextRequest, { params }: { params: { toolId: str
try {
const formData = await req.formData();
const file = formData.get('image') as File | null;
const maskFile = formData.get('mask') as File | null;
const secondaryFile = (formData.get('reference_image') as File | null) || (formData.get('image2') as File | null);
const prompt = (formData.get('prompt') as string) || (formData.get('text') as string) || '';
const style = (formData.get('style') as string) || '';
// Primary image extraction: check 'image' first, then common aliases
let file = formData.get('image') as File | null;
if (!file || typeof file === 'string') {
const candidateKeys = ['image0', 'file', 'source', 'original_image', 'source1', 'input_image'];
for (const key of candidateKeys) {
const val = formData.get(key) as File | null;
if (val && typeof val !== 'string' && val.size > 0) {
file = val;
break;
}
}
}
// Mask image extraction: check 'mask' first, then common aliases
let maskFile = formData.get('mask') as File | null;
if (!maskFile || typeof maskFile === 'string') {
const candidateMaskKeys = ['mask_image', 'mask0', 'eye_mask', 'mask_hair'];
for (const key of candidateMaskKeys) {
const val = formData.get(key) as File | null;
if (val && typeof val !== 'string' && val.size > 0) {
maskFile = val;
break;
}
}
}
// Secondary / Reference image extraction
let secondaryFile =
(formData.get('reference_image') as File | null) ||
(formData.get('reference') as File | null) ||
(formData.get('image2') as File | null) ||
(formData.get('source2') as File | null) ||
(formData.get('target') as File | null) ||
(formData.get('bg_image') as File | null) ||
(formData.get('face_segmented_image') as File | null) ||
(formData.get('secondImage') as File | null);
if (secondaryFile && typeof secondaryFile === 'string') {
secondaryFile = null;
}
const prompt =
(formData.get('prompt') as string) ||
(formData.get('text') as string) ||
(formData.get('userPrompt') as string) ||
(formData.get('custom_prompt') as string) ||
'';
const style =
(formData.get('style') as string) ||
(formData.get('effectId') as string) ||
(formData.get('colorId') as string) ||
(formData.get('effect_id') as string) ||
(formData.get('dress') as string) ||
(formData.get('expression') as string) ||
'';
const variantId = (formData.get('variant') as string) || tool.tryIt.endpointRef?.defaultVariant;
const optionsRaw = formData.get('options') as string | null;
let options: Record<string, any> = {};
+46 -14
View File
@@ -47,18 +47,31 @@ export function DynamicToolWorkspace({
variants = [],
locale = 'en',
}: DynamicToolWorkspaceProps) {
// Determine tool archetype from JSON (fallback intelligently based on category/id)
const pageType: ToolPageType = tool.pageType || (
tool.id.includes('lama') || tool.id.includes('eraser') || tool.id.includes('inpaint')
? 'masking'
: tool.thumbnails && tool.thumbnails.length > 0
? 'image-thumbnail'
: tool.id.includes('text') || tool.id.includes('prompt')
? 'text-to-image'
: tool.id.includes('swap') || tool.id.includes('blend')
? 'two-images'
: 'image-only'
);
// Normalize and determine tool archetype from JSON with resilient fallbacks
const resolvePageType = (rawType?: string): ToolPageType => {
if (rawType === 'masking' || rawType === 'two-images' || rawType === 'text-to-image' || rawType === 'image-thumbnail' || rawType === 'image-only') {
return rawType;
}
if (rawType === 'mask-editor') {
return (tool.thumbnails && tool.thumbnails.length > 0) ? 'image-thumbnail' : 'masking';
}
// Intelligent heuristic fallback based on tool characteristics
if (tool.id.includes('lama') || tool.id.includes('eraser') || tool.id.includes('inpaint') || tool.id.includes('replacer') || tool.id.includes('outpaint')) {
return 'masking';
}
if (tool.thumbnails && tool.thumbnails.length > 0) {
return 'image-thumbnail';
}
if (tool.id.includes('text') || tool.id.includes('prompt')) {
return 'text-to-image';
}
if (tool.id.includes('swap') || tool.id.includes('relight') || tool.id.includes('couple-composite')) {
return 'two-images';
}
return 'image-only';
};
const pageType: ToolPageType = resolvePageType(tool.pageType);
// View Mode: 'landing' (tool overview & upload) vs 'studio' (editing workspace)
const [viewMode, setViewMode] = useState<'landing' | 'studio'>('landing');
@@ -216,11 +229,17 @@ export function DynamicToolWorkspace({
const compUrl = URL.createObjectURL(fileToSend);
setComparisonBeforeUrl(compUrl);
formData.append(imageParamName, fileToSend, 'photo.jpg');
if (imageParamName !== 'image') {
formData.append('image', fileToSend, 'photo.jpg');
}
} else if (primaryPreview) {
setComparisonBeforeUrl(primaryPreview);
const res = await fetch(primaryPreview);
const blob = await res.blob();
formData.append(imageParamName, blob, 'sample.jpg');
if (imageParamName !== 'image') {
formData.append('image', blob, 'sample.jpg');
}
} else {
setStatus('error');
setErrorMessage('Please upload or select an image first.');
@@ -233,10 +252,16 @@ export function DynamicToolWorkspace({
const secondParamName = tool.twoImagesConfig?.secondImageParam || 'reference_image';
if (secondaryFile) {
formData.append(secondParamName, secondaryFile);
if (secondParamName !== 'reference_image') {
formData.append('reference_image', secondaryFile);
}
} else if (secondaryPreview) {
const res = await fetch(secondaryPreview);
const blob = await res.blob();
formData.append(secondParamName, blob, 'secondary.jpg');
if (secondParamName !== 'reference_image') {
formData.append('reference_image', blob, 'secondary.jpg');
}
} else {
setStatus('error');
setErrorMessage('Please upload the second reference image.');
@@ -251,6 +276,9 @@ export function DynamicToolWorkspace({
const maskBlob = await getMaskBlobRef.current();
if (maskBlob) {
formData.append(maskParamName, maskBlob, 'mask.png');
if (maskParamName !== 'mask') {
formData.append('mask', maskBlob, 'mask.png');
}
}
}
}
@@ -269,8 +297,12 @@ export function DynamicToolWorkspace({
// 5. Selected Thumbnail / Style Preset
if (selectedThumbnail) {
formData.append('style', selectedThumbnail.value);
formData.append('variant', selectedThumbnail.value);
const presetVal = selectedThumbnail.value || selectedThumbnail.id;
formData.append('style', presetVal);
formData.append('variant', presetVal);
formData.append('effectId', presetVal);
formData.append('colorId', presetVal);
formData.append('effect_id', presetVal);
}
const res = await fetch(`/api/try/${tool.id}`, {
+2 -2
View File
@@ -58,7 +58,7 @@ export function ThumbnailsSection({
<div className="relative w-14 h-14 sm:w-16 sm:h-16 rounded-xl overflow-hidden bg-paper-2">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={item.thumbnail || item.preview || ''}
src={item.thumbnail || item.preview || (item as any).previewUrl || ''}
alt={label}
className="w-full h-full object-cover transition-transform duration-300 group-hover:scale-105"
/>
@@ -121,7 +121,7 @@ export function ThumbnailsSection({
<div className="relative aspect-square w-full rounded-xl overflow-hidden bg-paper-2 mb-3">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={item.thumbnail || item.preview || ''}
src={item.thumbnail || item.preview || (item as any).previewUrl || ''}
alt={label}
className="w-full h-full object-cover transition-transform duration-500 group-hover:scale-105"
/>
+44 -16
View File
@@ -101,28 +101,41 @@ export async function executeBackendTryIt(
const formData = new FormData();
const blob = new Blob([new Uint8Array(imageBuffer)]);
// Find primary file parameter name (e.g. 'image0', 'image', 'original_image')
const fileParam = endpoint.params.find(
(p) => p.kind === 'file' && !p.name.toLowerCase().includes('mask') && !p.name.toLowerCase().includes('ref')
) || { name: 'image0' };
formData.append(fileParam.name, blob, filename);
// Find primary file parameter name
// Prioritize based on known conventions: 'original_image', 'image0', 'image', 'source', 'source1', 'file'
const primaryCandidates = ['original_image', 'image0', 'image', 'source', 'source1', 'file'];
const matchedPrimary = endpoint.params.find(
(p) => p.kind === 'file' && primaryCandidates.includes(p.name)
) || endpoint.params.find(
(p) => p.kind === 'file' && !p.name.toLowerCase().includes('mask') && !p.name.toLowerCase().includes('ref') && !p.name.toLowerCase().includes('target') && !p.name.toLowerCase().includes('bg') && !p.name.toLowerCase().includes('source2')
);
const fileParamName = matchedPrimary?.name || 'image0';
formData.append(fileParamName, blob, filename);
// If mask buffer provided (Inpainting / Custom Mask / Auto Mask)
if (options.maskBuffer && Buffer.isBuffer(options.maskBuffer)) {
const maskParam = endpoint.params.find(
(p) => p.name.toLowerCase().includes('mask')
) || { name: 'mask_image' };
(p) => p.kind === 'file' && p.name.toLowerCase().includes('mask')
);
const maskParamName = maskParam?.name || 'mask_image';
const maskBlob = new Blob([new Uint8Array(options.maskBuffer)]);
formData.append(maskParam.name, maskBlob, options.maskFilename || 'mask.png');
formData.append(maskParamName, maskBlob, options.maskFilename || 'mask.png');
}
// If secondary image buffer provided (Two-Images: Face Swap, Blend Collage, Couple Outfits)
// If secondary image buffer provided (Two-Images: Face Swap, Relighting, Try-on)
if (options.secondaryBuffer && Buffer.isBuffer(options.secondaryBuffer)) {
const secParam = endpoint.params.find(
(p) => p.kind === 'file' && p.name !== fileParam.name && !p.name.toLowerCase().includes('mask')
) || { name: 'reference_image' };
const secCandidates = ['target', 'bg_image', 'face_segmented_image', 'source2', 'reference_image', 'reference', 'image2'];
const matchedSec = endpoint.params.find(
(p) => p.kind === 'file' && secCandidates.includes(p.name)
) || endpoint.params.find(
(p) => p.kind === 'file' && p.name !== fileParamName && !p.name.toLowerCase().includes('mask')
);
const secParamName = matchedSec?.name || 'reference_image';
const secBlob = new Blob([new Uint8Array(options.secondaryBuffer)]);
formData.append(secParam.name, secBlob, options.secondaryFilename || 'reference.jpg');
formData.append(secParamName, secBlob, options.secondaryFilename || 'reference.jpg');
if (endpoint.params.some((p) => p.name === 'target') && !formData.has('target')) {
formData.append('target', secBlob, options.secondaryFilename || 'reference.jpg');
}
}
// Inject Secret Key (PRD §7.11: Secrets never reach browser)
@@ -142,16 +155,31 @@ export async function executeBackendTryIt(
}
}
// Ensure effectId / colorId / style_id synchronization if provided in options
const selectedEffect = options.style || options.effectId || options.colorId || options.effect_id;
// Ensure effectId / colorId / style_id / artid / dress / expression synchronization if provided in options
const selectedEffect = options.style || options.effectId || options.colorId || options.effect_id || options.artId || options.artid || options.dress || options.expression || options.cartoonId;
if (selectedEffect) {
for (const targetKey of ['colorId', 'effectId', 'effect_id', 'style_id', 'artId']) {
for (const targetKey of ['colorId', 'effectId', 'effect_id', 'style_id', 'artId', 'artid', 'cartoonId', 'dress', 'expression', 'style', 'option']) {
if (endpoint.params.some((p) => p.name === targetKey) && !formData.has(targetKey)) {
formData.append(targetKey, String(selectedEffect));
}
}
}
// Populate default values for required / missing parameters from catalog
for (const param of endpoint.params) {
if (param.kind !== 'file' && !formData.has(param.name)) {
if (param.default && param.default !== 'None' && param.default !== 'null') {
let val: any = param.default;
if (typeof val === 'string') {
val = val.replace(/^['"]|['"]$/g, '');
if (val.startsWith('Capped at ')) val = val.replace('Capped at ', '');
if (val === 'Random') val = String(Math.floor(Math.random() * 1000000));
}
formData.append(param.name, String(val));
}
}
}
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), endpoint.limits.timeoutMs || 60000);