[ 'id' => 'gpt-6-astra', 'name' => 'GPT-6 Astra (Flagship)', 'context' => 1050000, 'input_price' => 10.00, 'output_price' => 50.00, ], 'gpt-5.6-sol' => [ 'id' => 'gpt-5.6-sol', 'name' => 'GPT-5.6 Sol (Flagship)', 'context' => 1050000, 'input_price' => 5.00, 'output_price' => 30.00, ], 'gpt-5.6-terra' => [ 'id' => 'gpt-5.6-terra', 'name' => 'GPT-5.6 Terra (Balanced)', 'context' => 1050000, 'input_price' => 2.00, 'output_price' => 12.00, ], 'gpt-5.6-luna' => [ 'id' => 'gpt-5.6-luna', 'name' => 'GPT-5.6 Luna (Cost-efficient)', 'context' => 1050000, 'input_price' => 0.20, 'output_price' => 1.20, ], 'gpt-4o' => [ 'id' => 'gpt-4o', 'name' => 'GPT-4o (Legacy)', 'context' => 128000, 'input_price' => 2.50, 'output_price' => 10.00, ], ]; } public function get_api_key(): string { $options = get_option( 'aibp_settings', [] ); return $options['openai_api_key'] ?? ''; } public function generate( string $prompt, array $args = [] ): string { $api_key = $this->get_api_key(); if ( empty( $api_key ) ) { throw new \Exception( esc_html__( 'OpenAI API key is not configured.', 'ai-bulk-post' ) ); } $defaults = [ 'model' => 'gpt-5.6-luna', 'messages' => [ [ 'role' => 'user', 'content' => $prompt, ], ], 'temperature' => $args['temperature'] ?? 0.7, 'max_tokens' => $args['max_tokens'] ?? 4000, 'frequency_penalty' => 0, 'presence_penalty' => 0, ]; $request_args = wp_parse_args( $args, $defaults ); unset( $request_args['temperature'], $request_args['max_tokens'] ); $response = wp_remote_post( self::api_base_url() . '/chat/completions', [ 'headers' => [ 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', ], 'body' => wp_json_encode( $request_args ), 'timeout' => 120, ] ); if ( is_wp_error( $response ) ) { throw new \Exception( esc_html( $response->get_error_message() ) ); } $code = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $code !== 200 ) { $message = $body['error']['message'] ?? esc_html__( 'Unknown API error.', 'ai-bulk-post' ); throw new \Exception( esc_html( sprintf( 'OpenAI API error (%d): %s', $code, $message ) ) ); } return trim( $body['choices'][0]['message']['content'] ?? '' ); } public function validate_key( string $api_key ): bool { $response = wp_remote_get( self::api_base_url() . '/models', [ 'headers' => [ 'Authorization' => 'Bearer ' . $api_key, ], 'timeout' => 15, ] ); if ( is_wp_error( $response ) ) { return false; } return wp_remote_retrieve_response_code( $response ) === 200; } }