@imageconvertit/mcp-server 0.0.0-stage → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +130 -3
- package/package.json +50 -6
- package/src/authorize.js +74 -0
- package/src/index.js +734 -0
- package/src/lib/batch.js +150 -0
- package/src/lib/cloudAuth.js +177 -0
- package/src/lib/imageOps.js +272 -0
- package/src/lib/rembg.js +38 -0
- package/src/lib/runAi.js +312 -0
- package/src/tools/ai.js +319 -0
- package/src/tools/core.js +7 -0
- package/src/tools/ecommerce.js +607 -0
- package/src/tools/format.js +534 -0
package/src/lib/runAi.js
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client for POST /api/run-ai — credit-metered Cloud AI.
|
|
3
|
+
* Provider keys (FAL / FASHN / GROQ) never leave Cloudflare; MCP only sends
|
|
4
|
+
* Bearer api_token + image bytes / options.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from 'node:fs/promises';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import sharp from 'sharp';
|
|
10
|
+
import { clearApiTokenCache, ensureApiToken } from './cloudAuth.js';
|
|
11
|
+
|
|
12
|
+
const DEFAULT_BASE = 'https://imageconvertit.com';
|
|
13
|
+
const MAX_BYTES = 10 * 1024 * 1024; // ~10 MB binary (matches run-ai)
|
|
14
|
+
|
|
15
|
+
const MIME_BY_EXT = {
|
|
16
|
+
'.jpg': 'image/jpeg',
|
|
17
|
+
'.jpeg': 'image/jpeg',
|
|
18
|
+
'.png': 'image/png',
|
|
19
|
+
'.webp': 'image/webp',
|
|
20
|
+
'.gif': 'image/gif',
|
|
21
|
+
'.bmp': 'image/bmp',
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} filePath
|
|
26
|
+
* @returns {Promise<{ base64: string, mime_type: string, bytes: number }>}
|
|
27
|
+
*/
|
|
28
|
+
export async function readImageBase64(filePath) {
|
|
29
|
+
const abs = path.resolve(filePath);
|
|
30
|
+
const buf = await fs.readFile(abs);
|
|
31
|
+
if (buf.length > MAX_BYTES) {
|
|
32
|
+
throw new Error(`Image exceeds 10 MB limit (${(buf.length / 1e6).toFixed(1)} MB).`);
|
|
33
|
+
}
|
|
34
|
+
const ext = path.extname(abs).toLowerCase();
|
|
35
|
+
let mime_type = MIME_BY_EXT[ext] || 'image/jpeg';
|
|
36
|
+
// Prefer real mime from sharp when possible
|
|
37
|
+
try {
|
|
38
|
+
const meta = await sharp(buf).metadata();
|
|
39
|
+
if (meta.format === 'jpeg') mime_type = 'image/jpeg';
|
|
40
|
+
else if (meta.format === 'png') mime_type = 'image/png';
|
|
41
|
+
else if (meta.format === 'webp') mime_type = 'image/webp';
|
|
42
|
+
else if (meta.format === 'gif') mime_type = 'image/gif';
|
|
43
|
+
} catch { /* keep ext mime */ }
|
|
44
|
+
return { base64: buf.toString('base64'), mime_type, bytes: buf.length };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Local file → data URI for FASHN face/person/pose/bg / mask refs.
|
|
49
|
+
* Server isSafeDataUri only accepts jpeg|jpg|png|webp — convert anything else to PNG.
|
|
50
|
+
* @param {string|undefined|null} filePath
|
|
51
|
+
*/
|
|
52
|
+
export async function fileToDataUri(filePath) {
|
|
53
|
+
if (!filePath || typeof filePath !== 'string') return null;
|
|
54
|
+
const abs = path.resolve(filePath);
|
|
55
|
+
const buf = await fs.readFile(abs);
|
|
56
|
+
if (buf.length > MAX_BYTES) {
|
|
57
|
+
throw new Error(`Reference image exceeds 10 MB limit (${(buf.length / 1e6).toFixed(1)} MB).`);
|
|
58
|
+
}
|
|
59
|
+
let mime_type = 'image/png';
|
|
60
|
+
let outBuf = buf;
|
|
61
|
+
try {
|
|
62
|
+
const meta = await sharp(buf).metadata();
|
|
63
|
+
const fmt = meta.format;
|
|
64
|
+
if (fmt === 'jpeg') {
|
|
65
|
+
mime_type = 'image/jpeg';
|
|
66
|
+
outBuf = await sharp(buf).rotate().jpeg({ quality: 92 }).toBuffer();
|
|
67
|
+
} else if (fmt === 'png') {
|
|
68
|
+
mime_type = 'image/png';
|
|
69
|
+
outBuf = await sharp(buf).rotate().png().toBuffer();
|
|
70
|
+
} else if (fmt === 'webp') {
|
|
71
|
+
mime_type = 'image/webp';
|
|
72
|
+
outBuf = await sharp(buf).rotate().webp({ quality: 92 }).toBuffer();
|
|
73
|
+
} else {
|
|
74
|
+
// gif/bmp/tiff/heic → png so /api/run-ai isSafeDataUri accepts the ref
|
|
75
|
+
mime_type = 'image/png';
|
|
76
|
+
outBuf = await sharp(buf).rotate().png().toBuffer();
|
|
77
|
+
}
|
|
78
|
+
} catch (e) {
|
|
79
|
+
throw new Error(`Could not read reference image: ${e?.message ?? e}`);
|
|
80
|
+
}
|
|
81
|
+
if (outBuf.length > MAX_BYTES) {
|
|
82
|
+
throw new Error('Reference image exceeds 10 MB after conversion.');
|
|
83
|
+
}
|
|
84
|
+
return `data:${mime_type};base64,${outBuf.toString('base64')}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Save run-ai data URI (or array) to disk.
|
|
89
|
+
* @returns {Promise<string[]>} absolute output paths
|
|
90
|
+
*/
|
|
91
|
+
export async function saveDataUriResult(result, outputPath) {
|
|
92
|
+
const abs = path.resolve(outputPath);
|
|
93
|
+
await fs.mkdir(path.dirname(abs), { recursive: true });
|
|
94
|
+
|
|
95
|
+
const uris = Array.isArray(result) ? result : [result];
|
|
96
|
+
const outs = [];
|
|
97
|
+
for (let i = 0; i < uris.length; i++) {
|
|
98
|
+
const uri = uris[i];
|
|
99
|
+
if (typeof uri !== 'string' || !uri.startsWith('data:')) {
|
|
100
|
+
throw new Error('AI result was not a data URI. Please retry.');
|
|
101
|
+
}
|
|
102
|
+
const comma = uri.indexOf(',');
|
|
103
|
+
const meta = uri.slice(5, comma);
|
|
104
|
+
const mime = meta.split(';')[0] || 'image/png';
|
|
105
|
+
const b64 = uri.slice(comma + 1);
|
|
106
|
+
const buf = Buffer.from(b64, 'base64');
|
|
107
|
+
let out = abs;
|
|
108
|
+
if (uris.length > 1) {
|
|
109
|
+
const ext = path.extname(abs) || '.png';
|
|
110
|
+
const base = abs.slice(0, abs.length - ext.length);
|
|
111
|
+
out = `${base}_${i + 1}${ext || '.png'}`;
|
|
112
|
+
} else if (!path.extname(abs)) {
|
|
113
|
+
const ext = mime.includes('jpeg') || mime.includes('jpg') ? '.jpg' : '.png';
|
|
114
|
+
out = abs + ext;
|
|
115
|
+
}
|
|
116
|
+
await fs.writeFile(out, buf);
|
|
117
|
+
outs.push(out);
|
|
118
|
+
}
|
|
119
|
+
return outs;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Compute uncrop expand sides (mirrors website computeUncropExpand).
|
|
124
|
+
*/
|
|
125
|
+
export function computeUncropExpand(w, h, framePreset = 'around', amountKey = 'medium') {
|
|
126
|
+
const clamp = (n) => Math.max(0, Math.min(280, Math.round(n)));
|
|
127
|
+
const padRatio = { light: 0.08, medium: 0.16, strong: 0.26 }[amountKey] || 0.16;
|
|
128
|
+
const zoomMap = { light: 15, medium: 28, strong: 42 };
|
|
129
|
+
|
|
130
|
+
if (framePreset === 'zoom_out') {
|
|
131
|
+
return {
|
|
132
|
+
expandLeft: 0, expandRight: 0, expandTop: 0, expandBottom: 0,
|
|
133
|
+
zoomOut: zoomMap[amountKey] ?? 28,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let L = 0; let R = 0; let T = 0; let B = 0;
|
|
138
|
+
if (framePreset === 'around') {
|
|
139
|
+
const p = clamp(Math.min(w, h) * padRatio);
|
|
140
|
+
L = R = T = B = p;
|
|
141
|
+
} else if (framePreset === 'horizontal') {
|
|
142
|
+
const p = clamp(w * padRatio);
|
|
143
|
+
L = R = p;
|
|
144
|
+
} else if (framePreset === 'vertical') {
|
|
145
|
+
const p = clamp(h * padRatio);
|
|
146
|
+
T = B = p;
|
|
147
|
+
} else {
|
|
148
|
+
const ratios = { '1:1': 1, '4:5': 4 / 5, '9:16': 9 / 16, '16:9': 16 / 9, '3:2': 3 / 2 };
|
|
149
|
+
const target = ratios[framePreset] || 1;
|
|
150
|
+
let tw = w;
|
|
151
|
+
let th = h;
|
|
152
|
+
const cur = w / h;
|
|
153
|
+
if (cur > target) th = Math.round(w / target);
|
|
154
|
+
else tw = Math.round(h * target);
|
|
155
|
+
const breath = padRatio * 0.45;
|
|
156
|
+
tw = Math.round(tw * (1 + breath));
|
|
157
|
+
th = Math.round(th * (1 + breath));
|
|
158
|
+
const needW = Math.max(0, tw - w);
|
|
159
|
+
const needH = Math.max(0, th - h);
|
|
160
|
+
L = clamp(needW / 2);
|
|
161
|
+
R = clamp(needW - L);
|
|
162
|
+
T = clamp(needH / 2);
|
|
163
|
+
B = clamp(needH - T);
|
|
164
|
+
}
|
|
165
|
+
return { expandLeft: L, expandRight: R, expandTop: T, expandBottom: B, zoomOut: 0 };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Orient + optionally downscale long side to maxSide (matches website prepareUncropFile).
|
|
170
|
+
* Always returns oriented pixels so expand math matches what Fal receives.
|
|
171
|
+
* @returns {Promise<{ buffer: Buffer, mime_type: string, w: number, h: number }>}
|
|
172
|
+
*/
|
|
173
|
+
export async function prepareUncropBuffer(filePath, maxSide = 1024) {
|
|
174
|
+
const abs = path.resolve(filePath);
|
|
175
|
+
// Measure after EXIF orient via a decode pass
|
|
176
|
+
const { info } = await sharp(abs).rotate().ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
177
|
+
const w = info.width || 1;
|
|
178
|
+
const h = info.height || 1;
|
|
179
|
+
|
|
180
|
+
if (Math.max(w, h) <= maxSide) {
|
|
181
|
+
const buffer = await sharp(abs).rotate().jpeg({ quality: 92 }).toBuffer();
|
|
182
|
+
return { buffer, mime_type: 'image/jpeg', w, h };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const scale = maxSide / Math.max(w, h);
|
|
186
|
+
const nw = Math.max(1, Math.round(w * scale));
|
|
187
|
+
const nh = Math.max(1, Math.round(h * scale));
|
|
188
|
+
const buffer = await sharp(abs).rotate().resize(nw, nh, { fit: 'fill' }).jpeg({ quality: 92 }).toBuffer();
|
|
189
|
+
return { buffer, mime_type: 'image/jpeg', w: nw, h: nh };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Call /api/run-ai once. Deducts credits server-side; returns 402 if empty.
|
|
194
|
+
*
|
|
195
|
+
* @param {object} env
|
|
196
|
+
* @param {{
|
|
197
|
+
* tool_slug: string,
|
|
198
|
+
* image_base64?: string,
|
|
199
|
+
* mime_type?: string,
|
|
200
|
+
* options?: object|null,
|
|
201
|
+
* outputPath: string,
|
|
202
|
+
* allowEmptyImage?: boolean,
|
|
203
|
+
* }} args
|
|
204
|
+
*/
|
|
205
|
+
export async function runCloudAi(env, args) {
|
|
206
|
+
const {
|
|
207
|
+
tool_slug,
|
|
208
|
+
image_base64 = '',
|
|
209
|
+
mime_type = 'image/jpeg',
|
|
210
|
+
options = null,
|
|
211
|
+
outputPath,
|
|
212
|
+
allowEmptyImage = false,
|
|
213
|
+
} = args;
|
|
214
|
+
|
|
215
|
+
if (!tool_slug) return { ok: false, error: 'tool_slug is required.' };
|
|
216
|
+
if (!outputPath) return { ok: false, error: 'outputPath is required.' };
|
|
217
|
+
if (!allowEmptyImage && (!image_base64 || image_base64.length === 0)) {
|
|
218
|
+
return { ok: false, error: 'inputPath image is required.' };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const auth = await ensureApiToken(env);
|
|
222
|
+
if (auth.error) return { ok: false, error: auth.error };
|
|
223
|
+
|
|
224
|
+
const base = (env.ICI_API_BASE || DEFAULT_BASE).replace(/\/$/, '');
|
|
225
|
+
const payload = {
|
|
226
|
+
tool_slug,
|
|
227
|
+
image_base64: image_base64 || '',
|
|
228
|
+
mime_type: mime_type || 'image/jpeg',
|
|
229
|
+
};
|
|
230
|
+
if (options && typeof options === 'object' && !Array.isArray(options)) {
|
|
231
|
+
// Drop null/undefined/'' so server resolvers don't see dead keys; keep false/0
|
|
232
|
+
const cleaned = {};
|
|
233
|
+
for (const [k, v] of Object.entries(options)) {
|
|
234
|
+
if (v === null || v === undefined || v === '') continue;
|
|
235
|
+
cleaned[k] = v;
|
|
236
|
+
}
|
|
237
|
+
if (Object.keys(cleaned).length) payload.options = cleaned;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const headers = {
|
|
241
|
+
'Content-Type': 'application/json',
|
|
242
|
+
Accept: 'application/json',
|
|
243
|
+
Authorization: `Bearer ${auth.api_token}`,
|
|
244
|
+
};
|
|
245
|
+
if (auth.device_id) headers['X-Device-Id'] = auth.device_id;
|
|
246
|
+
|
|
247
|
+
const doFetch = async () => fetch(`${base}/api/run-ai`, {
|
|
248
|
+
method: 'POST',
|
|
249
|
+
headers,
|
|
250
|
+
body: JSON.stringify(payload),
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
let res;
|
|
254
|
+
try {
|
|
255
|
+
res = await doFetch();
|
|
256
|
+
} catch (e) {
|
|
257
|
+
return { ok: false, error: `Network error calling Cloud AI: ${e?.message ?? e}` };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// One retry on 401 after clearing cache (expired token).
|
|
261
|
+
if (res.status === 401) {
|
|
262
|
+
await clearApiTokenCache(env);
|
|
263
|
+
const retryAuth = await ensureApiToken(env);
|
|
264
|
+
if (retryAuth.error) return { ok: false, error: retryAuth.error };
|
|
265
|
+
headers.Authorization = `Bearer ${retryAuth.api_token}`;
|
|
266
|
+
if (retryAuth.device_id) headers['X-Device-Id'] = retryAuth.device_id;
|
|
267
|
+
try {
|
|
268
|
+
res = await doFetch();
|
|
269
|
+
} catch (e) {
|
|
270
|
+
return { ok: false, error: `Network error calling Cloud AI: ${e?.message ?? e}` };
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
let json = null;
|
|
275
|
+
const text = await res.text();
|
|
276
|
+
try {
|
|
277
|
+
json = JSON.parse(text);
|
|
278
|
+
} catch {
|
|
279
|
+
return {
|
|
280
|
+
ok: false,
|
|
281
|
+
error: res.status === 402
|
|
282
|
+
? 'Insufficient credits for this Cloud AI tool. Buy a credit pack on imageconvertit.com.'
|
|
283
|
+
: `AI service returned an unexpected response (${res.status}). Wait a few seconds and retry.`,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (!res.ok || !json?.success) {
|
|
288
|
+
const msg = json?.error || `Cloud AI failed (${res.status}).`;
|
|
289
|
+
// Never echo provider/infra details — server already sanitizes.
|
|
290
|
+
return {
|
|
291
|
+
ok: false,
|
|
292
|
+
error: msg,
|
|
293
|
+
status: res.status,
|
|
294
|
+
credits_remaining: typeof json?.credits_remaining === 'number' ? json.credits_remaining : undefined,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
let outs;
|
|
299
|
+
try {
|
|
300
|
+
outs = await saveDataUriResult(json.result, outputPath);
|
|
301
|
+
} catch (e) {
|
|
302
|
+
return { ok: false, error: e?.message ?? String(e) };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return {
|
|
306
|
+
ok: true,
|
|
307
|
+
tool_slug,
|
|
308
|
+
outputs: outs,
|
|
309
|
+
credits_remaining: json.credits_remaining,
|
|
310
|
+
resultType: json.resultType,
|
|
311
|
+
};
|
|
312
|
+
}
|
package/src/tools/ai.js
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cloud AI MCP tools — same credit wallet as imageconvertit.com.
|
|
3
|
+
* Flow: local path → base64 → POST /api/run-ai (Bearer api_token) → save result.
|
|
4
|
+
* FAL / FASHN / GROQ keys never appear in MCP env, logs, or responses.
|
|
5
|
+
* Zero credits → 402 from server (no free Cloud AI).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import {
|
|
10
|
+
computeUncropExpand,
|
|
11
|
+
fileToDataUri,
|
|
12
|
+
prepareUncropBuffer,
|
|
13
|
+
readImageBase64,
|
|
14
|
+
runCloudAi,
|
|
15
|
+
} from '../lib/runAi.js';
|
|
16
|
+
|
|
17
|
+
function defaultOut(inputPath, suffix) {
|
|
18
|
+
const abs = path.resolve(inputPath);
|
|
19
|
+
const ext = path.extname(abs) || '.png';
|
|
20
|
+
const base = abs.slice(0, abs.length - ext.length);
|
|
21
|
+
return `${base}${suffix}.png`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Catch filesystem / sharp errors so MCP returns JSON, not a thrown stack. */
|
|
25
|
+
async function guard(fn) {
|
|
26
|
+
try {
|
|
27
|
+
return await fn();
|
|
28
|
+
} catch (e) {
|
|
29
|
+
return { ok: false, error: e?.message ?? String(e) };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function withInput(env, inputPath, tool_slug, outputPath, options, suffix) {
|
|
34
|
+
return guard(async () => {
|
|
35
|
+
const out = outputPath || defaultOut(inputPath, suffix);
|
|
36
|
+
const { base64, mime_type } = await readImageBase64(inputPath);
|
|
37
|
+
return runCloudAi(env, {
|
|
38
|
+
tool_slug,
|
|
39
|
+
image_base64: base64,
|
|
40
|
+
mime_type,
|
|
41
|
+
options: options || null,
|
|
42
|
+
outputPath: out,
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** AI Fashion Model — garment/product → on-model (FASHN via server). */
|
|
48
|
+
export async function aiFashionModel(env, args = {}) {
|
|
49
|
+
return guard(async () => {
|
|
50
|
+
const {
|
|
51
|
+
inputPath,
|
|
52
|
+
outputPath,
|
|
53
|
+
mode = 'on_model',
|
|
54
|
+
qualityTier = 'quality',
|
|
55
|
+
modelPreset = 'auto',
|
|
56
|
+
posePreset = 'standing_front',
|
|
57
|
+
backgroundPreset = 'studio_white',
|
|
58
|
+
facePath,
|
|
59
|
+
faceMode = 'match_reference',
|
|
60
|
+
personPath,
|
|
61
|
+
poseRefPath,
|
|
62
|
+
backgroundRefPath,
|
|
63
|
+
extraPrompt = '',
|
|
64
|
+
} = args;
|
|
65
|
+
if (!inputPath) return { ok: false, error: 'inputPath is required (garment / product photo).' };
|
|
66
|
+
|
|
67
|
+
const options = {
|
|
68
|
+
mode,
|
|
69
|
+
qualityTier,
|
|
70
|
+
modelPreset,
|
|
71
|
+
posePreset,
|
|
72
|
+
backgroundPreset,
|
|
73
|
+
faceMode,
|
|
74
|
+
extraPrompt: typeof extraPrompt === 'string' ? extraPrompt : '',
|
|
75
|
+
faceDataUrl: await fileToDataUri(facePath),
|
|
76
|
+
personDataUrl: await fileToDataUri(personPath),
|
|
77
|
+
poseRefDataUrl: await fileToDataUri(poseRefPath),
|
|
78
|
+
backgroundRefDataUrl: await fileToDataUri(backgroundRefPath),
|
|
79
|
+
};
|
|
80
|
+
if (options.mode === 'try_on' && !options.personDataUrl) {
|
|
81
|
+
return {
|
|
82
|
+
ok: false,
|
|
83
|
+
error: 'Try-on mode needs personPath (person photo), or use mode=on_model.',
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return withInput(env, inputPath, 'ai-fashion-model', outputPath, options, '_on_model');
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** AI Virtual Try-On — garment + person. */
|
|
91
|
+
export async function aiVirtualTryOn(env, args = {}) {
|
|
92
|
+
return guard(async () => {
|
|
93
|
+
const {
|
|
94
|
+
inputPath,
|
|
95
|
+
personPath,
|
|
96
|
+
outputPath,
|
|
97
|
+
qualityTier = 'quality',
|
|
98
|
+
extraPrompt = '',
|
|
99
|
+
} = args;
|
|
100
|
+
if (!inputPath) return { ok: false, error: 'inputPath is required (garment photo).' };
|
|
101
|
+
if (!personPath) return { ok: false, error: 'personPath is required (person photo).' };
|
|
102
|
+
const personDataUrl = await fileToDataUri(personPath);
|
|
103
|
+
if (!personDataUrl) return { ok: false, error: 'Could not read personPath.' };
|
|
104
|
+
return withInput(env, inputPath, 'ai-virtual-try-on', outputPath, {
|
|
105
|
+
qualityTier,
|
|
106
|
+
personDataUrl,
|
|
107
|
+
extraPrompt: typeof extraPrompt === 'string' ? extraPrompt : '',
|
|
108
|
+
}, '_try_on');
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** AI Fashion Model Swap — keep outfit, change person. */
|
|
113
|
+
export async function aiFashionModelSwap(env, args = {}) {
|
|
114
|
+
return guard(async () => {
|
|
115
|
+
const {
|
|
116
|
+
inputPath,
|
|
117
|
+
outputPath,
|
|
118
|
+
qualityTier = 'quality',
|
|
119
|
+
identityPreset = 'auto',
|
|
120
|
+
facePath,
|
|
121
|
+
faceMode = 'match_reference',
|
|
122
|
+
extraPrompt = '',
|
|
123
|
+
} = args;
|
|
124
|
+
if (!inputPath) return { ok: false, error: 'inputPath is required (on-model / mannequin photo).' };
|
|
125
|
+
return withInput(env, inputPath, 'ai-fashion-model-swap', outputPath, {
|
|
126
|
+
qualityTier,
|
|
127
|
+
identityPreset,
|
|
128
|
+
faceMode,
|
|
129
|
+
faceDataUrl: await fileToDataUri(facePath),
|
|
130
|
+
extraPrompt: typeof extraPrompt === 'string' ? extraPrompt : '',
|
|
131
|
+
}, '_model_swap');
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** AI Fashion Model Creation — prompt-driven model (image optional pose ref). */
|
|
136
|
+
export async function aiFashionModelCreation(env, args = {}) {
|
|
137
|
+
return guard(async () => {
|
|
138
|
+
const {
|
|
139
|
+
inputPath,
|
|
140
|
+
outputPath,
|
|
141
|
+
qualityTier = 'quality',
|
|
142
|
+
modelPreset = 'woman_editorial',
|
|
143
|
+
posePreset = 'standing_front',
|
|
144
|
+
backgroundPreset = 'studio_white',
|
|
145
|
+
aspectRatio = '3:4',
|
|
146
|
+
clothingNotes = '',
|
|
147
|
+
facePath,
|
|
148
|
+
faceMode = 'match_reference',
|
|
149
|
+
poseRefPath,
|
|
150
|
+
extraPrompt = '',
|
|
151
|
+
} = args;
|
|
152
|
+
|
|
153
|
+
const options = {
|
|
154
|
+
qualityTier,
|
|
155
|
+
modelPreset,
|
|
156
|
+
posePreset,
|
|
157
|
+
backgroundPreset,
|
|
158
|
+
aspectRatio,
|
|
159
|
+
clothingNotes: typeof clothingNotes === 'string' ? clothingNotes : '',
|
|
160
|
+
faceMode,
|
|
161
|
+
faceDataUrl: await fileToDataUri(facePath),
|
|
162
|
+
poseRefDataUrl: await fileToDataUri(poseRefPath),
|
|
163
|
+
extraPrompt: typeof extraPrompt === 'string' ? extraPrompt : '',
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const out = outputPath
|
|
167
|
+
|| (inputPath ? defaultOut(inputPath, '_model_create') : path.resolve('ai_fashion_model_creation.png'));
|
|
168
|
+
|
|
169
|
+
if (inputPath) {
|
|
170
|
+
return withInput(env, inputPath, 'ai-fashion-model-creation', out, options, '_model_create');
|
|
171
|
+
}
|
|
172
|
+
return runCloudAi(env, {
|
|
173
|
+
tool_slug: 'ai-fashion-model-creation',
|
|
174
|
+
image_base64: '',
|
|
175
|
+
mime_type: 'image/png',
|
|
176
|
+
options,
|
|
177
|
+
outputPath: out,
|
|
178
|
+
allowEmptyImage: true,
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** AI Remove Object — prompt and/or mask image path. */
|
|
184
|
+
export async function aiRemoveObject(env, args = {}) {
|
|
185
|
+
return guard(async () => {
|
|
186
|
+
const {
|
|
187
|
+
inputPath,
|
|
188
|
+
outputPath,
|
|
189
|
+
removePrompt = '',
|
|
190
|
+
maskPath,
|
|
191
|
+
qualityModel = 'best_quality',
|
|
192
|
+
maskExpansion = 15,
|
|
193
|
+
} = args;
|
|
194
|
+
if (!inputPath) return { ok: false, error: 'inputPath is required.' };
|
|
195
|
+
const prompt = typeof removePrompt === 'string' ? removePrompt.trim() : '';
|
|
196
|
+
const maskDataUrl = await fileToDataUri(maskPath);
|
|
197
|
+
if (!maskDataUrl && prompt.length < 2) {
|
|
198
|
+
return { ok: false, error: 'Describe what to remove (removePrompt), or provide maskPath.' };
|
|
199
|
+
}
|
|
200
|
+
return withInput(env, inputPath, 'ai-remove-object', outputPath, {
|
|
201
|
+
removePrompt: prompt,
|
|
202
|
+
qualityModel,
|
|
203
|
+
maskExpansion,
|
|
204
|
+
...(maskDataUrl ? { maskDataUrl } : {}),
|
|
205
|
+
}, '_cleaned');
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** AI Watermark Remover. */
|
|
210
|
+
export async function aiWatermarkRemover(env, args = {}) {
|
|
211
|
+
const { inputPath, outputPath } = args;
|
|
212
|
+
if (!inputPath) return { ok: false, error: 'inputPath is required.' };
|
|
213
|
+
return withInput(env, inputPath, 'watermark-remover', outputPath, null, '_clean');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** AI Lifestyle Product Photos. */
|
|
217
|
+
export async function aiLifestyleProductPhotos(env, args = {}) {
|
|
218
|
+
const {
|
|
219
|
+
inputPath,
|
|
220
|
+
outputPath,
|
|
221
|
+
scenePreset = 'kitchen_counter',
|
|
222
|
+
qualityTier = 'best',
|
|
223
|
+
imageSize = '1024x1024',
|
|
224
|
+
sceneDetails = '',
|
|
225
|
+
placementMode = 'on_surface',
|
|
226
|
+
generateTwo = false,
|
|
227
|
+
} = args;
|
|
228
|
+
if (!inputPath) return { ok: false, error: 'inputPath is required.' };
|
|
229
|
+
if (scenePreset === 'custom' && String(sceneDetails || '').trim().length < 4) {
|
|
230
|
+
return { ok: false, error: 'Describe the lifestyle scene (sceneDetails), or pick a preset scenePreset.' };
|
|
231
|
+
}
|
|
232
|
+
return withInput(env, inputPath, 'ai-lifestyle-product-photos', outputPath, {
|
|
233
|
+
scenePreset,
|
|
234
|
+
qualityTier,
|
|
235
|
+
imageSize,
|
|
236
|
+
sceneDetails: typeof sceneDetails === 'string' ? sceneDetails.trim() : '',
|
|
237
|
+
placementMode: placementMode === 'in_use' ? 'in_use' : 'on_surface',
|
|
238
|
+
generateTwo: !!generateTwo,
|
|
239
|
+
}, '_lifestyle');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** AI Product Backgrounds. */
|
|
243
|
+
export async function aiProductBackgrounds(env, args = {}) {
|
|
244
|
+
const {
|
|
245
|
+
inputPath,
|
|
246
|
+
outputPath,
|
|
247
|
+
backdropPreset = 'studio_white',
|
|
248
|
+
qualityTier = 'best',
|
|
249
|
+
backdropDetails = '',
|
|
250
|
+
negativeDetails = '',
|
|
251
|
+
generateTwo = false,
|
|
252
|
+
} = args;
|
|
253
|
+
if (!inputPath) return { ok: false, error: 'inputPath is required.' };
|
|
254
|
+
if (backdropPreset === 'custom' && String(backdropDetails || '').trim().length < 4) {
|
|
255
|
+
return { ok: false, error: 'Describe the backdrop (backdropDetails), or pick a preset backdropPreset.' };
|
|
256
|
+
}
|
|
257
|
+
return withInput(env, inputPath, 'ai-product-backgrounds', outputPath, {
|
|
258
|
+
backdropPreset,
|
|
259
|
+
qualityTier,
|
|
260
|
+
backdropDetails: typeof backdropDetails === 'string' ? backdropDetails.trim() : '',
|
|
261
|
+
negativeDetails: typeof negativeDetails === 'string' ? negativeDetails.trim() : '',
|
|
262
|
+
generateTwo: !!generateTwo,
|
|
263
|
+
}, '_bg');
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** 8K Image Upscaler (ESRGAN via Fal — slug 8k-texture-upscaler). */
|
|
267
|
+
export async function ai8kUpscaler(env, args = {}) {
|
|
268
|
+
const { inputPath, outputPath } = args;
|
|
269
|
+
if (!inputPath) return { ok: false, error: 'inputPath is required.' };
|
|
270
|
+
return withInput(env, inputPath, '8k-texture-upscaler', outputPath, null, '_8k');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** AI Product Beautifier. */
|
|
274
|
+
export async function aiProductBeautifier(env, args = {}) {
|
|
275
|
+
const { inputPath, outputPath, style = 'product_photography' } = args;
|
|
276
|
+
if (!inputPath) return { ok: false, error: 'inputPath is required.' };
|
|
277
|
+
return withInput(env, inputPath, 'ai-product-beautifier', outputPath, { style }, '_beautified');
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** AI Image Colorizer. */
|
|
281
|
+
export async function aiImageColorizer(env, args = {}) {
|
|
282
|
+
const { inputPath, outputPath } = args;
|
|
283
|
+
if (!inputPath) return { ok: false, error: 'inputPath is required.' };
|
|
284
|
+
return withInput(env, inputPath, 'image-colorizer', outputPath, null, '_colorized');
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** AI Uncrop / expand. */
|
|
288
|
+
export async function aiUncrop(env, args = {}) {
|
|
289
|
+
return guard(async () => {
|
|
290
|
+
const {
|
|
291
|
+
inputPath,
|
|
292
|
+
outputPath,
|
|
293
|
+
framePreset = 'around',
|
|
294
|
+
expandAmount = 'medium',
|
|
295
|
+
expandPrompt = '',
|
|
296
|
+
} = args;
|
|
297
|
+
if (!inputPath) return { ok: false, error: 'inputPath is required.' };
|
|
298
|
+
|
|
299
|
+
const prepared = await prepareUncropBuffer(inputPath, 1024);
|
|
300
|
+
const expand = computeUncropExpand(prepared.w, prepared.h, framePreset, expandAmount);
|
|
301
|
+
const out = outputPath || defaultOut(inputPath, '_uncropped');
|
|
302
|
+
return runCloudAi(env, {
|
|
303
|
+
tool_slug: 'ai-uncrop',
|
|
304
|
+
image_base64: prepared.buffer.toString('base64'),
|
|
305
|
+
mime_type: prepared.mime_type,
|
|
306
|
+
options: {
|
|
307
|
+
framePreset,
|
|
308
|
+
expandAmount,
|
|
309
|
+
expandPrompt: typeof expandPrompt === 'string' ? expandPrompt.trim().slice(0, 200) : '',
|
|
310
|
+
expandLeft: expand.expandLeft,
|
|
311
|
+
expandRight: expand.expandRight,
|
|
312
|
+
expandTop: expand.expandTop,
|
|
313
|
+
expandBottom: expand.expandBottom,
|
|
314
|
+
zoomOut: expand.zoomOut,
|
|
315
|
+
},
|
|
316
|
+
outputPath: out,
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Legacy exports — kept for import compatibility.
|
|
3
|
+
* Prefer tools/format.js and tools/ecommerce.js (registered in index.js).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export { batchResize as batchResizeCompress } from './format.js';
|
|
7
|
+
export { batchWhiteBackground as batchAmazonPrep } from './ecommerce.js';
|