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