request.go 2.0 KB

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