11import logging
2+ from http import HTTPStatus
23from typing import Any , Dict , Optional
34
45from fastapi import APIRouter , Header , HTTPException , Request
1112 OpinionRequest ,
1213 RenameRequest ,
1314)
14- from database .conversation_db import get_message_id_by_index
1515from services .conversation_management_service import (
1616 create_new_conversation ,
1717 delete_conversation_service ,
2020 get_conversation_list_service ,
2121 get_sources_service ,
2222 rename_conversation_service ,
23- update_message_opinion_service ,
23+ update_message_opinion_service , get_message_id_by_index_impl ,
2424)
2525from utils .auth_utils import get_current_user_id , get_current_user_info
2626
@@ -50,7 +50,7 @@ async def create_new_conversation_endpoint(request: ConversationRequest, authori
5050 return ConversationResponse (code = 0 , message = "success" , data = conversation_data )
5151 except Exception as e :
5252 logging .error (f"Failed to create conversation: { str (e )} " )
53- raise HTTPException (status_code = 500 , detail = str (e ))
53+ raise HTTPException (status_code = HTTPStatus . INTERNAL_SERVER_ERROR , detail = str (e ))
5454
5555
5656@router .get ("/list" , response_model = ConversationResponse )
@@ -67,16 +67,12 @@ async def list_conversations_endpoint(authorization: Optional[str] = Header(None
6767 try :
6868 user_id , tenant_id = get_current_user_id (authorization )
6969 if not user_id :
70- raise HTTPException (status_code = 401 , detail = "未授权访问,请先登录" )
71-
70+ raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED , detail = "Unauthorized access, Please login first" )
7271 conversations = get_conversation_list_service (user_id )
7372 return ConversationResponse (code = 0 , message = "success" , data = conversations )
74- except HTTPException as he :
75- # Throw HTTP Exception Directly
76- raise he
7773 except Exception as e :
7874 logging .error (f"Failed to get conversation list: { str (e )} " )
79- raise HTTPException (status_code = 500 , detail = str (e ))
75+ raise HTTPException (status_code = HTTPStatus . INTERNAL_SERVER_ERROR , detail = str (e ))
8076
8177
8278@router .post ("/rename" , response_model = ConversationResponse )
@@ -100,9 +96,7 @@ async def rename_conversation_endpoint(request: RenameRequest, authorization: Op
10096 return ConversationResponse (code = 0 , message = "success" , data = True )
10197 except Exception as e :
10298 logging .error (f"Failed to rename conversation: { str (e )} " )
103- if isinstance (e , HTTPException ):
104- raise e
105- raise HTTPException (status_code = 500 , detail = str (e ))
99+ raise HTTPException (status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = str (e ))
106100
107101
108102@router .delete ("/{conversation_id}" , response_model = ConversationResponse )
@@ -123,9 +117,7 @@ async def delete_conversation_endpoint(conversation_id: int, authorization: Opti
123117 return ConversationResponse (code = 0 , message = "success" , data = True )
124118 except Exception as e :
125119 logging .error (f"Failed to delete conversation: { str (e )} " )
126- if isinstance (e , HTTPException ):
127- raise e
128- raise HTTPException (status_code = 500 , detail = str (e ))
120+ raise HTTPException (status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = str (e ))
129121
130122
131123@router .get ("/{conversation_id}" , response_model = ConversationResponse )
@@ -147,9 +139,7 @@ async def get_conversation_history_endpoint(conversation_id: int, authorization:
147139 return ConversationResponse (code = 0 , message = "success" , data = history_data )
148140 except Exception as e :
149141 logging .error (f"Failed to get conversation history: { str (e )} " )
150- if isinstance (e , HTTPException ):
151- raise e
152- raise HTTPException (status_code = 500 , detail = str (e ))
142+ raise HTTPException (status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = str (e ))
153143
154144
155145@router .post ("/sources" , response_model = Dict [str , Any ])
@@ -175,11 +165,7 @@ async def get_sources_endpoint(request: Dict[str, Any], authorization: Optional[
175165 return get_sources_service (conversation_id , message_id , source_type , user_id )
176166 except Exception as e :
177167 logging .error (f"Failed to get message sources: { str (e )} " )
178- return {
179- "code" : 500 ,
180- "message" : str (e ),
181- "data" : None
182- }
168+ raise HTTPException (status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = str (e ))
183169
184170
185171@router .post ("/generate_title" , response_model = ConversationResponse )
@@ -208,9 +194,7 @@ async def generate_conversation_title_endpoint(
208194 return ConversationResponse (code = 0 , message = "success" , data = title )
209195 except Exception as e :
210196 logging .error (f"Failed to generate conversation title: { str (e )} " )
211- if isinstance (e , HTTPException ):
212- raise e
213- raise HTTPException (status_code = 500 , detail = str (e ))
197+ raise HTTPException (status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = str (e ))
214198
215199
216200@router .post ("/message/update_opinion" , response_model = ConversationResponse )
@@ -230,9 +214,7 @@ async def update_opinion_endpoint(request: OpinionRequest, authorization: Option
230214 return ConversationResponse (code = 0 , message = "success" , data = True )
231215 except Exception as e :
232216 logging .error (f"Failed to update message like/dislike: { str (e )} " )
233- if isinstance (e , HTTPException ):
234- raise e
235- raise HTTPException (status_code = 500 , detail = str (e ))
217+ raise HTTPException (status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = str (e ))
236218
237219
238220@router .post ("/message/id" , response_model = ConversationResponse )
@@ -249,14 +231,8 @@ async def get_message_id_endpoint(request: MessageIdRequest):
249231 ConversationResponse object containing message_id
250232 """
251233 try :
252- message_id = get_message_id_by_index (
253- request .conversation_id , request .message_index )
254- if message_id is None :
255- raise HTTPException (status_code = 404 , detail = "Message not found" )
256-
234+ message_id = await get_message_id_by_index_impl (request .conversation_id , request .message_index )
257235 return ConversationResponse (code = 0 , message = "success" , data = message_id )
258236 except Exception as e :
259237 logging .error (f"Failed to get message ID: { str (e )} " )
260- if isinstance (e , HTTPException ):
261- raise e
262- raise HTTPException (status_code = 500 , detail = str (e ))
238+ raise HTTPException (status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = str (e ))
0 commit comments