io.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package serverless_runtime
  2. import (
  3. "bufio"
  4. "bytes"
  5. "context"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "time"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  12. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  13. "github.com/langgenius/dify-plugin-daemon/pkg/entities"
  14. "github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
  15. )
  16. func (r *AWSPluginRuntime) Listen(sessionId string) *entities.Broadcast[plugin_entities.SessionMessage] {
  17. l := entities.NewBroadcast[plugin_entities.SessionMessage]()
  18. // store the listener
  19. r.listeners.Store(sessionId, l)
  20. return l
  21. }
  22. // For AWS Lambda, write is equivalent to http request, it's not a normal stream like stdio and tcp
  23. func (r *AWSPluginRuntime) Write(sessionId string, data []byte) {
  24. l, ok := r.listeners.Load(sessionId)
  25. if !ok {
  26. log.Error("session %s not found", sessionId)
  27. return
  28. }
  29. url, err := url.JoinPath(r.LambdaURL, "invoke")
  30. if err != nil {
  31. l.Send(plugin_entities.SessionMessage{
  32. Type: plugin_entities.SESSION_MESSAGE_TYPE_ERROR,
  33. Data: parser.MarshalJsonBytes(plugin_entities.ErrorResponse{
  34. ErrorType: "PluginDaemonInnerError",
  35. Message: fmt.Sprintf("Error creating request: %v", err),
  36. }),
  37. })
  38. l.Close()
  39. r.Error(fmt.Sprintf("Error creating request: %v", err))
  40. return
  41. }
  42. connectTime := 240 * time.Second
  43. // create a new http request
  44. ctx, cancel := context.WithTimeout(context.Background(), connectTime)
  45. time.AfterFunc(connectTime, cancel)
  46. req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(data))
  47. if err != nil {
  48. r.Error(fmt.Sprintf("Error creating request: %v", err))
  49. return
  50. }
  51. req.Header.Set("Content-Type", "application/json")
  52. req.Header.Set("Accept", "text/event-stream")
  53. req.Header.Set("Dify-Plugin-Session-ID", sessionId)
  54. routine.Submit(map[string]string{
  55. "module": "serverless_runtime",
  56. "function": "Write",
  57. "session_id": sessionId,
  58. "lambda_url": r.LambdaURL,
  59. }, func() {
  60. // remove the session from listeners
  61. defer r.listeners.Delete(sessionId)
  62. defer l.Close()
  63. defer l.Send(plugin_entities.SessionMessage{
  64. Type: plugin_entities.SESSION_MESSAGE_TYPE_END,
  65. Data: []byte(""),
  66. })
  67. response, err := r.client.Do(req)
  68. if err != nil {
  69. l.Send(plugin_entities.SessionMessage{
  70. Type: plugin_entities.SESSION_MESSAGE_TYPE_ERROR,
  71. Data: parser.MarshalJsonBytes(plugin_entities.ErrorResponse{
  72. ErrorType: "PluginDaemonInnerError",
  73. Message: fmt.Sprintf("Error sending request to aws lambda: %v", err),
  74. }),
  75. })
  76. r.Error(fmt.Sprintf("Error sending request to aws lambda: %v", err))
  77. return
  78. }
  79. // write to data stream
  80. scanner := bufio.NewScanner(response.Body)
  81. // TODO: set a reasonable buffer size or use a reader, this is a temporary solution
  82. scanner.Buffer(make([]byte, 1024), 5*1024*1024)
  83. sessionAlive := true
  84. for scanner.Scan() && sessionAlive {
  85. bytes := scanner.Bytes()
  86. if len(bytes) == 0 {
  87. continue
  88. }
  89. plugin_entities.ParsePluginUniversalEvent(
  90. bytes,
  91. response.Status,
  92. func(session_id string, data []byte) {
  93. sessionMessage, err := parser.UnmarshalJsonBytes[plugin_entities.SessionMessage](data)
  94. if err != nil {
  95. l.Send(plugin_entities.SessionMessage{
  96. Type: plugin_entities.SESSION_MESSAGE_TYPE_ERROR,
  97. Data: parser.MarshalJsonBytes(plugin_entities.ErrorResponse{
  98. ErrorType: "PluginDaemonInnerError",
  99. Message: fmt.Sprintf("failed to parse session message %s, err: %v", bytes, err),
  100. }),
  101. })
  102. sessionAlive = false
  103. }
  104. l.Send(sessionMessage)
  105. },
  106. func() {},
  107. func(err string) {
  108. l.Send(plugin_entities.SessionMessage{
  109. Type: plugin_entities.SESSION_MESSAGE_TYPE_ERROR,
  110. Data: parser.MarshalJsonBytes(plugin_entities.ErrorResponse{
  111. ErrorType: "PluginDaemonInnerError",
  112. Message: fmt.Sprintf("encountered an error: %v", err),
  113. }),
  114. })
  115. },
  116. func(message string) {},
  117. )
  118. }
  119. if err := scanner.Err(); err != nil {
  120. l.Send(plugin_entities.SessionMessage{
  121. Type: plugin_entities.SESSION_MESSAGE_TYPE_ERROR,
  122. Data: parser.MarshalJsonBytes(plugin_entities.ErrorResponse{
  123. ErrorType: "PluginDaemonInnerError",
  124. Message: fmt.Sprintf("failed to read response body: %v", err),
  125. }),
  126. })
  127. }
  128. })
  129. }