io.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package aws_manager
  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/types/entities"
  11. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  12. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  13. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  14. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  15. )
  16. func (r *AWSPluginRuntime) Listen(session_id string) *entities.Broadcast[plugin_entities.SessionMessage] {
  17. l := entities.NewBroadcast[plugin_entities.SessionMessage]()
  18. // store the listener
  19. r.listeners.Store(session_id, 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(session_id string, data []byte) {
  24. l, ok := r.listeners.Load(session_id)
  25. if !ok {
  26. log.Error("session %s not found", session_id)
  27. return
  28. }
  29. url, err := url.JoinPath(r.lambda_url, "invoke")
  30. if err != nil {
  31. r.Error(fmt.Sprintf("Error creating request: %v", err))
  32. return
  33. }
  34. // create a new http request
  35. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  36. defer cancel()
  37. req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(data))
  38. if err != nil {
  39. r.Error(fmt.Sprintf("Error creating request: %v", err))
  40. return
  41. }
  42. req.Header.Set("Content-Type", "application/json")
  43. req.Header.Set("Accept", "text/event-stream")
  44. req.Header.Set("Dify-Plugin-Session-ID", session_id)
  45. routine.Submit(func() {
  46. // remove the session from listeners
  47. defer r.listeners.Delete(session_id)
  48. response, err := r.client.Do(req)
  49. if err != nil {
  50. r.Error(fmt.Sprintf("Error sending request to aws lambda: %v", err))
  51. return
  52. }
  53. // write to data stream
  54. scanner := bufio.NewScanner(response.Body)
  55. for scanner.Scan() {
  56. bytes := scanner.Bytes()
  57. if len(bytes) == 0 {
  58. continue
  59. }
  60. data, err := parser.UnmarshalJsonBytes[plugin_entities.SessionMessage](bytes)
  61. if err != nil {
  62. log.Error("unmarshal json failed: %s, failed to parse session message", err.Error())
  63. continue
  64. }
  65. data.RuntimeType = r.Type()
  66. l.Send(data)
  67. }
  68. l.Close()
  69. })
  70. }