修改claude system参数为数组,增加通用性 - #1692
Conversation
WalkthroughShifts Claude system handling from a single string to an array of media messages. Aggregates multiple system messages (including composite text pieces) into []dto.ClaudeMediaMessage and assigns to claudeRequest.System after processing all messages. Other request flow remains unchanged. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant C as Client
participant R as Relay (Claude)
participant A as Claude API
C->>R: Send messages (user/assistant/system)
R->>R: Iterate messages
R->>R: Aggregate system text pieces into []ClaudeMediaMessage
Note right of R: systemMessages = [ {type:"text", text:...}, ... ]
R->>A: POST ClaudeRequest { System: systemMessages, ... }
A-->>R: Response
R-->>C: Return response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
relay/channel/claude/relay-claude.go (1)
277-299: System as array: solid change; add explicit handling for non-text blocks and consider normalizing message content arraysThe switch to accumulating system into []ClaudeMediaMessage is correct and aligns with Anthropic’s array format expectations. One improvement: non-text system blocks are silently dropped. Emit a lightweight log so operators can detect unexpected inputs.
Apply this within the system-content loop:
for _, ctx := range message.ParseContent() { if ctx.Type == "text" { systemMessages = append(systemMessages, dto.ClaudeMediaMessage{ Type: "text", Text: common.GetPointer[string](ctx.Text), }) } + else { + // Surface unexpected system content types without failing the request. + common.SysLog(fmt.Sprintf("ignored non-text system content in system[]: type=%s", ctx.Type)) + } }Note: To fully match the PR goal of “messages/content 也采用数组形式”, also consider normalizing user/assistant string content to []ClaudeMediaMessage (see Line 349 where a plain string is still assigned). I can provide a follow-up patch if desired.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
relay/channel/claude/relay-claude.go(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
relay/channel/claude/relay-claude.go (2)
dto/claude.go (1)
ClaudeMediaMessage(17-36)common/utils.go (1)
GetPointer(218-220)
🔇 Additional comments (1)
relay/channel/claude/relay-claude.go (1)
405-408: Ignore DTO shape concern —Systemis defined asanywithjson:"system,omitempty", so assigning a[]dto.ClaudeMediaMessageis validLikely an incorrect or invalid review comment.
修改claude system参数为数组,增加通用性
将 openai 格式转换成claude时,如果system参数是一个字符串时,转换成claude格式后,system参数也是一个字符串,现在官方给的参考是字符串或数组皆可,但为了通用性,改为数组
最近,发现一个
Claude中转站支持Cherry Studio的Anthropic类型,但是通过new api (v0.9.0-alpha.16)(Anthropic)接入Cherry Studio(OpenAi类型),就系统提示词会失效,经过对比请求参数,发现是New api将OpenAi格式转为Anthropic格式后,system参数默认是一个字符串。查询
Anthropic文档,发现字符串和数组形式皆可,而Cherry Studio的system参数是数组,为了通用性,将system参数改为数组,哪怕只有一个字符串。下面是一些请求:
这个是
Cherry Studio发送的请求这个是
New api将上面这个请求转换发后发送的请求这个是
Cherry Studio通过Anthropic类型接入中转站发送的请求下面这个是我这个
commit修改后的请求Summary by CodeRabbit
New Features
Improvements