You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

factory.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """
  2. DifyAPI Repository Factory for creating repository instances.
  3. This factory is specifically designed for DifyAPI repositories that handle
  4. service-layer operations with dependency injection patterns.
  5. """
  6. import logging
  7. from sqlalchemy.orm import sessionmaker
  8. from configs import dify_config
  9. from core.repositories import DifyCoreRepositoryFactory, RepositoryImportError
  10. from repositories.api_workflow_node_execution_repository import DifyAPIWorkflowNodeExecutionRepository
  11. from repositories.api_workflow_run_repository import APIWorkflowRunRepository
  12. logger = logging.getLogger(__name__)
  13. class DifyAPIRepositoryFactory(DifyCoreRepositoryFactory):
  14. """
  15. Factory for creating DifyAPI repository instances based on configuration.
  16. This factory handles the creation of repositories that are specifically designed
  17. for service-layer operations and use dependency injection with sessionmaker
  18. for better testability and separation of concerns.
  19. """
  20. @classmethod
  21. def create_api_workflow_node_execution_repository(
  22. cls, session_maker: sessionmaker
  23. ) -> DifyAPIWorkflowNodeExecutionRepository:
  24. """
  25. Create a DifyAPIWorkflowNodeExecutionRepository instance based on configuration.
  26. This repository is designed for service-layer operations and uses dependency injection
  27. with a sessionmaker for better testability and separation of concerns. It provides
  28. database access patterns specifically needed by service classes, handling queries
  29. that involve database-specific fields and multi-tenancy concerns.
  30. Args:
  31. session_maker: SQLAlchemy sessionmaker to inject for database session management.
  32. Returns:
  33. Configured DifyAPIWorkflowNodeExecutionRepository instance
  34. Raises:
  35. RepositoryImportError: If the configured repository cannot be imported or instantiated
  36. """
  37. class_path = dify_config.API_WORKFLOW_NODE_EXECUTION_REPOSITORY
  38. try:
  39. repository_class = cls._import_class(class_path)
  40. cls._validate_repository_interface(repository_class, DifyAPIWorkflowNodeExecutionRepository)
  41. # Service repository requires session_maker parameter
  42. cls._validate_constructor_signature(repository_class, ["session_maker"])
  43. return repository_class(session_maker=session_maker) # type: ignore[no-any-return]
  44. except RepositoryImportError:
  45. # Re-raise our custom errors as-is
  46. raise
  47. except Exception as e:
  48. logger.exception("Failed to create DifyAPIWorkflowNodeExecutionRepository")
  49. raise RepositoryImportError(
  50. f"Failed to create DifyAPIWorkflowNodeExecutionRepository from '{class_path}': {e}"
  51. ) from e
  52. @classmethod
  53. def create_api_workflow_run_repository(cls, session_maker: sessionmaker) -> APIWorkflowRunRepository:
  54. """
  55. Create an APIWorkflowRunRepository instance based on configuration.
  56. This repository is designed for service-layer WorkflowRun operations and uses dependency
  57. injection with a sessionmaker for better testability and separation of concerns. It provides
  58. database access patterns specifically needed by service classes for workflow run management,
  59. including pagination, filtering, and bulk operations.
  60. Args:
  61. session_maker: SQLAlchemy sessionmaker to inject for database session management.
  62. Returns:
  63. Configured APIWorkflowRunRepository instance
  64. Raises:
  65. RepositoryImportError: If the configured repository cannot be imported or instantiated
  66. """
  67. class_path = dify_config.API_WORKFLOW_RUN_REPOSITORY
  68. try:
  69. repository_class = cls._import_class(class_path)
  70. cls._validate_repository_interface(repository_class, APIWorkflowRunRepository)
  71. # Service repository requires session_maker parameter
  72. cls._validate_constructor_signature(repository_class, ["session_maker"])
  73. return repository_class(session_maker=session_maker) # type: ignore[no-any-return]
  74. except RepositoryImportError:
  75. # Re-raise our custom errors as-is
  76. raise
  77. except Exception as e:
  78. logger.exception("Failed to create APIWorkflowRunRepository")
  79. raise RepositoryImportError(f"Failed to create APIWorkflowRunRepository from '{class_path}': {e}") from e