187 lines
5.7 KiB
Go
187 lines
5.7 KiB
Go
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"
|
|
)
|
|
|
|
// 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 checkConfigSelection(selection ckconfig.Selection) error {
|
|
if selection.Config.Set && strings.TrimSpace(selection.Config.Value) == "" {
|
|
return fmt.Errorf("--config must not be empty")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (loaders *nativeLoaders) application(ctx context.Context, selection ckconfig.Selection) (*configkit.Result[config.Document], error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
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, err
|
|
}
|
|
invocationDirectory, err := os.Getwd()
|
|
if err != nil {
|
|
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
|
|
}
|
|
}
|
|
|
|
func writeConfigLoadError(output io.Writer, err error) {
|
|
var loadError *configkit.LoadError
|
|
if errors.As(err, &loadError) {
|
|
if writeErr := configkit.WriteDiagnostics(output, loadError.Diagnostics()); writeErr == nil {
|
|
// ConfigKit's locations contain source IDs. Keep its renderer and
|
|
// supply the acquired-source legend so imported/overlay errors can
|
|
// be traced to a file. Quoting also escapes terminal controls.
|
|
for _, source := range loadError.Sources() {
|
|
label := source.Path
|
|
if label == "" {
|
|
label = source.Label
|
|
}
|
|
if _, err := fmt.Fprintf(output, "source %q: %q\n", source.ID, label); err != nil {
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
}
|
|
fmt.Fprintf(output, "notarius: %v\n", err)
|
|
}
|