wppaste
WordPress

WP_AI_Client_Ability_Function_Resolver

Since
7.0.0
Source
wp-includes/ai-client/class-wp-ai-client-ability-function-resolver.php:25
Resolves and executes WordPress Abilities API function calls from AI models.

Description

This class must be instantiated with the specific abilities that the AI model is allowed to execute, ensuring that only explicitly specified abilities can be called. This prevents the model from executing arbitrary abilities.

Compatibility

WordPress
since 7.0.0
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 7.0.0.

Properties · 1

$allowed_abilitiesarray<string,private
Map of allowed ability names for this instance.

Methods · 7

Source code

class WP_AI_Client_Ability_Function_Resolver { 	/**	 * Prefix used to identify ability function calls.	 *	 * @since 7.0.0	 * @var string	 */	private const ABILITY_PREFIX = 'wpab__'; 	/**	 * Map of allowed ability names for this instance.	 *	 * Keys are ability name strings, values are `true` for O(1) lookup.	 *	 * @since 7.0.0	 * @var array<string, true>	 */	private array $allowed_abilities; 	/**	 * Constructor.	 *	 * @since 7.0.0	 *	 * @param WP_Ability|string ...$abilities The abilities that this resolver is allowed to execute.	 */	public function __construct( ...$abilities ) {		$this->allowed_abilities = array(); 		foreach ( $abilities as $ability ) {			if ( $ability instanceof WP_Ability ) {				$this->allowed_abilities[ $ability->get_name() ] = true;			} elseif ( is_string( $ability ) ) {				$this->allowed_abilities[ $ability ] = true;			}		}	} 	/**	 * Checks if a function call is an ability call.	 *	 * @since 7.0.0	 *	 * @param FunctionCall $call The function call to check.	 * @return bool True if the function call is an ability call, false otherwise.	 */	public function is_ability_call( FunctionCall $call ): bool {		$name = $call->getName();		if ( null === $name ) {			return false;		} 		return str_starts_with( $name, self::ABILITY_PREFIX );	} 	/**	 * Executes a WordPress ability from a function call.	 *	 * Only abilities that were specified in the constructor are allowed to be	 * executed. If the ability is not in the allowed list, an error response	 * with code `ability_not_allowed` is returned.	 *	 * @since 7.0.0	 *	 * @param FunctionCall $call The function call to execute.	 * @return FunctionResponse The response from executing the ability.	 */	public function execute_ability( FunctionCall $call ): FunctionResponse {		$function_name = $call->getName() ?? 'unknown';		$function_id   = $call->getId() ?? 'unknown'; 		if ( ! $this->is_ability_call( $call ) ) {			return new FunctionResponse(				$function_id,				$function_name,				array(					'error' => __( 'Not an ability function call' ),					'code'  => 'invalid_ability_call',				)

Changelog

Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/ai-client/class-wp-ai-client-ability-function-resolver.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.