diff --git a/content/collections/tools/blend-collage.json b/content/collections/tools/blend-collage.json index 4cc752b..ea339ce 100644 --- a/content/collections/tools/blend-collage.json +++ b/content/collections/tools/blend-collage.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": false, - "pageType": "two-images", + "pageType": "image-thumbnail", "showThumbnailsSection": true, "thumbnailPlacement": "after-upload", "thumbnails": [ diff --git a/content/collections/tools/caricature-hair-face.json b/content/collections/tools/caricature-hair-face.json index 05111da..b2e21b6 100644 --- a/content/collections/tools/caricature-hair-face.json +++ b/content/collections/tools/caricature-hair-face.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": false, - "pageType": "mask-editor", + "pageType": "masking", "showThumbnailsSection": false, "thumbnailPlacement": "after-upload", "maskingConfig": { diff --git a/content/collections/tools/couple-dress-changer.json b/content/collections/tools/couple-dress-changer.json index c3e5dc6..e37f907 100644 --- a/content/collections/tools/couple-dress-changer.json +++ b/content/collections/tools/couple-dress-changer.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": true, - "pageType": "two-images", + "pageType": "image-thumbnail", "showThumbnailsSection": true, "thumbnailPlacement": "after-upload", "thumbnails": [ diff --git a/content/collections/tools/dress-changer.json b/content/collections/tools/dress-changer.json index 5b8abb3..1159764 100644 --- a/content/collections/tools/dress-changer.json +++ b/content/collections/tools/dress-changer.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": true, - "pageType": "mask-editor", + "pageType": "image-thumbnail", "showThumbnailsSection": true, "thumbnailPlacement": "after-upload", "thumbnails": [ diff --git a/content/collections/tools/dress-pattern.json b/content/collections/tools/dress-pattern.json index c227120..60b2db0 100644 --- a/content/collections/tools/dress-pattern.json +++ b/content/collections/tools/dress-pattern.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": false, - "pageType": "mask-editor", + "pageType": "image-thumbnail", "showThumbnailsSection": true, "thumbnailPlacement": "after-upload", "thumbnails": [ diff --git a/content/collections/tools/dress-replacer.json b/content/collections/tools/dress-replacer.json index 915d32e..367aaf9 100644 --- a/content/collections/tools/dress-replacer.json +++ b/content/collections/tools/dress-replacer.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": false, - "pageType": "mask-editor", + "pageType": "masking", "showThumbnailsSection": false, "thumbnailPlacement": "after-upload", "maskingConfig": { diff --git a/content/collections/tools/eye-color-changer.json b/content/collections/tools/eye-color-changer.json index 884305b..008a8ee 100644 --- a/content/collections/tools/eye-color-changer.json +++ b/content/collections/tools/eye-color-changer.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": true, - "pageType": "mask-editor", + "pageType": "image-thumbnail", "showThumbnailsSection": true, "thumbnailPlacement": "after-upload", "thumbnails": [ diff --git a/content/collections/tools/facial-expressions.json b/content/collections/tools/facial-expressions.json index 79d4740..0c72be9 100644 --- a/content/collections/tools/facial-expressions.json +++ b/content/collections/tools/facial-expressions.json @@ -14,7 +14,7 @@ "homeApp": "portrait-app", "alsoIn": [], "featured": false, - "pageType": "mask-editor", + "pageType": "image-thumbnail", "showThumbnailsSection": true, "thumbnailPlacement": "after-upload", "thumbnails": [ diff --git a/content/collections/tools/hair-color-changer.json b/content/collections/tools/hair-color-changer.json index 3488aa2..753ab46 100644 --- a/content/collections/tools/hair-color-changer.json +++ b/content/collections/tools/hair-color-changer.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": true, - "pageType": "mask-editor", + "pageType": "image-thumbnail", "showThumbnailsSection": true, "thumbnailPlacement": "after-upload", "thumbnails": [ diff --git a/content/collections/tools/hair-style-changer.json b/content/collections/tools/hair-style-changer.json index b38b466..1b630e8 100644 --- a/content/collections/tools/hair-style-changer.json +++ b/content/collections/tools/hair-style-changer.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": true, - "pageType": "mask-editor", + "pageType": "image-thumbnail", "showThumbnailsSection": true, "thumbnailPlacement": "after-upload", "thumbnails": [ diff --git a/content/collections/tools/hairstyles.json b/content/collections/tools/hairstyles.json index 8c9facb..cb6cdd1 100644 --- a/content/collections/tools/hairstyles.json +++ b/content/collections/tools/hairstyles.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": true, - "pageType": "mask-editor", + "pageType": "image-thumbnail", "showThumbnailsSection": true, "thumbnailPlacement": "after-upload", "thumbnails": [ diff --git a/content/collections/tools/image-outpainting.json b/content/collections/tools/image-outpainting.json index 0156a78..85a5fbc 100644 --- a/content/collections/tools/image-outpainting.json +++ b/content/collections/tools/image-outpainting.json @@ -14,7 +14,7 @@ "homeApp": "bg-cleaner-app", "alsoIn": [], "featured": false, - "pageType": "two-images", + "pageType": "masking", "showThumbnailsSection": false, "thumbnailPlacement": "after-upload", "twoImagesConfig": { diff --git a/content/collections/tools/inpaint-object-replacer.json b/content/collections/tools/inpaint-object-replacer.json index 9878df7..6d198ca 100644 --- a/content/collections/tools/inpaint-object-replacer.json +++ b/content/collections/tools/inpaint-object-replacer.json @@ -14,7 +14,7 @@ "homeApp": "bg-cleaner-app", "alsoIn": [], "featured": false, - "pageType": "two-images", + "pageType": "masking", "showThumbnailsSection": false, "thumbnailPlacement": "after-upload", "twoImagesConfig": { diff --git a/content/collections/tools/object-eraser.json b/content/collections/tools/object-eraser.json index 6aea5a5..3f42f9a 100644 --- a/content/collections/tools/object-eraser.json +++ b/content/collections/tools/object-eraser.json @@ -14,7 +14,7 @@ "homeApp": "bg-cleaner-app", "alsoIn": [], "featured": false, - "pageType": "two-images", + "pageType": "masking", "showThumbnailsSection": false, "thumbnailPlacement": "after-upload", "twoImagesConfig": { diff --git a/content/collections/tools/realistic-background-changer.json b/content/collections/tools/realistic-background-changer.json index 42c1b56..244be6f 100644 --- a/content/collections/tools/realistic-background-changer.json +++ b/content/collections/tools/realistic-background-changer.json @@ -14,7 +14,7 @@ "homeApp": "bg-cleaner-app", "alsoIn": [], "featured": false, - "pageType": "mask-editor", + "pageType": "masking", "showThumbnailsSection": false, "thumbnailPlacement": "after-upload", "maskingConfig": { diff --git a/content/collections/tools/stable-diffusion.json b/content/collections/tools/stable-diffusion.json index a59daf2..5986b30 100644 --- a/content/collections/tools/stable-diffusion.json +++ b/content/collections/tools/stable-diffusion.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": false, - "pageType": "mask-editor", + "pageType": "image-only", "showThumbnailsSection": false, "thumbnailPlacement": "after-upload", "maskingConfig": { diff --git a/content/collections/tools/text-to-caricature.json b/content/collections/tools/text-to-caricature.json index 158f3dc..7111015 100644 --- a/content/collections/tools/text-to-caricature.json +++ b/content/collections/tools/text-to-caricature.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": false, - "pageType": "mask-editor", + "pageType": "image-thumbnail", "showThumbnailsSection": true, "thumbnailPlacement": "after-upload", "thumbnails": [ diff --git a/content/collections/tools/text-to-cartoon.json b/content/collections/tools/text-to-cartoon.json index 1b1154c..1881793 100644 --- a/content/collections/tools/text-to-cartoon.json +++ b/content/collections/tools/text-to-cartoon.json @@ -14,7 +14,7 @@ "homeApp": "art-studio-app", "alsoIn": [], "featured": false, - "pageType": "mask-editor", + "pageType": "image-thumbnail", "showThumbnailsSection": true, "thumbnailPlacement": "after-upload", "thumbnails": [ diff --git a/src/app/api/try/[toolId]/route.ts b/src/app/api/try/[toolId]/route.ts index 8063f1e..24aafc5 100644 --- a/src/app/api/try/[toolId]/route.ts +++ b/src/app/api/try/[toolId]/route.ts @@ -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 = {}; diff --git a/src/components/tools/DynamicToolWorkspace.tsx b/src/components/tools/DynamicToolWorkspace.tsx index 79f3858..4113fdb 100644 --- a/src/components/tools/DynamicToolWorkspace.tsx +++ b/src/components/tools/DynamicToolWorkspace.tsx @@ -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}`, { diff --git a/src/components/tools/ThumbnailsSection.tsx b/src/components/tools/ThumbnailsSection.tsx index 3698698..3906cd9 100644 --- a/src/components/tools/ThumbnailsSection.tsx +++ b/src/components/tools/ThumbnailsSection.tsx @@ -58,7 +58,7 @@ export function ThumbnailsSection({
{/* eslint-disable-next-line @next/next/no-img-element */} {label} @@ -121,7 +121,7 @@ export function ThumbnailsSection({
{/* eslint-disable-next-line @next/next/no-img-element */} {label} diff --git a/src/lib/api/requestBuilder.ts b/src/lib/api/requestBuilder.ts index b11b56e..73906b3 100644 --- a/src/lib/api/requestBuilder.ts +++ b/src/lib/api/requestBuilder.ts @@ -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);