event.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package plugin_entities
  2. import (
  3. "encoding/json"
  4. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  5. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  6. )
  7. type PluginUniversalEvent struct {
  8. SessionId string `json:"session_id"`
  9. Event PluginEventType `json:"event"`
  10. Data json.RawMessage `json:"data"`
  11. }
  12. // ParsePluginUniversalEvent parses bytes into struct contains basic info of a message
  13. // it's the outermost layer of the protocol
  14. // error_handler will be called when data is not standard or itself it's an error message
  15. func ParsePluginUniversalEvent(
  16. data []byte,
  17. session_handler func(sessionId string, data []byte),
  18. heartbeat_handler func(),
  19. error_handler func(err string),
  20. info_handler func(message string),
  21. ) {
  22. // handle event
  23. event, err := parser.UnmarshalJsonBytes[PluginUniversalEvent](data)
  24. if err != nil {
  25. error_handler(err.Error())
  26. return
  27. }
  28. sessionId := event.SessionId
  29. switch event.Event {
  30. case PLUGIN_EVENT_LOG:
  31. if event.Event == PLUGIN_EVENT_LOG {
  32. logEvent, err := parser.UnmarshalJsonBytes[PluginLogEvent](
  33. event.Data,
  34. )
  35. if err != nil {
  36. log.Error("unmarshal json failed: %s", err.Error())
  37. return
  38. }
  39. info_handler(logEvent.Message)
  40. }
  41. case PLUGIN_EVENT_SESSION:
  42. session_handler(sessionId, event.Data)
  43. case PLUGIN_EVENT_ERROR:
  44. error_handler(string(event.Data))
  45. case PLUGIN_EVENT_HEARTBEAT:
  46. heartbeat_handler()
  47. }
  48. }
  49. type PluginEventType string
  50. const (
  51. PLUGIN_EVENT_LOG PluginEventType = "log"
  52. PLUGIN_EVENT_SESSION PluginEventType = "session"
  53. PLUGIN_EVENT_ERROR PluginEventType = "error"
  54. PLUGIN_EVENT_HEARTBEAT PluginEventType = "heartbeat"
  55. )
  56. type PluginLogEvent struct {
  57. Level string `json:"level"`
  58. Message string `json:"message"`
  59. Timestamp float64 `json:"timestamp"`
  60. }
  61. type SessionMessage struct {
  62. Type SESSION_MESSAGE_TYPE `json:"type" validate:"required"`
  63. Data json.RawMessage `json:"data" validate:"required"`
  64. }
  65. type SESSION_MESSAGE_TYPE string
  66. const (
  67. SESSION_MESSAGE_TYPE_STREAM SESSION_MESSAGE_TYPE = "stream"
  68. SESSION_MESSAGE_TYPE_END SESSION_MESSAGE_TYPE = "end"
  69. SESSION_MESSAGE_TYPE_ERROR SESSION_MESSAGE_TYPE = "error"
  70. SESSION_MESSAGE_TYPE_INVOKE SESSION_MESSAGE_TYPE = "invoke"
  71. )
  72. type ErrorResponse struct {
  73. Message string `json:"message"`
  74. ErrorType string `json:"error_type"`
  75. Args map[string]any `json:"args" validate:"omitempty,max=10"` // max 10 args
  76. }
  77. func (e *ErrorResponse) Error() string {
  78. return parser.MarshalJson(map[string]any{
  79. "message": e.Message,
  80. "error_type": e.ErrorType,
  81. "args": e.Args,
  82. })
  83. }