Skip to content

Conversation

brijsiyag-meesho
Copy link

@brijsiyag-meesho brijsiyag-meesho commented Sep 23, 2025

Description

Previously, Slack integration was using single_message_history but wasn't passing this to LLM for previous context.
This solution just passes all thread messages as message_history so LLM can get context of the thread.
Also we are not storing anything at onyx side so it shouldn't be issue in terms of slack

How Has This Been Tested?

Manually

Backporting (check the box to trigger backport action)

Note: You have to check that the action passes, otherwise resolve the conflicts manually and tag the patches.

  • This PR should be backported (make sure to check that the backport attempt succeeds)
  • [Optional] Override Linear Check

Summary by cubic

Added per-message history with token counting for Slack threads to improve context management and prevent token overflow in long conversations.

  • New Features
    • Introduced message_history parameter in stream_chat_message_objects.
    • Slack handler builds a list of PreviousMessage with token_count using the persona’s tokenizer.
    • Passes message_history and single_message_history to chat processing, with a safe fallback when message_history is None.

@brijsiyag-meesho brijsiyag-meesho requested a review from a team as a code owner September 23, 2025 08:21
Copy link

vercel bot commented Sep 23, 2025

@brijsiyag-meesho is attempting to deploy a commit to the Danswer Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Overview

Summary

This PR adds a message history parameter with token counting functionality for Slack integration. The main changes include:

  • Enhanced stream_chat_message_objects() - Added optional message_history parameter that allows passing pre-calculated PreviousMessage objects with accurate token counts
  • Improved Slack token counting - Updated Slack handler to calculate token counts for each message in the thread history using the LLM's tokenizer before creating PreviousMessage objects
  • Better performance - By pre-calculating token counts in the Slack handler, this avoids duplicate tokenization work and provides more accurate token counting for Slack thread contexts

The implementation maintains backward compatibility through proper null checking and fallback logic. When message_history is not provided, the system falls back to the existing behavior of creating message history from history_msgs.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • This is a straightforward enhancement that adds optional token counting functionality. The changes maintain backward compatibility through proper null checking, use appropriate type annotations, and follow the existing codebase patterns. No breaking changes or risky modifications.
  • No files require special attention

Important Files Changed

File Analysis

Filename        Score        Overview
backend/onyx/chat/process_message.py 5/5 Added optional message_history parameter with proper type annotation and fallback logic
backend/onyx/onyxbot/slack/handlers/handle_regular_answer.py 5/5 Added proper token counting for message history and pass calculated message_history to stream_chat_message_objects

Sequence Diagram

sequenceDiagram
    participant SlackHandler as Slack Handler
    participant TokenCount as Token Counter
    participant ProcessMsg as Process Message
    participant PreviousMsg as PreviousMessage

    SlackHandler->>+TokenCount: check_number_of_tokens(message, tokenizer)
    TokenCount-->>-SlackHandler: token_count
    
    SlackHandler->>+PreviousMsg: PreviousMessage(message, role, token_count, files=[], tool_call=None, ...)
    PreviousMsg-->>-SlackHandler: message_history_item
    
    SlackHandler->>+ProcessMsg: stream_chat_message_objects(message_history=message_history)
    
    ProcessMsg->>ProcessMsg: Check if message_history is None
    alt message_history is provided
        ProcessMsg->>ProcessMsg: Use provided message_history with token counts
    else message_history is None
        ProcessMsg->>ProcessMsg: Create message_history from history_msgs using PreviousMessage.from_chat_message()
    end
    
    ProcessMsg-->>-SlackHandler: answer_stream
Loading

2 files reviewed, no comments

Edit Code Review Bot Settings | Greptile

@brijsiyag-meesho
Copy link
Author

@Weves currently this PR contains some patchy solutions, if team thinks we can proceed with this than I will polish the code further.

@brijsiyag-meesho brijsiyag-meesho changed the title Add message history parameter with token counting for Slack integration feat: pass slack thread context to llm as message_history Sep 23, 2025
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files

Prompt for AI agents (all 1 issues)

Understand the root cause of the following 1 issues and fix them.


<file name="backend/onyx/chat/process_message.py">

<violation number="1" location="backend/onyx/chat/process_message.py:256">
Comment above this line is now misleading (describes a string). Update/move it to document single_message_history and add a brief comment for message_history.</violation>
</file>

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

# a string which represents the history of a conversation. Used in cases like
# Slack threads where the conversation cannot be represented by a chain of User/Assistant
# messages.
message_history: List[PreviousMessage] | None = None,
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment above this line is now misleading (describes a string). Update/move it to document single_message_history and add a brief comment for message_history.

Prompt for AI agents
Address the following comment on backend/onyx/chat/process_message.py at line 256:

<comment>Comment above this line is now misleading (describes a string). Update/move it to document single_message_history and add a brief comment for message_history.</comment>

<file context>
@@ -253,6 +253,7 @@ def stream_chat_message_objects(
     # a string which represents the history of a conversation. Used in cases like
     # Slack threads where the conversation cannot be represented by a chain of User/Assistant
     # messages.
+    message_history: List[PreviousMessage] | None = None,
     # NOTE: is not stored in the database at all.
     single_message_history: str | None = None,
</file context>
Fix with Cubic

@brijsiyag-meesho brijsiyag-meesho force-pushed the slack_thread_history branch 2 times, most recently from 544f3ad to b4b96ac Compare September 23, 2025 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant