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