3
3
from collections .abc import Generator
4
4
from typing import Optional , Union , cast
5
5
6
+ from sqlalchemy import select
7
+ from sqlalchemy .orm import Session
8
+
6
9
from core .app .app_config .entities import EasyUIBasedAppConfig , EasyUIBasedAppModelConfigFrom
7
10
from core .app .apps .base_app_generator import BaseAppGenerator
8
11
from core .app .apps .base_app_queue_manager import AppQueueManager
@@ -83,11 +86,10 @@ def _handle_response(
83
86
84
87
def _get_app_model_config (self , app_model : App , conversation : Optional [Conversation ] = None ) -> AppModelConfig :
85
88
if conversation :
86
- app_model_config = (
87
- db .session .query (AppModelConfig )
88
- .where (AppModelConfig .id == conversation .app_model_config_id , AppModelConfig .app_id == app_model .id )
89
- .first ()
89
+ stmt = select (AppModelConfig ).where (
90
+ AppModelConfig .id == conversation .app_model_config_id , AppModelConfig .app_id == app_model .id
90
91
)
92
+ app_model_config = db .session .scalar (stmt )
91
93
92
94
if not app_model_config :
93
95
raise AppModelConfigBrokenError ()
@@ -253,7 +255,8 @@ def _get_conversation(self, conversation_id: str) -> Conversation:
253
255
:param conversation_id: conversation id
254
256
:return: conversation
255
257
"""
256
- conversation = db .session .query (Conversation ).where (Conversation .id == conversation_id ).first ()
258
+ with Session (db .engine , expire_on_commit = False ) as session :
259
+ conversation = session .scalar (select (Conversation ).where (Conversation .id == conversation_id ))
257
260
258
261
if not conversation :
259
262
raise ConversationNotExistsError ("Conversation not exists" )
@@ -266,7 +269,8 @@ def _get_message(self, message_id: str) -> Message:
266
269
:param message_id: message id
267
270
:return: message
268
271
"""
269
- message = db .session .query (Message ).where (Message .id == message_id ).first ()
272
+ with Session (db .engine , expire_on_commit = False ) as session :
273
+ message = session .scalar (select (Message ).where (Message .id == message_id ))
270
274
271
275
if message is None :
272
276
raise MessageNotExistsError ("Message not exists" )
0 commit comments