config_manager = new ChatGPT_Config_Manager( $api_key ); $this->prompt_manager = new ChatGPT_Prompt_Manager( $custom_prompt ); // Initialize API client if config is available. if ( $this->config_manager->is_available() ) { $this->api_client = new ChatGPT_API_Client( $this->config_manager->get_api_key() ); $this->text_processor = new ChatGPT_Text_Processor( $this->api_client, $this->prompt_manager ); $this->api_tester = new ChatGPT_API_Tester( $this->text_processor, $this->config_manager ); } } /** * Check if the fine tuner is available (has API key). * * @return bool True if available. */ public function is_available() { return $this->config_manager && $this->config_manager->is_available(); } /** * Fine-tune text for better audio generation. * * @param string $text Text to fine-tune. * @return array Result with fine-tuned text and usage data. */ public function fine_tune_text( $text ) { if ( ! \Accessibility_Audio_TTS\Core\Privacy_Settings::openai_enabled() || ! $this->text_processor ) { accessibility_audio_tts_debug_log( 'ChatGPT text processor not available - skipping text optimization', true ); return array( 'success' => true, 'text' => $text, 'optimized' => false, 'usage' => null, ); } return $this->text_processor->fine_tune_text( $text ); } /** * Test API connection. * * @return array Test result. */ public function test_api_connection() { if ( ! $this->api_tester ) { return array( 'success' => false, 'error' => 'API tester not available', ); } return $this->api_tester->test_api_connection(); } /** * Get default prompt for text optimization. * * @return string Default prompt. */ public function get_default_prompt() { return $this->prompt_manager ? $this->prompt_manager->get_default_prompt() : ''; } /** * Validate API key format. * * @param string $api_key API key to validate. * @return bool True if valid. */ public static function validate_api_key( $api_key ) { $config_manager = new ChatGPT_Config_Manager(); return $config_manager->validate_api_key( $api_key ); } /** * Update API settings. * * @param string $api_key API key. * @param string $custom_prompt Custom prompt. * @return bool Success status. */ public function update_settings( $api_key, $custom_prompt = '' ) { $success = true; if ( $this->config_manager ) { $success = $this->config_manager->update_api_key( $api_key ) && $success; } if ( $this->prompt_manager ) { $success = $this->prompt_manager->update_prompt( $custom_prompt ) && $success; } // Reinitialize components if API key changed. if ( $success && $this->config_manager->is_available() ) { $this->api_client = new ChatGPT_API_Client( $this->config_manager->get_api_key() ); $this->text_processor = new ChatGPT_Text_Processor( $this->api_client, $this->prompt_manager ); $this->api_tester = new ChatGPT_API_Tester( $this->text_processor, $this->config_manager ); } return $success; } /** * Get current settings. * * @return array Current settings. */ public function get_settings() { $config_settings = $this->config_manager ? $this->config_manager->get_settings() : array(); $prompt_settings = $this->prompt_manager ? $this->prompt_manager->get_prompt_settings() : array(); return array_merge( $config_settings, $prompt_settings ); } /** * Clean and reset API key (fix for corrupted 164-character key). * * @param string $new_key New API key. * @return bool Success status. */ public static function clean_and_reset_api_key( $new_key ) { $config_manager = new ChatGPT_Config_Manager(); return $config_manager->clean_and_reset_api_key( $new_key ); } }