|
16 | 16 | from configuration import configuration |
17 | 17 | from models.config import Action |
18 | 18 | from models.database.conversations import ( |
19 | | - UserTurn, |
20 | 19 | UserConversation, |
21 | 20 | ) |
22 | 21 | from models.requests import ConversationUpdateRequest |
|
38 | 37 | check_configuration_loaded, |
39 | 38 | delete_conversation, |
40 | 39 | retrieve_conversation, |
| 40 | + retrieve_conversation_turns, |
41 | 41 | validate_and_retrieve_conversation, |
42 | 42 | ) |
43 | 43 | from utils.suid import ( |
44 | 44 | check_suid, |
45 | 45 | normalize_conversation_id, |
46 | 46 | to_llama_stack_conversation_id, |
47 | 47 | ) |
48 | | -from utils.conversations import build_conversation_turns_from_items |
| 48 | +from utils.conversations import ( |
| 49 | + build_conversation_turns_from_items, |
| 50 | + get_all_conversation_items, |
| 51 | +) |
49 | 52 | from log import get_logger |
50 | 53 |
|
51 | 54 | logger = get_logger(__name__) |
@@ -236,46 +239,23 @@ async def get_conversation_endpoint_handler( # pylint: disable=too-many-locals, |
236 | 239 | llama_stack_conv_id, |
237 | 240 | ) |
238 | 241 |
|
239 | | - # Use Conversations API to retrieve conversation items |
240 | | - conversation_items_response = await client.conversations.items.list( |
241 | | - conversation_id=llama_stack_conv_id, |
242 | | - after=None, |
243 | | - include=None, |
244 | | - limit=None, |
245 | | - order="asc", # oldest first |
246 | | - ) |
| 242 | + # Retrieve turns metadata from database (can be empty for legacy conversations) |
| 243 | + db_turns = retrieve_conversation_turns(normalized_conv_id) |
247 | 244 |
|
248 | | - if not conversation_items_response.data: |
| 245 | + # Use Conversations API to retrieve conversation items |
| 246 | + items = await get_all_conversation_items(client, llama_stack_conv_id) |
| 247 | + if not items: |
249 | 248 | logger.error("No items found for conversation %s", conversation_id) |
250 | 249 | response = NotFoundResponse( |
251 | 250 | resource="conversation", resource_id=normalized_conv_id |
252 | 251 | ).model_dump() |
253 | 252 | raise HTTPException(**response) |
254 | 253 |
|
255 | | - items = conversation_items_response.data |
256 | | - |
257 | 254 | logger.info( |
258 | 255 | "Successfully retrieved %d items for conversation %s", |
259 | 256 | len(items), |
260 | 257 | conversation_id, |
261 | 258 | ) |
262 | | - # Retrieve turns metadata from database |
263 | | - db_turns: list[UserTurn] = [] |
264 | | - try: |
265 | | - with get_session() as session: |
266 | | - db_turns = ( |
267 | | - session.query(UserTurn) |
268 | | - .filter_by(conversation_id=normalized_conv_id) |
269 | | - .order_by(UserTurn.turn_number) |
270 | | - .all() |
271 | | - ) |
272 | | - except SQLAlchemyError as e: |
273 | | - logger.error( |
274 | | - "Database error occurred while retrieving conversation turns for %s.", |
275 | | - normalized_conv_id, |
276 | | - ) |
277 | | - response = InternalServerErrorResponse.database_error() |
278 | | - raise HTTPException(**response.model_dump()) from e |
279 | 259 |
|
280 | 260 | # Build conversation turns from items and populate turns metadata |
281 | 261 | # Use conversation.created_at for legacy conversations without turn metadata |
|
0 commit comments