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.1.6 1.6.86 0.1.7 1.6.87 0.1.8 1.6.88 0.1.9 1.6.89 0.2.0 1.6.90
ai-engine / classes / rest.php
ai-engine / classes Last commit date
engines 2 years ago modules 2 years ago queries 2 years ago admin.php 2 years ago api.php 2 years ago core.php 2 years ago init.php 3 years ago reply.php 2 years ago rest.php 2 years ago
rest.php
972 lines
1 <?php
2
3 class Meow_MWAI_Rest
4 {
5 private $core = null;
6 private $namespace = 'mwai/v1';
7
8 public function __construct( $core ) {
9 $this->core = $core;
10 add_action( 'rest_api_init', array( $this, 'rest_init' ) );
11 }
12
13 function rest_init() {
14 try {
15 // Settings Endpoints
16 register_rest_route( $this->namespace, '/settings/update', array(
17 'methods' => 'POST',
18 'permission_callback' => [ $this->core, 'can_access_settings' ],
19 'callback' => [ $this, 'rest_settings_update' ],
20 ) );
21 register_rest_route( $this->namespace, '/settings/list', array(
22 'methods' => 'GET',
23 'permission_callback' => [ $this->core, 'can_access_settings' ],
24 'callback' => [ $this, 'rest_settings_list' ],
25 ) );
26 register_rest_route( $this->namespace, '/settings/reset', array(
27 'methods' => 'POST',
28 'permission_callback' => [ $this->core, 'can_access_settings' ],
29 'callback' => [ $this, 'rest_settings_reset' ],
30 ) );
31 register_rest_route( $this->namespace, '/settings/chatbots', array(
32 'methods' => ['GET', 'POST'],
33 'permission_callback' => [ $this->core, 'can_access_settings' ],
34 'callback' => [ $this, 'rest_settings_chatbots' ],
35 ) );
36 register_rest_route( $this->namespace, '/settings/themes', array(
37 'methods' => ['GET', 'POST'],
38 'permission_callback' => [ $this->core, 'can_access_settings' ],
39 'callback' => [ $this, 'rest_settings_themes' ],
40 ) );
41
42 // System Endpoints
43 register_rest_route( $this->namespace, '/system/logs/list', array(
44 'methods' => 'POST',
45 'permission_callback' => [ $this->core, 'can_access_settings' ],
46 'callback' => [ $this, 'rest_system_logs_list' ],
47 ) );
48 register_rest_route( $this->namespace, '/system/logs/delete', array(
49 'methods' => 'POST',
50 'permission_callback' => [ $this->core, 'can_access_settings' ],
51 'callback' => [ $this, 'rest_system_logs_delete' ],
52 ) );
53 register_rest_route( $this->namespace, '/system/logs/meta', array(
54 'methods' => 'POST',
55 'permission_callback' => [ $this->core, 'can_access_settings' ],
56 'callback' => [ $this, 'rest_system_logs_meta_get' ],
57 ) );
58 register_rest_route( $this->namespace, '/system/templates', array(
59 'methods' => 'POST',
60 'permission_callback' => [ $this->core, 'can_access_features' ],
61 'callback' => [ $this, 'rest_system_templates_save' ],
62 ) );
63 register_rest_route( $this->namespace, '/system/templates', array(
64 'methods' => 'GET',
65 'permission_callback' => [ $this->core, 'can_access_features' ],
66 'callback' => [ $this, 'rest_system_templates_get' ],
67 ) );
68
69 // AI Endpoints
70 register_rest_route( $this->namespace, '/ai/completions', array(
71 'methods' => 'POST',
72 'permission_callback' => [ $this->core, 'can_access_features' ],
73 'callback' => [ $this, 'rest_ai_completions' ],
74 ) );
75 register_rest_route( $this->namespace, '/ai/images', array(
76 'methods' => 'POST',
77 'permission_callback' => [ $this->core, 'can_access_features' ],
78 'callback' => [ $this, 'rest_ai_images' ],
79 ) );
80 register_rest_route( $this->namespace, '/ai/copilot', array(
81 'methods' => 'POST',
82 'permission_callback' => [ $this->core, 'can_access_features' ],
83 'callback' => [ $this, 'rest_ai_copilot' ],
84 ) );
85
86 register_rest_route( $this->namespace, '/ai/magic_wand', array(
87 'methods' => 'POST',
88 'callback' => [ $this, 'rest_ai_magic_wand' ],
89 'permission_callback' => [ $this->core, 'can_access_features' ],
90 ) );
91 register_rest_route( $this->namespace, '/ai/moderate', array(
92 'methods' => 'POST',
93 'permission_callback' => [ $this->core, 'can_access_settings' ],
94 'callback' => [ $this, 'rest_ai_moderate' ],
95 ) );
96 register_rest_route( $this->namespace, '/ai/transcribe_audio', array(
97 'methods' => 'POST',
98 'permission_callback' => [ $this->core, 'can_access_settings' ],
99 'callback' => [ $this, 'rest_ai_transcribe_audio' ],
100 ) );
101 register_rest_route( $this->namespace, '/ai/transcribe_image', array(
102 'methods' => 'POST',
103 'permission_callback' => [ $this->core, 'can_access_settings' ],
104 'callback' => [ $this, 'rest_ai_transcribe_image' ],
105 ) );
106 register_rest_route( $this->namespace, '/ai/json', array(
107 'methods' => 'POST',
108 'permission_callback' => [ $this->core, 'can_access_settings' ],
109 'callback' => [ $this, 'rest_ai_json' ],
110 ) );
111
112 // Helpers Endpoints
113 register_rest_route( $this->namespace, '/helpers/update_post_title', array(
114 'methods' => 'POST',
115 'permission_callback' => [ $this->core, 'can_access_features' ],
116 'callback' => [ $this, 'rest_helpers_update_title' ],
117 ) );
118 register_rest_route( $this->namespace, '/helpers/update_post_excerpt', array(
119 'methods' => 'POST',
120 'permission_callback' => [ $this->core, 'can_access_features' ],
121 'callback' => [ $this, 'rest_helpers_update_excerpt' ],
122 ) );
123 register_rest_route( $this->namespace, '/helpers/create_post', array(
124 'methods' => 'POST',
125 'permission_callback' => [ $this->core, 'can_access_features' ],
126 'callback' => [ $this, 'rest_helpers_create_post' ],
127 ) );
128 register_rest_route( $this->namespace, '/helpers/create_image', array(
129 'methods' => 'POST',
130 'permission_callback' => [ $this->core, 'can_access_features' ],
131 'callback' => [ $this, 'rest_helpers_create_images' ],
132 ) );
133 register_rest_route( $this->namespace, '/helpers/count_posts', array(
134 'methods' => 'GET',
135 'permission_callback' => [ $this->core, 'can_access_features' ],
136 'callback' => [ $this, 'rest_helpers_count_posts' ],
137 ) );
138 register_rest_route( $this->namespace, '/helpers/post_types', array(
139 'methods' => 'GET',
140 'permission_callback' => [ $this->core, 'can_access_features' ],
141 'callback' => [ $this, 'rest_helpers_post_types' ],
142 ) );
143 register_rest_route( $this->namespace, '/helpers/post_content', array(
144 'methods' => 'GET',
145 'permission_callback' => [ $this->core, 'can_access_features' ],
146 'callback' => [ $this, 'rest_helpers_post_content' ],
147 ) );
148
149 // OpenAI Endpoints
150 register_rest_route( $this->namespace, '/openai/files/list', array(
151 'methods' => 'GET',
152 'permission_callback' => [ $this->core, 'can_access_settings' ],
153 'callback' => [ $this, 'rest_openai_files_get' ],
154 ) );
155 register_rest_route( $this->namespace, '/openai/files/upload', array(
156 'methods' => 'POST',
157 'permission_callback' => [ $this->core, 'can_access_settings' ],
158 'callback' => [ $this, 'rest_openai_files_upload' ],
159 ) );
160 register_rest_route( $this->namespace, '/openai/files/delete', array(
161 'methods' => 'POST',
162 'permission_callback' => [ $this->core, 'can_access_settings' ],
163 'callback' => [ $this, 'rest_openai_files_delete' ],
164 ) );
165 register_rest_route( $this->namespace, '/openai/files/download', array(
166 'methods' => 'POST',
167 'permission_callback' => [ $this->core, 'can_access_settings' ],
168 'callback' => [ $this, 'rest_openai_files_download' ],
169 ) );
170 register_rest_route( $this->namespace, '/openai/files/finetune', array(
171 'methods' => 'POST',
172 'permission_callback' => [ $this->core, 'can_access_settings' ],
173 'callback' => [ $this, 'rest_openai_files_finetune' ],
174 ) );
175 register_rest_route( $this->namespace, '/openai/finetunes/list_deleted', array(
176 'methods' => 'GET',
177 'permission_callback' => [ $this->core, 'can_access_settings' ],
178 'callback' => [ $this, 'rest_openai_deleted_finetunes_get' ],
179 ) );
180
181 // register_rest_route( $this->namespace, '/openai/models', array(
182 // 'methods' => 'GET',
183 // 'permission_callback' => [ $this->core, 'can_access_settings' ],
184 // 'callback' => [ $this, 'rest_openai_models_get' ],
185 // ) );
186
187 register_rest_route( $this->namespace, '/openai/finetunes/list', array(
188 'methods' => 'GET',
189 'permission_callback' => [ $this->core, 'can_access_settings' ],
190 'callback' => [ $this, 'rest_openai_finetunes_get' ],
191 ) );
192 register_rest_route( $this->namespace, '/openai/finetunes/delete', array(
193 'methods' => 'POST',
194 'permission_callback' => [ $this->core, 'can_access_settings' ],
195 'callback' => [ $this, 'rest_openai_finetunes_delete' ],
196 ) );
197 register_rest_route( $this->namespace, '/openai/finetunes/cancel', array(
198 'methods' => 'POST',
199 'permission_callback' => [ $this->core, 'can_access_settings' ],
200 'callback' => [ $this, 'rest_openai_finetunes_cancel' ],
201 ) );
202 register_rest_route( $this->namespace, '/openai/incidents', array(
203 'methods' => 'GET',
204 'permission_callback' => [ $this->core, 'can_access_settings' ],
205 'callback' => [ $this, 'rest_openai_incidents' ],
206 ) );
207 }
208 catch ( Exception $e ) {
209 var_dump( $e );
210 }
211 }
212
213 function rest_settings_list() {
214 return new WP_REST_Response( [
215 'success' => true,
216 'options' => $this->core->get_all_options()
217 ], 200 );
218 }
219
220 function rest_settings_update( $request ) {
221 try {
222 $params = $request->get_json_params();
223 $value = $params['options'];
224 $options = $this->core->update_options( $value );
225 $success = !!$options;
226 $message = __( $success ? 'OK' : "Could not update options.", 'ai-engine' );
227 return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 );
228 }
229 catch ( Exception $e ) {
230 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
231 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
232 }
233 }
234
235 function rest_settings_reset() {
236 try {
237 $options = $this->core->reset_options();
238 $success = !!$options;
239 $message = __( $success ? 'OK' : "Could not reset options.", 'ai-engine' );
240 return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 );
241 }
242 catch ( Exception $e ) {
243 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
244 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
245 }
246 }
247
248 function createValidationResult( $result = true, $message = null) {
249 $message = $message ? $message : __( 'OK', 'ai-engine' );
250 return [ 'result' => $result, 'message' => $message ];
251 }
252
253 function validate_updated_option( $option_name ) {
254 $option_checkbox = get_option( 'mwai_option_checkbox', false );
255 $option_text = get_option( 'mwai_option_text', 'Default' );
256 if ( $option_checkbox === '' )
257 update_option( 'mwai_option_checkbox', false );
258 if ( $option_text === '' )
259 update_option( 'mwai_option_text', 'Default' );
260 return $this->createValidationResult();
261 }
262
263 function rest_ai_completions( $request ) {
264 try {
265 $params = $request->get_json_params();
266 $prompt = $params['prompt'];
267 $query = new Meow_MWAI_Query_Text( $prompt );
268 $query->injectParams( $params );
269
270 // Handle streaming
271 $stream = $params['stream'] ?? false;
272 $streamCallback = null;
273 if ( $stream ) {
274 $streamCallback = function( $reply ) {
275 //$raw = _wp_specialchars( $reply, ENT_NOQUOTES, 'UTF-8', true );
276 $raw = $reply;
277 $this->core->stream_push( [ 'type' => 'live', 'data' => $raw ] );
278 if ( ob_get_level() > 0 ) {
279 ob_flush();
280 }
281 flush();
282 };
283 header( 'Cache-Control: no-cache' );
284 header( 'Content-Type: text/event-stream' );
285 header( 'X-Accel-Buffering: no' ); // This is useful to disable buffering in nginx through headers.
286 ob_implicit_flush( true );
287 ob_end_flush();
288 }
289
290 // Process Reply
291 $reply = $this->core->ai->run( $query, $streamCallback );
292 $restRes = [
293 'success' => true,
294 'data' => $reply->result,
295 'usage' => $reply->usage
296 ];
297 if ( $stream ) {
298 $this->core->stream_push( [ 'type' => 'end', 'data' => json_encode( $restRes ) ] );
299 die();
300 }
301 return new WP_REST_Response( $restRes, 200 );
302 }
303 catch ( Exception $e ) {
304 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
305 if ( $stream ) {
306 $this->core->stream_push( [ 'type' => 'error', 'data' => $message ] );
307 }
308 else {
309 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
310 }
311 }
312 }
313
314 function rest_ai_images( $request ) {
315 try {
316 $params = $request->get_json_params();
317 $prompt = $params['prompt'];
318 $query = new Meow_MWAI_Query_Image( $prompt );
319 $query->injectParams( $params );
320 $reply = $this->core->ai->run( $query );
321 return new WP_REST_Response([ 'success' => true, 'data' => $reply->results, 'usage' => $reply->usage ], 200 );
322 }
323 catch ( Exception $e ) {
324 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
325 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
326 }
327 }
328
329 function rest_ai_magic_wand( $request ) {
330 try {
331 $params = $request->get_json_params();
332 $action = isset( $params['action'] ) ? $params['action'] : null;
333 $data = isset( $params['data'] ) ? $params['data'] : null;
334 if ( empty( $data ) || empty( $action ) ) {
335 return new WP_REST_Response([ 'success' => false, 'message' => "An action and some data are required." ], 500 );
336 }
337 $postId = isset( $data['postId'] ) ? $data['postId'] : null;
338 $text = isset( $data['text'] ) ? $data['text'] : null;
339 $selectedText = isset( $data['selectedText'] ) ? $data['selectedText'] : null;
340
341 // NOTE: As soon as we have a wide range of usages and possibilities,
342 // let's refactor this into a nice and extensible UI/API.
343 $query = new Meow_MWAI_Query_Text( "", 1024 );
344 $query->setEnv( 'admin-tools' );
345 // TODO: We should also use the envId (as the model belongs to it)
346 $model = $this->core->get_option( 'ai_default_model' );
347 if ( !empty( $model ) ) {
348 $query->setModel( $model );
349 }
350 $mode = 'replace';
351
352 $language = "";
353 $keepLanguage = "";
354 if ( !empty( $postId ) ) {
355 $language = $this->core->get_post_language( $postId );
356 $keepLanguage = " Ensure the reply is in the same language as the original text ({$language}).";
357 }
358
359 if ( $action === 'correctText' ) {
360 $query->setPrompt( "Correct the typos and grammar mistakes in this text without altering its content. Return only the corrected text, without explanations or additional content.{$keepLanguage}\n\n" . $text );
361 }
362 else if ( $action === 'enhanceText' ) {
363 $query->setPrompt( "Enhance this text by eliminating redundancies, utilizing a more suitable vocabulary, and refining its structure. Provide only the revised text, without explanations or any additional content.{$keepLanguage}\n\n" . $text );
364 }
365 else if ( $action === 'longerText' ) {
366 $query->setPrompt( "Expand the subsequent text to a minimum of three times its original length, integrating relevant and accurate information to enrich its content. If the text is a story, amplify its charm by elaborating on essential aspects, enhancing readability, and creating a sense of engagement for the reader. Maintain consistency in tone and vocabulary throughout the expansion process.{$keepLanguage}\n\n" . $text );
367 }
368 else if ( $action === 'shorterText' ) {
369 $query->setPrompt( "Condense the following text by reducing its length to half, while retaining the core elements of the original narrative. Focus on maintaining the essence of the story and its key details.{$keepLanguage}\n\n" . $text );
370 }
371 else if ( $action === 'suggestSynonyms' ) {
372 $mode = 'suggest';
373 $query->setPrompt( "Provide a synonym or rephrase the given word or sentence while retaining the original meaning and preserving the initial and final punctuation. Offer only the resulting word or expression, without additional context. If a suitable synonym or alternative cannot be identified, ensure that a creative response is still provided.{$keepLanguage}\n\n" . $selectedText );
374 $query->setTemperature( 1 );
375 $query->setMaxResults( 5 );
376 }
377 else if ( $action === 'generateImage' ) {
378 $mode = 'insert';
379 $query = new Meow_MWAI_Query_Image( "Generate an image that is relevant to the following text:\n\n" . $text );
380 }
381 else if ( $action === 'suggestImages' ) {
382 $mode = 'suggest';
383 $query = new Meow_MWAI_Query_Image( "Suggest three images that is relevant to the following text:\n\n" . $text );
384 throw new Exception( 'Not implemented yet.' );
385 }
386 else if ( $action === 'translateText' ) {
387 $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 );
388 }
389 else if ( $action === 'suggestExcerpts' ) {
390 $text = $this->core->getCleanPostContent( $postId );
391 $query->setPrompt( "Craft a clear, SEO-optimized introduction for the following text, using 120 to 170 characters. Ensure the introduction is concise and relevant, without including any URLs.{$keepLanguage}\n\n" . $text );
392 $query->setMaxResults( 5 );
393 }
394 else if ( $action === 'suggestTitles' ) {
395 $text = $this->core->getCleanPostContent( $postId );
396 $query->setPrompt( "Generate a concise, SEO-optimized title for the following text, without using quotes or any other formatting. Focus on clarity and relevance to the content.{$keepLanguage}\n\n" . $text );
397 $query->setMaxResults( 5 );
398 }
399 $reply = $this->core->ai->run( $query );
400
401 // If it's an image, let's add it to the Media Library and return it.
402 if ( $action === 'generateImage' ) {
403 preg_match( '/\!\[Image\]\((.*?)\)/', $reply->result, $matches );
404 $url = $matches[1] ?? $reply->result;
405 $attachmentId = $this->core->addImageFromURL( $url, null, null, null, null, null, $postId );
406 if ( empty( $attachmentId ) ) {
407 throw new Exception( 'Could not add the image to the Media Library.' );
408 }
409 $media = [
410 'id' => $attachmentId,
411 'url' => wp_get_attachment_url( $attachmentId ),
412 'title' => get_the_title( $attachmentId ),
413 'caption' => wp_get_attachment_caption( $attachmentId ),
414 'alt' => get_post_meta( $attachmentId, '_wp_attachment_image_alt', true )
415 ];
416 return new WP_REST_Response([
417 'success' => true,
418 'data' => [
419 'mode' => 'insertMedia',
420 'type' => 'image',
421 'media' => $media
422 ]
423 ], 200 );
424 }
425
426 return new WP_REST_Response([ 'success' => true, 'data' => [
427 'mode' => $mode,
428 'type' => $reply->type,
429 'result' => $reply->result,
430 'results' => $reply->results
431 ] ], 200 );
432 }
433 catch ( Exception $e ) {
434 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
435 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
436 }
437 }
438
439 function rest_ai_copilot( $request ) {
440 try {
441 $params = $request->get_json_params();
442 $action = sanitize_text_field( $params['action'] );
443 $prompt = sanitize_text_field( $params['prompt'] );
444 if ( empty( $action ) || empty( $prompt ) ) {
445 return new WP_REST_Response([ 'success' => false, 'message' => "Copilot needs an action and a prompt." ], 500 );
446 }
447 $query = new Meow_MWAI_Query_Text( $prompt, 2048 );
448 $query->setEnv( 'admin-tools' );
449 // TODO: We should also use the envId (as the model belongs to it)
450 $model = $this->core->get_option( 'ai_default_model' );
451 if ( !empty( $model ) ) {
452 $query->setModel( $model );
453 }
454 $reply = $this->core->ai->run( $query );
455 return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 );
456 }
457 catch ( Exception $e ) {
458 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
459 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
460 }
461 }
462
463 function rest_helpers_update_title( $request ) {
464 try {
465 $params = $request->get_json_params();
466 $title = sanitize_text_field( $params['title'] );
467 $postId = intval( $params['postId'] );
468 $post = get_post( $postId );
469 if ( !$post ) {
470 throw new Exception( 'There is no post with this ID.' );
471 }
472 $post->post_title = $title;
473 //$post->post_name = sanitize_title( $title );
474 wp_update_post( $post );
475 return new WP_REST_Response([ 'success' => true, 'message' => "Title updated." ], 200 );
476 }
477 catch ( Exception $e ) {
478 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
479 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
480 }
481 }
482
483 function rest_helpers_update_excerpt( $request ) {
484 try {
485 $params = $request->get_json_params();
486 $excerpt = sanitize_text_field( $params['excerpt'] );
487 $postId = intval( $params['postId'] );
488 $post = get_post( $postId );
489 if ( !$post ) {
490 throw new Exception( 'There is no post with this ID.' );
491 }
492 $post->post_excerpt = $excerpt;
493 wp_update_post( $post );
494 return new WP_REST_Response([ 'success' => true, 'message' => "Excerpt updated." ], 200 );
495 }
496 catch ( Exception $e ) {
497 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
498 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
499 }
500 }
501
502 function rest_helpers_create_post( $request ) {
503 try {
504 $params = $request->get_json_params();
505 $title = sanitize_text_field( $params['title'] );
506 $content = sanitize_textarea_field( $params['content'] );
507 $excerpt = sanitize_text_field( $params['excerpt'] );
508 $postType = sanitize_text_field( $params['postType'] );
509 $post = new stdClass();
510 $post->post_title = $title;
511 $post->post_excerpt = $excerpt;
512 $post->post_content = $content;
513 $post->post_status = 'draft';
514 $post->post_type = isset( $postType ) ? $postType : 'post';
515 $post->post_content = $this->core->markdown_to_html( $post->post_content );
516 $postId = wp_insert_post( $post );
517 return new WP_REST_Response([ 'success' => true, 'postId' => $postId ], 200 );
518 }
519 catch ( Exception $e ) {
520 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
521 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
522 }
523 }
524
525 function rest_helpers_create_images( $request ) {
526 try {
527 $params = $request->get_json_params();
528 $title = sanitize_text_field( $params['title'] );
529 $caption = sanitize_text_field( $params['caption'] );
530 $alt = sanitize_text_field( $params['alt'] );
531 $description = sanitize_text_field( $params['description'] );
532 $url = $params['url'];
533 $filename = sanitize_text_field( $params['filename'] );
534 $attachmentId = $this->core->addImageFromURL( $url, $filename, $title, $description, $caption, $alt );
535 return new WP_REST_Response([ 'success' => true, 'attachmentId' => $attachmentId ], 200 );
536 }
537 catch ( Exception $e ) {
538 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
539 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
540 }
541 }
542
543 function rest_openai_files_get() {
544 try {
545 $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null;
546 $openai = new Meow_MWAI_Engines_OpenAI( $this->core, $envId );
547 $files = $openai->list_files();
548 return new WP_REST_Response([ 'success' => true, 'files' => $files ], 200 );
549 }
550 catch ( Exception $e ) {
551 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
552 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
553 }
554 }
555
556 // function rest_openai_models_get() {
557 // try {
558 // $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
559 // $finetunes = $openai->listModels();
560 // return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
561 // }
562 // catch ( Exception $e ) {
563 // $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
564 // return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
565 // }
566 // }
567
568 function rest_openai_deleted_finetunes_get() {
569 try {
570 $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null;
571 $legacy = isset( $_GET['legacy'] ) ? $_GET['legacy'] === 'true' : false;
572 $openai = new Meow_MWAI_Engines_OpenAI( $this->core, $envId );
573 $finetunes = $openai->list_deleted_finetunes( $legacy );
574 return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
575 }
576 catch ( Exception $e ) {
577 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
578 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
579 }
580 }
581
582 function rest_openai_finetunes_get() {
583 try {
584 $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null;
585 $legacy = isset( $_GET['legacy'] ) ? $_GET['legacy'] === 'true' : false;
586 $openai = new Meow_MWAI_Engines_OpenAI( $this->core, $envId );
587 $finetunes = $openai->list_finetunes( $legacy );
588 return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
589 }
590 catch ( Exception $e ) {
591 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
592 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
593 }
594 }
595
596 function rest_openai_files_upload( $request ) {
597 try {
598 $params = $request->get_json_params();
599 $envId = $params['envId'];;
600 $filename = sanitize_text_field( $params['filename'] );
601 $data = $params['data'];
602 $openai = new Meow_MWAI_Engines_OpenAI( $this->core, $envId );
603 $file = $openai->upload_file( $filename, $data );
604 return new WP_REST_Response([ 'success' => true, 'file' => $file ], 200 );
605 }
606 catch ( Exception $e ) {
607 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
608 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
609 }
610 }
611
612 function rest_openai_files_delete( $request ) {
613 try {
614 $params = $request->get_json_params();
615 $envId = $params['envId'];;
616 $fileId = $params['fileId'];
617 $openai = new Meow_MWAI_Engines_OpenAI( $this->core, $envId );
618 $openai->delete_file( $fileId );
619 return new WP_REST_Response([ 'success' => true ], 200 );
620 }
621 catch ( Exception $e ) {
622 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
623 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
624 }
625 }
626
627 function rest_openai_finetunes_cancel( $request ) {
628 try {
629 $params = $request->get_json_params();
630 $envId = $params['envId'];;
631 $finetuneId = $params['finetuneId'];
632 $openai = new Meow_MWAI_Engines_OpenAI( $this->core, $envId );
633 $openai->cancel_finetune( $finetuneId );
634 return new WP_REST_Response([ 'success' => true ], 200 );
635 }
636 catch ( Exception $e ) {
637 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
638 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
639 }
640 }
641
642 function rest_openai_finetunes_delete( $request ) {
643 try {
644 $params = $request->get_json_params();
645 $envId = $params['envId'];;
646 $modelId = $params['modelId'];
647 $openai = new Meow_MWAI_Engines_OpenAI( $this->core, $envId );
648 $openai->delete_finetune( $modelId );
649 return new WP_REST_Response([ 'success' => true ], 200 );
650 }
651 catch ( Exception $e ) {
652 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
653 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
654 }
655 }
656
657 function rest_openai_files_download( $request ) {
658 try {
659 $params = $request->get_json_params();
660 $envId = $params['envId'];;
661 $fileId = $params['fileId'];
662 $openai = new Meow_MWAI_Engines_OpenAI( $this->core, $envId );
663 $data = $openai->download_file( $fileId );
664 return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 );
665 }
666 catch ( Exception $e ) {
667 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
668 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
669 }
670 }
671
672 function rest_openai_files_finetune( $request ) {
673 try {
674 $params = $request->get_json_params();
675 $envId = $params['envId'];;
676 $fileId = $params['fileId'];
677 $model = $params['model'];
678 $suffix = $params['suffix'];
679 $hyperparams = [
680 "nEpochs" => $params['nEpochs'],
681 "batchSize" => $params['batchSize']
682 ];
683 $openai = new Meow_MWAI_Engines_OpenAI( $this->core, $envId );
684 $finetune = $openai->run_finetune( $fileId, $model, $suffix, $hyperparams );
685 return new WP_REST_Response([ 'success' => true, 'finetune' => $finetune ], 200 );
686 }
687 catch ( Exception $e ) {
688 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
689 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
690 }
691 }
692
693 function rest_openai_incidents() {
694 try {
695 $transient = get_transient( 'mwai_openai_incidents' );
696 if ( $transient ) {
697 return new WP_REST_Response([ 'success' => true, 'incidents' => $transient ], 200 );
698 }
699 $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
700 $incidents = $openai->get_incidents();
701 set_transient( 'mwai_openai_incidents', $incidents, 60 * 10 );
702 return new WP_REST_Response([ 'success' => true, 'incidents' => $incidents ], 200 );
703 }
704 catch ( Exception $e ) {
705 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
706 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
707 }
708 }
709
710 function rest_helpers_count_posts( $request ) {
711 try {
712 $params = $request->get_query_params();
713 $postType = $params['postType'];
714 $postStatus = $params['postStatus'];
715 $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $postStatus ) : [ 'publish' ];
716 $count = wp_count_posts( $postType );
717 $count = array_sum( array_intersect_key( (array)$count, array_flip( $postStatus ) ) );
718 return new WP_REST_Response([ 'success' => true, 'count' => $count ], 200 );
719 }
720 catch ( Exception $e ) {
721 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
722 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
723 }
724 }
725
726 function rest_helpers_post_content( $request ) {
727 try {
728 $params = $request->get_query_params();
729 $offset = (int)$params['offset'];
730 $postType = $params['postType'];
731 $postStatus = $params['postStatus'];
732 $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $postStatus ) : [ 'publish' ];
733 $postId = (int)$params['postId'];
734
735 $post = null;
736 if ( !empty( $postId ) ) {
737 $post = get_post( $postId );
738 if ( $post->post_status !== 'publish' && $post->post_status !== 'future'
739 && $post->post_status !== 'draft' && $post->post_status !== 'private' ) {
740 $post = null;
741 }
742 }
743 else {
744 $posts = get_posts( [
745 'posts_per_page' => 1,
746 'post_type' => $postType,
747 'offset' => $offset,
748 'post_status' => $postStatus,
749 ] );
750 $post = count( $posts ) === 0 ? null : $posts[0];
751 }
752 if ( !$post ) {
753 return new WP_REST_Response([ 'success' => false, 'message' => 'Post not found' ], 404 );
754 }
755 $cleanPost = $this->core->getCleanPost( $post );
756 return new WP_REST_Response([ 'success' => true, 'content' => $cleanPost['content'],
757 'checksum' => $cleanPost['checksum'], 'language' => $cleanPost['language'], 'excerpt' => $cleanPost['excerpt'],
758 'postId' => $cleanPost['postId'], 'title' => $cleanPost['title'], 'url' => $cleanPost['url'] ], 200 );
759 }
760 catch ( Exception $e ) {
761 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
762 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
763 }
764 }
765
766 function rest_system_templates_get( $request ) {
767 try {
768 $params = $request->get_query_params();
769 $category = $params['category'];
770 $templates = [];
771 $templates_option = get_option( 'mwai_templates', [] );
772 if ( !is_array( $templates_option ) ) {
773 update_option( 'mwai_templates', [] );
774 }
775 $categories = array_column( $templates_option, 'category' );
776 $index = array_search( $category, $categories );
777 $templates = [];
778 if ( $index !== false ) {
779 $templates = $templates_option[$index]['templates'];
780 }
781 return new WP_REST_Response([ 'success' => true, 'templates' => $templates ], 200 );
782 }
783 catch ( Exception $e ) {
784 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
785 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
786 }
787 }
788
789 function rest_system_templates_save( $request ) {
790 try {
791 $params = $request->get_json_params();
792 $category = $params['category'];
793 $templates = $params['templates'];
794 $templates_option = get_option( 'mwai_templates', [] );
795 $categories = array_column( $templates_option, 'category' );
796 $index = array_search( $category, $categories );
797 if ( $index !== false && $index >= 0 ) {
798 $templates_option[$index]['templates'] = $templates;
799 }
800 else {
801 $group = [ 'category' => $category, 'templates' => $templates ];
802 $templates_option[] = $group;
803 }
804
805 update_option( 'mwai_templates', $templates_option );
806 return new WP_REST_Response([ 'success' => true ], 200 );
807 }
808 catch ( Exception $e ) {
809 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
810 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
811 }
812 }
813
814 function rest_system_logs_list( $request ) {
815 try {
816 $params = $request->get_json_params();
817 $offset = $params['offset'];
818 $limit = $params['limit'];
819 $filters = $params['filters'];
820 $sort = $params['sort'];
821 $logs = apply_filters( 'mwai_stats_logs', [], $offset, $limit, $filters, $sort );
822 return new WP_REST_Response([ 'success' => true, 'total' => $logs['total'], 'logs' => $logs['rows'] ], 200 );
823 }
824 catch ( Exception $e ) {
825 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
826 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
827 }
828 }
829
830 function rest_system_logs_delete( $request ) {
831 try {
832 $params = $request->get_json_params();
833 $logIds = $params['logIds'];
834 $success = apply_filters( 'mwai_stats_logs_delete', true, $logIds );
835 return new WP_REST_Response([ 'success' => $success ], 200 );
836 }
837 catch ( Exception $e ) {
838 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
839 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
840 }
841 }
842
843 function rest_system_logs_meta_get( $request ) {
844 try {
845 $params = $request->get_json_params();
846 $logId = $params['logId'];
847 $metaKeys = $params['metaKeys'];
848 $data = apply_filters( 'mwai_stats_logs_meta', [], $logId, $metaKeys );
849 return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 );
850 }
851 catch ( Exception $e ) {
852 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
853 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
854 }
855 }
856
857 function rest_ai_moderate( $request ) {
858 try {
859 $params = $request->get_json_params();
860 $envId = $params['envId'];
861 $text = $params['text'];
862 if ( !$text ) {
863 return new WP_REST_Response([ 'success' => false, 'message' => 'Text not found.' ], 404 );
864 }
865 $openai = new Meow_MWAI_Engines_OpenAI( $this->core, $envId );
866 $results = $openai->moderate( $text );
867 return new WP_REST_Response([ 'success' => true, 'results' => $results ], 200 );
868 }
869 catch ( Exception $e ) {
870 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
871 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
872 }
873 }
874
875 function rest_ai_transcribe_audio( $request ) {
876 try {
877 $params = $request->get_json_params();
878 $query = new Meow_MWAI_Query_Transcribe();
879 $query->injectParams( $params );
880 $query->setEnv('admin-tools');
881 $reply = $this->core->ai->run( $query );
882 return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 );
883 }
884 catch ( Exception $e ) {
885 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
886 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
887 }
888 }
889
890 function rest_ai_transcribe_image( $request ) {
891 try {
892 global $mwai;
893 $params = $request->get_json_params();
894 $prompt = !empty( $params['prompt'] ) ? $params['prompt'] : null;
895 $url = !empty( $params['url'] ) ? $params['url'] : null;
896 $path = !empty( $params['path'] ) ? $params['path'] : null;
897 $result = $mwai->simpleVisionQuery( $prompt, $url, $path );
898 return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 );
899 }
900 catch ( Exception $e ) {
901 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
902 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
903 }
904 }
905
906 function rest_ai_json( $request ) {
907 try {
908 global $mwai;
909 $params = $request->get_json_params();
910 $prompt = !empty( $params['prompt'] ) ? $params['prompt'] : null;
911 $result = $mwai->simpleJsonQuery( $prompt );
912 return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 );
913 }
914 catch ( Exception $e ) {
915 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
916 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
917 }
918 }
919
920 function rest_helpers_post_types() {
921 try {
922 $postTypes = $this->core->getPostTypes();
923 return new WP_REST_Response([ 'success' => true, 'postTypes' => $postTypes ], 200 );
924 }
925 catch ( Exception $e ) {
926 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
927 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
928 }
929 }
930
931 function rest_settings_themes( $request ) {
932 try {
933 $method = $request->get_method();
934 if ( $method === 'GET' ) {
935 $themes = $this->core->getThemes();
936 return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 );
937 }
938 else if ( $method === 'POST' ) {
939 $params = $request->get_json_params();
940 $themes = $params['themes'];
941 $themes = $this->core->updateThemes( $themes );
942 return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 );
943 }
944 }
945 catch ( Exception $e ) {
946 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
947 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
948 }
949 }
950
951 function rest_settings_chatbots( $request ) {
952 try {
953 $method = $request->get_method();
954 if ( $method === 'GET' ) {
955 $chatbots = $this->core->getChatbots();
956 return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 );
957 }
958 else if ( $method === 'POST' ) {
959 $params = $request->get_json_params();
960 $chatbots = $params['chatbots'];
961 $chatbots = $this->core->updateChatbots( $chatbots );
962 return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 );
963 }
964 return new WP_REST_Response([ 'success' => false, 'message' => 'Method not allowed' ], 405 );
965 }
966 catch ( Exception $e ) {
967 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
968 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
969 }
970 }
971 }
972