request.go 1.8 KB

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