1
1
import os
2
2
import time
3
- from typing import Any , Dict , Union , cast
3
+ from typing import Any , Dict , Union
4
4
5
5
from azure .cosmos .aio import ContainerProxy , CosmosClient
6
6
from azure .identity .aio import AzureDeveloperCliCredential , ManagedIdentityCredential
12
12
CONFIG_COSMOS_HISTORY_CONTAINER ,
13
13
CONFIG_CREDENTIAL ,
14
14
)
15
- from decorators import authenticated , authenticated_path
15
+ from decorators import authenticated
16
16
from error import error_response
17
17
18
18
chat_history_cosmosdb_bp = Blueprint ("chat_history_cosmos" , __name__ , static_folder = "static" )
@@ -54,7 +54,7 @@ async def get_chat_history(auth_claims: Dict[str, Any]):
54
54
if not current_app .config [CONFIG_CHAT_HISTORY_COSMOS_ENABLED ]:
55
55
return jsonify ({"error" : "Chat history not enabled" }), 405
56
56
57
- container = cast ( ContainerProxy , current_app .config [CONFIG_COSMOS_HISTORY_CONTAINER ])
57
+ container : ContainerProxy = current_app .config [CONFIG_COSMOS_HISTORY_CONTAINER ]
58
58
if not container :
59
59
return jsonify ({"error" : "Chat history not enabled" }), 405
60
60
@@ -103,25 +103,25 @@ async def get_chat_history(auth_claims: Dict[str, Any]):
103
103
return error_response (error , "/chat_history/items" )
104
104
105
105
106
- @chat_history_cosmosdb_bp .get ("/chat_history/items/<path >" )
107
- @authenticated_path
108
- async def get_chat_history_session (path : str , auth_claims : Dict [str , Any ]):
106
+ @chat_history_cosmosdb_bp .get ("/chat_history/items/<item_id >" )
107
+ @authenticated
108
+ async def get_chat_history_session (auth_claims : Dict [str , Any ], item_id : str ):
109
109
if not current_app .config [CONFIG_CHAT_HISTORY_COSMOS_ENABLED ]:
110
110
return jsonify ({"error" : "Chat history not enabled" }), 405
111
111
112
- container = cast ( ContainerProxy , current_app .config [CONFIG_COSMOS_HISTORY_CONTAINER ])
112
+ container : ContainerProxy = current_app .config [CONFIG_COSMOS_HISTORY_CONTAINER ]
113
113
if not container :
114
114
return jsonify ({"error" : "Chat history not enabled" }), 405
115
115
116
- if not path :
117
- return jsonify ({"error" : "Invalid path " }), 400
116
+ if not item_id :
117
+ return jsonify ({"error" : "Invalid item ID specifier " }), 400
118
118
119
119
entra_oid = auth_claims .get ("oid" )
120
120
if not entra_oid :
121
121
return jsonify ({"error" : "User OID not found" }), 401
122
122
123
123
try :
124
- res = await container .read_item (item = path , partition_key = entra_oid )
124
+ res = await container .read_item (item = item_id , partition_key = entra_oid )
125
125
return (
126
126
jsonify (
127
127
{
@@ -135,31 +135,31 @@ async def get_chat_history_session(path: str, auth_claims: Dict[str, Any]):
135
135
200 ,
136
136
)
137
137
except Exception as error :
138
- return error_response (error , f"/chat_history/items/{ path } " )
138
+ return error_response (error , f"/chat_history/items/{ item_id } " )
139
139
140
140
141
- @chat_history_cosmosdb_bp .delete ("/chat_history/items/<path >" )
142
- @authenticated_path
143
- async def delete_chat_history_session (path : str , auth_claims : Dict [str , Any ]):
141
+ @chat_history_cosmosdb_bp .delete ("/chat_history/items/<item_id >" )
142
+ @authenticated
143
+ async def delete_chat_history_session (auth_claims : Dict [str , Any ], item_id : str ):
144
144
if not current_app .config [CONFIG_CHAT_HISTORY_COSMOS_ENABLED ]:
145
145
return jsonify ({"error" : "Chat history not enabled" }), 405
146
146
147
- container = cast ( ContainerProxy , current_app .config [CONFIG_COSMOS_HISTORY_CONTAINER ])
147
+ container : ContainerProxy = current_app .config [CONFIG_COSMOS_HISTORY_CONTAINER ]
148
148
if not container :
149
149
return jsonify ({"error" : "Chat history not enabled" }), 405
150
150
151
- if not path :
151
+ if not item_id :
152
152
return jsonify ({"error" : "Invalid path" }), 400
153
153
154
154
entra_oid = auth_claims .get ("oid" )
155
155
if not entra_oid :
156
156
return jsonify ({"error" : "User OID not found" }), 401
157
157
158
158
try :
159
- await container .delete_item (item = path , partition_key = entra_oid )
159
+ await container .delete_item (item = item_id , partition_key = entra_oid )
160
160
return jsonify ({}), 200
161
161
except Exception as error :
162
- return error_response (error , f"/chat_history/items/{ path } " )
162
+ return error_response (error , f"/chat_history/items/{ item_id } " )
163
163
164
164
165
165
@chat_history_cosmosdb_bp .before_app_serving
@@ -169,9 +169,9 @@ async def setup_clients():
169
169
AZURE_CHAT_HISTORY_DATABASE = os .getenv ("AZURE_CHAT_HISTORY_DATABASE" )
170
170
AZURE_CHAT_HISTORY_CONTAINER = os .getenv ("AZURE_CHAT_HISTORY_CONTAINER" )
171
171
172
- azure_credential = cast (
173
- Union [ AzureDeveloperCliCredential , ManagedIdentityCredential ], current_app . config [ CONFIG_CREDENTIAL ]
174
- )
172
+ azure_credential : Union [ AzureDeveloperCliCredential , ManagedIdentityCredential ] = current_app . config [
173
+ CONFIG_CREDENTIAL
174
+ ]
175
175
176
176
if USE_CHAT_HISTORY_COSMOS :
177
177
current_app .logger .info ("USE_CHAT_HISTORY_COSMOS is true, setting up CosmosDB client" )
0 commit comments