endpoint.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/hex"
  6. "fmt"
  7. "sync/atomic"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "github.com/langgenius/dify-plugin-daemon/internal/core/dify_invocation"
  11. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon"
  12. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/access_types"
  13. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
  14. "github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
  15. "github.com/langgenius/dify-plugin-daemon/internal/db"
  16. "github.com/langgenius/dify-plugin-daemon/internal/service/install_service"
  17. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  18. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  19. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/requests"
  20. "github.com/langgenius/dify-plugin-daemon/internal/types/models"
  21. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  22. )
  23. func Endpoint(
  24. ctx *gin.Context,
  25. endpoint *models.Endpoint,
  26. plugin_installation *models.PluginInstallation,
  27. path string,
  28. ) {
  29. req := ctx.Request.Clone(context.Background())
  30. req.URL.Path = path
  31. var buffer bytes.Buffer
  32. err := req.Write(&buffer)
  33. if err != nil {
  34. ctx.JSON(500, gin.H{"error": err.Error()})
  35. }
  36. identifier, err := plugin_entities.NewPluginUniqueIdentifier(plugin_installation.PluginUniqueIdentifier)
  37. if err != nil {
  38. ctx.JSON(400, gin.H{"error": "Invalid plugin identifier, " + err.Error()})
  39. return
  40. }
  41. // fetch plugin
  42. manager := plugin_manager.Manager()
  43. runtime := manager.Get(identifier)
  44. if runtime == nil {
  45. ctx.JSON(404, gin.H{"error": "plugin not found"})
  46. return
  47. }
  48. // fetch endpoint declaration
  49. endpoint_declaration := runtime.Configuration().Endpoint
  50. if endpoint_declaration == nil {
  51. ctx.JSON(404, gin.H{"error": "endpoint declaration not found"})
  52. return
  53. }
  54. // decrypt settings
  55. settings, err := manager.BackwardsInvocation().InvokeEncrypt(&dify_invocation.InvokeEncryptRequest{
  56. BaseInvokeDifyRequest: dify_invocation.BaseInvokeDifyRequest{
  57. TenantId: endpoint.TenantID,
  58. UserId: "",
  59. Type: dify_invocation.INVOKE_TYPE_ENCRYPT,
  60. },
  61. InvokeEncryptSchema: dify_invocation.InvokeEncryptSchema{
  62. Opt: dify_invocation.ENCRYPT_OPT_DECRYPT,
  63. Namespace: dify_invocation.ENCRYPT_NAMESPACE_ENDPOINT,
  64. Identity: endpoint.ID,
  65. Data: endpoint.GetSettings(),
  66. Config: endpoint_declaration.Settings,
  67. },
  68. })
  69. if err != nil {
  70. ctx.JSON(500, gin.H{"error": "failed to decrypt data"})
  71. return
  72. }
  73. session := session_manager.NewSession(
  74. session_manager.NewSessionPayload{
  75. TenantID: endpoint.TenantID,
  76. UserID: "",
  77. PluginUniqueIdentifier: identifier,
  78. ClusterID: ctx.GetString("cluster_id"),
  79. InvokeFrom: access_types.PLUGIN_ACCESS_TYPE_ENDPOINT,
  80. Action: access_types.PLUGIN_ACCESS_ACTION_INVOKE_ENDPOINT,
  81. Declaration: runtime.Configuration(),
  82. BackwardsInvocation: manager.BackwardsInvocation(),
  83. IgnoreCache: false,
  84. },
  85. )
  86. defer session.Close(session_manager.CloseSessionPayload{
  87. IgnoreCache: false,
  88. })
  89. session.BindRuntime(runtime)
  90. status_code, headers, response, err := plugin_daemon.InvokeEndpoint(
  91. session, &requests.RequestInvokeEndpoint{
  92. RawHttpRequest: hex.EncodeToString(buffer.Bytes()),
  93. Settings: settings,
  94. },
  95. )
  96. if err != nil {
  97. ctx.JSON(500, gin.H{"error": err.Error()})
  98. return
  99. }
  100. defer response.Close()
  101. done := make(chan bool)
  102. closed := new(int32)
  103. ctx.Status(status_code)
  104. for k, v := range *headers {
  105. if len(v) > 0 {
  106. ctx.Writer.Header().Set(k, v[0])
  107. }
  108. }
  109. close := func() {
  110. if atomic.CompareAndSwapInt32(closed, 0, 1) {
  111. close(done)
  112. }
  113. }
  114. defer close()
  115. routine.Submit(func() {
  116. defer close()
  117. for response.Next() {
  118. chunk, err := response.Read()
  119. if err != nil {
  120. ctx.JSON(500, gin.H{"error": err.Error()})
  121. return
  122. }
  123. ctx.Writer.Write(chunk)
  124. ctx.Writer.Flush()
  125. }
  126. })
  127. select {
  128. case <-ctx.Writer.CloseNotify():
  129. case <-done:
  130. case <-time.After(30 * time.Second):
  131. ctx.JSON(500, gin.H{"error": "killed by timeout"})
  132. }
  133. }
  134. func EnableEndpoint(endpoint_id string, tenant_id string) *entities.Response {
  135. endpoint, err := db.GetOne[models.Endpoint](
  136. db.Equal("id", endpoint_id),
  137. db.Equal("tenant_id", tenant_id),
  138. )
  139. if err != nil {
  140. return entities.NewErrorResponse(-404, "Endpoint not found")
  141. }
  142. endpoint.Enabled = true
  143. if err := install_service.EnabledEndpoint(&endpoint); err != nil {
  144. return entities.NewErrorResponse(-500, "Failed to enable endpoint")
  145. }
  146. return entities.NewSuccessResponse("success")
  147. }
  148. func DisableEndpoint(endpoint_id string, tenant_id string) *entities.Response {
  149. endpoint, err := db.GetOne[models.Endpoint](
  150. db.Equal("id", endpoint_id),
  151. db.Equal("tenant_id", tenant_id),
  152. )
  153. if err != nil {
  154. return entities.NewErrorResponse(-404, "Endpoint not found")
  155. }
  156. endpoint.Enabled = false
  157. if err := install_service.DisabledEndpoint(&endpoint); err != nil {
  158. return entities.NewErrorResponse(-500, "Failed to disable endpoint")
  159. }
  160. return entities.NewSuccessResponse("success")
  161. }
  162. func ListEndpoints(tenant_id string, page int, page_size int) *entities.Response {
  163. endpoints, err := db.GetAll[models.Endpoint](
  164. db.Equal("tenant_id", tenant_id),
  165. db.OrderBy("created_at", true),
  166. db.Page(page, page_size),
  167. )
  168. if err != nil {
  169. return entities.NewErrorResponse(-500, fmt.Sprintf("failed to list endpoints: %v", err))
  170. }
  171. manager := plugin_manager.Manager()
  172. if manager == nil {
  173. return entities.NewErrorResponse(-500, "failed to get plugin manager")
  174. }
  175. // decrypt settings
  176. for i, endpoint := range endpoints {
  177. plugin_installation, err := db.GetOne[models.PluginInstallation](
  178. db.Equal("plugin_id", endpoint.PluginID),
  179. db.Equal("tenant_id", tenant_id),
  180. )
  181. if err != nil {
  182. return entities.NewErrorResponse(-404, fmt.Sprintf("failed to find plugin installation: %v", err))
  183. }
  184. plugin, err := db.GetOne[models.Plugin](
  185. db.Equal("plugin_unique_identifier", plugin_installation.PluginUniqueIdentifier),
  186. )
  187. if err != nil {
  188. return entities.NewErrorResponse(-404, fmt.Sprintf("failed to find plugin: %v", err))
  189. }
  190. plugin_declaration := plugin.Declaration
  191. if plugin_declaration.Endpoint == nil {
  192. return entities.NewErrorResponse(-404, "plugin does not have an endpoint")
  193. }
  194. decrypted_settings, err := manager.BackwardsInvocation().InvokeEncrypt(&dify_invocation.InvokeEncryptRequest{
  195. BaseInvokeDifyRequest: dify_invocation.BaseInvokeDifyRequest{
  196. TenantId: tenant_id,
  197. UserId: "",
  198. Type: dify_invocation.INVOKE_TYPE_ENCRYPT,
  199. },
  200. InvokeEncryptSchema: dify_invocation.InvokeEncryptSchema{
  201. Opt: dify_invocation.ENCRYPT_OPT_DECRYPT,
  202. Namespace: dify_invocation.ENCRYPT_NAMESPACE_ENDPOINT,
  203. Identity: endpoint.ID,
  204. Data: endpoint.GetSettings(),
  205. Config: plugin_declaration.Endpoint.Settings,
  206. },
  207. })
  208. if err != nil {
  209. return entities.NewErrorResponse(-500, fmt.Sprintf("failed to decrypt settings: %v", err))
  210. }
  211. endpoint.SetSettings(decrypted_settings)
  212. endpoints[i] = endpoint
  213. }
  214. return entities.NewSuccessResponse(endpoints)
  215. }