test( $provider, KeyStore::model(), KeyStore::key( $provider ) ); if ( true === $result ) { KeyStore::mark_tested(); self::redirect( 'own_key_ok' ); } set_transient( self::ERROR_TRANSIENT . get_current_user_id(), ErrorMessages::for_code( $result->get_error_code(), $result->getMessage() ), 5 * MINUTE_IN_SECONDS ); self::redirect( 'own_key_failed' ); } /** * Forget the key (the AI stops answering until a key is saved again; slash commands keep working). */ public function handle_delete(): void { if ( ! self::can_manage() ) { wp_die( esc_html__( 'You are not allowed to access this page.', 'addsynapse-store-assistant' ), 403 ); } check_admin_referer( self::ACTION_DELETE ); KeyStore::delete(); self::redirect( 'own_key_deleted' ); } /** * Redirect back with a notice code. * * @param string $notice Notice. */ private static function redirect( string $notice ): void { wp_safe_redirect( SettingsPage::url( array( 'addsynapse_assistant_notice' => $notice ) ) . '#addsynapse-assistant-own-key' ); exit; } /** * Provider display name. * * @param string $provider Provider id. */ public static function label( string $provider ): string { $labels = array( 'anthropic' => 'Anthropic (Claude)', 'openai' => 'OpenAI (GPT)', ); return $labels[ $provider ] ?? ''; } /** * Renders the section. * * @param string $notice Notice code of this page load. */ public static function render( string $notice ): void { $settings = KeyStore::settings(); echo '
' . esc_html__( 'Use the assistant with your own Anthropic or OpenAI API key. The plugin and every one of its tools are free; you pay your AI provider directly for what you use.', 'addsynapse-store-assistant' ) . '
'; echo '' . esc_html__( 'When you chat, your message, the conversation, a summary of the site structure and the results of the tools the assistant uses for your question (for example product names, prices, stock and sales figures — never customer names, emails or phone numbers) are sent from this server to the AI provider you choose, under that provider\'s terms and privacy policy. An image you attach in the chat is sent with that message (a reduced copy; you can turn this off below: «The AI sees the images you upload»). Nothing is sent before you press «Save and test», and nothing goes to AddSynapse.', 'addsynapse-store-assistant' ) . '
'; echo ''; echo esc_html__( 'Anthropic:', 'addsynapse-store-assistant' ) . ' ' . esc_html__( 'Terms', 'addsynapse-store-assistant' ) . ' · ' . esc_html__( 'Privacy', 'addsynapse-store-assistant' ) . ' — '; echo esc_html__( 'OpenAI:', 'addsynapse-store-assistant' ) . ' ' . esc_html__( 'Terms', 'addsynapse-store-assistant' ) . ' · ' . esc_html__( 'Privacy', 'addsynapse-store-assistant' ) . ''; echo '
'; if ( 'own_key_ok' === $notice ) { echo '' . esc_html__( 'The key works. The chat now uses your own AI key.', 'addsynapse-store-assistant' ) . '
' . esc_html__( 'The key was saved but the test failed, so the chat does not use it yet.', 'addsynapse-store-assistant' ) . ' ' . esc_html( $message ) . '
' . esc_html__( 'Choose a provider and one of its models (a model of the other provider cannot be used). Nothing was saved.', 'addsynapse-store-assistant' ) . '
' . esc_html__( 'This does not look like an API key (only letters, digits and - _ . : are allowed). Nothing was saved.', 'addsynapse-store-assistant' ) . '
' . esc_html__( 'Your AI key was removed from this site.', 'addsynapse-store-assistant' ) . '
' . esc_html__( 'The saved key can no longer be read (the site\'s security keys changed). Enter it again.', 'addsynapse-store-assistant' ) . '
' . esc_html( sprintf( __( 'Active: %1$s, model %2$s, key %3$s.', 'addsynapse-store-assistant' ), self::label( $settings['provider'] ), $settings['model'], KeyStore::hint() ) ) . '
'; } else { // Shop managers see that the AI is set up, never any part of the key (administrators only). /* translators: 1: provider name, 2: model id. */ echo '' . esc_html( sprintf( __( 'Active: %1$s, model %2$s.', 'addsynapse-store-assistant' ), self::label( $settings['provider'] ), $settings['model'] ) ) . '
'; } } if ( ! self::can_manage() ) { echo '' . esc_html__( 'Only an administrator can change the AI key.', 'addsynapse-store-assistant' ) . '
'; echo '| ' . esc_html__( 'AI calls', 'addsynapse-store-assistant' ) . ' | ' . esc_html__( 'Input tokens (of them from cache)', 'addsynapse-store-assistant' ) . ' | ' . esc_html__( 'Output tokens', 'addsynapse-store-assistant' ) . ' | ' . esc_html__( 'Estimated cost (USD)', 'addsynapse-store-assistant' ) . ' | |
|---|---|---|---|---|
| ' . esc_html( $label ) . ' | ' . esc_html( number_format_i18n( (int) $row['calls'] ) ) . ' | ' . esc_html( number_format_i18n( (int) $row['tokens_in'] ) . ' (' . number_format_i18n( (int) $row['cache_read'] ) . ')' ) . ' | ' . esc_html( number_format_i18n( (int) $row['tokens_out'] ) ) . ' | ' . esc_html( $cost ) . ' |
' . esc_html__( 'An estimate from this site\'s records at the providers\' list prices; your AI provider\'s dashboard shows the real bill.', 'addsynapse-store-assistant' ) . '
'; } }