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