Connection: the Cloudflare account, and optionally an AI Gateway. */ final class ConnectionScreen { public static function render(): string { return self::cloudflare() . self::gateway(); } private static function cloudflare(): string { $s = get_plugin_settings(); $connected = get_account_id() !== '' && get_api_key() !== ''; $body = ''; if ($connected && class_exists('\\WordPress\\AI\\Connector_Approval\\Approvals_Store') && !is_connector_approved()) { $body .= Ui::notice(__('The AI plugin blocks AIPCF until you allow it on Tools > Connector Approvals.', 'aipcf-ai-provider-for-cloudflare'), 'warn'); $body .= Ui::actions([['label' => __('Allow now', 'aipcf-ai-provider-for-cloudflare'), 'url' => SettingsPage::connectorApprovalUrl(), 'kind' => 'primary']]); } $accountHelp = defined('CLOUDFLARE_WORKERS_AI_ACCOUNT_ID') && \CLOUDFLARE_WORKERS_AI_ACCOUNT_ID ? __('Also set in wp-config.php, which is used when this is empty.', 'aipcf-ai-provider-for-cloudflare') : __('The 32-character code in your Cloudflare dashboard.', 'aipcf-ai-provider-for-cloudflare'); $body .= Ui::row( __('Account ID', 'aipcf-ai-provider-for-cloudflare'), Ui::text('account_id', (string) $s['account_id'], 'aipcf_account_id', ['placeholder' => 'fc6034fbe3a9c364856c3d380c47dc3e']), ['for' => 'aipcf_account_id', 'help' => $accountHelp, 'link' => [__('Find it', 'aipcf-ai-provider-for-cloudflare'), 'https://dash.cloudflare.com/?to=/:account/home']] ); $body .= self::tokenRow((string) $s['api_key'] !== ''); $body .= Ui::actions([ [ 'label' => __('Test the connection', 'aipcf-ai-provider-for-cloudflare'), 'url' => wp_nonce_url(admin_url('admin-post.php?action=aipcf_test_connection'), SettingsPage::NONCE_ACTION), 'busy' => __('Testing...', 'aipcf-ai-provider-for-cloudflare'), ], ]); return Ui::card(__('Cloudflare', 'aipcf-ai-provider-for-cloudflare'), $body, [ 'text' => __('AIPCF runs on Cloudflare Workers AI, with your own Cloudflare account.', 'aipcf-ai-provider-for-cloudflare'), 'pill' => SettingsPage::connectionPillUi(), ]); } /** * The API token: here, or on WordPress's Connectors screen when that holds it. */ private static function tokenRow(bool $savedHere): string { $label = __('API token', 'aipcf-ai-provider-for-cloudflare'); if (core_connector_is_available()) { $control = Ui::status(__('Kept on Settings > Connectors, with your other AI keys.', 'aipcf-ai-provider-for-cloudflare')) . Ui::actions([['label' => __('Open Connectors', 'aipcf-ai-provider-for-cloudflare'), 'url' => admin_url('options-connectors.php'), 'kind' => 'link']]); if ($savedHere) { // An older token saved in AIPCF still wins over the Connectors one. $control .= Ui::notice(__('A token saved in AIPCF earlier is still used instead. Remove it to use the one on Connectors.', 'aipcf-ai-provider-for-cloudflare'), 'warn') . sprintf( '', esc_attr(Ui::field('api_key_clear')), esc_html__('Remove the token saved in AIPCF', 'aipcf-ai-provider-for-cloudflare') ); } return Ui::row($label, $control); } $help = defined('CLOUDFLARE_WORKERS_AI_API_TOKEN') && \CLOUDFLARE_WORKERS_AI_API_TOKEN ? __('Also set in wp-config.php, which is used when this is empty.', 'aipcf-ai-provider-for-cloudflare') : __('Create one with the "Workers AI" template.', 'aipcf-ai-provider-for-cloudflare'); return Ui::row($label, Ui::secret('api_key', $savedHere, 'aipcf_api_key'), [ 'for' => 'aipcf_api_key', 'help' => $help, 'link' => [__('Create a token', 'aipcf-ai-provider-for-cloudflare'), 'https://dash.cloudflare.com/profile/api-tokens'], ]); } private static function gateway(): string { $s = get_plugin_settings(); $body = ''; $pill = ['', __('Off', 'aipcf-ai-provider-for-cloudflare')]; if (Gateway::id() !== '') { $down = get_transient(Gateway::DOWN_TRANSIENT); $pill = $down ? ['warn', __('Bypassed', 'aipcf-ai-provider-for-cloudflare')] : ['ok', __('On', 'aipcf-ai-provider-for-cloudflare')]; if ($down) { $body .= Ui::notice(__('An error made AIPCF go to Workers AI directly, for up to an hour.', 'aipcf-ai-provider-for-cloudflare'), 'warn'); } $last = get_option(Gateway::LAST_ERROR_OPTION); if (is_array($last) && !empty($last['reason'])) { $body .= Ui::more(__('Last gateway error', 'aipcf-ai-provider-for-cloudflare'), '

' . esc_html((string) $last['reason']) . '

'); } $body .= Ui::actions([ [ 'label' => __('Test the gateway', 'aipcf-ai-provider-for-cloudflare'), 'url' => wp_nonce_url(admin_url('admin-post.php?action=aipcf_gateway_test'), 'aipcf_gateway_test'), 'busy' => __('Testing...', 'aipcf-ai-provider-for-cloudflare'), ], ['label' => __('Gateway analytics', 'aipcf-ai-provider-for-cloudflare'), 'url' => Gateway::dashboardUrl(), 'kind' => 'link'], ]); } $body .= Ui::row( __('Gateway name', 'aipcf-ai-provider-for-cloudflare'), Ui::text('gateway_id', (string) ($s['gateway_id'] ?? ''), 'aipcf-gateway-id'), ['for' => 'aipcf-gateway-id', 'help' => __('Empty means requests go to Workers AI directly.', 'aipcf-ai-provider-for-cloudflare'), 'more' => self::gatewaySetup()] ); $body .= Ui::moreSettings( Ui::row( __('Prompt logs', 'aipcf-ai-provider-for-cloudflare'), Ui::check('gateway_log_payload', !empty($s['gateway_log_payload']), __('Keep prompt and answer text in the gateway logs', 'aipcf-ai-provider-for-cloudflare'), 'aipcf-gateway-log'), ['for' => 'aipcf-gateway-log', 'help' => __('With the Site assistant on, this includes visitors\' questions.', 'aipcf-ai-provider-for-cloudflare')] ) . Ui::row( __('Gateway token', 'aipcf-ai-provider-for-cloudflare'), Ui::secret('gateway_token', (string) ($s['gateway_token'] ?? '') !== '', 'aipcf-gateway-token'), ['for' => 'aipcf-gateway-token', 'help' => __('Only if the gateway has Authentication on.', 'aipcf-ai-provider-for-cloudflare')] ) . Ui::row( __('Dynamic route', 'aipcf-ai-provider-for-cloudflare'), Ui::text('dynamic_route', (string) ($s['dynamic_route'] ?? ''), 'aipcf-dynamic-route'), ['for' => 'aipcf-dynamic-route', 'help' => __('A route from the gateway dashboard that picks the model.', 'aipcf-ai-provider-for-cloudflare')] ) ); return Ui::card(__('AI Gateway', 'aipcf-ai-provider-for-cloudflare'), $body, [ 'text' => __('Optional. Adds edge caching, logs, rate limits and analytics in Cloudflare.', 'aipcf-ai-provider-for-cloudflare'), 'pill' => $pill, ]); } private static function gatewaySetup(): string { return '
    ' . '
  1. ' . esc_html__('In Cloudflare, open AI > AI Gateway > Create gateway, and enter its name here.', 'aipcf-ai-provider-for-cloudflare') . '
  2. ' . '
  3. ' . esc_html__('Rate limiting: 60 requests per 60 seconds, sliding window.', 'aipcf-ai-provider-for-cloudflare') . '
  4. ' . '
  5. ' . esc_html__('Leave its cache off: set the edge cache on Settings > Budget instead.', 'aipcf-ai-provider-for-cloudflare') . '
  6. ' . '
  7. ' . esc_html__('Keep the AIPCF daily budget on as the exact stop.', 'aipcf-ai-provider-for-cloudflare') . '
  8. ' . '
'; } }