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.6.83 0.1.4 1.6.84 0.1.5 1.6.85 0.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 admin.php 3 years ago api.php 3 years ago core.php 3 years ago init.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 reply.php 3 years ago rest.php 3 years ago
rest.php
848 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 'data' => $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 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
214 }
215 }
216
217 function rest_settings_reset() {
218 try {
219 $options = $this->core->reset_options();
220 $success = !!$options;
221 $message = __( $success ? 'OK' : "Could not reset options.", 'ai-engine' );
222 return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 );
223 }
224 catch ( Exception $e ) {
225 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
226 }
227 }
228
229 function createValidationResult( $result = true, $message = null) {
230 $message = $message ? $message : __( 'OK', 'ai-engine' );
231 return [ 'result' => $result, 'message' => $message ];
232 }
233
234 function validate_updated_option( $option_name ) {
235 $option_checkbox = get_option( 'mwai_option_checkbox', false );
236 $option_text = get_option( 'mwai_option_text', 'Default' );
237 if ( $option_checkbox === '' )
238 update_option( 'mwai_option_checkbox', false );
239 if ( $option_text === '' )
240 update_option( 'mwai_option_text', 'Default' );
241 return $this->createValidationResult();
242 }
243
244 function rest_ai_completions( $request ) {
245 try {
246 $params = $request->get_json_params();
247 $prompt = $params['prompt'];
248 $query = new Meow_MWAI_QueryText( $prompt );
249 $query->injectParams( $params );
250 $reply = $this->core->ai->run( $query );
251 return new WP_REST_Response([ 'success' => true, 'data' => $reply->result, 'usage' => $reply->usage ], 200 );
252 }
253 catch ( Exception $e ) {
254 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
255 }
256 }
257
258 function rest_ai_images( $request ) {
259 try {
260 $params = $request->get_json_params();
261 $prompt = $params['prompt'];
262 $query = new Meow_MWAI_QueryImage( $prompt );
263 $query->injectParams( $params );
264 $reply = $this->core->ai->run( $query );
265 return new WP_REST_Response([ 'success' => true, 'data' => $reply->results, 'usage' => $reply->usage ], 200 );
266 }
267 catch ( Exception $e ) {
268 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
269 }
270 }
271
272 function rest_ai_magic_wand( $request ) {
273 try {
274 $params = $request->get_json_params();
275 $action = isset( $params['action'] ) ? $params['action'] : null;
276 $data = isset( $params['data'] ) ? $params['data'] : null;
277 if ( empty( $data ) || empty( $action ) ) {
278 return new WP_REST_Response([ 'success' => false, 'message' => "An action and some data are required." ], 500 );
279 }
280 $postId = isset( $data['postId'] ) ? $data['postId'] : null;
281 $text = isset( $data['text'] ) ? $data['text'] : null;
282 $selectedText = isset( $data['selectedText'] ) ? $data['selectedText'] : null;
283
284 // NOTE: As soon as we have a wide range of usages and possibilities,
285 // let's refactor this into a nice and extensible UI/API.
286 $query = new Meow_MWAI_QueryText( "", 1024 );
287 $query->setEnv( 'admin-tools' );
288 $model = $this->core->get_option( 'assistants_model' );
289 if ( !empty( $model ) ) {
290 $query->setModel( $model );
291 }
292 $mode = 'replace';
293
294 $language = "";
295 $keepLanguage = "";
296 if ( !empty( $postId ) ) {
297 $language = $this->core->get_post_language( $postId );
298 $keepLanguage = " Ensure the reply is in the same language as the original text ({$language}).";
299 }
300
301 if ( $action === 'correctText' ) {
302 $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 );
303 }
304 else if ( $action === 'enhanceText' ) {
305 $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 );
306 }
307 else if ( $action === 'longerText' ) {
308 $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 );
309 }
310 else if ( $action === 'shorterText' ) {
311 $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 );
312 }
313 else if ( $action === 'suggestSynonyms' ) {
314 $mode = 'suggest';
315 $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 );
316 $query->setTemperature( 1 );
317 $query->setMaxResults( 5 );
318 }
319 else if ( $action === 'translateText' ) {
320 $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 );
321 }
322 else if ( $action === 'suggestExcerpts' ) {
323 $text = $this->core->getCleanPostContent( $postId );
324 $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 );
325 $query->setMaxResults( 5 );
326 }
327 else if ( $action === 'suggestTitles' ) {
328 $text = $this->core->getCleanPostContent( $postId );
329 $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 );
330 $query->setMaxResults( 5 );
331 }
332 $reply = $this->core->ai->run( $query );
333 return new WP_REST_Response([ 'success' => true, 'data' => [
334 'mode' => $mode,
335 'result' => $reply->result,
336 'results' => $reply->results
337 ] ], 200 );
338 }
339 catch ( Exception $e ) {
340 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
341 }
342 }
343
344 function rest_ai_copilot( $request ) {
345 try {
346 $params = $request->get_json_params();
347 $action = sanitize_text_field( $params['action'] );
348 $prompt = sanitize_text_field( $params['prompt'] );
349 if ( empty( $action ) || empty( $prompt ) ) {
350 return new WP_REST_Response([ 'success' => false, 'message' => "Copilot needs an action and a prompt." ], 500 );
351 }
352 $query = new Meow_MWAI_QueryText( $prompt, 2048 );
353 $query->setEnv( 'admin-tools' );
354 $model = $this->core->get_option( 'assistants_model' );
355 if ( !empty( $model ) ) {
356 $query->setModel( $model );
357 }
358 $reply = $this->core->ai->run( $query );
359 return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 );
360 }
361 catch ( Exception $e ) {
362 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
363 }
364 }
365
366 function rest_helpers_update_title( $request ) {
367 try {
368 $params = $request->get_json_params();
369 $title = sanitize_text_field( $params['title'] );
370 $postId = intval( $params['postId'] );
371 $post = get_post( $postId );
372 if ( !$post ) {
373 throw new Exception( 'There is no post with this ID.' );
374 }
375 $post->post_title = $title;
376 //$post->post_name = sanitize_title( $title );
377 wp_update_post( $post );
378 return new WP_REST_Response([ 'success' => true, 'message' => "Title updated." ], 200 );
379 }
380 catch ( Exception $e ) {
381 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
382 }
383 }
384
385 function rest_helpers_update_excerpt( $request ) {
386 try {
387 $params = $request->get_json_params();
388 $excerpt = sanitize_text_field( $params['excerpt'] );
389 $postId = intval( $params['postId'] );
390 $post = get_post( $postId );
391 if ( !$post ) {
392 throw new Exception( 'There is no post with this ID.' );
393 }
394 $post->post_excerpt = $excerpt;
395 wp_update_post( $post );
396 return new WP_REST_Response([ 'success' => true, 'message' => "Excerpt updated." ], 200 );
397 }
398 catch ( Exception $e ) {
399 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
400 }
401 }
402
403 function rest_helpers_create_post( $request ) {
404 try {
405 $params = $request->get_json_params();
406 $title = sanitize_text_field( $params['title'] );
407 $content = sanitize_textarea_field( $params['content'] );
408 $excerpt = sanitize_text_field( $params['excerpt'] );
409 $postType = sanitize_text_field( $params['postType'] );
410 $post = new stdClass();
411 $post->post_title = $title;
412 $post->post_excerpt = $excerpt;
413 $post->post_content = $content;
414 $post->post_status = 'draft';
415 $post->post_type = isset( $postType ) ? $postType : 'post';
416 $post->post_content = $this->core->markdown_to_html( $post->post_content );
417 $postId = wp_insert_post( $post );
418 return new WP_REST_Response([ 'success' => true, 'postId' => $postId ], 200 );
419 }
420 catch ( Exception $e ) {
421 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
422 }
423 }
424
425 function image_download( $url ) {
426 $args = array( 'timeout' => 60, );
427 $response = wp_remote_get( $url, $args );
428 if ( is_wp_error( $response ) ) {
429 throw new Exception( $response->get_error_message() );
430 }
431 $output = wp_remote_retrieve_body( $response );
432 if ( is_wp_error( $output ) ) {
433 throw new Exception( $output->get_error_message() );
434 }
435 return $output;
436 }
437
438 function rest_helpers_create_images( $request ) {
439 try {
440 $params = $request->get_json_params();
441 $title = sanitize_text_field( $params['title'] );
442 $caption = sanitize_text_field( $params['caption'] );
443 $alt = sanitize_text_field( $params['alt'] );
444 $description = sanitize_text_field( $params['description'] );
445 $url = $params['url'];
446 $filename = sanitize_text_field( $params['filename'] );
447 $image_data = $this->image_download( $url );
448 if ( !$image_data ) {
449 throw new Exception( 'Could not download the image.' );
450 }
451 $upload_dir = wp_upload_dir();
452 if ( empty( $filename ) ) {
453 $filename = basename( $url );
454 }
455 $wp_filetype = wp_check_filetype( $filename );
456 if ( wp_mkdir_p( $upload_dir['path'] ) ) {
457 $file = $upload_dir['path'] . '/' . $filename;
458 }
459 else {
460 $file = $upload_dir['basedir'] . '/' . $filename;
461 }
462
463 // Make sure the file is unique, if not, add a number to the end of the file before the extension
464 $i = 1;
465 $parts = pathinfo( $file );
466 while ( file_exists( $file ) ) {
467 $file = $parts['dirname'] . '/' . $parts['filename'] . '-' . $i . '.' . $parts['extension'];
468 $i++;
469 }
470
471 // Write the file
472 file_put_contents( $file, $image_data );
473 $attachment = [
474 'post_mime_type' => $wp_filetype['type'],
475 'post_title' => $title,
476 'post_content' => $description,
477 'post_excerpt' => $caption,
478 'post_status' => 'inherit'
479 ];
480 // Register the file as a Media Library attachment
481 $attachmentId = wp_insert_attachment( $attachment, $file );
482 require_once( ABSPATH . 'wp-admin/includes/image.php' );
483 $attachment_data = wp_generate_attachment_metadata( $attachmentId, $file );
484 wp_update_attachment_metadata( $attachmentId, $attachment_data );
485 update_post_meta( $attachmentId, '_wp_attachment_image_alt', $alt );
486 return new WP_REST_Response([ 'success' => true, 'attachmentId' => $attachmentId ], 200 );
487 }
488 catch ( Exception $e ) {
489 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
490 }
491 }
492
493 function rest_openai_files_get() {
494 try {
495 //$params = $request->get_json_params();
496 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
497 $files = $openai->listFiles();
498 return new WP_REST_Response([ 'success' => true, 'files' => $files ], 200 );
499 }
500 catch ( Exception $e ) {
501 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
502 }
503 }
504
505 function rest_openai_deleted_finetunes_get() {
506 try {
507 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
508 $finetunes = $openai->listDeletedFineTunes();
509 return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
510 }
511 catch ( Exception $e ) {
512 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
513 }
514 }
515
516 function rest_openai_finetunes_get() {
517 try {
518 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
519 $finetunes = $openai->listFineTunes();
520 return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
521 }
522 catch ( Exception $e ) {
523 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
524 }
525 }
526
527 function rest_openai_files_upload( $request ) {
528 try {
529 $params = $request->get_json_params();
530 $filename = sanitize_text_field( $params['filename'] );
531 $data = $params['data'];
532 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
533 $file = $openai->uploadFile( $filename, $data );
534 return new WP_REST_Response([ 'success' => true, 'file' => $file ], 200 );
535 }
536 catch ( Exception $e ) {
537 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
538 }
539 }
540
541 function rest_openai_files_delete( $request ) {
542 try {
543 $params = $request->get_json_params();
544 $fileId = $params['fileId'];
545 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
546 $openai->deleteFile( $fileId );
547 return new WP_REST_Response([ 'success' => true ], 200 );
548 }
549 catch ( Exception $e ) {
550 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
551 }
552 }
553
554 function rest_openai_finetunes_cancel( $request ) {
555 try {
556 $params = $request->get_json_params();
557 $finetuneId = $params['finetuneId'];
558 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
559 $openai->cancelFineTune( $finetuneId );
560 return new WP_REST_Response([ 'success' => true ], 200 );
561 }
562 catch ( Exception $e ) {
563 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
564 }
565 }
566
567 function rest_openai_finetunes_delete( $request ) {
568 try {
569 $params = $request->get_json_params();
570 $modelId = $params['modelId'];
571 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
572 $openai->deleteFineTune( $modelId );
573 return new WP_REST_Response([ 'success' => true ], 200 );
574 }
575 catch ( Exception $e ) {
576 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
577 }
578 }
579
580 function rest_openai_files_download( $request ) {
581 try {
582 $params = $request->get_json_params();
583 $fileId = $params['fileId'];
584 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
585 $data = $openai->downloadFile( $fileId );
586 return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 );
587 }
588 catch ( Exception $e ) {
589 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
590 }
591 }
592
593 function rest_openai_files_finetune( $request ) {
594 try {
595 $params = $request->get_json_params();
596 $fileId = $params['fileId'];
597 $model = $params['model'];
598 $suffix = $params['suffix'];
599 $hyperparams = [
600 "nEpochs" => $params['nEpochs'],
601 "batchSize" => $params['batchSize']
602 ];
603 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
604 $finetune = $openai->fineTuneFile( $fileId, $model, $suffix, $hyperparams );
605 return new WP_REST_Response([ 'success' => true, 'finetune' => $finetune ], 200 );
606 }
607 catch ( Exception $e ) {
608 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
609 }
610 }
611
612 function rest_openai_incidents() {
613 try {
614 $transient = get_transient( 'mwai_openai_incidents' );
615 if ( $transient ) {
616 return new WP_REST_Response([ 'success' => true, 'incidents' => $transient ], 200 );
617 }
618 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
619 $incidents = $openai->getIncidents();
620 set_transient( 'mwai_openai_incidents', $incidents, 60 * 10 );
621 return new WP_REST_Response([ 'success' => true, 'incidents' => $incidents ], 200 );
622 }
623 catch ( Exception $e ) {
624 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
625 }
626 }
627
628 function rest_helpers_count_posts( $request ) {
629 try {
630 $params = $request->get_query_params();
631 $postType = $params['postType'];
632 $postStatus = $params['postStatus'];
633 $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $postStatus ) : [ 'publish' ];
634 $count = wp_count_posts( $postType );
635 $count = array_sum( array_intersect_key( (array)$count, array_flip( $postStatus ) ) );
636 return new WP_REST_Response([ 'success' => true, 'count' => $count ], 200 );
637 }
638 catch ( Exception $e ) {
639 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
640 }
641 }
642
643 function rest_helpers_post_content( $request ) {
644 try {
645 $params = $request->get_query_params();
646 $offset = (int)$params['offset'];
647 $postType = $params['postType'];
648 $postStatus = $params['postStatus'];
649 $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $postStatus ) : [ 'publish' ];
650 $postId = (int)$params['postId'];
651
652 $post = null;
653 if ( !empty( $postId ) ) {
654 $post = get_post( $postId );
655 if ( $post->post_status !== 'publish' && $post->post_status !== 'future'
656 && $post->post_status !== 'draft' && $post->post_status !== 'private' ) {
657 $post = null;
658 }
659 }
660 else {
661 $posts = get_posts( [
662 'posts_per_page' => 1,
663 'post_type' => $postType,
664 'offset' => $offset,
665 'post_status' => $postStatus,
666 ] );
667 $post = count( $posts ) === 0 ? null : $posts[0];
668 }
669 if ( !$post ) {
670 return new WP_REST_Response([ 'success' => false, 'message' => 'Post not found' ], 404 );
671 }
672 $cleanPost = $this->core->getCleanPost( $post );
673 return new WP_REST_Response([ 'success' => true, 'content' => $cleanPost['content'],
674 'checksum' => $cleanPost['checksum'], 'language' => $cleanPost['language'], 'excerpt' => $cleanPost['excerpt'],
675 'postId' => $cleanPost['postId'], 'title' => $cleanPost['title'], 'url' => $cleanPost['url'] ], 200 );
676 }
677 catch ( Exception $e ) {
678 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
679 }
680 }
681
682 function rest_system_templates_get( $request ) {
683 try {
684 $params = $request->get_query_params();
685 $category = $params['category'];
686 $templates = [];
687 $templates_option = get_option( 'mwai_templates', [] );
688 if ( !is_array( $templates_option ) ) {
689 update_option( 'mwai_templates', [] );
690 }
691 $categories = array_column( $templates_option, 'category' );
692 $index = array_search( $category, $categories );
693 $templates = [];
694 if ( $index !== false ) {
695 $templates = $templates_option[$index]['templates'];
696 }
697 return new WP_REST_Response([ 'success' => true, 'templates' => $templates ], 200 );
698 }
699 catch ( Exception $e ) {
700 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
701 }
702 }
703
704 function rest_system_templates_save( $request ) {
705 try {
706 $params = $request->get_json_params();
707 $category = $params['category'];
708 $templates = $params['templates'];
709 $templates_option = get_option( 'mwai_templates', [] );
710 $categories = array_column( $templates_option, 'category' );
711 $index = array_search( $category, $categories );
712 if ( $index !== false && $index >= 0 ) {
713 $templates_option[$index]['templates'] = $templates;
714 }
715 else {
716 $group = [ 'category' => $category, 'templates' => $templates ];
717 $templates_option[] = $group;
718 }
719
720 update_option( 'mwai_templates', $templates_option );
721 return new WP_REST_Response([ 'success' => true ], 200 );
722 }
723 catch ( Exception $e ) {
724 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
725 }
726 }
727
728 function rest_system_logs_list( $request ) {
729 try {
730 $params = $request->get_json_params();
731 $offset = $params['offset'];
732 $limit = $params['limit'];
733 $filters = $params['filters'];
734 $sort = $params['sort'];
735 $logs = apply_filters( 'mwai_stats_logs', [], $offset, $limit, $filters, $sort );
736 return new WP_REST_Response([ 'success' => true, 'total' => $logs['total'], 'logs' => $logs['rows'] ], 200 );
737 }
738 catch ( Exception $e ) {
739 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
740 }
741 }
742
743 function rest_system_logs_delete( $request ) {
744 try {
745 $params = $request->get_json_params();
746 $logIds = $params['logIds'];
747 $success = apply_filters( 'mwai_stats_logs_delete', true, $logIds );
748 return new WP_REST_Response([ 'success' => $success ], 200 );
749 }
750 catch ( Exception $e ) {
751 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
752 }
753 }
754
755 function rest_system_logs_meta_get( $request ) {
756 try {
757 $params = $request->get_json_params();
758 $logId = $params['logId'];
759 $metaKeys = $params['metaKeys'];
760 $data = apply_filters( 'mwai_stats_logs_meta', [], $logId, $metaKeys );
761 return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 );
762 }
763 catch ( Exception $e ) {
764 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
765 }
766 }
767
768 function rest_ai_moderate( $request ) {
769 try {
770 $params = $request->get_json_params();
771 $text = $params['text'];
772 if ( !$text ) {
773 return new WP_REST_Response([ 'success' => false, 'message' => 'Text not found.' ], 404 );
774 }
775 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
776 $results = $openai->moderate( $text );
777 return new WP_REST_Response([ 'success' => true, 'results' => $results ], 200 );
778 }
779 catch ( Exception $e ) {
780 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
781 }
782
783 }
784
785 function rest_ai_transcribe( $request ) {
786 try {
787 $params = $request->get_json_params();
788 $query = new Meow_MWAI_QueryTranscribe();
789 $query->injectParams( $params );
790 $query->setEnv('admin-tools');
791 $reply = $this->core->ai->run( $query );
792 return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 );
793 }
794 catch ( Exception $e ) {
795 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
796 }
797 }
798
799 function rest_helpers_post_types() {
800 try {
801 $postTypes = $this->core->getPostTypes();
802 return new WP_REST_Response([ 'success' => true, 'postTypes' => $postTypes ], 200 );
803 }
804 catch ( Exception $e ) {
805 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
806 }
807 }
808
809 function rest_settings_themes( $request ) {
810 try {
811 $method = $request->get_method();
812 if ( $method === 'GET' ) {
813 $themes = $this->core->getThemes();
814 return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 );
815 }
816 else if ( $method === 'POST' ) {
817 $params = $request->get_json_params();
818 $themes = $params['themes'];
819 $themes = $this->core->updateThemes( $themes );
820 return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 );
821 }
822 }
823 catch ( Exception $e ) {
824 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
825 }
826 }
827
828 function rest_settings_chatbots( $request ) {
829 try {
830 $method = $request->get_method();
831 if ( $method === 'GET' ) {
832 $chatbots = $this->core->getChatbots();
833 return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 );
834 }
835 else if ( $method === 'POST' ) {
836 $params = $request->get_json_params();
837 $chatbots = $params['chatbots'];
838 $chatbots = $this->core->updateChatbots( $chatbots );
839 return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 );
840 }
841 return new WP_REST_Response([ 'success' => false, 'message' => 'Method not allowed' ], 405 );
842 }
843 catch ( Exception $e ) {
844 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
845 }
846 }
847 }
848