utils

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EnvironmentVariables = []EnvVar{

	{
		Name:         "CCPROXY_SPAWN_DEPTH",
		Description:  "Tracks the depth of process spawning to prevent infinite loops",
		Required:     false,
		DefaultValue: "0",
		ValidateFunc: ValidateSpawnDepth,
	},
	{
		Name:         "CCPROXY_FOREGROUND",
		Description:  "Set to '1' to indicate the process is running in foreground mode",
		Required:     false,
		DefaultValue: "0",
		ValidateFunc: ValidateBooleanFlag,
	},
	{
		Name:         "CCPROXY_TEST_MODE",
		Description:  "Set to '1' to enable test mode which disables background spawning",
		Required:     false,
		DefaultValue: "0",
		ValidateFunc: ValidateBooleanFlag,
	},
	{
		Name:         "CCPROXY_VERSION",
		Description:  "Override the version string reported by the server",
		Required:     false,
		DefaultValue: "",
		ValidateFunc: nil,
	},
	{
		Name:         "CCPROXY_HOST",
		Description:  "Host address to bind the server to",
		Required:     false,
		DefaultValue: "127.0.0.1",
		ValidateFunc: ValidateHostAddress,
	},
	{
		Name:         "CCPROXY_PORT",
		Description:  "Port number to bind the server to",
		Required:     false,
		DefaultValue: "3456",
		ValidateFunc: ValidatePort,
	},
	{
		Name:         "CCPROXY_LOG",
		Description:  "Enable logging (true/false)",
		Required:     false,
		DefaultValue: "false",
		ValidateFunc: ValidateBoolean,
	},
	{
		Name:         "CCPROXY_LOG_FILE",
		Description:  "Path to the log file",
		Required:     false,
		DefaultValue: "",
		ValidateFunc: ValidateFilePath,
	},
	{
		Name:         "CCPROXY_API_KEY",
		Description:  "API key for authentication",
		Required:     false,
		DefaultValue: "",
		ValidateFunc: nil,
	},
	{
		Name:         "CCPROXY_PROXY_URL",
		Description:  "HTTP/HTTPS proxy URL for outbound connections",
		Required:     false,
		DefaultValue: "",
		ValidateFunc: ValidateURL,
	},

	{
		Name:         "CCPROXY_HOME",
		Description:  "Home directory for CCProxy (used in tests)",
		Required:     false,
		DefaultValue: "",
		ValidateFunc: ValidateDirectoryPath,
	},
	{
		Name:         "CCPROXY_CONFIG_DIR",
		Description:  "Configuration directory path (used in tests)",
		Required:     false,
		DefaultValue: "",
		ValidateFunc: ValidateDirectoryPath,
	},
	{
		Name:         "CCPROXY_DATA_DIR",
		Description:  "Data directory path (used in tests)",
		Required:     false,
		DefaultValue: "",
		ValidateFunc: ValidateDirectoryPath,
	},
	{
		Name:         "CCPROXY_LOG_DIR",
		Description:  "Log directory path (used in tests)",
		Required:     false,
		DefaultValue: "",
		ValidateFunc: ValidateDirectoryPath,
	},
	{
		Name:         "CCPROXY_PID_FILE",
		Description:  "PID file path (used in tests)",
		Required:     false,
		DefaultValue: "",
		ValidateFunc: ValidateFilePath,
	},
}

EnvironmentVariables defines all CCProxy environment variables

Functions

func CleanupTempFiles

func CleanupTempFiles() error

CleanupTempFiles removes old temporary files from the temp directory

func CloseLogger

func CloseLogger() error

CloseLogger closes the log file if open

func CountMessageTokens

func CountMessageTokens(params *MessageCreateParams) (int, error)

CountMessageTokens counts tokens in a MessageCreateParams structure

func CountRequestTokens

func CountRequestTokens(bodyMap map[string]interface{}) int

CountRequestTokens estimates token count for a request This is a simplified implementation - in production you would use tiktoken-go

func CountResponseTokens

func CountResponseTokens(content string) int

CountResponseTokens estimates token count for a response

func CountTokens

func CountTokens(text string) (int, error)

CountTokens counts the number of tokens in a string

func DirExists

func DirExists(path string) bool

DirExists checks if a directory exists

func EnsureDir

func EnsureDir(path string) error

EnsureDir ensures a directory exists, creating it if necessary

func FileExists

func FileExists(path string) bool

FileExists checks if a file exists

func GetEncoder

func GetEncoder() (*tiktoken.Tiktoken, error)

GetEncoder returns the initialized encoder

func GetEnvironmentVariableDocumentation

func GetEnvironmentVariableDocumentation() string

GetEnvironmentVariableDocumentation returns a formatted string documenting all environment variables

func GetExecutablePath

func GetExecutablePath() (string, error)

GetExecutablePath returns the path to the current executable

func GetHomeDir

func GetHomeDir() (string, error)

GetHomeDir returns the CCProxy home directory path

func GetLogger

func GetLogger() *logrus.Logger

GetLogger returns the global logger instance

func GetReferenceCountPath

func GetReferenceCountPath() (string, error)

GetReferenceCountPath returns the path to the reference count file

func GetTempFile

func GetTempFile(prefix string) (string, error)

GetTempFile returns a path for a temporary file in the CCProxy temp directory

func GetTimestamp

func GetTimestamp() int64

GetTimestamp returns the current Unix timestamp

func InitLogger

func InitLogger(config *LogConfig) error

InitLogger initializes the global logger

func InitTokenizer

func InitTokenizer() error

InitTokenizer initializes the tiktoken encoder with cl100k_base encoding

func LogRequest

func LogRequest(method, path string, fields map[string]interface{})

LogRequest logs an HTTP request

func LogResponse

func LogResponse(statusCode int, duration float64, fields map[string]interface{})

LogResponse logs an HTTP response

func LogRouting

func LogRouting(model, provider, reason string)

LogRouting logs routing decisions

func LogShutdown

func LogShutdown(reason string)

LogShutdown logs service shutdown

func LogStartup

func LogStartup(port int, version string)

LogStartup logs service startup

func LogTransformer

func LogTransformer(name, direction string, success bool, err error)

LogTransformer logs transformer operations

func MaskAPIKey

func MaskAPIKey(key string) string

MaskAPIKey masks an API key for safe display

func ResolveConfigPath

func ResolveConfigPath(path string) (string, error)

ResolveConfigPath resolves a configuration file path

func ResolvePath

func ResolvePath(path string) (string, error)

ResolvePath resolves a path, handling both absolute and relative paths

func RotateLogFile

func RotateLogFile(maxSize int64) error

RotateLogFile rotates the log file

func ToJSONString

func ToJSONString(v interface{}) string

ToJSONString converts an interface{} to a JSON string

func ValidateBoolean

func ValidateBoolean(value string) error

ValidateBoolean validates boolean environment variables

func ValidateBooleanFlag

func ValidateBooleanFlag(value string) error

ValidateBooleanFlag validates environment variables that should be "0" or "1"

func ValidateDirectoryPath

func ValidateDirectoryPath(value string) error

ValidateDirectoryPath validates directory path (basic validation)

func ValidateEnvironmentVariables

func ValidateEnvironmentVariables() error

ValidateEnvironmentVariables validates all defined environment variables

func ValidateFilePath

func ValidateFilePath(value string) error

ValidateFilePath validates file path (basic validation)

func ValidateHostAddress

func ValidateHostAddress(value string) error

ValidateHostAddress validates host address (basic validation)

func ValidatePort

func ValidatePort(value string) error

ValidatePort validates port number

func ValidateSpawnDepth

func ValidateSpawnDepth(value string) error

ValidateSpawnDepth validates the CCPROXY_SPAWN_DEPTH environment variable

func ValidateURL

func ValidateURL(value string) error

ValidateURL validates URL format (basic validation)

func WriteFileAtomic

func WriteFileAtomic(path string, data []byte, perm os.FileMode) error

WriteFileAtomic writes data to a file atomically

Types

type EnvVar

type EnvVar struct {
	Name         string
	Description  string
	Required     bool
	DefaultValue string
	ValidateFunc func(value string) error
}

EnvVar represents an environment variable with validation rules

type HomeDir

type HomeDir struct {
	Root       string
	ConfigPath string
	LogPath    string
	PIDPath    string
	PluginsDir string
	TempDir    string
}

HomeDir represents the CCProxy home directory structure

func InitializeHomeDir

func InitializeHomeDir() (*HomeDir, error)

InitializeHomeDir creates and initializes the CCProxy home directory structure

type LogConfig

type LogConfig struct {
	Enabled  bool
	FilePath string
	Level    string
	Format   string // "json" or "text"
}

LogConfig represents logging configuration

type Message

type Message struct {
	Role    string      `json:"role"`
	Content interface{} `json:"content"` // Can be string or array of content objects
}

Message represents a message in the conversation

type MessageContent

type MessageContent interface{}

MessageContent represents content that can be either string or array

type MessageCreateParams

type MessageCreateParams struct {
	Model    string      `json:"model"`
	Messages []Message   `json:"messages"`
	System   interface{} `json:"system,omitempty"` // Can be string or []SystemContent
	Tools    []Tool      `json:"tools,omitempty"`
	Stream   bool        `json:"stream,omitempty"`
}

MessageCreateParams represents the parameters for creating a message

type SystemContent

type SystemContent struct {
	Type string   `json:"type"`
	Text []string `json:"text"`
}

SystemContent represents system message content

type TextContent

type TextContent struct {
	Type string `json:"type"`
	Text string `json:"text"`
}

TextContent represents text content in a message

type Tool

type Tool struct {
	Name        string      `json:"name"`
	Description string      `json:"description,omitempty"`
	InputSchema interface{} `json:"input_schema,omitempty"`
}

Tool represents a tool with its schema

type ToolResultContent

type ToolResultContent struct {
	Type    string      `json:"type"`
	ToolUse string      `json:"tool_use_id"`
	Content interface{} `json:"content"`
}

ToolResultContent represents tool result in a message

type ToolUseContent

type ToolUseContent struct {
	Type  string      `json:"type"`
	ID    string      `json:"id"`
	Name  string      `json:"name"`
	Input interface{} `json:"input"`
}

ToolUseContent represents tool use in a message

type ValidationDetail

type ValidationDetail struct {
	Name    string
	Value   string
	Valid   bool
	Error   string
	Default string
}

ValidationDetail contains details about a specific environment variable validation

type ValidationReport

type ValidationReport struct {
	Valid   bool
	Errors  []string
	Details map[string]ValidationDetail
}

ValidationReport represents the result of environment variable validation

func ValidateEnvironmentVariablesWithReport

func ValidateEnvironmentVariablesWithReport() *ValidationReport

ValidateEnvironmentVariablesWithReport validates all environment variables and returns a detailed report

Jump to

Keyboard shortcuts

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