$provider, 'from' => $from, 'to' => $to, 'time' => current_time( 'mysql' ), ), false ); $option = ( 'anthropic' === $provider ) ? 'askbot_anthropic_model' : 'askbot_openai_model'; if ( get_option( $option, false ) === $from ) { update_option( $option, $to ); } } add_action( 'admin_notices', 'askbot_model_fallback_notice' ); function askbot_model_fallback_notice() { $fb = get_option( 'askbot_model_fallback', false ); if ( ! is_array( $fb ) || ! current_user_can( 'manage_options' ) ) { return; } $en = ( askbot_admin_lang() === 'en' ); $who = ( 'anthropic' === ( $fb['provider'] ?? '' ) ) ? 'Anthropic' : 'OpenAI'; $msg = $en ? sprintf( 'Youjeeb: the AI model "%1$s" is no longer available at %2$s. The chatbot now automatically uses "%3$s" and keeps answering. Check the AI model in the Youjeeb settings, and update the plugin when a new version is available.', $fb['from'] ?? '', $who, $fb['to'] ?? '' ) : sprintf( 'Youjeeb : le modèle IA « %1$s » n\'est plus disponible chez %2$s. Le chatbot utilise désormais automatiquement « %3$s » et continue de répondre. Vérifiez le modèle IA dans les réglages Youjeeb, et mettez à jour le plugin dès qu\'une nouvelle version est disponible.', $fb['from'] ?? '', $who, $fb['to'] ?? '' ); $url = wp_nonce_url( admin_url( 'admin.php?page=askbot-settings&askbot_dismiss_fallback=1' ), 'askbot_dismiss_fallback' ); echo '

' . esc_html( $msg ) . ' ' . esc_html( $en ? 'Dismiss' : 'Masquer ce message' ) . '

'; } add_action( 'admin_init', 'askbot_maybe_dismiss_fallback_notice' ); function askbot_maybe_dismiss_fallback_notice() { if ( ! isset( $_GET['askbot_dismiss_fallback'], $_GET['_wpnonce'] ) || ! current_user_can( 'manage_options' ) ) { return; } if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'askbot_dismiss_fallback' ) ) { delete_option( 'askbot_model_fallback' ); } } /** * Appel Chat Completions OpenAI, compatible avec les modèles à raisonnement * (GPT-6) : raisonnement désactivé pour des réponses rapides, longueur limitée * par max_completion_tokens. * * Protections : * - modèle retiré (404 / model_not_found) → nouvel essai avec le modèle par défaut ; * - réponse vide coupée par la limite → nouvel essai sans limite. * * @param string $api_key Clé API. * @param string $model Modèle demandé. * @param array $messages Messages Chat Completions. * @param int $max_tokens Longueur maximale de la réponse. * @param int $timeout Délai en secondes. * @return array{ok:bool,content:string,error:string,model:string} */ function askbot_openai_chat_request( $api_key, $model, $messages, $max_tokens, $timeout = 60 ) { $send = function ( $model, $with_limit ) use ( $api_key, $messages, $max_tokens, $timeout ) { $payload = array( 'model' => $model, 'messages' => $messages, 'reasoning_effort' => 'none', ); if ( $with_limit ) { $payload['max_completion_tokens'] = (int) $max_tokens; } $body = wp_json_encode( $payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE ); if ( false === $body ) { return array( 'wp_error' => '❌ Erreur d\'encodage du message.' ); } $response = wp_remote_post( 'https://api.openai.com/v1/chat/completions', array( 'timeout' => $timeout, 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', ), 'body' => $body, ) ); if ( is_wp_error( $response ) ) { return array( 'wp_error' => '❌ Erreur de connexion: ' . $response->get_error_message() ); } return array( 'code' => wp_remote_retrieve_response_code( $response ), 'data' => json_decode( wp_remote_retrieve_body( $response ), true ), ); }; $fail = function ( $error, $model ) { return array( 'ok' => false, 'content' => '', 'error' => $error, 'model' => $model, ); }; $r = $send( $model, true ); if ( isset( $r['wp_error'] ) ) { return $fail( $r['wp_error'], $model ); } if ( $model !== ASKBOT_OPENAI_DEFAULT_MODEL && askbot_is_model_unavailable_error( $r['code'], $r['data'] ) ) { askbot_record_model_fallback( 'openai', $model, ASKBOT_OPENAI_DEFAULT_MODEL ); $model = ASKBOT_OPENAI_DEFAULT_MODEL; $r = $send( $model, true ); if ( isset( $r['wp_error'] ) ) { return $fail( $r['wp_error'], $model ); } } $content = $r['data']['choices'][0]['message']['content'] ?? null; $finish = $r['data']['choices'][0]['finish_reason'] ?? ''; if ( is_string( $content ) && '' === trim( $content ) && 'length' === $finish ) { $r2 = $send( $model, false ); if ( ! isset( $r2['wp_error'] ) ) { $r = $r2; $content = $r['data']['choices'][0]['message']['content'] ?? null; } } if ( isset( $r['data']['error'] ) ) { $m = is_array( $r['data']['error'] ) ? ( $r['data']['error']['message'] ?? '' ) : (string) $r['data']['error']; return $fail( '❌ Erreur OpenAI: ' . $m, $model ); } if ( is_string( $content ) && '' !== trim( $content ) ) { return array( 'ok' => true, 'content' => $content, 'error' => '', 'model' => $model, ); } return $fail( '❌ Réponse invalide de l\'API.', $model ); }