core = $mwai_core; if ( is_admin() ) { return; } add_shortcode( 'mwai_chat', array( $this, 'chat' ) ); add_shortcode( 'mwai_chatbot', array( $this, 'chat' ) ); add_shortcode( 'mwai_imagesbot', array( $this, 'imageschat' ) ); add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); if ( $this->core->get_option( 'shortcode_chat_inject' ) ) { add_action( 'wp_body_open', array( $this, 'inject_chat' ) ); } // Only for test now, but later we should probably import the JS/CSS if ( $this->core->get_option( 'shortcode_chat_syntax_highlighting' ) ) { wp_enqueue_script( 'mwai_chatbot', '//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js', [], null, false ); wp_enqueue_style( 'mwai_chatbot', '//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/stackoverflow-dark.min.css' ); } if ( $this->core->get_option( 'shortcode_chat_styles' ) ) { add_filter( 'mwai_chatbot_style', [ $this, 'apply_chat_styles' ], 10, 2 ); } } function rest_api_init() { try { register_rest_route( $this->namespace, '/chat', array( 'methods' => 'POST', 'callback' => array( $this, 'rest_chat' ), 'permission_callback' => '__return_true' ) ); register_rest_route( $this->namespace, '/imagesbot', array( 'methods' => 'POST', 'callback' => array( $this, 'rest_imagesbot' ), 'permission_callback' => '__return_true' ) ); } catch ( Exception $e ) { var_dump( $e ); } } function chatgpt_style( $id ) { $css = file_get_contents( MWAI_PATH . '/classes/modules/chatbot-chatgpt.css' ); $css = str_replace( '#mwai-chat-id', "#mwai-chat-{$id}", $css ); return ""; } function rest_chat( $request ) { try { $params = $request->get_json_params(); $session = $params['session']; $env = $params['env']; $prompt = $params['prompt']; $model = $params['model']; $temperature = $params['temperature']; $maxTokens = intval( $params['maxTokens'] ); $apiKey = $params['apiKey']; $stop = $params['stop']; $query = new Meow_MWAI_QueryText( $prompt, 1024 ); if ( $model ) { $query->setModel( $model ); } if ( $temperature ) { $query->setTemperature( $temperature ); } if ( $maxTokens ) { $query->setMaxTokens( $maxTokens ); } if ( $stop ) { $query->setStop( $stop ); } if ( $apiKey ) { $query->setApiKey( $apiKey ); } if ( $env ) { $query->setEnv( $env ); } if ( $session ) { $query->setSession( $session ); } $answer = $this->core->ai->run( $query ); $rawText = $answer->result; $html = apply_filters( 'mwai_chatbot_answer', $rawText ); $html = $this->core->markdown_to_html( $rawText ); return new WP_REST_Response([ 'success' => true, 'answer' => $rawText, 'html' => $html, 'usage' => $answer->usage ], 200 ); } catch ( Exception $e ) { return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); } } function rest_imagesbot( $request ) { try { $params = $request->get_json_params(); $session = $params['session']; $env = $params['env']; $prompt = $params['prompt']; $maxResults = $params['maxResults']; $apiKey = $params['apiKey']; $query = new Meow_MWAI_QueryImage( $prompt ); if ( $maxResults ) { $query->setMaxResults( $maxResults ); } if ( $apiKey ) { $query->setApiKey( $apiKey ); } if ( $env ) { $query->setEnv( $env ); } if ( $session ) { $query->setSession( $session ); } $answer = $this->core->ai->run( $query ); return new WP_REST_Response([ 'success' => true, 'images' => $answer->results, 'usage' => $answer->usage ], 200 ); } catch ( Exception $e ) { return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); } } function apply_chat_styles( $css, $chatbotId ) { $chatStyles = $this->core->get_option( 'shortcode_chat_styles' ); return preg_replace_callback( '/--mwai-(\w+):\s*([^;]+);/', function ( $matches ) use ($chatStyles ) { if( isset( $chatStyles[$matches[1]] ) ) { return "--mwai-" . $matches[1] . ": " . $chatStyles[$matches[1]] . ";"; } return $matches[0]; }, $css ); } function inject_chat() { $params = $this->core->get_option( 'shortcode_chat_params' ); echo $this->chat( $params ); } function imageschat( $atts ) { $atts['mode'] = 'images'; return $this->chat( $atts ); } function getCurrentUser() { if ( is_user_logged_in() ) { return wp_get_current_user(); } return null; } function handlePlaceholders( $data ) { if ( strpos( $data, '{' ) === false ) { return $data; } $placeholders_meta = [ '{FIRST_NAME}', '{LAST_NAME}' ]; $placeholders_data = [ '{USER_LOGIN}', '{DISPLAY_NAME}' ]; $user = $this->getCurrentUser(); if ( $user ) { foreach ( $placeholders_meta as $placeholder ) { if ( strpos( $data, $placeholder ) === false ) { continue; } $lcPlaceholder = substr( strtolower( $placeholder ), 1, -1 ); $value = get_user_meta( $user->ID, $lcPlaceholder, true ); $data = str_replace( $placeholder, $value, $data ); } foreach ( $placeholders_data as $placeholder ) { if ( strpos( $data, $placeholder ) === false ) { continue; } $lcPlaceholder = substr( strtolower( $placeholder ), 1, -1 ); $value = $user->data->$lcPlaceholder; $data = str_replace( $placeholder, $value, $data ); } } return $data; } function formatUserName( $userName ) { // Default avatar if ( empty( $userName ) ) { $user = $this->getCurrentUser(); if ( $user ) { // Gravatar $userName = '