Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
refactor(model): Refactor plugin model schema cache to be process-global to prevent redundant Daemon API calls (#31689)
Nie Ronghuaceb69147931c
diff --git a/api/configs/feature/__init__.py b/api/configs/feature/__init__.pyindex 786094f2959..4343a056ddd 100644--- a/api/configs/feature/__init__.py+++ b/api/configs/feature/__init__.py@@ -243,6 +243,11 @@ class PluginConfig(BaseSettings):default=15728640 * 12,)+ PLUGIN_MODEL_SCHEMA_CACHE_TTL: PositiveInt = Field(+ description="TTL in seconds for caching plugin model schemas in Redis",+ default=24 * 60 * 60,+ )+class MarketplaceConfig(BaseSettings):"""diff --git a/api/contexts/__init__.py b/api/contexts/__init__.pyindex 7c16bc231f5..c52dcf8a574 100644--- a/api/contexts/__init__.py+++ b/api/contexts/__init__.py@@ -6,7 +6,6 @@ from contexts.wrapper import RecyclableContextVarif TYPE_CHECKING:from core.datasource.__base.datasource_provider import DatasourcePluginProviderController- from core.model_runtime.entities.model_entities import AIModelEntityfrom core.plugin.entities.plugin_daemon import PluginModelProviderEntityfrom core.tools.plugin_tool.provider import PluginToolProviderControllerfrom core.trigger.provider import PluginTriggerProviderController@@ -29,12 +28,6 @@ plugin_model_providers_lock: RecyclableContextVar[Lock] = RecyclableContextVar(ContextVar("plugin_model_providers_lock"))-plugin_model_schema_lock: RecyclableContextVar[Lock] = RecyclableContextVar(ContextVar("plugin_model_schema_lock"))--plugin_model_schemas: RecyclableContextVar[dict[str, "AIModelEntity"]] = RecyclableContextVar(- ContextVar("plugin_model_schemas")-)-datasource_plugin_providers: RecyclableContextVar[dict[str, "DatasourcePluginProviderController"]] = (RecyclableContextVar(ContextVar("datasource_plugin_providers")))diff --git a/api/core/model_runtime/model_providers/__base/ai_model.py b/api/core/model_runtime/model_providers/__base/ai_model.pyindex 45f0335c2e0..c3e50eaddd3 100644--- a/api/core/model_runtime/model_providers/__base/ai_model.py+++ b/api/core/model_runtime/model_providers/__base/ai_model.py@@ -1,10 +1,11 @@import decimalimport hashlib-from threading import Lock+import logging-from pydantic import BaseModel, ConfigDict, Field+from pydantic import BaseModel, ConfigDict, Field, ValidationError+from redis import RedisError-import contexts+from configs import dify_configfrom core.model_runtime.entities.common_entities import I18nObjectfrom core.model_runtime.entities.defaults import PARAMETER_RULE_TEMPLATEfrom core.model_runtime.entities.model_entities import (@@ -24,6 +25,9 @@ from core.model_runtime.errors.invoke import (InvokeServerUnavailableError,)from core.plugin.entities.plugin_daemon import PluginModelProviderEntity+from extensions.ext_redis import redis_client++logger = logging.getLogger(__name__)class AIModel(BaseModel):@@ -144,34 +148,60 @@ class AIModel(BaseModel):plugin_model_manager = PluginModelClient()cache_key = f"{self.tenant_id}:{self.plugin_id}:{self.provider_name}:{self.model_type.value}:{model}"- # sort credentialssorted_credentials = sorted(credentials.items()) if credentials else []cache_key += ":".join([hashlib.md5(f"{k}:{v}".encode()).hexdigest() for k, v in sorted_credentials])+ cached_schema_json = Nonetry:- contexts.plugin_model_schemas.get()- except LookupError:- contexts.plugin_model_schemas.set({})- contexts.plugin_model_schema_lock.set(Lock())-- with contexts.plugin_model_schema_lock.get():- if cache_key in contexts.plugin_model_schemas.get():- return contexts.plugin_model_schemas.get()[cache_key]-- schema = plugin_model_manager.get_model_schema(- tenant_id=self.tenant_id,- user_id="unknown",- plugin_id=self.plugin_id,- provider=self.provider_name,- model_type=self.model_type.value,- model=model,- credentials=credentials or {},+ cached_schema_json = redis_client.get(cache_key)+ except (RedisError, RuntimeError) as exc:+ logger.warning(+ "Failed to read plugin model schema cache for model %s: %s",+ model,+ str(exc),+ exc_info=True,)+ if cached_schema_json:+ try:+ return AIModelEntity.model_validate_json(cached_schema_json)+ except ValidationError:+ logger.warning(+ "Failed to validate cached plugin model schema for model %s",+ model,+ exc_info=True,+ )+ try:+ redis_client.delete(cache_key)+ except (RedisError, RuntimeError) as exc:+ logger.warning(+ "Failed to delete invalid plugin model schema cache for model %s: %s",+ model,+ str(exc),+ exc_info=True,+ )- if schema:- contexts.plugin_model_schemas.get()[cache_key] = schema+ schema = plugin_model_manager.get_model_schema(+ tenant_id=self.tenant_id,+ user_id="unknown",+ plugin_id=self.plugin_id,+ provider=self.provider_name,+ model_type=self.model_type.value,+ model=model,+ credentials=credentials or {},+ )++ if schema:+ try:+ redis_client.setex(cache_key, dify_config.PLUGIN_MODEL_SCHEMA_CACHE_TTL, schema.model_dump_json())+ except (RedisError, RuntimeError) as exc:+ logger.warning(+ "Failed to write plugin model schema cache for model %s: %s",+ model,+ str(exc),+ exc_info=True,+ )- return schema+ return schemadef get_customizable_model_schema_from_credentials(self, model: str, credentials: dict) -> AIModelEntity | None:"""diff --git a/api/core/model_runtime/model_providers/model_provider_factory.py b/api/core/model_runtime/model_providers/model_provider_factory.pyindex 28f162a9287..64538a6779e 100644--- a/api/core/model_runtime/model_providers/model_provider_factory.py+++ b/api/core/model_runtime/model_providers/model_provider_factory.py@@ -5,7 +5,11 @@ import loggingfrom collections.abc import Sequencefrom threading import Lock+from pydantic import ValidationError+from redis import RedisError+import contexts+from configs import dify_configfrom core.model_runtime.entities.model_entities import AIModelEntity, ModelTypefrom core.model_runtime.entities.provider_entities import ProviderConfig, ProviderEntity, SimpleProviderEntityfrom core.model_runtime.model_providers.__base.ai_model import AIModel@@ -18,6 +22,7 @@ from core.model_runtime.model_providers.__base.tts_model import TTSModelfrom core.model_runtime.schema_validators.model_credential_schema_validator import ModelCredentialSchemaValidatorfrom core.model_runtime.schema_validators.provider_credential_schema_validator import ProviderCredentialSchemaValidatorfrom core.plugin.entities.plugin_daemon import PluginModelProviderEntity+from extensions.ext_redis import redis_clientfrom models.provider_ids import ModelProviderIDlogger = logging.getLogger(__name__)@@ -175,34 +180,60 @@ class ModelProviderFactory:"""plugin_id, provider_name = self.get_plugin_id_and_provider_name_from_provider(provider)cache_key = f"{self.tenant_id}:{plugin_id}:{provider_name}:{model_type.value}:{model}"- # sort credentialssorted_credentials = sorted(credentials.items()) if credentials else []cache_key += ":".join([hashlib.md5(f"{k}:{v}".encode()).hexdigest() for k, v in sorted_credentials])+ cached_schema_json = Nonetry:- contexts.plugin_model_schemas.get()- except LookupError:- contexts.plugin_model_schemas.set({})- contexts.plugin_model_schema_lock.set(Lock())-- with contexts.plugin_model_schema_lock.get():- if cache_key in contexts.plugin_model_schemas.get():- return contexts.plugin_model_schemas.get()[cache_key]-- schema = self.plugin_model_manager.get_model_schema(- tenant_id=self.tenant_id,- user_id="unknown",- plugin_id=plugin_id,- provider=provider_name,- model_type=model_type.value,- model=model,- credentials=credentials or {},+ cached_schema_json = redis_client.get(cache_key)+ except (RedisError, RuntimeError) as exc:+ logger.warning(+ "Failed to read plugin model schema cache for model %s: %s",+ model,+ str(exc),+ exc_info=True,)+ if cached_schema_json:+ try:+ return AIModelEntity.model_validate_json(cached_schema_json)+ except ValidationError:+ logger.warning(+ "Failed to validate cached plugin model schema for model %s",+ model,+ exc_info=True,+ )+ try:+ redis_client.delete(cache_key)+ except (RedisError, RuntimeError) as exc:+ logger.warning(+ "Failed to delete invalid plugin model schema cache for model %s: %s",+ model,+ str(exc),+ exc_info=True,+ )++ schema = self.plugin_model_manager.get_model_schema(+ tenant_id=self.tenant_id,+ user_id="unknown",+ plugin_id=plugin_id,+ provider=provider_name,+ model_type=model_type.value,+ model=model,+ credentials=credentials or {},+ )- if schema:- contexts.plugin_model_schemas.get()[cache_key] = schema-- return schema+ if schema:+ try:+ redis_client.setex(cache_key, dify_config.PLUGIN_MODEL_SCHEMA_CACHE_TTL, schema.model_dump_json())+ except (RedisError, RuntimeError) as exc:+ logger.warning(+ "Failed to write plugin model schema cache for model %s: %s",+ model,+ str(exc),+ exc_info=True,+ )++ return schemadef get_models(self,