Files
HeatGuard/internal/store/llm_settings.go
vikingowl a89720fded feat: add LLM provider settings to web UI with encrypted API key storage
Add full LLM configuration section to the setup page with provider
dropdown, model/API key fields for cloud providers (Anthropic, OpenAI,
Gemini), and endpoint/model fields for Ollama. API keys are encrypted
with AES-256-GCM and stored in the database.
2026-02-09 11:41:16 +01:00

42 lines
1.2 KiB
Go

package store
import "database/sql"
// LLMSettings holds the persisted LLM configuration.
type LLMSettings struct {
Provider string
Model string
Endpoint string
APIKeyEnc string // base64-encoded AES-256-GCM ciphertext
}
// GetLLMSettings returns the stored LLM settings, or defaults if none exist.
func (s *Store) GetLLMSettings() (*LLMSettings, error) {
ls := &LLMSettings{Provider: "none"}
err := s.db.QueryRow(
`SELECT provider, model, endpoint, api_key_enc FROM llm_settings WHERE id = 1`,
).Scan(&ls.Provider, &ls.Model, &ls.Endpoint, &ls.APIKeyEnc)
if err == sql.ErrNoRows {
return ls, nil
}
if err != nil {
return nil, err
}
return ls, nil
}
// SaveLLMSettings upserts the LLM configuration (single-row table).
func (s *Store) SaveLLMSettings(ls *LLMSettings) error {
_, err := s.db.Exec(`
INSERT INTO llm_settings (id, provider, model, endpoint, api_key_enc, updated_at)
VALUES (1, ?, ?, ?, ?, datetime('now'))
ON CONFLICT(id) DO UPDATE SET
provider = excluded.provider,
model = excluded.model,
endpoint = excluded.endpoint,
api_key_enc = excluded.api_key_enc,
updated_at = datetime('now')
`, ls.Provider, ls.Model, ls.Endpoint, ls.APIKeyEnc)
return err
}