kimaai_chatbot_response_payload

When it fires

Only when the answer does not stream: after the provider answered and the answer was saved, right before kimaai_chatbot_after_response and before the JSON goes back with status 200. A streamed answer has no JSON body to filter, so the filter does not run then; use kimaai_chatbot_final_message for the answer text on both paths. It does not run for an error either, nor for a quick action whose callback answers without the provider.

The chatbot, the admin assistant and the search answer share this request runner, so the filter fires for all three; $payload['assistantSurface'] is admin_assistant or search outside the chat. KimaAI’s widget reads message, sessionId, history and renderables: add keys of your own for a client of your own, and leave those as they are unless you mean to change what the widget shows. The search answer box builds its answer, and the cached copy, from the filtered response.

Parameters

NameTypeDescription
responsearrayThe JSON answer, with message, session_id, sessionId, history and, when there are any, renderables and sources.
ai_payloadarrayThe request to the provider, after kimaai_chatbot_ai_payload.
payloadarrayThe normalised request payload, with prompt, sessionId and chatbot_settings among others, and assistantSurface (admin_assistant or search) outside the chat.
chatbot_settingsarrayThe chatbot settings, or the admin assistant’s in its requests.

Returns

array: The JSON answer to send.

Example

add_filter(
  'kimaai_chatbot_response_payload',
  function (array $response, array $ai_payload): array {
    // A headless front end shows which model answered.
    $response['model'] = (string) ($ai_payload['aiModel'] ?? '');

    return $response;
  },
  10,
  2
);
  • kimaai_chatbot_after_response: fires after the answer, streamed or not.
  • kimaai_chatbot_final_message: changes the answer text on both paths.

What are your feelings