redis.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package helper
  2. import (
  3. "errors"
  4. "strings"
  5. "github.com/langgenius/dify-plugin-daemon/internal/db"
  6. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/models"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
  9. )
  10. var (
  11. ErrPluginNotFound = errors.New("plugin not found")
  12. )
  13. func CombinedGetPluginDeclaration(
  14. plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
  15. tenant_id string,
  16. runtime_type plugin_entities.PluginRuntimeType,
  17. ) (*plugin_entities.PluginDeclaration, error) {
  18. return cache.AutoGetWithGetter(
  19. strings.Join(
  20. []string{
  21. string(runtime_type),
  22. plugin_unique_identifier.String(),
  23. },
  24. ":",
  25. ),
  26. func() (*plugin_entities.PluginDeclaration, error) {
  27. if runtime_type != plugin_entities.PLUGIN_RUNTIME_TYPE_REMOTE {
  28. declaration, err := db.GetOne[models.PluginDeclaration](
  29. db.Equal("plugin_unique_identifier", plugin_unique_identifier.String()),
  30. )
  31. if err == db.ErrDatabaseNotFound {
  32. return nil, ErrPluginNotFound
  33. }
  34. if err != nil {
  35. return nil, err
  36. }
  37. return &declaration.Declaration, nil
  38. } else {
  39. // try to fetch the declaration from plugin if it's remote
  40. plugin, err := db.GetOne[models.Plugin](
  41. db.Equal("plugin_unique_identifier", plugin_unique_identifier.String()),
  42. db.Equal("install_type", string(plugin_entities.PLUGIN_RUNTIME_TYPE_REMOTE)),
  43. )
  44. if err == db.ErrDatabaseNotFound {
  45. return nil, ErrPluginNotFound
  46. }
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &plugin.Declaration, nil
  51. }
  52. },
  53. )
  54. }