mirror of
https://mirror.skon.top/github.com/langgenius/dify.git
synced 2026-04-20 15:20:15 +08:00
refactor(api): replace dict/Mapping with TypedDict in core/app (#33601)
Some checks failed
autofix.ci / autofix (push) Has been cancelled
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Has been cancelled
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Has been cancelled
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Has been cancelled
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Has been cancelled
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Has been cancelled
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Has been cancelled
Main CI Pipeline / Check Changed Files (push) Has been cancelled
Main CI Pipeline / API Tests (push) Has been cancelled
Main CI Pipeline / Web Tests (push) Has been cancelled
Main CI Pipeline / Style Check (push) Has been cancelled
Main CI Pipeline / VDB Tests (push) Has been cancelled
Main CI Pipeline / DB Migration Test (push) Has been cancelled
Trigger i18n Sync on Push / trigger (push) Has been cancelled
Some checks failed
autofix.ci / autofix (push) Has been cancelled
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Has been cancelled
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Has been cancelled
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Has been cancelled
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Has been cancelled
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Has been cancelled
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Has been cancelled
Main CI Pipeline / Check Changed Files (push) Has been cancelled
Main CI Pipeline / API Tests (push) Has been cancelled
Main CI Pipeline / Web Tests (push) Has been cancelled
Main CI Pipeline / Style Check (push) Has been cancelled
Main CI Pipeline / VDB Tests (push) Has been cancelled
Main CI Pipeline / DB Migration Test (push) Has been cancelled
Trigger i18n Sync on Push / trigger (push) Has been cancelled
This commit is contained in:
@@ -1,13 +1,36 @@
|
|||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
from typing import Any
|
from typing import Any, TypedDict
|
||||||
|
|
||||||
from configs import dify_config
|
from configs import dify_config
|
||||||
from constants import DEFAULT_FILE_NUMBER_LIMITS
|
from constants import DEFAULT_FILE_NUMBER_LIMITS
|
||||||
|
|
||||||
|
|
||||||
|
class SystemParametersDict(TypedDict):
|
||||||
|
image_file_size_limit: int
|
||||||
|
video_file_size_limit: int
|
||||||
|
audio_file_size_limit: int
|
||||||
|
file_size_limit: int
|
||||||
|
workflow_file_upload_limit: int
|
||||||
|
|
||||||
|
|
||||||
|
class AppParametersDict(TypedDict):
|
||||||
|
opening_statement: str | None
|
||||||
|
suggested_questions: list[str]
|
||||||
|
suggested_questions_after_answer: dict[str, Any]
|
||||||
|
speech_to_text: dict[str, Any]
|
||||||
|
text_to_speech: dict[str, Any]
|
||||||
|
retriever_resource: dict[str, Any]
|
||||||
|
annotation_reply: dict[str, Any]
|
||||||
|
more_like_this: dict[str, Any]
|
||||||
|
user_input_form: list[dict[str, Any]]
|
||||||
|
sensitive_word_avoidance: dict[str, Any]
|
||||||
|
file_upload: dict[str, Any]
|
||||||
|
system_parameters: SystemParametersDict
|
||||||
|
|
||||||
|
|
||||||
def get_parameters_from_feature_dict(
|
def get_parameters_from_feature_dict(
|
||||||
*, features_dict: Mapping[str, Any], user_input_form: list[dict[str, Any]]
|
*, features_dict: Mapping[str, Any], user_input_form: list[dict[str, Any]]
|
||||||
) -> Mapping[str, Any]:
|
) -> AppParametersDict:
|
||||||
"""
|
"""
|
||||||
Mapping from feature dict to webapp parameters
|
Mapping from feature dict to webapp parameters
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import time
|
|||||||
from collections.abc import Mapping, Sequence
|
from collections.abc import Mapping, Sequence
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Any, NewType, Union
|
from typing import Any, NewType, TypedDict, Union
|
||||||
|
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
@@ -76,6 +76,20 @@ NodeExecutionId = NewType("NodeExecutionId", str)
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class AccountCreatedByDict(TypedDict):
|
||||||
|
id: str
|
||||||
|
name: str
|
||||||
|
email: str
|
||||||
|
|
||||||
|
|
||||||
|
class EndUserCreatedByDict(TypedDict):
|
||||||
|
id: str
|
||||||
|
user: str
|
||||||
|
|
||||||
|
|
||||||
|
CreatedByDict = AccountCreatedByDict | EndUserCreatedByDict
|
||||||
|
|
||||||
|
|
||||||
@dataclass(slots=True)
|
@dataclass(slots=True)
|
||||||
class _NodeSnapshot:
|
class _NodeSnapshot:
|
||||||
"""In-memory cache for node metadata between start and completion events."""
|
"""In-memory cache for node metadata between start and completion events."""
|
||||||
@@ -249,19 +263,19 @@ class WorkflowResponseConverter:
|
|||||||
outputs_mapping = graph_runtime_state.outputs or {}
|
outputs_mapping = graph_runtime_state.outputs or {}
|
||||||
encoded_outputs = WorkflowRuntimeTypeConverter().to_json_encodable(outputs_mapping)
|
encoded_outputs = WorkflowRuntimeTypeConverter().to_json_encodable(outputs_mapping)
|
||||||
|
|
||||||
created_by: Mapping[str, object] | None
|
created_by: CreatedByDict | dict[str, object] = {}
|
||||||
user = self._user
|
user = self._user
|
||||||
if isinstance(user, Account):
|
if isinstance(user, Account):
|
||||||
created_by = {
|
created_by = AccountCreatedByDict(
|
||||||
"id": user.id,
|
id=user.id,
|
||||||
"name": user.name,
|
name=user.name,
|
||||||
"email": user.email,
|
email=user.email,
|
||||||
}
|
)
|
||||||
else:
|
elif isinstance(user, EndUser):
|
||||||
created_by = {
|
created_by = EndUserCreatedByDict(
|
||||||
"id": user.id,
|
id=user.id,
|
||||||
"user": user.session_id,
|
user=user.session_id,
|
||||||
}
|
)
|
||||||
|
|
||||||
return WorkflowFinishStreamResponse(
|
return WorkflowFinishStreamResponse(
|
||||||
task_id=task_id,
|
task_id=task_id,
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from typing import TypedDict
|
||||||
|
|
||||||
from core.tools.signature import sign_tool_file
|
from core.tools.signature import sign_tool_file
|
||||||
from dify_graph.file import helpers as file_helpers
|
from dify_graph.file import helpers as file_helpers
|
||||||
from dify_graph.file.enums import FileTransferMethod
|
from dify_graph.file.enums import FileTransferMethod
|
||||||
@@ -6,7 +8,20 @@ from models.model import MessageFile, UploadFile
|
|||||||
MAX_TOOL_FILE_EXTENSION_LENGTH = 10
|
MAX_TOOL_FILE_EXTENSION_LENGTH = 10
|
||||||
|
|
||||||
|
|
||||||
def prepare_file_dict(message_file: MessageFile, upload_files_map: dict[str, UploadFile]) -> dict:
|
class MessageFileInfoDict(TypedDict):
|
||||||
|
related_id: str
|
||||||
|
extension: str
|
||||||
|
filename: str
|
||||||
|
size: int
|
||||||
|
mime_type: str
|
||||||
|
transfer_method: str
|
||||||
|
type: str
|
||||||
|
url: str
|
||||||
|
upload_file_id: str
|
||||||
|
remote_url: str | None
|
||||||
|
|
||||||
|
|
||||||
|
def prepare_file_dict(message_file: MessageFile, upload_files_map: dict[str, UploadFile]) -> MessageFileInfoDict:
|
||||||
"""
|
"""
|
||||||
Prepare file dictionary for message end stream response.
|
Prepare file dictionary for message end stream response.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user