config

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var OutputLanguageMap = map[string]string{
	"en":    "English",
	"zh-cn": "Simplified Chinese",
	"zh-tw": "Traditional Chinese",
	"fr":    "French",
	"vi":    "Vietnamese",
	"ja":    "Japanese",
	"ko":    "Korean",
	"ru":    "Russian",
	"tr":    "Turkish",
	"id":    "Indonesian",
	"th":    "Thai",
	"de":    "German",
	"es":    "Spanish",
	"pt":    "Portuguese",
	"it":    "Italian",
	"ar":    "Arabic",
	"hi":    "Hindi",
	"el":    "Greek",
	"pl":    "Polish",
	"nl":    "Dutch",
	"sv":    "Swedish",
	"fi":    "Finnish",
	"hu":    "Hungarian",
	"cs":    "Czech",
	"ro":    "Romanian",
	"bg":    "Bulgarian",
	"uk":    "Ukrainian",
	"he":    "Hebrew",
	"lt":    "Lithuanian",
	"la":    "Latin",
	"ca":    "Catalan",
	"sr":    "Serbian",
	"sl":    "Slovenian",
	"mk":    "Macedonian",
	"lv":    "Latvian",
	"bn":    "Bengali",
	"ta":    "Tamil",
	"te":    "Telugu",
	"ml":    "Malayalam",
	"si":    "Sinhala",
	"fa":    "Persian",
	"ur":    "Urdu",
	"pa":    "Punjabi",
	"mr":    "Marathi",
}

OutputLanguageMap maps language codes to their names

Functions

func IsValidLanguage

func IsValidLanguage(lang string) bool

IsValidLanguage returns true if the given language code is valid, false otherwise. It uses the OutputLanguageMap to check if the language code is valid.

func MaskAPIKey

func MaskAPIKey(apiKey string, showFirst int) string

MaskAPIKey masks an API key by showing only the first few characters and replacing the rest with asterisks. It preserves common API key prefixes (e.g., "sk-", "gsk_") in the visible part.

Parameters:

  • apiKey: The API key string to be masked
  • showFirst: The number of characters to show after the prefix (or from start if no prefix)

Returns:

  • A string with the masked API key, preserving the prefix (if any) and showing the specified number of characters, with the remainder replaced by asterisks.
  • Returns empty string if input is empty

Example:

MaskAPIKey("sk-abc123def456", 3) returns "sk-abc***********"
MaskAPIKey("mykey123456", 4) returns "mkey******"

func MaskConfigAPIKeys

func MaskConfigAPIKeys(data map[string]interface{})

MaskConfigAPIKeys recursively traverses a configuration map and masks any API keys found within it. It specifically looks for keys named "api_key" and masks their string values using MaskAPIKey function, preserving only the first 3 characters visible.

Parameters:

  • data: A map[string]interface{} containing configuration data that may include API keys

The function modifies the input map in place, replacing sensitive API key values with masked versions. It handles nested maps by recursively processing them with the same masking logic.

Types

type Manager

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

Manager handles configuration management

func New

func New(configPath string) (*Manager, error)

New creates a new configuration manager

func (*Manager) Append

func (m *Manager) Append(key string, value interface{}) error

Append appends the given value to a list configuration.

If the key doesn't exist, it creates a new list with the given value. If the key exists but is not a list, it returns an error. If the key exists and is a list, it appends the given value to the list.

The method saves the configuration to the file and returns an error if the save fails.

func (*Manager) Get

func (m *Manager) Get(key string) (interface{}, bool)

Get returns the value associated with the given key. The key is split on the '.' character and the value is retrieved from the nested map.

If the key is not found, the second return value is false.

func (*Manager) GetClientConfig

func (m *Manager) GetClientConfig() (*types.ClientConfig, error)

GetClientConfig returns the client configuration for the currently selected provider.

It parses the following configuration options from the provider configuration: - api_key: the API key for the provider - api_base: the base URL for the provider's API (defaults to the default API base) - model: the model to use with the provider (defaults to the default model) - proxy: the proxy URL to use with the provider (defaults to an empty string) - max_tokens: the maximum number of tokens to generate (defaults to 2048) - top_p: the top-p sampling parameter (defaults to 1.0) - temperature: the temperature parameter (defaults to 1.0) - frequency_penalty: the frequency penalty parameter (defaults to 0.5) - retries: the number of times to retry the request if it fails (defaults to 3) - answer_path: the JSON path to the answer field in the response (defaults to an empty string) - completion_path: the JSON path to the completion field in the response (defaults to an empty string)

If any of the required configuration options are not set, an error is returned.

func (*Manager) GetFileIgnore

func (m *Manager) GetFileIgnore() []string

GetFileIgnore retrieves the list of file patterns to ignore from the configuration. It returns:

  • A slice of strings containing the ignore patterns if configured
  • nil if no patterns are configured or if the configuration is invalid

The patterns should be in a format compatible with filepath.Match

func (*Manager) GetOutputTranslateTitle

func (m *Manager) GetOutputTranslateTitle() bool

GetOutputTranslateTitle returns whether the title should be translated in the output. If the configuration value is not found, it returns true by default. If the configuration value exists but cannot be converted to boolean, it returns true.

func (*Manager) GetPath

func (m *Manager) GetPath() string

GetPath returns the path to the configuration file.

func (*Manager) GetPrompt

func (m *Manager) GetPrompt(isRich bool) string

GetPrompt returns the prompt template for the given rich/non-rich option. If the prompt is not set in the configuration, it returns the default prompt.

func (*Manager) GetSupportedKeys

func (m *Manager) GetSupportedKeys() []string

GetSupportedKeys returns a sorted list of all supported configuration keys.

The returned list will include the following keys:

  • provider
  • file_ignore
  • output.lang
  • output.rich_template
  • output.translate_title
  • console.verbose
  • <provider>.api_base
  • <provider>.api_key
  • <provider>.model
  • <provider>.retries
  • <provider>.proxy
  • <provider>.max_tokens
  • <provider>.top_p
  • <provider>.temperature
  • <provider>.frequency_penalty
  • <provider>.extra_headers
  • <provider>.completion_path
  • <provider>.answer_path
  • prompt.brief_commit_message
  • prompt.rich_commit_message
  • prompt.translation

The <provider> placeholder in the returned list will be replaced with the name of the current provider.

func (*Manager) GetTranslationPrompt

func (m *Manager) GetTranslationPrompt() string

GetTranslationPrompt retrieves the translation prompt from the configuration. If the prompt configuration is not set or if the translation prompt is not found, it returns the default translation prompt from defaults package.

Returns:

  • string: The translation prompt to be used

func (*Manager) List

func (m *Manager) List() (string, error)

List returns the current configuration as a YAML-formatted string. This method masks sensitive information such as API keys before converting to YAML. It excludes the prompt section from the configuration. Returns:

  • string: YAML representation of the configuration
  • error: If there was an error converting the config to YAML

func (*Manager) ListWithoutPrompt

func (m *Manager) ListWithoutPrompt() map[string]interface{}

ListWithoutPrompt returns a copy of the configuration that excludes the prompt section. This is useful when the user wants to list all configuration options without the prompt.

func (*Manager) Remove

func (m *Manager) Remove(key string, value string) error

Remove removes a configuration value or a value from a list.

If the value parameter is empty, the entire key is removed. If the value parameter is not empty, the value is removed from the list associated with the key.

Returns an error if the key is not found or if the value is not a list.

func (*Manager) Reset

func (m *Manager) Reset(promptOnly bool) error

Reset resets the configuration to default values. If promptOnly is true, only the prompt configuration is reset. Otherwise, all configuration is reset.

func (*Manager) Set

func (m *Manager) Set(key string, value interface{}) error

Set sets the value associated with the given key. The key is split on the '.' character and the value is set in the nested map.

If the key is not found, the value is not set.

If the key is "output.lang", the value must be a valid language code. If the key is "output.translate_title", the value must be a boolean.

The method saves the configuration to the file and returns an error if the save fails.

func (*Manager) SetProvider

func (m *Manager) SetProvider(provider, apiKey, apiBase, model string) error

SetProvider sets the provider configuration.

It takes the provider name, API key, API base, and model name as arguments. If the API base or model name is not specified, it defaults to the default values.

The method saves the configuration to the file and returns an error if the save fails.

func (*Manager) UpdateProviderConfig

func (m *Manager) UpdateProviderConfig(provider string, configs map[string]string) error

UpdateProviderConfig updates the configuration for a specific provider and saves it to the config file. It takes a provider name string and a map of configuration key-value pairs as input. The configuration values are converted from string to interface{} type before being stored. If there is an error updating or saving the configuration, it returns an error with context. Returns nil on successful update and save.

type ManagerInterface added in v0.2.3

type ManagerInterface interface {
	Get(key string) (interface{}, bool)
	Set(key string, value interface{}) error
	ListWithoutPrompt() map[string]interface{}
	List() (string, error)
	Reset(promptOnly bool) error
	Remove(key string, value string) error
	Append(key string, value interface{}) error

	GetClientConfig() (*types.ClientConfig, error)
	GetSupportedKeys() []string
	UpdateProviderConfig(provider string, configs map[string]string) error
	GetPrompt(isRich bool) string

	GetTranslationPrompt() string
	GetOutputTranslateTitle() bool
	GetFileIgnore() []string
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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