Share one native application configuration load and captured engine
This commit is contained in:
@@ -1,36 +1,159 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/assets"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
||||
"go.maximumdirect.net/ckconfig"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func loadConfigKit(configPath, overlay string, overrides []configkit.Override, opts Options) (*config.LoadedConfig, string, error) {
|
||||
var source configkit.Source
|
||||
if path := strings.TrimSpace(configPath); path != "" {
|
||||
source = configkit.File(path)
|
||||
} else if path, ok := opts.LookupEnv("NOTARIUS_CONFIG"); ok && strings.TrimSpace(path) != "" {
|
||||
source = configkit.File(strings.TrimSpace(path))
|
||||
} else {
|
||||
source = configkit.SearchConfig("notarius", configkit.Searches(
|
||||
configkit.WorkingDirectory(), configkit.UserConfigDirectory(), configkit.UnixSystemDirectories(),
|
||||
), configkit.SearchOptions{Required: true})
|
||||
// nativeLoaders belongs to one invocation. Construction performs no load,
|
||||
// engine construction, or run-storage allocation.
|
||||
type nativeLoaders struct {
|
||||
lookupEnv func(string) (string, bool)
|
||||
runtime promptkit.RuntimeConfig
|
||||
engineOptions []promptkit.Option
|
||||
selections []loadedSelection
|
||||
}
|
||||
|
||||
type loadedSelection struct {
|
||||
selection ckconfig.Selection
|
||||
result *configkit.Result[config.Document]
|
||||
engine *promptkit.Engine
|
||||
}
|
||||
|
||||
func (loaders *nativeLoaders) application(ctx context.Context, selection ckconfig.Selection) (*configkit.Result[config.Document], error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
workingDirectory, err := os.Getwd()
|
||||
for _, captured := range loaders.selections {
|
||||
if reflect.DeepEqual(captured.selection, selection) {
|
||||
return captured.result, nil
|
||||
}
|
||||
}
|
||||
source, err := loaders.mainSource(selection)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("determine invocation directory: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
loaded, err := config.LoadSource(source, config.LoadOptions{Overlay: overlay, Overrides: overrides, InvocationDirectory: workingDirectory})
|
||||
invocationDirectory, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
return nil, fmt.Errorf("determine invocation directory: %w", err)
|
||||
}
|
||||
options := configkit.LayeredOptions[config.Document]{
|
||||
Definition: config.Definition(invocationDirectory),
|
||||
Underlays: nativeUnderlays(),
|
||||
Overrides: selection.Overrides,
|
||||
}
|
||||
if selection.Overlay.Set {
|
||||
options.Overlay = selection.Overlay.Value
|
||||
}
|
||||
result, err := configkit.LoadLayered(configkit.Input{Source: source, Imports: true}, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
loaders.selections = append(loaders.selections, loadedSelection{selection: captureSelection(selection), result: result})
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (loaders *nativeLoaders) engine(ctx context.Context, selection ckconfig.Selection) (*promptkit.Engine, error) {
|
||||
result, err := loaders.application(ctx, selection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for index := range loaders.selections {
|
||||
captured := &loaders.selections[index]
|
||||
if captured.result != result {
|
||||
continue
|
||||
}
|
||||
if captured.engine == nil {
|
||||
captured.engine, err = promptkit.NewFromConfig(result, loaders.runtime, loaders.engineOptions...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return captured.engine, nil
|
||||
}
|
||||
return nil, fmt.Errorf("application selection was not retained")
|
||||
}
|
||||
|
||||
func (loaders *nativeLoaders) mainSource(selection ckconfig.Selection) (configkit.Source, error) {
|
||||
if selection.Config.Set {
|
||||
if strings.TrimSpace(selection.Config.Value) == "" {
|
||||
return configkit.Source{}, fmt.Errorf("--config must not be empty")
|
||||
}
|
||||
return configkit.File(strings.TrimSpace(selection.Config.Value)), nil
|
||||
}
|
||||
lookup := loaders.lookupEnv
|
||||
if lookup == nil {
|
||||
lookup = os.LookupEnv
|
||||
}
|
||||
if path, ok := lookup("NOTARIUS_CONFIG"); ok && strings.TrimSpace(path) != "" {
|
||||
return configkit.File(strings.TrimSpace(path)), nil
|
||||
}
|
||||
return configkit.SearchConfig("notarius", configkit.Searches(
|
||||
configkit.WorkingDirectory(), configkit.UserConfigDirectory(), configkit.UnixSystemDirectories(),
|
||||
), configkit.SearchOptions{Required: true}), nil
|
||||
}
|
||||
|
||||
func nativeUnderlays() []configkit.Underlay {
|
||||
filesystem := configkit.NewFilesystem("Notarius assets", assets.FS())
|
||||
// Both companion locations are imports of one native source in one strict
|
||||
// peer group. MainRelative uses the actual selected main source's directory.
|
||||
companions := promptkit.SettingsInput(configkit.Bytes("operator PromptKit companions", []byte(`config:
|
||||
imports:
|
||||
- path: prompts.yml
|
||||
required: false
|
||||
- dir: prompts
|
||||
pattern: ["*.yml", "*.yaml"]
|
||||
recursive: true
|
||||
required: false
|
||||
`)))
|
||||
companions.MainRelative = &configkit.MainRelativeContext{RequireBase: true}
|
||||
companions.Imports = true
|
||||
return []configkit.Underlay{
|
||||
promptkit.BundledOpenRouterUnderlay(),
|
||||
{Inputs: []configkit.Input{{
|
||||
Source: filesystem.File("notarius.yml"), Imports: true,
|
||||
Filesystem: &configkit.FilesystemContext{FS: &filesystem, Root: "."},
|
||||
}}},
|
||||
{Inputs: []configkit.Input{companions}},
|
||||
}
|
||||
}
|
||||
|
||||
func captureSelection(selection ckconfig.Selection) ckconfig.Selection {
|
||||
selection.Overrides = append([]configkit.Override(nil), selection.Overrides...)
|
||||
for index := range selection.Overrides {
|
||||
selection.Overrides[index].Value = captureOverrideValue(selection.Overrides[index].Value)
|
||||
}
|
||||
return selection
|
||||
}
|
||||
|
||||
func captureOverrideValue(value any) any {
|
||||
switch value := value.(type) {
|
||||
case map[string]any:
|
||||
copy := make(map[string]any, len(value))
|
||||
for key, item := range value {
|
||||
copy[key] = captureOverrideValue(item)
|
||||
}
|
||||
return copy
|
||||
case []any:
|
||||
copy := make([]any, len(value))
|
||||
for index, item := range value {
|
||||
copy[index] = captureOverrideValue(item)
|
||||
}
|
||||
return copy
|
||||
default:
|
||||
return value
|
||||
}
|
||||
return loaded, loaded.MainSourcePath(), nil
|
||||
}
|
||||
|
||||
func writeConfigLoadError(output io.Writer, err error) {
|
||||
|
||||
Reference in New Issue
Block a user