1212from src .agent .services .memory_service import MemoryService
1313from src .project .services .project_service import ProjectService
1414from src .agent .services .agent_service import RouterAgentService
15+ from src .project .repositories .project_repository import ProjectRepository
1516from src .query_usage .services .query_usage_service import QueryUsageService
1617from src .conversation .repositories .conversation_repository import ConversationRepository
1718
@@ -23,6 +24,7 @@ class ConversationService:
2324
2425 def __init__ (self ):
2526 self .repository = ConversationRepository ()
27+ self .project_repository = ProjectRepository ()
2628 self .memory_service = MemoryService ()
2729 self .project_service = ProjectService ()
2830 self .query_usage_service = QueryUsageService ()
@@ -43,6 +45,12 @@ def create_conversation(
4345 Returns:
4446 Conversation: The created conversation instance.
4547 """
48+ if name .strip () == "" :
49+ raise Exception ("Name is required" )
50+
51+ if not self .project_repository .get_by_id (db_session , project_id , user_id ):
52+ raise Exception ("Project not exists or not owned by user" )
53+
4654 conversation = Conversation (
4755 name = name , project_id = project_id , user_id = user_id )
4856 return self .repository .create (db_session , conversation )
@@ -106,6 +114,12 @@ def update_conversation(
106114 Raises:
107115 Exception: If the conversation is not found.
108116 """
117+ if data ["name" ].strip () == "" :
118+ raise Exception ("Name is required" )
119+
120+ if not self .project_repository .get_by_id (db_session , data ["project_id" ], user_id ):
121+ raise Exception ("Project not exists or not owned by user" )
122+
109123 return self .repository .update (db_session , conversation_id , data , user_id )
110124
111125 def delete_conversation (
@@ -143,6 +157,7 @@ async def send_message(
143157 message : str ,
144158 user_id : UUID ,
145159 conversation_id : UUID ,
160+ project_id : UUID ,
146161 message_type : str ,
147162 stream : bool
148163 ):
@@ -154,6 +169,7 @@ async def send_message(
154169 message (str): The message to be processed
155170 user_id (UUID): ID of the user sending the message
156171 conversation_id (UUID): ID of the conversation the message belongs to
172+ project_id (UUID): ID of the project associated with the conversation
157173 message_type (str): Type of the message
158174 stream (bool): If True, streams response tokens. If False, returns complete response
159175
@@ -168,6 +184,14 @@ async def send_message(
168184 Raises:
169185 Exception: If associated project is not found
170186 """
187+ conversation = self .repository .get_by_id (
188+ db_session , conversation_id , user_id )
189+
190+ if not conversation :
191+ raise Exception ("Conversation not found or is not owned by user" )
192+
193+ if conversation .project_id != project_id :
194+ raise Exception ("Project not found or is not owned by user" )
171195
172196 config = Config .get_config ()
173197
0 commit comments