runtime.go 1.8 KB

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