Backup-bdg/OpenHands
0
1from fastapi import Depends, HTTPException, Request, status2 3from openhands.core.logger import openhands_logger as logger4from openhands.server.shared import ConversationStoreImpl, config, conversation_manager5from openhands.server.user_auth import get_user_id6from openhands.storage.conversation.conversation_store import ConversationStore7 8 9async def get_conversation_store(request: Request) -> ConversationStore | None:10 conversation_store: ConversationStore | None = getattr(11 request.state, 'conversation_store', None12 )13 if conversation_store:14 return conversation_store15 user_id = await get_user_id(request)16 conversation_store = await ConversationStoreImpl.get_instance(config, user_id)17 request.state.conversation_store = conversation_store18 return conversation_store19 20 21async def get_conversation(22 conversation_id: str, user_id: str | None = Depends(get_user_id)23):24 """Grabs conversation id set by middleware. Adds the conversation_id to the openapi schema."""25 conversation = await conversation_manager.attach_to_conversation(26 conversation_id, user_id27 )28 if not conversation:29 logger.warn(30 f'get_conversation: conversation {conversation_id} not found, attach_to_conversation returned None',31 extra={'session_id': conversation_id, 'user_id': user_id},32 )33 raise HTTPException(34 status_code=status.HTTP_404_NOT_FOUND,35 detail=f'Conversation {conversation_id} not found',36 )37 try:38 yield conversation39 finally:40 await conversation_manager.detach_from_conversation(conversation)41 