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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 1 year ago api.php 1 year ago core.php 1 year ago discussion.php 1 year ago init.php 1 year ago logging.php 1 year ago reply.php 1 year ago rest.php 1 year ago
rest.php
984 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
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 ) use ( $query ) {
347 //$raw = _wp_specialchars( $reply, ENT_NOQUOTES, 'UTF-8', true );
348 $raw = $reply;
349 $this->core->stream_push( [ 'type' => 'live', 'data' => $raw ], $query );
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 ) ], $query );
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 ], $query );
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_helpers_count_posts( $request ) {
680 try {
681 $params = $request->get_query_params();
682 $postType = $params['postType'];
683 $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $params['postStatus'] ) : [ 'publish' ];
684 $count = wp_count_posts( $postType );
685 $count = array_sum( array_intersect_key( (array)$count, array_flip( $postStatus ) ) );
686 return new WP_REST_Response([ 'success' => true, 'count' => $count ], 200 );
687 }
688 catch ( Exception $e ) {
689 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
690 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
691 }
692 }
693
694 function rest_helpers_posts_ids( $request ) {
695 try {
696 $params = $request->get_query_params();
697 $postType = $params['postType'];
698 $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $params['postStatus'] ) : [ 'publish' ];
699 $posts = get_posts( [
700 'posts_per_page' => -1,
701 'post_type' => $postType,
702 'post_status' => $postStatus,
703 'fields' => 'ids'
704 ] );
705 return new WP_REST_Response([ 'success' => true, 'postIds' => $posts ], 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_post_content( $request ) {
714 try {
715 $params = $request->get_query_params();
716 $offset = (int)$params['offset'];
717 $postType = $params['postType'];
718 $postStatus = isset( $params['postStatus'] ) ? explode( ',', $params['postStatus'] ) : [ 'publish' ];
719 $postId = (int)$params['postId'];
720
721 $post = null;
722 if ( !empty( $postId ) ) {
723 $post = get_post( $postId );
724 if ( $post->post_status !== 'publish' && $post->post_status !== 'future'
725 && $post->post_status !== 'draft' && $post->post_status !== 'private' ) {
726 $post = null;
727 }
728 }
729 else {
730 $posts = get_posts( [
731 'posts_per_page' => 1,
732 'post_type' => $postType,
733 'offset' => $offset,
734 'post_status' => $postStatus,
735 ] );
736 $post = count( $posts ) === 0 ? null : $posts[0];
737 }
738 if ( !$post ) {
739 return new WP_REST_Response([ 'success' => false, 'message' => 'Post not found' ], 404 );
740 }
741 $cleanPost = $this->core->get_post( $post );
742 return new WP_REST_Response([ 'success' => true, 'content' => $cleanPost['content'],
743 'checksum' => $cleanPost['checksum'], 'language' => $cleanPost['language'], 'excerpt' => $cleanPost['excerpt'],
744 'postId' => $cleanPost['postId'], 'title' => $cleanPost['title'], 'url' => $cleanPost['url'] ], 200 );
745 }
746 catch ( Exception $e ) {
747 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
748 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
749 }
750 }
751
752 function rest_helpers_run_tasks( $request ) {
753 try {
754 do_action( 'mwai_tasks_run' );
755 return new WP_REST_Response([ 'success' => true ], 200 );
756 }
757 catch ( Exception $e ) {
758 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
759 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
760 }
761 }
762
763 function rest_system_templates_get( $request ) {
764 try {
765 $params = $request->get_query_params();
766 $category = $params['category'];
767 $templates = [];
768 $templates_option = get_option( 'mwai_templates', [] );
769 if ( !is_array( $templates_option ) ) {
770 update_option( 'mwai_templates', [] );
771 }
772 $categories = array_column( $templates_option, 'category' );
773 $index = array_search( $category, $categories );
774 $templates = [];
775 if ( $index !== false ) {
776 $templates = $templates_option[$index]['templates'];
777 }
778 return new WP_REST_Response([ 'success' => true, 'templates' => $templates ], 200 );
779 }
780 catch ( Exception $e ) {
781 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
782 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
783 }
784 }
785
786 function rest_system_templates_save( $request ) {
787 try {
788 $params = $request->get_json_params();
789 $category = $params['category'];
790 $templates = $params['templates'];
791 $templates_option = get_option( 'mwai_templates', [] );
792 $categories = array_column( $templates_option, 'category' );
793 $index = array_search( $category, $categories );
794 if ( $index !== false && $index >= 0 ) {
795 $templates_option[$index]['templates'] = $templates;
796 }
797 else {
798 $group = [ 'category' => $category, 'templates' => $templates ];
799 $templates_option[] = $group;
800 }
801
802 update_option( 'mwai_templates', $templates_option );
803 return new WP_REST_Response([ 'success' => true ], 200 );
804 }
805 catch ( Exception $e ) {
806 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
807 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
808 }
809 }
810
811 function rest_system_logs_list( $request ) {
812 try {
813 $params = $request->get_json_params();
814 $offset = $params['offset'];
815 $limit = $params['limit'];
816 $filters = $params['filters'];
817 $sort = $params['sort'];
818 $logs = apply_filters( 'mwai_stats_logs_list', [], $offset, $limit, $filters, $sort );
819 return new WP_REST_Response([ 'success' => true, 'total' => $logs['total'], 'logs' => $logs['rows'] ], 200 );
820 }
821 catch ( Exception $e ) {
822 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
823 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
824 }
825 }
826
827 function rest_system_logs_delete( $request ) {
828 try {
829 $params = $request->get_json_params();
830 $logIds = $params['logIds'];
831 $success = apply_filters( 'mwai_stats_logs_delete', true, $logIds );
832 return new WP_REST_Response([ 'success' => $success ], 200 );
833 }
834 catch ( Exception $e ) {
835 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
836 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
837 }
838 }
839
840 function rest_system_logs_meta_get( $request ) {
841 try {
842 $params = $request->get_json_params();
843 $logId = $params['logId'];
844 $metaKeys = $params['metaKeys'];
845 $data = apply_filters( 'mwai_stats_logs_meta', [], $logId, $metaKeys );
846 return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 );
847 }
848 catch ( Exception $e ) {
849 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
850 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
851 }
852 }
853
854 function rest_ai_moderate( $request ) {
855 try {
856 $params = $request->get_json_params();
857 $envId = $params['envId'];
858 $text = $params['text'];
859 if ( !$text ) {
860 return new WP_REST_Response([ 'success' => false, 'message' => 'Text not found.' ], 404 );
861 }
862 $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId );
863 $results = $openai->moderate( $text );
864 return new WP_REST_Response([ 'success' => true, 'results' => $results ], 200 );
865 }
866 catch ( Exception $e ) {
867 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
868 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
869 }
870 }
871
872 function rest_ai_transcribe_audio( $request ) {
873 try {
874 $params = $request->get_json_params();
875 $query = new Meow_MWAI_Query_Transcribe();
876 $query->inject_params( $params );
877 $query->set_scope('admin-tools');
878 $reply = $this->core->run_query( $query );
879 return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 );
880 }
881 catch ( Exception $e ) {
882 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
883 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
884 }
885 }
886
887 function rest_ai_transcribe_image( $request ) {
888 try {
889 global $mwai;
890 $params = $request->get_json_params();
891 $message = $this->retrieve_message( $params );
892 $url = !empty( $params['url'] ) ? $params['url'] : null;
893 // This could lead to a security issue, so let's avoid using path directly.
894 //$path = !empty( $params['path'] ) ? $params['path'] : null;
895 $result = $mwai->simpleVisionQuery( $message, $url );
896 return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 );
897 }
898 catch ( Exception $e ) {
899 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
900 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
901 }
902 }
903
904 function rest_ai_json( $request ) {
905 try {
906 global $mwai;
907 $params = $request->get_json_params();
908 $message = $this->retrieve_message( $params );
909 $result = $mwai->simpleJsonQuery( $message );
910 return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 );
911 }
912 catch ( Exception $e ) {
913 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
914 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
915 }
916 }
917
918 function rest_helpers_post_types() {
919 try {
920 $postTypes = $this->core->get_post_types();
921 return new WP_REST_Response([ 'success' => true, 'postTypes' => $postTypes ], 200 );
922 }
923 catch ( Exception $e ) {
924 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
925 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
926 }
927 }
928
929 function rest_settings_themes( $request ) {
930 try {
931 $method = $request->get_method();
932 if ( $method === 'GET' ) {
933 $themes = $this->core->get_themes();
934 return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 );
935 }
936 else if ( $method === 'POST' ) {
937 $params = $request->get_json_params();
938 $themes = $params['themes'];
939 $themes = $this->core->update_themes( $themes );
940 return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 );
941 }
942 }
943 catch ( Exception $e ) {
944 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
945 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
946 }
947 }
948
949 function rest_settings_chatbots( $request ) {
950 try {
951 $method = $request->get_method();
952 if ( $method === 'GET' ) {
953 $chatbots = $this->core->get_chatbots();
954 return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 );
955 }
956 else if ( $method === 'POST' ) {
957 $params = $request->get_json_params();
958 $chatbots = $params['chatbots'];
959 $chatbots = $this->core->update_chatbots( $chatbots );
960 return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 );
961 }
962 return new WP_REST_Response([ 'success' => false, 'message' => 'Method not allowed' ], 405 );
963 }
964 catch ( Exception $e ) {
965 $message = apply_filters( 'mwai_ai_exception', $e->getMessage() );
966 return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 );
967 }
968 }
969
970 #region Logs
971
972 function rest_get_logs() {
973 $logs = Meow_MWAI_Logging::get();
974 return new WP_REST_Response( [ 'success' => true, 'data' => $logs ], 200 );
975 }
976
977 function rest_clear_logs() {
978 Meow_MWAI_Logging::clear();
979 return new WP_REST_Response( [ 'success' => true ], 200 );
980 }
981
982 #endregion
983 }
984