hasProvider( LMStudioProvider::class ) ) { return; } $registry->registerProvider( LMStudioProvider::class ); } /** * Registers fallback authentication for LM Studio. * * Local LM Studio does not require authentication by default, so this sets an * empty API key only when no credentials were already configured. * * @since 1.0.0 */ public function register_fallback_auth(): void { if ( ! class_exists( AiClient::class ) ) { return; } $registry = AiClient::defaultRegistry(); if ( ! $registry->hasProvider( 'lmstudio' ) ) { return; } $auth = $registry->getProviderRequestAuthentication( 'lmstudio' ); if ( null !== $auth ) { return; } $registry->setProviderRequestAuthentication( 'lmstudio', new ApiKeyRequestAuthentication( '' ) ); } /** * Initializes the LM Studio settings. * * @since 1.0.0 */ public function initialize_settings(): void { $settings = new LMStudioSettings(); $settings->init(); } /** * Adds action links to the plugin list table. * * @since 1.0.0 * * @param array $links Existing action links. * @return array Modified action links. */ public function plugin_action_links( array $links ): array { $settings_link = sprintf( '%2$s', admin_url( 'options-general.php?page=ai-provider-for-lm-studio' ), esc_html__( 'Settings', 'ai-provider-for-lm-studio' ) ); array_unshift( $links, $settings_link ); return $links; } /** * Allows localhost requests to the configured LM Studio host. * * @since 1.0.0 * * @param bool $external Whether the request is external. * @param string $host The host of the request. * @param string $url The URL of the request. * @return bool Whether the request is allowed. */ public function allow_localhost_requests( $external, $host, $url ): bool { if ( strpos( $url, $this->get_lmstudio_host() ) !== false ) { return true; } return $external; } /** * Allows the configured LM Studio port. * * @since 1.0.0 * * @param array $ports The ports. * @return array The allowed ports. */ public function allow_lmstudio_ports( $ports ): array { $lmstudio_host = $this->get_lmstudio_host(); $lmstudio_port = wp_parse_url( $lmstudio_host, PHP_URL_PORT ); if ( ! $lmstudio_port ) { return $ports; } return array_merge( $ports, [ $lmstudio_port ] ); } /** * Extends timeout for requests to the configured LM Studio host. * * Some environments cap outbound requests at around 30 seconds by default, * which can be too low when LM Studio is loading or warming a model. * * @since 1.0.0 * * @param array $args HTTP request args. * @param string $url Request URL. * @return array Filtered HTTP request args. */ public function extend_lmstudio_timeout( array $args, string $url ): array { if ( strpos( $url, $this->get_lmstudio_host() ) === false ) { return $args; } $existing_timeout = isset( $args['timeout'] ) && is_numeric( $args['timeout'] ) ? (float) $args['timeout'] : 0.0; if ( $existing_timeout < 180.0 ) { // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout -- Local model warm-up can exceed the default HTTP timeout. $args['timeout'] = 180.0; } return $args; } }