settings = $settings; } public function get_id(): string { return 'anthropic'; } public function get_label(): string { return __( 'Anthropic (Claude)', 'acez-seo' ); } public function generate_text( string $prompt, int $max_tokens = 1024 ): string { $response = wp_remote_post( self::API_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' => $this->headers(), 'body' => wp_json_encode( array( 'model' => $this->model(), 'max_tokens' => $max_tokens, 'messages' => array( array( 'role' => 'user', 'content' => $prompt ), ), ) ), ) ); return $this->parse_response( $response ); } /** * Anthropic takes the image as a base64 `source` block, unlike OpenAI's * `image_url` data URI — the block order (image first, then the prompt) * matches what this class sent through the SDK. */ public function generate_from_image( string $prompt, ImagePayload $image ): string { $response = wp_remote_post( self::API_URL, array( 'timeout' => 60, 'headers' => $this->headers(), 'body' => wp_json_encode( array( 'model' => $this->model(), 'max_tokens' => 1024, 'messages' => array( array( 'role' => 'user', 'content' => array( array( 'type' => 'image', 'source' => array( 'type' => 'base64', 'media_type' => $image->mime_type, 'data' => $image->base64, ), ), array( 'type' => 'text', 'text' => $prompt ), ), ), ), ) ), ) ); 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 { $response = wp_remote_get( add_query_arg( array( 'limit' => 10 ), self::MODELS_URL ), array( 'timeout' => 20, 'headers' => $this->headers(), ) ); 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 = array(); // The Models API lists newest-released models first — no manual // sorting needed, unlike the OpenAI/Gemini REST endpoints. On the wire // the label field is snake_case `display_name` (the SDK exposed it as // `displayName`); fall back to the id so a future field rename can // never produce blank labels in the picker. foreach ( (array) ( $body['data'] ?? array() ) as $model_info ) { if ( ! is_array( $model_info ) ) { continue; } $id = (string) ( $model_info['id'] ?? '' ); if ( '' === $id ) { continue; } $label = (string) ( $model_info['display_name'] ?? '' ); $models[] = array( 'id' => $id, 'label' => '' !== $label ? $label : $id, ); } return $models; } /** * @return array */ private function headers(): 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). } return array( 'x-api-key' => $api_key, 'anthropic-version' => self::API_VERSION, 'Content-Type' => 'application/json', ); } /** * Mirrors the SDK's exception mapping this class used to rely on: a * connection failure, a timeout, an HTTP error status (with Anthropic's * own `error.message` when present) and an unusable body each raise the * same AiProviderException factory as before. * * @param \WP_Error|array $response */ private function parse_response( $response ): string { if ( is_wp_error( $response ) ) { if ( 'http_request_failed' === $response->get_error_code() && str_contains( $response->get_error_message(), 'timed out' ) ) { throw AiProviderException::request_failed( $this->get_label(), __( 'the request timed out', 'acez-seo' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- message reaches only JS textContent/window.alert plain-text sinks, never HTML (see AiProviderException docblock). } 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). } // `content` is a list of blocks; a response may open with a non-text // block (thinking, tool use), so take the FIRST text one rather than // index 0 — this is what the SDK loop did. foreach ( (array) ( is_array( $body ) ? ( $body['content'] ?? array() ) : array() ) as $block ) { if ( is_array( $block ) && 'text' === ( $block['type'] ?? '' ) && is_string( $block['text'] ?? null ) && '' !== trim( $block['text'] ) ) { return trim( $block['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). } private function model(): string { $model = $this->settings->get_model( $this->get_id() ); return '' !== $model ? $model : self::DEFAULT_MODEL; } }