executor

package
v1.0.34 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2025 License: MIT Imports: 54 Imported by: 0

Documentation

Overview

Package executor provides common functionality for provider executors.

BaseExecutor contains shared fields and methods that all concrete executors can embed to reduce boilerplate. Embedding is optional but recommended.

Package executor provides runtime execution capabilities for various AI service providers. This file contains shared constants used across all executors.

Package executor provides common utilities for executor implementations.

Package executor provides common utilities for executor implementations.

Error Handling:

This file provides standardized error handling across all executors:

  • HandleHTTPError: Reads error response body and returns categorized error
  • StatusError: Error type with HTTP status code, message, and retry-after
  • Error constructors: NewStatusError, NewAuthError, NewInternalError, etc.

StatusError implements these interfaces:

  • error: Standard error interface
  • StatusCode() int: HTTP status code for response
  • RetryAfter() *time.Duration: Optional retry-after hint
  • Category() ErrorCategory: Error classification for retry logic

All executors should use HandleHTTPError for consistent error handling:

if resp.StatusCode >= 400 {
    result := HandleHTTPError(resp, "my executor")
    return result.Error
}

Package executor provides runtime execution capabilities for various AI service providers. It includes stateless executors that handle API requests, streaming responses, token counting, and authentication refresh for different AI service providers.

Package executor contains provider executors. This file implements the Vertex AI Gemini executor that talks to Google Vertex AI endpoints using service account credentials imported by the CLI.

GeminiVertexExecutor handles requests to Google Vertex AI Gemini endpoints.

Authentication (Strategy Pattern):

  • Service Account: via auth.Metadata["service_account"] with OAuth2 token exchange
  • API Key: via auth.Attributes["api_key"] with x-goog-api-key header

The executor uses VertexAuthStrategy interface to abstract authentication:

  • serviceAccountStrategy: For project-based Vertex AI access
  • apiKeyStrategy: For AI Studio / Generative Language API access

Supported models: gemini-* models via Vertex AI

Features:

  • Streaming via SSE (streamGenerateContent)
  • Token counting via countTokens endpoint
  • Format translation (OpenAI/Claude -> Gemini)
  • Thinking content transformation for reasoning models

Package executor provides model fetching utilities shared across Gemini-family providers.

Package executor provides retry logic for multi-target execution with fallback.

Retry Handling:

This file provides two layers of retry logic:

  1. RetryHandler (new): Modern abstraction for multi-target execution - Configurable retry status codes (default: 429) - Configurable fallback codes (default: 429, 503) - Exponential backoff with server-provided delay hints - Multi-target fallback support

  2. rateLimitRetrier (legacy): Backward-compatible retry for rate limits - Used by older executor implementations - 1 retry with exponential backoff

RetryAction flow:

RetryActionSuccess      -> Request succeeded, return response
RetryActionRetryCurrent -> Wait and retry same target
RetryActionContinueNext -> Skip to next target in pool
RetryActionFail         -> All retries exhausted, return error

Usage:

handler := NewRetryHandler(DefaultRetryConfig())
action, err := handler.HandleResponse(ctx, resp.StatusCode, body, hasNextTarget)
switch action {
case RetryActionSuccess:
    return resp, nil
case RetryActionRetryCurrent:
    continue // retry loop
case RetryActionContinueNext:
    break // outer loop to next target
case RetryActionFail:
    return nil, err
}

Package executor provides streaming utilities for SSE-based API responses.

StreamingHelper abstracts the common SSE streaming pattern used across all executors, reducing ~50 lines of boilerplate per executor.

Key components:

  • StreamProcessor: Interface for translating SSE chunks (provider-specific)
  • StreamConfig: Configuration for buffer sizes, preprocessing, done handling
  • RunSSEStream: Main helper that handles goroutine, scanner, context cancellation
  • GeminiPreprocessor: Pre-built preprocessor for Gemini-family APIs
  • DataTagPreprocessor: Standard SSE "data:" prefix handler for OpenAI-style APIs

Usage:

processor := &myStreamProcessor{...}
out := RunSSEStream(ctx, resp.Body, reporter, processor, StreamConfig{
    ExecutorName:     "my executor",
    Preprocessor:     GeminiPreprocessor(),
    HandleDoneSignal: true,
})

Index

Constants

View Source
const (
	// DefaultClaudeUserAgent is the User-Agent header for Claude CLI requests.
	DefaultClaudeUserAgent = "claude-cli/1.0.83 (external, cli)"

	// DefaultCodexUserAgent is the User-Agent header for OpenAI Codex CLI requests.
	DefaultCodexUserAgent = "codex_cli_rs/1.104.1 (Mac OS 26.0.1; arm64) Apple_Terminal/464"

	// DefaultAntigravityUserAgent is the User-Agent header for Antigravity (Gemini CLI) requests.
	DefaultAntigravityUserAgent = "antigravity/1.11.5 windows/amd64"

	// DefaultQwenUserAgent is the User-Agent header for Qwen requests.
	DefaultQwenUserAgent = "google-api-nodejs-client/9.15.1"

	// DefaultIFlowUserAgent is the User-Agent header for iFlow requests.
	DefaultIFlowUserAgent = "iFlow-Cli"

	// DefaultCopilotUserAgent is the User-Agent header for GitHub Copilot requests.
	DefaultCopilotUserAgent = "GithubCopilot/1.0"
)
View Source
const (
	// ClaudeDefaultBaseURL is the default API endpoint for Anthropic Claude.
	ClaudeDefaultBaseURL = "https://api.anthropic.com"

	// CodexDefaultBaseURL is the default API endpoint for OpenAI Codex.
	CodexDefaultBaseURL = "https://chatgpt.com/backend-api/codex"

	// QwenDefaultBaseURL is the default API endpoint for Qwen.
	QwenDefaultBaseURL = "https://portal.qwen.ai/v1"

	// ClineDefaultBaseURL is the default API endpoint for Cline.
	ClineDefaultBaseURL = "https://api.cline.bot"

	// GeminiDefaultBaseURL is the default API endpoint for Google Gemini.
	GeminiDefaultBaseURL = "https://generativelanguage.googleapis.com"

	// AntigravityBaseURLDaily is the daily/sandbox endpoint for Antigravity.
	AntigravityBaseURLDaily = "https://daily-cloudcode-pa.sandbox.googleapis.com"

	// AntigravityBaseURLProd is the production endpoint for Antigravity.
	AntigravityBaseURLProd = "https://cloudcode-pa.googleapis.com"

	// GitHubCopilotDefaultBaseURL is the default API endpoint for GitHub Copilot.
	GitHubCopilotDefaultBaseURL = "https://api.githubcopilot.com"

	// KiroDefaultBaseURL is the default API endpoint for Kiro (Amazon Q).
	KiroDefaultBaseURL = "https://codewhisperer.us-east-1.amazonaws.com/generateAssistantResponse"
)
View Source
const (
	// DefaultHTTPTimeout is the default timeout for HTTP requests.
	// Used as fallback when no specific timeout is configured.
	DefaultHTTPTimeout = 60 * time.Second

	// DefaultRefreshSkew is the time buffer before token expiry to trigger refresh.
	// Tokens are refreshed this many seconds before they actually expire.
	DefaultRefreshSkew = 3000 * time.Second

	// KiroRefreshSkew is the time buffer before token expiry for Kiro provider.
	KiroRefreshSkew = 5 * time.Minute

	// KiroRequestTimeout is the timeout for Kiro API requests.
	KiroRequestTimeout = 120 * time.Second

	// GitHubCopilotTokenCacheTTL is the cache duration for GitHub Copilot tokens.
	GitHubCopilotTokenCacheTTL = 25 * time.Minute

	// TokenExpiryBuffer is the buffer time before token expiry for cache invalidation.
	TokenExpiryBuffer = 5 * time.Minute
)
View Source
const (
	// RateLimitBaseDelay is the initial delay for rate limit retries.
	// Exponential backoff: 1s, 2s, 4s, 8s, 16s = ~31s total.
	RateLimitBaseDelay = 1 * time.Second

	// RateLimitMaxDelay is the maximum delay between retry attempts.
	RateLimitMaxDelay = 20 * time.Second

	// AntigravityRetryBaseDelay is the base delay for Antigravity retries.
	AntigravityRetryBaseDelay = 2 * time.Second

	// AntigravityRetryMaxDelay is the maximum delay for Antigravity retries.
	AntigravityRetryMaxDelay = 30 * time.Second
)
View Source
const DefaultStreamBufferSize = 20 * 1024 * 1024

DefaultStreamBufferSize is 20MB - maximum buffer for SSE stream scanning. This size accommodates large tool call responses from LLM providers.

Variables

View Source
var (
	// ClaudeCredsConfig extracts credentials for Claude API.
	// Uses standard access_token from metadata.
	ClaudeCredsConfig = CredExtractorConfig{
		MetadataTokenKey: "access_token",
	}

	// CodexCredsConfig extracts credentials for Codex API.
	// Uses standard access_token from metadata.
	CodexCredsConfig = CredExtractorConfig{
		MetadataTokenKey: "access_token",
	}

	// ClineCredsConfig extracts credentials for Cline API.
	// Uses access_token from metadata and adds "workos:" prefix.
	ClineCredsConfig = CredExtractorConfig{
		MetadataTokenKey: "access_token",
		TokenPrefix:      "workos:",
	}

	// QwenCredsConfig extracts credentials for Qwen API.
	// Uses access_token from metadata and transforms resource_url to full API URL.
	QwenCredsConfig = CredExtractorConfig{
		MetadataTokenKey: "access_token",
		MetadataURLKey:   "resource_url",
		URLTransformFunc: func(url string) string {
			return "https://" + url + "/v1"
		},
	}

	// IFlowCredsConfig extracts credentials for iFlow API.
	// Uses api_key from metadata and enables whitespace trimming.
	IFlowCredsConfig = CredExtractorConfig{
		MetadataTokenKey: "api_key",
		MetadataURLKey:   "base_url",
		TrimWhitespace:   true,
	}

	// GeminiCredsConfig extracts credentials for Gemini API.
	// Uses access_token from metadata and checks nested token map structure.
	GeminiCredsConfig = CredExtractorConfig{
		MetadataTokenKey:    "access_token",
		CheckNestedTokenMap: true,
	}
)

Functions

func DefaultGeminiAlias added in v1.0.34

func DefaultGeminiAlias(upstreamName string) string

DefaultGeminiAlias returns the model ID as-is (no transformation).

func ExtractCreds added in v1.0.26

func ExtractCreds(a *auth.Auth, cfg CredExtractorConfig) (token, url string)

ExtractCreds extracts credentials from an auth object using the provided configuration. Returns (token, url) where token may include prefix and url may be transformed. This centralizes credential extraction logic across all executor implementations, eliminating duplicate extraction patterns while allowing provider-specific customization.

func ExtractRefreshToken added in v1.0.30

func ExtractRefreshToken(auth *auth.Auth) (string, bool)

ExtractRefreshToken extracts refresh token from auth metadata. Returns empty string and false if not found.

func FetchAIStudioModels added in v1.0.34

func FetchAIStudioModels(ctx context.Context, auth *cliproxyauth.Auth, relay *wsrelay.Manager) []*registry.ModelInfo

FetchAIStudioModels retrieves available models via websocket relay.

func FetchAntigravityModels

func FetchAntigravityModels(ctx context.Context, auth *cliproxyauth.Auth, cfg *config.Config) []*registry.ModelInfo

FetchAntigravityModels retrieves available models using the supplied auth.

func FetchCloudCodeModels added in v1.0.34

func FetchCloudCodeModels(ctx context.Context, httpClient *http.Client, cfg CloudCodeFetchConfig) []*registry.ModelInfo

FetchCloudCodeModels fetches models from Cloud Code Assist endpoint. This is shared between Antigravity and Gemini CLI providers.

func FetchGLAPIModels added in v1.0.34

func FetchGLAPIModels(ctx context.Context, httpClient *http.Client, cfg GLAPIFetchConfig) []*registry.ModelInfo

FetchGLAPIModels fetches models from the Generative Language API. Used by Gemini (API key), Vertex (API key mode), and AIStudio providers.

func FetchGeminiCLIModels added in v1.0.34

func FetchGeminiCLIModels(ctx context.Context, auth *cliproxyauth.Auth, cfg *config.Config) []*registry.ModelInfo

FetchGeminiCLIModels retrieves available models from Cloud Code Assist. Uses OAuth token authentication.

func FetchGeminiModels added in v1.0.34

func FetchGeminiModels(ctx context.Context, auth *cliproxyauth.Auth, cfg *config.Config) []*registry.ModelInfo

FetchGeminiModels retrieves available models from the Generative Language API. Uses API key or OAuth bearer token authentication.

func FetchVertexModels added in v1.0.34

func FetchVertexModels(ctx context.Context, auth *cliproxyauth.Auth, cfg *config.Config) []*registry.ModelInfo

FetchVertexModels retrieves available models from Vertex AI. Supports both API key and service account authentication.

func FilterSSEUsageMetadata

func FilterSSEUsageMetadata(payload []byte) []byte

FilterSSEUsageMetadata removes usageMetadata from SSE events that are not terminal (finishReason != "stop"). Stop chunks are left untouched. This function is shared between aistudio and antigravity executors.

func ParseCloudCodeModels added in v1.0.34

func ParseCloudCodeModels(body []byte, providerType string, aliasFunc ModelAliasFunc) []*registry.ModelInfo

ParseCloudCodeModels parses Cloud Code Assist response format. Response format: {"models": {"model-id": {...}, ...}} (MAP)

func ParseGLAPIModels added in v1.0.34

func ParseGLAPIModels(body []byte, providerType string) []*registry.ModelInfo

ParseGLAPIModels parses Generative Language API response format. Response format: {"models": [{...}, ...]} (ARRAY)

func ParseQuotaRetryDelay added in v1.0.21

func ParseQuotaRetryDelay(errorBody []byte) *time.Duration

ParseQuotaRetryDelay extracts the full quota reset delay from a Google API 429 error response. Unlike parseRetryDelay which is used for short-term retries (capped at 20s), this function returns the actual quota reset time which can be hours. It checks multiple sources in order of preference:

  1. RetryInfo.retryDelay (e.g., "7118.204539195s") - most accurate
  2. ErrorInfo.metadata.quotaResetDelay (e.g., "1h58m38.204539195s") - human-readable format

Returns nil if no quota delay information is found.

func RunSSEStream added in v1.0.30

func RunSSEStream(
	ctx context.Context,
	body io.ReadCloser,
	reporter *usageReporter,
	processor StreamProcessor,
	cfg StreamConfig,
) <-chan cliproxyexecutor.StreamChunk

RunSSEStream processes an SSE stream from the given body using the provided processor. It handles buffering, context cancellation, error reporting, and usage tracking.

Parameters:

  • ctx: Context for cancellation
  • body: The HTTP response body to read from (will be closed when done)
  • reporter: Usage reporter for tracking token usage (may be nil)
  • processor: The StreamProcessor implementation for this provider
  • cfg: Configuration options for stream processing

Returns a channel that emits StreamChunk values. The channel is closed when the stream ends or an error occurs.

func StripUsageMetadataFromJSON

func StripUsageMetadataFromJSON(rawJSON []byte) ([]byte, bool)

StripUsageMetadataFromJSON drops usageMetadata unless finishReason is present (terminal). It handles both formats: - Aistudio: candidates.0.finishReason - Antigravity: response.candidates.0.finishReason

func TranslateClaudeResponseNonStream

func TranslateClaudeResponseNonStream(cfg *config.Config, to sdktranslator.Format, claudeResponse []byte, model string) ([]byte, error)

func TranslateClaudeResponseStream

func TranslateClaudeResponseStream(cfg *config.Config, to sdktranslator.Format, claudeChunk []byte, model string, messageID string, state *from_ir.ClaudeStreamState) ([][]byte, error)

TranslateClaudeResponseStream converts Claude streaming chunk to target format.

func TranslateCodexResponseNonStream

func TranslateCodexResponseNonStream(cfg *config.Config, to sdktranslator.Format, codexResponse []byte, model string) ([]byte, error)

TranslateCodexResponseNonStream converts Codex (Responses API) non-streaming response to target format. Returns nil if new translator is disabled (caller should use old translator as fallback).

func TranslateCodexResponseStream

func TranslateCodexResponseStream(cfg *config.Config, to sdktranslator.Format, codexChunk []byte, model string, messageID string, state *CodexStreamState) ([][]byte, error)

TranslateCodexResponseStream converts Codex (Responses API) streaming chunk to target format.

func TranslateGeminiCLIResponseNonStream

func TranslateGeminiCLIResponseNonStream(cfg *config.Config, to sdktranslator.Format, geminiResponse []byte, model string) ([]byte, error)

TranslateGeminiCLIResponseNonStream converts Gemini CLI non-streaming response to target format.

func TranslateGeminiCLIResponseStream

func TranslateGeminiCLIResponseStream(cfg *config.Config, to sdktranslator.Format, geminiChunk []byte, model string, messageID string, state *GeminiCLIStreamState) ([][]byte, error)

TranslateGeminiCLIResponseStream converts Gemini CLI streaming chunk to target format. state parameter is optional but recommended for stateful conversions (e.g., Claude tool calls).

func TranslateGeminiResponseNonStream

func TranslateGeminiResponseNonStream(cfg *config.Config, to sdktranslator.Format, geminiResponse []byte, model string) ([]byte, error)

TranslateGeminiResponseNonStream converts Gemini (AI Studio) non-streaming response to target format.

func TranslateGeminiResponseStream

func TranslateGeminiResponseStream(cfg *config.Config, to sdktranslator.Format, geminiChunk []byte, model string, messageID string, state *GeminiCLIStreamState) ([][]byte, error)

TranslateGeminiResponseStream converts Gemini (AI Studio) streaming chunk to target format.

func TranslateOpenAIResponseNonStream

func TranslateOpenAIResponseNonStream(cfg *config.Config, to sdktranslator.Format, openaiResponse []byte, model string) ([]byte, error)

TranslateOpenAIResponseNonStream converts OpenAI non-streaming response to target format.

func TranslateOpenAIResponseStream

func TranslateOpenAIResponseStream(cfg *config.Config, to sdktranslator.Format, openaiChunk []byte, model string, messageID string, state *OpenAIStreamState) ([][]byte, error)

TranslateOpenAIResponseStream converts OpenAI streaming chunk to target format. This is used for OpenAI-compatible providers (like Ollama) to ensure reasoning_tokens is properly set.

func TranslateToClaude

func TranslateToClaude(cfg *config.Config, from sdktranslator.Format, model string, payload []byte, streaming bool, metadata map[string]any) ([]byte, error)

TranslateToClaude converts request to Claude API format.

func TranslateToCodex

func TranslateToCodex(cfg *config.Config, from sdktranslator.Format, model string, payload []byte, streaming bool, metadata map[string]any) ([]byte, error)

TranslateToCodex converts request to OpenAI Responses API format (Codex). metadata contains additional context like thinking overrides from request metadata.

func TranslateToGemini

func TranslateToGemini(cfg *config.Config, from sdktranslator.Format, model string, payload []byte, streaming bool, metadata map[string]any) ([]byte, error)

TranslateToGemini converts request to Gemini (AI Studio API) format. metadata contains additional context like thinking overrides from request metadata. This is a convenience wrapper around TranslateToGeminiWithTokens that discards token count.

func TranslateToGeminiCLI

func TranslateToGeminiCLI(cfg *config.Config, from sdktranslator.Format, model string, payload []byte, streaming bool, metadata map[string]any) ([]byte, error)

TranslateToGeminiCLI converts request to Gemini CLI format using canonical IR translator. Note: Antigravity uses the same format as Gemini CLI, so this function works for both. This is a convenience wrapper around TranslateToGeminiCLIWithTokens that discards token count.

func TranslateToOpenAI

func TranslateToOpenAI(cfg *config.Config, from sdktranslator.Format, model string, payload []byte, streaming bool, metadata map[string]any) ([]byte, error)

TranslateToOpenAI converts request to OpenAI Chat Completions API format. metadata contains additional context like thinking overrides from request metadata.

func TranslateTokenCount

func TranslateTokenCount(ctx context.Context, to, from sdktranslator.Format, count int64, usageJSON []byte) string

TranslateTokenCount converts token count response to target format. This delegates to sdktranslator.TranslateTokenCount since token count translation doesn't require IR-based conversion.

func UpdateRefreshMetadata added in v1.0.30

func UpdateRefreshMetadata(auth *auth.Auth, updates map[string]any, providerType string)

UpdateRefreshMetadata updates common metadata fields after token refresh. The updates map should contain provider-specific fields like "access_token", "refresh_token", etc. This function automatically adds "type" and "last_refresh" fields.

Types

type AIStudioExecutor

type AIStudioExecutor struct {
	// contains filtered or unexported fields
}

AIStudioExecutor routes AI Studio requests through a websocket-backed transport.

func NewAIStudioExecutor

func NewAIStudioExecutor(cfg *config.Config, provider string, relay *wsrelay.Manager) *AIStudioExecutor

NewAIStudioExecutor constructs a websocket executor for the provider name.

func (*AIStudioExecutor) Execute

func (*AIStudioExecutor) ExecuteStream

func (*AIStudioExecutor) Identifier

func (e *AIStudioExecutor) Identifier() string

Identifier returns the logical provider key for routing.

func (*AIStudioExecutor) PrepareRequest

func (e *AIStudioExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

PrepareRequest is a no-op because websocket transport already injects headers.

func (*AIStudioExecutor) Refresh

type AntigravityExecutor

type AntigravityExecutor struct {
	// contains filtered or unexported fields
}

AntigravityExecutor proxies requests to the antigravity upstream.

func NewAntigravityExecutor

func NewAntigravityExecutor(cfg *config.Config) *AntigravityExecutor

NewAntigravityExecutor constructs a new executor instance.

func (*AntigravityExecutor) CountTokens

CountTokens is not supported for the antigravity provider.

func (*AntigravityExecutor) Execute

Execute handles non-streaming requests via the antigravity generate endpoint.

func (*AntigravityExecutor) ExecuteStream

ExecuteStream handles streaming requests via the antigravity upstream.

func (*AntigravityExecutor) Identifier

func (e *AntigravityExecutor) Identifier() string

Identifier implements ProviderExecutor.

func (*AntigravityExecutor) PrepareRequest

func (e *AntigravityExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

PrepareRequest implements ProviderExecutor.

func (*AntigravityExecutor) Refresh

Refresh refreshes the OAuth token using the refresh token.

type BaseExecutor added in v1.0.30

type BaseExecutor struct {
	Cfg *config.Config
}

BaseExecutor provides common functionality that all provider executors can embed. It contains the shared configuration and provides default implementations for commonly used methods across all executors.

Usage:

type MyExecutor struct {
    BaseExecutor
}

func NewMyExecutor(cfg *config.Config) *MyExecutor {
    return &MyExecutor{BaseExecutor: BaseExecutor{Cfg: cfg}}
}

func (*BaseExecutor) ApplyPayloadConfig added in v1.0.30

func (b *BaseExecutor) ApplyPayloadConfig(model string, payload []byte) []byte

ApplyPayloadConfig applies payload default and override rules from configuration.

func (*BaseExecutor) Config added in v1.0.30

func (b *BaseExecutor) Config() *config.Config

func (*BaseExecutor) CountTokensNotSupported added in v1.0.30

func (b *BaseExecutor) CountTokensNotSupported(provider string) (cliproxyexecutor.Response, error)

CountTokensNotSupported returns a NotImplemented error for executors that don't support token counting.

func (*BaseExecutor) NewHTTPClient added in v1.0.30

func (b *BaseExecutor) NewHTTPClient(ctx context.Context, auth *cliproxyauth.Auth, timeout time.Duration) *http.Client

NewHTTPClient creates an HTTP client with proper proxy configuration.

func (*BaseExecutor) NewUsageReporter added in v1.0.30

func (b *BaseExecutor) NewUsageReporter(ctx context.Context, provider, model string, auth *cliproxyauth.Auth) *usageReporter

NewUsageReporter creates a new usage reporter for tracking API usage.

func (*BaseExecutor) PrepareRequest added in v1.0.30

func (b *BaseExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

PrepareRequest prepares the HTTP request for execution. This default implementation is a no-op that returns nil.

This method exists in the ProviderExecutor interface to support:

  • Backward compatibility: SDK users may have external executors that override this method
  • Future extensibility: Allows injecting credentials/headers into raw HTTP requests before execution (see sdk/cliproxy/auth/manager.go:RequestPreparer interface)
  • Custom providers: External providers using the SDK can override this to add custom authentication or request modification logic

Note: All internal executors currently return nil (no-op) because they handle request preparation inline within Execute/ExecuteStream methods. The interface method is retained for SDK API stability and custom provider support.

See also: sdk/cliproxy/auth/manager.go:InjectCredentials which calls this method through the RequestPreparer interface for SDK consumers.

func (*BaseExecutor) RefreshNoOp added in v1.0.30

func (b *BaseExecutor) RefreshNoOp(_ context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error)

RefreshNoOp is a no-op refresh for executors that don't require token refresh.

type ClaudeExecutor

type ClaudeExecutor struct {
	// contains filtered or unexported fields
}

ClaudeExecutor is a stateless executor for Anthropic Claude over the messages API. If api_key is unavailable on auth, it falls back to legacy via ClientAdapter.

func NewClaudeExecutor

func NewClaudeExecutor(cfg *config.Config) *ClaudeExecutor

func (*ClaudeExecutor) Execute

func (*ClaudeExecutor) ExecuteStream

func (*ClaudeExecutor) Identifier

func (e *ClaudeExecutor) Identifier() string

func (*ClaudeExecutor) PrepareRequest

func (e *ClaudeExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

func (*ClaudeExecutor) Refresh

type ClineExecutor

type ClineExecutor struct {
	// contains filtered or unexported fields
}

ClineExecutor is a stateless executor for Cline API using OpenAI-compatible chat completions.

func NewClineExecutor

func NewClineExecutor(cfg *config.Config) *ClineExecutor

NewClineExecutor creates a new Cline executor instance.

func (*ClineExecutor) CountTokens

CountTokens counts tokens in the request for Cline models.

func (*ClineExecutor) Execute

Execute performs a non-streaming request to Cline API.

func (*ClineExecutor) ExecuteStream

func (e *ClineExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (stream <-chan cliproxyexecutor.StreamChunk, err error)

ExecuteStream performs a streaming request to Cline API.

func (*ClineExecutor) Identifier

func (e *ClineExecutor) Identifier() string

Identifier returns the provider identifier for this executor.

func (*ClineExecutor) PrepareRequest

func (e *ClineExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

PrepareRequest prepares the HTTP request with necessary headers and authentication.

func (*ClineExecutor) Refresh

func (e *ClineExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error)

Refresh refreshes the Cline authentication tokens.

type CloudCodeFetchConfig added in v1.0.34

type CloudCodeFetchConfig struct {
	BaseURLs     []string       // Fallback order of base URLs
	Token        string         // Bearer token
	ProviderType string         // Provider type (e.g., "antigravity", "gemini-cli")
	UserAgent    string         // User-Agent header
	Host         string         // Optional Host header override
	AliasFunc    ModelAliasFunc // Function to convert upstream name to alias
}

CloudCodeFetchConfig holds configuration for fetching models from Cloud Code Assist.

type CodexExecutor

type CodexExecutor struct {
	// contains filtered or unexported fields
}

CodexExecutor is a stateless executor for Codex (OpenAI Responses API entrypoint). If api_key is unavailable on auth, it falls back to legacy via ClientAdapter.

func NewCodexExecutor

func NewCodexExecutor(cfg *config.Config) *CodexExecutor

func (*CodexExecutor) Execute

func (*CodexExecutor) ExecuteStream

func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (stream <-chan cliproxyexecutor.StreamChunk, err error)

func (*CodexExecutor) Identifier

func (e *CodexExecutor) Identifier() string

func (*CodexExecutor) PrepareRequest

func (e *CodexExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

func (*CodexExecutor) Refresh

func (e *CodexExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error)

type CodexStreamState

type CodexStreamState struct {
	ResponsesState *from_ir.ResponsesStreamState
	ClaudeState    *from_ir.ClaudeStreamState
	ToolCallIndex  int
}

CodexStreamState maintains state for Codex (Responses API) streaming conversions.

type CredExtractorConfig added in v1.0.26

type CredExtractorConfig struct {
	// MetadataTokenKey specifies the key in auth.Metadata to extract token from.
	// Common values: "access_token", "api_key"
	MetadataTokenKey string

	// MetadataURLKey specifies the key in auth.Metadata to extract base URL from.
	// Common values: "base_url", "resource_url" (optional)
	MetadataURLKey string

	// TokenPrefix is prepended to extracted tokens (e.g., "workos:" for Cline).
	TokenPrefix string

	// URLTransformFunc transforms the extracted URL if needed.
	// Example: resource_url -> "https://<url>/v1" for Qwen
	URLTransformFunc func(string) string

	// CheckNestedTokenMap enables checking auth.Metadata["token"].(map[string]any)["access_token"]
	// Used by Gemini which stores tokens in a nested map structure.
	CheckNestedTokenMap bool

	// TrimWhitespace enables strings.TrimSpace() on extracted values.
	// Used by iFlow provider.
	TrimWhitespace bool
}

CredExtractorConfig configures credential extraction behavior. This allows different providers to customize how credentials are extracted from auth objects.

type EventPreprocessor added in v1.0.21

type EventPreprocessor func(event *ir.UnifiedEvent, state *StreamState) (skip bool)

EventPreprocessor is called before each event is converted. It allows format-specific state tracking and event modification. Returns true if the event should be skipped (not converted).

type GLAPIFetchConfig added in v1.0.34

type GLAPIFetchConfig struct {
	BaseURL      string // Base URL (e.g., "https://generativelanguage.googleapis.com")
	APIKey       string // API key (mutually exclusive with Bearer)
	Bearer       string // Bearer token (mutually exclusive with APIKey)
	ProviderType string // Provider type (e.g., "gemini", "vertex", "aistudio")
}

GLAPIFetchConfig holds configuration for fetching models from Generative Language API.

type GeminiCLIExecutor

type GeminiCLIExecutor struct {
	// contains filtered or unexported fields
}

GeminiCLIExecutor talks to the Cloud Code Assist endpoint using OAuth credentials from auth metadata.

func NewGeminiCLIExecutor

func NewGeminiCLIExecutor(cfg *config.Config) *GeminiCLIExecutor

func (*GeminiCLIExecutor) Execute

func (*GeminiCLIExecutor) ExecuteStream

func (*GeminiCLIExecutor) Identifier

func (e *GeminiCLIExecutor) Identifier() string

func (*GeminiCLIExecutor) PrepareRequest

func (e *GeminiCLIExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

func (*GeminiCLIExecutor) Refresh

type GeminiCLIStreamState

type GeminiCLIStreamState struct {
	ClaudeState          *from_ir.ClaudeStreamState
	ToolCallIndex        int // Track tool call index across chunks for OpenAI format
	ReasoningTokensCount int // Track accumulated reasoning tokens for final usage chunk
	ReasoningCharsAccum  int // Track accumulated reasoning characters (for estimation if provider doesn't give count)

	ToolSchemaCtx *ir.ToolSchemaContext // Schema context for normalizing tool call parameters
	FinishSent    bool                  // Track if finish event was already sent (prevent duplicates)
	HasToolCalls  bool                  // Track if any tool calls were seen across chunks (for correct finish_reason)
}

GeminiCLIStreamState maintains state for stateful streaming conversions (e.g., Claude tool calls).

func NewAntigravityStreamState

func NewAntigravityStreamState(originalRequest []byte) *GeminiCLIStreamState

NewAntigravityStreamState creates a new stream state with tool schema context for Antigravity provider. Antigravity has a known issue where Gemini ignores tool parameter schemas and returns different parameter names (e.g., "path" instead of "target_file"). This function extracts the expected schema from the original request to normalize responses. Uses gjson for efficient extraction without full JSON unmarshaling.

type GeminiExecutor

type GeminiExecutor struct {
	// contains filtered or unexported fields
}

GeminiExecutor is a stateless executor for the official Gemini API using API keys.

func NewGeminiExecutor

func NewGeminiExecutor(cfg *config.Config) *GeminiExecutor

NewGeminiExecutor creates a new Gemini executor instance. Parameters:

  • cfg: The application configuration

Returns:

  • *GeminiExecutor: A new Gemini executor instance

func (*GeminiExecutor) Execute

Execute performs a non-streaming request to the Gemini API. It translates the request to Gemini format, sends it to the API, and translates the response back to the requested format. Parameters:

  • ctx: The context for the request
  • auth: The authentication information
  • req: The request to execute
  • opts: Additional execution options

Returns:

  • cliproxyexecutor.Response: The response from the API
  • error: An error if the request fails

func (*GeminiExecutor) ExecuteStream

func (*GeminiExecutor) Identifier

func (e *GeminiExecutor) Identifier() string

Identifier returns the executor identifier for Gemini.

func (*GeminiExecutor) PrepareRequest

func (e *GeminiExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

PrepareRequest prepares the HTTP request for execution (no-op for Gemini).

func (*GeminiExecutor) Refresh

type GeminiVertexExecutor

type GeminiVertexExecutor struct {
	// contains filtered or unexported fields
}

GeminiVertexExecutor sends requests to Vertex AI Gemini endpoints.

It supports two authentication modes via the Strategy pattern:

  • Service Account: Uses OAuth2 token exchange for project-scoped access
  • API Key: Uses static API key for AI Studio access

The executor translates requests from OpenAI/Claude format to Gemini format, handles streaming via SSE, and provides token counting capabilities.

func NewGeminiVertexExecutor

func NewGeminiVertexExecutor(cfg *config.Config) *GeminiVertexExecutor

NewGeminiVertexExecutor constructs the Vertex executor.

func (*GeminiVertexExecutor) CountTokens

CountTokens calls Vertex countTokens endpoint.

func (*GeminiVertexExecutor) Execute

Execute handles non-streaming requests.

func (*GeminiVertexExecutor) ExecuteStream

ExecuteStream handles SSE streaming for Vertex.

func (*GeminiVertexExecutor) Identifier

func (e *GeminiVertexExecutor) Identifier() string

Identifier returns provider key for manager routing.

func (*GeminiVertexExecutor) PrepareRequest

func (e *GeminiVertexExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

PrepareRequest is a no-op for Vertex.

func (*GeminiVertexExecutor) Refresh

Refresh is a no-op for service account based credentials.

type GitHubCopilotExecutor

type GitHubCopilotExecutor struct {
	// contains filtered or unexported fields
}

GitHubCopilotExecutor handles requests to the GitHub Copilot API.

func NewGitHubCopilotExecutor

func NewGitHubCopilotExecutor(cfg *config.Config) *GitHubCopilotExecutor

func (*GitHubCopilotExecutor) Execute

func (*GitHubCopilotExecutor) ExecuteStream

func (*GitHubCopilotExecutor) Identifier

func (e *GitHubCopilotExecutor) Identifier() string

func (*GitHubCopilotExecutor) PrepareRequest

func (e *GitHubCopilotExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

func (*GitHubCopilotExecutor) Refresh

type HTTPErrorResult added in v1.0.26

type HTTPErrorResult struct {
	Error      error
	StatusCode int
	Body       []byte
}

HTTPErrorResult contains the result of handling an HTTP error response. This standardizes error handling across all executors.

func HandleHTTPError added in v1.0.26

func HandleHTTPError(resp *http.Response, executorName string) HTTPErrorResult

HandleHTTPError reads error response body and returns categorized error. NOTE: This function does NOT close the response body. The caller is responsible for closing the body (typically via defer). This avoids double-close bugs when callers already have defer resp.Body.Close() set up. Parameters:

  • resp: HTTP response to handle
  • executorName: Name of the executor for logging (e.g., "claude executor")

Returns:

  • HTTPErrorResult with categorized error, status code, and body

All executors should use this function instead of manual error handling to ensure: - Consistent error categorization - Standardized logging

type IFlowExecutor

type IFlowExecutor struct {
	// contains filtered or unexported fields
}

IFlowExecutor executes OpenAI-compatible chat completions against the iFlow API using API keys derived from OAuth.

func NewIFlowExecutor

func NewIFlowExecutor(cfg *config.Config) *IFlowExecutor

NewIFlowExecutor constructs a new executor instance.

func (*IFlowExecutor) Execute

Execute performs a non-streaming chat completion request.

func (*IFlowExecutor) ExecuteStream

func (e *IFlowExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (stream <-chan cliproxyexecutor.StreamChunk, err error)

ExecuteStream performs a streaming chat completion request.

func (*IFlowExecutor) Identifier

func (e *IFlowExecutor) Identifier() string

Identifier returns the provider key.

func (*IFlowExecutor) PrepareRequest

func (e *IFlowExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

PrepareRequest implements ProviderExecutor but requires no preprocessing.

func (*IFlowExecutor) Refresh

func (e *IFlowExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error)

Refresh refreshes OAuth tokens or cookie-based API keys and updates the stored API key.

type KiroExecutor

type KiroExecutor struct {
	// contains filtered or unexported fields
}

func NewKiroExecutor

func NewKiroExecutor(cfg *config.Config) *KiroExecutor

func (*KiroExecutor) CountTokens

func (*KiroExecutor) ExecuteStream

func (*KiroExecutor) Identifier

func (e *KiroExecutor) Identifier() string

func (*KiroExecutor) Refresh

func (e *KiroExecutor) Refresh(ctx context.Context, auth *coreauth.Auth) (*coreauth.Auth, error)

type ModelAliasFunc added in v1.0.34

type ModelAliasFunc func(upstreamName string) string

ModelAliasFunc converts upstream model name to user-facing alias. Returns empty string if the model should be hidden.

type OpenAICompatExecutor

type OpenAICompatExecutor struct {
	// contains filtered or unexported fields
}

OpenAICompatExecutor implements a stateless executor for OpenAI-compatible providers. It performs request/response translation and executes against the provider base URL using per-auth credentials (API key) and per-auth HTTP transport (proxy) from context.

func NewOpenAICompatExecutor

func NewOpenAICompatExecutor(provider string, cfg *config.Config) *OpenAICompatExecutor

NewOpenAICompatExecutor creates an executor bound to a provider key (e.g., "openrouter").

func (*OpenAICompatExecutor) CountTokens

func (*OpenAICompatExecutor) Execute

func (*OpenAICompatExecutor) ExecuteStream

func (*OpenAICompatExecutor) Identifier

func (e *OpenAICompatExecutor) Identifier() string

Identifier implements cliproxyauth.ProviderExecutor.

func (*OpenAICompatExecutor) PrepareRequest

func (e *OpenAICompatExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

PrepareRequest is a no-op for now (credentials are added via headers at execution time).

func (*OpenAICompatExecutor) Refresh

Refresh is a no-op for API-key based compatibility providers.

type OpenAIStreamState

type OpenAIStreamState struct {
	ReasoningCharsAccum int // Track accumulated reasoning characters for token estimation
}

OpenAIStreamState maintains state for OpenAI → OpenAI streaming conversions.

type QwenExecutor

type QwenExecutor struct {
	// contains filtered or unexported fields
}

QwenExecutor is a stateless executor for Qwen Code using OpenAI-compatible chat completions. If access token is unavailable, it falls back to legacy via ClientAdapter.

func NewQwenExecutor

func NewQwenExecutor(cfg *config.Config) *QwenExecutor

func (*QwenExecutor) Execute

func (*QwenExecutor) ExecuteStream

func (e *QwenExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (stream <-chan cliproxyexecutor.StreamChunk, err error)

func (*QwenExecutor) Identifier

func (e *QwenExecutor) Identifier() string

func (*QwenExecutor) PrepareRequest

func (e *QwenExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error

func (*QwenExecutor) Refresh

func (e *QwenExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error)

type RetryAction added in v1.0.30

type RetryAction int

RetryAction represents the next action to take

const (
	RetryActionSuccess      RetryAction = iota // Request succeeded
	RetryActionContinueNext                    // Fallback to next target
	RetryActionRetryCurrent                    // Retry current target after delay
	RetryActionFail                            // All retries exhausted, fail
)

func (RetryAction) String added in v1.0.30

func (a RetryAction) String() string

String returns a string representation of the RetryAction

type RetryConfig added in v1.0.30

type RetryConfig struct {
	MaxRetries       int           // Max retries per target (default: 1)
	BaseDelay        time.Duration // Exponential backoff base (default: 1s)
	MaxDelay         time.Duration // Cap per-retry delay (default: 20s)
	RetryStatusCodes []int         // Status codes to retry (default: 429)
	FallbackCodes    []int         // Codes to fallback to next target (default: 429, 503)
	RetryOnErrors    bool          // Retry on network errors
}

RetryConfig defines retry behavior for multi-target execution

func AntigravityRetryConfig added in v1.0.30

func AntigravityRetryConfig() RetryConfig

AntigravityRetryConfig returns config matching Antigravity behavior Antigravity uses more aggressive retry with longer delays for quota limits

func DefaultRetryConfig added in v1.0.30

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns sensible defaults for retry behavior

type RetryHandler added in v1.0.30

type RetryHandler struct {
	// contains filtered or unexported fields
}

RetryHandler manages retry logic with multi-target fallback

func NewRetryHandler added in v1.0.30

func NewRetryHandler(cfg RetryConfig) *RetryHandler

NewRetryHandler creates a new RetryHandler with the given config

func (*RetryHandler) Config added in v1.0.30

func (h *RetryHandler) Config() RetryConfig

Config returns the current retry configuration

func (*RetryHandler) HandleError added in v1.0.30

func (h *RetryHandler) HandleError(ctx context.Context, err error, hasNextTarget bool) (RetryAction, error)

HandleError evaluates a network/transport error and returns the appropriate action.

func (*RetryHandler) HandleResponse added in v1.0.30

func (h *RetryHandler) HandleResponse(ctx context.Context, statusCode int, body []byte, hasNextTarget bool) (RetryAction, error)

HandleResponse evaluates an HTTP response and returns the appropriate action. It considers status codes, retry configuration, and whether a fallback target is available.

func (*RetryHandler) Reset added in v1.0.30

func (h *RetryHandler) Reset()

Reset resets the retry counter for a new request cycle

func (*RetryHandler) RetryCount added in v1.0.30

func (h *RetryHandler) RetryCount() int

RetryCount returns the current retry count

type SimpleStreamProcessor added in v1.0.30

type SimpleStreamProcessor struct {
	// ProcessFunc processes a single line and returns chunks and usage.
	ProcessFunc func(line []byte) (chunks [][]byte, usage *ir.Usage, err error)
}

SimpleStreamProcessor is a convenience wrapper for simple processing functions. It implements StreamProcessor for cases where ProcessDone is not needed.

func NewSimpleStreamProcessor added in v1.0.30

func NewSimpleStreamProcessor(fn func(line []byte) (chunks [][]byte, usage *ir.Usage, err error)) *SimpleStreamProcessor

NewSimpleStreamProcessor creates a SimpleStreamProcessor from a processing function.

func (*SimpleStreamProcessor) ProcessDone added in v1.0.30

func (p *SimpleStreamProcessor) ProcessDone() ([][]byte, error)

func (*SimpleStreamProcessor) ProcessLine added in v1.0.30

func (p *SimpleStreamProcessor) ProcessLine(line []byte) ([][]byte, *ir.Usage, error)

type StatusError added in v1.0.30

type StatusError struct {
	// contains filtered or unexported fields
}

StatusError represents an error with HTTP status code, message, optional retry-after, and error category for consistent error handling across all executors.

func NewAuthError added in v1.0.30

func NewAuthError(msg string) StatusError

NewAuthError creates a StatusError for authentication failures (401).

func NewInternalError added in v1.0.30

func NewInternalError(msg string) StatusError

NewInternalError creates a StatusError for internal server errors (500).

func NewNotImplementedError added in v1.0.30

func NewNotImplementedError(msg string) StatusError

NewNotImplementedError creates a StatusError for not implemented features (501).

func NewStatusError added in v1.0.30

func NewStatusError(code int, msg string, retryAfter *time.Duration) StatusError

NewStatusError creates a StatusError with automatic category classification.

func NewTimeoutError added in v1.0.30

func NewTimeoutError(msg string) StatusError

NewTimeoutError creates a StatusError for timeout errors (408).

func (StatusError) Category added in v1.0.30

func (e StatusError) Category() cliproxyauth.ErrorCategory

func (StatusError) Error added in v1.0.30

func (e StatusError) Error() string

func (StatusError) RetryAfter added in v1.0.30

func (e StatusError) RetryAfter() *time.Duration

func (StatusError) StatusCode added in v1.0.30

func (e StatusError) StatusCode() int

func (StatusError) Unwrap added in v1.0.30

func (e StatusError) Unwrap() error

Unwrap returns nil as StatusError doesn't wrap another error.

type StreamConfig added in v1.0.30

type StreamConfig struct {
	// ExecutorName identifies the executor for logging purposes.
	ExecutorName string

	// MaxBufferSize is the maximum buffer size for the scanner (default: DefaultStreamBufferSize).
	MaxBufferSize int

	// Preprocessor is an optional function to pre-process lines before the main processor.
	Preprocessor StreamPreprocessor

	// SkipEmptyLines skips lines that are empty after preprocessing.
	SkipEmptyLines bool

	// PassthroughOnEmpty sends the raw line if processor returns no chunks.
	// Useful for passthrough scenarios where translation may not produce output.
	PassthroughOnEmpty bool

	// EnsurePublished calls reporter.ensurePublished at the end of successful streams.
	// Use this when the upstream may not always return usage information.
	EnsurePublished bool

	// HandleDoneSignal calls ProcessDone() when [DONE] is encountered.
	HandleDoneSignal bool

	// SkipDoneInData skips lines that contain "data: [DONE]" (OpenAI-style done signal).
	SkipDoneInData bool
}

StreamConfig configures the behavior of RunSSEStream.

type StreamPreprocessor added in v1.0.30

type StreamPreprocessor func(line []byte) (payload []byte, skip bool)

StreamPreprocessor is a function that pre-processes SSE lines before the main processor. It can transform or filter lines before they reach the StreamProcessor. Returns:

  • payload: The preprocessed payload (may be modified or unchanged)
  • skip: If true, skip this line entirely (don't pass to processor)

func DataTagPreprocessor added in v1.0.30

func DataTagPreprocessor() StreamPreprocessor

DataTagPreprocessor creates a preprocessor that strips the "data: " prefix. It's suitable for standard SSE streams like OpenAI/Codex.

func GeminiPreprocessor added in v1.0.30

func GeminiPreprocessor() StreamPreprocessor

GeminiPreprocessor creates a preprocessor for Gemini/Antigravity streams. It applies FilterSSEUsageMetadata, extracts JSON payload, and validates JSON.

type StreamProcessor added in v1.0.30

type StreamProcessor interface {
	// ProcessLine processes a single SSE line and returns translated chunks.
	// The line parameter contains raw bytes from the scanner (may include "data:" prefix).
	// Returns:
	//   - chunks: Zero or more translated output chunks to send to client
	//   - usage: Optional usage information extracted from this line
	//   - err: Processing error (terminates the stream if non-nil)
	ProcessLine(line []byte) (chunks [][]byte, usage *ir.Usage, err error)

	// ProcessDone handles the [DONE] signal (optional cleanup/final chunks).
	// Called when the stream encounters a done signal, if HandleDoneSignal is true.
	ProcessDone() (chunks [][]byte, err error)
}

StreamProcessor defines the interface for processing SSE stream lines. Implementations handle provider-specific parsing and translation logic.

type StreamState added in v1.0.21

type StreamState struct {
	ClaudeState         *from_ir.ClaudeStreamState
	ToolCallIndex       int  // Track tool call index across chunks for OpenAI format
	ReasoningCharsAccum int  // Track accumulated reasoning characters (for estimation)
	FinishSent          bool // Track if finish event was already sent (prevent duplicates)
	HasToolCalls        bool // Track if any tool calls were seen (for correct finish_reason)
	ToolSchemaCtx       *ir.ToolSchemaContext
}

StreamState holds unified state for all streaming conversions. This reduces code duplication by providing a single state structure that can be used across different streaming translation functions.

func NewStreamState added in v1.0.21

func NewStreamState() *StreamState

NewStreamState creates a new stream state with optional tool schema context.

type StreamTranslationResult added in v1.0.26

type StreamTranslationResult struct {
	Chunks [][]byte  // Translated SSE chunks
	Usage  *ir.Usage // Usage extracted from IR events (nil if not present in this chunk)
}

StreamTranslationResult contains translated chunks and extracted usage from streaming response. This eliminates duplicate parsing by extracting usage during translation.

func TranslateClaudeResponseStreamWithUsage added in v1.0.26

func TranslateClaudeResponseStreamWithUsage(cfg *config.Config, to sdktranslator.Format, claudeChunk []byte, model string, messageID string, state *from_ir.ClaudeStreamState) (*StreamTranslationResult, error)

TranslateClaudeResponseStreamWithUsage converts Claude streaming chunk and extracts usage. This eliminates duplicate parsing by returning both translated chunks and usage in one operation.

func TranslateCodexResponseStreamWithUsage added in v1.0.26

func TranslateCodexResponseStreamWithUsage(cfg *config.Config, to sdktranslator.Format, codexChunk []byte, model string, messageID string, state *CodexStreamState) (*StreamTranslationResult, error)

TranslateCodexResponseStreamWithUsage converts Codex streaming chunk and extracts usage. This eliminates duplicate parsing by returning both translated chunks and usage in one operation.

func TranslateGeminiCLIResponseStreamWithUsage added in v1.0.26

func TranslateGeminiCLIResponseStreamWithUsage(cfg *config.Config, to sdktranslator.Format, geminiChunk []byte, model string, messageID string, state *GeminiCLIStreamState) (*StreamTranslationResult, error)

TranslateGeminiCLIResponseStreamWithUsage converts Gemini CLI streaming chunk and extracts usage. This eliminates duplicate parsing by returning both translated chunks and usage in one operation.

func TranslateGeminiResponseStreamWithUsage added in v1.0.26

func TranslateGeminiResponseStreamWithUsage(cfg *config.Config, to sdktranslator.Format, geminiChunk []byte, model string, messageID string, state *GeminiCLIStreamState) (*StreamTranslationResult, error)

TranslateGeminiResponseStreamWithUsage converts Gemini streaming chunk and extracts usage. This eliminates duplicate parsing by returning both translated chunks and usage in one operation.

func TranslateOpenAIResponseStreamWithUsage added in v1.0.26

func TranslateOpenAIResponseStreamWithUsage(cfg *config.Config, to sdktranslator.Format, openaiChunk []byte, model string, messageID string, state *OpenAIStreamState) (*StreamTranslationResult, error)

TranslateOpenAIResponseStreamWithUsage converts OpenAI streaming chunk and extracts usage. This eliminates duplicate parsing by returning both translated chunks and usage in one operation.

type TranslationResult added in v1.0.21

type TranslationResult struct {
	Payload              []byte                 // Translated payload
	EstimatedInputTokens int64                  // Pre-calculated input token count (0 if not applicable)
	IR                   *ir.UnifiedChatRequest // Parsed IR (for advanced use cases)
}

TranslationResult contains the translated payload and estimated token count. This allows callers to get both translation and token counting in a single operation.

func TranslateToGeminiCLIWithTokens added in v1.0.21

func TranslateToGeminiCLIWithTokens(cfg *config.Config, from sdktranslator.Format, model string, payload []byte, streaming bool, metadata map[string]any) (*TranslationResult, error)

TranslateToGeminiCLIWithTokens converts request to Gemini CLI format and counts tokens. Similar to TranslateToGeminiWithTokens but for Gemini CLI/Antigravity format.

func TranslateToGeminiWithTokens added in v1.0.21

func TranslateToGeminiWithTokens(cfg *config.Config, from sdktranslator.Format, model string, payload []byte, streaming bool, metadata map[string]any) (*TranslationResult, error)

TranslateToGeminiWithTokens converts request to Gemini format and counts tokens. This is the optimized path - translation and token counting share the same IR. Token counting is only performed for Claude source format (where input_tokens is needed). For other formats, EstimatedInputTokens will be 0.

type VertexAuthStrategy added in v1.0.30

type VertexAuthStrategy interface {
	// GetToken returns the authentication token (API key or access token).
	GetToken(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth) (string, error)
	// BuildURL builds the request URL for the given action.
	BuildURL(model, action string, opts cliproxyexecutor.Options) string
	// ApplyAuth applies authentication headers to the request.
	ApplyAuth(req *http.Request, token string)
}

VertexAuthStrategy defines the authentication strategy for Vertex AI requests. Implementations provide different authentication mechanisms for accessing Google AI endpoints.

Jump to

Keyboard shortcuts

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