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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 ui.php 3 years ago
rest.php
768 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, '/make_titles', array(
36 'methods' => 'POST',
37 'permission_callback' => array( $this->core, 'can_access_features' ),
38 'callback' => array( $this, 'make_titles' ),
39 ) );
40 register_rest_route( $this->namespace, '/make_excerpts', array(
41 'methods' => 'POST',
42 'permission_callback' => array( $this->core, 'can_access_features' ),
43 'callback' => array( $this, 'make_excerpts' ),
44 ) );
45 register_rest_route( $this->namespace, '/update_post_title', array(
46 'methods' => 'POST',
47 'permission_callback' => array( $this->core, 'can_access_features' ),
48 'callback' => array( $this, 'update_post_title' ),
49 ) );
50 register_rest_route( $this->namespace, '/update_post_excerpt', array(
51 'methods' => 'POST',
52 'permission_callback' => array( $this->core, 'can_access_features' ),
53 'callback' => array( $this, 'update_post_excerpt' ),
54 ) );
55 register_rest_route( $this->namespace, '/create_post', array(
56 'methods' => 'POST',
57 'permission_callback' => array( $this->core, 'can_access_features' ),
58 'callback' => array( $this, 'create_post' ),
59 ) );
60 register_rest_route( $this->namespace, '/create_image', array(
61 'methods' => 'POST',
62 'permission_callback' => array( $this->core, 'can_access_features' ),
63 'callback' => array( $this, 'create_image' ),
64 ) );
65 register_rest_route( $this->namespace, '/openai_files', array(
66 'methods' => 'GET',
67 'permission_callback' => array( $this->core, 'can_access_settings' ),
68 'callback' => array( $this, 'openai_files_get' ),
69 ) );
70 register_rest_route( $this->namespace, '/openai_files', array(
71 'methods' => 'DELETE',
72 'permission_callback' => array( $this->core, 'can_access_settings' ),
73 'callback' => array( $this, 'openai_files_delete' ),
74 ) );
75 register_rest_route( $this->namespace, '/openai_files', array(
76 'methods' => 'POST',
77 'permission_callback' => array( $this->core, 'can_access_settings' ),
78 'callback' => array( $this, 'openai_files_upload' ),
79 ) );
80 register_rest_route( $this->namespace, '/openai_files_download', array(
81 'methods' => 'POST',
82 'permission_callback' => array( $this->core, 'can_access_settings' ),
83 'callback' => array( $this, 'openai_files_download' ),
84 ) );
85 register_rest_route( $this->namespace, '/openai_files_finetune', array(
86 'methods' => 'POST',
87 'permission_callback' => array( $this->core, 'can_access_settings' ),
88 'callback' => array( $this, 'openai_files_finetune' ),
89 ) );
90 register_rest_route( $this->namespace, '/openai_deleted_finetunes', array(
91 'methods' => 'GET',
92 'permission_callback' => array( $this->core, 'can_access_settings' ),
93 'callback' => array( $this, 'openai_deleted_finetunes_get' ),
94 ) );
95 register_rest_route( $this->namespace, '/openai_finetunes', array(
96 'methods' => 'GET',
97 'permission_callback' => array( $this->core, 'can_access_settings' ),
98 'callback' => array( $this, 'openai_finetunes_get' ),
99 ) );
100 register_rest_route( $this->namespace, '/openai_finetunes', array(
101 'methods' => 'DELETE',
102 'permission_callback' => array( $this->core, 'can_access_settings' ),
103 'callback' => array( $this, 'openai_finetunes_delete' ),
104 ) );
105 register_rest_route( $this->namespace, '/openai_finetunes_cancel', array(
106 'methods' => 'POST',
107 'permission_callback' => array( $this->core, 'can_access_settings' ),
108 'callback' => array( $this, 'openai_finetunes_cancel' ),
109 ) );
110 register_rest_route( $this->namespace, '/openai_incidents', array(
111 'methods' => 'GET',
112 'permission_callback' => array( $this->core, 'can_access_settings' ),
113 'callback' => array( $this, 'openai_incidents' ),
114 ) );
115 register_rest_route( $this->namespace, '/count_posts', array(
116 'methods' => 'GET',
117 'permission_callback' => array( $this->core, 'can_access_features' ),
118 'callback' => array( $this, 'count_posts' ),
119 ) );
120 register_rest_route( $this->namespace, '/post_types', array(
121 'methods' => 'GET',
122 'permission_callback' => array( $this->core, 'can_access_features' ),
123 'callback' => array( $this, 'post_types' ),
124 ) );
125 register_rest_route( $this->namespace, '/post_content', array(
126 'methods' => 'GET',
127 'permission_callback' => array( $this->core, 'can_access_features' ),
128 'callback' => array( $this, 'post_content' ),
129 ) );
130 register_rest_route( $this->namespace, '/templates', array(
131 'methods' => 'GET',
132 'permission_callback' => array( $this->core, 'can_access_features' ),
133 'callback' => array( $this, 'templates_get' ),
134 ) );
135 register_rest_route( $this->namespace, '/templates', array(
136 'methods' => 'POST',
137 'permission_callback' => array( $this->core, 'can_access_features' ),
138 'callback' => array( $this, 'templates_save' ),
139 ) );
140 register_rest_route( $this->namespace, '/logs', array(
141 'methods' => 'POST',
142 'permission_callback' => array( $this->core, 'can_access_settings' ),
143 'callback' => array( $this, 'get_logs' ),
144 ) );
145 register_rest_route( $this->namespace, '/moderate', array(
146 'methods' => 'POST',
147 'permission_callback' => array( $this->core, 'can_access_settings' ),
148 'callback' => array( $this, 'moderate' ),
149 ) );
150 register_rest_route( $this->namespace, '/vectors', array(
151 'methods' => 'POST',
152 'permission_callback' => array( $this->core, 'can_access_settings' ),
153 'callback' => array( $this, 'get_vectors' ),
154 ) );
155 register_rest_route( $this->namespace, '/vector', array(
156 'methods' => 'POST',
157 'permission_callback' => array( $this->core, 'can_access_settings' ),
158 'callback' => array( $this, 'add_vector' ),
159 ) );
160 register_rest_route( $this->namespace, '/vectors_ref', array(
161 'methods' => 'POST',
162 'permission_callback' => array( $this->core, 'can_access_settings' ),
163 'callback' => array( $this, 'get_vectors_ref' ),
164 ) );
165 register_rest_route( $this->namespace, '/vector', array(
166 'methods' => 'PUT',
167 'permission_callback' => array( $this->core, 'can_access_settings' ),
168 'callback' => array( $this, 'modify_vector' ),
169 ) );
170 register_rest_route( $this->namespace, '/vectors', array(
171 'methods' => 'DELETE',
172 'permission_callback' => array( $this->core, 'can_access_settings' ),
173 'callback' => array( $this, 'delete_vectors' ),
174 ) );
175 register_rest_route( $this->namespace, '/transcribe', array(
176 'methods' => 'POST',
177 'permission_callback' => array( $this->core, 'can_access_settings' ),
178 'callback' => array( $this, 'transcribe' ),
179 ) );
180 }
181 catch ( Exception $e ) {
182 var_dump( $e );
183 }
184 }
185
186 function rest_all_settings() {
187 return new WP_REST_Response( [
188 'success' => true,
189 'data' => $this->core->get_all_options()
190 ], 200 );
191 }
192
193 function rest_update_option( $request ) {
194 try {
195 $params = $request->get_json_params();
196 $value = $params['options'];
197 $options = $this->core->update_options( $value );
198 $success = !!$options;
199 $message = __( $success ? 'OK' : "Could not update options.", 'ai-engine' );
200 return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 );
201 }
202 catch ( Exception $e ) {
203 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
204 }
205 }
206
207 function createValidationResult( $result = true, $message = null) {
208 $message = $message ? $message : __( 'OK', 'ai-engine' );
209 return [ 'result' => $result, 'message' => $message ];
210 }
211
212 function validate_updated_option( $option_name ) {
213 $option_checkbox = get_option( 'mwai_option_checkbox', false );
214 $option_text = get_option( 'mwai_option_text', 'Default' );
215 if ( $option_checkbox === '' )
216 update_option( 'mwai_option_checkbox', false );
217 if ( $option_text === '' )
218 update_option( 'mwai_option_text', 'Default' );
219 return $this->createValidationResult();
220 }
221
222 function make_completions( $request ) {
223 try {
224 $params = $request->get_json_params();
225 $prompt = $params['prompt'];
226 $query = new Meow_MWAI_QueryText( $prompt );
227 $query->injectParams( $params );
228 $answer = $this->core->ai->run( $query );
229 return new WP_REST_Response([ 'success' => true, 'data' => $answer->result, 'usage' => $answer->usage ], 200 );
230 }
231 catch ( Exception $e ) {
232 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
233 }
234 }
235
236 function make_images( $request ) {
237 try {
238 $params = $request->get_json_params();
239 $prompt = $params['prompt'];
240 $query = new Meow_MWAI_QueryImage( $prompt );
241 $query->injectParams( $params );
242 $answer = $this->core->ai->run( $query );
243 return new WP_REST_Response([ 'success' => true, 'data' => $answer->results, 'usage' => $answer->usage ], 200 );
244 }
245 catch ( Exception $e ) {
246 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
247 }
248 }
249
250 function make_titles( $request ) {
251 try {
252 $params = $request->get_json_params();
253 $postId = intval( $params['postId'] );
254 $text = $this->core->getCleanPostContent( $postId );
255 if ( empty( $text ) ) {
256 return new WP_REST_Response([ 'success' => false, 'message' => "No text found for this post." ], 500 );
257 }
258 $language = $this->core->get_post_language( $postId );
259 $prompt = "Using the same original language ($language), create a short but SEO-friendly title for this text: " . $text;
260 $query = new Meow_MWAI_QueryText( $prompt, 128 );
261 $query->setMaxResults( 5 );
262 $query->setEnv( 'admin-tools' );
263 $answer = $this->core->ai->run( $query );
264 return new WP_REST_Response([ 'success' => true, 'data' => $answer->results ], 200 );
265 }
266 catch ( Exception $e ) {
267 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
268 }
269 }
270
271 function make_excerpts( $request ) {
272 try {
273 $params = $request->get_json_params();
274 $postId = intval( $params['postId'] );
275 $text = $this->core->getCleanPostContent( $postId );
276 if ( empty( $text ) ) {
277 return new WP_REST_Response([ 'success' => false, 'message' => "No text found for this post." ], 500 );
278 }
279 $language = $this->core->get_post_language( $postId );
280 $prompt = "Using the same original language ($language), create a SEO-friendly introduction to this text, 120 to 170 characters max, no URLs: " . $text;
281 $query = new Meow_MWAI_QueryText( $prompt, 512 );
282 $query->setMaxResults( 5 );
283 $query->setEnv( 'admin-tools' );
284 $answer = $this->core->ai->run( $query );
285 return new WP_REST_Response([ 'success' => true, 'data' => $answer->results ], 200 );
286 }
287 catch ( Exception $e ) {
288 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
289 }
290 }
291
292 function update_post_title( $request ) {
293 try {
294 $params = $request->get_json_params();
295 $title = sanitize_text_field( $params['title'] );
296 $postId = intval( $params['postId'] );
297 $post = get_post( $postId );
298 if ( !$post ) {
299 throw new Exception( 'There is no post with this ID.' );
300 }
301 $post->post_title = $title;
302 //$post->post_name = sanitize_title( $title );
303 wp_update_post( $post );
304 return new WP_REST_Response([ 'success' => true, 'message' => "Title updated." ], 200 );
305 }
306 catch ( Exception $e ) {
307 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
308 }
309 }
310
311 function update_post_excerpt( $request ) {
312 try {
313 $params = $request->get_json_params();
314 $excerpt = sanitize_text_field( $params['excerpt'] );
315 $postId = intval( $params['postId'] );
316 $post = get_post( $postId );
317 if ( !$post ) {
318 throw new Exception( 'There is no post with this ID.' );
319 }
320 $post->post_excerpt = $excerpt;
321 wp_update_post( $post );
322 return new WP_REST_Response([ 'success' => true, 'message' => "Excerpt updated." ], 200 );
323 }
324 catch ( Exception $e ) {
325 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
326 }
327 }
328
329 function create_post( $request ) {
330 try {
331 $params = $request->get_json_params();
332 $title = sanitize_text_field( $params['title'] );
333 $content = sanitize_textarea_field( $params['content'] );
334 $excerpt = sanitize_text_field( $params['excerpt'] );
335 $post = new stdClass();
336 $post->post_title = $title;
337 $post->post_excerpt = $excerpt;
338 $post->post_content = $content;
339 $post->post_status = 'draft';
340 $post->post_type = 'post';
341 $post->post_content = $this->core->markdown_to_html( $post->post_content );
342 $postId = wp_insert_post( $post );
343 return new WP_REST_Response([ 'success' => true, 'postId' => $postId ], 200 );
344 }
345 catch ( Exception $e ) {
346 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
347 }
348 }
349
350 function image_download( $url ) {
351 $response = wp_remote_get( $url );
352 $output = wp_remote_retrieve_body( $response );
353 return $output;
354 }
355
356 function create_image( $request ) {
357 try {
358 $params = $request->get_json_params();
359 $title = sanitize_text_field( $params['title'] );
360 $caption = sanitize_text_field( $params['caption'] );
361 $alt = sanitize_text_field( $params['alt'] );
362 $description = sanitize_text_field( $params['description'] );
363 $url = $params['url'];
364 $filename = sanitize_text_field( $params['filename'] );
365 $image_data = $this->image_download( $url );
366 if ( !$image_data ) {
367 throw new Exception( 'Could not download the image.' );
368 }
369 $upload_dir = wp_upload_dir();
370 if ( empty( $filename ) ) {
371 $filename = basename( $url );
372 }
373 $wp_filetype = wp_check_filetype( $filename );
374 if ( wp_mkdir_p( $upload_dir['path'] ) ) {
375 $file = $upload_dir['path'] . '/' . $filename;
376 }
377 else {
378 $file = $upload_dir['basedir'] . '/' . $filename;
379 }
380
381 // Make sure the file is unique, if not, add a number to the end of the file before the extension
382 $i = 1;
383 $parts = pathinfo( $file );
384 while ( file_exists( $file ) ) {
385 $file = $parts['dirname'] . '/' . $parts['filename'] . '-' . $i . '.' . $parts['extension'];
386 $i++;
387 }
388
389 // Write the file
390 file_put_contents( $file, $image_data );
391 $attachment = [
392 'post_mime_type' => $wp_filetype['type'],
393 'post_title' => $title,
394 'post_content' => $description,
395 'post_excerpt' => $caption,
396 'post_status' => 'inherit'
397 ];
398 // Register the file as a Media Library attachment
399 $attachmentId = wp_insert_attachment( $attachment, $file );
400 require_once( ABSPATH . 'wp-admin/includes/image.php' );
401 $attachment_data = wp_generate_attachment_metadata( $attachmentId, $file );
402 wp_update_attachment_metadata( $attachmentId, $attachment_data );
403 update_post_meta( $attachmentId, '_wp_attachment_image_alt', $alt );
404 return new WP_REST_Response([ 'success' => true, 'attachmentId' => $attachmentId ], 200 );
405 }
406 catch ( Exception $e ) {
407 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
408 }
409 }
410
411 function openai_files_get() {
412 try {
413 //$params = $request->get_json_params();
414 $openai = new Meow_MWAI_OpenAI( $this->core );
415 $files = $openai->listFiles();
416 return new WP_REST_Response([ 'success' => true, 'files' => $files ], 200 );
417 }
418 catch ( Exception $e ) {
419 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
420 }
421 }
422
423 function openai_deleted_finetunes_get() {
424 try {
425 $openai = new Meow_MWAI_OpenAI( $this->core );
426 $finetunes = $openai->listDeletedFineTunes();
427 return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
428 }
429 catch ( Exception $e ) {
430 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
431 }
432 }
433
434 function openai_finetunes_get() {
435 try {
436 $openai = new Meow_MWAI_OpenAI( $this->core );
437 $finetunes = $openai->listFineTunes();
438 return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
439 }
440 catch ( Exception $e ) {
441 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
442 }
443 }
444
445 function openai_files_upload( $request ) {
446 try {
447 $params = $request->get_json_params();
448 $filename = sanitize_text_field( $params['filename'] );
449 $data = $params['data'];
450 $openai = new Meow_MWAI_OpenAI( $this->core );
451 $file = $openai->uploadFile( $filename, $data );
452 return new WP_REST_Response([ 'success' => true, 'file' => $file ], 200 );
453 }
454 catch ( Exception $e ) {
455 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
456 }
457 }
458
459 function openai_files_delete( $request ) {
460 try {
461 $params = $request->get_json_params();
462 $fileId = $params['fileId'];
463 $openai = new Meow_MWAI_OpenAI( $this->core );
464 $openai->deleteFile( $fileId );
465 return new WP_REST_Response([ 'success' => true ], 200 );
466 }
467 catch ( Exception $e ) {
468 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
469 }
470 }
471
472 function openai_finetunes_cancel( $request ) {
473 try {
474 $params = $request->get_json_params();
475 $finetuneId = $params['finetuneId'];
476 $openai = new Meow_MWAI_OpenAI( $this->core );
477 $openai->cancelFineTune( $finetuneId );
478 return new WP_REST_Response([ 'success' => true ], 200 );
479 }
480 catch ( Exception $e ) {
481 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
482 }
483 }
484
485 function openai_finetunes_delete( $request ) {
486 try {
487 $params = $request->get_json_params();
488 $modelId = $params['modelId'];
489 $openai = new Meow_MWAI_OpenAI( $this->core );
490 $openai->deleteFineTune( $modelId );
491 return new WP_REST_Response([ 'success' => true ], 200 );
492 }
493 catch ( Exception $e ) {
494 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
495 }
496 }
497
498 function openai_files_download( $request ) {
499 try {
500 $params = $request->get_json_params();
501 $fileId = $params['fileId'];
502 $openai = new Meow_MWAI_OpenAI( $this->core );
503 $data = $openai->downloadFile( $fileId );
504 return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 );
505 }
506 catch ( Exception $e ) {
507 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
508 }
509 }
510
511 function openai_files_finetune( $request ) {
512 try {
513 $params = $request->get_json_params();
514 $fileId = $params['fileId'];
515 $model = $params['model'];
516 $suffix = $params['suffix'];
517 $hyperparams = [
518 "nEpochs" => $params['nEpochs'],
519 "batchSize" => $params['batchSize']
520 ];
521 $openai = new Meow_MWAI_OpenAI( $this->core );
522 $finetune = $openai->fineTuneFile( $fileId, $model, $suffix, $hyperparams );
523 return new WP_REST_Response([ 'success' => true, 'finetune' => $finetune ], 200 );
524 }
525 catch ( Exception $e ) {
526 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
527 }
528 }
529
530 function openai_incidents() {
531 try {
532 $transient = get_transient( 'mwai_openai_incidents' );
533 if ( $transient ) {
534 return new WP_REST_Response([ 'success' => true, 'incidents' => $transient ], 200 );
535 }
536 $openai = new Meow_MWAI_OpenAI( $this->core );
537 $incidents = $openai->getIncidents();
538 set_transient( 'mwai_openai_incidents', $incidents, 60 * 10 );
539 return new WP_REST_Response([ 'success' => true, 'incidents' => $incidents ], 200 );
540 }
541 catch ( Exception $e ) {
542 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
543 }
544 }
545
546 function count_posts( $request ) {
547 try {
548 $params = $request->get_query_params();
549 $postType = $params['postType'];
550 $count = wp_count_posts( $postType );
551 return new WP_REST_Response([ 'success' => true, 'count' => $count ], 200 );
552 }
553 catch ( Exception $e ) {
554 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
555 }
556 }
557
558 function post_content( $request ) {
559 try {
560 $params = $request->get_query_params();
561 $offset = (int)$params['offset'];
562 $postType = $params['postType'];
563 $postId = (int)$params['postId'];
564 $post = null;
565 if ( !empty( $postId ) ) {
566 $post = get_post( $postId );
567 if ( $post->post_status !== 'publish' && $post->post_status !== 'future' && $post->post_status !== 'draft' ) {
568 $post = null;
569 }
570 }
571 else {
572 $posts = get_posts( [
573 'posts_per_page' => 1,
574 'post_type' => $postType,
575 'offset' => $offset,
576 'post_status' => 'publish'
577 ] );
578 $post = count( $posts ) === 0 ? null : $posts[0];
579 }
580 if ( !$post ) {
581 return new WP_REST_Response([ 'success' => false, 'message' => 'Post not found' ], 404 );
582 }
583 $language = $this->core->get_post_language( $post->ID );
584 $content = apply_filters( 'the_content', $post->post_content );
585 // Resolve html entities
586 $content = html_entity_decode( $content );
587 $content = wp_strip_all_tags( $content );
588 $content = preg_replace( '/[\r\n]+/', "\n", $content );
589 // Remove all the non-characters except \n
590 $checksum = wp_hash( $content );
591 $title = $post->post_title;
592 $excerpt = $post->post_excerpt;
593 $url = get_permalink( $post->ID );
594 return new WP_REST_Response([ 'success' => true, 'content' => $content, 'checksum' => $checksum,
595 'language' => $language, 'excerpt' => $excerpt,
596 'postId' => $post->ID, 'title' => $title, 'url' => $url ], 200 );
597 }
598 catch ( Exception $e ) {
599 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
600 }
601 }
602
603 function templates_get( $request ) {
604 try {
605 $params = $request->get_query_params();
606 $category = $params['category'];
607 $templates = [];
608 $templates_option = get_option( 'mwai_templates', [] );
609 if ( !is_array( $templates_option ) ) {
610 update_option( 'mwai_templates', [] );
611 }
612 $categories = array_column( $templates_option, 'category' );
613 $index = array_search( $category, $categories );
614 $templates = [];
615 if ( $index !== false ) {
616 $templates = $templates_option[$index]['templates'];
617 }
618 return new WP_REST_Response([ 'success' => true, 'templates' => $templates ], 200 );
619 }
620 catch ( Exception $e ) {
621 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
622 }
623 }
624
625 function templates_save( $request ) {
626 try {
627 $params = $request->get_json_params();
628 $category = $params['category'];
629 $templates = $params['templates'];
630 $templates_option = get_option( 'mwai_templates', [] );
631 $categories = array_column( $templates_option, 'category' );
632 $index = array_search( $category, $categories );
633 if ( $index !== false && $index >= 0 ) {
634 $templates_option[$index]['templates'] = $templates;
635 }
636 else {
637 $group = [ 'category' => $category, 'templates' => $templates ];
638 $templates_option[] = $group;
639 }
640
641 update_option( 'mwai_templates', $templates_option );
642 return new WP_REST_Response([ 'success' => true ], 200 );
643 }
644 catch ( Exception $e ) {
645 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
646 }
647 }
648
649 function get_logs( $request ) {
650 try {
651 $params = $request->get_json_params();
652 $offset = $params['offset'];
653 $limit = $params['limit'];
654 $filters = $params['filters'];
655 $sort = $params['sort'];
656 $logs = apply_filters( 'mwai_stats_logs', [], $offset, $limit, $filters, $sort );
657 return new WP_REST_Response([ 'success' => true, 'total' => $logs['total'], 'logs' => $logs['rows'] ], 200 );
658 }
659 catch ( Exception $e ) {
660 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
661 }
662 }
663
664 function moderate( $request ) {
665 try {
666 $params = $request->get_json_params();
667 $text = $params['text'];
668 if ( !$text ) {
669 return new WP_REST_Response([ 'success' => false, 'message' => 'Text not found.' ], 404 );
670 }
671 $openai = new Meow_MWAI_OpenAI( $this->core );
672 $results = $openai->moderate( $text );
673 return new WP_REST_Response([ 'success' => true, 'results' => $results ], 200 );
674 }
675 catch ( Exception $e ) {
676 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
677 }
678
679 }
680
681 function get_vectors( $request ) {
682 try {
683 $params = $request->get_json_params();
684 $offset = $params['offset'];
685 $limit = $params['limit'];
686 $filters = $params['filters'];
687 $sort = $params['sort'];
688 $vectors = apply_filters( 'mwai_embeddings_vectors', [], $offset, $limit, $filters, $sort );
689 return new WP_REST_Response([ 'success' => true, 'total' => $vectors['total'], 'vectors' => $vectors['rows'] ], 200 );
690 }
691 catch ( Exception $e ) {
692 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
693 }
694 }
695
696 function add_vector( $request ) {
697 try {
698 $params = $request->get_json_params();
699 $vector = $params['vector'];
700 $success = apply_filters( 'mwai_embeddings_vectors_add', false, $vector );
701 return new WP_REST_Response([ 'success' => $success, 'vector' => $vector ], 200 );
702 }
703 catch ( Exception $e ) {
704 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
705 }
706 }
707
708 function get_vectors_ref( $request ) {
709 try {
710 $params = $request->get_json_params();
711 $refId = $params['refId'];
712 $vectors = apply_filters( 'mwai_embeddings_vectors_ref', false, $refId );
713 return new WP_REST_Response([ 'success' => true, 'vectors' => $vectors ], 200 );
714 }
715 catch ( Exception $e ) {
716 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
717 }
718 }
719
720 function modify_vector( $request ) {
721 try {
722 $params = $request->get_json_params();
723 $vector = $params['vector'];
724 $success = apply_filters( 'mwai_embeddings_vectors_update', false, $vector );
725 return new WP_REST_Response([ 'success' => $success, 'vector' => $vector ], 200 );
726 }
727 catch ( Exception $e ) {
728 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
729 }
730 }
731
732 function delete_vectors( $request ) {
733 try {
734 $params = $request->get_json_params();
735 $ids = $params['ids'];
736 $success = apply_filters( 'mwai_embeddings_vectors_delete', false, $ids );
737 return new WP_REST_Response([ 'success' => $success ], 200 );
738 }
739 catch ( Exception $e ) {
740 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
741 }
742 }
743
744 function transcribe( $request ) {
745 try {
746 $params = $request->get_json_params();
747 $query = new Meow_MWAI_QueryTranscribe();
748 $query->injectParams( $params );
749 $query->setEnv('admin-tools');
750 $answer = $this->core->ai->run( $query );
751 return new WP_REST_Response([ 'success' => true, 'data' => $answer->result ], 200 );
752 }
753 catch ( Exception $e ) {
754 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
755 }
756 }
757
758 function post_types() {
759 try {
760 $postTypes = $this->core->getPostTypes();
761 return new WP_REST_Response([ 'success' => true, 'postTypes' => $postTypes ], 200 );
762 }
763 catch ( Exception $e ) {
764 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
765 }
766 }
767 }
768