dify/api/core/datasource/datasource_engine.py

225 lines
8.5 KiB
Python
Raw Normal View History

2025-04-17 15:07:23 +08:00
import json
from collections.abc import Generator, Iterable
from mimetypes import guess_type
2025-04-25 15:49:36 +08:00
from typing import Any, Optional, cast
2025-04-17 15:07:23 +08:00
from yarl import URL
from core.app.entities.app_invoke_entities import InvokeFrom
from core.callback_handler.workflow_tool_callback_handler import DifyWorkflowCallbackHandler
2025-04-25 15:49:36 +08:00
from core.datasource.__base.datasource_plugin import DatasourcePlugin
from core.datasource.entities.datasource_entities import (
DatasourceInvokeMessage,
DatasourceInvokeMessageBinary,
)
2025-04-17 15:07:23 +08:00
from core.file import FileType
from core.file.models import FileTransferMethod
from extensions.ext_database import db
from models.enums import CreatedByRole
from models.model import Message, MessageFile
2025-04-24 15:42:30 +08:00
class DatasourceEngine:
2025-04-17 15:07:23 +08:00
"""
2025-04-24 15:42:30 +08:00
Datasource runtime engine take care of the datasource executions.
2025-04-17 15:07:23 +08:00
"""
@staticmethod
2025-04-25 15:49:36 +08:00
def invoke_first_step(
datasource: DatasourcePlugin,
datasource_parameters: dict[str, Any],
2025-04-17 15:07:23 +08:00
user_id: str,
2025-04-25 15:49:36 +08:00
workflow_tool_callback: DifyWorkflowCallbackHandler,
2025-04-17 15:07:23 +08:00
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
2025-04-25 15:49:36 +08:00
) -> Generator[DatasourceInvokeMessage, None, None]:
2025-04-17 15:07:23 +08:00
"""
2025-04-25 15:49:36 +08:00
Workflow invokes the datasource with the given arguments.
2025-04-17 15:07:23 +08:00
"""
try:
# hit the callback handler
2025-04-27 14:31:19 +08:00
workflow_tool_callback.on_datasource_start(
datasource_name=datasource.entity.identity.name, datasource_inputs=datasource_parameters
)
2025-04-17 15:07:23 +08:00
2025-04-25 15:49:36 +08:00
if datasource.runtime and datasource.runtime.runtime_parameters:
datasource_parameters = {**datasource.runtime.runtime_parameters, **datasource_parameters}
2025-04-17 15:07:23 +08:00
2025-04-25 15:49:36 +08:00
response = datasource._invoke_first_step(
2025-04-17 15:07:23 +08:00
user_id=user_id,
2025-04-25 15:49:36 +08:00
datasource_parameters=datasource_parameters,
conversation_id=conversation_id,
app_id=app_id,
message_id=message_id,
2025-04-17 15:07:23 +08:00
)
# hit the callback handler
2025-04-25 15:49:36 +08:00
response = workflow_tool_callback.on_datasource_end(
datasource_name=datasource.entity.identity.name,
datasource_inputs=datasource_parameters,
datasource_outputs=response,
2025-04-17 15:07:23 +08:00
)
2025-04-25 15:49:36 +08:00
return response
2025-04-17 15:07:23 +08:00
except Exception as e:
2025-04-25 15:49:36 +08:00
workflow_tool_callback.on_tool_error(e)
raise e
2025-04-17 15:07:23 +08:00
@staticmethod
2025-04-25 15:49:36 +08:00
def invoke_second_step(
datasource: DatasourcePlugin,
datasource_parameters: dict[str, Any],
2025-04-17 15:07:23 +08:00
user_id: str,
workflow_tool_callback: DifyWorkflowCallbackHandler,
2025-04-25 15:49:36 +08:00
) -> Generator[DatasourceInvokeMessage, None, None]:
2025-04-17 15:07:23 +08:00
"""
2025-04-25 15:49:36 +08:00
Workflow invokes the datasource with the given arguments.
2025-04-17 15:07:23 +08:00
"""
try:
2025-04-25 15:49:36 +08:00
response = datasource._invoke_second_step(
2025-04-17 15:07:23 +08:00
user_id=user_id,
2025-04-25 15:49:36 +08:00
datasource_parameters=datasource_parameters,
2025-04-17 15:07:23 +08:00
)
return response
except Exception as e:
workflow_tool_callback.on_tool_error(e)
raise e
@staticmethod
2025-04-25 15:49:36 +08:00
def _convert_datasource_response_to_str(datasource_response: list[DatasourceInvokeMessage]) -> str:
2025-04-17 15:07:23 +08:00
"""
2025-04-25 15:49:36 +08:00
Handle datasource response
2025-04-17 15:07:23 +08:00
"""
result = ""
2025-04-25 15:49:36 +08:00
for response in datasource_response:
if response.type == DatasourceInvokeMessage.MessageType.TEXT:
result += cast(DatasourceInvokeMessage.TextMessage, response.message).text
elif response.type == DatasourceInvokeMessage.MessageType.LINK:
2025-04-17 15:07:23 +08:00
result += (
2025-04-25 15:49:36 +08:00
f"result link: {cast(DatasourceInvokeMessage.TextMessage, response.message).text}."
2025-04-17 15:07:23 +08:00
+ " please tell user to check it."
)
2025-04-27 14:31:19 +08:00
elif response.type in {
DatasourceInvokeMessage.MessageType.IMAGE_LINK,
DatasourceInvokeMessage.MessageType.IMAGE,
}:
2025-04-17 15:07:23 +08:00
result += (
"image has been created and sent to user already, "
+ "you do not need to create it, just tell the user to check it now."
)
2025-04-25 15:49:36 +08:00
elif response.type == DatasourceInvokeMessage.MessageType.JSON:
2025-04-17 15:07:23 +08:00
result = json.dumps(
2025-04-25 15:49:36 +08:00
cast(DatasourceInvokeMessage.JsonMessage, response.message).json_object, ensure_ascii=False
2025-04-17 15:07:23 +08:00
)
else:
result += str(response.message)
return result
@staticmethod
2025-04-25 15:49:36 +08:00
def _extract_datasource_response_binary_and_text(
datasource_response: list[DatasourceInvokeMessage],
) -> Generator[DatasourceInvokeMessageBinary, None, None]:
2025-04-17 15:07:23 +08:00
"""
2025-04-25 15:49:36 +08:00
Extract datasource response binary
2025-04-17 15:07:23 +08:00
"""
2025-04-25 15:49:36 +08:00
for response in datasource_response:
2025-04-27 14:31:19 +08:00
if response.type in {
DatasourceInvokeMessage.MessageType.IMAGE_LINK,
DatasourceInvokeMessage.MessageType.IMAGE,
}:
2025-04-17 15:07:23 +08:00
mimetype = None
if not response.meta:
raise ValueError("missing meta data")
if response.meta.get("mime_type"):
mimetype = response.meta.get("mime_type")
else:
try:
2025-04-25 15:49:36 +08:00
url = URL(cast(DatasourceInvokeMessage.TextMessage, response.message).text)
2025-04-17 15:07:23 +08:00
extension = url.suffix
guess_type_result, _ = guess_type(f"a{extension}")
if guess_type_result:
mimetype = guess_type_result
except Exception:
pass
if not mimetype:
mimetype = "image/jpeg"
2025-04-25 15:49:36 +08:00
yield DatasourceInvokeMessageBinary(
2025-04-17 15:07:23 +08:00
mimetype=response.meta.get("mime_type", "image/jpeg"),
2025-04-25 15:49:36 +08:00
url=cast(DatasourceInvokeMessage.TextMessage, response.message).text,
2025-04-17 15:07:23 +08:00
)
2025-04-25 15:49:36 +08:00
elif response.type == DatasourceInvokeMessage.MessageType.BLOB:
2025-04-17 15:07:23 +08:00
if not response.meta:
raise ValueError("missing meta data")
2025-04-25 15:49:36 +08:00
yield DatasourceInvokeMessageBinary(
2025-04-17 15:07:23 +08:00
mimetype=response.meta.get("mime_type", "application/octet-stream"),
2025-04-25 15:49:36 +08:00
url=cast(DatasourceInvokeMessage.TextMessage, response.message).text,
2025-04-17 15:07:23 +08:00
)
2025-04-25 15:49:36 +08:00
elif response.type == DatasourceInvokeMessage.MessageType.LINK:
2025-04-17 15:07:23 +08:00
# check if there is a mime type in meta
if response.meta and "mime_type" in response.meta:
2025-04-25 15:49:36 +08:00
yield DatasourceInvokeMessageBinary(
2025-04-17 15:07:23 +08:00
mimetype=response.meta.get("mime_type", "application/octet-stream")
if response.meta
else "application/octet-stream",
2025-04-25 15:49:36 +08:00
url=cast(DatasourceInvokeMessage.TextMessage, response.message).text,
2025-04-17 15:07:23 +08:00
)
@staticmethod
def _create_message_files(
2025-04-25 15:49:36 +08:00
datasource_messages: Iterable[DatasourceInvokeMessageBinary],
2025-04-17 15:07:23 +08:00
agent_message: Message,
invoke_from: InvokeFrom,
user_id: str,
) -> list[str]:
"""
Create message file
:return: message file ids
"""
result = []
2025-04-25 15:49:36 +08:00
for message in datasource_messages:
2025-04-17 15:07:23 +08:00
if "image" in message.mimetype:
file_type = FileType.IMAGE
elif "video" in message.mimetype:
file_type = FileType.VIDEO
elif "audio" in message.mimetype:
file_type = FileType.AUDIO
elif "text" in message.mimetype or "pdf" in message.mimetype:
file_type = FileType.DOCUMENT
else:
file_type = FileType.CUSTOM
# extract tool file id from url
tool_file_id = message.url.split("/")[-1].split(".")[0]
message_file = MessageFile(
message_id=agent_message.id,
type=file_type,
transfer_method=FileTransferMethod.TOOL_FILE,
belongs_to="assistant",
url=message.url,
upload_file_id=tool_file_id,
created_by_role=(
CreatedByRole.ACCOUNT
if invoke_from in {InvokeFrom.EXPLORE, InvokeFrom.DEBUGGER}
else CreatedByRole.END_USER
),
created_by=user_id,
)
db.session.add(message_file)
db.session.commit()
db.session.refresh(message_file)
result.append(message_file.id)
db.session.close()
return result