runtime.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. Type() PluginRuntimeType
  25. }
  26. PluginRuntimeSessionIOInterface interface {
  27. Listen(session_id string) *BytesIOListener
  28. Write(session_id string, data []byte)
  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 PluginRuntimeType string
  44. const (
  45. PLUGIN_RUNTIME_TYPE_LOCAL PluginRuntimeType = "local"
  46. PLUGIN_RUNTIME_TYPE_REMOTE PluginRuntimeType = "remote"
  47. PLUGIN_RUNTIME_TYPE_AWS PluginRuntimeType = "aws"
  48. )
  49. type PluginRuntimeState struct {
  50. Restarts int `json:"restarts"`
  51. Status string `json:"status"`
  52. RelativePath string `json:"relative_path"`
  53. ActiveAt *time.Time `json:"active_at"`
  54. StoppedAt *time.Time `json:"stopped_at"`
  55. Verified bool `json:"verified"`
  56. TenantID string `json:"tenant_id"`
  57. }
  58. const (
  59. PLUGIN_RUNTIME_STATUS_ACTIVE = "active"
  60. PLUGIN_RUNTIME_STATUS_LAUNCHING = "launching"
  61. PLUGIN_RUNTIME_STATUS_STOPPED = "stopped"
  62. PLUGIN_RUNTIME_STATUS_RESTARTING = "restarting"
  63. PLUGIN_RUNTIME_STATUS_PENDING = "pending"
  64. )
  65. type PluginConnector interface {
  66. OnMessage(func([]byte))
  67. Read([]byte) int
  68. Write([]byte) int
  69. }