unmarshal anthropic msg content. abort if err unmarshal#27
Conversation
|
@coderabbitai full review |
✅ Action performedFull review finished. |
WalkthroughAdds a ChangesAnthropic FlexContent and middleware error handling
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/policy/policy.go (1)
439-465: 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy liftPreserve Anthropic block structure when rewriting filtered messages.
This branch flattens
contentto one string and then always writes it back asFlexContent{Text: c}. Requests that originally contained block arrays lose non-text content and text-block boundaries, so policy filtering can corrupt otherwise valid Anthropic messages.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/policy/policy.go` around lines 439 - 465, The Anthropic rewrite path in policy.go is flattening message content into plain text and then rebuilding each message as FlexContent{Text: c}, which destroys the original block structure. Update the filtering flow around p.scan and the result.Updated rebuild loop so it preserves the original anthropic.Message content shape, especially block arrays and text-block boundaries, instead of always converting to a single text block. Use the existing converted.Messages entries as the source of truth when reconstructing each message.
🧹 Nitpick comments (1)
internal/server/web/proxy/middleware.go (1)
396-397: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winExtract an aborting JSON error helper.
This same
JSON(...); c.Abort(); returnblock is now copied across many endpoints. A small helper likeabortJSON(c, code, msg)would keep status/message handling consistent and make follow-up fixes much safer.Also applies to: 420-421, 447-448, 474-475, 499-500, 637-638, 660-661, 683-684, 705-706, 727-728, 746-747, 769-770, 792-793, 818-819, 843-844, 909-910, 929-930, 950-951, 980-981, 1009-1010
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/server/web/proxy/middleware.go` around lines 396 - 397, The repeated JSON error response plus abort pattern should be centralized to avoid copy-paste across these handlers. Add a helper such as abortJSON(c, code, msg) and update the affected middleware paths in middleware.go to call it instead of duplicating JSON(c, ...); c.Abort(); return. Keep the helper responsible for both writing the response and aborting the context so status/message handling stays consistent in all endpoints.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/provider/anthropic/anthropic.go`:
- Around line 44-53: The FlexContent.String method is using fmt.Sprintf on Raw
block arrays, which produces debug-style output instead of the real prompt text.
Update String to render Anthropic content arrays by extracting and concatenating
the underlying text from the block elements rather than formatting the slice/map
representation. Keep the existing nil and Text handling, and adjust the Raw
branch in FlexContent.String so policy scans and token counts see the actual
content.
In `@internal/server/web/proxy/middleware.go`:
- Around line 420-421: The request parsing branches in middleware.go are
returning http.StatusInternalServerError for invalid client JSON, which should
instead be a 400-class response. Update the JSON(c, ...) calls in the completion
request unmarshalling paths to use http.StatusBadRequest for these parse
failures, including the anthropic completion handler and the other listed
request decode branches in the same middleware logic. Keep the existing error
messages and Abort behavior, but ensure all JSON parse failures consistently map
to client errors.
---
Outside diff comments:
In `@internal/policy/policy.go`:
- Around line 439-465: The Anthropic rewrite path in policy.go is flattening
message content into plain text and then rebuilding each message as
FlexContent{Text: c}, which destroys the original block structure. Update the
filtering flow around p.scan and the result.Updated rebuild loop so it preserves
the original anthropic.Message content shape, especially block arrays and
text-block boundaries, instead of always converting to a single text block. Use
the existing converted.Messages entries as the source of truth when
reconstructing each message.
---
Nitpick comments:
In `@internal/server/web/proxy/middleware.go`:
- Around line 396-397: The repeated JSON error response plus abort pattern
should be centralized to avoid copy-paste across these handlers. Add a helper
such as abortJSON(c, code, msg) and update the affected middleware paths in
middleware.go to call it instead of duplicating JSON(c, ...); c.Abort(); return.
Keep the helper responsible for both writing the response and aborting the
context so status/message handling stays consistent in all endpoints.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 30e6e09a-3503-4834-8a42-b9093a44b1e6
📒 Files selected for processing (4)
internal/policy/policy.gointernal/provider/anthropic/anthropic.gointernal/provider/anthropic/cost.gointernal/server/web/proxy/middleware.go
https://bugtracker.codiodev.com/issue/codio-17842/Added-anthropic-key-doesnt-show-the-actual-amount-of-money
Summary by CodeRabbit