runtime.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. InitEnvironment() error
  18. StartPlugin() error
  19. Stopped() bool
  20. Stop()
  21. Configuration() *plugin_entities.PluginDeclaration
  22. RuntimeState() *PluginRuntimeState
  23. Wait() (<-chan bool, error)
  24. }
  25. PluginRuntimeSessionIOInterface interface {
  26. Listen(session_id string) *BytesIOListener
  27. Write(session_id string, data []byte)
  28. Request(session_id string, data []byte) ([]byte, error)
  29. }
  30. )
  31. func (r *PluginRuntime) Stopped() bool {
  32. return r.State.Status == PLUGIN_RUNTIME_STATUS_STOPPED
  33. }
  34. func (r *PluginRuntime) Stop() {
  35. r.State.Status = PLUGIN_RUNTIME_STATUS_STOPPED
  36. }
  37. func (r *PluginRuntime) Configuration() *plugin_entities.PluginDeclaration {
  38. return &r.Config
  39. }
  40. func (r *PluginRuntime) RuntimeState() *PluginRuntimeState {
  41. return &r.State
  42. }
  43. type PluginRuntimeState struct {
  44. Restarts int `json:"restarts"`
  45. Status string `json:"status"`
  46. RelativePath string `json:"relative_path"`
  47. ActiveAt *time.Time `json:"active_at"`
  48. StoppedAt *time.Time `json:"stopped_at"`
  49. Verified bool `json:"verified"`
  50. }
  51. const (
  52. PLUGIN_RUNTIME_STATUS_ACTIVE = "active"
  53. PLUGIN_RUNTIME_STATUS_LAUNCHING = "launching"
  54. PLUGIN_RUNTIME_STATUS_STOPPED = "stopped"
  55. PLUGIN_RUNTIME_STATUS_RESTARTING = "restarting"
  56. PLUGIN_RUNTIME_STATUS_PENDING = "pending"
  57. )
  58. type PluginConnector interface {
  59. OnMessage(func([]byte))
  60. Read([]byte) int
  61. Write([]byte) int
  62. }