generic.go 2.7 KB

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