client

package
v0.26041.1000-preview Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: MPL-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxIdleConns        = 100
	DefaultMaxIdleConnsPerHost = 2
)

Go defaults for reference (not used directly, only for documentation)

View Source
const (
	DefaultTransportTTL             = 120 * time.Minute // Default time-to-live for cached transports
	DefaultTransportCleanupInterval = 60 * time.Minute  // Default interval for cleanup task
)

Constants for transport TTL and cleanup interval

View Source
const ClaudeCodeSystemHeader = "You are Claude Code, Anthropic's official CLI for Claude."

ClaudeCodeSystemHeader is a special system message for Claude Code OAuth subscriptions

View Source
const ClaudeToolPrefix = ""

ClaudeToolPrefix is empty to match real Claude Code behavior (no tool name prefix).

View Source
const (
	ProxyURLNone = "none" // Special value to force direct connection (disable proxy)
)

Constants for proxy URL values

View Source
const ScenarioContextKey contextKey = "scenario"

Variables

ProviderTransportPolicy defines transport reuse behavior per provider type OAuth providers generally require per-session transports for proper isolation

Functions

func ApplyClaudeToolPrefix

func ApplyClaudeToolPrefix(body []byte, prefix string) []byte

ApplyClaudeToolPrefix applies a prefix to tool names in the request body. This is required for Claude Code OAuth tokens to avoid conflicts with built-in tools. The prefix is applied to user-defined tools only.

func CreateHTTPClientForProvider deprecated

func CreateHTTPClientForProvider(provider *typ.Provider, model string, sessionID typ.SessionID) *http.Client

CreateHTTPClientForProvider creates an HTTP client configured for the given provider It handles proxy and OAuth hooks if applicable The model parameter is used for transport pool keying (provider+model+proxy) The sessionID parameter is used for session-scoped transport creation for OAuth providers

Deprecated: Use createSessionBoundTransport instead. This function is kept for backward compatibility during the migration to SessionBoundTransport.

Returns a configured http.Client

func CreateHTTPClientWithProxy

func CreateHTTPClientWithProxy(proxyURL string) *http.Client

CreateHTTPClientWithProxy creates an HTTP client with proxy support

func IsClaudeOAuthToken

func IsClaudeOAuthToken(apiKey string) bool

IsClaudeOAuthToken checks if the given API key is a Claude OAuth token by checking for the "sk-ant-oat" prefix.

func IsModelsEndpointNotSupported

func IsModelsEndpointNotSupported(err error) bool

IsModelsEndpointNotSupported checks if an error is ErrModelsEndpointNotSupported

func NewTransportKey added in v0.260414.2000

func NewTransportKey(providerUUID string, proxyURL string, providerType oauth.ProviderType, session typ.SessionID) typ.TransportKey

NewTransportKey creates a TransportKey with optional session scoping. sessionID is only included in the key when:

  • providerType requires per-session transports (TransportPerSession)
  • session is not empty
  • session is not an IP-fallback (which would create one transport per IP)

Note: ProxyURL is NOT part of the key. Proxy is a provider configuration that affects how the transport is created, but doesn't create a separate pool.

func SetTransportConfig

func SetTransportConfig(config *TransportConfig)

SetTransportConfig updates the transport pool configuration Pass nil to reset to Go defaults (backward compatible) This affects newly created transports only, existing transports are not modified

func StripClaudeToolPrefixFromResponse

func StripClaudeToolPrefixFromResponse(body []byte, prefix string) []byte

StripClaudeToolPrefixFromResponse removes the tool prefix from tool names in the API response. This reverses the transformation done by ApplyClaudeToolPrefix.

func StripClaudeToolPrefixFromStreamLine

func StripClaudeToolPrefixFromStreamLine(line []byte, prefix string) []byte

StripClaudeToolPrefixFromStreamLine removes the tool prefix from tool names in a single SSE stream line. This is used for streaming responses.

Types

type AnthropicClient

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

AnthropicClient wraps the Anthropic SDK client

func NewAnthropicClient

func NewAnthropicClient(provider *typ.Provider, model string, sessionID typ.SessionID) (*AnthropicClient, error)

NewAnthropicClient creates a new Anthropic client wrapper

func (*AnthropicClient) APIStyle

func (c *AnthropicClient) APIStyle() protocol.APIStyle

ProviderType returns the provider type

func (*AnthropicClient) BetaMessagesNew

BetaMessagesNew creates a new beta message request

func (*AnthropicClient) BetaMessagesNewStreaming

BetaMessagesNewStreaming creates a new beta streaming message request

func (*AnthropicClient) Client

func (c *AnthropicClient) Client() *anthropic.Client

Client returns the underlying Anthropic SDK client

func (*AnthropicClient) Close

func (c *AnthropicClient) Close() error

Close closes any resources held by the client

func (*AnthropicClient) GetProvider

func (c *AnthropicClient) GetProvider() *typ.Provider

GetProvider returns the provider for this client

func (*AnthropicClient) HttpClient

func (c *AnthropicClient) HttpClient() *http.Client

HttpClient returns the underlying HTTP client for passthrough/proxy operations

func (*AnthropicClient) ListModels

func (c *AnthropicClient) ListModels(ctx context.Context) ([]string, error)

ListModels returns the list of available models from the Anthropic API

func (*AnthropicClient) MessagesCountTokens

MessagesCountTokens counts tokens for a message request

func (*AnthropicClient) MessagesNew

MessagesNew creates a new message request

func (*AnthropicClient) MessagesNewStreaming

MessagesNewStreaming creates a new streaming message request

func (*AnthropicClient) ProbeChatEndpoint

func (c *AnthropicClient) ProbeChatEndpoint(ctx context.Context, model string) ProbeResult

ProbeChatEndpoint tests the messages endpoint with a minimal request

func (*AnthropicClient) ProbeModelsEndpoint

func (c *AnthropicClient) ProbeModelsEndpoint(ctx context.Context) ProbeResult

ProbeModelsEndpoint tests the models list endpoint

func (*AnthropicClient) ProbeOptionsEndpoint

func (c *AnthropicClient) ProbeOptionsEndpoint(ctx context.Context) ProbeResult

ProbeOptionsEndpoint tests basic connectivity with an OPTIONS request

func (*AnthropicClient) SetRecordSink

func (c *AnthropicClient) SetRecordSink(sink *obs.Sink)

SetRecordSink sets the record sink for the client

type ClientPool

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

ClientPool manages client instances for different providers. With SessionBoundTransport, connection pooling is handled at the Transport layer, so ClientPool simply creates new client instances as needed.

Transports are automatically shared via TransportPool based on:

providerUUID + sessionID (for OAuth providers)

ProxyURL is used to configure the transport but is NOT part of the key.

Clients are automatically cleaned up via finalizers when garbage collected.

func NewClientPool

func NewClientPool() *ClientPool

NewClientPool creates a new ClientPool with default settings.

func (*ClientPool) GetAnthropicClient

func (p *ClientPool) GetAnthropicClient(ctx context.Context, provider *typ.Provider, model string) *AnthropicClient

GetAnthropicClient returns an Anthropic client wrapper for the specified provider. sessionID is resolved from ctx via typ.GetSessionID; pass context.Background() when no session is available.

func (*ClientPool) GetGoogleClient

func (p *ClientPool) GetGoogleClient(ctx context.Context, provider *typ.Provider, model string) *GoogleClient

GetGoogleClient returns a Google client wrapper for the specified provider. sessionID is resolved from ctx via typ.GetSessionID; pass context.Background() when no session is available.

func (*ClientPool) GetOpenAIClient

func (p *ClientPool) GetOpenAIClient(ctx context.Context, provider *typ.Provider, model string) *OpenAIClient

GetOpenAIClient returns an OpenAI client wrapper for the specified provider. sessionID is resolved from ctx via typ.GetSessionID; pass context.Background() when no session is available.

func (*ClientPool) GetRecordSink

func (p *ClientPool) GetRecordSink() *obs.Sink

GetRecordSink returns the record sink.

func (*ClientPool) InvalidateProvider

func (p *ClientPool) InvalidateProvider(providerUUID string)

InvalidateProvider invalidates all transports for a specific provider UUID. This should be called when provider credentials are updated (e.g., OAuth token refresh).

Note: Since ClientPool no longer caches clients, this only invalidates the TransportPool entries. Client instances will be garbage collected naturally.

func (*ClientPool) InvalidateSession added in v0.260409.1540

func (p *ClientPool) InvalidateSession(providerUUID, sessionID string)

InvalidateSession invalidates transports for a specific session. This is useful when a session ends or its OAuth token is revoked.

Note: Since ClientPool no longer caches clients, this only invalidates the TransportPool entries. Client instances will be garbage collected naturally.

func (*ClientPool) SetRecordSink

func (p *ClientPool) SetRecordSink(sink *obs.Sink)

SetRecordSink sets the record sink for the client pool. Note: This only affects newly created clients, not existing ones.

func (*ClientPool) Stats

func (p *ClientPool) Stats() map[string]interface{}

Stats provides statistics about the client pool and transport pool.

type ClientPoolBuilder

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

ClientPoolBuilder builds a ClientPool with specified configuration.

func NewClientPoolBuilder

func NewClientPoolBuilder() *ClientPoolBuilder

NewClientPoolBuilder creates a new builder with default settings.

func (*ClientPoolBuilder) Build

func (b *ClientPoolBuilder) Build() *ClientPool

Build creates the ClientPool with configured settings.

func (*ClientPoolBuilder) WithRecordSink

func (b *ClientPoolBuilder) WithRecordSink(sink *obs.Sink) *ClientPoolBuilder

WithRecordSink sets the record sink for all clients.

type ErrModelsEndpointNotSupported

type ErrModelsEndpointNotSupported struct {
	Provider string
	Reason   string
}

ErrModelsEndpointNotSupported is returned when the provider does not support the models endpoint

func (*ErrModelsEndpointNotSupported) Error

type GoogleClient

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

GoogleClient wraps the Google genai SDK client

func NewGoogleClient

func NewGoogleClient(provider *typ.Provider, model string, sessionID typ.SessionID) (*GoogleClient, error)

NewGoogleClient creates a new Google client wrapper sessionID is used for session-scoped transport creation for OAuth providers

func (*GoogleClient) APIStyle

func (c *GoogleClient) APIStyle() protocol.APIStyle

ProviderType returns the provider type

func (*GoogleClient) Client

func (c *GoogleClient) Client() *genai.Client

Client returns the underlying Google genai SDK client

func (*GoogleClient) Close

func (c *GoogleClient) Close() error

Close closes any resources held by the client

func (*GoogleClient) GenerateContent

func (c *GoogleClient) GenerateContent(ctx context.Context, model string, contents []*genai.Content, config *genai.GenerateContentConfig) (*genai.GenerateContentResponse, error)

GenerateContent generates content using the Google API

func (*GoogleClient) GenerateContentStream

func (c *GoogleClient) GenerateContentStream(ctx context.Context, model string, contents []*genai.Content, config *genai.GenerateContentConfig) iter.Seq2[*genai.GenerateContentResponse, error]

GenerateContentStream generates content using streaming

func (*GoogleClient) GetProvider

func (c *GoogleClient) GetProvider() *typ.Provider

GetProvider returns the provider for this client

func (*GoogleClient) ListModels

func (c *GoogleClient) ListModels(ctx context.Context) ([]string, error)

ListModels returns the list of available models from the Google Gemini API Note: Google genai SDK doesn't have a direct ListModels method, so we return ErrModelsEndpointNotSupported to signal the caller to use template fallback.

func (*GoogleClient) ProbeChatEndpoint

func (c *GoogleClient) ProbeChatEndpoint(ctx context.Context, model string) ProbeResult

ProbeChatEndpoint tests the chat endpoint with a minimal request

func (*GoogleClient) ProbeModelsEndpoint

func (c *GoogleClient) ProbeModelsEndpoint(ctx context.Context) ProbeResult

ProbeModelsEndpoint tests the models list endpoint

func (*GoogleClient) ProbeOptionsEndpoint

func (c *GoogleClient) ProbeOptionsEndpoint(ctx context.Context) ProbeResult

ProbeOptionsEndpoint tests basic connectivity with an OPTIONS request

func (*GoogleClient) SetRecordSink

func (c *GoogleClient) SetRecordSink(sink *obs.Sink)

SetRecordSink sets the record sink for the client

type HookFunc

type HookFunc func(req *http.Request) error

HookFunc is a function that can modify the request before it's sent

type ModelLister

type ModelLister interface {
	// ListModels returns the list of available models from the provider API
	// Returns ErrModelsEndpointNotSupported if the provider does not support the models endpoint
	ListModels(ctx context.Context) ([]string, error)
}

ModelLister defines the interface for fetching model lists from provider APIs

type OpenAIClient

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

OpenAIClient wraps the OpenAI SDK client

func NewOpenAIClient

func NewOpenAIClient(provider *typ.Provider, model string, sessionID typ.SessionID) (*OpenAIClient, error)

NewOpenAIClient creates a new OpenAI client wrapper

func (*OpenAIClient) APIStyle

func (c *OpenAIClient) APIStyle() protocol.APIStyle

ProviderType returns the provider type

func (*OpenAIClient) ChatCompletionsNew

ChatCompletionsNew creates a new chat completion request

func (*OpenAIClient) ChatCompletionsNewStreaming

ChatCompletionsNewStreaming creates a new streaming chat completion request

func (*OpenAIClient) Client

func (c *OpenAIClient) Client() *openai.Client

Client returns the underlying OpenAI SDK client

func (*OpenAIClient) Close

func (c *OpenAIClient) Close() error

Close closes any resources held by the client

func (*OpenAIClient) GetProvider

func (c *OpenAIClient) GetProvider() *typ.Provider

GetProvider returns the provider for this client

func (*OpenAIClient) HttpClient

func (c *OpenAIClient) HttpClient() *http.Client

HttpClient returns the underlying HTTP client for passthrough/proxy operations

func (*OpenAIClient) ListModels

func (c *OpenAIClient) ListModels(ctx context.Context) ([]string, error)

ListModels returns the list of available models from the OpenAI-compatible API

func (*OpenAIClient) ProbeChatEndpoint

func (c *OpenAIClient) ProbeChatEndpoint(ctx context.Context, model string) ProbeResult

ProbeChatEndpoint tests the chat completions endpoint with a minimal request

func (*OpenAIClient) ProbeModelsEndpoint

func (c *OpenAIClient) ProbeModelsEndpoint(ctx context.Context) ProbeResult

ProbeModelsEndpoint tests the models list endpoint

func (*OpenAIClient) ProbeOptionsEndpoint

func (c *OpenAIClient) ProbeOptionsEndpoint(ctx context.Context) ProbeResult

ProbeOptionsEndpoint tests basic connectivity with an OPTIONS request

func (*OpenAIClient) ResponsesNew

ResponsesNew creates a new Responses API request

func (*OpenAIClient) ResponsesNewStreaming

ResponsesNewStreaming creates a new streaming Responses API request

func (*OpenAIClient) SetRecordSink

func (c *OpenAIClient) SetRecordSink(sink *obs.Sink)

SetRecordSink sets the record sink for the client

type ProbeResult

type ProbeResult struct {
	Success          bool
	Message          string
	Content          string
	LatencyMs        int64
	ModelsCount      int
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
	ErrorMessage     string
}

ProbeResult represents the result of a probe operation

type ProbeUsage

type ProbeUsage struct {
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
}

ProbeUsage represents token usage from a probe operation

type Prober

type Prober interface {
	// ProbeChatEndpoint tests the chat/messages endpoint with a minimal request
	// Returns a ProbeResult with success status, latency, and any response content
	ProbeChatEndpoint(ctx context.Context, model string) ProbeResult

	// ProbeModelsEndpoint tests the models list endpoint
	// Returns a ProbeResult with the number of available models
	ProbeModelsEndpoint(ctx context.Context) ProbeResult

	// ProbeOptionsEndpoint tests basic connectivity with an OPTIONS request
	// Returns a ProbeResult indicating if the endpoint is reachable
	ProbeOptionsEndpoint(ctx context.Context) ProbeResult
}

Prober defines the interface for client probe capabilities

type RecordRoundTripper

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

RecordRoundTripper is an http.RoundTripper that records requests and responses

func NewRecordRoundTripper

func NewRecordRoundTripper(transport http.RoundTripper, recordSink *obs.Sink, provider *typ.Provider) *RecordRoundTripper

NewRecordRoundTripper creates a new record round tripper

func (*RecordRoundTripper) RoundTrip

func (r *RecordRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes a single HTTP transaction and records request/response

type SessionBoundTransport added in v0.260414.2000

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

SessionBoundTransport is a RoundTripper that binds to a specific session. It stores the sessionID and routes all requests through that session's transport. This enables session-scoped transport isolation while keeping the http.Client as a lightweight, stateless shell.

func (*SessionBoundTransport) RoundTrip added in v0.260414.2000

func (t *SessionBoundTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper for SessionBoundTransport. It gets the transport for the stored session from the pool and executes the request.

type TestTransportPool added in v0.260414.2000

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

TestTransportPool is a test double for TransportPool that tracks which transport keys were requested.

func NewTestTransportPool added in v0.260414.2000

func NewTestTransportPool() *TestTransportPool

NewTestTransportPool creates a new test transport pool.

func (*TestTransportPool) Clear added in v0.260414.2000

func (p *TestTransportPool) Clear()

Clear clears all transports and keys.

func (*TestTransportPool) GetTransport added in v0.260414.2000

func (p *TestTransportPool) GetTransport(providerUUID, model, proxyURL string, oauthType oauth.ProviderType, sessionID typ.SessionID) *http.Transport

GetTransport returns or creates a transport for the given configuration. It tracks the key for test verification.

func (*TestTransportPool) InvalidateSession added in v0.260414.2000

func (p *TestTransportPool) InvalidateSession(providerUUID, sessionID string)

InvalidateSession is a no-op for tests (unless specifically needed).

func (*TestTransportPool) Keys added in v0.260414.2000

func (p *TestTransportPool) Keys() []string

Keys returns all transport keys that were requested.

func (*TestTransportPool) Stats added in v0.260414.2000

func (p *TestTransportPool) Stats() map[string]interface{}

Stats returns statistics about the transport pool.

type TransportConfig

type TransportConfig struct {
	MaxIdleConns        *int  // nil = use Go default (100)
	MaxIdleConnsPerHost *int  // nil = use Go default (2)
	MaxConnsPerHost     *int  // nil = use Go default (0, no limit)
	DisableKeepAlives   *bool // nil = use Go default (false)

	// RespectEnvProxy controls whether providers without explicit proxy configuration
	// should use environment/system proxy settings (HTTP_PROXY, HTTPS_PROXY, macOS system proxy, etc.)
	// Default (nil): false - providers without proxy_url connect directly
	// Set to true: providers without proxy_url will use system/environment proxy
	RespectEnvProxy *bool // nil = use default (false)
}

TransportConfig holds the configuration for HTTP transport connection pooling All fields are pointers so that zero-value (nil) means "use Go default"

type TransportPool

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

TransportPool manages shared HTTP transports for clients Transports are keyed by: providerUUID + sessionID (for OAuth providers) This allows multiple clients to share the same connection pool when they use the same provider+session combination. For OAuth providers, transports are session-scoped to prevent cross-session contamination.

Note: ProxyURL is NOT part of the transport key. It's used to configure how the transport is created, but doesn't create a separate pool.

func GetGlobalTransportPool

func GetGlobalTransportPool() *TransportPool

GetGlobalTransportPool returns the global transport pool singleton

func (*TransportPool) Clear

func (tp *TransportPool) Clear()

Clear removes all transports from the pool and closes idle connections

func (*TransportPool) GetTransport

func (tp *TransportPool) GetTransport(providerUUID, model, proxyURL string, oauthType oauth.ProviderType, sessionID typ.SessionID) *http.Transport

GetTransport returns or creates a shared HTTP transport for the given configuration. The transport key is based on: providerUUID + sessionID (for OAuth providers). proxyURL is used to configure the transport but is NOT part of the key. sessionID is used to scope transports for OAuth providers that require per-session isolation.

func (*TransportPool) InvalidateProvider added in v0.260414.2000

func (tp *TransportPool) InvalidateProvider(providerUUID string)

InvalidateProvider removes all transports associated with a specific provider UUID. This should be called when provider credentials are updated (e.g., OAuth token refresh).

func (*TransportPool) InvalidateSession added in v0.260414.2000

func (tp *TransportPool) InvalidateSession(providerUUID, sessionID string)

InvalidateSession removes all transports associated with a specific session for a provider. This should be called when a session ends or its OAuth token is revoked.

func (*TransportPool) StartCleanupTask

func (tp *TransportPool) StartCleanupTask(interval, ttl time.Duration)

StartCleanupTask starts a periodic cleanup task that removes expired transports

func (*TransportPool) Stats

func (tp *TransportPool) Stats() map[string]interface{}

Stats returns statistics about the transport pool

type TransportPoolInterface added in v0.260414.2000

type TransportPoolInterface interface {
	GetTransport(providerUUID, model, proxyURL string, oauthType oauth.ProviderType, sessionID typ.SessionID) *http.Transport
}

TransportPoolInterface defines the interface for transport pools. This allows both the real TransportPool and test doubles to be used.

type TransportReusePolicy added in v0.260414.2000

type TransportReusePolicy string

TransportReusePolicy defines whether transports can be shared across sessions

const (
	// TransportReusable - transport can be shared across sessions (API-key providers)
	TransportReusable TransportReusePolicy = "reusable"
	// TransportPerSession - each session needs its own transport (OAuth providers)
	TransportPerSession TransportReusePolicy = "per_session"
)

func GetTransportReusePolicy added in v0.260414.2000

func GetTransportReusePolicy(providerType oauth.ProviderType) TransportReusePolicy

GetTransportReusePolicy returns the transport reuse policy for a provider type Returns TransportPerSession for unknown provider types (safer default)

Jump to

Keyboard shortcuts

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