session.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package session_manager
  2. import (
  3. "errors"
  4. "fmt"
  5. "sync"
  6. "time"
  7. "github.com/google/uuid"
  8. "github.com/langgenius/dify-plugin-daemon/internal/core/persistence"
  9. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/access_types"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
  12. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  13. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  14. )
  15. var (
  16. sessions map[string]*Session = map[string]*Session{}
  17. session_lock sync.RWMutex
  18. )
  19. // session need to implement the backwards_invocation.BackwardsInvocationWriter interface
  20. type Session struct {
  21. ID string `json:"id"`
  22. runtime plugin_entities.PluginRuntimeInterface `json:"-"`
  23. persistence *persistence.Persistence `json:"-"`
  24. TenantID string `json:"tenant_id"`
  25. UserID string `json:"user_id"`
  26. PluginIdentity string `json:"plugin_identity"`
  27. ClusterID string `json:"cluster_id"`
  28. InvokeFrom access_types.PluginAccessType `json:"invoke_from"`
  29. Action access_types.PluginAccessAction `json:"action"`
  30. Declaration *plugin_entities.PluginDeclaration `json:"declaration"`
  31. }
  32. func sessionKey(id string) string {
  33. return fmt.Sprintf("session_info:%s", id)
  34. }
  35. func NewSession(
  36. tenant_id string,
  37. user_id string,
  38. plugin_identity string,
  39. cluster_id string,
  40. invoke_from access_types.PluginAccessType,
  41. action access_types.PluginAccessAction,
  42. declaration *plugin_entities.PluginDeclaration,
  43. persistence *persistence.Persistence,
  44. ) *Session {
  45. s := &Session{
  46. ID: uuid.New().String(),
  47. TenantID: tenant_id,
  48. UserID: user_id,
  49. PluginIdentity: plugin_identity,
  50. ClusterID: cluster_id,
  51. InvokeFrom: invoke_from,
  52. Action: action,
  53. Declaration: declaration,
  54. persistence: persistence,
  55. }
  56. session_lock.Lock()
  57. sessions[s.ID] = s
  58. session_lock.Unlock()
  59. if err := cache.Store(sessionKey(s.ID), s, time.Minute*30); err != nil {
  60. log.Error("set session info to cache failed, %s", err)
  61. }
  62. return s
  63. }
  64. func GetSession(id string) *Session {
  65. session_lock.RLock()
  66. defer session_lock.RUnlock()
  67. return sessions[id]
  68. }
  69. func DeleteSession(id string) {
  70. session_lock.Lock()
  71. delete(sessions, id)
  72. session_lock.Unlock()
  73. if err := cache.Del(sessionKey(id)); err != nil {
  74. log.Error("delete session info from cache failed, %s", err)
  75. }
  76. }
  77. func (s *Session) Close() {
  78. DeleteSession(s.ID)
  79. }
  80. func (s *Session) BindRuntime(runtime plugin_entities.PluginRuntimeInterface) {
  81. s.runtime = runtime
  82. }
  83. func (s *Session) Runtime() plugin_entities.PluginRuntimeInterface {
  84. return s.runtime
  85. }
  86. func (s *Session) Storage() *persistence.Persistence {
  87. return s.persistence
  88. }
  89. type PLUGIN_IN_STREAM_EVENT string
  90. const (
  91. PLUGIN_IN_STREAM_EVENT_REQUEST PLUGIN_IN_STREAM_EVENT = "request"
  92. PLUGIN_IN_STREAM_EVENT_RESPONSE PLUGIN_IN_STREAM_EVENT = "backwards_response"
  93. )
  94. func (s *Session) Message(event PLUGIN_IN_STREAM_EVENT, data any) []byte {
  95. return parser.MarshalJsonBytes(map[string]any{
  96. "session_id": s.ID,
  97. "event": event,
  98. "data": data,
  99. })
  100. }
  101. func (s *Session) Write(event PLUGIN_IN_STREAM_EVENT, data any) error {
  102. if s.runtime == nil {
  103. return errors.New("runtime not bound")
  104. }
  105. s.runtime.Write(s.ID, s.Message(event, data))
  106. return nil
  107. }