get_articles(); $aizl_functions = $aizl_db_handler->get_functions(); class Create { private $table_agent; public function __construct() { $this->table_agent = Config::get_table_name('agent'); $this->table_conversation = Config::get_table_name('conversation'); add_action('wp_ajax_aizl_create_agent', array($this, 'aizl_create_agent')); add_action('wp_ajax_aizl_get_agent', array($this, 'aizl_get_agent')); add_action('wp_ajax_nopriv_aizl_get_agent', array($this, 'aizl_get_agent')); add_action('wp_ajax_aizl_delete_agent', array($this, 'aizl_delete_agent')); add_action('wp_ajax_aizl_check_function_availability', array($this, 'aizl_check_function_availability')); } public function aizl_create_agent() { if (!check_ajax_referer('aizl_setting', 'nonce', false)) { wp_send_json_error('Invalid nonce.'); return; } if (!isset($_POST['name'])) { wp_send_json_error('No agent name provided.'); return; } $agent_id = isset($_POST['agent_id']) ? sanitize_text_field(wp_unslash($_POST['agent_id'])) : null; $name = sanitize_text_field(wp_unslash($_POST['name'])); $instructions = isset($_POST['instructions']) ? wp_kses_post(wp_unslash($_POST['instructions'])) : ''; $greeting_message = isset($_POST['greeting_message']) ? wp_kses_post(wp_unslash($_POST['greeting_message'])) : ''; $model = isset($_POST['model']) ? sanitize_text_field(wp_unslash($_POST['model'])) : ''; $selected_articles = isset($_POST['articles']) ? array_map('intval', $_POST['articles']) : []; $selected_functions = isset($_POST['functions']) ? array_map('intval', $_POST['functions']) : []; $tools_object = $this->get_tools_object($selected_articles, $selected_functions); $tools = $tools_object['tools']; if ($agent_id) { $this->update_agent($name, $instructions, $greeting_message, $model, $selected_articles, $selected_functions, $tools, $agent_id); wp_send_json_success('The agent has been updated.'); } else { $agent_id = $this->save_agent($name, $instructions, $greeting_message, $model, $selected_articles, $selected_functions, $tools); wp_send_json_success('The agent has been created.'); } } private function save_agent($name, $instructions, $greeting_message, $model, $article_ids, $function_ids, $tools) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $result = $wpdb->insert($this->table_agent, array( 'name' => $name, 'instructions' => $instructions, 'greeting_message' => $greeting_message, 'model' => $model, 'article_ids' => json_encode($article_ids), 'function_ids' => json_encode($function_ids), 'tools' => json_encode($tools), 'created_time' => gmdate('Y-m-d H:i:s'), 'updated_time' => gmdate('Y-m-d H:i:s'), )); if ($result === false) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log('Database insert error: ' . $wpdb->last_error); return false; } return $wpdb->insert_id; } private function update_agent($name, $instructions, $greeting_message, $model, $article_ids, $function_ids, $tools, $agent_id) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $result = $wpdb->update($this->table_agent, array( 'name' => $name, 'instructions' => $instructions, 'greeting_message' => $greeting_message, 'model' => $model, 'article_ids' => json_encode($article_ids), 'function_ids' => json_encode($function_ids), 'tools' => json_encode($tools), 'updated_time' => gmdate('Y-m-d H:i:s'), ), array('id' => $agent_id)); if ($result === false) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log('Database update error: ' . $wpdb->last_error); return false; } return true; } private function get_tools_object($article_ids, $function_ids) { global $wpdb; $tools = []; $db_handler = new DBHandler(); // Add file search tool foreach ($article_ids as $article_id) { $vector_store_id = $db_handler->get_vector_store_id_by_article_id($article_id); if ($vector_store_id) { $vector_store_ids[] = $vector_store_id; } } if (!empty($vector_store_ids)) { $tools[] = array( 'type' => 'file_search', 'vector_store_ids' => $vector_store_ids, "max_num_results" => 3 ); } // Add function tools foreach ($function_ids as $function_id) { $function_data = $db_handler->get_function_by_id($function_id); if ($function_data) { $tools[] = json_decode($function_data->definition, true); } } return [ 'tools' => $tools ]; } public function aizl_get_agent() { if (!check_ajax_referer('aizl_setting', 'nonce', false)) { wp_send_json_error('Invalid nonce.'); return; } if (!isset($_POST['agent_id'])) { wp_send_json_error('No agent ID provided.'); return; } global $wpdb; $db_handler = new DBHandler(); $agent_id = intval($_POST['agent_id']); $agent = $db_handler->get_agent_by_id($agent_id); if (!$agent) { wp_send_json_error('Agent not found.'); return; } $agent->instructions = wp_kses_post(stripslashes($agent->instructions)); $agent->greeting_message = wp_kses_post(stripslashes($agent->greeting_message)); wp_send_json_success($agent); } public function aizl_delete_agent() { if (!check_ajax_referer('aizl_setting', 'nonce', false)) { wp_send_json_error('Invalid nonce.'); return; } if (!current_user_can('manage_options')) { wp_send_json_error('Unauthorized access.'); return; } if (!isset($_POST['agent_id'])) { wp_send_json_error('No agent ID provided.'); return; } global $wpdb; $agent_id = intval($_POST['agent_id']); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->delete($this->table_agent, array('id' => $agent_id), array('%d')); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->delete($this->table_conversation, array('agent_id' => $agent_id), array('%d')); wp_send_json_success('The agent has been deleted.'); } public function aizl_check_function_availability() { if (!check_ajax_referer('aizl_setting', 'nonce', false)) { wp_send_json_error('Invalid nonce.'); return; } $db_handler = new DBHandler(); $functions = $db_handler->get_functions(); $availability = array(); foreach ($functions as $function) { $is_available = $db_handler->is_function_available($function); $availability[$function->id] = $is_available; } wp_send_json_success($availability); } }