amazee.ai screen for configuring the endpoint URL.
*
* The API key itself stays on the core Settings > Connectors screen, where
* the AI client stores and resolves it. This screen only manages the endpoint
* URL and shows whether the connection works.
*/
class AmazeeIoSettings {
private const OPTION_GROUP = 'ai-provider-for-amazee-ai-settings';
private const OPTION_NAME = 'ai_provider_for_amazee_ai_settings';
private const PAGE_SLUG = 'ai-provider-for-amazee-ai';
private const SECTION_ID = 'ai_provider_for_amazee_ai_main';
/**
* Hooks the settings registration and screen into the admin.
*/
public function init(): void {
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'admin_menu', array( $this, 'register_settings_screen' ) );
add_action( 'admin_notices', array( $this, 'render_connectors_screen_notice' ) );
add_filter( 'script_module_data_options-connectors-wp-admin', array( $this, 'update_connector_description' ), 11 );
}
/**
* Drops the setup steps from the connector card once configured.
*
* The provider metadata description walks through the two setup steps.
* The swap cannot happen on `wp_connectors_init`: core builds the
* connector registry on `init` at priority 15 but hands the stored token
* to the AI client only at priority 20, so the token never resolves
* there. This filter runs when the Connectors screen embeds its data,
* well after both.
*
* @param mixed $data Script module data for the Connectors screen.
* @return mixed Filtered data.
*/
public function update_connector_description( $data ) {
if ( ! is_array( $data ) || ! isset( $data['connectors']['amazeeio']['description'] ) ) {
return $data;
}
$config = AmazeeIoAiProvider::getApiConfiguration();
if ( '' === $config['url'] || '' === $config['token'] ) {
return $data;
}
$data['connectors']['amazeeio']['description'] = __( 'Secure private AI for your site, hosted by amazee.ai.', 'ai-provider-for-amazee-ai' );
return $data;
}
/**
* Points to the settings screen from the Connectors screen while the
* endpoint URL is missing.
*
* The connector card itself cannot link anywhere: core renders the
* description as escaped plain text, so a notice is the only place on
* that screen a link to the settings screen can live.
*/
public function render_connectors_screen_notice(): void {
if ( 'options-connectors.php' !== $GLOBALS['pagenow'] || ! current_user_can( 'manage_options' ) ) {
return;
}
$config = AmazeeIoAiProvider::getApiConfiguration();
if ( '' !== $config['url'] ) {
return;
}
printf(
'
%s
',
sprintf(
/* translators: 1: opening link tag to the amazee.ai settings screen, 2: closing link tag */
esc_html__( 'amazee.ai — Step 1: set your endpoint URL on the %1$sSettings > amazee.ai%2$s screen. Step 2: enter your API key on the amazee.ai connector below.', 'ai-provider-for-amazee-ai' ),
'',
''
)
);
}
/**
* Registers the setting and its endpoint URL field.
*/
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 . '_endpoint_url',
__( 'Endpoint URL', 'ai-provider-for-amazee-ai' ),
array( $this, 'render_endpoint_url_field' ),
self::PAGE_SLUG,
self::SECTION_ID,
array( 'label_for' => self::OPTION_NAME . '-endpoint-url' )
);
}
/**
* Registers the settings screen under the Settings menu.
*/
public function register_settings_screen(): void {
add_options_page(
__( 'amazee.ai Settings', 'ai-provider-for-amazee-ai' ),
__( 'amazee.ai', 'ai-provider-for-amazee-ai' ),
'manage_options',
self::PAGE_SLUG,
array( $this, 'render_screen' )
);
}
/**
* Sanitizes the settings array.
*
* @param mixed $value The input value.
* @return array The sanitized settings.
*/
public function sanitize_settings( $value ): array {
if ( ! is_array( $value ) ) {
return array();
}
$endpoint_url = isset( $value['endpoint_url'] ) ? trim( (string) $value['endpoint_url'] ) : '';
if ( '' !== $endpoint_url ) {
$endpoint_url = rtrim( esc_url_raw( $endpoint_url ), '/' );
}
// The API key is sent with every request, so never store a URL that
// would send it unencrypted; keep the previous value instead.
if ( '' !== $endpoint_url && ! AmazeeIoAiProvider::isHttpsUrl( $endpoint_url ) ) {
if ( function_exists( 'add_settings_error' ) ) {
add_settings_error(
self::OPTION_NAME,
'amazee_endpoint_url_insecure',
__( 'The endpoint URL must start with https:// so your API key is never sent unencrypted. The previous URL was kept.', 'ai-provider-for-amazee-ai' )
);
}
return array( 'endpoint_url' => self::get_endpoint_url() );
}
// Tokens are bound to the region endpoint they were issued for, so a
// changed URL almost always needs a new token as well. The function
// guard keeps non-admin contexts (wp-cli, front end) from fataling,
// as add_settings_error() only exists in the admin.
$previous = self::get_endpoint_url();
if ( '' !== $previous && $previous !== $endpoint_url && function_exists( 'add_settings_error' ) ) {
add_settings_error(
self::OPTION_NAME,
'amazee_endpoint_url_changed',
sprintf(
/* translators: 1: opening link tag to the Connectors screen, 2: closing link tag */
__( 'The endpoint URL changed. amazee.ai API keys are bound to the region they were issued for, so you most likely need to update the key on the %1$sSettings > Connectors%2$s screen as well.', 'ai-provider-for-amazee-ai' ),
'',
''
),
'warning'
);
}
return array(
'endpoint_url' => $endpoint_url,
);
}
/**
* Renders the settings screen.
*/
public function render_screen(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
',
' ' . esc_html__( '(opens in a new tab)', 'ai-provider-for-amazee-ai' ) . ''
);
?>
';
printf(
/* translators: 1: opening link tag to the Connectors screen, 2: closing link tag */
esc_html__( 'Step 2: enter your API key for the amazee.ai connector on the %1$sSettings > Connectors%2$s screen.', 'ai-provider-for-amazee-ai' ),
'',
''
);
?>
render_connection_status(); ?>
/>
',
''
);
} else {
printf(
/* translators: 1: code tag, 2: closing code tag */
esc_html__( 'The endpoint URL for your amazee.ai region, for example %1$shttps://llm.us103.amazee.ai%2$s where %1$sus103%2$s is a US region.', 'ai-provider-for-amazee-ai' ),
'',
''
);
echo ' ';
esc_html_e( 'Regions are also available in the UK, Germany, Switzerland, Australia and more.', 'ai-provider-for-amazee-ai' );
echo ' ';
esc_html_e( 'There is no default: your API key only works with the region it was issued for, so copy the exact URL from my.amazee.io.', 'ai-provider-for-amazee-ai' );
}
?>
Connectors%2$s screen, as keys are bound to the region they were issued for.', 'ai-provider-for-amazee-ai' ),
'',
''
);
?>
%s',
esc_html__( 'Not connected yet: enter the endpoint URL and API key first.', 'ai-provider-for-amazee-ai' )
);
return;
}
try {
$models = AmazeeIoAiProvider::modelMetadataDirectory()->listModelMetadata();
} catch ( \Throwable $exception ) {
printf(
'
%s
',
sprintf(
/* translators: %s: error message returned by the API */
esc_html__( 'Could not connect: %s', 'ai-provider-for-amazee-ai' ),
esc_html( $exception->getMessage() )
)
);
return;
}
// Core notice styles instead of coloured text: the old green
// (#00a32a on white) failed WCAG AA contrast for body text.
printf(
'
%s
',
esc_html(
sprintf(
/* translators: %d: number of models */
_n( 'Connected — %d model available.', 'Connected — %d models available.', count( $models ), 'ai-provider-for-amazee-ai' ),
count( $models )
)
)
);
// The endpoint describes models in `model_info.metadata`, which the
// model directory caches raw in its transient but does not expose via
// ModelMetadata, so read the descriptions from the cache directly.
$descriptions = array();
$model_data = get_transient( AmazeeIoModelDirectory::cacheKey() );
if ( is_array( $model_data ) ) {
foreach ( $model_data as $info_node ) {
if ( ! is_array( $info_node ) || ! isset( $info_node['model_name'] ) ) {
continue;
}
$metadata = $info_node['model_info']['metadata'] ?? '';
$descriptions[ $info_node['model_name'] ] = is_string( $metadata ) ? $metadata : '';
}
}
$model_capabilities = array();
foreach ( $models as $model ) {
$model_capabilities[ $model->getId() ] = implode( ', ', self::capability_labels( $model ) );
}
ksort( $model_capabilities );
?>