coordinator.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package securityaudit
  2. import (
  3. "context"
  4. "errors"
  5. "net/http"
  6. "sync"
  7. )
  8. type LegacyEngine interface {
  9. Check(ctx context.Context, req Request) (*LegacyDecision, error)
  10. }
  11. type PromptEngine interface {
  12. EffectiveMode() Mode
  13. Enqueue(ctx context.Context, req Request) error
  14. Evaluate(ctx context.Context, req Request) (*PromptDecision, error)
  15. }
  16. type Coordinator struct {
  17. legacy LegacyEngine
  18. prompt PromptEngine
  19. }
  20. func NewCoordinator(legacy LegacyEngine, prompt PromptEngine) *Coordinator {
  21. return &Coordinator{legacy: legacy, prompt: prompt}
  22. }
  23. func (c *Coordinator) Check(ctx context.Context, req Request) Decision {
  24. if c == nil {
  25. return allowDecision(nil, nil)
  26. }
  27. mode := ModeOff
  28. if c.prompt != nil {
  29. mode = c.prompt.EffectiveMode()
  30. }
  31. switch mode {
  32. case ModeAsync:
  33. // Enqueue is deliberately best-effort. The implementation owns a bounded
  34. // context and copies request memory before it can outlive the Handler.
  35. _ = c.prompt.Enqueue(ctx, req.Clone())
  36. legacy, _ := c.checkLegacy(ctx, req)
  37. return prioritize(legacy, nil)
  38. case ModeBlocking:
  39. return c.checkBlocking(ctx, req)
  40. default:
  41. legacy, _ := c.checkLegacy(ctx, req)
  42. return prioritize(legacy, nil)
  43. }
  44. }
  45. func (c *Coordinator) checkBlocking(ctx context.Context, req Request) Decision {
  46. var wg sync.WaitGroup
  47. wg.Add(2)
  48. var legacy *LegacyDecision
  49. var prompt *PromptDecision
  50. go func() {
  51. defer wg.Done()
  52. legacy, _ = c.checkLegacy(ctx, req)
  53. }()
  54. go func() {
  55. defer wg.Done()
  56. if c.prompt == nil {
  57. prompt = unavailablePromptDecision(ErrorCodeUnavailable)
  58. return
  59. }
  60. result, err := c.prompt.Evaluate(ctx, req.Clone())
  61. if err != nil {
  62. var guardErr *GuardError
  63. if errors.As(err, &guardErr) && guardErr.Code == ErrorCodeInvalidResponse {
  64. prompt = unavailablePromptDecision(ErrorCodeInvalidResponse)
  65. return
  66. }
  67. prompt = unavailablePromptDecision(ErrorCodeUnavailable)
  68. return
  69. }
  70. if result == nil {
  71. prompt = unavailablePromptDecision(ErrorCodeUnavailable)
  72. return
  73. }
  74. prompt = result
  75. }()
  76. wg.Wait()
  77. return prioritize(legacy, prompt)
  78. }
  79. func (c *Coordinator) checkLegacy(ctx context.Context, req Request) (*LegacyDecision, error) {
  80. if c.legacy == nil {
  81. return nil, nil
  82. }
  83. return c.legacy.Check(ctx, req)
  84. }
  85. func prioritize(legacy *LegacyDecision, prompt *PromptDecision) Decision {
  86. if legacy != nil && legacy.Blocked {
  87. status := legacy.StatusCode
  88. if status < 400 || status > 599 {
  89. status = http.StatusForbidden
  90. }
  91. code := legacy.ErrorCode
  92. if code == "" {
  93. code = "content_policy_violation"
  94. }
  95. return Decision{
  96. Kind: DecisionBlock, HTTPStatus: status, ErrorCode: code, ClientMessage: legacy.Message,
  97. Legacy: legacy, Prompt: prompt, AllowNextStage: false,
  98. }
  99. }
  100. if prompt == nil {
  101. return allowDecision(legacy, nil)
  102. }
  103. switch prompt.Kind {
  104. case DecisionBlock:
  105. return Decision{Kind: DecisionBlock, HTTPStatus: http.StatusForbidden, ErrorCode: ErrorCodeBlocked,
  106. ClientMessage: "提示词安全审计拒绝了该请求,请调整输入后重试", Legacy: legacy, Prompt: prompt}
  107. case DecisionInvalid:
  108. return Decision{Kind: DecisionInvalid, HTTPStatus: http.StatusServiceUnavailable, ErrorCode: ErrorCodeInvalidResponse,
  109. ClientMessage: "提示词安全审计暂时不可用,请稍后重试", Legacy: legacy, Prompt: prompt}
  110. case DecisionUnavailable:
  111. return Decision{Kind: DecisionUnavailable, HTTPStatus: http.StatusServiceUnavailable, ErrorCode: ErrorCodeUnavailable,
  112. ClientMessage: "提示词安全审计暂时不可用,请稍后重试", Legacy: legacy, Prompt: prompt}
  113. case DecisionFlag:
  114. return Decision{Kind: DecisionFlag, HTTPStatus: http.StatusOK, Legacy: legacy, Prompt: prompt, AllowNextStage: true}
  115. default:
  116. return allowDecision(legacy, prompt)
  117. }
  118. }
  119. func allowDecision(legacy *LegacyDecision, prompt *PromptDecision) Decision {
  120. return Decision{Kind: DecisionAllow, HTTPStatus: http.StatusOK, Legacy: legacy, Prompt: prompt, AllowNextStage: true}
  121. }
  122. func unavailablePromptDecision(code string) *PromptDecision {
  123. kind := DecisionUnavailable
  124. if code == ErrorCodeInvalidResponse {
  125. kind = DecisionInvalid
  126. }
  127. return &PromptDecision{Kind: kind, ErrorCode: code, AllowNextStage: false}
  128. }