assist-feedback.php
10 months ago
assistant.php
6 months ago
base.php
6 months ago
dropped-file.php
5 months ago
edit-image.php
8 months ago
embed.php
7 months ago
feedback.php
10 months ago
function.php
8 months ago
image.php
6 months ago
parameter.php
11 months ago
text.php
6 months ago
transcribe.php
8 months ago
image.php
173 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Query_Image extends Meow_MWAI_Query_Base { |
| 4 | public ?string $resolution = null; |
| 5 | public ?string $style = null; |
| 6 | public ?string $localDownload = 'uploads'; |
| 7 | public ?string $localDownloadExpiry = 'uploads'; |
| 8 | public ?array $attachedFiles = null; |
| 9 | |
| 10 | #region Constructors, Serialization |
| 11 | |
| 12 | public function __construct( ?string $message = '', ?string $model = null ) { |
| 13 | parent::__construct( $message ); |
| 14 | $this->model = $model; |
| 15 | $this->feature = 'text-to-image'; // image-to-image, inpainting, etc |
| 16 | global $mwai_core; |
| 17 | $this->localDownload = $mwai_core->get_option( 'image_local_download' ); |
| 18 | $this->localDownloadExpiry = $mwai_core->get_option( 'image_expires_download' ); |
| 19 | } |
| 20 | |
| 21 | #[\ReturnTypeWillChange] |
| 22 | public function jsonSerialize(): array { |
| 23 | $json = [ |
| 24 | 'message' => $this->message, |
| 25 | |
| 26 | 'ai' => [ |
| 27 | 'model' => $this->model, |
| 28 | 'feature' => $this->feature, |
| 29 | 'resolution' => $this->resolution |
| 30 | ], |
| 31 | |
| 32 | 'system' => [ |
| 33 | 'class' => get_class( $this ), |
| 34 | 'envId' => $this->envId, |
| 35 | 'scope' => $this->scope, |
| 36 | 'session' => $this->session, |
| 37 | 'customId' => $this->customId, |
| 38 | ] |
| 39 | ]; |
| 40 | |
| 41 | if ( !empty( $this->context ) ) { |
| 42 | $json['context']['content'] = $this->context; |
| 43 | } |
| 44 | |
| 45 | return $json; |
| 46 | } |
| 47 | |
| 48 | #endregion |
| 49 | |
| 50 | #region Parameters |
| 51 | |
| 52 | public function set_resolution( string $resolution ) { |
| 53 | $this->resolution = $resolution; |
| 54 | } |
| 55 | |
| 56 | public function set_style( string $style ) { |
| 57 | $this->style = $style; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Set how the image will be treated locally, if it will be downloaded or not, etc. |
| 62 | * @param string $localDownload The local download method. Could be 'uploads', 'library' or null. |
| 63 | */ |
| 64 | public function set_local_download( ?string $localDownload ) { |
| 65 | $this->localDownload = $localDownload; |
| 66 | } |
| 67 | |
| 68 | public function add_file( Meow_MWAI_Query_DroppedFile $file ): void { |
| 69 | if ( $this->attachedFiles === null ) { |
| 70 | $this->attachedFiles = []; |
| 71 | } |
| 72 | $this->attachedFiles[] = $file; |
| 73 | } |
| 74 | |
| 75 | public function set_files( array $files ): void { |
| 76 | $this->attachedFiles = $files; |
| 77 | } |
| 78 | |
| 79 | public function get_files(): ?array { |
| 80 | return $this->attachedFiles; |
| 81 | } |
| 82 | |
| 83 | public function getAttachments(): array { |
| 84 | return $this->attachedFiles ?? []; |
| 85 | } |
| 86 | |
| 87 | // Based on the params of the query, update the attributes |
| 88 | public function inject_params( array $params ): void { |
| 89 | parent::inject_params( $params ); |
| 90 | $params = $this->convert_keys( $params ); |
| 91 | |
| 92 | if ( !empty( $params['resolution'] ) ) { |
| 93 | $this->set_resolution( $params['resolution'] ); |
| 94 | } |
| 95 | if ( !empty( $params['style'] ) ) { |
| 96 | $this->set_style( $params['style'] ); |
| 97 | } |
| 98 | // Check both camelCase and snake_case versions for compatibility |
| 99 | if ( array_key_exists( 'localDownload', $params ) ) { |
| 100 | $this->set_local_download( $params['localDownload'] ); |
| 101 | } |
| 102 | elseif ( array_key_exists( 'local_download', $params ) ) { |
| 103 | $this->set_local_download( $params['local_download'] ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | #endregion |
| 108 | |
| 109 | #region Final Checks |
| 110 | |
| 111 | public function final_checks() { |
| 112 | parent::final_checks(); |
| 113 | |
| 114 | // Since DALL-E 3 only supports 1 image, we force it. |
| 115 | // (Likely the same limitation for other models.) |
| 116 | $this->maxResults = 1; |
| 117 | |
| 118 | global $mwai_core; |
| 119 | |
| 120 | $engine = Meow_MWAI_Engines_Factory::get( $mwai_core, $this->envId ); |
| 121 | |
| 122 | // If model is empty, use the image-specific default model (not the general default) |
| 123 | if ( empty( $this->model ) ) { |
| 124 | $this->model = $mwai_core->get_option( 'ai_images_default_model' ); |
| 125 | if ( empty( $this->model ) ) { |
| 126 | // Fallback to general default if image-specific default is not set |
| 127 | $this->model = $mwai_core->get_option( 'ai_default_model' ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | $modelInfo = $engine->retrieve_model_info( $this->model ); |
| 132 | if ( empty( $modelInfo ) ) { |
| 133 | Meow_MWAI_Logging::error( 'No model info found for model: ' . $this->model, '🖼️' ); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | // Let's check for resolutions. |
| 138 | if ( !isset( $modelInfo['resolutions'] ) || empty( $modelInfo['resolutions'] ) ) { |
| 139 | // Skip warning for non-image models (e.g., when using image_generation as a tool) |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | // If we have no resolution set, we will use the first one |
| 144 | if ( empty( $this->resolution ) ) { |
| 145 | $this->resolution = $modelInfo['resolutions'][0]['name']; |
| 146 | } |
| 147 | |
| 148 | // If we have a resolutions array ([ name, label ]), let's ensure our current resolution (name) is supported |
| 149 | $resolutions = $modelInfo['resolutions']; |
| 150 | $found = false; |
| 151 | foreach ( $resolutions as $resolution ) { |
| 152 | if ( $resolution['name'] === $this->resolution ) { |
| 153 | $found = true; |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // If we don't find the resolution, we will set it to the first one. |
| 159 | if ( !$found ) { |
| 160 | $supportedResolutions = []; |
| 161 | foreach ( $resolutions as $resolution ) { |
| 162 | $supportedResolutions[] = $resolution['name']; |
| 163 | } |
| 164 | $supportedResolutions = implode( ', ', $supportedResolutions ); |
| 165 | $error = sprintf( 'The model %s does not support the resolution %s (using %s instead). Supported resolutions are: %s.', $this->model, $this->resolution, $resolutions[0]['name'], $supportedResolutions ); |
| 166 | $this->resolution = $resolutions[0]['name']; |
| 167 | Meow_MWAI_Logging::error( $error, '🖼️' ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | #endregion |
| 172 | } |
| 173 |