runtime.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package entities
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "encoding/binary"
  6. "encoding/gob"
  7. "encoding/hex"
  8. "hash/fnv"
  9. "time"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  12. )
  13. type (
  14. PluginRuntime struct {
  15. State PluginRuntimeState `json:"state"`
  16. Config plugin_entities.PluginDeclaration `json:"config"`
  17. onStopped []func() `json:"-"`
  18. }
  19. PluginRuntimeInterface interface {
  20. PluginRuntimeTimeLifeInterface
  21. PluginRuntimeSessionIOInterface
  22. }
  23. PluginRuntimeTimeLifeInterface interface {
  24. Configuration() *plugin_entities.PluginDeclaration
  25. Identity() (string, error)
  26. InitEnvironment() error
  27. StartPlugin() error
  28. Stopped() bool
  29. Stop()
  30. OnStop(func())
  31. TriggerStop()
  32. RuntimeState() *PluginRuntimeState
  33. Checksum() string
  34. Wait() (<-chan bool, error)
  35. Type() PluginRuntimeType
  36. }
  37. PluginRuntimeSessionIOInterface interface {
  38. Listen(session_id string) *BytesIOListener
  39. Write(session_id string, data []byte)
  40. }
  41. )
  42. func (r *PluginRuntime) Stopped() bool {
  43. return r.State.Status == PLUGIN_RUNTIME_STATUS_STOPPED
  44. }
  45. func (r *PluginRuntime) Stop() {
  46. r.State.Status = PLUGIN_RUNTIME_STATUS_STOPPED
  47. }
  48. func (r *PluginRuntime) Configuration() *plugin_entities.PluginDeclaration {
  49. return &r.Config
  50. }
  51. func (r *PluginRuntime) Identity() (string, error) {
  52. return r.Config.Identity(), nil
  53. }
  54. func (r *PluginRuntime) RuntimeState() *PluginRuntimeState {
  55. return &r.State
  56. }
  57. func (r *PluginRuntime) Checksum() string {
  58. buf := bytes.Buffer{}
  59. binary.Write(&buf, binary.BigEndian, parser.MarshalJsonBytes(r.Config))
  60. hash := sha256.New()
  61. hash.Write(buf.Bytes())
  62. return hex.EncodeToString(hash.Sum(nil))
  63. }
  64. func (r *PluginRuntime) OnStop(f func()) {
  65. r.onStopped = append(r.onStopped, f)
  66. }
  67. func (r *PluginRuntime) TriggerStop() {
  68. for _, f := range r.onStopped {
  69. f()
  70. }
  71. }
  72. type PluginRuntimeType string
  73. const (
  74. PLUGIN_RUNTIME_TYPE_LOCAL PluginRuntimeType = "local"
  75. PLUGIN_RUNTIME_TYPE_REMOTE PluginRuntimeType = "remote"
  76. PLUGIN_RUNTIME_TYPE_AWS PluginRuntimeType = "aws"
  77. )
  78. type PluginRuntimeState struct {
  79. Identity string `json:"identity"`
  80. Restarts int `json:"restarts"`
  81. Status string `json:"status"`
  82. RelativePath string `json:"relative_path"`
  83. ActiveAt *time.Time `json:"active_at"`
  84. StoppedAt *time.Time `json:"stopped_at"`
  85. Verified bool `json:"verified"`
  86. }
  87. func (s *PluginRuntimeState) Hash() (uint64, error) {
  88. buf := bytes.Buffer{}
  89. enc := gob.NewEncoder(&buf)
  90. err := enc.Encode(s)
  91. if err != nil {
  92. return 0, err
  93. }
  94. j := fnv.New64a()
  95. _, err = j.Write(buf.Bytes())
  96. if err != nil {
  97. return 0, err
  98. }
  99. return j.Sum64(), nil
  100. }
  101. const (
  102. PLUGIN_RUNTIME_STATUS_ACTIVE = "active"
  103. PLUGIN_RUNTIME_STATUS_LAUNCHING = "launching"
  104. PLUGIN_RUNTIME_STATUS_STOPPED = "stopped"
  105. PLUGIN_RUNTIME_STATUS_RESTARTING = "restarting"
  106. PLUGIN_RUNTIME_STATUS_PENDING = "pending"
  107. )