more env fixes

This commit is contained in:
Luxferre
2026-09-10 12:28:00 +03:00
parent b0a16e99e1
commit d9c7bb4407
4 changed files with 157 additions and 65 deletions
+85 -58
View File
@@ -208,64 +208,7 @@ func listModels(cfg *Cfg) (string, error) {
return b.String(), nil
}
func getCfg(path string) Cfg {
cfg := defCfg
// Environment variable fallbacks. These are applied BEFORE the config file is
// read so that any value explicitly set in the config file overrides them.
if v := strings.TrimSpace(os.Getenv("BANTAM_ENDPOINT")); v != "" {
cfg.Endpoint = v
}
if v := strings.TrimSpace(os.Getenv("BANTAM_MODEL")); v != "" {
cfg.Model = v
}
if v := strings.TrimSpace(os.Getenv("BANTAM_TEMP")); v != "" {
if f, e := strconv.ParseFloat(v, 64); e == nil {
cfg.Temperature = f
}
} else if v := strings.TrimSpace(os.Getenv("BANTAM_TEMPERATURE")); v != "" {
if f, e := strconv.ParseFloat(v, 64); e == nil {
cfg.Temperature = f
}
}
if v := strings.TrimSpace(os.Getenv("BANTAM_STREAM")); v != "" {
cfg.Stream = v == "true" || v == "1" || v == "yes"
}
if v := strings.TrimSpace(os.Getenv("BANTAM_COLOR")); v != "" {
cfg.Color = v
}
if v := strings.TrimSpace(os.Getenv("BANTAM_TIMEOUT")); v != "" {
cfg.Timeout = atoiD(v, cfg.Timeout)
}
if v := strings.TrimSpace(os.Getenv("BANTAM_SHELL_TIMEOUT")); v != "" {
cfg.ShellTimeout = atoiD(v, cfg.ShellTimeout)
}
if v := strings.TrimSpace(os.Getenv("BANTAM_MAX_AL_ITERATIONS")); v != "" {
cfg.MaxALIterations = atoiD(v, cfg.MaxALIterations)
}
if v := strings.TrimSpace(os.Getenv("BANTAM_CONTEXT_WINDOW")); v != "" {
cfg.ContextWindow = atoiD(v, cfg.ContextWindow)
}
re := "high"
if v := strings.TrimSpace(os.Getenv("BANTAM_REASONING_EFFORT")); v != "" {
re = v
}
cfg.Raw = map[string]string{
"endpoint": cfg.Endpoint, "model": cfg.Model, "temperature": fmt.Sprintf("%v", cfg.Temperature),
"api_key": cfg.APIKey, "stream": strconv.FormatBool(cfg.Stream), "color": cfg.Color,
"timeout": strconv.Itoa(cfg.Timeout), "shell_timeout": strconv.Itoa(cfg.ShellTimeout),
"max_al_iterations": strconv.Itoa(cfg.MaxALIterations),
"context_window": strconv.Itoa(cfg.ContextWindow),
"reasoning_effort": re,
}
if v := strings.TrimSpace(os.Getenv("BANTAM_TOOLS_DIR")); v != "" {
cfg.Raw["bantam_tools_dir"] = v
}
if v := strings.TrimSpace(os.Getenv("BANTAM_SKILLS_DIR")); v != "" {
cfg.Raw["bantam_skills_dir"] = v
}
func parseCfgFile(path string, cfg *Cfg) {
if d, err := os.ReadFile(path); err == nil {
for _, ln := range strings.Split(string(d), "\n") {
ln = strings.TrimSpace(ln)
@@ -287,6 +230,90 @@ func getCfg(path string) Cfg {
}
}
}
}
func applyEnvCfg(cfg *Cfg) {
if v := strings.TrimSpace(os.Getenv("BANTAM_ENDPOINT")); v != "" {
cfg.Endpoint = v
cfg.Raw["endpoint"] = v
}
if v := strings.TrimSpace(os.Getenv("BANTAM_MODEL")); v != "" {
cfg.Model = v
cfg.Raw["model"] = v
}
if v := strings.TrimSpace(os.Getenv("BANTAM_TEMP")); v != "" {
if f, e := strconv.ParseFloat(v, 64); e == nil {
cfg.Temperature = f
cfg.Raw["temperature"] = v
}
} else if v := strings.TrimSpace(os.Getenv("BANTAM_TEMPERATURE")); v != "" {
if f, e := strconv.ParseFloat(v, 64); e == nil {
cfg.Temperature = f
cfg.Raw["temperature"] = v
}
}
if v := strings.TrimSpace(os.Getenv("BANTAM_STREAM")); v != "" {
cfg.Stream = v == "true" || v == "1" || v == "yes"
cfg.Raw["stream"] = strconv.FormatBool(cfg.Stream)
}
if v := strings.TrimSpace(os.Getenv("BANTAM_COLOR")); v != "" {
cfg.Color = v
cfg.Raw["color"] = v
}
if v := strings.TrimSpace(os.Getenv("BANTAM_TIMEOUT")); v != "" {
cfg.Timeout = atoiD(v, cfg.Timeout)
cfg.Raw["timeout"] = strconv.Itoa(cfg.Timeout)
}
if v := strings.TrimSpace(os.Getenv("BANTAM_SHELL_TIMEOUT")); v != "" {
cfg.ShellTimeout = atoiD(v, cfg.ShellTimeout)
cfg.Raw["shell_timeout"] = strconv.Itoa(cfg.ShellTimeout)
}
if v := strings.TrimSpace(os.Getenv("BANTAM_MAX_AL_ITERATIONS")); v != "" {
cfg.MaxALIterations = atoiD(v, cfg.MaxALIterations)
cfg.Raw["max_al_iterations"] = strconv.Itoa(cfg.MaxALIterations)
}
if v := strings.TrimSpace(os.Getenv("BANTAM_CONTEXT_WINDOW")); v != "" {
cfg.ContextWindow = atoiD(v, cfg.ContextWindow)
cfg.Raw["context_window"] = strconv.Itoa(cfg.ContextWindow)
}
if v := strings.TrimSpace(os.Getenv("BANTAM_REASONING_EFFORT")); v != "" {
cfg.Raw["reasoning_effort"] = v
}
if v := strings.TrimSpace(os.Getenv("BANTAM_TOOLS_DIR")); v != "" {
cfg.Raw["bantam_tools_dir"] = v
}
if v := strings.TrimSpace(os.Getenv("BANTAM_SKILLS_DIR")); v != "" {
cfg.Raw["bantam_skills_dir"] = v
}
}
func getCfg(path string) Cfg {
cfg := defCfg
cfg.Raw = map[string]string{
"endpoint": cfg.Endpoint, "model": cfg.Model, "temperature": fmt.Sprintf("%v", cfg.Temperature),
"api_key": cfg.APIKey, "stream": strconv.FormatBool(cfg.Stream), "color": cfg.Color,
"timeout": strconv.Itoa(cfg.Timeout), "shell_timeout": strconv.Itoa(cfg.ShellTimeout),
"max_al_iterations": strconv.Itoa(cfg.MaxALIterations),
"context_window": strconv.Itoa(cfg.ContextWindow),
"reasoning_effort": "high",
}
if filepath.Base(path) == "model.cfg" {
// When .bantam.cfg is absent, model.cfg provides base defaults,
// and BANTAM_* environment variables override them.
parseCfgFile(path, &cfg)
applyEnvCfg(&cfg)
} else {
// If a model.cfg exists in the same directory, read it first as base defaults.
modelCfgPath := filepath.Join(filepath.Dir(path), "model.cfg")
if _, err := os.Stat(modelCfgPath); err == nil {
parseCfgFile(modelCfgPath, &cfg)
}
// Environment variables override model.cfg.
applyEnvCfg(&cfg)
// The explicit override file (.bantam.cfg) takes highest priority and overrides env vars.
parseCfgFile(path, &cfg)
}
// The api_key "-" / empty sentinel means "fall back to BANTAM_API_KEY"; this
// is evaluated after the file is read so an explicit key still wins.