Add service orchestration and command

Wire the intake and answer subsystems together into a running
application.

- config: maps YAML file to internal package configuration.
- service: manages lifecycle and graceful shutdown.
- cmd/raven: entry point for flag parsing and signal handling.
This commit is contained in:
dwrz
2026-01-04 21:07:26 +00:00
parent 486cc5fa52
commit 0de0793500
4 changed files with 827 additions and 0 deletions

151
internal/config/config.go Normal file
View File

@@ -0,0 +1,151 @@
// Package config loads and validates YAML configuration for raven.
package config
import (
"fmt"
"os"
"regexp"
"time"
"raven/internal/filter"
"raven/internal/imap"
"raven/internal/intake"
"raven/internal/llm"
"raven/internal/smtp"
"raven/internal/tool"
"gopkg.in/yaml.v3"
)
// Config holds all application configuration sections.
type Config struct {
// Concurrency is the number of concurrent answer workers.
// Defaults to 1.
Concurrency int `yaml:"concurrency"`
// ShutdownTimeout is the maximum time to wait for graceful shutdown.
// Defaults to "30s".
ShutdownTimeout string `yaml:"shutdown_timeout"`
// shutdownTimeout is the parsed duration, set during Load.
shutdownTimeout time.Duration `yaml:"-"`
// Filters configures server-side IMAP search criteria.
Filters filter.Filters `yaml:"filters"`
// IMAP configures the mail server connection for reading messages.
IMAP imap.Config `yaml:"imap"`
// Intake configures the message retrieval strategy (IDLE or poll).
Intake intake.Config `yaml:"intake"`
// LLM configures the language model client.
LLM llm.Config `yaml:"llm"`
// SMTP configures the mail server connection for sending responses.
// Credentials default to IMAP values if not specified.
SMTP smtp.SMTP `yaml:"smtp"`
// Tools defines external commands available to the LLM.
Tools []tool.Tool `yaml:"tools"`
}
// ApplyDefaults sets default values for optional configuration fields.
// Called automatically by Load before validation.
func (cfg *Config) ApplyDefaults() {
if cfg.Concurrency == 0 {
cfg.Concurrency = 1
}
if cfg.ShutdownTimeout == "" {
cfg.ShutdownTimeout = "30s"
}
// Intake
if cfg.Intake.Mode == "" {
cfg.Intake.Mode = intake.ModePoll
}
if cfg.Intake.PollInterval == "" {
cfg.Intake.PollInterval = "30s"
}
// SMTP defaults to IMAP credentials.
if cfg.SMTP.User == "" {
cfg.SMTP.User = cfg.IMAP.User
}
if cfg.SMTP.Password == "" {
cfg.SMTP.Password = cfg.IMAP.Password
}
if cfg.SMTP.From == "" {
cfg.SMTP.From = cfg.IMAP.User
}
}
// GetShutdownTimeout returns the parsed shutdown timeout duration.
func (c *Config) GetShutdownTimeout() time.Duration {
return c.shutdownTimeout
}
// Validate checks that all required fields are present and valid.
// Delegates to each subsection's Validate method.
func (c *Config) Validate() error {
if err := c.IMAP.Validate(); err != nil {
return fmt.Errorf("invalid imap config: %v", err)
}
if err := c.Intake.Validate(); err != nil {
return fmt.Errorf("invalid intake config: %v", err)
}
if err := c.LLM.Validate(); err != nil {
return fmt.Errorf("invalid llm config: %v", err)
}
if err := c.SMTP.Validate(); err != nil {
return fmt.Errorf("invalid smtp config: %v", err)
}
if _, err := time.ParseDuration(c.ShutdownTimeout); err != nil {
return fmt.Errorf(
"invalid shutdown_timeout %q: %w",
c.ShutdownTimeout, err,
)
}
for i, t := range c.Tools {
if err := t.Validate(); err != nil {
return fmt.Errorf("tools[%d] invalid: %v", i, err)
}
}
return nil
}
// Load reads a YAML configuration file, expands environment variables,
// applies defaults, and validates the result. Returns an error if the
// file cannot be read, parsed, or contains invalid configuration.
func Load(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}
// Expand environment variables.
// Unset variables are replaced with empty strings.
re := regexp.MustCompile(`\$\{([^}]+)\}`)
expanded := re.ReplaceAllStringFunc(
string(data),
func(match string) string {
// Extract variable name from ${VAR}.
v := match[2 : len(match)-1]
return os.Getenv(v)
},
)
var cfg Config
if err := yaml.Unmarshal([]byte(expanded), &cfg); err != nil {
return nil, fmt.Errorf("parse config: %w", err)
}
cfg.ApplyDefaults()
if err := cfg.Validate(); err != nil {
return nil, fmt.Errorf("invalid config: %v", err)
}
d, err := time.ParseDuration(cfg.ShutdownTimeout)
if err != nil {
return nil, fmt.Errorf(
"parse shutdown timeout: %v", err,
)
}
cfg.shutdownTimeout = d
return &cfg, nil
}

View File

@@ -0,0 +1,375 @@
package config
import (
"os"
"path/filepath"
"testing"
"time"
)
// validConfig returns a minimal valid YAML configuration.
func validConfig() string {
return `
imap:
host: imap.example.com
port: 993
user: test@example.com
password: secret
mailbox: INBOX
intake:
mode: poll
poll_interval: 30s
llm:
provider: llama.cpp
model: qwen3-4b
url: http://localhost:8080
smtp:
host: smtp.example.com
port: 587
`
}
func writeConfig(t *testing.T, content string) string {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatalf("write config file: %v", err)
}
return path
}
func TestLoad(t *testing.T) {
tests := []struct {
name string
config string
setup func(t *testing.T)
shouldErr bool
check func(t *testing.T, cfg *Config)
}{
{
name: "valid config with defaults",
config: validConfig(),
check: func(t *testing.T, cfg *Config) {
if cfg.Concurrency != 1 {
t.Errorf(
"Concurrency = %d, want 1",
cfg.Concurrency,
)
}
if cfg.ShutdownTimeout != "30s" {
t.Errorf(
"ShutdownTimeout = %q, want %q",
cfg.ShutdownTimeout, "30s",
)
}
if cfg.GetShutdownTimeout() != 30*time.Second {
t.Errorf(
"GetShutdownTimeout() = %v, want 30s",
cfg.GetShutdownTimeout(),
)
}
if cfg.SMTP.User != cfg.IMAP.User {
t.Errorf(
"SMTP.User = %q, want %q",
cfg.SMTP.User, cfg.IMAP.User,
)
}
if cfg.SMTP.Password != cfg.IMAP.Password {
t.Errorf(
"SMTP.Password not inherited from IMAP",
)
}
if cfg.SMTP.From != cfg.IMAP.User {
t.Errorf(
"SMTP.From = %q, want %q",
cfg.SMTP.From, cfg.IMAP.User,
)
}
if cfg.Intake.Mode != "poll" {
t.Errorf(
"Intake.Mode = %q, want %q",
cfg.Intake.Mode, "poll",
)
}
if cfg.Intake.PollInterval != "30s" {
t.Errorf(
"Intake.PollInterval = %q, want %q",
cfg.Intake.PollInterval, "30s",
)
}
},
},
{
name: "explicit values not overwritten",
config: `
concurrency: 5
shutdown_timeout: "60s"
imap:
host: imap.example.com
port: 993
user: test@example.com
password: secret
mailbox: INBOX
intake:
mode: idle
llm:
provider: llama.cpp
model: qwen3-4b
url: http://localhost:8080
smtp:
host: smtp.example.com
port: 587
user: smtp-user
password: smtp-password
from: sender@example.com
`,
check: func(t *testing.T, cfg *Config) {
if cfg.Concurrency != 5 {
t.Errorf(
"Concurrency = %d, want 5",
cfg.Concurrency,
)
}
if cfg.ShutdownTimeout != "60s" {
t.Errorf(
"ShutdownTimeout = %q, want %q",
cfg.ShutdownTimeout, "60s",
)
}
if cfg.GetShutdownTimeout() != 60*time.Second {
t.Errorf(
"GetShutdownTimeout() = %v, want 60s",
cfg.GetShutdownTimeout(),
)
}
if cfg.SMTP.User != "smtp-user" {
t.Errorf(
"SMTP.User = %q, want %q",
cfg.SMTP.User, "smtp-user",
)
}
if cfg.SMTP.Password != "smtp-password" {
t.Errorf(
"SMTP.Password = %q, want %q",
cfg.SMTP.Password,
"smtp-password",
)
}
if cfg.SMTP.From != "sender@example.com" {
t.Errorf(
"SMTP.From = %q, want %q",
cfg.SMTP.From,
"sender@example.com",
)
}
},
},
{
name: "env expansion",
config: `
imap:
host: imap.example.com
port: 993
user: test@example.com
password: ${TEST_IMAP_PASSWORD}
mailbox: INBOX
intake:
mode: poll
poll_interval: 30s
llm:
provider: llama.cpp
model: qwen3-4b
url: http://localhost:8080
smtp:
host: smtp.example.com
port: 587
`,
setup: func(t *testing.T) {
t.Setenv("TEST_IMAP_PASSWORD", "env-secret")
},
check: func(t *testing.T, cfg *Config) {
if cfg.IMAP.Password != "env-secret" {
t.Errorf(
"IMAP.Password = %q, want %q",
cfg.IMAP.Password,
"env-secret",
)
}
},
},
{
name: "env expansion unset var becomes empty",
config: `
imap:
host: imap.example.com
port: 993
user: test@example.com
password: ${TEST_UNSET_VAR}
mailbox: INBOX
intake:
mode: poll
poll_interval: 30s
llm:
provider: llama.cpp
model: qwen3-4b
url: http://localhost:8080
smtp:
host: smtp.example.com
port: 587
`,
setup: func(t *testing.T) {
t.Setenv("TEST_UNSET_VAR", "")
},
shouldErr: true,
},
{
name: "invalid yaml",
config: `imap: [invalid yaml`,
shouldErr: true,
},
{
name: "invalid shutdown_timeout",
config: `
imap:
host: imap.example.com
port: 993
user: test@example.com
password: secret
mailbox: INBOX
intake:
mode: poll
poll_interval: 30s
llm:
provider: llama.cpp
model: qwen3-4b
url: http://localhost:8080
smtp:
host: smtp.example.com
port: 587
shutdown_timeout: "not-a-duration"
`,
shouldErr: true,
},
{
name: "config with tools",
config: `
imap:
host: imap.example.com
port: 993
user: test@example.com
password: secret
mailbox: INBOX
intake:
mode: poll
poll_interval: 30s
llm:
provider: llama.cpp
model: qwen3-4b
url: http://localhost:8080
smtp:
host: smtp.example.com
port: 587
tools:
- name: echo
description: echoes input
command: echo
`,
check: func(t *testing.T, cfg *Config) {
if len(cfg.Tools) != 1 {
t.Errorf(
"len(Tools) = %d, want 1",
len(cfg.Tools),
)
}
if cfg.Tools[0].Name != "echo" {
t.Errorf(
"Tools[0].Name = %q, want %q",
cfg.Tools[0].Name, "echo",
)
}
},
},
{
name: "intake mode defaults when empty",
config: `
imap:
host: imap.example.com
port: 993
user: test@example.com
password: secret
mailbox: INBOX
llm:
provider: llama.cpp
model: qwen3-4b
url: http://localhost:8080
smtp:
host: smtp.example.com
port: 587
`,
check: func(t *testing.T, cfg *Config) {
if cfg.Intake.Mode != "poll" {
t.Errorf(
"Intake.Mode = %q, want %q",
cfg.Intake.Mode, "poll",
)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setup != nil {
tt.setup(t)
}
path := writeConfig(t, tt.config)
cfg, err := Load(path)
if tt.shouldErr {
if err == nil {
t.Fatal("Load() expected error")
}
return
}
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if tt.check != nil {
tt.check(t, cfg)
}
})
}
}
func TestLoad_FileNotFound(t *testing.T) {
if _, err := Load("/nonexistent/path/config.yaml"); err == nil {
t.Fatal("Load() expected error for missing file")
}
}