Construct engines from resolved application configuration
This commit is contained in:
112
engine.go
112
engine.go
@@ -5,10 +5,12 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit/internal/backend"
|
||||
"go.maximumdirect.net/promptkit/internal/configuration"
|
||||
"go.maximumdirect.net/promptkit/internal/domain"
|
||||
internalengine "go.maximumdirect.net/promptkit/internal/engine"
|
||||
"go.maximumdirect.net/promptkit/internal/observation"
|
||||
@@ -158,14 +160,15 @@ func (f optionFunc) apply(options *engineOptions) error {
|
||||
}
|
||||
|
||||
type engineOptions struct {
|
||||
internal internalengine.Options
|
||||
internal internalengine.Options
|
||||
disableBundledOpenRouter bool
|
||||
}
|
||||
|
||||
// WithBundledOpenRouter enables or disables the built-in OpenRouter underlay.
|
||||
// It is enabled by default; when repeated, the last option wins.
|
||||
func WithBundledOpenRouter(enabled bool) Option {
|
||||
return optionFunc(func(options *engineOptions) error {
|
||||
options.internal.SetBundledOpenRouter(enabled)
|
||||
options.disableBundledOpenRouter = !enabled
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -220,26 +223,121 @@ func WithArtifactReader(reader ArtifactReader) Option {
|
||||
// does not perform model requests or require credentials. A negative
|
||||
// Config.TransientRetries is invalid.
|
||||
func NewEngine(cfg Config, opts ...Option) (*Engine, error) {
|
||||
options, err := applyEngineOptions(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
runtime := RuntimeConfig{Timeout: cfg.Timeout, HTTPClient: cfg.HTTPClient, TransientRetries: cfg.TransientRetries}
|
||||
if _, err := domain.ResolveTransientRetries(2, runtime.TransientRetries); err != nil {
|
||||
return nil, newPublicOperationError(ErrInvalidConfig, err)
|
||||
}
|
||||
underlays := append([]configkit.Underlay(nil), cfg.Sources.Underlays...)
|
||||
if !options.disableBundledOpenRouter {
|
||||
underlays = append([]configkit.Underlay{BundledOpenRouterUnderlay()}, underlays...)
|
||||
}
|
||||
main := configkit.Input{Source: configkit.NoSource()}
|
||||
if cfg.Sources.Main != nil {
|
||||
main = *cfg.Sources.Main
|
||||
}
|
||||
definition := Configure(configkit.Definition[standaloneConfiguration]{}, func(value *standaloneConfiguration) *Settings { return &value.PromptKit })
|
||||
result, err := configkit.LoadLayered(main, configkit.LayeredOptions[standaloneConfiguration]{Underlays: underlays, Definition: definition})
|
||||
if err != nil {
|
||||
return nil, newPublicOperationError(ErrInvalidConfig, err)
|
||||
}
|
||||
return newFromConfig(result, runtime, options)
|
||||
}
|
||||
|
||||
type standaloneConfiguration struct {
|
||||
PromptKit Settings `yaml:"promptkit"`
|
||||
Application map[string]any `yaml:",inline"`
|
||||
}
|
||||
|
||||
// Configuration is the completed application-owned configuration evidence
|
||||
// needed to capture PromptKit resources without loading configuration again.
|
||||
type Configuration interface {
|
||||
Value(configkit.Path) (any, bool)
|
||||
InspectSubtree(configkit.Path) (configkit.Inspection, bool)
|
||||
Diagnostics() []configkit.Diagnostic
|
||||
}
|
||||
|
||||
// RuntimeConfig selects runtime-only client and retry settings for NewFromConfig.
|
||||
// Its fields have the same semantics as the corresponding Config fields.
|
||||
type RuntimeConfig struct {
|
||||
Timeout time.Duration
|
||||
HTTPClient *http.Client
|
||||
TransientRetries *int
|
||||
}
|
||||
|
||||
// NewFromConfig captures resources from an application-owned completed load.
|
||||
// The application must register PromptKit rules during that load. Callers must
|
||||
// not mutate its selected settings between loading and this handoff. After
|
||||
// construction, later settings mutations do not alter the engine. This method
|
||||
// does not load sources, rerun callbacks, or select bundled underlays.
|
||||
func NewFromConfig(result Configuration, cfg RuntimeConfig, opts ...Option) (*Engine, error) {
|
||||
options, err := applyEngineOptions(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newFromConfig(result, cfg, options)
|
||||
}
|
||||
|
||||
func applyEngineOptions(opts []Option) (engineOptions, error) {
|
||||
var options engineOptions
|
||||
for _, opt := range opts {
|
||||
if opt == nil {
|
||||
continue
|
||||
}
|
||||
if err := opt.apply(&options); err != nil {
|
||||
return nil, newPublicOperationError(ErrInvalidConfig, err)
|
||||
return engineOptions{}, newPublicOperationError(ErrInvalidConfig, err)
|
||||
}
|
||||
}
|
||||
return options, nil
|
||||
}
|
||||
|
||||
assembled, err := internalengine.New(
|
||||
internalengine.Config{Sources: internalengine.ConfigurationSources{Underlays: cfg.Sources.Underlays, Main: cfg.Sources.Main}, Timeout: cfg.Timeout, HTTPClient: cfg.HTTPClient, TransientRetries: copyIntPtr(cfg.TransientRetries)},
|
||||
options.internal,
|
||||
)
|
||||
func newFromConfig(result Configuration, cfg RuntimeConfig, options engineOptions) (*Engine, error) {
|
||||
if result == nil || isNilConfiguration(result) {
|
||||
return nil, newPublicOperationError(ErrInvalidConfig, errors.New("configuration result is nil"))
|
||||
}
|
||||
if _, err := domain.ResolveTransientRetries(2, cfg.TransientRetries); err != nil {
|
||||
return nil, newPublicOperationError(ErrInvalidConfig, err)
|
||||
}
|
||||
value, available := result.Value(configkit.Keys("promptkit"))
|
||||
var selected *Settings
|
||||
if available && value != nil {
|
||||
switch section := value.(type) {
|
||||
case Settings:
|
||||
selected = §ion
|
||||
case *Settings:
|
||||
selected = section
|
||||
default:
|
||||
return nil, newPublicOperationError(ErrInvalidConfig, fmt.Errorf("promptkit section has incompatible type %T", value))
|
||||
}
|
||||
}
|
||||
view, ok := result.InspectSubtree(configkit.Keys("promptkit"))
|
||||
if !ok {
|
||||
return nil, newPublicOperationError(ErrInvalidConfig, errors.New("configuration inspection is unavailable"))
|
||||
}
|
||||
snapshot, err := configuration.Capture(selected, view, result.Diagnostics())
|
||||
if err != nil {
|
||||
return nil, newPublicOperationError(ErrInvalidConfig, err)
|
||||
}
|
||||
assembled, err := internalengine.New(snapshot, internalengine.Config{Timeout: cfg.Timeout, HTTPClient: cfg.HTTPClient, TransientRetries: copyIntPtr(cfg.TransientRetries)}, options.internal)
|
||||
if err != nil {
|
||||
return nil, newPublicOperationError(ErrInvalidConfig, err)
|
||||
}
|
||||
return &Engine{internal: assembled}, nil
|
||||
}
|
||||
|
||||
func isNilConfiguration(result Configuration) bool {
|
||||
value := reflect.ValueOf(result)
|
||||
switch value.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
|
||||
return value.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// InspectPrompt resolves one prompt ID without selecting a profile or starting
|
||||
// execution work.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user