kimaai_chatbot_after_response

When it fires

Once per answer, after the answer was saved to the chat history, on both paths:

  • Not streamed: after kimaai_chatbot_response_payload, with the filtered JSON answer, before it goes back to the browser.
  • Streamed: after the last event with the full answer was sent, before the stream closes. $response has the same keys as on the other path: the answer as it was saved, with sessionId added. kimaai_chatbot_response_payload does not run for a stream, so keys a callback of that filter adds are only there when the answer did not stream.

The chatbot, the admin assistant and the search answer share these runners; $payload['assistantSurface'] is admin_assistant or search outside the chat. The action does not fire when the provider fails (see kimaai_chatbot_error), nor for a quick action whose callback answers without the provider. Up to 1.5.13 it fired only when the answer did not stream.

Parameters

NameTypeDescription
responsearrayThe 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.

Example

add_action(
  'kimaai_chatbot_after_response',
  function (array $response, array $ai_payload, array $payload): void {
    // Answers per day and surface, in an option of their own.
    $surface = $payload['assistantSurface'] ?? 'chatbot';
    $counts = (array) get_option('my_chatbot_answers', []);
    $day = gmdate('Y-m-d');
    $counts[$day][$surface] = (int) ($counts[$day][$surface] ?? 0) + 1;
    update_option('my_chatbot_answers', $counts, false);
  },
  10,
  3
);
  • kimaai_chatbot_error: fires instead when the provider fails.
  • kimaai_chatbot_before_request: fires before the request.
  • kimaai_chat_response_saved: fires when the answer is saved, with the provider’s raw response and its token usage.

What are your feelings