api_proxy = $api_proxy; $this->integration = $integration; add_action( 'wp_abilities_api_categories_init', array( $this, 'register_category' ) ); add_action( 'wp_abilities_api_init', array( $this, 'register' ) ); } public function register_category() { if ( function_exists( 'wp_register_ability_category' ) ) { wp_register_ability_category( 'agents24x7', array( 'label' => 'Agents24x7', 'description' => 'Connected site agents and work awaiting review.', ) ); } } /** Older WordPress installations do not load the Abilities API. */ public function register() { if ( ! function_exists( 'wp_register_ability' ) ) { return; } $empty_input = array( 'type' => 'object', 'additionalProperties' => false ); $agent_properties = array( 'id' => array( 'type' => 'string' ), 'name' => array( 'type' => 'string' ), 'isActive' => array( 'type' => 'boolean' ), ); $this->register_ability( 'list-agents', 'List Agents24x7 agents', 'List the hired agents for this connected site.', $empty_input, $this->list_schema( $agent_properties ), function () { $workspace = $this->call( 'query', 'wordpress/workspace:get' ); if ( is_wp_error( $workspace ) ) { return $workspace; } return $this->select_fields( $workspace['agents'], array( 'id', 'name', 'isActive' ) ); } ); $this->register_ability( 'ask-agent', 'Ask an Agents24x7 agent', 'Send a message as the site owner and acknowledge receipt. Read the reply in Agents24x7. Messages cannot approve, publish or make live site changes. Review and approve work in Agents24x7.', array( 'type' => 'object', 'properties' => array( 'agent_id' => array( 'type' => 'string', 'format' => 'uuid' ), 'message' => array( 'type' => 'string', 'minLength' => 1, 'maxLength' => 8000 ), ), 'required' => array( 'agent_id', 'message' ), 'additionalProperties' => false, ), array( 'type' => 'object', 'properties' => array( 'received' => array( 'type' => 'boolean' ) ), 'required' => array( 'received' ), 'additionalProperties' => false, ), function ( $input ) { $reply = $this->call( 'action', 'wordpress/mcp:askAgent', array( 'agentId' => $input['agent_id'], 'message' => $input['message'], 'idempotencyKey' => wp_generate_uuid4(), ) ); // Replies may quote shopper data; owners read them in the authenticated app. return is_wp_error( $reply ) ? $reply : array( 'received' => true ); } ); $this->register_ability( 'pending-drafts', 'List pending Agents24x7 drafts and approvals', 'List drafts and approvals waiting for review for this connected site. Review them in Agents24x7. No shopper conversations or personal details are included.', $empty_input, array( 'type' => 'object', 'properties' => array( 'drafts' => $this->list_schema( array( 'id' => array( 'type' => 'string' ), 'agentId' => array( 'type' => 'string' ), 'kind' => array( 'type' => 'string' ), ) ), 'approvals' => $this->list_schema( array( 'agentId' => array( 'type' => 'string' ), 'sourceId' => array( 'type' => 'string' ), ) ), ), 'required' => array( 'drafts', 'approvals' ), 'additionalProperties' => false, ), array( $this, 'pending_drafts' ) ); } public function pending_drafts() { $drafts = $this->call( 'query', 'wordpress/mcp:listPendingDrafts' ); if ( is_wp_error( $drafts ) ) { return $drafts; } $workspace = $this->call( 'query', 'wordpress/workspace:get' ); if ( is_wp_error( $workspace ) ) { return $workspace; } $approvals = array(); foreach ( $workspace['agents'] as $agent ) { $cards = $this->call( 'query', 'wordpress/approval:listReady', array( 'agentId' => $agent['id'] ) ); if ( is_wp_error( $cards ) ) { return $cards; } $approvals = array_merge( $approvals, $this->select_fields( $cards, array( 'agentId', 'sourceId' ) ) ); } // Card titles, item payloads and draft bodies can contain shopper details. return array( 'drafts' => $this->select_fields( $drafts, array( 'id', 'agentId', 'kind' ) ), 'approvals' => $approvals, ); } private function list_schema( $properties ) { return array( 'type' => 'array', 'items' => array( 'type' => 'object', 'properties' => $properties, 'required' => array_keys( $properties ), 'additionalProperties' => false, ), ); } private function select_fields( $rows, $fields ) { return array_map( function ( $row ) use ( $fields ) { return array_intersect_key( $row, array_flip( $fields ) ); }, $rows ); } private function call( $type, $function, $args = array() ) { if ( ! $this->is_allowed() ) { return new WP_Error( 'forbidden', esc_html__( 'Connect Agents24x7 and use a site administrator account.', 'agents24x7' ), array( 'status' => 403 ) ); } $response = $this->api_proxy->call_platform_function( $type, $function, $args ); if ( is_wp_error( $response ) ) { return $response; } $data = $response instanceof WP_REST_Response ? $response->get_data() : $response; return is_array( $data ) && array_key_exists( 'data', $data ) ? $data['data'] : $data; } private function register_ability( $name, $label, $description, $input_schema, $output_schema, $execute ) { wp_register_ability( 'agents24x7/' . $name, array( 'label' => $label, 'description' => $description, 'category' => 'agents24x7', 'input_schema' => $input_schema, 'output_schema' => $output_schema, 'execute_callback' => $execute, 'permission_callback' => function () { return $this->is_allowed(); }, 'meta' => array( 'show_in_rest' => false, 'mcp' => array( 'public' => false ), ), ) ); } private function is_allowed() { return current_user_can( 'manage_options' ) && $this->api_proxy->is_platform_linked() && class_exists( 'WP_Application_Passwords' ) && $this->integration->has_app_password(); } }