claude

package
v6.9.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 17 Imported by: 0

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClaudeTokenCount

func ClaudeTokenCount(ctx context.Context, count int64) []byte

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

func ConvertClaudeRequestToAntigravity(modelName string, inputRawJSON []byte, _ bool) []byte

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

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL