Budget: the daily budget, how the features share it, and caching. */ final class BudgetScreen { public static function render(): string { return self::budget() . self::shared() . self::caching(); } private static function budget(): string { $s = get_plugin_settings(); $a = Budget::allocation($s); $used = UsageTracker::countedNeurons(); $body = Ui::stats([[ 'value' => Ui::num($used), /* translators: %s: the daily budget or the free daily allowance, in neurons */ 'label' => sprintf(__('of %s neurons today', 'aipcf-ai-provider-for-cloudflare'), Ui::num($a['ceiling'])), 'state' => $used >= $a['ceiling'] ? 'warn' : '', ]]); if ($a['guarded'] && $used >= $a['ceiling']) { $body .= Ui::notice(UsageTracker::budgetAction() === 'cheapest' ? __('Used up for today. Text requests use the cheapest model and everything else waits until midnight UTC.', 'aipcf-ai-provider-for-cloudflare') : __('Used up for today. AI requests wait until midnight UTC.', 'aipcf-ai-provider-for-cloudflare'), 'bad'); } elseif (!$a['guarded'] && $used > $a['ceiling']) { $over = $used - $a['ceiling']; $body .= Ui::notice(sprintf( /* translators: 1: neurons over the free allowance, 2: US dollars */ __('Past the free allowance: %1$s neurons billed so far (about $%2$s). Switch on the budget to stop here.', 'aipcf-ai-provider-for-cloudflare'), Ui::num($over), number_format_i18n($over / 1000 * UsageTracker::USD_PER_1K_NEURONS, 2) ), 'bad'); } $body .= Ui::row( __('Daily budget', 'aipcf-ai-provider-for-cloudflare'), Ui::toggle('budget_enabled', !empty($s['budget_enabled']), __('Stop AI requests at a daily budget', 'aipcf-ai-provider-for-cloudflare'), 'aipcf-budget-enabled'), ['for' => 'aipcf-budget-enabled'] ); $body .= Ui::row( __('Budget', 'aipcf-ai-provider-for-cloudflare'), Ui::number('budget_neurons', (int) ($s['budget_neurons'] ?? UsageTracker::FREE_TIER), 'aipcf-budget-neurons', ['min' => 100, 'max' => 10000000, 'unit' => __('neurons a day', 'aipcf-ai-provider-for-cloudflare')]), ['for' => 'aipcf-budget-neurons', 'help' => __('10,000 is the free daily allowance.', 'aipcf-ai-provider-for-cloudflare')] ); $body .= Ui::row( __('When it is reached', 'aipcf-ai-provider-for-cloudflare'), Ui::select('budget_action', [ 'stop' => __('Pause AI requests until midnight UTC', 'aipcf-ai-provider-for-cloudflare'), /* translators: %s: model name */ 'cheapest' => sprintf(__('Switch text to the cheapest model (%s)', 'aipcf-ai-provider-for-cloudflare'), pretty_model_name(UsageTracker::cheapestTextModelId())), ], (string) ($s['budget_action'] ?? 'stop'), 'aipcf-budget-action'), ['for' => 'aipcf-budget-action', 'help' => __('Only text has a cheaper model; everything else pauses.', 'aipcf-ai-provider-for-cloudflare')] ); $body .= Ui::row( __('Whole account', 'aipcf-ai-provider-for-cloudflare'), Ui::check('budget_account', !empty($s['budget_account']), __('Count other apps on this account', 'aipcf-ai-provider-for-cloudflare'), 'aipcf-budget-account') . (AccountUsage::enabled() ? Ui::actions([['label' => __('Check now', 'aipcf-ai-provider-for-cloudflare'), 'url' => AccountUsage::checkUrl(), 'kind' => 'link']]) : ''), ['for' => 'aipcf-budget-account', 'help' => AccountUsage::summary() !== '' ? AccountUsage::summary() : __('Read every 15 minutes.', 'aipcf-ai-provider-for-cloudflare')] ); $body .= Ui::row( __('Email', 'aipcf-ai-provider-for-cloudflare'), Ui::check('budget_email', !empty($s['budget_email']), __('Email me at 80% and 100%, and a summary each month', 'aipcf-ai-provider-for-cloudflare'), 'aipcf-budget-email'), ['for' => 'aipcf-budget-email'] ); $body .= Ui::row( __('Paid providers', 'aipcf-ai-provider-for-cloudflare'), Ui::check('cloudflare_only', !empty($s['cloudflare_only']), __('Never fall back to Anthropic, Google or OpenAI', 'aipcf-ai-provider-for-cloudflare'), 'aipcf-cloudflare-only'), ['for' => 'aipcf-cloudflare-only'] ); return Ui::card(__('Daily budget', 'aipcf-ai-provider-for-cloudflare'), $body, [ 'text' => __('One limit for every AI request. 10,000 neurons a day are free, reset at midnight UTC.', 'aipcf-ai-provider-for-cloudflare'), 'pill' => !empty($s['budget_enabled']) ? ['ok', __('On', 'aipcf-ai-provider-for-cloudflare')] : ['', __('Off', 'aipcf-ai-provider-for-cloudflare')], ]); } /** * Each feature's own daily limit, and what it used today. */ private static function shared(): string { $s = get_plugin_settings(); $a = Budget::allocation($s); $today = UsageTracker::todayTotals(); $tasks = (array) ($today['tasks'] ?? []); $used = (float) ($today['neurons'] ?? 0); $pill = static fn(bool $on): string => $on ? Ui::pill(__('On', 'aipcf-ai-provider-for-cloudflare'), 'ok') : Ui::pill(__('Off', 'aipcf-ai-provider-for-cloudflare')); $names = [ 'backfill' => [__('Backfill', 'aipcf-ai-provider-for-cloudflare'), FeaturesPage::url('backfill')], 'embed' => [__('Content index', 'aipcf-ai-provider-for-cloudflare'), SettingsPage::settingsUrl('content-index')], 'links' => [__('Link assistant', 'aipcf-ai-provider-for-cloudflare'), FeaturesPage::url('links')], 'search' => [__('Search', 'aipcf-ai-provider-for-cloudflare'), FeaturesPage::url('search')], 'insights' => [__('Search insights', 'aipcf-ai-provider-for-cloudflare'), FeaturesPage::url('insights')], 'chat' => [__('Site assistant', 'aipcf-ai-provider-for-cloudflare'), AssistantPage::pageUrl()], 'agents' => [__('AI agents', 'aipcf-ai-provider-for-cloudflare'), FeaturesPage::url('agents')], ]; $usedBy = [ 'backfill' => UsageTracker::todayBackfillNeurons(), 'embed' => UsageTracker::todayEmbedNeurons(), 'links' => (float) ($tasks['links'] ?? 0), 'search' => (float) ($tasks['search'] ?? 0), 'insights' => (float) ($tasks['insights'] ?? 0), 'chat' => (float) ($tasks['chat'] ?? 0) + (float) ($tasks['chat-public'] ?? 0), 'agents' => (float) ($tasks['agents'] ?? 0), ]; $rows = []; $counted = 0.0; foreach ($a['tools'] as $t) { $u = $usedBy[$t['key']]; $counted += $u; $rows[] = [ Ui::link($names[$t['key']][0], $names[$t['key']][1]), $pill((bool) $t['on']), esc_html($t['limit'] > 0 ? Ui::num($t['limit']) : __('None of its own', 'aipcf-ai-provider-for-cloudflare')), esc_html(Ui::num($u)), ]; } $cap = (int) FeatureSettings::get('related_daily_queries'); $queries = (int) ($today['queries']['related'] ?? 0); $rows[] = [ Ui::link(__('Related posts', 'aipcf-ai-provider-for-cloudflare'), FeaturesPage::url('related')), $pill(!empty(FeatureSettings::get('related_enabled'))), /* translators: %s: searches of the content index a day */ esc_html($cap > 0 ? sprintf(__('No neurons; %s index searches', 'aipcf-ai-provider-for-cloudflare'), Ui::num($cap)) : __('No neurons', 'aipcf-ai-provider-for-cloudflare')), /* translators: %s: number of searches of the content index */ esc_html(sprintf(_n('%s search', '%s searches', $queries, 'aipcf-ai-provider-for-cloudflare'), Ui::num($queries))), ]; $rows[] = [ esc_html__('AI in the editor', 'aipcf-ai-provider-for-cloudflare'), '', esc_html__('What the features leave', 'aipcf-ai-provider-for-cloudflare'), esc_html(Ui::num(max(0.0, $used - $counted))), ]; $share = (int) round(100 * $a['limits'] / max(1, $a['ceiling'])); $over = $a['limits'] > $a['ceiling']; $foot = [ esc_html__('Features that are on', 'aipcf-ai-provider-for-cloudflare'), $over ? Ui::pill(__('Over', 'aipcf-ai-provider-for-cloudflare'), 'bad') : '', /* translators: 1: the features' limits added up, 2: the daily budget, 3: percentage */ esc_html(sprintf(__('%1$s of %2$s (%3$d%%)', 'aipcf-ai-provider-for-cloudflare'), Ui::num($a['limits']), Ui::num($a['ceiling']), $share)), /* translators: 1: neurons used today, 2: the daily budget */ esc_html(sprintf(__('%1$s of %2$s', 'aipcf-ai-provider-for-cloudflare'), Ui::num($used), Ui::num($a['ceiling']))), ]; $body = ''; if ($over) { $body .= Ui::notice(sprintf( /* translators: 1: the features' limits added up, 2: how much over the budget */ __('The features\' limits add up to %1$s neurons, %2$s more than the budget. Lower them or raise the budget.', 'aipcf-ai-provider-for-cloudflare'), Ui::num($a['limits']), Ui::num($a['limits'] - $a['ceiling']) ), 'warn'); } $body .= Ui::table( [__('Feature', 'aipcf-ai-provider-for-cloudflare'), __('Status', 'aipcf-ai-provider-for-cloudflare'), __('Daily limit', 'aipcf-ai-provider-for-cloudflare'), __('Used today', 'aipcf-ai-provider-for-cloudflare')], $rows, $foot ); return Ui::card(__('How it is shared', 'aipcf-ai-provider-for-cloudflare'), $body, [ 'text' => __('Each feature also has its own daily limit; reaching it stops only that feature.', 'aipcf-ai-provider-for-cloudflare'), ]); } private static function caching(): string { $s = get_plugin_settings(); $body = Ui::row( __('Edge cache', 'aipcf-ai-provider-for-cloudflare'), Ui::number('gateway_cache_ttl', (int) ($s['gateway_cache_ttl'] ?? 0), 'aipcf-gateway-cache-ttl', ['min' => 0, 'max' => 2592000, 'unit' => __('seconds', 'aipcf-ai-provider-for-cloudflare')]), [ 'for' => 'aipcf-gateway-cache-ttl', 'help' => __('Needs an AI Gateway. 0 is off; 3600 (an hour) is a good start.', 'aipcf-ai-provider-for-cloudflare'), 'more' => '

' . esc_html__('Creative requests are never cached. Regenerating within ten minutes skips the cache.', 'aipcf-ai-provider-for-cloudflare') . '

', ] ); $body .= Ui::moreSettings(Ui::row( __('Local cache', 'aipcf-ai-provider-for-cloudflare'), Ui::number('local_cache_ttl', (int) ($s['local_cache_ttl'] ?? 0), 'aipcf-local-cache-ttl', ['min' => 0, 'max' => 2592000, 'unit' => __('seconds', 'aipcf-ai-provider-for-cloudflare')]), ['for' => 'aipcf-local-cache-ttl', 'help' => __('Kept on this site, for sites without a gateway. 0 is off.', 'aipcf-ai-provider-for-cloudflare')] )); return Ui::card(__('Caching', 'aipcf-ai-provider-for-cloudflare'), $body, [ 'text' => __('The same request twice is answered from the cache, for no neurons.', 'aipcf-ai-provider-for-cloudflare'), ]); } }