Documentation
¶
Overview ¶
Package claude provides request translation functionality for Claude Code API compatibility. This package handles the conversion of Claude Code API requests into Antigravity-compatible JSON format, transforming message contents, system instructions, and tool declarations into the format expected by Antigravity API clients. It performs JSON data transformation to ensure compatibility between Claude Code API format and Antigravity 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 wrappers for Antigravity bypass mode.
Index ¶
- func ClaudeTokenCount(ctx context.Context, count int64) []byte
- func ConvertAntigravityResponseToClaude(ctx context.Context, _ string, ...) [][]byte
- func ConvertAntigravityResponseToClaudeNonStream(_ context.Context, _ string, ...) []byte
- func ConvertClaudeRequestToAntigravity(modelName string, inputRawJSON []byte, _ bool) []byte
- func RequireCachedThinkingSignatures(ctx context.Context, modelName string, rawJSON []byte) error
- func StripEmptySignatureThinkingBlocks(payload []byte) []byte
- func StripInvalidBypassSignatureThinkingBlocks(payload []byte) []byte
- func StripInvalidGeminiSignatureThinkingBlocks(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(ctx 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 Antigravity 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 Antigravity 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 Antigravity 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 Antigravity 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 Antigravity API. The function performs the following transformations: 1. Extracts the model information from the request 2. Restructures the JSON to match Antigravity 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 Antigravity API format
func RequireCachedThinkingSignatures ¶ added in v7.2.49
func StripEmptySignatureThinkingBlocks ¶
StripEmptySignatureThinkingBlocks removes thinking blocks whose signatures are empty or not valid Claude thinking signatures. These usually come from proxy-generated responses where no real Claude signature exists.
func StripInvalidBypassSignatureThinkingBlocks ¶ added in v7.2.49
func StripInvalidGeminiSignatureThinkingBlocks ¶ added in v7.2.88
StripInvalidGeminiSignatureThinkingBlocks preserves only thinking carriers whose signatures can be replayed to Gemini. Claude Code uses these carriers to return provider-native signatures from prior translated responses.
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
HasSemanticContent bool
LastSemanticKind string
HasWebSearchTool bool
WebSearchRequests int64
WebSearchTextBuffer strings.Builder
// Signature caching support
CurrentThinkingText strings.Builder // Accumulates thinking text for signature caching
CurrentThinkingSigned bool // Tracks whether the active thinking block already has its terminal signature
// 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.