assistants.php
3 years ago
chatbot-chatgpt.css
3 years ago
chatbot-chatgpt.scss
3 years ago
chatbot.php
3 years ago
chatbot_chats.php
3 years ago
chatbot_logs.php
3 years ago
chatbot_chats.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Modules_Chatbot_Chats { |
| 4 | private $wpdb = null; |
| 5 | private $table_chats = null; |
| 6 | private $db_check = false; |
| 7 | private $namespace = 'ai-engine/v1'; |
| 8 | |
| 9 | public function __construct() { |
| 10 | global $wpdb; |
| 11 | $this->wpdb = $wpdb; |
| 12 | $this->table_chats = $wpdb->prefix . 'mwai_chats'; |
| 13 | add_filter( 'mwai_chatbot_reply', [ $this, 'chatbot_reply' ], 10, 3 ); |
| 14 | add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
| 15 | } |
| 16 | |
| 17 | public function rest_api_init() { |
| 18 | register_rest_route( $this->namespace, '/chats', array( |
| 19 | 'methods' => 'POST', |
| 20 | 'callback' => array( $this, 'rest_chats' ), |
| 21 | 'permission_callback' => '__return_true' |
| 22 | ) ); |
| 23 | } |
| 24 | |
| 25 | function rest_chats( $request ) { |
| 26 | try { |
| 27 | $params = $request->get_json_params(); |
| 28 | $offset = $params['offset']; |
| 29 | $limit = $params['limit']; |
| 30 | $filters = $params['filters']; |
| 31 | $sort = $params['sort']; |
| 32 | $chats = $this->chats_query( [], $offset, $limit, $filters, $sort ); |
| 33 | return new WP_REST_Response([ 'success' => true, 'total' => $chats['total'], 'chats' => $chats['rows'] ], 200 ); |
| 34 | } |
| 35 | catch ( Exception $e ) { |
| 36 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | function chats_query( $chats = [], $offset = 0, $limit = null, $filters = null, $sort = null ) { |
| 41 | $offset = !empty( $offset ) ? intval( $offset ) : 0; |
| 42 | $limit = !empty( $limit ) ? intval( $limit ) : 100; |
| 43 | $filters = !empty( $filters ) ? $filters : []; |
| 44 | $sort = !empty( $sort ) ? $sort : [ "accessor" => "time", "by" => "desc" ]; |
| 45 | $query = "SELECT * FROM $this->table_chats"; |
| 46 | |
| 47 | // Filters |
| 48 | $where = array(); |
| 49 | if ( isset( $filters['from'] ) ) { |
| 50 | $where[] = "time >= '" . esc_sql( $filters['from'] ) . "'"; |
| 51 | } |
| 52 | if ( isset( $filters['to'] ) ) { |
| 53 | $where[] = "time <= '" . esc_sql( $filters['to'] ) . "'"; |
| 54 | } |
| 55 | if ( count( $where ) > 0 ) { |
| 56 | $query .= " WHERE " . implode( " AND ", $where ); |
| 57 | } |
| 58 | |
| 59 | // Count based on this query |
| 60 | $chats['total'] = $this->wpdb->get_var( "SELECT COUNT(*) FROM ($query) AS t" ); |
| 61 | |
| 62 | // Order by |
| 63 | $query .= " ORDER BY " . esc_sql( $sort['accessor'] ) . " " . esc_sql( $sort['by'] ); |
| 64 | |
| 65 | // Limits |
| 66 | if ( $limit > 0 ) { |
| 67 | $query .= " LIMIT $offset, $limit"; |
| 68 | } |
| 69 | |
| 70 | $chats['rows'] = $this->wpdb->get_results( $query, ARRAY_A ); |
| 71 | return $chats; |
| 72 | } |
| 73 | |
| 74 | function chatbot_reply( $rawText, $query, $params ) { |
| 75 | global $mwai_core; |
| 76 | $userIp = $mwai_core->get_ip_address(); |
| 77 | $userId = $mwai_core->get_user_id(); |
| 78 | $chatClientId = isset( $params['clientId'] ) ? $params['clientId'] : $query->session; |
| 79 | $ssChatId = hash( 'sha256', $userIp . $userId . $chatClientId ); |
| 80 | $this->check_db(); |
| 81 | $chat = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT * FROM $this->table_chats WHERE chatId = %s", $ssChatId ) ); |
| 82 | if ( $chat ) { |
| 83 | $chat->messages = json_decode( $chat->messages ); |
| 84 | $chat->messages[] = [ |
| 85 | 'type' => 'user', |
| 86 | 'text' => $params['rawInput'] |
| 87 | ]; |
| 88 | $chat->messages[] = [ |
| 89 | 'type' => 'ai', |
| 90 | 'text' => $rawText |
| 91 | ]; |
| 92 | $chat->messages = json_encode( $chat->messages ); |
| 93 | $chat->updated = wp_date( 'Y-m-d H:i:s' ); |
| 94 | $this->wpdb->update( $this->table_chats, (array) $chat, [ 'id' => $chat->id ] ); |
| 95 | } |
| 96 | else { |
| 97 | $chat = [ |
| 98 | 'chatId' => $ssChatId, |
| 99 | 'messages' => json_encode( [ |
| 100 | [ |
| 101 | 'type' => 'user', |
| 102 | 'text' => $params['rawInput'] |
| 103 | ], |
| 104 | [ |
| 105 | 'type' => 'ai', |
| 106 | 'text' => $rawText |
| 107 | ] |
| 108 | ] ), |
| 109 | 'extra' => json_encode( [ |
| 110 | 'ip' => $userIp, |
| 111 | 'userId' => $userId, |
| 112 | 'session' => $query->session, |
| 113 | 'model' => $query->model, |
| 114 | 'temperature' => $query->temperature |
| 115 | ] ) |
| 116 | ]; |
| 117 | $this->wpdb->insert( $this->table_chats, $chat ); |
| 118 | } |
| 119 | return $rawText; |
| 120 | } |
| 121 | |
| 122 | function check_db() { |
| 123 | if ( $this->db_check ) { |
| 124 | return true; |
| 125 | } |
| 126 | $this->db_check = !( strtolower( |
| 127 | $this->wpdb->get_var( "SHOW TABLES LIKE '$this->table_chats'" ) ) != strtolower( $this->table_chats ) |
| 128 | ); |
| 129 | if ( !$this->db_check ) { |
| 130 | $this->create_db(); |
| 131 | $this->db_check = !( strtolower( |
| 132 | $this->wpdb->get_var( "SHOW TABLES LIKE '$this->table_chats'" ) ) != strtolower( $this->table_chats ) |
| 133 | ); |
| 134 | } |
| 135 | return $this->db_check; |
| 136 | } |
| 137 | |
| 138 | function create_db() { |
| 139 | $charset_collate = $this->wpdb->get_charset_collate(); |
| 140 | $sqlLogs = "CREATE TABLE $this->table_chats ( |
| 141 | id BIGINT(20) NOT NULL AUTO_INCREMENT, |
| 142 | chatId VARCHAR(64) NOT NULL NULL, |
| 143 | messages TEXT NOT NULL NULL, |
| 144 | extra TEXT NOT NULL NULL, |
| 145 | created DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 146 | updated DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 147 | PRIMARY KEY (id) |
| 148 | ) $charset_collate;"; |
| 149 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 150 | dbDelta( $sqlLogs ); |
| 151 | } |
| 152 | |
| 153 | } |