Repository navigation
fix: link integration messages to chat sessions - #4750
Conversation
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]>
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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_messageReferences
- Direct instantiation of Pydantic models with internally generated data, as seen in
ChatSession(...), is acceptable without atry-exceptblock for validation errors, aligning with the practice for controlled data sources.
|
This is only for chat tools because they can decide whether to send in main chat or their app specific chat |
## 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)
## 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)
Summary
POST /v1/integrations/notification) were missingchat_session_id, so they were filtered out when the app queries messages — the user received a push notification but the message never appeared in chatadd_integration_chat_message()indatabase/chat.pythat looks up the user's existing chat session and setschat_session_idon the message, and adds the message ID to the session'smessage_idsadd_integration_chat_messageinstead ofadd_app_messageTest plan
chat_messages.target: "main"— message should appear in main chatchat_messages.target: "app"— message should appear in the app's chatadd_app_messagecallers (conversation processing, proactive notifications) are unaffected🤖 Generated with Claude Code