77from pydantic import BaseModel , Extra
88from sqlmodel import Session
99
10+ from app .core .db import engine
1011from app .api .deps import get_db , get_current_user_org_project
1112from app .api .routes .threads import send_callback
1213from app .crud .assistants import get_assistant_by_id
1617 get_ancestor_id_from_response ,
1718 get_conversation_by_ancestor_id ,
1819)
19- from app .models import UserProjectOrg , OpenAIConversationCreate
20+ from app .models import UserProjectOrg , OpenAIConversationCreate , OpenAIConversation
2021from app .utils import APIResponse , mask_string
2122from app .core .langfuse .langfuse import LangfuseTracer
2223
@@ -131,7 +132,8 @@ def process_response(
131132 tracer : LangfuseTracer ,
132133 project_id : int ,
133134 organization_id : int ,
134- session : Session ,
135+ ancestor_id : str ,
136+ latest_conversation : OpenAIConversation | None ,
135137):
136138 """Process a response and send callback with results, with Langfuse tracing."""
137139 logger .info (
@@ -151,18 +153,6 @@ def process_response(
151153 )
152154
153155 try :
154- # Get the latest conversation by ancestor ID to use as previous_response_id
155- ancestor_id = request .response_id
156- latest_conversation = None
157- if ancestor_id :
158- latest_conversation = get_conversation_by_ancestor_id (
159- session = session ,
160- ancestor_response_id = ancestor_id ,
161- project_id = project_id ,
162- )
163- if latest_conversation :
164- ancestor_id = latest_conversation .response_id
165-
166156 params = {
167157 "model" : assistant .model ,
168158 "previous_response_id" : ancestor_id ,
@@ -211,35 +201,36 @@ def process_response(
211201 "error" : None ,
212202 },
213203 )
214- # Set ancestor_response_id using CRUD function
215- ancestor_response_id = (
216- latest_conversation .ancestor_response_id
217- if latest_conversation
218- else get_ancestor_id_from_response (
219- session = session ,
220- current_response_id = response .id ,
221- previous_response_id = response .previous_response_id ,
222- project_id = project_id ,
204+
205+ with Session (engine ) as session :
206+ ancestor_response_id = (
207+ latest_conversation .ancestor_response_id
208+ if latest_conversation
209+ else get_ancestor_id_from_response (
210+ session = session ,
211+ current_response_id = response .id ,
212+ previous_response_id = response .previous_response_id ,
213+ project_id = project_id ,
214+ )
223215 )
224- )
225216
226- # Create conversation record in database
227- conversation_data = OpenAIConversationCreate (
228- response_id = response .id ,
229- previous_response_id = response .previous_response_id ,
230- ancestor_response_id = ancestor_response_id ,
231- user_question = request .question ,
232- response = response .output_text ,
233- model = response .model ,
234- assistant_id = request .assistant_id ,
235- )
217+ # Create conversation record in database
218+ conversation_data = OpenAIConversationCreate (
219+ response_id = response .id ,
220+ previous_response_id = response .previous_response_id ,
221+ ancestor_response_id = ancestor_response_id ,
222+ user_question = request .question ,
223+ response = response .output_text ,
224+ model = response .model ,
225+ assistant_id = request .assistant_id ,
226+ )
236227
237- create_conversation (
238- session = session ,
239- conversation = conversation_data ,
240- project_id = project_id ,
241- organization_id = organization_id ,
242- )
228+ create_conversation (
229+ session = session ,
230+ conversation = conversation_data ,
231+ project_id = project_id ,
232+ organization_id = organization_id ,
233+ )
243234
244235 request_dict = request .model_dump ()
245236 callback_response = ResponsesAPIResponse .success_response (
@@ -346,6 +337,17 @@ async def responses(
346337 response_id = request .response_id ,
347338 )
348339
340+ ancestor_id = request .response_id
341+ latest_conversation = None
342+ if ancestor_id :
343+ latest_conversation = get_conversation_by_ancestor_id (
344+ session = _session ,
345+ ancestor_response_id = ancestor_id ,
346+ project_id = project_id ,
347+ )
348+ if latest_conversation :
349+ ancestor_id = latest_conversation .response_id
350+
349351 background_tasks .add_task (
350352 process_response ,
351353 request ,
@@ -354,7 +356,8 @@ async def responses(
354356 tracer ,
355357 project_id ,
356358 organization_id ,
357- _session ,
359+ ancestor_id ,
360+ latest_conversation ,
358361 )
359362
360363 logger .info (
0 commit comments