aimogen_event_exit($message);
}
else
{
if (isset($_REQUEST['input_text']) && $_REQUEST['input_text'] != '')
{
$thread_id = '';
if(isset($_REQUEST['thread_id']) && trim(sanitize_text_field( wp_unslash($_REQUEST['thread_id']))) !== '')
{
$thread_id = sanitize_text_field( wp_unslash($_REQUEST['thread_id']));
}
$assistant_id = '';
if(isset($_REQUEST['assistant_id']) && trim(sanitize_text_field( wp_unslash($_REQUEST['assistant_id']))) !== '')
{
$assistant_id = sanitize_text_field( wp_unslash($_REQUEST['assistant_id']));
}
$aimogen_Chatbot_Settings = get_option('aimogen_Chatbot_Settings', false);
$no_internet = false;
$ia_raw = '';
if ( isset( $_REQUEST['internet_access'] ) ) {
$ia_raw = wp_unslash( $_REQUEST['internet_access'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
}
$ia = sanitize_text_field( $ia_raw );
$em_raw = '';
if ( isset( $_REQUEST['embeddings'] ) ) {
$em_raw = wp_unslash( $_REQUEST['embeddings'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
}
$em = sanitize_text_field( $em_raw );
if(isset($ia) && ($ia === 'no' || $ia === '0' || $ia == 'off' || $ia == 'disabled' || $ia == 'Disabled' || $ia == 'disable' || $ia == "false"))
{
$no_internet = true;
}
$no_embeddings = false;
if(isset($em) && ($em === 'no' || $em === '0' || $em == 'off' || $em == 'disabled' || $em == 'disable' || $em == 'Disabled' || $em == "false"))
{
$no_embeddings = true;
}
$aimogen_Main_Settings = get_option('aimogen_Main_Settings', false);
if (!isset($aimogen_Main_Settings['app_id']) || trim($aimogen_Main_Settings['app_id']) == '')
{
$aimogen_result = esc_html__('You need to insert a valid OpenAI/AimogenAPI API Key for this to work!', 'aimogen');
$this->aimogen_event_exit($aimogen_result);
}
if(isset($_REQUEST['user_token_cap_per_day']) && trim(sanitize_text_field( wp_unslash($_REQUEST['user_token_cap_per_day']))) !== '')
{
$user_token_cap_per_day = sanitize_text_field( wp_unslash($_REQUEST['user_token_cap_per_day']));
}
else
{
$user_token_cap_per_day = '';
}
if(!empty($user_token_cap_per_day))
{
$user_token_cap_per_day = intval($user_token_cap_per_day);
}
if(isset($_REQUEST['file_data']))
{
$file_data = sanitize_text_field( wp_unslash($_REQUEST['file_data']));
}
else
{
$file_data = '';
}
if(isset($_REQUEST['user_id']))
{
$user_id = sanitize_text_field( wp_unslash($_REQUEST['user_id']));
}
else
{
$user_id = '';
}
$forced_functions = array();
if(isset($_REQUEST['enable_god_mode']))
{
$enable_god_mode = sanitize_text_field( wp_unslash($_REQUEST['enable_god_mode']));
}
else
{
$enable_god_mode = '';
}
if($enable_god_mode === 'on' || $enable_god_mode === 'yes' || $enable_god_mode === 'true' || $enable_god_mode === '1' || $enable_god_mode === 'enable' || $enable_god_mode === 'enabled')
{
$function_result = '';
}
elseif(is_string($enable_god_mode) && !empty($enable_god_mode))
{
$forced_functions = explode(',', $enable_god_mode);
$function_result = '';
}
else
{
$function_result = 'disabled';
}
if(isset($_REQUEST['functions_result']) && trim(sanitize_text_field( wp_unslash($_REQUEST['functions_result']))) != '' && (!isset($_REQUEST['bufferid']) || sanitize_text_field( wp_unslash($_REQUEST['bufferid'])) == ''))
{
$fr = sanitize_text_field( wp_unslash($_REQUEST['functions_result']));
$fr = json_decode($fr, true);
if($fr !== null)
{
$function_result = $fr;
}
else
{
$fr = sanitize_text_field( wp_unslash($_REQUEST['functions_result']));
$fr = json_decode(wp_unslash($fr), true);
if($fr !== null)
{
$function_result = $fr;
}
}
}
$remember_data = false;
if(isset($_REQUEST['functions_result']) && trim(sanitize_text_field( wp_unslash($_REQUEST['functions_result']))) == '0' && (isset($_REQUEST['bufferid']) && sanitize_text_field( wp_unslash($_REQUEST['bufferid'])) != ''))
{
$buffer_id = sanitize_key( wp_unslash( $_REQUEST['bufferid'] ) );
$remember_data = get_transient('aimogen_ai_data_' . $buffer_id);
if(is_array($remember_data))
{
if(isset($remember_data[3]))
{
$fr = wp_unslash($remember_data[3]);
$fr = json_decode($fr, true);
if($fr !== null)
{
$function_result = $fr;
}
}
}
}
if(is_array($function_result) && is_array($function_result[0]))
{
if(isset($_REQUEST['run_id']) && trim(sanitize_text_field( wp_unslash($_REQUEST['run_id']))) != '')
{
for($k = 0; $k < count($function_result); $k++)
{
$function_result[$k]['run_id'] = sanitize_text_field( wp_unslash($_REQUEST['run_id']));
$function_result[$k]['thread_id'] = $thread_id;
}
}
}
elseif(is_string($function_result) && !empty($function_result))
{
$function_result = array('function_result' => $function_result);
}
if(isset($_REQUEST['pdf_data']))
{
$embedding_namespace = sanitize_text_field( wp_unslash($_REQUEST['pdf_data']));
}
else
{
$embedding_namespace = '';
}
if(empty($embedding_namespace))
{
if (isset($aimogen_Chatbot_Settings['persistent']) && $aimogen_Chatbot_Settings['persistent'] == 'vector')
{
$embedding_namespace = 'persistentchat_' . $user_id . '_' . $thread_id;
}
else
{
if(isset($_REQUEST['embeddings_namespace']) && !empty(sanitize_text_field( wp_unslash($_REQUEST['embeddings_namespace']))))
{
$embedding_namespace = sanitize_text_field( wp_unslash($_REQUEST['embeddings_namespace']));
}
}
}
if(isset($_REQUEST['bufferid']) && sanitize_text_field( wp_unslash($_REQUEST['bufferid'])) != '')
{
$buffer_id = sanitize_key( wp_unslash( $_REQUEST['bufferid'] ) );
if($remember_data === false)
{
$remember_data = get_transient('aimogen_ai_data_' . $buffer_id);
}
delete_transient('aimogen_ai_data_' . $buffer_id);
if(is_array($remember_data))
{
if(isset($remember_data[2]))
{
$input_text = wp_unslash($remember_data[0]);
$remember_string = wp_unslash($remember_data[1]);
$user_question = wp_unslash($remember_data[2]);
}
else
{
$message = esc_html__('Invalid bufferid sent!', 'aimogen');
$this->aimogen_event_exit($message);
}
}
else
{
$input_text = '';
$remember_string = '';
$user_question = '';
}
}
else
{
$input_text = sanitize_text_field( wp_unslash($_REQUEST['input_text']));
if ( isset( $_POST['user_question'] ) ) {
$user_question = sanitize_textarea_field( wp_unslash( $_POST['user_question'] ) );
}
else
{
$user_question = '';
}
if(isset($_REQUEST['remember_string']))
{
$remember_string = sanitize_text_field( wp_unslash($_REQUEST['remember_string']));
}
else
{
$remember_string = '';
}
}
if(isset($_REQUEST['forms_replace']) && trim(sanitize_text_field( wp_unslash($_REQUEST['forms_replace']))) === '1')
{
if (isset($aimogen_Main_Settings['form_prepend']) && trim($aimogen_Main_Settings['form_prepend']) != '')
{
$input_text = trim($aimogen_Main_Settings['form_prepend']) . ' ' . $input_text;
}
if (isset($aimogen_Main_Settings['form_append']) && trim($aimogen_Main_Settings['form_append']) != '')
{
$input_text = $input_text . ' ' . trim($aimogen_Main_Settings['form_append']);
}
$post_id = get_the_ID();
$input_text = aimogen_replaceEmbeddingsAIPostShortcodes($input_text, $post_id);
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) || !is_user_logged_in())
{
$input_text = str_replace('%%user_name%%', '', $input_text);
$input_text = str_replace('%%user_email%%', '' , $input_text);
$input_text = str_replace('%%user_display_name%%', '', $input_text);
$input_text = str_replace('%%user_role%%', '', $input_text);
$input_text = str_replace('%%user_id%%', '' , $input_text);
$input_text = str_replace('%%user_firstname%%', '' , $input_text);
$input_text = str_replace('%%user_lastname%%', '' , $input_text);
$input_text = str_replace('%%user_description%%', '' , $input_text);
$input_text = str_replace('%%user_url%%', '' , $input_text);
}
else
{
$input_text = str_replace('%%user_name%%', $current_user->user_login, $input_text);
$input_text = str_replace('%%user_email%%', $current_user->user_email , $input_text);
$input_text = str_replace('%%user_display_name%%', $current_user->display_name, $input_text);
$input_text = str_replace('%%user_role%%', implode(',', $current_user->roles), $input_text);
$input_text = str_replace('%%user_id%%', $current_user->ID , $input_text);
$input_text = str_replace('%%user_firstname%%', $current_user->user_firstname , $input_text);
$input_text = str_replace('%%user_lastname%%', $current_user->user_lastname , $input_text);
$user_desc = get_the_author_meta( 'description', $current_user->ID );
$input_text = str_replace('%%user_description%%', $user_desc , $input_text);
$user_url = get_the_author_meta( 'user_url', $current_user->ID );
$input_text = str_replace('%%user_url%%', $user_url , $input_text);
}
}
if (isset($aimogen_Chatbot_Settings['max_input_length']) && $aimogen_Chatbot_Settings['max_input_length'] != '' && is_numeric($aimogen_Chatbot_Settings['max_input_length']))
{
if(strlen($input_text) > intval($aimogen_Chatbot_Settings['max_input_length']))
{
$input_text = substr($input_text, 0, intval($aimogen_Chatbot_Settings['max_input_length']));
}
}
$is_modern_gpt = '0';
if(isset($_REQUEST['is_modern_gpt']))
{
$is_modern_gpt = sanitize_text_field( wp_unslash($_REQUEST['is_modern_gpt']));
}
if($is_modern_gpt == '1')
{
if(!empty($remember_string))
{
$remember_string = json_decode($remember_string, true);
if($remember_string === null)
{
$aimogen_result = esc_html__('Failed to decode conversation data!', 'aimogen');
$this->aimogen_event_exit($aimogen_result);
}
if(!is_array($remember_string))
{
$remember_string = [];
}
}
else
{
$remember_string = [];
}
if (isset($aimogen_Chatbot_Settings['chatbot_prepend']) && trim($aimogen_Chatbot_Settings['chatbot_prepend']) != '')
{
array_unshift($remember_string, array ('role' => 'user', 'content' => trim($aimogen_Chatbot_Settings['chatbot_prepend'])));
}
if (isset($aimogen_Chatbot_Settings['chatbot_append']) && trim($aimogen_Chatbot_Settings['chatbot_append']) != '')
{
$remember_string[] = array ('role' => 'user', 'content' => trim($aimogen_Chatbot_Settings['chatbot_append']));
}
$remember_string[] = array ('role' => 'user', 'content' => $input_text);
$input_text = $remember_string;
}
else
{
if (isset($aimogen_Chatbot_Settings['chatbot_prepend']) && trim($aimogen_Chatbot_Settings['chatbot_prepend']) != '')
{
$remember_string = trim($aimogen_Chatbot_Settings['chatbot_prepend']) . ' ' . $remember_string;
}
if (isset($aimogen_Chatbot_Settings['chatbot_append']) && trim($aimogen_Chatbot_Settings['chatbot_append']) != '')
{
$remember_string = $remember_string . ' ' . trim($aimogen_Chatbot_Settings['chatbot_append']);
}
if(!empty(trim($remember_string)))
{
$input_text = trim($remember_string) . PHP_EOL . $input_text;
}
}
if(isset($_REQUEST['model']))
{
$model = sanitize_text_field(wp_unslash($_REQUEST['model']));
}
else
{
$model = 'default';
}
if($model == 'default')
{
$model = AIMOGEN_DEFAULT_MODEL;
}
if(isset($_REQUEST['mcp_servers']))
{
$mcp_servers = sanitize_text_field(wp_unslash($_REQUEST['mcp_servers']));
}
else
{
$mcp_servers = '';
}
if(isset($_REQUEST['temp']))
{
$temperature = sanitize_text_field(wp_unslash($_REQUEST['temp']));
}
else
{
$temperature = '1';
}
if(isset($_REQUEST['top_p']))
{
$top_p = sanitize_text_field(wp_unslash($_REQUEST['top_p']));
}
else
{
$top_p = '1';
}
if(isset($_REQUEST['presence']))
{
$presence_penalty = sanitize_text_field(wp_unslash($_REQUEST['presence']));
}
else
{
$presence_penalty = '0';
}
if(isset($_REQUEST['frequency']))
{
$frequency_penalty = sanitize_text_field(wp_unslash($_REQUEST['frequency']));
}
else
{
$frequency_penalty = '0';
}
if(isset($_REQUEST['store_data']))
{
$store_data = sanitize_text_field(wp_unslash($_REQUEST['store_data']));
}
else
{
$store_data = 'off';
}
$models = aimogen_get_all_models();
if(!in_array($model, $models))
{
$aimogen_result = esc_html__('Invalid model provided: ', 'aimogen') . $model;
$this->aimogen_event_exit($aimogen_result);
}
$vision_file = '';
if(isset($_REQUEST['vision_file']))
{
if(aimogen_is_vision_model($model, $assistant_id))
{
$vision_file = sanitize_text_field( wp_unslash($_REQUEST['vision_file']));
}
}
$temperature = floatval($temperature);
$top_p = floatval($top_p);
$presence_penalty = floatval($presence_penalty);
$frequency_penalty = floatval($frequency_penalty);
if($temperature < 0 || $temperature > 2)
{
$aimogen_result = esc_html__('Invalid temperature provided: ', 'aimogen') . $temperature;
$this->aimogen_event_exit($aimogen_result);
}
if($top_p < 0 || $top_p > 1)
{
$aimogen_result = esc_html__('Invalid top_p provided: ', 'aimogen') . $top_p;
$this->aimogen_event_exit($aimogen_result);
}
if($presence_penalty < -2 || $presence_penalty > 2)
{
$aimogen_result = esc_html__('Invalid presence_penalty provided: ', 'aimogen') . $presence_penalty;
$this->aimogen_event_exit($aimogen_result);
}
if($frequency_penalty < -2 || $frequency_penalty > 2)
{
$aimogen_result = esc_html__('Invalid frequency_penalty provided: ', 'aimogen') . $frequency_penalty;
$this->aimogen_event_exit($aimogen_result);
}
$used_token_count = 0;
if(is_numeric($user_token_cap_per_day))
{
if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
{
/* translators: %s is the login URL */
$aimogen_result = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please log in to continue.', 'aimogen'), array( 'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
$this->aimogen_event_exit($aimogen_result);
}
$used_token_count = get_user_meta($user_id, 'aimogen_used_chat_tokens', true);
if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
{
$used_token_count = intval($used_token_count);
if($used_token_count > $user_token_cap_per_day)
{
$aimogen_result = esc_html__('Daily token count for your user account is exceeded! Please try again tomorrow.', 'aimogen');
$this->aimogen_event_exit($aimogen_result);
}
}
else
{
$used_token_count = 0;
}
}
$appids = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id']));
$appids = array_filter($appids);
$token = $appids[array_rand($appids)];
$max_tokens = aimogen_get_max_tokens($model);
if (isset($aimogen_Chatbot_Settings['max_tokens']) && $aimogen_Chatbot_Settings['max_tokens'] !== '' && is_numeric($aimogen_Chatbot_Settings['max_tokens']))
{
$max_tokens_chatbot = intval($aimogen_Chatbot_Settings['max_tokens']);
if(intval($max_tokens_chatbot) < $max_tokens)
{
$max_tokens = intval($max_tokens_chatbot);
if($max_tokens <= 0)
{
$max_tokens = 1000;
}
}
}
if($is_modern_gpt == '1')
{
$aitext = '';
foreach($input_text as $aimess)
{
if(isset($aimess['content']))
{
if(!is_array($aimess['content']))
{
$aitext .= $aimess['content'] . '\n';
}
else
{
foreach($aimess['content'] as $internalmess)
{
if($internalmess['type'] == 'text')
{
$aitext .= $internalmess['text'] . '\n';
}
}
}
}
}
$query_token_count = count(aimogen_encode($aitext));
$available_tokens = aimogen_compute_available_tokens($model, $max_tokens, $aitext, $query_token_count);
if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
{
$startIndex = intdiv(count($input_text), 2);
$input_text = array_slice($input_text, $startIndex);
$arrx = array_keys($input_text);
$lastindex = end($arrx);
$string_len = strlen($input_text[$lastindex]['content']);
$string_len = $string_len / 2;
$string_len = intval(0 - $string_len);
$input_text[$lastindex]['content'] = aimogen_substr($input_text[$lastindex]['content'], 0, $string_len);
$input_text[$lastindex]['content'] = trim($input_text[$lastindex]['content']);
$aitext = '';
foreach($input_text as $aimess)
{
if(isset($aimess['content']))
{
if(!is_array($aimess['content']))
{
$aitext .= $aimess['content'] . '\n';
}
else
{
foreach($aimess['content'] as $internalmess)
{
if($internalmess['type'] == 'text')
{
$aitext .= $internalmess['text'] . '\n';
}
}
}
}
}
$query_token_count = count(aimogen_encode($aitext));
$available_tokens = $max_tokens - $query_token_count;
}
}
else
{
$query_token_count = count(aimogen_encode($input_text));
$available_tokens = aimogen_compute_available_tokens($model, $max_tokens, $input_text, $query_token_count);
if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
{
$string_len = strlen($input_text);
$string_len = $string_len / 2;
$string_len = intval(0 - $string_len);
$input_text = aimogen_substr($input_text, 0, $string_len);
$input_text = trim($input_text);
if(empty($input_text))
{
$aimogen_result = esc_html__('Empty API seed expression provided (after processing)', 'aimogen');
$this->aimogen_event_exit($aimogen_result);
}
$query_token_count = count(aimogen_encode($input_text));
$available_tokens = $max_tokens - $query_token_count;
}
}
$error = '';
$finish_reason = '';
do_action('aimogen_calling_stream', $input_text, $model);
$zerret = aimogen_generate_text($token, $model, $input_text, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, true, 'shortcodeChat', 0, $finish_reason, $error, $no_internet, $no_embeddings, true, $vision_file, $user_question, 'user', $assistant_id, $thread_id, $embedding_namespace, $function_result, $file_data, false, $store_data, $forced_functions, $mcp_servers);
if($zerret === false && !empty($error))
{
$this->aimogen_event_exit($error);
}
}
else
{
$message = esc_html__('Empty input text provided!', 'aimogen');
$this->aimogen_event_exit($message);
}
}
exit;
}
}
private function aimogen_event_exit($message)
{
echo "event: message\n";
echo 'data: {"error":[{"message":"' . esc_js($message) . '"}]}';
echo "\n\n";
if (ob_get_length())
{
ob_end_flush();
}
flush();
echo 'data: {"choices":[{"finish_reason":"stop"}]}';
echo "\n\n";
if (ob_get_length())
{
ob_end_flush();
}
flush();
exit;
}
}
Aimogen_lite_Streaming::get_instance();
}
if(!class_exists('Aimogen_lite_Claude_Streaming'))
{
class Aimogen_lite_Claude_Streaming
{
private static $instance = null ;
public static function get_instance()
{
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct()
{
add_action('init', [$this, 'aimogen_claude_stream'], 1);
}
public function aimogen_claude_stream()
{
if(isset($_GET['aimogen_claude_stream']) && sanitize_text_field( wp_unslash($_GET['aimogen_claude_stream'])) == 'yes')
{
header('Content-type: text/event-stream');
header('Cache-Control: no-cache');
if (!isset($_REQUEST['nonce']) || !wp_verify_nonce(sanitize_text_field( wp_unslash ($_REQUEST['nonce'])), 'aimogen-streaming-nonce'))
{
$message = esc_html__('You are not allowed to do this action!', 'aimogen');
$this->aimogen_event_exit($message);
}
else
{
if (isset($_REQUEST['input_text']) && !empty($_REQUEST['input_text']))
{
if(!isset($_REQUEST['model']) || !isset($_REQUEST['temp']) || !isset($_REQUEST['top_p']) || !isset($_REQUEST['presence']) || !isset($_REQUEST['frequency']))
{
$message = esc_html__('Incomplete POST request for chat!', 'aimogen');
$this->aimogen_event_exit($message);
}
$aimogen_Main_Settings = get_option('aimogen_Main_Settings', false);
if (!isset($aimogen_Main_Settings['app_id_claude']) || trim($aimogen_Main_Settings['app_id_claude']) == '')
{
$aimogen_result = esc_html__('You need to insert a valid Anthropic Claude API Key for this to work!', 'aimogen');
$this->aimogen_event_exit($aimogen_result);
}
if(isset($_REQUEST['user_token_cap_per_day']))
{
$user_token_cap_per_day = sanitize_text_field( wp_unslash($_REQUEST['user_token_cap_per_day']));
if(!empty($user_token_cap_per_day))
{
$user_token_cap_per_day = intval($user_token_cap_per_day);
}
}
else
{
$user_token_cap_per_day = '';
}
if(isset($_REQUEST['user_id']))
{
$user_id = sanitize_text_field( wp_unslash($_REQUEST['user_id']));
}
else
{
$user_id = '';
}
if(isset($_REQUEST['file_data']))
{
$file_data = sanitize_text_field( wp_unslash($_REQUEST['file_data']));
}
else
{
$file_data = '';
}
$forced_functions = array();
if(isset($_REQUEST['enable_god_mode']))
{
$enable_god_mode = sanitize_text_field( wp_unslash($_REQUEST['enable_god_mode']));
}
else
{
$enable_god_mode = '';
}
if($enable_god_mode === 'on' || $enable_god_mode === 'yes' || $enable_god_mode === 'true' || $enable_god_mode === '1' || $enable_god_mode === 'enable' || $enable_god_mode === 'enabled')
{
$function_result = '';
}
elseif(is_string($enable_god_mode) && !empty($enable_god_mode))
{
$forced_functions = explode(',', $enable_god_mode);
$function_result = '';
}
else
{
$function_result = 'disabled';
}
if(isset($_REQUEST['functions_result']) && trim(sanitize_text_field( wp_unslash($_REQUEST['functions_result']))) != '' && (!isset($_REQUEST['bufferid']) || sanitize_text_field( wp_unslash($_REQUEST['bufferid'])) == ''))
{
$fr = sanitize_text_field( wp_unslash($_REQUEST['functions_result']));
$fr = json_decode($fr, true);
if($fr !== null)
{
$function_result = $fr;
}
else
{
$fr = sanitize_text_field( wp_unslash($_REQUEST['functions_result']));
$fr = json_decode(wp_unslash($fr), true);
if($fr !== null)
{
$function_result = $fr;
}
}
}
$remember_data = false;
if(isset($_REQUEST['functions_result']) && trim(sanitize_text_field( wp_unslash($_REQUEST['functions_result']))) == '0' && (isset($_REQUEST['bufferid']) && sanitize_text_field( wp_unslash($_REQUEST['bufferid'])) != ''))
{
$buffer_id = sanitize_key( wp_unslash( $_REQUEST['bufferid'] ) );
$remember_data = get_transient('aimogen_ai_data_' . $buffer_id);
if(is_array($remember_data))
{
if(isset($remember_data[3]))
{
$fr = wp_unslash($remember_data[3]);
$fr = json_decode($fr, true);
if($fr !== null)
{
$function_result = $fr;
}
}
}
}
if(is_array($function_result) && is_array($function_result[0]))
{
if(isset($_REQUEST['run_id']) && trim(sanitize_text_field( wp_unslash($_REQUEST['run_id']))) != '')
{
for($k = 0; $k < count($function_result); $k++)
{
$function_result[$k]['run_id'] = sanitize_text_field( wp_unslash($_REQUEST['run_id']));
}
}
}
elseif(is_string($function_result) && !empty($function_result))
{
$function_result = array('function_result' => $function_result);
}
if(isset($_REQUEST['store_data']))
{
$store_data = sanitize_text_field(wp_unslash($_REQUEST['store_data']));
}
else
{
$store_data = 'off';
}
$input_text = sanitize_text_field( wp_unslash($_REQUEST['input_text']));
if(isset($_REQUEST['vision_file']))
{
$vision_file = sanitize_text_field( wp_unslash($_REQUEST['vision_file']));
}
else
{
$vision_file = '';
}
$no_internet = false;
$ia_raw = '';
if ( isset( $_REQUEST['internet_access'] ) ) {
$ia_raw = wp_unslash( $_REQUEST['internet_access'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
}
$ia = sanitize_text_field( $ia_raw );
$em_raw = '';
if ( isset( $_REQUEST['embeddings'] ) ) {
$em_raw = wp_unslash( $_REQUEST['embeddings'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
}
$em = sanitize_text_field( $em_raw );
if(isset($ia) && ($ia === 'no' || $ia === '0' || $ia == 'off' || $ia == 'disabled' || $ia == 'Disabled' || $ia == 'disable' || $ia == "false"))
{
$no_internet = true;
}
$no_embeddings = false;
if(isset($em) && ($em === 'no' || $em === '0' || $em == 'off' || $em == 'disabled' || $em == 'disable' || $em == 'Disabled' || $em == "false"))
{
$no_embeddings = true;
}
$user_question = '';
if ( isset( $_REQUEST['user_question'] ) ) {
$user_question = sanitize_textarea_field( wp_unslash( $_REQUEST['user_question'] ) );
}
$aimogen_Chatbot_Settings = get_option('aimogen_Chatbot_Settings', false);
if(isset($_REQUEST['forms_replace']) && trim(sanitize_text_field( wp_unslash($_REQUEST['forms_replace']))) === '1')
{
if (isset($aimogen_Main_Settings['form_prepend']) && trim($aimogen_Main_Settings['form_prepend']) != '')
{
$input_text = trim($aimogen_Main_Settings['form_prepend']) . ' ' . $input_text;
}
if (isset($aimogen_Main_Settings['form_append']) && trim($aimogen_Main_Settings['form_append']) != '')
{
$input_text = $input_text . ' ' . trim($aimogen_Main_Settings['form_append']);
}
$post_id = get_the_ID();
$input_text = aimogen_replaceEmbeddingsAIPostShortcodes($input_text, $post_id);
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) || !is_user_logged_in())
{
$input_text = str_replace('%%user_name%%', '', $input_text);
$input_text = str_replace('%%user_email%%', '' , $input_text);
$input_text = str_replace('%%user_display_name%%', '', $input_text);
$input_text = str_replace('%%user_role%%', '', $input_text);
$input_text = str_replace('%%user_id%%', '' , $input_text);
$input_text = str_replace('%%user_firstname%%', '' , $input_text);
$input_text = str_replace('%%user_lastname%%', '' , $input_text);
$input_text = str_replace('%%user_description%%', '' , $input_text);
$input_text = str_replace('%%user_url%%', '' , $input_text);
}
else
{
$input_text = str_replace('%%user_name%%', $current_user->user_login, $input_text);
$input_text = str_replace('%%user_email%%', $current_user->user_email , $input_text);
$input_text = str_replace('%%user_display_name%%', $current_user->display_name, $input_text);
$input_text = str_replace('%%user_role%%', implode(',', $current_user->roles), $input_text);
$input_text = str_replace('%%user_id%%', $current_user->ID , $input_text);
$input_text = str_replace('%%user_firstname%%', $current_user->user_firstname , $input_text);
$input_text = str_replace('%%user_lastname%%', $current_user->user_lastname , $input_text);
$user_desc = get_the_author_meta( 'description', $current_user->ID );
$input_text = str_replace('%%user_description%%', $user_desc , $input_text);
$user_url = get_the_author_meta( 'user_url', $current_user->ID );
$input_text = str_replace('%%user_url%%', $user_url , $input_text);
}
}
if (isset($aimogen_Chatbot_Settings['max_input_length']) && $aimogen_Chatbot_Settings['max_input_length'] != '' && is_numeric($aimogen_Chatbot_Settings['max_input_length']))
{
if(strlen($input_text) > intval($aimogen_Chatbot_Settings['max_input_length']))
{
$input_text = substr($input_text, 0, intval($aimogen_Chatbot_Settings['max_input_length']));
}
}
if(isset($_REQUEST['pdf_data']))
{
$embedding_namespace = sanitize_text_field( wp_unslash($_REQUEST['pdf_data']));
}
else
{
$embedding_namespace = '';
}
if(empty($embedding_namespace))
{
if (isset($aimogen_Chatbot_Settings['persistent']) && $aimogen_Chatbot_Settings['persistent'] == 'vector')
{
$embedding_namespace = 'persistentchat_' . $user_id;
}
else
{
if(isset($_REQUEST['embeddings_namespace']) && !empty(sanitize_text_field( wp_unslash($_REQUEST['embeddings_namespace']))))
{
$embedding_namespace = sanitize_text_field( wp_unslash($_REQUEST['embeddings_namespace']));
}
}
}
$remember_string = '';
if(isset($_REQUEST['remember_string']))
{
$remember_string = sanitize_text_field( wp_unslash($_REQUEST['remember_string']));
}
if (isset($aimogen_Chatbot_Settings['chatbot_prepend']) && trim($aimogen_Chatbot_Settings['chatbot_prepend']) != '')
{
$remember_string = trim($aimogen_Chatbot_Settings['chatbot_prepend']) . ' ' . $remember_string;
}
if (isset($aimogen_Chatbot_Settings['chatbot_append']) && trim($aimogen_Chatbot_Settings['chatbot_append']) != '')
{
$remember_string = $remember_string . ' ' . trim($aimogen_Chatbot_Settings['chatbot_append']);
}
if(!empty(trim($remember_string)))
{
$input_text = trim($remember_string) . PHP_EOL . $input_text;
}
if(isset($_REQUEST['mcp_servers']))
{
$mcp_servers = sanitize_text_field(wp_unslash($_REQUEST['mcp_servers']));
}
else
{
$mcp_servers = '';
}
$model = sanitize_text_field(wp_unslash($_REQUEST['model']));
$temperature = sanitize_text_field( wp_unslash($_REQUEST['temp']));
$top_p = sanitize_text_field( wp_unslash($_REQUEST['top_p']));
$presence_penalty = sanitize_text_field( wp_unslash($_REQUEST['presence']));
$frequency_penalty = sanitize_text_field( wp_unslash($_REQUEST['frequency']));
$models = aimogen_get_all_models_claude();
if(!in_array($model, $models))
{
$aimogen_result = esc_html__('Invalid model provided: ', 'aimogen') . $model;
$this->aimogen_event_exit($aimogen_result);
}
$temperature = floatval($temperature);
$top_p = floatval($top_p);
$presence_penalty = floatval($presence_penalty);
$frequency_penalty = floatval($frequency_penalty);
if($temperature < 0 || $temperature > 2)
{
$aimogen_result = esc_html__('Invalid temperature provided: ', 'aimogen') . $temperature;
$this->aimogen_event_exit($aimogen_result);
}
if($top_p < 0 || $top_p > 1)
{
$aimogen_result = esc_html__('Invalid top_p provided: ', 'aimogen') . $top_p;
$this->aimogen_event_exit($aimogen_result);
}
if($presence_penalty < -2 || $presence_penalty > 2)
{
$aimogen_result = esc_html__('Invalid presence_penalty provided: ', 'aimogen') . $presence_penalty;
$this->aimogen_event_exit($aimogen_result);
}
if($frequency_penalty < -2 || $frequency_penalty > 2)
{
$aimogen_result = esc_html__('Invalid frequency_penalty provided: ', 'aimogen') . $frequency_penalty;
$this->aimogen_event_exit($aimogen_result);
}
$used_token_count = 0;
if(is_numeric($user_token_cap_per_day))
{
if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
{
/* translators: %s is the login URL */
$aimogen_result = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please log in to continue.', 'aimogen'), array( 'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
$this->aimogen_event_exit($aimogen_result);
}
$used_token_count = get_user_meta($user_id, 'aimogen_used_chat_tokens', true);
if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
{
$used_token_count = intval($used_token_count);
if($used_token_count > $user_token_cap_per_day)
{
$aimogen_result = esc_html__('The daily token count for your user account was exceeded! Please try again tomorrow.', 'aimogen');
$this->aimogen_event_exit($aimogen_result);
}
}
else
{
$used_token_count = 0;
}
}
$appids = preg_split('/\r\n|\r|\n/', trim($aimogen_Main_Settings['app_id']));
$appids = array_filter($appids);
$token = $appids[array_rand($appids)];
$token = apply_filters('aimogen_openai_api_key', $token);
$max_tokens = aimogen_get_max_tokens($model);
$query_token_count = count(aimogen_encode($input_text));
$available_tokens = aimogen_compute_available_tokens($model, $max_tokens, $input_text, $query_token_count);
$thread_id = '';
$error = '';
$finish_reason = '';
do_action('aimogen_calling_stream', $input_text, $model);
aimogen_generate_text($token, $model, $input_text, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, true, 'shortcodeChat', 0, $finish_reason, $error, $no_internet, $no_embeddings, true, $vision_file, $user_question, 'user', '', $thread_id, $embedding_namespace, $function_result, $file_data, false, $store_data, $forced_functions, $mcp_servers);
}
}
exit;
}
}
private function aimogen_event_exit($message)
{
echo "event: message\n";
echo 'data: {"error":[{"message":"' . esc_js($message) . '"}]}';
echo "\n\n";
if (ob_get_length())
{
ob_end_flush();
}
flush();
echo 'data: {"choices":[{"finish_reason":"stop"}]}';
echo "\n\n";
if (ob_get_length())
{
ob_end_flush();
}
flush();
exit;
}
}
Aimogen_lite_Claude_Streaming::get_instance();
}
?>