44Provides chat functionality with streaming support and conversation history.
55Uses API key authentication. Routes through Langflow (chat_service.langflow_chat).
66"""
7+
78import json
89from typing import Any
910
@@ -38,13 +39,15 @@ def _extract_sources(item: dict) -> list[dict]:
3839 sources = []
3940 for result in item .get ("results" , []):
4041 if isinstance (result , dict ) and "text" in result :
41- sources .append ({
42- "filename" : result .get ("filename" , "" ),
43- "text" : result .get ("text" , "" ),
44- "score" : result .get ("score" , 0 ),
45- "page" : result .get ("page" ),
46- "mimetype" : result .get ("mimetype" ),
47- })
42+ sources .append (
43+ {
44+ "filename" : result .get ("filename" , "" ),
45+ "text" : result .get ("text" , "" ),
46+ "score" : result .get ("score" , 0 ),
47+ "page" : result .get ("page" ),
48+ "mimetype" : result .get ("mimetype" ),
49+ }
50+ )
4851 return sources
4952
5053
@@ -121,6 +124,7 @@ async def chat_create_endpoint(
121124 jwt_token = user .jwt_token
122125 if body .chat_id :
123126 from api .chat import _assert_owns
127+
124128 await _assert_owns (body .chat_id , storage_user_id )
125129
126130 if body .filters :
@@ -146,7 +150,11 @@ async def chat_create_endpoint(
146150 return StreamingResponse (
147151 _transform_stream_to_sse (raw_stream , chat_id_container ),
148152 media_type = "text/event-stream" ,
149- headers = {"Cache-Control" : "no-cache" , "Connection" : "keep-alive" , "X-Accel-Buffering" : "no" },
153+ headers = {
154+ "Cache-Control" : "no-cache" ,
155+ "Connection" : "keep-alive" ,
156+ "X-Accel-Buffering" : "no" ,
157+ },
150158 )
151159 else :
152160 result = await chat_service .langflow_chat (
@@ -161,11 +169,13 @@ async def chat_create_endpoint(
161169 owner_email = user .email ,
162170 storage_user_id = storage_user_id ,
163171 )
164- return JSONResponse ({
165- "response" : result .get ("response" , "" ),
166- "chat_id" : result .get ("response_id" ),
167- "sources" : result .get ("sources" , []),
168- })
172+ return JSONResponse (
173+ {
174+ "response" : result .get ("response" , "" ),
175+ "chat_id" : result .get ("response_id" ),
176+ "sources" : result .get ("sources" , []),
177+ }
178+ )
169179
170180
171181async def chat_list_endpoint (
@@ -218,20 +228,28 @@ async def chat_get_endpoint(
218228 "timestamp" : msg .get ("timestamp" ),
219229 }
220230 # Include token usage if available (from Responses API)
221- usage = msg .get ("response_data" , {}).get ("usage" ) if isinstance (msg .get ("response_data" ), dict ) else None
231+ usage = (
232+ msg .get ("response_data" , {}).get ("usage" )
233+ if isinstance (msg .get ("response_data" ), dict )
234+ else None
235+ )
222236 if usage :
223237 message_data ["usage" ] = usage
224238 messages .append (message_data )
225239
226- return JSONResponse ({
227- "chat_id" : conversation .get ("response_id" ),
228- "title" : conversation .get ("title" , "" ),
229- "created_at" : conversation .get ("created_at" ),
230- "last_activity" : conversation .get ("last_activity" ),
231- "messages" : messages ,
232- })
240+ return JSONResponse (
241+ {
242+ "chat_id" : conversation .get ("response_id" ),
243+ "title" : conversation .get ("title" , "" ),
244+ "created_at" : conversation .get ("created_at" ),
245+ "last_activity" : conversation .get ("last_activity" ),
246+ "messages" : messages ,
247+ }
248+ )
233249 except Exception as e :
234- logger .error ("Failed to get conversation" , error = str (e ), user_id = user .user_id , chat_id = chat_id )
250+ logger .error (
251+ "Failed to get conversation" , error = str (e ), user_id = user .user_id , chat_id = chat_id
252+ )
235253 return JSONResponse ({"error" : f"Failed to get conversation: { str (e )} " }, status_code = 500 )
236254
237255
@@ -243,6 +261,7 @@ async def chat_delete_endpoint(
243261 """Delete a conversation. DELETE /v1/chat/{chat_id}"""
244262 try :
245263 from api .chat import _assert_owns
264+
246265 storage_user_id = _openrag_user_id (user )
247266 await _assert_owns (chat_id , storage_user_id )
248267 result = await chat_service .delete_session (storage_user_id , chat_id )
@@ -258,5 +277,7 @@ async def chat_delete_endpoint(
258277 except HTTPException :
259278 raise
260279 except Exception as e :
261- logger .error ("Failed to delete conversation" , error = str (e ), user_id = user .user_id , chat_id = chat_id )
280+ logger .error (
281+ "Failed to delete conversation" , error = str (e ), user_id = user .user_id , chat_id = chat_id
282+ )
262283 return JSONResponse ({"error" : f"Failed to delete conversation: { str (e )} " }, status_code = 500 )
0 commit comments