install.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package plugin_manager
  2. import (
  3. "fmt"
  4. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/serverless"
  5. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
  6. "github.com/langgenius/dify-plugin-daemon/internal/db"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/models"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/models/curd"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  12. )
  13. type PluginInstallEvent string
  14. const (
  15. PluginInstallEventInfo PluginInstallEvent = "info"
  16. PluginInstallEventDone PluginInstallEvent = "done"
  17. PluginInstallEventError PluginInstallEvent = "error"
  18. )
  19. type PluginInstallResponse struct {
  20. Event PluginInstallEvent `json:"event"`
  21. Data string `json:"data"`
  22. }
  23. // InstallToAWSFromPkg installs a plugin to AWS Lambda
  24. func (p *PluginManager) InstallToAWSFromPkg(tenant_id string, decoder decoder.PluginDecoder) (
  25. *stream.Stream[PluginInstallResponse], error,
  26. ) {
  27. checksum, err := decoder.Checksum()
  28. if err != nil {
  29. return nil, err
  30. }
  31. declaration, err := decoder.Manifest()
  32. if err != nil {
  33. return nil, err
  34. }
  35. unique_identity, err := decoder.UniqueIdentity()
  36. if err != nil {
  37. return nil, err
  38. }
  39. response, err := serverless.UploadPlugin(decoder)
  40. if err != nil {
  41. return nil, err
  42. }
  43. new_response := stream.NewStream[PluginInstallResponse](128)
  44. routine.Submit(func() {
  45. defer func() {
  46. new_response.Close()
  47. }()
  48. lambda_url := ""
  49. lambda_function_name := ""
  50. response.Async(func(r serverless.LaunchAWSLambdaFunctionResponse) {
  51. if r.Event == serverless.Info {
  52. new_response.Write(PluginInstallResponse{
  53. Event: PluginInstallEventInfo,
  54. Data: "Installing...",
  55. })
  56. } else if r.Event == serverless.Done {
  57. if lambda_url == "" || lambda_function_name == "" {
  58. new_response.Write(PluginInstallResponse{
  59. Event: PluginInstallEventError,
  60. Data: "Internal server error, failed to get lambda url or function name",
  61. })
  62. return
  63. }
  64. // check if the plugin is already installed
  65. _, err := db.GetOne[models.ServerlessRuntime](
  66. db.Equal("checksum", checksum),
  67. db.Equal("type", string(models.SERVERLESS_RUNTIME_TYPE_AWS_LAMBDA)),
  68. )
  69. if err == db.ErrDatabaseNotFound {
  70. // create a new serverless runtime
  71. serverless_model := &models.ServerlessRuntime{
  72. Checksum: checksum,
  73. Type: models.SERVERLESS_RUNTIME_TYPE_AWS_LAMBDA,
  74. FunctionURL: lambda_url,
  75. FunctionName: lambda_function_name,
  76. PluginUniqueIdentifier: unique_identity.String(),
  77. Declaration: declaration,
  78. }
  79. err = db.Create(serverless_model)
  80. if err != nil {
  81. new_response.Write(PluginInstallResponse{
  82. Event: PluginInstallEventError,
  83. Data: "Failed to create serverless runtime",
  84. })
  85. return
  86. }
  87. } else if err != nil {
  88. new_response.Write(PluginInstallResponse{
  89. Event: PluginInstallEventError,
  90. Data: "Failed to check if the plugin is already installed",
  91. })
  92. return
  93. }
  94. _, _, err = curd.CreatePlugin(
  95. tenant_id,
  96. unique_identity,
  97. plugin_entities.PLUGIN_RUNTIME_TYPE_AWS,
  98. &declaration,
  99. )
  100. if err != nil {
  101. new_response.Write(PluginInstallResponse{
  102. Event: PluginInstallEventError,
  103. Data: "Failed to create plugin",
  104. })
  105. return
  106. }
  107. new_response.Write(PluginInstallResponse{
  108. Event: PluginInstallEventDone,
  109. Data: "Installed",
  110. })
  111. } else if r.Event == serverless.Error {
  112. new_response.Write(PluginInstallResponse{
  113. Event: PluginInstallEventError,
  114. Data: "Internal server error",
  115. })
  116. } else if r.Event == serverless.LambdaUrl {
  117. lambda_url = r.Message
  118. } else if r.Event == serverless.Lambda {
  119. lambda_function_name = r.Message
  120. } else {
  121. new_response.WriteError(fmt.Errorf("unknown event: %s, with message: %s", r.Event, r.Message))
  122. }
  123. })
  124. })
  125. return new_response, nil
  126. }
  127. // InstallToLocal installs a plugin to local
  128. func (p *PluginManager) InstallToLocal(tenant_id string, decoder decoder.PluginDecoder) (
  129. *stream.Stream[PluginInstallResponse], error,
  130. ) {
  131. return nil, nil
  132. }