Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor: pass db.session explicitly to instruction_modify_legacy (#37403) (#43045)

Denis Ermakovbf55d1f6a60d
diff --git a/api/controllers/console/app/generator.py b/api/controllers/console/app/generator.py
index d29f8a1911b..b2cf228c9d6 100644
--- a/api/controllers/console/app/generator.py
+++ b/api/controllers/console/app/generator.py
@@ -401,6 +401,7 @@ class InstructionGenerateApi(Resource):
instruction=req_data.instruction,
model_config=req_data.model_config_data,
ideal_output=req_data.ideal_output,
+ session=session,
)
if req_data.node_id != "" and req_data.current != "": # For workflow node
return LLMGenerator.instruction_modify_workflow(
diff --git a/api/core/llm_generator/llm_generator.py b/api/core/llm_generator/llm_generator.py
index d4104e3c67f..ea066698a29 100644
--- a/api/core/llm_generator/llm_generator.py
+++ b/api/core/llm_generator/llm_generator.py
@@ -875,8 +875,9 @@ class LLMGenerator:
instruction: str,
model_config: ModelConfig,
ideal_output: str | None,
+ session: Session,
):
- last_run: Message | None = db.session.scalar(
+ last_run: Message | None = session.scalar(
select(Message)
.join(App, App.id == Message.app_id)
.where(Message.app_id == flow_id, App.tenant_id == tenant_id)
diff --git a/api/tests/unit_tests/controllers/console/app/test_generator_api.py b/api/tests/unit_tests/controllers/console/app/test_generator_api.py
index 184f0a3f988..94371295601 100644
--- a/api/tests/unit_tests/controllers/console/app/test_generator_api.py
+++ b/api/tests/unit_tests/controllers/console/app/test_generator_api.py
@@ -220,11 +220,13 @@ def test_instruction_generate_legacy_modify(
) -> None:
api = generator_module.InstructionGenerateApi()
method = unwrap(api.post)
- monkeypatch.setattr(
- generator_module.LLMGenerator,
- "instruction_modify_legacy",
- lambda **_kwargs: {"instruction": "ok"},
- )
+ captured: dict = {}
+
+ def _capture(**kwargs):
+ captured.update(kwargs)
+ return {"instruction": "ok"}
+
+ monkeypatch.setattr(generator_module.LLMGenerator, "instruction_modify_legacy", _capture)
with app.test_request_context(
"/console/api/instruction-generate",
@@ -240,6 +242,7 @@ def test_instruction_generate_legacy_modify(
response = method(api, InstructionGeneratePayload.model_validate(request.get_json()), sqlite_session, "t1")
assert response == {"instruction": "ok"}
+ assert captured["session"] is sqlite_session
@pytest.mark.parametrize("sqlite_session", [(App,)], indirect=True)
diff --git a/api/tests/unit_tests/core/llm_generator/test_llm_generator.py b/api/tests/unit_tests/core/llm_generator/test_llm_generator.py
index 617c45b3e98..98b51f2527c 100644
--- a/api/tests/unit_tests/core/llm_generator/test_llm_generator.py
+++ b/api/tests/unit_tests/core/llm_generator/test_llm_generator.py
@@ -736,6 +736,7 @@ class TestLLMGenerator:
"Test {{#last_run#}} and {{#current#}} and {{#error_message#}}",
model_config_entity,
"ideal",
+ session=database,
)
assert result == {"modified": "prompt"}
@@ -774,7 +775,7 @@ class TestLLMGenerator:
recording_model_instance.invoke_llm.return_value = _llm_result('{"modified": "prompt"}')
result = LLMGenerator.instruction_modify_legacy(
- app.tenant_id, app.id, "current", "instruction", model_config_entity, "ideal"
+ app.tenant_id, app.id, "current", "instruction", model_config_entity, "ideal", session=database
)
assert result == {"modified": "prompt"}
@@ -994,7 +995,7 @@ class TestLLMGenerator:
mock_model_instance.invoke_llm.return_value = response
result = LLMGenerator.instruction_modify_legacy(
- app.tenant_id, app.id, "current", "instruction", model_config_entity, "ideal"
+ app.tenant_id, app.id, "current", "instruction", model_config_entity, "ideal", session=database
)
assert error_fragment in result["error"]
@@ -1015,7 +1016,7 @@ class TestLLMGenerator:
mock_model_instance.invoke_llm.side_effect = model_error
result = LLMGenerator.instruction_modify_legacy(
- app.tenant_id, app.id, "current", "instruction", model_config_entity, "ideal"
+ app.tenant_id, app.id, "current", "instruction", model_config_entity, "ideal", session=database
)
assert error_fragment.lower() in result["error"].lower()