__init__.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. from typing import Annotated, Optional
  2. from pydantic import AliasChoices, Field, HttpUrl, NegativeInt, NonNegativeInt, PositiveInt, computed_field
  3. from pydantic_settings import BaseSettings
  4. from configs.feature.hosted_service import HostedServiceConfig
  5. class SecurityConfig(BaseSettings):
  6. """
  7. Security-related configurations for the application
  8. """
  9. SECRET_KEY: Optional[str] = Field(
  10. description="Secret key for secure session cookie signing."
  11. "Make sure you are changing this key for your deployment with a strong key."
  12. "Generate a strong key using `openssl rand -base64 42` or set via the `SECRET_KEY` environment variable.",
  13. default=None,
  14. )
  15. RESET_PASSWORD_TOKEN_EXPIRY_HOURS: PositiveInt = Field(
  16. description="Duration in hours for which a password reset token remains valid",
  17. default=24,
  18. )
  19. class AppExecutionConfig(BaseSettings):
  20. """
  21. Configuration parameters for application execution
  22. """
  23. APP_MAX_EXECUTION_TIME: PositiveInt = Field(
  24. description="Maximum allowed execution time for the application in seconds",
  25. default=1200,
  26. )
  27. APP_MAX_ACTIVE_REQUESTS: NonNegativeInt = Field(
  28. description="Maximum number of concurrent active requests per app (0 for unlimited)",
  29. default=0,
  30. )
  31. class CodeExecutionSandboxConfig(BaseSettings):
  32. """
  33. Configuration for the code execution sandbox environment
  34. """
  35. CODE_EXECUTION_ENDPOINT: HttpUrl = Field(
  36. description="URL endpoint for the code execution service",
  37. default="http://sandbox:8194",
  38. )
  39. CODE_EXECUTION_API_KEY: str = Field(
  40. description="API key for accessing the code execution service",
  41. default="dify-sandbox",
  42. )
  43. CODE_EXECUTION_CONNECT_TIMEOUT: Optional[float] = Field(
  44. description="Connection timeout in seconds for code execution requests",
  45. default=10.0,
  46. )
  47. CODE_EXECUTION_READ_TIMEOUT: Optional[float] = Field(
  48. description="Read timeout in seconds for code execution requests",
  49. default=60.0,
  50. )
  51. CODE_EXECUTION_WRITE_TIMEOUT: Optional[float] = Field(
  52. description="Write timeout in seconds for code execution request",
  53. default=10.0,
  54. )
  55. CODE_MAX_NUMBER: PositiveInt = Field(
  56. description="Maximum allowed numeric value in code execution",
  57. default=9223372036854775807,
  58. )
  59. CODE_MIN_NUMBER: NegativeInt = Field(
  60. description="Minimum allowed numeric value in code execution",
  61. default=-9223372036854775807,
  62. )
  63. CODE_MAX_DEPTH: PositiveInt = Field(
  64. description="Maximum allowed depth for nested structures in code execution",
  65. default=5,
  66. )
  67. CODE_MAX_PRECISION: PositiveInt = Field(
  68. description="mMaximum number of decimal places for floating-point numbers in code execution",
  69. default=20,
  70. )
  71. CODE_MAX_STRING_LENGTH: PositiveInt = Field(
  72. description="Maximum allowed length for strings in code execution",
  73. default=80000,
  74. )
  75. CODE_MAX_STRING_ARRAY_LENGTH: PositiveInt = Field(
  76. description="Maximum allowed length for string arrays in code execution",
  77. default=30,
  78. )
  79. CODE_MAX_OBJECT_ARRAY_LENGTH: PositiveInt = Field(
  80. description="Maximum allowed length for object arrays in code execution",
  81. default=30,
  82. )
  83. CODE_MAX_NUMBER_ARRAY_LENGTH: PositiveInt = Field(
  84. description="Maximum allowed length for numeric arrays in code execution",
  85. default=1000,
  86. )
  87. class EndpointConfig(BaseSettings):
  88. """
  89. Configuration for various application endpoints and URLs
  90. """
  91. CONSOLE_API_URL: str = Field(
  92. description="Base URL for the console API,"
  93. "used for login authentication callback or notion integration callbacks",
  94. default="",
  95. )
  96. CONSOLE_WEB_URL: str = Field(
  97. description="Base URL for the console web interface," "used for frontend references and CORS configuration",
  98. default="",
  99. )
  100. SERVICE_API_URL: str = Field(
  101. description="Base URL for the service API, displayed to users for API access",
  102. default="",
  103. )
  104. APP_WEB_URL: str = Field(
  105. description="Base URL for the web application, used for frontend references",
  106. default="",
  107. )
  108. class FileAccessConfig(BaseSettings):
  109. """
  110. Configuration for file access and handling
  111. """
  112. FILES_URL: str = Field(
  113. description="Base URL for file preview or download,"
  114. " used for frontend display and multi-model inputs"
  115. "Url is signed and has expiration time.",
  116. validation_alias=AliasChoices("FILES_URL", "CONSOLE_API_URL"),
  117. alias_priority=1,
  118. default="",
  119. )
  120. FILES_ACCESS_TIMEOUT: int = Field(
  121. description="Expiration time in seconds for file access URLs",
  122. default=300,
  123. )
  124. class FileUploadConfig(BaseSettings):
  125. """
  126. Configuration for file upload limitations
  127. """
  128. UPLOAD_FILE_SIZE_LIMIT: NonNegativeInt = Field(
  129. description="Maximum allowed file size for uploads in megabytes",
  130. default=15,
  131. )
  132. UPLOAD_FILE_BATCH_LIMIT: NonNegativeInt = Field(
  133. description="Maximum number of files allowed in a single upload batch",
  134. default=5,
  135. )
  136. UPLOAD_IMAGE_FILE_SIZE_LIMIT: NonNegativeInt = Field(
  137. description="Maximum allowed image file size for uploads in megabytes",
  138. default=10,
  139. )
  140. BATCH_UPLOAD_LIMIT: NonNegativeInt = Field(
  141. description="Maximum number of files allowed in a batch upload operation",
  142. default=20,
  143. )
  144. class HttpConfig(BaseSettings):
  145. """
  146. HTTP-related configurations for the application
  147. """
  148. API_COMPRESSION_ENABLED: bool = Field(
  149. description="Enable or disable gzip compression for HTTP responses",
  150. default=False,
  151. )
  152. inner_CONSOLE_CORS_ALLOW_ORIGINS: str = Field(
  153. description="Comma-separated list of allowed origins for CORS in the console",
  154. validation_alias=AliasChoices("CONSOLE_CORS_ALLOW_ORIGINS", "CONSOLE_WEB_URL"),
  155. default="",
  156. )
  157. @computed_field
  158. @property
  159. def CONSOLE_CORS_ALLOW_ORIGINS(self) -> list[str]:
  160. return self.inner_CONSOLE_CORS_ALLOW_ORIGINS.split(",")
  161. inner_WEB_API_CORS_ALLOW_ORIGINS: str = Field(
  162. description="",
  163. validation_alias=AliasChoices("WEB_API_CORS_ALLOW_ORIGINS"),
  164. default="*",
  165. )
  166. @computed_field
  167. @property
  168. def WEB_API_CORS_ALLOW_ORIGINS(self) -> list[str]:
  169. return self.inner_WEB_API_CORS_ALLOW_ORIGINS.split(",")
  170. HTTP_REQUEST_MAX_CONNECT_TIMEOUT: Annotated[
  171. PositiveInt, Field(ge=10, description="Maximum connection timeout in seconds for HTTP requests")
  172. ] = 10
  173. HTTP_REQUEST_MAX_READ_TIMEOUT: Annotated[
  174. PositiveInt, Field(ge=60, description="Maximum read timeout in seconds for HTTP requests")
  175. ] = 60
  176. HTTP_REQUEST_MAX_WRITE_TIMEOUT: Annotated[
  177. PositiveInt, Field(ge=10, description="Maximum write timeout in seconds for HTTP requests")
  178. ] = 20
  179. HTTP_REQUEST_NODE_MAX_BINARY_SIZE: PositiveInt = Field(
  180. description="Maximum allowed size in bytes for binary data in HTTP requests",
  181. default=10 * 1024 * 1024,
  182. )
  183. HTTP_REQUEST_NODE_MAX_TEXT_SIZE: PositiveInt = Field(
  184. description="Maximum allowed size in bytes for text data in HTTP requests",
  185. default=1 * 1024 * 1024,
  186. )
  187. SSRF_PROXY_HTTP_URL: Optional[str] = Field(
  188. description="Proxy URL for HTTP requests to prevent Server-Side Request Forgery (SSRF)",
  189. default=None,
  190. )
  191. SSRF_PROXY_HTTPS_URL: Optional[str] = Field(
  192. description="Proxy URL for HTTPS requests to prevent Server-Side Request Forgery (SSRF)",
  193. default=None,
  194. )
  195. RESPECT_XFORWARD_HEADERS_ENABLED: bool = Field(
  196. description="Enable or disable the X-Forwarded-For Proxy Fix middleware from Werkzeug"
  197. " to respect X-* headers to redirect clients",
  198. default=False,
  199. )
  200. class InnerAPIConfig(BaseSettings):
  201. """
  202. Configuration for internal API functionality
  203. """
  204. INNER_API: bool = Field(
  205. description="Enable or disable the internal API",
  206. default=False,
  207. )
  208. INNER_API_KEY: Optional[str] = Field(
  209. description="API key for accessing the internal API",
  210. default=None,
  211. )
  212. class LoggingConfig(BaseSettings):
  213. """
  214. Configuration for application logging
  215. """
  216. LOG_LEVEL: str = Field(
  217. description="Logging level, default to INFO. Set to ERROR for production environments.",
  218. default="INFO",
  219. )
  220. LOG_FILE: Optional[str] = Field(
  221. description="File path for log output.",
  222. default=None,
  223. )
  224. LOG_FORMAT: str = Field(
  225. description="Format string for log messages",
  226. default="%(asctime)s.%(msecs)03d %(levelname)s [%(threadName)s] [%(filename)s:%(lineno)d] - %(message)s",
  227. )
  228. LOG_DATEFORMAT: Optional[str] = Field(
  229. description="Date format string for log timestamps",
  230. default=None,
  231. )
  232. LOG_TZ: Optional[str] = Field(
  233. description="Timezone for log timestamps (e.g., 'America/New_York')",
  234. default=None,
  235. )
  236. class ModelLoadBalanceConfig(BaseSettings):
  237. """
  238. Configuration for model load balancing
  239. """
  240. MODEL_LB_ENABLED: bool = Field(
  241. description="Enable or disable load balancing for models",
  242. default=False,
  243. )
  244. class BillingConfig(BaseSettings):
  245. """
  246. Configuration for platform billing features
  247. """
  248. BILLING_ENABLED: bool = Field(
  249. description="Enable or disable billing functionality",
  250. default=False,
  251. )
  252. class UpdateConfig(BaseSettings):
  253. """
  254. Configuration for application update checks
  255. """
  256. CHECK_UPDATE_URL: str = Field(
  257. description="URL to check for application updates",
  258. default="https://updates.dify.ai",
  259. )
  260. class WorkflowConfig(BaseSettings):
  261. """
  262. Configuration for workflow execution
  263. """
  264. WORKFLOW_MAX_EXECUTION_STEPS: PositiveInt = Field(
  265. description="Maximum number of steps allowed in a single workflow execution",
  266. default=500,
  267. )
  268. WORKFLOW_MAX_EXECUTION_TIME: PositiveInt = Field(
  269. description="Maximum execution time in seconds for a single workflow",
  270. default=1200,
  271. )
  272. WORKFLOW_CALL_MAX_DEPTH: PositiveInt = Field(
  273. description="Maximum allowed depth for nested workflow calls",
  274. default=5,
  275. )
  276. MAX_VARIABLE_SIZE: PositiveInt = Field(
  277. description="Maximum size in bytes for a single variable in workflows. Default to 5KB.",
  278. default=5 * 1024,
  279. )
  280. class AuthConfig(BaseSettings):
  281. """
  282. Configuration for authentication and OAuth
  283. """
  284. OAUTH_REDIRECT_PATH: str = Field(
  285. description="Redirect path for OAuth authentication callbacks",
  286. default="/console/api/oauth/authorize",
  287. )
  288. GITHUB_CLIENT_ID: Optional[str] = Field(
  289. description="GitHub OAuth client ID",
  290. default=None,
  291. )
  292. GITHUB_CLIENT_SECRET: Optional[str] = Field(
  293. description="GitHub OAuth client secret",
  294. default=None,
  295. )
  296. GOOGLE_CLIENT_ID: Optional[str] = Field(
  297. description="Google OAuth client ID",
  298. default=None,
  299. )
  300. GOOGLE_CLIENT_SECRET: Optional[str] = Field(
  301. description="Google OAuth client secret",
  302. default=None,
  303. )
  304. ACCESS_TOKEN_EXPIRE_MINUTES: PositiveInt = Field(
  305. description="Expiration time for access tokens in minutes",
  306. default=60,
  307. )
  308. class ModerationConfig(BaseSettings):
  309. """
  310. Configuration for content moderation
  311. """
  312. MODERATION_BUFFER_SIZE: PositiveInt = Field(
  313. description="Size of the buffer for content moderation processing",
  314. default=300,
  315. )
  316. class ToolConfig(BaseSettings):
  317. """
  318. Configuration for tool management
  319. """
  320. TOOL_ICON_CACHE_MAX_AGE: PositiveInt = Field(
  321. description="Maximum age in seconds for caching tool icons",
  322. default=3600,
  323. )
  324. class MailConfig(BaseSettings):
  325. """
  326. Configuration for email services
  327. """
  328. MAIL_TYPE: Optional[str] = Field(
  329. description="Email service provider type ('smtp' or 'resend'), default to None.",
  330. default=None,
  331. )
  332. MAIL_DEFAULT_SEND_FROM: Optional[str] = Field(
  333. description="Default email address to use as the sender",
  334. default=None,
  335. )
  336. RESEND_API_KEY: Optional[str] = Field(
  337. description="API key for Resend email service",
  338. default=None,
  339. )
  340. RESEND_API_URL: Optional[str] = Field(
  341. description="API URL for Resend email service",
  342. default=None,
  343. )
  344. SMTP_SERVER: Optional[str] = Field(
  345. description="SMTP server hostname",
  346. default=None,
  347. )
  348. SMTP_PORT: Optional[int] = Field(
  349. description="SMTP server port number",
  350. default=465,
  351. )
  352. SMTP_USERNAME: Optional[str] = Field(
  353. description="Username for SMTP authentication",
  354. default=None,
  355. )
  356. SMTP_PASSWORD: Optional[str] = Field(
  357. description="Password for SMTP authentication",
  358. default=None,
  359. )
  360. SMTP_USE_TLS: bool = Field(
  361. description="Enable TLS encryption for SMTP connections",
  362. default=False,
  363. )
  364. SMTP_OPPORTUNISTIC_TLS: bool = Field(
  365. description="Enable opportunistic TLS for SMTP connections",
  366. default=False,
  367. )
  368. class RagEtlConfig(BaseSettings):
  369. """
  370. Configuration for RAG ETL processes
  371. """
  372. ETL_TYPE: str = Field(
  373. description="RAG ETL type ('dify' or 'Unstructured'), default to 'dify'",
  374. default="dify",
  375. )
  376. KEYWORD_DATA_SOURCE_TYPE: str = Field(
  377. description="Data source type for keyword extraction"
  378. " ('database' or other supported types), default to 'database'",
  379. default="database",
  380. )
  381. UNSTRUCTURED_API_URL: Optional[str] = Field(
  382. description="API URL for Unstructured.io service",
  383. default=None,
  384. )
  385. UNSTRUCTURED_API_KEY: Optional[str] = Field(
  386. description="API key for Unstructured.io service",
  387. default=None,
  388. )
  389. class DataSetConfig(BaseSettings):
  390. """
  391. Configuration for dataset management
  392. """
  393. PLAN_SANDBOX_CLEAN_DAY_SETTING: PositiveInt = Field(
  394. description="Interval in days for dataset cleanup operations - plan: sandbox",
  395. default=30,
  396. )
  397. PLAN_PRO_CLEAN_DAY_SETTING: PositiveInt = Field(
  398. description="Interval in days for dataset cleanup operations - plan: pro and team",
  399. default=7,
  400. )
  401. DATASET_OPERATOR_ENABLED: bool = Field(
  402. description="Enable or disable dataset operator functionality",
  403. default=False,
  404. )
  405. class WorkspaceConfig(BaseSettings):
  406. """
  407. Configuration for workspace management
  408. """
  409. INVITE_EXPIRY_HOURS: PositiveInt = Field(
  410. description="Expiration time in hours for workspace invitation links",
  411. default=72,
  412. )
  413. class IndexingConfig(BaseSettings):
  414. """
  415. Configuration for indexing operations
  416. """
  417. INDEXING_MAX_SEGMENTATION_TOKENS_LENGTH: PositiveInt = Field(
  418. description="Maximum token length for text segmentation during indexing",
  419. default=1000,
  420. )
  421. class ImageFormatConfig(BaseSettings):
  422. MULTIMODAL_SEND_IMAGE_FORMAT: str = Field(
  423. description="Format for sending images in multimodal contexts ('base64' or 'url'), default is base64",
  424. default="base64",
  425. )
  426. class CeleryBeatConfig(BaseSettings):
  427. CELERY_BEAT_SCHEDULER_TIME: int = Field(
  428. description="Interval in days for Celery Beat scheduler execution, default to 1 day",
  429. default=1,
  430. )
  431. class PositionConfig(BaseSettings):
  432. POSITION_PROVIDER_PINS: str = Field(
  433. description="Comma-separated list of pinned model providers",
  434. default="",
  435. )
  436. POSITION_PROVIDER_INCLUDES: str = Field(
  437. description="Comma-separated list of included model providers",
  438. default="",
  439. )
  440. POSITION_PROVIDER_EXCLUDES: str = Field(
  441. description="Comma-separated list of excluded model providers",
  442. default="",
  443. )
  444. POSITION_TOOL_PINS: str = Field(
  445. description="Comma-separated list of pinned tools",
  446. default="",
  447. )
  448. POSITION_TOOL_INCLUDES: str = Field(
  449. description="Comma-separated list of included tools",
  450. default="",
  451. )
  452. POSITION_TOOL_EXCLUDES: str = Field(
  453. description="Comma-separated list of excluded tools",
  454. default="",
  455. )
  456. @computed_field
  457. def POSITION_PROVIDER_PINS_LIST(self) -> list[str]:
  458. return [item.strip() for item in self.POSITION_PROVIDER_PINS.split(",") if item.strip() != ""]
  459. @computed_field
  460. def POSITION_PROVIDER_INCLUDES_SET(self) -> set[str]:
  461. return {item.strip() for item in self.POSITION_PROVIDER_INCLUDES.split(",") if item.strip() != ""}
  462. @computed_field
  463. def POSITION_PROVIDER_EXCLUDES_SET(self) -> set[str]:
  464. return {item.strip() for item in self.POSITION_PROVIDER_EXCLUDES.split(",") if item.strip() != ""}
  465. @computed_field
  466. def POSITION_TOOL_PINS_LIST(self) -> list[str]:
  467. return [item.strip() for item in self.POSITION_TOOL_PINS.split(",") if item.strip() != ""]
  468. @computed_field
  469. def POSITION_TOOL_INCLUDES_SET(self) -> set[str]:
  470. return {item.strip() for item in self.POSITION_TOOL_INCLUDES.split(",") if item.strip() != ""}
  471. @computed_field
  472. def POSITION_TOOL_EXCLUDES_SET(self) -> set[str]:
  473. return {item.strip() for item in self.POSITION_TOOL_EXCLUDES.split(",") if item.strip() != ""}
  474. class FeatureConfig(
  475. # place the configs in alphabet order
  476. AppExecutionConfig,
  477. AuthConfig, # Changed from OAuthConfig to AuthConfig
  478. BillingConfig,
  479. CodeExecutionSandboxConfig,
  480. DataSetConfig,
  481. EndpointConfig,
  482. FileAccessConfig,
  483. FileUploadConfig,
  484. HttpConfig,
  485. ImageFormatConfig,
  486. InnerAPIConfig,
  487. IndexingConfig,
  488. LoggingConfig,
  489. MailConfig,
  490. ModelLoadBalanceConfig,
  491. ModerationConfig,
  492. PositionConfig,
  493. RagEtlConfig,
  494. SecurityConfig,
  495. ToolConfig,
  496. UpdateConfig,
  497. WorkflowConfig,
  498. WorkspaceConfig,
  499. # hosted services config
  500. HostedServiceConfig,
  501. CeleryBeatConfig,
  502. ):
  503. pass