Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion llm/transformer/anthropic/outbound_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,15 @@ func buildBaseRequest(chatReq *llm.Request, config *Config) *MessageRequest {
MaxTokens: resolveMaxTokens(chatReq),
}

var userID string
if chatReq.Metadata != nil && chatReq.Metadata["user_id"] != "" {
req.Metadata = &AnthropicMetadata{UserID: chatReq.Metadata["user_id"]}
userID = chatReq.Metadata["user_id"]
} else if chatReq.User != nil && *chatReq.User != "" {
userID = *chatReq.User
}

if userID != "" {
req.Metadata = &AnthropicMetadata{UserID: shared.SanitizeUserID(userID)}
}

// DeepSeek Anthropic format supports output_config.effort. When reasoning_effort
Expand Down
15 changes: 13 additions & 2 deletions llm/transformer/doubao/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/looplj/axonhub/llm/internal/pkg/xurl"
"github.com/looplj/axonhub/llm/transformer"
"github.com/looplj/axonhub/llm/transformer/openai"
"github.com/looplj/axonhub/llm/transformer/shared"
)

// Config holds all configuration for the Doubao outbound transformer.
Expand Down Expand Up @@ -142,8 +143,18 @@ func (t *OutboundTransformer) TransformRequest(
RequestID: "",
}

if llmReq.Metadata != nil {
doubaoReq.UserID = llmReq.Metadata["user_id"]
var userID string
if llmReq.Metadata != nil && llmReq.Metadata["user_id"] != "" {
userID = llmReq.Metadata["user_id"]
} else if llmReq.User != nil && *llmReq.User != "" {
userID = *llmReq.User
}

if userID != "" {
doubaoReq.UserID = shared.SanitizeUserID(userID)
}

if llmReq.Metadata != nil && llmReq.Metadata["request_id"] != "" {
doubaoReq.RequestID = llmReq.Metadata["request_id"]
}

Expand Down
35 changes: 35 additions & 0 deletions llm/transformer/shared/userid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package shared

import "strings"

// SanitizeUserID ensures the user ID matches common platform constraints:
// ^[a-zA-Z0-9_-]{6,128}$
func SanitizeUserID(userID string) string {
if userID == "" {
return ""
}

var sb strings.Builder
for _, c := range userID {
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' {
sb.WriteRune(c)
} else {
sb.WriteRune('_')
}
}

sanitized := sb.String()

if len(sanitized) < 6 {
sanitized = "user_" + sanitized
if len(sanitized) < 6 {
sanitized = sanitized + strings.Repeat("_", 6-len(sanitized))
}
}

if len(sanitized) > 128 {
sanitized = sanitized[:128]
}

return sanitized
}
14 changes: 12 additions & 2 deletions llm/transformer/zai/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,18 @@ func (t *OutboundTransformer) TransformRequest(
RequestID: "",
}

if llmReq.Metadata != nil {
zaiReq.UserID = llmReq.Metadata["user_id"]
var userID string
if llmReq.Metadata != nil && llmReq.Metadata["user_id"] != "" {
userID = llmReq.Metadata["user_id"]
} else if llmReq.User != nil && *llmReq.User != "" {
userID = *llmReq.User
}

if userID != "" {
zaiReq.UserID = shared.SanitizeUserID(userID)
}

if llmReq.Metadata != nil && llmReq.Metadata["request_id"] != "" {
zaiReq.RequestID = llmReq.Metadata["request_id"]
}

Expand Down