Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

factory.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. logger.debug(f"Creating DifyAPIWorkflowNodeExecutionRepository from: {class_path}")
  39. try:
  40. repository_class = cls._import_class(class_path)
  41. cls._validate_repository_interface(repository_class, DifyAPIWorkflowNodeExecutionRepository)
  42. # Service repository requires session_maker parameter
  43. cls._validate_constructor_signature(repository_class, ["session_maker"])
  44. return repository_class(session_maker=session_maker) # type: ignore[no-any-return]
  45. except RepositoryImportError:
  46. # Re-raise our custom errors as-is
  47. raise
  48. except Exception as e:
  49. logger.exception("Failed to create DifyAPIWorkflowNodeExecutionRepository")
  50. raise RepositoryImportError(
  51. f"Failed to create DifyAPIWorkflowNodeExecutionRepository from '{class_path}': {e}"
  52. ) from e
  53. @classmethod
  54. def create_api_workflow_run_repository(cls, session_maker: sessionmaker) -> APIWorkflowRunRepository:
  55. """
  56. Create an APIWorkflowRunRepository instance based on configuration.
  57. This repository is designed for service-layer WorkflowRun operations and uses dependency
  58. injection with a sessionmaker for better testability and separation of concerns. It provides
  59. database access patterns specifically needed by service classes for workflow run management,
  60. including pagination, filtering, and bulk operations.
  61. Args:
  62. session_maker: SQLAlchemy sessionmaker to inject for database session management.
  63. Returns:
  64. Configured APIWorkflowRunRepository instance
  65. Raises:
  66. RepositoryImportError: If the configured repository cannot be imported or instantiated
  67. """
  68. class_path = dify_config.API_WORKFLOW_RUN_REPOSITORY
  69. logger.debug(f"Creating APIWorkflowRunRepository from: {class_path}")
  70. try:
  71. repository_class = cls._import_class(class_path)
  72. cls._validate_repository_interface(repository_class, APIWorkflowRunRepository)
  73. # Service repository requires session_maker parameter
  74. cls._validate_constructor_signature(repository_class, ["session_maker"])
  75. return repository_class(session_maker=session_maker) # type: ignore[no-any-return]
  76. except RepositoryImportError:
  77. # Re-raise our custom errors as-is
  78. raise
  79. except Exception as e:
  80. logger.exception("Failed to create APIWorkflowRunRepository")
  81. raise RepositoryImportError(f"Failed to create APIWorkflowRunRepository from '{class_path}': {e}") from e