|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kanvas\Connectors\PromptMine\Workflows\Activities; |
| 6 | + |
| 7 | +use Baka\Contracts\AppInterface; |
| 8 | +use Exception; |
| 9 | +use Illuminate\Database\Eloquent\Model; |
| 10 | +use Illuminate\Support\Facades\Http; |
| 11 | +use Kanvas\Workflow\Contracts\WorkflowActivityInterface; |
| 12 | +use Kanvas\Workflow\KanvasActivity; |
| 13 | +use Override; |
| 14 | + |
| 15 | +class PromptImageFilterActivity extends KanvasActivity implements WorkflowActivityInterface |
| 16 | +{ |
| 17 | + protected ?string $apiUrl = null; |
| 18 | + protected const int MAX_STATUS_CHECKS = 30; |
| 19 | + protected const int STATUS_CHECK_DELAY = 2; |
| 20 | + public $tries = 3; |
| 21 | + |
| 22 | + #[Override] |
| 23 | + public function execute(Model $entity, AppInterface $app, array $params): array |
| 24 | + { |
| 25 | + $messageFiles = $entity->getFiles(); |
| 26 | + $this->apiUrl = $entity->app->get('PROMPT_IMAGE_API_URL'); |
| 27 | + |
| 28 | + if (empty($this->apiUrl)) { |
| 29 | + return [ |
| 30 | + 'result' => false, |
| 31 | + 'message' => 'API URL not configured', |
| 32 | + ]; |
| 33 | + } |
| 34 | + |
| 35 | + if ($messageFiles->isEmpty()) { |
| 36 | + return [ |
| 37 | + 'result' => false, |
| 38 | + 'message' => 'Message does not have any files', |
| 39 | + ]; |
| 40 | + } |
| 41 | + |
| 42 | + $fileUrl = $messageFiles->first()->url; |
| 43 | + |
| 44 | + try { |
| 45 | + // Step 1: Submit the image for processing |
| 46 | + $submitResponse = $this->submitImage($fileUrl); |
| 47 | + |
| 48 | + if (! isset($submitResponse['request_id'])) { |
| 49 | + return [ |
| 50 | + 'result' => false, |
| 51 | + 'message' => 'Failed to submit image for processing', |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + $requestId = $submitResponse['request_id']; |
| 56 | + |
| 57 | + // Step 2: Check processing status until complete |
| 58 | + $statusResponse = $this->checkProcessingStatus($requestId); |
| 59 | + |
| 60 | + if ($statusResponse['status'] !== 'COMPLETED') { |
| 61 | + return [ |
| 62 | + 'result' => false, |
| 63 | + 'message' => 'Image processing did not complete successfully', |
| 64 | + ]; |
| 65 | + } |
| 66 | + |
| 67 | + // Step 3: Get the processed image result |
| 68 | + $resultResponse = $this->getProcessingResult($requestId); |
| 69 | + |
| 70 | + if (! isset($resultResponse['data']['image']['url'])) { |
| 71 | + return [ |
| 72 | + 'result' => false, |
| 73 | + 'message' => 'Failed to retrieve processed image', |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + $processedImageUrl = $resultResponse['data']['image']['url']; |
| 78 | + |
| 79 | + return [ |
| 80 | + 'message' => 'Image processed successfully', |
| 81 | + 'result' => true, |
| 82 | + 'user_id' => $entity->user->getId(), |
| 83 | + 'message_data' => $entity->message, |
| 84 | + 'message_id' => $entity->getId(), |
| 85 | + 'processed_image_url' => $processedImageUrl, |
| 86 | + 'original_image_url' => $fileUrl, |
| 87 | + 'request_id' => $requestId, |
| 88 | + ]; |
| 89 | + } catch (Exception $e) { |
| 90 | + return [ |
| 91 | + 'result' => false, |
| 92 | + 'message_id' => $entity->getId(), |
| 93 | + 'message' => 'Error processing image: ' . $e->getMessage(), |
| 94 | + ]; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Submit an image for processing |
| 100 | + */ |
| 101 | + protected function submitImage(string $imageUrl): array |
| 102 | + { |
| 103 | + $response = Http::withHeaders([ |
| 104 | + 'Content-Type' => 'application/json', |
| 105 | + ])->post($this->apiUrl, [ |
| 106 | + 'operation' => 'submit', |
| 107 | + 'image_url' => $imageUrl, |
| 108 | + 'model' => 'fal-ai/ghiblify', |
| 109 | + ]); |
| 110 | + |
| 111 | + return $response->json(); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Check the processing status of a submitted image |
| 116 | + */ |
| 117 | + protected function checkProcessingStatus(string $requestId): array |
| 118 | + { |
| 119 | + $attempts = 0; |
| 120 | + $statusResponse = []; |
| 121 | + |
| 122 | + while ($attempts < self::MAX_STATUS_CHECKS) { |
| 123 | + $response = Http::withHeaders([ |
| 124 | + 'Content-Type' => 'application/json', |
| 125 | + ])->post($this->apiUrl, [ |
| 126 | + 'operation' => 'status', |
| 127 | + 'requestId' => $requestId, |
| 128 | + 'model' => 'fal-ai/ghiblify', |
| 129 | + 'logs' => true, |
| 130 | + ]); |
| 131 | + |
| 132 | + $statusResponse = $response->json(); |
| 133 | + |
| 134 | + if ($statusResponse['status'] === 'COMPLETED') { |
| 135 | + break; |
| 136 | + } |
| 137 | + |
| 138 | + if ($statusResponse['status'] === 'FAILED') { |
| 139 | + throw new Exception('Image processing failed for this request' . $requestId); |
| 140 | + } |
| 141 | + |
| 142 | + // Wait before checking again |
| 143 | + sleep(self::STATUS_CHECK_DELAY); |
| 144 | + $attempts++; |
| 145 | + } |
| 146 | + |
| 147 | + if ($attempts >= self::MAX_STATUS_CHECKS) { |
| 148 | + throw new Exception('Image processing timed out' . $requestId); |
| 149 | + } |
| 150 | + |
| 151 | + return $statusResponse; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Get the result of a processed image |
| 156 | + */ |
| 157 | + protected function getProcessingResult(string $requestId): array |
| 158 | + { |
| 159 | + $response = Http::withHeaders([ |
| 160 | + 'Content-Type' => 'application/json', |
| 161 | + ])->post($this->apiUrl, [ |
| 162 | + 'operation' => 'result', |
| 163 | + 'requestId' => $requestId, |
| 164 | + 'model' => 'fal-ai/ghiblify', |
| 165 | + ]); |
| 166 | + |
| 167 | + return $response->json(); |
| 168 | + } |
| 169 | +} |
0 commit comments