Documentation
¶
Index ¶
- Variables
- func IsValidLanguage(lang string) bool
- func MaskAPIKey(apiKey string, showFirst int) string
- func MaskConfigAPIKeys(data map[string]interface{})
- type Manager
- func (m *Manager) Append(key string, value interface{}) error
- func (m *Manager) Get(key string) (interface{}, bool)
- func (m *Manager) GetClientConfig() (*types.ClientConfig, error)
- func (m *Manager) GetFileIgnore() []string
- func (m *Manager) GetNestedValue(keys []string) (interface{}, bool)
- func (m *Manager) GetOutputTranslateTitle() bool
- func (m *Manager) GetPath() string
- func (m *Manager) GetPrompt(isRich bool) string
- func (m *Manager) GetReviewPrompt() string
- func (m *Manager) GetSupportedKeys() []string
- func (m *Manager) GetTranslationPrompt() string
- func (m *Manager) GetWithDefault(key string, defaultValue interface{}) interface{}
- func (m *Manager) List() (string, error)
- func (m *Manager) ListWithoutPrompt() map[string]interface{}
- func (m *Manager) Load() error
- func (m *Manager) Remove(key string, value string) error
- func (m *Manager) Reset(promptOnly bool) error
- func (m *Manager) Save() error
- func (m *Manager) Set(key string, value interface{}) error
- func (m *Manager) SetNestedValue(keys []string, value interface{})
- func (m *Manager) SetProvider(provider, apiKey, apiBase, model string) error
- func (m *Manager) UpdateProviderConfig(provider string, configs map[string]string) error
- type ManagerInterface
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 (*Manager) Append ¶
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 ¶
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 ¶
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) GetNestedValue ¶ added in v0.3.0
GetNestedValue retrieves the value associated with the given key path from the configuration.
The key path is a slice of strings where each string is a key in a nested map. For example, the key path ["a", "b", "c"] would retrieve the value associated with the key "c" from the map "b" which is a value in the map "a".
If any of the keys in the path do not exist, the method returns (nil, false). If the key path is valid, the method returns the value associated with the last key in the path and true.
func (*Manager) GetOutputTranslateTitle ¶
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) GetPrompt ¶
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) GetReviewPrompt ¶ added in v0.3.0
func (*Manager) GetSupportedKeys ¶
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 ¶
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) GetWithDefault ¶ added in v0.3.0
GetWithDefault retrieves the value associated with the given key from the configuration. If the key is not found, it returns the provided defaultValue.
The key is split on the '.' character to navigate the nested map structure.
Parameters:
key - The configuration key to look up. defaultValue - The value to return if the key is not found.
Returns:
The configuration value associated with the key, or defaultValue if the key does not exist.
func (*Manager) List ¶
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 ¶
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) Load ¶ added in v0.3.0
Load reads the configuration from the file specified by the configPath field and unmarshals it into the config field.
If the file does not exist or an error occurs while reading or parsing the file, an error is returned.
func (*Manager) Remove ¶
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 ¶
Reset resets the configuration to default values. If promptOnly is true, only the prompt configuration is reset. Otherwise, all configuration is reset.
func (*Manager) Save ¶ added in v0.3.0
Save writes the configuration in the config field to the file specified by the configPath field.
If an error occurs while marshaling the configuration or writing the file, an error is returned.
func (*Manager) Set ¶
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" or "output.review_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) SetNestedValue ¶ added in v0.3.0
SetNestedValue sets the value associated with the given key path in the configuration.
The key path is a slice of strings where each string is a key in a nested map. For example, the key path ["a", "b", "c"] would set the value associated with the key "c" in the map "b" which is a value in the map "a".
If any of the keys in the path do not exist, the method creates them as needed. The method returns the value associated with the last key in the path.
func (*Manager) SetProvider ¶
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 ¶
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) GetWithDefault(key string, defaultValue interface{}) interface{} 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 Save() error GetClientConfig() (*types.ClientConfig, error) GetSupportedKeys() []string UpdateProviderConfig(provider string, configs map[string]string) error GetPrompt(isRich bool) string GetReviewPrompt() string GetNestedValue(keys []string) (interface{}, bool) SetNestedValue(keys []string, value interface{}) Load() error GetTranslationPrompt() string GetOutputTranslateTitle() bool GetFileIgnore() []string }