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