Photograph a seed packet → it fills in the plant and the purchase. This is the backend; the scan UI is a follow-up PR. Vision model config (mirrors the agent model from #79): - Migration 0011 adds instance_settings.vision_model; PANSY_VISION_MODEL is the env default. Precedence Settings → env → empty; the KEY stays in the env. - EffectiveVision resolves it; /capabilities advertises "vision" only when a model + key are configured, so the UI offers the scan button only when it works. Extraction is one-shot, NOT an agent loop (internal/vision): - majordomo.Generate[SeedPacket] derives a JSON schema from the struct tags and hands the image to the vision model; it can't call a tool, so it can't touch the garden — it only reads a picture and returns data. Numeric fields are pointers, so a field the packet doesn't print comes back nil, not a made-up 0. - Hermetic test: majordomo's fake provider returns canned packet JSON and Generate unmarshals it, image + derived schema included. No live model. The image is normalized to JPEG at the upload boundary (imagenorm from #80), which is where an iPhone HEIC becomes readable — majordomo's media path can't decode HEIC. imagenorm now links into the binary (~7 MB, the cost #80 deferred). The hard part is catalog matching, not OCR (internal/service/seed_packet.go): - A wrong auto-match splits a variety's seed-lot history across duplicate rows, so the service NEVER auto-creates. matchPlants surfaces RANKED candidates (exact name → variety-in-name → same species, conservative and name-based), the user confirms, and CreateFromPacket makes the plant (new or existing) + the lot. Exactly one of plantId/newPlant, refused otherwise. - Plants/lots aren't in the undo history (catalog/inventory), so no change set. - The extractor is injectable (service.WithPacketExtractor) so ExtractSeedPacket and the /scan endpoint test end to end against a fake, no live model. Endpoints: POST /seed-lots/scan (multipart image → proposal, reads only; extends the read deadline for a slow phone upload, caps the body, maps too-large/unreadable to clear statuses) and POST /seed-lots/from-packet (confirmed proposal → 201). Docs: README (PANSY_VISION_MODEL), DESIGN (routes + the decision and why the model can't touch the garden). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01H3zbym8Doka2d7D48maSgZ
193 lines
6.8 KiB
Go
193 lines
6.8 KiB
Go
// Package config loads pansy's runtime configuration from the environment.
|
|
//
|
|
// Every value has a sensible default so `pansy` runs with zero configuration
|
|
// for local use; production deployments override via PANSY_* env vars. Auth
|
|
// values (local + OIDC) are plumbed here now and consumed by the auth issues
|
|
// (#4 local, #5 OIDC).
|
|
package config
|
|
|
|
import (
|
|
"log/slog"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Registration gates local self-service signup.
|
|
const (
|
|
RegistrationOpen = "open"
|
|
RegistrationClosed = "closed"
|
|
)
|
|
|
|
// DefaultAgentModel is the assistant's model when PANSY_AGENT_MODEL is unset.
|
|
const DefaultAgentModel = "ollama-cloud/glm-5.2:cloud"
|
|
|
|
// Config is the fully-resolved runtime configuration.
|
|
type Config struct {
|
|
// Port is the TCP port the HTTP server listens on (PANSY_PORT, default 8080).
|
|
Port int
|
|
// DBPath is the SQLite database file path (PANSY_DB, default ./pansy.db).
|
|
DBPath string
|
|
// BaseURL is the externally-visible base URL, used to derive the OIDC
|
|
// redirect URI and absolute links (PANSY_BASE_URL). Empty in bare local dev.
|
|
BaseURL string
|
|
// Registration is "open" or "closed"; gates POST /auth/register for local
|
|
// accounts (PANSY_REGISTRATION, default open). OIDC JIT provisioning ignores
|
|
// this — the IdP gates access.
|
|
Registration string
|
|
// LocalAuth enables argon2id username/password auth (PANSY_LOCAL_AUTH,
|
|
// default true). Set false for pure-Authentik deployments.
|
|
LocalAuth bool
|
|
// OIDC holds the optional OpenID Connect provider settings.
|
|
OIDC OIDCConfig
|
|
// Agent holds the garden-assistant settings.
|
|
Agent AgentConfig
|
|
// TrustedProxies is the set of proxy CIDRs/IPs gin trusts for client IP
|
|
// resolution (PANSY_TRUSTED_PROXIES, comma-separated). Empty trusts none.
|
|
TrustedProxies []string
|
|
}
|
|
|
|
// OIDCConfig holds the OpenID Connect provider settings (Authentik is the
|
|
// primary IdP). Consumed by #5.
|
|
type OIDCConfig struct {
|
|
Issuer string // PANSY_OIDC_ISSUER — discovery base URL
|
|
ClientID string // PANSY_OIDC_CLIENT_ID
|
|
ClientSecret string // PANSY_OIDC_CLIENT_SECRET
|
|
ButtonLabel string // PANSY_OIDC_BUTTON_LABEL — login-page button text
|
|
}
|
|
|
|
// AgentConfig holds the garden assistant's settings.
|
|
type AgentConfig struct {
|
|
// Model is the majordomo model spec, passed VERBATIM to majordomo.Parse
|
|
// (PANSY_AGENT_MODEL). The grammar is majordomo's, not pansy's — parsing or
|
|
// validating it here would only mean two places to update when it grows. A
|
|
// comma-separated spec is a failover chain, so
|
|
// "ollama-cloud/glm-5.2:cloud,ollama-cloud/kimi-k2.6:cloud" gets a fallback
|
|
// for free.
|
|
Model string
|
|
// OllamaCloudAPIKey authenticates against Ollama Cloud
|
|
// (OLLAMA_CLOUD_API_KEY — the same secret name gadfly uses, which is why
|
|
// it isn't majordomo's own OLLAMA_API_KEY; pansy passes it explicitly rather
|
|
// than relying on ambient environment).
|
|
OllamaCloudAPIKey string
|
|
// Enabled turns the assistant on (PANSY_AGENT_ENABLED). Defaults to on when
|
|
// a key is present, so an instance with no key starts cleanly and simply
|
|
// doesn't offer the agent — the same shape as OIDC 404ing when unconfigured.
|
|
Enabled bool
|
|
// VisionModel is the model that reads a photographed seed packet
|
|
// (PANSY_VISION_MODEL). Empty by default: the seed-packet capture feature is
|
|
// only offered when a vision-capable model is configured (in env or Settings)
|
|
// and a key is present. Passed verbatim to majordomo.Parse, like Model.
|
|
VisionModel string
|
|
}
|
|
|
|
// Enabled reports whether enough OIDC config is present to attempt discovery.
|
|
func (o OIDCConfig) Enabled() bool {
|
|
return o.Issuer != "" && o.ClientID != ""
|
|
}
|
|
|
|
// OIDCReady reports whether OIDC login can actually be offered: it needs an
|
|
// issuer + client ID and a BaseURL to build the absolute redirect URI that
|
|
// providers require. Both the login page (via /auth/providers) and route
|
|
// registration gate on this, so the advertised methods match the live routes.
|
|
func (c *Config) OIDCReady() bool {
|
|
return c.OIDC.Enabled() && c.BaseURL != ""
|
|
}
|
|
|
|
// RegistrationOpen reports whether local self-service signup is allowed.
|
|
func (c *Config) RegistrationOpen() bool {
|
|
return c.Registration == RegistrationOpen
|
|
}
|
|
|
|
// Load reads configuration from the environment, applying defaults. It never
|
|
// fails: invalid numeric/boolean values fall back to the default and are logged.
|
|
func Load() *Config {
|
|
cfg := &Config{
|
|
Port: envInt("PANSY_PORT", 8080),
|
|
DBPath: envStr("PANSY_DB", "./pansy.db"),
|
|
BaseURL: strings.TrimRight(envStr("PANSY_BASE_URL", ""), "/"),
|
|
Registration: envStr("PANSY_REGISTRATION", RegistrationOpen),
|
|
LocalAuth: envBool("PANSY_LOCAL_AUTH", true),
|
|
OIDC: OIDCConfig{
|
|
Issuer: envStr("PANSY_OIDC_ISSUER", ""),
|
|
ClientID: envStr("PANSY_OIDC_CLIENT_ID", ""),
|
|
ClientSecret: envStr("PANSY_OIDC_CLIENT_SECRET", ""),
|
|
ButtonLabel: envStr("PANSY_OIDC_BUTTON_LABEL", "Sign in with Authentik"),
|
|
},
|
|
TrustedProxies: envList("PANSY_TRUSTED_PROXIES"),
|
|
}
|
|
|
|
agentKey := envStr("OLLAMA_CLOUD_API_KEY", "")
|
|
cfg.Agent = AgentConfig{
|
|
Model: envStr("PANSY_AGENT_MODEL", DefaultAgentModel),
|
|
OllamaCloudAPIKey: agentKey,
|
|
// Default on when a key is present: having configured the key IS the
|
|
// opt-in, and making people set a second flag to use what they just
|
|
// configured is a papercut with no upside.
|
|
Enabled: envBool("PANSY_AGENT_ENABLED", agentKey != ""),
|
|
// No default vision model: unlike chat there's no obvious safe default,
|
|
// and the feature stays off until an admin names one that can see.
|
|
VisionModel: envStr("PANSY_VISION_MODEL", ""),
|
|
}
|
|
|
|
if cfg.Registration != RegistrationOpen && cfg.Registration != RegistrationClosed {
|
|
slog.Warn("config: invalid PANSY_REGISTRATION, defaulting to open", "value", cfg.Registration)
|
|
cfg.Registration = RegistrationOpen
|
|
}
|
|
|
|
if cfg.Port < 1 || cfg.Port > 65535 {
|
|
slog.Warn("config: PANSY_PORT out of range, using default", "value", cfg.Port, "default", 8080)
|
|
cfg.Port = 8080
|
|
}
|
|
|
|
return cfg
|
|
}
|
|
|
|
func envStr(key, def string) string {
|
|
if v, ok := os.LookupEnv(key); ok && v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|
|
|
|
func envInt(key string, def int) int {
|
|
v, ok := os.LookupEnv(key)
|
|
if !ok || v == "" {
|
|
return def
|
|
}
|
|
n, err := strconv.Atoi(v)
|
|
if err != nil {
|
|
slog.Warn("config: invalid int env, using default", "key", key, "value", v, "default", def)
|
|
return def
|
|
}
|
|
return n
|
|
}
|
|
|
|
func envBool(key string, def bool) bool {
|
|
v, ok := os.LookupEnv(key)
|
|
if !ok || v == "" {
|
|
return def
|
|
}
|
|
b, err := strconv.ParseBool(v)
|
|
if err != nil {
|
|
slog.Warn("config: invalid bool env, using default", "key", key, "value", v, "default", def)
|
|
return def
|
|
}
|
|
return b
|
|
}
|
|
|
|
func envList(key string) []string {
|
|
v, ok := os.LookupEnv(key)
|
|
if !ok || v == "" {
|
|
return nil
|
|
}
|
|
parts := strings.Split(v, ",")
|
|
out := make([]string, 0, len(parts))
|
|
for _, p := range parts {
|
|
if p = strings.TrimSpace(p); p != "" {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|