manager.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package plugin_manager
  2. import (
  3. "fmt"
  4. "github.com/langgenius/dify-plugin-daemon/internal/cluster"
  5. "github.com/langgenius/dify-plugin-daemon/internal/core/dify_invocation"
  6. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  10. )
  11. type PluginManager struct {
  12. cluster *cluster.Cluster
  13. }
  14. var (
  15. manager *PluginManager
  16. )
  17. func InitGlobalPluginManager(cluster *cluster.Cluster) {
  18. manager = &PluginManager{
  19. cluster: cluster,
  20. }
  21. }
  22. func GetGlobalPluginManager() *PluginManager {
  23. return manager
  24. }
  25. func (p *PluginManager) List() []entities.PluginRuntimeInterface {
  26. var runtimes []entities.PluginRuntimeInterface
  27. m.Range(func(key, value interface{}) bool {
  28. if v, ok := value.(entities.PluginRuntimeInterface); ok {
  29. runtimes = append(runtimes, v)
  30. }
  31. return true
  32. })
  33. return runtimes
  34. }
  35. func (p *PluginManager) Get(identity string) entities.PluginRuntimeInterface {
  36. if v, ok := m.Load(identity); ok {
  37. if r, ok := v.(entities.PluginRuntimeInterface); ok {
  38. return r
  39. }
  40. }
  41. return nil
  42. }
  43. func (p *PluginManager) Put(path string, binary []byte) {
  44. //TODO: put binary into
  45. }
  46. func (p *PluginManager) Delete(identity string) {
  47. //TODO: delete binary from
  48. }
  49. func (p *PluginManager) Init(configuration *app.Config) {
  50. // TODO: init plugin manager
  51. log.Info("start plugin manager daemon...")
  52. // init redis client
  53. if err := cache.InitRedisClient(
  54. fmt.Sprintf("%s:%d", configuration.RedisHost, configuration.RedisPort),
  55. configuration.RedisPass,
  56. ); err != nil {
  57. log.Panic("init redis client failed: %s", err.Error())
  58. }
  59. if err := dify_invocation.InitDifyInvocationDaemon(
  60. configuration.PluginInnerApiURL, configuration.PluginInnerApiKey,
  61. ); err != nil {
  62. log.Panic("init dify invocation daemon failed: %s", err.Error())
  63. }
  64. // start plugin watcher
  65. p.startWatcher(configuration)
  66. }