Documentation
¶
Index ¶
- Constants
- func ApplyAnthropicBetaDeepSeekThinkingPatch(req *anthropic.BetaMessageNewParams)
- func ApplyAnthropicBetaMetadataTransform(req *anthropic.BetaMessageNewParams, extra map[string]any) *anthropic.BetaMessageNewParams
- func ApplyAnthropicBetaModelTransform(req *anthropic.BetaMessageNewParams, model string) *anthropic.BetaMessageNewParams
- func ApplyAnthropicV1DeepSeekThinkingPatch(req *anthropic.MessageNewParams)
- func ApplyAnthropicV1MetadataTransform(req *anthropic.MessageNewParams, extra map[string]any) *anthropic.MessageNewParams
- func ApplyAnthropicV1ModelTransform(req *anthropic.MessageNewParams, model string) *anthropic.MessageNewParams
- func ApplyClaudeCodeBetaCompatRoleRewrite(req *anthropic.BetaMessageNewParams)
- func ApplyClaudeCodeCompatRoleRewrite(req *anthropic.MessageNewParams)
- func ApplyCodexResponsesTransform(req *responses.ResponseNewParams, originalRequest interface{}) *responses.ResponseNewParams
- func ApplyCursorCompatContentNormalization(req *openai.ChatCompletionNewParams)
- func ApplyProviderTransforms(req *openai.ChatCompletionNewParams, providerURL, model string, ...) *openai.ChatCompletionNewParams
- func ApplyResponseTransforms(resp map[string]interface{}, providerURL, model string) map[string]interface{}
- func ApplyThinkingEffort(req interface{}, effort string) error
- func BuildClaudeCodeBillingHeader(ccVersion, existing string) string
- func ClaudeCodeVersionFromExtra(extra map[string]any) string
- func FitAnthropicThinkingBudget(budget, maxTokens int64) (fitted int64, ok bool)
- func GenHex5() string
- func IsBillingHeaderText(text string) bool
- func SanitizeAnthropicBetaThinkingConfig(req *anthropic.BetaMessageNewParams)
- func SanitizeAnthropicV1ThinkingConfig(req *anthropic.MessageNewParams)
- func SplitProviderHostPath(providerURL string) (host, path string)
Constants ¶
const ClaudeCodeVersion = "2.1.86"
const ClaudeCodeVersionExtraKey = "claude_code_version"
ClaudeCodeVersionExtraKey carries the flag in TransformContext.Extra.
const FingerprintSalt = "59cf53e54c78"
FingerprintSalt is the salt used in computeFingerprint. IMPORTANT: Must stay in sync with Claude Code's FINGERPRINT_SALT constant.
const MinAnthropicThinkingBudget int64 = 1024
MinAnthropicThinkingBudget is Anthropic's smallest accepted budget_tokens.
Variables ¶
This section is empty.
Functions ¶
func ApplyAnthropicBetaDeepSeekThinkingPatch ¶
func ApplyAnthropicBetaDeepSeekThinkingPatch(req *anthropic.BetaMessageNewParams)
ApplyAnthropicBetaDeepSeekThinkingPatch is the Beta-variant of ApplyAnthropicV1DeepSeekThinkingPatch.
func ApplyAnthropicBetaMetadataTransform ¶
func ApplyAnthropicBetaMetadataTransform(req *anthropic.BetaMessageNewParams, extra map[string]any) *anthropic.BetaMessageNewParams
ApplyAnthropicBetaMetadataTransform injects OAuth user_id into Anthropic beta request metadata. This adds metadata.user_id in JSON format for Anthropic API tracking.
Note: Only injects metadata when provider is OAuth and has valid UserID.
func ApplyAnthropicBetaModelTransform ¶
func ApplyAnthropicBetaModelTransform(req *anthropic.BetaMessageNewParams, model string) *anthropic.BetaMessageNewParams
ApplyAnthropicBetaModelTransform applies Anthropic API beta model-specific filtering. Same rules as V1 but for BetaMessageNewParams.
func ApplyAnthropicV1DeepSeekThinkingPatch ¶
func ApplyAnthropicV1DeepSeekThinkingPatch(req *anthropic.MessageNewParams)
ApplyAnthropicV1DeepSeekThinkingPatch ensures every assistant message carries at least one thinking block. DeepSeek's Anthropic-compatible endpoint rejects assistant turns that lack one — mirrors the reasoning_content fill in applyDeepSeekTransform for the OpenAI Chat path.
func ApplyAnthropicV1MetadataTransform ¶
func ApplyAnthropicV1MetadataTransform(req *anthropic.MessageNewParams, extra map[string]any) *anthropic.MessageNewParams
ApplyAnthropicV1MetadataTransform injects OAuth user_id into Anthropic v1 request metadata. This adds metadata.user_id in JSON format for Anthropic API tracking.
Note: Only injects metadata when provider is OAuth and has valid UserID.
func ApplyAnthropicV1ModelTransform ¶
func ApplyAnthropicV1ModelTransform(req *anthropic.MessageNewParams, model string) *anthropic.MessageNewParams
ApplyAnthropicV1ModelTransform reconciles the request's thinking config with what the target model actually supports:
- adaptive requested on a non-adaptive model → enabled(budget) derived from output_config.effort via typ.ThinkingBudgetMapping, or disabled when no effort is present (budget is the fallback dialect).
- enabled(budget) requested on an adaptive-only model (Opus 4.7+) → adaptive, with output_config.effort derived from the budget via typ.ThinkingEffortFromBudget (effort is the fallback in that direction).
- thinking disabled on a mandatory-thinking model (Opus/Sonnet 5.5, Fable) → adaptive: those models 400 on thinking.type=disabled, so a client's "off" or the thinking_effort=off rule flag degrades to adaptive.
- output_config.effort clamped to the model's supported levels independently of thinking mode. Anthropic effort controls the whole response and is valid without explicitly enabled thinking.
Note: This applies to ALL Anthropic API requests, regardless of authentication method (API key or OAuth token). The limitation is in the Anthropic API itself, not the auth method.
func ApplyClaudeCodeBetaCompatRoleRewrite ¶ added in v0.260611.1
func ApplyClaudeCodeBetaCompatRoleRewrite(req *anthropic.BetaMessageNewParams)
ApplyClaudeCodeBetaCompatRoleRewrite is the beta-namespace counterpart of ApplyClaudeCodeCompatRoleRewrite. The Beta SDK types are structurally identical but distinct, so the logic is duplicated rather than shared.
func ApplyClaudeCodeCompatRoleRewrite ¶ added in v0.260611.1
func ApplyClaudeCodeCompatRoleRewrite(req *anthropic.MessageNewParams)
ApplyClaudeCodeCompatRoleRewrite normalizes any "system" role inside the Anthropic messages array so the request becomes valid for third-party Anthropic-compatible providers, which only accept "user"/"assistant" roles in messages and reject the non-standard mid-conversation "system" role.
The normalization is position-aware on purpose. A system-role entry in messages is, by the Anthropic mid-conversation-system contract, always a mid-conversation operator message (it cannot be messages[0]); the global system prompt already lives in the top-level system field. So we never hoist it — hoisting would reorder an "as of turn N" instruction to a global one and invalidate the prompt cache. Instead each system block is folded, in place, into a user turn under one hard constraint: the result must never contain two consecutive user messages (strict providers reject that — "roles must alternate" — which would just trade one 400 for another).
The merge *direction* is not a free choice; it is forced by the system block's neighbours. Enumerating (prev role, next role):
prev=user, next=user → collapse prev+system+next into one user turn prev=user, next=assistant → merge backward into the preceding user prev=user, next=∅ (end) → merge backward into the preceding user prev=assistant, next=user → merge forward into the following user prev=assistant, next=assistant → stand alone as its own user turn prev=assistant, next=∅ → stand alone as its own user turn prev=∅, next=user → merge forward into the following user prev=∅, next=assistant → stand alone as its own user turn prev=∅, next=∅ → stand alone as its own user turn (sole msg)
In one rule: fold backward when the preceding turn is a user, otherwise fold forward into the next user, and only stand alone when neither neighbour is a user. The direction can only be decided once the *next* role is known, so system content is held in a pending buffer and placed when the following non-system message (or the end of the array) is reached.
Non-system messages with no pending system pass through untouched; a request with no system role is a pure pass-through (genuinely pre-existing consecutive user turns are left as-is — we only avoid *creating* new ones).
func ApplyCodexResponsesTransform ¶
func ApplyCodexResponsesTransform(req *responses.ResponseNewParams, originalRequest interface{}) *responses.ResponseNewParams
ApplyCodexResponsesTransform applies Codex-specific transformations to a Responses API request. This is called from VendorTransform for Codex backend providers.
It handles:
- Stream configuration (always enabled for Codex)
- Reasoning config (converts Anthropic "thinking" to Codex "reasoning")
- Tool name shortening (64 char limit)
- Tool parameter normalization
- Special tool mappings (web_search_20250305 -> web_search)
Reference: ref/anthropic2codex.go.ref
func ApplyCursorCompatContentNormalization ¶
func ApplyCursorCompatContentNormalization(req *openai.ChatCompletionNewParams)
ApplyCursorCompatContentNormalization flattens rich content in messages for Cursor compatibility. Applies to ALL providers when cursor_compat is enabled.
func ApplyProviderTransforms ¶
func ApplyProviderTransforms(req *openai.ChatCompletionNewParams, providerURL, model string, config *protocol.OpenAIConfig) *openai.ChatCompletionNewParams
ApplyProviderTransforms applies provider-specific transformations to an OpenAI Chat request. The dispatch matches the provider URL's host (and, where a vendor's quirk is scoped to one path on a shared host, a path prefix too) — short, explicit, and parallel to the per-shape dispatch in VendorTransform.
New providers are added as new cases here; aliases (e.g. multiple URLs that share a vendor's quirks) sit in the same case body.
func ApplyResponseTransforms ¶
func ApplyResponseTransforms(resp map[string]interface{}, providerURL, model string) map[string]interface{}
ApplyResponseTransforms applies provider-specific transformations to a response, dispatched by the parsed host of providerURL (see SplitProviderHostPath) — not by searching for a vendor hostname as a substring anywhere in providerURL, so a base URL that merely mentions a vendor's hostname in its path or query isn't mistaken for that vendor.
func ApplyThinkingEffort ¶
ApplyThinkingEffort enforces a thinking_effort level on any supported request shape. Shared by both the rule-level and scenario-level thinking transforms.
Effort semantics:
- "" (default): pass through, no change.
- "off": force thinking disabled. For Anthropic this sets OfDisabled; for OpenAI this clears reasoning_effort and removes any stray `thinking` extension, so the rule's level is the only thinking signal sent. We clear rather than send OpenAI's "none" because "none" is rejected by models older than gpt-5.1.
- "minimal"/"low"/"medium"/"high"/"xhigh"/"max": force thinking enabled at that level. OpenAI targets get reasoning_effort verbatim (all six are SDK-defined). Anthropic targets get output_config.effort (the native effort dialect on Claude 4.5+) plus a budget_tokens fallback; the vendor-stage model transform reconciles per-model support (see request_anthropic_model.go).
func BuildClaudeCodeBillingHeader ¶ added in v1.261001.1
BuildClaudeCodeBillingHeader renders the billing header block. ccVersion is "<ver>.<fp>"; existing is the inbound block (or "") whose preserved fields are kept.
func ClaudeCodeVersionFromExtra ¶ added in v1.261001.1
ClaudeCodeVersionFromExtra returns the selected version, or "" for legacy.
func FitAnthropicThinkingBudget ¶ added in v1.261001.1
FitAnthropicThinkingBudget enforces Anthropic's wire rule 1024 <= budget_tokens < max_tokens without raising max_tokens (the operator limit): a smaller budget is raised to 1024, one that reaches max_tokens is capped to max_tokens-1. ok is false when max_tokens leaves no room for a valid budget (<= 1024); the caller decides whether that turns thinking off or is an error. It is the one budget fit both the rule's thinking step and the target half's output limit use.
func IsBillingHeaderText ¶ added in v1.261001.1
IsBillingHeaderText reports whether a system block is the billing header.
func SanitizeAnthropicBetaThinkingConfig ¶ added in v0.260611.2
func SanitizeAnthropicBetaThinkingConfig(req *anthropic.BetaMessageNewParams)
SanitizeAnthropicBetaThinkingConfig is the Beta-variant of SanitizeAnthropicV1ThinkingConfig.
func SanitizeAnthropicV1ThinkingConfig ¶ added in v0.260611.2
func SanitizeAnthropicV1ThinkingConfig(req *anthropic.MessageNewParams)
SanitizeAnthropicV1ThinkingConfig ensures thinking config consistency for providers (notably DeepSeek) that enforce strict thinking/effort semantics.
This function NEVER modifies message content (thinking blocks in context are always preserved). It only adjusts the request-level thinking config header.
func SplitProviderHostPath ¶ added in v0.260819.0
SplitProviderHostPath splits a provider base URL into its lowercased host and lowercased path via net/url, which already handles userinfo, port, and bracketed IPv6 literals correctly — no need to hand-roll that parsing.
The one thing net/url won't do is a bare, scheme-less host: Provider.APIBase is sometimes stored without one (e.g. "api.deepseek.com" rather than "https://api.deepseek.com"), and net/url.Parse treats that as a relative path rather than a host. A default scheme is prepended when one is missing so parsing lands on the host as intended.
Every vendor-dispatch site in this package (request-side transforms, response-side transforms) and in transform.VendorTransform's Anthropic-shape dispatch shares this helper rather than re-parsing providerURL itself. Match on the parsed host (and, for vendors scoped to one path on a shared host, a path prefix) instead of strings.Contains-ing the raw URL text — the old pattern let a base URL that merely *mentioned* a vendor's hostname in its path or query (e.g. a proxy at "https://gateway.example.com/relay?target=api.deepseek.com") get mistaken for that vendor.
Types ¶
This section is empty.
Source Files
¶
- claude_code_billing_header.go
- provider_host.go
- request_anthropic_claude_code_compat.go
- request_anthropic_model.go
- request_openai_codex.go
- request_openai_deepseek.go
- request_openai_extensions.go
- request_openai_gemini.go
- request_openai_kimi.go
- request_openai_reasoning_tier.go
- request_thinking.go
- response_openai_extensions.go