registry

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package registry provides capability registration and discovery.

This package implements the factory pattern for dynamic capability loading. Capabilities self-register via init() functions, enabling modular plugin-style architecture.

Index

Constants

This section is empty.

Variables

View Source
var DeprecatedKeys = map[string]string{
	"judge_model":            "set model inside judge.config (YAML) or judge_config map instead",
	"judge_generator_config": "use judge_config instead",
	"judge_generator":        "use judge_generator_type instead",
}

DeprecatedKeys lists all deprecated config keys and their migration guidance. This is the single source of truth for deprecated configuration.

View Source
var ErrNotFound = fmt.Errorf("capability not found")

ErrNotFound is returned when a capability is not registered.

Functions

func ApplyOptions

func ApplyOptions[C any](cfg C, opts ...Option[C]) C

ApplyOptions applies a series of options to a config struct. Returns the modified config.

Usage:

cfg := ApplyOptions(DefaultOpenAIConfig(), WithModel("gpt-4"), WithTemp(0.5))

func FromMap

func FromMap[C any, T any](factory TypedFactory[C, T], parser func(Config) (C, error)) func(Config) (T, error)

FromMap adapts a TypedFactory to work with legacy registry.Config (map[string]any). This enables gradual migration: new code uses typed configs, while maintaining backward compatibility with YAML/JSON configuration.

Usage:

typedFactory := func(cfg OpenAIConfig) (*OpenAI, error) { ... }
parser := func(m Config) (OpenAIConfig, error) { ... }
legacyFactory := FromMap(typedFactory, parser)
generators.Register("openai.OpenAI", legacyFactory)

func FromMapNoConfig

func FromMapNoConfig[T any](factory TypedFactory[NoConfig, T]) func(Config) (T, error)

FromMapNoConfig adapts a NoConfig TypedFactory to legacy registry.Config. Use this for factories that ignore configuration.

Usage:

typedFactory := func(_ NoConfig) (*MyProbe, error) { return &MyProbe{}, nil }
legacyFactory := FromMapNoConfig(typedFactory)
probes.Register("myprobe.MyProbe", legacyFactory)

func GetAPIKeyWithEnv

func GetAPIKeyWithEnv(cfg Config, envVar string, generatorName string) (string, error)

GetAPIKeyWithEnv retrieves an API key from config, falling back to an environment variable. Returns an error if neither source provides a value.

func GetBool

func GetBool(cfg Config, key string, defaultValue bool) bool

GetBool retrieves a bool value from Config with a default fallback.

func GetFloat32

func GetFloat32(cfg Config, key string, defaultValue float32) float32

GetFloat32 retrieves a float32 value from Config with a default fallback. Handles both float64 and int.

func GetFloat64

func GetFloat64(cfg Config, key string, defaultValue float64) float64

GetFloat64 retrieves a float64 value from Config with a default fallback. Handles both float64 and int.

func GetInt

func GetInt(cfg Config, key string, defaultValue int) int

GetInt retrieves an int value from Config with a default fallback. Handles both int and float64 (JSON numbers are float64).

func GetOptionalAPIKeyWithEnv

func GetOptionalAPIKeyWithEnv(cfg Config, envVar string) string

GetOptionalAPIKeyWithEnv retrieves an API key from config or an environment variable. Returns empty string without error if neither is set.

func GetString

func GetString(cfg Config, key string, defaultValue string) string

GetString retrieves a string value from Config with a default fallback.

func GetStringSlice

func GetStringSlice(cfg Config, key string, defaultValue []string) []string

GetStringSlice retrieves a []string from Config with a default fallback. Handles both []string and []any (where elements are strings).

func MaskAPIKey added in v0.0.2

func MaskAPIKey(key string) string

MaskAPIKey masks an API key for safe display in logs and error messages. Shows only first 3 and last 3 characters. Keys of 6 or fewer characters are fully masked. Empty keys display as "<empty>".

This function uses byte-based indexing (len/slice), which assumes the key contains only ASCII characters. This is standard for API keys (e.g., OpenAI "sk-...", Anthropic "sk-ant-...", Azure hex subscription keys). Do not use with keys containing multi-byte UTF-8 characters.

Examples:

"sk-1234567890abcdef" -> "sk-***def"
"short" -> "***"
"" -> "<empty>"

func NewWithOptions

func NewWithOptions[C any, T any](
	defaults C,
	factory TypedFactory[C, T],
	opts ...Option[C],
) (T, error)

NewWithOptions creates a component using functional options. This is a convenience wrapper for common factory patterns.

Usage:

func NewOpenAI(opts ...OpenAIOption) (*OpenAI, error) {
    cfg := ApplyOptions(DefaultOpenAIConfig(), opts...)
    return newOpenAIFromConfig(cfg)
}

func RequireString

func RequireString(cfg Config, key string) (string, error)

RequireString retrieves a required string value from Config. Returns an error if the key is missing or not a string.

func RequireStringSlice

func RequireStringSlice(cfg Config, key string) ([]string, error)

RequireStringSlice retrieves a required []string from Config. Returns an error if the key is missing or not a slice of strings.

func WarnDeprecatedKeys added in v0.0.10

func WarnDeprecatedKeys(cfg Config)

WarnDeprecatedKeys checks cfg for any keys in DeprecatedKeys and emits slog.Warn for each. Each key only warns once per process to avoid log spam during scans.

Types

type Config

type Config map[string]any

Config holds configuration for capability instantiation.

type NoConfig

type NoConfig struct{}

NoConfig is a marker type for factories that don't require configuration. Use this instead of ignoring the registry.Config parameter.

Usage:

var factory TypedFactory[NoConfig, *MyProbe] = func(_ NoConfig) (*MyProbe, error) { return &MyProbe{}, nil }

type Option

type Option[C any] func(*C)

Option is a generic functional option type. Each config struct should define its own Option type alias.

Usage:

type OpenAIOption = Option[OpenAIConfig]
func WithModel(m string) OpenAIOption { return func(c *OpenAIConfig) { c.Model = m } }

type PluginCache

type PluginCache struct {
	// contains filtered or unexported fields
}

PluginCache manages cached plugin metadata for fast startup. It is safe for concurrent use.

func NewPluginCache

func NewPluginCache(path string) *PluginCache

NewPluginCache creates a new plugin cache with the given file path.

func (*PluginCache) Clear

func (c *PluginCache) Clear()

Clear removes all entries from the cache.

func (*PluginCache) Get

func (c *PluginCache) Get(category, name string) (PluginMeta, bool)

Get retrieves plugin metadata from the cache. Returns false if the entry does not exist.

func (*PluginCache) IsValid

func (c *PluginCache) IsValid(category, name, currentHash string) bool

IsValid checks if a cached entry is still valid based on file hash. Returns false if the entry doesn't exist or the hash doesn't match.

func (*PluginCache) List

func (c *PluginCache) List(category string) []PluginMeta

List returns all cached metadata for a given category.

func (*PluginCache) Load

func (c *PluginCache) Load() error

Load reads the cache from disk.

func (*PluginCache) Save

func (c *PluginCache) Save() error

Save writes the cache to disk as JSON.

func (*PluginCache) Set

func (c *PluginCache) Set(category, name string, meta PluginMeta) error

Set stores plugin metadata in the cache.

type PluginMeta

type PluginMeta struct {
	Name        string        `json:"name"`
	Description string        `json:"description"`
	Active      bool          `json:"active"`
	FileHash    string        `json:"file_hash"`
	LoadTime    time.Duration `json:"load_time"`
	CachedAt    time.Time     `json:"cached_at"`
}

PluginMeta holds cached metadata about a plugin.

type Registry

type Registry[T any] struct {
	// contains filtered or unexported fields
}

Registry manages registered capabilities of a specific type. It is safe for concurrent use.

func New

func New[T any](name string) *Registry[T]

New creates a new registry with the given name.

func (*Registry[T]) Count

func (r *Registry[T]) Count() int

Count returns the number of registered capabilities.

func (*Registry[T]) Create

func (r *Registry[T]) Create(name string, cfg Config) (T, error)

Create instantiates a capability by name with the given config.

func (*Registry[T]) Get

func (r *Registry[T]) Get(name string) (func(Config) (T, error), bool)

Get retrieves a factory function by name.

func (*Registry[T]) Has

func (r *Registry[T]) Has(name string) bool

Has checks if a capability is registered.

func (*Registry[T]) List

func (r *Registry[T]) List() []string

List returns all registered capability names, sorted alphabetically.

func (*Registry[T]) Name

func (r *Registry[T]) Name() string

Name returns the registry name.

func (*Registry[T]) Register

func (r *Registry[T]) Register(name string, factory func(Config) (T, error))

Register adds a factory function for the given capability name. If a factory with the same name already exists, it is replaced.

type TypedFactory

type TypedFactory[C any, T any] func(C) (T, error)

TypedFactory is a generic factory function that creates components from typed configuration. This provides compile-time type safety.

Usage:

type OpenAIConfig struct { Model string; APIKey string }
var factory TypedFactory[OpenAIConfig, *OpenAI] = func(cfg OpenAIConfig) (*OpenAI, error) { ... }

Jump to

Keyboard shortcuts

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