mirror of
https://github.com/alvinunreal/tmuxai.git
synced 2026-10-05 22:35:26 +02:00
2.6 KiB
2.6 KiB
config/
Responsibility
- Centralize tmuxai runtime configuration: define schema, establish defaults, load user/custom configuration, normalize values, and expose filesystem paths used by other subsystems (config dir, kb dir).
Design
Configis a single strongly-typed root struct (config.go) with nested structs for providers and feature areas (OpenRouter,ModelConfig,KnowledgeBase,WebSearch,WebFetch,Tmux, etc.) andmapstructuretags for Viper binding/unmarshal.DefaultConfig()provides an explicit baseline, including sane defaults and default URLs/providers/timeouts, plus initialized map/slice fields to avoid nils where reasonable.Load(...)performs canonicalized configuration resolution in one pass: choose config source (explicit path →TMUXAI_CONFIGenv → default search paths), configure env binding (TMUXAI_*, dotted keys mapped to underscores), read config (optional), unmarshal into the struct, then post-process env variable expansion.EnumerateConfigKeysrecursively derives dotted key names from struct tags to allow env binding for all fields, including nested structs.ResolveEnvKeyInConfigrecursively traverses values via reflection, expanding$VARreferences in strings; map values are deep-copied when needed for non-addressable map entries.TryInferTypeis a lightweight parser for dynamic string values (bool/int) used by callers needing best-effort type inference.- Helper funcs include
GetConfigDir,GetConfigFilePath, andGetKBDirfor workspace path discovery and setup.
Data & Control Flow
- Startup/configure call:
Load(configFilePath?)→DefaultConfig→ Viper search/path/env setup →ReadInConfig(non-fatal if missing file) →Unmarshalinto defaults →ResolveEnvKeyInConfigmutation pass. - On demand, config consumers read the returned
*Config; they may callGetConfigDir/GetConfigFilePathfor canonical paths orGetKBDirfor KB storage (which lazily creates~/.config/tmuxai/kb). - Env expansion is recursive across nested structs and maps, with pointers/maps guarded to avoid nil deref.
Integration Points
- Exposes configuration values consumed by command handlers and service modules (LLM providers, web search/fetch, knowledge base, tmux actions).
- Reads env vars from the process (
TMUXAI_*) and YAML config (config.yamlin.or$HOME/.config/tmuxaiunless overridden). - Filesystem integration via
os.UserHomeDir,os.MkdirAll, and path construction in helpers to ensure config/kb directories exist when needed. - External dependency surface:
github.com/spf13/viperis used for file/env/config parsing/unmarshal.