[model_id => display_label] */ public static function get_model_choices( $provider ) { if ( self::PROVIDER_ANTHROPIC === $provider ) { return array( 'claude-opus-4-7' => 'Claude Opus 4.7', 'claude-sonnet-4-6' => 'Claude Sonnet 4.6', 'claude-haiku-4-5-20251001' => 'Claude Haiku 4.5', ); } return array( 'gpt-4o' => 'GPT-4o', 'gpt-4-turbo' => 'GPT-4 Turbo', 'gpt-3.5-turbo' => 'GPT-3.5 Turbo', ); } /** * Return $model if it is valid for the provider, else fall back to the * provider's default. Prevents stale model ids leaking across providers * after a switch. */ public static function validate_model_for_provider( $model, $provider ) { $choices = self::get_model_choices( $provider ); if ( is_string( $model ) && '' !== $model && isset( $choices[ $model ] ) ) { return $model; } return self::get_default_model( $provider ); } /** * Convert an OpenAI-style messages array (with possible `system` items) * into Anthropic's required shape: a single `system` string plus * strictly-alternating user/assistant `messages`. * * @param array $openai_messages [{role: 'system'|'user'|'assistant', content: string}, ...] * @return array ['system' => string, 'messages' => array] */ public static function convert_messages_for_anthropic( $openai_messages ) { $system_chunks = array(); $messages = array(); if ( ! is_array( $openai_messages ) ) { return array( 'system' => '', 'messages' => array() ); } foreach ( $openai_messages as $msg ) { if ( ! is_array( $msg ) || ! isset( $msg['role'], $msg['content'] ) ) { continue; } $role = (string) $msg['role']; $content = is_string( $msg['content'] ) ? $msg['content'] : ''; if ( 'system' === $role ) { if ( '' !== trim( $content ) ) { $system_chunks[] = $content; } continue; } if ( 'user' !== $role && 'assistant' !== $role ) { continue; } // Merge consecutive same-role items into a single turn. $last_index = count( $messages ) - 1; if ( $last_index >= 0 && $messages[ $last_index ]['role'] === $role ) { $messages[ $last_index ]['content'] .= "\n\n" . $content; } else { $messages[] = array( 'role' => $role, 'content' => $content, ); } } return array( 'system' => implode( "\n\n", $system_chunks ), 'messages' => $messages, ); } /** * Parse a provider error response body into a user-safe message and log * full detail when WP_DEBUG_LOG is on. Never logs the API key. */ public static function parse_error_body( $body, $provider, $http_code = 0 ) { $data = is_string( $body ) ? json_decode( $body, true ) : null; if ( self::PROVIDER_ANTHROPIC === $provider ) { $message = ''; if ( is_array( $data ) ) { if ( isset( $data['error']['message'] ) ) { $message = (string) $data['error']['message']; } elseif ( isset( $data['message'] ) ) { $message = (string) $data['message']; } } if ( '' === $message ) { $message = __( 'Unknown error occurred.', 'a1ai-chatbot' ); } } else { $message = isset( $data['error']['message'] ) ? (string) $data['error']['message'] : __( 'Unknown error occurred.', 'a1ai-chatbot' ); } if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Debug logging gated behind WP_DEBUG error_log( sprintf( 'A1AI [%s]: API error [%d] %s', $provider, (int) $http_code, $message ) ); } return sanitize_text_field( $message ); } }