kimaai_chatbot_response_payload

kimaai_chatbot_response_payload Filter #

The kimaai_chatbot_response_payload filter lets you modify the AI response payload right after the AI provider returns and before the response is sent back to the frontend.

Use it to:

  • sanitize or redact content
  • enforce formatting (Markdown → plain text, add prefixes, etc.)
  • attach extra fields for your frontend (metadata, citations, debug flags)
  • normalize response structure across providers
  • apply business rules (e.g., remove links, block certain output)

When it runs #

In the chatbot REST handler:

  1. The plugin calls the AI provider: AiService::run($ai_payload, 'chat')
  2. It receives $response (array)
  3. It applies this filter:
$response = apply_filters('kimaai_chatbot_response_payload', $response, $ai_payload, $payload, $chatbot_settings);
  1. Fires kimaai_chatbot_after_response
  2. Returns the final $response in a WP_REST_Response (HTTP 200)

This filter is your last chance to change what the frontend receives on success.


Filter signature #

/**
 * Filter: mutate the AI response before returning to the client.
 *
 * @param array $response Response from the AI service.
 * @param array $ai_payload The provider payload that was sent.
 * @param array $payload Normalized payload.
 * @param array $chatbot_settings Settings.
 * @return array
 */
$response = apply_filters('kimaai_chatbot_response_payload', $response, $ai_payload, $payload, $chatbot_settings);

Parameters #

1) $response (array) #

The response array returned by AiService::run() (and subsequently returned to the frontend after your modifications).

The exact shape depends on your AiService implementation and provider, but commonly includes fields like:

  • message / content
  • role
  • usage (tokens, etc.)
  • tool call information (if applicable)

2) $ai_payload (array) #

The AI request payload that was sent (prompt, model, tool info, etc.).

3) $payload (array) #

Normalized request context (prompt, postId, sessionId, selectedTools, etc.).

4) $chatbot_settings (array) #

Settings used for this request.

Return value #

Return the modified $response array.


Basic example: add a server timestamp #

add_filter('kimaai_chatbot_response_payload', function (array $response): array {
  $response['serverTime'] = time();
  return $response;
}, 10, 1);

Example: redact emails in the AI output #

add_filter('kimaai_chatbot_response_payload', function (array $response): array {
  // Adjust the key to match your response shape.
  if (!isset($response['content']) || !is_string($response['content'])) {
    return $response;
  }

  $response['content'] = preg_replace(
    '/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/iu',
    '[redacted email]',
    $response['content']
  ) ?? $response['content'];

  return $response;
}, 10, 1);

Useful if you don’t want the chatbot to produce clickable URLs.

add_filter('kimaai_chatbot_response_payload', function (array $response): array {
  $key = isset($response['content']) ? 'content' : (isset($response['message']) ? 'message' : null);
  if (!$key || !is_string($response[$key])) {
    return $response;
  }

  // Remove http(s) URLs.
  $response[$key] = preg_replace('/https?:\/\/\S+/iu', '[link removed]', $response[$key]) ?? $response[$key];

  return $response;
}, 10, 1);

Example: normalize output to a content key #

If different providers return different shapes, you can standardize for your frontend.

add_filter('kimaai_chatbot_response_payload', function (array $response): array {
  if (!isset($response['content'])) {
    if (isset($response['message']) && is_string($response['message'])) {
      $response['content'] = $response['message'];
      unset($response['message']);
    }
  }

  return $response;
}, 10, 1);

Example: add debug metadata for admins only #

add_filter('kimaai_chatbot_response_payload', function (
  array $response,
  array $ai_payload,
  array $payload
): array {
  if (!is_user_logged_in() || !current_user_can('manage_options')) {
    return $response;
  }

  $response['_debug'] = [
    'provider' => (string) ($ai_payload['aiProvider'] ?? ''),
    'model' => (string) ($ai_payload['aiModel'] ?? ''),
    'sessionId' => (string) ($payload['sessionId'] ?? ''),
    'tools_count' => !empty($ai_payload['tools']) ? count($ai_payload['tools']) : 0,
  ];

  return $response;
}, 10, 3);

Notes and best practices #

  • This filter runs only on the success path (HTTP 200). Exceptions are handled by kimaai_chatbot_error.
  • Don’t echo output—this is a REST request.
  • Keep transformations efficient; this runs for every chatbot message.
  • Avoid leaking sensitive info:
    • be careful adding debug fields
    • consider removing token usage or internal traces from responses shown to end users

  • kimaai_chatbot_after_response — telemetry after successful responses
  • kimaai_chatbot_error — telemetry on exceptions
  • kimaai_chatbot_ai_payload — modify outgoing request payload
  • kimaai_chatbot_prompt — normalize/redact user input earlier
Updated on December 13, 2025