request.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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(
  31. session_manager.PLUGIN_IN_STREAM_EVENT_RESPONSE,
  32. NewErrorEvent(bi.id, err.Error()),
  33. )
  34. }
  35. func (bi *BackwardsInvocation) WriteResponse(message string, data any) {
  36. bi.session.Write(
  37. session_manager.PLUGIN_IN_STREAM_EVENT_RESPONSE,
  38. NewResponseEvent(bi.id, message, parser.StructToMap(data)),
  39. )
  40. }
  41. func (bi *BackwardsInvocation) EndResponse() {
  42. bi.session.Write(
  43. session_manager.PLUGIN_IN_STREAM_EVENT_RESPONSE,
  44. NewEndEvent(bi.id),
  45. )
  46. }
  47. func (bi *BackwardsInvocation) Type() BackwardsInvocationType {
  48. return bi.typ
  49. }
  50. func (bi *BackwardsInvocation) RequestData() map[string]any {
  51. return bi.detailed_request
  52. }
  53. func (bi *BackwardsInvocation) TenantID() (string, error) {
  54. if bi.session == nil {
  55. return "", fmt.Errorf("session is nil")
  56. }
  57. return bi.session.TenantID(), nil
  58. }
  59. func (bi *BackwardsInvocation) UserID() (string, error) {
  60. if bi.session == nil {
  61. return "", fmt.Errorf("session is nil")
  62. }
  63. return bi.session.UserID(), nil
  64. }