Skip to content

Commit b1ce7cc

Browse files
authored
Merge branch 'langgenius:main' into feat/schedule-trigger
2 parents 5ccca1e + be3af1e commit b1ce7cc

File tree

92 files changed

+1352
-372
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1352
-372
lines changed

api/controllers/console/datasets/datasets_document.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,6 @@ def post(self):
354354
parser.add_argument("embedding_model_provider", type=str, required=False, nullable=True, location="json")
355355
args = parser.parse_args()
356356

357-
# The role of the current user in the ta table must be admin, owner, or editor, or dataset_operator
358-
if not current_user.is_dataset_editor:
359-
raise Forbidden()
360357
knowledge_config = KnowledgeConfig(**args)
361358
if knowledge_config.indexing_technique == "high_quality":
362359
if knowledge_config.embedding_model is None or knowledge_config.embedding_model_provider is None:

api/core/agent/base_agent_runner.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ def save_agent_thought(
334334
"""
335335
Save agent thought
336336
"""
337-
agent_thought = db.session.query(MessageAgentThought).where(MessageAgentThought.id == agent_thought_id).first()
337+
stmt = select(MessageAgentThought).where(MessageAgentThought.id == agent_thought_id)
338+
agent_thought = db.session.scalar(stmt)
338339
if not agent_thought:
339340
raise ValueError("agent thought not found")
340341

@@ -492,7 +493,8 @@ def organize_agent_history(self, prompt_messages: list[PromptMessage]) -> list[P
492493
return result
493494

494495
def organize_agent_user_prompt(self, message: Message) -> UserPromptMessage:
495-
files = db.session.query(MessageFile).where(MessageFile.message_id == message.id).all()
496+
stmt = select(MessageFile).where(MessageFile.message_id == message.id)
497+
files = db.session.scalars(stmt).all()
496498
if not files:
497499
return UserPromptMessage(content=message.query)
498500
if message.app_model_config:

api/core/app/apps/advanced_chat/app_generator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,12 @@ def _generate(
450450

451451
worker_thread.start()
452452

453+
# release database connection, because the following new thread operations may take a long time
454+
db.session.refresh(workflow)
455+
db.session.refresh(message)
456+
db.session.refresh(user)
457+
db.session.close()
458+
453459
# return response or stream generator
454460
response = self._handle_advanced_chat_response(
455461
application_generate_entity=application_generate_entity,

api/core/app/apps/advanced_chat/app_runner.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ def run(self) -> None:
7272
app_config = self.application_generate_entity.app_config
7373
app_config = cast(AdvancedChatAppConfig, app_config)
7474

75-
app_record = db.session.query(App).where(App.id == app_config.app_id).first()
75+
with Session(db.engine, expire_on_commit=False) as session:
76+
app_record = session.scalar(select(App).where(App.id == app_config.app_id))
77+
7678
if not app_record:
7779
raise ValueError("App not found")
7880

api/core/app/apps/agent_chat/app_runner.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
22
from typing import cast
33

4+
from sqlalchemy import select
5+
46
from core.agent.cot_chat_agent_runner import CotChatAgentRunner
57
from core.agent.cot_completion_agent_runner import CotCompletionAgentRunner
68
from core.agent.entities import AgentEntity
@@ -44,8 +46,8 @@ def run(
4446
"""
4547
app_config = application_generate_entity.app_config
4648
app_config = cast(AgentChatAppConfig, app_config)
47-
48-
app_record = db.session.query(App).where(App.id == app_config.app_id).first()
49+
app_stmt = select(App).where(App.id == app_config.app_id)
50+
app_record = db.session.scalar(app_stmt)
4951
if not app_record:
5052
raise ValueError("App not found")
5153

@@ -182,11 +184,12 @@ def run(
182184

183185
if {ModelFeature.MULTI_TOOL_CALL, ModelFeature.TOOL_CALL}.intersection(model_schema.features or []):
184186
agent_entity.strategy = AgentEntity.Strategy.FUNCTION_CALLING
185-
186-
conversation_result = db.session.query(Conversation).where(Conversation.id == conversation.id).first()
187+
conversation_stmt = select(Conversation).where(Conversation.id == conversation.id)
188+
conversation_result = db.session.scalar(conversation_stmt)
187189
if conversation_result is None:
188190
raise ValueError("Conversation not found")
189-
message_result = db.session.query(Message).where(Message.id == message.id).first()
191+
msg_stmt = select(Message).where(Message.id == message.id)
192+
message_result = db.session.scalar(msg_stmt)
190193
if message_result is None:
191194
raise ValueError("Message not found")
192195
db.session.close()

api/core/app/apps/chat/app_runner.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
22
from typing import cast
33

4+
from sqlalchemy import select
5+
46
from core.app.apps.base_app_queue_manager import AppQueueManager, PublishFrom
57
from core.app.apps.base_app_runner import AppRunner
68
from core.app.apps.chat.app_config_manager import ChatAppConfig
@@ -42,8 +44,8 @@ def run(
4244
"""
4345
app_config = application_generate_entity.app_config
4446
app_config = cast(ChatAppConfig, app_config)
45-
46-
app_record = db.session.query(App).where(App.id == app_config.app_id).first()
47+
stmt = select(App).where(App.id == app_config.app_id)
48+
app_record = db.session.scalar(stmt)
4749
if not app_record:
4850
raise ValueError("App not found")
4951

api/core/app/apps/completion/app_generator.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from flask import Flask, copy_current_request_context, current_app
88
from pydantic import ValidationError
9+
from sqlalchemy import select
910

1011
from configs import dify_config
1112
from core.app.app_config.easy_ui_based_app.model_config.converter import ModelConfigConverter
@@ -248,17 +249,14 @@ def generate_more_like_this(
248249
:param invoke_from: invoke from source
249250
:param stream: is stream
250251
"""
251-
message = (
252-
db.session.query(Message)
253-
.where(
254-
Message.id == message_id,
255-
Message.app_id == app_model.id,
256-
Message.from_source == ("api" if isinstance(user, EndUser) else "console"),
257-
Message.from_end_user_id == (user.id if isinstance(user, EndUser) else None),
258-
Message.from_account_id == (user.id if isinstance(user, Account) else None),
259-
)
260-
.first()
252+
stmt = select(Message).where(
253+
Message.id == message_id,
254+
Message.app_id == app_model.id,
255+
Message.from_source == ("api" if isinstance(user, EndUser) else "console"),
256+
Message.from_end_user_id == (user.id if isinstance(user, EndUser) else None),
257+
Message.from_account_id == (user.id if isinstance(user, Account) else None),
261258
)
259+
message = db.session.scalar(stmt)
262260

263261
if not message:
264262
raise MessageNotExistsError()

api/core/app/apps/completion/app_runner.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
22
from typing import cast
33

4+
from sqlalchemy import select
5+
46
from core.app.apps.base_app_queue_manager import AppQueueManager
57
from core.app.apps.base_app_runner import AppRunner
68
from core.app.apps.completion.app_config_manager import CompletionAppConfig
@@ -35,8 +37,8 @@ def run(
3537
"""
3638
app_config = application_generate_entity.app_config
3739
app_config = cast(CompletionAppConfig, app_config)
38-
39-
app_record = db.session.query(App).where(App.id == app_config.app_id).first()
40+
stmt = select(App).where(App.id == app_config.app_id)
41+
app_record = db.session.scalar(stmt)
4042
if not app_record:
4143
raise ValueError("App not found")
4244

api/core/app/apps/message_based_app_generator.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
from collections.abc import Generator
44
from typing import Optional, Union, cast
55

6+
from sqlalchemy import select
7+
from sqlalchemy.orm import Session
8+
69
from core.app.app_config.entities import EasyUIBasedAppConfig, EasyUIBasedAppModelConfigFrom
710
from core.app.apps.base_app_generator import BaseAppGenerator
811
from core.app.apps.base_app_queue_manager import AppQueueManager
@@ -83,11 +86,10 @@ def _handle_response(
8386

8487
def _get_app_model_config(self, app_model: App, conversation: Optional[Conversation] = None) -> AppModelConfig:
8588
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
9091
)
92+
app_model_config = db.session.scalar(stmt)
9193

9294
if not app_model_config:
9395
raise AppModelConfigBrokenError()
@@ -253,7 +255,8 @@ def _get_conversation(self, conversation_id: str) -> Conversation:
253255
:param conversation_id: conversation id
254256
:return: conversation
255257
"""
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))
257260

258261
if not conversation:
259262
raise ConversationNotExistsError("Conversation not exists")
@@ -266,7 +269,8 @@ def _get_message(self, message_id: str) -> Message:
266269
:param message_id: message id
267270
:return: message
268271
"""
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))
270274

271275
if message is None:
272276
raise MessageNotExistsError("Message not exists")

api/core/app/features/annotation_reply/annotation_reply.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
22
from typing import Optional
33

4+
from sqlalchemy import select
5+
46
from core.app.entities.app_invoke_entities import InvokeFrom
57
from core.rag.datasource.vdb.vector_factory import Vector
68
from extensions.ext_database import db
@@ -25,9 +27,8 @@ def query(
2527
:param invoke_from: invoke from
2628
:return:
2729
"""
28-
annotation_setting = (
29-
db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_record.id).first()
30-
)
30+
stmt = select(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_record.id)
31+
annotation_setting = db.session.scalar(stmt)
3132

3233
if not annotation_setting:
3334
return None

0 commit comments

Comments
 (0)