backendtypes

package
v1.0.65 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package backendtypes defines types for backend server configuration and API communication.

This package provides shared type definitions used by the backend package and applications built on ai-provider-kit. It separates type definitions from implementation to allow clean imports without circular dependencies.

Type Categories

The package defines several categories of types:

Configuration Types

BackendConfig and related types define how the backend server is configured:

  • ServerConfig: HTTP server settings (host, port, timeouts)
  • AuthConfig: Authentication configuration
  • LoggingConfig: Logging settings
  • CORSConfig: Cross-origin resource sharing settings
  • ExtensionConfig: Per-extension configuration

Request Types

Request types define the structure of incoming API requests:

  • ChatRequest: Chat completion requests
  • StreamRequest: Streaming requests

Response Types

Response types define the structure of API responses:

  • ChatResponse: Chat completion responses
  • ErrorResponse: Standard error format
  • HealthResponse: Health check responses

Usage

Import this package to use backend types without importing the full backend implementation:

import "github.com/cecil-the-coder/ai-provider-kit/pkg/backendtypes"

config := backendtypes.BackendConfig{
    Server: backendtypes.ServerConfig{Port: 8080},
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Code    string `json:"code"`
	Message string `json:"message"`
	Details string `json:"details,omitempty"`
}

type APIResponse

type APIResponse struct {
	Success   bool        `json:"success"`
	Data      interface{} `json:"data,omitempty"`
	Error     *APIError   `json:"error,omitempty"`
	RequestID string      `json:"request_id,omitempty"`
	Timestamp time.Time   `json:"timestamp"`
}

APIResponse is the standard response wrapper

type AuthConfig

type AuthConfig struct {
	Enabled     bool     `yaml:"enabled"`
	APIPassword string   `yaml:"api_password"`
	APIKeyEnv   string   `yaml:"api_key_env"`
	PublicPaths []string `yaml:"public_paths"`
}

type BackendConfig

type BackendConfig struct {
	Server     ServerConfig                     `yaml:"server"`
	Auth       AuthConfig                       `yaml:"auth"`
	Logging    LoggingConfig                    `yaml:"logging"`
	CORS       CORSConfig                       `yaml:"cors"`
	Providers  map[string]*types.ProviderConfig `yaml:"providers"`
	Extensions map[string]ExtensionConfig       `yaml:"extensions"`
}

BackendConfig defines the configuration for the backend server

type CORSConfig

type CORSConfig struct {
	Enabled        bool     `yaml:"enabled"`
	AllowedOrigins []string `yaml:"allowed_origins"`
	AllowedMethods []string `yaml:"allowed_methods"`
	AllowedHeaders []string `yaml:"allowed_headers"`
}

type ExtensionConfig

type ExtensionConfig struct {
	Enabled bool                   `yaml:"enabled"`
	Config  map[string]interface{} `yaml:"config"`
}

type GenerateRequest

type GenerateRequest struct {
	Provider    string                 `json:"provider,omitempty"`
	Model       string                 `json:"model,omitempty"`
	Prompt      string                 `json:"prompt"`
	Messages    []types.ChatMessage    `json:"messages,omitempty"`
	MaxTokens   int                    `json:"max_tokens,omitempty"`
	Temperature float64                `json:"temperature,omitempty"`
	Stream      bool                   `json:"stream,omitempty"`
	Tools       []types.Tool           `json:"tools,omitempty"`
	Metadata    map[string]interface{} `json:"metadata,omitempty"`
}

GenerateRequest represents a code/chat generation request

type GenerateResponse

type GenerateResponse struct {
	Content  string                 `json:"content"`
	Model    string                 `json:"model"`
	Provider string                 `json:"provider"`
	Usage    *UsageInfo             `json:"usage,omitempty"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

GenerateResponse for code/chat generation

type HealthResponse

type HealthResponse struct {
	Status    string                    `json:"status"`
	Version   string                    `json:"version"`
	Uptime    string                    `json:"uptime"`
	Providers map[string]ProviderHealth `json:"providers,omitempty"`
}

HealthResponse for health endpoints

type LoggingConfig

type LoggingConfig struct {
	Level  string `yaml:"level"`
	Format string `yaml:"format"` // "json" or "text"
}

type ProviderConfigRequest

type ProviderConfigRequest struct {
	Type         string   `json:"type"`
	APIKey       string   `json:"api_key,omitempty"`
	APIKeys      []string `json:"api_keys,omitempty"`
	DefaultModel string   `json:"default_model,omitempty"`
	BaseURL      string   `json:"base_url,omitempty"`
	Enabled      bool     `json:"enabled"`
}

ProviderConfigRequest for updating provider configuration

type ProviderHealth

type ProviderHealth struct {
	Status  string `json:"status"`
	Latency int64  `json:"latency_ms"`
	Message string `json:"message,omitempty"`
}

type ProviderInfo

type ProviderInfo struct {
	Name        string   `json:"name"`
	Type        string   `json:"type"`
	Enabled     bool     `json:"enabled"`
	Healthy     bool     `json:"healthy"`
	Models      []string `json:"models,omitempty"`
	Description string   `json:"description,omitempty"`
}

ProviderInfo for provider listing

type QuotaAllResponse added in v1.0.65

type QuotaAllResponse struct {
	// Quotas maps provider:model to quota information
	Quotas map[string]*QuotaResponse `json:"quotas"`

	// Summary provides overall summary
	Summary *QuotaSummaryResponse `json:"summary"`
}

QuotaAllResponse represents a response containing all quota information

type QuotaConfigResponse added in v1.0.65

type QuotaConfigResponse struct {
	Type       string                 `json:"type"`
	Period     string                 `json:"period"`
	Limit      int                    `json:"limit"`
	ResetAt    int64                  `json:"reset_at"` // Unix timestamp
	CustomData map[string]interface{} `json:"custom_data,omitempty"`
}

QuotaConfigResponse represents quota configuration in API responses

type QuotaHealthResponse added in v1.0.65

type QuotaHealthResponse struct {
	// Healthy indicates if quota tracking is operational
	Healthy bool `json:"healthy"`

	// Message provides additional health information
	Message string `json:"message,omitempty"`

	// ProvidersTracked is the number of providers being tracked
	ProvidersTracked int `json:"providers_tracked"`

	// LastUpdate is when quota information was last updated
	LastUpdate int64 `json:"last_update,omitempty"` // Unix timestamp
}

QuotaHealthResponse represents the health status of quota tracking

type QuotaHistoryRequest added in v1.0.65

type QuotaHistoryRequest struct {
	// Provider is the name of the AI provider
	Provider string `json:"provider"`

	// Model is the model identifier (optional - if empty, returns all models)
	Model string `json:"model,omitempty"`

	// StartTime is the start of the time range (Unix timestamp)
	StartTime int64 `json:"start_time,omitempty"`

	// EndTime is the end of the time range (Unix timestamp)
	EndTime int64 `json:"end_time,omitempty"`

	// Limit is the maximum number of records to return
	Limit int `json:"limit,omitempty"`
}

QuotaHistoryRequest represents a request for quota history

type QuotaHistoryResponse added in v1.0.65

type QuotaHistoryResponse struct {
	Provider    string                 `json:"provider"`
	Model       string                 `json:"model,omitempty"`
	Records     []*QuotaRecordResponse `json:"records"`
	TotalUsage  map[string]int         `json:"total_usage"`
	StartTime   int64                  `json:"start_time"` // Unix timestamp
	EndTime     int64                  `json:"end_time"`   // Unix timestamp
	RecordCount int                    `json:"record_count"`
}

QuotaHistoryResponse represents historical quota usage in API responses

type QuotaRecordResponse added in v1.0.65

type QuotaRecordResponse struct {
	ID        string         `json:"id"`
	Provider  string         `json:"provider"`
	Model     string         `json:"model"`
	Timestamp int64          `json:"timestamp"` // Unix timestamp
	Operation string         `json:"operation"`
	Usage     map[string]int `json:"usage"`
}

QuotaRecordResponse represents a quota usage record in API responses

type QuotaRequest added in v1.0.65

type QuotaRequest struct {
	// Provider is the name of the AI provider
	Provider string `json:"provider"`

	// Model is the model identifier (optional - if empty, returns provider-wide quota)
	Model string `json:"model,omitempty"`

	// FetchFromProvider if true, fetches real-time quota from the provider API
	// if the provider supports it. If false, returns cached quota information.
	FetchFromProvider bool `json:"fetch_from_provider,omitempty"`
}

QuotaRequest represents a request for quota information

type QuotaResponse added in v1.0.65

type QuotaResponse struct {
	Provider             string                          `json:"provider"`
	ProviderType         string                          `json:"provider_type"`
	Model                string                          `json:"model"`
	Timestamp            int64                           `json:"timestamp"` // Unix timestamp
	Quotas               map[string]*QuotaUsageResponse  `json:"quotas"`
	ProviderQuotaConfigs map[string]*QuotaConfigResponse `json:"provider_quota_configs,omitempty"`
	CustomUsage          map[string]interface{}          `json:"custom_usage,omitempty"`
	Metadata             map[string]interface{}          `json:"metadata,omitempty"`

	// Computed fields
	AnyQuotaExceeded bool `json:"any_quota_exceeded"`
	Healthy          bool `json:"healthy"`
}

QuotaResponse represents quota information in API responses

type QuotaSummaryRequest added in v1.0.65

type QuotaSummaryRequest struct {
	// Provider filters to a specific provider (optional)
	Provider string `json:"provider,omitempty"`
}

QuotaSummaryRequest represents a request for quota summary

type QuotaSummaryResponse added in v1.0.65

type QuotaSummaryResponse struct {
	// TotalProviders is the number of providers with quota data
	TotalProviders int `json:"total_providers"`

	// ProviderModels maps provider names to their models
	ProviderModels map[string][]string `json:"provider_models"`

	// HistoryRecords is the total number of history records
	HistoryRecords int `json:"history_records"`

	// QuotaTypes counts quota types across all providers
	QuotaTypes map[string]int `json:"quota_types"`
}

QuotaSummaryResponse represents a summary of all quotas

type QuotaUsageResponse added in v1.0.65

type QuotaUsageResponse struct {
	Type             string  `json:"type"`   // "requests", "tokens", "input_tokens", "output_tokens", "daily", "custom"
	Period           string  `json:"period"` // "minute", "hour", "day", "week", "month", "custom"
	Used             int     `json:"used"`
	Limit            int     `json:"limit"`
	Remaining        int     `json:"remaining"`
	RemainingPercent float64 `json:"remaining_percent"`
	ResetAt          int64   `json:"reset_at"`          // Unix timestamp
	PeriodStartedAt  int64   `json:"period_started_at"` // Unix timestamp
}

QuotaUsageResponse represents quota usage in API responses

type RegisterQuotaProviderRequest added in v1.0.65

type RegisterQuotaProviderRequest struct {
	// Name is the unique name for the quota provider
	Name string `json:"name"`

	// ProviderType is the type of the AI provider
	ProviderType types.ProviderType `json:"provider_type"`
}

RegisterQuotaProviderRequest represents a request to register a quota provider

type ServerConfig

type ServerConfig struct {
	Host            string        `yaml:"host"`
	Port            int           `yaml:"port"`
	Version         string        `yaml:"version"`
	ReadTimeout     time.Duration `yaml:"read_timeout"`
	WriteTimeout    time.Duration `yaml:"write_timeout"`
	ShutdownTimeout time.Duration `yaml:"shutdown_timeout"`
}

type SetQuotaRequest added in v1.0.65

type SetQuotaRequest struct {
	// Provider is the name of the AI provider
	Provider string `json:"provider"`

	// Model is the model identifier (optional - if empty, applies to all models)
	Model string `json:"model,omitempty"`

	// Quotas is a map of quota type to limit value
	Quotas map[string]int `json:"quotas"`

	// ResetTime is the reset time for the quota (Unix timestamp, optional)
	ResetTime int64 `json:"reset_time,omitempty"`
}

SetQuotaRequest represents a request to set quota limits

type UsageInfo

type UsageInfo = types.Usage

UsageInfo is a type alias to types.Usage to avoid duplication

Jump to

Keyboard shortcuts

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