task.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package backwards_invocation
  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/access_types"
  6. "github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/model_entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/tool_entities"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  12. )
  13. func InvokeDify(
  14. runtime plugin_entities.PluginRuntimeInterface,
  15. invoke_from access_types.PluginAccessType,
  16. session *session_manager.Session,
  17. writer BackwardsInvocationWriter,
  18. data []byte,
  19. ) error {
  20. // unmarshal invoke data
  21. request, err := parser.UnmarshalJsonBytes2Map(data)
  22. if err != nil {
  23. return fmt.Errorf("unmarshal invoke request failed: %s", err.Error())
  24. }
  25. if request == nil {
  26. return fmt.Errorf("invoke request is empty")
  27. }
  28. // prepare invocation arguments
  29. request_handle, err := prepareDifyInvocationArguments(session, writer, request)
  30. if err != nil {
  31. return err
  32. }
  33. if invoke_from == access_types.PLUGIN_ACCESS_TYPE_MODEL {
  34. request_handle.WriteError(fmt.Errorf("you can not invoke dify from %s", invoke_from))
  35. request_handle.EndResponse()
  36. return nil
  37. }
  38. // check permission
  39. if err := checkPermission(runtime, request_handle); err != nil {
  40. request_handle.WriteError(err)
  41. request_handle.EndResponse()
  42. return nil
  43. }
  44. // dispatch invocation task
  45. routine.Submit(func() {
  46. dispatchDifyInvocationTask(request_handle)
  47. defer request_handle.EndResponse()
  48. })
  49. return nil
  50. }
  51. var (
  52. permissionMapping = map[dify_invocation.InvokeType]map[string]any{
  53. dify_invocation.INVOKE_TYPE_TOOL: {
  54. "func": func(runtime plugin_entities.PluginRuntimeTimeLifeInterface) bool {
  55. return runtime.Configuration().Resource.Permission.AllowInvokeTool()
  56. },
  57. "error": "permission denied, you need to enable tool access in plugin manifest",
  58. },
  59. dify_invocation.INVOKE_TYPE_LLM: {
  60. "func": func(runtime plugin_entities.PluginRuntimeTimeLifeInterface) bool {
  61. return runtime.Configuration().Resource.Permission.AllowInvokeLLM()
  62. },
  63. "error": "permission denied, you need to enable llm access in plugin manifest",
  64. },
  65. dify_invocation.INVOKE_TYPE_TEXT_EMBEDDING: {
  66. "func": func(runtime plugin_entities.PluginRuntimeTimeLifeInterface) bool {
  67. return runtime.Configuration().Resource.Permission.AllowInvokeTextEmbedding()
  68. },
  69. "error": "permission denied, you need to enable text-embedding access in plugin manifest",
  70. },
  71. dify_invocation.INVOKE_TYPE_RERANK: {
  72. "func": func(runtime plugin_entities.PluginRuntimeTimeLifeInterface) bool {
  73. return runtime.Configuration().Resource.Permission.AllowInvokeRerank()
  74. },
  75. "error": "permission denied, you need to enable rerank access in plugin manifest",
  76. },
  77. dify_invocation.INVOKE_TYPE_TTS: {
  78. "func": func(runtime plugin_entities.PluginRuntimeTimeLifeInterface) bool {
  79. return runtime.Configuration().Resource.Permission.AllowInvokeTTS()
  80. },
  81. "error": "permission denied, you need to enable tts access in plugin manifest",
  82. },
  83. dify_invocation.INVOKE_TYPE_SPEECH2TEXT: {
  84. "func": func(runtime plugin_entities.PluginRuntimeTimeLifeInterface) bool {
  85. return runtime.Configuration().Resource.Permission.AllowInvokeSpeech2Text()
  86. },
  87. "error": "permission denied, you need to enable speech2text access in plugin manifest",
  88. },
  89. dify_invocation.INVOKE_TYPE_MODERATION: {
  90. "func": func(runtime plugin_entities.PluginRuntimeTimeLifeInterface) bool {
  91. return runtime.Configuration().Resource.Permission.AllowInvokeModeration()
  92. },
  93. "error": "permission denied, you need to enable moderation access in plugin manifest",
  94. },
  95. dify_invocation.INVOKE_TYPE_NODE: {
  96. "func": func(runtime plugin_entities.PluginRuntimeTimeLifeInterface) bool {
  97. return runtime.Configuration().Resource.Permission.AllowInvokeNode()
  98. },
  99. "error": "permission denied, you need to enable node access in plugin manifest",
  100. },
  101. }
  102. )
  103. func checkPermission(runtime plugin_entities.PluginRuntimeTimeLifeInterface, request_handle *BackwardsInvocation) error {
  104. permission, ok := permissionMapping[request_handle.Type()]
  105. if !ok {
  106. return fmt.Errorf("unsupported invoke type: %s", request_handle.Type())
  107. }
  108. permission_func, ok := permission["func"].(func(runtime plugin_entities.PluginRuntimeTimeLifeInterface) bool)
  109. if !ok {
  110. return fmt.Errorf("permission function not found: %s", request_handle.Type())
  111. }
  112. if !permission_func(runtime) {
  113. return fmt.Errorf(permission["error"].(string))
  114. }
  115. return nil
  116. }
  117. func prepareDifyInvocationArguments(
  118. session *session_manager.Session,
  119. writer BackwardsInvocationWriter,
  120. request map[string]any,
  121. ) (*BackwardsInvocation, error) {
  122. typ, ok := request["type"].(string)
  123. if !ok {
  124. return nil, fmt.Errorf("invoke request missing type: %s", request)
  125. }
  126. // get request id
  127. backwards_request_id, ok := request["backwards_request_id"].(string)
  128. if !ok {
  129. return nil, fmt.Errorf("invoke request missing request_id: %s", request)
  130. }
  131. // get request
  132. detailed_request, ok := request["request"].(map[string]any)
  133. if !ok {
  134. return nil, fmt.Errorf("invoke request missing request: %s", request)
  135. }
  136. return NewBackwardsInvocation(
  137. BackwardsInvocationType(typ),
  138. backwards_request_id,
  139. session,
  140. writer,
  141. detailed_request,
  142. ), nil
  143. }
  144. var (
  145. dispatchMapping = map[dify_invocation.InvokeType]func(handle *BackwardsInvocation){
  146. dify_invocation.INVOKE_TYPE_TOOL: func(handle *BackwardsInvocation) {
  147. genericDispatchTask[dify_invocation.InvokeToolRequest](handle, executeDifyInvocationToolTask)
  148. },
  149. dify_invocation.INVOKE_TYPE_LLM: func(handle *BackwardsInvocation) {
  150. genericDispatchTask[dify_invocation.InvokeLLMRequest](handle, executeDifyInvocationLLMTask)
  151. },
  152. dify_invocation.INVOKE_TYPE_TEXT_EMBEDDING: func(handle *BackwardsInvocation) {
  153. genericDispatchTask[dify_invocation.InvokeTextEmbeddingRequest](handle, executeDifyInvocationTextEmbeddingTask)
  154. },
  155. dify_invocation.INVOKE_TYPE_RERANK: func(handle *BackwardsInvocation) {
  156. genericDispatchTask[dify_invocation.InvokeRerankRequest](handle, executeDifyInvocationRerankTask)
  157. },
  158. dify_invocation.INVOKE_TYPE_TTS: func(handle *BackwardsInvocation) {
  159. genericDispatchTask[dify_invocation.InvokeTTSRequest](handle, executeDifyInvocationTTSTask)
  160. },
  161. dify_invocation.INVOKE_TYPE_SPEECH2TEXT: func(handle *BackwardsInvocation) {
  162. genericDispatchTask[dify_invocation.InvokeSpeech2TextRequest](handle, executeDifyInvocationSpeech2TextTask)
  163. },
  164. dify_invocation.INVOKE_TYPE_MODERATION: func(handle *BackwardsInvocation) {
  165. genericDispatchTask[dify_invocation.InvokeModerationRequest](handle, executeDifyInvocationModerationTask)
  166. },
  167. }
  168. )
  169. func genericDispatchTask[T any](
  170. handle *BackwardsInvocation,
  171. dispatch func(
  172. handle *BackwardsInvocation,
  173. request *T,
  174. ),
  175. ) {
  176. r, err := parser.MapToStruct[T](handle.RequestData())
  177. if err != nil {
  178. handle.WriteError(fmt.Errorf("unmarshal invoke tool request failed: %s", err.Error()))
  179. return
  180. }
  181. dispatch(handle, r)
  182. }
  183. func dispatchDifyInvocationTask(handle *BackwardsInvocation) {
  184. request_data := handle.RequestData()
  185. tenant_id, err := handle.TenantID()
  186. if err != nil {
  187. handle.WriteError(fmt.Errorf("get tenant id failed: %s", err.Error()))
  188. return
  189. }
  190. request_data["tenant_id"] = tenant_id
  191. user_id, err := handle.UserID()
  192. if err != nil {
  193. handle.WriteError(fmt.Errorf("get user id failed: %s", err.Error()))
  194. return
  195. }
  196. request_data["user_id"] = user_id
  197. typ := handle.Type()
  198. request_data["type"] = typ
  199. for t, v := range dispatchMapping {
  200. if t == handle.Type() {
  201. v(handle)
  202. return
  203. }
  204. }
  205. handle.WriteError(fmt.Errorf("unsupported invoke type: %s", handle.Type()))
  206. }
  207. func executeDifyInvocationToolTask(
  208. handle *BackwardsInvocation,
  209. request *dify_invocation.InvokeToolRequest,
  210. ) {
  211. response, err := dify_invocation.InvokeTool(request)
  212. if err != nil {
  213. handle.WriteError(fmt.Errorf("invoke tool failed: %s", err.Error()))
  214. return
  215. }
  216. response.Wrap(func(t tool_entities.ToolResponseChunk) {
  217. handle.WriteResponse("stream", t)
  218. })
  219. }
  220. func executeDifyInvocationLLMTask(
  221. handle *BackwardsInvocation,
  222. request *dify_invocation.InvokeLLMRequest,
  223. ) {
  224. response, err := dify_invocation.InvokeLLM(request)
  225. if err != nil {
  226. handle.WriteError(fmt.Errorf("invoke llm model failed: %s", err.Error()))
  227. return
  228. }
  229. response.Wrap(func(t model_entities.LLMResultChunk) {
  230. handle.WriteResponse("stream", t)
  231. })
  232. }
  233. func executeDifyInvocationTextEmbeddingTask(
  234. handle *BackwardsInvocation,
  235. request *dify_invocation.InvokeTextEmbeddingRequest,
  236. ) {
  237. response, err := dify_invocation.InvokeTextEmbedding(request)
  238. if err != nil {
  239. handle.WriteError(fmt.Errorf("invoke text-embedding model failed: %s", err.Error()))
  240. return
  241. }
  242. handle.WriteResponse("struct", response)
  243. }
  244. func executeDifyInvocationRerankTask(
  245. handle *BackwardsInvocation,
  246. request *dify_invocation.InvokeRerankRequest,
  247. ) {
  248. response, err := dify_invocation.InvokeRerank(request)
  249. if err != nil {
  250. handle.WriteError(fmt.Errorf("invoke rerank model failed: %s", err.Error()))
  251. return
  252. }
  253. handle.WriteResponse("struct", response)
  254. }
  255. func executeDifyInvocationTTSTask(
  256. handle *BackwardsInvocation,
  257. request *dify_invocation.InvokeTTSRequest,
  258. ) {
  259. response, err := dify_invocation.InvokeTTS(request)
  260. if err != nil {
  261. handle.WriteError(fmt.Errorf("invoke tts model failed: %s", err.Error()))
  262. return
  263. }
  264. response.Wrap(func(t model_entities.TTSResult) {
  265. handle.WriteResponse("struct", t)
  266. })
  267. }
  268. func executeDifyInvocationSpeech2TextTask(
  269. handle *BackwardsInvocation,
  270. request *dify_invocation.InvokeSpeech2TextRequest,
  271. ) {
  272. response, err := dify_invocation.InvokeSpeech2Text(request)
  273. if err != nil {
  274. handle.WriteError(fmt.Errorf("invoke speech2text model failed: %s", err.Error()))
  275. return
  276. }
  277. handle.WriteResponse("struct", response)
  278. }
  279. func executeDifyInvocationModerationTask(
  280. handle *BackwardsInvocation,
  281. request *dify_invocation.InvokeModerationRequest,
  282. ) {
  283. response, err := dify_invocation.InvokeModeration(request)
  284. if err != nil {
  285. handle.WriteError(fmt.Errorf("invoke moderation model failed: %s", err.Error()))
  286. return
  287. }
  288. handle.WriteResponse("struct", response)
  289. }