Skip to content

fix: link integration messages to chat sessions - #4750

Merged
mdmohsin7 merged 2 commits into
mainfrom
fix/integration-messages-chat-session
Feb 11, 2026
Merged

mdmohsin7 merged 2 commits into
mainfrom
fix/integration-messages-chat-session

Conversation

@mdmohsin7

Copy link
Copy Markdown
Member

Summary

  • Messages sent via the notification API (POST /v1/integrations/notification) were missing chat_session_id, so they were filtered out when the app queries messages — the user received a push notification but the message never appeared in chat
  • Added add_integration_chat_message() in database/chat.py that looks up the user's existing chat session and sets chat_session_id on the message, and adds the message ID to the session's message_ids
  • Updated the notification endpoint to use add_integration_chat_message instead of add_app_message

Test plan

  • Send a notification via the API with chat_messages.target: "main" — message should appear in main chat
  • Send a notification via the API with chat_messages.target: "app" — message should appear in the app's chat
  • Verify existing add_app_message callers (conversation processing, proactive notifications) are unaffected

🤖 Generated with Claude Code

mdmohsin7 and others added 2 commits February 11, 2026 22:11
Messages sent via the notification API were missing chat_session_id,
causing them to be filtered out when the app queries messages.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Switch from add_app_message to add_integration_chat_message so that
messages from integrations are properly linked to chat sessions

Co-Authored-By: Claude Opus 4.6 <[email protected]>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an issue where messages from integrations were not appearing in the chat. The fix introduces a new function, add_integration_chat_message, to correctly associate these messages with a user's chat session. The notification endpoint is updated to use this new function.

The overall approach is correct. However, I've identified a critical issue in the implementation of add_integration_chat_message. It currently only works if a chat session already exists. If no session is found, the message is created without a session ID and will still not appear in the chat, which was the original problem. I've provided a suggestion to create a new chat session if one doesn't exist to ensure the fix is robust.

Comment thread backend/database/chat.py
Comment on lines +87 to +106
def add_integration_chat_message(text: str, app_id: Optional[str], uid: str) -> Message:
"""Add a chat message from an external integration (e.g. notification API),
linking it to the user's existing chat session so it appears in the chat feed."""
chat_session = get_chat_session(uid, app_id=app_id)
chat_session_id = chat_session['id'] if chat_session else None

ai_message = Message(
id=str(uuid.uuid4()),
text=text,
created_at=datetime.now(timezone.utc),
sender='ai',
app_id=app_id,
from_external_integration=True,
type='text',
chat_session_id=chat_session_id,
)
add_message(uid, ai_message.dict())
if chat_session_id:
add_message_to_chat_session(uid, chat_session_id, ai_message.id)
return ai_message

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

The current implementation only links the integration message to a chat session if one already exists. If get_chat_session returns None, the message is created with chat_session_id=None and is not added to any session's message_ids. This means the message will still not appear in the chat feed if it's the first message for that user/app combination, which partially defeats the purpose of this fix.

To ensure the message always appears, a new chat session should be created if one doesn't exist. This guarantees that every integration message is linked to a session.

def add_integration_chat_message(text: str, app_id: Optional[str], uid: str) -> Message:
    """Add a chat message from an external integration (e.g. notification API),
    linking it to the user's existing chat session so it appears in the chat feed."""
    chat_session = get_chat_session(uid, app_id=app_id)
    if not chat_session:
        from models.chat import ChatSession
        new_session_data = ChatSession(
            id=str(uuid.uuid4()),
            app_id=app_id,
            created_at=datetime.now(timezone.utc),
        ).dict()
        chat_session = add_chat_session(uid, new_session_data)
    chat_session_id = chat_session['id']

    ai_message = Message(
        id=str(uuid.uuid4()),
        text=text,
        created_at=datetime.now(timezone.utc),
        sender='ai',
        app_id=app_id,
        from_external_integration=True,
        type='text',
        chat_session_id=chat_session_id,
    )
    add_message(uid, ai_message.dict())
    add_message_to_chat_session(uid, chat_session_id, ai_message.id)
    return ai_message
References
  1. Direct instantiation of Pydantic models with internally generated data, as seen in ChatSession(...), is acceptable without a try-except block for validation errors, aligning with the practice for controlled data sources.

@mdmohsin7

Copy link
Copy Markdown
Member Author

This is only for chat tools because they can decide whether to send in main chat or their app specific chat

@mdmohsin7
mdmohsin7 merged commit 80f723a into main Feb 11, 2026
1 check passed
@mdmohsin7
mdmohsin7 deleted the fix/integration-messages-chat-session branch February 11, 2026 17:07
@beastoin beastoin mentioned this pull request Feb 12, 2026
ellaaicare pushed a commit to ellaaicare/omi that referenced this pull request Apr 12, 2026
## Summary
- Messages sent via the notification API (`POST
/v1/integrations/notification`) were missing `chat_session_id`, so they
were filtered out when the app queries messages — the user received a
push notification but the message never appeared in chat
- Added `add_integration_chat_message()` in `database/chat.py` that
looks up the user's existing chat session and sets `chat_session_id` on
the message, and adds the message ID to the session's `message_ids`
- Updated the notification endpoint to use
`add_integration_chat_message` instead of `add_app_message`

## Test plan
- [ ] Send a notification via the API with `chat_messages.target:
"main"` — message should appear in main chat
- [ ] Send a notification via the API with `chat_messages.target: "app"`
— message should appear in the app's chat
- [ ] Verify existing `add_app_message` callers (conversation
processing, proactive notifications) are unaffected

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Glucksberg pushed a commit to Glucksberg/omi-local that referenced this pull request Apr 28, 2026
## Summary
- Messages sent via the notification API (`POST
/v1/integrations/notification`) were missing `chat_session_id`, so they
were filtered out when the app queries messages — the user received a
push notification but the message never appeared in chat
- Added `add_integration_chat_message()` in `database/chat.py` that
looks up the user's existing chat session and sets `chat_session_id` on
the message, and adds the message ID to the session's `message_ids`
- Updated the notification endpoint to use
`add_integration_chat_message` instead of `add_app_message`

## Test plan
- [ ] Send a notification via the API with `chat_messages.target:
"main"` — message should appear in main chat
- [ ] Send a notification via the API with `chat_messages.target: "app"`
— message should appear in the app's chat
- [ ] Verify existing `add_app_message` callers (conversation
processing, proactive notifications) are unaffected

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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