runtime.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. Connector PluginConnector `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. RuntimeState() *PluginRuntimeState
  24. Wait() (<-chan bool, error)
  25. Type() PluginRuntimeType
  26. }
  27. PluginRuntimeSessionIOInterface interface {
  28. Listen(session_id string) *BytesIOListener
  29. Write(session_id string, data []byte)
  30. }
  31. )
  32. func (r *PluginRuntime) Stopped() bool {
  33. return r.State.Status == PLUGIN_RUNTIME_STATUS_STOPPED
  34. }
  35. func (r *PluginRuntime) Stop() {
  36. r.State.Status = PLUGIN_RUNTIME_STATUS_STOPPED
  37. }
  38. func (r *PluginRuntime) Configuration() *plugin_entities.PluginDeclaration {
  39. return &r.Config
  40. }
  41. func (r *PluginRuntime) Identity() (string, error) {
  42. return r.Config.Identity(), nil
  43. }
  44. func (r *PluginRuntime) RuntimeState() *PluginRuntimeState {
  45. return &r.State
  46. }
  47. type PluginRuntimeType string
  48. const (
  49. PLUGIN_RUNTIME_TYPE_LOCAL PluginRuntimeType = "local"
  50. PLUGIN_RUNTIME_TYPE_REMOTE PluginRuntimeType = "remote"
  51. PLUGIN_RUNTIME_TYPE_AWS PluginRuntimeType = "aws"
  52. )
  53. type PluginRuntimeState struct {
  54. Restarts int `json:"restarts"`
  55. Status string `json:"status"`
  56. RelativePath string `json:"relative_path"`
  57. ActiveAt *time.Time `json:"active_at"`
  58. StoppedAt *time.Time `json:"stopped_at"`
  59. Verified bool `json:"verified"`
  60. }
  61. const (
  62. PLUGIN_RUNTIME_STATUS_ACTIVE = "active"
  63. PLUGIN_RUNTIME_STATUS_LAUNCHING = "launching"
  64. PLUGIN_RUNTIME_STATUS_STOPPED = "stopped"
  65. PLUGIN_RUNTIME_STATUS_RESTARTING = "restarting"
  66. PLUGIN_RUNTIME_STATUS_PENDING = "pending"
  67. )
  68. type PluginConnector interface {
  69. OnMessage(func([]byte))
  70. Read([]byte) int
  71. Write([]byte) int
  72. }