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