generic.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package plugin_daemon
  2. import (
  3. "errors"
  4. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/access_types"
  5. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/backwards_invocation"
  6. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/backwards_invocation/transaction"
  7. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
  8. "github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  12. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  13. )
  14. func genericInvokePlugin[Req any, Rsp any](
  15. session *session_manager.Session,
  16. request *Req,
  17. response_buffer_size int,
  18. typ access_types.PluginAccessType,
  19. action access_types.PluginAccessAction,
  20. ) (*stream.StreamResponse[Rsp], error) {
  21. runtime := plugin_manager.GetGlobalPluginManager().Get(session.PluginIdentity())
  22. if runtime == nil {
  23. return nil, errors.New("plugin not found")
  24. }
  25. response := stream.NewStreamResponse[Rsp](response_buffer_size)
  26. listener := runtime.Listen(session.ID())
  27. listener.Listen(func(chunk plugin_entities.SessionMessage) {
  28. switch chunk.Type {
  29. case plugin_entities.SESSION_MESSAGE_TYPE_STREAM:
  30. chunk, err := parser.UnmarshalJsonBytes[Rsp](chunk.Data)
  31. if err != nil {
  32. log.Error("unmarshal json failed: %s", err.Error())
  33. response.WriteError(err)
  34. } else {
  35. response.Write(chunk)
  36. }
  37. case plugin_entities.SESSION_MESSAGE_TYPE_INVOKE:
  38. // check if the request contains a aws_event_id
  39. var writer backwards_invocation.BackwardsInvocationWriter
  40. if chunk.RuntimeType == plugin_entities.PLUGIN_RUNTIME_TYPE_AWS {
  41. writer = transaction.NewAWSTransactionWriter(session, chunk.SessionWriter)
  42. } else {
  43. writer = transaction.NewFullDuplexEventWriter(session)
  44. }
  45. if err := backwards_invocation.InvokeDify(runtime, typ, session, writer, chunk.Data); err != nil {
  46. log.Error("invoke dify failed: %s", err.Error())
  47. return
  48. }
  49. case plugin_entities.SESSION_MESSAGE_TYPE_END:
  50. response.Close()
  51. case plugin_entities.SESSION_MESSAGE_TYPE_ERROR:
  52. e, err := parser.UnmarshalJsonBytes[plugin_entities.ErrorResponse](chunk.Data)
  53. if err != nil {
  54. break
  55. }
  56. response.WriteError(errors.New(e.Error))
  57. response.Close()
  58. default:
  59. response.WriteError(errors.New("unknown stream message type: " + string(chunk.Type)))
  60. response.Close()
  61. }
  62. })
  63. response.OnClose(func() {
  64. listener.Close()
  65. })
  66. session.Write(
  67. session_manager.PLUGIN_IN_STREAM_EVENT_REQUEST,
  68. getInvokePluginMap(
  69. session,
  70. typ,
  71. action,
  72. request,
  73. ),
  74. )
  75. return response, nil
  76. }
  77. func getInvokePluginMap(
  78. session *session_manager.Session,
  79. typ access_types.PluginAccessType,
  80. action access_types.PluginAccessAction,
  81. request any,
  82. ) map[string]any {
  83. req := getBasicPluginAccessMap(session.UserID(), typ, action)
  84. for k, v := range parser.StructToMap(request) {
  85. req[k] = v
  86. }
  87. return req
  88. }