invoke_dify.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. defer request_handle.End()
  31. if invoke_from == PLUGIN_ACCESS_TYPE_MODEL {
  32. request_handle.WriteError(fmt.Errorf("you can not invoke dify from %s", invoke_from))
  33. return nil
  34. }
  35. // dispatch invocation task
  36. routine.Submit(func() {
  37. dispatchDifyInvocationTask(request_handle)
  38. })
  39. return nil
  40. }
  41. func prepareDifyInvocationArguments(session *session_manager.Session, request map[string]any) (*backwards_invocation.BackwardsInvocation, error) {
  42. typ, ok := request["type"].(string)
  43. if !ok {
  44. return nil, fmt.Errorf("invoke request missing type: %s", request)
  45. }
  46. // get request id
  47. backwards_request_id, ok := request["backwards_request_id"].(string)
  48. if !ok {
  49. return nil, fmt.Errorf("invoke request missing request_id: %s", request)
  50. }
  51. // get request
  52. detailed_request, ok := request["request"].(map[string]any)
  53. if !ok {
  54. return nil, fmt.Errorf("invoke request missing request: %s", request)
  55. }
  56. return backwards_invocation.NewBackwardsInvocation(
  57. backwards_invocation.BackwardsInvocationType(typ),
  58. backwards_request_id, session, detailed_request,
  59. ), nil
  60. }
  61. func dispatchDifyInvocationTask(handle *backwards_invocation.BackwardsInvocation) {
  62. request_data := handle.RequestData()
  63. tenant_id, err := handle.TenantID()
  64. if err != nil {
  65. handle.WriteError(fmt.Errorf("get tenant id failed: %s", err.Error()))
  66. return
  67. }
  68. request_data["tenant_id"] = tenant_id
  69. user_id, err := handle.UserID()
  70. if err != nil {
  71. handle.WriteError(fmt.Errorf("get user id failed: %s", err.Error()))
  72. return
  73. }
  74. request_data["user_id"] = user_id
  75. switch handle.Type() {
  76. case dify_invocation.INVOKE_TYPE_TOOL:
  77. r, err := parser.MapToStruct[dify_invocation.InvokeToolRequest](handle.RequestData())
  78. if err != nil {
  79. handle.WriteError(fmt.Errorf("unmarshal invoke tool request failed: %s", err.Error()))
  80. return
  81. }
  82. executeDifyInvocationToolTask(handle, r)
  83. default:
  84. handle.WriteError(fmt.Errorf("unsupported invoke type: %s", handle.Type()))
  85. }
  86. }
  87. func executeDifyInvocationToolTask(handle *backwards_invocation.BackwardsInvocation, request *dify_invocation.InvokeToolRequest) {
  88. handle.Write("stream", tool_entities.ToolResponseChunk{
  89. Type: "text",
  90. Message: map[string]any{
  91. "text": "hello world",
  92. },
  93. })
  94. handle.End()
  95. }