@zodic/shared 0.0.388 → 0.0.389
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/app/services/ArtifactService.ts +76 -48
- package/package.json +1 -1
|
@@ -434,11 +434,11 @@ export class ArtifactService {
|
|
|
434
434
|
const drizzle = this.context.drizzle();
|
|
435
435
|
const { users, artifactFaceswap, cosmicMirrorImages, userArtifacts } =
|
|
436
436
|
schema;
|
|
437
|
-
|
|
437
|
+
|
|
438
438
|
console.log(
|
|
439
439
|
`Starting retryStaleCosmicMirror for userArtifactId: ${userArtifactId}`
|
|
440
440
|
);
|
|
441
|
-
|
|
441
|
+
|
|
442
442
|
// Fetch userId from userArtifacts
|
|
443
443
|
console.log('Fetching user artifact to get userId...');
|
|
444
444
|
const [artifact] = await drizzle
|
|
@@ -446,7 +446,7 @@ export class ArtifactService {
|
|
|
446
446
|
.from(userArtifacts)
|
|
447
447
|
.where(eq(userArtifacts.id, userArtifactId))
|
|
448
448
|
.limit(1);
|
|
449
|
-
|
|
449
|
+
|
|
450
450
|
if (!artifact || !artifact.userId) {
|
|
451
451
|
console.error('User artifact or userId not found');
|
|
452
452
|
return {
|
|
@@ -455,10 +455,10 @@ export class ArtifactService {
|
|
|
455
455
|
statusCode: 400,
|
|
456
456
|
};
|
|
457
457
|
}
|
|
458
|
-
|
|
458
|
+
|
|
459
459
|
const userId = artifact.userId;
|
|
460
460
|
console.log(`Fetched userId: ${userId}`);
|
|
461
|
-
|
|
461
|
+
|
|
462
462
|
// Fetch user photo
|
|
463
463
|
console.log('Fetching user photo from database...');
|
|
464
464
|
const [user] = await drizzle
|
|
@@ -469,9 +469,9 @@ export class ArtifactService {
|
|
|
469
469
|
.from(users)
|
|
470
470
|
.where(eq(users.id, userId))
|
|
471
471
|
.limit(1);
|
|
472
|
-
|
|
472
|
+
|
|
473
473
|
console.log('User fetch result:', user);
|
|
474
|
-
|
|
474
|
+
|
|
475
475
|
const sourceImage = user?.userPhotoUrl || user?.userProfileImage;
|
|
476
476
|
if (!sourceImage) {
|
|
477
477
|
console.error('No user photo found for faceswap');
|
|
@@ -482,16 +482,16 @@ export class ArtifactService {
|
|
|
482
482
|
};
|
|
483
483
|
}
|
|
484
484
|
console.log(`Source image URL: ${sourceImage}`);
|
|
485
|
-
|
|
485
|
+
|
|
486
486
|
// Get all images from D1
|
|
487
487
|
console.log('Fetching images from D1 cosmicMirrorImages table...');
|
|
488
488
|
const images = await drizzle
|
|
489
489
|
.select()
|
|
490
490
|
.from(cosmicMirrorImages)
|
|
491
491
|
.where(eq(cosmicMirrorImages.userArtifactId, userArtifactId));
|
|
492
|
-
|
|
492
|
+
|
|
493
493
|
console.log('Images retrieved from D1:', images);
|
|
494
|
-
|
|
494
|
+
|
|
495
495
|
if (images.length === 0) {
|
|
496
496
|
console.warn('No images found in D1 for this userArtifactId');
|
|
497
497
|
return {
|
|
@@ -500,12 +500,12 @@ export class ArtifactService {
|
|
|
500
500
|
statusCode: 400,
|
|
501
501
|
};
|
|
502
502
|
}
|
|
503
|
-
|
|
504
|
-
// Process faceswaps for all images
|
|
503
|
+
|
|
504
|
+
// Process faceswaps for all images with a 2-second delay between calls
|
|
505
505
|
let faceswapCount = 0;
|
|
506
|
-
for (const image of images) {
|
|
506
|
+
for (const [index, image] of images.entries()) {
|
|
507
507
|
console.log(`Processing image with leonardoId: ${image.leonardoId}`);
|
|
508
|
-
|
|
508
|
+
|
|
509
509
|
if (!image.url) {
|
|
510
510
|
console.warn(
|
|
511
511
|
`Image with leonardoId ${image.leonardoId} does not contain a URL:`,
|
|
@@ -516,49 +516,71 @@ export class ArtifactService {
|
|
|
516
516
|
console.log(
|
|
517
517
|
`Target image URL for leonardoId ${image.leonardoId}: ${image.url}`
|
|
518
518
|
);
|
|
519
|
-
|
|
519
|
+
|
|
520
520
|
console.log(
|
|
521
521
|
`Calling faceSwap API for source: ${sourceImage}, target: ${image.url}`
|
|
522
522
|
);
|
|
523
523
|
let faceSwapResponse: any;
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
524
|
+
let attempts = 0;
|
|
525
|
+
const maxAttempts = index === 0 ? 2 : 1; // Retry once for the first image
|
|
526
|
+
const timeoutDuration = index === 0 ? 20000 : 12000; // 20s for first image, 12s for others
|
|
527
|
+
|
|
528
|
+
while (attempts < maxAttempts) {
|
|
529
|
+
try {
|
|
530
|
+
// Add a timeout for the FaceSwap API call
|
|
531
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
532
|
+
setTimeout(
|
|
533
|
+
() =>
|
|
534
|
+
reject(
|
|
535
|
+
new Error(
|
|
536
|
+
`FaceSwap API call timed out after ${timeoutDuration / 1000} seconds`
|
|
537
|
+
)
|
|
538
|
+
),
|
|
539
|
+
timeoutDuration
|
|
540
|
+
);
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
faceSwapResponse = await Promise.race([
|
|
544
|
+
this.context.api().callPiApi.faceSwap({
|
|
545
|
+
sourceImageUrl: sourceImage,
|
|
546
|
+
targetImageUrl: image.url,
|
|
547
|
+
}),
|
|
548
|
+
timeoutPromise,
|
|
549
|
+
]);
|
|
550
|
+
|
|
551
|
+
// Log the response only if the API call succeeded (not timed out)
|
|
552
|
+
console.log(
|
|
553
|
+
`faceSwap response for leonardoId ${image.leonardoId}:`,
|
|
554
|
+
faceSwapResponse
|
|
531
555
|
);
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
err
|
|
550
|
-
);
|
|
551
|
-
continue;
|
|
556
|
+
break; // Exit retry loop on success
|
|
557
|
+
} catch (err) {
|
|
558
|
+
attempts++;
|
|
559
|
+
console.error(
|
|
560
|
+
`faceSwap API call attempt ${attempts}/${maxAttempts} failed for leonardoId ${image.leonardoId}:`,
|
|
561
|
+
err
|
|
562
|
+
);
|
|
563
|
+
if (attempts < maxAttempts) {
|
|
564
|
+
console.log('Retrying after 2 seconds...');
|
|
565
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
566
|
+
} else {
|
|
567
|
+
console.error(
|
|
568
|
+
`All attempts failed for leonardoId ${image.leonardoId}, skipping...`
|
|
569
|
+
);
|
|
570
|
+
continue; // Skip to the next image if all retries fail
|
|
571
|
+
}
|
|
572
|
+
}
|
|
552
573
|
}
|
|
553
|
-
|
|
574
|
+
|
|
575
|
+
// Skip if no valid response after retries
|
|
554
576
|
if (!faceSwapResponse || !faceSwapResponse.taskId) {
|
|
555
577
|
console.error(
|
|
556
|
-
`faceSwap response for leonardoId ${image.leonardoId} does not contain a taskId:`,
|
|
578
|
+
`faceSwap response for leonardoId ${image.leonardoId} does not contain a taskId or failed after retries:`,
|
|
557
579
|
faceSwapResponse
|
|
558
580
|
);
|
|
559
581
|
continue;
|
|
560
582
|
}
|
|
561
|
-
|
|
583
|
+
|
|
562
584
|
// Create faceswap record
|
|
563
585
|
console.log(
|
|
564
586
|
`Creating artifactFaceswap record for taskId: ${faceSwapResponse.taskId}`
|
|
@@ -582,8 +604,14 @@ export class ArtifactService {
|
|
|
582
604
|
);
|
|
583
605
|
continue;
|
|
584
606
|
}
|
|
607
|
+
|
|
608
|
+
// Introduce a 2-second delay before the next API call (if there are more images)
|
|
609
|
+
if (index < images.length - 1) {
|
|
610
|
+
console.log('Waiting 2 seconds before the next faceSwap API call...');
|
|
611
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
612
|
+
}
|
|
585
613
|
}
|
|
586
|
-
|
|
614
|
+
|
|
587
615
|
if (faceswapCount === 0) {
|
|
588
616
|
console.error('No artifactFaceswap records were created');
|
|
589
617
|
return {
|
|
@@ -592,7 +620,7 @@ export class ArtifactService {
|
|
|
592
620
|
statusCode: 500,
|
|
593
621
|
};
|
|
594
622
|
}
|
|
595
|
-
|
|
623
|
+
|
|
596
624
|
// Update user artifact status
|
|
597
625
|
console.log(
|
|
598
626
|
`Updating userArtifacts status to 'faceswap' for userArtifactId: ${userArtifactId}`
|
|
@@ -614,7 +642,7 @@ export class ArtifactService {
|
|
|
614
642
|
statusCode: 500,
|
|
615
643
|
};
|
|
616
644
|
}
|
|
617
|
-
|
|
645
|
+
|
|
618
646
|
console.log(
|
|
619
647
|
`retryStaleCosmicMirror completed successfully. Created ${faceswapCount} faceswap records.`
|
|
620
648
|
);
|