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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
996 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 // We do not need the images to be downloaded (if set by the options) because it will be
396 // downloaded and added anyway to the Media Library.
397 $query->set_local_download( null );
398 }
399 else if ( $action === 'suggestImages' ) {
400 $mode = 'suggest';
401 $query = new Meow_MWAI_Query_Image( "Suggest three images that is relevant to the following text:\n\n" . $text );
402 throw new Exception( 'Not implemented yet.' );
403 }
404 else if ( $action === 'translateText' ) {
405 $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 );
406 }
407 else if ( $action === 'suggestExcerpts' ) {
408 $text = $this->core->get_post_content( $postId );
409 $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 );
410 $query->set_max_results( 5 );
411 }
412 else if ( $action === 'suggestTitles' ) {
413 $text = $this->core->get_post_content( $postId );
414 $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 );
415 $query->set_max_results( 5 );
416 }
417 $reply = $this->core->run_query( $query );
418
419 // If it's an image, let's add it to the Media Library and return it.
420 if ( $action === 'generateImage' ) {
421 preg_match( '/\!\[Image\]\((.*?)\)/', $reply->result, $matches );
422 $url = $matches[1] ?? $reply->result;
423 $attachmentId = $this->core->add_image_from_url( $url, null, null, null, null, null, $postId );
424 if ( empty( $attachmentId ) ) {
425 throw new Exception( 'Could not add the image to the Media Library.' );
426 }
427 $media = [
428 'id' => $attachmentId,
429 'url' => wp_get_attachment_url( $attachmentId ),
430 'title' => get_the_title( $attachmentId ),
431 'caption' => wp_get_attachment_caption( $attachmentId ),
432 'alt' => get_post_meta( $attachmentId, '_wp_attachment_image_alt', true )
433 ];
434 return new WP_REST_Response([
435 'success' => true,
436 'data' => [
437 'mode' => 'insertMedia',
438 'type' => 'image',
439 'media' => $media
440 ]
441 ], 200 );
442 }
443
444 return new WP_REST_Response([ 'success' => true, 'data' => [
445 'mode' => $mode,
446 'type' => $reply->type,
447 'result' => $reply->result,
448 'results' => $reply->results
449 ] ], 200 );
450 }
451 catch ( Exception $e ) {
452 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
453 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
454 }
455 }
456
457 function rest_ai_copilot( $request ) {
458 try {
459 $params = $request->get_json_params();
460 $action = sanitize_text_field( $params['action'] );
461 $message = sanitize_text_field( $params['prompt'] );
462 if ( empty( $action ) || empty( $message ) ) {
463 return new WP_REST_Response([ 'success' => false, 'message' => "Copilot needs an action and a prompt." ], 500 );
464 }
465 $query = new Meow_MWAI_Query_Text( $message, 2048 );
466 $query->set_scope( 'admin-tools' );
467 $model = $this->core->get_option( 'ai_default_model' );
468 $env = $this->core->get_option( 'ai_default_env' );
469 if ( !empty( $model ) ) {
470 $query->set_model( $model );
471 }
472 if ( !empty( $env ) ) {
473 $query->set_env_id( $env );
474 }
475 $reply = $this->core->run_query( $query );
476 return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 );
477 }
478 catch ( Exception $e ) {
479 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
480 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
481 }
482 }
483
484 function rest_helpers_update_title( $request ) {
485 try {
486 $params = $request->get_json_params();
487 $title = sanitize_text_field( $params['title'] );
488 $postId = intval( $params['postId'] );
489 $post = get_post( $postId );
490 if ( !$post ) {
491 throw new Exception( 'There is no post with this ID.' );
492 }
493 $post->post_title = $title;
494 //$post->post_name = sanitize_title( $title );
495 wp_update_post( $post );
496 return new WP_REST_Response([ 'success' => true, 'message' => "Title updated." ], 200 );
497 }
498 catch ( Exception $e ) {
499 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
500 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
501 }
502 }
503
504 function rest_helpers_update_excerpt( $request ) {
505 try {
506 $params = $request->get_json_params();
507 $excerpt = sanitize_text_field( $params['excerpt'] );
508 $postId = intval( $params['postId'] );
509 $post = get_post( $postId );
510 if ( !$post ) {
511 throw new Exception( 'There is no post with this ID.' );
512 }
513 $post->post_excerpt = $excerpt;
514 wp_update_post( $post );
515 return new WP_REST_Response([ 'success' => true, 'message' => "Excerpt updated." ], 200 );
516 }
517 catch ( Exception $e ) {
518 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
519 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
520 }
521 }
522
523 function rest_helpers_create_post( $request ) {
524 try {
525 $params = $request->get_json_params();
526 $title = sanitize_text_field( $params['title'] );
527 $content = sanitize_textarea_field( $params['content'] );
528 $excerpt = sanitize_text_field( $params['excerpt'] );
529 $postType = sanitize_text_field( $params['postType'] );
530 $post = new stdClass();
531 $post->post_title = $title;
532 $post->post_excerpt = $excerpt;
533 $post->post_content = $content;
534 $post->post_status = 'draft';
535 $post->post_type = isset( $postType ) ? $postType : 'post';
536 // TODO: Let's try to avoid using Markdown to create the Post
537 // Instead, we should create Gutenberg Blocks, or simple HTML.
538 // Then, we can get rid of the library for Markdown.
539 $post->post_content = $this->core->markdown_to_html( $post->post_content );
540 $postId = wp_insert_post( $post );
541 return new WP_REST_Response([ 'success' => true, 'postId' => $postId ], 200 );
542 }
543 catch ( Exception $e ) {
544 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
545 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
546 }
547 }
548
549 function rest_helpers_create_images( $request ) {
550 try {
551 $params = $request->get_json_params();
552 $title = sanitize_text_field( $params['title'] );
553 $caption = sanitize_text_field( $params['caption'] );
554 $alt = sanitize_text_field( $params['alt'] );
555 $description = sanitize_text_field( $params['description'] );
556 $url = $params['url'];
557 $filename = sanitize_text_field( $params['filename'] );
558 $attachmentId = $this->core->add_image_from_url( $url, $filename, $title, $description, $caption, $alt );
559 return new WP_REST_Response([ 'success' => true, 'attachmentId' => $attachmentId ], 200 );
560 }
561 catch ( Exception $e ) {
562 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
563 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
564 }
565 }
566
567 function rest_openai_files_get() {
568 try {
569 $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null;
570 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
571 $files = $openai->list_files();
572 return new WP_REST_Response([ 'success' => true, 'files' => $files ], 200 );
573 }
574 catch ( Exception $e ) {
575 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
576 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
577 }
578 }
579
580 // function rest_openai_models_get() {
581 // try {
582 // $openai = new Meow_MWAI_Engines_OpenAI( $this->core );
583 // $finetunes = $openai->listModels();
584 // return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
585 // }
586 // catch ( Exception $e ) {
587 // $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
588 // return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
589 // }
590 // }
591
592 function rest_openai_deleted_finetunes_get() {
593 try {
594 $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null;
595 $legacy = isset( $_GET['legacy'] ) ? $_GET['legacy'] === 'true' : false;
596 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
597 $finetunes = $openai->list_deleted_finetunes( $legacy );
598 return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
599 }
600 catch ( Exception $e ) {
601 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
602 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
603 }
604 }
605
606 function rest_openai_finetunes_get() {
607 try {
608 $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null;
609 $legacy = isset( $_GET['legacy'] ) ? $_GET['legacy'] === 'true' : false;
610 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
611 $finetunes = $openai->list_finetunes( $legacy );
612 return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
613 }
614 catch ( Exception $e ) {
615 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
616 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
617 }
618 }
619
620 function rest_openai_files_upload( $request ) {
621 try {
622 $params = $request->get_json_params();
623 $envId = $params['envId'];;
624 $filename = sanitize_text_field( $params['filename'] );
625 $data = $params['data'];
626 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
627 $file = $openai->upload_file( $filename, $data );
628 return new WP_REST_Response([ 'success' => true, 'file' => $file ], 200 );
629 }
630 catch ( Exception $e ) {
631 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
632 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
633 }
634 }
635
636 function rest_openai_files_delete( $request ) {
637 try {
638 $params = $request->get_json_params();
639 $envId = $params['envId'];;
640 $fileId = $params['fileId'];
641 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
642 $openai->delete_file( $fileId );
643 return new WP_REST_Response([ 'success' => true ], 200 );
644 }
645 catch ( Exception $e ) {
646 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
647 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
648 }
649 }
650
651 function rest_openai_finetunes_cancel( $request ) {
652 try {
653 $params = $request->get_json_params();
654 $envId = $params['envId'];;
655 $finetuneId = $params['finetuneId'];
656 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
657 $openai->cancel_finetune( $finetuneId );
658 return new WP_REST_Response([ 'success' => true ], 200 );
659 }
660 catch ( Exception $e ) {
661 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
662 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
663 }
664 }
665
666 function rest_openai_finetunes_delete( $request ) {
667 try {
668 $params = $request->get_json_params();
669 $envId = $params['envId'];;
670 $modelId = $params['modelId'];
671 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
672 $openai->delete_finetune( $modelId );
673 return new WP_REST_Response([ 'success' => true ], 200 );
674 }
675 catch ( Exception $e ) {
676 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
677 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
678 }
679 }
680
681 function rest_openai_files_download( $request ) {
682 try {
683 $params = $request->get_json_params();
684 $envId = $params['envId'];;
685 $fileId = $params['fileId'];
686 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
687 $data = $openai->download_file( $fileId );
688 return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 );
689 }
690 catch ( Exception $e ) {
691 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
692 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
693 }
694 }
695
696 function rest_openai_files_finetune( $request ) {
697 try {
698 $params = $request->get_json_params();
699 $envId = $params['envId'];;
700 $fileId = $params['fileId'];
701 $model = $params['model'];
702 $suffix = $params['suffix'];
703 $hyperparams = [
704 "nEpochs" => $params['nEpochs'],
705 "batchSize" => $params['batchSize']
706 ];
707 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
708 $finetune = $openai->run_finetune( $fileId, $model, $suffix, $hyperparams );
709 return new WP_REST_Response([ 'success' => true, 'finetune' => $finetune ], 200 );
710 }
711 catch ( Exception $e ) {
712 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
713 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
714 }
715 }
716
717 function rest_openai_incidents() {
718 try {
719 $transient = get_transient( 'mwai_openai_incidents' );
720 if ( $transient ) {
721 return new WP_REST_Response([ 'success' => true, 'incidents' => $transient ], 200 );
722 }
723 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core );
724 $incidents = $openai->get_incidents();
725 set_transient( 'mwai_openai_incidents', $incidents, 60 * 10 );
726 return new WP_REST_Response([ 'success' => true, 'incidents' => $incidents ], 200 );
727 }
728 catch ( Exception $e ) {
729 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
730 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
731 }
732 }
733
734 function rest_helpers_count_posts( $request ) {
735 try {
736 $params = $request->get_query_params();
737 $postType = $params['postType'];
738 $postStatus = $params['postStatus'];
739 $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $postStatus ) : [ 'publish' ];
740 $count = wp_count_posts( $postType );
741 $count = array_sum( array_intersect_key( (array)$count, array_flip( $postStatus ) ) );
742 return new WP_REST_Response([ 'success' => true, 'count' => $count ], 200 );
743 }
744 catch ( Exception $e ) {
745 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
746 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
747 }
748 }
749
750 function rest_helpers_post_content( $request ) {
751 try {
752 $params = $request->get_query_params();
753 $offset = (int)$params['offset'];
754 $postType = $params['postType'];
755 $postStatus = $params['postStatus'];
756 $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $postStatus ) : [ 'publish' ];
757 $postId = (int)$params['postId'];
758
759 $post = null;
760 if ( !empty( $postId ) ) {
761 $post = get_post( $postId );
762 if ( $post->post_status !== 'publish' && $post->post_status !== 'future'
763 && $post->post_status !== 'draft' && $post->post_status !== 'private' ) {
764 $post = null;
765 }
766 }
767 else {
768 $posts = get_posts( [
769 'posts_per_page' => 1,
770 'post_type' => $postType,
771 'offset' => $offset,
772 'post_status' => $postStatus,
773 ] );
774 $post = count( $posts ) === 0 ? null : $posts[0];
775 }
776 if ( !$post ) {
777 return new WP_REST_Response([ 'success' => false, 'message' => 'Post not found' ], 404 );
778 }
779 $cleanPost = $this->core->get_post( $post );
780 return new WP_REST_Response([ 'success' => true, 'content' => $cleanPost['content'],
781 'checksum' => $cleanPost['checksum'], 'language' => $cleanPost['language'], 'excerpt' => $cleanPost['excerpt'],
782 'postId' => $cleanPost['postId'], 'title' => $cleanPost['title'], 'url' => $cleanPost['url'] ], 200 );
783 }
784 catch ( Exception $e ) {
785 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
786 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
787 }
788 }
789
790 function rest_system_templates_get( $request ) {
791 try {
792 $params = $request->get_query_params();
793 $category = $params['category'];
794 $templates = [];
795 $templates_option = get_option( 'mwai_templates', [] );
796 if ( !is_array( $templates_option ) ) {
797 update_option( 'mwai_templates', [] );
798 }
799 $categories = array_column( $templates_option, 'category' );
800 $index = array_search( $category, $categories );
801 $templates = [];
802 if ( $index !== false ) {
803 $templates = $templates_option[$index]['templates'];
804 }
805 return new WP_REST_Response([ 'success' => true, 'templates' => $templates ], 200 );
806 }
807 catch ( Exception $e ) {
808 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
809 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
810 }
811 }
812
813 function rest_system_templates_save( $request ) {
814 try {
815 $params = $request->get_json_params();
816 $category = $params['category'];
817 $templates = $params['templates'];
818 $templates_option = get_option( 'mwai_templates', [] );
819 $categories = array_column( $templates_option, 'category' );
820 $index = array_search( $category, $categories );
821 if ( $index !== false && $index >= 0 ) {
822 $templates_option[$index]['templates'] = $templates;
823 }
824 else {
825 $group = [ 'category' => $category, 'templates' => $templates ];
826 $templates_option[] = $group;
827 }
828
829 update_option( 'mwai_templates', $templates_option );
830 return new WP_REST_Response([ 'success' => true ], 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_logs_list( $request ) {
839 try {
840 $params = $request->get_json_params();
841 $offset = $params['offset'];
842 $limit = $params['limit'];
843 $filters = $params['filters'];
844 $sort = $params['sort'];
845 $logs = apply_filters( 'mwai_stats_logs', [], $offset, $limit, $filters, $sort );
846 return new WP_REST_Response([ 'success' => true, 'total' => $logs['total'], 'logs' => $logs['rows'] ], 200 );
847 }
848 catch ( Exception $e ) {
849 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
850 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
851 }
852 }
853
854 function rest_system_logs_delete( $request ) {
855 try {
856 $params = $request->get_json_params();
857 $logIds = $params['logIds'];
858 $success = apply_filters( 'mwai_stats_logs_delete', true, $logIds );
859 return new WP_REST_Response([ 'success' => $success ], 200 );
860 }
861 catch ( Exception $e ) {
862 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
863 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
864 }
865 }
866
867 function rest_system_logs_meta_get( $request ) {
868 try {
869 $params = $request->get_json_params();
870 $logId = $params['logId'];
871 $metaKeys = $params['metaKeys'];
872 $data = apply_filters( 'mwai_stats_logs_meta', [], $logId, $metaKeys );
873 return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 );
874 }
875 catch ( Exception $e ) {
876 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
877 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
878 }
879 }
880
881 function rest_ai_moderate( $request ) {
882 try {
883 $params = $request->get_json_params();
884 $envId = $params['envId'];
885 $text = $params['text'];
886 if ( !$text ) {
887 return new WP_REST_Response([ 'success' => false, 'message' => 'Text not found.' ], 404 );
888 }
889 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
890 $results = $openai->moderate( $text );
891 return new WP_REST_Response([ 'success' => true, 'results' => $results ], 200 );
892 }
893 catch ( Exception $e ) {
894 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
895 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
896 }
897 }
898
899 function rest_ai_transcribe_audio( $request ) {
900 try {
901 $params = $request->get_json_params();
902 $query = new Meow_MWAI_Query_Transcribe();
903 $query->inject_params( $params );
904 $query->set_scope('admin-tools');
905 $reply = $this->core->run_query( $query );
906 return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 );
907 }
908 catch ( Exception $e ) {
909 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
910 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
911 }
912 }
913
914 function rest_ai_transcribe_image( $request ) {
915 try {
916 global $mwai;
917 $params = $request->get_json_params();
918 $message = !empty( $params['prompt'] ) ? $params['prompt'] : null;
919 $url = !empty( $params['url'] ) ? $params['url'] : null;
920 $path = !empty( $params['path'] ) ? $params['path'] : null;
921 $result = $mwai->simpleVisionQuery( $message, $url, $path );
922 return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 );
923 }
924 catch ( Exception $e ) {
925 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
926 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
927 }
928 }
929
930 function rest_ai_json( $request ) {
931 try {
932 global $mwai;
933 $params = $request->get_json_params();
934 $message = !empty( $params['prompt'] ) ? $params['prompt'] : null;
935 $result = $mwai->simpleJsonQuery( $message );
936 return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 );
937 }
938 catch ( Exception $e ) {
939 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
940 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
941 }
942 }
943
944 function rest_helpers_post_types() {
945 try {
946 $postTypes = $this->core->get_post_types();
947 return new WP_REST_Response([ 'success' => true, 'postTypes' => $postTypes ], 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_settings_themes( $request ) {
956 try {
957 $method = $request->get_method();
958 if ( $method === 'GET' ) {
959 $themes = $this->core->get_themes();
960 return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 );
961 }
962 else if ( $method === 'POST' ) {
963 $params = $request->get_json_params();
964 $themes = $params['themes'];
965 $themes = $this->core->update_themes( $themes );
966 return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 );
967 }
968 }
969 catch ( Exception $e ) {
970 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
971 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
972 }
973 }
974
975 function rest_settings_chatbots( $request ) {
976 try {
977 $method = $request->get_method();
978 if ( $method === 'GET' ) {
979 $chatbots = $this->core->get_chatbots();
980 return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 );
981 }
982 else if ( $method === 'POST' ) {
983 $params = $request->get_json_params();
984 $chatbots = $params['chatbots'];
985 $chatbots = $this->core->update_chatbots( $chatbots );
986 return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 );
987 }
988 return new WP_REST_Response([ 'success' => false, 'message' => 'Method not allowed' ], 405 );
989 }
990 catch ( Exception $e ) {
991 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
992 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
993 }
994 }
995 }
996