runtime.go 5.8 KB

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