kimaai_chatbot_promp

kimaai_chatbot_prompt Filter #

The kimaai_chatbot_prompt filter lets you mutate or normalize the incoming user message (“prompt”) before any blocking checks (gate), rate limiting decisions, tool selection, or AI provider calls.

Typical uses:

  • trimming / normalizing whitespace
  • stripping unwanted HTML
  • redacting sensitive data (emails, phone numbers, card-like patterns)
  • appending extra context (lightweight, user-safe metadata)
  • enforcing your own prompt formatting rules

When it runs #

In the chatbot REST handler (/kimaai/v1/chatbot), the plugin reads the JSON body and builds a normalized payload:

  • prompt from message
  • postId from request

Then it applies this filter:

$payload['prompt'] = apply_filters('kimaai_chatbot_prompt', $payload['prompt'], $data, $request);

This occurs before:

  • kimaai_chatbot_gate (allow/block)
  • max messages per day check
  • max input length check
  • tools registry / provider request build
  • AI request

Filter signature #

/**
 * Filter: mutate/normalize the incoming user message before any checks.
 *
 * @param string $prompt The raw/normalized prompt text.
 * @param array $data Full JSON body from the request.
 * @param WP_REST_Request $request The REST request object.
 * @return string The modified prompt.
 */
$payload['prompt'] = apply_filters('kimaai_chatbot_prompt', $payload['prompt'], $data, $request);

Parameters #

1) $prompt (string) #

The current normalized prompt string (initially from the request JSON message).

2) $data (array) #

The raw JSON body from the REST request (e.g. message, postId, etc.).

3) $request (WP_REST_Request) #

The full request object (headers, route, parameters).

Return value #

Return the final prompt string to be used for the rest of the request lifecycle.


Basic example: trim and normalize whitespace #

add_filter('kimaai_chatbot_prompt', function (string $prompt): string {
  // Collapse repeated whitespace to a single space.
  $prompt = preg_replace('/\s+/u', ' ', $prompt) ?? $prompt;

  return trim($prompt);
}, 10, 1);

Example: strip HTML / shortcodes #

If your frontend can send HTML (or you want to be defensive), sanitize it:

add_filter('kimaai_chatbot_prompt', function (string $prompt): string {
  // Remove shortcodes, then strip tags.
  $prompt = strip_shortcodes($prompt);
  $prompt = wp_strip_all_tags($prompt, true);

  return trim($prompt);
}, 10, 1);

Example: redact sensitive patterns (privacy) #

This example redacts emails and phone-like patterns before anything else processes the message.

add_filter('kimaai_chatbot_prompt', function (string $prompt): string {
  // Redact emails.
  $prompt = preg_replace('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/iu', '[redacted email]', $prompt) ?? $prompt;

  // Redact phone-ish patterns (very simple).
  $prompt = preg_replace('/(\+?\d[\d\s().-]{7,}\d)/u', '[redacted phone]', $prompt) ?? $prompt;

  return $prompt;
}, 10, 1);

Example: prepend lightweight context (post/page info) #

You can add context that helps the AI answer better. Keep it short and user-safe.

add_filter('kimaai_chatbot_prompt', function (string $prompt, array $data): string {
  $post_id = isset($data['postId']) ? (int) $data['postId'] : 0;
  if ($post_id <= 0) {
    return $prompt;
  }

  $title = get_the_title($post_id);
  if (!$title) {
    return $prompt;
  }

  return "Page: {$title}\nUser: {$prompt}";
}, 10, 2);

Example: enforce a consistent “Q:” format #

Useful if you want a consistent structure for downstream logging or prompting:

add_filter('kimaai_chatbot_prompt', function (string $prompt): string {
  $prompt = trim($prompt);

  // Avoid double-prefixing.
  if (stripos($prompt, 'Q:') === 0) {
    return $prompt;
  }

  return 'Q: ' . $prompt;
}, 10, 1);

Notes and best practices #

  • This filter runs very early, so it’s the best place for:
    • sanitization
    • normalization
    • redaction
  • Be mindful of the plugin’s max input length check (maxInputLength) which runs later. If you add text, you may push some prompts over the limit.
  • Always return a string. If you return something else, later code may behave unexpectedly.
  • Don’t echo output—this runs during a REST request.

  • kimaai_chatbot_gate — block/allow requests after prompt normalization
  • kimaai_chatbot_blocked — fires when the gate blocks a request
  • kimaai_chatbot_ai_payload — modify the provider request payload
  • kimaai_chatbot_before_request / kimaai_chatbot_after_response — telemetry around the AI call
Updated on December 13, 2025