llama.cpp for configuring the * server base URL. * * @since 0.0.1 */ class LlamaCppSettings { private const DEFAULT_BASE_URL = 'http://127.0.0.1:8080'; private const OPTION_GROUP = 'aipf_llamacpp_settings_group'; private const OPTION_NAME = 'aipf_llamacpp_settings'; private const PAGE_SLUG = 'aipf-llamacpp'; private const SECTION_ID = 'ai_provider_for_llamacpp_main'; /** * Initializes the settings. * * @since 0.0.1 */ public function init(): void { add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_menu', array( $this, 'register_settings_screen' ) ); add_action( 'wp_ajax_aipf_llamacpp_model_details', array( $this, 'ajax_model_details' ) ); } /** * Registers the setting and settings fields. * * @since 0.0.1 */ public function register_settings(): void { register_setting( self::OPTION_GROUP, self::OPTION_NAME, array( 'type' => 'array', 'default' => array(), 'sanitize_callback' => array( $this, 'sanitize_settings' ), ) ); add_settings_section( self::SECTION_ID, '', '__return_empty_string', self::PAGE_SLUG ); add_settings_field( self::OPTION_NAME . '_base_url', __( 'Server URL', 'ai-provider-for-llamacpp' ), array( $this, 'render_base_url_field' ), self::PAGE_SLUG, self::SECTION_ID, array( 'label_for' => self::OPTION_NAME . '-base-url' ) ); } /** * Registers the settings screen under the Settings menu. * * @since 0.0.1 */ public function register_settings_screen(): void { add_options_page( __( 'llama.cpp Settings', 'ai-provider-for-llamacpp' ), __( 'llama.cpp', 'ai-provider-for-llamacpp' ), 'manage_options', self::PAGE_SLUG, array( $this, 'render_screen' ) ); } /** * Sanitizes the settings array. * * @since 0.0.1 * * @param mixed $value The raw input value. * @return array */ public function sanitize_settings( $value ): array { if ( ! is_array( $value ) ) { return array(); } $base_url = isset( $value['base_url'] ) ? trim( (string) $value['base_url'] ) : ''; if ( '' !== $base_url ) { $base_url = rtrim( esc_url_raw( $base_url ), '/' ); } return array( 'base_url' => $base_url, ); } /** * Renders the settings page. * * @since 0.0.1 */ public function render_screen(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } ?>

Connectors%3$s screen.', 'ai-provider-for-llamacpp' ), '' . esc_html( self::DEFAULT_BASE_URL ) . '', '', '' ); ?>

render_models_section(); ?> render_getting_started_section(); ?>
fetch_models_info( $base_url ); ?>

get_error_message() ) ); ?>

$model['id'] ?? __( 'Unknown', 'ai-provider-for-llamacpp' ), 'status' => '' !== $status_value ? ucfirst( $status_value ) : '-', 'object_type' => $model['object'] ?? 'model', 'owned_by' => $model['owned_by'] ?? '-', ); } $table = new ModelsListTable( $rows ); $table->prepare_items(); $table->display(); $this->render_model_details_modal(); } /** * Renders the Getting Started guide below the Available Models section. */ private function render_getting_started_section(): void { $docs_file = AIPF_LLAMACPP_PLUGIN_DIR . 'docs/getting-started.html'; if ( ! file_exists( $docs_file ) ) { return; } $html = file_get_contents( $docs_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents if ( false === $html ) { return; } ?>
▶
fetch_models_info( $base_url ); if ( is_wp_error( $models ) ) { wp_send_json_error( $models->get_error_message() ); } // Find the requested model. $model = null; foreach ( $models as $m ) { if ( ( $m['id'] ?? '' ) === $model_id ) { $model = $m; break; } } if ( null === $model ) { wp_send_json_error( __( 'Model not found on the server.', 'ai-provider-for-llamacpp' ) ); } $props = $this->fetch_props_info( $base_url ); $is_router = ! empty( $props ) && ! is_wp_error( $props ) && ( $props['role'] ?? '' ) === 'router'; $meta = $model['meta'] ?? array(); $status_value = $model['status']['value'] ?? ''; $model_ctx = 0; $model_slots = 0; $is_remote = false; if ( ! is_wp_error( $props ) && is_array( $props ) && ! $is_router ) { $model_ctx = (int) ( $props['default_generation_settings']['params']['n_ctx'] ?? $props['default_generation_settings']['n_ctx'] ?? 0 ); $model_slots = (int) ( $props['total_slots'] ?? 0 ); } // In router mode, try to fetch full metadata. if ( $is_router && 'loaded' === $status_value ) { $args = $model['status']['args'] ?? array(); // Try sub-server directly (works when server is on the same machine). $port = $this->extract_arg_value( $args, '--port' ); if ( $port > 0 ) { $sub_base = 'http://127.0.0.1:' . $port; $sub_meta = $this->fetch_sub_model_meta( $sub_base ); if ( ! empty( $sub_meta ) ) { $meta = $sub_meta; } $sub_props = $this->fetch_props_info( $sub_base ); if ( ! is_wp_error( $sub_props ) && is_array( $sub_props ) ) { $model_ctx = (int) ( $sub_props['default_generation_settings']['n_ctx'] ?? 0 ); $model_slots = (int) ( $sub_props['total_slots'] ?? 0 ); } } // Fallback: /props?model=X proxied through the router to the sub-server. if ( empty( $meta ) || 0 === $model_ctx ) { $props_url = rtrim( $base_url, '/' ) . '/props?model=' . rawurlencode( $model_id ); $model_props = $this->fetch_props_for_url( $props_url ); if ( ! is_wp_error( $model_props ) && is_array( $model_props ) ) { if ( 0 === $model_ctx ) { $model_ctx = (int) ( $model_props['default_generation_settings']['n_ctx'] ?? 0 ); } if ( 0 === $model_slots ) { $model_slots = (int) ( $model_props['total_slots'] ?? 0 ); } $is_remote = true; } } // Fallback: /slots endpoint through the main server URL. if ( 0 === $model_ctx ) { $slots_data = $this->fetch_slots_info( $base_url, $model_id ); if ( ! empty( $slots_data ) ) { $model_ctx = (int) ( $slots_data[0]['n_ctx'] ?? 0 ); $model_slots = count( $slots_data ); $is_remote = true; } } // Fallback: extract --ctx-size from args. if ( 0 === $model_ctx ) { $ctx_arg = $this->extract_arg_value( $args, '--ctx-size' ); if ( $ctx_arg > 0 ) { $model_ctx = $ctx_arg; } } } $em = '-'; $rows = array( __( 'Runtime Context', 'ai-provider-for-llamacpp' ) => $model_ctx > 0 ? number_format_i18n( $model_ctx ) . ' ' . __( 'tokens', 'ai-provider-for-llamacpp' ) : $em, __( 'Slots', 'ai-provider-for-llamacpp' ) => $model_slots > 0 ? (string) $model_slots : $em, __( 'Training Context', 'ai-provider-for-llamacpp' ) => ! empty( $meta['n_ctx_train'] ) ? number_format_i18n( (int) $meta['n_ctx_train'] ) . ' ' . __( 'tokens', 'ai-provider-for-llamacpp' ) : $em, __( 'Parameters', 'ai-provider-for-llamacpp' ) => ! empty( $meta['n_params'] ) ? ModelsListTable::format_number_short( (int) $meta['n_params'] ) : $em, __( 'Model Size', 'ai-provider-for-llamacpp' ) => ! empty( $meta['size'] ) ? size_format( (int) $meta['size'] ) : $em, __( 'Embedding', 'ai-provider-for-llamacpp' ) => ! empty( $meta['n_embd'] ) ? number_format_i18n( (int) $meta['n_embd'] ) : $em, __( 'Vocabulary', 'ai-provider-for-llamacpp' ) => ! empty( $meta['n_vocab'] ) ? number_format_i18n( (int) $meta['n_vocab'] ) : $em, ); ob_start(); echo ''; foreach ( $rows as $label => $value ) { echo ''; } echo '
' . esc_html( $label ) . '' . esc_html( $value ) . '
'; if ( $is_remote && empty( $meta ) ) { echo '

'; echo esc_html__( 'Some details are only available when WordPress and the llama.cpp server are on the same machine. Remote servers (ngrok, VPS) expose limited metadata through the router.', 'ai-provider-for-llamacpp' ); echo '

'; } $html = ob_get_clean(); wp_send_json_success( array( 'html' => $html ) ); } /** * Extracts a flag value from a model's status args array. * * @param array $args Command-line args from status. * @param string $flag The flag to look for (e.g. '--port'). * @return int The integer value, or 0 if not found. */ private function extract_arg_value( array $args, string $flag ): int { foreach ( $args as $i => $arg ) { if ( $flag === $arg && isset( $args[ $i + 1 ] ) ) { return (int) $args[ $i + 1 ]; } } return 0; } /** * Fetches slot info from the /slots endpoint for a specific model. * * @param string $base_url The server base URL. * @param string $model_id The model ID. * @return array> Slots data, or empty array. */ private function fetch_slots_info( string $base_url, string $model_id ): array { $url = rtrim( $base_url, '/' ) . '/slots?model=' . rawurlencode( $model_id ); $is_local = in_array( wp_parse_url( $url, PHP_URL_HOST ), array( '127.0.0.1', 'localhost' ), true ); $response = wp_remote_get( $url, array( 'timeout' => 3, 'sslverify' => ! $is_local, ) ); if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { return array(); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); return is_array( $body ) ? $body : array(); } /** * Fetches model meta from a sub-server's /v1/models endpoint. * * @param string $base_url The sub-server base URL. * @return array Meta array, or empty if unavailable. */ private function fetch_sub_model_meta( string $base_url ): array { $url = rtrim( $base_url, '/' ) . '/v1/models'; $response = wp_remote_get( $url, array( 'timeout' => 3, 'sslverify' => false ) ); if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { return array(); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); $data = $body['data'] ?? array(); if ( ! empty( $data[0]['meta'] ) ) { return (array) $data[0]['meta']; } return array(); } /** * Fetches model information from the llama.cpp /v1/models endpoint. * * @since 0.0.2 * * @param string $base_url The server base URL. * @return array>|\WP_Error List of model data or error. */ private function fetch_models_info( string $base_url ) { $url = rtrim( $base_url, '/' ) . '/v1/models'; $response = wp_remote_get( $url, array( 'timeout' => 5, 'sslverify' => wp_parse_url( $url, PHP_URL_HOST ) !== '127.0.0.1', ) ); if ( is_wp_error( $response ) ) { return $response; } if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { return new \WP_Error( 'llamacpp_models_error', sprintf( /* translators: %d: HTTP status code */ __( 'Server returned HTTP %d', 'ai-provider-for-llamacpp' ), wp_remote_retrieve_response_code( $response ) ) ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); return (array) ( $body['data'] ?? array() ); } /** * Fetches server properties from the llama.cpp /props endpoint. * * @since 0.0.2 * * @param string $base_url The server base URL. * @return array|\WP_Error Props data or error. */ private function fetch_props_info( string $base_url ) { $url = rtrim( $base_url, '/' ) . '/props'; $response = wp_remote_get( $url, array( 'timeout' => 3, 'sslverify' => wp_parse_url( $url, PHP_URL_HOST ) !== '127.0.0.1', ) ); if ( is_wp_error( $response ) ) { return $response; } if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { return new \WP_Error( 'llamacpp_props_error', 'Failed to fetch /props' ); } return (array) json_decode( wp_remote_retrieve_body( $response ), true ); } /** * Fetches properties from a fully-formed URL. * * @param string $url The full props URL (e.g. with ?model= param). * @return array|\WP_Error Props data or error. */ private function fetch_props_for_url( string $url ) { $is_local = in_array( wp_parse_url( $url, PHP_URL_HOST ), array( '127.0.0.1', 'localhost' ), true ); $response = wp_remote_get( $url, array( 'timeout' => 3, 'sslverify' => ! $is_local, ) ); if ( is_wp_error( $response ) ) { return $response; } if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { return new \WP_Error( 'llamacpp_props_error', 'Failed to fetch props' ); } return (array) json_decode( wp_remote_retrieve_body( $response ), true ); } /** * Renders the server URL field. * * @since 0.0.1 */ public function render_base_url_field(): void { $settings = self::get_settings(); $value = $settings['base_url'] ?? ''; ?>

', '' ); ?>

*/ public static function get_settings(): array { return (array) get_option( self::OPTION_NAME, array() ); } /** * Returns the configured base URL, falling back to the default. * * @since 0.0.1 * * @return string */ public static function get_base_url(): string { $settings = self::get_settings(); if ( isset( $settings['base_url'] ) && '' !== $settings['base_url'] ) { return $settings['base_url']; } return self::DEFAULT_BASE_URL; } }