您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

factory.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. Repository factory for dynamically creating repository instances based on configuration.
  3. This module provides a Django-like settings system for repository implementations,
  4. allowing users to configure different repository backends through string paths.
  5. """
  6. from typing import Union
  7. from sqlalchemy.engine import Engine
  8. from sqlalchemy.orm import sessionmaker
  9. from configs import dify_config
  10. from core.workflow.repositories.workflow_execution_repository import WorkflowExecutionRepository
  11. from core.workflow.repositories.workflow_node_execution_repository import WorkflowNodeExecutionRepository
  12. from libs.module_loading import import_string
  13. from models import Account, EndUser
  14. from models.enums import WorkflowRunTriggeredFrom
  15. from models.workflow import WorkflowNodeExecutionTriggeredFrom
  16. class RepositoryImportError(Exception):
  17. """Raised when a repository implementation cannot be imported or instantiated."""
  18. pass
  19. class DifyCoreRepositoryFactory:
  20. """
  21. Factory for creating repository instances based on configuration.
  22. This factory supports Django-like settings where repository implementations
  23. are specified as module paths (e.g., 'module.submodule.ClassName').
  24. """
  25. @classmethod
  26. def create_workflow_execution_repository(
  27. cls,
  28. session_factory: Union[sessionmaker, Engine],
  29. user: Union[Account, EndUser],
  30. app_id: str,
  31. triggered_from: WorkflowRunTriggeredFrom,
  32. ) -> WorkflowExecutionRepository:
  33. """
  34. Create a WorkflowExecutionRepository instance based on configuration.
  35. Args:
  36. session_factory: SQLAlchemy sessionmaker or engine
  37. user: Account or EndUser object
  38. app_id: Application ID
  39. triggered_from: Source of the execution trigger
  40. Returns:
  41. Configured WorkflowExecutionRepository instance
  42. Raises:
  43. RepositoryImportError: If the configured repository cannot be created
  44. """
  45. class_path = dify_config.CORE_WORKFLOW_EXECUTION_REPOSITORY
  46. try:
  47. repository_class = import_string(class_path)
  48. return repository_class( # type: ignore[no-any-return]
  49. session_factory=session_factory,
  50. user=user,
  51. app_id=app_id,
  52. triggered_from=triggered_from,
  53. )
  54. except (ImportError, Exception) as e:
  55. raise RepositoryImportError(f"Failed to create WorkflowExecutionRepository from '{class_path}': {e}") from e
  56. @classmethod
  57. def create_workflow_node_execution_repository(
  58. cls,
  59. session_factory: Union[sessionmaker, Engine],
  60. user: Union[Account, EndUser],
  61. app_id: str,
  62. triggered_from: WorkflowNodeExecutionTriggeredFrom,
  63. ) -> WorkflowNodeExecutionRepository:
  64. """
  65. Create a WorkflowNodeExecutionRepository instance based on configuration.
  66. Args:
  67. session_factory: SQLAlchemy sessionmaker or engine
  68. user: Account or EndUser object
  69. app_id: Application ID
  70. triggered_from: Source of the execution trigger
  71. Returns:
  72. Configured WorkflowNodeExecutionRepository instance
  73. Raises:
  74. RepositoryImportError: If the configured repository cannot be created
  75. """
  76. class_path = dify_config.CORE_WORKFLOW_NODE_EXECUTION_REPOSITORY
  77. try:
  78. repository_class = import_string(class_path)
  79. return repository_class( # type: ignore[no-any-return]
  80. session_factory=session_factory,
  81. user=user,
  82. app_id=app_id,
  83. triggered_from=triggered_from,
  84. )
  85. except (ImportError, Exception) as e:
  86. raise RepositoryImportError(
  87. f"Failed to create WorkflowNodeExecutionRepository from '{class_path}': {e}"
  88. ) from e