settings = $settings ?? new SettingsRepository(); $this->grammar_runtime = $grammar_runtime ?? new CommandRuntime( null, null, null, null, null, null, new CommandPolicy( $this->settings ) ); $this->batch_runtime = new CommandBatchRuntime( fn(): array => array( 'site_shape' => $this->grammar_runtime->site_shape() ) ); } /** * Return installed-runtime command grammar discovery. * * @param WP_REST_Request $request REST request. */ public function capabilities( WP_REST_Request $request ): WP_REST_Response { unset( $request ); // One canonical discovery shape for both gate states: `enabled` reports // the WordPress-to-ActionPanel request gate, and a disabled site simply // advertises an empty capability catalog. return new WP_REST_Response( array( 'plugin_version' => defined( 'ACTIONPANEL_AI_PLUGIN_VERSION' ) ? ACTIONPANEL_AI_PLUGIN_VERSION : '1.0.0', 'enabled' => $this->settings->get_allow_requests(), 'site_identity' => array( 'site_url' => get_site_url(), 'home_url' => get_home_url(), 'admin_url' => admin_url(), ), 'grammar' => $this->grammar_runtime->discovery(), ) ); } /** * Execute a flat command batch and return one terminal result per command. * * @param WP_REST_Request $request REST request. */ public function execute( WP_REST_Request $request ): WP_REST_Response { if ( ! $this->settings->get_allow_requests() ) { return new WP_REST_Response( array( 'error' => 'actionpanel_ai_requests_disabled', 'message' => __( 'Allow WordPress to send requests to ActionPanel AI before executing assistant commands.', 'actionpanel-ai' ), ), 403 ); } $commands = $request->get_param( 'commands' ); if ( ! is_array( $commands ) ) { return new WP_REST_Response( array( 'error' => 'invalid_command_batch', 'message' => '`commands` must be an array.', ), 400 ); } if ( count( $commands ) > self::MAX_BATCH_COMMANDS ) { return new WP_REST_Response( array( 'error' => 'command_batch_limit_exceeded', 'message' => 'Command batches are limited to ' . self::MAX_BATCH_COMMANDS . ' commands; split the work into smaller batches.', 'max' => self::MAX_BATCH_COMMANDS, ), 400 ); } return new WP_REST_Response( array( 'batch_id' => $request->get_param( 'batch_id' ), 'results' => $this->batch_runtime->run( $commands, fn( array $command ): array => $this->grammar_runtime->run( $command ) ), ) ); } }