runtime.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package entities
  2. import (
  3. "time"
  4. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  5. )
  6. type (
  7. PluginRuntime struct {
  8. State PluginRuntimeState `json:"state"`
  9. Config plugin_entities.PluginDeclaration `json:"config"`
  10. onStopped []func() `json:"-"`
  11. }
  12. PluginRuntimeInterface interface {
  13. PluginRuntimeTimeLifeInterface
  14. PluginRuntimeSessionIOInterface
  15. }
  16. PluginRuntimeTimeLifeInterface interface {
  17. Configuration() *plugin_entities.PluginDeclaration
  18. Identity() (string, error)
  19. InitEnvironment() error
  20. StartPlugin() error
  21. Stopped() bool
  22. Stop()
  23. OnStop(func())
  24. TriggerStop()
  25. RuntimeState() *PluginRuntimeState
  26. Wait() (<-chan bool, error)
  27. Type() PluginRuntimeType
  28. }
  29. PluginRuntimeSessionIOInterface interface {
  30. Listen(session_id string) *BytesIOListener
  31. Write(session_id string, data []byte)
  32. }
  33. )
  34. func (r *PluginRuntime) Stopped() bool {
  35. return r.State.Status == PLUGIN_RUNTIME_STATUS_STOPPED
  36. }
  37. func (r *PluginRuntime) Stop() {
  38. r.State.Status = PLUGIN_RUNTIME_STATUS_STOPPED
  39. }
  40. func (r *PluginRuntime) Configuration() *plugin_entities.PluginDeclaration {
  41. return &r.Config
  42. }
  43. func (r *PluginRuntime) Identity() (string, error) {
  44. return r.Config.Identity(), nil
  45. }
  46. func (r *PluginRuntime) RuntimeState() *PluginRuntimeState {
  47. return &r.State
  48. }
  49. func (r *PluginRuntime) OnStop(f func()) {
  50. r.onStopped = append(r.onStopped, f)
  51. }
  52. func (r *PluginRuntime) TriggerStop() {
  53. for _, f := range r.onStopped {
  54. f()
  55. }
  56. }
  57. type PluginRuntimeType string
  58. const (
  59. PLUGIN_RUNTIME_TYPE_LOCAL PluginRuntimeType = "local"
  60. PLUGIN_RUNTIME_TYPE_REMOTE PluginRuntimeType = "remote"
  61. PLUGIN_RUNTIME_TYPE_AWS PluginRuntimeType = "aws"
  62. )
  63. type PluginRuntimeState struct {
  64. Restarts int `json:"restarts"`
  65. Status string `json:"status"`
  66. RelativePath string `json:"relative_path"`
  67. ActiveAt *time.Time `json:"active_at"`
  68. StoppedAt *time.Time `json:"stopped_at"`
  69. Verified bool `json:"verified"`
  70. }
  71. const (
  72. PLUGIN_RUNTIME_STATUS_ACTIVE = "active"
  73. PLUGIN_RUNTIME_STATUS_LAUNCHING = "launching"
  74. PLUGIN_RUNTIME_STATUS_STOPPED = "stopped"
  75. PLUGIN_RUNTIME_STATUS_RESTARTING = "restarting"
  76. PLUGIN_RUNTIME_STATUS_PENDING = "pending"
  77. )