mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-20 15:13:44 +08:00
feat(anthropic): support both string and array content formats
- Add MessageContent type to handle both string and array content formats - Implement UnmarshalJSON to automatically detect content format - Add ToContentArray() method for unified content processing - Update Message and Response structs to use MessageContent - Fix all content processing logic in main.go and controller - Resolve JSON parsing errors for different content formats 支持Anthropic协议字符串和数组内容格式: - 添加MessageContent类型处理字符串和数组两种内容格式 - 实现UnmarshalJSON自动检测内容格式 - 添加ToContentArray()方法统一内容处理 - 更新Message和Response结构体使用MessageContent - 修复main.go和controller中的所有内容处理逻辑 - 解决不同内容格式的JSON解析错误
This commit is contained in:
@@ -29,9 +29,58 @@ type Content struct {
|
||||
ToolUseId string `json:"tool_use_id,omitempty"`
|
||||
}
|
||||
|
||||
// MessageContent can handle both string and array formats for the content field
|
||||
type MessageContent struct {
|
||||
value interface{}
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler to handle both string and array formats
|
||||
func (m *MessageContent) UnmarshalJSON(data []byte) error {
|
||||
// Try to unmarshal as string first
|
||||
var str string
|
||||
if err := json.Unmarshal(data, &str); err == nil {
|
||||
m.value = str
|
||||
return nil
|
||||
}
|
||||
|
||||
// If that fails, try to unmarshal as array of Content
|
||||
var arr []Content
|
||||
if err := json.Unmarshal(data, &arr); err == nil {
|
||||
m.value = arr
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("message content must be either a string or an array of content blocks")
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler
|
||||
func (m MessageContent) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(m.value)
|
||||
}
|
||||
|
||||
// ToContentArray converts the message content to a []Content array
|
||||
func (m MessageContent) ToContentArray() []Content {
|
||||
if m.value == nil {
|
||||
return []Content{}
|
||||
}
|
||||
|
||||
switch v := m.value.(type) {
|
||||
case string:
|
||||
// Convert string to a single text content block
|
||||
return []Content{{
|
||||
Type: "text",
|
||||
Text: v,
|
||||
}}
|
||||
case []Content:
|
||||
return v
|
||||
default:
|
||||
return []Content{}
|
||||
}
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Role string `json:"role"`
|
||||
Content []Content `json:"content"`
|
||||
Role string `json:"role"`
|
||||
Content MessageContent `json:"content"`
|
||||
}
|
||||
|
||||
type Tool struct {
|
||||
@@ -146,15 +195,15 @@ type Error struct {
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Role string `json:"role"`
|
||||
Content []Content `json:"content"`
|
||||
Model string `json:"model"`
|
||||
StopReason *string `json:"stop_reason"`
|
||||
StopSequence *string `json:"stop_sequence"`
|
||||
Usage Usage `json:"usage"`
|
||||
Error Error `json:"error"`
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Role string `json:"role"`
|
||||
Content MessageContent `json:"content"`
|
||||
Model string `json:"model"`
|
||||
StopReason *string `json:"stop_reason"`
|
||||
StopSequence *string `json:"stop_sequence"`
|
||||
Usage Usage `json:"usage"`
|
||||
Error Error `json:"error"`
|
||||
}
|
||||
|
||||
type Delta struct {
|
||||
|
||||
Reference in New Issue
Block a user