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.1.6 1.6.86 0.1.7 1.6.87 0.1.8 1.6.88 0.1.9 1.6.89 0.2.0 1.6.90
ai-engine / classes / rest.php
ai-engine / classes Last commit date
engines 3 years ago modules 3 years ago queries 3 years ago tests 3 years ago admin.php 3 years ago api.php 3 years ago core.php 3 years ago init.php 3 years ago reply.php 3 years ago rest.php 3 years ago
rest.php
880 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 function rest_init() {
14 try {
15 // Settings Endpoints
16 register_rest_route( $this->namespace, '/settings/update', array(
17 'methods' => 'POST',
18 'permission_callback' => array( $this->core, 'can_access_settings' ),
19 'callback' => array( $this, 'rest_settings_update' )
20 ) );
21 register_rest_route( $this->namespace, '/settings/list', array(
22 'methods' => 'GET',
23 'permission_callback' => array( $this->core, 'can_access_settings' ),
24 'callback' => array( $this, 'rest_settings_list' ),
25 ) );
26 register_rest_route( $this->namespace, '/settings/reset', array(
27 'methods' => 'POST',
28 'permission_callback' => array( $this->core, 'can_access_settings' ),
29 'callback' => array( $this, 'rest_settings_reset' ),
30 ) );
31 register_rest_route( $this->namespace, '/settings/chatbots', array(
32 'methods' => ['GET', 'POST'],
33 'permission_callback' => array( $this->core, 'can_access_settings' ),
34 'callback' => array( $this, 'rest_settings_chatbots' ),
35 ) );
36 register_rest_route( $this->namespace, '/settings/themes', array(
37 'methods' => ['GET', 'POST'],
38 'permission_callback' => array( $this->core, 'can_access_settings' ),
39 'callback' => array( $this, 'rest_settings_themes' ),
40 ) );
41
42 // System Endpoints
43 register_rest_route( $this->namespace, '/system/logs/list', array(
44 'methods' => 'POST',
45 'permission_callback' => array( $this->core, 'can_access_settings' ),
46 'callback' => array( $this, 'rest_system_logs_list' ),
47 ) );
48 register_rest_route( $this->namespace, '/system/logs/delete', array(
49 'methods' => 'POST',
50 'permission_callback' => array( $this->core, 'can_access_settings' ),
51 'callback' => array( $this, 'rest_system_logs_delete' ),
52 ) );
53 register_rest_route( $this->namespace, '/system/logs/meta', array(
54 'methods' => 'POST',
55 'permission_callback' => array( $this->core, 'can_access_settings' ),
56 'callback' => array( $this, 'rest_system_logs_meta_get' ),
57 ) );
58 register_rest_route( $this->namespace, '/system/templates', array(
59 'methods' => 'POST',
60 'permission_callback' => array( $this->core, 'can_access_features' ),
61 'callback' => array( $this, 'rest_system_templates_save' ),
62 ) );
63 register_rest_route( $this->namespace, '/system/templates', array(
64 'methods' => 'GET',
65 'permission_callback' => array( $this->core, 'can_access_features' ),
66 'callback' => array( $this, 'rest_system_templates_get' ),
67 ) );
68
69 // AI Endpoints
70 register_rest_route( $this->namespace, '/ai/completions', array(
71 'methods' => 'POST',
72 'permission_callback' => array( $this->core, 'can_access_features' ),
73 'callback' => array( $this, 'rest_ai_completions' ),
74 ) );
75 register_rest_route( $this->namespace, '/ai/images', array(
76 'methods' => 'POST',
77 'permission_callback' => array( $this->core, 'can_access_features' ),
78 'callback' => array( $this, 'rest_ai_images' ),
79 ) );
80 register_rest_route( $this->namespace, '/ai/copilot', array(
81 'methods' => 'POST',
82 'permission_callback' => array( $this->core, 'can_access_features' ),
83 'callback' => array( $this, 'rest_ai_copilot' ),
84 ) );
85
86 register_rest_route( $this->namespace, '/ai/magic_wand', array(
87 'methods' => 'POST',
88 'callback' => array( $this, 'rest_ai_magic_wand' ),
89 'permission_callback' => array( $this->core, 'can_access_features' ),
90 ) );
91 register_rest_route( $this->namespace, '/ai/moderate', array(
92 'methods' => 'POST',
93 'permission_callback' => array( $this->core, 'can_access_settings' ),
94 'callback' => array( $this, 'rest_ai_moderate' ),
95 ) );
96 register_rest_route( $this->namespace, '/ai/transcribe', array(
97 'methods' => 'POST',
98 'permission_callback' => array( $this->core, 'can_access_settings' ),
99 'callback' => array( $this, 'rest_ai_transcribe' ),
100 ) );
101
102 // Helpers Endpoints
103 register_rest_route( $this->namespace, '/helpers/update_post_title', array(
104 'methods' => 'POST',
105 'permission_callback' => array( $this->core, 'can_access_features' ),
106 'callback' => array( $this, 'rest_helpers_update_title' ),
107 ) );
108 register_rest_route( $this->namespace, '/helpers/update_post_excerpt', array(
109 'methods' => 'POST',
110 'permission_callback' => array( $this->core, 'can_access_features' ),
111 'callback' => array( $this, 'rest_helpers_update_excerpt' ),
112 ) );
113 register_rest_route( $this->namespace, '/helpers/create_post', array(
114 'methods' => 'POST',
115 'permission_callback' => array( $this->core, 'can_access_features' ),
116 'callback' => array( $this, 'rest_helpers_create_post' ),
117 ) );
118 register_rest_route( $this->namespace, '/helpers/create_image', array(
119 'methods' => 'POST',
120 'permission_callback' => array( $this->core, 'can_access_features' ),
121 'callback' => array( $this, 'rest_helpers_create_images' ),
122 ) );
123 register_rest_route( $this->namespace, '/helpers/count_posts', array(
124 'methods' => 'GET',
125 'permission_callback' => array( $this->core, 'can_access_features' ),
126 'callback' => array( $this, 'rest_helpers_count_posts' ),
127 ) );
128 register_rest_route( $this->namespace, '/helpers/post_types', array(
129 'methods' => 'GET',
130 'permission_callback' => array( $this->core, 'can_access_features' ),
131 'callback' => array( $this, 'rest_helpers_post_types' ),
132 ) );
133 register_rest_route( $this->namespace, '/helpers/post_content', array(
134 'methods' => 'GET',
135 'permission_callback' => array( $this->core, 'can_access_features' ),
136 'callback' => array( $this, 'rest_helpers_post_content' ),
137 ) );
138
139 // OpenAI Endpoints
140 register_rest_route( $this->namespace, '/openai/files/list', array(
141 'methods' => 'GET',
142 'permission_callback' => array( $this->core, 'can_access_settings' ),
143 'callback' => array( $this, 'rest_openai_files_get' ),
144 ) );
145 register_rest_route( $this->namespace, '/openai/files/upload', array(
146 'methods' => 'POST',
147 'permission_callback' => array( $this->core, 'can_access_settings' ),
148 'callback' => array( $this, 'rest_openai_files_upload' ),
149 ) );
150 register_rest_route( $this->namespace, '/openai/files/delete', array(
151 'methods' => 'POST',
152 'permission_callback' => array( $this->core, 'can_access_settings' ),
153 'callback' => array( $this, 'rest_openai_files_delete' ),
154 ) );
155 register_rest_route( $this->namespace, '/openai/files/download', array(
156 'methods' => 'POST',
157 'permission_callback' => array( $this->core, 'can_access_settings' ),
158 'callback' => array( $this, 'rest_openai_files_download' ),
159 ) );
160 register_rest_route( $this->namespace, '/openai/files/finetune', array(
161 'methods' => 'POST',
162 'permission_callback' => array( $this->core, 'can_access_settings' ),
163 'callback' => array( $this, 'rest_openai_files_finetune' ),
164 ) );
165 register_rest_route( $this->namespace, '/openai/finetunes/list_deleted', array(
166 'methods' => 'GET',
167 'permission_callback' => array( $this->core, 'can_access_settings' ),
168 'callback' => array( $this, 'rest_openai_deleted_finetunes_get' ),
169 ) );
170 register_rest_route( $this->namespace, '/openai/finetunes/list', array(
171 'methods' => 'GET',
172 'permission_callback' => array( $this->core, 'can_access_settings' ),
173 'callback' => array( $this, 'rest_openai_finetunes_get' ),
174 ) );
175 register_rest_route( $this->namespace, '/openai/finetunes/delete', array(
176 'methods' => 'POST',
177 'permission_callback' => array( $this->core, 'can_access_settings' ),
178 'callback' => array( $this, 'rest_openai_finetunes_delete' ),
179 ) );
180 register_rest_route( $this->namespace, '/openai/finetunes/cancel', array(
181 'methods' => 'POST',
182 'permission_callback' => array( $this->core, 'can_access_settings' ),
183 'callback' => array( $this, 'rest_openai_finetunes_cancel' ),
184 ) );
185 register_rest_route( $this->namespace, '/openai/incidents', array(
186 'methods' => 'GET',
187 'permission_callback' => array( $this->core, 'can_access_settings' ),
188 'callback' => array( $this, 'rest_openai_incidents' ),
189 ) );
190 }
191 catch ( Exception $e ) {
192 var_dump( $e );
193 }
194 }
195
196 function rest_settings_list() {
197 return new WP_REST_Response( [
198 'success' => true,
199 'options' => $this->core->get_all_options()
200 ], 200 );
201 }
202
203 function rest_settings_update( $request ) {
204 try {
205 $params = $request->get_json_params();
206 $value = $params['options'];
207 $options = $this->core->update_options( $value );
208 $success = !!$options;
209 $message = __( $success ? 'OK' : "Could not update options.", 'ai-engine' );
210 return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 );
211 }
212 catch ( Exception $e ) {
213 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
214 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
215 }
216 }
217
218 function rest_settings_reset() {
219 try {
220 $options = $this->core->reset_options();
221 $success = !!$options;
222 $message = __( $success ? 'OK' : "Could not reset options.", 'ai-engine' );
223 return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 );
224 }
225 catch ( Exception $e ) {
226 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
227 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
228 }
229 }
230
231 function createValidationResult( $result = true, $message = null) {
232 $message = $message ? $message : __( 'OK', 'ai-engine' );
233 return [ 'result' => $result, 'message' => $message ];
234 }
235
236 function validate_updated_option( $option_name ) {
237 $option_checkbox = get_option( 'mwai_option_checkbox', false );
238 $option_text = get_option( 'mwai_option_text', 'Default' );
239 if ( $option_checkbox === '' )
240 update_option( 'mwai_option_checkbox', false );
241 if ( $option_text === '' )
242 update_option( 'mwai_option_text', 'Default' );
243 return $this->createValidationResult();
244 }
245
246 function rest_ai_completions( $request ) {
247 try {
248 $params = $request->get_json_params();
249 $prompt = $params['prompt'];
250 $query = new Meow_MWAI_Query_Text( $prompt );
251 $query->injectParams( $params );
252 $reply = $this->core->ai->run( $query );
253 return new WP_REST_Response([ 'success' => true, 'data' => $reply->result, 'usage' => $reply->usage ], 200 );
254 }
255 catch ( Exception $e ) {
256 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
257 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
258 }
259 }
260
261 function rest_ai_images( $request ) {
262 try {
263 $params = $request->get_json_params();
264 $prompt = $params['prompt'];
265 $query = new Meow_MWAI_Query_Image( $prompt );
266 $query->injectParams( $params );
267 $reply = $this->core->ai->run( $query );
268 return new WP_REST_Response([ 'success' => true, 'data' => $reply->results, 'usage' => $reply->usage ], 200 );
269 }
270 catch ( Exception $e ) {
271 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
272 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
273 }
274 }
275
276 function rest_ai_magic_wand( $request ) {
277 try {
278 $params = $request->get_json_params();
279 $action = isset( $params['action'] ) ? $params['action'] : null;
280 $data = isset( $params['data'] ) ? $params['data'] : null;
281 if ( empty( $data ) || empty( $action ) ) {
282 return new WP_REST_Response([ 'success' => false, 'message' => "An action and some data are required." ], 500 );
283 }
284 $postId = isset( $data['postId'] ) ? $data['postId'] : null;
285 $text = isset( $data['text'] ) ? $data['text'] : null;
286 $selectedText = isset( $data['selectedText'] ) ? $data['selectedText'] : null;
287
288 // NOTE: As soon as we have a wide range of usages and possibilities,
289 // let's refactor this into a nice and extensible UI/API.
290 $query = new Meow_MWAI_Query_Text( "", 1024 );
291 $query->setEnv( 'admin-tools' );
292 $model = $this->core->get_option( 'assistants_model' );
293 if ( !empty( $model ) ) {
294 $query->setModel( $model );
295 }
296 $mode = 'replace';
297
298 $language = "";
299 $keepLanguage = "";
300 if ( !empty( $postId ) ) {
301 $language = $this->core->get_post_language( $postId );
302 $keepLanguage = " Ensure the reply is in the same language as the original text ({$language}).";
303 }
304
305 if ( $action === 'correctText' ) {
306 $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.{$keepLanguage}\n\n" . $text );
307 }
308 else if ( $action === 'enhanceText' ) {
309 $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.{$keepLanguage}\n\n" . $text );
310 }
311 else if ( $action === 'longerText' ) {
312 $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.{$keepLanguage}\n\n" . $text );
313 }
314 else if ( $action === 'shorterText' ) {
315 $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.{$keepLanguage}\n\n" . $text );
316 }
317 else if ( $action === 'suggestSynonyms' ) {
318 $mode = 'suggest';
319 $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.{$keepLanguage}\n\n" . $selectedText );
320 $query->setTemperature( 1 );
321 $query->setMaxResults( 5 );
322 }
323 else if ( $action === 'translateText' ) {
324 $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 );
325 }
326 else if ( $action === 'suggestExcerpts' ) {
327 $text = $this->core->getCleanPostContent( $postId );
328 $query->setPrompt( "Craft a clear, SEO-optimized introduction for the following text, using 120 to 170 characters. Ensure the introduction is concise and relevant, without including any URLs.{$keepLanguage}\n\n" . $text );
329 $query->setMaxResults( 5 );
330 }
331 else if ( $action === 'suggestTitles' ) {
332 $text = $this->core->getCleanPostContent( $postId );
333 $query->setPrompt( "Generate a concise, SEO-optimized title for the following text, without using quotes or any other formatting. Focus on clarity and relevance to the content.{$keepLanguage}\n\n" . $text );
334 $query->setMaxResults( 5 );
335 }
336 $reply = $this->core->ai->run( $query );
337 return new WP_REST_Response([ 'success' => true, 'data' => [
338 'mode' => $mode,
339 'result' => $reply->result,
340 'results' => $reply->results
341 ] ], 200 );
342 }
343 catch ( Exception $e ) {
344 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
345 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
346 }
347 }
348
349 function rest_ai_copilot( $request ) {
350 try {
351 $params = $request->get_json_params();
352 $action = sanitize_text_field( $params['action'] );
353 $prompt = sanitize_text_field( $params['prompt'] );
354 if ( empty( $action ) || empty( $prompt ) ) {
355 return new WP_REST_Response([ 'success' => false, 'message' => "Copilot needs an action and a prompt." ], 500 );
356 }
357 $query = new Meow_MWAI_Query_Text( $prompt, 2048 );
358 $query->setEnv( 'admin-tools' );
359 $model = $this->core->get_option( 'assistants_model' );
360 if ( !empty( $model ) ) {
361 $query->setModel( $model );
362 }
363 $reply = $this->core->ai->run( $query );
364 return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 );
365 }
366 catch ( Exception $e ) {
367 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
368 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
369 }
370 }
371
372 function rest_helpers_update_title( $request ) {
373 try {
374 $params = $request->get_json_params();
375 $title = sanitize_text_field( $params['title'] );
376 $postId = intval( $params['postId'] );
377 $post = get_post( $postId );
378 if ( !$post ) {
379 throw new Exception( 'There is no post with this ID.' );
380 }
381 $post->post_title = $title;
382 //$post->post_name = sanitize_title( $title );
383 wp_update_post( $post );
384 return new WP_REST_Response([ 'success' => true, 'message' => "Title updated." ], 200 );
385 }
386 catch ( Exception $e ) {
387 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
388 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
389 }
390 }
391
392 function rest_helpers_update_excerpt( $request ) {
393 try {
394 $params = $request->get_json_params();
395 $excerpt = sanitize_text_field( $params['excerpt'] );
396 $postId = intval( $params['postId'] );
397 $post = get_post( $postId );
398 if ( !$post ) {
399 throw new Exception( 'There is no post with this ID.' );
400 }
401 $post->post_excerpt = $excerpt;
402 wp_update_post( $post );
403 return new WP_REST_Response([ 'success' => true, 'message' => "Excerpt updated." ], 200 );
404 }
405 catch ( Exception $e ) {
406 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
407 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
408 }
409 }
410
411 function rest_helpers_create_post( $request ) {
412 try {
413 $params = $request->get_json_params();
414 $title = sanitize_text_field( $params['title'] );
415 $content = sanitize_textarea_field( $params['content'] );
416 $excerpt = sanitize_text_field( $params['excerpt'] );
417 $postType = sanitize_text_field( $params['postType'] );
418 $post = new stdClass();
419 $post->post_title = $title;
420 $post->post_excerpt = $excerpt;
421 $post->post_content = $content;
422 $post->post_status = 'draft';
423 $post->post_type = isset( $postType ) ? $postType : 'post';
424 $post->post_content = $this->core->markdown_to_html( $post->post_content );
425 $postId = wp_insert_post( $post );
426 return new WP_REST_Response([ 'success' => true, 'postId' => $postId ], 200 );
427 }
428 catch ( Exception $e ) {
429 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
430 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
431 }
432 }
433
434 function image_download( $url ) {
435 $args = array( 'timeout' => 60, );
436 $response = wp_remote_get( $url, $args );
437 if ( is_wp_error( $response ) ) {
438 throw new Exception( $response->get_error_message() );
439 }
440 $output = wp_remote_retrieve_body( $response );
441 if ( is_wp_error( $output ) ) {
442 throw new Exception( $output->get_error_message() );
443 }
444 return $output;
445 }
446
447 function rest_helpers_create_images( $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 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
499 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
500 }
501 }
502
503 function rest_openai_files_get() {
504 try {
505 //$params = $request->get_json_params();
506 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
507 $files = $openai->listFiles();
508 return new WP_REST_Response([ 'success' => true, 'files' => $files ], 200 );
509 }
510 catch ( Exception $e ) {
511 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
512 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
513 }
514 }
515
516 function rest_openai_deleted_finetunes_get() {
517 try {
518 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
519 $finetunes = $openai->listDeletedFineTunes();
520 return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
521 }
522 catch ( Exception $e ) {
523 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
524 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
525 }
526 }
527
528 function rest_openai_finetunes_get() {
529 try {
530 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
531 $finetunes = $openai->listFineTunes();
532 return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
533 }
534 catch ( Exception $e ) {
535 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
536 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
537 }
538 }
539
540 function rest_openai_files_upload( $request ) {
541 try {
542 $params = $request->get_json_params();
543 $filename = sanitize_text_field( $params['filename'] );
544 $data = $params['data'];
545 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
546 $file = $openai->uploadFile( $filename, $data );
547 return new WP_REST_Response([ 'success' => true, 'file' => $file ], 200 );
548 }
549 catch ( Exception $e ) {
550 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
551 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
552 }
553 }
554
555 function rest_openai_files_delete( $request ) {
556 try {
557 $params = $request->get_json_params();
558 $fileId = $params['fileId'];
559 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
560 $openai->deleteFile( $fileId );
561 return new WP_REST_Response([ 'success' => true ], 200 );
562 }
563 catch ( Exception $e ) {
564 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
565 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
566 }
567 }
568
569 function rest_openai_finetunes_cancel( $request ) {
570 try {
571 $params = $request->get_json_params();
572 $finetuneId = $params['finetuneId'];
573 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
574 $openai->cancelFineTune( $finetuneId );
575 return new WP_REST_Response([ 'success' => true ], 200 );
576 }
577 catch ( Exception $e ) {
578 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
579 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
580 }
581 }
582
583 function rest_openai_finetunes_delete( $request ) {
584 try {
585 $params = $request->get_json_params();
586 $modelId = $params['modelId'];
587 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
588 $openai->deleteFineTune( $modelId );
589 return new WP_REST_Response([ 'success' => true ], 200 );
590 }
591 catch ( Exception $e ) {
592 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
593 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
594 }
595 }
596
597 function rest_openai_files_download( $request ) {
598 try {
599 $params = $request->get_json_params();
600 $fileId = $params['fileId'];
601 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
602 $data = $openai->downloadFile( $fileId );
603 return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 );
604 }
605 catch ( Exception $e ) {
606 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
607 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
608 }
609 }
610
611 function rest_openai_files_finetune( $request ) {
612 try {
613 $params = $request->get_json_params();
614 $fileId = $params['fileId'];
615 $model = $params['model'];
616 $suffix = $params['suffix'];
617 $hyperparams = [
618 "nEpochs" => $params['nEpochs'],
619 "batchSize" => $params['batchSize']
620 ];
621 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
622 $finetune = $openai->fineTuneFile( $fileId, $model, $suffix, $hyperparams );
623 return new WP_REST_Response([ 'success' => true, 'finetune' => $finetune ], 200 );
624 }
625 catch ( Exception $e ) {
626 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
627 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
628 }
629 }
630
631 function rest_openai_incidents() {
632 try {
633 $transient = get_transient( 'mwai_openai_incidents' );
634 if ( $transient ) {
635 return new WP_REST_Response([ 'success' => true, 'incidents' => $transient ], 200 );
636 }
637 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
638 $incidents = $openai->getIncidents();
639 set_transient( 'mwai_openai_incidents', $incidents, 60 * 10 );
640 return new WP_REST_Response([ 'success' => true, 'incidents' => $incidents ], 200 );
641 }
642 catch ( Exception $e ) {
643 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
644 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
645 }
646 }
647
648 function rest_helpers_count_posts( $request ) {
649 try {
650 $params = $request->get_query_params();
651 $postType = $params['postType'];
652 $postStatus = $params['postStatus'];
653 $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $postStatus ) : [ 'publish' ];
654 $count = wp_count_posts( $postType );
655 $count = array_sum( array_intersect_key( (array)$count, array_flip( $postStatus ) ) );
656 return new WP_REST_Response([ 'success' => true, 'count' => $count ], 200 );
657 }
658 catch ( Exception $e ) {
659 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
660 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
661 }
662 }
663
664 function rest_helpers_post_content( $request ) {
665 try {
666 $params = $request->get_query_params();
667 $offset = (int)$params['offset'];
668 $postType = $params['postType'];
669 $postStatus = $params['postStatus'];
670 $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $postStatus ) : [ 'publish' ];
671 $postId = (int)$params['postId'];
672
673 $post = null;
674 if ( !empty( $postId ) ) {
675 $post = get_post( $postId );
676 if ( $post->post_status !== 'publish' && $post->post_status !== 'future'
677 && $post->post_status !== 'draft' && $post->post_status !== 'private' ) {
678 $post = null;
679 }
680 }
681 else {
682 $posts = get_posts( [
683 'posts_per_page' => 1,
684 'post_type' => $postType,
685 'offset' => $offset,
686 'post_status' => $postStatus,
687 ] );
688 $post = count( $posts ) === 0 ? null : $posts[0];
689 }
690 if ( !$post ) {
691 return new WP_REST_Response([ 'success' => false, 'message' => 'Post not found' ], 404 );
692 }
693 $cleanPost = $this->core->getCleanPost( $post );
694 return new WP_REST_Response([ 'success' => true, 'content' => $cleanPost['content'],
695 'checksum' => $cleanPost['checksum'], 'language' => $cleanPost['language'], 'excerpt' => $cleanPost['excerpt'],
696 'postId' => $cleanPost['postId'], 'title' => $cleanPost['title'], 'url' => $cleanPost['url'] ], 200 );
697 }
698 catch ( Exception $e ) {
699 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
700 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
701 }
702 }
703
704 function rest_system_templates_get( $request ) {
705 try {
706 $params = $request->get_query_params();
707 $category = $params['category'];
708 $templates = [];
709 $templates_option = get_option( 'mwai_templates', [] );
710 if ( !is_array( $templates_option ) ) {
711 update_option( 'mwai_templates', [] );
712 }
713 $categories = array_column( $templates_option, 'category' );
714 $index = array_search( $category, $categories );
715 $templates = [];
716 if ( $index !== false ) {
717 $templates = $templates_option[$index]['templates'];
718 }
719 return new WP_REST_Response([ 'success' => true, 'templates' => $templates ], 200 );
720 }
721 catch ( Exception $e ) {
722 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
723 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
724 }
725 }
726
727 function rest_system_templates_save( $request ) {
728 try {
729 $params = $request->get_json_params();
730 $category = $params['category'];
731 $templates = $params['templates'];
732 $templates_option = get_option( 'mwai_templates', [] );
733 $categories = array_column( $templates_option, 'category' );
734 $index = array_search( $category, $categories );
735 if ( $index !== false && $index >= 0 ) {
736 $templates_option[$index]['templates'] = $templates;
737 }
738 else {
739 $group = [ 'category' => $category, 'templates' => $templates ];
740 $templates_option[] = $group;
741 }
742
743 update_option( 'mwai_templates', $templates_option );
744 return new WP_REST_Response([ 'success' => true ], 200 );
745 }
746 catch ( Exception $e ) {
747 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
748 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
749 }
750 }
751
752 function rest_system_logs_list( $request ) {
753 try {
754 $params = $request->get_json_params();
755 $offset = $params['offset'];
756 $limit = $params['limit'];
757 $filters = $params['filters'];
758 $sort = $params['sort'];
759 $logs = apply_filters( 'mwai_stats_logs', [], $offset, $limit, $filters, $sort );
760 return new WP_REST_Response([ 'success' => true, 'total' => $logs['total'], 'logs' => $logs['rows'] ], 200 );
761 }
762 catch ( Exception $e ) {
763 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
764 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
765 }
766 }
767
768 function rest_system_logs_delete( $request ) {
769 try {
770 $params = $request->get_json_params();
771 $logIds = $params['logIds'];
772 $success = apply_filters( 'mwai_stats_logs_delete', true, $logIds );
773 return new WP_REST_Response([ 'success' => $success ], 200 );
774 }
775 catch ( Exception $e ) {
776 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
777 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
778 }
779 }
780
781 function rest_system_logs_meta_get( $request ) {
782 try {
783 $params = $request->get_json_params();
784 $logId = $params['logId'];
785 $metaKeys = $params['metaKeys'];
786 $data = apply_filters( 'mwai_stats_logs_meta', [], $logId, $metaKeys );
787 return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 );
788 }
789 catch ( Exception $e ) {
790 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
791 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
792 }
793 }
794
795 function rest_ai_moderate( $request ) {
796 try {
797 $params = $request->get_json_params();
798 $text = $params['text'];
799 if ( !$text ) {
800 return new WP_REST_Response([ 'success' => false, 'message' => 'Text not found.' ], 404 );
801 }
802 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
803 $results = $openai->moderate( $text );
804 return new WP_REST_Response([ 'success' => true, 'results' => $results ], 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
813 function rest_ai_transcribe( $request ) {
814 try {
815 $params = $request->get_json_params();
816 $query = new Meow_MWAI_Query_Transcribe();
817 $query->injectParams( $params );
818 $query->setEnv('admin-tools');
819 $reply = $this->core->ai->run( $query );
820 return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 );
821 }
822 catch ( Exception $e ) {
823 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
824 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
825 }
826 }
827
828 function rest_helpers_post_types() {
829 try {
830 $postTypes = $this->core->getPostTypes();
831 return new WP_REST_Response([ 'success' => true, 'postTypes' => $postTypes ], 200 );
832 }
833 catch ( Exception $e ) {
834 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
835 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
836 }
837 }
838
839 function rest_settings_themes( $request ) {
840 try {
841 $method = $request->get_method();
842 if ( $method === 'GET' ) {
843 $themes = $this->core->getThemes();
844 return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 );
845 }
846 else if ( $method === 'POST' ) {
847 $params = $request->get_json_params();
848 $themes = $params['themes'];
849 $themes = $this->core->updateThemes( $themes );
850 return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 );
851 }
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_settings_chatbots( $request ) {
860 try {
861 $method = $request->get_method();
862 if ( $method === 'GET' ) {
863 $chatbots = $this->core->getChatbots();
864 return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 );
865 }
866 else if ( $method === 'POST' ) {
867 $params = $request->get_json_params();
868 $chatbots = $params['chatbots'];
869 $chatbots = $this->core->updateChatbots( $chatbots );
870 return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 );
871 }
872 return new WP_REST_Response([ 'success' => false, 'message' => 'Method not allowed' ], 405 );
873 }
874 catch ( Exception $e ) {
875 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
876 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
877 }
878 }
879 }
880