config

package
v0.7.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package config loads chatz's configuration from environment variables via gonfiguration. LLM upstreams are provided as a JSON array in CHATZ_UPSTREAMS; the app queries each provider for its models and merges the results. MCP servers are configured separately in the DB and UI.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidUpstream marks an invalid CHATZ_UPSTREAMS entry.
	ErrInvalidUpstream = errors.New("invalid upstream")

	// ErrInvalidUpstreamRuntime marks invalid LLM request bounds.
	ErrInvalidUpstreamRuntime = errors.New("invalid upstream runtime limits")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	HTTPListenAddress      string `default:":8080" env:"CHATZ_HTTP_LISTENADDRESS"`      //nolint:lll // env tag
	MetricsListenAddress   string `default:":9091" env:"CHATZ_METRICS_LISTENADDRESS"`   //nolint:lll // env tag
	ProfilingListenAddress string `default:":6060" env:"CHATZ_PROFILING_LISTENADDRESS"` //nolint:lll // env tag

	DBHostname          string        `default:"localhost"                      env:"CHATZ_DB_HOSTNAME"`            //nolint:lll // env tag
	DBPort              int           `default:"5432"                           env:"CHATZ_DB_PORT"`                //nolint:lll // env tag
	DBUsername          string        `default:"chatz"                          env:"CHATZ_DB_USERNAME"`            //nolint:lll // env tag
	DBPassword          string        `default:"chatz"                          env:"CHATZ_DB_PASSWORD"`            //nolint:lll // env tag
	DBName              string        `default:"chatz"                          env:"CHATZ_DB_NAME"`                //nolint:lll // env tag
	DBIsSSL             bool          `default:"false"                          env:"CHATZ_DB_ISSSL"`               //nolint:lll // env tag
	DBDriver            db.Driver     `default:"postgres"                       env:"CHATZ_DB_DRIVER"`              //nolint:lll // env tag
	DBSQLitePath        string        `default:"/data/chatz.sqlite"             env:"CHATZ_DB_SQLITE_PATH"`         //nolint:lll // env tag
	DBSQLiteBusyTimeout time.Duration `default:"5s"                             env:"CHATZ_DB_SQLITE_BUSY_TIMEOUT"` //nolint:lll // env tag
	BackupStatusPath    string        `default:"/data/chatz-backup-status.json" env:"CHATZ_BACKUP_STATUS_PATH"`     //nolint:lll // env tag
	BackupMaxAge        time.Duration `default:"24h"                            env:"CHATZ_BACKUP_MAX_AGE"`         //nolint:lll // env tag

	SessionSecret    string `env:"CHATZ_SESSION_SECRET"`
	SecretsKey       string `env:"CHATZ_SECRETS_KEY"`
	AuthPasswordless bool   `default:"false"            env:"CHATZ_AUTH_PASSWORDLESS"` //nolint:lll // env tag
	ShowcaseMode     bool   `default:"false"            env:"CHATZ_SHOWCASE_MODE"`     //nolint:lll // env tag
	DefaultModel     string `env:"CHATZ_DEFAULT_MODEL"`

	UpstreamConnectTimeout    time.Duration `default:"10s" env:"CHATZ_UPSTREAM_CONNECT_TIMEOUT"`     //nolint:lll // env tag
	UpstreamFirstTokenTimeout time.Duration `default:"45s" env:"CHATZ_UPSTREAM_FIRST_TOKEN_TIMEOUT"` //nolint:lll // env tag
	UpstreamTurnTimeout       time.Duration `default:"5m"  env:"CHATZ_UPSTREAM_TURN_TIMEOUT"`        //nolint:lll // env tag
	UpstreamConcurrency       int           `default:"8"   env:"CHATZ_UPSTREAM_CONCURRENCY"`         //nolint:lll // env tag

	// UpstreamsJSON is a JSON array of Upstream. Empty leaves Chatz running
	// without model providers until the operator configures an Elelem driver.
	UpstreamsJSON string `env:"CHATZ_UPSTREAMS"`

	// ForceRealLLM makes upstream drivers talk to the real provider even under
	// `go test`, where they otherwise resolve to the scripted double. Nothing
	// in the repo sets it: the operator opts in from their own `.env`, which
	// `make test-real` forwards into the container. Leave it false elsewhere.
	ForceRealLLM bool `default:"false" env:"CHATZ_FORCE_REAL_LLM"`
}

Config is the full app configuration, parsed from the environment.

func Parse

func Parse() (Config, error)

Parse reads the configuration from the environment.

func (Config) DBConfig

func (c Config) DBConfig() db.Config

DBConfig maps the DB fields to the db package's connection config.

func (Config) ReadinessConfig

func (c Config) ReadinessConfig(appVersion, commit string) operations.Config

ReadinessConfig maps the local backup-marker policy to the operations service. New validates it during startup, before the HTTP server is exposed.

func (Config) SecretsBox

func (c Config) SecretsBox() (*secrets.Box, error)

SecretsBox builds the AEAD box used to seal/open at-rest secrets (MCP HTTP headers + stdio env) from CHATZ_SECRETS_KEY (base64, 32 bytes). An unset key returns a nil box: the app boots without encryption, and any attempt to store an actual secret later fails with secrets.ErrNotConfigured rather than writing plaintext.

func (Config) UpstreamRuntimeConfig

func (c Config) UpstreamRuntimeConfig() (UpstreamRuntime, error)

UpstreamRuntimeConfig converts configured LLM runtime bounds into a typed, validated value before startup creates any outbound provider clients.

func (Config) Upstreams

func (c Config) Upstreams() ([]Upstream, error)

Upstreams returns the explicitly configured Elelem driver upstreams.

type Model

type Model struct {
	ID                  string      `json:"id"`
	Alias               string      `json:"alias"`
	FallbackModels      []string    `json:"fallbackModels"`
	ContextWindow       int         `json:"contextWindow"`
	MaxOutputTokens     int         `json:"maxOutputTokens"`
	SupportsTools       *bool       `json:"supportsTools"`
	SupportsReasoning   *bool       `json:"supportsReasoning"`
	SupportsVision      *bool       `json:"supportsVision"`
	SupportsFiles       *bool       `json:"supportsFiles"`
	FirstTokenLatencyMs int64       `json:"expectedFirstTokenLatencyMs"`
	InputTokenPrice     *TokenPrice `json:"inputPricePerMillionTokens"`
	OutputTokenPrice    *TokenPrice `json:"outputPricePerMillionTokens"`
}

Model supplies optional public metadata for one model that an upstream may advertise. Discovery remains authoritative: an entry never creates a model that the provider did not list.

type TokenPrice

type TokenPrice struct {
	AmountSmallestUnit int64  `json:"amountSmallestUnit"`
	Currency           string `json:"currency"`
}

TokenPrice is the configured price for one million input or output tokens. AmountSmallestUnit uses the currency's smallest unit; it is never a float.

type Upstream

type Upstream struct {
	Name      string           `json:"name"`
	Provider  UpstreamProvider `json:"provider"`
	BaseURL   string           `json:"baseUrl"`
	APIKeyEnv string           `json:"apiKeyEnv"`
	Models    []Model          `json:"models"`
}

Upstream configures one provider endpoint. OpenAI-compatible services such as Ollama and vLLM use the openai provider.

func (Upstream) APIKey

func (u Upstream) APIKey() string

APIKey resolves the key from the env var the config named in APIKeyEnv. Empty when there is no ref or the var is unset — fine for keyless local endpoints like Ollama. Keys are referenced by env name, never stored inline (secrets rule); os.Getenv here dereferences a runtime-chosen name, which gonfiguration (fixed field bindings) can't express.

type UpstreamProvider

type UpstreamProvider string

UpstreamProvider identifies the Elelem driver used by an LLM upstream.

const (
	UpstreamProviderOpenAI    UpstreamProvider = "openai"
	UpstreamProviderAnthropic UpstreamProvider = "anthropic"
)

Supported upstream providers.

type UpstreamRuntime

type UpstreamRuntime struct {
	ConnectTimeout    time.Duration
	FirstTokenTimeout time.Duration
	TurnTimeout       time.Duration
	Concurrency       int
}

UpstreamRuntime is the validated set of request bounds shared by every configured model upstream. Per-upstream customization belongs in a future explicit config surface rather than unvalidated JSON fields.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL