feat(settings): ambient_mode and ambient_cadence_secs fields
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrInvalidBackend marks a settings value whose ai_backend is not a known
|
// ErrInvalidBackend marks a settings value whose ai_backend is not a known
|
||||||
@@ -16,6 +17,27 @@ import (
|
|||||||
// without importing the ai package.
|
// without importing the ai package.
|
||||||
var ErrInvalidBackend = errors.New("settings: invalid ai backend")
|
var ErrInvalidBackend = errors.New("settings: invalid ai backend")
|
||||||
|
|
||||||
|
// ErrInvalidAmbientMode marks a settings value whose ambient_mode is not a
|
||||||
|
// known mode. The applier returns it (wrapped) so the HTTP layer can map it to
|
||||||
|
// 400 without importing the ambient package.
|
||||||
|
var ErrInvalidAmbientMode = errors.New("settings: invalid ambient mode")
|
||||||
|
|
||||||
|
// Ambient mode dial values.
|
||||||
|
const (
|
||||||
|
AmbientOff = "off"
|
||||||
|
AmbientStatus = "status"
|
||||||
|
AmbientNotify = "notify"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ValidAmbientMode reports whether m is a known ambient mode constant.
|
||||||
|
func ValidAmbientMode(m string) bool {
|
||||||
|
switch m {
|
||||||
|
case AmbientOff, AmbientStatus, AmbientNotify:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// Settings is the user-editable configuration. Field names mirror the env vars
|
// Settings is the user-editable configuration. Field names mirror the env vars
|
||||||
// they replace: KEEL_AI_BACKEND, KEEL_MARVIN_CMD, KEEL_KNOWLEDGE_FILE, KEEL_AW_URL.
|
// they replace: KEEL_AI_BACKEND, KEEL_MARVIN_CMD, KEEL_KNOWLEDGE_FILE, KEEL_AW_URL.
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
@@ -23,6 +45,8 @@ type Settings struct {
|
|||||||
MarvinCmd string `json:"marvin_cmd"`
|
MarvinCmd string `json:"marvin_cmd"`
|
||||||
KnowledgePath string `json:"knowledge_path"`
|
KnowledgePath string `json:"knowledge_path"`
|
||||||
AWURL string `json:"aw_url"`
|
AWURL string `json:"aw_url"`
|
||||||
|
AmbientMode string `json:"ambient_mode"`
|
||||||
|
AmbientCadenceSecs int `json:"ambient_cadence_secs"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultPath returns ~/.keel/settings.json.
|
// DefaultPath returns ~/.keel/settings.json.
|
||||||
@@ -77,10 +101,22 @@ func SeedFromEnv() Settings {
|
|||||||
if awURL == "" {
|
if awURL == "" {
|
||||||
awURL = "http://localhost:5600"
|
awURL = "http://localhost:5600"
|
||||||
}
|
}
|
||||||
|
ambientMode := os.Getenv("KEEL_AMBIENT_MODE")
|
||||||
|
if ambientMode == "" {
|
||||||
|
ambientMode = AmbientNotify
|
||||||
|
}
|
||||||
|
ambientCadenceSecs := 300
|
||||||
|
if v := os.Getenv("KEEL_AMBIENT_CADENCE_SECS"); v != "" {
|
||||||
|
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
||||||
|
ambientCadenceSecs = n
|
||||||
|
}
|
||||||
|
}
|
||||||
return Settings{
|
return Settings{
|
||||||
AIBackend: backend,
|
AIBackend: backend,
|
||||||
MarvinCmd: os.Getenv("KEEL_MARVIN_CMD"),
|
MarvinCmd: os.Getenv("KEEL_MARVIN_CMD"),
|
||||||
KnowledgePath: os.Getenv("KEEL_KNOWLEDGE_FILE"),
|
KnowledgePath: os.Getenv("KEEL_KNOWLEDGE_FILE"),
|
||||||
AWURL: awURL,
|
AWURL: awURL,
|
||||||
|
AmbientMode: ambientMode,
|
||||||
|
AmbientCadenceSecs: ambientCadenceSecs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,10 @@ func TestSeedFromEnvReadsVars(t *testing.T) {
|
|||||||
t.Setenv("KEEL_MARVIN_CMD", "uv run am")
|
t.Setenv("KEEL_MARVIN_CMD", "uv run am")
|
||||||
t.Setenv("KEEL_KNOWLEDGE_FILE", "/tmp/k.md")
|
t.Setenv("KEEL_KNOWLEDGE_FILE", "/tmp/k.md")
|
||||||
t.Setenv("KEEL_AW_URL", "http://aw.local:5600")
|
t.Setenv("KEEL_AW_URL", "http://aw.local:5600")
|
||||||
|
t.Setenv("KEEL_AMBIENT_MODE", "")
|
||||||
|
t.Setenv("KEEL_AMBIENT_CADENCE_SECS", "")
|
||||||
got := SeedFromEnv()
|
got := SeedFromEnv()
|
||||||
want := Settings{AIBackend: "codex", MarvinCmd: "uv run am", KnowledgePath: "/tmp/k.md", AWURL: "http://aw.local:5600"}
|
want := Settings{AIBackend: "codex", MarvinCmd: "uv run am", KnowledgePath: "/tmp/k.md", AWURL: "http://aw.local:5600", AmbientMode: AmbientNotify, AmbientCadenceSecs: 300}
|
||||||
if got != want {
|
if got != want {
|
||||||
t.Errorf("SeedFromEnv = %+v, want %+v", got, want)
|
t.Errorf("SeedFromEnv = %+v, want %+v", got, want)
|
||||||
}
|
}
|
||||||
@@ -73,3 +75,38 @@ func TestSeedFromEnvAWURLDefault(t *testing.T) {
|
|||||||
t.Fatalf("default AWURL = %q", got)
|
t.Fatalf("default AWURL = %q", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidAmbientMode(t *testing.T) {
|
||||||
|
for _, m := range []string{AmbientOff, AmbientStatus, AmbientNotify} {
|
||||||
|
if !ValidAmbientMode(m) {
|
||||||
|
t.Fatalf("%q should be valid", m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ValidAmbientMode("loud") {
|
||||||
|
t.Fatal("loud should be invalid")
|
||||||
|
}
|
||||||
|
if ValidAmbientMode("") {
|
||||||
|
t.Fatal("empty should be invalid (callers default before validating)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSeedFromEnvAmbientDefaults(t *testing.T) {
|
||||||
|
t.Setenv("KEEL_AMBIENT_MODE", "")
|
||||||
|
t.Setenv("KEEL_AMBIENT_CADENCE_SECS", "")
|
||||||
|
s := SeedFromEnv()
|
||||||
|
if s.AmbientMode != AmbientNotify {
|
||||||
|
t.Fatalf("ambient_mode default = %q, want %q", s.AmbientMode, AmbientNotify)
|
||||||
|
}
|
||||||
|
if s.AmbientCadenceSecs != 300 {
|
||||||
|
t.Fatalf("ambient_cadence_secs default = %d, want 300", s.AmbientCadenceSecs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSeedFromEnvAmbientFromEnv(t *testing.T) {
|
||||||
|
t.Setenv("KEEL_AMBIENT_MODE", "status")
|
||||||
|
t.Setenv("KEEL_AMBIENT_CADENCE_SECS", "120")
|
||||||
|
s := SeedFromEnv()
|
||||||
|
if s.AmbientMode != "status" || s.AmbientCadenceSecs != 120 {
|
||||||
|
t.Fatalf("env not honored: mode=%q cadence=%d", s.AmbientMode, s.AmbientCadenceSecs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user