runtime.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package entities
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "encoding/gob"
  6. "encoding/hex"
  7. "hash/fnv"
  8. "time"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  10. )
  11. type (
  12. PluginRuntime struct {
  13. State PluginRuntimeState `json:"state"`
  14. Config plugin_entities.PluginDeclaration `json:"config"`
  15. onStopped []func() `json:"-"`
  16. }
  17. PluginRuntimeInterface interface {
  18. PluginRuntimeTimeLifeInterface
  19. PluginRuntimeSessionIOInterface
  20. PluginRuntimeDockerInterface
  21. }
  22. PluginRuntimeTimeLifeInterface interface {
  23. // returns the plugin configuration
  24. Configuration() *plugin_entities.PluginDeclaration
  25. // unique identity of the plugin
  26. Identity() (string, error)
  27. // hashed identity of the plugin
  28. HashedIdentity() (string, error)
  29. // before the plugin starts, it will call this method to initialize the environment
  30. InitEnvironment() error
  31. // start the plugin, returns errors if the plugin fails to start and hangs until the plugin stops
  32. StartPlugin() error
  33. // returns true if the plugin is stopped
  34. Stopped() bool
  35. // stop the plugin
  36. Stop()
  37. // add a function to be called when the plugin stops
  38. OnStop(func())
  39. // trigger the stop event
  40. TriggerStop()
  41. // returns the runtime state of the plugin
  42. RuntimeState() PluginRuntimeState
  43. // Update the runtime state of the plugin
  44. UpdateScheduledAt(t time.Time)
  45. // returns the checksum of the plugin
  46. Checksum() string
  47. // wait for the plugin to stop
  48. Wait() (<-chan bool, error)
  49. // returns the runtime type of the plugin
  50. Type() PluginRuntimeType
  51. // set the plugin to active
  52. SetActive()
  53. // set the plugin to launching
  54. SetLaunching()
  55. // set the plugin to restarting
  56. SetRestarting()
  57. // set the plugin to pending
  58. SetPending()
  59. // set the active time of the plugin
  60. SetActiveAt(t time.Time)
  61. // set the scheduled time of the plugin
  62. SetScheduledAt(t time.Time)
  63. // add restarts to the plugin
  64. AddRestarts()
  65. }
  66. PluginRuntimeSessionIOInterface interface {
  67. Listen(session_id string) *BytesIOListener
  68. Write(session_id string, data []byte)
  69. }
  70. PluginRuntimeDockerInterface interface {
  71. DockerImage() (string, error)
  72. }
  73. )
  74. func (r *PluginRuntime) Stopped() bool {
  75. return r.State.Status == PLUGIN_RUNTIME_STATUS_STOPPED
  76. }
  77. func (r *PluginRuntime) Stop() {
  78. r.State.Status = PLUGIN_RUNTIME_STATUS_STOPPED
  79. }
  80. func (r *PluginRuntime) Configuration() *plugin_entities.PluginDeclaration {
  81. return &r.Config
  82. }
  83. func (r *PluginRuntime) Identity() (string, error) {
  84. return r.Config.Identity(), nil
  85. }
  86. func HashedIdentity(identity string) string {
  87. hash := sha256.New()
  88. hash.Write([]byte(identity))
  89. return hex.EncodeToString(hash.Sum(nil))
  90. }
  91. func (r *PluginRuntime) HashedIdentity() (string, error) {
  92. return HashedIdentity(r.Config.Identity()), nil
  93. }
  94. func (r *PluginRuntime) RuntimeState() PluginRuntimeState {
  95. return r.State
  96. }
  97. func (r *PluginRuntime) UpdateScheduledAt(t time.Time) {
  98. r.State.ScheduledAt = &t
  99. }
  100. func (r *PluginRuntime) InitState() {
  101. r.State = PluginRuntimeState{
  102. Restarts: 0,
  103. Status: PLUGIN_RUNTIME_STATUS_PENDING,
  104. ActiveAt: nil,
  105. StoppedAt: nil,
  106. Verified: false,
  107. ScheduledAt: nil,
  108. Logs: []string{},
  109. }
  110. }
  111. func (r *PluginRuntime) SetActive() {
  112. r.State.Status = PLUGIN_RUNTIME_STATUS_ACTIVE
  113. }
  114. func (r *PluginRuntime) SetLaunching() {
  115. r.State.Status = PLUGIN_RUNTIME_STATUS_LAUNCHING
  116. }
  117. func (r *PluginRuntime) SetRestarting() {
  118. r.State.Status = PLUGIN_RUNTIME_STATUS_RESTARTING
  119. }
  120. func (r *PluginRuntime) SetPending() {
  121. r.State.Status = PLUGIN_RUNTIME_STATUS_PENDING
  122. }
  123. func (r *PluginRuntime) SetActiveAt(t time.Time) {
  124. r.State.ActiveAt = &t
  125. }
  126. func (r *PluginRuntime) SetScheduledAt(t time.Time) {
  127. r.State.ScheduledAt = &t
  128. }
  129. func (r *PluginRuntime) AddRestarts() {
  130. r.State.Restarts++
  131. }
  132. func (r *PluginRuntime) OnStop(f func()) {
  133. r.onStopped = append(r.onStopped, f)
  134. }
  135. func (r *PluginRuntime) TriggerStop() {
  136. for _, f := range r.onStopped {
  137. f()
  138. }
  139. }
  140. type PluginRuntimeType string
  141. const (
  142. PLUGIN_RUNTIME_TYPE_LOCAL PluginRuntimeType = "local"
  143. PLUGIN_RUNTIME_TYPE_REMOTE PluginRuntimeType = "remote"
  144. PLUGIN_RUNTIME_TYPE_AWS PluginRuntimeType = "aws"
  145. )
  146. type PluginRuntimeState struct {
  147. Restarts int `json:"restarts"`
  148. Status string `json:"status"`
  149. AbsolutePath string `json:"absolute_path"`
  150. ActiveAt *time.Time `json:"active_at"`
  151. StoppedAt *time.Time `json:"stopped_at"`
  152. Verified bool `json:"verified"`
  153. ScheduledAt *time.Time `json:"scheduled_at"`
  154. Logs []string `json:"logs"`
  155. }
  156. func (s *PluginRuntimeState) Hash() (uint64, error) {
  157. buf := bytes.Buffer{}
  158. enc := gob.NewEncoder(&buf)
  159. err := enc.Encode(s)
  160. if err != nil {
  161. return 0, err
  162. }
  163. j := fnv.New64a()
  164. _, err = j.Write(buf.Bytes())
  165. if err != nil {
  166. return 0, err
  167. }
  168. return j.Sum64(), nil
  169. }
  170. const (
  171. PLUGIN_RUNTIME_STATUS_ACTIVE = "active"
  172. PLUGIN_RUNTIME_STATUS_LAUNCHING = "launching"
  173. PLUGIN_RUNTIME_STATUS_STOPPED = "stopped"
  174. PLUGIN_RUNTIME_STATUS_RESTARTING = "restarting"
  175. PLUGIN_RUNTIME_STATUS_PENDING = "pending"
  176. )