163 lines
5.5 KiB
PHP
163 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Api\Plugins;
|
|
|
|
use App\Models\ApiProvider;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class ComfyUi implements ApiPluginInterface
|
|
{
|
|
use LoggablePlugin;
|
|
|
|
protected $apiProvider;
|
|
|
|
public function __construct(ApiProvider $apiProvider)
|
|
{
|
|
$this->apiProvider = $apiProvider;
|
|
$this->logInfo('ComfyUi plugin initialized.', ['provider_name' => $apiProvider->name]);
|
|
}
|
|
|
|
public function getIdentifier(): string
|
|
{
|
|
return 'comfyui';
|
|
}
|
|
|
|
public function getName(): string
|
|
{
|
|
return 'ComfyUI';
|
|
}
|
|
|
|
public function isEnabled(): bool
|
|
{
|
|
return $this->apiProvider->enabled;
|
|
}
|
|
|
|
public function enable(): bool
|
|
{
|
|
$this->apiProvider->enabled = true;
|
|
$result = $this->apiProvider->save();
|
|
if ($result) {
|
|
$this->logInfo('ComfyUi plugin enabled.', ['provider_name' => $this->apiProvider->name]);
|
|
} else {
|
|
$this->logError('Failed to enable ComfyUi plugin.', ['provider_name' => $this->apiProvider->name]);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function disable(): bool
|
|
{
|
|
$this->apiProvider->enabled = false;
|
|
$result = $this->apiProvider->save();
|
|
if ($result) {
|
|
$this->logInfo('ComfyUi plugin disabled.', ['provider_name' => $this->apiProvider->name]);
|
|
} else {
|
|
$this->logError('Failed to disable ComfyUi plugin.', ['provider_name' => $this->apiProvider->name]);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function getStatus(string $imageUUID): array
|
|
{
|
|
$this->logDebug('Getting status for image.', ['image_uuid' => $imageUUID]);
|
|
// Implement ComfyUI specific status check
|
|
return ['status' => 'unknown'];
|
|
}
|
|
|
|
public function getProgress(string $imageUUID): array
|
|
{
|
|
$this->logDebug('Getting progress for image.', ['image_uuid' => $imageUUID]);
|
|
// Implement ComfyUI specific progress check
|
|
return ['progress' => 0];
|
|
}
|
|
|
|
public function processImageStyleChange(string $imagePath, string $prompt, string $modelId, ?string $parameters = null): array
|
|
{
|
|
$this->logInfo('Starting ComfyUI style change process.', ['image_path' => $imagePath]);
|
|
|
|
// 1. Upload image to ComfyUI
|
|
$uploadResponse = $this->uploadImage($imagePath);
|
|
$filename = $uploadResponse['name'];
|
|
|
|
// 2. Construct the prompt
|
|
$promptData = $this->constructPrompt($prompt, $filename, $modelId, $parameters);
|
|
|
|
// 3. Queue the prompt
|
|
$queueResponse = $this->queuePrompt($promptData);
|
|
$promptId = $queueResponse['prompt_id'];
|
|
|
|
// 4. Wait for and get the result
|
|
$result = $this->waitForResult($promptId);
|
|
|
|
return ['base64Data' => $result];
|
|
}
|
|
|
|
private function uploadImage(string $imagePath): array
|
|
{
|
|
$this->logInfo('Uploading image to ComfyUI.', ['image_path' => $imagePath]);
|
|
$response = Http::attach(
|
|
'image', file_get_contents($imagePath), basename($imagePath)
|
|
)->post($this->apiProvider->api_url . '/upload/image');
|
|
|
|
if ($response->failed()) {
|
|
$this->logError('ComfyUI image upload failed.', ['response' => $response->body()]);
|
|
throw new \Exception('Failed to upload image to ComfyUI');
|
|
}
|
|
|
|
return $response->json();
|
|
}
|
|
|
|
private function constructPrompt(string $prompt, string $filename, string $modelId, ?string $parameters): array
|
|
{
|
|
if (empty($parameters)) {
|
|
throw new \Exception('ComfyUI workflow (parameters) is missing.');
|
|
}
|
|
|
|
$workflow = $parameters;
|
|
$workflow = str_replace('__PROMPT__', $prompt, $workflow);
|
|
$workflow = str_replace('__FILENAME__', $filename, $workflow);
|
|
$workflow = str_replace('__MODEL_ID__', $modelId, $workflow);
|
|
|
|
return json_decode($workflow, true);
|
|
}
|
|
|
|
private function queuePrompt(array $promptData): array
|
|
{
|
|
$this->logInfo('Queueing prompt in ComfyUI.');
|
|
$response = Http::post($this->apiProvider->api_url . '/prompt', ['prompt' => $promptData]);
|
|
|
|
if ($response->failed()) {
|
|
$this->logError('Failed to queue prompt in ComfyUI.', ['response' => $response->body()]);
|
|
throw new \Exception('Failed to queue prompt in ComfyUI');
|
|
}
|
|
|
|
return $response->json();
|
|
}
|
|
|
|
private function waitForResult(string $promptId): string
|
|
{
|
|
$this->logInfo('Waiting for ComfyUI result.', ['prompt_id' => $promptId]);
|
|
while (true) {
|
|
$response = Http::get($this->apiProvider->api_url . '/history/' . $promptId);
|
|
$data = $response->json();
|
|
|
|
if (!empty($data[$promptId]['outputs'])) {
|
|
$outputs = $data[$promptId]['outputs'];
|
|
// Assuming the first output with an image is the one we want
|
|
foreach ($outputs as $output) {
|
|
if (isset($output['images'][0]['type']) && $output['images'][0]['type'] === 'output') {
|
|
$imageUrl = sprintf('%s/view?filename=%s&subfolder=%s&type=output',
|
|
$this->apiProvider->api_url,
|
|
$output['images'][0]['filename'],
|
|
$output['images'][0]['subfolder']
|
|
);
|
|
$image_data = file_get_contents($imageUrl);
|
|
return base64_encode($image_data);
|
|
}
|
|
}
|
|
}
|
|
|
|
sleep(2); // Wait for 2 seconds before polling again
|
|
}
|
|
}
|
|
}
|