connector.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "config": map[string]any{
  48. "identity": identity,
  49. "checksum": checksum,
  50. },
  51. }
  52. url, err := url.JoinPath(baseurl.String(), "/v1/lambda/fetch")
  53. if err != nil {
  54. return nil, err
  55. }
  56. response, err := http_requests.PostAndParse[entities.GenericResponse[LambdaFunction]](
  57. client,
  58. url,
  59. http_requests.HttpHeader(map[string]string{
  60. "Authorization": SERVERLESS_CONNECTOR_API_KEY,
  61. }),
  62. http_requests.HttpPayloadJson(request),
  63. )
  64. if err != nil {
  65. return nil, err
  66. }
  67. if response.Code != 0 {
  68. if response.Code == -404 {
  69. return nil, ErrNoLambdaFunction
  70. }
  71. return nil, fmt.Errorf("unexpected response from serverless connector: %s", response.Message)
  72. }
  73. return &response.Data, nil
  74. }
  75. type LaunchAWSLambdaFunctionEvent string
  76. const (
  77. Error LaunchAWSLambdaFunctionEvent = "error"
  78. Info LaunchAWSLambdaFunctionEvent = "info"
  79. Lambda LaunchAWSLambdaFunctionEvent = "lambda"
  80. LambdaUrl LaunchAWSLambdaFunctionEvent = "lambda_url"
  81. Done LaunchAWSLambdaFunctionEvent = "done"
  82. )
  83. type LaunchAWSLambdaFunctionResponse struct {
  84. Event LaunchAWSLambdaFunctionEvent `json:"event"`
  85. Message string `json:"message"`
  86. }
  87. // Launch the lambda function from serverless connector, it will receive the context_tar as the input
  88. // and build it a docker image, then run it on serverless platform like AWS Lambda
  89. // it returns a event stream, the caller should consider it as a async operation
  90. func LaunchLambda(identity string, checksum string, context_tar io.Reader) (*stream.Stream[LaunchAWSLambdaFunctionResponse], error) {
  91. url, err := url.JoinPath(baseurl.String(), "/v1/lambda/launch")
  92. if err != nil {
  93. return nil, err
  94. }
  95. response, err := http_requests.PostAndParseStream[LaunchAWSLambdaFunctionResponse](
  96. client,
  97. url,
  98. http_requests.HttpHeader(map[string]string{
  99. "Authorization": SERVERLESS_CONNECTOR_API_KEY,
  100. }),
  101. http_requests.HttpReadTimeout(300),
  102. http_requests.HttpWriteTimeout(300),
  103. http_requests.HttpPayloadMultipart(
  104. map[string]string{
  105. "identity": identity,
  106. "checksum": checksum,
  107. },
  108. map[string]io.Reader{
  109. "context": context_tar,
  110. },
  111. ),
  112. )
  113. if err != nil {
  114. return nil, err
  115. }
  116. return response, nil
  117. }