invoke_dify.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package plugin_daemon
  2. import (
  3. "fmt"
  4. "github.com/langgenius/dify-plugin-daemon/internal/core/dify_invocation"
  5. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/backwards_invocation"
  6. "github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/tool_entities"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  11. )
  12. func invokeDify(
  13. runtime entities.PluginRuntimeInterface,
  14. invoke_from PluginAccessType,
  15. session *session_manager.Session, data []byte,
  16. ) error {
  17. // unmarshal invoke data
  18. request, err := parser.UnmarshalJsonBytes2Map(data)
  19. if err != nil {
  20. return fmt.Errorf("unmarshal invoke request failed: %s", err.Error())
  21. }
  22. if request == nil {
  23. return fmt.Errorf("invoke request is empty")
  24. }
  25. // prepare invocation arguments
  26. request_handle, err := prepareDifyInvocationArguments(session, request)
  27. if err != nil {
  28. return err
  29. }
  30. if invoke_from == PLUGIN_ACCESS_TYPE_MODEL {
  31. request_handle.WriteError(fmt.Errorf("you can not invoke dify from %s", invoke_from))
  32. request_handle.EndResponse()
  33. return nil
  34. }
  35. // dispatch invocation task
  36. routine.Submit(func() {
  37. dispatchDifyInvocationTask(request_handle)
  38. defer request_handle.EndResponse()
  39. })
  40. return nil
  41. }
  42. func prepareDifyInvocationArguments(session *session_manager.Session, request map[string]any) (*backwards_invocation.BackwardsInvocation, error) {
  43. typ, ok := request["type"].(string)
  44. if !ok {
  45. return nil, fmt.Errorf("invoke request missing type: %s", request)
  46. }
  47. // get request id
  48. backwards_request_id, ok := request["backwards_request_id"].(string)
  49. if !ok {
  50. return nil, fmt.Errorf("invoke request missing request_id: %s", request)
  51. }
  52. // get request
  53. detailed_request, ok := request["request"].(map[string]any)
  54. if !ok {
  55. return nil, fmt.Errorf("invoke request missing request: %s", request)
  56. }
  57. return backwards_invocation.NewBackwardsInvocation(
  58. backwards_invocation.BackwardsInvocationType(typ),
  59. backwards_request_id, session, detailed_request,
  60. ), nil
  61. }
  62. var (
  63. dispatchMapping = map[dify_invocation.InvokeType]func(handle *backwards_invocation.BackwardsInvocation){
  64. dify_invocation.INVOKE_TYPE_TOOL: func(handle *backwards_invocation.BackwardsInvocation) {
  65. genericDispatchTask[dify_invocation.InvokeToolRequest](handle, executeDifyInvocationToolTask)
  66. },
  67. dify_invocation.INVOKE_TYPE_LLM: func(handle *backwards_invocation.BackwardsInvocation) {
  68. genericDispatchTask[dify_invocation.InvokeLLMRequest](handle, executeDifyInvocationLLMTask)
  69. },
  70. dify_invocation.INVOKE_TYPE_TEXT_EMBEDDING: func(handle *backwards_invocation.BackwardsInvocation) {
  71. genericDispatchTask[dify_invocation.InvokeTextEmbeddingRequest](handle, executeDifyInvocationTextEmbeddingTask)
  72. },
  73. dify_invocation.INVOKE_TYPE_RERANK: func(handle *backwards_invocation.BackwardsInvocation) {
  74. genericDispatchTask[dify_invocation.InvokeRerankRequest](handle, executeDifyInvocationRerankTask)
  75. },
  76. dify_invocation.INVOKE_TYPE_TTS: func(handle *backwards_invocation.BackwardsInvocation) {
  77. genericDispatchTask[dify_invocation.InvokeTTSRequest](handle, executeDifyInvocationTTSTask)
  78. },
  79. dify_invocation.INVOKE_TYPE_SPEECH2TEXT: func(handle *backwards_invocation.BackwardsInvocation) {
  80. genericDispatchTask[dify_invocation.InvokeSpeech2TextRequest](handle, executeDifyInvocationSpeech2TextTask)
  81. },
  82. dify_invocation.INVOKE_TYPE_MODERATION: func(handle *backwards_invocation.BackwardsInvocation) {
  83. genericDispatchTask[dify_invocation.InvokeModerationRequest](handle, executeDifyInvocationModerationTask)
  84. },
  85. }
  86. )
  87. func genericDispatchTask[T any](
  88. handle *backwards_invocation.BackwardsInvocation,
  89. dispatch func(
  90. handle *backwards_invocation.BackwardsInvocation,
  91. request *T,
  92. ),
  93. ) {
  94. r, err := parser.MapToStruct[T](handle.RequestData())
  95. if err != nil {
  96. handle.WriteError(fmt.Errorf("unmarshal invoke tool request failed: %s", err.Error()))
  97. return
  98. }
  99. dispatch(handle, r)
  100. }
  101. func dispatchDifyInvocationTask(handle *backwards_invocation.BackwardsInvocation) {
  102. request_data := handle.RequestData()
  103. tenant_id, err := handle.TenantID()
  104. if err != nil {
  105. handle.WriteError(fmt.Errorf("get tenant id failed: %s", err.Error()))
  106. return
  107. }
  108. request_data["tenant_id"] = tenant_id
  109. user_id, err := handle.UserID()
  110. if err != nil {
  111. handle.WriteError(fmt.Errorf("get user id failed: %s", err.Error()))
  112. return
  113. }
  114. request_data["user_id"] = user_id
  115. for t, v := range dispatchMapping {
  116. if t == handle.Type() {
  117. v(handle)
  118. return
  119. }
  120. }
  121. handle.WriteError(fmt.Errorf("unsupported invoke type: %s", handle.Type()))
  122. }
  123. func executeDifyInvocationToolTask(
  124. handle *backwards_invocation.BackwardsInvocation,
  125. request *dify_invocation.InvokeToolRequest,
  126. ) {
  127. response, err := dify_invocation.InvokeTool(request)
  128. if err != nil {
  129. handle.WriteError(fmt.Errorf("invoke tool failed: %s", err.Error()))
  130. return
  131. }
  132. response.Wrap(func(t tool_entities.ToolResponseChunk) {
  133. handle.WriteResponse("stream", t)
  134. })
  135. }
  136. func executeDifyInvocationLLMTask(
  137. handle *backwards_invocation.BackwardsInvocation,
  138. request *dify_invocation.InvokeLLMRequest,
  139. ) {
  140. }
  141. func executeDifyInvocationTextEmbeddingTask(
  142. handle *backwards_invocation.BackwardsInvocation,
  143. request *dify_invocation.InvokeTextEmbeddingRequest,
  144. ) {
  145. }
  146. func executeDifyInvocationRerankTask(
  147. handle *backwards_invocation.BackwardsInvocation,
  148. request *dify_invocation.InvokeRerankRequest,
  149. ) {
  150. }
  151. func executeDifyInvocationTTSTask(
  152. handle *backwards_invocation.BackwardsInvocation,
  153. request *dify_invocation.InvokeTTSRequest,
  154. ) {
  155. }
  156. func executeDifyInvocationSpeech2TextTask(
  157. handle *backwards_invocation.BackwardsInvocation,
  158. request *dify_invocation.InvokeSpeech2TextRequest,
  159. ) {
  160. }
  161. func executeDifyInvocationModerationTask(
  162. handle *backwards_invocation.BackwardsInvocation,
  163. request *dify_invocation.InvokeModerationRequest,
  164. ) {
  165. }