client

package
v5.2.5 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 34 Imported by: 0

Documentation

Overview

Package client provides HTTP client functionality for interacting with Anthropic's Claude API. It handles authentication, request/response translation, streaming communication, and quota management for Claude models.

Package client defines the interface and base structure for AI API clients. It provides a common interface that all supported AI service clients must implement, including methods for sending messages, handling streams, and managing authentication.

Package client defines the interface and base structure for AI API clients. It provides a common interface that all supported AI service clients must implement, including methods for sending messages, handling streams, and managing authentication.

Package client defines the interface and base structure for AI API clients. It provides a common interface that all supported AI service clients must implement, including methods for sending messages, handling streams, and managing authentication.

Package client defines the interface and base structure for AI API clients. It provides a common interface that all supported AI service clients must implement, including methods for sending messages, handling streams, and managing authentication.

Package client defines the interface and base structure for AI API clients. It provides a common interface that all supported AI service clients must implement, including methods for sending messages, handling streams, and managing authentication.

Package client defines the interface and base structure for AI API clients. It provides a common interface that all supported AI service clients must implement, including methods for sending messages, handling streams, and managing authentication.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClaudeClient

type ClaudeClient struct {
	ClientBase
	// contains filtered or unexported fields
}

ClaudeClient implements the Client interface for Anthropic's Claude API. It provides methods for authenticating with Claude and sending requests to Claude models.

func NewClaudeClient

func NewClaudeClient(cfg *config.Config, ts *claude.ClaudeTokenStorage) *ClaudeClient

NewClaudeClient creates a new Claude client instance using token-based authentication. It initializes the client with the provided configuration and token storage.

Parameters:

  • cfg: The application configuration.
  • ts: The token storage for Claude authentication.

Returns:

  • *ClaudeClient: A new Claude client instance.

func NewClaudeClientWithKey

func NewClaudeClientWithKey(cfg *config.Config, apiKeyIndex int) *ClaudeClient

NewClaudeClientWithKey creates a new Claude client instance using API key authentication. It initializes the client with the provided configuration and selects the API key at the specified index from the configuration.

Parameters:

  • cfg: The application configuration.
  • apiKeyIndex: The index of the API key to use from the configuration.

Returns:

  • *ClaudeClient: A new Claude client instance.

func (*ClaudeClient) APIRequest

func (c *ClaudeClient) APIRequest(ctx context.Context, modelName, endpoint string, body interface{}, _ string, _ bool) (io.ReadCloser, *interfaces.ErrorMessage)

APIRequest handles making HTTP requests to the Claude API endpoints. It manages authentication, request preparation, and response handling.

Parameters:

  • ctx: The context for the request, which may contain additional request metadata.
  • modelName: The name of the model being requested.
  • endpoint: The API endpoint path to call (e.g., "/v1/messages").
  • body: The request body, either as a byte array or an object to be marshaled to JSON.
  • alt: An alternative response format parameter (unused in this implementation).
  • stream: A boolean indicating if the request is for a streaming response (unused in this implementation).

Returns:

  • io.ReadCloser: The response body reader if successful.
  • *interfaces.ErrorMessage: Error information if the request fails.

func (*ClaudeClient) CanProvideModel

func (c *ClaudeClient) CanProvideModel(modelName string) bool

CanProvideModel checks if this client can provide the specified model. It returns true if the model is supported by Claude, false otherwise.

Parameters:

  • modelName: The name of the model to check.

Returns:

  • bool: True if the model is supported, false otherwise.

func (*ClaudeClient) GetAPIKey

func (c *ClaudeClient) GetAPIKey() string

GetAPIKey returns the API key for Claude API requests. If an API key index is specified, it returns the corresponding key from the configuration. Otherwise, it returns an empty string, indicating token-based authentication should be used.

func (*ClaudeClient) GetEmail

func (c *ClaudeClient) GetEmail() string

GetEmail returns the email address associated with the client's token storage. If the client is using API key authentication, it returns an empty string.

func (*ClaudeClient) GetRequestMutex

func (c *ClaudeClient) GetRequestMutex() *sync.Mutex

GetRequestMutex returns the mutex used to synchronize requests for this client. This ensures that only one request is processed at a time for quota management.

Returns:

  • *sync.Mutex: The mutex used for request synchronization

func (*ClaudeClient) GetUserAgent

func (c *ClaudeClient) GetUserAgent() string

GetUserAgent returns the user agent string for Claude API requests. This identifies the client as the Claude CLI to the Anthropic API.

func (*ClaudeClient) IsAvailable added in v5.2.1

func (c *ClaudeClient) IsAvailable() bool

IsAvailable returns true if the client is available for use.

func (*ClaudeClient) IsModelQuotaExceeded

func (c *ClaudeClient) IsModelQuotaExceeded(model string) bool

IsModelQuotaExceeded returns true if the specified model has exceeded its quota and no fallback options are available.

Parameters:

  • model: The name of the model to check.

Returns:

  • bool: True if the model's quota is exceeded, false otherwise.

func (*ClaudeClient) Provider

func (c *ClaudeClient) Provider() string

Provider returns the provider name for this client. This method returns "claude" to identify Anthropic's Claude as the provider.

func (*ClaudeClient) RefreshTokens

func (c *ClaudeClient) RefreshTokens(ctx context.Context) error

RefreshTokens refreshes the access tokens if they have expired. It uses the refresh token to obtain new access tokens from the Claude authentication service. If successful, it updates the token storage and persists the new tokens to disk.

Parameters:

  • ctx: The context for the request.

Returns:

  • error: An error if the refresh operation fails, nil otherwise.

func (*ClaudeClient) SaveTokenToFile

func (c *ClaudeClient) SaveTokenToFile() error

SaveTokenToFile persists the authentication tokens to disk. It saves the token data to a JSON file in the configured authentication directory, with a filename based on the user's email address.

Returns:

  • error: An error if the save operation fails, nil otherwise.

func (*ClaudeClient) SendRawMessage

func (c *ClaudeClient) SendRawMessage(ctx context.Context, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)

SendRawMessage sends a raw message to Claude API and returns the response. It handles request translation, API communication, error handling, and response translation.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • []byte: The response body.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*ClaudeClient) SendRawMessageStream

func (c *ClaudeClient) SendRawMessageStream(ctx context.Context, modelName string, rawJSON []byte, alt string) (<-chan []byte, <-chan *interfaces.ErrorMessage)

SendRawMessageStream sends a raw streaming message to Claude API. It returns two channels: one for receiving response data chunks and one for errors.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • <-chan []byte: A channel for receiving response data chunks.
  • <-chan *interfaces.ErrorMessage: A channel for receiving error messages.

func (*ClaudeClient) SendRawTokenCount

func (c *ClaudeClient) SendRawTokenCount(_ context.Context, _ string, _ []byte, _ string) ([]byte, *interfaces.ErrorMessage)

SendRawTokenCount sends a token count request to Claude API. Currently, this functionality is not implemented for Claude models. It returns a NotImplemented error.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • []byte: Always nil for this implementation.
  • *interfaces.ErrorMessage: An error message indicating that the feature is not implemented.

func (*ClaudeClient) SetUnavailable added in v5.2.1

func (c *ClaudeClient) SetUnavailable()

SetUnavailable sets the client to unavailable.

func (*ClaudeClient) TokenStorage

func (c *ClaudeClient) TokenStorage() auth.TokenStorage

TokenStorage returns the token storage interface used by this client. This provides access to the authentication token management system.

func (*ClaudeClient) Type

func (c *ClaudeClient) Type() string

Type returns the client type identifier. This method returns "claude" to identify this client as a Claude API client.

type ClientBase

type ClientBase struct {
	// RequestMutex ensures only one request is processed at a time for quota management.
	RequestMutex *sync.Mutex
	// contains filtered or unexported fields
}

ClientBase provides a common base structure for all AI API clients. It implements shared functionality such as request synchronization, HTTP client management, configuration access, token storage, and quota tracking.

func (*ClientBase) AddAPIResponseData

func (c *ClientBase) AddAPIResponseData(ctx context.Context, line []byte)

AddAPIResponseData adds API response data to the Gin context for logging purposes. This method appends the provided data to any existing response data in the context, or creates a new entry if none exists. It only performs this operation if request logging is enabled in the configuration.

Parameters:

  • ctx: The context for the request
  • line: The response data to be added

func (*ClientBase) ClearModelQuotaExceeded

func (c *ClientBase) ClearModelQuotaExceeded(modelID string)

ClearModelQuotaExceeded clears quota exceeded status for a model Parameters:

  • modelID: The model to clear quota status for

func (*ClientBase) GetClientID

func (c *ClientBase) GetClientID() string

GetClientID returns the unique identifier for this client

func (*ClientBase) GetRequestMutex

func (c *ClientBase) GetRequestMutex() *sync.Mutex

GetRequestMutex returns the mutex used to synchronize requests for this client. This ensures that only one request is processed at a time for quota management.

Returns:

  • *sync.Mutex: The mutex used for request synchronization

func (*ClientBase) InitializeModelRegistry

func (c *ClientBase) InitializeModelRegistry(clientID string)

InitializeModelRegistry initializes the model registry for this client This should be called by all client implementations during construction

func (*ClientBase) RegisterModels

func (c *ClientBase) RegisterModels(provider string, models []*registry.ModelInfo)

RegisterModels registers the models that this client can provide Parameters:

  • provider: The provider name (e.g., "gemini", "claude", "openai")
  • models: The list of models this client supports

func (*ClientBase) SetModelQuotaExceeded

func (c *ClientBase) SetModelQuotaExceeded(modelID string)

SetModelQuotaExceeded marks a model as quota exceeded in the registry Parameters:

  • modelID: The model that exceeded quota

func (*ClientBase) UnregisterClient

func (c *ClientBase) UnregisterClient()

UnregisterClient removes this client from the model registry

type CodexClient

type CodexClient struct {
	ClientBase
	// contains filtered or unexported fields
}

CodexClient implements the Client interface for OpenAI API

func NewCodexClient

func NewCodexClient(cfg *config.Config, ts *codex.CodexTokenStorage) (*CodexClient, error)

NewCodexClient creates a new OpenAI client instance using token-based authentication

Parameters:

  • cfg: The application configuration.
  • ts: The token storage for Codex authentication.

Returns:

  • *CodexClient: A new Codex client instance.
  • error: An error if the client creation fails.

func NewCodexClientWithKey

func NewCodexClientWithKey(cfg *config.Config, apiKeyIndex int) *CodexClient

NewCodexClientWithKey creates a new Codex client instance using API key authentication. It initializes the client with the provided configuration and selects the API key at the specified index from the configuration.

Parameters:

  • cfg: The application configuration.
  • apiKeyIndex: The index of the API key to use from the configuration.

Returns:

  • *CodexClient: A new Codex client instance.

func (*CodexClient) APIRequest

func (c *CodexClient) APIRequest(ctx context.Context, modelName, endpoint string, body interface{}, _ string, _ bool) (io.ReadCloser, *interfaces.ErrorMessage)

APIRequest handles making requests to the CLI API endpoints.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • endpoint: The API endpoint to call.
  • body: The request body.
  • alt: An alternative response format parameter.
  • stream: A boolean indicating if the request is for a streaming response.

Returns:

  • io.ReadCloser: The response body reader.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*CodexClient) CanProvideModel

func (c *CodexClient) CanProvideModel(modelName string) bool

CanProvideModel checks if this client can provide the specified model.

Parameters:

  • modelName: The name of the model to check.

Returns:

  • bool: True if the model is supported, false otherwise.

func (*CodexClient) GetAPIKey

func (c *CodexClient) GetAPIKey() string

GetAPIKey returns the API key for Codex API requests. If an API key index is specified, it returns the corresponding key from the configuration. Otherwise, it returns an empty string, indicating token-based authentication should be used.

func (*CodexClient) GetEmail

func (c *CodexClient) GetEmail() string

GetEmail returns the email associated with the client's token storage. If the client is using API key authentication, it returns the API key.

func (*CodexClient) GetRequestMutex

func (c *CodexClient) GetRequestMutex() *sync.Mutex

GetRequestMutex returns the mutex used to synchronize requests for this client. This ensures that only one request is processed at a time for quota management.

Returns:

  • *sync.Mutex: The mutex used for request synchronization

func (*CodexClient) GetUserAgent

func (c *CodexClient) GetUserAgent() string

GetUserAgent returns the user agent string for OpenAI API requests

func (*CodexClient) IsAvailable added in v5.2.1

func (c *CodexClient) IsAvailable() bool

IsAvailable returns true if the client is available for use.

func (*CodexClient) IsModelQuotaExceeded

func (c *CodexClient) IsModelQuotaExceeded(model string) bool

IsModelQuotaExceeded returns true if the specified model has exceeded its quota and no fallback options are available.

Parameters:

  • model: The name of the model to check.

Returns:

  • bool: True if the model's quota is exceeded, false otherwise.

func (*CodexClient) Provider

func (c *CodexClient) Provider() string

Provider returns the provider name for this client.

func (*CodexClient) RefreshTokens

func (c *CodexClient) RefreshTokens(ctx context.Context) error

RefreshTokens refreshes the access tokens if needed

Parameters:

  • ctx: The context for the request.

Returns:

  • error: An error if the refresh operation fails, nil otherwise.

func (*CodexClient) SaveTokenToFile

func (c *CodexClient) SaveTokenToFile() error

SaveTokenToFile persists the token storage to disk

Returns:

  • error: An error if the save operation fails, nil otherwise.

func (*CodexClient) SendRawMessage

func (c *CodexClient) SendRawMessage(ctx context.Context, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)

SendRawMessage sends a raw message to OpenAI API

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • []byte: The response body.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*CodexClient) SendRawMessageStream

func (c *CodexClient) SendRawMessageStream(ctx context.Context, modelName string, rawJSON []byte, alt string) (<-chan []byte, <-chan *interfaces.ErrorMessage)

SendRawMessageStream sends a raw streaming message to OpenAI API

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • <-chan []byte: A channel for receiving response data chunks.
  • <-chan *interfaces.ErrorMessage: A channel for receiving error messages.

func (*CodexClient) SendRawTokenCount

func (c *CodexClient) SendRawTokenCount(_ context.Context, _ string, _ []byte, _ string) ([]byte, *interfaces.ErrorMessage)

SendRawTokenCount sends a token count request to OpenAI API

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • []byte: Always nil for this implementation.
  • *interfaces.ErrorMessage: An error message indicating that the feature is not implemented.

func (*CodexClient) SetUnavailable added in v5.2.1

func (c *CodexClient) SetUnavailable()

SetUnavailable sets the client to unavailable.

func (*CodexClient) TokenStorage

func (c *CodexClient) TokenStorage() auth.TokenStorage

TokenStorage returns the token storage for this client.

func (*CodexClient) Type

func (c *CodexClient) Type() string

Type returns the client type

type GeminiCLIClient

type GeminiCLIClient struct {
	ClientBase
}

GeminiCLIClient is the main client for interacting with the CLI API.

func NewGeminiCLIClient

func NewGeminiCLIClient(httpClient *http.Client, ts *geminiAuth.GeminiTokenStorage, cfg *config.Config) *GeminiCLIClient

NewGeminiCLIClient creates a new CLI API client.

Parameters:

  • httpClient: The HTTP client to use for requests.
  • ts: The token storage for Gemini authentication.
  • cfg: The application configuration.

Returns:

  • *GeminiCLIClient: A new Gemini CLI client instance.

func (*GeminiCLIClient) APIRequest

func (c *GeminiCLIClient) APIRequest(ctx context.Context, modelName, endpoint string, body interface{}, alt string, stream bool) (io.ReadCloser, *interfaces.ErrorMessage)

APIRequest handles making requests to the CLI API endpoints.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • endpoint: The API endpoint to call.
  • body: The request body.
  • alt: An alternative response format parameter.
  • stream: A boolean indicating if the request is for a streaming response.

Returns:

  • io.ReadCloser: The response body reader.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*GeminiCLIClient) CanProvideModel

func (c *GeminiCLIClient) CanProvideModel(modelName string) bool

CanProvideModel checks if this client can provide the specified model.

Parameters:

  • modelName: The name of the model to check.

Returns:

  • bool: True if the model is supported, false otherwise.

func (*GeminiCLIClient) CheckCloudAPIIsEnabled

func (c *GeminiCLIClient) CheckCloudAPIIsEnabled() (bool, error)

CheckCloudAPIIsEnabled sends a simple test request to the API to verify that the Cloud AI API is enabled for the user's project. It provides an activation URL if the API is disabled.

Returns:

  • bool: True if the API is enabled, false otherwise.
  • error: An error if the request fails, nil otherwise.

func (*GeminiCLIClient) GetEmail

func (c *GeminiCLIClient) GetEmail() string

GetEmail returns the email address associated with the client's token storage.

func (*GeminiCLIClient) GetProjectID

func (c *GeminiCLIClient) GetProjectID() string

GetProjectID returns the Google Cloud project ID from the client's token storage.

func (*GeminiCLIClient) GetProjectList

func (c *GeminiCLIClient) GetProjectList(ctx context.Context) (*interfaces.GCPProject, error)

GetProjectList fetches a list of Google Cloud projects accessible by the user.

Parameters:

  • ctx: The context for the request.

Returns:

  • *interfaces.GCPProject: A list of GCP projects.
  • error: An error if the request fails, nil otherwise.

func (*GeminiCLIClient) GetRequestMutex

func (c *GeminiCLIClient) GetRequestMutex() *sync.Mutex

GetRequestMutex returns the mutex used to synchronize requests for this client. This ensures that only one request is processed at a time for quota management.

Returns:

  • *sync.Mutex: The mutex used for request synchronization

func (*GeminiCLIClient) GetUserAgent

func (c *GeminiCLIClient) GetUserAgent() string

GetUserAgent constructs the User-Agent string for HTTP requests.

func (*GeminiCLIClient) IsAuto

func (c *GeminiCLIClient) IsAuto() bool

IsAuto returns whether the client is operating in automatic mode.

func (*GeminiCLIClient) IsAvailable added in v5.2.1

func (c *GeminiCLIClient) IsAvailable() bool

IsAvailable returns true if the client is available for use.

func (*GeminiCLIClient) IsChecked

func (c *GeminiCLIClient) IsChecked() bool

IsChecked returns whether the client's token storage has been checked.

func (*GeminiCLIClient) IsModelQuotaExceeded

func (c *GeminiCLIClient) IsModelQuotaExceeded(model string) bool

IsModelQuotaExceeded returns true if the specified model has exceeded its quota and no fallback options are available.

Parameters:

  • model: The name of the model to check.

Returns:

  • bool: True if the model's quota is exceeded, false otherwise.

func (*GeminiCLIClient) Provider

func (c *GeminiCLIClient) Provider() string

Provider returns the provider name for this client.

func (*GeminiCLIClient) RefreshTokens

func (c *GeminiCLIClient) RefreshTokens(ctx context.Context) error

RefreshTokens is not applicable for Gemini CLI clients as they use API keys.

func (*GeminiCLIClient) SaveTokenToFile

func (c *GeminiCLIClient) SaveTokenToFile() error

SaveTokenToFile serializes the client's current token storage to a JSON file. The filename is constructed from the user's email and project ID.

Returns:

  • error: An error if the save operation fails, nil otherwise.

func (*GeminiCLIClient) SendRawMessage

func (c *GeminiCLIClient) SendRawMessage(ctx context.Context, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)

SendRawMessage handles a single conversational turn, including tool calls.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • []byte: The response body.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*GeminiCLIClient) SendRawMessageStream

func (c *GeminiCLIClient) SendRawMessageStream(ctx context.Context, modelName string, rawJSON []byte, alt string) (<-chan []byte, <-chan *interfaces.ErrorMessage)

SendRawMessageStream handles a single conversational turn, including tool calls.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • <-chan []byte: A channel for receiving response data chunks.
  • <-chan *interfaces.ErrorMessage: A channel for receiving error messages.

func (*GeminiCLIClient) SendRawTokenCount

func (c *GeminiCLIClient) SendRawTokenCount(ctx context.Context, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)

SendRawTokenCount handles a token count.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • []byte: The response body.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*GeminiCLIClient) SetIsAuto

func (c *GeminiCLIClient) SetIsAuto(auto bool)

SetIsAuto configures whether the client should operate in automatic mode.

Parameters:

  • auto: A boolean indicating if automatic mode should be enabled.

func (*GeminiCLIClient) SetIsChecked

func (c *GeminiCLIClient) SetIsChecked(checked bool)

SetIsChecked sets the checked status for the client's token storage.

Parameters:

  • checked: A boolean indicating if the token storage has been checked.

func (*GeminiCLIClient) SetProjectID

func (c *GeminiCLIClient) SetProjectID(projectID string)

SetProjectID updates the project ID for the client's token storage.

Parameters:

  • projectID: The new project ID.

func (*GeminiCLIClient) SetUnavailable added in v5.2.1

func (c *GeminiCLIClient) SetUnavailable()

SetUnavailable sets the client to unavailable.

func (*GeminiCLIClient) SetupUser

func (c *GeminiCLIClient) SetupUser(ctx context.Context, email, projectID string) error

SetupUser performs the initial user onboarding and setup.

Parameters:

  • ctx: The context for the request.
  • email: The user's email address.
  • projectID: The Google Cloud project ID.

Returns:

  • error: An error if the setup fails, nil otherwise.

func (*GeminiCLIClient) Type

func (c *GeminiCLIClient) Type() string

Type returns the client type

type GeminiClient

type GeminiClient struct {
	ClientBase
	// contains filtered or unexported fields
}

GeminiClient is the main client for interacting with the CLI API.

func NewGeminiClient

func NewGeminiClient(httpClient *http.Client, cfg *config.Config, glAPIKey string) *GeminiClient

NewGeminiClient creates a new CLI API client.

Parameters:

  • httpClient: The HTTP client to use for requests.
  • cfg: The application configuration.
  • glAPIKey: The Google Cloud API key.

Returns:

  • *GeminiClient: A new Gemini client instance.

func (*GeminiClient) APIRequest

func (c *GeminiClient) APIRequest(ctx context.Context, modelName, endpoint string, body interface{}, alt string, stream bool) (io.ReadCloser, *interfaces.ErrorMessage)

APIRequest handles making requests to the CLI API endpoints.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • endpoint: The API endpoint to call.
  • body: The request body.
  • alt: An alternative response format parameter.
  • stream: A boolean indicating if the request is for a streaming response.

Returns:

  • io.ReadCloser: The response body reader.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*GeminiClient) CanProvideModel

func (c *GeminiClient) CanProvideModel(modelName string) bool

CanProvideModel checks if this client can provide the specified model.

Parameters:

  • modelName: The name of the model to check.

Returns:

  • bool: True if the model is supported, false otherwise.

func (*GeminiClient) GetEmail

func (c *GeminiClient) GetEmail() string

GetEmail returns the email address associated with the client's token storage.

func (*GeminiClient) GetRequestMutex

func (c *GeminiClient) GetRequestMutex() *sync.Mutex

GetRequestMutex returns the mutex used to synchronize requests for this client. This ensures that only one request is processed at a time for quota management.

Returns:

  • *sync.Mutex: The mutex used for request synchronization

func (*GeminiClient) GetUserAgent

func (c *GeminiClient) GetUserAgent() string

GetUserAgent constructs the User-Agent string for HTTP requests.

func (*GeminiClient) IsAvailable added in v5.2.1

func (c *GeminiClient) IsAvailable() bool

IsAvailable returns true if the client is available for use.

func (*GeminiClient) IsModelQuotaExceeded

func (c *GeminiClient) IsModelQuotaExceeded(model string) bool

IsModelQuotaExceeded returns true if the specified model has exceeded its quota and no fallback options are available.

Parameters:

  • model: The name of the model to check.

Returns:

  • bool: True if the model's quota is exceeded, false otherwise.

func (*GeminiClient) Provider

func (c *GeminiClient) Provider() string

Provider returns the provider name for this client.

func (*GeminiClient) RefreshTokens

func (c *GeminiClient) RefreshTokens(ctx context.Context) error

func (*GeminiClient) SaveTokenToFile

func (c *GeminiClient) SaveTokenToFile() error

SaveTokenToFile serializes the client's current token storage to a JSON file. The filename is constructed from the user's email and project ID.

Returns:

  • error: Always nil for this implementation.

func (*GeminiClient) SendRawMessage

func (c *GeminiClient) SendRawMessage(ctx context.Context, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)

SendRawMessage handles a single conversational turn, including tool calls.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • []byte: The response body.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*GeminiClient) SendRawMessageStream

func (c *GeminiClient) SendRawMessageStream(ctx context.Context, modelName string, rawJSON []byte, alt string) (<-chan []byte, <-chan *interfaces.ErrorMessage)

SendRawMessageStream handles a single conversational turn, including tool calls.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • <-chan []byte: A channel for receiving response data chunks.
  • <-chan *interfaces.ErrorMessage: A channel for receiving error messages.

func (*GeminiClient) SendRawTokenCount

func (c *GeminiClient) SendRawTokenCount(ctx context.Context, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)

SendRawTokenCount handles a token count.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • []byte: The response body.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*GeminiClient) SetUnavailable added in v5.2.1

func (c *GeminiClient) SetUnavailable()

SetUnavailable sets the client to unavailable.

func (*GeminiClient) Type

func (c *GeminiClient) Type() string

Type returns the client type

type GeminiWebClient added in v5.2.1

type GeminiWebClient struct {
	ClientBase
	// contains filtered or unexported fields
}

func NewGeminiWebClient added in v5.2.1

func NewGeminiWebClient(cfg *config.Config, ts *gemini.GeminiWebTokenStorage, tokenFilePath string) (*GeminiWebClient, error)

func (*GeminiWebClient) CanProvideModel added in v5.2.1

func (c *GeminiWebClient) CanProvideModel(modelName string) bool

func (*GeminiWebClient) EnsureRegistered added in v5.2.1

func (c *GeminiWebClient) EnsureRegistered()

EnsureRegistered registers models if the client is ready and not yet registered. It is safe to call multiple times.

func (*GeminiWebClient) GetEmail added in v5.2.1

func (c *GeminiWebClient) GetEmail() string

func (*GeminiWebClient) GetRequestMutex added in v5.2.1

func (c *GeminiWebClient) GetRequestMutex() *sync.Mutex

func (*GeminiWebClient) GetUserAgent added in v5.2.1

func (c *GeminiWebClient) GetUserAgent() string

func (*GeminiWebClient) Init added in v5.2.1

func (c *GeminiWebClient) Init() error

func (*GeminiWebClient) IsAvailable added in v5.2.1

func (c *GeminiWebClient) IsAvailable() bool

IsAvailable returns true if the client is available for use.

func (*GeminiWebClient) IsModelQuotaExceeded added in v5.2.1

func (c *GeminiWebClient) IsModelQuotaExceeded(model string) bool

func (*GeminiWebClient) IsReady added in v5.2.1

func (c *GeminiWebClient) IsReady() bool

IsReady reports whether the underlying Gemini Web client is initialized and running.

func (*GeminiWebClient) Provider added in v5.2.1

func (c *GeminiWebClient) Provider() string

func (*GeminiWebClient) RefreshTokens added in v5.2.1

func (c *GeminiWebClient) RefreshTokens(ctx context.Context) error

func (*GeminiWebClient) SaveTokenToFile added in v5.2.1

func (c *GeminiWebClient) SaveTokenToFile() error

SaveTokenToFile persists current cookies to a cookie snapshot via gemini-web helpers.

func (*GeminiWebClient) SendRawMessage added in v5.2.1

func (c *GeminiWebClient) SendRawMessage(ctx context.Context, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)

func (*GeminiWebClient) SendRawMessageStream added in v5.2.1

func (c *GeminiWebClient) SendRawMessageStream(ctx context.Context, modelName string, rawJSON []byte, alt string) (<-chan []byte, <-chan *interfaces.ErrorMessage)

func (*GeminiWebClient) SendRawTokenCount added in v5.2.1

func (c *GeminiWebClient) SendRawTokenCount(ctx context.Context, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)

func (*GeminiWebClient) SetUnavailable added in v5.2.1

func (c *GeminiWebClient) SetUnavailable()

SetUnavailable sets the client to unavailable.

func (*GeminiWebClient) StableClientID added in v5.2.1

func (c *GeminiWebClient) StableClientID() string

func (*GeminiWebClient) Type added in v5.2.1

func (c *GeminiWebClient) Type() string

func (*GeminiWebClient) UnregisterClient added in v5.2.1

func (c *GeminiWebClient) UnregisterClient()

func (*GeminiWebClient) UnregisterClientWithReason added in v5.2.1

func (c *GeminiWebClient) UnregisterClientWithReason(reason interfaces.UnregisterReason)

UnregisterClientWithReason allows the watcher to avoid recreating deleted auth files.

type OpenAICompatibilityClient

type OpenAICompatibilityClient struct {
	ClientBase
	// contains filtered or unexported fields
}

OpenAICompatibilityClient implements the Client interface for external OpenAI-compatible API providers. This client handles requests to external services that support OpenAI-compatible APIs, such as OpenRouter, Together.ai, and other similar services.

func NewOpenAICompatibilityClient

func NewOpenAICompatibilityClient(cfg *config.Config, compatConfig *config.OpenAICompatibility, apiKeyIndex int) (*OpenAICompatibilityClient, error)

NewOpenAICompatibilityClient creates a new OpenAI compatibility client instance.

Parameters:

  • cfg: The application configuration.
  • compatConfig: The OpenAI compatibility configuration for the specific provider.

Returns:

  • *OpenAICompatibilityClient: A new OpenAI compatibility client instance.
  • error: An error if the client creation fails.

func (*OpenAICompatibilityClient) APIRequest

func (c *OpenAICompatibilityClient) APIRequest(ctx context.Context, modelName string, endpoint string, rawJSON []byte, alt string, stream bool) (io.ReadCloser, *interfaces.ErrorMessage)

APIRequest makes an HTTP request to the OpenAI-compatible API.

Parameters:

  • ctx: The context for the request.
  • modelName: The model name to use.
  • endpoint: The API endpoint path.
  • rawJSON: The raw JSON request data.
  • alt: Alternative response format (not used for OpenAI compatibility).
  • stream: Whether this is a streaming request.

Returns:

  • io.ReadCloser: The response body reader.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*OpenAICompatibilityClient) CanProvideModel

func (c *OpenAICompatibilityClient) CanProvideModel(modelName string) bool

CanProvideModel checks if this client can provide the specified model alias.

Parameters:

  • modelName: The name/alias of the model to check.

Returns:

  • bool: True if the model alias is supported, false otherwise.

func (*OpenAICompatibilityClient) GetActualModelName

func (c *OpenAICompatibilityClient) GetActualModelName(alias string) string

GetActualModelName returns the actual model name to use with the external API based on the provided alias.

func (*OpenAICompatibilityClient) GetCurrentAPIKey

func (c *OpenAICompatibilityClient) GetCurrentAPIKey() string

GetCurrentAPIKey returns the current API key to use, with rotation support.

func (*OpenAICompatibilityClient) GetEmail

func (c *OpenAICompatibilityClient) GetEmail() string

GetEmail returns a placeholder email for this OpenAI compatibility client. Since these clients don't use traditional email-based authentication, we return the provider name as an identifier.

func (*OpenAICompatibilityClient) GetRequestMutex

func (c *OpenAICompatibilityClient) GetRequestMutex() *sync.Mutex

GetRequestMutex returns the mutex used to synchronize requests for this client. This ensures that only one request is processed at a time for quota management.

Returns:

  • *sync.Mutex: The mutex used for request synchronization

func (*OpenAICompatibilityClient) GetUserAgent

func (c *OpenAICompatibilityClient) GetUserAgent() string

GetUserAgent returns the user agent string for OpenAI compatibility API requests.

func (*OpenAICompatibilityClient) IsAvailable added in v5.2.1

func (c *OpenAICompatibilityClient) IsAvailable() bool

IsAvailable returns true if the client is available for use.

func (*OpenAICompatibilityClient) IsModelQuotaExceeded

func (c *OpenAICompatibilityClient) IsModelQuotaExceeded(model string) bool

IsModelQuotaExceeded checks if the specified model has exceeded its quota. For OpenAI compatibility clients, this is based on tracked quota exceeded times.

func (*OpenAICompatibilityClient) Provider

func (c *OpenAICompatibilityClient) Provider() string

Provider returns the provider name for this client.

func (*OpenAICompatibilityClient) RefreshTokens

func (c *OpenAICompatibilityClient) RefreshTokens(ctx context.Context) error

RefreshTokens is not applicable for OpenAI compatibility clients as they use API keys.

func (*OpenAICompatibilityClient) SaveTokenToFile

func (c *OpenAICompatibilityClient) SaveTokenToFile() error

SaveTokenToFile returns nil as this client type doesn't use traditional token storage.

func (*OpenAICompatibilityClient) SendRawMessage

func (c *OpenAICompatibilityClient) SendRawMessage(ctx context.Context, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)

SendRawMessage sends a raw message to the OpenAI-compatible API.

Parameters:

  • ctx: The context for the request.
  • modelName: The model alias name to use.
  • rawJSON: The raw JSON request data.
  • alt: Alternative response format parameter.

Returns:

  • []byte: The response data from the API.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*OpenAICompatibilityClient) SendRawMessageStream

func (c *OpenAICompatibilityClient) SendRawMessageStream(ctx context.Context, modelName string, rawJSON []byte, alt string) (<-chan []byte, <-chan *interfaces.ErrorMessage)

SendRawMessageStream sends a raw streaming message to the OpenAI-compatible API.

Parameters:

  • ctx: The context for the request.
  • modelName: The model alias name to use.
  • rawJSON: The raw JSON request data.
  • alt: Alternative response format parameter.

Returns:

  • <-chan []byte: A channel that will receive response chunks.
  • <-chan *interfaces.ErrorMessage: A channel that will receive error messages.

func (*OpenAICompatibilityClient) SendRawTokenCount

func (c *OpenAICompatibilityClient) SendRawTokenCount(ctx context.Context, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)

SendRawTokenCount sends a token count request (not implemented for OpenAI compatibility). This method is required by the Client interface but not supported by OpenAI compatibility clients.

func (*OpenAICompatibilityClient) SetUnavailable added in v5.2.1

func (c *OpenAICompatibilityClient) SetUnavailable()

SetUnavailable sets the client to unavailable.

func (*OpenAICompatibilityClient) TokenStorage

func (c *OpenAICompatibilityClient) TokenStorage() auth.TokenStorage

TokenStorage returns nil as this client doesn't use traditional token storage.

func (*OpenAICompatibilityClient) Type

Type returns the client type.

type QwenClient

type QwenClient struct {
	ClientBase
	// contains filtered or unexported fields
}

QwenClient implements the Client interface for OpenAI API

func NewQwenClient

func NewQwenClient(cfg *config.Config, ts *qwen.QwenTokenStorage, tokenFilePath ...string) *QwenClient

NewQwenClient creates a new OpenAI client instance

Parameters:

  • cfg: The application configuration.
  • ts: The token storage for Qwen authentication.

Returns:

  • *QwenClient: A new Qwen client instance.

func (*QwenClient) APIRequest

func (c *QwenClient) APIRequest(ctx context.Context, modelName, endpoint string, body interface{}, _ string, _ bool) (io.ReadCloser, *interfaces.ErrorMessage)

APIRequest handles making requests to the CLI API endpoints.

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • endpoint: The API endpoint to call.
  • body: The request body.
  • alt: An alternative response format parameter.
  • stream: A boolean indicating if the request is for a streaming response.

Returns:

  • io.ReadCloser: The response body reader.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*QwenClient) CanProvideModel

func (c *QwenClient) CanProvideModel(modelName string) bool

CanProvideModel checks if this client can provide the specified model.

Parameters:

  • modelName: The name of the model to check.

Returns:

  • bool: True if the model is supported, false otherwise.

func (*QwenClient) GetEmail

func (c *QwenClient) GetEmail() string

GetEmail returns the email associated with the client's token storage.

func (*QwenClient) GetRequestMutex

func (c *QwenClient) GetRequestMutex() *sync.Mutex

GetRequestMutex returns the mutex used to synchronize requests for this client. This ensures that only one request is processed at a time for quota management.

Returns:

  • *sync.Mutex: The mutex used for request synchronization

func (*QwenClient) GetUserAgent

func (c *QwenClient) GetUserAgent() string

GetUserAgent returns the user agent string for OpenAI API requests

func (*QwenClient) IsAvailable added in v5.2.1

func (c *QwenClient) IsAvailable() bool

IsAvailable returns true if the client is available for use.

func (*QwenClient) IsModelQuotaExceeded

func (c *QwenClient) IsModelQuotaExceeded(model string) bool

IsModelQuotaExceeded returns true if the specified model has exceeded its quota and no fallback options are available.

Parameters:

  • model: The name of the model to check.

Returns:

  • bool: True if the model's quota is exceeded, false otherwise.

func (*QwenClient) Provider

func (c *QwenClient) Provider() string

Provider returns the provider name for this client.

func (*QwenClient) RefreshTokens

func (c *QwenClient) RefreshTokens(ctx context.Context) error

RefreshTokens refreshes the access tokens if needed

Parameters:

  • ctx: The context for the request.

Returns:

  • error: An error if the refresh operation fails, nil otherwise.

func (*QwenClient) SaveTokenToFile

func (c *QwenClient) SaveTokenToFile() error

SaveTokenToFile persists the token storage to disk

Returns:

  • error: An error if the save operation fails, nil otherwise.

func (*QwenClient) SendRawMessage

func (c *QwenClient) SendRawMessage(ctx context.Context, modelName string, rawJSON []byte, alt string) ([]byte, *interfaces.ErrorMessage)

SendRawMessage sends a raw message to OpenAI API

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • []byte: The response body.
  • *interfaces.ErrorMessage: An error message if the request fails.

func (*QwenClient) SendRawMessageStream

func (c *QwenClient) SendRawMessageStream(ctx context.Context, modelName string, rawJSON []byte, alt string) (<-chan []byte, <-chan *interfaces.ErrorMessage)

SendRawMessageStream sends a raw streaming message to OpenAI API

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • <-chan []byte: A channel for receiving response data chunks.
  • <-chan *interfaces.ErrorMessage: A channel for receiving error messages.

func (*QwenClient) SendRawTokenCount

func (c *QwenClient) SendRawTokenCount(_ context.Context, _ string, _ []byte, _ string) ([]byte, *interfaces.ErrorMessage)

SendRawTokenCount sends a token count request to OpenAI API

Parameters:

  • ctx: The context for the request.
  • modelName: The name of the model to use.
  • rawJSON: The raw JSON request body.
  • alt: An alternative response format parameter.

Returns:

  • []byte: Always nil for this implementation.
  • *interfaces.ErrorMessage: An error message indicating that the feature is not implemented.

func (*QwenClient) SetUnavailable added in v5.2.1

func (c *QwenClient) SetUnavailable()

SetUnavailable sets the client to unavailable.

func (*QwenClient) TokenStorage

func (c *QwenClient) TokenStorage() auth.TokenStorage

TokenStorage returns the token storage for this client.

func (*QwenClient) Type

func (c *QwenClient) Type() string

Type returns the client type

func (*QwenClient) UnregisterClient added in v5.2.1

func (c *QwenClient) UnregisterClient()

UnregisterClient flushes cookie snapshot back into the main token file.

func (*QwenClient) UnregisterClientWithReason added in v5.2.1

func (c *QwenClient) UnregisterClientWithReason(reason interfaces.UnregisterReason)

UnregisterClientWithReason allows the watcher to adjust persistence behaviour.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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