feat(image): define provider-neutral image capability contract and config
Adds internal/domain/image (Provider/Registry per the plugin engine family shape, but no Lifecycle -- a provider is call-scoped, not long-running) and config.ImageConfig with the same explicit enabled=true cost gate MinimaxConfig already uses, since a hosted provider spends real API credits and a local one can saturate the host GPU. Design doc at docs/prds/image-capability-contract.md per CLAUDE.md's rule for undesigned capabilities. No provider implementation lands here -- that's the epic's next child bead. Also re-links all 7 epic children that were unlinked from archie-core-1786748942120-1-6636e629 (same orphaning pattern found in the curator/memory epics earlier this session).
This commit is contained in:
1 parent
622961a875
commit
75158b7fc7
10 files changed
+915
-38
No files matched your search
@@ -283,6 +283,40 @@ func (d Dispatch) LabelValues() []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// ImageConfig configures the image generation capability
|
||||
// (internal/domain/image). See docs/prds/image-capability-contract.md.
|
||||
type ImageConfig struct {
|
||||
// Default selects which registered provider name /image uses when the
|
||||
// request/session has not already picked one. Empty means /image must
|
||||
// always ask which mechanism to use -- a valid, and the default, state.
|
||||
Default string `toml:"default" yaml:"default" json:"default"`
|
||||
// Hosted configures hosted (paid) providers, keyed by provider name.
|
||||
Hosted map[string]ImageHostedProvider `toml:"hosted" yaml:"hosted" json:"hosted"`
|
||||
// Local configures local GPU recipes, keyed by provider name.
|
||||
Local map[string]ImageLocalProvider `toml:"local" yaml:"local" json:"local"`
|
||||
}
|
||||
|
||||
// ImageHostedProvider configures one hosted image provider. Mirrors
|
||||
// MinimaxConfig's cost guard: Enabled defaults false, since a hosted
|
||||
// provider spends real API credits per call and must never activate from
|
||||
// the mere presence of a key.
|
||||
type ImageHostedProvider struct {
|
||||
Enabled bool `toml:"enabled" yaml:"enabled" json:"enabled"`
|
||||
Class string `toml:"class" yaml:"class" json:"class"`
|
||||
APIKeyEnv string `toml:"api_key_env" yaml:"api_key_env" json:"api_key_env"`
|
||||
APIKey secret.SecretRef `toml:"api_key" yaml:"api_key" json:"-"`
|
||||
BaseURL string `toml:"base_url" yaml:"base_url" json:"base_url,omitempty"`
|
||||
}
|
||||
|
||||
// ImageLocalProvider configures one local GPU image recipe. Enabled
|
||||
// defaults false too: a local backend can saturate the host GPU/CPU, its
|
||||
// own kind of unwanted cost, so it gets the same explicit gate as a hosted
|
||||
// provider rather than an on-by-default treatment.
|
||||
type ImageLocalProvider struct {
|
||||
Enabled bool `toml:"enabled" yaml:"enabled" json:"enabled"`
|
||||
Backend string `toml:"backend" yaml:"backend" json:"backend"`
|
||||
}
|
||||
|
||||
// MemoryConfig holds memory provider configuration.
|
||||
type MemoryConfig struct {
|
||||
// Engine selects among the domain/memory engine family
|
||||
@@ -443,6 +477,10 @@ type Config struct {
|
||||
// Memory holds memory provider configuration (from config.memory.yaml).
|
||||
Memory MemoryConfig `toml:"memory" yaml:"memory"`
|
||||
|
||||
// Image configures the image generation capability. Absent is valid
|
||||
// and registers no provider.
|
||||
Image ImageConfig `toml:"image" yaml:"image" json:"image"`
|
||||
|
||||
// Tools holds MCP server and tool policy configuration (from config.tools.yaml).
|
||||
Tools ToolsConfig `toml:"tools" yaml:"tools"`
|
||||
// Indexing locates the workspace codesearch index.
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
// Package image is the image generation capability family: a
|
||||
// provider-neutral boundary for hosted (gpt-image-2-class) and local GPU
|
||||
// backends, so /image routing can target either without depending on
|
||||
// provider-specific types. See docs/prds/image-capability-contract.md for
|
||||
// the design and epic archie-core-1786748942120-1-6636e629.
|
||||
//
|
||||
// Unlike curator and memory (internal/domain/curator,
|
||||
// internal/domain/memory), a Provider has no Lifecycle: it is call-scoped,
|
||||
// not long-running. There is nothing to Start, and "is this usable right
|
||||
// now" is exactly what Generate/Edit returning ErrUnavailable already
|
||||
// communicates — a separate Health poll would duplicate that. This package
|
||||
// defines the contract only; no provider implementation lives here.
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Sentinel errors every provider wraps with fmt.Errorf("%w: %s", ...) so
|
||||
// callers can errors.Is regardless of backend.
|
||||
var (
|
||||
// ErrUnavailable means the provider cannot serve right now (missing
|
||||
// credential, backend down). Per the epic: this must degrade the
|
||||
// capability to a reported error, never prevent Archie from starting
|
||||
// or break text chat.
|
||||
ErrUnavailable = errors.New("image provider unavailable")
|
||||
// ErrUnsupported means the request needs a capability (e.g. Edit, or
|
||||
// Mask) the provider does not declare.
|
||||
ErrUnsupported = errors.New("image capability unsupported by provider")
|
||||
// ErrInputTooLarge means an input image exceeded a limit the provider
|
||||
// itself enforces. Caller-side limits are the routing layer's concern,
|
||||
// not this contract's — see the design doc.
|
||||
ErrInputTooLarge = errors.New("image input exceeds configured limit")
|
||||
// ErrTimeout means the provider's own deadline elapsed, distinct from
|
||||
// the caller's ctx being canceled.
|
||||
ErrTimeout = errors.New("image request timed out")
|
||||
// ErrCanceled means the caller's ctx was canceled.
|
||||
ErrCanceled = errors.New("image request canceled")
|
||||
)
|
||||
|
||||
// ProviderClass distinguishes hosted (spends money/quota per call) from
|
||||
// local (spends host GPU/time). Selection and config validation key off
|
||||
// this, never off Name, so a new provider cannot silently slip through the
|
||||
// hosted-cost gate under an unrecognized name.
|
||||
type ProviderClass string
|
||||
|
||||
const (
|
||||
ClassHosted ProviderClass = "hosted"
|
||||
ClassLocal ProviderClass = "local"
|
||||
)
|
||||
|
||||
// Capability declares what one provider supports, discoverable before a
|
||||
// request is built so a caller can reject an unsupported combination (e.g.
|
||||
// edit-with-mask on a generate-only provider) instead of sending it and
|
||||
// parsing a provider-specific error back out.
|
||||
type Capability struct {
|
||||
Generate bool
|
||||
// Edit requires at least one input image. MaxInputs bounds how many.
|
||||
Edit bool
|
||||
MaxInputs int
|
||||
// Mask reports whether Edit supports restricting the edited region to
|
||||
// a mask image.
|
||||
Mask bool
|
||||
// Sizes lists provider-declared output sizes (e.g. "1024x1024"). Empty
|
||||
// means the provider does not constrain size.
|
||||
Sizes []string
|
||||
}
|
||||
|
||||
// Validate checks that a declared Capability is internally consistent:
|
||||
// Edit implies at least one allowed input, and Mask cannot be declared
|
||||
// without Edit.
|
||||
func (c Capability) Validate() error {
|
||||
if c.Edit && c.MaxInputs < 1 {
|
||||
return errors.New("image capability: edit requires MaxInputs >= 1")
|
||||
}
|
||||
if c.Mask && !c.Edit {
|
||||
return errors.New("image capability: mask requires edit")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ImageData carries image bytes in-process, never a path or URL: the
|
||||
// domain layer must not assume a filesystem or a fetchable location.
|
||||
type ImageData struct {
|
||||
Bytes []byte
|
||||
MIMEType string
|
||||
}
|
||||
|
||||
// GenerateRequest asks a provider to create images from a prompt alone.
|
||||
type GenerateRequest struct {
|
||||
Prompt string
|
||||
// Size is a provider-declared value from Capability.Sizes, or "" for
|
||||
// the provider's default.
|
||||
Size string
|
||||
// N is the number of images requested. Zero means 1.
|
||||
N int
|
||||
// Options carries provider-specific overflow (e.g. "quality"),
|
||||
// deliberately untyped so the contract does not grow a field per
|
||||
// provider.
|
||||
Options map[string]string
|
||||
}
|
||||
|
||||
// Validate checks the fields every provider needs regardless of backend.
|
||||
func (r GenerateRequest) Validate() error {
|
||||
if r.Prompt == "" {
|
||||
return errors.New("image generate request: prompt must not be empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditRequest asks a provider to edit one or more input images per a
|
||||
// prompt, optionally restricted to a masked region.
|
||||
type EditRequest struct {
|
||||
Prompt string
|
||||
Inputs []ImageData
|
||||
// Mask restricts the edited region. Only valid when the provider's
|
||||
// Capability.Mask is true.
|
||||
Mask *ImageData
|
||||
Size string
|
||||
N int
|
||||
|
||||
Options map[string]string
|
||||
}
|
||||
|
||||
// Validate checks the fields every provider needs regardless of backend.
|
||||
// Capability-specific bounds (MaxInputs, Mask support) are the provider's
|
||||
// own responsibility to enforce against its declared Capability.
|
||||
func (r EditRequest) Validate() error {
|
||||
if r.Prompt == "" {
|
||||
return errors.New("image edit request: prompt must not be empty")
|
||||
}
|
||||
if len(r.Inputs) == 0 {
|
||||
return errors.New("image edit request: at least one input image is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Result is what a provider returns from Generate or Edit.
|
||||
type Result struct {
|
||||
Images []ImageData
|
||||
Provider string
|
||||
Model string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// Provider is the typed contract every image backend implements — hosted
|
||||
// (gpt-image-2-class) or local (GPU recipe).
|
||||
type Provider interface {
|
||||
Name() string
|
||||
Class() ProviderClass
|
||||
Capability() Capability
|
||||
Generate(ctx context.Context, req GenerateRequest) (Result, error)
|
||||
Edit(ctx context.Context, req EditRequest) (Result, error)
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package image
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCapabilityValidateRejectsEditWithoutInputs(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := Capability{Edit: true, MaxInputs: 0}
|
||||
if err := c.Validate(); err == nil {
|
||||
t.Fatal("Validate() = nil, want error for edit with MaxInputs 0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCapabilityValidateRejectsMaskWithoutEdit(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := Capability{Mask: true}
|
||||
if err := c.Validate(); err == nil {
|
||||
t.Fatal("Validate() = nil, want error for mask without edit")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCapabilityValidateAcceptsGenerateOnly(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := Capability{Generate: true}
|
||||
if err := c.Validate(); err != nil {
|
||||
t.Fatalf("Validate() = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCapabilityValidateAcceptsEditWithMask(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := Capability{Edit: true, MaxInputs: 1, Mask: true}
|
||||
if err := c.Validate(); err != nil {
|
||||
t.Fatalf("Validate() = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateRequestValidateRequiresPrompt(t *testing.T) {
|
||||
t.Parallel()
|
||||
if err := (GenerateRequest{}).Validate(); err == nil {
|
||||
t.Fatal("Validate() = nil, want error for empty prompt")
|
||||
}
|
||||
if err := (GenerateRequest{Prompt: "a cat"}).Validate(); err != nil {
|
||||
t.Fatalf("Validate() = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEditRequestValidateRequiresPromptAndInputs(t *testing.T) {
|
||||
t.Parallel()
|
||||
cases := []struct {
|
||||
name string
|
||||
req EditRequest
|
||||
want bool // true if a valid request
|
||||
}{
|
||||
{"empty", EditRequest{}, false},
|
||||
{"no inputs", EditRequest{Prompt: "add a hat"}, false},
|
||||
{"no prompt", EditRequest{Inputs: []ImageData{{Bytes: []byte("x")}}}, false},
|
||||
{"valid", EditRequest{Prompt: "add a hat", Inputs: []ImageData{{Bytes: []byte("x")}}}, true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := tc.req.Validate()
|
||||
if (err == nil) != tc.want {
|
||||
t.Fatalf("Validate() = %v, want valid=%v", err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"slices"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// ErrDuplicate is returned when a provider name is already registered.
|
||||
var ErrDuplicate = errors.New("duplicate image provider")
|
||||
|
||||
// Registry is the image family owner: registration and discovery only. A
|
||||
// Provider is call-scoped (see package doc), so unlike curator's and
|
||||
// memory's registries there is no Start/Health/Stop to orchestrate here.
|
||||
type Registry struct {
|
||||
mu sync.RWMutex
|
||||
providers map[string]Provider
|
||||
}
|
||||
|
||||
// NewRegistry builds an empty provider registry.
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{providers: make(map[string]Provider)}
|
||||
}
|
||||
|
||||
// Register validates the provider's declared capability and adds it.
|
||||
// Registration fails on a nil provider, an invalid Capability, or a
|
||||
// duplicate name.
|
||||
func (r *Registry) Register(p Provider) error {
|
||||
if isNilProvider(p) {
|
||||
return errors.New("image: nil provider")
|
||||
}
|
||||
if err := p.Capability().Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if _, dup := r.providers[p.Name()]; dup {
|
||||
return fmt.Errorf("%w: %s", ErrDuplicate, p.Name())
|
||||
}
|
||||
r.providers[p.Name()] = p
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get returns the registered provider by name.
|
||||
func (r *Registry) Get(name string) (Provider, bool) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
p, ok := r.providers[name]
|
||||
return p, ok
|
||||
}
|
||||
|
||||
// Names returns every registered provider name, sorted.
|
||||
func (r *Registry) Names() []string {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
names := make([]string, 0, len(r.providers))
|
||||
for name := range r.providers {
|
||||
names = append(names, name)
|
||||
}
|
||||
slices.Sort(names)
|
||||
return names
|
||||
}
|
||||
|
||||
// ByClass returns the sorted names of registered providers matching class.
|
||||
func (r *Registry) ByClass(class ProviderClass) []string {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
var names []string
|
||||
for name, p := range r.providers {
|
||||
if p.Class() == class {
|
||||
names = append(names, name)
|
||||
}
|
||||
}
|
||||
slices.Sort(names)
|
||||
return names
|
||||
}
|
||||
|
||||
func isNilProvider(p Provider) bool {
|
||||
v := reflect.ValueOf(p)
|
||||
switch v.Kind() {
|
||||
case reflect.Invalid:
|
||||
return true
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
||||
return v.IsNil()
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type fakeProvider struct {
|
||||
name string
|
||||
class ProviderClass
|
||||
cap Capability
|
||||
}
|
||||
|
||||
func (f fakeProvider) Name() string { return f.name }
|
||||
func (f fakeProvider) Class() ProviderClass { return f.class }
|
||||
func (f fakeProvider) Capability() Capability { return f.cap }
|
||||
|
||||
func (f fakeProvider) Generate(context.Context, GenerateRequest) (Result, error) {
|
||||
return Result{Provider: f.name, CreatedAt: time.Unix(0, 0)}, nil
|
||||
}
|
||||
|
||||
func (f fakeProvider) Edit(context.Context, EditRequest) (Result, error) {
|
||||
if !f.cap.Edit {
|
||||
return Result{}, ErrUnsupported
|
||||
}
|
||||
return Result{Provider: f.name, CreatedAt: time.Unix(0, 0)}, nil
|
||||
}
|
||||
|
||||
func TestRegistryRegisterAndGet(t *testing.T) {
|
||||
t.Parallel()
|
||||
r := NewRegistry()
|
||||
p := fakeProvider{name: "hosted-a", class: ClassHosted, cap: Capability{Generate: true}}
|
||||
if err := r.Register(p); err != nil {
|
||||
t.Fatalf("Register() = %v", err)
|
||||
}
|
||||
got, ok := r.Get("hosted-a")
|
||||
if !ok || got.Name() != "hosted-a" {
|
||||
t.Fatalf("Get() = %v, %v, want hosted-a", got, ok)
|
||||
}
|
||||
if _, ok := r.Get("missing"); ok {
|
||||
t.Fatal("Get(missing) = true, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryRegisterRejectsDuplicate(t *testing.T) {
|
||||
t.Parallel()
|
||||
r := NewRegistry()
|
||||
p := fakeProvider{name: "hosted-a", class: ClassHosted, cap: Capability{Generate: true}}
|
||||
if err := r.Register(p); err != nil {
|
||||
t.Fatalf("Register() = %v", err)
|
||||
}
|
||||
err := r.Register(p)
|
||||
if !errors.Is(err, ErrDuplicate) {
|
||||
t.Fatalf("Register(dup) = %v, want ErrDuplicate", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryRegisterRejectsNilProvider(t *testing.T) {
|
||||
t.Parallel()
|
||||
r := NewRegistry()
|
||||
var p Provider
|
||||
if err := r.Register(p); err == nil {
|
||||
t.Fatal("Register(nil) = nil, want error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryRegisterRejectsInvalidCapability(t *testing.T) {
|
||||
t.Parallel()
|
||||
r := NewRegistry()
|
||||
p := fakeProvider{name: "bad", class: ClassHosted, cap: Capability{Mask: true}}
|
||||
if err := r.Register(p); err == nil {
|
||||
t.Fatal("Register(invalid capability) = nil, want error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryNamesSorted(t *testing.T) {
|
||||
t.Parallel()
|
||||
r := NewRegistry()
|
||||
for _, name := range []string{"zeta", "alpha", "mid"} {
|
||||
if err := r.Register(fakeProvider{name: name, class: ClassHosted, cap: Capability{Generate: true}}); err != nil {
|
||||
t.Fatalf("Register(%s) = %v", name, err)
|
||||
}
|
||||
}
|
||||
got := r.Names()
|
||||
want := []string{"alpha", "mid", "zeta"}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("Names() = %v, want %v", got, want)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("Names() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryByClassFilters(t *testing.T) {
|
||||
t.Parallel()
|
||||
r := NewRegistry()
|
||||
if err := r.Register(fakeProvider{name: "hosted-a", class: ClassHosted, cap: Capability{Generate: true}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.Register(fakeProvider{name: "local-a", class: ClassLocal, cap: Capability{Generate: true}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hosted := r.ByClass(ClassHosted)
|
||||
if len(hosted) != 1 || hosted[0] != "hosted-a" {
|
||||
t.Fatalf("ByClass(hosted) = %v, want [hosted-a]", hosted)
|
||||
}
|
||||
local := r.ByClass(ClassLocal)
|
||||
if len(local) != 1 || local[0] != "local-a" {
|
||||
t.Fatalf("ByClass(local) = %v, want [local-a]", local)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFakeProviderEditRespectsCapability(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := fakeProvider{name: "generate-only", class: ClassHosted, cap: Capability{Generate: true}}
|
||||
_, err := p.Edit(context.Background(), EditRequest{Prompt: "x", Inputs: []ImageData{{Bytes: []byte("x")}}})
|
||||
if !errors.Is(err, ErrUnsupported) {
|
||||
t.Fatalf("Edit() = %v, want ErrUnsupported", err)
|
||||
}
|
||||
}
|
||||
@@ -87,9 +87,49 @@ func validate(cfg *config.Config) error {
|
||||
if err := validateMemory(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateImage(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
return validateCapture(cfg)
|
||||
}
|
||||
|
||||
// validateImage rejects an enabled hosted provider with no class or no
|
||||
// resolvable credential, and a Default naming a provider that is not both
|
||||
// present and enabled -- either would let /image silently fall back to
|
||||
// "always ask" (harmless) or fail at call time with a confusing error
|
||||
// (not harmless). An absent [image] section is valid and registers no
|
||||
// provider, matching the epic's non-goal that a paid provider must never
|
||||
// activate by omission.
|
||||
func validateImage(cfg *config.Config) error {
|
||||
for name, p := range cfg.Image.Hosted {
|
||||
if !p.Enabled {
|
||||
continue
|
||||
}
|
||||
if p.Class == "" {
|
||||
return fmt.Errorf("%w: image.hosted.%s.class is required when enabled", ErrInvalidInput, name)
|
||||
}
|
||||
if p.APIKeyEnv == "" && p.APIKey == (secret.SecretRef{}) {
|
||||
return fmt.Errorf("%w: image.hosted.%s requires api_key_env or api_key when enabled", ErrInvalidInput, name)
|
||||
}
|
||||
}
|
||||
for name, p := range cfg.Image.Local {
|
||||
if p.Enabled && p.Backend == "" {
|
||||
return fmt.Errorf("%w: image.local.%s.backend is required when enabled", ErrInvalidInput, name)
|
||||
}
|
||||
}
|
||||
if cfg.Image.Default != "" {
|
||||
hosted, hostedOK := cfg.Image.Hosted[cfg.Image.Default]
|
||||
local, localOK := cfg.Image.Local[cfg.Image.Default]
|
||||
switch {
|
||||
case hostedOK && hosted.Enabled, localOK && local.Enabled:
|
||||
// found and enabled
|
||||
default:
|
||||
return fmt.Errorf("%w: image.default %q must name an enabled provider under image.hosted or image.local", ErrInvalidInput, cfg.Image.Default)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateMemory rejects an engine name outside memoryEngines. Empty is
|
||||
// valid input to Validate (applyDefaults resolves it to memoryEngineBuiltin
|
||||
// before this runs on the real load path); it is not "unknown" so it does
|
||||
|
||||
@@ -380,3 +380,104 @@ func TestValidateMemoryRejectsProviderSetToAnything(t *testing.T) {
|
||||
t.Errorf("error text = %q, want it to name the field and the value", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateImageAcceptsAbsentSection(t *testing.T) {
|
||||
cfg := minimalValidConfig()
|
||||
if err := validateImage(&cfg); err != nil {
|
||||
t.Fatalf("validateImage(absent) = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateImageRejectsEnabledHostedWithoutClass(t *testing.T) {
|
||||
cfg := minimalValidConfig()
|
||||
cfg.Image.Hosted = map[string]config.ImageHostedProvider{
|
||||
"openai": {Enabled: true, APIKeyEnv: "OPENAI_API_KEY"},
|
||||
}
|
||||
err := validateImage(&cfg)
|
||||
if !errors.Is(err, ErrInvalidInput) {
|
||||
t.Fatalf("validateImage(no class) = %v, want wrapping ErrInvalidInput", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "image.hosted.openai.class") {
|
||||
t.Errorf("error text = %q, want it to name the field", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateImageRejectsEnabledHostedWithoutCredential(t *testing.T) {
|
||||
cfg := minimalValidConfig()
|
||||
cfg.Image.Hosted = map[string]config.ImageHostedProvider{
|
||||
"openai": {Enabled: true, Class: "openai"},
|
||||
}
|
||||
err := validateImage(&cfg)
|
||||
if !errors.Is(err, ErrInvalidInput) {
|
||||
t.Fatalf("validateImage(no credential) = %v, want wrapping ErrInvalidInput", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateImageAcceptsDisabledHostedWithoutCredential(t *testing.T) {
|
||||
cfg := minimalValidConfig()
|
||||
cfg.Image.Hosted = map[string]config.ImageHostedProvider{
|
||||
"openai": {Enabled: false},
|
||||
}
|
||||
if err := validateImage(&cfg); err != nil {
|
||||
t.Fatalf("validateImage(disabled) = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateImageAcceptsEnabledHostedWithCredential(t *testing.T) {
|
||||
cfg := minimalValidConfig()
|
||||
cfg.Image.Hosted = map[string]config.ImageHostedProvider{
|
||||
"openai": {Enabled: true, Class: "openai", APIKeyEnv: "OPENAI_API_KEY"},
|
||||
}
|
||||
if err := validateImage(&cfg); err != nil {
|
||||
t.Fatalf("validateImage(valid hosted) = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateImageRejectsEnabledLocalWithoutBackend(t *testing.T) {
|
||||
cfg := minimalValidConfig()
|
||||
cfg.Image.Local = map[string]config.ImageLocalProvider{
|
||||
"sdxl": {Enabled: true},
|
||||
}
|
||||
err := validateImage(&cfg)
|
||||
if !errors.Is(err, ErrInvalidInput) {
|
||||
t.Fatalf("validateImage(no backend) = %v, want wrapping ErrInvalidInput", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "image.local.sdxl.backend") {
|
||||
t.Errorf("error text = %q, want it to name the field", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateImageRejectsDefaultNamingDisabledProvider(t *testing.T) {
|
||||
cfg := minimalValidConfig()
|
||||
cfg.Image.Default = "openai"
|
||||
cfg.Image.Hosted = map[string]config.ImageHostedProvider{
|
||||
"openai": {Enabled: false},
|
||||
}
|
||||
err := validateImage(&cfg)
|
||||
if !errors.Is(err, ErrInvalidInput) {
|
||||
t.Fatalf("validateImage(default names disabled) = %v, want wrapping ErrInvalidInput", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), `image.default "openai"`) {
|
||||
t.Errorf("error text = %q, want it to name the field and value", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateImageRejectsDefaultNamingUnregisteredProvider(t *testing.T) {
|
||||
cfg := minimalValidConfig()
|
||||
cfg.Image.Default = "missing"
|
||||
err := validateImage(&cfg)
|
||||
if !errors.Is(err, ErrInvalidInput) {
|
||||
t.Fatalf("validateImage(default names unregistered) = %v, want wrapping ErrInvalidInput", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateImageAcceptsDefaultNamingEnabledLocalProvider(t *testing.T) {
|
||||
cfg := minimalValidConfig()
|
||||
cfg.Image.Default = "sdxl"
|
||||
cfg.Image.Local = map[string]config.ImageLocalProvider{
|
||||
"sdxl": {Enabled: true, Backend: "sdxl-turbo"},
|
||||
}
|
||||
if err := validateImage(&cfg); err != nil {
|
||||
t.Fatalf("validateImage(default names enabled local) = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user