supportcandy
/
includes
/
admin
/
ai-chatbot
/
tools
/
core
/
class-wpsc-acb-search-knowledge-base.php
class-wpsc-acb-search-knowledge-base.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.2, at includes/admin/ai-chatbot/tools/core/class-wpsc-acb-search-knowledge-base.php
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly! |
| 4 | } |
| 5 | |
| 6 | if ( ! class_exists( 'WPSC_ACB_Search_Knowledge_Base' ) ) : |
| 7 | |
| 8 | final class WPSC_ACB_Search_Knowledge_Base { |
| 9 | |
| 10 | /** |
| 11 | * Initialize the tool. |
| 12 | */ |
| 13 | public static function init() { |
| 14 | |
| 15 | add_filter( 'wpsc_acb_tool_registry', array( __CLASS__, 'register_tool' ) ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Register the tool in the registry. |
| 20 | * |
| 21 | * @param array $registry Current tool registry. |
| 22 | * @return array |
| 23 | */ |
| 24 | public static function register_tool( $registry ) { |
| 25 | |
| 26 | $registry['search_knowledge_base'] = array( |
| 27 | 'name' => 'search_knowledge_base', |
| 28 | 'description' => 'Search the knowledge base for information relevant to the user question. Use this tool for informational questions, troubleshooting, setup steps, policy/process questions, and product capability queries when an action tool does not fit. Do not use this tool for ticket-confirmation decisions or spam moderation decisions (use detect_spam for spam).', |
| 29 | 'parameters' => array( |
| 30 | 'type' => 'object', |
| 31 | 'properties' => array( |
| 32 | 'query' => array( |
| 33 | 'type' => 'string', |
| 34 | 'description' => __( 'A focused, standalone search query derived from the user request. If the customer message is a follow-up that relies on earlier conversation context (for example "how do I set it up?" or "what about email notifications?"), resolve any pronouns or vague references using the conversation history and write the query as a fully self-contained question naming the actual topic - never pass an ambiguous reference as-is.', 'wpsc-ps' ), |
| 35 | ), |
| 36 | ), |
| 37 | 'required' => array( 'query' ), |
| 38 | 'additionalProperties' => false, |
| 39 | ), |
| 40 | 'handler' => 'execute_tool_search_knowledge_base', |
| 41 | 'class' => __CLASS__, |
| 42 | ); |
| 43 | return $registry; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Execute search knowledge base tool. |
| 48 | * |
| 49 | * @param array $args Tool arguments. |
| 50 | * @param string $session_id Session ID. |
| 51 | * @return array |
| 52 | */ |
| 53 | public static function execute_tool_search_knowledge_base( $args, $session_id ) { |
| 54 | |
| 55 | $ai_settings = get_option( 'wpsc-ps-ai-assistant-settings', array() ); |
| 56 | $query = isset( $args['query'] ) ? sanitize_text_field( (string) $args['query'] ) : ''; |
| 57 | $provider = WPSC_AIBOT_Provider_Factory::get_current_provider( $ai_settings['provider'] ); |
| 58 | $prompt = self::build_search_prompt(); |
| 59 | return $provider->search_knowledge_base( $ai_settings, $prompt, $query ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Build a prompt for retrieval-only knowledge search. |
| 64 | * |
| 65 | * @return string |
| 66 | */ |
| 67 | private static function build_search_prompt() { |
| 68 | |
| 69 | $lines = array( |
| 70 | 'You are a support knowledge search assistant.', |
| 71 | |
| 72 | 'STRICT RESPONSE RULES:', |
| 73 | '- Use ONLY information from file_search results. Never use outside knowledge, and never guess, infer, or fill gaps with assumptions.', |
| 74 | '- A result counts as a match if it addresses the same underlying question or intent the customer is asking, even if the exact wording, phrasing, or terminology differs (synonyms, rephrasing, abbreviations) - do not require literal keyword overlap.', |
| 75 | '- Do not use a result just because it mentions the same general product/feature/category without actually answering what was asked - being loosely or generically related is not enough.', |
| 76 | '- If multiple results are only loosely or generically related, prefer the single most specific one over blending them.', |
| 77 | '- If no result actually answers the question, even after considering rephrasing and synonyms, respond EXACTLY with: [NO_KB_FOUND]. Never invent an answer instead.', |
| 78 | '- Never state a phone number, email address, physical address, or other contact detail unless that exact detail appears verbatim in the file_search results - never invent, complete, guess, or "helpfully" fill in one, even if it seems plausible or typical for a support context.', |
| 79 | '- Never state any other specific fact (a number, date, price, or named detail) that does not appear verbatim in the file_search results - if the results only support a general statement, give the general statement rather than a more specific-sounding invented one.', |
| 80 | '- Output must be valid HTML only.', |
| 81 | '- DO NOT use Markdown in any form.', |
| 82 | '- DO NOT use **, __, #, backticks, or any Markdown symbols.', |
| 83 | '- DO NOT format text with asterisks.', |
| 84 | '- Convert any emphasis into plain text or HTML <strong> tags only if necessary.', |
| 85 | '- Use only these HTML tags: <p>, <ul>, <ol>, <li>, <br>, <strong>', |
| 86 | |
| 87 | 'SOURCE HANDLING RULE:', |
| 88 | '- Never reveal how or where the information was retrieved.', |
| 89 | '- Never include citations or references.', |
| 90 | '- Never mention knowledge base, files, or search.', |
| 91 | |
| 92 | 'STYLE:', |
| 93 | '- Keep the response concise and practical.', |
| 94 | '- Use simple and clear language.', |
| 95 | '- Maintain conversational continuity.', |
| 96 | |
| 97 | 'OUTPUT:', |
| 98 | '- Return ONLY HTML.', |
| 99 | '- Do NOT include explanations, notes, or formatting outside HTML.', |
| 100 | ); |
| 101 | |
| 102 | return implode( "\n", $lines ); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | endif; |
| 107 | WPSC_ACB_Search_Knowledge_Base::init(); |
| 108 |