Repository navigation
LCORE-4760: Exposed v3/conversations endpoint #3346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
asimurka
wants to merge
1
commit into
lightspeed-core:main
Choose a base branch
from
asimurka:v3-conversations-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,482
−877
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,275 @@ | ||
| """Handler for REST API calls to manage agentic display-history conversations.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from fastapi import APIRouter, Depends, Request | ||
| from fastapi.concurrency import run_in_threadpool | ||
| from opentelemetry import trace | ||
|
|
||
| from authentication import get_auth_dependency | ||
| from authorization.middleware import authorize | ||
| from configuration import configuration | ||
| from conversations.display_history.ids import validate_conversation_id | ||
| from conversations.display_history.persistence.facade.conversation import ( | ||
| delete_conversation, | ||
| get_conversation, | ||
| list_conversations, | ||
| update_conversation_topic, | ||
| ) | ||
| from log import get_logger | ||
| from models.api.requests import ConversationUpdateRequest | ||
| from models.api.responses.constants import UNAUTHORIZED_OPENAPI_EXAMPLES | ||
| from models.api.responses.error import ( | ||
| BadRequestResponse, | ||
| ForbiddenResponse, | ||
| InternalServerErrorResponse, | ||
| NotFoundResponse, | ||
| ServiceUnavailableResponse, | ||
| UnauthorizedResponse, | ||
| ) | ||
| from models.api.responses.successful import ( | ||
| AgenticConversationGetResponse, | ||
| AgenticConversationListResponse, | ||
| ConversationDeleteResponse, | ||
| ConversationUpdateResponse, | ||
| ) | ||
| from models.config import Action | ||
| from utils.endpoints import check_configuration_loaded | ||
| from utils.types import Responses | ||
|
|
||
| logger = get_logger(__name__) | ||
| tracer = trace.get_tracer(__name__) | ||
| router = APIRouter(tags=["conversations_v3"]) | ||
|
|
||
|
|
||
| conversation_get_responses: Responses = { | ||
| 200: AgenticConversationGetResponse.openapi_response(), | ||
| 400: BadRequestResponse.openapi_response(examples=["conversation_id"]), | ||
| 401: UnauthorizedResponse.openapi_response(examples=UNAUTHORIZED_OPENAPI_EXAMPLES), | ||
| 403: ForbiddenResponse.openapi_response(examples=["conversation read", "endpoint"]), | ||
| 404: NotFoundResponse.openapi_response(examples=["conversation"]), | ||
| 500: InternalServerErrorResponse.openapi_response( | ||
| examples=["database operation", "configuration"] | ||
| ), | ||
| 503: ServiceUnavailableResponse.openapi_response(examples=["kubernetes api"]), | ||
| } | ||
|
|
||
| conversation_delete_responses: Responses = { | ||
| 200: ConversationDeleteResponse.openapi_response(), | ||
| 400: BadRequestResponse.openapi_response(examples=["conversation_id"]), | ||
| 401: UnauthorizedResponse.openapi_response(examples=UNAUTHORIZED_OPENAPI_EXAMPLES), | ||
| 403: ForbiddenResponse.openapi_response( | ||
| examples=["conversation delete", "endpoint"] | ||
| ), | ||
| 500: InternalServerErrorResponse.openapi_response( | ||
| examples=["database operation", "configuration"] | ||
| ), | ||
| 503: ServiceUnavailableResponse.openapi_response(examples=["kubernetes api"]), | ||
| } | ||
|
|
||
| conversations_list_responses: Responses = { | ||
| 200: AgenticConversationListResponse.openapi_response(), | ||
| 401: UnauthorizedResponse.openapi_response(examples=UNAUTHORIZED_OPENAPI_EXAMPLES), | ||
| 403: ForbiddenResponse.openapi_response(examples=["endpoint"]), | ||
| 500: InternalServerErrorResponse.openapi_response( | ||
| examples=["database operation", "configuration"] | ||
| ), | ||
| 503: ServiceUnavailableResponse.openapi_response(examples=["kubernetes api"]), | ||
| } | ||
|
|
||
| conversation_update_responses: Responses = { | ||
| 200: ConversationUpdateResponse.openapi_response(), | ||
| 400: BadRequestResponse.openapi_response(examples=["conversation_id"]), | ||
| 401: UnauthorizedResponse.openapi_response(examples=UNAUTHORIZED_OPENAPI_EXAMPLES), | ||
| 403: ForbiddenResponse.openapi_response(examples=["endpoint"]), | ||
| 404: NotFoundResponse.openapi_response(examples=["conversation"]), | ||
| 500: InternalServerErrorResponse.openapi_response( | ||
| examples=["database operation", "configuration"] | ||
| ), | ||
| 503: ServiceUnavailableResponse.openapi_response(examples=["kubernetes api"]), | ||
| } | ||
|
|
||
|
|
||
| @router.get("/conversations", responses=conversations_list_responses) | ||
| @authorize(Action.LIST_CONVERSATIONS) | ||
| async def get_conversations_list_endpoint_handler( | ||
| request: Request, | ||
| auth: Any = Depends(get_auth_dependency()), | ||
| ) -> AgenticConversationListResponse: | ||
| """ | ||
| Handle request to retrieve all conversations for the authenticated user. | ||
|
|
||
| ### Parameters: | ||
| - request: The incoming HTTP request (used by middleware). | ||
| - auth: Auth context tuple resolved from the authentication dependency. | ||
|
|
||
| ### Returns: | ||
| - AgenticConversationListResponse: Metadata for conversations visible to the caller. | ||
|
|
||
| ### Raises: | ||
| - HTTPException: | ||
| - 401: Unauthorized - Missing or invalid credentials | ||
| - 403: Forbidden - Insufficient permissions | ||
| - 500: Internal Server Error - Configuration not loaded or database failure | ||
| - 503: Service Unavailable - Kubernetes API unavailable when required by auth | ||
| """ | ||
| with tracer.start_as_current_span("conversations_v3.list") as span: | ||
| check_configuration_loaded(configuration) | ||
| user_id, _, _, _ = auth | ||
| others_allowed = ( | ||
| Action.LIST_OTHERS_CONVERSATIONS in request.state.authorized_actions | ||
| ) | ||
| logger.info("Retrieving conversations for user %s", user_id) | ||
| conversations = await run_in_threadpool( | ||
| list_conversations, user_id, others_allowed | ||
| ) | ||
| span.set_attribute("conversations.count", len(conversations)) | ||
| return AgenticConversationListResponse(conversations=conversations) | ||
|
|
||
|
|
||
| @router.get( | ||
| "/conversations/{conversation_id}", | ||
| responses=conversation_get_responses, | ||
| ) | ||
| @authorize(Action.GET_CONVERSATION) | ||
| async def get_conversation_endpoint_handler( | ||
| request: Request, | ||
| conversation_id: str, | ||
| auth: Any = Depends(get_auth_dependency()), | ||
| ) -> AgenticConversationGetResponse: | ||
| """ | ||
| Handle request to retrieve a conversation identified by its ID. | ||
|
|
||
| Loads agent display history from persistence, including ordered agent turns. | ||
|
|
||
| ### Parameters: | ||
| - request: The incoming HTTP request (used by middleware). | ||
| - conversation_id: Unique identifier of the conversation to retrieve. | ||
| - auth: Auth context tuple resolved from the authentication dependency. | ||
|
|
||
| ### Returns: | ||
| - AgenticConversationGetResponse: Conversation metadata and agent turn history. | ||
|
|
||
| ### Raises: | ||
| - HTTPException: | ||
| - 400: Bad Request - Invalid conversation ID format | ||
| - 401: Unauthorized - Missing or invalid credentials | ||
| - 403: Forbidden - Insufficient permissions to read this conversation | ||
| - 404: Not Found - Conversation does not exist | ||
| - 500: Internal Server Error - Configuration not loaded or database failure | ||
| - 503: Service Unavailable - Kubernetes API unavailable when required by auth | ||
| """ | ||
| with tracer.start_as_current_span("conversations_v3.get") as span: | ||
| check_configuration_loaded(configuration) | ||
| validate_conversation_id(conversation_id) | ||
| user_id, _, _, _ = auth | ||
| others_allowed = ( | ||
| Action.READ_OTHERS_CONVERSATIONS in request.state.authorized_actions | ||
| ) | ||
| logger.info("Retrieving conversation %s for user %s", conversation_id, user_id) | ||
| conversation = await run_in_threadpool( | ||
| get_conversation, conversation_id, user_id, others_allowed | ||
| ) | ||
| span.set_attribute("conversations.found", True) | ||
| span.set_attribute("conversations.turns.count", len(conversation.chat_history)) | ||
| return AgenticConversationGetResponse.model_validate(conversation) | ||
|
|
||
|
|
||
| @router.delete( | ||
| "/conversations/{conversation_id}", responses=conversation_delete_responses | ||
| ) | ||
| @authorize(Action.DELETE_CONVERSATION) | ||
| async def delete_conversation_endpoint_handler( | ||
| request: Request, | ||
| conversation_id: str, | ||
| auth: Any = Depends(get_auth_dependency()), | ||
| ) -> ConversationDeleteResponse: | ||
| """ | ||
| Handle request to delete a conversation by ID. | ||
|
|
||
| ### Parameters: | ||
| - request: The incoming HTTP request (used by middleware). | ||
| - conversation_id: Unique identifier of the conversation to delete. | ||
| - auth: Auth context tuple resolved from the authentication dependency. | ||
|
|
||
| ### Returns: | ||
| - ConversationDeleteResponse: Whether the conversation was deleted. | ||
|
|
||
| ### Raises: | ||
| - HTTPException: | ||
| - 400: Bad Request - Invalid conversation ID format | ||
| - 401: Unauthorized - Missing or invalid credentials | ||
| - 403: Forbidden - Insufficient permissions to delete this conversation | ||
| - 500: Internal Server Error - Configuration not loaded or database failure | ||
| - 503: Service Unavailable - Kubernetes API unavailable when required by auth | ||
| """ | ||
| with tracer.start_as_current_span("conversations_v3.delete") as span: | ||
| check_configuration_loaded(configuration) | ||
| validate_conversation_id(conversation_id) | ||
| user_id, _, _, _ = auth | ||
| others_allowed = ( | ||
| Action.DELETE_OTHERS_CONVERSATIONS in request.state.authorized_actions | ||
| ) | ||
| logger.info("Deleting conversation %s for user %s", conversation_id, user_id) | ||
| deleted = await run_in_threadpool( | ||
| delete_conversation, conversation_id, user_id, others_allowed | ||
| ) | ||
| span.set_attribute("conversations.deleted", deleted) | ||
| return ConversationDeleteResponse( | ||
| deleted=deleted, conversation_id=conversation_id | ||
| ) | ||
|
|
||
|
|
||
| @router.put("/conversations/{conversation_id}", responses=conversation_update_responses) | ||
| @authorize(Action.UPDATE_CONVERSATION) | ||
| async def update_conversation_endpoint_handler( | ||
| request: Request, | ||
| conversation_id: str, | ||
| update_request: ConversationUpdateRequest, | ||
| auth: Any = Depends(get_auth_dependency()), | ||
| ) -> ConversationUpdateResponse: | ||
| """ | ||
| Handle request to update a conversation topic summary by ID. | ||
|
|
||
| ### Parameters: | ||
| - request: The incoming HTTP request (used by middleware). | ||
| - conversation_id: Unique identifier of the conversation to update. | ||
| - update_request: Request body containing the new topic summary. | ||
| - auth: Auth context tuple resolved from the authentication dependency. | ||
|
|
||
| ### Returns: | ||
| - ConversationUpdateResponse: Result of the update operation. | ||
|
|
||
| ### Raises: | ||
| - HTTPException: | ||
| - 400: Bad Request - Invalid conversation ID format | ||
| - 401: Unauthorized - Missing or invalid credentials | ||
| - 403: Forbidden - Insufficient permissions to update this conversation | ||
| - 404: Not Found - Conversation does not exist | ||
| - 500: Internal Server Error - Configuration not loaded or database failure | ||
| - 503: Service Unavailable - Kubernetes API unavailable when required by auth | ||
| """ | ||
| with tracer.start_as_current_span("conversations_v3.update") as span: | ||
| check_configuration_loaded(configuration) | ||
| validate_conversation_id(conversation_id) | ||
| user_id, _, _, _ = auth | ||
| others_allowed = ( | ||
| Action.QUERY_OTHERS_CONVERSATIONS in request.state.authorized_actions | ||
|
asimurka marked this conversation as resolved.
Resolved
|
||
| ) | ||
| logger.info( | ||
| "Updating topic summary for conversation %s for user %s", | ||
| conversation_id, | ||
| user_id, | ||
| ) | ||
| result = await run_in_threadpool( | ||
| update_conversation_topic, | ||
| conversation_id, | ||
| user_id, | ||
| others_allowed, | ||
| update_request.topic_summary, | ||
| ) | ||
| span.set_attribute("conversations.updated", result) | ||
| return ConversationUpdateResponse( | ||
| conversation_id=conversation_id, | ||
| success=result, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to a computed field