mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
ChatCompletionsRequest and ResponsesRequest have no metadata field and the conversion never sets AnthropicRequest.Metadata, so the user_id a client sends in metadata never reached the Anthropic request. withChatCompletionsMetadataUserID sets metadata.user_id on the converted body, byte for byte, when the client's top-level metadata.user_id is a string that ParseMetadataUserID accepts. Other metadata keys and ids that do not parse are not carried, and the body is unchanged in every other case. It is called right after the Anthropic body is marshaled in GatewayService.ForwardAsChatCompletions, for non-OAuth accounts only, and in OpenAIGatewayService.forwardChatCompletionsViaNativeAnthropic, which only serves API key accounts. OAuth accounts are unchanged: the mimicry already synthesizes their metadata.user_id. Fixes #8783
33 lines
1.2 KiB
Go
33 lines
1.2 KiB
Go
package service
|
|
|
|
import (
|
|
"github.com/tidwall/gjson"
|
|
"github.com/tidwall/sjson"
|
|
)
|
|
|
|
// withChatCompletionsMetadataUserID carries the client's top-level
|
|
// metadata.user_id of a Chat Completions request into the Anthropic body
|
|
// converted from it. ChatCompletionsRequest and ResponsesRequest have no
|
|
// metadata field, so the conversion never sets AnthropicRequest.Metadata and
|
|
// without this the Anthropic request never sees the id the client sent.
|
|
//
|
|
// Only a string user_id that ParseMetadataUserID accepts (JSON form with
|
|
// non-empty device_id and session_id, or the legacy form) is carried, as the
|
|
// raw JSON string token of the original body, so its bytes do not change.
|
|
// Every other metadata key is dropped. In every other case anthropicBody is
|
|
// returned as it is.
|
|
func withChatCompletionsMetadataUserID(chatBody, anthropicBody []byte) []byte {
|
|
userID := gjson.GetBytes(chatBody, "metadata.user_id")
|
|
if userID.Type != gjson.String {
|
|
return anthropicBody
|
|
}
|
|
if parsed := ParseMetadataUserID(userID.String()); parsed == nil || parsed.SessionID == "" {
|
|
return anthropicBody
|
|
}
|
|
out, err := sjson.SetRawBytes(anthropicBody, "metadata", []byte(`{"user_id":`+userID.Raw+`}`))
|
|
if err != nil {
|
|
return anthropicBody
|
|
}
|
|
return out
|
|
}
|