Documentation
¶
Overview ¶
Package claude provides request translation functionality for Claude Code API compatibility. This package handles the conversion of Claude Code API requests into Gemini CLI-compatible JSON format, transforming message contents, system instructions, and tool declarations into the format expected by Gemini CLI API clients. It performs JSON data transformation to ensure compatibility between Claude Code API format and Gemini CLI API's expected format.
Package claude provides response translation functionality for Claude Code API compatibility. This package handles the conversion of backend client responses into Claude Code-compatible Server-Sent Events (SSE) format, implementing a sophisticated state machine that manages different response types including text content, thinking processes, and function calls. The translation ensures proper sequencing of SSE events and maintains state across multiple response chunks to provide a seamless streaming experience.
Claude thinking signature validation for Antigravity bypass mode.
Spec reference: SIGNATURE-CHANNEL-SPEC.md
Encoding Detection (Spec §3) ¶
Claude signatures use base64 encoding in one or two layers. The raw string's first character determines the encoding depth — this is mathematically equivalent to the spec's "decode first, check byte" approach:
- 'E' prefix → single-layer: payload[0]==0x12, first 6 bits = 000100 = base64 index 4 = 'E'
- 'R' prefix → double-layer: inner[0]=='E' (0x45), first 6 bits = 010001 = base64 index 17 = 'R'
All valid signatures are normalized to R-form (double-layer base64) before sending to the Antigravity backend.
Protobuf Structure (Spec §4.1, §4.2) — strict mode only ¶
After base64 decoding to raw bytes (first byte must be 0x12):
Top-level protobuf ├── Field 2 (bytes): container ← extractBytesField(payload, 2) │ ├── Field 1 (bytes): channel block ← extractBytesField(container, 1) │ │ ├── Field 1 (varint): channel_id [required] → routing_class (11 | 12) │ │ ├── Field 2 (varint): infra [optional] → infrastructure_class (aws=1 | google=2) │ │ ├── Field 3 (varint): version=2 [skipped] │ │ ├── Field 5 (bytes): ECDSA sig [skipped, per Spec §11] │ │ ├── Field 6 (bytes): model_text [optional] → schema_features │ │ └── Field 7 (varint): unknown [optional] → schema_features │ ├── Field 2 (bytes): nonce 12B [skipped] │ ├── Field 3 (bytes): session 12B [skipped] │ ├── Field 4 (bytes): SHA-384 48B [skipped] │ └── Field 5 (bytes): metadata [skipped, per Spec §11] └── Field 3 (varint): =1 [skipped]
Output Dimensions (Spec §8) ¶
routing_class: routing_class_11 | routing_class_12 | unknown infrastructure_class: infra_default (absent) | infra_aws (1) | infra_google (2) | infra_unknown schema_features: compact_schema (len 70-72, no f6/f7) | extended_model_tagged_schema (f6 exists) | unknown legacy_route_hint: only for ch=11 — legacy_default_group | legacy_aws_group | legacy_vertex_direct/proxy
Compatibility ¶
Verified against all confirmed spec samples (Anthropic Max 20x, Azure, Vertex, Bedrock) and legacy ch=11 signatures. Both single-layer (E) and double-layer (R) encodings are supported. Historical cache-mode 'modelGroup#' prefixes are stripped.
Index ¶
- func ClaudeTokenCount(ctx context.Context, count int64) []byte
- func ConvertAntigravityResponseToClaude(_ context.Context, _ string, ...) [][]byte
- func ConvertAntigravityResponseToClaudeNonStream(_ context.Context, _ string, ...) []byte
- func ConvertClaudeRequestToAntigravity(modelName string, inputRawJSON []byte, _ bool) []byte
- func StripEmptySignatureThinkingBlocks(payload []byte) []byte
- func ValidateClaudeBypassSignatures(inputRawJSON []byte) error
- type Params
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertAntigravityResponseToClaude ¶
func ConvertAntigravityResponseToClaude(_ context.Context, _ string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) [][]byte
ConvertAntigravityResponseToClaude performs sophisticated streaming response format conversion. This function implements a complex state machine that translates backend client responses into Claude Code-compatible Server-Sent Events (SSE) format. It manages different response types and handles state transitions between content blocks, thinking processes, and function calls.
Response type states: 0=none, 1=content, 2=thinking, 3=function The function maintains state across multiple calls to ensure proper SSE event sequencing.
Parameters:
- ctx: The context for the request, used for cancellation and timeout handling
- modelName: The name of the model being used for the response (unused in current implementation)
- rawJSON: The raw JSON response from the Gemini CLI API
- param: A pointer to a parameter object for maintaining state between calls
Returns:
- [][]byte: A slice of bytes, each containing a Claude Code-compatible SSE payload.
func ConvertAntigravityResponseToClaudeNonStream ¶
func ConvertAntigravityResponseToClaudeNonStream(_ context.Context, _ string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, _ *any) []byte
ConvertAntigravityResponseToClaudeNonStream converts a non-streaming Gemini CLI response to a non-streaming Claude response.
Parameters:
- ctx: The context for the request.
- modelName: The name of the model.
- rawJSON: The raw JSON response from the Gemini CLI API.
- param: A pointer to a parameter object for the conversion.
Returns:
- []byte: A Claude-compatible JSON response.
func ConvertClaudeRequestToAntigravity ¶
ConvertClaudeRequestToAntigravity parses and transforms a Claude Code API request into Gemini CLI API format. It extracts the model name, system instruction, message contents, and tool declarations from the raw JSON request and returns them in the format expected by the Gemini CLI API. The function performs the following transformations: 1. Extracts the model information from the request 2. Restructures the JSON to match Gemini CLI API format 3. Converts system instructions to the expected format 4. Maps message contents with proper role transformations 5. Handles tool declarations and tool choices 6. Maps generation configuration parameters
Parameters:
- modelName: The name of the model to use for the request
- rawJSON: The raw JSON request data from the Claude Code API
- stream: A boolean indicating if the request is for a streaming response (unused in current implementation)
Returns:
- []byte: The transformed request data in Gemini CLI API format
func StripEmptySignatureThinkingBlocks ¶
StripEmptySignatureThinkingBlocks removes thinking blocks whose signatures are empty after trimming whitespace and any optional cache prefix.
Types ¶
type Params ¶
type Params struct {
HasFirstResponse bool // Indicates if the initial message_start event has been sent
ResponseType int // Current response type: 0=none, 1=content, 2=thinking, 3=function
ResponseIndex int // Index counter for content blocks in the streaming response
HasFinishReason bool // Tracks whether a finish reason has been observed
FinishReason string // The finish reason string returned by the provider
HasUsageMetadata bool // Tracks whether usage metadata has been observed
PromptTokenCount int64 // Cached prompt token count from usage metadata
CandidatesTokenCount int64 // Cached candidate token count from usage metadata
ThoughtsTokenCount int64 // Cached thinking token count from usage metadata
TotalTokenCount int64 // Cached total token count from usage metadata
CachedTokenCount int64 // Cached content token count (indicates prompt caching)
HasSentFinalEvents bool // Indicates if final content/message events have been sent
HasToolUse bool // Indicates if tool use was observed in the stream
HasContent bool // Tracks whether any content (text, thinking, or tool use) has been output
// Signature caching support
CurrentThinkingText strings.Builder // Accumulates thinking text for signature caching
// Reverse map: sanitized Gemini function name → original Claude tool name.
// Populated lazily on the first response chunk from the original request JSON.
ToolNameMap map[string]string
}
Params holds parameters for response conversion and maintains state across streaming chunks. This structure tracks the current state of the response translation process to ensure proper sequencing of SSE events and transitions between different content types.