manager.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, configuration *app.Config) {
  18. manager = &PluginManager{
  19. cluster: cluster,
  20. }
  21. manager.Init(configuration)
  22. }
  23. func GetGlobalPluginManager() *PluginManager {
  24. return manager
  25. }
  26. func (p *PluginManager) List() []entities.PluginRuntimeInterface {
  27. var runtimes []entities.PluginRuntimeInterface
  28. m.Range(func(key, value interface{}) bool {
  29. if v, ok := value.(entities.PluginRuntimeInterface); ok {
  30. runtimes = append(runtimes, v)
  31. }
  32. return true
  33. })
  34. return runtimes
  35. }
  36. func (p *PluginManager) Get(identity string) entities.PluginRuntimeInterface {
  37. if v, ok := m.Load(identity); ok {
  38. if r, ok := v.(entities.PluginRuntimeInterface); ok {
  39. return r
  40. }
  41. }
  42. return nil
  43. }
  44. func (p *PluginManager) Put(path string, binary []byte) {
  45. //TODO: put binary into
  46. }
  47. func (p *PluginManager) Delete(identity string) {
  48. //TODO: delete binary from
  49. }
  50. func (p *PluginManager) Init(configuration *app.Config) {
  51. // TODO: init plugin manager
  52. log.Info("start plugin manager daemon...")
  53. // init redis client
  54. if err := cache.InitRedisClient(
  55. fmt.Sprintf("%s:%d", configuration.RedisHost, configuration.RedisPort),
  56. configuration.RedisPass,
  57. ); err != nil {
  58. log.Panic("init redis client failed: %s", err.Error())
  59. }
  60. if err := dify_invocation.InitDifyInvocationDaemon(
  61. configuration.PluginInnerApiURL, configuration.PluginInnerApiKey,
  62. ); err != nil {
  63. log.Panic("init dify invocation daemon failed: %s", err.Error())
  64. }
  65. // start plugin watcher
  66. p.startWatcher(configuration)
  67. }