Created on 21 November 2024, 2 months ago

Problem/Motivation

When using streamed output with the "Allow History" option enabled in the Assistant, and the user is anonymous, an error occurs in the streamed message output. The error details are as follows:

Failed to start the session because headers have already been sent by "/var/www/html/web/modules/contrib/ai/modules/ai_chatbot/src/Form/ChatForm.php" at line 159. in Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->start() (line 132 of /var/www/html/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php).

Steps to reproduce

  1. Create an Assistant with the "Allow History" setting enabled and set to any "Session"-based option.
  2. Add the Assistant to a Chatbox block and enable streaming for the output.
  3. Interact with the Assistant through the Chatbox block as an anonymous user.
  4. Observe the error message appearing beneath the Assistant's response.
🐛 Bug report
Status

Active

Version

1.0

Component

Other Submodules

Created by

🇩🇪Germany ayrmax

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @ayrmax
  • 🇮🇳India Vivek Panicker Kolkata

    To add to this, I find that this happens mainly for Anonymous users.
    The issue happens here .
    The form response is already set and after that the $http_response->setCallback` is executed.

  • 🇺🇸United States shasha821110

    following on this.

  • 🇬🇧United Kingdom MrDaleSmith

    I wonder if we should instead have a method inside $this->aiAssistantRunner to retrieve the session: this could then be used by any code using the assistant API without it having to reroll its own solution?

    Something along the lines of:

      public function getSession () {
        $thread_id = $this->getThreadsKey();
        $session = $this->getTempStore()->get($thread_id);
          if (empty($session)) {
            $session['output_contexts'] = [];
            $this->aiAssistantRunner->getTempStore()->set($thread_id, $session);
          }
       return $session;
      }
    
  • 🇮🇳India Vivek Panicker Kolkata

    I think using the getTempstore() is what is causing the issue.
    Instead we should try to use cookies, as that would reduce the load on the page, and also not cause any caching issues.

  • 🇮🇳India Vivek Panicker Kolkata

    I tried the patch provided in #4.
    But it doesn't work properly for me.
    I get the following response from the chatbot:

    I am sorry, but I could not find any relevant information in the archives. Please try to ask the question in a different way or try to ask a different question.
    

    Works fine for me in admin.

  • 🇩🇪Germany marcus_johansson

    Moving to cookies will need some thought, since there are both privacy and security concerns connected to it. Also architectural. Also writing/editing a cookie after headers are sent would cause the same issue. And they have a limit of 4k in certain browsers.

    The biggest issue is that we are trying to solve it via AI Assistants API, meaning that the frontend can not get involved. Otherwise localstorage would be an option, though once again security and privacy concerns. We could add a mode called frontend/client/whatever handled, where the assistant only takes whatever messages it gets. But the complexity of handling every possible action based on it being handled by backend or frontend is too much.

    For now I have just done the easiest solution, which is to start the session in the streamed output. There is a MR here: https://git.drupalcode.org/project/ai/-/merge_requests/377 - if you have the possibility to test it, please do.

    Also note that the we are moving away from the old custom AI Chatbot to the Deepchat version - we are already showing warnings for it. Its basically just to switch the block type. But that helps us remove a lot of frontend concerns and make the chatbot look more professional. Most likely the old chatbot will be in a contrib module (if we find maintainers) or removed before we go to production.

    • marcus_johansson committed 8895c9b0 on 3488961-headers-have-already
      Issue #3488961: Deepchat should not set historical messages on none
      
  • 🇮🇳India Vivek Panicker Kolkata

    @marcus_johansson I applied the patch https://git.drupalcode.org/project/ai/-/merge_requests/377.diff.
    Now the chatbot doesn't give the error - "Headers have already been sent".
    But when you ask some contextual question, it gives the response -
    "I am sorry, but I could not find any relevant information in the archives. Please try to ask the question in a different way or try to ask a different question."
    Seems like it's not getting the context for some reason.

    The situation is like:

    Me: Tell me about xyz product.
    Chatbot: xzy is a good product....
    Me: What can you tell me about it's availability?
    Chatbot: "I am sorry, but I could not find any relevant information in the archives. Please try to ask the question in a different way or try to ask a different question."
    

    If I repeat the above steps again, the same things happen again.

    Works fine for admin.
    Note: I have provided access to the anonymous, admin and authenticated users in the advanced settings of the AI Assistant.

  • 🇩🇪Germany marcus_johansson

    Thanks all for the issue and testing, its getting merged.

  • 🇺🇸United States shasha821110

    tested the patch and seems works good so far. Thanks Marcus.

  • Status changed to Fixed about 1 hour ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024