'TransformX FAQPage', 'instructions' => $block['system_message'], 'tools' => [ [ 'type' => 'file_search' ] ], 'tool_resources' => [ 'file_search' => [ 'vector_store_ids' => [ $vector_id ] ] ], 'model' => $model, ]; if ( in_array( $model, [ 'o3-mini', 'o1' ], true ) ) { $payload['reasoning_effort'] = 'medium'; } else { $payload['temperature'] = 0.5; } $assistant_id = OpenAI::create_assistant( $payload ); OpenAI::add_message( $thread_id, 'user', $block['user_message'] ); $run_id = OpenAI::run_assistant( $thread_id, $assistant_id, $block['assistant_instructions'], [ [ 'type' => 'file_search' ] ] ); try { OpenAI::wait_until_complete( $thread_id, $run_id ); } catch ( \RuntimeException $e ) { try { OpenAI::wait_until_complete( $thread_id, $run_id ); } catch ( \RuntimeException $e2 ) { self::cleanup( $assistant_id, $vector_id, $thread_id ); throw new \RuntimeException( 'FAQ generation failed: ' . esc_html( $e2->getMessage() ) ); } } $resp = OpenAI::latest_assistant_message( $thread_id ); $data = json_decode( $resp, true ); if ( ! is_array( $data ) ) { throw new \RuntimeException( 'Failed to decode FAQ JSON: ' . esc_html( json_last_error_msg() ) ); } Context::save_json( 'faq-schema.json', $data ); update_post_meta( $post_id, '_aipub_faq_schema', wp_json_encode( $data ) ); self::cleanup( $assistant_id, $vector_id, $thread_id ); return $data; } /** * Launch asynchronous FAQ generation for a post via non-blocking HTTP. * If the background request fails (e.g. due to blocked HTTP), the job is * processed immediately as a fallback so FAQ creation isn't reliant on * cron. * * @param int $post_id WordPress post ID. * @return string Job identifier for polling. */ public static function start_async_job( int $post_id ): string { $job_id = wp_generate_uuid4(); $session = [ 'job_id' => $job_id, 'job_status' => 'queued', 'post_id' => $post_id, ]; Context::save_json( 'faq-session.json', $session ); $url = add_query_arg( [ 'action' => 'aipub_process_faq_job', 'job_id' => rawurlencode( $job_id ), 'nonce' => wp_create_nonce( 'aipub_process_faq_job' ), ], admin_url( 'admin-ajax.php' ) ); // Only send WP login cookies so the AJAX call authenticates properly. $cookies = []; $cookie_names = [ 'wordpress_logged_in_' . COOKIEHASH, 'wordpress_' . COOKIEHASH, ]; foreach ( $cookie_names as $name ) { if ( isset( $_COOKIE[ $name ] ) ) { $cookies[] = new \WP_Http_Cookie( [ 'name' => sanitize_key( wp_unslash( $name ) ), 'value' => sanitize_text_field( wp_unslash( $_COOKIE[ $name ] ) ), ] ); } } $resp = wp_remote_post( $url, [ 'blocking' => false, 'timeout' => 0.01, 'cookies' => $cookies, ] ); // Fallback if the async HTTP request failed. This avoids relying on // WP Cron so FAQ generation completes even when non-blocking requests // are blocked or return unexpected data. if ( is_wp_error( $resp ) || ! is_array( $resp ) ) { self::process_job( $job_id ); } return $job_id; } /** * Process an async FAQ job triggered via HTTP request. * * @param string $job_id Job identifier. */ public static function process_job( string $job_id ): void { ignore_user_abort( true ); $session = Context::load_json( 'faq-session.json' ) ?: []; if ( ( $session['job_id'] ?? '' ) !== $job_id ) { return; } $session['job_status'] = 'running'; Context::save_json( 'faq-session.json', $session ); try { $data = self::generate( (int) $session['post_id'] ); $session['job_status'] = 'done'; $session['result'] = $data; Context::save_json( 'faq-session.json', $session ); } catch ( \Throwable $e ) { $session['job_status'] = 'error'; $session['error'] = $e->getMessage(); Context::save_json( 'faq-session.json', $session ); } } /** * AJAX entry point for processing a queued FAQ job. */ public static function ajax_process_job(): void { check_ajax_referer( 'aipub_process_faq_job', 'nonce' ); $job_id = isset( $_REQUEST['job_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['job_id'] ) ) : ''; if ( ! $job_id ) { wp_die( 'Missing job ID', 400 ); } self::process_job( $job_id ); wp_die(); } /** Clean up OpenAI resources. */ private static function cleanup( string $assistant_id, string $vector_id, string $thread_id ): void { OpenAI::delete_assistant( $assistant_id ); OpenAI::delete_thread( $thread_id ); OpenAI::delete_vector_store( $vector_id ); } /** Load prompts from JSON file. */ private static function load_faq_prompts(): array { static $prompts = null; if ( null === $prompts ) { $path = AIPUB_DIR . 'includes/data/faqpage_prompts.json'; if ( ! file_exists( $path ) ) { throw new \RuntimeException( 'Missing faqpage_prompts.json file.' ); } $data = json_decode( file_get_contents( $path ), true ); if ( ! is_array( $data ) ) { throw new \RuntimeException( 'Invalid JSON in faqpage_prompts.json: ' . esc_html( json_last_error_msg() ) ); } $prompts = $data; } return $prompts; } } // Register async FAQ processor for internal requests. add_action( 'wp_ajax_aipub_process_faq_job', [ '\\AiPublisher\\Faq', 'ajax_process_job' ] ); add_action( 'wp_ajax_nopriv_aipub_process_faq_job', [ '\\AiPublisher\\Faq', 'ajax_process_job' ] );