modules
3 years ago
admin.php
3 years ago
ai.php
3 years ago
answer.php
3 years ago
api.php
3 years ago
core.php
3 years ago
init.php
3 years ago
openai.php
3 years ago
query.php
3 years ago
queryembed.php
3 years ago
queryimage.php
3 years ago
querytext.php
3 years ago
querytranscribe.php
3 years ago
rest.php
3 years ago
security.php
3 years ago
rest.php
888 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Rest |
| 4 | { |
| 5 | private $core = null; |
| 6 | private $namespace = 'ai-engine/v1'; |
| 7 | |
| 8 | public function __construct( $core ) { |
| 9 | $this->core = $core; |
| 10 | add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
| 11 | } |
| 12 | |
| 13 | function rest_api_init() { |
| 14 | try { |
| 15 | register_rest_route( $this->namespace, '/update_option', array( |
| 16 | 'methods' => 'POST', |
| 17 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 18 | 'callback' => array( $this, 'rest_update_option' ) |
| 19 | ) ); |
| 20 | register_rest_route( $this->namespace, '/all_settings', array( |
| 21 | 'methods' => 'GET', |
| 22 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 23 | 'callback' => array( $this, 'rest_all_settings' ), |
| 24 | ) ); |
| 25 | register_rest_route( $this->namespace, '/make_completions', array( |
| 26 | 'methods' => 'POST', |
| 27 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 28 | 'callback' => array( $this, 'make_completions' ), |
| 29 | ) ); |
| 30 | register_rest_route( $this->namespace, '/make_images', array( |
| 31 | 'methods' => 'POST', |
| 32 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 33 | 'callback' => array( $this, 'make_images' ), |
| 34 | ) ); |
| 35 | register_rest_route( $this->namespace, '/make_titles', array( |
| 36 | 'methods' => 'POST', |
| 37 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 38 | 'callback' => array( $this, 'make_titles' ), |
| 39 | ) ); |
| 40 | register_rest_route( $this->namespace, '/make_excerpts', array( |
| 41 | 'methods' => 'POST', |
| 42 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 43 | 'callback' => array( $this, 'make_excerpts' ), |
| 44 | ) ); |
| 45 | register_rest_route( $this->namespace, '/update_post_title', array( |
| 46 | 'methods' => 'POST', |
| 47 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 48 | 'callback' => array( $this, 'update_post_title' ), |
| 49 | ) ); |
| 50 | register_rest_route( $this->namespace, '/update_post_excerpt', array( |
| 51 | 'methods' => 'POST', |
| 52 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 53 | 'callback' => array( $this, 'update_post_excerpt' ), |
| 54 | ) ); |
| 55 | register_rest_route( $this->namespace, '/copilot', array( |
| 56 | 'methods' => 'POST', |
| 57 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 58 | 'callback' => array( $this, 'copilot' ), |
| 59 | ) ); |
| 60 | register_rest_route( $this->namespace, '/create_post', array( |
| 61 | 'methods' => 'POST', |
| 62 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 63 | 'callback' => array( $this, 'create_post' ), |
| 64 | ) ); |
| 65 | register_rest_route( $this->namespace, '/create_image', array( |
| 66 | 'methods' => 'POST', |
| 67 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 68 | 'callback' => array( $this, 'create_image' ), |
| 69 | ) ); |
| 70 | register_rest_route( $this->namespace, '/openai_files', array( |
| 71 | 'methods' => 'GET', |
| 72 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 73 | 'callback' => array( $this, 'openai_files_get' ), |
| 74 | ) ); |
| 75 | register_rest_route( $this->namespace, '/openai_files', array( |
| 76 | 'methods' => 'DELETE', |
| 77 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 78 | 'callback' => array( $this, 'openai_files_delete' ), |
| 79 | ) ); |
| 80 | register_rest_route( $this->namespace, '/openai_files', array( |
| 81 | 'methods' => 'POST', |
| 82 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 83 | 'callback' => array( $this, 'openai_files_upload' ), |
| 84 | ) ); |
| 85 | register_rest_route( $this->namespace, '/openai_files_download', array( |
| 86 | 'methods' => 'POST', |
| 87 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 88 | 'callback' => array( $this, 'openai_files_download' ), |
| 89 | ) ); |
| 90 | register_rest_route( $this->namespace, '/openai_files_finetune', array( |
| 91 | 'methods' => 'POST', |
| 92 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 93 | 'callback' => array( $this, 'openai_files_finetune' ), |
| 94 | ) ); |
| 95 | register_rest_route( $this->namespace, '/openai_deleted_finetunes', array( |
| 96 | 'methods' => 'GET', |
| 97 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 98 | 'callback' => array( $this, 'openai_deleted_finetunes_get' ), |
| 99 | ) ); |
| 100 | register_rest_route( $this->namespace, '/openai_finetunes', array( |
| 101 | 'methods' => 'GET', |
| 102 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 103 | 'callback' => array( $this, 'openai_finetunes_get' ), |
| 104 | ) ); |
| 105 | register_rest_route( $this->namespace, '/openai_finetunes', array( |
| 106 | 'methods' => 'DELETE', |
| 107 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 108 | 'callback' => array( $this, 'openai_finetunes_delete' ), |
| 109 | ) ); |
| 110 | register_rest_route( $this->namespace, '/openai_finetunes_cancel', array( |
| 111 | 'methods' => 'POST', |
| 112 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 113 | 'callback' => array( $this, 'openai_finetunes_cancel' ), |
| 114 | ) ); |
| 115 | register_rest_route( $this->namespace, '/openai_incidents', array( |
| 116 | 'methods' => 'GET', |
| 117 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 118 | 'callback' => array( $this, 'openai_incidents' ), |
| 119 | ) ); |
| 120 | register_rest_route( $this->namespace, '/chatbots', array( |
| 121 | 'methods' => ['GET', 'PUT'], |
| 122 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 123 | 'callback' => array( $this, 'rest_chatbots' ), |
| 124 | ) ); |
| 125 | register_rest_route( $this->namespace, '/themes', array( |
| 126 | 'methods' => ['GET', 'PUT'], |
| 127 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 128 | 'callback' => array( $this, 'rest_themes' ), |
| 129 | ) ); |
| 130 | register_rest_route( $this->namespace, '/count_posts', array( |
| 131 | 'methods' => 'GET', |
| 132 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 133 | 'callback' => array( $this, 'count_posts' ), |
| 134 | ) ); |
| 135 | register_rest_route( $this->namespace, '/post_types', array( |
| 136 | 'methods' => 'GET', |
| 137 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 138 | 'callback' => array( $this, 'post_types' ), |
| 139 | ) ); |
| 140 | register_rest_route( $this->namespace, '/post_content', array( |
| 141 | 'methods' => 'GET', |
| 142 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 143 | 'callback' => array( $this, 'post_content' ), |
| 144 | ) ); |
| 145 | register_rest_route( $this->namespace, '/templates', array( |
| 146 | 'methods' => 'GET', |
| 147 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 148 | 'callback' => array( $this, 'templates_get' ), |
| 149 | ) ); |
| 150 | register_rest_route( $this->namespace, '/templates', array( |
| 151 | 'methods' => 'POST', |
| 152 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 153 | 'callback' => array( $this, 'templates_save' ), |
| 154 | ) ); |
| 155 | register_rest_route( $this->namespace, '/magic_wand', array( |
| 156 | 'methods' => 'POST', |
| 157 | 'callback' => array( $this, 'magic_wand' ), |
| 158 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 159 | ) ); |
| 160 | register_rest_route( $this->namespace, '/logs', array( |
| 161 | 'methods' => 'POST', |
| 162 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 163 | 'callback' => array( $this, 'get_logs' ), |
| 164 | ) ); |
| 165 | register_rest_route( $this->namespace, '/moderate', array( |
| 166 | 'methods' => 'POST', |
| 167 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 168 | 'callback' => array( $this, 'moderate' ), |
| 169 | ) ); |
| 170 | register_rest_route( $this->namespace, '/vectors', array( |
| 171 | 'methods' => 'POST', |
| 172 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 173 | 'callback' => array( $this, 'get_vectors' ), |
| 174 | ) ); |
| 175 | register_rest_route( $this->namespace, '/vector', array( |
| 176 | 'methods' => 'POST', |
| 177 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 178 | 'callback' => array( $this, 'add_vector' ), |
| 179 | ) ); |
| 180 | register_rest_route( $this->namespace, '/vectors_ref', array( |
| 181 | 'methods' => 'POST', |
| 182 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 183 | 'callback' => array( $this, 'get_vectors_ref' ), |
| 184 | ) ); |
| 185 | register_rest_route( $this->namespace, '/vector', array( |
| 186 | 'methods' => 'PUT', |
| 187 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 188 | 'callback' => array( $this, 'modify_vector' ), |
| 189 | ) ); |
| 190 | register_rest_route( $this->namespace, '/vectors', array( |
| 191 | 'methods' => 'DELETE', |
| 192 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 193 | 'callback' => array( $this, 'delete_vectors' ), |
| 194 | ) ); |
| 195 | register_rest_route( $this->namespace, '/transcribe', array( |
| 196 | 'methods' => 'POST', |
| 197 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 198 | 'callback' => array( $this, 'transcribe' ), |
| 199 | ) ); |
| 200 | } |
| 201 | catch ( Exception $e ) { |
| 202 | var_dump( $e ); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | function rest_all_settings() { |
| 207 | return new WP_REST_Response( [ |
| 208 | 'success' => true, |
| 209 | 'data' => $this->core->get_all_options() |
| 210 | ], 200 ); |
| 211 | } |
| 212 | |
| 213 | function rest_update_option( $request ) { |
| 214 | try { |
| 215 | $params = $request->get_json_params(); |
| 216 | $value = $params['options']; |
| 217 | $options = $this->core->update_options( $value ); |
| 218 | $success = !!$options; |
| 219 | $message = __( $success ? 'OK' : "Could not update options.", 'ai-engine' ); |
| 220 | return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 ); |
| 221 | } |
| 222 | catch ( Exception $e ) { |
| 223 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | function createValidationResult( $result = true, $message = null) { |
| 228 | $message = $message ? $message : __( 'OK', 'ai-engine' ); |
| 229 | return [ 'result' => $result, 'message' => $message ]; |
| 230 | } |
| 231 | |
| 232 | function validate_updated_option( $option_name ) { |
| 233 | $option_checkbox = get_option( 'mwai_option_checkbox', false ); |
| 234 | $option_text = get_option( 'mwai_option_text', 'Default' ); |
| 235 | if ( $option_checkbox === '' ) |
| 236 | update_option( 'mwai_option_checkbox', false ); |
| 237 | if ( $option_text === '' ) |
| 238 | update_option( 'mwai_option_text', 'Default' ); |
| 239 | return $this->createValidationResult(); |
| 240 | } |
| 241 | |
| 242 | function make_completions( $request ) { |
| 243 | try { |
| 244 | $params = $request->get_json_params(); |
| 245 | $prompt = $params['prompt']; |
| 246 | $query = new Meow_MWAI_QueryText( $prompt ); |
| 247 | $query->injectParams( $params ); |
| 248 | $answer = $this->core->ai->run( $query ); |
| 249 | return new WP_REST_Response([ 'success' => true, 'data' => $answer->result, 'usage' => $answer->usage ], 200 ); |
| 250 | } |
| 251 | catch ( Exception $e ) { |
| 252 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | function make_images( $request ) { |
| 257 | try { |
| 258 | $params = $request->get_json_params(); |
| 259 | $prompt = $params['prompt']; |
| 260 | $query = new Meow_MWAI_QueryImage( $prompt ); |
| 261 | $query->injectParams( $params ); |
| 262 | $answer = $this->core->ai->run( $query ); |
| 263 | return new WP_REST_Response([ 'success' => true, 'data' => $answer->results, 'usage' => $answer->usage ], 200 ); |
| 264 | } |
| 265 | catch ( Exception $e ) { |
| 266 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | function magic_wand( $request ) { |
| 271 | try { |
| 272 | $params = $request->get_json_params(); |
| 273 | $action = isset( $params['action'] ) ? $params['action'] : null; |
| 274 | $data = isset( $params['data'] ) ? $params['data'] : null; |
| 275 | if ( empty( $data ) || empty( $action ) ) { |
| 276 | return new WP_REST_Response([ 'success' => false, 'message' => "An action and some data are required." ], 500 ); |
| 277 | } |
| 278 | $postId = isset( $data['postId'] ) ? $data['postId'] : null; |
| 279 | $text = isset( $data['text'] ) ? $data['text'] : null; |
| 280 | $selectedText = isset( $data['selectedText'] ) ? $data['selectedText'] : null; |
| 281 | |
| 282 | // TODO: As soon as we have a wide range of usages and possibilities, |
| 283 | // let's refactor this into a nice and extensible UI/API. |
| 284 | $query = new Meow_MWAI_QueryText( "", 1024 ); |
| 285 | $query->setEnv( 'admin-tools' ); |
| 286 | $mode = 'replace'; |
| 287 | if ( $action === 'correctText' ) { |
| 288 | $query->setPrompt( "Correct the typos and grammar mistakes in this text without altering its content. Return only the corrected text, without explanations or additional content.\n\n" . $text ); |
| 289 | } |
| 290 | else if ( $action === 'enhanceText' ) { |
| 291 | $query->setPrompt( "Enhance this text by eliminating redundancies, utilizing a more suitable vocabulary, and refining its structure. Provide only the revised text, without explanations or any additional content.\n\n" . $text ); |
| 292 | } |
| 293 | else if ( $action === 'longerText' ) { |
| 294 | $query->setPrompt( "Expand the subsequent text to a minimum of three times its original length, integrating relevant and accurate information to enrich its content. If the text is a story, amplify its charm by elaborating on essential aspects, enhancing readability, and creating a sense of engagement for the reader. Maintain consistency in tone and vocabulary throughout the expansion process.\n\n" . $text ); |
| 295 | } |
| 296 | else if ( $action === 'shorterText' ) { |
| 297 | $query->setPrompt( "Condense the following text by reducing its length to half, while retaining the core elements of the original narrative. Focus on maintaining the essence of the story and its key details.\n\n" . $text ); |
| 298 | } |
| 299 | else if ( $action === 'suggestSynonyms' ) { |
| 300 | $mode = 'suggest'; |
| 301 | $query->setPrompt( "Provide a synonym or rephrase the given word or sentence while retaining the original meaning and preserving the initial and final punctuation. Offer only the resulting word or expression, without additional context. If a suitable synonym or alternative cannot be identified, ensure that a creative response is still provided.\n\n" . $selectedText ); |
| 302 | $query->setTemperature( 1 ); |
| 303 | $query->setMaxResults( 5 ); |
| 304 | } |
| 305 | else if ( $action === 'translateText' ) { |
| 306 | $query->setPrompt( "Translate the text into {LANGUAGE}, preserving the tone, mood, and nuance, while staying as true as possible to the original meaning. Provide only the translated text, without any additional content.\n\n" . $text ); |
| 307 | $language = $this->core->get_post_language( $postId ); |
| 308 | $query->replace( '{LANGUAGE}', $language ); |
| 309 | } |
| 310 | $answer = $this->core->ai->run( $query ); |
| 311 | return new WP_REST_Response([ 'success' => true, 'data' => [ |
| 312 | 'mode' => $mode, |
| 313 | 'result' => $answer->result, |
| 314 | 'results' => $answer->results |
| 315 | ] ], 200 ); |
| 316 | } |
| 317 | catch ( Exception $e ) { |
| 318 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | function make_titles( $request ) { |
| 323 | try { |
| 324 | $params = $request->get_json_params(); |
| 325 | $postId = intval( $params['postId'] ); |
| 326 | $text = $this->core->getCleanPostContent( $postId ); |
| 327 | if ( empty( $text ) ) { |
| 328 | return new WP_REST_Response([ 'success' => false, 'message' => "No text found for this post." ], 500 ); |
| 329 | } |
| 330 | $language = $this->core->get_post_language( $postId ); |
| 331 | $prompt = "Using the same original language ($language), create a short but SEO-friendly title for this text: " . $text; |
| 332 | $query = new Meow_MWAI_QueryText( $prompt, 128 ); |
| 333 | $query->setMaxResults( 5 ); |
| 334 | $query->setEnv( 'admin-tools' ); |
| 335 | $answer = $this->core->ai->run( $query ); |
| 336 | return new WP_REST_Response([ 'success' => true, 'data' => $answer->results ], 200 ); |
| 337 | } |
| 338 | catch ( Exception $e ) { |
| 339 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | function make_excerpts( $request ) { |
| 344 | try { |
| 345 | $params = $request->get_json_params(); |
| 346 | $postId = intval( $params['postId'] ); |
| 347 | $text = $this->core->getCleanPostContent( $postId ); |
| 348 | if ( empty( $text ) ) { |
| 349 | return new WP_REST_Response([ 'success' => false, 'message' => "No text found for this post." ], 500 ); |
| 350 | } |
| 351 | $language = $this->core->get_post_language( $postId ); |
| 352 | $prompt = "Using the same original language ($language), create a SEO-friendly introduction to this text, 120 to 170 characters max, no URLs: " . $text; |
| 353 | $query = new Meow_MWAI_QueryText( $prompt, 512 ); |
| 354 | $query->setMaxResults( 5 ); |
| 355 | $query->setEnv( 'admin-tools' ); |
| 356 | $answer = $this->core->ai->run( $query ); |
| 357 | return new WP_REST_Response([ 'success' => true, 'data' => $answer->results ], 200 ); |
| 358 | } |
| 359 | catch ( Exception $e ) { |
| 360 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | function copilot( $request ) { |
| 365 | try { |
| 366 | $params = $request->get_json_params(); |
| 367 | $action = sanitize_text_field( $params['action'] ); |
| 368 | $prompt = sanitize_text_field( $params['prompt'] ); |
| 369 | if ( empty( $action ) || empty( $prompt ) ) { |
| 370 | return new WP_REST_Response([ 'success' => false, 'message' => "Copilot needs an action and a prompt." ], 500 ); |
| 371 | } |
| 372 | $query = new Meow_MWAI_QueryText( $prompt, 2048 ); |
| 373 | $query->setEnv( 'admin-tools' ); |
| 374 | $answer = $this->core->ai->run( $query ); |
| 375 | return new WP_REST_Response([ 'success' => true, 'data' => $answer->result ], 200 ); |
| 376 | } |
| 377 | catch ( Exception $e ) { |
| 378 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | function update_post_title( $request ) { |
| 383 | try { |
| 384 | $params = $request->get_json_params(); |
| 385 | $title = sanitize_text_field( $params['title'] ); |
| 386 | $postId = intval( $params['postId'] ); |
| 387 | $post = get_post( $postId ); |
| 388 | if ( !$post ) { |
| 389 | throw new Exception( 'There is no post with this ID.' ); |
| 390 | } |
| 391 | $post->post_title = $title; |
| 392 | //$post->post_name = sanitize_title( $title ); |
| 393 | wp_update_post( $post ); |
| 394 | return new WP_REST_Response([ 'success' => true, 'message' => "Title updated." ], 200 ); |
| 395 | } |
| 396 | catch ( Exception $e ) { |
| 397 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | function update_post_excerpt( $request ) { |
| 402 | try { |
| 403 | $params = $request->get_json_params(); |
| 404 | $excerpt = sanitize_text_field( $params['excerpt'] ); |
| 405 | $postId = intval( $params['postId'] ); |
| 406 | $post = get_post( $postId ); |
| 407 | if ( !$post ) { |
| 408 | throw new Exception( 'There is no post with this ID.' ); |
| 409 | } |
| 410 | $post->post_excerpt = $excerpt; |
| 411 | wp_update_post( $post ); |
| 412 | return new WP_REST_Response([ 'success' => true, 'message' => "Excerpt updated." ], 200 ); |
| 413 | } |
| 414 | catch ( Exception $e ) { |
| 415 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | function create_post( $request ) { |
| 420 | try { |
| 421 | $params = $request->get_json_params(); |
| 422 | $title = sanitize_text_field( $params['title'] ); |
| 423 | $content = sanitize_textarea_field( $params['content'] ); |
| 424 | $excerpt = sanitize_text_field( $params['excerpt'] ); |
| 425 | $postType = sanitize_text_field( $params['postType'] ); |
| 426 | $post = new stdClass(); |
| 427 | $post->post_title = $title; |
| 428 | $post->post_excerpt = $excerpt; |
| 429 | $post->post_content = $content; |
| 430 | $post->post_status = 'draft'; |
| 431 | $post->post_type = isset( $postType ) ? $postType : 'post'; |
| 432 | $post->post_content = $this->core->markdown_to_html( $post->post_content ); |
| 433 | $postId = wp_insert_post( $post ); |
| 434 | return new WP_REST_Response([ 'success' => true, 'postId' => $postId ], 200 ); |
| 435 | } |
| 436 | catch ( Exception $e ) { |
| 437 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | function image_download( $url ) { |
| 442 | $response = wp_remote_get( $url ); |
| 443 | $output = wp_remote_retrieve_body( $response ); |
| 444 | return $output; |
| 445 | } |
| 446 | |
| 447 | function create_image( $request ) { |
| 448 | try { |
| 449 | $params = $request->get_json_params(); |
| 450 | $title = sanitize_text_field( $params['title'] ); |
| 451 | $caption = sanitize_text_field( $params['caption'] ); |
| 452 | $alt = sanitize_text_field( $params['alt'] ); |
| 453 | $description = sanitize_text_field( $params['description'] ); |
| 454 | $url = $params['url']; |
| 455 | $filename = sanitize_text_field( $params['filename'] ); |
| 456 | $image_data = $this->image_download( $url ); |
| 457 | if ( !$image_data ) { |
| 458 | throw new Exception( 'Could not download the image.' ); |
| 459 | } |
| 460 | $upload_dir = wp_upload_dir(); |
| 461 | if ( empty( $filename ) ) { |
| 462 | $filename = basename( $url ); |
| 463 | } |
| 464 | $wp_filetype = wp_check_filetype( $filename ); |
| 465 | if ( wp_mkdir_p( $upload_dir['path'] ) ) { |
| 466 | $file = $upload_dir['path'] . '/' . $filename; |
| 467 | } |
| 468 | else { |
| 469 | $file = $upload_dir['basedir'] . '/' . $filename; |
| 470 | } |
| 471 | |
| 472 | // Make sure the file is unique, if not, add a number to the end of the file before the extension |
| 473 | $i = 1; |
| 474 | $parts = pathinfo( $file ); |
| 475 | while ( file_exists( $file ) ) { |
| 476 | $file = $parts['dirname'] . '/' . $parts['filename'] . '-' . $i . '.' . $parts['extension']; |
| 477 | $i++; |
| 478 | } |
| 479 | |
| 480 | // Write the file |
| 481 | file_put_contents( $file, $image_data ); |
| 482 | $attachment = [ |
| 483 | 'post_mime_type' => $wp_filetype['type'], |
| 484 | 'post_title' => $title, |
| 485 | 'post_content' => $description, |
| 486 | 'post_excerpt' => $caption, |
| 487 | 'post_status' => 'inherit' |
| 488 | ]; |
| 489 | // Register the file as a Media Library attachment |
| 490 | $attachmentId = wp_insert_attachment( $attachment, $file ); |
| 491 | require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
| 492 | $attachment_data = wp_generate_attachment_metadata( $attachmentId, $file ); |
| 493 | wp_update_attachment_metadata( $attachmentId, $attachment_data ); |
| 494 | update_post_meta( $attachmentId, '_wp_attachment_image_alt', $alt ); |
| 495 | return new WP_REST_Response([ 'success' => true, 'attachmentId' => $attachmentId ], 200 ); |
| 496 | } |
| 497 | catch ( Exception $e ) { |
| 498 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | function openai_files_get() { |
| 503 | try { |
| 504 | //$params = $request->get_json_params(); |
| 505 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 506 | $files = $openai->listFiles(); |
| 507 | return new WP_REST_Response([ 'success' => true, 'files' => $files ], 200 ); |
| 508 | } |
| 509 | catch ( Exception $e ) { |
| 510 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | function openai_deleted_finetunes_get() { |
| 515 | try { |
| 516 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 517 | $finetunes = $openai->listDeletedFineTunes(); |
| 518 | return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 ); |
| 519 | } |
| 520 | catch ( Exception $e ) { |
| 521 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | function openai_finetunes_get() { |
| 526 | try { |
| 527 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 528 | $finetunes = $openai->listFineTunes(); |
| 529 | return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 ); |
| 530 | } |
| 531 | catch ( Exception $e ) { |
| 532 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | function openai_files_upload( $request ) { |
| 537 | try { |
| 538 | $params = $request->get_json_params(); |
| 539 | $filename = sanitize_text_field( $params['filename'] ); |
| 540 | $data = $params['data']; |
| 541 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 542 | $file = $openai->uploadFile( $filename, $data ); |
| 543 | return new WP_REST_Response([ 'success' => true, 'file' => $file ], 200 ); |
| 544 | } |
| 545 | catch ( Exception $e ) { |
| 546 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | function openai_files_delete( $request ) { |
| 551 | try { |
| 552 | $params = $request->get_json_params(); |
| 553 | $fileId = $params['fileId']; |
| 554 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 555 | $openai->deleteFile( $fileId ); |
| 556 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 557 | } |
| 558 | catch ( Exception $e ) { |
| 559 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | function openai_finetunes_cancel( $request ) { |
| 564 | try { |
| 565 | $params = $request->get_json_params(); |
| 566 | $finetuneId = $params['finetuneId']; |
| 567 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 568 | $openai->cancelFineTune( $finetuneId ); |
| 569 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 570 | } |
| 571 | catch ( Exception $e ) { |
| 572 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | function openai_finetunes_delete( $request ) { |
| 577 | try { |
| 578 | $params = $request->get_json_params(); |
| 579 | $modelId = $params['modelId']; |
| 580 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 581 | $openai->deleteFineTune( $modelId ); |
| 582 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 583 | } |
| 584 | catch ( Exception $e ) { |
| 585 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | function openai_files_download( $request ) { |
| 590 | try { |
| 591 | $params = $request->get_json_params(); |
| 592 | $fileId = $params['fileId']; |
| 593 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 594 | $data = $openai->downloadFile( $fileId ); |
| 595 | return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 ); |
| 596 | } |
| 597 | catch ( Exception $e ) { |
| 598 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | function openai_files_finetune( $request ) { |
| 603 | try { |
| 604 | $params = $request->get_json_params(); |
| 605 | $fileId = $params['fileId']; |
| 606 | $model = $params['model']; |
| 607 | $suffix = $params['suffix']; |
| 608 | $hyperparams = [ |
| 609 | "nEpochs" => $params['nEpochs'], |
| 610 | "batchSize" => $params['batchSize'] |
| 611 | ]; |
| 612 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 613 | $finetune = $openai->fineTuneFile( $fileId, $model, $suffix, $hyperparams ); |
| 614 | return new WP_REST_Response([ 'success' => true, 'finetune' => $finetune ], 200 ); |
| 615 | } |
| 616 | catch ( Exception $e ) { |
| 617 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | function openai_incidents() { |
| 622 | try { |
| 623 | $transient = get_transient( 'mwai_openai_incidents' ); |
| 624 | if ( $transient ) { |
| 625 | return new WP_REST_Response([ 'success' => true, 'incidents' => $transient ], 200 ); |
| 626 | } |
| 627 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 628 | $incidents = $openai->getIncidents(); |
| 629 | set_transient( 'mwai_openai_incidents', $incidents, 60 * 10 ); |
| 630 | return new WP_REST_Response([ 'success' => true, 'incidents' => $incidents ], 200 ); |
| 631 | } |
| 632 | catch ( Exception $e ) { |
| 633 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | function count_posts( $request ) { |
| 638 | try { |
| 639 | $params = $request->get_query_params(); |
| 640 | $postType = $params['postType']; |
| 641 | $count = wp_count_posts( $postType ); |
| 642 | return new WP_REST_Response([ 'success' => true, 'count' => $count ], 200 ); |
| 643 | } |
| 644 | catch ( Exception $e ) { |
| 645 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | function post_content( $request ) { |
| 650 | try { |
| 651 | $params = $request->get_query_params(); |
| 652 | $offset = (int)$params['offset']; |
| 653 | $postType = $params['postType']; |
| 654 | $postId = (int)$params['postId']; |
| 655 | $post = null; |
| 656 | if ( !empty( $postId ) ) { |
| 657 | $post = get_post( $postId ); |
| 658 | if ( $post->post_status !== 'publish' && $post->post_status !== 'future' && $post->post_status !== 'draft' ) { |
| 659 | $post = null; |
| 660 | } |
| 661 | } |
| 662 | else { |
| 663 | $posts = get_posts( [ |
| 664 | 'posts_per_page' => 1, |
| 665 | 'post_type' => $postType, |
| 666 | 'offset' => $offset, |
| 667 | 'post_status' => 'publish' |
| 668 | ] ); |
| 669 | $post = count( $posts ) === 0 ? null : $posts[0]; |
| 670 | } |
| 671 | if ( !$post ) { |
| 672 | return new WP_REST_Response([ 'success' => false, 'message' => 'Post not found' ], 404 ); |
| 673 | } |
| 674 | $cleanPost = $this->core->getCleanPost( $post ); |
| 675 | return new WP_REST_Response([ 'success' => true, 'content' => $cleanPost['content'], |
| 676 | 'checksum' => $cleanPost['checksum'], 'language' => $cleanPost['language'], 'excerpt' => $cleanPost['excerpt'], |
| 677 | 'postId' => $cleanPost['postId'], 'title' => $cleanPost['title'], 'url' => $cleanPost['url'] ], 200 ); |
| 678 | } |
| 679 | catch ( Exception $e ) { |
| 680 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | function templates_get( $request ) { |
| 685 | try { |
| 686 | $params = $request->get_query_params(); |
| 687 | $category = $params['category']; |
| 688 | $templates = []; |
| 689 | $templates_option = get_option( 'mwai_templates', [] ); |
| 690 | if ( !is_array( $templates_option ) ) { |
| 691 | update_option( 'mwai_templates', [] ); |
| 692 | } |
| 693 | $categories = array_column( $templates_option, 'category' ); |
| 694 | $index = array_search( $category, $categories ); |
| 695 | $templates = []; |
| 696 | if ( $index !== false ) { |
| 697 | $templates = $templates_option[$index]['templates']; |
| 698 | } |
| 699 | return new WP_REST_Response([ 'success' => true, 'templates' => $templates ], 200 ); |
| 700 | } |
| 701 | catch ( Exception $e ) { |
| 702 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | function templates_save( $request ) { |
| 707 | try { |
| 708 | $params = $request->get_json_params(); |
| 709 | $category = $params['category']; |
| 710 | $templates = $params['templates']; |
| 711 | $templates_option = get_option( 'mwai_templates', [] ); |
| 712 | $categories = array_column( $templates_option, 'category' ); |
| 713 | $index = array_search( $category, $categories ); |
| 714 | if ( $index !== false && $index >= 0 ) { |
| 715 | $templates_option[$index]['templates'] = $templates; |
| 716 | } |
| 717 | else { |
| 718 | $group = [ 'category' => $category, 'templates' => $templates ]; |
| 719 | $templates_option[] = $group; |
| 720 | } |
| 721 | |
| 722 | update_option( 'mwai_templates', $templates_option ); |
| 723 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 724 | } |
| 725 | catch ( Exception $e ) { |
| 726 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | function get_logs( $request ) { |
| 731 | try { |
| 732 | $params = $request->get_json_params(); |
| 733 | $offset = $params['offset']; |
| 734 | $limit = $params['limit']; |
| 735 | $filters = $params['filters']; |
| 736 | $sort = $params['sort']; |
| 737 | $logs = apply_filters( 'mwai_stats_logs', [], $offset, $limit, $filters, $sort ); |
| 738 | return new WP_REST_Response([ 'success' => true, 'total' => $logs['total'], 'logs' => $logs['rows'] ], 200 ); |
| 739 | } |
| 740 | catch ( Exception $e ) { |
| 741 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | function moderate( $request ) { |
| 746 | try { |
| 747 | $params = $request->get_json_params(); |
| 748 | $text = $params['text']; |
| 749 | if ( !$text ) { |
| 750 | return new WP_REST_Response([ 'success' => false, 'message' => 'Text not found.' ], 404 ); |
| 751 | } |
| 752 | $openai = new Meow_MWAI_OpenAI( $this->core ); |
| 753 | $results = $openai->moderate( $text ); |
| 754 | return new WP_REST_Response([ 'success' => true, 'results' => $results ], 200 ); |
| 755 | } |
| 756 | catch ( Exception $e ) { |
| 757 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 758 | } |
| 759 | |
| 760 | } |
| 761 | |
| 762 | function get_vectors( $request ) { |
| 763 | try { |
| 764 | $params = $request->get_json_params(); |
| 765 | $offset = $params['offset']; |
| 766 | $limit = $params['limit']; |
| 767 | $filters = $params['filters']; |
| 768 | $sort = $params['sort']; |
| 769 | $vectors = apply_filters( 'mwai_embeddings_vectors', [], $offset, $limit, $filters, $sort ); |
| 770 | return new WP_REST_Response([ 'success' => true, 'total' => $vectors['total'], 'vectors' => $vectors['rows'] ], 200 ); |
| 771 | } |
| 772 | catch ( Exception $e ) { |
| 773 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | function add_vector( $request ) { |
| 778 | try { |
| 779 | $params = $request->get_json_params(); |
| 780 | $vector = $params['vector']; |
| 781 | $success = apply_filters( 'mwai_embeddings_vectors_add', false, $vector ); |
| 782 | return new WP_REST_Response([ 'success' => $success, 'vector' => $vector ], 200 ); |
| 783 | } |
| 784 | catch ( Exception $e ) { |
| 785 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | function get_vectors_ref( $request ) { |
| 790 | try { |
| 791 | $params = $request->get_json_params(); |
| 792 | $refId = $params['refId']; |
| 793 | $vectors = apply_filters( 'mwai_embeddings_vectors_ref', false, $refId ); |
| 794 | return new WP_REST_Response([ 'success' => true, 'vectors' => $vectors ], 200 ); |
| 795 | } |
| 796 | catch ( Exception $e ) { |
| 797 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | function modify_vector( $request ) { |
| 802 | try { |
| 803 | $params = $request->get_json_params(); |
| 804 | $vector = $params['vector']; |
| 805 | $success = apply_filters( 'mwai_embeddings_vectors_update', false, $vector ); |
| 806 | return new WP_REST_Response([ 'success' => $success, 'vector' => $vector ], 200 ); |
| 807 | } |
| 808 | catch ( Exception $e ) { |
| 809 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | function delete_vectors( $request ) { |
| 814 | try { |
| 815 | $params = $request->get_json_params(); |
| 816 | $ids = $params['ids']; |
| 817 | $success = apply_filters( 'mwai_embeddings_vectors_delete', false, $ids ); |
| 818 | return new WP_REST_Response([ 'success' => $success ], 200 ); |
| 819 | } |
| 820 | catch ( Exception $e ) { |
| 821 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | function transcribe( $request ) { |
| 826 | try { |
| 827 | $params = $request->get_json_params(); |
| 828 | $query = new Meow_MWAI_QueryTranscribe(); |
| 829 | $query->injectParams( $params ); |
| 830 | $query->setEnv('admin-tools'); |
| 831 | $answer = $this->core->ai->run( $query ); |
| 832 | return new WP_REST_Response([ 'success' => true, 'data' => $answer->result ], 200 ); |
| 833 | } |
| 834 | catch ( Exception $e ) { |
| 835 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | function post_types() { |
| 840 | try { |
| 841 | $postTypes = $this->core->getPostTypes(); |
| 842 | return new WP_REST_Response([ 'success' => true, 'postTypes' => $postTypes ], 200 ); |
| 843 | } |
| 844 | catch ( Exception $e ) { |
| 845 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | function rest_themes( $request ) { |
| 850 | try { |
| 851 | $method = $request->get_method(); |
| 852 | if ( $method === 'GET' ) { |
| 853 | $themes = $this->core->getThemes(); |
| 854 | return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 ); |
| 855 | } |
| 856 | else if ( $method === 'PUT' ) { |
| 857 | $params = $request->get_json_params(); |
| 858 | $themes = $params['themes']; |
| 859 | $themes = $this->core->updateThemes( $themes ); |
| 860 | return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 ); |
| 861 | } |
| 862 | } |
| 863 | catch ( Exception $e ) { |
| 864 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | function rest_chatbots( $request ) { |
| 869 | try { |
| 870 | $method = $request->get_method(); |
| 871 | if ( $method === 'GET' ) { |
| 872 | $chatbots = $this->core->getChatbots(); |
| 873 | return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 ); |
| 874 | } |
| 875 | else if ( $method === 'PUT' ) { |
| 876 | $params = $request->get_json_params(); |
| 877 | $chatbots = $params['chatbots']; |
| 878 | $chatbots = $this->core->updateChatbots( $chatbots ); |
| 879 | return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 ); |
| 880 | } |
| 881 | return new WP_REST_Response([ 'success' => false, 'message' => 'Method not allowed' ], 405 ); |
| 882 | } |
| 883 | catch ( Exception $e ) { |
| 884 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 |