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