settings = $settings; } public function get_id(): string { return 'gemini'; } public function get_label(): string { return __( 'Google Gemini', 'acez-seo' ); } public function generate_text( string $prompt, int $max_tokens = 1024 ): string { $api_key = $this->settings->get_api_key( $this->get_id() ); if ( '' === $api_key ) { throw AiProviderException::missing_api_key( $this->get_label() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- message reaches only JS textContent/window.alert plain-text sinks, never HTML (see AiProviderException docblock). } $url = self::API_BASE . rawurlencode( $this->model() ) . ':generateContent?key=' . rawurlencode( $api_key ); $response = wp_remote_post( $url, array( // Long-form output (M62 articles) needs materially more wall // time than a one-line meta field — same reasoning as the // 60s vision timeout below. 'timeout' => $max_tokens > 1024 ? 90 : 30, 'headers' => array( 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( array( 'contents' => array( array( 'parts' => array( array( 'text' => $prompt ), ), ), ), 'generationConfig' => array( 'maxOutputTokens' => $max_tokens, ), ) ), ) ); return $this->parse_response( $response ); } public function generate_from_image( string $prompt, ImagePayload $image ): string { $api_key = $this->settings->get_api_key( $this->get_id() ); if ( '' === $api_key ) { throw AiProviderException::missing_api_key( $this->get_label() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- message reaches only JS textContent/window.alert plain-text sinks, never HTML (see AiProviderException docblock). } $url = self::API_BASE . rawurlencode( $this->model() ) . ':generateContent?key=' . rawurlencode( $api_key ); $response = wp_remote_post( $url, array( 'timeout' => 60, 'headers' => array( 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( array( 'contents' => array( array( 'parts' => array( array( 'text' => $prompt ), array( 'inline_data' => array( 'mime_type' => $image->mime_type, 'data' => $image->base64, ), ), ), ), ), ) ), ) ); return $this->parse_response( $response ); } public function test_connection(): void { $result = $this->generate_text( 'Reply with one word: OK.' ); if ( '' === $result ) { throw AiProviderException::unexpected_response( $this->get_label() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- message reaches only JS textContent/window.alert plain-text sinks, never HTML (see AiProviderException docblock). } } /** * @return list */ public function list_models(): array { $api_key = $this->settings->get_api_key( $this->get_id() ); if ( '' === $api_key ) { throw AiProviderException::missing_api_key( $this->get_label() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- message reaches only JS textContent/window.alert plain-text sinks, never HTML (see AiProviderException docblock). } $response = wp_remote_get( 'https://generativelanguage.googleapis.com/v1beta/models?key=' . rawurlencode( $api_key ), // phpcs:ignore PluginCheck.CodeAnalysis.AIProvider.DirectIntegration -- see the class-const declaration above for the full rationale (M67). array( 'timeout' => 20 ) ); if ( is_wp_error( $response ) ) { throw AiProviderException::connection_failed( $this->get_label() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- message reaches only JS textContent/window.alert plain-text sinks, never HTML (see AiProviderException docblock). } $status = (int) wp_remote_retrieve_response_code( $response ); $body = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( $status < 200 || $status >= 300 || ! is_array( $body ) ) { $detail = is_array( $body ) ? (string) ( $body['error']['message'] ?? '' ) : ''; throw AiProviderException::from_http_status( $this->get_label(), $status, $detail ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- message reaches only JS textContent/window.alert plain-text sinks, never HTML (see AiProviderException docblock). } $models = is_array( $body['models'] ?? null ) ? $body['models'] : array(); // Only text-generation-capable models make sense here — the API also // lists embedding/vision-only entries. The Gemini API does not expose // a release date, so "10 last" is the first 10 matching entries in // the order Google returns them (no reliable recency sort available). $models = array_filter( $models, static function ( $model ): bool { $methods = is_array( $model ) && is_array( $model['supportedGenerationMethods'] ?? null ) ? $model['supportedGenerationMethods'] : array(); return in_array( 'generateContent', $methods, true ); } ); $models = array_slice( array_values( $models ), 0, 10 ); return array_map( static function ( $model ): array { $name = (string) ( $model['name'] ?? '' ); $id = str_starts_with( $name, 'models/' ) ? substr( $name, strlen( 'models/' ) ) : $name; return array( 'id' => $id, 'label' => '' !== (string) ( $model['displayName'] ?? '' ) ? (string) $model['displayName'] : $id, ); }, $models ); } /** * @param \WP_Error|array $response */ private function parse_response( $response ): string { if ( is_wp_error( $response ) ) { throw AiProviderException::connection_failed( $this->get_label() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- message reaches only JS textContent/window.alert plain-text sinks, never HTML (see AiProviderException docblock). } $status = (int) wp_remote_retrieve_response_code( $response ); $body = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( $status < 200 || $status >= 300 ) { $detail = is_array( $body ) ? (string) ( $body['error']['message'] ?? '' ) : ''; throw AiProviderException::from_http_status( $this->get_label(), $status, $detail ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- message reaches only JS textContent/window.alert plain-text sinks, never HTML (see AiProviderException docblock). } $text = is_array( $body ) ? ( $body['candidates'][0]['content']['parts'][0]['text'] ?? null ) : null; if ( ! is_string( $text ) || '' === trim( $text ) ) { throw AiProviderException::unexpected_response( $this->get_label() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- message reaches only JS textContent/window.alert plain-text sinks, never HTML (see AiProviderException docblock). } return trim( $text ); } private function model(): string { $model = $this->settings->get_model( $this->get_id() ); return '' !== $model ? $model : self::DEFAULT_MODEL; } }