session = Context::load_json( self::SESSION_FILE ) ?: []; } private function save_session(): void { Context::save_json( self::SESSION_FILE, $this->session ); } public function __construct() { $json_path = AIPUB_DIR . 'includes/data/brand_strategy_elements.json'; $raw = file_get_contents( $json_path ); $this->elements = json_decode( $raw, true )['brand_strategy_elements'] ?? []; $this->api_key = get_option( 'aipub_openai_api_key', '' ); $this->model = get_option( 'aipub_ai_model', 'o1' ); } /** * Generate all brand strategy elements and stream results per element. * * Streams each element as a JSON line: { field: name, content: text }. * Saves to storage once complete. * * @throws \Exception */ public function generate_stream(): void { $prev_zlib = ini_get( 'zlib.output_compression' ); aipub_with_time_limit( self::TIMEOUT_SECS, function () { if ( function_exists( 'ini_set' ) ) { // phpcs:ignore WordPress.PHP.DiscouragedFunctions.Discouraged,Squiz.PHP.DiscouragedFunctions.Discouraged -- disable compression for this stream @ini_set( 'zlib.output_compression', 'Off' ); } if ( function_exists( 'apache_setenv' ) ) { @apache_setenv( 'no-gzip', '1' ); } while ( ob_get_level() ) { @ob_end_flush(); } header( 'Content-Type: text/plain; charset=utf-8' ); if ( empty( $this->api_key ) ) { header( 'HTTP/1.1 400 Bad Request' ); echo 'OpenAI API key not configured.'; return; } $files = $this->collect_site_data(); $thread_id = $this->create_thread(); $vector_store_id = $this->create_vector_store( $files ); $assistant_id = $this->create_assistant( $vector_store_id ); $results = []; foreach ( $this->elements as $i => $element ) { $this->add_message( $thread_id, $element['user_message'] ); $tokens = $i >= 7 ? 100000 : 300000; $search = true; $raw = $this->run_and_fetch_retry( $thread_id, $assistant_id, $element, $tokens, $search ); $clean = $this->clean( $raw ); $results[ $element['name'] ] = $clean; echo wp_json_encode( [ 'field' => $element['name'], 'content' => $clean ] ) . "\n"; @flush(); } update_option( 'aipub_brand_strategy', $results ); Context::save_json( 'brand-strategy.json', $results ); update_option( 'aipub_brand_complete', 'yes' ); $this->delete_assistant( $assistant_id ); $this->delete_vector_store( $vector_store_id ); $this->delete_thread( $thread_id ); } ); if ( function_exists( 'ini_set' ) ) { // phpcs:ignore WordPress.PHP.DiscouragedFunctions.Discouraged,Squiz.PHP.DiscouragedFunctions.Discouraged -- restore previous compression setting @ini_set( 'zlib.output_compression', $prev_zlib ); } return; } /** * Generate all brand strategy elements. * * @return array * @throws \Exception */ public function generate() { return aipub_with_time_limit( self::TIMEOUT_SECS, function () { if ( empty( $this->api_key ) ) { throw new \Exception( 'OpenAI API key not configured.' ); } $files = $this->collect_site_data(); $thread_id = $this->create_thread(); $vector_store_id = $this->create_vector_store( $files ); $assistant_id = $this->create_assistant( $vector_store_id ); $results = []; foreach ( $this->elements as $i => $element ) { $this->add_message( $thread_id, $element['user_message'] ); $tokens = $i >= 7 ? 100000 : 300000; $search = true; $raw = $this->run_and_fetch_retry( $thread_id, $assistant_id, $element, $tokens, $search ); $results[ $element['name'] ] = $this->clean( $raw ); } update_option( 'aipub_brand_strategy', $results ); Context::save_json( 'brand-strategy.json', $results ); // Mark the Brand Strategy wizard as complete after generation update_option( 'aipub_brand_complete', 'yes' ); $this->delete_assistant( $assistant_id ); $this->delete_vector_store( $vector_store_id ); $this->delete_thread( $thread_id ); return $results; } ); } /** * Gather pages, categories, tags, and user attachments. * * @return string[] File paths. */ protected function collect_site_data(): array { // Use the shared Context directory for storing JSON artefacts. $upload_dir = Context::dir(); $files = []; // Pages: fetch front-end HTML, extract main content between and