WP_AI_Client_Ability_Function_Resolver::execute_ability( WordPressAiClientToolsDTOFunctionCall $call ): WordPressAiClientToolsDTOFunctionResponse

Executes a WordPress ability from a function call.

Description

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.

Parameters

$callWordPressAiClientToolsDTOFunctionCallrequired
The function call to execute.

Return

WordPressAiClientToolsDTOFunctionResponse The response from executing the ability.

Source

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',
			)
		);
	}

	$ability_name = self::function_name_to_ability_name( $function_name );

	if ( ! isset( $this->allowed_abilities[ $ability_name ] ) ) {
		return new FunctionResponse(
			$function_id,
			$function_name,
			array(
				/* translators: %s: ability name */
				'error' => sprintf( __( 'Ability "%s" was not specified in the allowed abilities list.' ), $ability_name ),
				'code'  => 'ability_not_allowed',
			)
		);
	}

	$ability = wp_get_ability( $ability_name );

	if ( ! $ability instanceof WP_Ability ) {
		return new FunctionResponse(
			$function_id,
			$function_name,
			array(
				/* translators: %s: ability name */
				'error' => sprintf( __( 'Ability "%s" not found' ), $ability_name ),
				'code'  => 'ability_not_found',
			)
		);
	}

	$args   = $call->getArgs();
	$result = $ability->execute( ! empty( $args ) ? $args : null );

	if ( is_wp_error( $result ) ) {
		return new FunctionResponse(
			$function_id,
			$function_name,
			array(
				'error' => $result->get_error_message(),
				'code'  => $result->get_error_code(),
				'data'  => $result->get_error_data(),
			)
		);
	}

	return new FunctionResponse(
		$function_id,
		$function_name,
		$result
	);
}

Changelog

VersionDescription
7.0.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.