diff --git a/api/controllers/console/explore/trial.py b/api/controllers/console/explore/trial.py index f743f62725..eaa5c74739 100644 --- a/api/controllers/console/explore/trial.py +++ b/api/controllers/console/explore/trial.py @@ -87,10 +87,15 @@ class TrialChatApi(TrialAppResource): try: if not isinstance(current_user, Account): raise ValueError("current_user must be an Account instance") + + # Get IDs before they might be detached from session + app_id = app_model.id + user_id = current_user.id + response = AppGenerateService.generate( app_model=app_model, user=current_user, args=args, invoke_from=InvokeFrom.EXPLORE, streaming=True ) - RecommendedAppService.add_trial_app_record(app_model.id, current_user.id) + RecommendedAppService.add_trial_app_record(app_id, user_id) return helper.compact_generate_response(response) except services.errors.conversation.ConversationNotExistsError: raise NotFound("Conversation Not Exists.") @@ -163,8 +168,13 @@ class TrialChatAudioApi(TrialAppResource): try: if not isinstance(current_user, Account): raise ValueError("current_user must be an Account instance") + + # Get IDs before they might be detached from session + app_id = app_model.id + user_id = current_user.id + response = AudioService.transcript_asr(app_model=app_model, file=file, end_user=None) - RecommendedAppService.add_trial_app_record(app_model.id, current_user.id) + RecommendedAppService.add_trial_app_record(app_id, user_id) return response except services.errors.app_model_config.AppModelConfigBrokenError: logger.exception("App model config broken.") @@ -209,8 +219,13 @@ class TrialChatTextApi(TrialAppResource): voice = args.get("voice", None) if not isinstance(current_user, Account): raise ValueError("current_user must be an Account instance") + + # Get IDs before they might be detached from session + app_id = app_model.id + user_id = current_user.id + response = AudioService.transcript_tts(app_model=app_model, text=text, voice=voice, message_id=message_id) - RecommendedAppService.add_trial_app_record(app_model.id, current_user.id) + RecommendedAppService.add_trial_app_record(app_id, user_id) return response except services.errors.app_model_config.AppModelConfigBrokenError: logger.exception("App model config broken.") @@ -259,11 +274,16 @@ class TrialCompletionApi(TrialAppResource): try: if not isinstance(current_user, Account): raise ValueError("current_user must be an Account instance") + + # Get IDs before they might be detached from session + app_id = app_model.id + user_id = current_user.id + response = AppGenerateService.generate( app_model=app_model, user=current_user, args=args, invoke_from=InvokeFrom.EXPLORE, streaming=streaming ) - RecommendedAppService.add_trial_app_record(app_model.id, current_user.id) + RecommendedAppService.add_trial_app_record(app_id, user_id) return helper.compact_generate_response(response) except services.errors.conversation.ConversationNotExistsError: raise NotFound("Conversation Not Exists.") diff --git a/api/services/recommended_app_service.py b/api/services/recommended_app_service.py index 9d3a663553..be6e94a526 100644 --- a/api/services/recommended_app_service.py +++ b/api/services/recommended_app_service.py @@ -60,14 +60,13 @@ class RecommendedAppService: :param app_id: app id :return: """ - with db.session as session: - account_trial_app_record = session.query(AccountTrialAppRecord).where( - AccountTrialAppRecord.app_id == app_id, - AccountTrialAppRecord.account_id == account_id - ).first() - if account_trial_app_record: - account_trial_app_record.count += 1 - session.commit() - else: - session.add(AccountTrialAppRecord(app_id=app_id, count=1, account_id=account_id)) - session.commit() + account_trial_app_record = db.session.query(AccountTrialAppRecord).where( + AccountTrialAppRecord.app_id == app_id, + AccountTrialAppRecord.account_id == account_id + ).first() + if account_trial_app_record: + account_trial_app_record.count += 1 + db.session.commit() + else: + db.session.add(AccountTrialAppRecord(app_id=app_id, count=1, account_id=account_id)) + db.session.commit()