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