state.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package install_service
  2. import (
  3. "time"
  4. "github.com/langgenius/dify-plugin-daemon/internal/db"
  5. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  6. "github.com/langgenius/dify-plugin-daemon/internal/types/models"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/models/curd"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/strings"
  9. )
  10. func InstallPlugin(
  11. tenant_id string,
  12. user_id string,
  13. runtime entities.PluginRuntimeInterface,
  14. configuration map[string]any,
  15. ) (string, error) {
  16. identity, err := runtime.Identity()
  17. if err != nil {
  18. return "", err
  19. }
  20. plugin := &models.Plugin{
  21. PluginID: identity,
  22. Refers: 0,
  23. Checksum: runtime.Checksum(),
  24. InstallType: runtime.Type(),
  25. ManifestType: runtime.Configuration().Type,
  26. }
  27. plugin, installation, err := curd.CreatePlugin(tenant_id, user_id, plugin, configuration)
  28. if err != nil {
  29. return "", err
  30. }
  31. // check if there is a webhook for the plugin
  32. if runtime.Configuration().Resource.Permission.AllowRegistryWebhook() {
  33. _, err := InstallWebhook(plugin.PluginID, installation.ID, tenant_id, user_id)
  34. if err != nil {
  35. return "", err
  36. }
  37. }
  38. return installation.ID, nil
  39. }
  40. func UninstallPlugin(tenant_id string, installation_id string, runtime entities.PluginRuntimeInterface) error {
  41. identity, err := runtime.Identity()
  42. if err != nil {
  43. return err
  44. }
  45. // delete the plugin from db
  46. resp, err := curd.DeletePlugin(tenant_id, identity, installation_id)
  47. if err != nil {
  48. return err
  49. }
  50. // delete the webhook from db
  51. if runtime.Configuration().Resource.Permission.AllowRegistryWebhook() {
  52. // get the webhook from db
  53. webhook, err := GetWebhook(tenant_id, identity, resp.Installation.ID)
  54. if err != nil && err != db.ErrDatabaseNotFound {
  55. return err
  56. } else if err == db.ErrDatabaseNotFound {
  57. return nil
  58. }
  59. err = UninstallWebhook(webhook)
  60. if err != nil {
  61. return err
  62. }
  63. }
  64. return nil
  65. }
  66. // installs a plugin to db,
  67. // returns the webhook id
  68. func InstallWebhook(plugin_id string, installation_id string, tenant_id string, user_id string) (string, error) {
  69. installation := &models.Webhook{
  70. HookID: strings.RandomString(64),
  71. PluginID: plugin_id,
  72. TenantID: tenant_id,
  73. UserID: user_id,
  74. ExpiredAt: time.Now().Add(time.Hour * 24 * 365 * 10),
  75. PluginInstallationId: installation_id,
  76. }
  77. if err := db.Create(&installation); err != nil {
  78. return "", err
  79. }
  80. return installation.HookID, nil
  81. }
  82. func GetWebhook(tenant_id string, plugin_id string, installation_id string) (*models.Webhook, error) {
  83. webhook, err := db.GetOne[models.Webhook](
  84. db.Equal("tenant_id", tenant_id),
  85. db.Equal("plugin_id", plugin_id),
  86. db.Equal("plugin_installation_id", installation_id),
  87. )
  88. if err != nil {
  89. return nil, err
  90. }
  91. return &webhook, nil
  92. }
  93. // uninstalls a plugin from db
  94. func UninstallWebhook(webhook *models.Webhook) error {
  95. return db.Delete(webhook)
  96. }