chatbot.php
2 years ago
discussions.php
2 years ago
files.php
2 years ago
security.php
2 years ago
utilities.php
2 years ago
discussions.php
304 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Modules_Discussions { |
| 4 | private $wpdb = null; |
| 5 | private $core = null; |
| 6 | private $table_chats = null; |
| 7 | private $db_check = false; |
| 8 | private $namespace_admin = 'mwai/v1'; |
| 9 | private $namespace_ui = 'mwai-ui/v1'; |
| 10 | |
| 11 | public function __construct() { |
| 12 | global $wpdb; |
| 13 | global $mwai_core; |
| 14 | $this->core = $mwai_core; |
| 15 | $this->wpdb = $wpdb; |
| 16 | $this->table_chats = $wpdb->prefix . 'mwai_chats'; |
| 17 | |
| 18 | if ( $this->core->get_option( 'shortcode_chat_discussions' ) ) { |
| 19 | add_filter( 'mwai_chatbot_reply', [ $this, 'chatbot_reply' ], 10, 4 ); |
| 20 | add_action( 'rest_api_init', [ $this, 'rest_api_init' ] ); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | public function rest_api_init() { |
| 25 | |
| 26 | // Admin |
| 27 | register_rest_route( $this->namespace_admin, '/discussions/list', [ |
| 28 | 'methods' => 'POST', |
| 29 | 'callback' => [ $this, 'rest_discussions_list' ], |
| 30 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 31 | ] ); |
| 32 | register_rest_route( $this->namespace_admin, '/discussions/delete', [ |
| 33 | 'methods' => 'POST', |
| 34 | 'callback' => [ $this, 'rest_discussions_delete' ], |
| 35 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 36 | ] ); |
| 37 | |
| 38 | // UI |
| 39 | register_rest_route( $this->namespace_ui, '/discussions/list', [ |
| 40 | 'methods' => 'POST', |
| 41 | 'callback' => [ $this, 'rest_discussions_ui_list' ], |
| 42 | 'permission_callback' => '__return_true' |
| 43 | ] ); |
| 44 | } |
| 45 | |
| 46 | function rest_discussions_list( $request ) { |
| 47 | try { |
| 48 | $params = $request->get_json_params(); |
| 49 | $offset = $params['offset']; |
| 50 | $limit = $params['limit']; |
| 51 | $filters = $params['filters']; |
| 52 | $sort = $params['sort']; |
| 53 | $chats = $this->chats_query( [], $offset, $limit, $filters, $sort ); |
| 54 | return new WP_REST_Response([ 'success' => true, 'total' => $chats['total'], 'chats' => $chats['rows'] ], 200 ); |
| 55 | } |
| 56 | catch ( Exception $e ) { |
| 57 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | function rest_discussions_ui_list( $request ) { |
| 62 | try { |
| 63 | $params = $request->get_json_params(); |
| 64 | $offset = isset( $params['offset'] ) ? $params['offset'] : 0; |
| 65 | $limit = isset( $params['limit'] ) ? $params['limit'] : 10; |
| 66 | $botId = isset( $params['botId'] ) ? $params['botId'] : null; |
| 67 | $customId = isset( $params['customId'] ) ? $params['customId'] : null; |
| 68 | |
| 69 | if ( !is_null( $customId ) ) { |
| 70 | $botId = $customId; |
| 71 | } |
| 72 | |
| 73 | if ( is_null( $botId ) ) { |
| 74 | return new WP_REST_Response([ 'success' => false, 'message' => "Bot ID is required." ], 200 ); |
| 75 | } |
| 76 | |
| 77 | $userId = get_current_user_id(); |
| 78 | if ( !$userId ) { |
| 79 | return new WP_REST_Response([ 'success' => false, 'message' => "You need to be connected." ], 200 ); |
| 80 | } |
| 81 | $filters = [ |
| 82 | [ 'accessor' => 'user', 'value' => $userId ], |
| 83 | [ 'accessor' => 'botId', 'value' => $botId ], |
| 84 | ]; |
| 85 | $chats = $this->chats_query( [], $offset, $limit, $filters ); |
| 86 | return new WP_REST_Response([ 'success' => true, 'total' => $chats['total'], 'chats' => $chats['rows'] ], 200 ); |
| 87 | } |
| 88 | catch ( Exception $e ) { |
| 89 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | function rest_discussions_delete( $request ) { |
| 94 | try { |
| 95 | $params = $request->get_json_params(); |
| 96 | $chatsIds = $params['chatIds']; |
| 97 | if ( is_array( $chatsIds ) ) { |
| 98 | if ( count( $chatsIds ) === 0 ) { |
| 99 | $this->wpdb->query( "TRUNCATE TABLE $this->table_chats" ); |
| 100 | } |
| 101 | foreach( $chatsIds as $chatId ) { |
| 102 | $this->wpdb->delete( $this->table_chats, [ 'chatId' => $chatId ] ); |
| 103 | } |
| 104 | } |
| 105 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 106 | } |
| 107 | catch ( Exception $e ) { |
| 108 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Get latest discussion for the given parameter |
| 113 | function get_discussion( $botId, $chatId ) { |
| 114 | $this->check_db(); |
| 115 | $chat = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT * |
| 116 | FROM $this->table_chats |
| 117 | WHERE chatId = %s AND botId = %s", $chatId, $botId |
| 118 | ) ); |
| 119 | if ( $chat ) { |
| 120 | $chat->messages = json_decode( $chat->messages ); |
| 121 | return $chat; |
| 122 | } |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | function chats_query( $chats = [], $offset = 0, $limit = null, $filters = null, $sort = null ) { |
| 127 | $this->check_db(); |
| 128 | $offset = !empty( $offset ) ? intval( $offset ) : 0; |
| 129 | $limit = !empty( $limit ) ? intval( $limit ) : 5; |
| 130 | $filters = !empty( $filters ) ? $filters : []; |
| 131 | $sort = !empty( $sort ) ? $sort : [ 'accessor' => 'updated', 'by' => 'desc' ]; |
| 132 | $query = "SELECT * FROM $this->table_chats"; |
| 133 | |
| 134 | // Filters |
| 135 | if ( is_array( $filters ) ) { |
| 136 | $where = array(); |
| 137 | foreach ( $filters as $filter ) { |
| 138 | if ( $filter['accessor'] === 'user' ) { |
| 139 | $value = esc_sql( $filter['value'] ); |
| 140 | if ( is_null( $value ) || $value === '' ) { |
| 141 | continue; |
| 142 | } |
| 143 | $isIP = filter_var( $value, FILTER_VALIDATE_IP ); |
| 144 | if ( $isIP ) { |
| 145 | $where[] = "ip = '{$value}'"; |
| 146 | } |
| 147 | else { |
| 148 | $where[] = "userId = '{$value}'"; |
| 149 | } |
| 150 | } |
| 151 | if ( $filter['accessor'] === 'botId' ) { |
| 152 | $value = esc_sql( $filter['value'] ); |
| 153 | if ( is_null( $value ) || $value === '' ) { |
| 154 | continue; |
| 155 | } |
| 156 | $where[] = "botId = '{$value}'"; |
| 157 | } |
| 158 | if ( $filter['accessor'] === 'preview' ) { |
| 159 | $value = $filter['value']; |
| 160 | if ( empty( $value ) ) { |
| 161 | continue; |
| 162 | } |
| 163 | $where[] = "messages LIKE '%{$value}%'"; |
| 164 | } |
| 165 | } |
| 166 | if ( count( $where ) > 0 ) { |
| 167 | $query .= " WHERE " . implode( " AND ", $where ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Count based on this query |
| 172 | $chats['total'] = $this->wpdb->get_var( "SELECT COUNT(*) FROM ($query) AS t" ); |
| 173 | |
| 174 | // Order by |
| 175 | $query .= " ORDER BY " . esc_sql( $sort['accessor'] ) . " " . esc_sql( $sort['by'] ); |
| 176 | |
| 177 | // Limits |
| 178 | if ( $limit > 0 ) { |
| 179 | $query .= " LIMIT $offset, $limit"; |
| 180 | } |
| 181 | |
| 182 | $chats['rows'] = $this->wpdb->get_results( $query, ARRAY_A ); |
| 183 | return $chats; |
| 184 | } |
| 185 | |
| 186 | public function chatbot_reply( $rawText, $query, $params, $extra ) { |
| 187 | global $mwai_core; |
| 188 | $userIp = $mwai_core->get_ip_address(); |
| 189 | $userId = $mwai_core->get_user_id(); |
| 190 | $botId = isset( $params['botId'] ) ? $params['botId'] : null; |
| 191 | $chatId = isset( $params['chatId'] ) ? $params['chatId'] : $query->session; |
| 192 | $customId = isset( $params['customId'] ) ? $params['customId'] : null; |
| 193 | $threadId = $query instanceof Meow_MWAI_Query_Assistant ? $query->threadId : null; |
| 194 | |
| 195 | if ( !empty( $customId ) ) { |
| 196 | $botId = $customId; |
| 197 | } |
| 198 | $newMessage = isset( $params['newMessage'] ) ? $params['newMessage'] : $query->get_message(); |
| 199 | //$chatId = hash( 'sha256', $userIp . $userId . $clientChatId ); |
| 200 | $this->check_db(); |
| 201 | $chat = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT * FROM $this->table_chats WHERE chatId = %s", $chatId ) ); |
| 202 | $messageExtra = [ |
| 203 | 'embeddings' => isset( $extra['embeddings'] ) ? $extra['embeddings'] : null |
| 204 | ]; |
| 205 | $chatExtra = [ |
| 206 | 'session' => $query->session, |
| 207 | 'model' => $query->model, |
| 208 | ]; |
| 209 | if ( !empty( $query->temperature ) ) { |
| 210 | $chatExtra['temperature'] = $query->temperature; |
| 211 | } |
| 212 | if ( !empty( $query->context ) ) { |
| 213 | $chatExtra['context'] = $query->context; |
| 214 | } |
| 215 | if ( $query instanceof Meow_MWAI_Query_Assistant ) { |
| 216 | $chatExtra['assistantId'] = $query->assistantId; |
| 217 | $chatExtra['threadId'] = $query->threadId; |
| 218 | } |
| 219 | if ( $chat ) { |
| 220 | $chat->messages = json_decode( $chat->messages ); |
| 221 | $chat->messages[] = [ 'role' => 'user', 'content' => $newMessage ]; |
| 222 | $chat->messages[] = [ 'role' => 'assistant', 'content' => $rawText, 'extra' => $messageExtra ]; |
| 223 | $chat->messages = json_encode( $chat->messages ); |
| 224 | $this->wpdb->update( $this->table_chats, [ |
| 225 | 'userId' => $userId, |
| 226 | 'messages' => $chat->messages, |
| 227 | 'updated' => date( 'Y-m-d H:i:s' ) |
| 228 | ], [ 'id' => $chat->id ] ); |
| 229 | } |
| 230 | else { |
| 231 | $chat = [ |
| 232 | 'userId' => $userId, |
| 233 | 'ip' => $userIp, |
| 234 | 'messages' => json_encode( [ |
| 235 | [ 'role' => 'user', 'content' => $newMessage ], |
| 236 | [ 'role' => 'assistant', 'content' => $rawText, 'extra' => $messageExtra ] |
| 237 | ] ), |
| 238 | 'extra' => json_encode( $chatExtra ), |
| 239 | 'botId' => $botId, |
| 240 | 'chatId' => $chatId, |
| 241 | 'threadId' => $threadId, |
| 242 | 'created' => date( 'Y-m-d H:i:s' ), |
| 243 | 'updated' => date( 'Y-m-d H:i:s' ) |
| 244 | ]; |
| 245 | $this->wpdb->insert( $this->table_chats, $chat ); |
| 246 | } |
| 247 | return $rawText; |
| 248 | } |
| 249 | |
| 250 | function check_db() { |
| 251 | if ( $this->db_check ) { |
| 252 | return true; |
| 253 | } |
| 254 | $this->db_check = !( strtolower( |
| 255 | $this->wpdb->get_var( "SHOW TABLES LIKE '$this->table_chats'" ) ) != strtolower( $this->table_chats ) |
| 256 | ); |
| 257 | if ( !$this->db_check ) { |
| 258 | $this->create_db(); |
| 259 | $this->db_check = !( strtolower( |
| 260 | $this->wpdb->get_var( "SHOW TABLES LIKE '$this->table_chats'" ) ) != strtolower( $this->table_chats ) |
| 261 | ); |
| 262 | } |
| 263 | |
| 264 | // LATER: REMOVE THIS AFTER SEPTEMBER 2023 |
| 265 | // Make sure the column "userId" and "ip "exist in the $this->table_chats table |
| 266 | $this->db_check = $this->db_check && $this->wpdb->get_var( "SHOW COLUMNS FROM $this->table_chats LIKE 'userId'" ); |
| 267 | if ( !$this->db_check ) { |
| 268 | $this->wpdb->query( "ALTER TABLE $this->table_chats ADD COLUMN userId BIGINT(20) NULL" ); |
| 269 | $this->wpdb->query( "ALTER TABLE $this->table_chats ADD COLUMN ip VARCHAR(64) NULL" ); |
| 270 | $this->wpdb->query( "ALTER TABLE $this->table_chats ADD COLUMN botId VARCHAR(64) NULL" ); |
| 271 | $this->db_check = true; |
| 272 | } |
| 273 | |
| 274 | // LATER: REMOVE THIS AFTER JANUARY 2024 |
| 275 | $this->db_check = $this->db_check && $this->wpdb->get_var( "SHOW COLUMNS FROM $this->table_chats LIKE 'threadId'" ); |
| 276 | if ( !$this->db_check ) { |
| 277 | $this->wpdb->query( "ALTER TABLE $this->table_chats ADD COLUMN threadId VARCHAR(64) NULL" ); |
| 278 | $this->db_check = true; |
| 279 | } |
| 280 | |
| 281 | return $this->db_check; |
| 282 | } |
| 283 | |
| 284 | function create_db() { |
| 285 | $charset_collate = $this->wpdb->get_charset_collate(); |
| 286 | $sqlLogs = "CREATE TABLE $this->table_chats ( |
| 287 | id BIGINT(20) NOT NULL AUTO_INCREMENT, |
| 288 | userId BIGINT(20) NULL, |
| 289 | ip VARCHAR(64) NULL, |
| 290 | messages TEXT NOT NULL NULL, |
| 291 | extra TEXT NOT NULL NULL, |
| 292 | botId VARCHAR(64) NULL, |
| 293 | chatId VARCHAR(64) NOT NULL, |
| 294 | threadId VARCHAR(64) NULL, |
| 295 | created DATETIME NOT NULL, |
| 296 | updated DATETIME NOT NULL, |
| 297 | PRIMARY KEY (id), |
| 298 | INDEX chatId (chatId) |
| 299 | ) $charset_collate;"; |
| 300 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 301 | dbDelta( $sqlLogs ); |
| 302 | } |
| 303 | |
| 304 | } |