Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def get(self, provider_id: str):
if context is None:
raise Forbidden("Invalid context_id")

user_id, tenant_id = context.get("user_id"), context.get("tenant_id")
user_id: str = context["user_id"]
tenant_id: str = context["tenant_id"]
datasource_provider_id = DatasourceProviderID(provider_id)
plugin_id = datasource_provider_id.plugin_id
datasource_provider_service = DatasourceProviderService()
Expand All @@ -141,7 +142,7 @@ def get(self, provider_id: str):
system_credentials=oauth_client_params,
request=request,
)
credential_id = context.get("credential_id")
credential_id: str | None = context.get("credential_id")
if credential_id:
datasource_provider_service.reauthorize_datasource_oauth_provider(
tenant_id=tenant_id,
Expand All @@ -150,7 +151,7 @@ def get(self, provider_id: str):
name=oauth_response.metadata.get("name") or None,
expire_at=oauth_response.expires_at,
credentials=dict(oauth_response.credentials),
credential_id=context.get("credential_id"),
credential_id=credential_id,
)
else:
datasource_provider_service.add_datasource_oauth_provider(
Expand Down
3 changes: 2 additions & 1 deletion api/controllers/console/workspace/tool_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,8 @@ def get(self, provider):
tool_provider = ToolProviderID(provider)
plugin_id = tool_provider.plugin_id
provider_name = tool_provider.provider_name
user_id, tenant_id = context.get("user_id"), context.get("tenant_id")
user_id: str = context["user_id"]
tenant_id: str = context["tenant_id"]

oauth_handler = OAuthHandler()
oauth_client_params = BuiltinToolManageService.get_oauth_client(tenant_id, provider)
Expand Down
6 changes: 3 additions & 3 deletions api/controllers/console/workspace/trigger_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,9 @@ def get(self, provider):
provider_id = TriggerProviderID(provider)
plugin_id = provider_id.plugin_id
provider_name = provider_id.provider_name
user_id = context.get("user_id")
tenant_id = context.get("tenant_id")
subscription_builder_id = context.get("subscription_builder_id")
user_id: str = context["user_id"]
tenant_id: str = context["tenant_id"]
subscription_builder_id: str = context["subscription_builder_id"]

# Get OAuth client configuration
oauth_client_params = TriggerProviderService.get_oauth_client(
Expand Down
16 changes: 13 additions & 3 deletions api/services/account_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@
from hashlib import sha256
from typing import Any, cast

from pydantic import BaseModel
from pydantic import BaseModel, TypeAdapter
from sqlalchemy import func, select
from sqlalchemy.orm import Session
from typing_extensions import TypedDict


class InvitationData(TypedDict):
account_id: str
email: str
workspace_id: str


_invitation_adapter: TypeAdapter[InvitationData] = TypeAdapter(InvitationData)
from werkzeug.exceptions import Unauthorized

from configs import dify_config
Expand Down Expand Up @@ -1571,7 +1581,7 @@ def get_invitation_if_token_valid(
@classmethod
def get_invitation_by_token(
cls, token: str, workspace_id: str | None = None, email: str | None = None
) -> dict[str, str] | None:
) -> InvitationData | None:
if workspace_id is not None and email is not None:
email_hash = sha256(email.encode()).hexdigest()
cache_key = f"member_invite_token:{workspace_id}, {email_hash}:{token}"
Expand All @@ -1590,7 +1600,7 @@ def get_invitation_by_token(
if not data:
return None

invitation: dict = json.loads(data)
invitation = _invitation_adapter.validate_json(data)
return invitation

@classmethod
Expand Down
10 changes: 7 additions & 3 deletions api/services/message_service.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
from collections.abc import Sequence
from typing import Union

from graphon.model_runtime.entities.model_entities import ModelType
from pydantic import TypeAdapter
from sqlalchemy.orm import sessionmaker

from core.app.apps.advanced_chat.app_config_manager import AdvancedChatAppConfigManager
Expand All @@ -17,7 +17,7 @@
from libs.infinite_scroll_pagination import InfiniteScrollPagination
from models import Account
from models.enums import FeedbackFromSource, FeedbackRating
from models.model import App, AppMode, AppModelConfig, EndUser, Message, MessageFeedback
from models.model import App, AppMode, AppModelConfig, AppModelConfigDict, EndUser, Message, MessageFeedback
from repositories.execution_extra_content_repository import ExecutionExtraContentRepository
from repositories.sqlalchemy_execution_extra_content_repository import (
SQLAlchemyExecutionExtraContentRepository,
Expand All @@ -31,6 +31,8 @@
)
from services.workflow_service import WorkflowService

_app_model_config_adapter: TypeAdapter[AppModelConfigDict] = TypeAdapter(AppModelConfigDict)


def _create_execution_extra_content_repository() -> ExecutionExtraContentRepository:
session_maker = sessionmaker(bind=db.engine, expire_on_commit=False)
Expand Down Expand Up @@ -286,7 +288,9 @@ def get_suggested_questions_after_answer(
.first()
)
else:
conversation_override_model_configs = json.loads(conversation.override_model_configs)
conversation_override_model_configs = _app_model_config_adapter.validate_json(
conversation.override_model_configs
)
app_model_config = AppModelConfig(
app_id=app_model.id,
)
Expand Down
11 changes: 5 additions & 6 deletions api/services/model_load_balancing_service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging
from json import JSONDecodeError
from typing import Union
from typing import Any, Union

from graphon.model_runtime.entities.model_entities import ModelType
from graphon.model_runtime.entities.provider_entities import (
Expand Down Expand Up @@ -168,10 +167,10 @@ def get_load_balancing_configs(

try:
if load_balancing_config.encrypted_config:
credentials: dict[str, object] = json.loads(load_balancing_config.encrypted_config)
credentials: dict[str, Any] = json.loads(load_balancing_config.encrypted_config)
Comment thread
asukaminato0721 marked this conversation as resolved.
Resolved
else:
credentials = {}
except JSONDecodeError:
except (json.JSONDecodeError, ValueError):
credentials = {}

# Get provider credential secret variables
Expand Down Expand Up @@ -256,7 +255,7 @@ def get_load_balancing_config(
credentials = json.loads(load_balancing_model_config.encrypted_config)
else:
credentials = {}
except JSONDecodeError:
except (json.JSONDecodeError, ValueError):
credentials = {}

# Get credential form schemas from model credential schema or provider credential schema
Expand Down Expand Up @@ -575,7 +574,7 @@ def _custom_credentials_validate(
original_credentials = json.loads(load_balancing_model_config.encrypted_config)
else:
original_credentials = {}
except JSONDecodeError:
except (json.JSONDecodeError, ValueError):
original_credentials = {}

# encrypt credentials
Expand Down
37 changes: 24 additions & 13 deletions api/services/plugin/plugin_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import sqlalchemy as sa
import tqdm
from flask import Flask, current_app
from pydantic import TypeAdapter
from sqlalchemy.orm import Session
from typing_extensions import TypedDict

from core.agent.entities import AgentToolEntity
from core.helper import marketplace
Expand All @@ -33,6 +35,14 @@
excluded_providers = ["time", "audio", "code", "webscraper"]


class _TenantPluginRecord(TypedDict):
tenant_id: str
plugins: list[str]


_tenant_plugin_adapter: TypeAdapter[_TenantPluginRecord] = TypeAdapter(_TenantPluginRecord)


class PluginMigration:
@classmethod
def extract_plugins(cls, filepath: str, workers: int):
Expand Down Expand Up @@ -308,9 +318,8 @@ def extract_unique_plugins(cls, extracted_plugins: str) -> Mapping[str, Any]:
logger.info("Extracting unique plugins from %s", extracted_plugins)
with open(extracted_plugins) as f:
for line in f:
data = json.loads(line)
new_plugin_ids = data.get("plugins", [])
for plugin_id in new_plugin_ids:
data = _tenant_plugin_adapter.validate_json(line)
for plugin_id in data["plugins"]:
if plugin_id not in plugin_ids:
plugin_ids.append(plugin_id)

Expand Down Expand Up @@ -381,21 +390,23 @@ def install(tenant_id: str, plugin_ids: list[str]):
Read line by line, and install plugins for each tenant.
"""
for line in f:
data = json.loads(line)
tenant_id = data.get("tenant_id")
plugin_ids = data.get("plugins", [])
current_not_installed = {
"tenant_id": tenant_id,
"plugin_not_exist": [],
}
data = _tenant_plugin_adapter.validate_json(line)
tenant_id = data["tenant_id"]
plugin_ids = data["plugins"]
plugin_not_exist: list[str] = []
# get plugin unique identifier
for plugin_id in plugin_ids:
unique_identifier = plugins.get(plugin_id)
if unique_identifier:
current_not_installed["plugin_not_exist"].append(plugin_id)
plugin_not_exist.append(plugin_id)

if current_not_installed["plugin_not_exist"]:
not_installed.append(current_not_installed)
if plugin_not_exist:
not_installed.append(
{
"tenant_id": tenant_id,
"plugin_not_exist": plugin_not_exist,
}
)

thread_pool.submit(install, tenant_id, plugin_ids)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""

import io
import json
import logging
import time
import zipfile
Expand All @@ -17,8 +16,23 @@
from typing import Any, cast

import click
from pydantic import TypeAdapter
from sqlalchemy.dialects.postgresql import insert as pg_insert
from sqlalchemy.engine import CursorResult
from typing_extensions import TypedDict


class _TableInfo(TypedDict, total=False):
row_count: int


class ArchiveManifest(TypedDict, total=False):
tables: dict[str, _TableInfo]
schema_version: str


_manifest_adapter: TypeAdapter[ArchiveManifest] = TypeAdapter(ArchiveManifest)

from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker

from extensions.ext_database import db
Expand Down Expand Up @@ -239,12 +253,12 @@ def _get_workflow_run_repo(self) -> APIWorkflowRunRepository:
return self.workflow_run_repo

@staticmethod
def _load_manifest_from_zip(archive: zipfile.ZipFile) -> dict[str, Any]:
def _load_manifest_from_zip(archive: zipfile.ZipFile) -> ArchiveManifest:
try:
data = archive.read("manifest.json")
except KeyError as e:
raise ValueError("manifest.json missing from archive bundle") from e
return json.loads(data.decode("utf-8"))
return _manifest_adapter.validate_json(data)

def _restore_table_records(
self,
Expand Down Expand Up @@ -332,7 +346,7 @@ def _convert_datetime_fields(

return result

def _get_schema_version(self, manifest: dict[str, Any]) -> str:
def _get_schema_version(self, manifest: ArchiveManifest) -> str:
schema_version = manifest.get("schema_version")
if not schema_version:
logger.warning("Manifest missing schema_version; defaulting to 1.0")
Expand Down
10 changes: 6 additions & 4 deletions api/services/tools/tools_transform_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections.abc import Mapping
from typing import Any, Union

from pydantic import ValidationError
from pydantic import TypeAdapter, ValidationError
from yarl import URL

from configs import dify_config
Expand Down Expand Up @@ -31,6 +31,8 @@

logger = logging.getLogger(__name__)

_mcp_tools_adapter: TypeAdapter[list[MCPTool]] = TypeAdapter(list[MCPTool])


class ToolTransformService:
_MCP_SCHEMA_TYPE_RESOLUTION_MAX_DEPTH = 10
Expand All @@ -53,7 +55,7 @@ def get_tool_provider_icon_url(
if isinstance(icon, str):
return json.loads(icon)
return icon
except Exception:
except (json.JSONDecodeError, ValueError):
return {"background": "#252525", "content": "\ud83d\ude01"}
elif provider_type == ToolProviderType.MCP:
return icon
Expand Down Expand Up @@ -247,8 +249,8 @@ def mcp_provider_to_user_provider(

response = provider_entity.to_api_response(user_name=user_name, include_sensitive=include_sensitive)
try:
mcp_tools = [MCPTool(**tool) for tool in json.loads(db_provider.tools)]
except (ValidationError, json.JSONDecodeError):
mcp_tools = _mcp_tools_adapter.validate_json(db_provider.tools)
except (ValidationError, ValueError):
mcp_tools = []
# Add additional fields specific to the transform
response["id"] = db_provider.server_identifier if not for_list else db_provider.id
Expand Down
18 changes: 12 additions & 6 deletions api/services/trigger/trigger_subscription_builder_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging
import uuid
from collections.abc import Mapping
Expand All @@ -7,6 +6,7 @@
from typing import Any

from flask import Request, Response
from pydantic import TypeAdapter

from core.plugin.entities.plugin_daemon import CredentialType
from core.plugin.entities.request import TriggerDispatchResponse
Expand All @@ -29,6 +29,8 @@

logger = logging.getLogger(__name__)

_request_logs_adapter: TypeAdapter[list[RequestLog]] = TypeAdapter(list[RequestLog])


class TriggerSubscriptionBuilderService:
"""Service for managing trigger providers and credentials"""
Expand Down Expand Up @@ -398,7 +400,7 @@ def get_subscription_builder(cls, endpoint_id: str) -> SubscriptionBuilder | Non
cache_key = cls.encode_cache_key(endpoint_id)
subscription_cache = redis_client.get(cache_key)
if subscription_cache:
return SubscriptionBuilder.model_validate(json.loads(subscription_cache))
return SubscriptionBuilder.model_validate_json(subscription_cache)

return None

Expand All @@ -423,12 +425,16 @@ def append_log(cls, endpoint_id: str, request: Request, response: Response) -> N
)

key = f"trigger:subscription:builder:logs:{endpoint_id}"
logs = json.loads(redis_client.get(key) or "[]")
logs.append(log.model_dump(mode="json"))
logs = _request_logs_adapter.validate_json(redis_client.get(key) or b"[]")
logs.append(log)

# Keep last N logs
logs = logs[-cls.__VALIDATION_REQUEST_CACHE_COUNT__ :]
redis_client.setex(key, cls.__VALIDATION_REQUEST_CACHE_EXPIRE_SECONDS__, json.dumps(logs, default=str))
redis_client.setex(
key,
cls.__VALIDATION_REQUEST_CACHE_EXPIRE_SECONDS__,
_request_logs_adapter.dump_json(logs),
)

@classmethod
def list_logs(cls, endpoint_id: str) -> list[RequestLog]:
Expand All @@ -437,7 +443,7 @@ def list_logs(cls, endpoint_id: str) -> list[RequestLog]:
logs_json = redis_client.get(key)
if not logs_json:
return []
return [RequestLog.model_validate(log) for log in json.loads(logs_json)]
return _request_logs_adapter.validate_json(logs_json)

@classmethod
def process_builder_validation_endpoint(cls, endpoint_id: str, request: Request) -> Response | None:
Expand Down
Loading
Loading