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
+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);