1
- from pydantic_settings import SettingsConfigDict
1
+ import logging
2
+ from typing import Any
2
3
3
- from configs .deploy import DeploymentConfig
4
- from configs .enterprise import EnterpriseFeatureConfig
5
- from configs .extra import ExtraServiceConfig
6
- from configs .feature import FeatureConfig
7
- from configs .middleware import MiddlewareConfig
8
- from configs .packaging import PackagingInfo
4
+ from pydantic .fields import FieldInfo
5
+ from pydantic_settings import BaseSettings , PydanticBaseSettingsSource , SettingsConfigDict
6
+
7
+ from .deploy import DeploymentConfig
8
+ from .enterprise import EnterpriseFeatureConfig
9
+ from .extra import ExtraServiceConfig
10
+ from .feature import FeatureConfig
11
+ from .middleware import MiddlewareConfig
12
+ from .packaging import PackagingInfo
13
+ from .remote_settings_sources import RemoteSettingsSource , RemoteSettingsSourceConfig , RemoteSettingsSourceName
14
+ from .remote_settings_sources .apollo import ApolloSettingsSource
15
+
16
+ logger = logging .getLogger (__name__ )
17
+
18
+
19
+ class RemoteSettingsSourceFactory (PydanticBaseSettingsSource ):
20
+ def __init__ (self , settings_cls : type [BaseSettings ]):
21
+ super ().__init__ (settings_cls )
22
+
23
+ def get_field_value (self , field : FieldInfo , field_name : str ) -> tuple [Any , str , bool ]:
24
+ raise NotImplementedError
25
+
26
+ def __call__ (self ) -> dict [str , Any ]:
27
+ current_state = self .current_state
28
+ remote_source_name = current_state .get ("REMOTE_SETTINGS_SOURCE_NAME" )
29
+ if not remote_source_name :
30
+ return {}
31
+
32
+ remote_source : RemoteSettingsSource | None = None
33
+ match remote_source_name :
34
+ case RemoteSettingsSourceName .APOLLO :
35
+ remote_source = ApolloSettingsSource (current_state )
36
+ case _:
37
+ logger .warning (f"Unsupported remote source: { remote_source_name } " )
38
+ return {}
39
+
40
+ d : dict [str , Any ] = {}
41
+
42
+ for field_name , field in self .settings_cls .model_fields .items ():
43
+ field_value , field_key , value_is_complex = remote_source .get_field_value (field , field_name )
44
+ field_value = remote_source .prepare_field_value (field_name , field , field_value , value_is_complex )
45
+ if field_value is not None :
46
+ d [field_key ] = field_value
47
+
48
+ return d
9
49
10
50
11
51
class DifyConfig (
@@ -19,6 +59,8 @@ class DifyConfig(
19
59
MiddlewareConfig ,
20
60
# Extra service configs
21
61
ExtraServiceConfig ,
62
+ # Remote source configs
63
+ RemoteSettingsSourceConfig ,
22
64
# Enterprise feature configs
23
65
# **Before using, please contact [email protected] by email to inquire about licensing matters.**
24
66
EnterpriseFeatureConfig ,
@@ -35,3 +77,20 @@ class DifyConfig(
35
77
# please consider to arrange it in the proper config group of existed or added
36
78
# for better readability and maintainability.
37
79
# Thanks for your concentration and consideration.
80
+
81
+ @classmethod
82
+ def settings_customise_sources (
83
+ cls ,
84
+ settings_cls : type [BaseSettings ],
85
+ init_settings : PydanticBaseSettingsSource ,
86
+ env_settings : PydanticBaseSettingsSource ,
87
+ dotenv_settings : PydanticBaseSettingsSource ,
88
+ file_secret_settings : PydanticBaseSettingsSource ,
89
+ ) -> tuple [PydanticBaseSettingsSource , ...]:
90
+ return (
91
+ init_settings ,
92
+ env_settings ,
93
+ RemoteSettingsSourceFactory (settings_cls ),
94
+ dotenv_settings ,
95
+ file_secret_settings ,
96
+ )
0 commit comments