watcher.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package plugin_manager
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path"
  8. "strings"
  9. "time"
  10. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/basic_manager"
  11. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/local_manager"
  12. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/positive_manager"
  13. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/remote_manager"
  14. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
  15. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/verifier"
  16. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  17. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  18. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  19. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  20. )
  21. func (p *PluginManager) startLocalWatcher(config *app.Config) {
  22. go func() {
  23. log.Info("start to handle new plugins in path: %s", config.PluginStoragePath)
  24. p.handleNewLocalPlugins(config)
  25. for range time.NewTicker(time.Second * 30).C {
  26. p.handleNewLocalPlugins(config)
  27. }
  28. }()
  29. }
  30. func (p *PluginManager) startRemoteWatcher(config *app.Config) {
  31. // launch TCP debugging server if enabled
  32. if config.PluginRemoteInstallingEnabled {
  33. server := remote_manager.NewRemotePluginServer(config, p.mediaManager)
  34. go func() {
  35. err := server.Launch()
  36. if err != nil {
  37. log.Error("start remote plugin server failed: %s", err.Error())
  38. }
  39. }()
  40. go func() {
  41. server.Wrap(func(rpr *remote_manager.RemotePluginRuntime) {
  42. routine.Submit(func() {
  43. defer func() {
  44. if err := recover(); err != nil {
  45. log.Error("plugin runtime error: %v", err)
  46. }
  47. }()
  48. p.fullDuplexLifetime(rpr)
  49. })
  50. })
  51. }()
  52. }
  53. }
  54. func (p *PluginManager) handleNewLocalPlugins(config *app.Config) {
  55. // load local plugins firstly
  56. for plugin := range p.loadNewLocalPlugins(config.PluginStoragePath) {
  57. // get assets
  58. assets, err := plugin.Decoder.Assets()
  59. if err != nil {
  60. log.Error("get plugin assets error: %v", err)
  61. continue
  62. }
  63. local_plugin_runtime := local_manager.NewLocalPluginRuntime()
  64. local_plugin_runtime.PluginRuntime = plugin.Runtime
  65. local_plugin_runtime.PositivePluginRuntime = positive_manager.PositivePluginRuntime{
  66. BasicPluginRuntime: basic_manager.NewBasicPluginRuntime(p.mediaManager),
  67. LocalPackagePath: plugin.Runtime.State.AbsolutePath,
  68. WorkingPath: plugin.Runtime.State.WorkingPath,
  69. Decoder: plugin.Decoder,
  70. }
  71. if err := local_plugin_runtime.RemapAssets(
  72. &local_plugin_runtime.Config,
  73. assets,
  74. ); err != nil {
  75. log.Error("remap plugin assets error: %v", err)
  76. continue
  77. }
  78. identity, err := local_plugin_runtime.Identity()
  79. if err != nil {
  80. log.Error("get plugin identity error: %v", err)
  81. continue
  82. }
  83. // store the plugin in the storage, avoid duplicate loading
  84. p.runningPluginInStorage.Store(plugin.Runtime.State.AbsolutePath, identity.String())
  85. // local plugin
  86. routine.Submit(func() {
  87. defer func() {
  88. if r := recover(); r != nil {
  89. log.Error("plugin runtime error: %v", r)
  90. }
  91. }()
  92. // delete the plugin from the storage when the plugin is stopped
  93. defer p.runningPluginInStorage.Delete(plugin.Runtime.State.AbsolutePath)
  94. p.fullDuplexLifetime(local_plugin_runtime)
  95. })
  96. }
  97. }
  98. type pluginRuntimeWithDecoder struct {
  99. Runtime plugin_entities.PluginRuntime
  100. Decoder decoder.PluginDecoder
  101. }
  102. // chan should be closed after using that
  103. func (p *PluginManager) loadNewLocalPlugins(root_path string) <-chan *pluginRuntimeWithDecoder {
  104. ch := make(chan *pluginRuntimeWithDecoder)
  105. plugins, err := os.ReadDir(root_path)
  106. if err != nil {
  107. log.Error("no plugin found in path: %s", root_path)
  108. close(ch)
  109. return ch
  110. }
  111. routine.Submit(func() {
  112. for _, plugin := range plugins {
  113. if !plugin.IsDir() {
  114. abs_path := path.Join(root_path, plugin.Name())
  115. if _, ok := p.runningPluginInStorage.Load(abs_path); ok {
  116. // if the plugin is already running, skip it
  117. continue
  118. }
  119. plugin, err := p.loadPlugin(abs_path)
  120. if err != nil {
  121. log.Error("load plugin error: %v", err)
  122. continue
  123. }
  124. ch <- plugin
  125. }
  126. }
  127. close(ch)
  128. })
  129. return ch
  130. }
  131. func (p *PluginManager) loadPlugin(plugin_path string) (*pluginRuntimeWithDecoder, error) {
  132. pack, err := os.Open(plugin_path)
  133. if err != nil {
  134. return nil, errors.Join(err, fmt.Errorf("open plugin package error"))
  135. }
  136. defer pack.Close()
  137. if info, err := pack.Stat(); err != nil {
  138. return nil, errors.Join(err, fmt.Errorf("get plugin package info error"))
  139. } else if info.Size() > p.maxPluginPackageSize {
  140. log.Error("plugin package size is too large: %d", info.Size())
  141. return nil, err
  142. }
  143. plugin_zip, err := io.ReadAll(pack)
  144. if err != nil {
  145. return nil, errors.Join(err, fmt.Errorf("read plugin package error"))
  146. }
  147. decoder, err := decoder.NewZipPluginDecoder(plugin_zip)
  148. if err != nil {
  149. return nil, errors.Join(err, fmt.Errorf("create plugin decoder error"))
  150. }
  151. // get manifest
  152. manifest, err := decoder.Manifest()
  153. if err != nil {
  154. return nil, errors.Join(err, fmt.Errorf("get plugin manifest error"))
  155. }
  156. // check if already exists
  157. if _, exist := p.m.Load(manifest.Identity()); exist {
  158. return nil, errors.Join(fmt.Errorf("plugin already exists: %s", manifest.Identity()), err)
  159. }
  160. checksum, err := decoder.Checksum()
  161. if err != nil {
  162. return nil, errors.Join(err, fmt.Errorf("calculate checksum error"))
  163. }
  164. identity := manifest.Identity()
  165. // replace : with -
  166. identity = strings.ReplaceAll(identity, ":", "-")
  167. plugin_working_path := path.Join(p.workingDirectory, fmt.Sprintf("%s@%s", identity, checksum))
  168. // check if working directory exists
  169. if _, err := os.Stat(plugin_working_path); err == nil {
  170. return nil, errors.Join(fmt.Errorf("plugin working directory already exists: %s", plugin_working_path), err)
  171. }
  172. // extract to working directory
  173. if err := decoder.ExtractTo(plugin_working_path); err != nil {
  174. return nil, errors.Join(fmt.Errorf("extract plugin to working directory error: %v", err), err)
  175. }
  176. return &pluginRuntimeWithDecoder{
  177. Runtime: plugin_entities.PluginRuntime{
  178. Config: manifest,
  179. State: plugin_entities.PluginRuntimeState{
  180. Status: plugin_entities.PLUGIN_RUNTIME_STATUS_PENDING,
  181. Restarts: 0,
  182. AbsolutePath: plugin_path,
  183. WorkingPath: plugin_working_path,
  184. ActiveAt: nil,
  185. Verified: verifier.VerifyPlugin(decoder) == nil,
  186. },
  187. },
  188. Decoder: decoder,
  189. }, nil
  190. }