d10n/config/config.go

96 lines
2.4 KiB
Go

package config
import (
"fmt"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
// ChunkConfig stores configuration for chunked translation
type ChunkConfig struct {
Enabled bool `yaml:"enabled"`
Size int `yaml:"size"`
Prompt string `yaml:"prompt"`
Context int `yaml:"context"`
}
// Config stores the application configuration
type Config struct {
APIBase string `yaml:"api_base"`
APIKey string `yaml:"api_key"`
Model string `yaml:"model"`
SystemPrompt string `yaml:"system_prompt"`
// Concurrency related settings
Concurrency int `yaml:"concurrency"`
// Chunk related settings
Chunk ChunkConfig `yaml:"chunk"`
}
// Default system prompt as a placeholder
const DefaultSystemPrompt = "Placeholder"
const DefaultChunkPrompt = "Please continue translation"
// LoadConfig loads configuration from ~/.config/d10n.yaml
func LoadConfig() (*Config, error) {
// Get user's home directory
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("could not get home directory: %w", err)
}
// Default config path
configPath := filepath.Join(homeDir, ".config", "d10n.yaml")
// Check if config file exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return &Config{
SystemPrompt: DefaultSystemPrompt,
Concurrency: 3, // Default concurrency
Chunk: ChunkConfig{
Enabled: false, // Chunking disabled by default
Size: 10240, // Default chunk size in tokens
Prompt: DefaultChunkPrompt, // Default chunk prompt
Context: 2, // Default context size
},
}, nil
}
// Read config file
data, err := os.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("could not read config file: %w", err)
}
// Parse config
var config Config
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("could not parse config file: %w", err)
}
// Set default values if not specified
if config.SystemPrompt == "" {
config.SystemPrompt = DefaultSystemPrompt
}
// Set default for concurrency
if config.Concurrency <= 0 {
config.Concurrency = 3
}
// Set defaults for chunk settings
if config.Chunk.Size <= 0 {
config.Chunk.Size = 10240
}
if config.Chunk.Prompt == "" {
config.Chunk.Prompt = DefaultChunkPrompt
}
if config.Chunk.Context <= 0 {
config.Chunk.Context = 2
}
return &config, nil
}