session.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. PluginUniqueIdentifier plugin_entities.PluginUniqueIdentifier `json:"plugin_unique_identifier"`
  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_unique_identifier plugin_entities.PluginUniqueIdentifier,
  39. cluster_id string,
  40. invoke_from access_types.PluginAccessType,
  41. action access_types.PluginAccessAction,
  42. declaration *plugin_entities.PluginDeclaration,
  43. ) *Session {
  44. s := &Session{
  45. ID: uuid.New().String(),
  46. TenantID: tenant_id,
  47. UserID: user_id,
  48. PluginUniqueIdentifier: plugin_unique_identifier,
  49. ClusterID: cluster_id,
  50. InvokeFrom: invoke_from,
  51. Action: action,
  52. Declaration: declaration,
  53. }
  54. session_lock.Lock()
  55. sessions[s.ID] = s
  56. session_lock.Unlock()
  57. if err := cache.Store(sessionKey(s.ID), s, time.Minute*30); err != nil {
  58. log.Error("set session info to cache failed, %s", err)
  59. }
  60. return s
  61. }
  62. func GetSession(id string) *Session {
  63. session_lock.RLock()
  64. session := sessions[id]
  65. session_lock.RUnlock()
  66. if session == nil {
  67. // if session not found, it may be generated by another node, try to get it from cache
  68. session, err := cache.Get[Session](sessionKey(id))
  69. if err != nil {
  70. log.Error("get session info from cache failed, %s", err)
  71. return nil
  72. }
  73. return session
  74. }
  75. return session
  76. }
  77. func DeleteSession(id string) {
  78. session_lock.Lock()
  79. delete(sessions, id)
  80. session_lock.Unlock()
  81. if err := cache.Del(sessionKey(id)); err != nil {
  82. log.Error("delete session info from cache failed, %s", err)
  83. }
  84. }
  85. func (s *Session) Close() {
  86. DeleteSession(s.ID)
  87. }
  88. func (s *Session) BindRuntime(runtime plugin_entities.PluginRuntimeInterface) {
  89. s.runtime = runtime
  90. }
  91. func (s *Session) Runtime() plugin_entities.PluginRuntimeInterface {
  92. return s.runtime
  93. }
  94. type PLUGIN_IN_STREAM_EVENT string
  95. const (
  96. PLUGIN_IN_STREAM_EVENT_REQUEST PLUGIN_IN_STREAM_EVENT = "request"
  97. PLUGIN_IN_STREAM_EVENT_RESPONSE PLUGIN_IN_STREAM_EVENT = "backwards_response"
  98. )
  99. func (s *Session) Message(event PLUGIN_IN_STREAM_EVENT, data any) []byte {
  100. return parser.MarshalJsonBytes(map[string]any{
  101. "session_id": s.ID,
  102. "event": event,
  103. "data": data,
  104. })
  105. }
  106. func (s *Session) Write(event PLUGIN_IN_STREAM_EVENT, data any) error {
  107. if s.runtime == nil {
  108. return errors.New("runtime not bound")
  109. }
  110. s.runtime.Write(s.ID, s.Message(event, data))
  111. return nil
  112. }