55
66from llama_stack_client import APIConnectionError , NotFoundError
77
8- from fastapi import APIRouter , HTTPException , status , Depends
8+ from fastapi import APIRouter , HTTPException , Request , status , Depends
99
1010from client import AsyncLlamaStackClientHolder
1111from configuration import configuration
12+ from app .database import get_session
13+ from auth import get_auth_dependency
14+ from authorization .middleware import authorize
15+ from models .config import Action
16+ from models .database .conversations import UserConversation
1217from models .responses import (
1318 ConversationResponse ,
1419 ConversationDeleteResponse ,
1520 ConversationsListResponse ,
1621 ConversationDetails ,
1722)
18- from models .database .conversations import UserConversation
19- from auth import get_auth_dependency
20- from app .database import get_session
2123from utils .endpoints import check_configuration_loaded , validate_conversation_ownership
2224from utils .suid import check_suid
2325
@@ -146,7 +148,9 @@ def simplify_session_data(session_data: dict) -> list[dict[str, Any]]:
146148
147149
148150@router .get ("/conversations" , responses = conversations_list_responses )
149- def get_conversations_list_endpoint_handler (
151+ @authorize (Action .LIST_CONVERSATIONS )
152+ async def get_conversations_list_endpoint_handler (
153+ request : Request ,
150154 auth : Any = Depends (auth_dependency ),
151155) -> ConversationsListResponse :
152156 """Handle request to retrieve all conversations for the authenticated user."""
@@ -158,11 +162,16 @@ def get_conversations_list_endpoint_handler(
158162
159163 with get_session () as session :
160164 try :
161- # Get all conversations for this user
162- user_conversations = (
163- session .query (UserConversation ).filter_by (user_id = user_id ).all ()
165+ query = session .query (UserConversation )
166+
167+ filtered_query = (
168+ query
169+ if Action .LIST_OTHERS_CONVERSATIONS in request .state .authorized_actions
170+ else query .filter_by (user_id = user_id )
164171 )
165172
173+ user_conversations = filtered_query .all ()
174+
166175 # Return conversation summaries with metadata
167176 conversations = [
168177 ConversationDetails (
@@ -200,7 +209,9 @@ def get_conversations_list_endpoint_handler(
200209
201210
202211@router .get ("/conversations/{conversation_id}" , responses = conversation_responses )
212+ @authorize (Action .GET_CONVERSATION )
203213async def get_conversation_endpoint_handler (
214+ request : Request ,
204215 conversation_id : str ,
205216 auth : Any = Depends (auth_dependency ),
206217) -> ConversationResponse :
@@ -239,6 +250,9 @@ async def get_conversation_endpoint_handler(
239250 validate_conversation_ownership (
240251 user_id = user_id ,
241252 conversation_id = conversation_id ,
253+ others_allowed = (
254+ Action .READ_OTHERS_CONVERSATIONS in request .state .authorized_actions
255+ ),
242256 )
243257
244258 agent_id = conversation_id
@@ -309,7 +323,9 @@ async def get_conversation_endpoint_handler(
309323@router .delete (
310324 "/conversations/{conversation_id}" , responses = conversation_delete_responses
311325)
326+ @authorize (Action .DELETE_CONVERSATION )
312327async def delete_conversation_endpoint_handler (
328+ request : Request ,
313329 conversation_id : str ,
314330 auth : Any = Depends (auth_dependency ),
315331) -> ConversationDeleteResponse :
@@ -342,6 +358,9 @@ async def delete_conversation_endpoint_handler(
342358 validate_conversation_ownership (
343359 user_id = user_id ,
344360 conversation_id = conversation_id ,
361+ others_allowed = (
362+ Action .DELETE_OTHERS_CONVERSATIONS in request .state .authorized_actions
363+ ),
345364 )
346365
347366 agent_id = conversation_id
0 commit comments