Files
gnoma/internal/config/load.go
vikingowl cb2d63d06f feat: Ollama/gemma4 compat — /init flow, stream filter, safety fixes
provider/openai:
- Fix doubled tool call args (argsComplete flag): Ollama sends complete
  args in the first streaming chunk then repeats them as delta, causing
  doubled JSON and 400 errors in elfs
- Handle fs: prefix (gemma4 uses fs:grep instead of fs.grep)
- Add Reasoning field support for Ollama thinking output

cmd/gnoma:
- Early TTY detection so logger is created with correct destination
  before any component gets a reference to it (fixes slog WARN bleed
  into TUI textarea)

permission:
- Exempt spawn_elfs and agent tools from safety scanner: elf prompt
  text may legitimately mention .env/.ssh/credentials patterns and
  should not be blocked

tui/app:
- /init retry chain: no-tool-calls → spawn_elfs nudge → write nudge
  (ask for plain text output) → TUI fallback write from streamBuf
- looksLikeAgentsMD + extractMarkdownDoc: validate and clean fallback
  content before writing (reject refusals, strip narrative preambles)
- Collapse thinking output to 3 lines; ctrl+o to expand (live stream
  and committed messages)
- Stream-level filter for model pseudo-tool-call blocks: suppresses
  <<tool_code>>...</tool_code>> and <<function_call>>...<tool_call|>
  from entering streamBuf across chunk boundaries
- sanitizeAssistantText regex covers both block formats
- Reset streamFilterClose at every turn start
2026-04-05 19:24:51 +02:00

129 lines
2.9 KiB
Go

package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
)
// Load reads and merges config from all layers.
// Order (lowest to highest priority):
// 1. Defaults
// 2. Global config: ~/.config/gnoma/config.toml
// 3. Project config: .gnoma/config.toml
// 4. Environment variables
func Load() (*Config, error) {
cfg := Defaults()
// Layer 1: Global config
globalPath := globalConfigPath()
if err := loadTOML(&cfg, globalPath); err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("loading global config %s: %w", globalPath, err)
}
// Layer 2: Project config
projectPath := projectConfigPath()
if err := loadTOML(&cfg, projectPath); err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("loading project config %s: %w", projectPath, err)
}
// Layer 3: Environment variables
applyEnv(&cfg)
return &cfg, nil
}
func loadTOML(cfg *Config, path string) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
_, err = toml.Decode(string(data), cfg)
return err
}
func globalConfigPath() string {
// XDG_CONFIG_HOME or ~/.config
configDir := os.Getenv("XDG_CONFIG_HOME")
if configDir == "" {
home, _ := os.UserHomeDir()
configDir = filepath.Join(home, ".config")
}
return filepath.Join(configDir, "gnoma", "config.toml")
}
// ProjectRoot walks up from cwd to find the nearest directory containing
// a go.mod, .git, or .gnoma directory. Falls back to cwd if none found.
func ProjectRoot() string {
cwd, err := os.Getwd()
if err != nil {
return "."
}
dir := cwd
for {
for _, marker := range []string{"go.mod", ".git", ".gnoma"} {
if _, err := os.Stat(filepath.Join(dir, marker)); err == nil {
return dir
}
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
return cwd
}
func projectConfigPath() string {
return filepath.Join(ProjectRoot(), ".gnoma", "config.toml")
}
func applyEnv(cfg *Config) {
envKeys := map[string]string{
"mistral": "MISTRAL_API_KEY",
"anthropic": "ANTHROPIC_API_KEY",
"openai": "OPENAI_API_KEY",
"google": "GEMINI_API_KEY",
}
// Also check alternative names
altKeys := map[string][]string{
"anthropic": {"ANTHROPICS_API_KEY"},
"google": {"GOOGLE_API_KEY"},
}
for provider, envVar := range envKeys {
if key := os.Getenv(envVar); key != "" {
cfg.Provider.APIKeys[provider] = key
continue
}
for _, alt := range altKeys[provider] {
if key := os.Getenv(alt); key != "" {
cfg.Provider.APIKeys[provider] = key
break
}
}
}
// Resolve ${VAR} references in configured API keys
for k, v := range cfg.Provider.APIKeys {
if strings.HasPrefix(v, "${") && strings.HasSuffix(v, "}") {
envName := v[2 : len(v)-1]
if resolved := os.Getenv(envName); resolved != "" {
cfg.Provider.APIKeys[k] = resolved
}
}
}
// Provider override
if p := os.Getenv("GNOMA_PROVIDER"); p != "" {
cfg.Provider.Default = p
}
if m := os.Getenv("GNOMA_MODEL"); m != "" {
cfg.Provider.Model = m
}
}