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