Skip to content

feat(ai-security-guard): add fallback JSON paths for response content extraction#3738

Open
JianweiWang wants to merge 11 commits intohigress-group:mainfrom
JianweiWang:fix/support_anthropic_response_check
Open

feat(ai-security-guard): add fallback JSON paths for response content extraction#3738
JianweiWang wants to merge 11 commits intohigress-group:mainfrom
JianweiWang:fix/support_anthropic_response_check

Conversation

@JianweiWang
Copy link
Copy Markdown
Contributor

Summary

Add configurable fallback JSON paths (responseContentFallbackJsonPaths and responseStreamContentFallbackJsonPaths) to the ai-security-guard plugin, enabling response content extraction from non-OpenAI formats such as Anthropic Claude.

When the primary extraction path yields empty content, the plugin tries fallback paths in order. Paths equal to the primary path are automatically skipped. Setting either field to an empty array disables fallback explicitly.

Key implementation details

  • Default fallback paths cover both OpenAI and Claude response formats
  • Effective fallback paths are pre-computed in the response header phase and cached in request context to avoid per-chunk recalculation
  • Handles bare JSON, SSE data payloads, and multi-line data events
  • Default variables are unexported; exposed via functions returning copies

Changed files

  • plugins/wasm-go/extensions/ai-security-guard/config/config.go — config struct & fallback path logic
  • plugins/wasm-go/extensions/ai-security-guard/lvwang/common/text/openai.go — response content extraction with fallback
  • plugins/wasm-go/extensions/ai-security-guard/lvwang/common/text/openai_test.go — unit tests for fallback extraction (new)
  • plugins/wasm-go/extensions/ai-security-guard/main_test.go — integration tests
  • plugins/wasm-go/extensions/ai-security-guard/README.md / README_EN.md — documentation updates

JianweiWang and others added 9 commits April 8, 2026 19:23
… extraction

Add configurable fallback paths (responseContentFallbackJsonPaths and
responseStreamContentFallbackJsonPaths) to support extracting response
content from non-OpenAI formats such as Anthropic Claude.

When the primary extraction path yields empty content, the plugin tries
fallback paths in order. Paths equal to the primary path are
automatically skipped. Setting either field to an empty array disables
fallback explicitly.

Key implementation details:
- Default fallback paths cover OpenAI and Claude response formats
- Effective fallback paths are pre-computed in the response header phase
  and cached in request context to avoid per-chunk recalculation
- Handles bare JSON, SSE data payloads, and multi-line data events
- Default variables are unexported; exposed via functions returning copies
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ JianweiWang
❌ github-actions[bot]
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 20, 2026

Codecov Report

❌ Patch coverage is 98.03922% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ons/ai-security-guard/lvwang/common/text/openai.go 97.43% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

Add focused fallback extraction and config-validation test cases to cover previously missed patch branches, including streaming SSE edge cases and fallback-path caching behavior. Refactor the fallback context helper to a minimal interface so cache logic can be unit-tested without wasm host coupling.

Made-with: Cursor
Copy link
Copy Markdown
Collaborator

@rinfx rinfx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants