request.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/session_manager"
  6. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  7. )
  8. type BackwardsInvocationType = dify_invocation.InvokeType
  9. type BackwardsInvocation struct {
  10. typ BackwardsInvocationType
  11. id string
  12. detailed_request map[string]any
  13. session *session_manager.Session
  14. }
  15. func NewBackwardsInvocation(
  16. typ BackwardsInvocationType,
  17. id string, session *session_manager.Session, detailed_request map[string]any,
  18. ) *BackwardsInvocation {
  19. return &BackwardsInvocation{
  20. typ: typ,
  21. id: id,
  22. detailed_request: detailed_request,
  23. session: session,
  24. }
  25. }
  26. func (bi *BackwardsInvocation) GetID() string {
  27. return bi.id
  28. }
  29. func (bi *BackwardsInvocation) WriteError(err error) {
  30. bi.session.Write(parser.MarshalJsonBytes(NewErrorEvent(bi.id, err.Error())))
  31. }
  32. func (bi *BackwardsInvocation) Write(message string, data any) {
  33. bi.session.Write(parser.MarshalJsonBytes(NewResponseEvent(bi.id, message, parser.StructToMap(data))))
  34. }
  35. func (bi *BackwardsInvocation) End() {
  36. bi.session.Write(parser.MarshalJsonBytes(NewEndEvent(bi.id)))
  37. }
  38. func (bi *BackwardsInvocation) Type() BackwardsInvocationType {
  39. return bi.typ
  40. }
  41. func (bi *BackwardsInvocation) RequestData() map[string]any {
  42. return bi.detailed_request
  43. }
  44. func (bi *BackwardsInvocation) TenantID() (string, error) {
  45. if bi.session == nil {
  46. return "", fmt.Errorf("session is nil")
  47. }
  48. return bi.session.TenantID(), nil
  49. }
  50. func (bi *BackwardsInvocation) UserID() (string, error) {
  51. if bi.session == nil {
  52. return "", fmt.Errorf("session is nil")
  53. }
  54. return bi.session.UserID(), nil
  55. }