Skip to content

Commit

Permalink
feat: child chats
Browse files Browse the repository at this point in the history
  • Loading branch information
okradze committed Dec 11, 2023
1 parent 7ca7b23 commit e6133ff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
15 changes: 14 additions & 1 deletion apps/server/controllers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,29 @@ def get_chat_messages(
raise HTTPException(status_code=401, detail="Unauthorized")

session_id = None

if auth:
session_id = get_chat_session_id(
auth.user.id, auth.account.id, agent_id, team_id, chat_id
)
else:
session_id = get_chat_session_id(None, None, None, None, chat_id)

session_ids = [session_id]

# Get child chat session ids to fetch messages from
if chat_id:
child_chats = (
db.session.query(ChatModel).filter(ChatModel.parent_id == chat_id).all()
)

session_ids.extend(
[get_chat_session_id(chat_id=child_chat.id) for child_chat in child_chats]
)

chat_messages = (
db.session.query(ChatMessageModel)
.filter(ChatMessageModel.session_id == session_id)
.filter(ChatMessageModel.session_id.in_(session_ids))
.order_by(ChatMessageModel.created_on.desc())
.limit(50)
.options(
Expand Down
2 changes: 2 additions & 0 deletions apps/server/models/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class ChatModel(BaseModel):

voice_url = Column(String, nullable=True)

parent_id = Column(UUID, ForeignKey("chat.id", ondelete="CASCADE"), index=True)

creator_user_id = Column(
UUID,
ForeignKey("user.id", name="fk_creator_user_id", ondelete="CASCADE"),
Expand Down
1 change: 1 addition & 0 deletions apps/server/typings/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ChatInput(BaseModel):
is_public: Optional[bool]
agent_id: Optional[UUID] = None
team_id: Optional[UUID] = None
parent_id: Optional[UUID] = None


class UpdateChatInput(BaseModel):
Expand Down
23 changes: 14 additions & 9 deletions apps/server/utils/chat.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from typing import Tuple, List
from uuid import UUID
import json
import re
from enum import Enum
from typing import List, Tuple
from uuid import UUID

from models.chat import ChatModel
from typings.chat import ChatOutput
from utils.account import \
convert_model_to_response as account_convert_model_to_response
from utils.agent import \
convert_model_to_response as agent_convert_model_to_response
from utils.team import \
convert_model_to_response as team_convert_model_to_response
from utils.type import convert_value_to_type
from models.chat import ChatModel
from utils.user import convert_model_to_response as user_convert_model_to_response
from utils.team import convert_model_to_response as team_convert_model_to_response
from utils.agent import convert_model_to_response as agent_convert_model_to_response
from utils.account import convert_model_to_response as account_convert_model_to_response
from utils.user import \
convert_model_to_response as user_convert_model_to_response


class MentionModule(Enum):
Expand All @@ -19,8 +24,8 @@ class MentionModule(Enum):


def get_chat_session_id(
user_id: UUID,
account_id: UUID,
user_id: UUID = None,
account_id: UUID = None,
agent_id: UUID = None,
team_id: UUID = None,
chat_id: UUID = None,
Expand Down

0 comments on commit e6133ff

Please sign in to comment.