[ 'id' => 'gemini-3.8-flash', 'name' => 'Gemini 3.8 Flash', 'context' => 1000000, 'input_price' => 0.75, 'output_price' => 3.75, ], 'gemini-3.7-flash' => [ 'id' => 'gemini-3.7-flash', 'name' => 'Gemini 3.7 Flash', 'context' => 1000000, 'input_price' => 0.75, 'output_price' => 3.75, ], 'gemini-3.6-flash' => [ 'id' => 'gemini-3.6-flash', 'name' => 'Gemini 3.6 Flash', 'context' => 1000000, 'input_price' => 0.75, 'output_price' => 3.75, ], 'gemini-2.5-flash' => [ 'id' => 'gemini-2.5-flash', 'name' => 'Gemini 2.5 Flash', 'context' => 1000000, 'input_price' => 0.15, 'output_price' => 0.60, ], ]; } public function get_api_key(): string { $options = get_option( 'aibp_settings', [] ); return $options['google_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__( 'Google Gemini API key is not configured.', 'ai-bulk-post' ) ); } $model = $args['model'] ?? 'gemini-2.5-flash'; $request_body = [ 'contents' => [ [ 'parts' => [ ['text' => $prompt], ], ], ], 'generationConfig' => [ 'temperature' => $args['temperature'] ?? 0.7, 'maxOutputTokens' => $args['max_tokens'] ?? 4000, ], ]; $url = sprintf( '%s/models/%s:generateContent?key=%s', self::api_base_url(), $model, $api_key ); $response = wp_remote_post( $url, [ 'headers' => ['Content-Type' => 'application/json'], 'body' => wp_json_encode( $request_body ), '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( 'Google Gemini API error (%d): %s', $code, $message ) ) ); } return trim( $body['candidates'][0]['content']['parts'][0]['text'] ?? '' ); } public function validate_key( string $api_key ): bool { $response = wp_remote_get( sprintf( '%s/models?key=%s', self::api_base_url(), $api_key ), ['timeout' => 15] ); if ( is_wp_error( $response ) ) { return false; } return wp_remote_retrieve_response_code( $response ) === 200; } }