mirror of
https://github.com/langgenius/dify.git
synced 2026-02-09 23:20:12 -05:00
feat: implement sandbox layer integration in app generators
This commit is contained in:
@@ -15,6 +15,7 @@ from sqlalchemy.orm import Session, sessionmaker
|
|||||||
import contexts
|
import contexts
|
||||||
from configs import dify_config
|
from configs import dify_config
|
||||||
from constants import UUID_NIL
|
from constants import UUID_NIL
|
||||||
|
from core.app.layers.sandbox_layer import SandboxLayer
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from controllers.console.app.workflow import LoopNodeRunPayload
|
from controllers.console.app.workflow import LoopNodeRunPayload
|
||||||
@@ -30,7 +31,6 @@ from core.app.apps.message_based_app_queue_manager import MessageBasedAppQueueMa
|
|||||||
from core.app.entities.app_invoke_entities import AdvancedChatAppGenerateEntity, InvokeFrom
|
from core.app.entities.app_invoke_entities import AdvancedChatAppGenerateEntity, InvokeFrom
|
||||||
from core.app.entities.task_entities import ChatbotAppBlockingResponse, ChatbotAppStreamResponse
|
from core.app.entities.task_entities import ChatbotAppBlockingResponse, ChatbotAppStreamResponse
|
||||||
from core.app.layers.pause_state_persist_layer import PauseStateLayerConfig, PauseStatePersistenceLayer
|
from core.app.layers.pause_state_persist_layer import PauseStateLayerConfig, PauseStatePersistenceLayer
|
||||||
from core.app.layers.sandbox_layer import SandboxLayer
|
|
||||||
from core.helper.trace_id_helper import extract_external_trace_id_from_args
|
from core.helper.trace_id_helper import extract_external_trace_id_from_args
|
||||||
from core.model_runtime.errors.invoke import InvokeAuthorizationError
|
from core.model_runtime.errors.invoke import InvokeAuthorizationError
|
||||||
from core.ops.ops_trace_manager import TraceQueueManager
|
from core.ops.ops_trace_manager import TraceQueueManager
|
||||||
@@ -500,6 +500,27 @@ class AdvancedChatAppGenerator(MessageBasedAppGenerator):
|
|||||||
state_owner_user_id=pause_state_config.state_owner_user_id,
|
state_owner_user_id=pause_state_config.state_owner_user_id,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
sandbox: Sandbox | None = None
|
||||||
|
if workflow.get_feature(WorkflowFeatures.SANDBOX).enabled:
|
||||||
|
sandbox_provider = SandboxProviderService.get_sandbox_provider(
|
||||||
|
application_generate_entity.app_config.tenant_id
|
||||||
|
)
|
||||||
|
if workflow.version == Workflow.VERSION_DRAFT:
|
||||||
|
sandbox = SandboxService.create_draft(
|
||||||
|
tenant_id=application_generate_entity.app_config.tenant_id,
|
||||||
|
app_id=application_generate_entity.app_config.app_id,
|
||||||
|
user_id=application_generate_entity.user_id,
|
||||||
|
sandbox_provider=sandbox_provider,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
sandbox = SandboxService.create(
|
||||||
|
tenant_id=application_generate_entity.app_config.tenant_id,
|
||||||
|
app_id=application_generate_entity.app_config.app_id,
|
||||||
|
user_id=application_generate_entity.user_id,
|
||||||
|
sandbox_id=conversation.id,
|
||||||
|
sandbox_provider=sandbox_provider,
|
||||||
|
)
|
||||||
|
graph_layers.append(SandboxLayer(sandbox))
|
||||||
|
|
||||||
# new thread with request context and contextvars
|
# new thread with request context and contextvars
|
||||||
context = contextvars.copy_context()
|
context = contextvars.copy_context()
|
||||||
@@ -518,6 +539,7 @@ class AdvancedChatAppGenerator(MessageBasedAppGenerator):
|
|||||||
"workflow_node_execution_repository": workflow_node_execution_repository,
|
"workflow_node_execution_repository": workflow_node_execution_repository,
|
||||||
"graph_engine_layers": tuple(graph_layers),
|
"graph_engine_layers": tuple(graph_layers),
|
||||||
"graph_runtime_state": graph_runtime_state,
|
"graph_runtime_state": graph_runtime_state,
|
||||||
|
"sandbox": sandbox,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -565,6 +587,7 @@ class AdvancedChatAppGenerator(MessageBasedAppGenerator):
|
|||||||
workflow_node_execution_repository: WorkflowNodeExecutionRepository,
|
workflow_node_execution_repository: WorkflowNodeExecutionRepository,
|
||||||
graph_engine_layers: Sequence[GraphEngineLayer] = (),
|
graph_engine_layers: Sequence[GraphEngineLayer] = (),
|
||||||
graph_runtime_state: GraphRuntimeState | None = None,
|
graph_runtime_state: GraphRuntimeState | None = None,
|
||||||
|
sandbox: Sandbox | None = None,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Generate worker in a new thread.
|
Generate worker in a new thread.
|
||||||
@@ -592,29 +615,6 @@ class AdvancedChatAppGenerator(MessageBasedAppGenerator):
|
|||||||
if workflow is None:
|
if workflow is None:
|
||||||
raise ValueError("Workflow not found")
|
raise ValueError("Workflow not found")
|
||||||
|
|
||||||
sandbox: Sandbox | None = None
|
|
||||||
graph_engine_layers: tuple = ()
|
|
||||||
if workflow.get_feature(WorkflowFeatures.SANDBOX).enabled:
|
|
||||||
sandbox_provider = SandboxProviderService.get_sandbox_provider(
|
|
||||||
application_generate_entity.app_config.tenant_id
|
|
||||||
)
|
|
||||||
if workflow.version == Workflow.VERSION_DRAFT:
|
|
||||||
sandbox = SandboxService.create_draft(
|
|
||||||
tenant_id=application_generate_entity.app_config.tenant_id,
|
|
||||||
app_id=application_generate_entity.app_config.app_id,
|
|
||||||
user_id=application_generate_entity.user_id,
|
|
||||||
sandbox_provider=sandbox_provider,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
sandbox = SandboxService.create(
|
|
||||||
tenant_id=application_generate_entity.app_config.tenant_id,
|
|
||||||
app_id=application_generate_entity.app_config.app_id,
|
|
||||||
user_id=application_generate_entity.user_id,
|
|
||||||
sandbox_id=conversation_id,
|
|
||||||
sandbox_provider=sandbox_provider,
|
|
||||||
)
|
|
||||||
graph_engine_layers = (SandboxLayer(sandbox=sandbox),)
|
|
||||||
|
|
||||||
# Determine system_user_id based on invocation source
|
# Determine system_user_id based on invocation source
|
||||||
is_external_api_call = application_generate_entity.invoke_from in {
|
is_external_api_call = application_generate_entity.invoke_from in {
|
||||||
InvokeFrom.WEB_APP,
|
InvokeFrom.WEB_APP,
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ from core.helper.trace_id_helper import extract_external_trace_id_from_args
|
|||||||
from core.model_runtime.errors.invoke import InvokeAuthorizationError
|
from core.model_runtime.errors.invoke import InvokeAuthorizationError
|
||||||
from core.ops.ops_trace_manager import TraceQueueManager
|
from core.ops.ops_trace_manager import TraceQueueManager
|
||||||
from core.repositories import DifyCoreRepositoryFactory
|
from core.repositories import DifyCoreRepositoryFactory
|
||||||
from core.sandbox import Sandbox
|
from core.sandbox.sandbox import Sandbox
|
||||||
from core.workflow.graph_engine.layers.base import GraphEngineLayer
|
from core.workflow.graph_engine.layers.base import GraphEngineLayer
|
||||||
from core.workflow.repositories.draft_variable_repository import DraftVariableSaverFactory
|
from core.workflow.repositories.draft_variable_repository import DraftVariableSaverFactory
|
||||||
from core.workflow.repositories.workflow_execution_repository import WorkflowExecutionRepository
|
from core.workflow.repositories.workflow_execution_repository import WorkflowExecutionRepository
|
||||||
@@ -316,6 +316,28 @@ class WorkflowAppGenerator(BaseAppGenerator):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
sandbox: Sandbox | None = None
|
||||||
|
if workflow.get_feature(WorkflowFeatures.SANDBOX).enabled:
|
||||||
|
sandbox_provider = SandboxProviderService.get_sandbox_provider(
|
||||||
|
application_generate_entity.app_config.tenant_id
|
||||||
|
)
|
||||||
|
if workflow.version == Workflow.VERSION_DRAFT:
|
||||||
|
sandbox = SandboxService.create_draft(
|
||||||
|
tenant_id=application_generate_entity.app_config.tenant_id,
|
||||||
|
app_id=application_generate_entity.app_config.app_id,
|
||||||
|
user_id=application_generate_entity.user_id,
|
||||||
|
sandbox_provider=sandbox_provider,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
sandbox = SandboxService.create(
|
||||||
|
tenant_id=application_generate_entity.app_config.tenant_id,
|
||||||
|
app_id=application_generate_entity.app_config.app_id,
|
||||||
|
user_id=application_generate_entity.user_id,
|
||||||
|
sandbox_id=application_generate_entity.workflow_execution_id,
|
||||||
|
sandbox_provider=sandbox_provider,
|
||||||
|
)
|
||||||
|
graph_layers.append(SandboxLayer(sandbox=sandbox))
|
||||||
|
|
||||||
# new thread with request context and contextvars
|
# new thread with request context and contextvars
|
||||||
context = contextvars.copy_context()
|
context = contextvars.copy_context()
|
||||||
|
|
||||||
@@ -335,6 +357,7 @@ class WorkflowAppGenerator(BaseAppGenerator):
|
|||||||
"workflow_node_execution_repository": workflow_node_execution_repository,
|
"workflow_node_execution_repository": workflow_node_execution_repository,
|
||||||
"graph_engine_layers": tuple(graph_layers),
|
"graph_engine_layers": tuple(graph_layers),
|
||||||
"graph_runtime_state": graph_runtime_state,
|
"graph_runtime_state": graph_runtime_state,
|
||||||
|
"sandbox": sandbox,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -533,6 +556,7 @@ class WorkflowAppGenerator(BaseAppGenerator):
|
|||||||
root_node_id: str | None = None,
|
root_node_id: str | None = None,
|
||||||
graph_engine_layers: Sequence[GraphEngineLayer] = (),
|
graph_engine_layers: Sequence[GraphEngineLayer] = (),
|
||||||
graph_runtime_state: GraphRuntimeState | None = None,
|
graph_runtime_state: GraphRuntimeState | None = None,
|
||||||
|
sandbox: Sandbox | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Generate worker in a new thread.
|
Generate worker in a new thread.
|
||||||
@@ -554,31 +578,6 @@ class WorkflowAppGenerator(BaseAppGenerator):
|
|||||||
if workflow is None:
|
if workflow is None:
|
||||||
raise ValueError("Workflow not found")
|
raise ValueError("Workflow not found")
|
||||||
|
|
||||||
sandbox: Sandbox | None = None
|
|
||||||
if workflow.get_feature(WorkflowFeatures.SANDBOX).enabled:
|
|
||||||
sandbox_provider = SandboxProviderService.get_sandbox_provider(
|
|
||||||
application_generate_entity.app_config.tenant_id
|
|
||||||
)
|
|
||||||
if workflow.version == Workflow.VERSION_DRAFT:
|
|
||||||
sandbox = SandboxService.create_draft(
|
|
||||||
tenant_id=application_generate_entity.app_config.tenant_id,
|
|
||||||
app_id=application_generate_entity.app_config.app_id,
|
|
||||||
user_id=application_generate_entity.user_id,
|
|
||||||
sandbox_provider=sandbox_provider,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
sandbox = SandboxService.create(
|
|
||||||
tenant_id=application_generate_entity.app_config.tenant_id,
|
|
||||||
app_id=application_generate_entity.app_config.app_id,
|
|
||||||
user_id=application_generate_entity.user_id,
|
|
||||||
sandbox_id=application_generate_entity.workflow_execution_id,
|
|
||||||
sandbox_provider=sandbox_provider,
|
|
||||||
)
|
|
||||||
graph_engine_layers = (
|
|
||||||
*graph_engine_layers,
|
|
||||||
SandboxLayer(sandbox=sandbox),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Determine system_user_id based on invocation source
|
# Determine system_user_id based on invocation source
|
||||||
is_external_api_call = application_generate_entity.invoke_from in {
|
is_external_api_call = application_generate_entity.invoke_from in {
|
||||||
InvokeFrom.WEB_APP,
|
InvokeFrom.WEB_APP,
|
||||||
|
|||||||
Reference in New Issue
Block a user