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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 1 year ago modules 1 year ago queries 1 year ago admin.php 2 years ago api.php 1 year ago core.php 1 year ago init.php 2 years ago logging.php 1 year ago reply.php 1 year ago rest.php 1 year ago
rest.php
990 lines
1 <?php
2
3 class Meow_MWAI_Rest
4 {
5 private $core = null;
6 private $namespace = 'mwai/v1';
7
8 public function __construct( $core ) {
9 $this->core = $core;
10 add_action( 'rest_api_init', array( $this, 'rest_init' ) );
11 }
12
13 /**
14 * Retrieve the message from the parameters and optionally sanitize it.
15 *
16 * @param array &$params The parameters array, passed by reference.
17 * @param bool $sanitize Whether to sanitize the message using sanitize_text_field.
18 * @return string The retrieved (and optionally sanitized) message.
19 */
20 function retrieve_message( &$params, $sanitize = false ) : string {
21 if ( isset( $params['message'] ) ) {
22 $message = $params['message'];
23 }
24 elseif ( isset( $params['prompt'] ) ) {
25 $message = $params['prompt'];
26 unset( $params['prompt'] );
27 $params['message'] = $message;
28 Meow_MWAI_Logging::deprecated( '"prompt" is deprecated, please use "message" instead.' );
29 }
30 else {
31 $message = "";
32 }
33
34 if ( $sanitize ) {
35 $message = sanitize_text_field( $message );
36 }
37
38 return $message;
39 }
40
41 function rest_init() {
42 try {
43 // Session Endpoint
44 register_rest_route( $this->namespace, '/start_session', array(
45 'methods' => 'POST',
46 'permission_callback' => [ $this->core, 'can_start_session' ],
47 'callback' => [ $this, 'rest_start_session' ],
48 ) );
49
50 // Settings Endpoints
51 register_rest_route( $this->namespace, '/settings/update', array(
52 'methods' => 'POST',
53 'permission_callback' => [ $this->core, 'can_access_settings' ],
54 'callback' => [ $this, 'rest_settings_update' ],
55 ) );
56 register_rest_route( $this->namespace, '/settings/options', array(
57 'methods' => 'GET',
58 'permission_callback' => [ $this->core, 'can_access_settings' ],
59 'callback' => [ $this, 'rest_settings_list' ],
60 ) );
61 register_rest_route( $this->namespace, '/settings/reset', array(
62 'methods' => 'POST',
63 'permission_callback' => [ $this->core, 'can_access_settings' ],
64 'callback' => [ $this, 'rest_settings_reset' ],
65 ) );
66 register_rest_route( $this->namespace, '/settings/chatbots', array(
67 'methods' => ['GET', 'POST'],
68 'permission_callback' => [ $this->core, 'can_access_settings' ],
69 'callback' => [ $this, 'rest_settings_chatbots' ],
70 ) );
71 register_rest_route( $this->namespace, '/settings/themes', array(
72 'methods' => ['GET', 'POST'],
73 'permission_callback' => [ $this->core, 'can_access_settings' ],
74 'callback' => [ $this, 'rest_settings_themes' ],
75 ) );
76
77 // System Endpoints
78 register_rest_route( $this->namespace, '/system/logs/list', array(
79 'methods' => 'POST',
80 'permission_callback' => [ $this->core, 'can_access_settings' ],
81 'callback' => [ $this, 'rest_system_logs_list' ],
82 ) );
83 register_rest_route( $this->namespace, '/system/logs/delete', array(
84 'methods' => 'POST',
85 'permission_callback' => [ $this->core, 'can_access_settings' ],
86 'callback' => [ $this, 'rest_system_logs_delete' ],
87 ) );
88 register_rest_route( $this->namespace, '/system/logs/meta', array(
89 'methods' => 'POST',
90 'permission_callback' => [ $this->core, 'can_access_settings' ],
91 'callback' => [ $this, 'rest_system_logs_meta_get' ],
92 ) );
93 register_rest_route( $this->namespace, '/system/templates', array(
94 'methods' => 'POST',
95 'permission_callback' => [ $this->core, 'can_access_features' ],
96 'callback' => [ $this, 'rest_system_templates_save' ],
97 ) );
98 register_rest_route( $this->namespace, '/system/templates', array(
99 'methods' => 'GET',
100 'permission_callback' => [ $this->core, 'can_access_features' ],
101 'callback' => [ $this, 'rest_system_templates_get' ],
102 ) );
103
104 // AI Endpoints
105 register_rest_route( $this->namespace, '/ai/models', array(
106 'methods' => 'POST',
107 'permission_callback' => [ $this->core, 'can_access_features' ],
108 'callback' => [ $this, 'rest_ai_models' ],
109 ) );
110 register_rest_route( $this->namespace, '/ai/completions', array(
111 'methods' => 'POST',
112 'permission_callback' => [ $this->core, 'can_access_features' ],
113 'callback' => [ $this, 'rest_ai_completions' ],
114 ) );
115 register_rest_route( $this->namespace, '/ai/images', array(
116 'methods' => 'POST',
117 'permission_callback' => [ $this->core, 'can_access_features' ],
118 'callback' => [ $this, 'rest_ai_images' ],
119 ) );
120 register_rest_route( $this->namespace, '/ai/copilot', array(
121 'methods' => 'POST',
122 'permission_callback' => [ $this->core, 'can_access_features' ],
123 'callback' => [ $this, 'rest_ai_copilot' ],
124 ) );
125
126 register_rest_route( $this->namespace, '/ai/magic_wand', array(
127 'methods' => 'POST',
128 'callback' => [ $this, 'rest_ai_magic_wand' ],
129 'permission_callback' => [ $this->core, 'can_access_features' ],
130 ) );
131 register_rest_route( $this->namespace, '/ai/moderate', array(
132 'methods' => 'POST',
133 'permission_callback' => [ $this->core, 'can_access_settings' ],
134 'callback' => [ $this, 'rest_ai_moderate' ],
135 ) );
136 register_rest_route( $this->namespace, '/ai/transcribe_audio', array(
137 'methods' => 'POST',
138 'permission_callback' => [ $this->core, 'can_access_settings' ],
139 'callback' => [ $this, 'rest_ai_transcribe_audio' ],
140 ) );
141 register_rest_route( $this->namespace, '/ai/transcribe_image', array(
142 'methods' => 'POST',
143 'permission_callback' => [ $this->core, 'can_access_settings' ],
144 'callback' => [ $this, 'rest_ai_transcribe_image' ],
145 ) );
146 register_rest_route( $this->namespace, '/ai/json', array(
147 'methods' => 'POST',
148 'permission_callback' => [ $this->core, 'can_access_settings' ],
149 'callback' => [ $this, 'rest_ai_json' ],
150 ) );
151
152 // Helpers Endpoints
153 register_rest_route( $this->namespace, '/helpers/update_post_title', array(
154 'methods' => 'POST',
155 'permission_callback' => [ $this->core, 'can_access_features' ],
156 'callback' => [ $this, 'rest_helpers_update_title' ],
157 ) );
158 register_rest_route( $this->namespace, '/helpers/update_post_excerpt', array(
159 'methods' => 'POST',
160 'permission_callback' => [ $this->core, 'can_access_features' ],
161 'callback' => [ $this, 'rest_helpers_update_excerpt' ],
162 ) );
163 register_rest_route( $this->namespace, '/helpers/create_post', array(
164 'methods' => 'POST',
165 'permission_callback' => [ $this->core, 'can_access_features' ],
166 'callback' => [ $this, 'rest_helpers_create_post' ],
167 ) );
168 register_rest_route( $this->namespace, '/helpers/create_image', array(
169 'methods' => 'POST',
170 'permission_callback' => [ $this->core, 'can_access_features' ],
171 'callback' => [ $this, 'rest_helpers_create_images' ],
172 ) );
173 register_rest_route( $this->namespace, '/helpers/count_posts', array(
174 'methods' => 'GET',
175 'permission_callback' => [ $this->core, 'can_access_features' ],
176 'callback' => [ $this, 'rest_helpers_count_posts' ],
177 ) );
178 register_rest_route( $this->namespace, '/helpers/posts_ids', array(
179 'methods' => 'GET',
180 'permission_callback' => [ $this->core, 'can_access_features' ],
181 'callback' => [ $this, 'rest_helpers_posts_ids' ],
182 ) );
183 register_rest_route( $this->namespace, '/helpers/post_types', array(
184 'methods' => 'GET',
185 'permission_callback' => [ $this->core, 'can_access_features' ],
186 'callback' => [ $this, 'rest_helpers_post_types' ],
187 ) );
188 register_rest_route( $this->namespace, '/helpers/post_content', array(
189 'methods' => 'GET',
190 'permission_callback' => [ $this->core, 'can_access_features' ],
191 'callback' => [ $this, 'rest_helpers_post_content' ],
192 ) );
193
194 // OpenAI Endpoints
195 register_rest_route( $this->namespace, '/openai/files/list', array(
196 'methods' => 'GET',
197 'permission_callback' => [ $this->core, 'can_access_settings' ],
198 'callback' => [ $this, 'rest_openai_files_get' ],
199 ) );
200 register_rest_route( $this->namespace, '/openai/files/upload', array(
201 'methods' => 'POST',
202 'permission_callback' => [ $this->core, 'can_access_settings' ],
203 'callback' => [ $this, 'rest_openai_files_upload' ],
204 ) );
205 register_rest_route( $this->namespace, '/openai/files/delete', array(
206 'methods' => 'POST',
207 'permission_callback' => [ $this->core, 'can_access_settings' ],
208 'callback' => [ $this, 'rest_openai_files_delete' ],
209 ) );
210 register_rest_route( $this->namespace, '/openai/files/download', array(
211 'methods' => 'POST',
212 'permission_callback' => [ $this->core, 'can_access_settings' ],
213 'callback' => [ $this, 'rest_openai_files_download' ],
214 ) );
215 register_rest_route( $this->namespace, '/openai/files/finetune', array(
216 'methods' => 'POST',
217 'permission_callback' => [ $this->core, 'can_access_settings' ],
218 'callback' => [ $this, 'rest_openai_files_finetune' ],
219 ) );
220 register_rest_route( $this->namespace, '/openai/finetunes/list_deleted', array(
221 'methods' => 'GET',
222 'permission_callback' => [ $this->core, 'can_access_settings' ],
223 'callback' => [ $this, 'rest_openai_deleted_finetunes_get' ],
224 ) );
225
226 // register_rest_route( $this->namespace, '/openai/models', array(
227 // 'methods' => 'GET',
228 // 'permission_callback' => [ $this->core, 'can_access_settings' ],
229 // 'callback' => [ $this, 'rest_openai_models_get' ],
230 // ) );
231
232 register_rest_route( $this->namespace, '/openai/finetunes/list', array(
233 'methods' => 'GET',
234 'permission_callback' => [ $this->core, 'can_access_settings' ],
235 'callback' => [ $this, 'rest_openai_finetunes_get' ],
236 ) );
237 register_rest_route( $this->namespace, '/openai/finetunes/delete', array(
238 'methods' => 'POST',
239 'permission_callback' => [ $this->core, 'can_access_settings' ],
240 'callback' => [ $this, 'rest_openai_finetunes_delete' ],
241 ) );
242 register_rest_route( $this->namespace, '/openai/finetunes/cancel', array(
243 'methods' => 'POST',
244 'permission_callback' => [ $this->core, 'can_access_settings' ],
245 'callback' => [ $this, 'rest_openai_finetunes_cancel' ],
246 ) );
247 register_rest_route( $this->namespace, '/openai/incidents', array(
248 'methods' => 'GET',
249 'permission_callback' => [ $this->core, 'can_access_settings' ],
250 'callback' => [ $this, 'rest_openai_incidents' ],
251 ) );
252
253 // Logging Endpoints
254 register_rest_route( $this->namespace, '/get_logs', array(
255 'methods' => 'GET',
256 'permission_callback' => array( $this->core, 'can_access_features' ),
257 'callback' => array( $this, 'rest_get_logs' )
258 ) );
259 register_rest_route( $this->namespace, '/clear_logs', array(
260 'methods' => 'GET',
261 'permission_callback' => array( $this->core, 'can_access_features' ),
262 'callback' => array( $this, 'rest_clear_logs' )
263 ) );
264 }
265 catch ( Exception $e ) {
266 var_dump( $e );
267 }
268 }
269
270 function rest_start_session() {
271 try {
272 $sessionId = $this->core->get_session_id();
273 $restNonce = $this->core->get_nonce( true );
274 return new WP_REST_Response( [
275 'success' => true,
276 'sessionId' => $sessionId,
277 'restNonce' => $restNonce
278 ], 200 );
279 }
280 catch ( Exception $e ) {
281 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
282 return new WP_REST_Response( [ 'success' => false, 'message' => $message ], 500 );
283 }
284 }
285
286 function rest_settings_list() {
287 return new WP_REST_Response( [
288 'success' => true,
289 'options' => $this->core->get_all_options()
290 ], 200 );
291 }
292
293 function rest_settings_update( $request ) {
294 try {
295 $params = $request->get_json_params();
296 $value = $params['options'];
297 $options = $this->core->update_options( $value );
298 $success = !!$options;
299 $message = __( $success ? 'OK' : "Could not update options.", 'ai-engine' );
300 return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 );
301 }
302 catch ( Exception $e ) {
303 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
304 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
305 }
306 }
307
308 function rest_settings_reset() {
309 try {
310 $options = $this->core->reset_options();
311 $success = !!$options;
312 $message = __( $success ? 'OK' : "Could not reset options.", 'ai-engine' );
313 return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 );
314 }
315 catch ( Exception $e ) {
316 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
317 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
318 }
319 }
320
321 function rest_ai_models( $request ) {
322 try {
323 $params = $request->get_json_params();
324 $envId = $params['envId'];
325 $engine = Meow_MWAI_Engines_Factory::get( $this->core, $envId );
326 $models = $engine->retrieve_models();
327 return new WP_REST_Response([ 'success' => true, 'models' => $models ], 200 );
328 }
329 catch ( Exception $e ) {
330 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
331 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
332 }
333 }
334
335 function rest_ai_completions( $request ) {
336 try {
337 $params = $request->get_json_params();
338 $message = $this->retrieve_message( $params );
339 $query = new Meow_MWAI_Query_Text( $message );
340 $query->inject_params( $params );
341
342 // Handle streaming
343 $stream = $params['stream'] ?? false;
344 $streamCallback = null;
345 if ( $stream ) {
346 $streamCallback = function( $reply ) {
347 //$raw = _wp_specialchars( $reply, ENT_NOQUOTES, 'UTF-8', true );
348 $raw = $reply;
349 $this->core->stream_push( [ 'type' => 'live', 'data' => $raw ] );
350 if ( ob_get_level() > 0 ) {
351 ob_flush();
352 }
353 flush();
354 };
355 header( 'Cache-Control: no-cache' );
356 header( 'Content-Type: text/event-stream' );
357 header( 'X-Accel-Buffering: no' ); // This is useful to disable buffering in nginx through headers.
358 ob_implicit_flush( true );
359 ob_end_flush();
360 }
361
362 // Process Reply
363 $reply = $this->core->run_query( $query, $streamCallback );
364 $restRes = [
365 'success' => true,
366 'data' => $reply->result,
367 'usage' => $reply->usage
368 ];
369 if ( $stream ) {
370 $this->core->stream_push( [ 'type' => 'end', 'data' => json_encode( $restRes ) ] );
371 die();
372 }
373 return new WP_REST_Response( $restRes, 200 );
374 }
375 catch ( Exception $e ) {
376 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
377 if ( $stream ) {
378 $this->core->stream_push( [ 'type' => 'error', 'data' => $message ] );
379 }
380 else {
381 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
382 }
383 }
384 }
385
386 function rest_ai_images( $request ) {
387 try {
388 $params = $request->get_json_params();
389 $message = $this->retrieve_message( $params );
390 $query = new Meow_MWAI_Query_Image( $message );
391 $query->inject_params( $params );
392 $reply = $this->core->run_query( $query );
393 return new WP_REST_Response([ 'success' => true, 'data' => $reply->results, 'usage' => $reply->usage ], 200 );
394 }
395 catch ( Exception $e ) {
396 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
397 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
398 }
399 }
400
401 function rest_ai_magic_wand( $request ) {
402 try {
403 $params = $request->get_json_params();
404 $action = isset( $params['action'] ) ? $params['action'] : null;
405 $data = isset( $params['data'] ) ? $params['data'] : null;
406 if ( empty( $data ) || empty( $action ) ) {
407 return new WP_REST_Response([ 'success' => false, 'message' => "An action and some data are required." ], 500 );
408 }
409 $data = apply_filters( 'mwai_magic_wand_' . $action, "", $data );
410 return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 );
411 }
412 catch ( Exception $e ) {
413 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
414 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
415 }
416 }
417
418 function rest_ai_copilot( $request ) {
419 try {
420 $params = $request->get_json_params();
421 $action = sanitize_text_field( $params['action'] );
422 $message = $this->retrieve_message( $params, true );
423 $context = sanitize_text_field( $params['context'] );
424 $postId = !empty( $params['postId'] ) ? intval( $params['postId'] ) : null;
425 if ( empty( $action ) || empty( $message ) ) {
426 return new WP_REST_Response([ 'success' => false, 'message' => "Copilot needs an action and a prompt." ], 500 );
427 }
428
429 global $mwai;
430 $result = null;
431 $params = [ 'scope' => 'copilot' ];
432
433 if ( $action === 'text' ) {
434 $prompt = "Here is the current article: \n\n===\n\n" . $context . "\n\n===\n\nIn this article, instead of the [== CURRENT BLOCK ==] placeholder, the author needs additional content. This new content should use the same tone, style, context, it should naturally flow in the article. The author shared additional information for this request:\n\n===\n\n" . $message . "\n\n===\n\nPlease provide the additional content. Only output the additional content, not the entire article, no need for extra information, and no need for the placeholders. Only output the content that should be added.";
435 if ( !empty( $model ) ) {
436 $params['model'] = $model;
437 }
438 $result = $mwai->simpleTextQuery( $prompt, $params );
439 }
440 else if ( $action === 'image' ) {
441 $prompt = "Here is the current article: \n\n===\n\n" . $context . "\n\n===\n\nIn this article, instead of the [== CURRENT BLOCK ==] placeholder, the author needs an image. Please write a detailed description (prompt) for that image that would fit this context. The image should be relevant to the article. The author shared additional information for this request:\n\n===\n\n" . $message . "\n\n===\n\nPlease only output the description for the image, not the entire article, no need for extra information, and no need for the placeholders. Only output the description.";
442
443 // Create the image
444 $simplifiedPrompt = $mwai->simpleTextQuery( $prompt, $params );
445 $media = $mwai->imageQueryForMediaLibrary( $simplifiedPrompt, $params, $postId );
446 $result = [ 'media' => $media ];
447 }
448 return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 );
449 }
450 catch ( Exception $e ) {
451 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
452 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
453 }
454 }
455
456 function rest_helpers_update_title( $request ) {
457 try {
458 $params = $request->get_json_params();
459 $title = sanitize_text_field( $params['title'] );
460 $postId = intval( $params['postId'] );
461 $post = get_post( $postId );
462 if ( !$post ) {
463 throw new Exception( 'There is no post with this ID.' );
464 }
465 $post->post_title = $title;
466 //$post->post_name = sanitize_title( $title );
467 wp_update_post( $post );
468 return new WP_REST_Response([ 'success' => true, 'message' => "Title updated." ], 200 );
469 }
470 catch ( Exception $e ) {
471 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
472 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
473 }
474 }
475
476 function rest_helpers_update_excerpt( $request ) {
477 try {
478 $params = $request->get_json_params();
479 $excerpt = sanitize_text_field( $params['excerpt'] );
480 $postId = intval( $params['postId'] );
481 $post = get_post( $postId );
482 if ( !$post ) {
483 throw new Exception( 'There is no post with this ID.' );
484 }
485 $post->post_excerpt = $excerpt;
486 wp_update_post( $post );
487 return new WP_REST_Response([ 'success' => true, 'message' => "Excerpt updated." ], 200 );
488 }
489 catch ( Exception $e ) {
490 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
491 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
492 }
493 }
494
495 function rest_helpers_create_post( $request ) {
496 try {
497 $params = $request->get_json_params();
498 $title = sanitize_text_field( $params['title'] );
499 $content = sanitize_textarea_field( $params['content'] );
500 $excerpt = sanitize_text_field( $params['excerpt'] );
501 $postType = sanitize_text_field( $params['postType'] );
502 $post = new stdClass();
503 $post->post_title = $title;
504 $post->post_excerpt = $excerpt;
505 $post->post_content = $content;
506 $post->post_status = 'draft';
507 $post->post_type = isset( $postType ) ? $postType : 'post';
508 // TODO: Let's try to avoid using Markdown to create the Post
509 // Instead, we should create Gutenberg Blocks, or simple HTML.
510 // Then, we can get rid of the library for Markdown.
511 $post->post_content = $this->core->markdown_to_html( $post->post_content );
512 $postId = wp_insert_post( $post );
513 return new WP_REST_Response([ 'success' => true, 'postId' => $postId ], 200 );
514 }
515 catch ( Exception $e ) {
516 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
517 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
518 }
519 }
520
521 function rest_helpers_create_images( $request ) {
522 try {
523 $params = $request->get_json_params();
524 $title = sanitize_text_field( $params['title'] );
525 $caption = sanitize_text_field( $params['caption'] );
526 $alt = sanitize_text_field( $params['alt'] );
527 $description = sanitize_text_field( $params['description'] );
528 $url = $params['url'];
529 $filename = sanitize_text_field( $params['filename'] );
530 $attachmentId = $this->core->add_image_from_url( $url, $filename, $title, $description, $caption, $alt );
531 return new WP_REST_Response([ 'success' => true, 'attachmentId' => $attachmentId ], 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_openai_files_get() {
540 try {
541 $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null;
542 $purposeFilter = isset( $_GET['purpose'] ) ? $_GET['purpose'] : null;
543 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
544 $files = $openai->list_files( $purposeFilter );
545 return new WP_REST_Response([ 'success' => true, 'files' => $files ], 200 );
546 }
547 catch ( Exception $e ) {
548 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
549 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
550 }
551 }
552
553 function rest_openai_deleted_finetunes_get() {
554 try {
555 $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null;
556 $legacy = isset( $_GET['legacy'] ) ? $_GET['legacy'] === 'true' : false;
557 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
558 $finetunes = $openai->list_deleted_finetunes( $legacy );
559 return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 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_finetunes_get() {
568 try {
569 $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null;
570 $legacy = isset( $_GET['legacy'] ) ? $_GET['legacy'] === 'true' : false;
571 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
572 $finetunes = $openai->list_finetunes( $legacy );
573 return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 );
574 }
575 catch ( Exception $e ) {
576 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
577 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
578 }
579 }
580
581 function rest_openai_files_upload( $request ) {
582 try {
583 $params = $request->get_json_params();
584 $envId = $params['envId'];;
585 $filename = sanitize_text_field( $params['filename'] );
586 $data = $params['data'];
587 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
588 $file = $openai->upload_file( $filename, $data );
589 return new WP_REST_Response([ 'success' => true, 'file' => $file ], 200 );
590 }
591 catch ( Exception $e ) {
592 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
593 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
594 }
595 }
596
597 function rest_openai_files_delete( $request ) {
598 try {
599 $params = $request->get_json_params();
600 $envId = $params['envId'];;
601 $fileId = $params['fileId'];
602 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
603 $openai->delete_file( $fileId );
604 return new WP_REST_Response([ 'success' => true ], 200 );
605 }
606 catch ( Exception $e ) {
607 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
608 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
609 }
610 }
611
612 function rest_openai_finetunes_cancel( $request ) {
613 try {
614 $params = $request->get_json_params();
615 $envId = $params['envId'];;
616 $finetuneId = $params['finetuneId'];
617 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
618 $openai->cancel_finetune( $finetuneId );
619 return new WP_REST_Response([ 'success' => true ], 200 );
620 }
621 catch ( Exception $e ) {
622 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
623 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
624 }
625 }
626
627 function rest_openai_finetunes_delete( $request ) {
628 try {
629 $params = $request->get_json_params();
630 $envId = $params['envId'];;
631 $modelId = $params['modelId'];
632 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
633 $openai->delete_finetune( $modelId );
634 return new WP_REST_Response([ 'success' => true ], 200 );
635 }
636 catch ( Exception $e ) {
637 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
638 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
639 }
640 }
641
642 function rest_openai_files_download( $request ) {
643 try {
644 $params = $request->get_json_params();
645 $envId = $params['envId'];;
646 $fileId = $params['fileId'];
647 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
648 $filename = $openai->download_file( $fileId );
649 $data = file_get_contents( $filename );
650 return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 );
651 }
652 catch ( Exception $e ) {
653 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
654 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
655 }
656 }
657
658 function rest_openai_files_finetune( $request ) {
659 try {
660 $params = $request->get_json_params();
661 $envId = $params['envId'];;
662 $fileId = $params['fileId'];
663 $model = $params['model'];
664 $suffix = $params['suffix'];
665 $hyperparams = [
666 "nEpochs" => isset( $params['nEpochs'] ) ? $params['nEpochs'] : null,
667 "batchSize" => isset( $params['batchSize'] ) ? $params['batchSize'] : null,
668 ];
669 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
670 $finetune = $openai->run_finetune( $fileId, $model, $suffix, $hyperparams );
671 return new WP_REST_Response([ 'success' => true, 'finetune' => $finetune ], 200 );
672 }
673 catch ( Exception $e ) {
674 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
675 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
676 }
677 }
678
679 function rest_openai_incidents() {
680 try {
681 $transient = get_transient( 'mwai_openai_incidents' );
682 if ( $transient ) {
683 return new WP_REST_Response([ 'success' => true, 'incidents' => $transient ], 200 );
684 }
685 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core );
686 $incidents = $openai->get_incidents();
687 set_transient( 'mwai_openai_incidents', $incidents, 60 * 10 );
688 return new WP_REST_Response([ 'success' => true, 'incidents' => $incidents ], 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_helpers_count_posts( $request ) {
697 try {
698 $params = $request->get_query_params();
699 $postType = $params['postType'];
700 $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $params['postStatus'] ) : [ 'publish' ];
701 $count = wp_count_posts( $postType );
702 $count = array_sum( array_intersect_key( (array)$count, array_flip( $postStatus ) ) );
703 return new WP_REST_Response([ 'success' => true, 'count' => $count ], 200 );
704 }
705 catch ( Exception $e ) {
706 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
707 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
708 }
709 }
710
711 function rest_helpers_posts_ids( $request ) {
712 try {
713 $params = $request->get_query_params();
714 $postType = $params['postType'];
715 $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $params['postStatus'] ) : [ 'publish' ];
716 $posts = get_posts( [
717 'posts_per_page' => -1,
718 'post_type' => $postType,
719 'post_status' => $postStatus,
720 'fields' => 'ids'
721 ] );
722 return new WP_REST_Response([ 'success' => true, 'postIds' => $posts ], 200 );
723 }
724 catch ( Exception $e ) {
725 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
726 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
727 }
728 }
729
730 function rest_helpers_post_content( $request ) {
731 try {
732 $params = $request->get_query_params();
733 $offset = (int)$params['offset'];
734 $postType = $params['postType'];
735 $postStatus = isset( $params['postStatus'] ) ? explode( ',', $params['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_list', [], $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 = Meow_MWAI_Engines_Factory::get_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_scope('admin-tools');
884 $reply = $this->core->run_query( $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 $message = $this->retrieve_message( $params );
898 $url = !empty( $params['url'] ) ? $params['url'] : null;
899 // This could lead to a security issue, so let's avoid using path directly.
900 //$path = !empty( $params['path'] ) ? $params['path'] : null;
901 $result = $mwai->simpleVisionQuery( $message, $url );
902 return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 );
903 }
904 catch ( Exception $e ) {
905 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
906 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
907 }
908 }
909
910 function rest_ai_json( $request ) {
911 try {
912 global $mwai;
913 $params = $request->get_json_params();
914 $message = $this->retrieve_message( $params );
915 $result = $mwai->simpleJsonQuery( $message );
916 return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 );
917 }
918 catch ( Exception $e ) {
919 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
920 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
921 }
922 }
923
924 function rest_helpers_post_types() {
925 try {
926 $postTypes = $this->core->get_post_types();
927 return new WP_REST_Response([ 'success' => true, 'postTypes' => $postTypes ], 200 );
928 }
929 catch ( Exception $e ) {
930 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
931 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
932 }
933 }
934
935 function rest_settings_themes( $request ) {
936 try {
937 $method = $request->get_method();
938 if ( $method === 'GET' ) {
939 $themes = $this->core->get_themes();
940 return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 );
941 }
942 else if ( $method === 'POST' ) {
943 $params = $request->get_json_params();
944 $themes = $params['themes'];
945 $themes = $this->core->update_themes( $themes );
946 return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 );
947 }
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_chatbots( $request ) {
956 try {
957 $method = $request->get_method();
958 if ( $method === 'GET' ) {
959 $chatbots = $this->core->get_chatbots();
960 return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 );
961 }
962 else if ( $method === 'POST' ) {
963 $params = $request->get_json_params();
964 $chatbots = $params['chatbots'];
965 $chatbots = $this->core->update_chatbots( $chatbots );
966 return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 );
967 }
968 return new WP_REST_Response([ 'success' => false, 'message' => 'Method not allowed' ], 405 );
969 }
970 catch ( Exception $e ) {
971 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
972 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
973 }
974 }
975
976 #region Logs
977
978 function rest_get_logs() {
979 $logs = Meow_MWAI_Logging::get();
980 return new WP_REST_Response( [ 'success' => true, 'data' => $logs ], 200 );
981 }
982
983 function rest_clear_logs() {
984 Meow_MWAI_Logging::clear();
985 return new WP_REST_Response( [ 'success' => true ], 200 );
986 }
987
988 #endregion
989 }
990