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