Documentation
¶
Overview ¶
Package gemini provides request translation functionality for Gemini to Claude Code API compatibility. It handles parsing and transforming Gemini API requests into Claude Code API format, extracting model information, system instructions, message contents, and tool declarations. The package performs JSON data transformation to ensure compatibility between Gemini API format and Claude Code API's expected format.
Package gemini provides response translation functionality for Claude Code to Gemini API compatibility. This package handles the conversion of Claude Code API responses into Gemini-compatible JSON format, transforming streaming events and non-streaming responses into the format expected by Gemini API clients. It supports both streaming and non-streaming modes, handling text content, tool calls, and usage metadata appropriately.
Index ¶
- func ConvertClaudeResponseToGemini(_ context.Context, modelName string, ...) []string
- func ConvertClaudeResponseToGeminiNonStream(_ context.Context, modelName string, ...) string
- func ConvertGeminiRequestToClaude(modelName string, inputRawJSON []byte, stream bool) []byte
- func GeminiTokenCount(ctx context.Context, count int64) string
- type ConvertAnthropicResponseToGeminiParams
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertClaudeResponseToGemini ¶
func ConvertClaudeResponseToGemini(_ context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string
ConvertClaudeResponseToGemini converts Claude Code streaming response format to Gemini format. This function processes various Claude Code event types and transforms them into Gemini-compatible JSON responses. It handles text content, tool calls, reasoning content, and usage metadata, outputting responses that match the Gemini API format. The function supports incremental updates for streaming responses and maintains state information to properly assemble multi-part tool calls.
Parameters:
- ctx: The context for the request, used for cancellation and timeout handling
- modelName: The name of the model being used for the response
- rawJSON: The raw JSON response from the Claude Code API
- param: A pointer to a parameter object for maintaining state between calls
Returns:
- []string: A slice of strings, each containing a Gemini-compatible JSON response
func ConvertClaudeResponseToGeminiNonStream ¶
func ConvertClaudeResponseToGeminiNonStream(_ context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, _ *any) string
ConvertClaudeResponseToGeminiNonStream converts a non-streaming Claude Code response to a non-streaming Gemini response. This function processes the complete Claude Code response and transforms it into a single Gemini-compatible JSON response. It handles message content, tool calls, reasoning content, and usage metadata, combining all the information into a single response that matches the Gemini API format.
Parameters:
- ctx: The context for the request, used for cancellation and timeout handling
- modelName: The name of the model being used for the response
- rawJSON: The raw JSON response from the Claude Code API
- param: A pointer to a parameter object for the conversion (unused in current implementation)
Returns:
- string: A Gemini-compatible JSON response containing all message content and metadata
func ConvertGeminiRequestToClaude ¶
ConvertGeminiRequestToClaude parses and transforms a Gemini API request into Claude Code 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 Claude Code API. The function performs comprehensive transformation including: 1. Model name mapping and generation configuration extraction 2. System instruction conversion to Claude Code format 3. Message content conversion with proper role mapping 4. Tool call and tool result handling with FIFO queue for ID matching 5. Image and file data conversion to Claude Code base64 format 6. Tool declaration and tool choice configuration mapping
Parameters:
- modelName: The name of the model to use for the request
- rawJSON: The raw JSON request data from the Gemini API
- stream: A boolean indicating if the request is for a streaming response
Returns:
- []byte: The transformed request data in Claude Code API format
Types ¶
type ConvertAnthropicResponseToGeminiParams ¶
type ConvertAnthropicResponseToGeminiParams struct {
Model string
CreatedAt int64
ResponseID string
LastStorageOutput string
IsStreaming bool
// Streaming state for tool_use assembly
// Keyed by content_block index from Claude SSE events
ToolUseNames map[int]string // function/tool name per block index
ToolUseArgs map[int]*strings.Builder // accumulates partial_json across deltas
}
ConvertAnthropicResponseToGeminiParams holds parameters for response conversion It also carries minimal streaming state across calls to assemble tool_use input_json_delta. This structure maintains state information needed for proper conversion of streaming responses from Claude Code format to Gemini format, particularly for handling tool calls that span multiple streaming events.