connector.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package serverless
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/url"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/http_requests"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  10. )
  11. var ()
  12. type LambdaFunction struct {
  13. FunctionName string `json:"function_name" validate:"required"`
  14. FunctionARN string `json:"function_arn" validate:"required"`
  15. FunctionURL string `json:"function_url" validate:"required"`
  16. }
  17. // Ping the serverless connector, return error if failed
  18. func Ping() error {
  19. url, err := url.JoinPath(baseurl.String(), "/ping")
  20. if err != nil {
  21. return err
  22. }
  23. response, err := http_requests.PostAndParse[entities.GenericResponse[string]](
  24. client,
  25. url,
  26. http_requests.HttpHeader(map[string]string{
  27. "Authorization": SERVERLESS_CONNECTOR_API_KEY,
  28. }),
  29. )
  30. if err != nil {
  31. return err
  32. }
  33. if response.Code != 0 {
  34. return fmt.Errorf("unexpected response from serverless connector: %s", response.Message)
  35. }
  36. if response.Data != "pong" {
  37. return fmt.Errorf("unexpected response from serverless connector: %s", response.Data)
  38. }
  39. return nil
  40. }
  41. var (
  42. ErrNoLambdaFunction = errors.New("no lambda function found")
  43. )
  44. // Fetch the lambda function from serverless connector, return error if failed
  45. func FetchLambda(identity string, checksum string) (*LambdaFunction, error) {
  46. request := map[string]any{
  47. "plugin": map[string]any{
  48. "config": map[string]any{
  49. "identity": identity,
  50. "checksum": checksum,
  51. },
  52. },
  53. }
  54. url, err := url.JoinPath(baseurl.String(), "/v1/lambda/fetch")
  55. if err != nil {
  56. return nil, err
  57. }
  58. response, err := http_requests.PostAndParse[entities.GenericResponse[LambdaFunction]](
  59. client,
  60. url,
  61. http_requests.HttpHeader(map[string]string{
  62. "Authorization": SERVERLESS_CONNECTOR_API_KEY,
  63. }),
  64. http_requests.HttpPayloadJson(request),
  65. )
  66. if err != nil {
  67. return nil, err
  68. }
  69. if response.Code != 0 {
  70. if response.Code == -404 {
  71. return nil, ErrNoLambdaFunction
  72. }
  73. return nil, fmt.Errorf("unexpected response from serverless connector: %s", response.Message)
  74. }
  75. return &response.Data, nil
  76. }
  77. type LaunchAWSLambdaFunctionEvent string
  78. const (
  79. Error LaunchAWSLambdaFunctionEvent = "error"
  80. Info LaunchAWSLambdaFunctionEvent = "info"
  81. Lambda LaunchAWSLambdaFunctionEvent = "lambda"
  82. LambdaUrl LaunchAWSLambdaFunctionEvent = "lambda_url"
  83. Done LaunchAWSLambdaFunctionEvent = "done"
  84. )
  85. type LaunchAWSLambdaFunctionResponse struct {
  86. Event LaunchAWSLambdaFunctionEvent `json:"event"`
  87. Message string `json:"message"`
  88. }
  89. // Launch the lambda function from serverless connector, it will receive the context_tar as the input
  90. // and build it a docker image, then run it on serverless platform like AWS Lambda
  91. // it returns a event stream, the caller should consider it as a async operation
  92. func LaunchLambda(identity string, checksum string, context_tar io.Reader) (*stream.Stream[LaunchAWSLambdaFunctionResponse], error) {
  93. url, err := url.JoinPath(baseurl.String(), "/v1/lambda/launch")
  94. if err != nil {
  95. return nil, err
  96. }
  97. response, err := http_requests.PostAndParseStream[LaunchAWSLambdaFunctionResponse](
  98. client,
  99. url,
  100. http_requests.HttpHeader(map[string]string{
  101. "Authorization": SERVERLESS_CONNECTOR_API_KEY,
  102. }),
  103. http_requests.HttpReadTimeout(300),
  104. http_requests.HttpWriteTimeout(300),
  105. http_requests.HttpPayloadMultipart(
  106. map[string]string{
  107. "identity": identity,
  108. "checksum": checksum,
  109. },
  110. map[string]io.Reader{
  111. "context": context_tar,
  112. },
  113. ),
  114. http_requests.HttpRaiseErrorWhenStreamDataNotMatch(true),
  115. )
  116. if err != nil {
  117. return nil, err
  118. }
  119. return response, nil
  120. }