mirror of
https://github.com/langgenius/dify.git
synced 2026-04-23 04:06:13 +08:00
feat(agent-sandbox): new tool resolver and bash execution implementation
This commit is contained in:
@ -4,6 +4,7 @@ import shlex
|
||||
from collections.abc import Mapping, Sequence
|
||||
from typing import Any
|
||||
|
||||
from core.sandbox.debug import sandbox_debug
|
||||
from core.sandbox.manager import SandboxManager
|
||||
from core.virtual_environment.__base.command_future import CommandCancelledError, CommandTimeoutError
|
||||
from core.virtual_environment.__base.virtual_environment import VirtualEnvironment
|
||||
@ -83,6 +84,9 @@ class CommandNode(Node[CommandNodeData]):
|
||||
|
||||
try:
|
||||
command = shlex.split(raw_command)
|
||||
|
||||
sandbox_debug("command_node", "command", command)
|
||||
|
||||
future = sandbox.run_command(connection_handle, command, cwd=working_directory)
|
||||
result = future.result(timeout=timeout)
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ from sqlalchemy import select
|
||||
|
||||
from core.agent.entities import AgentEntity, AgentLog, AgentResult, AgentToolEntity, ExecutionContext
|
||||
from core.agent.patterns import StrategyFactory
|
||||
from core.app.entities.app_invoke_entities import ModelConfigWithCredentialsEntity
|
||||
from core.app.entities.app_invoke_entities import InvokeFrom, ModelConfigWithCredentialsEntity
|
||||
from core.file import File, FileTransferMethod, FileType, file_manager
|
||||
from core.helper.code_executor import CodeExecutor, CodeLanguage
|
||||
from core.llm_generator.output_parser.errors import OutputParserError
|
||||
@ -1581,6 +1581,35 @@ class LLMNode(Node[LLMNodeData]):
|
||||
result = yield from self._process_tool_outputs(outputs)
|
||||
return result
|
||||
|
||||
def _prepare_sandbox_tools(self) -> list[Tool]:
|
||||
"""Prepare sandbox tools."""
|
||||
tool_instances = []
|
||||
|
||||
for tool in self._node_data.tools or []:
|
||||
try:
|
||||
# Get tool runtime from ToolManager
|
||||
tool_runtime = ToolManager.get_tool_runtime(
|
||||
tenant_id=self.tenant_id,
|
||||
tool_name=tool.tool_name,
|
||||
provider_id=tool.provider_name,
|
||||
provider_type=tool.type,
|
||||
invoke_from=InvokeFrom.AGENT,
|
||||
credential_id=tool.credential_id,
|
||||
)
|
||||
|
||||
# Apply custom description from extra field if available
|
||||
if tool.extra.get("description") and tool_runtime.entity.description:
|
||||
tool_runtime.entity.description.llm = (
|
||||
tool.extra.get("description") or tool_runtime.entity.description.llm
|
||||
)
|
||||
|
||||
tool_instances.append(tool_runtime)
|
||||
except Exception as e:
|
||||
logger.warning("Failed to load tool %s: %s", tool, str(e))
|
||||
continue
|
||||
|
||||
return tool_instances
|
||||
|
||||
def _invoke_llm_with_sandbox(
|
||||
self,
|
||||
model_instance: ModelInstance,
|
||||
@ -1592,7 +1621,7 @@ class LLMNode(Node[LLMNodeData]):
|
||||
if not workflow_execution_id:
|
||||
raise LLMNodeError("workflow_execution_id is required for sandbox runtime mode")
|
||||
|
||||
configured_tools = self._prepare_tool_instances(variable_pool)
|
||||
configured_tools = self._prepare_sandbox_tools()
|
||||
|
||||
result: LLMGenerationData | None = None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user