Connectors. * * @package ACS_Contact_Form */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * System instruction handed to the model. * * @return string */ function acscf_ai_system_instruction() { return __( 'You are a customer support agent. Write only the body of a reply email to the inquiry you receive. Do not invent facts; when something is unclear, ask about it instead. Never commit to a price or a delivery date on your own.', 'acs-contact-form' ); } /** * Build the prompt handed to the model. * * @param array $entry Entry data. * @return string */ function acscf_ai_build_prompt( $entry ) { $ai = acscf_get_ai_settings(); $tones = array( 'polite' => __( 'a polite business tone', 'acs-contact-form' ), 'friendly' => __( 'a warm, friendly tone', 'acs-contact-form' ), 'brief' => __( 'a short, concise tone', 'acs-contact-form' ), ); $tone = isset( $tones[ $ai['tone'] ] ) ? $tones[ $ai['tone'] ] : $tones['polite']; $lines = array(); $lines[] = __( 'Write a draft reply email to the following inquiry.', 'acs-contact-form' ); $lines[] = ''; /* translators: %s: site name. */ $lines[] = sprintf( __( 'Site: %s', 'acs-contact-form' ), get_bloginfo( 'name' ) ); if ( '' !== $ai['sender_name'] ) { /* translators: %s: name of the person replying. */ $lines[] = sprintf( __( 'Signed by: %s', 'acs-contact-form' ), $ai['sender_name'] ); } /* translators: %s: tone description. */ $lines[] = sprintf( __( 'Tone: %s', 'acs-contact-form' ), $tone ); /* translators: %s: category label. */ $lines[] = sprintf( __( 'Category: %s', 'acs-contact-form' ), acscf_category_label( $entry['category'] ) ); $lines[] = ''; /* translators: %s: sender name. */ $lines[] = sprintf( __( 'From: %s', 'acs-contact-form' ), '' !== $entry['name'] ? $entry['name'] : '-' ); /* translators: %s: subject. */ $lines[] = sprintf( __( 'Subject: %s', 'acs-contact-form' ), '' !== $entry['subject'] ? $entry['subject'] : '-' ); $lines[] = ''; $lines[] = __( '--- Inquiry ---', 'acs-contact-form' ); foreach ( $entry['fields'] as $field ) { $lines[] = $field['label'] . ': ' . $field['value']; } $lines[] = __( '--- End of inquiry ---', 'acs-contact-form' ); $lines[] = ''; $lines[] = __( 'Rules: do not invent facts; when something is unclear, ask about it instead; never commit to a price or a delivery date on your own; output the body only, with no preamble and no explanation.', 'acs-contact-form' ); return implode( "\n", $lines ); } /** * Ask the WordPress core AI Client for a reply draft. * * The model, the provider and the credentials all belong to core. This plugin * only hands over the prompt and reads the text back. * * @param array $entry Entry data. * @return string|WP_Error */ function acscf_generate_reply_draft( $entry ) { if ( ! acscf_ai_client_available() ) { return new WP_Error( 'acscf_ai_unsupported', __( 'AI features are not available on this site.', 'acs-contact-form' ) ); } if ( ! acscf_ai_is_enabled() ) { return new WP_Error( 'acscf_ai_disabled', __( 'No AI provider is connected. Connect one under Settings › Connectors.', 'acs-contact-form' ) ); } $result = wp_ai_client_prompt( acscf_ai_build_prompt( $entry ) ) ->using_system_instruction( acscf_ai_system_instruction() ) ->using_max_tokens( 1200 ) ->generate_text(); if ( is_wp_error( $result ) ) { return new WP_Error( 'acscf_ai_failed', sprintf( /* translators: %s: error message returned by the WordPress AI Client. */ __( 'The reply draft could not be created. %s', 'acs-contact-form' ), $result->get_error_message() ) ); } $text = trim( (string) $result ); if ( '' === $text ) { return new WP_Error( 'acscf_ai_empty', __( 'No reply draft was returned.', 'acs-contact-form' ) ); } return $text; } /** * Create a draft right after a submission, when that option is on. * * @param int $entry_id Entry post ID. * @return void */ function acscf_maybe_auto_draft( $entry_id ) { $ai = acscf_get_ai_settings(); if ( '1' !== $ai['auto_draft'] || ! acscf_ai_is_enabled() ) { return; } $entry = acscf_get_entry( $entry_id ); if ( ! $entry ) { return; } $draft = acscf_generate_reply_draft( $entry ); if ( ! is_wp_error( $draft ) ) { update_post_meta( $entry_id, '_acscf_draft', wp_kses_post( $draft ) ); } } /** * Ajax handler for the "make a draft" button. * * @return void */ function acscf_ajax_generate_draft() { if ( ! check_ajax_referer( 'acscf_ai_draft', 'nonce', false ) ) { wp_send_json_error( array( 'message' => __( 'Your session has expired. Please reload the page.', 'acs-contact-form' ) ) ); } if ( ! current_user_can( acscf_manage_capability() ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to do this.', 'acs-contact-form' ) ) ); } if ( ! acscf_ai_is_enabled() ) { wp_send_json_error( array( 'message' => __( 'No AI provider is connected. Connect one under Settings › Connectors.', 'acs-contact-form' ) ) ); } $entry_id = isset( $_POST['entry_id'] ) ? absint( wp_unslash( $_POST['entry_id'] ) ) : 0; $entry = acscf_get_entry( $entry_id ); if ( ! $entry ) { wp_send_json_error( array( 'message' => __( 'Inquiry not found.', 'acs-contact-form' ) ) ); } $draft = acscf_generate_reply_draft( $entry ); if ( is_wp_error( $draft ) ) { wp_send_json_error( array( 'message' => $draft->get_error_message() ) ); } update_post_meta( $entry_id, '_acscf_draft', wp_kses_post( $draft ) ); wp_send_json_success( array( 'draft' => $draft ) ); } add_action( 'wp_ajax_acscf_ai_draft', 'acscf_ajax_generate_draft' );