fix(tools): correct toolpage archetypes across all tools and ensure robust form field mapping
This commit is contained in:
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": false,
|
"featured": false,
|
||||||
"pageType": "two-images",
|
"pageType": "image-thumbnail",
|
||||||
"showThumbnailsSection": true,
|
"showThumbnailsSection": true,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"thumbnails": [
|
"thumbnails": [
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": false,
|
"featured": false,
|
||||||
"pageType": "mask-editor",
|
"pageType": "masking",
|
||||||
"showThumbnailsSection": false,
|
"showThumbnailsSection": false,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"maskingConfig": {
|
"maskingConfig": {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": true,
|
"featured": true,
|
||||||
"pageType": "two-images",
|
"pageType": "image-thumbnail",
|
||||||
"showThumbnailsSection": true,
|
"showThumbnailsSection": true,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"thumbnails": [
|
"thumbnails": [
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": true,
|
"featured": true,
|
||||||
"pageType": "mask-editor",
|
"pageType": "image-thumbnail",
|
||||||
"showThumbnailsSection": true,
|
"showThumbnailsSection": true,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"thumbnails": [
|
"thumbnails": [
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": false,
|
"featured": false,
|
||||||
"pageType": "mask-editor",
|
"pageType": "image-thumbnail",
|
||||||
"showThumbnailsSection": true,
|
"showThumbnailsSection": true,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"thumbnails": [
|
"thumbnails": [
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": false,
|
"featured": false,
|
||||||
"pageType": "mask-editor",
|
"pageType": "masking",
|
||||||
"showThumbnailsSection": false,
|
"showThumbnailsSection": false,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"maskingConfig": {
|
"maskingConfig": {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": true,
|
"featured": true,
|
||||||
"pageType": "mask-editor",
|
"pageType": "image-thumbnail",
|
||||||
"showThumbnailsSection": true,
|
"showThumbnailsSection": true,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"thumbnails": [
|
"thumbnails": [
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "portrait-app",
|
"homeApp": "portrait-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": false,
|
"featured": false,
|
||||||
"pageType": "mask-editor",
|
"pageType": "image-thumbnail",
|
||||||
"showThumbnailsSection": true,
|
"showThumbnailsSection": true,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"thumbnails": [
|
"thumbnails": [
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": true,
|
"featured": true,
|
||||||
"pageType": "mask-editor",
|
"pageType": "image-thumbnail",
|
||||||
"showThumbnailsSection": true,
|
"showThumbnailsSection": true,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"thumbnails": [
|
"thumbnails": [
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": true,
|
"featured": true,
|
||||||
"pageType": "mask-editor",
|
"pageType": "image-thumbnail",
|
||||||
"showThumbnailsSection": true,
|
"showThumbnailsSection": true,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"thumbnails": [
|
"thumbnails": [
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": true,
|
"featured": true,
|
||||||
"pageType": "mask-editor",
|
"pageType": "image-thumbnail",
|
||||||
"showThumbnailsSection": true,
|
"showThumbnailsSection": true,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"thumbnails": [
|
"thumbnails": [
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "bg-cleaner-app",
|
"homeApp": "bg-cleaner-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": false,
|
"featured": false,
|
||||||
"pageType": "two-images",
|
"pageType": "masking",
|
||||||
"showThumbnailsSection": false,
|
"showThumbnailsSection": false,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"twoImagesConfig": {
|
"twoImagesConfig": {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "bg-cleaner-app",
|
"homeApp": "bg-cleaner-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": false,
|
"featured": false,
|
||||||
"pageType": "two-images",
|
"pageType": "masking",
|
||||||
"showThumbnailsSection": false,
|
"showThumbnailsSection": false,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"twoImagesConfig": {
|
"twoImagesConfig": {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "bg-cleaner-app",
|
"homeApp": "bg-cleaner-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": false,
|
"featured": false,
|
||||||
"pageType": "two-images",
|
"pageType": "masking",
|
||||||
"showThumbnailsSection": false,
|
"showThumbnailsSection": false,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"twoImagesConfig": {
|
"twoImagesConfig": {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "bg-cleaner-app",
|
"homeApp": "bg-cleaner-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": false,
|
"featured": false,
|
||||||
"pageType": "mask-editor",
|
"pageType": "masking",
|
||||||
"showThumbnailsSection": false,
|
"showThumbnailsSection": false,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"maskingConfig": {
|
"maskingConfig": {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": false,
|
"featured": false,
|
||||||
"pageType": "mask-editor",
|
"pageType": "image-only",
|
||||||
"showThumbnailsSection": false,
|
"showThumbnailsSection": false,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"maskingConfig": {
|
"maskingConfig": {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": false,
|
"featured": false,
|
||||||
"pageType": "mask-editor",
|
"pageType": "image-thumbnail",
|
||||||
"showThumbnailsSection": true,
|
"showThumbnailsSection": true,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"thumbnails": [
|
"thumbnails": [
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"homeApp": "art-studio-app",
|
"homeApp": "art-studio-app",
|
||||||
"alsoIn": [],
|
"alsoIn": [],
|
||||||
"featured": false,
|
"featured": false,
|
||||||
"pageType": "mask-editor",
|
"pageType": "image-thumbnail",
|
||||||
"showThumbnailsSection": true,
|
"showThumbnailsSection": true,
|
||||||
"thumbnailPlacement": "after-upload",
|
"thumbnailPlacement": "after-upload",
|
||||||
"thumbnails": [
|
"thumbnails": [
|
||||||
|
|||||||
@@ -62,11 +62,61 @@ export async function POST(req: NextRequest, { params }: { params: { toolId: str
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const formData = await req.formData();
|
const formData = await req.formData();
|
||||||
const file = formData.get('image') as File | null;
|
|
||||||
const maskFile = formData.get('mask') as File | null;
|
// Primary image extraction: check 'image' first, then common aliases
|
||||||
const secondaryFile = (formData.get('reference_image') as File | null) || (formData.get('image2') as File | null);
|
let file = formData.get('image') as File | null;
|
||||||
const prompt = (formData.get('prompt') as string) || (formData.get('text') as string) || '';
|
if (!file || typeof file === 'string') {
|
||||||
const style = (formData.get('style') as 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 variantId = (formData.get('variant') as string) || tool.tryIt.endpointRef?.defaultVariant;
|
||||||
const optionsRaw = formData.get('options') as string | null;
|
const optionsRaw = formData.get('options') as string | null;
|
||||||
let options: Record<string, any> = {};
|
let options: Record<string, any> = {};
|
||||||
|
|||||||
@@ -47,18 +47,31 @@ export function DynamicToolWorkspace({
|
|||||||
variants = [],
|
variants = [],
|
||||||
locale = 'en',
|
locale = 'en',
|
||||||
}: DynamicToolWorkspaceProps) {
|
}: DynamicToolWorkspaceProps) {
|
||||||
// Determine tool archetype from JSON (fallback intelligently based on category/id)
|
// Normalize and determine tool archetype from JSON with resilient fallbacks
|
||||||
const pageType: ToolPageType = tool.pageType || (
|
const resolvePageType = (rawType?: string): ToolPageType => {
|
||||||
tool.id.includes('lama') || tool.id.includes('eraser') || tool.id.includes('inpaint')
|
if (rawType === 'masking' || rawType === 'two-images' || rawType === 'text-to-image' || rawType === 'image-thumbnail' || rawType === 'image-only') {
|
||||||
? 'masking'
|
return rawType;
|
||||||
: tool.thumbnails && tool.thumbnails.length > 0
|
}
|
||||||
? 'image-thumbnail'
|
if (rawType === 'mask-editor') {
|
||||||
: tool.id.includes('text') || tool.id.includes('prompt')
|
return (tool.thumbnails && tool.thumbnails.length > 0) ? 'image-thumbnail' : 'masking';
|
||||||
? 'text-to-image'
|
}
|
||||||
: tool.id.includes('swap') || tool.id.includes('blend')
|
// Intelligent heuristic fallback based on tool characteristics
|
||||||
? 'two-images'
|
if (tool.id.includes('lama') || tool.id.includes('eraser') || tool.id.includes('inpaint') || tool.id.includes('replacer') || tool.id.includes('outpaint')) {
|
||||||
: 'image-only'
|
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)
|
// View Mode: 'landing' (tool overview & upload) vs 'studio' (editing workspace)
|
||||||
const [viewMode, setViewMode] = useState<'landing' | 'studio'>('landing');
|
const [viewMode, setViewMode] = useState<'landing' | 'studio'>('landing');
|
||||||
@@ -216,11 +229,17 @@ export function DynamicToolWorkspace({
|
|||||||
const compUrl = URL.createObjectURL(fileToSend);
|
const compUrl = URL.createObjectURL(fileToSend);
|
||||||
setComparisonBeforeUrl(compUrl);
|
setComparisonBeforeUrl(compUrl);
|
||||||
formData.append(imageParamName, fileToSend, 'photo.jpg');
|
formData.append(imageParamName, fileToSend, 'photo.jpg');
|
||||||
|
if (imageParamName !== 'image') {
|
||||||
|
formData.append('image', fileToSend, 'photo.jpg');
|
||||||
|
}
|
||||||
} else if (primaryPreview) {
|
} else if (primaryPreview) {
|
||||||
setComparisonBeforeUrl(primaryPreview);
|
setComparisonBeforeUrl(primaryPreview);
|
||||||
const res = await fetch(primaryPreview);
|
const res = await fetch(primaryPreview);
|
||||||
const blob = await res.blob();
|
const blob = await res.blob();
|
||||||
formData.append(imageParamName, blob, 'sample.jpg');
|
formData.append(imageParamName, blob, 'sample.jpg');
|
||||||
|
if (imageParamName !== 'image') {
|
||||||
|
formData.append('image', blob, 'sample.jpg');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
setStatus('error');
|
setStatus('error');
|
||||||
setErrorMessage('Please upload or select an image first.');
|
setErrorMessage('Please upload or select an image first.');
|
||||||
@@ -233,10 +252,16 @@ export function DynamicToolWorkspace({
|
|||||||
const secondParamName = tool.twoImagesConfig?.secondImageParam || 'reference_image';
|
const secondParamName = tool.twoImagesConfig?.secondImageParam || 'reference_image';
|
||||||
if (secondaryFile) {
|
if (secondaryFile) {
|
||||||
formData.append(secondParamName, secondaryFile);
|
formData.append(secondParamName, secondaryFile);
|
||||||
|
if (secondParamName !== 'reference_image') {
|
||||||
|
formData.append('reference_image', secondaryFile);
|
||||||
|
}
|
||||||
} else if (secondaryPreview) {
|
} else if (secondaryPreview) {
|
||||||
const res = await fetch(secondaryPreview);
|
const res = await fetch(secondaryPreview);
|
||||||
const blob = await res.blob();
|
const blob = await res.blob();
|
||||||
formData.append(secondParamName, blob, 'secondary.jpg');
|
formData.append(secondParamName, blob, 'secondary.jpg');
|
||||||
|
if (secondParamName !== 'reference_image') {
|
||||||
|
formData.append('reference_image', blob, 'secondary.jpg');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
setStatus('error');
|
setStatus('error');
|
||||||
setErrorMessage('Please upload the second reference image.');
|
setErrorMessage('Please upload the second reference image.');
|
||||||
@@ -251,6 +276,9 @@ export function DynamicToolWorkspace({
|
|||||||
const maskBlob = await getMaskBlobRef.current();
|
const maskBlob = await getMaskBlobRef.current();
|
||||||
if (maskBlob) {
|
if (maskBlob) {
|
||||||
formData.append(maskParamName, maskBlob, 'mask.png');
|
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
|
// 5. Selected Thumbnail / Style Preset
|
||||||
if (selectedThumbnail) {
|
if (selectedThumbnail) {
|
||||||
formData.append('style', selectedThumbnail.value);
|
const presetVal = selectedThumbnail.value || selectedThumbnail.id;
|
||||||
formData.append('variant', selectedThumbnail.value);
|
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}`, {
|
const res = await fetch(`/api/try/${tool.id}`, {
|
||||||
|
|||||||
@@ -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">
|
<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 */}
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||||
<img
|
<img
|
||||||
src={item.thumbnail || item.preview || ''}
|
src={item.thumbnail || item.preview || (item as any).previewUrl || ''}
|
||||||
alt={label}
|
alt={label}
|
||||||
className="w-full h-full object-cover transition-transform duration-300 group-hover:scale-105"
|
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">
|
<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 */}
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||||
<img
|
<img
|
||||||
src={item.thumbnail || item.preview || ''}
|
src={item.thumbnail || item.preview || (item as any).previewUrl || ''}
|
||||||
alt={label}
|
alt={label}
|
||||||
className="w-full h-full object-cover transition-transform duration-500 group-hover:scale-105"
|
className="w-full h-full object-cover transition-transform duration-500 group-hover:scale-105"
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -101,28 +101,41 @@ export async function executeBackendTryIt(
|
|||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
const blob = new Blob([new Uint8Array(imageBuffer)]);
|
const blob = new Blob([new Uint8Array(imageBuffer)]);
|
||||||
|
|
||||||
// Find primary file parameter name (e.g. 'image0', 'image', 'original_image')
|
// Find primary file parameter name
|
||||||
const fileParam = endpoint.params.find(
|
// Prioritize based on known conventions: 'original_image', 'image0', 'image', 'source', 'source1', 'file'
|
||||||
(p) => p.kind === 'file' && !p.name.toLowerCase().includes('mask') && !p.name.toLowerCase().includes('ref')
|
const primaryCandidates = ['original_image', 'image0', 'image', 'source', 'source1', 'file'];
|
||||||
) || { name: 'image0' };
|
const matchedPrimary = endpoint.params.find(
|
||||||
formData.append(fileParam.name, blob, filename);
|
(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 mask buffer provided (Inpainting / Custom Mask / Auto Mask)
|
||||||
if (options.maskBuffer && Buffer.isBuffer(options.maskBuffer)) {
|
if (options.maskBuffer && Buffer.isBuffer(options.maskBuffer)) {
|
||||||
const maskParam = endpoint.params.find(
|
const maskParam = endpoint.params.find(
|
||||||
(p) => p.name.toLowerCase().includes('mask')
|
(p) => p.kind === 'file' && p.name.toLowerCase().includes('mask')
|
||||||
) || { name: 'mask_image' };
|
);
|
||||||
|
const maskParamName = maskParam?.name || 'mask_image';
|
||||||
const maskBlob = new Blob([new Uint8Array(options.maskBuffer)]);
|
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)) {
|
if (options.secondaryBuffer && Buffer.isBuffer(options.secondaryBuffer)) {
|
||||||
const secParam = endpoint.params.find(
|
const secCandidates = ['target', 'bg_image', 'face_segmented_image', 'source2', 'reference_image', 'reference', 'image2'];
|
||||||
(p) => p.kind === 'file' && p.name !== fileParam.name && !p.name.toLowerCase().includes('mask')
|
const matchedSec = endpoint.params.find(
|
||||||
) || { name: 'reference_image' };
|
(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)]);
|
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)
|
// 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
|
// Ensure effectId / colorId / style_id / artid / dress / expression synchronization if provided in options
|
||||||
const selectedEffect = options.style || options.effectId || options.colorId || options.effect_id;
|
const selectedEffect = options.style || options.effectId || options.colorId || options.effect_id || options.artId || options.artid || options.dress || options.expression || options.cartoonId;
|
||||||
if (selectedEffect) {
|
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)) {
|
if (endpoint.params.some((p) => p.name === targetKey) && !formData.has(targetKey)) {
|
||||||
formData.append(targetKey, String(selectedEffect));
|
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 controller = new AbortController();
|
||||||
const timeoutId = setTimeout(() => controller.abort(), endpoint.limits.timeoutMs || 60000);
|
const timeoutId = setTimeout(() => controller.abort(), endpoint.limits.timeoutMs || 60000);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user