engines
2 years ago
modules
2 years ago
queries
2 years ago
admin.php
2 years ago
api.php
2 years ago
core.php
2 years ago
init.php
2 years ago
reply.php
2 years ago
rest.php
2 years ago
rest.php
956 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Rest |
| 4 | { |
| 5 | private $core = null; |
| 6 | private $namespace = 'mwai/v1'; |
| 7 | |
| 8 | public function __construct( $core ) { |
| 9 | $this->core = $core; |
| 10 | add_action( 'rest_api_init', array( $this, 'rest_init' ) ); |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Retrieve the message from the parameters and optionally sanitize it. |
| 15 | * |
| 16 | * @param array &$params The parameters array, passed by reference. |
| 17 | * @param bool $sanitize Whether to sanitize the message using sanitize_text_field. |
| 18 | * @return string The retrieved (and optionally sanitized) message. |
| 19 | */ |
| 20 | function retrieve_message( &$params, $sanitize = false ) : string { |
| 21 | if ( isset( $params['message'] ) ) { |
| 22 | $message = $params['message']; |
| 23 | } |
| 24 | elseif ( isset( $params['prompt'] ) ) { |
| 25 | $message = $params['prompt']; |
| 26 | unset( $params['prompt'] ); |
| 27 | $params['message'] = $message; |
| 28 | $this->core->log( '⚠️ "prompt" is deprecated, please use "message" instead.' ); |
| 29 | } |
| 30 | else { |
| 31 | $message = ""; |
| 32 | } |
| 33 | |
| 34 | if ( $sanitize ) { |
| 35 | $message = sanitize_text_field( $message ); |
| 36 | } |
| 37 | |
| 38 | return $message; |
| 39 | } |
| 40 | |
| 41 | function rest_init() { |
| 42 | try { |
| 43 | // Settings Endpoints |
| 44 | register_rest_route( $this->namespace, '/settings/update', array( |
| 45 | 'methods' => 'POST', |
| 46 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 47 | 'callback' => [ $this, 'rest_settings_update' ], |
| 48 | ) ); |
| 49 | register_rest_route( $this->namespace, '/settings/options', array( |
| 50 | 'methods' => 'GET', |
| 51 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 52 | 'callback' => [ $this, 'rest_settings_list' ], |
| 53 | ) ); |
| 54 | register_rest_route( $this->namespace, '/settings/reset', array( |
| 55 | 'methods' => 'POST', |
| 56 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 57 | 'callback' => [ $this, 'rest_settings_reset' ], |
| 58 | ) ); |
| 59 | register_rest_route( $this->namespace, '/settings/chatbots', array( |
| 60 | 'methods' => ['GET', 'POST'], |
| 61 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 62 | 'callback' => [ $this, 'rest_settings_chatbots' ], |
| 63 | ) ); |
| 64 | register_rest_route( $this->namespace, '/settings/themes', array( |
| 65 | 'methods' => ['GET', 'POST'], |
| 66 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 67 | 'callback' => [ $this, 'rest_settings_themes' ], |
| 68 | ) ); |
| 69 | |
| 70 | // System Endpoints |
| 71 | register_rest_route( $this->namespace, '/system/logs/list', array( |
| 72 | 'methods' => 'POST', |
| 73 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 74 | 'callback' => [ $this, 'rest_system_logs_list' ], |
| 75 | ) ); |
| 76 | register_rest_route( $this->namespace, '/system/logs/delete', array( |
| 77 | 'methods' => 'POST', |
| 78 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 79 | 'callback' => [ $this, 'rest_system_logs_delete' ], |
| 80 | ) ); |
| 81 | register_rest_route( $this->namespace, '/system/logs/meta', array( |
| 82 | 'methods' => 'POST', |
| 83 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 84 | 'callback' => [ $this, 'rest_system_logs_meta_get' ], |
| 85 | ) ); |
| 86 | register_rest_route( $this->namespace, '/system/templates', array( |
| 87 | 'methods' => 'POST', |
| 88 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 89 | 'callback' => [ $this, 'rest_system_templates_save' ], |
| 90 | ) ); |
| 91 | register_rest_route( $this->namespace, '/system/templates', array( |
| 92 | 'methods' => 'GET', |
| 93 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 94 | 'callback' => [ $this, 'rest_system_templates_get' ], |
| 95 | ) ); |
| 96 | |
| 97 | // AI Endpoints |
| 98 | register_rest_route( $this->namespace, '/ai/models', array( |
| 99 | 'methods' => 'POST', |
| 100 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 101 | 'callback' => [ $this, 'rest_ai_models' ], |
| 102 | ) ); |
| 103 | register_rest_route( $this->namespace, '/ai/completions', array( |
| 104 | 'methods' => 'POST', |
| 105 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 106 | 'callback' => [ $this, 'rest_ai_completions' ], |
| 107 | ) ); |
| 108 | register_rest_route( $this->namespace, '/ai/images', array( |
| 109 | 'methods' => 'POST', |
| 110 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 111 | 'callback' => [ $this, 'rest_ai_images' ], |
| 112 | ) ); |
| 113 | register_rest_route( $this->namespace, '/ai/copilot', array( |
| 114 | 'methods' => 'POST', |
| 115 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 116 | 'callback' => [ $this, 'rest_ai_copilot' ], |
| 117 | ) ); |
| 118 | |
| 119 | register_rest_route( $this->namespace, '/ai/magic_wand', array( |
| 120 | 'methods' => 'POST', |
| 121 | 'callback' => [ $this, 'rest_ai_magic_wand' ], |
| 122 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 123 | ) ); |
| 124 | register_rest_route( $this->namespace, '/ai/moderate', array( |
| 125 | 'methods' => 'POST', |
| 126 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 127 | 'callback' => [ $this, 'rest_ai_moderate' ], |
| 128 | ) ); |
| 129 | register_rest_route( $this->namespace, '/ai/transcribe_audio', array( |
| 130 | 'methods' => 'POST', |
| 131 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 132 | 'callback' => [ $this, 'rest_ai_transcribe_audio' ], |
| 133 | ) ); |
| 134 | register_rest_route( $this->namespace, '/ai/transcribe_image', array( |
| 135 | 'methods' => 'POST', |
| 136 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 137 | 'callback' => [ $this, 'rest_ai_transcribe_image' ], |
| 138 | ) ); |
| 139 | register_rest_route( $this->namespace, '/ai/json', array( |
| 140 | 'methods' => 'POST', |
| 141 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 142 | 'callback' => [ $this, 'rest_ai_json' ], |
| 143 | ) ); |
| 144 | |
| 145 | // Helpers Endpoints |
| 146 | register_rest_route( $this->namespace, '/helpers/update_post_title', array( |
| 147 | 'methods' => 'POST', |
| 148 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 149 | 'callback' => [ $this, 'rest_helpers_update_title' ], |
| 150 | ) ); |
| 151 | register_rest_route( $this->namespace, '/helpers/update_post_excerpt', array( |
| 152 | 'methods' => 'POST', |
| 153 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 154 | 'callback' => [ $this, 'rest_helpers_update_excerpt' ], |
| 155 | ) ); |
| 156 | register_rest_route( $this->namespace, '/helpers/create_post', array( |
| 157 | 'methods' => 'POST', |
| 158 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 159 | 'callback' => [ $this, 'rest_helpers_create_post' ], |
| 160 | ) ); |
| 161 | register_rest_route( $this->namespace, '/helpers/create_image', array( |
| 162 | 'methods' => 'POST', |
| 163 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 164 | 'callback' => [ $this, 'rest_helpers_create_images' ], |
| 165 | ) ); |
| 166 | register_rest_route( $this->namespace, '/helpers/count_posts', array( |
| 167 | 'methods' => 'GET', |
| 168 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 169 | 'callback' => [ $this, 'rest_helpers_count_posts' ], |
| 170 | ) ); |
| 171 | register_rest_route( $this->namespace, '/helpers/posts_ids', array( |
| 172 | 'methods' => 'GET', |
| 173 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 174 | 'callback' => [ $this, 'rest_helpers_posts_ids' ], |
| 175 | ) ); |
| 176 | register_rest_route( $this->namespace, '/helpers/post_types', array( |
| 177 | 'methods' => 'GET', |
| 178 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 179 | 'callback' => [ $this, 'rest_helpers_post_types' ], |
| 180 | ) ); |
| 181 | register_rest_route( $this->namespace, '/helpers/post_content', array( |
| 182 | 'methods' => 'GET', |
| 183 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 184 | 'callback' => [ $this, 'rest_helpers_post_content' ], |
| 185 | ) ); |
| 186 | |
| 187 | // OpenAI Endpoints |
| 188 | register_rest_route( $this->namespace, '/openai/files/list', array( |
| 189 | 'methods' => 'GET', |
| 190 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 191 | 'callback' => [ $this, 'rest_openai_files_get' ], |
| 192 | ) ); |
| 193 | register_rest_route( $this->namespace, '/openai/files/upload', array( |
| 194 | 'methods' => 'POST', |
| 195 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 196 | 'callback' => [ $this, 'rest_openai_files_upload' ], |
| 197 | ) ); |
| 198 | register_rest_route( $this->namespace, '/openai/files/delete', array( |
| 199 | 'methods' => 'POST', |
| 200 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 201 | 'callback' => [ $this, 'rest_openai_files_delete' ], |
| 202 | ) ); |
| 203 | register_rest_route( $this->namespace, '/openai/files/download', array( |
| 204 | 'methods' => 'POST', |
| 205 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 206 | 'callback' => [ $this, 'rest_openai_files_download' ], |
| 207 | ) ); |
| 208 | register_rest_route( $this->namespace, '/openai/files/finetune', array( |
| 209 | 'methods' => 'POST', |
| 210 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 211 | 'callback' => [ $this, 'rest_openai_files_finetune' ], |
| 212 | ) ); |
| 213 | register_rest_route( $this->namespace, '/openai/finetunes/list_deleted', array( |
| 214 | 'methods' => 'GET', |
| 215 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 216 | 'callback' => [ $this, 'rest_openai_deleted_finetunes_get' ], |
| 217 | ) ); |
| 218 | |
| 219 | // register_rest_route( $this->namespace, '/openai/models', array( |
| 220 | // 'methods' => 'GET', |
| 221 | // 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 222 | // 'callback' => [ $this, 'rest_openai_models_get' ], |
| 223 | // ) ); |
| 224 | |
| 225 | register_rest_route( $this->namespace, '/openai/finetunes/list', array( |
| 226 | 'methods' => 'GET', |
| 227 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 228 | 'callback' => [ $this, 'rest_openai_finetunes_get' ], |
| 229 | ) ); |
| 230 | register_rest_route( $this->namespace, '/openai/finetunes/delete', array( |
| 231 | 'methods' => 'POST', |
| 232 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 233 | 'callback' => [ $this, 'rest_openai_finetunes_delete' ], |
| 234 | ) ); |
| 235 | register_rest_route( $this->namespace, '/openai/finetunes/cancel', array( |
| 236 | 'methods' => 'POST', |
| 237 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 238 | 'callback' => [ $this, 'rest_openai_finetunes_cancel' ], |
| 239 | ) ); |
| 240 | register_rest_route( $this->namespace, '/openai/incidents', array( |
| 241 | 'methods' => 'GET', |
| 242 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 243 | 'callback' => [ $this, 'rest_openai_incidents' ], |
| 244 | ) ); |
| 245 | |
| 246 | // Logging Endpoints |
| 247 | register_rest_route( $this->namespace, '/get_logs', array( |
| 248 | 'methods' => 'GET', |
| 249 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 250 | 'callback' => array( $this, 'rest_get_logs' ) |
| 251 | ) ); |
| 252 | register_rest_route( $this->namespace, '/clear_logs', array( |
| 253 | 'methods' => 'GET', |
| 254 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 255 | 'callback' => array( $this, 'rest_clear_logs' ) |
| 256 | ) ); |
| 257 | } |
| 258 | catch ( Exception $e ) { |
| 259 | var_dump( $e ); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | function rest_settings_list() { |
| 264 | return new WP_REST_Response( [ |
| 265 | 'success' => true, |
| 266 | 'options' => $this->core->get_all_options() |
| 267 | ], 200 ); |
| 268 | } |
| 269 | |
| 270 | function rest_settings_update( $request ) { |
| 271 | try { |
| 272 | $params = $request->get_json_params(); |
| 273 | $value = $params['options']; |
| 274 | $options = $this->core->update_options( $value ); |
| 275 | $success = !!$options; |
| 276 | $message = __( $success ? 'OK' : "Could not update options.", 'ai-engine' ); |
| 277 | return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 ); |
| 278 | } |
| 279 | catch ( Exception $e ) { |
| 280 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 281 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | function rest_settings_reset() { |
| 286 | try { |
| 287 | $options = $this->core->reset_options(); |
| 288 | $success = !!$options; |
| 289 | $message = __( $success ? 'OK' : "Could not reset options.", 'ai-engine' ); |
| 290 | return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 ); |
| 291 | } |
| 292 | catch ( Exception $e ) { |
| 293 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 294 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | function rest_ai_models( $request ) { |
| 299 | try { |
| 300 | $params = $request->get_json_params(); |
| 301 | $envId = $params['envId']; |
| 302 | $engine = Meow_MWAI_Engines_Factory::get( $this->core, $envId ); |
| 303 | $models = $engine->retrieve_models(); |
| 304 | return new WP_REST_Response([ 'success' => true, 'models' => $models ], 200 ); |
| 305 | } |
| 306 | catch ( Exception $e ) { |
| 307 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 308 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | function rest_ai_completions( $request ) { |
| 313 | try { |
| 314 | $params = $request->get_json_params(); |
| 315 | $message = $this->retrieve_message( $params ); |
| 316 | $query = new Meow_MWAI_Query_Text( $message ); |
| 317 | $query->inject_params( $params ); |
| 318 | |
| 319 | // Handle streaming |
| 320 | $stream = $params['stream'] ?? false; |
| 321 | $streamCallback = null; |
| 322 | if ( $stream ) { |
| 323 | $streamCallback = function( $reply ) { |
| 324 | //$raw = _wp_specialchars( $reply, ENT_NOQUOTES, 'UTF-8', true ); |
| 325 | $raw = $reply; |
| 326 | $this->core->stream_push( [ 'type' => 'live', 'data' => $raw ] ); |
| 327 | if ( ob_get_level() > 0 ) { |
| 328 | ob_flush(); |
| 329 | } |
| 330 | flush(); |
| 331 | }; |
| 332 | header( 'Cache-Control: no-cache' ); |
| 333 | header( 'Content-Type: text/event-stream' ); |
| 334 | header( 'X-Accel-Buffering: no' ); // This is useful to disable buffering in nginx through headers. |
| 335 | ob_implicit_flush( true ); |
| 336 | ob_end_flush(); |
| 337 | } |
| 338 | |
| 339 | // Process Reply |
| 340 | $reply = $this->core->run_query( $query, $streamCallback ); |
| 341 | $restRes = [ |
| 342 | 'success' => true, |
| 343 | 'data' => $reply->result, |
| 344 | 'usage' => $reply->usage |
| 345 | ]; |
| 346 | if ( $stream ) { |
| 347 | $this->core->stream_push( [ 'type' => 'end', 'data' => json_encode( $restRes ) ] ); |
| 348 | die(); |
| 349 | } |
| 350 | return new WP_REST_Response( $restRes, 200 ); |
| 351 | } |
| 352 | catch ( Exception $e ) { |
| 353 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 354 | if ( $stream ) { |
| 355 | $this->core->stream_push( [ 'type' => 'error', 'data' => $message ] ); |
| 356 | } |
| 357 | else { |
| 358 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | function rest_ai_images( $request ) { |
| 364 | try { |
| 365 | $params = $request->get_json_params(); |
| 366 | $message = $this->retrieve_message( $params ); |
| 367 | $query = new Meow_MWAI_Query_Image( $message ); |
| 368 | $query->inject_params( $params ); |
| 369 | $reply = $this->core->run_query( $query ); |
| 370 | return new WP_REST_Response([ 'success' => true, 'data' => $reply->results, 'usage' => $reply->usage ], 200 ); |
| 371 | } |
| 372 | catch ( Exception $e ) { |
| 373 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 374 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | function rest_ai_magic_wand( $request ) { |
| 379 | try { |
| 380 | $params = $request->get_json_params(); |
| 381 | $action = isset( $params['action'] ) ? $params['action'] : null; |
| 382 | $data = isset( $params['data'] ) ? $params['data'] : null; |
| 383 | if ( empty( $data ) || empty( $action ) ) { |
| 384 | return new WP_REST_Response([ 'success' => false, 'message' => "An action and some data are required." ], 500 ); |
| 385 | } |
| 386 | $data = apply_filters( 'mwai_magic_wand_' . $action, "", $data ); |
| 387 | return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 ); |
| 388 | } |
| 389 | catch ( Exception $e ) { |
| 390 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 391 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | function rest_ai_copilot( $request ) { |
| 396 | try { |
| 397 | $params = $request->get_json_params(); |
| 398 | $action = sanitize_text_field( $params['action'] ); |
| 399 | $message = $this->retrieve_message( $params, true ); |
| 400 | if ( empty( $action ) || empty( $message ) ) { |
| 401 | return new WP_REST_Response([ 'success' => false, 'message' => "Copilot needs an action and a prompt." ], 500 ); |
| 402 | } |
| 403 | $query = new Meow_MWAI_Query_Text( $message, 2048 ); |
| 404 | $query->set_scope( 'admin-tools' ); |
| 405 | $model = $this->core->get_option( 'ai_default_model' ); |
| 406 | $env = $this->core->get_option( 'ai_default_env' ); |
| 407 | if ( !empty( $env ) ) { |
| 408 | $query->set_env_id( $env ); |
| 409 | } |
| 410 | if ( !empty( $model ) ) { |
| 411 | $query->set_model( $model ); |
| 412 | } |
| 413 | $reply = $this->core->run_query( $query ); |
| 414 | return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 ); |
| 415 | } |
| 416 | catch ( Exception $e ) { |
| 417 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 418 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | function rest_helpers_update_title( $request ) { |
| 423 | try { |
| 424 | $params = $request->get_json_params(); |
| 425 | $title = sanitize_text_field( $params['title'] ); |
| 426 | $postId = intval( $params['postId'] ); |
| 427 | $post = get_post( $postId ); |
| 428 | if ( !$post ) { |
| 429 | throw new Exception( 'There is no post with this ID.' ); |
| 430 | } |
| 431 | $post->post_title = $title; |
| 432 | //$post->post_name = sanitize_title( $title ); |
| 433 | wp_update_post( $post ); |
| 434 | return new WP_REST_Response([ 'success' => true, 'message' => "Title updated." ], 200 ); |
| 435 | } |
| 436 | catch ( Exception $e ) { |
| 437 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 438 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | function rest_helpers_update_excerpt( $request ) { |
| 443 | try { |
| 444 | $params = $request->get_json_params(); |
| 445 | $excerpt = sanitize_text_field( $params['excerpt'] ); |
| 446 | $postId = intval( $params['postId'] ); |
| 447 | $post = get_post( $postId ); |
| 448 | if ( !$post ) { |
| 449 | throw new Exception( 'There is no post with this ID.' ); |
| 450 | } |
| 451 | $post->post_excerpt = $excerpt; |
| 452 | wp_update_post( $post ); |
| 453 | return new WP_REST_Response([ 'success' => true, 'message' => "Excerpt updated." ], 200 ); |
| 454 | } |
| 455 | catch ( Exception $e ) { |
| 456 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 457 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | function rest_helpers_create_post( $request ) { |
| 462 | try { |
| 463 | $params = $request->get_json_params(); |
| 464 | $title = sanitize_text_field( $params['title'] ); |
| 465 | $content = sanitize_textarea_field( $params['content'] ); |
| 466 | $excerpt = sanitize_text_field( $params['excerpt'] ); |
| 467 | $postType = sanitize_text_field( $params['postType'] ); |
| 468 | $post = new stdClass(); |
| 469 | $post->post_title = $title; |
| 470 | $post->post_excerpt = $excerpt; |
| 471 | $post->post_content = $content; |
| 472 | $post->post_status = 'draft'; |
| 473 | $post->post_type = isset( $postType ) ? $postType : 'post'; |
| 474 | // TODO: Let's try to avoid using Markdown to create the Post |
| 475 | // Instead, we should create Gutenberg Blocks, or simple HTML. |
| 476 | // Then, we can get rid of the library for Markdown. |
| 477 | $post->post_content = $this->core->markdown_to_html( $post->post_content ); |
| 478 | $postId = wp_insert_post( $post ); |
| 479 | return new WP_REST_Response([ 'success' => true, 'postId' => $postId ], 200 ); |
| 480 | } |
| 481 | catch ( Exception $e ) { |
| 482 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 483 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | function rest_helpers_create_images( $request ) { |
| 488 | try { |
| 489 | $params = $request->get_json_params(); |
| 490 | $title = sanitize_text_field( $params['title'] ); |
| 491 | $caption = sanitize_text_field( $params['caption'] ); |
| 492 | $alt = sanitize_text_field( $params['alt'] ); |
| 493 | $description = sanitize_text_field( $params['description'] ); |
| 494 | $url = $params['url']; |
| 495 | $filename = sanitize_text_field( $params['filename'] ); |
| 496 | $attachmentId = $this->core->add_image_from_url( $url, $filename, $title, $description, $caption, $alt ); |
| 497 | return new WP_REST_Response([ 'success' => true, 'attachmentId' => $attachmentId ], 200 ); |
| 498 | } |
| 499 | catch ( Exception $e ) { |
| 500 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 501 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | function rest_openai_files_get() { |
| 506 | try { |
| 507 | $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null; |
| 508 | $purposeFilter = isset( $_GET['purpose'] ) ? $_GET['purpose'] : null; |
| 509 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 510 | $files = $openai->list_files( $purposeFilter ); |
| 511 | return new WP_REST_Response([ 'success' => true, 'files' => $files ], 200 ); |
| 512 | } |
| 513 | catch ( Exception $e ) { |
| 514 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 515 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | function rest_openai_deleted_finetunes_get() { |
| 520 | try { |
| 521 | $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null; |
| 522 | $legacy = isset( $_GET['legacy'] ) ? $_GET['legacy'] === 'true' : false; |
| 523 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 524 | $finetunes = $openai->list_deleted_finetunes( $legacy ); |
| 525 | return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 ); |
| 526 | } |
| 527 | catch ( Exception $e ) { |
| 528 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 529 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | function rest_openai_finetunes_get() { |
| 534 | try { |
| 535 | $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null; |
| 536 | $legacy = isset( $_GET['legacy'] ) ? $_GET['legacy'] === 'true' : false; |
| 537 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 538 | $finetunes = $openai->list_finetunes( $legacy ); |
| 539 | return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 ); |
| 540 | } |
| 541 | catch ( Exception $e ) { |
| 542 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 543 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | function rest_openai_files_upload( $request ) { |
| 548 | try { |
| 549 | $params = $request->get_json_params(); |
| 550 | $envId = $params['envId'];; |
| 551 | $filename = sanitize_text_field( $params['filename'] ); |
| 552 | $data = $params['data']; |
| 553 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 554 | $file = $openai->upload_file( $filename, $data ); |
| 555 | return new WP_REST_Response([ 'success' => true, 'file' => $file ], 200 ); |
| 556 | } |
| 557 | catch ( Exception $e ) { |
| 558 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 559 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | function rest_openai_files_delete( $request ) { |
| 564 | try { |
| 565 | $params = $request->get_json_params(); |
| 566 | $envId = $params['envId'];; |
| 567 | $fileId = $params['fileId']; |
| 568 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 569 | $openai->delete_file( $fileId ); |
| 570 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 571 | } |
| 572 | catch ( Exception $e ) { |
| 573 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 574 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | function rest_openai_finetunes_cancel( $request ) { |
| 579 | try { |
| 580 | $params = $request->get_json_params(); |
| 581 | $envId = $params['envId'];; |
| 582 | $finetuneId = $params['finetuneId']; |
| 583 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 584 | $openai->cancel_finetune( $finetuneId ); |
| 585 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 586 | } |
| 587 | catch ( Exception $e ) { |
| 588 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 589 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | function rest_openai_finetunes_delete( $request ) { |
| 594 | try { |
| 595 | $params = $request->get_json_params(); |
| 596 | $envId = $params['envId'];; |
| 597 | $modelId = $params['modelId']; |
| 598 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 599 | $openai->delete_finetune( $modelId ); |
| 600 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 601 | } |
| 602 | catch ( Exception $e ) { |
| 603 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 604 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | function rest_openai_files_download( $request ) { |
| 609 | try { |
| 610 | $params = $request->get_json_params(); |
| 611 | $envId = $params['envId'];; |
| 612 | $fileId = $params['fileId']; |
| 613 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 614 | $filename = $openai->download_file( $fileId ); |
| 615 | $data = file_get_contents( $filename ); |
| 616 | return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 ); |
| 617 | } |
| 618 | catch ( Exception $e ) { |
| 619 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 620 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | function rest_openai_files_finetune( $request ) { |
| 625 | try { |
| 626 | $params = $request->get_json_params(); |
| 627 | $envId = $params['envId'];; |
| 628 | $fileId = $params['fileId']; |
| 629 | $model = $params['model']; |
| 630 | $suffix = $params['suffix']; |
| 631 | $hyperparams = [ |
| 632 | "nEpochs" => isset( $params['nEpochs'] ) ? $params['nEpochs'] : null, |
| 633 | "batchSize" => isset( $params['batchSize'] ) ? $params['batchSize'] : null, |
| 634 | ]; |
| 635 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 636 | $finetune = $openai->run_finetune( $fileId, $model, $suffix, $hyperparams ); |
| 637 | return new WP_REST_Response([ 'success' => true, 'finetune' => $finetune ], 200 ); |
| 638 | } |
| 639 | catch ( Exception $e ) { |
| 640 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 641 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | function rest_openai_incidents() { |
| 646 | try { |
| 647 | $transient = get_transient( 'mwai_openai_incidents' ); |
| 648 | if ( $transient ) { |
| 649 | return new WP_REST_Response([ 'success' => true, 'incidents' => $transient ], 200 ); |
| 650 | } |
| 651 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core ); |
| 652 | $incidents = $openai->get_incidents(); |
| 653 | set_transient( 'mwai_openai_incidents', $incidents, 60 * 10 ); |
| 654 | return new WP_REST_Response([ 'success' => true, 'incidents' => $incidents ], 200 ); |
| 655 | } |
| 656 | catch ( Exception $e ) { |
| 657 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 658 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | function rest_helpers_count_posts( $request ) { |
| 663 | try { |
| 664 | $params = $request->get_query_params(); |
| 665 | $postType = $params['postType']; |
| 666 | $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $params['postStatus'] ) : [ 'publish' ]; |
| 667 | $count = wp_count_posts( $postType ); |
| 668 | $count = array_sum( array_intersect_key( (array)$count, array_flip( $postStatus ) ) ); |
| 669 | return new WP_REST_Response([ 'success' => true, 'count' => $count ], 200 ); |
| 670 | } |
| 671 | catch ( Exception $e ) { |
| 672 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 673 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | function rest_helpers_posts_ids( $request ) { |
| 678 | try { |
| 679 | $params = $request->get_query_params(); |
| 680 | $postType = $params['postType']; |
| 681 | $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $params['postStatus'] ) : [ 'publish' ]; |
| 682 | $posts = get_posts( [ |
| 683 | 'posts_per_page' => -1, |
| 684 | 'post_type' => $postType, |
| 685 | 'post_status' => $postStatus, |
| 686 | 'fields' => 'ids' |
| 687 | ] ); |
| 688 | return new WP_REST_Response([ 'success' => true, 'postIds' => $posts ], 200 ); |
| 689 | } |
| 690 | catch ( Exception $e ) { |
| 691 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 692 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | function rest_helpers_post_content( $request ) { |
| 697 | try { |
| 698 | $params = $request->get_query_params(); |
| 699 | $offset = (int)$params['offset']; |
| 700 | $postType = $params['postType']; |
| 701 | $postStatus = isset( $params['postStatus'] ) ? explode( ',', $params['postStatus'] ) : [ 'publish' ]; |
| 702 | $postId = (int)$params['postId']; |
| 703 | |
| 704 | $post = null; |
| 705 | if ( !empty( $postId ) ) { |
| 706 | $post = get_post( $postId ); |
| 707 | if ( $post->post_status !== 'publish' && $post->post_status !== 'future' |
| 708 | && $post->post_status !== 'draft' && $post->post_status !== 'private' ) { |
| 709 | $post = null; |
| 710 | } |
| 711 | } |
| 712 | else { |
| 713 | $posts = get_posts( [ |
| 714 | 'posts_per_page' => 1, |
| 715 | 'post_type' => $postType, |
| 716 | 'offset' => $offset, |
| 717 | 'post_status' => $postStatus, |
| 718 | ] ); |
| 719 | $post = count( $posts ) === 0 ? null : $posts[0]; |
| 720 | } |
| 721 | if ( !$post ) { |
| 722 | return new WP_REST_Response([ 'success' => false, 'message' => 'Post not found' ], 404 ); |
| 723 | } |
| 724 | $cleanPost = $this->core->get_post( $post ); |
| 725 | return new WP_REST_Response([ 'success' => true, 'content' => $cleanPost['content'], |
| 726 | 'checksum' => $cleanPost['checksum'], 'language' => $cleanPost['language'], 'excerpt' => $cleanPost['excerpt'], |
| 727 | 'postId' => $cleanPost['postId'], 'title' => $cleanPost['title'], 'url' => $cleanPost['url'] ], 200 ); |
| 728 | } |
| 729 | catch ( Exception $e ) { |
| 730 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 731 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | function rest_system_templates_get( $request ) { |
| 736 | try { |
| 737 | $params = $request->get_query_params(); |
| 738 | $category = $params['category']; |
| 739 | $templates = []; |
| 740 | $templates_option = get_option( 'mwai_templates', [] ); |
| 741 | if ( !is_array( $templates_option ) ) { |
| 742 | update_option( 'mwai_templates', [] ); |
| 743 | } |
| 744 | $categories = array_column( $templates_option, 'category' ); |
| 745 | $index = array_search( $category, $categories ); |
| 746 | $templates = []; |
| 747 | if ( $index !== false ) { |
| 748 | $templates = $templates_option[$index]['templates']; |
| 749 | } |
| 750 | return new WP_REST_Response([ 'success' => true, 'templates' => $templates ], 200 ); |
| 751 | } |
| 752 | catch ( Exception $e ) { |
| 753 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 754 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | function rest_system_templates_save( $request ) { |
| 759 | try { |
| 760 | $params = $request->get_json_params(); |
| 761 | $category = $params['category']; |
| 762 | $templates = $params['templates']; |
| 763 | $templates_option = get_option( 'mwai_templates', [] ); |
| 764 | $categories = array_column( $templates_option, 'category' ); |
| 765 | $index = array_search( $category, $categories ); |
| 766 | if ( $index !== false && $index >= 0 ) { |
| 767 | $templates_option[$index]['templates'] = $templates; |
| 768 | } |
| 769 | else { |
| 770 | $group = [ 'category' => $category, 'templates' => $templates ]; |
| 771 | $templates_option[] = $group; |
| 772 | } |
| 773 | |
| 774 | update_option( 'mwai_templates', $templates_option ); |
| 775 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 776 | } |
| 777 | catch ( Exception $e ) { |
| 778 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 779 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | function rest_system_logs_list( $request ) { |
| 784 | try { |
| 785 | $params = $request->get_json_params(); |
| 786 | $offset = $params['offset']; |
| 787 | $limit = $params['limit']; |
| 788 | $filters = $params['filters']; |
| 789 | $sort = $params['sort']; |
| 790 | $logs = apply_filters( 'mwai_stats_logs', [], $offset, $limit, $filters, $sort ); |
| 791 | return new WP_REST_Response([ 'success' => true, 'total' => $logs['total'], 'logs' => $logs['rows'] ], 200 ); |
| 792 | } |
| 793 | catch ( Exception $e ) { |
| 794 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 795 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | function rest_system_logs_delete( $request ) { |
| 800 | try { |
| 801 | $params = $request->get_json_params(); |
| 802 | $logIds = $params['logIds']; |
| 803 | $success = apply_filters( 'mwai_stats_logs_delete', true, $logIds ); |
| 804 | return new WP_REST_Response([ 'success' => $success ], 200 ); |
| 805 | } |
| 806 | catch ( Exception $e ) { |
| 807 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 808 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | function rest_system_logs_meta_get( $request ) { |
| 813 | try { |
| 814 | $params = $request->get_json_params(); |
| 815 | $logId = $params['logId']; |
| 816 | $metaKeys = $params['metaKeys']; |
| 817 | $data = apply_filters( 'mwai_stats_logs_meta', [], $logId, $metaKeys ); |
| 818 | return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 ); |
| 819 | } |
| 820 | catch ( Exception $e ) { |
| 821 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 822 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | function rest_ai_moderate( $request ) { |
| 827 | try { |
| 828 | $params = $request->get_json_params(); |
| 829 | $envId = $params['envId']; |
| 830 | $text = $params['text']; |
| 831 | if ( !$text ) { |
| 832 | return new WP_REST_Response([ 'success' => false, 'message' => 'Text not found.' ], 404 ); |
| 833 | } |
| 834 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 835 | $results = $openai->moderate( $text ); |
| 836 | return new WP_REST_Response([ 'success' => true, 'results' => $results ], 200 ); |
| 837 | } |
| 838 | catch ( Exception $e ) { |
| 839 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 840 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | function rest_ai_transcribe_audio( $request ) { |
| 845 | try { |
| 846 | $params = $request->get_json_params(); |
| 847 | $query = new Meow_MWAI_Query_Transcribe(); |
| 848 | $query->inject_params( $params ); |
| 849 | $query->set_scope('admin-tools'); |
| 850 | $reply = $this->core->run_query( $query ); |
| 851 | return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 ); |
| 852 | } |
| 853 | catch ( Exception $e ) { |
| 854 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 855 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | function rest_ai_transcribe_image( $request ) { |
| 860 | try { |
| 861 | global $mwai; |
| 862 | $params = $request->get_json_params(); |
| 863 | $message = $this->retrieve_message( $params ); |
| 864 | $url = !empty( $params['url'] ) ? $params['url'] : null; |
| 865 | $path = !empty( $params['path'] ) ? $params['path'] : null; |
| 866 | $result = $mwai->simpleVisionQuery( $message, $url, $path ); |
| 867 | return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 ); |
| 868 | } |
| 869 | catch ( Exception $e ) { |
| 870 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 871 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | function rest_ai_json( $request ) { |
| 876 | try { |
| 877 | global $mwai; |
| 878 | $params = $request->get_json_params(); |
| 879 | $message = $this->retrieve_message( $params ); |
| 880 | $result = $mwai->simpleJsonQuery( $message ); |
| 881 | return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 ); |
| 882 | } |
| 883 | catch ( Exception $e ) { |
| 884 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 885 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | function rest_helpers_post_types() { |
| 890 | try { |
| 891 | $postTypes = $this->core->get_post_types(); |
| 892 | return new WP_REST_Response([ 'success' => true, 'postTypes' => $postTypes ], 200 ); |
| 893 | } |
| 894 | catch ( Exception $e ) { |
| 895 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 896 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | function rest_settings_themes( $request ) { |
| 901 | try { |
| 902 | $method = $request->get_method(); |
| 903 | if ( $method === 'GET' ) { |
| 904 | $themes = $this->core->get_themes(); |
| 905 | return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 ); |
| 906 | } |
| 907 | else if ( $method === 'POST' ) { |
| 908 | $params = $request->get_json_params(); |
| 909 | $themes = $params['themes']; |
| 910 | $themes = $this->core->update_themes( $themes ); |
| 911 | return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 ); |
| 912 | } |
| 913 | } |
| 914 | catch ( Exception $e ) { |
| 915 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 916 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | function rest_settings_chatbots( $request ) { |
| 921 | try { |
| 922 | $method = $request->get_method(); |
| 923 | if ( $method === 'GET' ) { |
| 924 | $chatbots = $this->core->get_chatbots(); |
| 925 | return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 ); |
| 926 | } |
| 927 | else if ( $method === 'POST' ) { |
| 928 | $params = $request->get_json_params(); |
| 929 | $chatbots = $params['chatbots']; |
| 930 | $chatbots = $this->core->update_chatbots( $chatbots ); |
| 931 | return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 ); |
| 932 | } |
| 933 | return new WP_REST_Response([ 'success' => false, 'message' => 'Method not allowed' ], 405 ); |
| 934 | } |
| 935 | catch ( Exception $e ) { |
| 936 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 937 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | #region Logs |
| 942 | |
| 943 | function rest_get_logs() { |
| 944 | $logs = $this->core->get_logs(); |
| 945 | return new WP_REST_Response( [ 'success' => true, 'data' => $logs ], 200 ); |
| 946 | } |
| 947 | |
| 948 | function rest_clear_logs() { |
| 949 | $this->core->clear_logs(); |
| 950 | return new WP_REST_Response( [ 'success' => true ], 200 ); |
| 951 | } |
| 952 | |
| 953 | |
| 954 | #endregion |
| 955 | } |
| 956 |