endpoint.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/hex"
  6. "sync/atomic"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "github.com/langgenius/dify-plugin-daemon/internal/core/dify_invocation"
  10. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon"
  11. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/access_types"
  12. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
  13. "github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
  14. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  15. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/requests"
  16. "github.com/langgenius/dify-plugin-daemon/internal/types/models"
  17. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  18. )
  19. func Endpoint(
  20. ctx *gin.Context,
  21. endpoint *models.Endpoint,
  22. plugin_installation *models.PluginInstallation,
  23. path string,
  24. ) {
  25. req := ctx.Request.Clone(context.Background())
  26. req.URL.Path = path
  27. var buffer bytes.Buffer
  28. err := req.Write(&buffer)
  29. if err != nil {
  30. ctx.JSON(500, gin.H{"error": err.Error()})
  31. }
  32. // fetch plugin
  33. manager := plugin_manager.GetGlobalPluginManager()
  34. runtime := manager.Get(plugin_installation.PluginIdentity)
  35. if runtime == nil {
  36. ctx.JSON(404, gin.H{"error": "plugin not found"})
  37. return
  38. }
  39. // fetch endpoint declaration
  40. endpoint_declaration := runtime.Configuration().Endpoint
  41. if endpoint_declaration == nil {
  42. ctx.JSON(404, gin.H{"error": "endpoint declaration not found"})
  43. return
  44. }
  45. // decrypt settings
  46. settings, err := dify_invocation.InvokeEncrypt(&dify_invocation.InvokeEncryptRequest{
  47. BaseInvokeDifyRequest: dify_invocation.BaseInvokeDifyRequest{
  48. TenantId: endpoint.TenantID,
  49. UserId: "",
  50. Type: dify_invocation.INVOKE_TYPE_ENCRYPT,
  51. },
  52. InvokeEncryptSchema: dify_invocation.InvokeEncryptSchema{
  53. Opt: dify_invocation.ENCRYPT_OPT_DECRYPT,
  54. Namespace: dify_invocation.ENCRYPT_NAMESPACE_ENDPOINT,
  55. Identity: endpoint.ID,
  56. Data: endpoint.GetSettings(),
  57. Config: endpoint_declaration.Settings,
  58. },
  59. })
  60. if err != nil {
  61. ctx.JSON(500, gin.H{"error": "failed to decrypt data"})
  62. return
  63. }
  64. session := session_manager.NewSession(
  65. endpoint.TenantID,
  66. "",
  67. plugin_entities.PluginIdentity(plugin_installation.PluginIdentity),
  68. ctx.GetString("cluster_id"),
  69. access_types.PLUGIN_ACCESS_TYPE_ENDPOINT,
  70. access_types.PLUGIN_ACCESS_ACTION_INVOKE_ENDPOINT,
  71. runtime.Configuration(),
  72. )
  73. defer session.Close()
  74. session.BindRuntime(runtime)
  75. status_code, headers, response, err := plugin_daemon.InvokeEndpoint(
  76. session, &requests.RequestInvokeEndpoint{
  77. RawHttpRequest: hex.EncodeToString(buffer.Bytes()),
  78. Settings: settings,
  79. },
  80. )
  81. if err != nil {
  82. ctx.JSON(500, gin.H{"error": err.Error()})
  83. return
  84. }
  85. defer response.Close()
  86. done := make(chan bool)
  87. closed := new(int32)
  88. ctx.Status(status_code)
  89. for k, v := range *headers {
  90. if len(v) > 0 {
  91. ctx.Writer.Header().Set(k, v[0])
  92. }
  93. }
  94. close := func() {
  95. if atomic.CompareAndSwapInt32(closed, 0, 1) {
  96. close(done)
  97. }
  98. }
  99. defer close()
  100. routine.Submit(func() {
  101. defer close()
  102. for response.Next() {
  103. chunk, err := response.Read()
  104. if err != nil {
  105. ctx.JSON(500, gin.H{"error": err.Error()})
  106. return
  107. }
  108. ctx.Writer.Write(chunk)
  109. ctx.Writer.Flush()
  110. }
  111. })
  112. select {
  113. case <-ctx.Writer.CloseNotify():
  114. case <-done:
  115. case <-time.After(30 * time.Second):
  116. ctx.JSON(500, gin.H{"error": "killed by timeout"})
  117. }
  118. }